From 05766d5e96b63bfb9f7c98ac8b38bcad1c787a8b Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Thu, 7 Feb 2019 16:53:15 +0100 Subject: [PATCH] plugin: Parse and react to the result of the peer_connected hook Final step for the `peer_connected` hook, we parse the result and act accordingly. Currently we just close the underlying connection, but we may want to clean up peers that did not end up with a channel. Signed-off-by: Christian Decker --- lightningd/peer_control.c | 24 +++++++++++++++++++++++- wallet/test/run-wallet.c | 7 +++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 966f32296..8a29b7040 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -632,7 +632,23 @@ static struct peer_connected_hook_response * peer_connected_deserialize(const tal_t *ctx, const char *buffer, const jsmntok_t *toks) { - return NULL; + const jsmntok_t *resulttok; + struct peer_connected_hook_response *resp; + resulttok = json_get_member(buffer, toks, "result"); + if (!resulttok) { + return NULL; + } + + resp = tal(ctx, struct peer_connected_hook_response); + if (json_tok_streq(buffer, resulttok, "continue")) + resp->result = PEER_CONNECTED_CONTINUE; + else if (json_tok_streq(buffer, resulttok, "disconnect")) + resp->result = PEER_CONNECTED_DISCONNECT; + else + fatal("Plugin returned an invalid response to the connected " + "hook: %s", buffer); + + return resp; } static void @@ -648,6 +664,12 @@ peer_connected_hook_cb(struct peer_connected_hook_payload *payload, int peer_fd = payload->peer_fd; u8 *error; + if (response && response->result == PEER_CONNECTED_DISCONNECT) { + close(peer_fd); + tal_free(payload); + return; + } + if (channel) { log_debug(channel->log, "Peer has reconnected, state %s", channel_state_name(channel)); diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 596dedb73..d9cf39a90 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -257,6 +257,10 @@ void json_array_start(struct json_stream *js UNNEEDED, const char *fieldname UNN struct json_escaped *json_escaped_string_(const tal_t *ctx UNNEEDED, const void *bytes UNNEEDED, size_t len UNNEEDED) { fprintf(stderr, "json_escaped_string_ called!\n"); abort(); } +/* Generated stub for json_get_member */ +const jsmntok_t *json_get_member(const char *buffer UNNEEDED, const jsmntok_t tok[] UNNEEDED, + const char *label UNNEEDED) +{ fprintf(stderr, "json_get_member called!\n"); abort(); } /* Generated stub for json_object_end */ void json_object_end(struct json_stream *js UNNEEDED) { fprintf(stderr, "json_object_end called!\n"); abort(); } @@ -276,6 +280,9 @@ const char *json_tok_full(const char *buffer UNNEEDED, const jsmntok_t *t UNNEED /* Generated stub for json_tok_full_len */ int json_tok_full_len(const jsmntok_t *t UNNEEDED) { fprintf(stderr, "json_tok_full_len called!\n"); abort(); } +/* Generated stub for json_tok_streq */ +bool json_tok_streq(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, const char *str UNNEEDED) +{ fprintf(stderr, "json_tok_streq called!\n"); abort(); } /* Generated stub for json_to_pubkey */ bool json_to_pubkey(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, struct pubkey *pubkey UNNEEDED)