diff --git a/common/wireaddr.c b/common/wireaddr.c index 8fd901784..2873fe764 100644 --- a/common/wireaddr.c +++ b/common/wireaddr.c @@ -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; +} diff --git a/common/wireaddr.h b/common/wireaddr.h index feb6bfe0b..74e02fc06 100644 --- a/common/wireaddr.h +++ b/common/wireaddr.h @@ -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 */ diff --git a/connectd/connectd.c b/connectd/connectd.c index c47e11b9e..f3c1e1605 100644 --- a/connectd/connectd.c +++ b/connectd/connectd.c @@ -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)