diff --git a/channeld/channeld.c b/channeld/channeld.c index 0862691e0918..8bcb09c3059e 100644 --- a/channeld/channeld.c +++ b/channeld/channeld.c @@ -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) @@ -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 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); @@ -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); diff --git a/channeld/channeld_wire.csv b/channeld/channeld_wire.csv index e5bcef6a989b..60e9fd0c2239 100644 --- a/channeld/channeld_wire.csv +++ b/channeld/channeld_wire.csv @@ -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, diff --git a/connectd/connectd.c b/connectd/connectd.c index 86020e626bea..4b5eab724d8b 100644 --- a/connectd/connectd.c +++ b/connectd/connectd.c @@ -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, ","); @@ -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 */ } /*~ I speak web socket. @@ -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); @@ -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); } } @@ -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; } diff --git a/connectd/connectd_wire.csv b/connectd/connectd_wire.csv index 4ae3708a0114..3b71de3b2681 100644 --- a/connectd/connectd_wire.csv +++ b/connectd/connectd_wire.csv @@ -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 diff --git a/contrib/startup_regtest.sh b/contrib/startup_regtest.sh index d14e2d7b2fcd..c6282f42d32a 100755 --- a/contrib/startup_regtest.sh +++ b/contrib/startup_regtest.sh @@ -177,6 +177,7 @@ 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" @@ -184,7 +185,7 @@ start_nodes() { 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 diff --git a/lightningd/channel_control.c b/lightningd/channel_control.c index 3e09c97b9901..332e8e8a6415 100644 --- a/lightningd/channel_control.c +++ b/lightningd/channel_control.c @@ -1386,6 +1386,27 @@ static void handle_local_anchors(struct channel *channel, const u8 *msg) } } +#include +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); @@ -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: @@ -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: @@ -2348,7 +2373,7 @@ 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; @@ -2356,16 +2381,16 @@ static struct command_result *json_alt_addr(struct command *cmd, 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); @@ -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! */ diff --git a/lightningd/connect_control.c b/lightningd/connect_control.c index 4bda0a21c5ff..bd175f4092ac 100644 --- a/lightningd/connect_control.c +++ b/lightningd/connect_control.c @@ -305,12 +305,12 @@ static void gossipd_got_addrs(struct subd *subd, transient = !peer || !peer_any_channel(peer, channel_state_wants_peercomms, NULL); connectmsg = towire_connectd_connect_to_peer(NULL, - &d->id, - addrs, - d->addrhint, - d->peer_alt_addrs, - d->dns_fallback, - transient); + &d->id, + addrs, + d->addrhint, + d->peer_alt_addrs, + d->dns_fallback, + transient); subd_send_msg(d->ld->connectd, take(connectmsg)); tal_free(d); @@ -563,20 +563,23 @@ static void handle_custommsg_in(struct lightningd *ld, const u8 *msg) plugin_hook_call_custommsg(ld, NULL, p); } -static void handle_alt_addr_in(struct lightningd *ld, const u8 *msg) +static void handle_peer_alt_addr_in(struct lightningd *ld, const u8 *msg) { struct pubkey p_pk; - u8 *peer_alt_addr; + u8 *p_alt_addr; - if (!fromwire_connectd_alt_addr(tmpctx, msg, &p_pk, &peer_alt_addr)) { - log_broken(ld->log, "handle_alt_addr_in: Malformed alt_addr message: %s", - tal_hex(tmpctx, msg)); + if (!fromwire_connectd_peer_alt_addr(tmpctx, msg, &p_pk, &p_alt_addr)) { + log_broken(ld->log, + "handle_alt_addr_in: Malformed alt_addr message: %s", + tal_hex(tmpctx, msg)); return; } - struct node_id p_node_id; - node_id_from_pubkey(&p_node_id, &p_pk); - wallet_add_alt_addr(ld->wallet->db, &p_node_id, (char *)peer_alt_addr, false); + struct node_id p_id; + node_id_from_pubkey(&p_id, &p_pk); + wallet_add_alt_addr(ld->wallet->db, &p_id, (char *)p_alt_addr, false); + + /* FIXME(maxrantil): Free all things needed, p_pk, peer_alt_addr etc */ } static void handle_alt_addr_whitelist_in(struct lightningd *ld, const u8 *msg) @@ -584,11 +587,13 @@ static void handle_alt_addr_whitelist_in(struct lightningd *ld, const u8 *msg) struct pubkey p_pk; u8 *incoming_addr; - if (!fromwire_connectd_alt_addr_whitelist(tmpctx, msg, &p_pk, &incoming_addr)) { + if (!fromwire_connectd_alt_addr_whitelist(tmpctx, + msg, + &p_pk, + &incoming_addr)) { log_broken(ld->log, "handle_alt_addr_whitelist_in: Malformed alt_addr_whitelist message: %s", tal_hex(tmpctx, msg)); - return; } @@ -600,10 +605,12 @@ static void handle_alt_addr_whitelist_in(struct lightningd *ld, const u8 *msg) bool is_whitelisted = (alt_addr != NULL); u8 *reply = towire_connectd_alt_addr_whitelist_reply(tmpctx, - &p_pk, - incoming_addr, - is_whitelisted); + &p_pk, + incoming_addr, + is_whitelisted); subd_send_msg(ld->connectd, take(reply)); + + /* FIXME(maxrantil): free p_pk & incoming_addr */ } static void connectd_start_shutdown_reply(struct subd *connectd, @@ -641,10 +648,13 @@ static void startup_connect_one_done(struct lightningd *ld) channel_gossip_startup_done(ld); } +#include static unsigned connectd_msg(struct subd *connectd, const u8 *msg, const int *fds) { enum connectd_wire t = fromwire_peektype(msg); + fprintf(stderr, "\nReceived message type: %u\n", t); + switch (t) { /* These are messages we send, not them. */ case WIRE_CONNECTD_INIT: @@ -697,8 +707,8 @@ static unsigned connectd_msg(struct subd *connectd, const u8 *msg, const int *fd handle_custommsg_in(connectd->ld, msg); break; - case WIRE_CONNECTD_ALT_ADDR: - handle_alt_addr_in(connectd->ld, msg); + case WIRE_CONNECTD_PEER_ALT_ADDR: + handle_peer_alt_addr_in(connectd->ld, msg); break; case WIRE_CONNECTD_ALT_ADDR_WHITELIST: diff --git a/lightningd/options.c b/lightningd/options.c index 16ddba6c2e86..b584825a196b 100644 --- a/lightningd/options.c +++ b/lightningd/options.c @@ -450,17 +450,21 @@ static char *opt_add_alt_addr(const char *arg, struct lightningd *ld) return NULL; } - if (!ld->alt_addr) { + if (!ld->alt_addr) ld->alt_addr = (u8 *)tal_strdup(ld, arg); - } else { - ld->alt_addr = (u8 *)tal_fmt(ld, "%s,%s", ld->alt_addr, arg); - } + else + ld->alt_addr = (u8 *)tal_fmt(ld, + "%s,%s", + ld->alt_addr, + arg); - if (!ld->alt_bind_addr) { + if (!ld->alt_bind_addr) ld->alt_bind_addr = (u8 *)tal_strdup(ld, arg); - } else { - ld->alt_bind_addr = (u8 *)tal_fmt(ld, "%s,%s", ld->alt_bind_addr, arg); - } + else + ld->alt_bind_addr = (u8 *)tal_fmt(ld, + "%s,%s", + ld->alt_bind_addr, + arg); return opt_add_addr_withtype(arg, ld, ADDR_LISTEN); } @@ -474,11 +478,13 @@ static char *opt_add_alt_bind_addr(const char *arg, struct lightningd *ld) return NULL; } - if (!ld->alt_bind_addr) { + if (!ld->alt_bind_addr) ld->alt_bind_addr = (u8 *)tal_strdup(ld, arg); - } else { - ld->alt_bind_addr = (u8 *)tal_fmt(ld, "%s,%s", ld->alt_bind_addr, arg); - } + else + ld->alt_bind_addr = (u8 *)tal_fmt(ld, + "%s,%s", + ld->alt_bind_addr, + arg); return opt_add_addr_withtype(arg, ld, ADDR_LISTEN); } @@ -488,13 +494,16 @@ static char *opt_add_alt_announce_addr(const char *arg, struct lightningd *ld) assert(arg != NULL); /* 'alt-bind-addr' must be set before 'alt-announce-addr'. */ - if (ld == NULL || ld->alt_bind_addr == NULL) { - return tal_fmt(tmpctx, "argument must first be bound by 'alt-bind-addr'"); - } + if (ld == NULL || ld->alt_bind_addr == NULL) + return tal_fmt(tmpctx, + "argument must first be bound by 'alt-bind-addr'"); /* Check if the argument matches any of the bound alt-bind addresses */ bool match_found = false; - char **bind_addrs = tal_strsplit(tmpctx, (char *)ld->alt_bind_addr, ",", STR_NO_EMPTY); + char **bind_addrs = tal_strsplit(tmpctx, + (char *)ld->alt_bind_addr, + ",", + STR_NO_EMPTY); for (size_t i = 0; bind_addrs[i] != NULL; i++) { if (strcmp(arg, bind_addrs[i]) == 0) { match_found = true; @@ -502,15 +511,17 @@ static char *opt_add_alt_announce_addr(const char *arg, struct lightningd *ld) } } - if (!match_found) { - return tal_fmt(tmpctx, "argument must match a bound 'alt-bind-addr'"); - } + if (!match_found) + return tal_fmt(tmpctx, + "argument must match a bound 'alt-bind-addr'"); - if (!ld->alt_addr) { + if (!ld->alt_addr) ld->alt_addr = (u8 *)tal_strdup(ld, arg); - } else { - ld->alt_addr = (u8 *)tal_fmt(ld, "%s,%s", ld->alt_addr, arg); - } + else + ld->alt_addr = (u8 *)tal_fmt(ld, + "%s,%s", + ld->alt_addr, + arg); return NULL; } diff --git a/tests/test_connection.py b/tests/test_connection.py index 9ea9b31a72dd..8e9024a2e1f9 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -131,7 +131,7 @@ def reconnect_and_verify(l1, l2, alt_addr, alt_port): assert connected_peer['netaddr'][0] == f'{alt_addr}:{alt_port}', f"Connection not using alt-addr: {connected_peer['netaddr'][0]}" def restart_and_verify(l1, l2, alt_addr, alt_port): - # Restart l2 and check if l1 reconnects automatically + # Restart l2 and check if it reconnects automatically l2.restart() wait_for(lambda: only_one(l1.rpc.listpeers(l2.info['id'])['peers'])['connected'], timeout=10) diff --git a/wallet/wallet.c b/wallet/wallet.c index 7935f534a997..6beab1de63c0 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -1027,26 +1027,49 @@ static struct peer *wallet_peer_load(struct wallet *w, const u64 dbid) return peer; } +#include void wallet_add_alt_addr(struct db *db, const struct node_id *node_id, - const char *alt_addr, bool is_our_alt_addr) + const char *alt_addr, bool is_our_alt_addr) { struct db_stmt *stmt; - if (!alt_addr) + if (!alt_addr) { + fprintf(stderr, "wallet_add_alt_addr: alt_addr is null\n"); return; + } + + // if (is_our_alt_addr) + // stmt = db_prepare_v2(db, SQL("UPDATE peers SET our_alt_addr=? WHERE node_id=?")); + // else + // stmt = db_prepare_v2(db, SQL("UPDATE peers SET peer_alt_addr=? WHERE node_id=?")); - if (is_our_alt_addr) + if (is_our_alt_addr) { + fprintf(stderr, "Preparing to update our_alt_addr for node_id=%s with alt_addr=%s\n", + fmt_node_id(tmpctx, node_id), alt_addr); stmt = db_prepare_v2(db, SQL("UPDATE peers SET our_alt_addr=? WHERE node_id=?")); - else + } else { + fprintf(stderr, "Preparing to update peer_alt_addr for node_id=%s with alt_addr=%s\n", + fmt_node_id(tmpctx, node_id), alt_addr); stmt = db_prepare_v2(db, SQL("UPDATE peers SET peer_alt_addr=? WHERE node_id=?")); + } db_bind_text(stmt, alt_addr); db_bind_node_id(stmt, node_id); + // db_exec_prepared_v2(take(stmt)); +// } + + fprintf(stderr, "Executing statement for node_id=%s with alt_addr=%s\n", + fmt_node_id(tmpctx, node_id), alt_addr); + db_exec_prepared_v2(take(stmt)); + + fprintf(stderr, "Successfully updated alt_addr for node_id=%s with alt_addr=%s\n", + fmt_node_id(tmpctx, node_id), alt_addr); } static struct wireaddr_internal *handle_alt_addr_failure(struct wallet *w, - struct db_stmt *stmt, bool transaction_started) + struct db_stmt *stmt, + bool transaction_started) { if (stmt) tal_free(stmt); @@ -1057,10 +1080,13 @@ static struct wireaddr_internal *handle_alt_addr_failure(struct wallet *w, } struct wireaddr_internal *wallet_get_alt_addrs(struct wallet *w, - const struct node_id *node_id, bool use_our_alt_addrs) + const struct node_id *node_id, + bool use_our_alt_addrs) { struct db_stmt *stmt; - struct wireaddr_internal *alt_addrs = tal_arr(tmpctx, struct wireaddr_internal, 0); //NULL as ctx? + struct wireaddr_internal *alt_addrs = tal_arr(tmpctx, + struct wireaddr_internal, + 0); //NULL as ctx? const char *addr_column = use_our_alt_addrs ? "our_alt_addr" : "peer_alt_addr"; bool transaction_started = false; @@ -1077,6 +1103,7 @@ struct wireaddr_internal *wallet_get_alt_addrs(struct wallet *w, if (!stmt) { log_debug(w->log, "Failed to prepare statement for node_id %s", fmt_node_id(tmpctx, node_id)); + fprintf(stderr, "Failed to prepare statement for node_id %s\n", fmt_node_id(tmpctx, node_id)); //REMOVE return handle_alt_addr_failure(w, stmt, transaction_started); } @@ -1084,32 +1111,43 @@ struct wireaddr_internal *wallet_get_alt_addrs(struct wallet *w, db_query_prepared(stmt); if (!db_step(stmt)) { - log_debug(w->log, "No alternative address found for peer %s", - fmt_node_id(tmpctx, node_id)); + fprintf(stderr, "No alternative address found for peer %s\n", fmt_node_id(tmpctx, node_id)); //REMOVE + log_debug(w->log, + "No alternative address found for peer %s", + fmt_node_id(tmpctx, node_id)); return handle_alt_addr_failure(w, stmt, transaction_started); } const char *addr_str = db_col_strdup(tmpctx, stmt, addr_column); if (!addr_str) { - log_debug(w->log, "Invalid address string retrieved for peer %s", - fmt_node_id(tmpctx, node_id)); + log_debug(w->log, + "Invalid address string retrieved for peer %s", + fmt_node_id(tmpctx, node_id)); return handle_alt_addr_failure(w, stmt, transaction_started); } if (*addr_str == '\0') { - log_debug(w->log, "Empty address string retrieved for peer %s", - fmt_node_id(tmpctx, node_id)); + log_debug(w->log, + "Empty address string retrieved for peer %s", + fmt_node_id(tmpctx, node_id)); return handle_alt_addr_failure(w, stmt, transaction_started); } char **addr_list = tal_strsplit(tmpctx, addr_str, ",", STR_NO_EMPTY); for (size_t i = 0; addr_list[i] != NULL; i++) { - struct wireaddr_internal *alt_addr = tal(tmpctx, struct wireaddr_internal); - const char *err = parse_wireaddr_internal(tmpctx, addr_list[i], - chainparams_get_ln_port(chainparams), false, alt_addr); + struct wireaddr_internal *alt_addr = tal(tmpctx, + struct wireaddr_internal); + const char *err = parse_wireaddr_internal(tmpctx, + addr_list[i], + chainparams_get_ln_port(chainparams), + false, + alt_addr); if (err) { - log_debug(w->log, "Invalid alternative address %s for peer %s: %s", - addr_list[i], fmt_node_id(tmpctx, node_id), err); + log_debug(w->log, + "Invalid alternative address %s for peer %s: %s", + addr_list[i], + fmt_node_id(tmpctx, node_id), + err); tal_free(alt_addr); continue; }