Skip to content

Commit

Permalink
connectd: ensure htables are always tal objects.
Browse files Browse the repository at this point in the history
We want to change the htable allocator to use tal, which will need
this.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Jan 12, 2023
1 parent 4a570c9 commit 81e57dc
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
21 changes: 11 additions & 10 deletions connectd/connectd.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ void destroy_peer(struct peer *peer)
{
assert(!peer->draining);

if (!peer_htable_del(&peer->daemon->peers, peer))
if (!peer_htable_del(peer->daemon->peers, peer))
abort();

/* Tell gossipd to stop asking this peer gossip queries */
Expand Down Expand Up @@ -257,7 +257,7 @@ static struct peer *new_peer(struct daemon *daemon,

/* Now we own it */
tal_steal(peer, peer->to_peer);
peer_htable_add(&daemon->peers, peer);
peer_htable_add(daemon->peers, peer);
tal_add_destructor(peer, destroy_peer);

return peer;
Expand All @@ -282,7 +282,7 @@ struct io_plan *peer_connected(struct io_conn *conn,
bool option_gossip_queries;

/* We remove any previous connection immediately, on the assumption it's dead */
peer = peer_htable_get(&daemon->peers, id);
peer = peer_htable_get(daemon->peers, id);
if (peer)
tal_free(peer);

Expand Down Expand Up @@ -1724,7 +1724,7 @@ static void try_connect_peer(struct daemon *daemon,
struct connecting *connect;

/* Already existing? Must have crossed over, it'll know soon. */
if (peer_htable_get(&daemon->peers, id))
if (peer_htable_get(daemon->peers, id))
return;

/* If we're trying to connect it right now, that's OK. */
Expand Down Expand Up @@ -1827,7 +1827,7 @@ static void peer_discard(struct daemon *daemon, const u8 *msg)

/* We should stay in sync with lightningd, but this can happen
* under stress. */
peer = peer_htable_get(&daemon->peers, &id);
peer = peer_htable_get(daemon->peers, &id);
if (!peer)
return;
/* If it's reconnected already, it will learn soon. */
Expand All @@ -1852,7 +1852,7 @@ static void peer_final_msg(struct io_conn *conn,

/* This can happen if peer hung up on us (or wrong counter
* if it reconnected). */
peer = peer_htable_get(&daemon->peers, &id);
peer = peer_htable_get(daemon->peers, &id);
if (peer && peer->counter == counter)
multiplex_final_msg(peer, take(finalmsg));
}
Expand All @@ -1868,7 +1868,7 @@ static void dev_connect_memleak(struct daemon *daemon, const u8 *msg)

/* Now delete daemon and those which it has pointers to. */
memleak_scan_obj(memtable, daemon);
memleak_scan_htable(memtable, &daemon->peers.raw);
memleak_scan_htable(memtable, &daemon->peers->raw);

found_leak = dump_memleak(memtable, memleak_status_broken);
daemon_conn_send(daemon->master,
Expand Down Expand Up @@ -1995,7 +1995,7 @@ static struct io_plan *recv_gossip(struct io_conn *conn,
status_failed(STATUS_FAIL_GOSSIP_IO, "Unknown msg %i",
fromwire_peektype(msg));

peer = peer_htable_get(&daemon->peers, &dst);
peer = peer_htable_get(daemon->peers, &dst);
if (peer)
inject_peer_msg(peer, take(gossip_msg));

Expand All @@ -2007,7 +2007,7 @@ static struct io_plan *recv_gossip(struct io_conn *conn,
#if DEVELOPER
static void memleak_daemon_cb(struct htable *memtable, struct daemon *daemon)
{
memleak_scan_htable(memtable, &daemon->peers.raw);
memleak_scan_htable(memtable, &daemon->peers->raw);
}
#endif /* DEVELOPER */

Expand All @@ -2023,7 +2023,8 @@ int main(int argc, char *argv[])
/* Allocate and set up our simple top-level structure. */
daemon = tal(NULL, struct daemon);
daemon->connection_counter = 1;
peer_htable_init(&daemon->peers);
daemon->peers = tal(daemon, struct peer_htable);
peer_htable_init(daemon->peers);
memleak_add_helper(daemon, memleak_daemon_cb);
list_head_init(&daemon->connecting);
timers_init(&daemon->timers, time_mono());
Expand Down
2 changes: 1 addition & 1 deletion connectd/connectd.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ struct daemon {

/* Peers that we've handed to `lightningd`, which it hasn't told us
* have disconnected. */
struct peer_htable peers;
struct peer_htable *peers;

/* Peers we are trying to reach */
struct list_head connecting;
Expand Down
6 changes: 3 additions & 3 deletions connectd/multiplex.c
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ void send_custommsg(struct daemon *daemon, const u8 *msg)
master_badmsg(WIRE_CONNECTD_CUSTOMMSG_OUT, msg);

/* Races can happen: this might be gone by now. */
peer = peer_htable_get(&daemon->peers, &id);
peer = peer_htable_get(daemon->peers, &id);
if (peer)
inject_peer_msg(peer, take(custommsg));
}
Expand Down Expand Up @@ -1242,7 +1242,7 @@ void peer_connect_subd(struct daemon *daemon, const u8 *msg, int fd)
master_badmsg(WIRE_CONNECTD_PEER_CONNECT_SUBD, msg);

/* Races can happen: this might be gone by now (or reconnected!). */
peer = peer_htable_get(&daemon->peers, &id);
peer = peer_htable_get(daemon->peers, &id);
if (!peer || peer->counter != counter) {
close(fd);
return;
Expand Down Expand Up @@ -1276,7 +1276,7 @@ void send_manual_ping(struct daemon *daemon, const u8 *msg)
if (!fromwire_connectd_ping(msg, &id, &num_pong_bytes, &len))
master_badmsg(WIRE_CONNECTD_PING, msg);

peer = peer_htable_get(&daemon->peers, &id);
peer = peer_htable_get(daemon->peers, &id);
if (!peer) {
daemon_conn_send(daemon->master,
take(towire_connectd_ping_reply(NULL,
Expand Down
4 changes: 2 additions & 2 deletions connectd/onion_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void onionmsg_req(struct daemon *daemon, const u8 *msg)

/* Even though lightningd checks for valid ids, there's a race
* where it might vanish before we read this command. */
peer = peer_htable_get(&daemon->peers, &id);
peer = peer_htable_get(daemon->peers, &id);
if (peer) {
u8 *omsg = towire_onion_message(NULL, &blinding, onionmsg);
inject_peer_msg(peer, take(omsg));
Expand Down Expand Up @@ -86,7 +86,7 @@ void handle_onion_message(struct daemon *daemon,

/* FIXME: Handle short_channel_id! */
node_id_from_pubkey(&next_node_id, &next_node);
next_peer = peer_htable_get(&daemon->peers, &next_node_id);
next_peer = peer_htable_get(daemon->peers, &next_node_id);
if (!next_peer) {
status_peer_debug(&peer->id,
"onion msg: unknown next peer %s",
Expand Down

0 comments on commit 81e57dc

Please sign in to comment.