Skip to content

Commit f4d840c

Browse files
committed
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.
1 parent 59d3f66 commit f4d840c

14 files changed

+87
-65
lines changed

Diff for: auto_tests/TCP_test.c

+15-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "../toxcore/TCP_server.h"
99
#include "../toxcore/crypto_core.h"
1010
#include "../toxcore/mono_time.h"
11+
#include "../toxcore/net_profile.h"
1112
#include "../toxcore/network.h"
1213
#include "auto_test_support.h"
1314

@@ -737,6 +738,9 @@ static void test_tcp_connection(void)
737738
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
738739
Logger *logger = logger_new(mem);
739740

741+
Net_Profile *tcp_np = netprof_new(logger, mem);
742+
ck_assert(tcp_np != nullptr);
743+
740744
tcp_data_callback_called = 0;
741745
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
742746
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
@@ -747,12 +751,12 @@ static void test_tcp_connection(void)
747751
TCP_Proxy_Info proxy_info;
748752
proxy_info.proxy_type = TCP_PROXY_NONE;
749753
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);
754+
TCP_Connections *tc_1 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info, tcp_np);
751755
ck_assert_msg(tc_1 != nullptr, "Failed to create TCP connections");
752756
ck_assert_msg(pk_equal(tcp_connections_public_key(tc_1), self_public_key), "Wrong public key");
753757

754758
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);
759+
TCP_Connections *tc_2 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info, tcp_np);
756760
ck_assert_msg(tc_2 != nullptr, "Failed to create TCP connections");
757761
ck_assert_msg(pk_equal(tcp_connections_public_key(tc_2), self_public_key), "Wrong public key");
758762

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

818+
netprof_kill(mem, tcp_np);
819+
814820
kill_tcp_server(tcp_s);
815821
kill_tcp_connections(tc_1);
816822
kill_tcp_connections(tc_2);
@@ -852,6 +858,9 @@ static void test_tcp_connection2(void)
852858
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
853859
Logger *logger = logger_new(mem);
854860

861+
Net_Profile *tcp_np = netprof_new(logger, mem);
862+
ck_assert(tcp_np != nullptr);
863+
855864
tcp_oobdata_callback_called = 0;
856865
tcp_data_callback_called = 0;
857866

@@ -864,12 +873,12 @@ static void test_tcp_connection2(void)
864873
TCP_Proxy_Info proxy_info;
865874
proxy_info.proxy_type = TCP_PROXY_NONE;
866875
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);
876+
TCP_Connections *tc_1 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info, tcp_np);
868877
ck_assert_msg(tc_1 != nullptr, "Failed to create TCP connections");
869878
ck_assert_msg(pk_equal(tcp_connections_public_key(tc_1), self_public_key), "Wrong public key");
870879

871880
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);
881+
TCP_Connections *tc_2 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info, tcp_np);
873882
ck_assert_msg(tc_2 != nullptr, "Failed to create TCP connections");
874883
ck_assert_msg(pk_equal(tcp_connections_public_key(tc_2), self_public_key), "Wrong public key");
875884

@@ -921,6 +930,8 @@ static void test_tcp_connection2(void)
921930
ck_assert_msg(tcp_data_callback_called, "could not recv packet.");
922931
ck_assert_msg(kill_tcp_connection_to(tc_1, 0) == 0, "could not kill connection to\n");
923932

933+
netprof_kill(mem, tcp_np);
934+
924935
kill_tcp_server(tcp_s);
925936
kill_tcp_connections(tc_1);
926937
kill_tcp_connections(tc_2);

Diff for: auto_tests/forwarding_test.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ typedef struct Forwarding_Subtox {
9696
Logger *log;
9797
Mono_Time *mono_time;
9898
Networking_Core *net;
99+
Net_Profile *tcp_np;
99100
DHT *dht;
100101
Net_Crypto *c;
101102
Forwarding *forwarding;
@@ -126,8 +127,11 @@ static Forwarding_Subtox *new_forwarding_subtox(const Memory *mem, bool no_udp,
126127

127128
subtox->dht = new_dht(subtox->log, mem, rng, ns, subtox->mono_time, subtox->net, true, true);
128129

130+
subtox->tcp_np = netprof_new(subtox->log, mem);
131+
ck_assert(subtox->tcp_np != nullptr);
132+
129133
const TCP_Proxy_Info inf = {{{{0}}}};
130-
subtox->c = new_net_crypto(subtox->log, mem, rng, ns, subtox->mono_time, subtox->dht, &inf);
134+
subtox->c = new_net_crypto(subtox->log, mem, rng, ns, subtox->mono_time, subtox->dht, &inf, subtox->tcp_np);
131135

132136
subtox->forwarding = new_forwarding(subtox->log, mem, rng, subtox->mono_time, subtox->dht);
133137
ck_assert(subtox->forwarding != nullptr);
@@ -143,6 +147,7 @@ static void kill_forwarding_subtox(const Memory *mem, Forwarding_Subtox *subtox)
143147
kill_announcements(subtox->announce);
144148
kill_forwarding(subtox->forwarding);
145149
kill_net_crypto(subtox->c);
150+
netprof_kill(mem, subtox->tcp_np);
146151
kill_dht(subtox->dht);
147152
kill_networking(subtox->net);
148153
mono_time_free(mem, subtox->mono_time);

Diff for: auto_tests/onion_test.c

+17-1
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ static void test_basic(void)
396396
typedef struct {
397397
Logger *log;
398398
Mono_Time *mono_time;
399+
Net_Profile *tcp_np;
399400
Onion *onion;
400401
Onion_Announce *onion_a;
401402
Onion_Client *onion_c;
@@ -471,10 +472,24 @@ static Onions *new_onions(const Memory *mem, const Random *rng, uint16_t port, u
471472
return nullptr;
472473
}
473474

475+
on->tcp_np = netprof_new(on->log, mem);
476+
477+
if (!on->tcp_np) {
478+
kill_onion_announce(on->onion_a);
479+
kill_onion(on->onion);
480+
kill_dht(dht);
481+
kill_networking(net);
482+
mono_time_free(mem, on->mono_time);
483+
logger_kill(on->log);
484+
free(on);
485+
return nullptr;
486+
}
487+
474488
TCP_Proxy_Info inf = {{{{0}}}};
475-
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));
489+
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));
476490

477491
if (!on->onion_c) {
492+
netprof_kill(mem, on->tcp_np);
478493
kill_onion_announce(on->onion_a);
479494
kill_onion(on->onion);
480495
kill_dht(dht);
@@ -506,6 +521,7 @@ static void kill_onions(const Memory *mem, Onions *on)
506521
kill_onion_announce(on->onion_a);
507522
kill_onion(on->onion);
508523
kill_net_crypto(c);
524+
netprof_kill(mem, on->tcp_np);
509525
kill_dht(dht);
510526
kill_networking(net);
511527
mono_time_free(mem, on->mono_time);

Diff for: toxcore/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,7 @@ cc_library(
10581058
":mem",
10591059
":mono_time",
10601060
":net_crypto",
1061+
":net_profile",
10611062
":network",
10621063
":onion",
10631064
":onion_announce",

Diff for: toxcore/Messenger.c

+20-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "mem.h"
3535
#include "mono_time.h"
3636
#include "net_crypto.h"
37+
#include "net_profile.h"
3738
#include "network.h"
3839
#include "onion.h"
3940
#include "onion_announce.h"
@@ -3531,11 +3532,24 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random *
35313532
return nullptr;
35323533
}
35333534

3534-
m->net_crypto = new_net_crypto(m->log, m->mem, m->rng, m->ns, m->mono_time, m->dht, &options->proxy_info);
3535+
m->tcp_np = netprof_new(m->log, mem);
3536+
3537+
if (m->tcp_np == nullptr) {
3538+
LOGGER_WARNING(m->log, "TCP netprof initialisation failed");
3539+
kill_dht(m->dht);
3540+
kill_networking(m->net);
3541+
friendreq_kill(m->fr);
3542+
logger_kill(m->log);
3543+
mem_delete(mem, m);
3544+
return nullptr;
3545+
}
3546+
3547+
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);
35353548

35363549
if (m->net_crypto == nullptr) {
35373550
LOGGER_WARNING(m->log, "net_crypto initialisation failed");
35383551

3552+
netprof_kill(mem, m->tcp_np);
35393553
kill_dht(m->dht);
35403554
kill_networking(m->net);
35413555
friendreq_kill(m->fr);
@@ -3550,6 +3564,7 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random *
35503564
LOGGER_WARNING(m->log, "DHT group chats initialisation failed");
35513565

35523566
kill_net_crypto(m->net_crypto);
3567+
netprof_kill(mem, m->tcp_np);
35533568
kill_dht(m->dht);
35543569
kill_networking(m->net);
35553570
friendreq_kill(m->fr);
@@ -3589,6 +3604,7 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random *
35893604
kill_announcements(m->announce);
35903605
kill_forwarding(m->forwarding);
35913606
kill_net_crypto(m->net_crypto);
3607+
netprof_kill(mem, m->tcp_np);
35923608
kill_dht(m->dht);
35933609
kill_networking(m->net);
35943610
friendreq_kill(m->fr);
@@ -3612,6 +3628,7 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random *
36123628
kill_announcements(m->announce);
36133629
kill_forwarding(m->forwarding);
36143630
kill_net_crypto(m->net_crypto);
3631+
netprof_kill(mem, m->tcp_np);
36153632
kill_dht(m->dht);
36163633
kill_networking(m->net);
36173634
friendreq_kill(m->fr);
@@ -3637,6 +3654,7 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random *
36373654
kill_announcements(m->announce);
36383655
kill_forwarding(m->forwarding);
36393656
kill_net_crypto(m->net_crypto);
3657+
netprof_kill(mem, m->tcp_np);
36403658
kill_dht(m->dht);
36413659
kill_networking(m->net);
36423660
friendreq_kill(m->fr);
@@ -3692,6 +3710,7 @@ void kill_messenger(Messenger *m)
36923710
kill_announcements(m->announce);
36933711
kill_forwarding(m->forwarding);
36943712
kill_net_crypto(m->net_crypto);
3713+
netprof_kill(m->mem, m->tcp_np);
36953714
kill_dht(m->dht);
36963715
kill_networking(m->net);
36973716

Diff for: toxcore/Messenger.h

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "mem.h"
2626
#include "mono_time.h"
2727
#include "net_crypto.h"
28+
#include "net_profile.h"
2829
#include "network.h"
2930
#include "onion.h"
3031
#include "onion_announce.h"
@@ -248,6 +249,7 @@ struct Messenger {
248249

249250
Networking_Core *net;
250251
Net_Crypto *net_crypto;
252+
Net_Profile *tcp_np;
251253
DHT *dht;
252254

253255
Forwarding *forwarding;

Diff for: 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-
}

Diff for: 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 */

Diff for: 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 = m->tcp_np;
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);

Diff for: toxcore/group_common.h

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "logger.h"
2121
#include "mem.h"
2222
#include "mono_time.h"
23+
#include "net_profile.h"
2324
#include "network.h"
2425

2526
#define MAX_GC_PART_MESSAGE_SIZE 128
@@ -377,6 +378,7 @@ typedef void gc_rejected_cb(const Messenger *m, uint32_t group_number, unsigned
377378
typedef struct GC_Session {
378379
Messenger *messenger;
379380
GC_Chat *chats;
381+
Net_Profile *tcp_np;
380382
struct GC_Announces_List *announces_list;
381383

382384
uint32_t chats_index;

Diff for: toxcore/net_crypto.c

+2-17
Original file line numberDiff line numberDiff line change
@@ -2988,7 +2988,7 @@ void load_secret_key(Net_Crypto *c, const uint8_t *sk)
29882988
* Sets all the global connection variables to their default values.
29892989
*/
29902990
Net_Crypto *new_net_crypto(const Logger *log, const Memory *mem, const Random *rng, const Network *ns,
2991-
Mono_Time *mono_time, DHT *dht, const TCP_Proxy_Info *proxy_info)
2991+
Mono_Time *mono_time, DHT *dht, const TCP_Proxy_Info *proxy_info, Net_Profile *tcp_np)
29922992
{
29932993
if (dht == nullptr) {
29942994
return nullptr;
@@ -3006,7 +3006,7 @@ Net_Crypto *new_net_crypto(const Logger *log, const Memory *mem, const Random *r
30063006
temp->mono_time = mono_time;
30073007
temp->ns = ns;
30083008

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

30113011
if (temp->tcp_c == nullptr) {
30123012
mem_delete(mem, temp);
@@ -3098,18 +3098,3 @@ void kill_net_crypto(Net_Crypto *c)
30983098
crypto_memzero(c, sizeof(Net_Crypto));
30993099
mem_delete(mem, c);
31003100
}
3101-
3102-
const Net_Profile *nc_get_tcp_client_net_profile(const Net_Crypto *c)
3103-
{
3104-
if (c == nullptr) {
3105-
return nullptr;
3106-
}
3107-
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);
3115-
}

0 commit comments

Comments
 (0)