From e972b208c7574eb2f2783609a49088d59edf73c8 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Mon, 8 May 2017 23:22:59 +0200 Subject: [PATCH] routing: Passing back all addresses on getnodes --- daemon/json.c | 20 ++++++++++++++++++++ daemon/json.h | 4 ++++ lightningd/gossip/gossip.c | 1 + lightningd/gossip_control.c | 11 +++++------ lightningd/gossip_msg.c | 15 +++++++++++++++ lightningd/gossip_msg.h | 1 + 6 files changed, 46 insertions(+), 6 deletions(-) diff --git a/daemon/json.c b/daemon/json.c index b5c64a5a0..cfeec9d78 100644 --- a/daemon/json.c +++ b/daemon/json.c @@ -1,5 +1,6 @@ /* JSON core and helpers */ #include "json.h" +#include #include #include #include @@ -453,6 +454,25 @@ void json_add_short_channel_id(struct json_result *response, json_add_string(response, fieldname, str); } +void json_add_address(struct json_result *response, const char *fieldname, + const struct ipaddr *addr) +{ + json_object_start(response, fieldname); + char *addrstr = tal_arr(response, char, INET6_ADDRSTRLEN); + if (addr->type == ADDR_TYPE_IPV4) { + inet_ntop(AF_INET, addr->addr, addrstr, INET_ADDRSTRLEN); + json_add_string(response, "type", "ipv4"); + json_add_string(response, "address", addrstr); + json_add_num(response, "port", addr->port); + } else if (addr->type == ADDR_TYPE_IPV6) { + inet_ntop(AF_INET6, addr->addr, addrstr, INET6_ADDRSTRLEN); + json_add_string(response, "type", "ipv6"); + json_add_string(response, "address", addrstr); + json_add_num(response, "port", addr->port); + } + json_object_end(response); +} + void json_add_object(struct json_result *result, ...) { va_list ap; diff --git a/daemon/json.h b/daemon/json.h index 178c26856..b8371aff4 100644 --- a/daemon/json.h +++ b/daemon/json.h @@ -112,6 +112,10 @@ void json_add_short_channel_id(struct json_result *response, const char *fieldname, const struct short_channel_id *id); +/* JSON serialize a network address for a node */ +void json_add_address(struct json_result *response, const char *fieldname, + const struct ipaddr *addr); + void json_add_object(struct json_result *result, ...); const char *json_result_string(const struct json_result *result); diff --git a/lightningd/gossip/gossip.c b/lightningd/gossip/gossip.c index d60b9bdef..80407db33 100644 --- a/lightningd/gossip/gossip.c +++ b/lightningd/gossip/gossip.c @@ -572,6 +572,7 @@ static struct io_plan *getnodes(struct io_conn *conn, struct daemon *daemon) nodes[node_count].nodeid = n->id; nodes[node_count].hostname = n->hostname; nodes[node_count].port = n->port; + nodes[node_count].addresses = n->addresses; node_count++; n = node_map_next(daemon->rstate->nodes, &i); } diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index a7e7c4b7c..8c63da670 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -198,7 +198,7 @@ static bool json_getnodes_reply(struct subd *gossip, const u8 *reply, { struct gossip_getnodes_entry *nodes; struct json_result *response = new_json_result(cmd); - size_t i; + size_t i, j; if (!fromwire_gossip_getnodes_reply(reply, reply, NULL, &nodes)) { command_fail(cmd, "Malformed gossip_getnodes response"); @@ -211,12 +211,11 @@ static bool json_getnodes_reply(struct subd *gossip, const u8 *reply, for (i = 0; i < tal_count(nodes); i++) { json_object_start(response, NULL); json_add_pubkey(response, "nodeid", &nodes[i].nodeid); - if (tal_len(nodes[i].hostname) > 0) { - json_add_string(response, "hostname", nodes[i].hostname); - } else { - json_add_null(response, "hostname"); + json_array_start(response, "addresses"); + for (j=0; jnodeid); + numaddresses = fromwire_u8(pptr, max); + + entry->addresses = tal_arr(ctx, struct ipaddr, numaddresses); + for (i=0; iaddresses); + } + hostnamelen = fromwire_u8(pptr, max); entry->hostname = tal_arr(ctx, char, hostnamelen); fromwire_u8_array(pptr, max, (u8*)entry->hostname, hostnamelen); @@ -13,7 +21,14 @@ void fromwire_gossip_getnodes_entry(const tal_t *ctx, const u8 **pptr, size_t *m void towire_gossip_getnodes_entry(u8 **pptr, const struct gossip_getnodes_entry *entry) { u8 hostnamelen; + u8 i, numaddresses = tal_count(entry->addresses); towire_pubkey(pptr, &entry->nodeid); + towire_u8(pptr, numaddresses); + + for (i=0; iaddresses[i]); + } + if (entry->hostname) { hostnamelen = strlen(entry->hostname); towire_u8(pptr, hostnamelen); diff --git a/lightningd/gossip_msg.h b/lightningd/gossip_msg.h index a4133dbcb..3bf10d277 100644 --- a/lightningd/gossip_msg.h +++ b/lightningd/gossip_msg.h @@ -6,6 +6,7 @@ struct gossip_getnodes_entry { struct pubkey nodeid; + struct ipaddr *addresses; char *hostname; u16 port; };