Skip to content

Commit

Permalink
Just to showcase last days of work
Browse files Browse the repository at this point in the history
Signed-off-by: Max Rantil <[email protected]>
  • Loading branch information
maxrantil committed Jun 11, 2024
1 parent ed56faa commit 44315f2
Show file tree
Hide file tree
Showing 16 changed files with 366 additions and 62 deletions.
30 changes: 26 additions & 4 deletions channeld/channeld.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ struct peer {
};

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 billboard_update(const struct peer *peer)
{
Expand Down Expand Up @@ -535,16 +536,34 @@ 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 handle_peer_alt_addr(struct peer *peer, const u8 *msg)
{
struct pubkey peer_id;
u8 *alt_addr;
// u32 *timestamp = NULL;
fprintf(stderr, "handle_peer_alt_addr: called with message\n");
if (!fromwire_peer_alt_address(peer, (u8 *)msg, &peer_id, &alt_addr/* , timestamp */)) {
master_badmsg(WIRE_PEER_ALT_ADDRESS, (void *)msg);
// 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);
}
fprintf(stderr, "Alternative address for peer %s received.\n", type_to_string(tmpctx, struct pubkey, &peer_id));
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)
Expand Down Expand Up @@ -4182,6 +4201,9 @@ 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;

Expand Down
1 change: 1 addition & 0 deletions channeld/channeld.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
#include <ccan/tal/tal.h>

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 */
1 change: 1 addition & 0 deletions common/wireaddr.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ 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
};
Expand Down
164 changes: 164 additions & 0 deletions connectd/connectd.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <common/gossip_store.h>
#include <common/jsonrpc_errors.h>
#include <common/memleak.h>
// #include <common/peer_io.h>
#include <common/status.h>
#include <common/subdaemon.h>
#include <common/timeout.h>
Expand All @@ -48,6 +49,7 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <wire/peer_wiregen.h>
#include <wire/wire_io.h>
#include <wire/wire_sync.h>

Expand All @@ -58,6 +60,9 @@
#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
Expand Down Expand Up @@ -298,6 +303,27 @@ 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);
}
Expand Down Expand Up @@ -1143,6 +1169,90 @@ 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 <assert.h>
#include <stdio.h>

// 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.
Expand Down Expand Up @@ -1194,13 +1304,19 @@ 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];
bool announce = (proposed_listen_announce[i] & ADDR_ANNOUNCE);
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:
Expand Down Expand Up @@ -1672,6 +1788,8 @@ 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
Expand All @@ -1694,6 +1812,8 @@ 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;
}

Expand Down Expand Up @@ -1768,13 +1888,57 @@ 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);

/* Now we kick it off by recursively trying connect->addrs[connect->addrnum] */
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");
// }
// }

/* 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)
{
Expand Down
13 changes: 13 additions & 0 deletions connectd/connectd.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ 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 {
Expand All @@ -157,6 +159,13 @@ 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)
Expand Down Expand Up @@ -292,4 +301,8 @@ 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);

#endif /* LIGHTNING_CONNECTD_CONNECTD_H */
1 change: 1 addition & 0 deletions contrib/startup_regtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ start_nodes() {
log-level=debug
log-file=$LIGHTNING_DIR/l$i/log
addr=localhost:$socket
alt-addr=127.21.21.21:$socket
allow-deprecated-apis=false
developer
dev-fast-gossip
Expand Down
2 changes: 2 additions & 0 deletions lightningd/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -798,5 +798,7 @@ 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 */
8 changes: 8 additions & 0 deletions lightningd/connect_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ 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);
Expand Down Expand Up @@ -364,6 +366,12 @@ static void try_connect(const tal_t *ctx,
/* Update any channel billboards */
peer = peer_by_id(ld, id);
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");
// }
struct channel *channel;
list_for_each(&peer->channels, channel, list) {
if (!channel_state_wants_peercomms(channel->state))
Expand Down
2 changes: 2 additions & 0 deletions lightningd/connect_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ 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);
Expand Down
Loading

0 comments on commit 44315f2

Please sign in to comment.