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 <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2018-07-09 20:47:59 +09:30 committed by Christian Decker
parent f9e5dc7ee8
commit ff6a6f8deb
3 changed files with 36 additions and 16 deletions

View File

@ -1,13 +1,11 @@
#include <bitcoin/pubkey.h> #include <bitcoin/pubkey.h>
#include <bitcoin/script.h> #include <bitcoin/script.h>
#include <ccan/fdpass/fdpass.h>
#include <channeld/gen_channel_wire.h> #include <channeld/gen_channel_wire.h>
#include <common/memleak.h> #include <common/memleak.h>
#include <common/timeout.h> #include <common/timeout.h>
#include <common/utils.h> #include <common/utils.h>
#include <errno.h> #include <errno.h>
#include <gossipd/gossip_constants.h> #include <gossipd/gossip_constants.h>
#include <hsmd/capabilities.h>
#include <hsmd/gen_hsm_client_wire.h> #include <hsmd/gen_hsm_client_wire.h>
#include <inttypes.h> #include <inttypes.h>
#include <lightningd/channel_control.h> #include <lightningd/channel_control.h>
@ -179,7 +177,7 @@ bool peer_start_channeld(struct channel *channel,
const u8 *funding_signed, const u8 *funding_signed,
bool reconnected) bool reconnected)
{ {
u8 *msg, *initmsg; u8 *initmsg;
int hsmfd; int hsmfd;
struct added_htlc *htlcs; struct added_htlc *htlcs;
enum htlc_state *htlc_states; enum htlc_state *htlc_states;
@ -193,19 +191,9 @@ bool peer_start_channeld(struct channel *channel,
const struct config *cfg = &ld->config; const struct config *cfg = &ld->config;
bool reached_announce_depth; bool reached_announce_depth;
msg = towire_hsm_client_hsmfd(tmpctx, &channel->peer->id, hsmfd = hsm_get_client_fd(ld, &channel->peer->id,
channel->dbid, channel->dbid,
HSM_CAP_SIGN_GOSSIP | HSM_CAP_ECDH); 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));
channel_set_owner(channel, channel_set_owner(channel,
new_channel_subd(ld, new_channel_subd(ld,

View File

@ -2,6 +2,7 @@
#include "lightningd.h" #include "lightningd.h"
#include "subd.h" #include "subd.h"
#include <ccan/err/err.h> #include <ccan/err/err.h>
#include <ccan/fdpass/fdpass.h>
#include <ccan/io/io.h> #include <ccan/io/io.h>
#include <ccan/take/take.h> #include <ccan/take/take.h>
#include <common/status.h> #include <common/status.h>
@ -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) void hsm_init(struct lightningd *ld)
{ {
u8 *msg; u8 *msg;

View File

@ -3,9 +3,17 @@
#include "config.h" #include "config.h"
#include <ccan/short_types/short_types.h> #include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h> #include <ccan/tal/tal.h>
#include <hsmd/capabilities.h>
#include <stdbool.h> #include <stdbool.h>
struct lightningd; 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); u8 *hsm_sync_read(const tal_t *ctx, struct lightningd *ld);
void hsm_init(struct lightningd *ld); void hsm_init(struct lightningd *ld);