From 89e8d26f456593164a256ba809cf5566c130b6e0 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 31 Jan 2018 13:23:42 +1030 Subject: [PATCH] openingd: use common/read_peer_msg. We need to override the err_pkt handler, so we can tell the master that it's just a current-channel negotiation failure. Signed-off-by: Rusty Russell --- openingd/Makefile | 1 + openingd/opening.c | 99 +++++++++------------------------------------- 2 files changed, 20 insertions(+), 80 deletions(-) diff --git a/openingd/Makefile b/openingd/Makefile index 89490bc82..bb7b36881 100644 --- a/openingd/Makefile +++ b/openingd/Makefile @@ -55,6 +55,7 @@ OPENINGD_COMMON_OBJS := \ common/peer_failed.o \ common/permute_tx.o \ common/pseudorand.o \ + common/read_peer_msg.o \ common/status.o \ common/subdaemon.o \ common/type_to_string.o \ diff --git a/openingd/opening.c b/openingd/opening.c index dd3da50dd..b0548b301 100644 --- a/openingd/opening.c +++ b/openingd/opening.c @@ -12,8 +12,8 @@ #include #include #include -#include #include +#include #include #include #include @@ -203,88 +203,27 @@ static void temporary_channel_id(struct channel_id *channel_id) channel_id->id[i] = pseudorand(256); } -static void handle_ping(const u8 *msg, - struct crypto_state *cs, - const struct channel_id *our_channel_id) +/* This handles the case where there's an error only for this channel */ +static void opening_errpkt(const char *desc, + bool this_channel_only, + struct state *state) { - u8 *pong; - - if (!check_ping_make_pong(msg, msg, &pong)) - peer_failed(PEER_FD, cs, our_channel_id, "Bad ping"); - - status_trace("Got ping, sending %s", pong ? - wire_type_name(fromwire_peektype(pong)) - : "nothing"); - - if (pong && !sync_crypto_write(cs, PEER_FD, take(pong))) - status_failed(STATUS_FAIL_PEER_IO, - "Failed writing pong: %s", strerror(errno)); + if (this_channel_only) + negotiation_failed(state, false, "Error packet: %s", desc); + else + status_failed(STATUS_FAIL_PEER_BAD, "Received ERROR %s", desc); } -/* Handle random messages we might get, returning NULL if we handled it. */ -static u8 *read_peer_msg(struct state *state) +/* Handle random messages we might get, returning the first non-handled one. */ +static u8 *opening_read_peer_msg(struct state *state) { u8 *msg; - struct channel_id channel_id; - - msg = sync_crypto_read(state, &state->cs, PEER_FD); - if (!msg) - status_failed(STATUS_FAIL_PEER_IO, - "Failed reading from peer: %s", strerror(errno)); - if (is_gossip_msg(msg)) { - /* Forward to gossip daemon */ - wire_sync_write(GOSSIP_FD, take(msg)); - return NULL; - } - - if (fromwire_peektype(msg) == WIRE_PING) { - handle_ping(msg, &state->cs, &state->channel_id); - return tal_free(msg); - } - - if (fromwire_peektype(msg) == WIRE_ERROR) { - struct channel_id chanid; - char *err = sanitize_error(msg, msg, &chanid); - - /* BOLT #1: - * - * The channel is referred to by `channel_id`, unless - * `channel_id` is 0 (i.e. all bytes are 0), in which - * case it refers to all channels. - * ... - - * The receiving node: - * - upon receiving `error`: - * - MUST fail the channel referred to by the error - * message. - * - if no existing channel is referred to by the - * message: - * - MUST ignore the message. - */ - if (channel_id_is_all(&chanid)) - status_failed(STATUS_FAIL_PEER_BAD, - "Received ERROR %s", err); - else if (structeq(&chanid, &state->channel_id)) - negotiation_failed(state, false, - "Error packet: %s", err); - - return tal_free(msg); - } - - /* They're talking about a different channel? */ - if (extract_channel_id(msg, &channel_id) - && !structeq(&channel_id, &state->channel_id)) { - status_trace("Rejecting %s for unknown channel_id %s", - wire_type_name(fromwire_peektype(msg)), - type_to_string(msg, struct channel_id, - &channel_id)); - sync_crypto_write(&state->cs, PEER_FD, - take(towire_errorfmt(msg, &channel_id, - "Multiple channels" - " unsupported"))); - return tal_free(msg); - } + while ((msg = read_peer_msg(state, &state->cs, &state->channel_id, + sync_crypto_write_arg, + status_fail_io, + opening_errpkt, + state)) == NULL); return msg; } @@ -353,7 +292,7 @@ static u8 *funder_channel(struct state *state, state->remoteconf = tal(state, struct channel_config); - while ((msg = read_peer_msg(state)) == NULL); + msg = opening_read_peer_msg(state); /* BOLT #2: * @@ -474,7 +413,7 @@ static u8 *funder_channel(struct state *state, * commitment transaction, so they can broadcast it knowing they can * redeem their funds if they need to. */ - while ((msg = read_peer_msg(state)) == NULL); + msg = opening_read_peer_msg(state); if (!fromwire_funding_signed(msg, NULL, &id_in, &sig)) peer_failed(PEER_FD, &state->cs, &state->channel_id, @@ -657,7 +596,7 @@ static u8 *fundee_channel(struct state *state, status_failed(STATUS_FAIL_PEER_IO, "Writing accept_channel: %s", strerror(errno)); - while ((msg = read_peer_msg(state)) == NULL); + msg = opening_read_peer_msg(state); if (!fromwire_funding_created(msg, NULL, &id_in, &state->funding_txid,