Skip to content

Commit 131a324

Browse files
committed
WIP, the saving of 'our_alt_addr' to our own db is not working.
This is just for showing the process before mid-term evaluation. Signed-off-by: Max Rantil <[email protected]>
1 parent b9721c3 commit 131a324

File tree

12 files changed

+270
-138
lines changed

12 files changed

+270
-138
lines changed

.msggen.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6695,39 +6695,39 @@
66956695
"deprecated": false
66966696
},
66976697
"ListConfigs.configs.alt-addr": {
6698-
"added": "v24.05",
6698+
"added": "v24.08",
66996699
"deprecated": false
67006700
},
67016701
"ListConfigs.configs.alt-addr.sources[]": {
6702-
"added": "v24.05",
6702+
"added": "v24.08",
67036703
"deprecated": false
67046704
},
67056705
"ListConfigs.configs.alt-addr.values_str[]": {
6706-
"added": "v24.05",
6706+
"added": "v24.08",
67076707
"deprecated": false
67086708
},
67096709
"ListConfigs.configs.alt-announce-addr": {
6710-
"added": "v24.05",
6710+
"added": "v24.08",
67116711
"deprecated": false
67126712
},
67136713
"ListConfigs.configs.alt-announce-addr.sources[]": {
6714-
"added": "v24.05",
6714+
"added": "v24.08",
67156715
"deprecated": false
67166716
},
67176717
"ListConfigs.configs.alt-announce-addr.values_str[]": {
6718-
"added": "v24.05",
6718+
"added": "v24.08",
67196719
"deprecated": false
67206720
},
67216721
"ListConfigs.configs.alt-bind-addr": {
6722-
"added": "v24.05",
6722+
"added": "v24.08",
67236723
"deprecated": false
67246724
},
67256725
"ListConfigs.configs.alt-bind-addr.sources[]": {
6726-
"added": "v24.05",
6726+
"added": "v24.08",
67276727
"deprecated": false
67286728
},
67296729
"ListConfigs.configs.alt-bind-addr.values_str[]": {
6730-
"added": "v24.05",
6730+
"added": "v24.08",
67316731
"deprecated": false
67326732
},
67336733
"ListConfigs.configs.always-use-proxy": {
@@ -8799,7 +8799,7 @@
87998799
"deprecated": false
88008800
},
88018801
"ListPeers.peers[].alt_addrs[]": {
8802-
"added": "v24.05",
8802+
"added": "v24.08",
88038803
"deprecated": false
88048804
},
88058805
"ListPeers.peers[].channels[]": {
@@ -11659,4 +11659,4 @@
1165911659
"deprecated": false
1166011660
}
1166111661
}
11662-
}
11662+
}

channeld/channeld.c

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -544,12 +544,21 @@ static void handle_peer_splice_locked(struct peer *peer, const u8 *msg)
544544

545545
static void send_peer_our_alt_addr(struct peer *peer)
546546
{
547-
struct pubkey node_id;
547+
struct pubkey p_pk;
548548

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));
549+
if (pubkey_from_node_id(&p_pk, &peer->id)) {
550+
/* Send our alt addr to peer db - because who doesn't love secret addresses? */
551+
u8 *peer_msg = towire_peer_alt_addr(peer, &p_pk,
552+
peer->our_alt_addr); /* FIXME(maxrantil): Make addr(s)! */
553+
peer_write(peer->pps, take(peer_msg));
554+
555+
/* We need the addrs in our own db too for later whitelist confirmation */
556+
u8 *msg = towire_channeld_our_alt_addrs(tmpctx, &p_pk,
557+
peer->our_alt_addr);
558+
wire_sync_write(MASTER_FD, take(msg));
552559
}
560+
561+
/* FIXME(maxrantil): Do we only free in the fromwire_() or here too?? test */
553562
}
554563

555564
static void handle_peer_channel_ready(struct peer *peer, const u8 *msg)
@@ -5703,28 +5712,33 @@ static void handle_dev_quiesce(struct peer *peer, const u8 *msg)
57035712
maybe_send_stfu(peer);
57045713
}
57055714

5706-
static void handle_channeld_alt_addr(struct peer *peer, const u8 *msg)
5715+
static void handle_channeld_peer_alt_addrs(struct peer *peer, const u8 *msg)
57075716
{
5708-
struct pubkey peer_pk;
5717+
struct pubkey p_pk;
57095718
u8 *our_alt_addr;
57105719

5711-
if (!fromwire_channeld_alt_addr(peer, msg, &peer_pk, &our_alt_addr)) {
5712-
master_badmsg(WIRE_CHANNELD_ALT_ADDR, msg);
5713-
}
5720+
if (!fromwire_channeld_peer_alt_addrs(peer, msg, &p_pk, &our_alt_addr))
5721+
master_badmsg(WIRE_CHANNELD_PEER_ALT_ADDRS, msg);
57145722

5715-
if (pubkey_from_node_id(&peer_pk, &peer->id)) {
5716-
u8 *peer_msg = towire_peer_alt_addr(peer, &peer_pk, our_alt_addr);
5723+
if (pubkey_from_node_id(&p_pk, &peer->id)) {
5724+
u8 *peer_msg = towire_peer_alt_addr(peer,
5725+
&p_pk,
5726+
our_alt_addr);
57175727
peer_write(peer->pps, take(peer_msg));
57185728
}
5729+
5730+
/* FIXME(maxrantil): free p_pk & our_alt_addr */
57195731
}
57205732

5733+
#include <stdio.h>
57215734
static void req_in(struct peer *peer, const u8 *msg)
57225735
{
57235736
enum channeld_wire t = fromwire_peektype(msg);
57245737

57255738
switch (t) {
5726-
case WIRE_CHANNELD_ALT_ADDR:
5727-
handle_channeld_alt_addr(peer, msg);
5739+
case WIRE_CHANNELD_PEER_ALT_ADDRS:
5740+
fprintf(stderr, "WIRE_CHANNELD_PEER_ALT_ADDRS message recieved\n");
5741+
handle_channeld_peer_alt_addrs(peer, msg);
57285742
return;
57295743
case WIRE_CHANNELD_FUNDING_DEPTH:
57305744
handle_funding_depth(peer, msg);
@@ -5823,6 +5837,7 @@ static void req_in(struct peer *peer, const u8 *msg)
58235837
case WIRE_CHANNELD_SPLICE_STATE_ERROR:
58245838
case WIRE_CHANNELD_LOCAL_ANCHOR_INFO:
58255839
case WIRE_CHANNELD_REESTABLISHED:
5840+
case WIRE_CHANNELD_OUR_ALT_ADDRS:
58265841
break;
58275842
}
58285843
master_badmsg(-1, msg);

channeld/channeld_wire.csv

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,14 @@ msgdata,channeld_upgraded,new_type,channel_type,
353353
msgtype,channeld_blockheight,1012
354354
msgdata,channeld_blockheight,blockheight,u32,
355355

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,
356+
# master -> channeld: send peer our alternative addresses
357+
msgtype,channeld_peer_alt_addrs,1035
358+
msgdata,channeld_peer_alt_addrs,node_id,point,
359+
msgdata,channeld_peer_alt_addrs,alt_addr_len,u16,
360+
msgdata,channeld_peer_alt_addrs,alt_addr,u8,alt_addr_len,
361+
362+
# channeld -> master: send our alternative addresses to our db
363+
msgtype,channeld_our_alt_addrs,1037
364+
msgdata,channeld_our_alt_addrs,node_id,point,
365+
msgdata,channeld_our_alt_addrs,alt_addr_len,u16,
366+
msgdata,channeld_our_alt_addrs,alt_addr,u8,alt_addr_len,

connectd/connectd.c

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -318,19 +318,44 @@ static struct io_plan *handshake_in_success(struct io_conn *conn,
318318
node_id_from_pubkey(&id, id_key);
319319
status_peer_debug(&id, "Connect IN");
320320

321+
// Retrieve and print local addresses and ports
322+
struct sockaddr_in local_addr;
323+
socklen_t addr_len = sizeof(struct sockaddr_in);
324+
325+
// Get local address and port
326+
if (getsockname(io_conn_fd(conn), (struct sockaddr *)&local_addr, &addr_len) == -1) {
327+
perror("getsockname failed");
328+
} else {
329+
fprintf(stderr, "--> Listening on %s:%d\n",
330+
inet_ntoa(local_addr.sin_addr),
331+
ntohs(local_addr.sin_port));
332+
}
333+
321334
/* Confirm that peer connects to the alt-bind-addr you sent */
322335
if (daemon->alt_bind_addr) {
323-
char *incoming_addr = fmt_wireaddr_internal(tmpctx, addr);
324-
fprintf(stderr, "--> incoming connection addr: '%s'\n", incoming_addr);
325-
326-
char *bind_addrs = tal_strdup(NULL, (char *)daemon->alt_bind_addr);
336+
char listening_addr[INET_ADDRSTRLEN];
337+
inet_ntop(AF_INET, &(local_addr.sin_addr), listening_addr, INET_ADDRSTRLEN);
338+
int listening_port = ntohs(local_addr.sin_port);
339+
340+
// Create a string with the full address including port
341+
char full_listening_addr[INET_ADDRSTRLEN + 6]; // Extra space for ':port'
342+
snprintf(full_listening_addr,
343+
sizeof(full_listening_addr),
344+
"%s:%d",
345+
listening_addr,
346+
listening_port);
347+
348+
char *bind_addrs = tal_strdup(tmpctx, (char *)daemon->alt_bind_addr);
327349
char *token = strtok(bind_addrs, ",");
328350

329351
while (token != NULL) {
330-
fprintf(stderr, "--> comparing '%s' with token '%s'\n", incoming_addr, token);
331-
if (strcmp(incoming_addr, token) == 0) {
352+
fprintf(stderr, "--> comparing '%s' with token '%s'\n", full_listening_addr, token);
353+
if (strcmp(full_listening_addr, token) == 0) {
354+
char *whitelist_addr = tal_strdup(tmpctx, full_listening_addr);
332355
fprintf(stderr, "--> INSIDE\n");
333-
towire_connectd_alt_addr_whitelist(tmpctx, id_key, (u8 *)incoming_addr);
356+
u8 *msg = towire_connectd_alt_addr_whitelist(NULL, id_key, (u8 *)whitelist_addr);
357+
daemon_conn_send(daemon->master, take(msg));
358+
tal_free(whitelist_addr);
334359
break;
335360
}
336361
token = strtok(NULL, ",");
@@ -533,18 +558,16 @@ static struct io_plan *connection_in(struct io_conn *conn,
533558

534559
void handle_peer_alt_addr(struct peer *peer, const u8 *msg)
535560
{
536-
u8 *peer_alt_addr;
537-
struct pubkey peer_id;
538-
/* FIXME(maxrantil): u32 *timestamp = NULL; */
561+
u8 *p_alt_addrs;
562+
struct pubkey p_id;
539563

540-
if (!fromwire_peer_alt_addr(peer, msg, &peer_id, &peer_alt_addr/* FIXME(maxrantil): , timestamp */)) {
564+
if (!fromwire_peer_alt_addr(peer, msg, &p_id, &p_alt_addrs))
541565
master_badmsg(WIRE_PEER_ALT_ADDR, msg);
542-
}
543566

544-
u8 *fwd_msg = towire_connectd_alt_addr(NULL, &peer_id, peer_alt_addr);
567+
u8 *fwd_msg = towire_connectd_peer_alt_addr(tmpctx, &p_id, p_alt_addrs);
545568
daemon_conn_send(peer->daemon->master, take(fwd_msg));
546569

547-
tal_free(peer_alt_addr); /* FIXME(maxrantil): Investigare further on freeing like this */
570+
tal_free(p_alt_addrs); /* FIXME(maxrantil): Investigare further on freeing like this */
548571
}
549572

550573
/*~ <hello>I speak web socket</hello>.
@@ -2127,18 +2150,18 @@ static void dev_exhaust_fds(struct daemon *daemon, const u8 *msg)
21272150
daemon->dev_exhausted_fds = true;
21282151
}
21292152

2130-
#include <stdio.h>
21312153
static void handle_alt_addr_whitelist_reply(struct daemon *daemon, const u8 *msg)
21322154
{
21332155
struct pubkey p_pk;
21342156
struct peer *peer;
21352157
u8 *incoming_addr;
21362158
bool is_whitelisted;
21372159

2138-
fprintf(stderr, "INSIDE WHITELIST\n");
2139-
21402160
if (!fromwire_connectd_alt_addr_whitelist_reply(tmpctx,
2141-
msg, &p_pk, &incoming_addr, &is_whitelisted)) {
2161+
msg,
2162+
&p_pk,
2163+
&incoming_addr,
2164+
&is_whitelisted)) {
21422165
master_badmsg(WIRE_CONNECTD_ALT_ADDR_WHITELIST_REPLY, msg);
21432166
return;
21442167
}
@@ -2155,12 +2178,14 @@ static void handle_alt_addr_whitelist_reply(struct daemon *daemon, const u8 *msg
21552178

21562179
if (is_whitelisted) {
21572180
status_peer_unusual(&peer->id,
2158-
"Peer's address %s is in the whitelist. Accepting connection.",
2159-
incoming_addr);
2181+
"Peer's address %s is in the whitelist. Accepting connection.",
2182+
incoming_addr);
2183+
fprintf(stderr, "END GOAL! YES\n");
21602184
} else {
21612185
status_peer_unusual(&peer->id,
2162-
"Connection attempt from address %s which is not in the whitelist. The peer has not received an alternative address from me. Closing connection.",
2163-
incoming_addr);
2186+
"Connection attempt from address %s which is not in the whitelist. The peer has not received an alternative address from me. Closing connection.",
2187+
incoming_addr);
2188+
fprintf(stderr, "END GOAL! NO\n");
21642189
io_close(peer->to_peer);
21652190
}
21662191
}
@@ -2267,7 +2292,7 @@ static struct io_plan *recv_req(struct io_conn *conn,
22672292
case WIRE_CONNECTD_CUSTOMMSG_IN:
22682293
case WIRE_CONNECTD_PEER_DISCONNECT_DONE:
22692294
case WIRE_CONNECTD_START_SHUTDOWN_REPLY:
2270-
case WIRE_CONNECTD_ALT_ADDR:
2295+
case WIRE_CONNECTD_PEER_ALT_ADDR:
22712296
case WIRE_CONNECTD_ALT_ADDR_WHITELIST:
22722297
break;
22732298
}

connectd/connectd_wire.csv

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,10 @@ msgtype,connectd_dev_suppress_gossip,2032
173173
msgtype,connectd_dev_exhaust_fds,2036
174174

175175
# connectd -> master: alternative connection address
176-
msgtype,connectd_alt_addr,2037
177-
msgdata,connectd_alt_addr,node_id,point,
178-
msgdata,connectd_alt_addr,addr_len,u8,
179-
msgdata,connectd_alt_addr,addr,byte,addr_len,
176+
msgtype,connectd_peer_alt_addr,2037
177+
msgdata,connectd_peer_alt_addr,node_id,point,
178+
msgdata,connectd_peer_alt_addr,addr_len,u8,
179+
msgdata,connectd_peer_alt_addr,addr,byte,addr_len,
180180

181181
# connectd -> master: alternative connection whitelist
182182
msgtype,connectd_alt_addr_whitelist,2038

contrib/startup_regtest.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,15 @@ start_nodes() {
177177

178178
for i in $(seq "$node_count"); do
179179
socket=$(( 7070 + i * 101))
180+
alt_port=$(( 7070 + i * 102))
180181
mkdir -p "$LIGHTNING_DIR/l$i"
181182
# Node config
182183
cat <<- EOF > "$LIGHTNING_DIR/l$i/config"
183184
network=$network
184185
log-level=debug
185186
log-file=$LIGHTNING_DIR/l$i/log
186187
addr=localhost:$socket
187-
alt-addr=127.0.0.21:$socket
188+
alt-addr=127.0.0.21:$alt_port
188189
allow-deprecated-apis=false
189190
developer
190191
dev-fast-gossip

0 commit comments

Comments
 (0)