Browse Source

lightningd/handshake: convert to subd.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 8 years ago
parent
commit
e467afce06
  1. 17
      lightningd/handshake/Makefile
  2. 33
      lightningd/handshake/handshake.c
  3. 12
      lightningd/handshake/handshake_control_wire_csv
  4. 30
      lightningd/handshake/handshake_wire.csv
  5. 38
      lightningd/peer_control.c

17
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)

33
lightningd/handshake/handshake.c

@ -11,8 +11,7 @@
#include <ccan/short_types/short_types.h>
#include <errno.h>
#include <lightningd/debug.h>
#include <lightningd/handshake/gen_handshake_control_wire.h>
#include <lightningd/handshake/gen_handshake_status_wire.h>
#include <lightningd/handshake/gen_handshake_wire.h>
#include <lightningd/hsm/client.h>
#include <secp256k1.h>
#include <secp256k1_ecdh.h>
@ -24,8 +23,6 @@
#include <wire/wire.h>
#include <wire/wire_sync.h>
/* 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;
}

12
lightningd/handshake/handshake_control_wire_csv

@ -1,12 +0,0 @@
#include <lightningd/cryptomsg.h>
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

30
lightningd/handshake/handshake_status_wire_csv → lightningd/handshake/handshake_wire.csv

@ -1,5 +1,5 @@
#include <lightningd/cryptomsg.h>
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
Can't render this file because it has a wrong number of fields in line 2.

38
lightningd/peer_control.c

@ -20,8 +20,7 @@
#include <lightningd/channel/gen_channel_status_wire.h>
#include <lightningd/funding_tx.h>
#include <lightningd/gossip/gen_gossip_wire.h>
#include <lightningd/handshake/gen_handshake_control_wire.h>
#include <lightningd/handshake/gen_handshake_status_wire.h>
#include <lightningd/handshake/gen_handshake_wire.h>
#include <lightningd/hsm/gen_hsm_wire.h>
#include <lightningd/key_derive.h>
#include <lightningd/opening/gen_opening_control_wire.h>
@ -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:

Loading…
Cancel
Save