Skip to content

Commit 625dae3

Browse files
committed
refactor: TCP connection netprof objects are now owned by net_crypto
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.
1 parent 59d3f66 commit 625dae3

File tree

7 files changed

+35
-41
lines changed

7 files changed

+35
-41
lines changed

auto_tests/TCP_test.c

+14-4
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,9 @@ static void test_tcp_connection(void)
737737
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
738738
Logger *logger = logger_new(mem);
739739

740+
Net_Profile *tcp_np = netprof_new(logger, mem);
741+
ck_assert(tcp_np != nullptr);
742+
740743
tcp_data_callback_called = 0;
741744
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
742745
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
@@ -747,12 +750,12 @@ static void test_tcp_connection(void)
747750
TCP_Proxy_Info proxy_info;
748751
proxy_info.proxy_type = TCP_PROXY_NONE;
749752
crypto_new_keypair(rng, self_public_key, self_secret_key);
750-
TCP_Connections *tc_1 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info);
753+
TCP_Connections *tc_1 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info, tcp_np);
751754
ck_assert_msg(tc_1 != nullptr, "Failed to create TCP connections");
752755
ck_assert_msg(pk_equal(tcp_connections_public_key(tc_1), self_public_key), "Wrong public key");
753756

754757
crypto_new_keypair(rng, self_public_key, self_secret_key);
755-
TCP_Connections *tc_2 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info);
758+
TCP_Connections *tc_2 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info, tcp_np);
756759
ck_assert_msg(tc_2 != nullptr, "Failed to create TCP connections");
757760
ck_assert_msg(pk_equal(tcp_connections_public_key(tc_2), self_public_key), "Wrong public key");
758761

@@ -811,6 +814,8 @@ static void test_tcp_connection(void)
811814
ck_assert_msg(send_packet_tcp_connection(tc_1, 0, (const uint8_t *)"Gentoo", 6) == -1, "could send packet.");
812815
ck_assert_msg(kill_tcp_connection_to(tc_2, 0) == 0, "could not kill connection to\n");
813816

817+
netprof_kill(tcp_np);
818+
814819
kill_tcp_server(tcp_s);
815820
kill_tcp_connections(tc_1);
816821
kill_tcp_connections(tc_2);
@@ -852,6 +857,9 @@ static void test_tcp_connection2(void)
852857
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
853858
Logger *logger = logger_new(mem);
854859

860+
Net_Profile *tcp_np = netprof_new(logger, mem);
861+
ck_assert(tcp_np != nullptr);
862+
855863
tcp_oobdata_callback_called = 0;
856864
tcp_data_callback_called = 0;
857865

@@ -864,12 +872,12 @@ static void test_tcp_connection2(void)
864872
TCP_Proxy_Info proxy_info;
865873
proxy_info.proxy_type = TCP_PROXY_NONE;
866874
crypto_new_keypair(rng, self_public_key, self_secret_key);
867-
TCP_Connections *tc_1 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info);
875+
TCP_Connections *tc_1 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info, tcp_np);
868876
ck_assert_msg(tc_1 != nullptr, "Failed to create TCP connections");
869877
ck_assert_msg(pk_equal(tcp_connections_public_key(tc_1), self_public_key), "Wrong public key");
870878

871879
crypto_new_keypair(rng, self_public_key, self_secret_key);
872-
TCP_Connections *tc_2 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info);
880+
TCP_Connections *tc_2 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info, tcp_np);
873881
ck_assert_msg(tc_2 != nullptr, "Failed to create TCP connections");
874882
ck_assert_msg(pk_equal(tcp_connections_public_key(tc_2), self_public_key), "Wrong public key");
875883

@@ -921,6 +929,8 @@ static void test_tcp_connection2(void)
921929
ck_assert_msg(tcp_data_callback_called, "could not recv packet.");
922930
ck_assert_msg(kill_tcp_connection_to(tc_1, 0) == 0, "could not kill connection to\n");
923931

932+
netprof_kill(tcp_np);
933+
924934
kill_tcp_server(tcp_s);
925935
kill_tcp_connections(tc_1);
926936
kill_tcp_connections(tc_2);

toxcore/TCP_connection.c

+2-18
Original file line numberDiff line numberDiff line change
@@ -1596,7 +1596,7 @@ int set_tcp_onion_status(TCP_Connections *tcp_c, bool status)
15961596
* Returns NULL on failure.
15971597
*/
15981598
TCP_Connections *new_tcp_connections(const Logger *logger, const Memory *mem, const Random *rng, const Network *ns,
1599-
Mono_Time *mono_time, const uint8_t *secret_key, const TCP_Proxy_Info *proxy_info)
1599+
Mono_Time *mono_time, const uint8_t *secret_key, const TCP_Proxy_Info *proxy_info, Net_Profile *tcp_np)
16001600
{
16011601
assert(logger != nullptr);
16021602
assert(mem != nullptr);
@@ -1614,14 +1614,7 @@ TCP_Connections *new_tcp_connections(const Logger *logger, const Memory *mem, co
16141614
return nullptr;
16151615
}
16161616

1617-
Net_Profile *np = netprof_new(logger, mem);
1618-
1619-
if (np == nullptr) {
1620-
mem_delete(mem, temp);
1621-
return nullptr;
1622-
}
1623-
1624-
temp->net_profile = np;
1617+
temp->net_profile = tcp_np;
16251618
temp->logger = logger;
16261619
temp->mem = mem;
16271620
temp->rng = rng;
@@ -1736,17 +1729,8 @@ void kill_tcp_connections(TCP_Connections *tcp_c)
17361729

17371730
crypto_memzero(tcp_c->self_secret_key, sizeof(tcp_c->self_secret_key));
17381731

1739-
netprof_kill(tcp_c->mem, tcp_c->net_profile);
17401732
mem_delete(tcp_c->mem, tcp_c->tcp_connections);
17411733
mem_delete(tcp_c->mem, tcp_c->connections);
17421734
mem_delete(tcp_c->mem, tcp_c);
17431735
}
17441736

1745-
const Net_Profile *tcp_connection_get_client_net_profile(const TCP_Connections *tcp_c)
1746-
{
1747-
if (tcp_c == nullptr) {
1748-
return nullptr;
1749-
}
1750-
1751-
return tcp_c->net_profile;
1752-
}

toxcore/TCP_connection.h

+1-8
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ uint32_t tcp_copy_connected_relays_index(const TCP_Connections *tcp_c, Node_form
307307
*/
308308
non_null()
309309
TCP_Connections *new_tcp_connections(const Logger *logger, const Memory *mem, const Random *rng, const Network *ns,
310-
Mono_Time *mono_time, const uint8_t *secret_key, const TCP_Proxy_Info *proxy_info);
310+
Mono_Time *mono_time, const uint8_t *secret_key, const TCP_Proxy_Info *proxy_info, Net_Profile *tcp_np);
311311

312312
non_null()
313313
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
318318
nullable(1)
319319
void kill_tcp_connections(TCP_Connections *tcp_c);
320320

321-
/** @brief a pointer to the tcp client net profile associated with tcp_c.
322-
*
323-
* @retval null if tcp_c is null.
324-
*/
325-
non_null()
326-
const Net_Profile *tcp_connection_get_client_net_profile(const TCP_Connections *tcp_c);
327-
328321
#endif /* C_TOXCORE_TOXCORE_TCP_CONNECTION_H */

toxcore/group_chats.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -7418,7 +7418,7 @@ static bool init_gc_tcp_connection(const GC_Session *c, GC_Chat *chat)
74187418
const Messenger *m = c->messenger;
74197419

74207420
chat->tcp_conn = new_tcp_connections(chat->log, chat->mem, chat->rng, m->ns, chat->mono_time, chat->self_secret_key.enc,
7421-
&m->options.proxy_info);
7421+
&m->options.proxy_info, c->tcp_np);
74227422

74237423
if (chat->tcp_conn == nullptr) {
74247424
return false;
@@ -8274,6 +8274,7 @@ GC_Session *new_dht_groupchats(Messenger *m)
82748274

82758275
c->messenger = m;
82768276
c->announces_list = m->group_announce;
8277+
c->tcp_np = nc_get_tcp_client_net_profile(m->net_crypto);
82778278

82788279
networking_registerhandler(m->net, NET_PACKET_GC_LOSSLESS, &handle_gc_udp_packet, m);
82798280
networking_registerhandler(m->net, NET_PACKET_GC_LOSSY, &handle_gc_udp_packet, m);

toxcore/group_common.h

+1
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ typedef void gc_rejected_cb(const Messenger *m, uint32_t group_number, unsigned
377377
typedef struct GC_Session {
378378
Messenger *messenger;
379379
GC_Chat *chats;
380+
Net_Profile *tcp_np;
380381
struct GC_Announces_List *announces_list;
381382

382383
uint32_t chats_index;

toxcore/net_crypto.c

+14-9
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ struct Net_Crypto {
137137
const Random *rng;
138138
Mono_Time *mono_time;
139139
const Network *ns;
140+
Net_Profile *tcp_np;
140141

141142
DHT *dht;
142143
TCP_Connections *tcp_c;
@@ -3000,15 +3001,24 @@ Net_Crypto *new_net_crypto(const Logger *log, const Memory *mem, const Random *r
30003001
return nullptr;
30013002
}
30023003

3004+
Net_Profile *tcp_np = netprof_new(log, mem);
3005+
3006+
if (tcp_np == nullptr) {
3007+
mem_delete(mem, temp);
3008+
return nullptr;
3009+
}
3010+
30033011
temp->log = log;
30043012
temp->mem = mem;
30053013
temp->rng = rng;
30063014
temp->mono_time = mono_time;
30073015
temp->ns = ns;
3016+
temp->tcp_np = tcp_np;
30083017

3009-
temp->tcp_c = new_tcp_connections(log, mem, rng, ns, mono_time, dht_get_self_secret_key(dht), proxy_info);
3018+
temp->tcp_c = new_tcp_connections(log, mem, rng, ns, mono_time, dht_get_self_secret_key(dht), proxy_info, tcp_np);
30103019

30113020
if (temp->tcp_c == nullptr) {
3021+
netprof_kill(mem, tcp_np);
30123022
mem_delete(mem, temp);
30133023
return nullptr;
30143024
}
@@ -3090,6 +3100,7 @@ void kill_net_crypto(Net_Crypto *c)
30903100
}
30913101

30923102
kill_tcp_connections(c->tcp_c);
3103+
netprof_kill(mem, c->tcp_np);
30933104
bs_list_free(&c->ip_port_list);
30943105
networking_registerhandler(dht_get_net(c->dht), NET_PACKET_COOKIE_REQUEST, nullptr, nullptr);
30953106
networking_registerhandler(dht_get_net(c->dht), NET_PACKET_COOKIE_RESPONSE, nullptr, nullptr);
@@ -3099,17 +3110,11 @@ void kill_net_crypto(Net_Crypto *c)
30993110
mem_delete(mem, c);
31003111
}
31013112

3102-
const Net_Profile *nc_get_tcp_client_net_profile(const Net_Crypto *c)
3113+
Net_Profile *nc_get_tcp_client_net_profile(Net_Crypto *c)
31033114
{
31043115
if (c == nullptr) {
31053116
return nullptr;
31063117
}
31073118

3108-
const TCP_Connections *tcp_c = nc_get_tcp_c(c);
3109-
3110-
if (tcp_c == nullptr) {
3111-
return nullptr;
3112-
}
3113-
3114-
return tcp_connection_get_client_net_profile(tcp_c);
3119+
return c->tcp_np;
31153120
}

toxcore/net_crypto.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ void kill_net_crypto(Net_Crypto *c);
424424
* Returns null if `c` is null or the TCP_Connections associated with `c` is null.
425425
*/
426426
non_null()
427-
const Net_Profile *nc_get_tcp_client_net_profile(const Net_Crypto *c);
427+
Net_Profile *nc_get_tcp_client_net_profile(Net_Crypto *c);
428428

429429
#ifdef __cplusplus
430430
} /* extern "C" */

0 commit comments

Comments
 (0)