Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gossipd: use remote addr #5052

Merged
merged 10 commits into from
Mar 11, 2022
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 */
63 changes: 20 additions & 43 deletions connectd/connectd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1177,36 +1177,13 @@ static bool public_address(struct daemon *daemon, struct wireaddr *wireaddr)
return address_routable(wireaddr, daemon->dev_allow_localhost);
}

static void add_announcable(struct wireaddr **announcable,
const struct wireaddr *addr)
static void add_announceable(struct wireaddr **announceable,
const struct wireaddr *addr)
{
/*~ utils.h contains a convenience macro tal_arr_expand which
* reallocates a tal_arr to make it one longer, then returns a pointer
* to the (new) last element. */
tal_arr_expand(announcable, *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;
tal_arr_expand(announceable, *addr);
}

/* We need to have a bound address we can tell Tor to connect to */
Expand Down Expand Up @@ -1238,7 +1215,7 @@ static bool want_tor(const struct wireaddr_internal *proposed_wireaddr)
* announce, ones we announce but don't bind to, and ones we bind to and
* announce if they seem to be public addresses.
*
* This routine sorts out the mess: it populates the *announcable array,
* This routine sorts out the mess: it populates the *announceable array,
* and returns the addresses we bound to (by convention, return is allocated
* off `ctx` argument).
*
Expand All @@ -1254,7 +1231,7 @@ setup_listeners(const tal_t *ctx,
/* For each one, listen, announce or both */
const enum addr_listen_announce *proposed_listen_announce,
const char *tor_password,
struct wireaddr **announcable,
struct wireaddr **announceable,
char **errstr)
{
struct sockaddr_un addrun;
Expand All @@ -1267,7 +1244,7 @@ setup_listeners(const tal_t *ctx,

/* Start with empty arrays, for tal_arr_expand() */
listen_fds = tal_arr(ctx, const struct listen_fd *, 0);
*announcable = tal_arr(ctx, struct wireaddr, 0);
*announceable = tal_arr(ctx, struct wireaddr, 0);

/* Add addresses we've explicitly been told to *first*: implicit
* addresses will be discarded then if we have multiple. */
Expand All @@ -1282,7 +1259,7 @@ setup_listeners(const tal_t *ctx,
/* You can only announce wiretypes, not internal formats! */
assert(proposed_wireaddr[i].itype
== ADDR_INTERNAL_WIREADDR);
add_announcable(announcable, &wa.u.wireaddr);
add_announceable(announceable, &wa.u.wireaddr);
}

/* Now look for listening addresses. */
Expand Down Expand Up @@ -1338,8 +1315,8 @@ setup_listeners(const tal_t *ctx,
tal_steal(listen_fds, lfd));
if (announce
&& public_address(daemon, &wa.u.wireaddr))
add_announcable(announcable,
&wa.u.wireaddr);
add_announceable(announceable,
&wa.u.wireaddr);
}
ipv6_ok = (lfd != NULL);

Expand All @@ -1356,7 +1333,7 @@ setup_listeners(const tal_t *ctx,
tal_steal(listen_fds, lfd));
if (announce
&& public_address(daemon, &wa.u.wireaddr))
add_announcable(announcable,
add_announceable(announceable,
&wa.u.wireaddr);
} else if (!ipv6_ok) {
/* Both failed, return now, errstr set. */
Expand All @@ -1372,7 +1349,7 @@ setup_listeners(const tal_t *ctx,
return NULL;
tal_arr_expand(&listen_fds, tal_steal(listen_fds, lfd));
if (announce && public_address(daemon, &wa.u.wireaddr))
add_announcable(announcable, &wa.u.wireaddr);
add_announceable(announceable, &wa.u.wireaddr);
continue;
case ADDR_INTERNAL_FORPROXY:
break;
Expand Down Expand Up @@ -1425,10 +1402,10 @@ setup_listeners(const tal_t *ctx,
* there is also at least one address of
* different type.
*/
if (tal_count(*announcable) != 0) {
if (tal_count(*announceable) != 0) {
wireaddr_from_websocket(&addr.u.wireaddr,
daemon->websocket_port);
add_announcable(announcable,
add_announceable(announceable,
&addr.u.wireaddr);
} else {
status_unusual("Bound to websocket %s,"
Expand Down Expand Up @@ -1469,7 +1446,7 @@ setup_listeners(const tal_t *ctx,
if (!(proposed_listen_announce[i] & ADDR_ANNOUNCE)) {
continue;
};
add_announcable(announcable, toraddr);
add_announceable(announceable, toraddr);
}

/* Now we have bindings, set up any Tor static addresses: we will point
Expand Down Expand Up @@ -1516,7 +1493,7 @@ setup_listeners(const tal_t *ctx,
if (!(proposed_listen_announce[i] & ADDR_ANNOUNCE)) {
continue;
};
add_announcable(announcable, toraddr);
add_announceable(announceable, toraddr);
}

/*~ The spec used to ban more than one address of each type, but
Expand All @@ -1527,7 +1504,7 @@ setup_listeners(const tal_t *ctx,
*...
* - MUST place address descriptors in ascending order.
*/
asort(*announcable, tal_count(*announcable), wireaddr_cmp_type, NULL);
asort(*announceable, tal_count(*announceable), wireaddr_cmp_type, NULL);

*errstr = NULL;
return listen_fds;
Expand All @@ -1543,7 +1520,7 @@ static void connect_init(struct daemon *daemon, const u8 *msg)
struct wireaddr_internal *binding;
struct wireaddr_internal *proposed_wireaddr;
enum addr_listen_announce *proposed_listen_announce;
struct wireaddr *announcable;
struct wireaddr *announceable;
char *tor_password;
bool dev_fast_gossip;
bool dev_disconnect;
Expand Down Expand Up @@ -1602,7 +1579,7 @@ static void connect_init(struct daemon *daemon, const u8 *msg)
proposed_wireaddr,
proposed_listen_announce,
tor_password,
&announcable,
&announceable,
&errstr);

/* Free up old allocations */
Expand All @@ -1623,13 +1600,13 @@ static void connect_init(struct daemon *daemon, const u8 *msg)
daemon_conn_send(daemon->master,
take(towire_connectd_init_reply(NULL,
binding,
announcable,
announceable,
errstr)));
/*~ Who cares about a little once-off memory leak? Turns out we do!
* We have a memory leak checker which scans for allocated memory
* with no pointers to it (a tell-tale leak sign, though with tal it's
* not always a real problem), and this would (did!) trigger it. */
tal_free(announcable);
tal_free(announceable);

#if DEVELOPER
if (dev_disconnect)
Expand Down
6 changes: 3 additions & 3 deletions connectd/connectd_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ msgdata,connectd_init,dev_disconnect,bool,
msgtype,connectd_init_reply,2100
msgdata,connectd_init_reply,num_bindings,u16,
msgdata,connectd_init_reply,bindings,wireaddr_internal,num_bindings
msgdata,connectd_init_reply,num_announcable,u16,
msgdata,connectd_init_reply,announcable,wireaddr,num_announcable
msgdata,connectd_init_reply,num_announceable,u16,
msgdata,connectd_init_reply,announceable,wireaddr,num_announceable
msgdata,connectd_init_reply,failmsg,?wirestring,

# Activate the connect daemon, so others can connect.
Expand Down Expand Up @@ -75,7 +75,7 @@ msgdata,connectd_peer_connected,features,u8,flen
msgtype,connectd_peer_disconnected,2015
msgdata,connectd_peer_disconnected,id,node_id,

# master -> connectd: give message to peer and disconnect.
# master -> connectd: give message to peer and disconnect.
msgtype,connectd_peer_final_msg,2003
msgdata,connectd_peer_final_msg,id,node_id,
msgdata,connectd_peer_final_msg,len,u16,
Expand Down
13 changes: 8 additions & 5 deletions connectd/peer_exchange_initmsg.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ static struct io_plan *peer_init_received(struct io_conn *conn,

/* fetch optional tlv `remote_addr` */
remote_addr = NULL;
#if EXPERIMENTAL_FEATURES /* BOLT1 remote_addr #917 */

/* BOLT-remote-address #1:
* The receiving node:
* ...
Expand All @@ -100,7 +100,12 @@ static struct io_plan *peer_init_received(struct io_conn *conn,
switch (tlvs->remote_addr->type) {
case ADDR_TYPE_IPV4:
case ADDR_TYPE_IPV6:
remote_addr = tal_steal(peer, tlvs->remote_addr);
#if DEVELOPER /* ignore private addresses (non-DEVELOPER builds) */
if (address_routable(tlvs->remote_addr, true))
#else
if (address_routable(tlvs->remote_addr, false))
#endif /* DEVELOPER */
remote_addr = tal_steal(peer, tlvs->remote_addr);
break;
/* We are only interested in IP addresses */
case ADDR_TYPE_TOR_V2_REMOVED:
Expand All @@ -110,7 +115,6 @@ static struct io_plan *peer_init_received(struct io_conn *conn,
break;
}
}
#endif

/* The globalfeatures field is now unused, but there was a
* window where it was: combine the two. */
Expand Down Expand Up @@ -212,7 +216,7 @@ struct io_plan *peer_exchange_initmsg(struct io_conn *conn,

/* set optional tlv `remote_addr` on incoming IP connections */
tlvs->remote_addr = NULL;
#if EXPERIMENTAL_FEATURES /* BOLT1 remote_addr #917 */

/* BOLT-remote-address #1:
* The sending node:
* ...
Expand All @@ -236,7 +240,6 @@ struct io_plan *peer_exchange_initmsg(struct io_conn *conn,
break;
}
}
#endif

/* Initially, there were two sets of feature bits: global and local.
* Local affected peer nodes only, global affected everyone. Both were
Expand Down
Loading