diff --git a/lightningd/crypto_sync.c b/lightningd/crypto_sync.c index 8209c62a2..9f1ee67d8 100644 --- a/lightningd/crypto_sync.c +++ b/lightningd/crypto_sync.c @@ -9,16 +9,14 @@ #include #include -bool sync_crypto_write(struct crypto_state *cs, int fd, const void *msg) +bool sync_crypto_write(struct crypto_state *cs, int fd, const void *msg TAKES) { - u8 *enc = cryptomsg_encrypt_msg(msg, cs, msg); + int type = fromwire_peektype(msg); + u8 *enc = cryptomsg_encrypt_msg(NULL, cs, msg); bool ret; bool post_sabotage = false; - status_trace("Writing crypto with sn=%"PRIu64" msg=%s", - cs->sn-2, tal_hex(trc, msg)); - - switch (dev_disconnect(fromwire_peektype(msg))) { + switch (dev_disconnect(type)) { case DEV_DISCONNECT_BEFORE: dev_sabotage_fd(fd); return false; diff --git a/lightningd/crypto_sync.h b/lightningd/crypto_sync.h index df190590e..4d3aff859 100644 --- a/lightningd/crypto_sync.h +++ b/lightningd/crypto_sync.h @@ -6,7 +6,7 @@ struct crypto_state; -bool sync_crypto_write(struct crypto_state *cs, int fd, const void *msg); +bool sync_crypto_write(struct crypto_state *cs, int fd, const void *msg TAKES); u8 *sync_crypto_read(const tal_t *ctx, struct crypto_state *cs, int fd); #endif /* LIGHTNING_LIGHTNINGD_CRYPTO_SYNC_H */ diff --git a/lightningd/cryptomsg.c b/lightningd/cryptomsg.c index 1a3a4dde2..ef3ac9da2 100644 --- a/lightningd/cryptomsg.c +++ b/lightningd/cryptomsg.c @@ -236,7 +236,7 @@ static struct io_plan *peer_write_done(struct io_conn *conn, u8 *cryptomsg_encrypt_msg(const tal_t *ctx, struct crypto_state *cs, - const u8 *msg) + const u8 *msg TAKES) { unsigned char npub[crypto_aead_chacha20poly1305_ietf_NPUBBYTES]; unsigned long long clen, mlen = tal_count(msg); @@ -314,6 +314,8 @@ u8 *cryptomsg_encrypt_msg(const tal_t *ctx, maybe_rotate_key(&cs->sn, &cs->sk, &cs->s_ck); + if (taken(msg)) + tal_free(msg); return out; } @@ -336,8 +338,6 @@ struct io_plan *peer_write_message(struct io_conn *conn, assert(!pcs->out); pcs->out = cryptomsg_encrypt_msg(conn, &pcs->cs, msg); - if (taken(msg)) - tal_free(msg); pcs->next_out = next; post = peer_write_done; diff --git a/lightningd/opening/opening.c b/lightningd/opening/opening.c index 72fba6c8e..8b221f5a9 100644 --- a/lightningd/opening/opening.c +++ b/lightningd/opening/opening.c @@ -181,10 +181,10 @@ static u8 *read_next_peer_msg(struct state *state, const tal_t *ctx) return tal_free(msg); } if (pong && !sync_crypto_write(&state->cs, PEER_FD, - pong)) - peer_failed(PEER_FD, &state->cs, NULL, WIRE_OPENING_PEER_WRITE_FAILED, + take(pong))) + peer_failed(PEER_FD, &state->cs, NULL, + WIRE_OPENING_PEER_WRITE_FAILED, "Sending pong"); - tal_free(pong); } else if (is_gossip_msg(msg)) { /* We relay gossip to gossipd, but don't relay from */ if (!wire_sync_write(GOSSIP_FD, take(msg))) @@ -568,7 +568,7 @@ static u8 *fundee_channel(struct state *state, &ours->delayed_payment, &state->next_per_commit[LOCAL]); - if (!sync_crypto_write(&state->cs, PEER_FD, msg)) + if (!sync_crypto_write(&state->cs, PEER_FD, take(msg))) peer_failed(PEER_FD, &state->cs, NULL, WIRE_OPENING_PEER_WRITE_FAILED, "Writing accept_channel"); diff --git a/lightningd/peer_failed.c b/lightningd/peer_failed.c index 16c566279..d118ef6cb 100644 --- a/lightningd/peer_failed.c +++ b/lightningd/peer_failed.c @@ -36,7 +36,7 @@ void peer_failed(int peer_fd, struct crypto_state *cs, /* This is only best-effort; don't block. */ fcntl(peer_fd, F_SETFL, fcntl(peer_fd, F_GETFL) | O_NONBLOCK); - sync_crypto_write(cs, peer_fd, msg); + sync_crypto_write(cs, peer_fd, take(msg)); status_failed(error_code, "%s", errmsg); }