diff --git a/channeld/channel.c b/channeld/channel.c index a70fb4a0f..0ddf34198 100644 --- a/channeld/channel.c +++ b/channeld/channel.c @@ -2488,9 +2488,7 @@ int main(int argc, char *argv[]) if (msg) { status_trace("Now dealing with deferred gossip %u", fromwire_peektype(msg)); - handle_gossip_msg(take(msg), &peer->cs, - sync_crypto_write_arg, - NULL); + handle_gossip_msg(PEER_FD, &peer->cs, take(msg)); continue; } @@ -2523,9 +2521,7 @@ int main(int argc, char *argv[]) * connection comes in. */ if (!msg) peer_failed_connection_lost(); - handle_gossip_msg(msg, &peer->cs, - sync_crypto_write_arg, - peer); + handle_gossip_msg(PEER_FD, &peer->cs, take(msg)); } else if (FD_ISSET(PEER_FD, &rfds)) { /* This could take forever, but who cares? */ msg = sync_crypto_read(tmpctx, &peer->cs, PEER_FD); diff --git a/closingd/closing.c b/closingd/closing.c index dbacb46e4..bef2e9886 100644 --- a/closingd/closing.c +++ b/closingd/closing.c @@ -90,8 +90,7 @@ static u8 *closing_read_peer_msg(const tal_t *ctx, msg = peer_or_gossip_sync_read(ctx, PEER_FD, GOSSIP_FD, cs, &from_gossipd); if (from_gossipd) { - handle_gossip_msg(msg, cs, sync_crypto_write_arg, - NULL); + handle_gossip_msg(PEER_FD, cs, take(msg)); continue; } if (!handle_peer_gossip_or_error(PEER_FD, GOSSIP_FD, cs, diff --git a/common/read_peer_msg.c b/common/read_peer_msg.c index f1a4a2e5c..667a047c9 100644 --- a/common/read_peer_msg.c +++ b/common/read_peer_msg.c @@ -80,11 +80,7 @@ bool is_wrong_channel(const u8 *msg, const struct channel_id *expected, return !channel_id_eq(expected, actual); } -void handle_gossip_msg_(const u8 *msg TAKES, int peer_fd, - struct crypto_state *cs, - void (*send_msg)(struct crypto_state *cs, int fd, - const u8 *TAKES, void *arg), - void *arg) +void handle_gossip_msg(int peer_fd, struct crypto_state *cs, const u8 *msg TAKES) { u8 *gossip; @@ -96,10 +92,10 @@ void handle_gossip_msg_(const u8 *msg TAKES, int peer_fd, /* Gossipd can send us gossip messages, OR errors */ if (is_msg_for_gossipd(gossip)) { - send_msg(cs, peer_fd, gossip, arg); + sync_crypto_write(cs, peer_fd, gossip); } else if (fromwire_peektype(gossip) == WIRE_ERROR) { status_debug("Gossipd told us to send error"); - send_msg(cs, peer_fd, gossip, arg); + sync_crypto_write(cs, peer_fd, gossip); peer_failed_connection_lost(); } else { status_broken("Gossipd gave us bad send_gossip message %s", @@ -155,69 +151,3 @@ handled: tal_free(msg); return true; } - -u8 *read_peer_msg_(const tal_t *ctx, - int peer_fd, int gossip_fd, - struct crypto_state *cs, - const struct channel_id *channel, - void (*send_reply)(struct crypto_state *cs, int fd, - const u8 *TAKES, void *arg), - void *arg) -{ - u8 *msg; - bool from_gossipd, all_channels; - struct channel_id actual; - char *err; - - if (gossip_fd > 0) { - msg = peer_or_gossip_sync_read(ctx, peer_fd, gossip_fd, - cs, &from_gossipd); - } else { - msg = sync_crypto_read(ctx, cs, peer_fd); - from_gossipd = false; - } - - if (from_gossipd) { - handle_gossip_msg_(take(msg), peer_fd, cs, send_reply, arg); - return NULL; - } - - if (is_msg_for_gossipd(msg)) { - /* Forward to gossip daemon */ - wire_sync_write(gossip_fd, take(msg)); - return NULL; - } - - if (is_peer_error(tmpctx, msg, channel, &err, &all_channels)) { - if (err) - peer_failed_received_errmsg(peer_fd, gossip_fd, - cs, err, - all_channels - ? NULL : channel); - - /* Ignore unknown channel errors. */ - return tal_free(msg); - } - - /* They're talking about a different channel? */ - if (is_wrong_channel(msg, channel, &actual)) { - status_trace("Rejecting %s for unknown channel_id %s", - wire_type_name(fromwire_peektype(msg)), - type_to_string(tmpctx, struct channel_id, &actual)); - send_reply(cs, peer_fd, - take(towire_errorfmt(NULL, &actual, - "Multiple channels" - " unsupported")), - arg); - return tal_free(msg); - } - - return msg; -} - -/* Helper: sync_crypto_write, with extra args it ignores */ -void sync_crypto_write_arg(struct crypto_state *cs, int fd, const u8 *msg, - void *unused UNUSED) -{ - sync_crypto_write(cs, fd, msg); -} diff --git a/common/read_peer_msg.h b/common/read_peer_msg.h index 97a1a1724..af56c86e6 100644 --- a/common/read_peer_msg.h +++ b/common/read_peer_msg.h @@ -3,7 +3,6 @@ #include "config.h" #include #include -#include struct crypto_state; struct channel_id; @@ -71,59 +70,8 @@ bool handle_peer_gossip_or_error(int peer_fd, int gossip_fd, const struct channel_id *channel_id, const u8 *msg TAKES); -/** - * read_peer_msg - read & decode in a peer message, handling common ones. - * @ctx: context to allocate return packet from. - * @cs: the cryptostate (updated) - * @chanid: the channel id (for identifying errors) - * @send_reply: the way to send a reply packet (eg. sync_crypto_write_arg) - * - * This returns NULL if it handled the message, so it's normally called in - * a loop. - */ -#define read_peer_msg(ctx, cs, chanid, send_reply, arg) \ - read_peer_msg_((ctx), PEER_FD, GOSSIP_FD, (cs), \ - (chanid), \ - typesafe_cb_preargs(void, void *, (send_reply), (arg), \ - struct crypto_state *, int, \ - const u8 *), \ - arg) - -/* Like the above, but don't read from GOSSIP_FD */ -#define read_peer_msg_nogossip(ctx, cs, chanid, send_reply, arg) \ - read_peer_msg_((ctx), PEER_FD, -1, (cs), \ - (chanid), \ - typesafe_cb_preargs(void, void *, (send_reply), (arg), \ - struct crypto_state *, int, \ - const u8 *), \ - arg) - -/* Helper: sync_crypto_write, with extra args it ignores */ -void sync_crypto_write_arg(struct crypto_state *cs, int fd, const u8 *TAKES, - void *unused); - -/* Handler for a gossip msg; used by channeld since it queues them. */ -#define handle_gossip_msg(msg, cs, send_reply, arg) \ - handle_gossip_msg_((msg), PEER_FD, (cs), \ - typesafe_cb_preargs(bool, void *, \ - (send_reply), (arg), \ - struct crypto_state *, int, \ - const u8 *), \ - arg) - -void handle_gossip_msg_(const u8 *msg TAKES, - int peer_fd, - struct crypto_state *cs, - void (*send_msg)(struct crypto_state *cs, int fd, - const u8 *TAKES, void *arg), - void *arg); - -u8 *read_peer_msg_(const tal_t *ctx, - int peer_fd, int gossip_fd, - struct crypto_state *cs, - const struct channel_id *channel, - void (*send_reply)(struct crypto_state *cs, int fd, - const u8 *TAKES, void *arg), - void *arg); +/* We got this message from gossipd: forward/quit as it asks. */ +void handle_gossip_msg(int peer_fd, struct crypto_state *cs, + const u8 *msg TAKES); #endif /* LIGHTNING_COMMON_READ_PEER_MSG_H */ diff --git a/openingd/opening.c b/openingd/opening.c index bf4680196..491809ad0 100644 --- a/openingd/opening.c +++ b/openingd/opening.c @@ -236,8 +236,7 @@ static u8 *opening_read_peer_msg(const tal_t *ctx, struct state *state) msg = peer_or_gossip_sync_read(ctx, PEER_FD, GOSSIP_FD, &state->cs, &from_gossipd); if (from_gossipd) { - handle_gossip_msg(msg, &state->cs, sync_crypto_write_arg, - NULL); + handle_gossip_msg(PEER_FD, &state->cs, take(msg)); continue; } if (!handle_peer_gossip_or_error(PEER_FD, GOSSIP_FD, &state->cs,