Browse Source

routing: Passing back all addresses on getnodes

ppa-0.6.1
Christian Decker 8 years ago
committed by Rusty Russell
parent
commit
e972b208c7
  1. 20
      daemon/json.c
  2. 4
      daemon/json.h
  3. 1
      lightningd/gossip/gossip.c
  4. 11
      lightningd/gossip_control.c
  5. 15
      lightningd/gossip_msg.c
  6. 1
      lightningd/gossip_msg.h

20
daemon/json.c

@ -1,5 +1,6 @@
/* JSON core and helpers */ /* JSON core and helpers */
#include "json.h" #include "json.h"
#include <arpa/inet.h>
#include <assert.h> #include <assert.h>
#include <ccan/build_assert/build_assert.h> #include <ccan/build_assert/build_assert.h>
#include <ccan/str/hex/hex.h> #include <ccan/str/hex/hex.h>
@ -453,6 +454,25 @@ void json_add_short_channel_id(struct json_result *response,
json_add_string(response, fieldname, str); 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, ...) void json_add_object(struct json_result *result, ...)
{ {
va_list ap; va_list ap;

4
daemon/json.h

@ -112,6 +112,10 @@ void json_add_short_channel_id(struct json_result *response,
const char *fieldname, const char *fieldname,
const struct short_channel_id *id); 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, ...); void json_add_object(struct json_result *result, ...);
const char *json_result_string(const struct json_result *result); const char *json_result_string(const struct json_result *result);

1
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].nodeid = n->id;
nodes[node_count].hostname = n->hostname; nodes[node_count].hostname = n->hostname;
nodes[node_count].port = n->port; nodes[node_count].port = n->port;
nodes[node_count].addresses = n->addresses;
node_count++; node_count++;
n = node_map_next(daemon->rstate->nodes, &i); n = node_map_next(daemon->rstate->nodes, &i);
} }

11
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 gossip_getnodes_entry *nodes;
struct json_result *response = new_json_result(cmd); struct json_result *response = new_json_result(cmd);
size_t i; size_t i, j;
if (!fromwire_gossip_getnodes_reply(reply, reply, NULL, &nodes)) { if (!fromwire_gossip_getnodes_reply(reply, reply, NULL, &nodes)) {
command_fail(cmd, "Malformed gossip_getnodes response"); 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++) { for (i = 0; i < tal_count(nodes); i++) {
json_object_start(response, NULL); json_object_start(response, NULL);
json_add_pubkey(response, "nodeid", &nodes[i].nodeid); json_add_pubkey(response, "nodeid", &nodes[i].nodeid);
if (tal_len(nodes[i].hostname) > 0) { json_array_start(response, "addresses");
json_add_string(response, "hostname", nodes[i].hostname); for (j=0; j<tal_count(nodes[i].addresses); j++) {
} else { json_add_address(response, NULL, &nodes[i].addresses[j]);
json_add_null(response, "hostname");
} }
json_add_num(response, "port", nodes[i].port); json_array_end(response);
json_object_end(response); json_object_end(response);
} }
json_array_end(response); json_array_end(response);

15
lightningd/gossip_msg.c

@ -4,7 +4,15 @@
void fromwire_gossip_getnodes_entry(const tal_t *ctx, const u8 **pptr, size_t *max, struct gossip_getnodes_entry *entry) void fromwire_gossip_getnodes_entry(const tal_t *ctx, const u8 **pptr, size_t *max, struct gossip_getnodes_entry *entry)
{ {
u8 hostnamelen; u8 hostnamelen;
u8 numaddresses, i;
fromwire_pubkey(pptr, max, &entry->nodeid); fromwire_pubkey(pptr, max, &entry->nodeid);
numaddresses = fromwire_u8(pptr, max);
entry->addresses = tal_arr(ctx, struct ipaddr, numaddresses);
for (i=0; i<numaddresses; i++) {
fromwire_ipaddr(pptr, max, entry->addresses);
}
hostnamelen = fromwire_u8(pptr, max); hostnamelen = fromwire_u8(pptr, max);
entry->hostname = tal_arr(ctx, char, hostnamelen); entry->hostname = tal_arr(ctx, char, hostnamelen);
fromwire_u8_array(pptr, max, (u8*)entry->hostname, 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) void towire_gossip_getnodes_entry(u8 **pptr, const struct gossip_getnodes_entry *entry)
{ {
u8 hostnamelen; u8 hostnamelen;
u8 i, numaddresses = tal_count(entry->addresses);
towire_pubkey(pptr, &entry->nodeid); towire_pubkey(pptr, &entry->nodeid);
towire_u8(pptr, numaddresses);
for (i=0; i<numaddresses; i++) {
towire_ipaddr(pptr, &entry->addresses[i]);
}
if (entry->hostname) { if (entry->hostname) {
hostnamelen = strlen(entry->hostname); hostnamelen = strlen(entry->hostname);
towire_u8(pptr, hostnamelen); towire_u8(pptr, hostnamelen);

1
lightningd/gossip_msg.h

@ -6,6 +6,7 @@
struct gossip_getnodes_entry { struct gossip_getnodes_entry {
struct pubkey nodeid; struct pubkey nodeid;
struct ipaddr *addresses;
char *hostname; char *hostname;
u16 port; u16 port;
}; };

Loading…
Cancel
Save