From dba7f9002f261ab46efa1f3655e64f314dab75ab Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 24 Jul 2018 15:48:58 +0930 Subject: [PATCH] gossipd: provide connectd with address resolution. Signed-off-by: Rusty Russell --- connectd/connect.c | 27 +++++++++++++++++++++++++-- connectd/connect_gossip_wire.csv | 10 ++++++++++ gossipd/gossip.c | 31 ++++++++++++++++++++++++++++++- tests/test_lightningd.py | 1 - 4 files changed, 65 insertions(+), 4 deletions(-) diff --git a/connectd/connect.c b/connectd/connect.c index d50207dcc..763913135 100644 --- a/connectd/connect.c +++ b/connectd/connect.c @@ -1505,8 +1505,31 @@ seed_resolve_addr(const tal_t *ctx, const struct pubkey *id, static struct wireaddr_internal * gossip_resolve_addr(const tal_t *ctx, const struct pubkey *id) { - /* FIXME: Ask gossipd! */ - return NULL; + u8 *msg; + struct wireaddr *addrs; + struct wireaddr_internal *addr; + + msg = towire_gossip_get_addrs(NULL, id); + if (!wire_sync_write(GOSSIPCTL_FD, take(msg))) + status_failed(STATUS_FAIL_INTERNAL_ERROR, + "Failed writing to gossipctl: %s", + strerror(errno)); + + msg = wire_sync_read(tmpctx, GOSSIPCTL_FD); + if (!fromwire_gossip_get_addrs_reply(tmpctx, msg, &addrs)) + status_failed(STATUS_FAIL_INTERNAL_ERROR, + "Failed parsing get_addrs_reply gossipctl: %s", + tal_hex(tmpctx, msg)); + + if (!addrs) + return NULL; + + /* FIXME: Don't just take first address! */ + addr = tal(ctx, struct wireaddr_internal); + addr->itype = ADDR_INTERNAL_WIREADDR; + addr->u.wireaddr = addrs[0]; + + return addr; } static void try_reach_peer(struct daemon *daemon, const struct pubkey *id, diff --git a/connectd/connect_gossip_wire.csv b/connectd/connect_gossip_wire.csv index dba261d4b..76f829e56 100644 --- a/connectd/connect_gossip_wire.csv +++ b/connectd/connect_gossip_wire.csv @@ -1,3 +1,5 @@ +#include + # Communication between gossipd and connectd. gossip_new_peer,4000 gossip_new_peer,,id,struct pubkey @@ -9,3 +11,11 @@ gossip_new_peer,,initial_routing_sync,bool # if success: + fd. gossip_new_peer_reply,4100 gossip_new_peer_reply,,success,bool + +# Connectd asks gossipd for any known addresses for that node. +gossip_get_addrs,4001 +gossip_get_addrs,,id,struct pubkey + +gossip_get_addrs_reply,4101 +gossip_get_addrs_reply,,num,u16 +gossip_get_addrs_reply,,addrs,num*struct wireaddr diff --git a/gossipd/gossip.c b/gossipd/gossip.c index 88ba4d569..296d8355c 100644 --- a/gossipd/gossip.c +++ b/gossipd/gossip.c @@ -2164,6 +2164,31 @@ static struct io_plan *recv_req(struct io_conn *conn, struct daemon_conn *master t, tal_hex(tmpctx, master->msg_in)); } +static struct io_plan *connectd_get_address(struct io_conn *conn, + struct daemon *daemon, + const u8 *msg) +{ + struct pubkey id; + struct node *node; + const struct wireaddr *addrs; + + if (!fromwire_gossip_get_addrs(msg, &id)) { + status_broken("Bad gossip_get_addrs msg from connectd: %s", + tal_hex(tmpctx, msg)); + return io_close(conn); + } + + node = get_node(daemon->rstate, &id); + if (node) + addrs = node->addresses; + else + addrs = NULL; + + daemon_conn_send(&daemon->connectd, + take(towire_gossip_get_addrs_reply(NULL, addrs))); + return daemon_conn_read_next(conn, &daemon->connectd); +} + static struct io_plan *connectd_req(struct io_conn *conn, struct daemon_conn *connectd) { @@ -2174,8 +2199,12 @@ static struct io_plan *connectd_req(struct io_conn *conn, case WIRE_GOSSIP_NEW_PEER: return connectd_new_peer(conn, daemon, connectd->msg_in); - /* We send this, don't receive it. */ + case WIRE_GOSSIP_GET_ADDRS: + return connectd_get_address(conn, daemon, connectd->msg_in); + + /* We send these, don't receive them. */ case WIRE_GOSSIP_NEW_PEER_REPLY: + case WIRE_GOSSIP_GET_ADDRS_REPLY: break; } diff --git a/tests/test_lightningd.py b/tests/test_lightningd.py index efb138388..ecc5e9595 100644 --- a/tests/test_lightningd.py +++ b/tests/test_lightningd.py @@ -695,7 +695,6 @@ class LightningDTests(BaseLightningDTests): l1.rpc.connect, '032cf15d1ad9c4a08d26eab1918f732d8ef8fdc6abb9640bf3db174372c491304e', 'localhost', l2.port) @unittest.skipIf(not DEVELOPER, "needs --dev-allow-localhost") - @unittest.skip("FIXME: Re-enable once gossipd gives out addresses to connectd") def test_connect_by_gossip(self): """Test connecting to an unknown peer using node gossip """