Skip to content

Commit

Permalink
WIP, the saving of 'our_alt_addr' to our own db is not working.
Browse files Browse the repository at this point in the history
This is just for showing the process before mid-term evaluation.

Signed-off-by: Max Rantil <[email protected]>
  • Loading branch information
maxrantil committed Jul 15, 2024
1 parent b9721c3 commit 131a324
Show file tree
Hide file tree
Showing 12 changed files with 270 additions and 138 deletions.
22 changes: 11 additions & 11 deletions .msggen.json
Original file line number Diff line number Diff line change
Expand Up @@ -6695,39 +6695,39 @@
"deprecated": false
},
"ListConfigs.configs.alt-addr": {
"added": "v24.05",
"added": "v24.08",
"deprecated": false
},
"ListConfigs.configs.alt-addr.sources[]": {
"added": "v24.05",
"added": "v24.08",
"deprecated": false
},
"ListConfigs.configs.alt-addr.values_str[]": {
"added": "v24.05",
"added": "v24.08",
"deprecated": false
},
"ListConfigs.configs.alt-announce-addr": {
"added": "v24.05",
"added": "v24.08",
"deprecated": false
},
"ListConfigs.configs.alt-announce-addr.sources[]": {
"added": "v24.05",
"added": "v24.08",
"deprecated": false
},
"ListConfigs.configs.alt-announce-addr.values_str[]": {
"added": "v24.05",
"added": "v24.08",
"deprecated": false
},
"ListConfigs.configs.alt-bind-addr": {
"added": "v24.05",
"added": "v24.08",
"deprecated": false
},
"ListConfigs.configs.alt-bind-addr.sources[]": {
"added": "v24.05",
"added": "v24.08",
"deprecated": false
},
"ListConfigs.configs.alt-bind-addr.values_str[]": {
"added": "v24.05",
"added": "v24.08",
"deprecated": false
},
"ListConfigs.configs.always-use-proxy": {
Expand Down Expand Up @@ -8799,7 +8799,7 @@
"deprecated": false
},
"ListPeers.peers[].alt_addrs[]": {
"added": "v24.05",
"added": "v24.08",
"deprecated": false
},
"ListPeers.peers[].channels[]": {
Expand Down Expand Up @@ -11659,4 +11659,4 @@
"deprecated": false
}
}
}
}
41 changes: 28 additions & 13 deletions channeld/channeld.c
Original file line number Diff line number Diff line change
Expand Up @@ -544,12 +544,21 @@ static void handle_peer_splice_locked(struct peer *peer, const u8 *msg)

static void send_peer_our_alt_addr(struct peer *peer)
{
struct pubkey node_id;
struct pubkey p_pk;

if (pubkey_from_node_id(&node_id, &peer->id)) {
u8 *msg = towire_peer_alt_addr(peer, &node_id, peer->our_alt_addr);
peer_write(peer->pps, take(msg));
if (pubkey_from_node_id(&p_pk, &peer->id)) {
/* Send our alt addr to peer db - because who doesn't love secret addresses? */
u8 *peer_msg = towire_peer_alt_addr(peer, &p_pk,
peer->our_alt_addr); /* FIXME(maxrantil): Make addr(s)! */
peer_write(peer->pps, take(peer_msg));

/* We need the addrs in our own db too for later whitelist confirmation */
u8 *msg = towire_channeld_our_alt_addrs(tmpctx, &p_pk,
peer->our_alt_addr);
wire_sync_write(MASTER_FD, take(msg));
}

/* FIXME(maxrantil): Do we only free in the fromwire_() or here too?? test */
}

static void handle_peer_channel_ready(struct peer *peer, const u8 *msg)
Expand Down Expand Up @@ -5703,28 +5712,33 @@ static void handle_dev_quiesce(struct peer *peer, const u8 *msg)
maybe_send_stfu(peer);
}

static void handle_channeld_alt_addr(struct peer *peer, const u8 *msg)
static void handle_channeld_peer_alt_addrs(struct peer *peer, const u8 *msg)
{
struct pubkey peer_pk;
struct pubkey p_pk;
u8 *our_alt_addr;

if (!fromwire_channeld_alt_addr(peer, msg, &peer_pk, &our_alt_addr)) {
master_badmsg(WIRE_CHANNELD_ALT_ADDR, msg);
}
if (!fromwire_channeld_peer_alt_addrs(peer, msg, &p_pk, &our_alt_addr))
master_badmsg(WIRE_CHANNELD_PEER_ALT_ADDRS, msg);

if (pubkey_from_node_id(&peer_pk, &peer->id)) {
u8 *peer_msg = towire_peer_alt_addr(peer, &peer_pk, our_alt_addr);
if (pubkey_from_node_id(&p_pk, &peer->id)) {
u8 *peer_msg = towire_peer_alt_addr(peer,
&p_pk,
our_alt_addr);
peer_write(peer->pps, take(peer_msg));
}

/* FIXME(maxrantil): free p_pk & our_alt_addr */
}

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

switch (t) {
case WIRE_CHANNELD_ALT_ADDR:
handle_channeld_alt_addr(peer, msg);
case WIRE_CHANNELD_PEER_ALT_ADDRS:
fprintf(stderr, "WIRE_CHANNELD_PEER_ALT_ADDRS message recieved\n");
handle_channeld_peer_alt_addrs(peer, msg);
return;
case WIRE_CHANNELD_FUNDING_DEPTH:
handle_funding_depth(peer, msg);
Expand Down Expand Up @@ -5823,6 +5837,7 @@ static void req_in(struct peer *peer, const u8 *msg)
case WIRE_CHANNELD_SPLICE_STATE_ERROR:
case WIRE_CHANNELD_LOCAL_ANCHOR_INFO:
case WIRE_CHANNELD_REESTABLISHED:
case WIRE_CHANNELD_OUR_ALT_ADDRS:
break;
}
master_badmsg(-1, msg);
Expand Down
16 changes: 11 additions & 5 deletions channeld/channeld_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,14 @@ msgdata,channeld_upgraded,new_type,channel_type,
msgtype,channeld_blockheight,1012
msgdata,channeld_blockheight,blockheight,u32,

# master -> channeld Send peer alternative addresses
msgtype,channeld_alt_addr,1014
msgdata,channeld_alt_addr,node_id,point,
msgdata,channeld_alt_addr,alt_addr_len,u16,
msgdata,channeld_alt_addr,alt_addr,u8,alt_addr_len,
# master -> channeld: send peer our alternative addresses
msgtype,channeld_peer_alt_addrs,1035
msgdata,channeld_peer_alt_addrs,node_id,point,
msgdata,channeld_peer_alt_addrs,alt_addr_len,u16,
msgdata,channeld_peer_alt_addrs,alt_addr,u8,alt_addr_len,

# channeld -> master: send our alternative addresses to our db
msgtype,channeld_our_alt_addrs,1037
msgdata,channeld_our_alt_addrs,node_id,point,
msgdata,channeld_our_alt_addrs,alt_addr_len,u16,
msgdata,channeld_our_alt_addrs,alt_addr,u8,alt_addr_len,
71 changes: 48 additions & 23 deletions connectd/connectd.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,19 +318,44 @@ static struct io_plan *handshake_in_success(struct io_conn *conn,
node_id_from_pubkey(&id, id_key);
status_peer_debug(&id, "Connect IN");

// Retrieve and print local addresses and ports
struct sockaddr_in local_addr;
socklen_t addr_len = sizeof(struct sockaddr_in);

// Get local address and port
if (getsockname(io_conn_fd(conn), (struct sockaddr *)&local_addr, &addr_len) == -1) {
perror("getsockname failed");
} else {
fprintf(stderr, "--> Listening on %s:%d\n",
inet_ntoa(local_addr.sin_addr),
ntohs(local_addr.sin_port));
}

/* Confirm that peer connects to the alt-bind-addr you sent */
if (daemon->alt_bind_addr) {
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 listening_addr[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(local_addr.sin_addr), listening_addr, INET_ADDRSTRLEN);
int listening_port = ntohs(local_addr.sin_port);

// Create a string with the full address including port
char full_listening_addr[INET_ADDRSTRLEN + 6]; // Extra space for ':port'
snprintf(full_listening_addr,
sizeof(full_listening_addr),
"%s:%d",
listening_addr,
listening_port);

char *bind_addrs = tal_strdup(tmpctx, (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, "--> comparing '%s' with token '%s'\n", full_listening_addr, token);
if (strcmp(full_listening_addr, token) == 0) {
char *whitelist_addr = tal_strdup(tmpctx, full_listening_addr);
fprintf(stderr, "--> INSIDE\n");
towire_connectd_alt_addr_whitelist(tmpctx, id_key, (u8 *)incoming_addr);
u8 *msg = towire_connectd_alt_addr_whitelist(NULL, id_key, (u8 *)whitelist_addr);
daemon_conn_send(daemon->master, take(msg));
tal_free(whitelist_addr);
break;
}
token = strtok(NULL, ",");
Expand Down Expand Up @@ -533,18 +558,16 @@ static struct io_plan *connection_in(struct io_conn *conn,

void handle_peer_alt_addr(struct peer *peer, const u8 *msg)
{
u8 *peer_alt_addr;
struct pubkey peer_id;
/* FIXME(maxrantil): u32 *timestamp = NULL; */
u8 *p_alt_addrs;
struct pubkey p_id;

if (!fromwire_peer_alt_addr(peer, msg, &peer_id, &peer_alt_addr/* FIXME(maxrantil): , timestamp */)) {
if (!fromwire_peer_alt_addr(peer, msg, &p_id, &p_alt_addrs))
master_badmsg(WIRE_PEER_ALT_ADDR, msg);
}

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

tal_free(peer_alt_addr); /* FIXME(maxrantil): Investigare further on freeing like this */
tal_free(p_alt_addrs); /* FIXME(maxrantil): Investigare further on freeing like this */
}

/*~ <hello>I speak web socket</hello>.
Expand Down Expand Up @@ -2127,18 +2150,18 @@ 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)) {
msg,
&p_pk,
&incoming_addr,
&is_whitelisted)) {
master_badmsg(WIRE_CONNECTD_ALT_ADDR_WHITELIST_REPLY, msg);
return;
}
Expand All @@ -2155,12 +2178,14 @@ static void handle_alt_addr_whitelist_reply(struct daemon *daemon, const u8 *msg

if (is_whitelisted) {
status_peer_unusual(&peer->id,
"Peer's address %s is in the whitelist. Accepting connection.",
incoming_addr);
"Peer's address %s is in the whitelist. Accepting connection.",
incoming_addr);
fprintf(stderr, "END GOAL! YES\n");
} else {
status_peer_unusual(&peer->id,
"Connection attempt from address %s which is not in the whitelist. The peer has not received an alternative address from me. Closing connection.",
incoming_addr);
"Connection attempt from address %s which is not in the whitelist. The peer has not received an alternative address from me. Closing connection.",
incoming_addr);
fprintf(stderr, "END GOAL! NO\n");
io_close(peer->to_peer);
}
}
Expand Down Expand Up @@ -2267,7 +2292,7 @@ static struct io_plan *recv_req(struct io_conn *conn,
case WIRE_CONNECTD_CUSTOMMSG_IN:
case WIRE_CONNECTD_PEER_DISCONNECT_DONE:
case WIRE_CONNECTD_START_SHUTDOWN_REPLY:
case WIRE_CONNECTD_ALT_ADDR:
case WIRE_CONNECTD_PEER_ALT_ADDR:
case WIRE_CONNECTD_ALT_ADDR_WHITELIST:
break;
}
Expand Down
8 changes: 4 additions & 4 deletions connectd/connectd_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ msgtype,connectd_dev_suppress_gossip,2032
msgtype,connectd_dev_exhaust_fds,2036

# connectd -> master: alternative connection address
msgtype,connectd_alt_addr,2037
msgdata,connectd_alt_addr,node_id,point,
msgdata,connectd_alt_addr,addr_len,u8,
msgdata,connectd_alt_addr,addr,byte,addr_len,
msgtype,connectd_peer_alt_addr,2037
msgdata,connectd_peer_alt_addr,node_id,point,
msgdata,connectd_peer_alt_addr,addr_len,u8,
msgdata,connectd_peer_alt_addr,addr,byte,addr_len,

# connectd -> master: alternative connection whitelist
msgtype,connectd_alt_addr_whitelist,2038
Expand Down
3 changes: 2 additions & 1 deletion contrib/startup_regtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,15 @@ start_nodes() {

for i in $(seq "$node_count"); do
socket=$(( 7070 + i * 101))
alt_port=$(( 7070 + i * 102))
mkdir -p "$LIGHTNING_DIR/l$i"
# Node config
cat <<- EOF > "$LIGHTNING_DIR/l$i/config"
network=$network
log-level=debug
log-file=$LIGHTNING_DIR/l$i/log
addr=localhost:$socket
alt-addr=127.0.0.21:$socket
alt-addr=127.0.0.21:$alt_port
allow-deprecated-apis=false
developer
dev-fast-gossip
Expand Down
Loading

0 comments on commit 131a324

Please sign in to comment.