From 26f17e87a3de9bfcdead53b635c9bdadf71e2dfd Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Thu, 13 Dec 2018 13:58:40 +0100 Subject: [PATCH] plugin: Add connect and disconnect notifications Signed-off-by: Christian Decker --- lightningd/notification.c | 22 +++++++++++++++++++++ lightningd/notification.h | 5 +++++ lightningd/opening_control.c | 2 ++ lightningd/peer_control.c | 4 ++++ lightningd/test/run-invoice-select-inchan.c | 7 +++++++ wallet/test/run-wallet.c | 7 +++++++ 6 files changed, 47 insertions(+) diff --git a/lightningd/notification.c b/lightningd/notification.c index 021c16ab5..6b85a4a5f 100644 --- a/lightningd/notification.c +++ b/lightningd/notification.c @@ -2,6 +2,8 @@ #include const char *notification_topics[] = { + "connect", + "disconnect", }; bool notifications_have_topic(const char *topic) @@ -11,3 +13,23 @@ bool notifications_have_topic(const char *topic) return true; return false; } + +void notify_connect(struct lightningd *ld, struct pubkey *nodeid, + struct wireaddr_internal *addr) +{ + struct jsonrpc_notification *n = + jsonrpc_notification_start(NULL, notification_topics[0]); + json_add_pubkey(n->stream, "id", nodeid); + json_add_address_internal(n->stream, "address", addr); + jsonrpc_notification_end(n); + plugins_notify(ld->plugins, take(n)); +} + +void notify_disconnect(struct lightningd *ld, struct pubkey *nodeid) +{ + struct jsonrpc_notification *n = + jsonrpc_notification_start(NULL, notification_topics[1]); + json_add_pubkey(n->stream, "id", nodeid); + jsonrpc_notification_end(n); + plugins_notify(ld->plugins, take(n)); +} diff --git a/lightningd/notification.h b/lightningd/notification.h index 1cdc1712c..6d4869edd 100644 --- a/lightningd/notification.h +++ b/lightningd/notification.h @@ -2,8 +2,13 @@ #define LIGHTNING_LIGHTNINGD_NOTIFICATION_H #include "config.h" #include +#include #include bool notifications_have_topic(const char *topic); +void notify_connect(struct lightningd *ld, struct pubkey *nodeid, + struct wireaddr_internal *addr); +void notify_disconnect(struct lightningd *ld, struct pubkey *nodeid); + #endif /* LIGHTNING_LIGHTNINGD_NOTIFICATION_H */ diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index 662320545..60dd261de 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -85,6 +86,7 @@ static void uncommitted_channel_disconnect(struct uncommitted_channel *uc, subd_send_msg(uc->peer->ld->connectd, msg); if (uc->fc) was_pending(command_fail(uc->fc->cmd, LIGHTNINGD, "%s", desc)); + notify_disconnect(uc->peer->ld, &uc->peer->id); } void kill_uncommitted_channel(struct uncommitted_channel *uc, diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index afb5fd089..3b4292dc6 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -381,6 +382,7 @@ void channel_errmsg(struct channel *channel, /* Make sure channel_fail_permanent doesn't tell connectd we died! */ channel->connected = false; + notify_disconnect(channel->peer->ld, &channel->peer->id); /* BOLT #1: * @@ -504,6 +506,8 @@ void peer_connected(struct lightningd *ld, const u8 *msg, abort(); } + notify_connect(ld, &id, &addr); + /* No err, all good. */ error = NULL; diff --git a/lightningd/test/run-invoice-select-inchan.c b/lightningd/test/run-invoice-select-inchan.c index 6d16f4c83..20e31fd5a 100644 --- a/lightningd/test/run-invoice-select-inchan.c +++ b/lightningd/test/run-invoice-select-inchan.c @@ -240,6 +240,13 @@ struct oneshot *new_reltimer_(struct timers *timers UNNEEDED, struct timerel expire UNNEEDED, void (*cb)(void *) UNNEEDED, void *arg UNNEEDED) { fprintf(stderr, "new_reltimer_ called!\n"); abort(); } +/* Generated stub for notify_connect */ +void notify_connect(struct lightningd *ld UNNEEDED, struct pubkey *nodeid UNNEEDED, + struct wireaddr_internal *addr UNNEEDED) +{ fprintf(stderr, "notify_connect called!\n"); abort(); } +/* Generated stub for notify_disconnect */ +void notify_disconnect(struct lightningd *ld UNNEEDED, struct pubkey *nodeid UNNEEDED) +{ fprintf(stderr, "notify_disconnect called!\n"); abort(); } /* Generated stub for null_response */ struct json_stream *null_response(struct command *cmd UNNEEDED) { fprintf(stderr, "null_response called!\n"); abort(); } diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 85a06467d..063532b9d 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -292,6 +292,13 @@ void log_add(struct log *log UNNEEDED, const char *fmt UNNEEDED, ...) void log_io(struct log *log UNNEEDED, enum log_level dir UNNEEDED, const char *comment UNNEEDED, const void *data UNNEEDED, size_t len UNNEEDED) { fprintf(stderr, "log_io called!\n"); abort(); } +/* Generated stub for notify_connect */ +void notify_connect(struct lightningd *ld UNNEEDED, struct pubkey *nodeid UNNEEDED, + struct wireaddr_internal *addr UNNEEDED) +{ fprintf(stderr, "notify_connect called!\n"); abort(); } +/* Generated stub for notify_disconnect */ +void notify_disconnect(struct lightningd *ld UNNEEDED, struct pubkey *nodeid UNNEEDED) +{ fprintf(stderr, "notify_disconnect called!\n"); abort(); } /* Generated stub for null_response */ struct json_stream *null_response(struct command *cmd UNNEEDED) { fprintf(stderr, "null_response called!\n"); abort(); }