mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 15:14:23 +01:00
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 <rusty@rustcorp.com.au>
This commit is contained in:
@@ -55,6 +55,7 @@ OPENINGD_COMMON_OBJS := \
|
|||||||
common/peer_failed.o \
|
common/peer_failed.o \
|
||||||
common/permute_tx.o \
|
common/permute_tx.o \
|
||||||
common/pseudorand.o \
|
common/pseudorand.o \
|
||||||
|
common/read_peer_msg.o \
|
||||||
common/status.o \
|
common/status.o \
|
||||||
common/subdaemon.o \
|
common/subdaemon.o \
|
||||||
common/type_to_string.o \
|
common/type_to_string.o \
|
||||||
|
|||||||
@@ -12,8 +12,8 @@
|
|||||||
#include <common/initial_channel.h>
|
#include <common/initial_channel.h>
|
||||||
#include <common/key_derive.h>
|
#include <common/key_derive.h>
|
||||||
#include <common/peer_failed.h>
|
#include <common/peer_failed.h>
|
||||||
#include <common/ping.h>
|
|
||||||
#include <common/pseudorand.h>
|
#include <common/pseudorand.h>
|
||||||
|
#include <common/read_peer_msg.h>
|
||||||
#include <common/status.h>
|
#include <common/status.h>
|
||||||
#include <common/subdaemon.h>
|
#include <common/subdaemon.h>
|
||||||
#include <common/type_to_string.h>
|
#include <common/type_to_string.h>
|
||||||
@@ -203,88 +203,27 @@ static void temporary_channel_id(struct channel_id *channel_id)
|
|||||||
channel_id->id[i] = pseudorand(256);
|
channel_id->id[i] = pseudorand(256);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void handle_ping(const u8 *msg,
|
/* This handles the case where there's an error only for this channel */
|
||||||
struct crypto_state *cs,
|
static void opening_errpkt(const char *desc,
|
||||||
const struct channel_id *our_channel_id)
|
bool this_channel_only,
|
||||||
|
struct state *state)
|
||||||
{
|
{
|
||||||
u8 *pong;
|
if (this_channel_only)
|
||||||
|
negotiation_failed(state, false, "Error packet: %s", desc);
|
||||||
if (!check_ping_make_pong(msg, msg, &pong))
|
else
|
||||||
peer_failed(PEER_FD, cs, our_channel_id, "Bad ping");
|
status_failed(STATUS_FAIL_PEER_BAD, "Received ERROR %s", desc);
|
||||||
|
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Handle random messages we might get, returning NULL if we handled it. */
|
/* Handle random messages we might get, returning the first non-handled one. */
|
||||||
static u8 *read_peer_msg(struct state *state)
|
static u8 *opening_read_peer_msg(struct state *state)
|
||||||
{
|
{
|
||||||
u8 *msg;
|
u8 *msg;
|
||||||
struct channel_id channel_id;
|
|
||||||
|
|
||||||
msg = sync_crypto_read(state, &state->cs, PEER_FD);
|
while ((msg = read_peer_msg(state, &state->cs, &state->channel_id,
|
||||||
if (!msg)
|
sync_crypto_write_arg,
|
||||||
status_failed(STATUS_FAIL_PEER_IO,
|
status_fail_io,
|
||||||
"Failed reading from peer: %s", strerror(errno));
|
opening_errpkt,
|
||||||
|
state)) == NULL);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
@@ -353,7 +292,7 @@ static u8 *funder_channel(struct state *state,
|
|||||||
|
|
||||||
state->remoteconf = tal(state, struct channel_config);
|
state->remoteconf = tal(state, struct channel_config);
|
||||||
|
|
||||||
while ((msg = read_peer_msg(state)) == NULL);
|
msg = opening_read_peer_msg(state);
|
||||||
|
|
||||||
/* BOLT #2:
|
/* BOLT #2:
|
||||||
*
|
*
|
||||||
@@ -474,7 +413,7 @@ static u8 *funder_channel(struct state *state,
|
|||||||
* commitment transaction, so they can broadcast it knowing they can
|
* commitment transaction, so they can broadcast it knowing they can
|
||||||
* redeem their funds if they need to.
|
* 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))
|
if (!fromwire_funding_signed(msg, NULL, &id_in, &sig))
|
||||||
peer_failed(PEER_FD, &state->cs, &state->channel_id,
|
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,
|
status_failed(STATUS_FAIL_PEER_IO,
|
||||||
"Writing accept_channel: %s", strerror(errno));
|
"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,
|
if (!fromwire_funding_created(msg, NULL, &id_in,
|
||||||
&state->funding_txid,
|
&state->funding_txid,
|
||||||
|
|||||||
Reference in New Issue
Block a user