Skip to content

Commit

Permalink
lightningd: tell connectd about the custom messages.
Browse files Browse the repository at this point in the history
We re-send whenever a plugin which allows them starts/finishes.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Sep 18, 2023
1 parent e9bd072 commit c8594fd
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 11 deletions.
5 changes: 5 additions & 0 deletions connectd/connectd.c
Original file line number Diff line number Diff line change
Expand Up @@ -2081,6 +2081,10 @@ static struct io_plan *recv_req(struct io_conn *conn,
start_shutdown(daemon, msg);
goto out;

case WIRE_CONNECTD_SET_CUSTOMMSGS:
set_custommsgs(daemon, msg);
goto out;

case WIRE_CONNECTD_DEV_MEMLEAK:
#if DEVELOPER
dev_connect_memleak(daemon, msg);
Expand Down Expand Up @@ -2186,6 +2190,7 @@ int main(int argc, char *argv[])
timers_init(&daemon->timers, time_mono());
daemon->gossip_store_fd = -1;
daemon->shutting_down = false;
daemon->custom_msgs = NULL;

/* stdin == control */
daemon->master = daemon_conn_new(daemon, STDIN_FILENO, recv_req, NULL,
Expand Down
3 changes: 3 additions & 0 deletions connectd/connectd.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ struct daemon {
/* Shutting down, don't send new stuff */
bool shutting_down;

/* What (even) custom messages we accept */
u16 *custom_msgs;

#if DEVELOPER
/* Hack to speed up gossip timer */
bool dev_fast_gossip;
Expand Down
5 changes: 5 additions & 0 deletions connectd/connectd_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ msgtype,connectd_activate,2025
# Do we listen?
msgdata,connectd_activate,listen,bool,

# Set the allowed (i.e. don't hang up on!) unknown messages.
msgtype,connectd_set_custommsgs,2007
msgdata,connectd_set_custommsgs,len,u32,
msgdata,connectd_set_custommsgs,msgnums,u16,len

# Connectd->master, I am ready.
msgtype,connectd_activate_reply,2125
msgdata,connectd_activate_reply,failmsg,?wirestring,
Expand Down
49 changes: 38 additions & 11 deletions connectd/multiplex.c
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,13 @@ static void send_ping(struct peer *peer)
set_ping_timer(peer);
}

void set_custommsgs(struct daemon *daemon, const u8 *msg)
{
tal_free(daemon->custom_msgs);
if (!fromwire_connectd_set_custommsgs(daemon, msg, &daemon->custom_msgs))
master_badmsg(WIRE_CONNECTD_SET_CUSTOMMSGS, msg);
}

void send_custommsg(struct daemon *daemon, const u8 *msg)
{
struct node_id id;
Expand Down Expand Up @@ -712,24 +719,44 @@ static void handle_gossip_timestamp_filter_in(struct peer *peer, const u8 *msg)
wake_gossip(peer);
}

static bool find_custom_msg(const u16 *custom_msgs, u16 type)
{
for (size_t i = 0; i < tal_count(custom_msgs); i++) {
if (custom_msgs[i] == type)
return true;
}
return false;
}

static bool handle_custommsg(struct daemon *daemon,
struct peer *peer,
const u8 *msg)
{
enum peer_wire type = fromwire_peektype(msg);
if (type % 2 == 1 && !peer_wire_is_internal(type)) {
/* The message is not part of the messages we know how to
* handle. Assuming this is a custommsg, we just forward it to the
* master. */
status_peer_io(LOG_IO_IN, &peer->id, msg);
daemon_conn_send(daemon->master,
take(towire_connectd_custommsg_in(NULL,
&peer->id,
msg)));
return true;
} else {

/* Messages we expect to handle ourselves. */
if (peer_wire_is_internal(type))
return false;

/* We log it, since it's not going to a subdaemon */
status_peer_io(LOG_IO_IN, &peer->id, msg);

/* Even unknown messages must be explicitly allowed */
if (type % 2 == 0 && !find_custom_msg(daemon->custom_msgs, type)) {
send_warning(peer, "Invalid unknown even msg %s",
tal_hex(msg, msg));
/* We "handled" it... */
return true;
}

/* The message is not part of the messages we know how to
* handle. Assuming this is a custommsg, we just forward it to the
* master. */
daemon_conn_send(daemon->master,
take(towire_connectd_custommsg_in(NULL,
&peer->id,
msg)));
return true;
}

/* We handle pings and gossip messages. */
Expand Down
3 changes: 3 additions & 0 deletions connectd/multiplex.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ void send_manual_ping(struct daemon *daemon, const u8 *msg);
/* When lightningd says to send a custom message (from a plugin) */
void send_custommsg(struct daemon *daemon, const u8 *msg);

/* When lightningd says what custom messages we can recv */
void set_custommsgs(struct daemon *daemon, const u8 *msg);

/* Lightningd wants to talk to you. */
void peer_connect_subd(struct daemon *daemon, const u8 *msg, int fd);
#endif /* LIGHTNING_CONNECTD_MULTIPLEX_H */
1 change: 1 addition & 0 deletions lightningd/connect_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,7 @@ static unsigned connectd_msg(struct subd *connectd, const u8 *msg, const int *fd
case WIRE_CONNECTD_SEND_ONIONMSG:
case WIRE_CONNECTD_CUSTOMMSG_OUT:
case WIRE_CONNECTD_START_SHUTDOWN:
case WIRE_CONNECTD_SET_CUSTOMMSGS:
/* This is a reply, so never gets through to here. */
case WIRE_CONNECTD_INIT_REPLY:
case WIRE_CONNECTD_ACTIVATE_REPLY:
Expand Down
29 changes: 29 additions & 0 deletions lightningd/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
#include <common/memleak.h>
#include <common/timeout.h>
#include <common/version.h>
#include <connectd/connectd_wiregen.h>
#include <dirent.h>
#include <errno.h>
#include <lightningd/io_loop_with_timers.h>
#include <lightningd/notification.h>
#include <lightningd/plugin.h>
#include <lightningd/plugin_control.h>
#include <lightningd/plugin_hook.h>
#include <lightningd/subd.h>
#include <sys/stat.h>

/* Only this file can include this generated header! */
Expand Down Expand Up @@ -196,6 +198,28 @@ struct command_result *plugin_register_all_complete(struct lightningd *ld,
return NULL;
}

static void tell_connectd_custommsgs(struct plugins *plugins)
{
struct plugin *p;
size_t n = 0;
u16 *all_msgs = tal_arr(tmpctx, u16, n);

/* Not when shutting down */
if (!plugins->ld->connectd)
return;

/* Gather from all plugins. */
list_for_each(&plugins->plugins, p, list) {
tal_resize(&all_msgs, n + tal_count(p->custom_msgs));
memcpy(all_msgs + n, p->custom_msgs, tal_bytelen(p->custom_msgs));
n += tal_count(p->custom_msgs);
}

/* Don't bother sorting or uniquifying. If plugins are dumb, they deserve it. */
subd_send_msg(plugins->ld->connectd,
take(towire_connectd_set_custommsgs(NULL, all_msgs)));
}

static void destroy_plugin(struct plugin *p)
{
struct plugin_rpccall *call;
Expand Down Expand Up @@ -236,6 +260,9 @@ static void destroy_plugin(struct plugin *p)
"shutting down lightningd!");
lightningd_exit(p->plugins->ld, 1);
}

if (tal_count(p->custom_msgs))
tell_connectd_custommsgs(p->plugins);
}

static u32 file_checksum(struct lightningd *ld, const char *path)
Expand Down Expand Up @@ -1958,6 +1985,8 @@ static void plugin_config_cb(const char *buffer,
plugin_cmd_succeeded(plugin->start_cmd, plugin);
plugin->start_cmd = NULL;
}
if (tal_count(plugin->custom_msgs))
tell_connectd_custommsgs(plugin->plugins);
check_plugins_initted(plugin->plugins);
}

Expand Down

0 comments on commit c8594fd

Please sign in to comment.