Skip to content

Commit f6bacf0

Browse files
committed
Connect to peers alt addr (if provided) when reconnecting. If both nodes
provide alt addrsses only the connection_out will utilize the alt addr. Signed-off-by: Max Rantil <rantil@pm.me>
1 parent b1b9a49 commit f6bacf0

File tree

15 files changed

+143
-53
lines changed

15 files changed

+143
-53
lines changed

channeld/channeld.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,11 @@ struct peer {
196196
bool experimental_upgrade;
197197

198198
/* Alt address for peer connections not publicly announced */
199-
u8 *alt_addr;
199+
u8 *our_alt_addr;
200200
};
201201

202202
static void start_commit_timer(struct peer *peer);
203-
static void send_peer_alt_address(struct peer *peer);
203+
static void send_peer_our_alt_address(struct peer *peer);
204204

205205
static void billboard_update(const struct peer *peer)
206206
{
@@ -542,11 +542,11 @@ static void handle_peer_splice_locked(struct peer *peer, const u8 *msg)
542542
check_mutual_splice_locked(peer);
543543
}
544544

545-
static void send_peer_alt_address(struct peer *peer) {
545+
static void send_peer_our_alt_address(struct peer *peer) {
546546
struct pubkey node_id;
547547

548548
if (pubkey_from_node_id(&node_id, &peer->id)) {
549-
u8 *msg = towire_peer_alt_address(peer, &node_id, peer->alt_addr);
549+
u8 *msg = towire_peer_alt_address(peer, &node_id, peer->our_alt_addr);
550550
peer_write(peer->pps, take(msg));
551551
}
552552
}
@@ -4179,8 +4179,8 @@ static void peer_in(struct peer *peer, const u8 *msg)
41794179

41804180
check_tx_abort(peer, msg);
41814181

4182-
if (peer->alt_addr)
4183-
send_peer_alt_address(peer);
4182+
if (peer->our_alt_addr)
4183+
send_peer_our_alt_address(peer);
41844184

41854185
/* If we're in STFU mode and aren't waiting for a STFU mode
41864186
* specific message, the only valid message was tx_abort */
@@ -5890,7 +5890,7 @@ static void init_channel(struct peer *peer)
58905890
&peer->experimental_upgrade,
58915891
&peer->splice_state->inflights,
58925892
&peer->local_alias,
5893-
&peer->alt_addr,
5893+
&peer->our_alt_addr,
58945894
&peer->id)) {
58955895
master_badmsg(WIRE_CHANNELD_INIT, msg);
58965896
}

connectd/connectd.c

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -506,9 +506,30 @@ static struct io_plan *connection_in(struct io_conn *conn,
506506

507507
conn_in_arg.daemon = daemon;
508508
conn_in_arg.is_websocket = false;
509+
510+
// HOW CAN WE GET THE peer->alt_addr AND USE THAT INSTEAD OF &conn_in_arg.addr????
511+
509512
return conn_in(conn, &conn_in_arg);
510513
}
511514

515+
void handle_peer_alt_address(struct peer *peer, const u8 *msg)
516+
{
517+
u8 *peer_alt_addr;
518+
struct pubkey peer_id;
519+
// u32 *timestamp = NULL;
520+
521+
if (!fromwire_peer_alt_address(peer, msg, &peer_id, &peer_alt_addr/* , timestamp */)) {
522+
master_badmsg(WIRE_PEER_ALT_ADDRESS, msg);
523+
}
524+
525+
// peer->alt_addr = tal_strdup(peer, (char *)peer_alt_addr); // This is WIP, Wanted to utilize this in `connction_in()` but didn't manage that yet.
526+
527+
msg = towire_connectd_alt_address(NULL, &peer_id, peer_alt_addr);
528+
daemon_conn_send(peer->daemon->master, take(msg));
529+
530+
tal_free(peer_alt_addr);
531+
}
532+
512533
/*~ <hello>I speak web socket</hello>.
513534
*
514535
* Actually that's dumb, websocket (aka rfc6455) looks nothing like that. */
@@ -1414,7 +1435,8 @@ static void connect_init(struct daemon *daemon, const u8 *msg)
14141435
&daemon->dev_fast_gossip,
14151436
&dev_disconnect,
14161437
&daemon->dev_no_ping_timer,
1417-
&daemon->dev_handshake_no_reply)) {
1438+
&daemon->dev_handshake_no_reply/* , //This is WIP
1439+
&daemon->alt_addr */)) {
14181440
/* This is a helper which prints the type expected and the actual
14191441
* message, then exits (it should never be called!). */
14201442
master_badmsg(WIRE_CONNECTD_INIT, msg);
@@ -1776,22 +1798,6 @@ static void try_connect_peer(struct daemon *daemon,
17761798
try_connect_one_addr(connect);
17771799
}
17781800

1779-
void handle_peer_alt_addr(struct peer *peer, const u8 *msg)
1780-
{
1781-
u8 *alt_addr;
1782-
struct pubkey peer_id;
1783-
1784-
// u32 *timestamp = NULL;
1785-
if (!fromwire_peer_alt_address(peer, msg, &peer_id, &alt_addr/* , timestamp */)) {
1786-
master_badmsg(WIRE_PEER_ALT_ADDRESS, msg);
1787-
}
1788-
1789-
msg = towire_connectd_alt_address(NULL, &peer_id, alt_addr);
1790-
daemon_conn_send(peer->daemon->master, take(msg));
1791-
1792-
tal_free(alt_addr);
1793-
}
1794-
17951801
/* lightningd tells us to connect to a peer by id, with optional addr hint. */
17961802
static void connect_to_peer(struct daemon *daemon, const u8 *msg)
17971803
{

connectd/connectd.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ struct peer {
6161

6262
/* The pubkey of the node */
6363
struct node_id id;
64+
65+
//This is WIP
66+
/* char *alt_addr; */
67+
6468
/* Counters and keys for symmetric crypto */
6569
struct crypto_state cs;
6670

@@ -225,6 +229,9 @@ struct daemon {
225229
* resort, but doing so leaks our address so can be disabled. */
226230
bool use_dns;
227231

232+
// This is WIP
233+
/* u8 *alt_addr; */
234+
228235
/* The address that the broken response returns instead of
229236
* NXDOMAIN. NULL if we have not detected a broken resolver. */
230237
struct sockaddr *broken_resolver_response;
@@ -294,6 +301,6 @@ void destroy_peer(struct peer *peer);
294301
void close_random_connection(struct daemon *daemon);
295302

296303
/* Handles alternative address message from peer. */
297-
void handle_peer_alt_addr(struct peer *peer, const u8 *msg);
304+
void handle_peer_alt_address(struct peer *peer, const u8 *msg);
298305

299306
#endif /* LIGHTNING_CONNECTD_CONNECTD_H */

connectd/connectd_wire.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ msgdata,connectd_init,dev_disconnect,bool,
2727
msgdata,connectd_init,dev_no_ping_timer,bool,
2828
# Allow incoming connections, but don't talk.
2929
msgdata,connectd_init,dev_noreply,bool,
30+
# Use an alternative private address if provided by peer.
31+
#msgdata,connectd_init,alt_addr_len,u16,
32+
#msgdata,connectd_init,alt_addr,u8,alt_addr_len,
3033

3134
# Connectd->master, here are the addresses I bound, can announce.
3235
msgtype,connectd_init_reply,2100

connectd/multiplex.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ static bool handle_message_locally(struct peer *peer, const u8 *msg)
773773
handle_onion_message(peer->daemon, peer, msg);
774774
return true;
775775
} else if (type == WIRE_PEER_ALT_ADDRESS) { // IS THIS THE RIGHT PLACE ?? IT WORKS BUT...
776-
handle_peer_alt_addr(peer, msg);
776+
handle_peer_alt_address(peer, msg);
777777
return true;
778778
} else if (handle_custommsg(peer->daemon, peer, msg)) {
779779
return true;

doc/schemas/lightning-listconfigs.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,7 @@
11851185
],
11861186
"properties": {
11871187
"values_str": {
1188-
"added": "v24.05",
1188+
"added": "v24.05",
11891189
"type": "array",
11901190
"items": {
11911191
"type": "string",

lightningd/channel.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,4 +798,5 @@ const u8 *channel_update_for_error(const tal_t *ctx,
798798

799799
struct amount_msat htlc_max_possible_send(const struct channel *channel);
800800

801+
801802
#endif /* LIGHTNING_LIGHTNINGD_CHANNEL_H */

lightningd/channel_control.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1726,7 +1726,7 @@ bool peer_start_channeld(struct channel *channel,
17261726
cast_const2(const struct inflight **,
17271727
inflights),
17281728
*channel->alias[LOCAL],
1729-
ld->alt_addr,
1729+
ld->our_alt_addr,
17301730
&ld->id);
17311731

17321732
/* We don't expect a response: we are triggered by funding_depth_cb. */

lightningd/connect_control.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,12 @@ static void try_connect(const tal_t *ctx,
335335
{
336336
struct delayed_reconnect *d;
337337
struct peer *peer;
338+
const struct wireaddr_internal *alt_addr;
339+
340+
alt_addr = wallet_get_peer_alt_addr(ld->wallet, id);
341+
if (alt_addr) {
342+
addrhint = alt_addr; //IS THIS CORRECT TO JUST OVERWRITE IT LIKE THIS??
343+
}
338344

339345
/* Don't stack, unless this is an instant reconnect */
340346
d = delayed_reconnect_map_get(ld->delayed_reconnect_map, id);
@@ -364,9 +370,6 @@ static void try_connect(const tal_t *ctx,
364370
/* Update any channel billboards */
365371
peer = peer_by_id(ld, id);
366372
if (peer) {
367-
// struct pubkey pubkey;
368-
// if (pubkey_from_node_id(&pubkey, id)) {
369-
// send_peer_alt_address(peer, &pubkey, (const u8 *)"127.21.21.21"); // THIS MIGHT BE THE RIGHT PLACE IN THE END, NEED TO SEND ANOTHER MSG FROM MASTER -> CHANNELD then????
370373
struct channel *channel;
371374
list_for_each(&peer->channels, channel, list) {
372375
if (!channel_state_wants_peercomms(channel->state))
@@ -559,20 +562,21 @@ static void handle_custommsg_in(struct lightningd *ld, const u8 *msg)
559562
plugin_hook_call_custommsg(ld, NULL, p);
560563
}
561564

562-
static void handle_alt_addr_in(struct lightningd *ld, const u8 *msg)
565+
static void handle_peer_alt_addr_in(struct lightningd *ld, const u8 *msg)
563566
{
564-
struct pubkey node_id;
565-
struct node_id id;
566-
u8 *alt_addr;
567+
struct pubkey peer_node_id;
568+
u8 *peer_alt_addr;
567569

568-
if (!fromwire_connectd_alt_address(tmpctx, msg, &node_id, &alt_addr)) {
570+
if (!fromwire_connectd_alt_address(tmpctx, msg, &peer_node_id, &peer_alt_addr)) {
569571
log_broken(ld->log, "Malformed peer_alt_addr_msg: %s",
570572
tal_hex(tmpctx, msg));
571573
return;
572574
}
573575

574-
node_id_from_pubkey(&id, &node_id);
575-
wallet_peer_alt_addr(ld->wallet->db, &id, (char *)alt_addr);
576+
struct node_id id;
577+
node_id_from_pubkey(&id, &peer_node_id);
578+
wallet_add_peer_alt_addr(ld->wallet->db, &id, (char *)peer_alt_addr);
579+
// SHOULD/CAN WE ALSO SAVE IT TO THE peer struct HERE, TO BE ABLE TO GET IN IT CONNECTD LATER FOR THE `connection_in`???
576580
}
577581

578582
static void connectd_start_shutdown_reply(struct subd *connectd,
@@ -666,7 +670,7 @@ static unsigned connectd_msg(struct subd *connectd, const u8 *msg, const int *fd
666670
break;
667671

668672
case WIRE_CONNECTD_ALT_ADDRESS:
669-
handle_alt_addr_in(connectd->ld, msg);
673+
handle_peer_alt_addr_in(connectd->ld, msg);
670674
break;
671675
}
672676
return 0;
@@ -788,7 +792,8 @@ int connectd_init(struct lightningd *ld)
788792
ld->dev_fast_gossip,
789793
ld->dev_disconnect_fd >= 0,
790794
ld->dev_no_ping_timer,
791-
ld->dev_handshake_no_reply);
795+
ld->dev_handshake_no_reply/* , //This is WIP
796+
ld->our_alt_addr */);
792797

793798
subd_req(ld->connectd, ld->connectd, take(msg), -1, 0,
794799
connect_init_done, NULL);

lightningd/lightningd.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,6 @@ struct lightningd {
175175
/* Do we want to reconnect to other peers having only unannouced channels with us? */
176176
bool reconnect_private;
177177

178-
/* Alt address for peer connections not publicly announced */
179-
u8 *alt_addr;
180-
181178
/* How many outstanding startup connection attempts? */
182179
size_t num_startup_connects;
183180

@@ -196,6 +193,9 @@ struct lightningd {
196193
struct wireaddr_internal *binding;
197194
struct wireaddr *announceable;
198195

196+
/* Alternative address for peer connections not publicly announced */
197+
u8 *our_alt_addr;
198+
199199
/* Current node announcement (if any) */
200200
const u8 *node_announcement;
201201

0 commit comments

Comments
 (0)