Skip to content

Commit 821e28d

Browse files
committed
Changelog-Added: Introduce support for alternative addresses for peer connections
- Added new configuration parameters: alt-addr, alt-bind-addr, and alt-announce-addr. - Enhanced peer connection handling to use alternative addresses for selective private communications. - Added database fields to store alternative addresses for peers. - Implemented new RPC commands managing alternative addresses. - Updated tests to verify the behavior of alternative address handling in different scenarios. Signed-off-by: Max Rantil <rantil@pm.me>
1 parent 11586ab commit 821e28d

36 files changed

+1041
-283
lines changed

.msggen.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,6 +1589,7 @@
15891589
"ListConfigs.configs.addr": 47,
15901590
"ListConfigs.configs.alias": 30,
15911591
"ListConfigs.configs.allow-deprecated-apis": 14,
1592+
"ListConfigs.configs.alt-addr": 71,
15921593
"ListConfigs.configs.always-use-proxy": 17,
15931594
"ListConfigs.configs.announce-addr": 48,
15941595
"ListConfigs.configs.announce-addr-discovered": 54,
@@ -1672,6 +1673,10 @@
16721673
"ListConfigs.configs.allow-deprecated-apis.source": 2,
16731674
"ListConfigs.configs.allow-deprecated-apis.value_bool": 1
16741675
},
1676+
"ListconfigsConfigsAlt-addr": {
1677+
"ListConfigs.configs.alt-addr.sources[]": 2,
1678+
"ListConfigs.configs.alt-addr.values_str[]": 1
1679+
},
16751680
"ListconfigsConfigsAlways-use-proxy": {
16761681
"ListConfigs.configs.always-use-proxy.source": 2,
16771682
"ListConfigs.configs.always-use-proxy.value_bool": 1
@@ -6678,6 +6683,18 @@
66786683
"added": "pre-v0.10.1",
66796684
"deprecated": false
66806685
},
6686+
"ListConfigs.configs.alt-addr": {
6687+
"added": "v24.05",
6688+
"deprecated": false
6689+
},
6690+
"ListConfigs.configs.alt-addr.sources[]": {
6691+
"added": "v24.05",
6692+
"deprecated": false
6693+
},
6694+
"ListConfigs.configs.alt-addr.values_str[]": {
6695+
"added": "v24.05",
6696+
"deprecated": false
6697+
},
66816698
"ListConfigs.configs.always-use-proxy": {
66826699
"added": "pre-v0.10.1",
66836700
"deprecated": false

channeld/channeld.c

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ struct peer {
6868
bool channel_ready[NUM_SIDES];
6969
u64 next_index[NUM_SIDES];
7070

71+
/* ID of peer */
72+
struct node_id id;
73+
7174
/* --developer? */
7275
bool developer;
7376

@@ -191,9 +194,13 @@ struct peer {
191194

192195
/* --experimental-upgrade-protocol */
193196
bool experimental_upgrade;
197+
198+
/* Alt address for peer connections not publicly announced */
199+
u8 *our_alt_addr;
194200
};
195201

196202
static void start_commit_timer(struct peer *peer);
203+
static void send_peer_our_alt_addr(struct peer *peer);
197204

198205
static void billboard_update(const struct peer *peer)
199206
{
@@ -535,6 +542,16 @@ static void handle_peer_splice_locked(struct peer *peer, const u8 *msg)
535542
check_mutual_splice_locked(peer);
536543
}
537544

545+
static void send_peer_our_alt_addr(struct peer *peer)
546+
{
547+
struct pubkey node_id;
548+
549+
if (pubkey_from_node_id(&node_id, &peer->id)) {
550+
u8 *msg = towire_peer_alt_addr(peer, &node_id, peer->our_alt_addr);
551+
peer_write(peer->pps, take(msg));
552+
}
553+
}
554+
538555
static void handle_peer_channel_ready(struct peer *peer, const u8 *msg)
539556
{
540557
struct channel_id chanid;
@@ -4163,6 +4180,9 @@ static void peer_in(struct peer *peer, const u8 *msg)
41634180

41644181
check_tx_abort(peer, msg);
41654182

4183+
if (peer->our_alt_addr)
4184+
send_peer_our_alt_addr(peer);
4185+
41664186
/* If we're in STFU mode and aren't waiting for a STFU mode
41674187
* specific message, the only valid message was tx_abort */
41684188
if (is_stfu_active(peer) && !peer->stfu_wait_single_msg) {
@@ -4295,6 +4315,7 @@ static void peer_in(struct peer *peer, const u8 *msg)
42954315
case WIRE_ONION_MESSAGE:
42964316
case WIRE_PEER_STORAGE:
42974317
case WIRE_YOUR_PEER_STORAGE:
4318+
case WIRE_PEER_ALT_ADDR:
42984319
abort();
42994320
}
43004321

@@ -5682,11 +5703,29 @@ static void handle_dev_quiesce(struct peer *peer, const u8 *msg)
56825703
maybe_send_stfu(peer);
56835704
}
56845705

5706+
static void handle_channeld_alt_addr(struct peer *peer, const u8 *msg)
5707+
{
5708+
struct pubkey peer_pk;
5709+
u8 *our_alt_addr;
5710+
5711+
if (!fromwire_channeld_alt_addr(peer, msg, &peer_pk, &our_alt_addr)) {
5712+
master_badmsg(WIRE_CHANNELD_ALT_ADDR, msg);
5713+
}
5714+
5715+
if (pubkey_from_node_id(&peer_pk, &peer->id)) {
5716+
u8 *peer_msg = towire_peer_alt_addr(peer, &peer_pk, our_alt_addr);
5717+
peer_write(peer->pps, take(peer_msg));
5718+
}
5719+
}
5720+
56855721
static void req_in(struct peer *peer, const u8 *msg)
56865722
{
56875723
enum channeld_wire t = fromwire_peektype(msg);
56885724

56895725
switch (t) {
5726+
case WIRE_CHANNELD_ALT_ADDR:
5727+
handle_channeld_alt_addr(peer, msg);
5728+
return;
56905729
case WIRE_CHANNELD_FUNDING_DEPTH:
56915730
handle_funding_depth(peer, msg);
56925731
return;
@@ -5869,7 +5908,9 @@ static void init_channel(struct peer *peer)
58695908
&reestablish_only,
58705909
&peer->experimental_upgrade,
58715910
&peer->splice_state->inflights,
5872-
&peer->local_alias)) {
5911+
&peer->local_alias,
5912+
&peer->our_alt_addr,
5913+
&peer->id)) {
58735914
master_badmsg(WIRE_CHANNELD_INIT, msg);
58745915
}
58755916

channeld/channeld_wire.csv

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ msgdata,channeld_init,experimental_upgrade,bool,
7777
msgdata,channeld_init,num_inflights,u16,
7878
msgdata,channeld_init,inflights,inflight,num_inflights
7979
msgdata,channeld_init,scid_alias,short_channel_id,
80+
msgdata,channeld_init,alt_addr_len,u16,
81+
msgdata,channeld_init,alt_addr,u8,alt_addr_len,
82+
msgdata,channeld_init,id,node_id,
8083

8184
# channeld->lightningd: successfully negotated reestablishment.
8285
msgtype,channeld_reestablished,1101
@@ -349,3 +352,9 @@ msgdata,channeld_upgraded,new_type,channel_type,
349352
# Tell peer about our latest and greatest blockheight.
350353
msgtype,channeld_blockheight,1012
351354
msgdata,channeld_blockheight,blockheight,u32,
355+
356+
# master -> channeld Send peer alternative addresses
357+
msgtype,channeld_alt_addr,1014
358+
msgdata,channeld_alt_addr,node_id,point,
359+
msgdata,channeld_alt_addr,alt_addr_len,u16,
360+
msgdata,channeld_alt_addr,alt_addr,u8,alt_addr_len,

cln-grpc/proto/node.proto

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cln-grpc/src/convert.rs

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cln-rpc/src/model.rs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/interactivetx.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ static u8 *read_next_msg(const tal_t *ctx,
186186
case WIRE_SPLICE:
187187
case WIRE_SPLICE_ACK:
188188
case WIRE_SPLICE_LOCKED:
189+
case WIRE_PEER_ALT_ADDR:
189190
*error = tal_fmt(ctx,
190191
"Received invalid message from peer: %d", t);
191192
return NULL;
@@ -736,6 +737,7 @@ char *process_interactivetx_updates(const tal_t *ctx,
736737
case WIRE_SPLICE_ACK:
737738
case WIRE_STFU:
738739
case WIRE_SPLICE_LOCKED:
740+
case WIRE_PEER_ALT_ADDR:
739741
return tal_fmt(ctx, "Unexpected wire message %s",
740742
tal_hex(ctx, msg));
741743
}

connectd/connectd.c

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include <sys/types.h>
4949
#include <sys/wait.h>
5050
#include <unistd.h>
51+
#include <wire/peer_wiregen.h>
5152
#include <wire/wire_io.h>
5253
#include <wire/wire_sync.h>
5354

@@ -316,6 +317,26 @@ static struct io_plan *handshake_in_success(struct io_conn *conn,
316317
struct node_id id;
317318
node_id_from_pubkey(&id, id_key);
318319
status_peer_debug(&id, "Connect IN");
320+
321+
/* FIXME(maxrantil): loop all alt_bind_addr after making it an array (code down-under)*/
322+
/* bool is_whitelisted = false;
323+
324+
if (daemon->alt_bind_addr) {
325+
for (size_t i = 0; i < tal_count(daemon->alt_bind_addr); i++) {
326+
if (strcmp(incoming_addr, (char *)daemon->alt_bind_addr[i]) == 0) {
327+
is_whitelisted = true;
328+
break;
329+
}
330+
}
331+
} */
332+
333+
char *incoming_addr = fmt_wireaddr_internal(tmpctx, addr);
334+
335+
/* Confirm that peer connects to the alt-bind-addr you sent */
336+
if (daemon->alt_bind_addr)
337+
if (strcmp(incoming_addr, (char *)daemon->alt_bind_addr) == 0)
338+
towire_connectd_alt_addr_whitelist(tmpctx, id_key, (u8 *)incoming_addr);
339+
319340
return peer_exchange_initmsg(conn, daemon, daemon->our_features,
320341
cs, &id, addr, timeout, is_websocket, true);
321342
}
@@ -505,9 +526,26 @@ static struct io_plan *connection_in(struct io_conn *conn,
505526

506527
conn_in_arg.daemon = daemon;
507528
conn_in_arg.is_websocket = false;
529+
508530
return conn_in(conn, &conn_in_arg);
509531
}
510532

533+
void handle_peer_alt_addr(struct peer *peer, const u8 *msg)
534+
{
535+
u8 *peer_alt_addr;
536+
struct pubkey peer_id;
537+
/* FIXME(maxrantil): u32 *timestamp = NULL; */
538+
539+
if (!fromwire_peer_alt_addr(peer, msg, &peer_id, &peer_alt_addr/* FIXME(maxrantil): , timestamp */)) {
540+
master_badmsg(WIRE_PEER_ALT_ADDR, msg);
541+
}
542+
543+
u8 *fwd_msg = towire_connectd_alt_addr(NULL, &peer_id, peer_alt_addr);
544+
daemon_conn_send(peer->daemon->master, take(fwd_msg));
545+
546+
tal_free(peer_alt_addr); /* FIXME(maxrantil): Investigare further on freeing like this */
547+
}
548+
511549
/*~ <hello>I speak web socket</hello>.
512550
*
513551
* Actually that's dumb, websocket (aka rfc6455) looks nothing like that. */
@@ -1413,7 +1451,8 @@ static void connect_init(struct daemon *daemon, const u8 *msg)
14131451
&daemon->dev_fast_gossip,
14141452
&dev_disconnect,
14151453
&daemon->dev_no_ping_timer,
1416-
&daemon->dev_handshake_no_reply)) {
1454+
&daemon->dev_handshake_no_reply,
1455+
&daemon->alt_bind_addr)) {
14171456
/* This is a helper which prints the type expected and the actual
14181457
* message, then exits (it should never be called!). */
14191458
master_badmsg(WIRE_CONNECTD_INIT, msg);
@@ -2059,6 +2098,41 @@ static void dev_exhaust_fds(struct daemon *daemon, const u8 *msg)
20592098
daemon->dev_exhausted_fds = true;
20602099
}
20612100

2101+
static void handle_alt_addr_whitelist_reply(struct daemon *daemon, const u8 *msg)
2102+
{
2103+
struct pubkey p_pk;
2104+
struct peer *peer;
2105+
u8 *incoming_addr;
2106+
bool is_whitelisted;
2107+
2108+
if (!fromwire_connectd_alt_addr_whitelist_reply(tmpctx,
2109+
msg, &p_pk, &incoming_addr, &is_whitelisted)) {
2110+
master_badmsg(WIRE_CONNECTD_ALT_ADDR_WHITELIST_REPLY, msg);
2111+
return;
2112+
}
2113+
2114+
struct node_id id;
2115+
node_id_from_pubkey(&id, &p_pk);
2116+
peer = peer_htable_get(daemon->peers, &id);
2117+
2118+
if (!peer) {
2119+
status_broken("handle_alt_addr_whitelist_reply: Could not find peer for node_id: %s",
2120+
fmt_node_id(tmpctx, &id));
2121+
return;
2122+
}
2123+
2124+
if (is_whitelisted) {
2125+
status_peer_unusual(&peer->id,
2126+
"Peer's address %s is in the whitelist. Accepting connection.",
2127+
incoming_addr);
2128+
} else {
2129+
status_peer_unusual(&peer->id,
2130+
"Connection attempt from address %s which is not in the whitelist. The peer has not received an alternative address from me. Closing connection.",
2131+
incoming_addr);
2132+
io_close(peer->to_peer);
2133+
}
2134+
}
2135+
20622136
static struct io_plan *recv_peer_connect_subd(struct io_conn *conn,
20632137
const u8 *msg,
20642138
int fd,
@@ -2145,6 +2219,11 @@ static struct io_plan *recv_req(struct io_conn *conn,
21452219
dev_exhaust_fds(daemon, msg);
21462220
goto out;
21472221
}
2222+
case WIRE_CONNECTD_ALT_ADDR_WHITELIST_REPLY:
2223+
if (daemon->developer) {
2224+
handle_alt_addr_whitelist_reply(daemon, msg);
2225+
goto out;
2226+
}
21482227
/* Fall thru */
21492228
/* We send these, we don't receive them */
21502229
case WIRE_CONNECTD_INIT_REPLY:
@@ -2158,6 +2237,8 @@ static struct io_plan *recv_req(struct io_conn *conn,
21582237
case WIRE_CONNECTD_CUSTOMMSG_IN:
21592238
case WIRE_CONNECTD_PEER_DISCONNECT_DONE:
21602239
case WIRE_CONNECTD_START_SHUTDOWN_REPLY:
2240+
case WIRE_CONNECTD_ALT_ADDR:
2241+
case WIRE_CONNECTD_ALT_ADDR_WHITELIST:
21612242
break;
21622243
}
21632244

0 commit comments

Comments
 (0)