refactor: TCP connection netprof objects are now owned by Messenger

This allows us to have a single netprof object for all TCP client
connections, which makes it easier to query and keep track of
TCP network data for groupchats. Previously we were not properly
querying groupchat TCP network data via the netprof API.
This commit is contained in:
jfreegman
2025-03-18 21:32:41 -04:00
parent 59d3f6674f
commit 76bc4c496d
15 changed files with 96 additions and 65 deletions
+15 -4
View File
@@ -8,6 +8,7 @@
#include "../toxcore/TCP_server.h"
#include "../toxcore/crypto_core.h"
#include "../toxcore/mono_time.h"
#include "../toxcore/net_profile.h"
#include "../toxcore/network.h"
#include "auto_test_support.h"
@@ -737,6 +738,9 @@ static void test_tcp_connection(void)
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
Logger *logger = logger_new(mem);
Net_Profile *tcp_np = netprof_new(logger, mem);
ck_assert(tcp_np != nullptr);
tcp_data_callback_called = 0;
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
@@ -747,12 +751,12 @@ static void test_tcp_connection(void)
TCP_Proxy_Info proxy_info;
proxy_info.proxy_type = TCP_PROXY_NONE;
crypto_new_keypair(rng, self_public_key, self_secret_key);
TCP_Connections *tc_1 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info);
TCP_Connections *tc_1 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info, tcp_np);
ck_assert_msg(tc_1 != nullptr, "Failed to create TCP connections");
ck_assert_msg(pk_equal(tcp_connections_public_key(tc_1), self_public_key), "Wrong public key");
crypto_new_keypair(rng, self_public_key, self_secret_key);
TCP_Connections *tc_2 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info);
TCP_Connections *tc_2 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info, tcp_np);
ck_assert_msg(tc_2 != nullptr, "Failed to create TCP connections");
ck_assert_msg(pk_equal(tcp_connections_public_key(tc_2), self_public_key), "Wrong public key");
@@ -815,6 +819,8 @@ static void test_tcp_connection(void)
kill_tcp_connections(tc_1);
kill_tcp_connections(tc_2);
netprof_kill(mem, tcp_np);
logger_kill(logger);
mono_time_free(mem, mono_time);
}
@@ -852,6 +858,9 @@ static void test_tcp_connection2(void)
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
Logger *logger = logger_new(mem);
Net_Profile *tcp_np = netprof_new(logger, mem);
ck_assert(tcp_np != nullptr);
tcp_oobdata_callback_called = 0;
tcp_data_callback_called = 0;
@@ -864,12 +873,12 @@ static void test_tcp_connection2(void)
TCP_Proxy_Info proxy_info;
proxy_info.proxy_type = TCP_PROXY_NONE;
crypto_new_keypair(rng, self_public_key, self_secret_key);
TCP_Connections *tc_1 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info);
TCP_Connections *tc_1 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info, tcp_np);
ck_assert_msg(tc_1 != nullptr, "Failed to create TCP connections");
ck_assert_msg(pk_equal(tcp_connections_public_key(tc_1), self_public_key), "Wrong public key");
crypto_new_keypair(rng, self_public_key, self_secret_key);
TCP_Connections *tc_2 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info);
TCP_Connections *tc_2 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info, tcp_np);
ck_assert_msg(tc_2 != nullptr, "Failed to create TCP connections");
ck_assert_msg(pk_equal(tcp_connections_public_key(tc_2), self_public_key), "Wrong public key");
@@ -921,6 +930,8 @@ static void test_tcp_connection2(void)
ck_assert_msg(tcp_data_callback_called, "could not recv packet.");
ck_assert_msg(kill_tcp_connection_to(tc_1, 0) == 0, "could not kill connection to\n");
netprof_kill(mem, tcp_np);
kill_tcp_server(tcp_s);
kill_tcp_connections(tc_1);
kill_tcp_connections(tc_2);
+6 -1
View File
@@ -96,6 +96,7 @@ typedef struct Forwarding_Subtox {
Logger *log;
Mono_Time *mono_time;
Networking_Core *net;
Net_Profile *tcp_np;
DHT *dht;
Net_Crypto *c;
Forwarding *forwarding;
@@ -126,8 +127,11 @@ static Forwarding_Subtox *new_forwarding_subtox(const Memory *mem, bool no_udp,
subtox->dht = new_dht(subtox->log, mem, rng, ns, subtox->mono_time, subtox->net, true, true);
subtox->tcp_np = netprof_new(subtox->log, mem);
ck_assert(subtox->tcp_np != nullptr);
const TCP_Proxy_Info inf = {{{{0}}}};
subtox->c = new_net_crypto(subtox->log, mem, rng, ns, subtox->mono_time, subtox->dht, &inf);
subtox->c = new_net_crypto(subtox->log, mem, rng, ns, subtox->mono_time, subtox->dht, &inf, subtox->tcp_np);
subtox->forwarding = new_forwarding(subtox->log, mem, rng, subtox->mono_time, subtox->dht);
ck_assert(subtox->forwarding != nullptr);
@@ -143,6 +147,7 @@ static void kill_forwarding_subtox(const Memory *mem, Forwarding_Subtox *subtox)
kill_announcements(subtox->announce);
kill_forwarding(subtox->forwarding);
kill_net_crypto(subtox->c);
netprof_kill(mem, subtox->tcp_np);
kill_dht(subtox->dht);
kill_networking(subtox->net);
mono_time_free(mem, subtox->mono_time);
+17 -1
View File
@@ -396,6 +396,7 @@ static void test_basic(void)
typedef struct {
Logger *log;
Mono_Time *mono_time;
Net_Profile *tcp_np;
Onion *onion;
Onion_Announce *onion_a;
Onion_Client *onion_c;
@@ -471,10 +472,24 @@ static Onions *new_onions(const Memory *mem, const Random *rng, uint16_t port, u
return nullptr;
}
on->tcp_np = netprof_new(on->log, mem);
if (!on->tcp_np) {
kill_onion_announce(on->onion_a);
kill_onion(on->onion);
kill_dht(dht);
kill_networking(net);
mono_time_free(mem, on->mono_time);
logger_kill(on->log);
free(on);
return nullptr;
}
TCP_Proxy_Info inf = {{{{0}}}};
on->onion_c = new_onion_client(on->log, mem, rng, on->mono_time, new_net_crypto(on->log, mem, rng, ns, on->mono_time, dht, &inf));
on->onion_c = new_onion_client(on->log, mem, rng, on->mono_time, new_net_crypto(on->log, mem, rng, ns, on->mono_time, dht, &inf, on->tcp_np));
if (!on->onion_c) {
netprof_kill(mem, on->tcp_np);
kill_onion_announce(on->onion_a);
kill_onion(on->onion);
kill_dht(dht);
@@ -506,6 +521,7 @@ static void kill_onions(const Memory *mem, Onions *on)
kill_onion_announce(on->onion_a);
kill_onion(on->onion);
kill_net_crypto(c);
netprof_kill(mem, on->tcp_np);
kill_dht(dht);
kill_networking(net);
mono_time_free(mem, on->mono_time);
+1
View File
@@ -1058,6 +1058,7 @@ cc_library(
":mem",
":mono_time",
":net_crypto",
":net_profile",
":network",
":onion",
":onion_announce",
+20 -1
View File
@@ -34,6 +34,7 @@
#include "mem.h"
#include "mono_time.h"
#include "net_crypto.h"
#include "net_profile.h"
#include "network.h"
#include "onion.h"
#include "onion_announce.h"
@@ -3531,11 +3532,24 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random *
return nullptr;
}
m->net_crypto = new_net_crypto(m->log, m->mem, m->rng, m->ns, m->mono_time, m->dht, &options->proxy_info);
m->tcp_np = netprof_new(m->log, mem);
if (m->tcp_np == nullptr) {
LOGGER_WARNING(m->log, "TCP netprof initialisation failed");
kill_dht(m->dht);
kill_networking(m->net);
friendreq_kill(m->fr);
logger_kill(m->log);
mem_delete(mem, m);
return nullptr;
}
m->net_crypto = new_net_crypto(m->log, m->mem, m->rng, m->ns, m->mono_time, m->dht, &options->proxy_info, m->tcp_np);
if (m->net_crypto == nullptr) {
LOGGER_WARNING(m->log, "net_crypto initialisation failed");
netprof_kill(mem, m->tcp_np);
kill_dht(m->dht);
kill_networking(m->net);
friendreq_kill(m->fr);
@@ -3550,6 +3564,7 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random *
LOGGER_WARNING(m->log, "DHT group chats initialisation failed");
kill_net_crypto(m->net_crypto);
netprof_kill(mem, m->tcp_np);
kill_dht(m->dht);
kill_networking(m->net);
friendreq_kill(m->fr);
@@ -3589,6 +3604,7 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random *
kill_announcements(m->announce);
kill_forwarding(m->forwarding);
kill_net_crypto(m->net_crypto);
netprof_kill(mem, m->tcp_np);
kill_dht(m->dht);
kill_networking(m->net);
friendreq_kill(m->fr);
@@ -3612,6 +3628,7 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random *
kill_announcements(m->announce);
kill_forwarding(m->forwarding);
kill_net_crypto(m->net_crypto);
netprof_kill(mem, m->tcp_np);
kill_dht(m->dht);
kill_networking(m->net);
friendreq_kill(m->fr);
@@ -3637,6 +3654,7 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random *
kill_announcements(m->announce);
kill_forwarding(m->forwarding);
kill_net_crypto(m->net_crypto);
netprof_kill(mem, m->tcp_np);
kill_dht(m->dht);
kill_networking(m->net);
friendreq_kill(m->fr);
@@ -3692,6 +3710,7 @@ void kill_messenger(Messenger *m)
kill_announcements(m->announce);
kill_forwarding(m->forwarding);
kill_net_crypto(m->net_crypto);
netprof_kill(m->mem, m->tcp_np);
kill_dht(m->dht);
kill_networking(m->net);
+2
View File
@@ -25,6 +25,7 @@
#include "mem.h"
#include "mono_time.h"
#include "net_crypto.h"
#include "net_profile.h"
#include "network.h"
#include "onion.h"
#include "onion_announce.h"
@@ -248,6 +249,7 @@ struct Messenger {
Networking_Core *net;
Net_Crypto *net_crypto;
Net_Profile *tcp_np;
DHT *dht;
Forwarding *forwarding;
+2 -18
View File
@@ -1596,7 +1596,7 @@ int set_tcp_onion_status(TCP_Connections *tcp_c, bool status)
* Returns NULL on failure.
*/
TCP_Connections *new_tcp_connections(const Logger *logger, const Memory *mem, const Random *rng, const Network *ns,
Mono_Time *mono_time, const uint8_t *secret_key, const TCP_Proxy_Info *proxy_info)
Mono_Time *mono_time, const uint8_t *secret_key, const TCP_Proxy_Info *proxy_info, Net_Profile *tcp_np)
{
assert(logger != nullptr);
assert(mem != nullptr);
@@ -1614,14 +1614,7 @@ TCP_Connections *new_tcp_connections(const Logger *logger, const Memory *mem, co
return nullptr;
}
Net_Profile *np = netprof_new(logger, mem);
if (np == nullptr) {
mem_delete(mem, temp);
return nullptr;
}
temp->net_profile = np;
temp->net_profile = tcp_np;
temp->logger = logger;
temp->mem = mem;
temp->rng = rng;
@@ -1736,17 +1729,8 @@ void kill_tcp_connections(TCP_Connections *tcp_c)
crypto_memzero(tcp_c->self_secret_key, sizeof(tcp_c->self_secret_key));
netprof_kill(tcp_c->mem, tcp_c->net_profile);
mem_delete(tcp_c->mem, tcp_c->tcp_connections);
mem_delete(tcp_c->mem, tcp_c->connections);
mem_delete(tcp_c->mem, tcp_c);
}
const Net_Profile *tcp_connection_get_client_net_profile(const TCP_Connections *tcp_c)
{
if (tcp_c == nullptr) {
return nullptr;
}
return tcp_c->net_profile;
}
+1 -8
View File
@@ -307,7 +307,7 @@ uint32_t tcp_copy_connected_relays_index(const TCP_Connections *tcp_c, Node_form
*/
non_null()
TCP_Connections *new_tcp_connections(const Logger *logger, const Memory *mem, const Random *rng, const Network *ns,
Mono_Time *mono_time, const uint8_t *secret_key, const TCP_Proxy_Info *proxy_info);
Mono_Time *mono_time, const uint8_t *secret_key, const TCP_Proxy_Info *proxy_info, Net_Profile *tcp_np);
non_null()
int kill_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connections_number);
@@ -318,11 +318,4 @@ void do_tcp_connections(const Logger *logger, TCP_Connections *tcp_c, void *user
nullable(1)
void kill_tcp_connections(TCP_Connections *tcp_c);
/** @brief a pointer to the tcp client net profile associated with tcp_c.
*
* @retval null if tcp_c is null.
*/
non_null()
const Net_Profile *tcp_connection_get_client_net_profile(const TCP_Connections *tcp_c);
#endif /* C_TOXCORE_TOXCORE_TCP_CONNECTION_H */
+2 -1
View File
@@ -7418,7 +7418,7 @@ static bool init_gc_tcp_connection(const GC_Session *c, GC_Chat *chat)
const Messenger *m = c->messenger;
chat->tcp_conn = new_tcp_connections(chat->log, chat->mem, chat->rng, m->ns, chat->mono_time, chat->self_secret_key.enc,
&m->options.proxy_info);
&m->options.proxy_info, c->tcp_np);
if (chat->tcp_conn == nullptr) {
return false;
@@ -8274,6 +8274,7 @@ GC_Session *new_dht_groupchats(Messenger *m)
c->messenger = m;
c->announces_list = m->group_announce;
c->tcp_np = m->tcp_np;
networking_registerhandler(m->net, NET_PACKET_GC_LOSSLESS, &handle_gc_udp_packet, m);
networking_registerhandler(m->net, NET_PACKET_GC_LOSSY, &handle_gc_udp_packet, m);
+2
View File
@@ -20,6 +20,7 @@
#include "logger.h"
#include "mem.h"
#include "mono_time.h"
#include "net_profile.h"
#include "network.h"
#define MAX_GC_PART_MESSAGE_SIZE 128
@@ -377,6 +378,7 @@ typedef void gc_rejected_cb(const Messenger *m, uint32_t group_number, unsigned
typedef struct GC_Session {
Messenger *messenger;
GC_Chat *chats;
Net_Profile *tcp_np;
struct GC_Announces_List *announces_list;
uint32_t chats_index;
+2 -17
View File
@@ -2988,7 +2988,7 @@ void load_secret_key(Net_Crypto *c, const uint8_t *sk)
* Sets all the global connection variables to their default values.
*/
Net_Crypto *new_net_crypto(const Logger *log, const Memory *mem, const Random *rng, const Network *ns,
Mono_Time *mono_time, DHT *dht, const TCP_Proxy_Info *proxy_info)
Mono_Time *mono_time, DHT *dht, const TCP_Proxy_Info *proxy_info, Net_Profile *tcp_np)
{
if (dht == nullptr) {
return nullptr;
@@ -3006,7 +3006,7 @@ Net_Crypto *new_net_crypto(const Logger *log, const Memory *mem, const Random *r
temp->mono_time = mono_time;
temp->ns = ns;
temp->tcp_c = new_tcp_connections(log, mem, rng, ns, mono_time, dht_get_self_secret_key(dht), proxy_info);
temp->tcp_c = new_tcp_connections(log, mem, rng, ns, mono_time, dht_get_self_secret_key(dht), proxy_info, tcp_np);
if (temp->tcp_c == nullptr) {
mem_delete(mem, temp);
@@ -3098,18 +3098,3 @@ void kill_net_crypto(Net_Crypto *c)
crypto_memzero(c, sizeof(Net_Crypto));
mem_delete(mem, c);
}
const Net_Profile *nc_get_tcp_client_net_profile(const Net_Crypto *c)
{
if (c == nullptr) {
return nullptr;
}
const TCP_Connections *tcp_c = nc_get_tcp_c(c);
if (tcp_c == nullptr) {
return nullptr;
}
return tcp_connection_get_client_net_profile(tcp_c);
}
+1 -8
View File
@@ -406,7 +406,7 @@ void load_secret_key(Net_Crypto *c, const uint8_t *sk);
*/
non_null()
Net_Crypto *new_net_crypto(const Logger *log, const Memory *mem, const Random *rng, const Network *ns,
Mono_Time *mono_time, DHT *dht, const TCP_Proxy_Info *proxy_info);
Mono_Time *mono_time, DHT *dht, const TCP_Proxy_Info *proxy_info, Net_Profile *tcp_np);
/** return the optimal interval in ms for running do_net_crypto. */
non_null()
@@ -419,13 +419,6 @@ void do_net_crypto(Net_Crypto *c, void *userdata);
nullable(1)
void kill_net_crypto(Net_Crypto *c);
/**
* Returns a pointer to the net profile object for the TCP client associated with `c`.
* Returns null if `c` is null or the TCP_Connections associated with `c` is null.
*/
non_null()
const Net_Profile *nc_get_tcp_client_net_profile(const Net_Crypto *c);
#ifdef __cplusplus
} /* extern "C" */
#endif
+13 -2
View File
@@ -10,6 +10,7 @@
#include "../testing/fuzzing/fuzz_tox.hh"
#include "DHT.h"
#include "TCP_client.h"
#include "net_profile.h"
#include "network.h"
namespace {
@@ -65,12 +66,20 @@ void TestNetCrypto(Fuzz_Data &input)
return;
}
Net_Profile *tcp_np = netprof_new(logger.get(), sys.mem.get());
if (tcp_np == nullptr) {
return;
}
const TCP_Proxy_Info proxy_info = {0};
const Ptr<Net_Crypto> net_crypto(new_net_crypto(logger.get(), sys.mem.get(), sys.rng.get(),
sys.ns.get(), mono_time.get(), dht.get(), &proxy_info),
const Ptr<Net_Crypto> net_crypto(
new_net_crypto(logger.get(), sys.mem.get(), sys.rng.get(), sys.ns.get(), mono_time.get(),
dht.get(), &proxy_info, tcp_np),
kill_net_crypto);
if (net_crypto == nullptr) {
netprof_kill(sys.mem.get(), tcp_np);
return;
}
@@ -81,6 +90,8 @@ void TestNetCrypto(Fuzz_Data &input)
// "Sleep"
sys.clock += System::BOOTSTRAP_ITERATION_INTERVAL;
}
netprof_kill(sys.mem.get(), tcp_np);
}
} // namespace
+8
View File
@@ -15,6 +15,10 @@
#include "logger.h"
#include "mem.h"
#ifdef __cplusplus
extern "C" {
#endif
/* The max number of packet ID's (must fit inside one byte) */
#define NET_PROF_MAX_PACKET_IDS 256
@@ -70,4 +74,8 @@ Net_Profile *netprof_new(const Logger *log, const Memory *mem);
non_null(1) nullable(2)
void netprof_kill(const Memory *mem, Net_Profile *net_profile);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* C_TOXCORE_TOXCORE_NET_PROFILE_H */
+4 -4
View File
@@ -237,7 +237,7 @@ uint64_t tox_netprof_get_packet_id_count(const Tox *tox, Tox_Netprof_Packet_Type
tox_lock(tox);
const Net_Profile *tcp_c_profile = nc_get_tcp_client_net_profile(tox->m->net_crypto);
const Net_Profile *tcp_c_profile = tox->m->tcp_np;
const Net_Profile *tcp_s_profile = tcp_server_get_net_profile(tox->m->tcp_server);
const Packet_Direction dir = (Packet_Direction) direction;
@@ -286,7 +286,7 @@ uint64_t tox_netprof_get_packet_total_count(const Tox *tox, Tox_Netprof_Packet_T
tox_lock(tox);
const Net_Profile *tcp_c_profile = nc_get_tcp_client_net_profile(tox->m->net_crypto);
const Net_Profile *tcp_c_profile = tox->m->tcp_np;
const Net_Profile *tcp_s_profile = tcp_server_get_net_profile(tox->m->tcp_server);
const Packet_Direction dir = (Packet_Direction) direction;
@@ -335,7 +335,7 @@ uint64_t tox_netprof_get_packet_id_bytes(const Tox *tox, Tox_Netprof_Packet_Type
tox_lock(tox);
const Net_Profile *tcp_c_profile = nc_get_tcp_client_net_profile(tox->m->net_crypto);
const Net_Profile *tcp_c_profile = tox->m->tcp_np;
const Net_Profile *tcp_s_profile = tcp_server_get_net_profile(tox->m->tcp_server);
const Packet_Direction dir = (Packet_Direction) direction;
@@ -384,7 +384,7 @@ uint64_t tox_netprof_get_packet_total_bytes(const Tox *tox, Tox_Netprof_Packet_T
tox_lock(tox);
const Net_Profile *tcp_c_profile = nc_get_tcp_client_net_profile(tox->m->net_crypto);
const Net_Profile *tcp_c_profile = tox->m->tcp_np;
const Net_Profile *tcp_s_profile = tcp_server_get_net_profile(tox->m->tcp_server);
const Packet_Direction dir = (Packet_Direction) direction;