routing: Reading multiple addresses from node_announcements

This commit is contained in:
Christian Decker
2017-05-08 21:26:46 +02:00
committed by Rusty Russell
parent ed9668339d
commit 26892e79bb
4 changed files with 38 additions and 1 deletions

View File

@@ -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);

View File

@@ -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;

View File

@@ -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,

View File

@@ -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;