Browse Source

sync_crypto_write: support take(msg)

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 8 years ago
committed by Christian Decker
parent
commit
456fa39380
  1. 10
      lightningd/crypto_sync.c
  2. 2
      lightningd/crypto_sync.h
  3. 6
      lightningd/cryptomsg.c
  4. 8
      lightningd/opening/opening.c
  5. 2
      lightningd/peer_failed.c

10
lightningd/crypto_sync.c

@ -9,16 +9,14 @@
#include <wire/wire.h>
#include <wire/wire_sync.h>
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;

2
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 */

6
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;

8
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");

2
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);
}

Loading…
Cancel
Save