Browse Source

sync_crypto_write/sync_crypto_read: just fail, don't return NULL.

There's only one thing the caller ever does, just do that internally.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 6 years ago
parent
commit
0b08601951
  1. 48
      channeld/channel.c
  2. 6
      closingd/closing.c
  3. 18
      common/crypto_sync.c
  4. 5
      common/crypto_sync.h
  5. 35
      common/read_peer_msg.c
  6. 10
      common/read_peer_msg.h
  7. 1
      connectd/Makefile
  8. 1
      gossipd/Makefile
  9. 9
      openingd/opening.c

48
channeld/channel.c

@ -326,8 +326,7 @@ static void send_announcement_signatures(struct peer *peer)
NULL, &peer->channel_id, &peer->short_channel_ids[LOCAL],
&peer->announcement_node_sigs[LOCAL],
&peer->announcement_bitcoin_sigs[LOCAL]);
if (!sync_crypto_write(&peer->cs, PEER_FD, take(msg)))
peer_failed_connection_lost();
sync_crypto_write(&peer->cs, PEER_FD, take(msg));
}
/* Tentatively create a channel_announcement, possibly with invalid
@ -671,8 +670,7 @@ static void maybe_send_shutdown(struct peer *peer)
send_channel_update(peer, ROUTING_FLAGS_DISABLED);
msg = towire_shutdown(NULL, &peer->channel_id, peer->final_scriptpubkey);
if (!sync_crypto_write(&peer->cs, PEER_FD, take(msg)))
peer_failed_connection_lost();
sync_crypto_write(&peer->cs, PEER_FD, take(msg));
peer->send_shutdown = false;
peer->shutdown_sent[LOCAL] = true;
billboard_update(peer);
@ -992,8 +990,7 @@ static void send_commit(struct peer *peer)
feerate, max);
msg = towire_update_fee(NULL, &peer->channel_id, feerate);
if (!sync_crypto_write(&peer->cs, PEER_FD, take(msg)))
peer_failed_connection_lost();
sync_crypto_write(&peer->cs, PEER_FD, take(msg));
}
/* BOLT #2:
@ -1035,8 +1032,7 @@ static void send_commit(struct peer *peer)
msg = towire_commitment_signed(NULL, &peer->channel_id,
&peer->next_commit_sigs->commit_sig,
peer->next_commit_sigs->htlc_sigs);
if (!sync_crypto_write(&peer->cs, PEER_FD, take(msg)))
peer_failed_connection_lost();
sync_crypto_write(&peer->cs, PEER_FD, take(msg));
peer->next_commit_sigs = tal_free(peer->next_commit_sigs);
maybe_send_shutdown(peer);
@ -1100,8 +1096,7 @@ static void send_revocation(struct peer *peer)
start_commit_timer(peer);
}
if (!sync_crypto_write(&peer->cs, PEER_FD, take(msg)))
peer_failed_connection_lost();
sync_crypto_write(&peer->cs, PEER_FD, take(msg));
}
static u8 *got_commitsig_msg(const tal_t *ctx,
@ -1658,8 +1653,7 @@ static void resend_revoke(struct peer *peer)
struct pubkey point;
/* Current commit is peer->next_index[LOCAL]-1, revoke prior */
u8 *msg = make_revocation_msg(peer, peer->next_index[LOCAL]-2, &point);
if (!sync_crypto_write(&peer->cs, PEER_FD, take(msg)))
peer_failed_connection_lost();
sync_crypto_write(&peer->cs, PEER_FD, take(msg));
}
static void send_fail_or_fulfill(struct peer *peer, const struct htlc *h)
@ -1698,8 +1692,7 @@ static void send_fail_or_fulfill(struct peer *peer, const struct htlc *h)
&peer->channel_id,
"HTLC %"PRIu64" state %s not failed/fulfilled",
h->id, htlc_state_name(h->state));
if (!sync_crypto_write(&peer->cs, PEER_FD, take(msg)))
peer_failed_connection_lost();
sync_crypto_write(&peer->cs, PEER_FD, take(msg));
}
static void resend_commitment(struct peer *peer, const struct changed_htlc *last)
@ -1740,8 +1733,7 @@ static void resend_commitment(struct peer *peer, const struct changed_htlc *last
abs_locktime_to_blocks(
&h->expiry),
h->routing);
if (!sync_crypto_write(&peer->cs, PEER_FD, take(msg)))
peer_failed_connection_lost();
sync_crypto_write(&peer->cs, PEER_FD, take(msg));
} else if (h->state == SENT_REMOVE_COMMIT) {
send_fail_or_fulfill(peer, h);
}
@ -1751,8 +1743,7 @@ static void resend_commitment(struct peer *peer, const struct changed_htlc *last
if (peer->channel->funder == LOCAL) {
msg = towire_update_fee(NULL, &peer->channel_id,
channel_feerate(peer->channel, REMOTE));
if (!sync_crypto_write(&peer->cs, PEER_FD, take(msg)))
peer_failed_connection_lost();
sync_crypto_write(&peer->cs, PEER_FD, take(msg));
}
/* Re-send the commitment_signed itself. */
@ -1760,8 +1751,7 @@ static void resend_commitment(struct peer *peer, const struct changed_htlc *last
msg = towire_commitment_signed(NULL, &peer->channel_id,
&commit_sigs->commit_sig,
commit_sigs->htlc_sigs);
if (!sync_crypto_write(&peer->cs, PEER_FD, take(msg)))
peer_failed_connection_lost();
sync_crypto_write(&peer->cs, PEER_FD, take(msg));
tal_free(commit_sigs);
/* If we have already received the revocation for the previous, the
@ -1811,8 +1801,7 @@ static void peer_reconnect(struct peer *peer)
msg = towire_channel_reestablish(NULL, &peer->channel_id,
peer->next_index[LOCAL],
peer->revocations_received);
if (!sync_crypto_write(&peer->cs, PEER_FD, take(msg)))
peer_failed_connection_lost();
sync_crypto_write(&peer->cs, PEER_FD, take(msg));
peer_billboard(false, "Sent reestablish, waiting for theirs");
@ -1856,8 +1845,7 @@ static void peer_reconnect(struct peer *peer)
msg = towire_funding_locked(NULL,
&peer->channel_id,
&peer->next_local_per_commit);
if (!sync_crypto_write(&peer->cs, PEER_FD, take(msg)))
peer_failed_connection_lost();
sync_crypto_write(&peer->cs, PEER_FD, take(msg));
}
/* Note: next_index is the index of the current commit we're working
@ -1999,8 +1987,7 @@ static void handle_funding_locked(struct peer *peer, const u8 *msg)
msg = towire_funding_locked(NULL,
&peer->channel_id,
&peer->next_local_per_commit);
if (!sync_crypto_write(&peer->cs, PEER_FD, take(msg)))
peer_failed_connection_lost();
sync_crypto_write(&peer->cs, PEER_FD, take(msg));
peer->funding_locked[LOCAL] = true;
}
@ -2047,8 +2034,7 @@ static void handle_offer_htlc(struct peer *peer, const u8 *inmsg)
peer->htlc_id, amount_msat,
&payment_hash, cltv_expiry,
onion_routing_packet);
if (!sync_crypto_write(&peer->cs, PEER_FD, take(msg)))
peer_failed_connection_lost();
sync_crypto_write(&peer->cs, PEER_FD, take(msg));
start_commit_timer(peer);
/* Tell the master. */
msg = towire_channel_offer_htlc_reply(NULL, peer->htlc_id,
@ -2412,10 +2398,8 @@ static void init_channel(struct peer *peer)
peer_reconnect(peer);
/* If we have a funding_signed message, send that immediately */
if (funding_signed) {
if (!sync_crypto_write(&peer->cs, PEER_FD, take(funding_signed)))
peer_failed_connection_lost();
}
if (funding_signed)
sync_crypto_write(&peer->cs, PEER_FD, take(funding_signed));
/* Reenable channel */
channel_announcement_negotiate(peer);

6
closingd/closing.c

@ -107,8 +107,7 @@ static void do_reconnect(struct crypto_state *cs,
msg = towire_channel_reestablish(NULL, channel_id,
next_index[LOCAL],
revocations_received);
if (!sync_crypto_write(cs, PEER_FD, take(msg)))
peer_failed_connection_lost();
sync_crypto_write(cs, PEER_FD, take(msg));
/* They might have already send reestablish, which triggered us */
while (!channel_reestablish) {
@ -189,8 +188,7 @@ static void send_offer(struct crypto_state *cs,
status_trace("sending fee offer %"PRIu64, fee_to_offer);
msg = towire_closing_signed(NULL, channel_id, fee_to_offer, &our_sig);
if (!sync_crypto_write(cs, PEER_FD, take(msg)))
peer_failed_connection_lost();
sync_crypto_write(cs, PEER_FD, take(msg));
}
static void tell_master_their_offer(const secp256k1_ecdsa_signature *their_sig,

18
common/crypto_sync.c

@ -2,6 +2,7 @@
#include <common/crypto_sync.h>
#include <common/cryptomsg.h>
#include <common/dev_disconnect.h>
#include <common/peer_failed.h>
#include <common/status.h>
#include <common/utils.h>
#include <errno.h>
@ -9,14 +10,13 @@
#include <wire/wire.h>
#include <wire/wire_sync.h>
bool sync_crypto_write(struct crypto_state *cs, int fd, const void *msg TAKES)
void sync_crypto_write(struct crypto_state *cs, int fd, const void *msg TAKES)
{
#if DEVELOPER
bool post_sabotage = false;
int type = fromwire_peektype(msg);
#endif
u8 *enc;
bool ret;
status_peer_io(LOG_IO_OUT, msg);
enc = cryptomsg_encrypt_msg(NULL, cs, msg);
@ -25,7 +25,7 @@ bool sync_crypto_write(struct crypto_state *cs, int fd, const void *msg TAKES)
switch (dev_disconnect(type)) {
case DEV_DISCONNECT_BEFORE:
dev_sabotage_fd(fd);
return false;
peer_failed_connection_lost();
case DEV_DISCONNECT_DROPPKT:
enc = tal_free(enc); /* FALL THRU */
case DEV_DISCONNECT_AFTER:
@ -38,14 +38,14 @@ bool sync_crypto_write(struct crypto_state *cs, int fd, const void *msg TAKES)
break;
}
#endif
ret = write_all(fd, enc, tal_count(enc));
if (!write_all(fd, enc, tal_count(enc)))
peer_failed_connection_lost();
tal_free(enc);
#if DEVELOPER
if (post_sabotage)
dev_sabotage_fd(fd);
#endif
return ret;
}
u8 *sync_crypto_read(const tal_t *ctx, struct crypto_state *cs, int fd)
@ -55,24 +55,24 @@ u8 *sync_crypto_read(const tal_t *ctx, struct crypto_state *cs, int fd)
if (!read_all(fd, hdr, sizeof(hdr))) {
status_trace("Failed reading header: %s", strerror(errno));
return NULL;
peer_failed_connection_lost();
}
if (!cryptomsg_decrypt_header(cs, hdr, &len)) {
status_trace("Failed hdr decrypt with rn=%"PRIu64, cs->rn-1);
return NULL;
peer_failed_connection_lost();
}
enc = tal_arr(ctx, u8, len + 16);
if (!read_all(fd, enc, tal_count(enc))) {
status_trace("Failed reading body: %s", strerror(errno));
return tal_free(enc);
peer_failed_connection_lost();
}
dec = cryptomsg_decrypt_body(ctx, cs, enc);
tal_free(enc);
if (!dec)
status_trace("Failed body decrypt with rn=%"PRIu64, cs->rn-2);
peer_failed_connection_lost();
else
status_peer_io(LOG_IO_IN, dec);

5
common/crypto_sync.h

@ -6,7 +6,10 @@
struct crypto_state;
bool sync_crypto_write(struct crypto_state *cs, int fd, const void *msg TAKES);
/* Exits with peer_failed_connection_lost() if write fails. */
void sync_crypto_write(struct crypto_state *cs, int fd, const void *msg TAKES);
/* Exits with peer_failed_connection_lost() if can't read packet. */
u8 *sync_crypto_read(const tal_t *ctx, struct crypto_state *cs, int fd);
#endif /* LIGHTNING_COMMON_CRYPTO_SYNC_H */

35
common/read_peer_msg.c

@ -37,8 +37,6 @@ u8 *peer_or_gossip_sync_read(const tal_t *ctx,
}
msg = sync_crypto_read(ctx, cs, peer_fd);
if (!msg)
peer_failed_connection_lost();
*from_gossipd = false;
return msg;
}
@ -84,7 +82,7 @@ bool is_wrong_channel(const u8 *msg, const struct channel_id *expected,
void handle_gossip_msg_(const u8 *msg TAKES, int peer_fd,
struct crypto_state *cs,
bool (*send_msg)(struct crypto_state *cs, int fd,
void (*send_msg)(struct crypto_state *cs, int fd,
const u8 *TAKES, void *arg),
void *arg)
{
@ -98,8 +96,7 @@ void handle_gossip_msg_(const u8 *msg TAKES, int peer_fd,
/* Gossipd can send us gossip messages, OR errors */
if (is_msg_for_gossipd(gossip)) {
if (!send_msg(cs, peer_fd, gossip, arg))
peer_failed_connection_lost();
send_msg(cs, peer_fd, gossip, arg);
} else if (fromwire_peektype(gossip) == WIRE_ERROR) {
status_debug("Gossipd told us to send error");
send_msg(cs, peer_fd, gossip, arg);
@ -144,11 +141,10 @@ bool handle_peer_gossip_or_error(int peer_fd, int gossip_fd,
status_trace("Rejecting %s for unknown channel_id %s",
wire_type_name(fromwire_peektype(msg)),
type_to_string(tmpctx, struct channel_id, &actual));
if (!sync_crypto_write(cs, peer_fd,
take(towire_errorfmt(NULL, &actual,
"Multiple channels"
" unsupported"))))
peer_failed_connection_lost();
sync_crypto_write(cs, peer_fd,
take(towire_errorfmt(NULL, &actual,
"Multiple channels"
" unsupported")));
goto handled;
}
@ -164,7 +160,7 @@ u8 *read_peer_msg_(const tal_t *ctx,
int peer_fd, int gossip_fd,
struct crypto_state *cs,
const struct channel_id *channel,
bool (*send_reply)(struct crypto_state *cs, int fd,
void (*send_reply)(struct crypto_state *cs, int fd,
const u8 *TAKES, void *arg),
void *arg)
{
@ -178,8 +174,6 @@ u8 *read_peer_msg_(const tal_t *ctx,
cs, &from_gossipd);
} else {
msg = sync_crypto_read(ctx, cs, peer_fd);
if (!msg)
peer_failed_connection_lost();
from_gossipd = false;
}
@ -210,12 +204,11 @@ u8 *read_peer_msg_(const tal_t *ctx,
status_trace("Rejecting %s for unknown channel_id %s",
wire_type_name(fromwire_peektype(msg)),
type_to_string(tmpctx, struct channel_id, &actual));
if (!send_reply(cs, peer_fd,
take(towire_errorfmt(NULL, &actual,
"Multiple channels"
" unsupported")),
arg))
peer_failed_connection_lost();
send_reply(cs, peer_fd,
take(towire_errorfmt(NULL, &actual,
"Multiple channels"
" unsupported")),
arg);
return tal_free(msg);
}
@ -223,8 +216,8 @@ u8 *read_peer_msg_(const tal_t *ctx,
}
/* Helper: sync_crypto_write, with extra args it ignores */
bool sync_crypto_write_arg(struct crypto_state *cs, int fd, const u8 *msg,
void sync_crypto_write_arg(struct crypto_state *cs, int fd, const u8 *msg,
void *unused UNUSED)
{
return sync_crypto_write(cs, fd, msg);
sync_crypto_write(cs, fd, msg);
}

10
common/read_peer_msg.h

@ -84,7 +84,7 @@ bool handle_peer_gossip_or_error(int peer_fd, int gossip_fd,
#define read_peer_msg(ctx, cs, chanid, send_reply, arg) \
read_peer_msg_((ctx), PEER_FD, GOSSIP_FD, (cs), \
(chanid), \
typesafe_cb_preargs(bool, void *, (send_reply), (arg), \
typesafe_cb_preargs(void, void *, (send_reply), (arg), \
struct crypto_state *, int, \
const u8 *), \
arg)
@ -93,13 +93,13 @@ bool handle_peer_gossip_or_error(int peer_fd, int gossip_fd,
#define read_peer_msg_nogossip(ctx, cs, chanid, send_reply, arg) \
read_peer_msg_((ctx), PEER_FD, -1, (cs), \
(chanid), \
typesafe_cb_preargs(bool, void *, (send_reply), (arg), \
typesafe_cb_preargs(void, void *, (send_reply), (arg), \
struct crypto_state *, int, \
const u8 *), \
arg)
/* Helper: sync_crypto_write, with extra args it ignores */
bool sync_crypto_write_arg(struct crypto_state *cs, int fd, const u8 *TAKES,
void sync_crypto_write_arg(struct crypto_state *cs, int fd, const u8 *TAKES,
void *unused);
/* Handler for a gossip msg; used by channeld since it queues them. */
@ -114,7 +114,7 @@ bool sync_crypto_write_arg(struct crypto_state *cs, int fd, const u8 *TAKES,
void handle_gossip_msg_(const u8 *msg TAKES,
int peer_fd,
struct crypto_state *cs,
bool (*send_msg)(struct crypto_state *cs, int fd,
void (*send_msg)(struct crypto_state *cs, int fd,
const u8 *TAKES, void *arg),
void *arg);
@ -122,7 +122,7 @@ u8 *read_peer_msg_(const tal_t *ctx,
int peer_fd, int gossip_fd,
struct crypto_state *cs,
const struct channel_id *channel,
bool (*send_reply)(struct crypto_state *cs, int fd,
void (*send_reply)(struct crypto_state *cs, int fd,
const u8 *TAKES, void *arg),
void *arg);

1
connectd/Makefile

@ -41,7 +41,6 @@ CONNECTD_COMMON_OBJS := \
common/bech32_util.o \
common/bip32.o \
common/crypto_state.o \
common/crypto_sync.o \
common/cryptomsg.o \
common/daemon.o \
common/daemon_conn.o \

1
gossipd/Makefile

@ -40,7 +40,6 @@ GOSSIPD_COMMON_OBJS := \
common/bech32_util.o \
common/bip32.o \
common/crypto_state.o \
common/crypto_sync.o \
common/cryptomsg.o \
common/daemon.o \
common/daemon_conn.o \

9
openingd/opening.c

@ -297,8 +297,7 @@ static u8 *funder_channel(struct state *state,
&ours->htlc,
&state->next_per_commit[LOCAL],
channel_flags);
if (!sync_crypto_write(&state->cs, PEER_FD, msg))
peer_failed_connection_lost();
sync_crypto_write(&state->cs, PEER_FD, msg);
state->remoteconf = tal(state, struct channel_config);
@ -458,8 +457,7 @@ static u8 *funder_channel(struct state *state,
&state->funding_txid,
state->funding_txout,
&sig);
if (!sync_crypto_write(&state->cs, PEER_FD, msg))
peer_failed_connection_lost();
sync_crypto_write(&state->cs, PEER_FD, msg);
/* BOLT #2:
*
@ -690,8 +688,7 @@ static u8 *fundee_channel(struct state *state,
&ours->htlc,
&state->next_per_commit[LOCAL]);
if (!sync_crypto_write(&state->cs, PEER_FD, take(msg)))
peer_failed_connection_lost();
sync_crypto_write(&state->cs, PEER_FD, take(msg));
peer_billboard(false,
"Incoming channel: accepted, now waiting for them to create funding tx");

Loading…
Cancel
Save