diff --git a/daemon/routing.c b/daemon/routing.c index f1d90ea8d..380de8898 100644 --- a/daemon/routing.c +++ b/daemon/routing.c @@ -79,6 +79,7 @@ struct node *new_node(struct routing_state *rstate, n->hostname = NULL; n->node_announcement = NULL; n->last_timestamp = 0; + n->addresses = tal_arr(n, struct ipaddr, 0); node_map_add(rstate->nodes, n); tal_add_destructor(n, destroy_node); @@ -836,6 +837,25 @@ void handle_channel_update(struct routing_state *rstate, const u8 *update, size_ tal_free(tmpctx); } +static struct ipaddr *read_addresses(const tal_t *ctx, u8 *ser) +{ + const u8 *cursor = ser; + size_t max = tal_len(ser); + struct ipaddr *ipaddrs = tal_arr(ctx, struct ipaddr, 0); + int numaddrs = 0; + while (cursor < ser + max) { + numaddrs += 1; + tal_resize(&ipaddrs, numaddrs); + fromwire_ipaddr(&cursor, &max, &ipaddrs[numaddrs-1]); + if (cursor == NULL) { + /* Parsing address failed */ + tal_free(ipaddrs); + return NULL; + } + } + return ipaddrs; +} + void handle_node_announcement( struct routing_state *rstate, const u8 *node_ann, size_t len) { @@ -849,6 +869,7 @@ void handle_node_announcement( u8 alias[32]; u8 *features, *addresses; const tal_t *tmpctx = tal_tmpctx(rstate); + struct ipaddr *ipaddrs; serialized = tal_dup_arr(tmpctx, u8, node_ann, len, 0); if (!fromwire_node_announcement(tmpctx, serialized, NULL, @@ -885,13 +906,24 @@ void handle_node_announcement( return; } + ipaddrs = read_addresses(tmpctx, addresses); + if (!ipaddrs) { + log_debug(rstate->base_log, "Unable to parse addresses."); + tal_free(serialized); + return; + } + tal_free(node->addresses); + node->addresses = tal_steal(node, ipaddrs); + node->last_timestamp = timestamp; node->hostname = tal_free(node->hostname); + if (!read_ip(node, addresses, &node->hostname, &node->port)) { /* FIXME: SHOULD fail connection here. */ tal_free(serialized); return; } + memcpy(node->rgb_color, rgb_color, 3); u8 *tag = tal_arr(tmpctx, u8, 0); diff --git a/daemon/routing.h b/daemon/routing.h index 40e86e17b..9732611a5 100644 --- a/daemon/routing.h +++ b/daemon/routing.h @@ -45,6 +45,7 @@ struct node { struct pubkey id; /* IP/Hostname and port of this node (may be NULL) */ + struct ipaddr *addresses; char *hostname; int port; diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index cc9bb2edd..e2f49615e 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -1259,12 +1259,15 @@ static u8 *create_node_announcement(const tal_t *ctx, struct lightningd *ld, u8 rgb[3] = {0x77, 0x88, 0x99}; u8 alias[32]; u8 *features = NULL; - u8 *addresses = NULL; + u8 *addresses = tal_arr(ctx, u8, 0); u8 *announcement; if (!sig) { sig = tal(ctx, secp256k1_ecdsa_signature); memset(sig, 0, sizeof(*sig)); } + if (ld->dstate.config.ipaddr.type != ADDR_TYPE_PADDING) { + towire_ipaddr(&addresses, &ld->dstate.config.ipaddr); + } memset(alias, 0, sizeof(alias)); announcement = towire_node_announcement(ctx, sig, timestamp, &ld->dstate.id, rgb, diff --git a/wire/fromwire.c b/wire/fromwire.c index 4dd16b9ca..9781d40be 100644 --- a/wire/fromwire.c +++ b/wire/fromwire.c @@ -173,6 +173,7 @@ void fromwire_ipaddr(const u8 **cursor, size_t *max, struct ipaddr *addr) } addr->type = **cursor; + *cursor += 1; switch (addr->type) { case 1: addr->addrlen = 4;