From f97a51cc0f0b0f8c8f6396971dc32ecad405924b Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 16 Apr 2021 14:01:26 +0930 Subject: [PATCH] lightningd: don't send other messages until we've received version. This avoids subdaemons complaining about malformed messages from us, or doing the completely wrong thing, if they are really the wrong version. Signed-off-by: Rusty Russell --- lightningd/subd.c | 13 ++++++++++++- lightningd/subd.h | 3 +++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lightningd/subd.c b/lightningd/subd.c index c0467d4c1..0a9b0c2be 100644 --- a/lightningd/subd.c +++ b/lightningd/subd.c @@ -419,6 +419,11 @@ static bool handle_version(struct subd *sd, const u8 *msg) io_break(sd->ld); return false; } + + sd->rcvd_version = true; + /* In case there are outgoing msgs, we can send now. */ + msg_wake(sd->outq); + return true; } @@ -605,10 +610,15 @@ static void destroy_subd(struct subd *sd) static struct io_plan *msg_send_next(struct io_conn *conn, struct subd *sd) { - const u8 *msg = msg_dequeue(sd->outq); + const u8 *msg; int fd; + /* Don't send if we haven't read version! */ + if (!sd->rcvd_version) + return msg_queue_wait(conn, sd->outq, msg_send_next, sd); + /* Nothing to do? Wait for msg_enqueue. */ + msg = msg_dequeue(sd->outq); if (!msg) return msg_queue_wait(conn, sd->outq, msg_send_next, sd); @@ -702,6 +712,7 @@ static struct subd *new_subd(struct lightningd *ld, tal_add_destructor(sd, destroy_subd); list_head_init(&sd->reqs); sd->channel = channel; + sd->rcvd_version = false; if (node_id) sd->node_id = tal_dup(sd, struct node_id, node_id); else diff --git a/lightningd/subd.h b/lightningd/subd.h index 7fc584cad..94fabf9e3 100644 --- a/lightningd/subd.h +++ b/lightningd/subd.h @@ -32,6 +32,9 @@ struct subd { /* If we are associated with a single channel, this points to it. */ void *channel; + /* Have we received the version msg yet? Don't send until we do. */ + bool rcvd_version; + /* For logging */ struct log *log; const struct node_id *node_id;