Skip to content

Commit

Permalink
wireaddr: adds wireaddr_eq_without_port and wireaddr_cmp_type
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
m-schmoock authored and rustyrussell committed Mar 11, 2022
1 parent f198146 commit b930b8c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
30 changes: 30 additions & 0 deletions common/wireaddr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}
4 changes: 4 additions & 0 deletions common/wireaddr.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
23 changes: 0 additions & 23 deletions connectd/connectd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b930b8c

Please sign in to comment.