diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index 8c63da670..7ae8250e3 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -38,9 +38,8 @@ static void peer_bad_message(struct subd *gossip, const u8 *msg) log_debug(gossip->log, "Peer %s gave bad msg %s", type_to_string(msg, struct pubkey, peer->id), tal_hex(msg, msg)); - peer_set_condition(peer, "Bad message %s during gossip phase", - gossip_wire_type_name(fromwire_peektype(msg))); - tal_free(peer); + peer_fail(peer, "Bad message %s during gossip phase", + gossip_wire_type_name(fromwire_peektype(msg))); } static void peer_failed(struct subd *gossip, const u8 *msg) @@ -61,8 +60,7 @@ static void peer_failed(struct subd *gossip, const u8 *msg) log_unusual(gossip->log, "Peer %s failed: %.*s", type_to_string(msg, struct pubkey, peer->id), (int)tal_len(err), (const char *)err); - peer_set_condition(peer, "Error during gossip phase"); - tal_free(peer); + peer_fail(peer, "Error during gossip phase"); } static void peer_nongossip(struct subd *gossip, const u8 *msg, diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 22e1e09b6..4ebecc340 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -43,6 +43,18 @@ static void destroy_peer(struct peer *peer) peer->condition); } +void peer_fail(struct peer *peer, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + log_unusual(peer->log, "Peer has failed: "); + logv(peer->log, -1, fmt, ap); + va_end(ap); + + tal_free(peer); +} + void peer_set_condition(struct peer *peer, const char *fmt, ...) { va_list ap; @@ -169,8 +181,8 @@ static bool peer_got_handshake_hsmfd(struct subd *hsm, const u8 *msg, assert(tal_count(fds) == 1); if (!fromwire_hsmctl_hsmfd_ecdh_fd_reply(msg, NULL)) { - log_unusual(peer->ld->log, "Malformed hsmfd response: %s", - tal_hex(peer, msg)); + peer_fail(peer, "Malformed hsmfd response: %s", + tal_hex(peer, msg)); goto error; } @@ -182,9 +194,8 @@ static bool peer_got_handshake_hsmfd(struct subd *hsm, const u8 *msg, NULL, NULL, fds[0], peer->fd, -1); if (!peer->owner) { - log_unusual(peer->ld->log, "Could not subdaemon handshake: %s", - strerror(errno)); - peer_set_condition(peer, "Failed to subdaemon handshake"); + peer_fail(peer, "Could not subdaemon handshake: %s", + strerror(errno)); goto error; } @@ -207,7 +218,6 @@ static bool peer_got_handshake_hsmfd(struct subd *hsm, const u8 *msg, error: close(fds[0]); - tal_free(peer); return true; } @@ -1391,8 +1401,7 @@ static bool peer_start_channeld_hsmfd(struct subd *hsm, const u8 *resp, if (!cds->peer->owner) { log_unusual(cds->peer->log, "Could not subdaemon channel: %s", strerror(errno)); - peer_set_condition(cds->peer, "Failed to subdaemon channel"); - tal_free(cds->peer); + peer_fail(cds->peer, "Failed to subdaemon channel"); return true; } cds->peer->fd = -1; @@ -1672,8 +1681,8 @@ void peer_accept_open(struct peer *peer, if (fromwire_peektype(from_peer) != WIRE_OPEN_CHANNEL) { log_unusual(peer->log, "Strange message to exit gossip: %u", fromwire_peektype(from_peer)); - peer_set_condition(peer, "Bad message during gossiping"); - tal_free(peer); + peer_fail(peer, "Bad message during gossiping: %s", + tal_hex(peer, from_peer)); return; } @@ -1683,10 +1692,8 @@ void peer_accept_open(struct peer *peer, NULL, NULL, peer->fd, -1); if (!peer->owner) { - log_unusual(ld->log, "Could not subdaemon opening: %s", - strerror(errno)); - peer_set_condition(peer, "Failed to subdaemon opening"); - tal_free(peer); + peer_fail(peer, "Failed to subdaemon opening: %s", + strerror(errno)); return; } /* We handed off peer fd */ @@ -1716,8 +1723,7 @@ void peer_accept_open(struct peer *peer, /* Careful here! Their message could push us overlength! */ if (tal_len(msg) >= 65536) { - peer_set_condition(peer, "Unacceptably long open_channel"); - tal_free(peer); + peer_fail(peer, "Unacceptably long open_channel"); return; } subd_req(peer, peer->owner, take(msg), -1, 0, opening_accept_reply, peer); @@ -1756,10 +1762,8 @@ static bool gossip_peer_released(struct subd *gossip, NULL, NULL, fc->peer->fd, -1); if (!opening) { - log_unusual(ld->log, "Could not subdaemon opening: %s", - strerror(errno)); - peer_set_condition(fc->peer, "Failed to subdaemon opening"); - tal_free(fc->peer); + peer_fail(fc->peer, "Failed to subdaemon opening: %s", + strerror(errno)); return true; } fc->peer->owner = opening; diff --git a/lightningd/peer_control.h b/lightningd/peer_control.h index a9c438beb..5737ff56f 100644 --- a/lightningd/peer_control.h +++ b/lightningd/peer_control.h @@ -76,6 +76,9 @@ struct peer *peer_from_json(struct lightningd *ld, void peer_accept_open(struct peer *peer, const struct crypto_state *cs, const u8 *msg); +/* Peer has failed. */ +PRINTF_FMT(2,3) void peer_fail(struct peer *peer, const char *fmt, ...); + PRINTF_FMT(2,3) void peer_set_condition(struct peer *peer, const char *fmt, ...); void setup_listeners(struct lightningd *ld); #endif /* LIGHTNING_LIGHTNINGD_PEER_CONTROL_H */ diff --git a/lightningd/peer_failed.c b/lightningd/peer_failed.c index b8e3c1005..e1f343f7b 100644 --- a/lightningd/peer_failed.c +++ b/lightningd/peer_failed.c @@ -30,6 +30,8 @@ void peer_failed(int peer_fd, struct crypto_state *cs, va_start(ap, fmt); errmsg = tal_vfmt(NULL, fmt, ap); va_end(ap); + /* Make sure it's correct length for error. */ + tal_resize(&errmsg, strlen(errmsg)+1); msg = towire_error(errmsg, channel_id, (const u8 *)errmsg); /* This is only best-effort; don't block. */