Skip to content

Commit c66e10f

Browse files
committed
refactor: Minor refactoring of get_close_nodes functions.
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.
1 parent ebc9643 commit c66e10f

File tree

4 files changed

+46
-24
lines changed

4 files changed

+46
-24
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
a8e6d6d075090f4e6d27f59dd2e859a152948b3fac7f0b073386172339ec5d8d /usr/local/bin/tox-bootstrapd
1+
7e86d4f1c4aadce01a03153f2101ac1486f6de65f824b7b0cccbbfbf832c180a /usr/local/bin/tox-bootstrapd

toxcore/DHT.c

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -781,10 +781,11 @@ bool add_to_list(
781781
* helper for `get_close_nodes()`. argument list is a monster :D
782782
*/
783783
non_null()
784-
static void get_close_nodes_inner(uint64_t cur_time, const uint8_t *public_key, Node_format *nodes_list,
785-
Family sa_family, const Client_data *client_list, uint32_t client_list_length,
786-
uint32_t *num_nodes_ptr, bool is_lan,
787-
bool want_announce)
784+
static void get_close_nodes_inner(
785+
uint64_t cur_time, const uint8_t *public_key,
786+
Node_format *nodes_list, uint32_t *num_nodes_ptr,
787+
Family sa_family, const Client_data *client_list, uint32_t client_list_length,
788+
bool is_lan, bool want_announce)
788789
{
789790
if (!net_family_is_ipv4(sa_family) && !net_family_is_ipv6(sa_family) && !net_family_is_unspec(sa_family)) {
790791
return;
@@ -851,28 +852,44 @@ static void get_close_nodes_inner(uint64_t cur_time, const uint8_t *public_key,
851852
* want_announce: return only nodes which implement the dht announcements protocol.
852853
*/
853854
non_null()
854-
static int get_somewhat_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list,
855-
Family sa_family, bool is_lan, bool want_announce)
855+
static int get_somewhat_close_nodes(
856+
uint64_t cur_time, const uint8_t *public_key, Node_format *nodes_list,
857+
Family sa_family, const Client_data *close_clientlist,
858+
const DHT_Friend *friends_list, uint16_t friends_list_size,
859+
bool is_lan, bool want_announce)
856860
{
861+
memset(nodes_list, 0, MAX_SENT_NODES * sizeof(Node_format));
862+
857863
uint32_t num_nodes = 0;
858-
get_close_nodes_inner(dht->cur_time, public_key, nodes_list, sa_family,
859-
dht->close_clientlist, LCLIENT_LIST, &num_nodes, is_lan, want_announce);
864+
get_close_nodes_inner(
865+
cur_time, public_key,
866+
nodes_list, &num_nodes,
867+
sa_family, close_clientlist, LCLIENT_LIST,
868+
is_lan, want_announce);
860869

861-
for (uint32_t i = 0; i < dht->num_friends; ++i) {
862-
get_close_nodes_inner(dht->cur_time, public_key, nodes_list, sa_family,
863-
dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS,
864-
&num_nodes, is_lan, want_announce);
870+
for (uint16_t i = 0; i < friends_list_size; ++i) {
871+
const DHT_Friend *dht_friend = &friends_list[i];
872+
873+
get_close_nodes_inner(
874+
cur_time, public_key,
875+
nodes_list, &num_nodes,
876+
sa_family, dht_friend->client_list, MAX_FRIEND_CLIENTS,
877+
is_lan, want_announce);
865878
}
866879

867880
return num_nodes;
868881
}
869882

870-
int get_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list, Family sa_family,
871-
bool is_lan, bool want_announce)
883+
int get_close_nodes(
884+
const DHT *dht, const uint8_t *public_key,
885+
Node_format *nodes_list, Family sa_family,
886+
bool is_lan, bool want_announce)
872887
{
873-
memset(nodes_list, 0, MAX_SENT_NODES * sizeof(Node_format));
874-
return get_somewhat_close_nodes(dht, public_key, nodes_list, sa_family,
875-
is_lan, want_announce);
888+
return get_somewhat_close_nodes(
889+
dht->cur_time, public_key, nodes_list,
890+
sa_family, dht->close_clientlist,
891+
dht->friends_list, dht->num_friends,
892+
is_lan, want_announce);
876893
}
877894

878895
typedef struct DHT_Cmp_Data {

toxcore/DHT.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,18 +384,20 @@ void set_announce_node(DHT *dht, const uint8_t *public_key);
384384
#endif
385385

386386
/**
387-
* Get the (maximum MAX_SENT_NODES) closest nodes to public_key we know
387+
* @brief Get the (maximum MAX_SENT_NODES) closest nodes to public_key we know
388388
* and put them in nodes_list (must be MAX_SENT_NODES big).
389389
*
390-
* sa_family = family (IPv4 or IPv6) (0 if we don't care)?
391-
* is_LAN = return some LAN ips (true or false)
392-
* want_announce: return only nodes which implement the dht announcements protocol.
390+
* @param sa_family family (IPv4 or IPv6) (0 if we don't care)?
391+
* @param is_lan return some LAN ips (true or false).
392+
* @param want_announce return only nodes which implement the dht announcements protocol.
393393
*
394394
* @return the number of nodes returned.
395395
*/
396396
non_null()
397-
int get_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list, Family sa_family,
398-
bool is_lan, bool want_announce);
397+
int get_close_nodes(
398+
const DHT *dht, const uint8_t *public_key,
399+
Node_format *nodes_list, Family sa_family,
400+
bool is_lan, bool want_announce);
399401

400402

401403
/** @brief Put up to max_num nodes in nodes from the random friends.

toxcore/TCP_server.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ struct TCP_Server {
9292
BS_List accepted_key_list;
9393
};
9494

95+
static_assert(sizeof(TCP_Server) < 7 * 1024 * 1024,
96+
"TCP_Server struct should not grow more; it's already 6MB");
97+
9598
const uint8_t *tcp_server_public_key(const TCP_Server *tcp_server)
9699
{
97100
return tcp_server->public_key;

0 commit comments

Comments
 (0)