From 594eb8109cd956be13aae4b9413e9d1d20544be0 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Fri, 30 Sep 2016 15:30:19 +0200 Subject: [PATCH] jsonrpc: Added 'getnodes' to list known nodes. getnodes returns an object containing a single array of 'nodes'. Each element contains the node's ID, its hostname and its port. If unknown (because we haven't seen a node announcement yet) then the port is 0 and the hostname is null. --- daemon/jsonrpc.c | 1 + daemon/jsonrpc.h | 1 + daemon/routing.c | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/daemon/jsonrpc.c b/daemon/jsonrpc.c index 6a1369b22..04decb9d8 100644 --- a/daemon/jsonrpc.c +++ b/daemon/jsonrpc.c @@ -285,6 +285,7 @@ static const struct json_command *cmdlist[] = { &getlog_command, &connect_command, &getpeers_command, + &getnodes_command, &gethtlcs_command, &close_command, &newaddr_command, diff --git a/daemon/jsonrpc.h b/daemon/jsonrpc.h index 4c5ad6159..72b4dbebc 100644 --- a/daemon/jsonrpc.h +++ b/daemon/jsonrpc.h @@ -61,6 +61,7 @@ extern const struct json_command newaddr_command; extern const struct json_command connect_command; extern const struct json_command close_command; extern const struct json_command getpeers_command; +extern const struct json_command getnodes_command; /* Invoice management. */ extern const struct json_command invoice_command; diff --git a/daemon/routing.c b/daemon/routing.c index dec8dc7fe..e74724dc9 100644 --- a/daemon/routing.c +++ b/daemon/routing.c @@ -64,6 +64,7 @@ struct node *new_node(struct lightningd_state *dstate, n->id = *id; n->in = tal_arr(n, struct node_connection *, 0); n->out = tal_arr(n, struct node_connection *, 0); + n->port = 0; node_map_add(dstate->nodes, n); tal_add_destructor(n, destroy_node); @@ -511,4 +512,40 @@ const struct json_command dev_routefail_command = { "Returns an empty result on success" }; +static void json_getnodes(struct command *cmd, + const char *buffer, const jsmntok_t *params) +{ + struct json_result *response = new_json_result(cmd); + struct node *n; + struct node_map_iter i; + + n = node_map_first(cmd->dstate->nodes, &i); + + json_object_start(response, NULL); + json_array_start(response, "nodes"); + + while (n != NULL) { + json_object_start(response, NULL); + json_add_pubkey(response, cmd->dstate->secpctx, + "nodeid", &n->id); + json_add_num(response, "port", n->port); + if (!n->port) + json_add_null(response, "hostname"); + else + json_add_string(response, "hostname", n->hostname); + json_object_end(response); + n = node_map_next(cmd->dstate->nodes, &i); + } + + json_array_end(response); + json_object_end(response); + command_success(cmd, response); +} + +const struct json_command getnodes_command = { + "getnodes", + json_getnodes, + "List all known nodes in the network.", + "Returns a 'nodes' array" +};