From 6bfc3d8692fe61e0ac84bd36fb26e773ec828044 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sat, 8 Jan 2022 23:50:29 +1030 Subject: [PATCH] gossipwith: create our own internal sync_crypto functions. This avoids changes to crypto_sync which are coming in next patch. Signed-off-by: Rusty Russell --- devtools/Makefile | 2 +- devtools/gossipwith.c | 86 ++++++++++++++++++++++++++----------------- 2 files changed, 54 insertions(+), 34 deletions(-) diff --git a/devtools/Makefile b/devtools/Makefile index b4c23f03a..e61f928f1 100644 --- a/devtools/Makefile +++ b/devtools/Makefile @@ -70,7 +70,7 @@ devtools/onion: $(DEVTOOLS_COMMON_OBJS) $(JSMN_OBJS) $(CCAN_OBJS) $(BITCOIN_OBJS devtools/blindedpath: $(DEVTOOLS_COMMON_OBJS) $(JSMN_OBJS) $(CCAN_OBJS) common/blinding.o $(BITCOIN_OBJS) wire/fromwire.o wire/towire.o devtools/blindedpath.o common/onion.o common/onionreply.o common/sphinx.o -devtools/gossipwith: $(DEVTOOLS_COMMON_OBJS) $(JSMN_OBJS) $(CCAN_OBJS) $(BITCOIN_OBJS) wire/fromwire.o wire/towire.o wire/peer$(EXP)_wiregen.o devtools/gossipwith.o common/cryptomsg.o common/cryptomsg.o common/crypto_sync.o +devtools/gossipwith: $(DEVTOOLS_COMMON_OBJS) $(JSMN_OBJS) $(CCAN_OBJS) $(BITCOIN_OBJS) wire/fromwire.o wire/towire.o wire/peer$(EXP)_wiregen.o devtools/gossipwith.o common/cryptomsg.o common/cryptomsg.o $(DEVTOOLS_OBJS) $(DEVTOOLS_TOOL_OBJS): wire/wire.h diff --git a/devtools/gossipwith.c b/devtools/gossipwith.c index 66956ca66..3e7f0481a 100644 --- a/devtools/gossipwith.c +++ b/devtools/gossipwith.c @@ -9,12 +9,12 @@ #include #include #include -#include -#include +#include #include #include #include #include +#include #include #include #include @@ -69,23 +69,6 @@ void status_fmt(enum log_level level, { } -#if DEVELOPER -void dev_sabotage_fd(int fd, bool close_fd) -{ - abort(); -} - -void dev_blackhole_fd(int fd) -{ - abort(); -} - -enum dev_disconnect dev_disconnect(int pkt_type) -{ - return DEV_DISCONNECT_NORMAL; -} -#endif - static char *opt_set_network(const char *arg, void *unused) { assert(arg != NULL); @@ -102,11 +85,6 @@ static void opt_show_network(char buf[OPT_SHOW_LEN], const void *unused) snprintf(buf, OPT_SHOW_LEN, "%s", chainparams->network_name); } -void peer_failed_connection_lost(void) -{ - exit(0); -} - void ecdh(const struct pubkey *point, struct secret *ss) { if (secp256k1_ecdh(secp256k1_ctx, ss->data, &point->pubkey, @@ -140,18 +118,60 @@ static struct io_plan *simple_read(struct io_conn *conn, return next(conn, next_arg); } +static void sync_crypto_write(int peer_fd, struct crypto_state *cs, const void *msg TAKES) +{ + u8 *enc; + + enc = cryptomsg_encrypt_msg(NULL, cs, msg); + + if (!write_all(peer_fd, enc, tal_count(enc))) + exit(1); + tal_free(enc); +} + +static u8 *sync_crypto_read(const tal_t *ctx, int peer_fd, struct crypto_state *cs) +{ + u8 hdr[18], *enc, *dec; + u16 len; + + if (!read_all(peer_fd, hdr, sizeof(hdr))) { + status_debug("Failed reading header: %s", strerror(errno)); + exit(0); + } + + if (!cryptomsg_decrypt_header(cs, hdr, &len)) { + status_debug("Failed hdr decrypt with rn=%"PRIu64, + cs->rn-1); + exit(1); + } + + enc = tal_arr(ctx, u8, len + 16); + if (!read_all(peer_fd, enc, tal_count(enc))) { + status_debug("Failed reading body: %s", strerror(errno)); + exit(1); + } + + dec = cryptomsg_decrypt_body(ctx, cs, enc); + tal_free(enc); + if (!dec) + exit(1); + else + status_peer_io(LOG_IO_IN, NULL, dec); + + return dec; +} + static struct io_plan *handshake_success(struct io_conn *conn, const struct pubkey *them, const struct wireaddr_internal *addr, - struct crypto_state *orig_cs, + struct crypto_state *cs, struct oneshot *timer, char **args) { u8 *msg; - struct per_peer_state *pps = new_per_peer_state(conn, orig_cs); + int peer_fd = io_conn_fd(conn); struct pollfd pollfd[2]; - pps->peer_fd = io_conn_fd(conn); if (initial_sync) set_feature_bit(&features, OPTIONAL_FEATURE(OPT_INITIAL_ROUTING_SYNC)); @@ -165,9 +185,9 @@ static struct io_plan *handshake_success(struct io_conn *conn, } msg = towire_init(NULL, NULL, features, tlvs); - sync_crypto_write(pps, take(msg)); + sync_crypto_write(peer_fd, cs, take(msg)); /* Ignore their init message. */ - tal_free(sync_crypto_read(NULL, pps)); + tal_free(sync_crypto_read(NULL, peer_fd, cs)); tal_free(tlvs); } @@ -176,14 +196,14 @@ static struct io_plan *handshake_success(struct io_conn *conn, else pollfd[0].fd = -1; pollfd[0].events = POLLIN; - pollfd[1].fd = pps->peer_fd; + pollfd[1].fd = peer_fd; pollfd[1].events = POLLIN; while (*args) { u8 *m = tal_hexdata(NULL, *args, strlen(*args)); if (!m) errx(1, "Invalid hexdata '%s'", *args); - sync_crypto_write(pps, take(m)); + sync_crypto_write(peer_fd, cs, take(m)); args++; } @@ -204,10 +224,10 @@ static struct io_plan *handshake_success(struct io_conn *conn, if (!read_all(STDIN_FILENO, msg, tal_bytelen(msg))) err(1, "Only read partial message"); - sync_crypto_write(pps, take(msg)); + sync_crypto_write(peer_fd, cs, take(msg)); } } else if (pollfd[1].revents & POLLIN) { - msg = sync_crypto_read(NULL, pps); + msg = sync_crypto_read(NULL, peer_fd, cs); if (!msg) err(1, "Reading msg"); if (hex) {