Skip to content

Commit fd219e8

Browse files
m-schmoockkandycoder
authored andcommitted
connectd: prefer non-TOR connections and filter wireaddr hint duplicate
This does two things: - It moves non-tor addresses upfront so it prefers normal connection which are less laggy and more reliable. - It prevents connectd from trying the same wire_addr twice when the addr_hint was given and gossip also added the same address. Changelog-Changed: connectd: try non-TOR connections first Changelog-Fixed: connectd: do not try address hint twice
1 parent 8890e72 commit fd219e8

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

connectd/connectd.c

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,9 +1459,18 @@ static void add_seed_addrs(struct wireaddr_internal **addrs,
14591459
}
14601460
}
14611461

1462+
static bool wireaddr_int_equals_wireaddr(struct wireaddr_internal *addr_a,
1463+
struct wireaddr *addr_b)
1464+
{
1465+
if (!addr_a || !addr_b)
1466+
return false;
1467+
return wireaddr_eq(&addr_a->u.wireaddr, addr_b);
1468+
}
1469+
14621470
/*~ This asks gossipd for any addresses advertized by the node. */
14631471
static void add_gossip_addrs(struct wireaddr_internal **addrs,
1464-
const struct node_id *id)
1472+
const struct node_id *id,
1473+
struct wireaddr_internal *addrhint)
14651474
{
14661475
u8 *msg;
14671476
struct wireaddr *normal_addrs;
@@ -1483,6 +1492,24 @@ static void add_gossip_addrs(struct wireaddr_internal **addrs,
14831492

14841493
/* Wrap each one in a wireaddr_internal and add to addrs. */
14851494
for (size_t i = 0; i < tal_count(normal_addrs); i++) {
1495+
/* add TOR addresses in a second loop */
1496+
if (normal_addrs[i].type == ADDR_TYPE_TOR_V2 ||
1497+
normal_addrs[i].type == ADDR_TYPE_TOR_V3)
1498+
continue;
1499+
if (wireaddr_int_equals_wireaddr(addrhint, &normal_addrs[i]))
1500+
continue;
1501+
struct wireaddr_internal addr;
1502+
addr.itype = ADDR_INTERNAL_WIREADDR;
1503+
addr.u.wireaddr = normal_addrs[i];
1504+
tal_arr_expand(addrs, addr);
1505+
}
1506+
/* so connectd prefers direct connections if possible. */
1507+
for (size_t i = 0; i < tal_count(normal_addrs); i++) {
1508+
if (normal_addrs[i].type != ADDR_TYPE_TOR_V2 &&
1509+
normal_addrs[i].type != ADDR_TYPE_TOR_V3)
1510+
continue;
1511+
if (wireaddr_int_equals_wireaddr(addrhint, &normal_addrs[i]))
1512+
continue;
14861513
struct wireaddr_internal addr;
14871514
addr.itype = ADDR_INTERNAL_WIREADDR;
14881515
addr.u.wireaddr = normal_addrs[i];
@@ -1524,10 +1551,11 @@ static void try_connect_peer(struct daemon *daemon,
15241551
addrs = tal_arr(tmpctx, struct wireaddr_internal, 0);
15251552

15261553
/* They can supply an optional address for the connect RPC */
1554+
/* We add this first so its tried first by connectd */
15271555
if (addrhint)
15281556
tal_arr_expand(&addrs, *addrhint);
15291557

1530-
add_gossip_addrs(&addrs, id);
1558+
add_gossip_addrs(&addrs, id, addrhint);
15311559

15321560
if (tal_count(addrs) == 0) {
15331561
/* Don't resolve via DNS seed if we're supposed to use proxy. */

0 commit comments

Comments
 (0)