From 08ccb4b6f065b7bfcac3410a346b959109fd693c Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 22 Jan 2016 06:41:48 +1030 Subject: [PATCH] getpeers: new command. Signed-off-by: Rusty Russell --- daemon/cryptopkt.c | 5 ++--- daemon/jsonrpc.c | 1 + daemon/jsonrpc.h | 1 + daemon/peer.c | 33 ++++++++++++++++++++++++++++++++- daemon/peer.h | 3 +++ 5 files changed, 39 insertions(+), 4 deletions(-) diff --git a/daemon/cryptopkt.c b/daemon/cryptopkt.c index ac2c1f4f6..45bf7f138 100644 --- a/daemon/cryptopkt.c +++ b/daemon/cryptopkt.c @@ -361,7 +361,6 @@ static struct io_plan *check_proof(struct io_conn *conn, struct peer *peer) struct sha256_double sha; struct signature sig; struct io_plan *(*cb)(struct io_conn *, struct peer *); - struct pubkey id; Authenticate *auth; auth = pkt_unwrap(peer, PKT__PKT_AUTH); @@ -373,7 +372,7 @@ static struct io_plan *check_proof(struct io_conn *conn, struct peer *peer) return io_close(conn); } - if (!proto_to_pubkey(peer->state->secpctx, auth->node_id, &id)) { + if (!proto_to_pubkey(peer->state->secpctx, auth->node_id, &peer->id)) { log_unusual(peer->log, "Invalid auth id"); return io_close(conn); } @@ -382,7 +381,7 @@ static struct io_plan *check_proof(struct io_conn *conn, struct peer *peer) sha256_double(&sha, neg->our_sessionpubkey, sizeof(neg->our_sessionpubkey)); - if (!check_signed_hash(peer->state->secpctx, &sha, &sig, &id)) { + if (!check_signed_hash(peer->state->secpctx, &sha, &sig, &peer->id)) { log_unusual(peer->log, "Bad auth signature"); return io_close(conn); } diff --git a/daemon/jsonrpc.c b/daemon/jsonrpc.c index 7457a766e..94e9ffddd 100644 --- a/daemon/jsonrpc.c +++ b/daemon/jsonrpc.c @@ -196,6 +196,7 @@ static const struct json_command *cmdlist[] = { &stop_command, &getlog_command, &connect_command, + &getpeers_command, /* Developer/debugging options. */ &echo_command, }; diff --git a/daemon/jsonrpc.h b/daemon/jsonrpc.h index d2aa2c11c..f804fd146 100644 --- a/daemon/jsonrpc.h +++ b/daemon/jsonrpc.h @@ -57,5 +57,6 @@ void setup_jsonrpc(struct lightningd_state *state, const char *rpc_filename); /* Commands (from other files) */ extern const struct json_command connect_command; +extern const struct json_command getpeers_command; #endif /* LIGHTNING_DAEMON_JSONRPC_H */ diff --git a/daemon/peer.c b/daemon/peer.c index 4904a981d..bf9c1910e 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -5,6 +5,7 @@ #include "log.h" #include "peer.h" #include +#include #include #include #include @@ -30,7 +31,9 @@ static struct io_plan *peer_test_check(struct io_conn *conn, struct peer *peer) || strcmp(peer->inpkt->error->problem, "hello") != 0) fatal("Bad packet '%.6s'", peer->inpkt->error->problem); log_info(peer->log, "Successful hello!"); - return io_close(conn); + + /* Sleep forever... */ + return io_wait(conn, peer, io_close_cb, NULL); } static struct io_plan *peer_test_read(struct io_conn *conn, struct peer *peer) @@ -245,3 +248,31 @@ const struct json_command connect_command = { "Connect to a {host} at {port}", "Returns an empty result on success" }; + +/* FIXME: Somehow we should show running DNS lookups! */ +/* FIXME: Show status of peers! */ +static void json_getpeers(struct command *cmd, + const char *buffer, const jsmntok_t *params) +{ + struct peer *p; + struct json_result *response = new_json_result(cmd); + + json_object_start(response, NULL); + json_array_start(response, "peers"); + list_for_each(&cmd->state->peers, p, list) { + json_object_start(response, NULL); + json_add_string(response, "name", log_prefix(p->log)); + json_add_hex(response, "id", p->id.der, pubkey_derlen(&p->id)); + json_object_end(response); + } + json_array_end(response); + json_object_end(response); + command_success(cmd, response); +} + +const struct json_command getpeers_command = { + "getpeers", + json_getpeers, + "List the current peers", + "Returns a 'peers' array" +}; diff --git a/daemon/peer.h b/daemon/peer.h index 705b7ba0e..ea0725b7f 100644 --- a/daemon/peer.h +++ b/daemon/peer.h @@ -15,6 +15,9 @@ struct peer { /* The other end's address. */ struct netaddr addr; + /* Their ID. */ + struct pubkey id; + /* Current received packet. */ Pkt *inpkt;