From e467afce06b984d43ac849979a6c65293c0a3ff7 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 10 Mar 2017 21:21:43 +1030 Subject: [PATCH] lightningd/handshake: convert to subd. Signed-off-by: Rusty Russell --- lightningd/handshake/Makefile | 17 +++------ lightningd/handshake/handshake.c | 33 ++++++---------- .../handshake/handshake_control_wire_csv | 12 ------ ...ake_status_wire_csv => handshake_wire.csv} | 30 +++++++++++---- lightningd/peer_control.c | 38 +++++++++---------- 5 files changed, 57 insertions(+), 73 deletions(-) delete mode 100644 lightningd/handshake/handshake_control_wire_csv rename lightningd/handshake/{handshake_status_wire_csv => handshake_wire.csv} (55%) diff --git a/lightningd/handshake/Makefile b/lightningd/handshake/Makefile index 3a4cff2e2..53e56a790 100644 --- a/lightningd/handshake/Makefile +++ b/lightningd/handshake/Makefile @@ -10,8 +10,7 @@ lightningd/handshake-all: lightningd/lightningd_handshake # lightningd/handshake needs these: LIGHTNINGD_HANDSHAKE_HEADERS := \ - lightningd/handshake/gen_handshake_control_wire.h \ - lightningd/handshake/gen_handshake_status_wire.h + lightningd/handshake/gen_handshake_wire.h LIGHTNINGD_HANDSHAKE_SRC := lightningd/handshake/handshake.c \ $(LIGHTNINGD_HANDSHAKE_HEADERS:.h=.c) LIGHTNINGD_HANDSHAKE_OBJS := $(LIGHTNINGD_HANDSHAKE_SRC:.c=.o) @@ -30,17 +29,11 @@ LIGHTNINGD_HEADERS_GEN += $(LIGHTNINGD_HANDSHAKE_HEADERS) $(LIGHTNINGD_HANDSHAKE_OBJS): $(LIGHTNINGD_HEADERS) -lightningd/handshake/gen_handshake_control_wire.h: $(WIRE_GEN) lightningd/handshake/handshake_control_wire_csv - $(WIRE_GEN) --header $@ handshake_control_wire_type < lightningd/handshake/handshake_control_wire_csv > $@ +lightningd/handshake/gen_handshake_wire.h: $(WIRE_GEN) lightningd/handshake/handshake_wire.csv + $(WIRE_GEN) --header $@ handshake_wire_type < lightningd/handshake/handshake_wire.csv > $@ -lightningd/handshake/gen_handshake_control_wire.c: $(WIRE_GEN) lightningd/handshake/handshake_control_wire_csv - $(WIRE_GEN) ${@:.c=.h} handshake_control_wire_type < lightningd/handshake/handshake_control_wire_csv > $@ - -lightningd/handshake/gen_handshake_status_wire.h: $(WIRE_GEN) lightningd/handshake/handshake_status_wire_csv - $(WIRE_GEN) --header $@ handshake_status_wire_type < lightningd/handshake/handshake_status_wire_csv > $@ - -lightningd/handshake/gen_handshake_status_wire.c: $(WIRE_GEN) lightningd/handshake/handshake_status_wire_csv - $(WIRE_GEN) ${@:.c=.h} handshake_status_wire_type < lightningd/handshake/handshake_status_wire_csv > $@ +lightningd/handshake/gen_handshake_wire.c: $(WIRE_GEN) lightningd/handshake/handshake_wire.csv + $(WIRE_GEN) ${@:.c=.h} handshake_wire_type < lightningd/handshake/handshake_wire.csv > $@ LIGHTNINGD_HANDSHAKE_OBJS := $(LIGHTNINGD_HANDSHAKE_SRC:.c=.o) $(LIGHTNINGD_HANDSHAKE_GEN_SRC:.c=.o) diff --git a/lightningd/handshake/handshake.c b/lightningd/handshake/handshake.c index 93c89e4db..57817defe 100644 --- a/lightningd/handshake/handshake.c +++ b/lightningd/handshake/handshake.c @@ -11,8 +11,7 @@ #include #include #include -#include -#include +#include #include #include #include @@ -24,8 +23,6 @@ #include #include -/* Stdout == status, stdin == requests, 3 == hsmfd */ -#define STATUS_FD STDOUT_FILENO #define REQ_FD STDIN_FILENO /* Representing chacha keys and ecdh results we derive them from; @@ -982,48 +979,40 @@ int main(int argc, char *argv[]) subdaemon_debug(argc, argv); secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN); - status_setup(STATUS_FD); + status_setup(REQ_FD); hsm_setup(hsmfd); msg = wire_sync_read(NULL, REQ_FD); if (!msg) status_failed(WIRE_HANDSHAKE_BAD_COMMAND, "%s", strerror(errno)); - if (clientfd < 0) - status_failed(WIRE_HANDSHAKE_BAD_FDPASS, "%s", strerror(errno)); - if (fromwire_handshake_responder_req(msg, NULL, &my_id)) { + if (fromwire_handshake_responder(msg, NULL, &my_id)) { responder(clientfd, &my_id, &their_id, &ck, &sk, &rk); cs.rn = cs.sn = 0; cs.sk = sk.s; cs.rk = rk.s; cs.r_ck = cs.s_ck = ck.s; wire_sync_write(REQ_FD, - towire_handshake_responder_resp(msg, - &their_id, - &cs)); - } else if (fromwire_handshake_initiator_req(msg, NULL, &my_id, - &their_id)) { + towire_handshake_responder_reply(msg, + &their_id, + &cs)); + } else if (fromwire_handshake_initiator(msg, NULL, &my_id, + &their_id)) { initiator(clientfd, &my_id, &their_id, &ck, &sk, &rk); cs.rn = cs.sn = 0; cs.sk = sk.s; cs.rk = rk.s; cs.r_ck = cs.s_ck = ck.s; wire_sync_write(REQ_FD, - towire_handshake_initiator_resp(msg, &cs)); + towire_handshake_initiator_reply(msg, &cs)); } else - status_failed(WIRE_HANDSHAKE_BAD_COMMAND, "%i", fromwire_peektype(msg)); + status_failed(WIRE_HANDSHAKE_BAD_COMMAND, "%i", + fromwire_peektype(msg)); /* Hand back the fd. */ fdpass_send(REQ_FD, clientfd); - /* Wait for exit command (avoid status close being read before reqfd) */ - msg = wire_sync_read(msg, REQ_FD); - if (!msg) - status_failed(WIRE_HANDSHAKE_BAD_COMMAND, "%s", strerror(errno)); - if (!fromwire_handshake_exit_req(msg, NULL)) - status_failed(WIRE_HANDSHAKE_BAD_COMMAND, "Expected exit req not %i", - fromwire_peektype(msg)); tal_free(msg); return 0; } diff --git a/lightningd/handshake/handshake_control_wire_csv b/lightningd/handshake/handshake_control_wire_csv deleted file mode 100644 index 6092005f8..000000000 --- a/lightningd/handshake/handshake_control_wire_csv +++ /dev/null @@ -1,12 +0,0 @@ -#include -handshake_responder_req,0 -handshake_responder_req,0,my_id,33 -handshake_responder_resp,1 -handshake_responder_resp,0,initiator_id,33 -handshake_responder_resp,33,cs,144,struct crypto_state -handshake_initiator_req,2 -handshake_initiator_req,0,my_id,33 -handshake_initiator_req,33,responder_id,33 -handshake_initiator_resp,3 -handshake_initiator_resp,0,cs,144,struct crypto_state -handshake_exit_req,4 diff --git a/lightningd/handshake/handshake_status_wire_csv b/lightningd/handshake/handshake_wire.csv similarity index 55% rename from lightningd/handshake/handshake_status_wire_csv rename to lightningd/handshake/handshake_wire.csv index 8d36a2001..2a46dee57 100644 --- a/lightningd/handshake/handshake_status_wire_csv +++ b/lightningd/handshake/handshake_wire.csv @@ -1,5 +1,5 @@ +#include handshake_bad_command,0x8000 -handshake_bad_fdpass,0x8001 initr_act1_bad_ecdh_for_ss,0x8011 initr_act1_write_failed,0x8012 initr_act2_read_failed,0x8013 @@ -22,10 +22,26 @@ respr_act3_bad_ciphertext,0x8023 respr_act3_bad_pubkey,0x8024 respr_act3_bad_ecdh_for_ss,0x8025 respr_act3_bad_tag,0x8026 -initr_act_one,1 -initr_act_two,2 -initr_act_three,3 -respr_act_one,4 -respr_act_two,5 -respr_act_three,6 + +# FIXME: This is probably too finegrained. +initr_act_one,1001 +initr_act_two,1002 +initr_act_three,1003 +respr_act_one,1011 +respr_act_two,1012 +respr_act_three,1013 success,0 + +handshake_responder,1 +handshake_responder,1,my_id,33 +handshake_responder_reply,101 +handshake_responder_reply,0,initiator_id,33 +handshake_responder_reply,33,cs,144,struct crypto_state + +handshake_initiator,2 +handshake_initiator,0,my_id,33 +handshake_initiator,33,responder_id,33 + +handshake_initiator_reply,102 +handshake_initiator_reply,0,cs,144,struct crypto_state + diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 190de5bc3..b3098db28 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -20,8 +20,7 @@ #include #include #include -#include -#include +#include #include #include #include @@ -111,7 +110,7 @@ struct peer *peer_by_id(struct lightningd *ld, const struct pubkey *id) return NULL; } -static void handshake_succeeded(struct subdaemon *hs, const u8 *msg, +static bool handshake_succeeded(struct subd *hs, const u8 *msg, struct peer *peer) { struct crypto_state cs; @@ -119,13 +118,13 @@ static void handshake_succeeded(struct subdaemon *hs, const u8 *msg, if (!peer->id) { struct pubkey id; - if (!fromwire_handshake_responder_resp(msg, NULL, &id, &cs)) + if (!fromwire_handshake_responder_reply(msg, NULL, &id, &cs)) goto err; peer->id = tal_dup(peer, struct pubkey, &id); log_info_struct(hs->log, "Peer in from %s", struct pubkey, peer->id); } else { - if (!fromwire_handshake_initiator_resp(msg, NULL, &cs)) + if (!fromwire_handshake_initiator_reply(msg, NULL, &cs)) goto err; log_info_struct(hs->log, "Peer out to %s", struct pubkey, peer->id); @@ -133,29 +132,27 @@ static void handshake_succeeded(struct subdaemon *hs, const u8 *msg, /* FIXME: Look for peer duplicates! */ - /* Tell handshaked to exit. */ - subdaemon_req(peer->owner, take(towire_handshake_exit_req(msg)), - -1, NULL, NULL, NULL); - /* FIXME! */ peer->owner = (struct subdaemon *)peer->ld->gossip; tal_steal(peer->owner, peer); peer_set_condition(peer, "Beginning gossip"); /* Tell gossip to handle it now. */ - msg = towire_gossipctl_new_peer(msg, peer->unique_id, &cs); - subd_send_msg(peer->ld->gossip, msg); + msg = towire_gossipctl_new_peer(peer, peer->unique_id, &cs); + subd_send_msg(peer->ld->gossip, take(msg)); subd_send_fd(peer->ld->gossip, peer->fd); /* Peer struct longer owns fd. */ peer->fd = -1; - return; + /* Tell handshaked to exit. */ + return false; err: log_broken(hs->log, "Malformed resp: %s", tal_hex(peer, msg)); close(peer->fd); tal_free(peer); + return false; } static bool peer_got_handshake_hsmfd(struct subd *hsm, const u8 *msg, @@ -170,10 +167,10 @@ static bool peer_got_handshake_hsmfd(struct subd *hsm, const u8 *msg, } /* Give handshake daemon the hsm fd. */ - peer->owner = new_subdaemon(peer->ld, peer->ld, + /* FIXME! */ + peer->owner = (struct subdaemon *)new_subd(peer->ld, peer->ld, "lightningd_handshake", peer, - handshake_status_wire_type_name, - handshake_control_wire_type_name, + handshake_wire_type_name, NULL, NULL, peer->hsmfd, peer->fd, -1); if (!peer->owner) { @@ -187,18 +184,19 @@ static bool peer_got_handshake_hsmfd(struct subd *hsm, const u8 *msg, peer->fd = -1; if (peer->id) { - req = towire_handshake_initiator_req(peer, &peer->ld->dstate.id, - peer->id); + req = towire_handshake_initiator(peer, &peer->ld->dstate.id, + peer->id); peer_set_condition(peer, "Starting handshake as initiator"); } else { - req = towire_handshake_responder_req(peer, &peer->ld->dstate.id); + req = towire_handshake_responder(peer, &peer->ld->dstate.id); peer_set_condition(peer, "Starting handshake as responder"); } /* Now hand peer request to the handshake daemon: hands it * back on success */ - subdaemon_req(peer->owner, take(req), -1, &peer->fd, - handshake_succeeded, peer); + /* FIXME! subdaemon */ + subd_req((struct subd *)peer->owner, take(req), -1, &peer->fd, + handshake_succeeded, peer); return true; error: