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 a6e2fa1
Show file tree
Hide file tree
Showing 10 changed files with 251 additions and 113 deletions.
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,
68 changes: 49 additions & 19 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,18 @@ 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;
u8 *peer_alt_addrs;
struct pubkey peer_id;
/* FIXME(maxrantil): u32 *timestamp = NULL; */

if (!fromwire_peer_alt_addr(peer, msg, &peer_id, &peer_alt_addr/* FIXME(maxrantil): , timestamp */)) {
if (!fromwire_peer_alt_addr(peer, msg, &peer_id, &peer_alt_addrs/* FIXME(maxrantil): , timestamp */)) {
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, &peer_id, peer_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(peer_alt_addrs); /* FIXME(maxrantil): Investigare further on freeing like this */
}

/*~ <hello>I speak web socket</hello>.
Expand Down Expand Up @@ -2135,14 +2160,17 @@ static void handle_alt_addr_whitelist_reply(struct daemon *daemon, const u8 *msg
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;
}

fprintf(stderr, "INSIDE handle_alt_addr_whitelist_reply\n");

struct node_id id;
node_id_from_pubkey(&id, &p_pk);
peer = peer_htable_get(daemon->peers, &id);
Expand All @@ -2155,12 +2183,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 +2297,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
43 changes: 35 additions & 8 deletions lightningd/channel_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,27 @@ static void handle_local_anchors(struct channel *channel, const u8 *msg)
}
}

#include <stdio.h>
static void handle_our_alt_addrs(struct lightningd *ld, struct channel *channel, const u8 *msg)
{
struct pubkey p_pk;
struct node_id p_id;
u8 *our_alt_addr;

if (!fromwire_channeld_our_alt_addrs(tmpctx, msg, &p_pk, &our_alt_addr)) {
channel_internal_error(channel,
"bad channeld_our_alt_addrs %s",
tal_hex(channel, msg));
return;
}

// fprintf(stderr, "\n!!!YEEESSS!!! NEXT!\n");
node_id_from_pubkey(&p_id, &p_pk);
wallet_add_alt_addr(ld->wallet->db, &p_id, (char *)our_alt_addr, true);

/* FIXME(maxrantil): free peer_pk & our_alt_addr & all things needed */
}

static unsigned channel_msg(struct subd *sd, const u8 *msg, const int *fds)
{
enum channeld_wire t = fromwire_peektype(msg);
Expand Down Expand Up @@ -1466,9 +1487,12 @@ static unsigned channel_msg(struct subd *sd, const u8 *msg, const int *fds)
case WIRE_CHANNELD_UPGRADED:
handle_channel_upgrade(sd->channel, msg);
break;
case WIRE_CHANNELD_OUR_ALT_ADDRS:
handle_our_alt_addrs(sd->ld, sd->channel, msg);
break;

/* And we never get these from channeld. */
case WIRE_CHANNELD_INIT:
case WIRE_CHANNELD_ALT_ADDR:
case WIRE_CHANNELD_FUNDING_DEPTH:
case WIRE_CHANNELD_OFFER_HTLC:
case WIRE_CHANNELD_FULFILL_HTLC:
Expand All @@ -1483,6 +1507,7 @@ static unsigned channel_msg(struct subd *sd, const u8 *msg, const int *fds)
case WIRE_CHANNELD_DEV_MEMLEAK:
case WIRE_CHANNELD_DEV_QUIESCE:
case WIRE_CHANNELD_GOT_INFLIGHT:
case WIRE_CHANNELD_PEER_ALT_ADDRS:
/* Replies go to requests. */
case WIRE_CHANNELD_OFFER_HTLC_REPLY:
case WIRE_CHANNELD_DEV_REENABLE_COMMIT_REPLY:
Expand Down Expand Up @@ -2348,24 +2373,24 @@ static struct command_result *json_alt_addr(struct command *cmd,
const jsmntok_t *obj UNNEEDED,
const jsmntok_t *params)
{
struct node_id *peer_node_id;
struct node_id *p_id;
struct pubkey p_pk; /* FIXME(maxrantil): maybe take this away again (send node_id struct instead) to clean it up? */
struct peer *peer;
struct channel *channel;
const char *our_alt_addr;
bool more_than_one;

if (!param_check(cmd, buffer, params,
p_req("node_id", param_node_id, &peer_node_id),
p_req("node_id", param_node_id, &p_id),
p_req("alt_addr", param_string, &our_alt_addr),
NULL))
return command_param_failed();

peer = peer_by_id(cmd->ld, peer_node_id);
peer = peer_by_id(cmd->ld, p_id);
if (!peer) {
return command_fail(cmd, JSONRPC2_INVALID_REQUEST,
"No such peer: %s",
fmt_node_id(cmd, peer_node_id));
fmt_node_id(cmd, p_id));
}

channel = peer_any_channel(peer, channel_state_can_add_htlc, &more_than_one);
Expand All @@ -2378,13 +2403,15 @@ static struct command_result *json_alt_addr(struct command *cmd,
if (command_check_only(cmd))
return command_check_done(cmd);

if (pubkey_from_node_id(&p_pk, peer_node_id)) {
if (pubkey_from_node_id(&p_pk, p_id)) {
/* FIXME(maxrantil): make 'our_alt_addr into a double pointer, array of arrays, for sending many. */
u8 *msg = towire_channeld_alt_addr(peer, &p_pk, (u8 *)our_alt_addr);
u8 *msg = towire_channeld_peer_alt_addrs(peer,
&p_pk,
(u8 *)our_alt_addr);
subd_send_msg(channel->owner, take(msg));
}

wallet_add_alt_addr(cmd->ld->wallet->db, peer_node_id, our_alt_addr, true);
wallet_add_alt_addr(cmd->ld->wallet->db, p_id, our_alt_addr, true);

/* FIXME(maxrantil), We need to add this to 'listnodes' command too! */

Expand Down
Loading

0 comments on commit a6e2fa1

Please sign in to comment.