2525#include <common/gossip_store.h>
2626#include <common/jsonrpc_errors.h>
2727#include <common/memleak.h>
28+ // #include <common/peer_io.h>
2829#include <common/status.h>
2930#include <common/subdaemon.h>
3031#include <common/timeout.h>
4849#include <sys/types.h>
4950#include <sys/wait.h>
5051#include <unistd.h>
52+ #include <wire/peer_wiregen.h>
5153#include <wire/wire_io.h>
5254#include <wire/wire_sync.h>
5355
5860#define HSM_FD 3
5961#define GOSSIPCTL_FD 4
6062
63+ void peer_connected_wrapper (struct daemon * daemon , struct peer * peer );
64+
65+
6166/*~ C programs should generally be written bottom-to-top, with the root
6267 * function at the bottom, and functions it calls above it. That avoids
6368 * us having to pre-declare functions; but in the case of mutual recursion
@@ -298,6 +303,27 @@ struct io_plan *peer_connected(struct io_conn *conn,
298303 * it that, too. */
299304 daemon_conn_send (daemon -> master , take (msg ));
300305
306+ /* Invoke the callback if set */
307+ // if (connect && connect->cb) {
308+ // connect->cb(daemon, peer);
309+ // struct pubkey pubkey;
310+ // if (pubkey_from_node_id(&pubkey, id)) {
311+ // send_peer_alt_address(peer, &pubkey, (const u8 *)"127.21.21.21");
312+ // } else {
313+ // status_peer_unusual(id, "THIS IS A TEST 8");
314+ // }
315+ // struct pubkey pubkey;
316+ // if (pubkey_from_node_id(&pubkey, id)) {
317+ // send_peer_alt_address(peer, &pubkey, (const u8 *)"127.21.21.21");
318+ // u8 *msg = towire_peer_alt_address(peer, &pubkey, (const u8 *)"127.21.21.21");
319+ // peer_write(peer->pps, take(msg));
320+ // status_info("Sent alternative address message to peer");
321+ // } else {
322+ // status_peer_unusual(id, "THIS IS A TEST 8");
323+ // }
324+ // tal_free(connect);
325+ // }
326+
301327 /*~ Now we set up this connection to read/write from subd */
302328 return multiplex_peer_setup (conn , peer );
303329}
@@ -1143,6 +1169,90 @@ static bool want_tor(const struct wireaddr_internal *proposed_wireaddr)
11431169 return false;
11441170}
11451171
1172+ // static void send_peer_message(const struct daemon *daemon) {
1173+ // // Placeholder for message sending logic to all peers
1174+ // fprintf(stderr, "Peer message sent due to ALT_ADDR_LISTEN.\n");
1175+
1176+ // u8 *msg = towire_peer_alt_address(peer, node_id, alt_address);
1177+
1178+ // peer_write(peer->pps, take(msg));
1179+ // fprintf(stderr, "Sent alternative address message to peer.\n");
1180+ #include <assert.h>
1181+ #include <stdio.h>
1182+
1183+ // Assuming a serialization function towire_peer_alt_address exists
1184+ // And assuming peer_write sends messages to the peers
1185+
1186+ // static void send_peer_message(const struct daemon *daemon, const u8 *alt_address) {
1187+ // fprintf(stderr, "Preparing to send ALT_ADDR_LISTEN message to all peers.\n");
1188+
1189+ // struct peer *peer;
1190+ // struct peer_htable_iter it;
1191+ // struct pubkey pb;
1192+ // u8 *msg;
1193+
1194+ // // Start iterating from the first peer
1195+ // peer = peer_htable_first(daemon->peers, &it);
1196+ // while (peer) {
1197+ // if (!pubkey_from_node_id(&pb, &peer->id)) {
1198+ // fprintf(stderr, "Failed to get public key for peer %s.\n", fmt_node_id(tmpctx, &peer->id));
1199+ // continue;
1200+ // }
1201+ // // Create a message for the current peer with the alternative address
1202+ // msg = towire_peer_alt_address(tmpctx, &pb, alt_address); // Assuming tmpctx or some context
1203+ // if (msg == NULL) {
1204+ // fprintf(stderr, "Failed to create message for peer %s.\n", fmt_node_id(tmpctx, &peer->id));
1205+ // continue;
1206+ // }
1207+
1208+ // // // Ensure `peer->pps` is the correct member that holds the peer's connection context
1209+ // // if (peer_write(peer->pps, take(msg))) { // Update this line if `pps` is not correct
1210+ // if (io_write_wire(conn, take(msg), io_close_cb, NULL);) { // Update this line if `pps` is not correct
1211+ // fprintf(stderr, "Sent ALT_ADDR_LISTEN message to peer %s.\n", fmt_node_id(tmpctx, &peer->id));
1212+ // } else {
1213+ // fprintf(stderr, "Failed to send ALT_ADDR_LISTEN message to peer %s.\n", fmt_node_id(tmpctx, &peer->id));
1214+ // }
1215+
1216+ // // Move to the next peer
1217+ // peer = peer_htable_next(daemon->peers, &it);
1218+ // }
1219+ // }
1220+
1221+ void broadcast_alt_address (struct daemon * daemon , const u8 * alt_address ) {
1222+ struct peer * peer ;
1223+ struct peer_htable_iter it ;
1224+
1225+ fprintf (stderr , "Starting to broadcast alternative address to all peers.\n" );
1226+ peer = peer_htable_first (daemon -> peers , & it );
1227+
1228+ if (!peer ) {
1229+ fprintf (stderr , "No peers to broadcast to.\n" );
1230+ }
1231+
1232+ while (peer ) {
1233+ struct pubkey pb ;
1234+ if (pubkey_from_node_id (& pb , & peer -> id )) {
1235+ fprintf (stderr , "Public key successfully retrieved for peer %s.\n" , fmt_node_id (tmpctx , & peer -> id ));
1236+ u8 * msg = towire_peer_alt_address (tmpctx , & pb , alt_address );
1237+ if (msg ) {
1238+ fprintf (stderr , "Message successfully serialized for peer %s.\n" , fmt_node_id (tmpctx , & peer -> id ));
1239+ if (io_write_wire (peer -> to_peer , take (msg ), io_close_cb , NULL )) {
1240+ fprintf (stderr , "Message successfully sent to peer %s.\n" , fmt_node_id (tmpctx , & peer -> id ));
1241+ } else {
1242+ fprintf (stderr , "Failed to send message to peer %s.\n" , fmt_node_id (tmpctx , & peer -> id ));
1243+ }
1244+ } else {
1245+ fprintf (stderr , "Failed to serialize message for peer %s.\n" , fmt_node_id (tmpctx , & peer -> id ));
1246+ }
1247+ } else {
1248+ fprintf (stderr , "Failed to get public key for peer %s.\n" , fmt_node_id (tmpctx , & peer -> id ));
1249+ }
1250+ peer = peer_htable_next (daemon -> peers , & it );
1251+ }
1252+ }
1253+
1254+
1255+
11461256/*~ The user can specify three kinds of addresses: ones we bind to but don't
11471257 * announce, ones we announce but don't bind to, and ones we bind to and
11481258 * announce if they seem to be public addresses.
@@ -1194,13 +1304,19 @@ setup_listeners(const tal_t *ctx,
11941304 add_announceable (announceable , & wa .u .wireaddr .wireaddr );
11951305 }
11961306
1307+
11971308 /* Now look for listening addresses. */
11981309 for (size_t i = 0 ; i < tal_count (proposed_wireaddr ); i ++ ) {
11991310 struct wireaddr_internal wa = proposed_wireaddr [i ];
12001311 bool announce = (proposed_listen_announce [i ] & ADDR_ANNOUNCE );
12011312 if (!(proposed_listen_announce [i ] & ADDR_LISTEN ))
12021313 continue ;
12031314
1315+ if (ALT_ADDR_LISTEN ) {
1316+ broadcast_alt_address (daemon , (const u8 * )"127.22.22.22" ); // Send a message to all peers when ALT_ADDR_LISTEN is used
1317+ // send_peer_message(daemon, (const u8 *)"127.22.22.22"); // Send a message to all peers when ALT_ADDR_LISTEN is used
1318+ }
1319+
12041320 switch (wa .itype ) {
12051321 /* We support UNIX domain sockets, but can't announce */
12061322 case ADDR_INTERNAL_SOCKNAME :
@@ -1672,6 +1788,8 @@ static void add_gossip_addrs(struct wireaddr_internal **addrs,
16721788 add_gossip_addrs_bytypes (addrs , normal_addrs , addrhint , types [i ]);
16731789}
16741790
1791+ typedef void (* peer_connected_cb )(struct daemon * daemon , struct peer * peer );
1792+
16751793/*~ Consumes addrhint if not NULL.
16761794 *
16771795 * That's a pretty ugly interface: we should use TAKEN, but we only have one
@@ -1694,6 +1812,8 @@ static void try_connect_peer(struct daemon *daemon,
16941812 /* Note if we explicitly tried to connect non-transiently */
16951813 if (!transient )
16961814 peer -> prio = PRIO_DELIBERATE ;
1815+ // if (cb)
1816+ // cb(daemon, peer);
16971817 return ;
16981818 }
16991819
@@ -1768,13 +1888,57 @@ static void try_connect_peer(struct daemon *daemon,
17681888 connect -> errors = tal_strdup (connect , "" );
17691889 connect -> conn = NULL ;
17701890 connect -> transient = transient ;
1891+ // connect->cb = cb;
17711892 connecting_htable_add (daemon -> connecting , connect );
17721893 tal_add_destructor (connect , destroy_connecting );
17731894
17741895 /* Now we kick it off by recursively trying connect->addrs[connect->addrnum] */
17751896 try_connect_one_addr (connect );
17761897}
17771898
1899+ // void peer_connected_wrapper(struct daemon *daemon, struct peer *peer) {
1900+ // // Extract necessary information from the peer or connecting structure
1901+ // struct io_conn *conn = peer->conn;
1902+ // struct node_id *id = &peer->id;
1903+ // struct wireaddr_internal *addr = &peer->addr;
1904+ // struct crypto_state *cs = &peer->cs;
1905+ // const u8 *their_features = peer->their_features;
1906+ // enum is_websocket is_websocket = peer->is_websocket;
1907+ // bool incoming = peer->incoming;
1908+
1909+ // // Call the actual peer_connected function
1910+ // peer_connected(conn, daemon, id, addr, NULL, cs, their_features, is_websocket, incoming);
1911+ // }
1912+
1913+ // void peer_connected_wrapper(struct daemon *daemon, struct peer *peer) {
1914+ // struct connecting *connect = find_connecting(daemon, &peer->id);
1915+
1916+ // if (connect) {
1917+ // struct io_conn *conn = connect->conn;
1918+ // struct node_id *id = &peer->id;
1919+ // // struct wireaddr_internal *addr = connect->addrs + connect->addrnum;
1920+ // struct crypto_state *cs = &peer->cs;
1921+ // const u8 *their_features = connect->their_features; // Adjust this as needed
1922+ // enum is_websocket is_websocket = peer->is_websocket;
1923+ // bool incoming = connect->incoming;
1924+
1925+ // // Create a wireaddr_internal structure with the hardcoded IP address
1926+ // struct wireaddr_internal hardcoded_addr;
1927+ // hardcoded_addr.itype = ADDR_INTERNAL_WIREADDR;
1928+ // hardcoded_addr.u.wireaddr.wireaddr.type = ADDR_TYPE_IPV4;
1929+ // hardcoded_addr.u.wireaddr.wireaddr.addrlen = 4;
1930+ // inet_pton(AF_INET, "127.21.21.21", &hardcoded_addr.u.wireaddr.wireaddr.addr);
1931+ // hardcoded_addr.u.wireaddr.wireaddr.port = connect->addrs[connect->addrnum].u.wireaddr.wireaddr.port;
1932+
1933+
1934+ // // Call the actual peer_connected function
1935+ // // peer_connected(conn, daemon, id, addr, NULL, cs, their_features, is_websocket, incoming);
1936+ // peer_connected(conn, daemon, id, &hardcoded_addr, NULL, cs, their_features, is_websocket, incoming);
1937+ // } else {
1938+ // status_broken("Failed to find connecting structure for peer");
1939+ // }
1940+ // }
1941+
17781942/* lightningd tells us to connect to a peer by id, with optional addr hint. */
17791943static void connect_to_peer (struct daemon * daemon , const u8 * msg )
17801944{
0 commit comments