mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-18 21:35:11 +01:00
wireaddr: adds wireaddr_eq_without_port and wireaddr_cmp_type
Adds wireaddr_eq_without_port so it can be used later. Moves wireaddr_cmp_type from connectd.c to this file, so it can be reused later.
This commit is contained in:
parent
f1981461ef
commit
b930b8c548
@ -19,6 +19,13 @@ bool wireaddr_eq(const struct wireaddr *a, const struct wireaddr *b)
|
||||
return memeq(a->addr, a->addrlen, b->addr, b->addrlen);
|
||||
}
|
||||
|
||||
bool wireaddr_eq_without_port(const struct wireaddr *a, const struct wireaddr *b)
|
||||
{
|
||||
if (a->type != b->type)
|
||||
return false;
|
||||
return memeq(a->addr, a->addrlen, b->addr, b->addrlen);
|
||||
}
|
||||
|
||||
/* Returns false if we didn't parse it, and *cursor == NULL if malformed. */
|
||||
bool fromwire_wireaddr(const u8 **cursor, size_t *max, struct wireaddr *addr)
|
||||
{
|
||||
@ -874,3 +881,26 @@ bool all_tor_addresses(const struct wireaddr_internal *wireaddr)
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/*~ ccan/asort provides a type-safe sorting function; it requires a comparison
|
||||
* function, which takes an optional extra argument which is usually unused as
|
||||
* here, but deeply painful if you need it and don't have it! */
|
||||
int wireaddr_cmp_type(const struct wireaddr *a,
|
||||
const struct wireaddr *b, void *unused)
|
||||
{
|
||||
/* This works, but of course it's inefficient. We don't
|
||||
* really care, since it's called only once at startup. */
|
||||
u8 *a_wire = tal_arr(tmpctx, u8, 0), *b_wire = tal_arr(tmpctx, u8, 0);
|
||||
int cmp, minlen;
|
||||
|
||||
towire_wireaddr(&a_wire, a);
|
||||
towire_wireaddr(&b_wire, b);
|
||||
|
||||
minlen = tal_bytelen(a_wire) < tal_bytelen(b_wire)
|
||||
? tal_bytelen(a_wire) : tal_bytelen(b_wire);
|
||||
cmp = memcmp(a_wire, b_wire, minlen);
|
||||
/* On a tie, shorter one goes first. */
|
||||
if (cmp == 0)
|
||||
return tal_bytelen(a_wire) - tal_bytelen(b_wire);
|
||||
return cmp;
|
||||
}
|
||||
|
@ -69,6 +69,7 @@ struct wireaddr {
|
||||
};
|
||||
|
||||
bool wireaddr_eq(const struct wireaddr *a, const struct wireaddr *b);
|
||||
bool wireaddr_eq_without_port(const struct wireaddr *a, const struct wireaddr *b);
|
||||
|
||||
/* We use wireaddr to tell gossipd both what to listen on, and what to
|
||||
* announce */
|
||||
@ -197,4 +198,7 @@ bool all_tor_addresses(const struct wireaddr_internal *wireaddr);
|
||||
/* Decode an array of serialized addresses from node_announcement */
|
||||
struct wireaddr *fromwire_wireaddr_array(const tal_t *ctx, const u8 *ser);
|
||||
|
||||
int wireaddr_cmp_type(const struct wireaddr *a,
|
||||
const struct wireaddr *b, void *unused);
|
||||
|
||||
#endif /* LIGHTNING_COMMON_WIREADDR_H */
|
||||
|
@ -1186,29 +1186,6 @@ static void add_announceable(struct wireaddr **announceable,
|
||||
tal_arr_expand(announceable, *addr);
|
||||
}
|
||||
|
||||
/*~ ccan/asort provides a type-safe sorting function; it requires a comparison
|
||||
* function, which takes an optional extra argument which is usually unused as
|
||||
* here, but deeply painful if you need it and don't have it! */
|
||||
static int wireaddr_cmp_type(const struct wireaddr *a,
|
||||
const struct wireaddr *b, void *unused)
|
||||
{
|
||||
/* This works, but of course it's inefficient. We don't
|
||||
* really care, since it's called only once at startup. */
|
||||
u8 *a_wire = tal_arr(tmpctx, u8, 0), *b_wire = tal_arr(tmpctx, u8, 0);
|
||||
int cmp, minlen;
|
||||
|
||||
towire_wireaddr(&a_wire, a);
|
||||
towire_wireaddr(&b_wire, b);
|
||||
|
||||
minlen = tal_bytelen(a_wire) < tal_bytelen(b_wire)
|
||||
? tal_bytelen(a_wire) : tal_bytelen(b_wire);
|
||||
cmp = memcmp(a_wire, b_wire, minlen);
|
||||
/* On a tie, shorter one goes first. */
|
||||
if (cmp == 0)
|
||||
return tal_bytelen(a_wire) - tal_bytelen(b_wire);
|
||||
return cmp;
|
||||
}
|
||||
|
||||
/* We need to have a bound address we can tell Tor to connect to */
|
||||
static const struct wireaddr *
|
||||
find_local_address(const struct listen_fd **listen_fds)
|
||||
|
Loading…
Reference in New Issue
Block a user