mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-03-01 17:47:30 +01:00
read_peer_msg: handle incoming gossip from gossipd.
This means that openingd and closingd now forward our gossip. But the real reason we want to do this is that it gives an easy way for gossipd to kill any active daemon, by closing its fd: previously closingd and openingd didn't read the fd, so tended not to notice. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
ab9d9ef3b8
commit
b68fb24758
5 changed files with 89 additions and 28 deletions
|
@ -298,29 +298,6 @@ static void enqueue_peer_msg(struct peer *peer, const u8 *msg TAKES)
|
||||||
msg_enqueue(&peer->peer_out, msg);
|
msg_enqueue(&peer->peer_out, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void gossip_in(struct peer *peer, const u8 *msg)
|
|
||||||
{
|
|
||||||
u8 *gossip;
|
|
||||||
|
|
||||||
if (!fromwire_gossip_send_gossip(msg, msg, &gossip))
|
|
||||||
status_failed(STATUS_FAIL_GOSSIP_IO,
|
|
||||||
"Got bad message from gossipd: %s",
|
|
||||||
tal_hex(msg, msg));
|
|
||||||
|
|
||||||
if (is_msg_for_gossipd(gossip))
|
|
||||||
enqueue_peer_msg(peer, gossip);
|
|
||||||
else if (fromwire_peektype(gossip) == WIRE_ERROR) {
|
|
||||||
struct channel_id channel_id;
|
|
||||||
char *what = sanitize_error(msg, msg, &channel_id);
|
|
||||||
peer_failed(&peer->cs, &channel_id,
|
|
||||||
"gossipd said: %s", what);
|
|
||||||
} else
|
|
||||||
status_failed(STATUS_FAIL_GOSSIP_IO,
|
|
||||||
"Got bad message type %s from gossipd: %s",
|
|
||||||
wire_type_name(fromwire_peektype(gossip)),
|
|
||||||
tal_hex(msg, msg));
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Send a temporary `channel_announcement` and `channel_update`. These
|
/* Send a temporary `channel_announcement` and `channel_update`. These
|
||||||
* are unsigned and mainly used to tell gossip about the channel
|
* are unsigned and mainly used to tell gossip about the channel
|
||||||
* before we have reached the `announcement_depth`, not being signed
|
* before we have reached the `announcement_depth`, not being signed
|
||||||
|
@ -2632,8 +2609,10 @@ int main(int argc, char *argv[])
|
||||||
if (msg) {
|
if (msg) {
|
||||||
status_trace("Now dealing with deferred gossip %u",
|
status_trace("Now dealing with deferred gossip %u",
|
||||||
fromwire_peektype(msg));
|
fromwire_peektype(msg));
|
||||||
gossip_in(peer, msg);
|
handle_gossip_msg(take(msg), &peer->cs,
|
||||||
tal_free(msg);
|
channeld_send_reply,
|
||||||
|
channeld_io_error,
|
||||||
|
peer);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2687,7 +2666,10 @@ int main(int argc, char *argv[])
|
||||||
status_failed(STATUS_FAIL_GOSSIP_IO,
|
status_failed(STATUS_FAIL_GOSSIP_IO,
|
||||||
"Can't read command: %s",
|
"Can't read command: %s",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
gossip_in(peer, msg);
|
handle_gossip_msg(msg, &peer->cs,
|
||||||
|
channeld_send_reply,
|
||||||
|
channeld_io_error,
|
||||||
|
peer);
|
||||||
} else if (FD_ISSET(PEER_FD, &rfds)) {
|
} else if (FD_ISSET(PEER_FD, &rfds)) {
|
||||||
/* This could take forever, but who cares? */
|
/* This could take forever, but who cares? */
|
||||||
msg = channeld_read_peer_msg(peer);
|
msg = channeld_read_peer_msg(peer);
|
||||||
|
|
|
@ -66,7 +66,10 @@ CLOSINGD_COMMON_OBJS := \
|
||||||
common/type_to_string.o \
|
common/type_to_string.o \
|
||||||
common/utils.o \
|
common/utils.o \
|
||||||
common/version.o \
|
common/version.o \
|
||||||
common/wire_error.o
|
common/wire_error.o \
|
||||||
|
common/wireaddr.o \
|
||||||
|
gossipd/gen_gossip_wire.o \
|
||||||
|
lightningd/gossip_msg.o
|
||||||
|
|
||||||
closingd/gen_closing_wire.h: $(WIRE_GEN) closingd/closing_wire.csv
|
closingd/gen_closing_wire.h: $(WIRE_GEN) closingd/closing_wire.csv
|
||||||
$(WIRE_GEN) --header $@ closing_wire_type < closingd/closing_wire.csv > $@
|
$(WIRE_GEN) --header $@ closing_wire_type < closingd/closing_wire.csv > $@
|
||||||
|
|
|
@ -8,6 +8,8 @@
|
||||||
#include <common/utils.h>
|
#include <common/utils.h>
|
||||||
#include <common/wire_error.h>
|
#include <common/wire_error.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <gossipd/gen_gossip_wire.h>
|
||||||
|
#include <sys/select.h>
|
||||||
#include <wire/peer_wire.h>
|
#include <wire/peer_wire.h>
|
||||||
#include <wire/wire_sync.h>
|
#include <wire/wire_sync.h>
|
||||||
|
|
||||||
|
@ -38,6 +40,38 @@ static void handle_ping(const u8 *msg,
|
||||||
io_error(arg);
|
io_error(arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void handle_gossip_msg_(const u8 *msg TAKES, int peer_fd,
|
||||||
|
struct crypto_state *cs,
|
||||||
|
bool (*send_msg)(struct crypto_state *cs, int fd,
|
||||||
|
const u8 *TAKES, void *arg),
|
||||||
|
void (*io_error)(void *arg),
|
||||||
|
void *arg)
|
||||||
|
{
|
||||||
|
u8 *gossip;
|
||||||
|
|
||||||
|
if (!fromwire_gossip_send_gossip(tmpctx, msg, &gossip)) {
|
||||||
|
status_broken("Got bad message from gossipd: %s",
|
||||||
|
tal_hex(msg, msg));
|
||||||
|
io_error(arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Gossipd can send us gossip messages, OR errors */
|
||||||
|
if (is_msg_for_gossipd(gossip)) {
|
||||||
|
if (!send_msg(cs, peer_fd, gossip, arg))
|
||||||
|
io_error(arg);
|
||||||
|
} else if (fromwire_peektype(gossip) == WIRE_ERROR) {
|
||||||
|
status_debug("Gossipd old us to send error");
|
||||||
|
send_msg(cs, peer_fd, gossip, arg);
|
||||||
|
io_error(arg);
|
||||||
|
} else {
|
||||||
|
status_broken("Gossipd gave us bad send_gossip message %s",
|
||||||
|
tal_hex(msg, msg));
|
||||||
|
io_error(arg);
|
||||||
|
}
|
||||||
|
if (taken(msg))
|
||||||
|
tal_free(msg);
|
||||||
|
}
|
||||||
|
|
||||||
u8 *read_peer_msg_(const tal_t *ctx,
|
u8 *read_peer_msg_(const tal_t *ctx,
|
||||||
int peer_fd, int gossip_fd,
|
int peer_fd, int gossip_fd,
|
||||||
struct crypto_state *cs,
|
struct crypto_state *cs,
|
||||||
|
@ -49,6 +83,27 @@ u8 *read_peer_msg_(const tal_t *ctx,
|
||||||
{
|
{
|
||||||
u8 *msg;
|
u8 *msg;
|
||||||
struct channel_id chanid;
|
struct channel_id chanid;
|
||||||
|
fd_set readfds;
|
||||||
|
|
||||||
|
FD_ZERO(&readfds);
|
||||||
|
FD_SET(peer_fd, &readfds);
|
||||||
|
FD_SET(gossip_fd, &readfds);
|
||||||
|
|
||||||
|
select(peer_fd > gossip_fd ? peer_fd + 1 : gossip_fd + 1,
|
||||||
|
&readfds, NULL, NULL, NULL);
|
||||||
|
|
||||||
|
if (FD_ISSET(gossip_fd, &readfds)) {
|
||||||
|
/* gossipd uses this to kill us, so not a surprise if it
|
||||||
|
happens. */
|
||||||
|
msg = wire_sync_read(NULL, gossip_fd);
|
||||||
|
if (!msg) {
|
||||||
|
status_debug("Error reading gossip msg");
|
||||||
|
io_error(arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
handle_gossip_msg_(msg, peer_fd, cs, send_reply, io_error, arg);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
msg = sync_crypto_read(ctx, cs, peer_fd);
|
msg = sync_crypto_read(ctx, cs, peer_fd);
|
||||||
if (!msg)
|
if (!msg)
|
||||||
|
|
|
@ -36,6 +36,24 @@ bool sync_crypto_write_arg(struct crypto_state *cs, int fd, const u8 *TAKES,
|
||||||
/* Helper: calls peer_failed_connection_lost. */
|
/* Helper: calls peer_failed_connection_lost. */
|
||||||
void status_fail_io(void *unused);
|
void status_fail_io(void *unused);
|
||||||
|
|
||||||
|
/* Handler for a gossip msg; used by channeld since it queues them. */
|
||||||
|
#define handle_gossip_msg(msg, cs, send_reply, io_error, arg) \
|
||||||
|
handle_gossip_msg_((msg), PEER_FD, (cs), \
|
||||||
|
typesafe_cb_preargs(bool, void *, \
|
||||||
|
(send_reply), (arg), \
|
||||||
|
struct crypto_state *, int, \
|
||||||
|
const u8 *), \
|
||||||
|
typesafe_cb(void, void *, (io_error), (arg)), \
|
||||||
|
arg)
|
||||||
|
|
||||||
|
void handle_gossip_msg_(const u8 *msg TAKES,
|
||||||
|
int peer_fd,
|
||||||
|
struct crypto_state *cs,
|
||||||
|
bool (*send_msg)(struct crypto_state *cs, int fd,
|
||||||
|
const u8 *TAKES, void *arg),
|
||||||
|
void (*io_error)(void *arg),
|
||||||
|
void *arg);
|
||||||
|
|
||||||
u8 *read_peer_msg_(const tal_t *ctx,
|
u8 *read_peer_msg_(const tal_t *ctx,
|
||||||
int peer_fd, int gossip_fd,
|
int peer_fd, int gossip_fd,
|
||||||
struct crypto_state *cs,
|
struct crypto_state *cs,
|
||||||
|
|
|
@ -67,7 +67,10 @@ OPENINGD_COMMON_OBJS := \
|
||||||
common/utils.o \
|
common/utils.o \
|
||||||
common/utxo.o \
|
common/utxo.o \
|
||||||
common/version.o \
|
common/version.o \
|
||||||
common/wire_error.o
|
common/wire_error.o \
|
||||||
|
common/wireaddr.o \
|
||||||
|
gossipd/gen_gossip_wire.o \
|
||||||
|
lightningd/gossip_msg.o
|
||||||
|
|
||||||
$(LIGHTNINGD_OPENING_OBJS): $(LIGHTNINGD_HEADERS)
|
$(LIGHTNINGD_OPENING_OBJS): $(LIGHTNINGD_HEADERS)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue