Skip to content

Commit 44315f2

Browse files
committed
Just to showcase last days of work
Signed-off-by: Max Rantil <[email protected]>
1 parent ed56faa commit 44315f2

File tree

16 files changed

+366
-62
lines changed

16 files changed

+366
-62
lines changed

channeld/channeld.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ struct peer {
194194
};
195195

196196
static void start_commit_timer(struct peer *peer);
197+
// void send_peer_alt_address(struct peer *peer, const struct pubkey *node_id, const u8 *alt_address);
197198

198199
static void billboard_update(const struct peer *peer)
199200
{
@@ -535,16 +536,34 @@ static void handle_peer_splice_locked(struct peer *peer, const u8 *msg)
535536
check_mutual_splice_locked(peer);
536537
}
537538

539+
// void send_peer_alt_address(struct peer *peer, const struct pubkey *node_id, const u8 *alt_address)
540+
// {
541+
// u8 *msg = towire_peer_alt_address(peer, node_id, alt_address);
542+
543+
// peer_write(peer->pps, take(msg));
544+
// fprintf(stderr, "Sent alternative address message to peer");
545+
// }
546+
538547
static void handle_peer_alt_addr(struct peer *peer, const u8 *msg)
539548
{
540549
struct pubkey peer_id;
541550
u8 *alt_addr;
542551
// u32 *timestamp = NULL;
543-
fprintf(stderr, "handle_peer_alt_addr: called with message\n");
544-
if (!fromwire_peer_alt_address(peer, (u8 *)msg, &peer_id, &alt_addr/* , timestamp */)) {
545-
master_badmsg(WIRE_PEER_ALT_ADDRESS, (void *)msg);
552+
// fprintf(stderr, "Entering handle_peer_alt_eight with msg %s\n", msg);
553+
fprintf(stderr, "1 THIS IS A TEST\n");
554+
status_info("2 THIS IS A TEST");
555+
if (!fromwire_peer_alt_address(tmpctx, msg, &peer_id, &alt_addr/* , timestamp */)) {
556+
master_badmsg(WIRE_PEER_ALT_ADDRESS, msg);
546557
}
547-
fprintf(stderr, "Alternative address for peer %s received.\n", type_to_string(tmpctx, struct pubkey, &peer_id));
558+
fprintf(stderr, "3 THIS IS A TEST\n");
559+
status_info("3.5 THIS IS A TEST");
560+
// peer = peer_htable_get(daemon->peers, &id);
561+
// if (!peer)
562+
// return;
563+
564+
// Store the alternative address in the peer structure
565+
// peer->alt_address = alt_addr; // Assuming you have such a field in the struct
566+
// log_info(peer->log, "Received alt address: %s", alt_addr);
548567

549568
// struct peer *peer = peer_htable_get(daemon->peers, &peer_id);
550569
// if (!peer)
@@ -4182,6 +4201,9 @@ static void peer_in(struct peer *peer, const u8 *msg)
41824201
{
41834202
enum peer_wire type = fromwire_peektype(msg);
41844203

4204+
// fprintf(stderr, "4 THIS IS A TEST\n");
4205+
// status_info("5 THIS IS A TEST %s", msg);
4206+
41854207
if (handle_peer_error_or_warning(peer->pps, msg))
41864208
return;
41874209

channeld/channeld.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
#include <ccan/tal/tal.h>
77

88
const u8 *hsm_req(const tal_t *ctx, const u8 *req TAKES);
9+
// void send_peer_alt_address(struct peer *peer, const struct pubkey *node_id, const u8 *alt_address);
910

1011
#endif /* LIGHTNING_CHANNELD_CHANNELD_H */

common/wireaddr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ bool wireaddr_eq_without_port(const struct wireaddr *a, const struct wireaddr *b
5656
* announce */
5757
enum addr_listen_announce {
5858
ADDR_LISTEN = (1 << 0),
59+
ALT_ADDR_LISTEN = (2 << 0),
5960
ADDR_ANNOUNCE = (1 << 1),
6061
ADDR_LISTEN_AND_ANNOUNCE = ADDR_LISTEN|ADDR_ANNOUNCE
6162
};

connectd/connectd.c

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
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>
@@ -48,6 +49,7 @@
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

@@ -58,6 +60,9 @@
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. */
17791943
static void connect_to_peer(struct daemon *daemon, const u8 *msg)
17801944
{

connectd/connectd.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ HTABLE_DEFINE_TYPE(struct peer,
132132
peer_eq_node_id,
133133
peer_htable);
134134

135+
// typedef void (*peer_connected_cb)(struct daemon *daemon, struct peer *peer);
136+
135137
/*~ Peers we're trying to reach: we iterate through addrs until we succeed
136138
* or fail. */
137139
struct connecting {
@@ -157,6 +159,13 @@ struct connecting {
157159

158160
/* Is this a transient connection? */
159161
bool transient;
162+
163+
/* Callback to call when connected */
164+
// peer_connected_cb cb;
165+
166+
/* Additional fields as needed */
167+
// const u8 *their_features;
168+
// bool incoming;
160169
};
161170

162171
static const struct node_id *connecting_keyof(const struct connecting *connecting)
@@ -292,4 +301,8 @@ void destroy_peer(struct peer *peer);
292301

293302
/* Remove a random connection, when under stress. */
294303
void close_random_connection(struct daemon *daemon);
304+
305+
void send_peer_alt_address(struct peer *peer, const struct pubkey *node_id, const u8 *alt_address);
306+
void broadcast_alt_address(struct daemon *daemon, const u8 *alt_address);
307+
295308
#endif /* LIGHTNING_CONNECTD_CONNECTD_H */

contrib/startup_regtest.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ start_nodes() {
184184
log-level=debug
185185
log-file=$LIGHTNING_DIR/l$i/log
186186
addr=localhost:$socket
187+
alt-addr=127.21.21.21:$socket
187188
allow-deprecated-apis=false
188189
developer
189190
dev-fast-gossip

lightningd/channel.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,5 +798,7 @@ const u8 *channel_update_for_error(const tal_t *ctx,
798798

799799
struct amount_msat htlc_max_possible_send(const struct channel *channel);
800800

801+
// void send_peer_alt_address(struct peer *peer, const struct pubkey *node_id, const u8 *alt_address);
802+
801803

802804
#endif /* LIGHTNING_LIGHTNINGD_CHANNEL_H */

lightningd/connect_control.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ struct connect {
2929
struct command *cmd;
3030
};
3131

32+
// void send_peer_alt_address(struct peer *peer, const struct pubkey *node_id, const u8 *alt_address);
33+
3234
static void destroy_connect(struct connect *c)
3335
{
3436
list_del(&c->list);
@@ -364,6 +366,12 @@ static void try_connect(const tal_t *ctx,
364366
/* Update any channel billboards */
365367
peer = peer_by_id(ld, id);
366368
if (peer) {
369+
// struct pubkey pubkey;
370+
// if (pubkey_from_node_id(&pubkey, id)) {
371+
// send_peer_alt_address(peer, &pubkey, (const u8 *)"127.21.21.21");
372+
// } else {
373+
// log_peer_debug(ld->log, id, "THIS IS A TEST 9");
374+
// }
367375
struct channel *channel;
368376
list_for_each(&peer->channels, channel, list) {
369377
if (!channel_state_wants_peercomms(channel->state))

lightningd/connect_control.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ void force_peer_disconnect(struct lightningd *ld,
2020
const struct peer *peer,
2121
const char *why);
2222

23+
// void send_peer_alt_address(struct peer *peer, const struct pubkey *node_id, const u8 *alt_address);
24+
2325
void try_reconnect(const tal_t *ctx,
2426
struct peer *peer,
2527
const struct wireaddr_internal *addrhint);

0 commit comments

Comments
 (0)