lightningd: tell connectd about the custom messages.

We re-send whenever a plugin which allows them starts/finishes.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2023-10-23 21:59:51 +10:30
parent 774719530e
commit ad7dcf381e
7 changed files with 88 additions and 11 deletions

View File

@ -2100,6 +2100,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 (daemon->developer) {
dev_connect_memleak(daemon, msg);
@ -2209,6 +2213,7 @@ int main(int argc, char *argv[])
daemon->gossip_store_fd = -1;
daemon->shutting_down = false;
daemon->dev_suppress_gossip = false;
daemon->custom_msgs = NULL;
/* stdin == control */
daemon->master = daemon_conn_new(daemon, STDIN_FILENO, recv_req, NULL,

View File

@ -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;
/* Hack to speed up gossip timer */
bool dev_fast_gossip;
/* Hack to avoid ping timeouts */

View File

@ -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,

1 #include <bitcoin/block.h>
40 msgtype,connectd_activate_reply,2125 msgtype,connectd_set_custommsgs,2007
41 msgdata,connectd_activate_reply,failmsg,?wirestring, msgdata,connectd_set_custommsgs,len,u32,
42 # Master -> connectd: connect to a peer. msgdata,connectd_set_custommsgs,msgnums,u16,len
43 # Connectd->master, I am ready.
44 msgtype,connectd_activate_reply,2125
45 msgdata,connectd_activate_reply,failmsg,?wirestring,
46 # Master -> connectd: connect to a peer.
47 msgtype,connectd_connect_to_peer,2001
48 msgtype,connectd_connect_to_peer,2001 msgdata,connectd_connect_to_peer,id,node_id,
49 msgdata,connectd_connect_to_peer,id,node_id, msgdata,connectd_connect_to_peer,len,u32,
50 msgdata,connectd_connect_to_peer,len,u32, msgdata,connectd_connect_to_peer,addrs,wireaddr,len

View File

@ -559,6 +559,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;
@ -701,24 +708,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. */

View File

@ -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);

View File

@ -579,6 +579,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:

View File

@ -16,6 +16,7 @@
#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>
@ -23,6 +24,7 @@
#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! */
@ -192,6 +194,32 @@ 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) {
size_t num = tal_count(p->custom_msgs);
/* Blah blah blah memcpy NULL blah blah */
if (num == 0)
continue;
tal_resize(&all_msgs, n + num);
memcpy(all_msgs + n, p->custom_msgs, num * sizeof(*p->custom_msgs));
n += num;
}
/* 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;
@ -232,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)
@ -1950,6 +1981,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);
}