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.
This commit is contained in:
Christian Decker 2017-03-09 14:24:32 +01:00
parent 6319035033
commit 3045e25624
3 changed files with 31 additions and 2 deletions

View File

@ -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 */

View File

@ -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;

View File

@ -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);