diff --git a/channeld/Makefile b/channeld/Makefile index 4b660bb64..54b783856 100644 --- a/channeld/Makefile +++ b/channeld/Makefile @@ -40,7 +40,6 @@ CHANNELD_COMMON_OBJS := \ common/channel_config.o \ common/channel_id.o \ common/channel_type.o \ - common/crypto_state.o \ common/cryptomsg.o \ common/daemon.o \ common/daemon_conn.o \ diff --git a/closingd/Makefile b/closingd/Makefile index d641d7857..c9fc0570c 100644 --- a/closingd/Makefile +++ b/closingd/Makefile @@ -26,7 +26,6 @@ CLOSINGD_COMMON_OBJS := \ common/bip32.o \ common/channel_id.o \ common/close_tx.o \ - common/crypto_state.o \ common/cryptomsg.o \ common/daemon.o \ common/daemon_conn.o \ diff --git a/common/Makefile b/common/Makefile index d4f9ae8d3..9c8dbfe39 100644 --- a/common/Makefile +++ b/common/Makefile @@ -22,7 +22,6 @@ COMMON_SRC_NOGEN := \ common/close_tx.c \ common/coin_mvt.c \ common/configdir.c \ - common/crypto_state.c \ common/cryptomsg.c \ common/daemon.c \ common/daemon_conn.c \ @@ -97,6 +96,7 @@ COMMON_SRC_GEN := common/status_wiregen.c common/peer_status_wiregen.c COMMON_HEADERS_NOGEN := $(COMMON_SRC_NOGEN:.c=.h) \ common/closing_fee.h \ + common/crypto_state.h \ common/ecdh.h \ common/errcode.h \ common/gossip_constants.h \ diff --git a/common/crypto_state.c b/common/crypto_state.c deleted file mode 100644 index d592ddfd3..000000000 --- a/common/crypto_state.c +++ /dev/null @@ -1,23 +0,0 @@ -#include "config.h" -#include -#include - -void towire_crypto_state(u8 **ptr, const struct crypto_state *cs) -{ - towire_u64(ptr, cs->rn); - towire_u64(ptr, cs->sn); - towire_secret(ptr, &cs->sk); - towire_secret(ptr, &cs->rk); - towire_secret(ptr, &cs->s_ck); - towire_secret(ptr, &cs->r_ck); -} - -void fromwire_crypto_state(const u8 **ptr, size_t *max, struct crypto_state *cs) -{ - cs->rn = fromwire_u64(ptr, max); - cs->sn = fromwire_u64(ptr, max); - fromwire_secret(ptr, max, &cs->sk); - fromwire_secret(ptr, max, &cs->rk); - fromwire_secret(ptr, max, &cs->s_ck); - fromwire_secret(ptr, max, &cs->r_ck); -} diff --git a/common/crypto_state.h b/common/crypto_state.h index c2fa658c8..8a2674719 100644 --- a/common/crypto_state.h +++ b/common/crypto_state.h @@ -12,7 +12,4 @@ struct crypto_state { struct secret s_ck, r_ck; }; -void towire_crypto_state(u8 **pptr, const struct crypto_state *cs); -void fromwire_crypto_state(const u8 **ptr, size_t *max, struct crypto_state *cs); - #endif /* LIGHTNING_COMMON_CRYPTO_STATE_H */ diff --git a/common/per_peer_state.c b/common/per_peer_state.c index 18b2472ff..de11384e2 100644 --- a/common/per_peer_state.c +++ b/common/per_peer_state.c @@ -19,12 +19,10 @@ static void destroy_per_peer_state(struct per_peer_state *pps) close(pps->gossip_store_fd); } -struct per_peer_state *new_per_peer_state(const tal_t *ctx, - const struct crypto_state *cs) +struct per_peer_state *new_per_peer_state(const tal_t *ctx) { struct per_peer_state *pps = tal(ctx, struct per_peer_state); - pps->cs = *cs; pps->gs = NULL; pps->peer_fd = pps->gossip_fd = pps->gossip_store_fd = -1; pps->grf = new_gossip_rcvd_filter(pps); @@ -69,7 +67,6 @@ void fromwire_gossip_state(const u8 **cursor, size_t *max, void towire_per_peer_state(u8 **pptr, const struct per_peer_state *pps) { - towire_crypto_state(pptr, &pps->cs); towire_bool(pptr, pps->gs != NULL); if (pps->gs) towire_gossip_state(pptr, pps->gs); @@ -89,11 +86,9 @@ void per_peer_state_fdpass_send(int fd, const struct per_peer_state *pps) struct per_peer_state *fromwire_per_peer_state(const tal_t *ctx, const u8 **cursor, size_t *max) { - struct crypto_state cs; struct per_peer_state *pps; - fromwire_crypto_state(cursor, max, &cs); - pps = new_per_peer_state(ctx, &cs); + pps = new_per_peer_state(ctx); if (fromwire_bool(cursor, max)) { pps->gs = tal(pps, struct gossip_state); fromwire_gossip_state(cursor, max, pps->gs); diff --git a/common/per_peer_state.h b/common/per_peer_state.h index 45bd1f172..e5e3b914c 100644 --- a/common/per_peer_state.h +++ b/common/per_peer_state.h @@ -15,9 +15,6 @@ struct gossip_state { /* Things we hand between daemons to talk to peers. */ struct per_peer_state { - /* Cryptographic state needed to exchange messages with the peer (as - * featured in BOLT #8) */ - struct crypto_state cs; /* NULL if it's not initialized yet */ struct gossip_state *gs; /* Cache of msgs we have received, to avoid re-xmitting from store */ @@ -28,8 +25,7 @@ struct per_peer_state { /* Allocate a new per-peer state and add destructor to close fds if set; * sets fds to -1 and ->gs to NULL.. */ -struct per_peer_state *new_per_peer_state(const tal_t *ctx, - const struct crypto_state *cs); +struct per_peer_state *new_per_peer_state(const tal_t *ctx); /* Initialize the fds (must be -1 previous) */ void per_peer_state_set_fds(struct per_peer_state *pps, diff --git a/connectd/Makefile b/connectd/Makefile index 343a3624c..a6de25985 100644 --- a/connectd/Makefile +++ b/connectd/Makefile @@ -42,7 +42,6 @@ CONNECTD_COMMON_OBJS := \ common/bigsize.o \ common/bip32.o \ common/channel_id.o \ - common/crypto_state.o \ common/cryptomsg.o \ common/daemon.o \ common/daemon_conn.o \ diff --git a/connectd/connectd.c b/connectd/connectd.c index 2dc053923..fdbdefa26 100644 --- a/connectd/connectd.c +++ b/connectd/connectd.c @@ -448,7 +448,7 @@ static struct peer *new_peer(struct daemon *daemon, struct peer *peer = tal(daemon, struct peer); peer->id = *id; - peer->pps = new_per_peer_state(peer, cs); + peer->cs = *cs; peer->final_msg = NULL; peer->subd_in = NULL; peer->peer_in = NULL; @@ -461,12 +461,6 @@ static struct peer *new_peer(struct daemon *daemon, if (!multiplex_subd_setup(peer, fd_for_subd)) return tal_free(peer); - /* If gossipd can't give us a file descriptor, we give up connecting. */ - if (!get_gossipfds(daemon, id, their_features, peer->pps)) { - close(*fd_for_subd); - return tal_free(peer); - } - peer->to_peer = tal_steal(peer, conn); peer_htable_add(&daemon->peers, peer); tal_add_destructor2(peer, destroy_peer, daemon); @@ -488,6 +482,7 @@ struct io_plan *peer_connected(struct io_conn *conn, int unsup; size_t depender, missing; int subd_fd; + struct per_peer_state *pps; peer = peer_htable_get(&daemon->peers, id); if (peer) @@ -545,20 +540,28 @@ struct io_plan *peer_connected(struct io_conn *conn, if (!peer) return io_close(conn); + pps = new_per_peer_state(tmpctx); + + /* If gossipd can't give us a file descriptor, we give up connecting. */ + if (!get_gossipfds(daemon, id, their_features, pps)) { + close(subd_fd); + return tal_free(peer); + } + /* Create message to tell master peer has connected. */ msg = towire_connectd_peer_connected(NULL, id, addr, incoming, - peer->pps, their_features); + pps, their_features); /*~ daemon_conn is a message queue for inter-daemon communication: we * queue up the `connect_peer_connected` message to tell lightningd * we have connected, and give the peer and gossip fds. */ daemon_conn_send(daemon->master, take(msg)); daemon_conn_send_fd(daemon->master, subd_fd); - daemon_conn_send_fd(daemon->master, peer->pps->gossip_fd); - daemon_conn_send_fd(daemon->master, peer->pps->gossip_store_fd); + daemon_conn_send_fd(daemon->master, pps->gossip_fd); + daemon_conn_send_fd(daemon->master, pps->gossip_store_fd); /* Don't try to close these on freeing. */ - peer->pps->gossip_store_fd = peer->pps->gossip_fd = -1; + pps->gossip_store_fd = pps->gossip_fd = -1; /*~ Now we set up this connection to read/write from subd */ return multiplex_peer_setup(conn, peer); diff --git a/connectd/multiplex.c b/connectd/multiplex.c index fd7668b3b..349f76111 100644 --- a/connectd/multiplex.c +++ b/connectd/multiplex.c @@ -39,7 +39,7 @@ static struct io_plan *encrypt_and_send(struct peer *peer, struct peer *peer)) { /* We free this and the encrypted version in next write_to_peer */ - peer->sent_to_peer = cryptomsg_encrypt_msg(peer, &peer->pps->cs, msg); + peer->sent_to_peer = cryptomsg_encrypt_msg(peer, &peer->cs, msg); return io_write(peer->to_peer, peer->sent_to_peer, tal_bytelen(peer->sent_to_peer), @@ -127,7 +127,7 @@ static struct io_plan *read_body_from_peer_done(struct io_conn *peer_conn, { u8 *decrypted; - decrypted = cryptomsg_decrypt_body(NULL, &peer->pps->cs, + decrypted = cryptomsg_decrypt_body(NULL, &peer->cs, peer->peer_in); if (!decrypted) return io_close(peer_conn); @@ -145,7 +145,7 @@ static struct io_plan *read_body_from_peer(struct io_conn *peer_conn, { u16 len; - if (!cryptomsg_decrypt_header(&peer->pps->cs, peer->peer_in, &len)) + if (!cryptomsg_decrypt_header(&peer->cs, peer->peer_in, &len)) return io_close(peer_conn); tal_resize(&peer->peer_in, (u32)len + CRYPTOMSG_BODY_OVERHEAD); diff --git a/connectd/multiplex.h b/connectd/multiplex.h index e96982bf8..12ccfa082 100644 --- a/connectd/multiplex.h +++ b/connectd/multiplex.h @@ -2,12 +2,14 @@ #define LIGHTNING_CONNECTD_MULTIPLEX_H #include "config.h" #include +#include #include #include struct peer { struct node_id id; - struct per_peer_state *pps; + /* Counters and keys for symmetric crypto */ + struct crypto_state cs; /* Connection to the peer */ struct io_conn *to_peer; diff --git a/devtools/Makefile b/devtools/Makefile index e61f928f1..5498db468 100644 --- a/devtools/Makefile +++ b/devtools/Makefile @@ -21,7 +21,6 @@ DEVTOOLS_COMMON_OBJS := \ common/bolt11.o \ common/blockheight_states.o \ common/channel_id.o \ - common/crypto_state.o \ common/decode_array.o \ common/features.o \ common/fee_states.o \ diff --git a/gossipd/Makefile b/gossipd/Makefile index f31acfe92..13c8eada0 100644 --- a/gossipd/Makefile +++ b/gossipd/Makefile @@ -37,7 +37,6 @@ GOSSIPD_COMMON_OBJS := \ common/blinding.o \ common/blindedpath.o \ common/channel_id.o \ - common/crypto_state.o \ common/cryptomsg.o \ common/daemon.o \ common/daemon_conn.o \ diff --git a/lightningd/Makefile b/lightningd/Makefile index 2cdb6b0d0..24c57250a 100644 --- a/lightningd/Makefile +++ b/lightningd/Makefile @@ -81,7 +81,6 @@ LIGHTNINGD_COMMON_OBJS := \ common/channel_type.o \ common/coin_mvt.o \ common/configdir.o \ - common/crypto_state.o \ common/daemon.o \ common/derive_basepoints.o \ common/ecdh_hsmd.o \ diff --git a/openingd/Makefile b/openingd/Makefile index 0ff843e47..6cbf12546 100644 --- a/openingd/Makefile +++ b/openingd/Makefile @@ -41,7 +41,6 @@ OPENINGD_COMMON_OBJS := \ common/channel_config.o \ common/channel_id.o \ common/channel_type.o \ - common/crypto_state.o \ common/cryptomsg.o \ common/daemon.o \ common/daemon_conn.o \