mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-02-22 14:42:40 +01:00
sync_crypto_write: support take(msg)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
de5bf56ffa
commit
456fa39380
5 changed files with 13 additions and 15 deletions
|
@ -9,16 +9,14 @@
|
||||||
#include <wire/wire.h>
|
#include <wire/wire.h>
|
||||||
#include <wire/wire_sync.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 ret;
|
||||||
bool post_sabotage = false;
|
bool post_sabotage = false;
|
||||||
|
|
||||||
status_trace("Writing crypto with sn=%"PRIu64" msg=%s",
|
switch (dev_disconnect(type)) {
|
||||||
cs->sn-2, tal_hex(trc, msg));
|
|
||||||
|
|
||||||
switch (dev_disconnect(fromwire_peektype(msg))) {
|
|
||||||
case DEV_DISCONNECT_BEFORE:
|
case DEV_DISCONNECT_BEFORE:
|
||||||
dev_sabotage_fd(fd);
|
dev_sabotage_fd(fd);
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
|
|
||||||
struct crypto_state;
|
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);
|
u8 *sync_crypto_read(const tal_t *ctx, struct crypto_state *cs, int fd);
|
||||||
|
|
||||||
#endif /* LIGHTNING_LIGHTNINGD_CRYPTO_SYNC_H */
|
#endif /* LIGHTNING_LIGHTNINGD_CRYPTO_SYNC_H */
|
||||||
|
|
|
@ -236,7 +236,7 @@ static struct io_plan *peer_write_done(struct io_conn *conn,
|
||||||
|
|
||||||
u8 *cryptomsg_encrypt_msg(const tal_t *ctx,
|
u8 *cryptomsg_encrypt_msg(const tal_t *ctx,
|
||||||
struct crypto_state *cs,
|
struct crypto_state *cs,
|
||||||
const u8 *msg)
|
const u8 *msg TAKES)
|
||||||
{
|
{
|
||||||
unsigned char npub[crypto_aead_chacha20poly1305_ietf_NPUBBYTES];
|
unsigned char npub[crypto_aead_chacha20poly1305_ietf_NPUBBYTES];
|
||||||
unsigned long long clen, mlen = tal_count(msg);
|
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);
|
maybe_rotate_key(&cs->sn, &cs->sk, &cs->s_ck);
|
||||||
|
|
||||||
|
if (taken(msg))
|
||||||
|
tal_free(msg);
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -336,8 +338,6 @@ struct io_plan *peer_write_message(struct io_conn *conn,
|
||||||
assert(!pcs->out);
|
assert(!pcs->out);
|
||||||
|
|
||||||
pcs->out = cryptomsg_encrypt_msg(conn, &pcs->cs, msg);
|
pcs->out = cryptomsg_encrypt_msg(conn, &pcs->cs, msg);
|
||||||
if (taken(msg))
|
|
||||||
tal_free(msg);
|
|
||||||
pcs->next_out = next;
|
pcs->next_out = next;
|
||||||
|
|
||||||
post = peer_write_done;
|
post = peer_write_done;
|
||||||
|
|
|
@ -181,10 +181,10 @@ static u8 *read_next_peer_msg(struct state *state, const tal_t *ctx)
|
||||||
return tal_free(msg);
|
return tal_free(msg);
|
||||||
}
|
}
|
||||||
if (pong && !sync_crypto_write(&state->cs, PEER_FD,
|
if (pong && !sync_crypto_write(&state->cs, PEER_FD,
|
||||||
pong))
|
take(pong)))
|
||||||
peer_failed(PEER_FD, &state->cs, NULL, WIRE_OPENING_PEER_WRITE_FAILED,
|
peer_failed(PEER_FD, &state->cs, NULL,
|
||||||
|
WIRE_OPENING_PEER_WRITE_FAILED,
|
||||||
"Sending pong");
|
"Sending pong");
|
||||||
tal_free(pong);
|
|
||||||
} else if (is_gossip_msg(msg)) {
|
} else if (is_gossip_msg(msg)) {
|
||||||
/* We relay gossip to gossipd, but don't relay from */
|
/* We relay gossip to gossipd, but don't relay from */
|
||||||
if (!wire_sync_write(GOSSIP_FD, take(msg)))
|
if (!wire_sync_write(GOSSIP_FD, take(msg)))
|
||||||
|
@ -568,7 +568,7 @@ static u8 *fundee_channel(struct state *state,
|
||||||
&ours->delayed_payment,
|
&ours->delayed_payment,
|
||||||
&state->next_per_commit[LOCAL]);
|
&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,
|
peer_failed(PEER_FD, &state->cs, NULL, WIRE_OPENING_PEER_WRITE_FAILED,
|
||||||
"Writing accept_channel");
|
"Writing accept_channel");
|
||||||
|
|
||||||
|
|
|
@ -36,7 +36,7 @@ void peer_failed(int peer_fd, struct crypto_state *cs,
|
||||||
|
|
||||||
/* This is only best-effort; don't block. */
|
/* This is only best-effort; don't block. */
|
||||||
fcntl(peer_fd, F_SETFL, fcntl(peer_fd, F_GETFL) | O_NONBLOCK);
|
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);
|
status_failed(error_code, "%s", errmsg);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue