From ff6a6f8deb99f027255ff65cae7241ee940d7b97 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 9 Jul 2018 20:47:59 +0930 Subject: [PATCH] lightningd: create hsm_get_client_fd() helper. We're going to use this more when we hand hsm fds to openingd, onchaind and closingd. Signed-off-by: Rusty Russell --- lightningd/channel_control.c | 20 ++++---------------- lightningd/hsm_control.c | 24 ++++++++++++++++++++++++ lightningd/hsm_control.h | 8 ++++++++ 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/lightningd/channel_control.c b/lightningd/channel_control.c index 6c14977ca..c0a6bedf7 100644 --- a/lightningd/channel_control.c +++ b/lightningd/channel_control.c @@ -1,13 +1,11 @@ #include #include -#include #include #include #include #include #include #include -#include #include #include #include @@ -179,7 +177,7 @@ bool peer_start_channeld(struct channel *channel, const u8 *funding_signed, bool reconnected) { - u8 *msg, *initmsg; + u8 *initmsg; int hsmfd; struct added_htlc *htlcs; enum htlc_state *htlc_states; @@ -193,19 +191,9 @@ bool peer_start_channeld(struct channel *channel, const struct config *cfg = &ld->config; bool reached_announce_depth; - msg = towire_hsm_client_hsmfd(tmpctx, &channel->peer->id, - channel->dbid, - HSM_CAP_SIGN_GOSSIP | HSM_CAP_ECDH); - if (!wire_sync_write(ld->hsm_fd, take(msg))) - fatal("Could not write to HSM: %s", strerror(errno)); - - msg = hsm_sync_read(tmpctx, ld); - if (!fromwire_hsm_client_hsmfd_reply(msg)) - fatal("Bad reply from HSM: %s", tal_hex(tmpctx, msg)); - - hsmfd = fdpass_recv(ld->hsm_fd); - if (hsmfd < 0) - fatal("Could not read fd from HSM: %s", strerror(errno)); + hsmfd = hsm_get_client_fd(ld, &channel->peer->id, + channel->dbid, + HSM_CAP_SIGN_GOSSIP | HSM_CAP_ECDH); channel_set_owner(channel, new_channel_subd(ld, diff --git a/lightningd/hsm_control.c b/lightningd/hsm_control.c index 7ae168620..160278a36 100644 --- a/lightningd/hsm_control.c +++ b/lightningd/hsm_control.c @@ -2,6 +2,7 @@ #include "lightningd.h" #include "subd.h" #include +#include #include #include #include @@ -30,6 +31,29 @@ u8 *hsm_sync_read(const tal_t *ctx, struct lightningd *ld) } } +int hsm_get_client_fd(struct lightningd *ld, + const struct pubkey *id, + u64 dbid, + int capabilities) +{ + int hsm_fd; + u8 *msg; + + assert(dbid); + msg = towire_hsm_client_hsmfd(NULL, id, dbid, capabilities); + if (!wire_sync_write(ld->hsm_fd, take(msg))) + fatal("Could not write to HSM: %s", strerror(errno)); + + msg = hsm_sync_read(tmpctx, ld); + if (!fromwire_hsm_client_hsmfd_reply(msg)) + fatal("Bad reply from HSM: %s", tal_hex(tmpctx, msg)); + + hsm_fd = fdpass_recv(ld->hsm_fd); + if (hsm_fd < 0) + fatal("Could not read fd from HSM: %s", strerror(errno)); + return hsm_fd; +} + void hsm_init(struct lightningd *ld) { u8 *msg; diff --git a/lightningd/hsm_control.h b/lightningd/hsm_control.h index dcad2df45..c820309ad 100644 --- a/lightningd/hsm_control.h +++ b/lightningd/hsm_control.h @@ -3,9 +3,17 @@ #include "config.h" #include #include +#include #include struct lightningd; +struct pubkey; + +/* Ask HSM for a new fd for a subdaemon to use. */ +int hsm_get_client_fd(struct lightningd *ld, + const struct pubkey *id, + u64 dbid, + int capabilities); u8 *hsm_sync_read(const tal_t *ctx, struct lightningd *ld); void hsm_init(struct lightningd *ld);