Skip to content

Commit 24018af

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 <[email protected]>
1 parent b1b9a49 commit 24018af

File tree

15 files changed

+150
-54
lines changed

15 files changed

+150
-54
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: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -506,9 +506,26 @@ 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+
509510
return conn_in(conn, &conn_in_arg);
510511
}
511512

513+
void handle_peer_alt_address(struct peer *peer, const u8 *msg)
514+
{
515+
u8 *peer_alt_addr;
516+
struct pubkey peer_id;
517+
/* u32 *timestamp = NULL; */ /* TODO */
518+
519+
if (!fromwire_peer_alt_address(peer, msg, &peer_id, &peer_alt_addr/* , timestamp */)) {
520+
master_badmsg(WIRE_PEER_ALT_ADDRESS, msg);
521+
}
522+
523+
msg = towire_connectd_alt_address(NULL, &peer_id, peer_alt_addr);
524+
daemon_conn_send(peer->daemon->master, take(msg));
525+
526+
tal_free(peer_alt_addr);
527+
}
528+
512529
/*~ <hello>I speak web socket</hello>.
513530
*
514531
* Actually that's dumb, websocket (aka rfc6455) looks nothing like that. */
@@ -1414,7 +1431,8 @@ static void connect_init(struct daemon *daemon, const u8 *msg)
14141431
&daemon->dev_fast_gossip,
14151432
&dev_disconnect,
14161433
&daemon->dev_no_ping_timer,
1417-
&daemon->dev_handshake_no_reply)) {
1434+
&daemon->dev_handshake_no_reply/* , //This is WIP
1435+
&daemon->alt_addr */)) {
14181436
/* This is a helper which prints the type expected and the actual
14191437
* message, then exits (it should never be called!). */
14201438
master_badmsg(WIRE_CONNECTD_INIT, msg);
@@ -1776,22 +1794,6 @@ static void try_connect_peer(struct daemon *daemon,
17761794
try_connect_one_addr(connect);
17771795
}
17781796

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-
17951797
/* lightningd tells us to connect to a peer by id, with optional addr hint. */
17961798
static void connect_to_peer(struct daemon *daemon, const u8 *msg)
17971799
{

connectd/connectd.h

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

6262
/* The pubkey of the node */
6363
struct node_id id;
64+
6465
/* Counters and keys for symmetric crypto */
6566
struct crypto_state cs;
6667

@@ -225,6 +226,9 @@ struct daemon {
225226
* resort, but doing so leaks our address so can be disabled. */
226227
bool use_dns;
227228

229+
// This is WIP
230+
/* u8 *alt_addr; */
231+
228232
/* The address that the broken response returns instead of
229233
* NXDOMAIN. NULL if we have not detected a broken resolver. */
230234
struct sockaddr *broken_resolver_response;
@@ -294,6 +298,6 @@ void destroy_peer(struct peer *peer);
294298
void close_random_connection(struct daemon *daemon);
295299

296300
/* Handles alternative address message from peer. */
297-
void handle_peer_alt_addr(struct peer *peer, const u8 *msg);
301+
void handle_peer_alt_address(struct peer *peer, const u8 *msg);
298302

299303
#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: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,9 @@ 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);
338341

339342
/* Don't stack, unless this is an instant reconnect */
340343
d = delayed_reconnect_map_get(ld->delayed_reconnect_map, id);
@@ -349,7 +352,7 @@ static void try_connect(const tal_t *ctx,
349352
d = tal(ctx, struct delayed_reconnect);
350353
d->ld = ld;
351354
d->id = *id;
352-
d->addrhint = tal_dup_or_null(d, struct wireaddr_internal, addrhint);
355+
d->addrhint = tal_dup_or_null(d, struct wireaddr_internal, alt_addr ? alt_addr : addrhint);
353356
d->dns_fallback = dns_fallback;
354357
delayed_reconnect_map_add(ld->delayed_reconnect_map, d);
355358
tal_add_destructor(d, destroy_delayed_reconnect);
@@ -364,9 +367,6 @@ static void try_connect(const tal_t *ctx,
364367
/* Update any channel billboards */
365368
peer = peer_by_id(ld, id);
366369
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????
370370
struct channel *channel;
371371
list_for_each(&peer->channels, channel, list) {
372372
if (!channel_state_wants_peercomms(channel->state))
@@ -559,20 +559,21 @@ static void handle_custommsg_in(struct lightningd *ld, const u8 *msg)
559559
plugin_hook_call_custommsg(ld, NULL, p);
560560
}
561561

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

568-
if (!fromwire_connectd_alt_address(tmpctx, msg, &node_id, &alt_addr)) {
567+
if (!fromwire_connectd_alt_address(tmpctx, msg, &peer_node_id, &peer_alt_addr)) {
569568
log_broken(ld->log, "Malformed peer_alt_addr_msg: %s",
570569
tal_hex(tmpctx, msg));
571570
return;
572571
}
573572

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

578579
static void connectd_start_shutdown_reply(struct subd *connectd,
@@ -666,7 +667,7 @@ static unsigned connectd_msg(struct subd *connectd, const u8 *msg, const int *fd
666667
break;
667668

668669
case WIRE_CONNECTD_ALT_ADDRESS:
669-
handle_alt_addr_in(connectd->ld, msg);
670+
handle_peer_alt_addr_in(connectd->ld, msg);
670671
break;
671672
}
672673
return 0;
@@ -788,7 +789,8 @@ int connectd_init(struct lightningd *ld)
788789
ld->dev_fast_gossip,
789790
ld->dev_disconnect_fd >= 0,
790791
ld->dev_no_ping_timer,
791-
ld->dev_handshake_no_reply);
792+
ld->dev_handshake_no_reply/* , //This is WIP
793+
ld->our_alt_addr */);
792794

793795
subd_req(ld->connectd, ld->connectd, take(msg), -1, 0,
794796
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)