Skip to content

Commit

Permalink
Handle many alt_addrs
Browse files Browse the repository at this point in the history
This implementation utilizes a string with comma-separated alternative ddresses,
future implementation will utilize an array of wireaddr_internal structs.

Signed-off-by: Max Rantil <[email protected]>
  • Loading branch information
maxrantil committed Jul 11, 2024
1 parent e6ff7da commit b9721c3
Show file tree
Hide file tree
Showing 15 changed files with 1,204 additions and 1,068 deletions.
39 changes: 39 additions & 0 deletions .msggen.json
Original file line number Diff line number Diff line change
Expand Up @@ -1590,6 +1590,8 @@
"ListConfigs.configs.alias": 30,
"ListConfigs.configs.allow-deprecated-apis": 14,
"ListConfigs.configs.alt-addr": 71,
"ListConfigs.configs.alt-announce-addr": 73,
"ListConfigs.configs.alt-bind-addr": 72,
"ListConfigs.configs.always-use-proxy": 17,
"ListConfigs.configs.announce-addr": 48,
"ListConfigs.configs.announce-addr-discovered": 54,
Expand Down Expand Up @@ -1677,6 +1679,14 @@
"ListConfigs.configs.alt-addr.sources[]": 2,
"ListConfigs.configs.alt-addr.values_str[]": 1
},
"ListconfigsConfigsAlt-announce-addr": {
"ListConfigs.configs.alt-addr.sources[]": 2,
"ListConfigs.configs.alt-addr.values_str[]": 1
},
"ListconfigsConfigsAlt-bind-addr": {
"ListConfigs.configs.alt-addr.sources[]": 2,
"ListConfigs.configs.alt-addr.values_str[]": 1
},
"ListconfigsConfigsAlways-use-proxy": {
"ListConfigs.configs.always-use-proxy.source": 2,
"ListConfigs.configs.always-use-proxy.value_bool": 1
Expand Down Expand Up @@ -2366,6 +2376,7 @@
"ListPeerChannels.channels[]": 1
},
"ListpeersPeers": {
"ListPeers.peers[].alt_addrs[]": 9,
"ListPeers.peers[].channels[]": 4,
"ListPeers.peers[].connected": 2,
"ListPeers.peers[].features": 6,
Expand Down Expand Up @@ -6695,6 +6706,30 @@
"added": "v24.05",
"deprecated": false
},
"ListConfigs.configs.alt-announce-addr": {
"added": "v24.05",
"deprecated": false
},
"ListConfigs.configs.alt-announce-addr.sources[]": {
"added": "v24.05",
"deprecated": false
},
"ListConfigs.configs.alt-announce-addr.values_str[]": {
"added": "v24.05",
"deprecated": false
},
"ListConfigs.configs.alt-bind-addr": {
"added": "v24.05",
"deprecated": false
},
"ListConfigs.configs.alt-bind-addr.sources[]": {
"added": "v24.05",
"deprecated": false
},
"ListConfigs.configs.alt-bind-addr.values_str[]": {
"added": "v24.05",
"deprecated": false
},
"ListConfigs.configs.always-use-proxy": {
"added": "pre-v0.10.1",
"deprecated": false
Expand Down Expand Up @@ -8763,6 +8798,10 @@
"added": "pre-v0.10.1",
"deprecated": false
},
"ListPeers.peers[].alt_addrs[]": {
"added": "v24.05",
"deprecated": false
},
"ListPeers.peers[].channels[]": {
"added": "pre-v0.10.1",
"deprecated": "v23.02"
Expand Down
70 changes: 50 additions & 20 deletions connectd/connectd.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,24 +318,25 @@ static struct io_plan *handshake_in_success(struct io_conn *conn,
node_id_from_pubkey(&id, id_key);
status_peer_debug(&id, "Connect IN");

/* FIXME(maxrantil): loop all alt_bind_addr after making it an array (code down-under)*/
/* bool is_whitelisted = false;
/* Confirm that peer connects to the alt-bind-addr you sent */
if (daemon->alt_bind_addr) {
for (size_t i = 0; i < tal_count(daemon->alt_bind_addr); i++) {
if (strcmp(incoming_addr, (char *)daemon->alt_bind_addr[i]) == 0) {
is_whitelisted = true;
char *incoming_addr = fmt_wireaddr_internal(tmpctx, addr);
fprintf(stderr, "--> incoming connection addr: '%s'\n", incoming_addr);

char *bind_addrs = tal_strdup(NULL, (char *)daemon->alt_bind_addr);
char *token = strtok(bind_addrs, ",");

while (token != NULL) {
fprintf(stderr, "--> comparing '%s' with token '%s'\n", incoming_addr, token);
if (strcmp(incoming_addr, token) == 0) {
fprintf(stderr, "--> INSIDE\n");
towire_connectd_alt_addr_whitelist(tmpctx, id_key, (u8 *)incoming_addr);
break;
}
token = strtok(NULL, ",");
}
} */

char *incoming_addr = fmt_wireaddr_internal(tmpctx, addr);

/* Confirm that peer connects to the alt-bind-addr you sent */
if (daemon->alt_bind_addr)
if (strcmp(incoming_addr, (char *)daemon->alt_bind_addr) == 0)
towire_connectd_alt_addr_whitelist(tmpctx, id_key, (u8 *)incoming_addr);
tal_free(bind_addrs);
}

return peer_exchange_initmsg(conn, daemon, daemon->our_features,
cs, &id, addr, timeout, is_websocket, true);
Expand Down Expand Up @@ -655,6 +656,9 @@ static struct io_plan *handshake_out_success(struct io_conn *conn,
node_id_from_pubkey(&id, key);
connect->connstate = "Exchanging init messages";
status_peer_debug(&id, "Connect OUT");

fprintf(stderr, "<-- outgoing connection addr: '%s'\n", fmt_wireaddr_internal(tmpctx, addr));

return peer_exchange_initmsg(conn, connect->daemon,
connect->daemon->our_features,
cs, &id, addr, timeout, is_websocket, false);
Expand Down Expand Up @@ -1719,6 +1723,7 @@ static void try_connect_peer(struct daemon *daemon,
const struct node_id *id,
struct wireaddr *gossip_addrs,
struct wireaddr_internal *addrhint STEALS,
struct wireaddr_internal *peer_alt_addrs STEALS,
bool dns_fallback,
bool transient)
{
Expand Down Expand Up @@ -1746,6 +1751,13 @@ static void try_connect_peer(struct daemon *daemon,
tal_arr_expand(&connect->addrs, *addrhint);
}

/* Update addrs with peer_alt_addrs if provided */
if (peer_alt_addrs) {
for (size_t i = 0; i < tal_count(peer_alt_addrs); i++) {
tal_arr_expand(&connect->addrs, peer_alt_addrs[i]);
}
}

return;
}

Expand All @@ -1764,6 +1776,22 @@ static void try_connect_peer(struct daemon *daemon,
&& !addrhint->u.wireaddr.is_websocket
? &addrhint->u.wireaddr.wireaddr : NULL);

/*
* FIXME(maxrantil): Question for Alex:
* Expanding the addrs array with peer_alt_addrs here (after addrhint) only allows
* using alt_addrs from node2 to node1 when connecting, but not from node1 to node2.
* If you start a regtest environment with (start_ln, fund_node, and then repeatedly
* stop_ln and start_ln), you will see that it only works about 50% of the time.
* This is because every second time or so, the nodes alternate as sender and receiver.
* If we move the for loop 10 lines up, before the addrhint if statement,
* the alt_addrs will be utilized by both nodes when initializing a connection.
* What are your thoughts on this?
*/

/* Add all peer_alt_addrs next so they're tried after addrhint by connectd */
for (size_t i = 0; i < tal_count(peer_alt_addrs); i++)
tal_arr_expand(&addrs, peer_alt_addrs[i]);

if (tal_count(addrs) == 0) {
/* Don't resolve via DNS seed if we're supposed to use proxy. */
if (use_proxy) {
Expand Down Expand Up @@ -1820,16 +1848,17 @@ static void connect_to_peer(struct daemon *daemon, const u8 *msg)
struct node_id id;
struct wireaddr_internal *addrhint;
struct wireaddr *addrs;
struct wireaddr_internal *peer_alt_addrs;
bool dns_fallback;
bool transient;

if (!fromwire_connectd_connect_to_peer(tmpctx, msg,
&id, &addrs, &addrhint,
&id, &addrs, &addrhint, &peer_alt_addrs,
&dns_fallback,
&transient))
master_badmsg(WIRE_CONNECTD_CONNECT_TO_PEER, msg);

try_connect_peer(daemon, &id, addrs, addrhint, dns_fallback, transient);
try_connect_peer(daemon, &id, addrs, addrhint, peer_alt_addrs, dns_fallback, transient);
}

/* lightningd tells us a peer should be disconnected. */
Expand Down Expand Up @@ -2098,13 +2127,16 @@ static void dev_exhaust_fds(struct daemon *daemon, const u8 *msg)
daemon->dev_exhausted_fds = true;
}

#include <stdio.h>
static void handle_alt_addr_whitelist_reply(struct daemon *daemon, const u8 *msg)
{
struct pubkey p_pk;
struct peer *peer;
u8 *incoming_addr;
bool is_whitelisted;

fprintf(stderr, "INSIDE WHITELIST\n");

if (!fromwire_connectd_alt_addr_whitelist_reply(tmpctx,
msg, &p_pk, &incoming_addr, &is_whitelisted)) {
master_badmsg(WIRE_CONNECTD_ALT_ADDR_WHITELIST_REPLY, msg);
Expand Down Expand Up @@ -2220,10 +2252,8 @@ static struct io_plan *recv_req(struct io_conn *conn,
goto out;
}
case WIRE_CONNECTD_ALT_ADDR_WHITELIST_REPLY:
if (daemon->developer) {
handle_alt_addr_whitelist_reply(daemon, msg);
goto out;
}
handle_alt_addr_whitelist_reply(daemon, msg);
goto out;
/* Fall thru */
/* We send these, we don't receive them */
case WIRE_CONNECTD_INIT_REPLY:
Expand Down
3 changes: 2 additions & 1 deletion connectd/connectd_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ msgdata,connectd_init,dev_disconnect,bool,
msgdata,connectd_init,dev_no_ping_timer,bool,
# Allow incoming connections, but don't talk.
msgdata,connectd_init,dev_noreply,bool,
# Whitelist for incomming alternative addrdress connections
# Whitelist for incoming alternative address connections
msgdata,connectd_init,bind_alt_addr_len,u8,
msgdata,connectd_init,bind_alt_addr,byte,bind_alt_addr_len,

Expand Down Expand Up @@ -59,6 +59,7 @@ msgdata,connectd_connect_to_peer,id,node_id,
msgdata,connectd_connect_to_peer,len,u32,
msgdata,connectd_connect_to_peer,addrs,wireaddr,len
msgdata,connectd_connect_to_peer,addrhint,?wireaddr_internal,
msgdata,connectd_connect_to_peer,peer_alt_addrs,?wireaddr_internal,
msgdata,connectd_connect_to_peer,dns_fallback,bool,
msgdata,connectd_connect_to_peer,transient,bool,

Expand Down
20 changes: 16 additions & 4 deletions contrib/msggen/msggen/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4146,7 +4146,7 @@
"netaddr": [
"127.0.0.1:40119"
],
"alt_addr": "127.0.0.21:7171",
"alt_addr": "127.0.0.1:34321",
"features": "08a0000a8a5961",
"log": [
{
Expand Down Expand Up @@ -21204,7 +21204,19 @@
]
}
},
"alt_addr": {},
"alt_addrs": {
"type": "array",
"description": [
"An array containing zero or more alternative addresses for the peer."
],
"items": {
"type": "string",
"description": [
"Address, e.g. 1.2.3.4:1234."
]
},
"added": "v24.05"
},
"remote_addr": {
"type": "string",
"description": [
Expand Down Expand Up @@ -21249,7 +21261,7 @@
"netaddr": [
"127.0.0.1:44619"
],
"alt_addr": "127.0.0.21:7171",
"alt_addr": "127.0.0.1:34321",
"features": "08a0000a0a69a2"
}
]
Expand All @@ -21273,7 +21285,7 @@
"netaddr": [
"127.0.0.1:48862"
],
"alt_addr": "127.0.0.21:7171",
"alt_addr": "127.0.0.1:34321",
"features": "08a0000a0a69a2"
}
]
Expand Down
1,904 changes: 952 additions & 952 deletions contrib/pyln-grpc-proto/pyln/grpc/node_pb2.py

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions contrib/pyln-testing/pyln/testing/grpc2py.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def listpeers_peers_log2py(m):

def listpeers_peers2py(m):
return remove_default({
"alt_addrs": [m.alt_addrs for i in m.alt_addrs], # ArrayField[primitive] in generate_composite
"log": [listpeers_peers_log2py(i) for i in m.log], # ArrayField[composite] in generate_composite
"netaddr": [m.netaddr for i in m.netaddr], # ArrayField[primitive] in generate_composite
"connected": m.connected, # PrimitiveField in generate_composite
Expand Down
1 change: 1 addition & 0 deletions contrib/startup_regtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ start_nodes() {
log-level=debug
log-file=$LIGHTNING_DIR/l$i/log
addr=localhost:$socket
alt-addr=127.0.0.21:$socket
allow-deprecated-apis=false
developer
dev-fast-gossip
Expand Down
14 changes: 13 additions & 1 deletion doc/schemas/lightning-listpeers.json
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,19 @@
]
}
},
"alt_addr": {},
"alt_addrs": {
"type": "array",
"description": [
"An array containing zero or more alternative addresses for the peer."
],
"items": {
"type": "string",
"description": [
"Address, e.g. 1.2.3.4:1234."
]
},
"added": "v24.05"
},
"remote_addr": {
"type": "string",
"description": [
Expand Down
Loading

0 comments on commit b9721c3

Please sign in to comment.