Skip to content

Commit

Permalink
refactor: Minor refactoring of get_close_nodes functions.
Browse files Browse the repository at this point in the history
Avoiding passing down the entire DHT struct pointer to the inner
functions makes it possible in the future to write unit tests without
having to construct a full DHT object.
  • Loading branch information
iphydf committed Jan 10, 2024
1 parent ebc9643 commit c66e10f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 24 deletions.
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/docker/tox-bootstrapd.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
a8e6d6d075090f4e6d27f59dd2e859a152948b3fac7f0b073386172339ec5d8d /usr/local/bin/tox-bootstrapd
7e86d4f1c4aadce01a03153f2101ac1486f6de65f824b7b0cccbbfbf832c180a /usr/local/bin/tox-bootstrapd
51 changes: 34 additions & 17 deletions toxcore/DHT.c
Original file line number Diff line number Diff line change
Expand Up @@ -781,10 +781,11 @@ bool add_to_list(
* helper for `get_close_nodes()`. argument list is a monster :D
*/
non_null()
static void get_close_nodes_inner(uint64_t cur_time, const uint8_t *public_key, Node_format *nodes_list,
Family sa_family, const Client_data *client_list, uint32_t client_list_length,
uint32_t *num_nodes_ptr, bool is_lan,
bool want_announce)
static void get_close_nodes_inner(
uint64_t cur_time, const uint8_t *public_key,
Node_format *nodes_list, uint32_t *num_nodes_ptr,
Family sa_family, const Client_data *client_list, uint32_t client_list_length,
bool is_lan, bool want_announce)
{
if (!net_family_is_ipv4(sa_family) && !net_family_is_ipv6(sa_family) && !net_family_is_unspec(sa_family)) {
return;
Expand Down Expand Up @@ -851,28 +852,44 @@ static void get_close_nodes_inner(uint64_t cur_time, const uint8_t *public_key,
* want_announce: return only nodes which implement the dht announcements protocol.
*/
non_null()
static int get_somewhat_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list,
Family sa_family, bool is_lan, bool want_announce)
static int get_somewhat_close_nodes(
uint64_t cur_time, const uint8_t *public_key, Node_format *nodes_list,
Family sa_family, const Client_data *close_clientlist,
const DHT_Friend *friends_list, uint16_t friends_list_size,
bool is_lan, bool want_announce)
{
memset(nodes_list, 0, MAX_SENT_NODES * sizeof(Node_format));

uint32_t num_nodes = 0;
get_close_nodes_inner(dht->cur_time, public_key, nodes_list, sa_family,
dht->close_clientlist, LCLIENT_LIST, &num_nodes, is_lan, want_announce);
get_close_nodes_inner(
cur_time, public_key,
nodes_list, &num_nodes,
sa_family, close_clientlist, LCLIENT_LIST,
is_lan, want_announce);

for (uint32_t i = 0; i < dht->num_friends; ++i) {
get_close_nodes_inner(dht->cur_time, public_key, nodes_list, sa_family,
dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS,
&num_nodes, is_lan, want_announce);
for (uint16_t i = 0; i < friends_list_size; ++i) {
const DHT_Friend *dht_friend = &friends_list[i];

get_close_nodes_inner(
cur_time, public_key,
nodes_list, &num_nodes,
sa_family, dht_friend->client_list, MAX_FRIEND_CLIENTS,
is_lan, want_announce);
}

return num_nodes;
}

int get_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list, Family sa_family,
bool is_lan, bool want_announce)
int get_close_nodes(
const DHT *dht, const uint8_t *public_key,
Node_format *nodes_list, Family sa_family,
bool is_lan, bool want_announce)
{
memset(nodes_list, 0, MAX_SENT_NODES * sizeof(Node_format));
return get_somewhat_close_nodes(dht, public_key, nodes_list, sa_family,
is_lan, want_announce);
return get_somewhat_close_nodes(
dht->cur_time, public_key, nodes_list,
sa_family, dht->close_clientlist,
dht->friends_list, dht->num_friends,
is_lan, want_announce);
}

typedef struct DHT_Cmp_Data {
Expand Down
14 changes: 8 additions & 6 deletions toxcore/DHT.h
Original file line number Diff line number Diff line change
Expand Up @@ -384,18 +384,20 @@ void set_announce_node(DHT *dht, const uint8_t *public_key);
#endif

/**
* Get the (maximum MAX_SENT_NODES) closest nodes to public_key we know
* @brief Get the (maximum MAX_SENT_NODES) closest nodes to public_key we know
* and put them in nodes_list (must be MAX_SENT_NODES big).
*
* sa_family = family (IPv4 or IPv6) (0 if we don't care)?
* is_LAN = return some LAN ips (true or false)
* want_announce: return only nodes which implement the dht announcements protocol.
* @param sa_family family (IPv4 or IPv6) (0 if we don't care)?
* @param is_lan return some LAN ips (true or false).
* @param want_announce return only nodes which implement the dht announcements protocol.
*
* @return the number of nodes returned.
*/
non_null()
int get_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list, Family sa_family,
bool is_lan, bool want_announce);
int get_close_nodes(
const DHT *dht, const uint8_t *public_key,
Node_format *nodes_list, Family sa_family,
bool is_lan, bool want_announce);


/** @brief Put up to max_num nodes in nodes from the random friends.
Expand Down
3 changes: 3 additions & 0 deletions toxcore/TCP_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ struct TCP_Server {
BS_List accepted_key_list;
};

static_assert(sizeof(TCP_Server) < 7 * 1024 * 1024,
"TCP_Server struct should not grow more; it's already 6MB");

const uint8_t *tcp_server_public_key(const TCP_Server *tcp_server)
{
return tcp_server->public_key;
Expand Down

0 comments on commit c66e10f

Please sign in to comment.