diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index f3681ab01..3b9d8c79b 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -4,6 +4,7 @@ #include "subdaemon.h" #include #include +#include #include #include #include @@ -76,7 +77,16 @@ static void peer_ready(struct subdaemon *gossip, const u8 *msg) log_info_struct(gossip->log, "Peer %s ready for channel open", struct pubkey, peer->id); - /* FIXME: finish json connect cmd if any. */ + if (peer->connect_cmd) { + struct json_result *response; + response = new_json_result(peer->connect_cmd); + + json_object_start(response, NULL); + json_add_pubkey(response, "id", peer->id); + json_object_end(response); + command_success(peer->connect_cmd, response); + peer->connect_cmd = NULL; + } } static enum subdaemon_status gossip_status(struct subdaemon *gossip, diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 9991670b4..308a887a4 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -23,9 +23,13 @@ static void destroy_peer(struct peer *peer) list_del_from(&peer->ld->peers, &peer->list); if (peer->fd >= 0) close(peer->fd); + if (peer->connect_cmd) + /* FIXME: Better diagnostics */ + command_fail(peer->connect_cmd, "Connect failed"); } -static struct peer *new_peer(const tal_t *ctx, struct lightningd *ld, int fd) +static struct peer *new_peer(const tal_t *ctx, struct lightningd *ld, int fd, + struct command *cmd) { static u64 id_counter; struct peer *peer = tal(ctx, struct peer); @@ -34,6 +38,7 @@ static struct peer *new_peer(const tal_t *ctx, struct lightningd *ld, int fd) peer->owner = NULL; peer->id = NULL; peer->fd = fd; + peer->connect_cmd = cmd; list_add_tail(&ld->peers, &peer->list); tal_add_destructor(peer, destroy_peer); return peer; @@ -143,7 +148,7 @@ error: /* FIXME: timeout handshake if taking too long? */ static struct io_plan *peer_in(struct io_conn *conn, struct lightningd *ld) { - struct peer *peer = new_peer(ld, ld, io_conn_fd(conn)); + struct peer *peer = new_peer(ld, ld, io_conn_fd(conn), NULL); /* Get HSM fd for this peer. */ subdaemon_req(ld->hsm, @@ -268,7 +273,7 @@ static struct io_plan *peer_out(struct io_conn *conn, struct json_connecting *jc) { struct lightningd *ld = ld_from_dstate(jc->cmd->dstate); - struct peer *peer = new_peer(ld, ld, io_conn_fd(conn)); + struct peer *peer = new_peer(ld, ld, io_conn_fd(conn), jc->cmd); /* We already know ID we're trying to reach. */ peer->id = tal_dup(peer, struct pubkey, &jc->id); diff --git a/lightningd/peer_control.h b/lightningd/peer_control.h index 2d6e549b1..230404e6c 100644 --- a/lightningd/peer_control.h +++ b/lightningd/peer_control.h @@ -23,6 +23,9 @@ struct peer { /* HSM connection for this peer. */ int hsmfd; + + /* Json command which made us connect (if any) */ + struct command *connect_cmd; }; struct peer *peer_by_unique_id(struct lightningd *ld, u64 unique_id); diff --git a/lightningd/test/test-basic b/lightningd/test/test-basic index 6b3eb9804..b79a56bd8 100755 --- a/lightningd/test/test-basic +++ b/lightningd/test/test-basic @@ -11,8 +11,12 @@ setup_lightning 2 start_lightningd 2 lightningd/lightningd -# FIXME: Try to connect. -# lcli1 connect localhost $PORT2 $ID2 +if lcli1 connect localhost $PORT2 $ID1; then + echo Connected OK with wrong ID? >&2 + exit 1 +fi + +lcli1 connect localhost $PORT2 $ID2 # FIXME: Check status.