From 221a96cdeb28299a38d7f59dbc13ba3ea566b6b0 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 13 Sep 2016 03:37:07 +0930 Subject: [PATCH] getinfo: new RPC command Useful for getting ID, what port (if not set in config file). Signed-off-by: Rusty Russell --- daemon/jsonrpc.c | 22 ++++++++++++++++++++++ daemon/lightningd.c | 1 + daemon/lightningd.h | 3 +++ daemon/peer.c | 9 ++++----- 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/daemon/jsonrpc.c b/daemon/jsonrpc.c index 7408fc309..4902ae3db 100644 --- a/daemon/jsonrpc.c +++ b/daemon/jsonrpc.c @@ -255,6 +255,28 @@ static const struct json_command dev_restart_command = { "Simple restart test for developers" }; +static void json_getinfo(struct command *cmd, + const char *buffer, const jsmntok_t *params) +{ + struct json_result *response = new_json_result(cmd); + + json_object_start(response, NULL); + json_add_pubkey(response, cmd->dstate->secpctx, "id", &cmd->dstate->id); + /* FIXME: Keep netaddrs and list them all. */ + if (cmd->dstate->portnum) + json_add_num(response, "port", cmd->dstate->portnum); + json_add_bool(response, "testnet", cmd->dstate->config.testnet); + json_object_end(response); + command_success(cmd, response); +} + +static const struct json_command getinfo_command = { + "getinfo", + json_getinfo, + "Get general information about this node", + "Returns {id}, {port}, {testnet}, etc." +}; + static const struct json_command *cmdlist[] = { &help_command, &stop_command, diff --git a/daemon/lightningd.c b/daemon/lightningd.c index fef17f629..c0635e530 100644 --- a/daemon/lightningd.c +++ b/daemon/lightningd.c @@ -254,6 +254,7 @@ static struct lightningd_state *lightningd_state(void) list_head_init(&dstate->peers); list_head_init(&dstate->pay_commands); + dstate->portnum = 0; timers_init(&dstate->timers, controlled_time()); txwatch_hash_init(&dstate->txwatches); txowatch_hash_init(&dstate->txowatches); diff --git a/daemon/lightningd.h b/daemon/lightningd.h index dc8f8d162..f8d1e479f 100644 --- a/daemon/lightningd.h +++ b/daemon/lightningd.h @@ -72,6 +72,9 @@ struct lightningd_state { char *config_dir; char *rpc_filename; + /* Port we're listening on */ + u16 portnum; + /* Configuration settings. */ struct config config; diff --git a/daemon/peer.c b/daemon/peer.c index 0cde78326..8cee96647 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -2745,7 +2745,6 @@ void setup_listeners(struct lightningd_state *dstate, unsigned int portnum) struct sockaddr_in6 addr6; socklen_t len; int fd1, fd2; - u16 listen_port; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; @@ -2771,10 +2770,10 @@ void setup_listeners(struct lightningd_state *dstate, unsigned int portnum) close_noerr(fd1); } else { addr.sin_port = in6.sin6_port; - listen_port = ntohs(addr.sin_port); + dstate->portnum = ntohs(addr.sin_port); log_info(dstate->base_log, "Creating IPv6 listener on port %u", - listen_port); + dstate->portnum); io_new_listener(dstate, fd1, peer_connected_in, dstate); } } @@ -2790,10 +2789,10 @@ void setup_listeners(struct lightningd_state *dstate, unsigned int portnum) strerror(errno)); close_noerr(fd2); } else { - listen_port = ntohs(addr.sin_port); + dstate->portnum = ntohs(addr.sin_port); log_info(dstate->base_log, "Creating IPv4 listener on port %u", - listen_port); + dstate->portnum); io_new_listener(dstate, fd2, peer_connected_in, dstate); } }