From 7f7ad4f89f4edb9c068bda72481a2172b9102b7b Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 16 Apr 2019 09:41:48 +0930 Subject: [PATCH] connected_hook: allow hook to specify an error message. Signed-off-by: Rusty Russell --- CHANGELOG.md | 1 + doc/PLUGINS.md | 6 ++++++ lightningd/peer_control.c | 9 +++++++++ tests/plugins/reject.py | 2 +- tests/test_plugin.py | 5 +++++ 5 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ed54724b..27f6a29df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Config: Adds parameter `min-capacity-sat` to reject tiny channels. - JSON API: `listforwards` now includes the time an HTLC was received and when it was resolved. Both are expressed as UNIX timestamps to facilitate parsing (Issue [#2491](https://github.com/ElementsProject/lightning/issues/2491), PR [#2528](https://github.com/ElementsProject/lightning/pull/2528)) - JSON API: new plugin `invoice_payment` hook for intercepting invoices before they're paid. +- plugin: the `connected` hook can now send an `error_message` to the rejected peer. ### Changed diff --git a/doc/PLUGINS.md b/doc/PLUGINS.md index 9ee4ed82a..2a4987d14 100644 --- a/doc/PLUGINS.md +++ b/doc/PLUGINS.md @@ -268,6 +268,12 @@ gossiped list of known addresses. In particular this means that the port for incoming connections is an ephemeral port, that may not be available for reconnections. +The returned result must contain a `result` member which is either +the string `disconnect` or `continue`. If `disconnect` and +there's a member `error_message`, that member is sent to the peer +before disconnection. + + #### `db_write` This hook is called whenever a change is about to be committed to the database. diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 47d57977f..4649ef0ac 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -706,6 +706,15 @@ peer_connected_hook_cb(struct peer_connected_hook_payload *payload, } if (json_tok_streq(buffer, resulttok, "disconnect")) { + const jsmntok_t *m = json_get_member(buffer, toks, + "error_message"); + if (m) { + error = towire_errorfmt(tmpctx, NULL, + "%.*s", + m->end - m->start, + buffer + m->start); + goto send_error; + } close(peer_fd); tal_free(payload); return; diff --git a/tests/plugins/reject.py b/tests/plugins/reject.py index 685057856..64f9c4ff4 100755 --- a/tests/plugins/reject.py +++ b/tests/plugins/reject.py @@ -16,7 +16,7 @@ plugin = Plugin() def on_connected(peer, plugin): if peer['id'] in plugin.reject_ids: print("{} is in reject list, disconnecting".format(peer['id'])) - return {'result': 'disconnect'} + return {'result': 'disconnect', 'error_message': 'You are in reject list'} print("{} is allowed".format(peer['id'])) return {'result': 'continue'} diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 64dc97d72..f59d41362 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -164,6 +164,11 @@ def test_plugin_connected_hook(node_factory): l3.connect(l1) l1.daemon.wait_for_log(r"{} is in reject list".format(l3.info['id'])) + # FIXME: this error occurs *after* connection, so we connect then drop. + l3.daemon.wait_for_log(r"lightning_openingd-{} chan #1: peer_in WIRE_ERROR" + .format(l1.info['id'])) + l3.daemon.wait_for_log(r"You are in reject list") + peer = l1.rpc.listpeers(l3.info['id'])['peers'] assert(peer == [] or not peer[0]['connected'])