From b1b9a49e26daa8ac90964bd7508c4f93f6d5a935 Mon Sep 17 00:00:00 2001 From: Max Rantil Date: Mon, 17 Jun 2024 16:23:36 +0200 Subject: [PATCH] Process incoming alternative addresses and store them to the CoreLN database Signed-off-by: Max Rantil --- channeld/channeld.c | 65 ++++--------- channeld/channeld.h | 1 - channeld/channeld_wire.csv | 3 + common/wireaddr.h | 1 - connectd/connectd.c | 179 ++++------------------------------- connectd/connectd.h | 13 +-- connectd/connectd_wire.csv | 6 ++ connectd/multiplex.c | 3 + lightningd/channel.h | 3 - lightningd/channel_control.c | 4 +- lightningd/connect_control.c | 27 ++++-- lightningd/connect_control.h | 2 - lightningd/lightningd.h | 3 + lightningd/options.c | 56 ++--------- lightningd/peer_control.c | 30 ------ lightningd/peer_control.h | 4 - wallet/db.c | 1 - wallet/wallet.c | 37 +------- wire/peer_wire.csv | 2 +- 19 files changed, 88 insertions(+), 352 deletions(-) diff --git a/channeld/channeld.c b/channeld/channeld.c index c50f43408387..c0a875b8d8bf 100644 --- a/channeld/channeld.c +++ b/channeld/channeld.c @@ -68,6 +68,9 @@ struct peer { bool channel_ready[NUM_SIDES]; u64 next_index[NUM_SIDES]; + /* ID of peer */ + struct node_id id; + /* --developer? */ bool developer; @@ -191,10 +194,13 @@ struct peer { /* --experimental-upgrade-protocol */ bool experimental_upgrade; + + /* Alt address for peer connections not publicly announced */ + u8 *alt_addr; }; static void start_commit_timer(struct peer *peer); -// void send_peer_alt_address(struct peer *peer, const struct pubkey *node_id, const u8 *alt_address); +static void send_peer_alt_address(struct peer *peer); static void billboard_update(const struct peer *peer) { @@ -536,46 +542,13 @@ static void handle_peer_splice_locked(struct peer *peer, const u8 *msg) check_mutual_splice_locked(peer); } -// void send_peer_alt_address(struct peer *peer, const struct pubkey *node_id, const u8 *alt_address) -// { -// u8 *msg = towire_peer_alt_address(peer, node_id, alt_address); - -// peer_write(peer->pps, take(msg)); -// fprintf(stderr, "Sent alternative address message to peer"); -// } +static void send_peer_alt_address(struct peer *peer) { + struct pubkey node_id; -static void handle_peer_alt_addr(struct peer *peer, const u8 *msg) -{ - struct pubkey peer_id; - u8 *alt_addr; - // u32 *timestamp = NULL; - // fprintf(stderr, "Entering handle_peer_alt_eight with msg %s\n", msg); - fprintf(stderr, "1 THIS IS A TEST\n"); - status_info("2 THIS IS A TEST"); - if (!fromwire_peer_alt_address(tmpctx, msg, &peer_id, &alt_addr/* , timestamp */)) { - master_badmsg(WIRE_PEER_ALT_ADDRESS, msg); + if (pubkey_from_node_id(&node_id, &peer->id)) { + u8 *msg = towire_peer_alt_address(peer, &node_id, peer->alt_addr); + peer_write(peer->pps, take(msg)); } - fprintf(stderr, "3 THIS IS A TEST\n"); - status_info("3.5 THIS IS A TEST"); - // peer = peer_htable_get(daemon->peers, &id); - // if (!peer) - // return; - - // Store the alternative address in the peer structure - // peer->alt_address = alt_addr; // Assuming you have such a field in the struct - // log_info(peer->log, "Received alt address: %s", alt_addr); - - // struct peer *peer = peer_htable_get(daemon->peers, &peer_id); - // if (!peer) - // return; // Peer not found - - // updating peer information in a database: - // update_peer_address(peer, &alt_addr); - - // Optionally, trigger actions that use the new address immediately, - // such as attempting a new connection: - // try_connect_to_peer(peer, &alt_addr); - } static void handle_peer_channel_ready(struct peer *peer, const u8 *msg) @@ -4201,14 +4174,14 @@ static void peer_in(struct peer *peer, const u8 *msg) { enum peer_wire type = fromwire_peektype(msg); - // fprintf(stderr, "4 THIS IS A TEST\n"); - // status_info("5 THIS IS A TEST %s", msg); - if (handle_peer_error_or_warning(peer->pps, msg)) return; check_tx_abort(peer, msg); + if (peer->alt_addr) + send_peer_alt_address(peer); + /* If we're in STFU mode and aren't waiting for a STFU mode * specific message, the only valid message was tx_abort */ if (is_stfu_active(peer) && !peer->stfu_wait_single_msg) { @@ -4300,9 +4273,6 @@ static void peer_in(struct peer *peer, const u8 *msg) case WIRE_SPLICE_LOCKED: handle_peer_splice_locked(peer, msg); return; - case WIRE_PEER_ALT_ADDRESS: - handle_peer_alt_addr(peer, msg); - return; case WIRE_INIT: case WIRE_OPEN_CHANNEL: case WIRE_ACCEPT_CHANNEL: @@ -4344,6 +4314,7 @@ static void peer_in(struct peer *peer, const u8 *msg) case WIRE_ONION_MESSAGE: case WIRE_PEER_STORAGE: case WIRE_YOUR_PEER_STORAGE: + case WIRE_PEER_ALT_ADDRESS: abort(); } @@ -5918,7 +5889,9 @@ static void init_channel(struct peer *peer) &reestablish_only, &peer->experimental_upgrade, &peer->splice_state->inflights, - &peer->local_alias)) { + &peer->local_alias, + &peer->alt_addr, + &peer->id)) { master_badmsg(WIRE_CHANNELD_INIT, msg); } diff --git a/channeld/channeld.h b/channeld/channeld.h index b3cbe35c6972..a2b26b08246a 100644 --- a/channeld/channeld.h +++ b/channeld/channeld.h @@ -6,6 +6,5 @@ #include const u8 *hsm_req(const tal_t *ctx, const u8 *req TAKES); -// void send_peer_alt_address(struct peer *peer, const struct pubkey *node_id, const u8 *alt_address); #endif /* LIGHTNING_CHANNELD_CHANNELD_H */ diff --git a/channeld/channeld_wire.csv b/channeld/channeld_wire.csv index d68b87a25dc1..2cec2669dae9 100644 --- a/channeld/channeld_wire.csv +++ b/channeld/channeld_wire.csv @@ -77,6 +77,9 @@ msgdata,channeld_init,experimental_upgrade,bool, msgdata,channeld_init,num_inflights,u16, msgdata,channeld_init,inflights,inflight,num_inflights msgdata,channeld_init,scid_alias,short_channel_id, +msgdata,channeld_init,alt_addr_len,u16, +msgdata,channeld_init,alt_addr,u8,alt_addr_len, +msgdata,channeld_init,id,node_id, # channeld->lightningd: successfully negotated reestablishment. msgtype,channeld_reestablished,1101 diff --git a/common/wireaddr.h b/common/wireaddr.h index 4c73020ee3b9..d9b0cbfdda68 100644 --- a/common/wireaddr.h +++ b/common/wireaddr.h @@ -56,7 +56,6 @@ bool wireaddr_eq_without_port(const struct wireaddr *a, const struct wireaddr *b * announce */ enum addr_listen_announce { ADDR_LISTEN = (1 << 0), - ALT_ADDR_LISTEN = (2 << 0), ADDR_ANNOUNCE = (1 << 1), ADDR_LISTEN_AND_ANNOUNCE = ADDR_LISTEN|ADDR_ANNOUNCE }; diff --git a/connectd/connectd.c b/connectd/connectd.c index 2b578693f04c..1b2f7e25a2ba 100644 --- a/connectd/connectd.c +++ b/connectd/connectd.c @@ -25,7 +25,6 @@ #include #include #include -// #include #include #include #include @@ -60,9 +59,6 @@ #define HSM_FD 3 #define GOSSIPCTL_FD 4 -void peer_connected_wrapper(struct daemon *daemon, struct peer *peer); - - /*~ C programs should generally be written bottom-to-top, with the root * function at the bottom, and functions it calls above it. That avoids * us having to pre-declare functions; but in the case of mutual recursion @@ -303,27 +299,6 @@ struct io_plan *peer_connected(struct io_conn *conn, * it that, too. */ daemon_conn_send(daemon->master, take(msg)); - /* Invoke the callback if set */ - // if (connect && connect->cb) { - // connect->cb(daemon, peer); - // struct pubkey pubkey; - // if (pubkey_from_node_id(&pubkey, id)) { - // send_peer_alt_address(peer, &pubkey, (const u8 *)"127.21.21.21"); - // } else { - // status_peer_unusual(id, "THIS IS A TEST 8"); - // } - // struct pubkey pubkey; - // if (pubkey_from_node_id(&pubkey, id)) { - // send_peer_alt_address(peer, &pubkey, (const u8 *)"127.21.21.21"); - // u8 *msg = towire_peer_alt_address(peer, &pubkey, (const u8 *)"127.21.21.21"); - // peer_write(peer->pps, take(msg)); - // status_info("Sent alternative address message to peer"); - // } else { - // status_peer_unusual(id, "THIS IS A TEST 8"); - // } - // tal_free(connect); - // } - /*~ Now we set up this connection to read/write from subd */ return multiplex_peer_setup(conn, peer); } @@ -1169,90 +1144,6 @@ static bool want_tor(const struct wireaddr_internal *proposed_wireaddr) return false; } -// static void send_peer_message(const struct daemon *daemon) { -// // Placeholder for message sending logic to all peers -// fprintf(stderr, "Peer message sent due to ALT_ADDR_LISTEN.\n"); - -// u8 *msg = towire_peer_alt_address(peer, node_id, alt_address); - -// peer_write(peer->pps, take(msg)); -// fprintf(stderr, "Sent alternative address message to peer.\n"); - #include -#include - -// Assuming a serialization function towire_peer_alt_address exists -// And assuming peer_write sends messages to the peers - -// static void send_peer_message(const struct daemon *daemon, const u8 *alt_address) { -// fprintf(stderr, "Preparing to send ALT_ADDR_LISTEN message to all peers.\n"); - -// struct peer *peer; -// struct peer_htable_iter it; -// struct pubkey pb; -// u8 *msg; - -// // Start iterating from the first peer -// peer = peer_htable_first(daemon->peers, &it); -// while (peer) { -// if (!pubkey_from_node_id(&pb, &peer->id)) { -// fprintf(stderr, "Failed to get public key for peer %s.\n", fmt_node_id(tmpctx, &peer->id)); -// continue; -// } -// // Create a message for the current peer with the alternative address -// msg = towire_peer_alt_address(tmpctx, &pb, alt_address); // Assuming tmpctx or some context -// if (msg == NULL) { -// fprintf(stderr, "Failed to create message for peer %s.\n", fmt_node_id(tmpctx, &peer->id)); -// continue; -// } - -// // // Ensure `peer->pps` is the correct member that holds the peer's connection context -// // if (peer_write(peer->pps, take(msg))) { // Update this line if `pps` is not correct -// if (io_write_wire(conn, take(msg), io_close_cb, NULL);) { // Update this line if `pps` is not correct -// fprintf(stderr, "Sent ALT_ADDR_LISTEN message to peer %s.\n", fmt_node_id(tmpctx, &peer->id)); -// } else { -// fprintf(stderr, "Failed to send ALT_ADDR_LISTEN message to peer %s.\n", fmt_node_id(tmpctx, &peer->id)); -// } - -// // Move to the next peer -// peer = peer_htable_next(daemon->peers, &it); -// } -// } - -void broadcast_alt_address(struct daemon *daemon, const u8 *alt_address) { - struct peer *peer; - struct peer_htable_iter it; - - fprintf(stderr, "Starting to broadcast alternative address to all peers.\n"); - peer = peer_htable_first(daemon->peers, &it); - - if (!peer) { - fprintf(stderr, "No peers to broadcast to.\n"); - } - - while (peer) { - struct pubkey pb; - if (pubkey_from_node_id(&pb, &peer->id)) { - fprintf(stderr, "Public key successfully retrieved for peer %s.\n", fmt_node_id(tmpctx, &peer->id)); - u8 *msg = towire_peer_alt_address(tmpctx, &pb, alt_address); - if (msg) { - fprintf(stderr, "Message successfully serialized for peer %s.\n", fmt_node_id(tmpctx, &peer->id)); - if (io_write_wire(peer->to_peer, take(msg), io_close_cb, NULL)) { - fprintf(stderr, "Message successfully sent to peer %s.\n", fmt_node_id(tmpctx, &peer->id)); - } else { - fprintf(stderr, "Failed to send message to peer %s.\n", fmt_node_id(tmpctx, &peer->id)); - } - } else { - fprintf(stderr, "Failed to serialize message for peer %s.\n", fmt_node_id(tmpctx, &peer->id)); - } - } else { - fprintf(stderr, "Failed to get public key for peer %s.\n", fmt_node_id(tmpctx, &peer->id)); - } - peer = peer_htable_next(daemon->peers, &it); - } -} - - - /*~ The user can specify three kinds of addresses: ones we bind to but don't * announce, ones we announce but don't bind to, and ones we bind to and * announce if they seem to be public addresses. @@ -1304,7 +1195,6 @@ setup_listeners(const tal_t *ctx, add_announceable(announceable, &wa.u.wireaddr.wireaddr); } - /* Now look for listening addresses. */ for (size_t i = 0; i < tal_count(proposed_wireaddr); i++) { struct wireaddr_internal wa = proposed_wireaddr[i]; @@ -1312,11 +1202,6 @@ setup_listeners(const tal_t *ctx, if (!(proposed_listen_announce[i] & ADDR_LISTEN)) continue; - if (ALT_ADDR_LISTEN) { - broadcast_alt_address(daemon, (const u8 *)"127.22.22.22"); // Send a message to all peers when ALT_ADDR_LISTEN is used - // send_peer_message(daemon, (const u8 *)"127.22.22.22"); // Send a message to all peers when ALT_ADDR_LISTEN is used - } - switch (wa.itype) { /* We support UNIX domain sockets, but can't announce */ case ADDR_INTERNAL_SOCKNAME: @@ -1788,8 +1673,6 @@ static void add_gossip_addrs(struct wireaddr_internal **addrs, add_gossip_addrs_bytypes(addrs, normal_addrs, addrhint, types[i]); } -typedef void (*peer_connected_cb)(struct daemon *daemon, struct peer *peer); - /*~ Consumes addrhint if not NULL. * * That's a pretty ugly interface: we should use TAKEN, but we only have one @@ -1812,8 +1695,6 @@ static void try_connect_peer(struct daemon *daemon, /* Note if we explicitly tried to connect non-transiently */ if (!transient) peer->prio = PRIO_DELIBERATE; - // if (cb) - // cb(daemon, peer); return; } @@ -1888,7 +1769,6 @@ static void try_connect_peer(struct daemon *daemon, connect->errors = tal_strdup(connect, ""); connect->conn = NULL; connect->transient = transient; - // connect->cb = cb; connecting_htable_add(daemon->connecting, connect); tal_add_destructor(connect, destroy_connecting); @@ -1896,48 +1776,21 @@ static void try_connect_peer(struct daemon *daemon, try_connect_one_addr(connect); } -// void peer_connected_wrapper(struct daemon *daemon, struct peer *peer) { -// // Extract necessary information from the peer or connecting structure -// struct io_conn *conn = peer->conn; -// struct node_id *id = &peer->id; -// struct wireaddr_internal *addr = &peer->addr; -// struct crypto_state *cs = &peer->cs; -// const u8 *their_features = peer->their_features; -// enum is_websocket is_websocket = peer->is_websocket; -// bool incoming = peer->incoming; - -// // Call the actual peer_connected function -// peer_connected(conn, daemon, id, addr, NULL, cs, their_features, is_websocket, incoming); -// } - -// void peer_connected_wrapper(struct daemon *daemon, struct peer *peer) { -// struct connecting *connect = find_connecting(daemon, &peer->id); - -// if (connect) { -// struct io_conn *conn = connect->conn; -// struct node_id *id = &peer->id; -// // struct wireaddr_internal *addr = connect->addrs + connect->addrnum; -// struct crypto_state *cs = &peer->cs; -// const u8 *their_features = connect->their_features; // Adjust this as needed -// enum is_websocket is_websocket = peer->is_websocket; -// bool incoming = connect->incoming; - -// // Create a wireaddr_internal structure with the hardcoded IP address -// struct wireaddr_internal hardcoded_addr; -// hardcoded_addr.itype = ADDR_INTERNAL_WIREADDR; -// hardcoded_addr.u.wireaddr.wireaddr.type = ADDR_TYPE_IPV4; -// hardcoded_addr.u.wireaddr.wireaddr.addrlen = 4; -// inet_pton(AF_INET, "127.21.21.21", &hardcoded_addr.u.wireaddr.wireaddr.addr); -// hardcoded_addr.u.wireaddr.wireaddr.port = connect->addrs[connect->addrnum].u.wireaddr.wireaddr.port; - - -// // Call the actual peer_connected function -// // peer_connected(conn, daemon, id, addr, NULL, cs, their_features, is_websocket, incoming); -// peer_connected(conn, daemon, id, &hardcoded_addr, NULL, cs, their_features, is_websocket, incoming); -// } else { -// status_broken("Failed to find connecting structure for peer"); -// } -// } +void handle_peer_alt_addr(struct peer *peer, const u8 *msg) +{ + u8 *alt_addr; + struct pubkey peer_id; + + // u32 *timestamp = NULL; + if (!fromwire_peer_alt_address(peer, msg, &peer_id, &alt_addr/* , timestamp */)) { + master_badmsg(WIRE_PEER_ALT_ADDRESS, msg); + } + + msg = towire_connectd_alt_address(NULL, &peer_id, alt_addr); + daemon_conn_send(peer->daemon->master, take(msg)); + + tal_free(alt_addr); +} /* lightningd tells us to connect to a peer by id, with optional addr hint. */ static void connect_to_peer(struct daemon *daemon, const u8 *msg) @@ -2250,7 +2103,6 @@ static struct io_plan *recv_req(struct io_conn *conn, goto out; case WIRE_CONNECTD_CONNECT_TO_PEER: - // case WIRE_PEER_ALT_ADDRESS: connect_to_peer(daemon, msg); goto out; @@ -2323,6 +2175,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_ADDRESS: break; } diff --git a/connectd/connectd.h b/connectd/connectd.h index 042d9f59c577..83e9090e2512 100644 --- a/connectd/connectd.h +++ b/connectd/connectd.h @@ -132,8 +132,6 @@ HTABLE_DEFINE_TYPE(struct peer, peer_eq_node_id, peer_htable); -// typedef void (*peer_connected_cb)(struct daemon *daemon, struct peer *peer); - /*~ Peers we're trying to reach: we iterate through addrs until we succeed * or fail. */ struct connecting { @@ -159,13 +157,6 @@ struct connecting { /* Is this a transient connection? */ bool transient; - - /* Callback to call when connected */ - // peer_connected_cb cb; - - /* Additional fields as needed */ - // const u8 *their_features; - // bool incoming; }; static const struct node_id *connecting_keyof(const struct connecting *connecting) @@ -302,7 +293,7 @@ void destroy_peer(struct peer *peer); /* Remove a random connection, when under stress. */ void close_random_connection(struct daemon *daemon); -void send_peer_alt_address(struct peer *peer, const struct pubkey *node_id, const u8 *alt_address); -void broadcast_alt_address(struct daemon *daemon, const u8 *alt_address); +/* Handles alternative address message from peer. */ +void handle_peer_alt_addr(struct peer *peer, const u8 *msg); #endif /* LIGHTNING_CONNECTD_CONNECTD_H */ diff --git a/connectd/connectd_wire.csv b/connectd/connectd_wire.csv index d15ffc66cc88..4b72b7b91071 100644 --- a/connectd/connectd_wire.csv +++ b/connectd/connectd_wire.csv @@ -167,3 +167,9 @@ msgtype,connectd_dev_suppress_gossip,2032 # master -> connect: waste all your fds. msgtype,connectd_dev_exhaust_fds,2036 + +# connectd -> master: peer alternative connection address +msgtype,connectd_alt_address,2037 +msgdata,connectd_alt_address,node_id,point, +msgdata,connectd_alt_address,address_len,u8, +msgdata,connectd_alt_address,address,byte,address_len, diff --git a/connectd/multiplex.c b/connectd/multiplex.c index 1d659bb7c9dc..812c92738532 100644 --- a/connectd/multiplex.c +++ b/connectd/multiplex.c @@ -772,6 +772,9 @@ static bool handle_message_locally(struct peer *peer, const u8 *msg) } else if (type == WIRE_ONION_MESSAGE) { handle_onion_message(peer->daemon, peer, msg); return true; + } else if (type == WIRE_PEER_ALT_ADDRESS) { // IS THIS THE RIGHT PLACE ?? IT WORKS BUT... + handle_peer_alt_addr(peer, msg); + return true; } else if (handle_custommsg(peer->daemon, peer, msg)) { return true; } diff --git a/lightningd/channel.h b/lightningd/channel.h index e872c804f89f..9565eca22ad5 100644 --- a/lightningd/channel.h +++ b/lightningd/channel.h @@ -798,7 +798,4 @@ const u8 *channel_update_for_error(const tal_t *ctx, struct amount_msat htlc_max_possible_send(const struct channel *channel); -// void send_peer_alt_address(struct peer *peer, const struct pubkey *node_id, const u8 *alt_address); - - #endif /* LIGHTNING_LIGHTNINGD_CHANNEL_H */ diff --git a/lightningd/channel_control.c b/lightningd/channel_control.c index 2a15fdb92fdb..5e0735f9872f 100644 --- a/lightningd/channel_control.c +++ b/lightningd/channel_control.c @@ -1725,7 +1725,9 @@ bool peer_start_channeld(struct channel *channel, ld->experimental_upgrade_protocol, cast_const2(const struct inflight **, inflights), - *channel->alias[LOCAL]); + *channel->alias[LOCAL], + ld->alt_addr, + &ld->id); /* We don't expect a response: we are triggered by funding_depth_cb. */ subd_send_msg(channel->owner, take(initmsg)); diff --git a/lightningd/connect_control.c b/lightningd/connect_control.c index a5f9a388f3ee..d22d8b3d72b2 100644 --- a/lightningd/connect_control.c +++ b/lightningd/connect_control.c @@ -29,8 +29,6 @@ struct connect { struct command *cmd; }; -// void send_peer_alt_address(struct peer *peer, const struct pubkey *node_id, const u8 *alt_address); - static void destroy_connect(struct connect *c) { list_del(&c->list); @@ -368,10 +366,7 @@ static void try_connect(const tal_t *ctx, if (peer) { // struct pubkey pubkey; // if (pubkey_from_node_id(&pubkey, id)) { - // send_peer_alt_address(peer, &pubkey, (const u8 *)"127.21.21.21"); - // } else { - // log_peer_debug(ld->log, id, "THIS IS A TEST 9"); - // } + // send_peer_alt_address(peer, &pubkey, (const u8 *)"127.21.21.21"); // THIS MIGHT BE THE RIGHT PLACE IN THE END, NEED TO SEND ANOTHER MSG FROM MASTER -> CHANNELD then???? struct channel *channel; list_for_each(&peer->channels, channel, list) { if (!channel_state_wants_peercomms(channel->state)) @@ -564,6 +559,22 @@ 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) +{ + struct pubkey node_id; + struct node_id id; + u8 *alt_addr; + + if (!fromwire_connectd_alt_address(tmpctx, msg, &node_id, &alt_addr)) { + log_broken(ld->log, "Malformed peer_alt_addr_msg: %s", + tal_hex(tmpctx, msg)); + return; + } + + node_id_from_pubkey(&id, &node_id); + wallet_peer_alt_addr(ld->wallet->db, &id, (char *)alt_addr); +} + static void connectd_start_shutdown_reply(struct subd *connectd, const u8 *reply, const int *fds UNUSED, @@ -653,6 +664,10 @@ static unsigned connectd_msg(struct subd *connectd, const u8 *msg, const int *fd case WIRE_CONNECTD_CUSTOMMSG_IN: handle_custommsg_in(connectd->ld, msg); break; + + case WIRE_CONNECTD_ALT_ADDRESS: + handle_alt_addr_in(connectd->ld, msg); + break; } return 0; } diff --git a/lightningd/connect_control.h b/lightningd/connect_control.h index 2a0776b4e4d9..a6819f1fbb29 100644 --- a/lightningd/connect_control.h +++ b/lightningd/connect_control.h @@ -20,8 +20,6 @@ void force_peer_disconnect(struct lightningd *ld, const struct peer *peer, const char *why); -// void send_peer_alt_address(struct peer *peer, const struct pubkey *node_id, const u8 *alt_address); - void try_reconnect(const tal_t *ctx, struct peer *peer, const struct wireaddr_internal *addrhint); diff --git a/lightningd/lightningd.h b/lightningd/lightningd.h index 090e8b6c52cd..d06cf2b951b5 100644 --- a/lightningd/lightningd.h +++ b/lightningd/lightningd.h @@ -175,6 +175,9 @@ struct lightningd { /* Do we want to reconnect to other peers having only unannouced channels with us? */ bool reconnect_private; + /* Alt address for peer connections not publicly announced */ + u8 *alt_addr; + /* How many outstanding startup connection attempts? */ size_t num_startup_connects; diff --git a/lightningd/options.c b/lightningd/options.c index 8c530a8f9370..ad94f356982f 100644 --- a/lightningd/options.c +++ b/lightningd/options.c @@ -21,7 +21,6 @@ #include #include #include -// #include #include #include #include @@ -441,53 +440,18 @@ static char *opt_add_bind_addr(const char *arg, struct lightningd *ld) return opt_add_addr_withtype(arg, ld, ADDR_LISTEN); } -// static char *opt_add_alt_addr(const char *arg, struct lightningd *ld) //use bind-addr func instead? -// { -// return opt_add_addr_withtype(arg, ld, ADDR_LISTEN); -// } - static char *opt_add_alt_addr(const char *arg, struct lightningd *ld) { - // struct peer_htable_iter it; - // struct peer *peer = peer_htable_first(ld->peers, &it); - - char *result = opt_add_addr_withtype(arg, ld, ALT_ADDR_LISTEN); - opt_add_addr_withtype(arg, ld, ADDR_LISTEN); - - // If the addition of the address was successful, broadcast it to all peers - fprintf(stderr, "1HERE\n"); - // fprintf(stderr, "%s\n", result); - // if (result == NULL) { // NULL result means success - fprintf(stderr, "2HERE\n"); - // log_debug(ld->log, "Adding alternative address: %s", arg); - // if (!ld->wallet || !ld->wallet->db) { - // log_broken(ld->log, "Invalid wallet or database reference"); - // return NULL; - // } - // wallet_peer_alt_addr(ld->wallet->db, &ld->id, arg); - log_debug(ld->log, "Alternative address added successfully"); - fprintf(stderr, "3HERE\n"); - // while (peer) { - // if (pubkey_from_node_id(&pubkey, &peer->id)) { - // send_peer_alt_address(peer, &pubkey, alt_address); - /* } */ /* else { - status_peer_unusual(&peer->id, "Failed to obtain public key"); - } */ - // Attempt to add the address and listen/announce it based on the type - // } - // Convert the string arg to a u8 array if necessary. This is a placeholder conversion. - // const u8 *alt_address = (const u8 *)arg; - - // send_alt_address_to_all_peers(ld, alt_address); - - // // Optionally log the success of address addition and broadcasting - // status_info("Successfully added and broadcasted alternative address: %s", arg); - // } else { - // // Log failure to add the address - // status_peer_unusual(&ld->id, "Failed to add alternative address: %s", arg); - // } - - return result; + assert(arg != NULL); + + ld->alt_addr = tal_free(ld->alt_addr); + + ld->alt_addr = (u8 *)tal_strdup(ld, arg); + if (ld->alt_addr == NULL) { + return tal_fmt(tmpctx, "Failed to allocate memory for address: %s", arg); + } + + return opt_add_addr_withtype(arg, ld, ADDR_LISTEN); } static char *opt_subdaemon(const char *arg, struct lightningd *ld) diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 69d33212d435..5c93158f2256 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -3534,33 +3534,3 @@ void peer_dev_memleak(struct lightningd *ld, struct leak_detect *leaks) } } } - -// #include - -// void send_peer_alt_address(struct peer *peer, const struct pubkey *node_id, const u8 *alt_address) -// { -// assert(peer != NULL); -// assert(node_id != NULL); -// assert(alt_address != NULL); - -// u8 *msg = towire_peer_alt_address(peer, node_id, alt_address); -// peer_write(peer->pps, take(msg)); -// status_info("Sent alternative address message to peer"); -// } - -// void send_alt_address_to_all_peers(struct lightningd *ld, const u8 *alt_address) -// { -// struct peer_htable_iter it; - // struct peer *peer = peer_htable_first(ld->peers, &it); -// struct pubkey pubkey; - -// while (peer) { -// if (pubkey_from_node_id(&pubkey, &peer->id)) { -// send_peer_alt_address(peer, &pubkey, alt_address); -// } else { -// status_peer_unusual(&peer->id, "Failed to obtain public key"); -// } - -// peer = peer_htable_next(ld->peers, &it); -// } -// } diff --git a/lightningd/peer_control.h b/lightningd/peer_control.h index a75ac7db13d0..a693e0d87e08 100644 --- a/lightningd/peer_control.h +++ b/lightningd/peer_control.h @@ -87,10 +87,6 @@ void peer_connected(struct lightningd *ld, const u8 *msg); void peer_disconnect_done(struct lightningd *ld, const u8 *msg); void peer_spoke(struct lightningd *ld, const u8 *msg); -void send_peer_alt_address(struct peer *peer, const struct pubkey *node_id, const u8 *alt_address); -void send_alt_address_to_all_peers(struct lightningd *ld, const u8 *alt_address); - - /* Could be configurable. */ #define OUR_CHANNEL_FLAGS CHANNEL_FLAGS_ANNOUNCE_CHANNEL diff --git a/wallet/db.c b/wallet/db.c index bd0e1713d638..79f82c27cce9 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -1022,7 +1022,6 @@ static struct migration dbmigrations[] = { {SQL("ALTER TABLE channels ADD last_stable_connection BIGINT DEFAULT 0;"), NULL}, {NULL, migrate_initialize_alias_local}, {SQL("ALTER TABLE peers ADD COLUMN alt_addr TEXT;"), NULL}, - {SQL("ALTER TABLE vars ADD COLUMN alt_addr TEXT;"), NULL}, }; /** diff --git a/wallet/wallet.c b/wallet/wallet.c index 169ffb40ee7f..fc1dd7fa2da3 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -1027,44 +1027,9 @@ static struct peer *wallet_peer_load(struct wallet *w, const u64 dbid) return peer; } -#include - void wallet_peer_alt_addr(struct db *db, const struct node_id *node_id, const char *alt_addr) { struct db_stmt *stmt; - // bool transaction_started = false; - - // Print all parameters to stderr - fprintf(stderr, "INSIDE wallet_peer_alt_addr:\n"); - fprintf(stderr, "db pointer: %p\n", (void *)db); // Print the pointer value of db - fprintf(stderr, "Node ID: %s\n", fmt_node_id(tmpctx, node_id)); - fprintf(stderr, "alt_addr: %s\n", alt_addr ? alt_addr : "NULL"); // Print alt_addr, checking if it is NULL - - - fprintf(stderr, "INSIDE Adding alternative address: %s", alt_addr); - // if (!db_in_transaction(db)) { - // db_begin_transaction(db); - // transaction_started = true; - // } - - if (alt_addr != NULL) { - stmt = db_prepare_v2(db, SQL("UPDATE vars SET alt_addr=? WHERE node_id=?")); - db_bind_text(stmt, alt_addr); - } else { - stmt = db_prepare_v2(db, SQL("UPDATE vars SET alt_addr=NULL WHERE node_id=?")); - } - db_bind_node_id(stmt, node_id); - db_exec_prepared_v2(take(stmt)); - - - // if (transaction_started) { - // db_commit_transaction(db); - // // tal_free(stmt); - // } - - - -/* struct db_stmt *stmt; if (alt_addr != NULL) { stmt = db_prepare_v2(db, SQL("UPDATE peers SET alt_addr=? WHERE node_id=?")); @@ -1074,7 +1039,7 @@ void wallet_peer_alt_addr(struct db *db, const struct node_id *node_id, const ch } db_bind_node_id(stmt, node_id); db_exec_prepared_v2(take(stmt)); - */} +} static struct bitcoin_signature * wallet_htlc_sigs_load(const tal_t *ctx, struct wallet *w, u64 channelid, diff --git a/wire/peer_wire.csv b/wire/peer_wire.csv index ee4bb76016c6..93aa9a29f3b1 100644 --- a/wire/peer_wire.csv +++ b/wire/peer_wire.csv @@ -393,6 +393,6 @@ msgdata,onion_message,len,u16, msgdata,onion_message,onionmsg,byte,len, msgtype,peer_alt_address,208 msgdata,peer_alt_address,node_id,point, -msgdata,peer_alt_address,address_len,u16, +msgdata,peer_alt_address,address_len,u8, msgdata,peer_alt_address,address,byte,address_len, #msgdata,peer_alt_address,timestamp,u32,