Skip to content

Commit

Permalink
per_peer_state: remove struct crypto_state
Browse files Browse the repository at this point in the history
Now that connectd does the crypto, no need to hand around crypto_state.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Jan 20, 2022
1 parent ce8b694 commit 9c0bb44
Show file tree
Hide file tree
Showing 15 changed files with 24 additions and 61 deletions.
1 change: 0 additions & 1 deletion channeld/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
1 change: 0 additions & 1 deletion closingd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
2 changes: 1 addition & 1 deletion common/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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 \
Expand Down
23 changes: 0 additions & 23 deletions common/crypto_state.c

This file was deleted.

3 changes: 0 additions & 3 deletions common/crypto_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
9 changes: 2 additions & 7 deletions common/per_peer_state.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
6 changes: 1 addition & 5 deletions common/per_peer_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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,
Expand Down
1 change: 0 additions & 1 deletion connectd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
25 changes: 14 additions & 11 deletions connectd/connectd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions connectd/multiplex.c
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion connectd/multiplex.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
#define LIGHTNING_CONNECTD_MULTIPLEX_H
#include "config.h"
#include <ccan/short_types/short_types.h>
#include <common/crypto_state.h>
#include <common/msg_queue.h>
#include <common/node_id.h>

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;
Expand Down
1 change: 0 additions & 1 deletion devtools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
1 change: 0 additions & 1 deletion gossipd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
1 change: 0 additions & 1 deletion lightningd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
1 change: 0 additions & 1 deletion openingd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down

0 comments on commit 9c0bb44

Please sign in to comment.