Skip to content

Commit

Permalink
Connect to peers alt addr (if provided) when reconnecting. If both nodes
Browse files Browse the repository at this point in the history
provide alt addrsses only the connection_out will utilize the alt addr.

Signed-off-by: Max Rantil <[email protected]>
  • Loading branch information
maxrantil committed Jun 26, 2024
1 parent b1b9a49 commit 24018af
Show file tree
Hide file tree
Showing 15 changed files with 150 additions and 54 deletions.
14 changes: 7 additions & 7 deletions channeld/channeld.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,11 @@ struct peer {
bool experimental_upgrade;

/* Alt address for peer connections not publicly announced */
u8 *alt_addr;
u8 *our_alt_addr;
};

static void start_commit_timer(struct peer *peer);
static void send_peer_alt_address(struct peer *peer);
static void send_peer_our_alt_address(struct peer *peer);

static void billboard_update(const struct peer *peer)
{
Expand Down Expand Up @@ -542,11 +542,11 @@ static void handle_peer_splice_locked(struct peer *peer, const u8 *msg)
check_mutual_splice_locked(peer);
}

static void send_peer_alt_address(struct peer *peer) {
static void send_peer_our_alt_address(struct peer *peer) {
struct pubkey node_id;

if (pubkey_from_node_id(&node_id, &peer->id)) {
u8 *msg = towire_peer_alt_address(peer, &node_id, peer->alt_addr);
u8 *msg = towire_peer_alt_address(peer, &node_id, peer->our_alt_addr);
peer_write(peer->pps, take(msg));
}
}
Expand Down Expand Up @@ -4179,8 +4179,8 @@ static void peer_in(struct peer *peer, const u8 *msg)

check_tx_abort(peer, msg);

if (peer->alt_addr)
send_peer_alt_address(peer);
if (peer->our_alt_addr)
send_peer_our_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 */
Expand Down Expand Up @@ -5890,7 +5890,7 @@ static void init_channel(struct peer *peer)
&peer->experimental_upgrade,
&peer->splice_state->inflights,
&peer->local_alias,
&peer->alt_addr,
&peer->our_alt_addr,
&peer->id)) {
master_badmsg(WIRE_CHANNELD_INIT, msg);
}
Expand Down
36 changes: 19 additions & 17 deletions connectd/connectd.c
Original file line number Diff line number Diff line change
Expand Up @@ -506,9 +506,26 @@ static struct io_plan *connection_in(struct io_conn *conn,

conn_in_arg.daemon = daemon;
conn_in_arg.is_websocket = false;

return conn_in(conn, &conn_in_arg);
}

void handle_peer_alt_address(struct peer *peer, const u8 *msg)
{
u8 *peer_alt_addr;
struct pubkey peer_id;
/* u32 *timestamp = NULL; */ /* TODO */

if (!fromwire_peer_alt_address(peer, msg, &peer_id, &peer_alt_addr/* , timestamp */)) {
master_badmsg(WIRE_PEER_ALT_ADDRESS, msg);
}

msg = towire_connectd_alt_address(NULL, &peer_id, peer_alt_addr);
daemon_conn_send(peer->daemon->master, take(msg));

tal_free(peer_alt_addr);
}

/*~ <hello>I speak web socket</hello>.
*
* Actually that's dumb, websocket (aka rfc6455) looks nothing like that. */
Expand Down Expand Up @@ -1414,7 +1431,8 @@ static void connect_init(struct daemon *daemon, const u8 *msg)
&daemon->dev_fast_gossip,
&dev_disconnect,
&daemon->dev_no_ping_timer,
&daemon->dev_handshake_no_reply)) {
&daemon->dev_handshake_no_reply/* , //This is WIP
&daemon->alt_addr */)) {
/* This is a helper which prints the type expected and the actual
* message, then exits (it should never be called!). */
master_badmsg(WIRE_CONNECTD_INIT, msg);
Expand Down Expand Up @@ -1776,22 +1794,6 @@ static void try_connect_peer(struct daemon *daemon,
try_connect_one_addr(connect);
}

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)
{
Expand Down
6 changes: 5 additions & 1 deletion connectd/connectd.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ struct peer {

/* The pubkey of the node */
struct node_id id;

/* Counters and keys for symmetric crypto */
struct crypto_state cs;

Expand Down Expand Up @@ -225,6 +226,9 @@ struct daemon {
* resort, but doing so leaks our address so can be disabled. */
bool use_dns;

// This is WIP
/* u8 *alt_addr; */

/* The address that the broken response returns instead of
* NXDOMAIN. NULL if we have not detected a broken resolver. */
struct sockaddr *broken_resolver_response;
Expand Down Expand Up @@ -294,6 +298,6 @@ void destroy_peer(struct peer *peer);
void close_random_connection(struct daemon *daemon);

/* Handles alternative address message from peer. */
void handle_peer_alt_addr(struct peer *peer, const u8 *msg);
void handle_peer_alt_address(struct peer *peer, const u8 *msg);

#endif /* LIGHTNING_CONNECTD_CONNECTD_H */
3 changes: 3 additions & 0 deletions connectd/connectd_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ msgdata,connectd_init,dev_disconnect,bool,
msgdata,connectd_init,dev_no_ping_timer,bool,
# Allow incoming connections, but don't talk.
msgdata,connectd_init,dev_noreply,bool,
# Use an alternative private address if provided by peer.
#msgdata,connectd_init,alt_addr_len,u16,
#msgdata,connectd_init,alt_addr,u8,alt_addr_len,

# Connectd->master, here are the addresses I bound, can announce.
msgtype,connectd_init_reply,2100
Expand Down
2 changes: 1 addition & 1 deletion connectd/multiplex.c
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ static bool handle_message_locally(struct peer *peer, const u8 *msg)
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);
handle_peer_alt_address(peer, msg);
return true;
} else if (handle_custommsg(peer->daemon, peer, msg)) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion doc/schemas/lightning-listconfigs.json
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,7 @@
],
"properties": {
"values_str": {
"added": "v24.05",
"added": "v24.05",
"type": "array",
"items": {
"type": "string",
Expand Down
1 change: 1 addition & 0 deletions lightningd/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -798,4 +798,5 @@ const u8 *channel_update_for_error(const tal_t *ctx,

struct amount_msat htlc_max_possible_send(const struct channel *channel);


#endif /* LIGHTNING_LIGHTNINGD_CHANNEL_H */
2 changes: 1 addition & 1 deletion lightningd/channel_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -1726,7 +1726,7 @@ bool peer_start_channeld(struct channel *channel,
cast_const2(const struct inflight **,
inflights),
*channel->alias[LOCAL],
ld->alt_addr,
ld->our_alt_addr,
&ld->id);

/* We don't expect a response: we are triggered by funding_depth_cb. */
Expand Down
28 changes: 15 additions & 13 deletions lightningd/connect_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,9 @@ static void try_connect(const tal_t *ctx,
{
struct delayed_reconnect *d;
struct peer *peer;
const struct wireaddr_internal *alt_addr;

alt_addr = wallet_get_peer_alt_addr(ld->wallet, id);

/* Don't stack, unless this is an instant reconnect */
d = delayed_reconnect_map_get(ld->delayed_reconnect_map, id);
Expand All @@ -349,7 +352,7 @@ static void try_connect(const tal_t *ctx,
d = tal(ctx, struct delayed_reconnect);
d->ld = ld;
d->id = *id;
d->addrhint = tal_dup_or_null(d, struct wireaddr_internal, addrhint);
d->addrhint = tal_dup_or_null(d, struct wireaddr_internal, alt_addr ? alt_addr : addrhint);
d->dns_fallback = dns_fallback;
delayed_reconnect_map_add(ld->delayed_reconnect_map, d);
tal_add_destructor(d, destroy_delayed_reconnect);
Expand All @@ -364,9 +367,6 @@ 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"); // 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))
Expand Down Expand Up @@ -559,20 +559,21 @@ 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 node_id;
struct node_id id;
u8 *alt_addr;
struct pubkey peer_node_id;
u8 *peer_alt_addr;

if (!fromwire_connectd_alt_address(tmpctx, msg, &node_id, &alt_addr)) {
if (!fromwire_connectd_alt_address(tmpctx, msg, &peer_node_id, &peer_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);
struct node_id id;
node_id_from_pubkey(&id, &peer_node_id);
wallet_add_peer_alt_addr(ld->wallet->db, &id, (char *)peer_alt_addr);
// SHOULD/CAN WE ALSO SAVE IT TO THE peer struct HERE, TO BE ABLE TO GET IN IT CONNECTD LATER FOR THE `connection_in`???
}

static void connectd_start_shutdown_reply(struct subd *connectd,
Expand Down Expand Up @@ -666,7 +667,7 @@ static unsigned connectd_msg(struct subd *connectd, const u8 *msg, const int *fd
break;

case WIRE_CONNECTD_ALT_ADDRESS:
handle_alt_addr_in(connectd->ld, msg);
handle_peer_alt_addr_in(connectd->ld, msg);
break;
}
return 0;
Expand Down Expand Up @@ -788,7 +789,8 @@ int connectd_init(struct lightningd *ld)
ld->dev_fast_gossip,
ld->dev_disconnect_fd >= 0,
ld->dev_no_ping_timer,
ld->dev_handshake_no_reply);
ld->dev_handshake_no_reply/* , //This is WIP
ld->our_alt_addr */);

subd_req(ld->connectd, ld->connectd, take(msg), -1, 0,
connect_init_done, NULL);
Expand Down
6 changes: 3 additions & 3 deletions lightningd/lightningd.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,6 @@ 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;

Expand All @@ -196,6 +193,9 @@ struct lightningd {
struct wireaddr_internal *binding;
struct wireaddr *announceable;

/* Alternative address for peer connections not publicly announced */
u8 *our_alt_addr;

/* Current node announcement (if any) */
const u8 *node_announcement;

Expand Down
6 changes: 3 additions & 3 deletions lightningd/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -444,10 +444,10 @@ static char *opt_add_alt_addr(const char *arg, struct lightningd *ld)
{
assert(arg != NULL);

ld->alt_addr = tal_free(ld->alt_addr);
ld->our_alt_addr = tal_free(ld->our_alt_addr);

ld->alt_addr = (u8 *)tal_strdup(ld, arg);
if (ld->alt_addr == NULL) {
ld->our_alt_addr = (u8 *)tal_strdup(ld, arg);
if (ld->our_alt_addr == NULL) {
return tal_fmt(tmpctx, "Failed to allocate memory for address: %s", arg);
}

Expand Down
7 changes: 7 additions & 0 deletions lightningd/peer_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -2250,6 +2250,13 @@ static void json_add_peer(struct lightningd *ld,
fmt_wireaddr(response, p->remote_addr));
}

struct wireaddr_internal *peer_alt_addr;
peer_alt_addr = wallet_get_peer_alt_addr(ld->wallet, &p->id);
if (peer_alt_addr) {
json_add_string(response, "alt_addr",
fmt_wireaddr_internal(tmpctx, peer_alt_addr));
}

/* Note: If !PEER_CONNECTED, peer may use different features on reconnect */
json_add_hex_talarr(response, "features", p->their_features);

Expand Down
4 changes: 0 additions & 4 deletions pytest.ini

This file was deleted.

73 changes: 72 additions & 1 deletion wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,7 @@ static struct peer *wallet_peer_load(struct wallet *w, const u64 dbid)
return peer;
}

void wallet_peer_alt_addr(struct db *db, const struct node_id *node_id, const char *alt_addr)
void wallet_add_peer_alt_addr(struct db *db, const struct node_id *node_id, const char *alt_addr)
{
struct db_stmt *stmt;

Expand All @@ -1041,6 +1041,77 @@ void wallet_peer_alt_addr(struct db *db, const struct node_id *node_id, const ch
db_exec_prepared_v2(take(stmt));
}

static struct wireaddr_internal *handle_alt_addr_failure(struct wallet *w, struct db_stmt *stmt, bool transaction_started)
{
tal_free(stmt);
if (transaction_started) {
db_commit_transaction(w->db);
}
return NULL;
}

struct wireaddr_internal *wallet_get_peer_alt_addr(struct wallet *w, const struct node_id *node_id)
{
struct db_stmt *stmt;
struct wireaddr_internal *alt_address = NULL;
bool transaction_started = false;

if (!db_in_transaction(w->db)) {
db_begin_transaction(w->db);
transaction_started = true;
}

stmt = db_prepare_v2(w->db, SQL("SELECT alt_addr FROM peers WHERE node_id=?"));
if (!stmt) {
log_broken(w->log, "Failed to prepare statement for node_id %s", fmt_node_id(tmpctx, node_id));
return handle_alt_addr_failure(w, stmt, transaction_started);
}

db_bind_node_id(stmt, node_id);
db_query_prepared(stmt);

if (!db_step(stmt)) {
log_broken(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, "alt_addr");
if (!addr_str) {
log_broken(w->log, "No 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_broken(w->log, "Empty address string retrieved for peer %s", fmt_node_id(tmpctx, node_id));
tal_free(addr_str);
return handle_alt_addr_failure(w, stmt, transaction_started);
}

alt_address = tal(tmpctx, struct wireaddr_internal);
if (!alt_address) {
log_broken(w->log, "Memory allocation failed for alternative address of peer %s", fmt_node_id(tmpctx, node_id));
tal_free(addr_str);
return handle_alt_addr_failure(w, stmt, transaction_started);
}

const char *err = parse_wireaddr_internal(tmpctx, addr_str, 0, false, alt_address);
if (err) {
log_broken(w->log, "Invalid alternative address %s for peer %s: %s",
addr_str, fmt_node_id(tmpctx, node_id), err);
tal_free(alt_address);
alt_address = NULL;
}

tal_free(addr_str);
tal_free(stmt);

if (transaction_started) {
db_commit_transaction(w->db);
}

return alt_address;
}

static struct bitcoin_signature *
wallet_htlc_sigs_load(const tal_t *ctx, struct wallet *w, u64 channelid,
bool option_anchors)
Expand Down
Loading

0 comments on commit 24018af

Please sign in to comment.