From 3045e25624c7333c3fd479aae6452361083d31d0 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Thu, 9 Mar 2017 14:24:32 +0100 Subject: [PATCH] gossip: Returning a gossip-client upon peer_ready This fd is handed to the owner of the peer so that it can talk to the gossip daemon. --- lightningd/gossip/gossip.c | 21 +++++++++++++++++++++ lightningd/gossip_control.c | 9 +++++++-- lightningd/peer_control.h | 3 +++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lightningd/gossip/gossip.c b/lightningd/gossip/gossip.c index e3da4459d..b07539b47 100644 --- a/lightningd/gossip/gossip.c +++ b/lightningd/gossip/gossip.c @@ -62,6 +62,9 @@ struct peer { u8 **msg_out; /* Is it time to continue the staggered broadcast? */ bool gossip_sync; + + /* The peer owner will use this to talk to gossipd */ + int proxy_fd; }; static void destroy_peer(struct peer *peer) @@ -227,10 +230,21 @@ static bool has_even_bit(const u8 *bitmap) return false; } +static int peer_create_gossip_client(struct peer *peer) +{ + int fds[2]; + if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds) != 0) { + return -1; + } + peer->proxy_fd = fds[0]; + return fds[1]; +} + static struct io_plan *peer_parse_init(struct io_conn *conn, struct peer *peer, u8 *msg) { u8 *gfeatures, *lfeatures; + int client_fd; if (!fromwire_init(msg, msg, NULL, &gfeatures, &lfeatures)) { peer->error = tal_fmt(msg, "Bad init: %s", tal_hex(msg, msg)); @@ -255,12 +269,19 @@ static struct io_plan *peer_parse_init(struct io_conn *conn, return io_close(conn); } + client_fd = peer_create_gossip_client(peer); + if (client_fd == -1) { + peer->error = tal_fmt(msg, "Internal error"); + return io_close(conn); + } + /* BOLT #1: * * Each node MUST wait to receive `init` before sending any other * messages. */ status_send(towire_gossipstatus_peer_ready(msg, peer->unique_id)); + status_send_fd(client_fd); /* Need to go duplex here, otherwise backpressure would mean * we both wait indefinitely */ diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index c497315ac..a3ddf2b31 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -71,7 +71,7 @@ static void peer_nongossip(struct subd *gossip, const u8 *msg, int fd) peer_accept_open(peer, &cs, inner); } -static void peer_ready(struct subd *gossip, const u8 *msg) +static void peer_ready(struct subd *gossip, const u8 *msg, int fd) { u64 unique_id; struct peer *peer; @@ -98,6 +98,8 @@ static void peer_ready(struct subd *gossip, const u8 *msg) peer->connect_cmd = NULL; } + peer->gossip_client_fd = fd; + peer_set_condition(peer, "Exchanging gossip"); } @@ -128,7 +130,10 @@ static enum subd_msg_ret gossip_msg(struct subd *gossip, peer_nongossip(gossip, msg, fd); break; case WIRE_GOSSIPSTATUS_PEER_READY: - peer_ready(gossip, msg); + if (fd == -1) { + return SUBD_NEED_FD; + } + peer_ready(gossip, msg, fd); break; } return SUBD_COMPLETE; diff --git a/lightningd/peer_control.h b/lightningd/peer_control.h index e8ccde0b7..3e855e9de 100644 --- a/lightningd/peer_control.h +++ b/lightningd/peer_control.h @@ -52,6 +52,9 @@ struct peer { /* Secret seed (FIXME: Move to hsm!) */ struct privkey *seed; + + /* Gossip client fd, forwarded to the respective owner */ + int gossip_client_fd; }; struct peer *peer_by_unique_id(struct lightningd *ld, u64 unique_id);