From c66e10fb7ab3916f005f46c590a89617ea6f569c Mon Sep 17 00:00:00 2001
From: iphydf <iphydf@users.noreply.github.com>
Date: Wed, 10 Jan 2024 12:57:30 +0000
Subject: [PATCH] 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.
---
 .../docker/tox-bootstrapd.sha256              |  2 +-
 toxcore/DHT.c                                 | 51 ++++++++++++-------
 toxcore/DHT.h                                 | 14 ++---
 toxcore/TCP_server.c                          |  3 ++
 4 files changed, 46 insertions(+), 24 deletions(-)

diff --git a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256
index 8bd37bd7e3..11772283fe 100644
--- a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256
+++ b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256
@@ -1 +1 @@
-a8e6d6d075090f4e6d27f59dd2e859a152948b3fac7f0b073386172339ec5d8d  /usr/local/bin/tox-bootstrapd
+7e86d4f1c4aadce01a03153f2101ac1486f6de65f824b7b0cccbbfbf832c180a  /usr/local/bin/tox-bootstrapd
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index c8c5a2dacf..d83d48c9fd 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -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;
@@ -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 {
diff --git a/toxcore/DHT.h b/toxcore/DHT.h
index 145626706d..8dedc470fe 100644
--- a/toxcore/DHT.h
+++ b/toxcore/DHT.h
@@ -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.
diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c
index a54047d778..1a3c7aff7e 100644
--- a/toxcore/TCP_server.c
+++ b/toxcore/TCP_server.c
@@ -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;