Skip to content

Commit 80fabd4

Browse files
committed
feat: Implement Tox network profiler
1 parent 05abe08 commit 80fabd4

31 files changed

+942
-39
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ testing/data
3636

3737
# Vim
3838
*.swp
39+
*.nvimlog
3940

4041
# Object files
4142
*.o

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,8 @@ set(toxcore_SOURCES
307307
toxcore/mem.h
308308
toxcore/mono_time.c
309309
toxcore/mono_time.h
310+
toxcore/net_profile.c
311+
toxcore/net_profile.h
310312
toxcore/net_crypto.c
311313
toxcore/net_crypto.h
312314
toxcore/network.c

auto_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ auto_test(invalid_udp_proxy)
7171
auto_test(lan_discovery)
7272
auto_test(lossless_packet)
7373
auto_test(lossy_packet)
74+
auto_test(netprof)
7475
auto_test(network)
7576
auto_test(onion)
7677
auto_test(overflow_recvq)

auto_tests/TCP_test.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,12 @@ static void test_basic(void)
112112

113113
// Sending the handshake
114114
ck_assert_msg(net_send(ns, logger, sock, handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1,
115-
&localhost) == TCP_CLIENT_HANDSHAKE_SIZE - 1,
115+
&localhost, nullptr) == TCP_CLIENT_HANDSHAKE_SIZE - 1,
116116
"An attempt to send the initial handshake minus last byte failed.");
117117

118118
do_tcp_server_delay(tcp_s, mono_time, 50);
119119

120-
ck_assert_msg(net_send(ns, logger, sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1, &localhost) == 1,
120+
ck_assert_msg(net_send(ns, logger, sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1, &localhost, nullptr) == 1,
121121
"The attempt to send the last byte of handshake failed.");
122122

123123
free(handshake);
@@ -156,7 +156,7 @@ static void test_basic(void)
156156
msg_length = sizeof(r_req) - i;
157157
}
158158

159-
ck_assert_msg(net_send(ns, logger, sock, r_req + i, msg_length, &localhost) == msg_length,
159+
ck_assert_msg(net_send(ns, logger, sock, r_req + i, msg_length, &localhost, nullptr) == msg_length,
160160
"Failed to send request after completing the handshake.");
161161
i += msg_length;
162162

@@ -242,12 +242,12 @@ static struct sec_TCP_con *new_tcp_con(const Logger *logger, const Memory *mem,
242242
"Failed to encrypt the outgoing handshake.");
243243

244244
ck_assert_msg(net_send(ns, logger, sock, handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1,
245-
&localhost) == TCP_CLIENT_HANDSHAKE_SIZE - 1,
245+
&localhost, nullptr) == TCP_CLIENT_HANDSHAKE_SIZE - 1,
246246
"Failed to send the first portion of the handshake to the TCP relay server.");
247247

248248
do_tcp_server_delay(tcp_s, mono_time, 50);
249249

250-
ck_assert_msg(net_send(ns, logger, sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1, &localhost) == 1,
250+
ck_assert_msg(net_send(ns, logger, sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1, &localhost, nullptr) == 1,
251251
"Failed to send last byte of handshake.");
252252

253253
do_tcp_server_delay(tcp_s, mono_time, 50);
@@ -291,7 +291,7 @@ static int write_packet_tcp_test_connection(const Logger *logger, struct sec_TCP
291291
localhost.ip = get_loopback();
292292
localhost.port = 0;
293293

294-
ck_assert_msg(net_send(con->ns, logger, con->sock, packet, packet_size, &localhost) == packet_size,
294+
ck_assert_msg(net_send(con->ns, logger, con->sock, packet, packet_size, &localhost, nullptr) == packet_size,
295295
"Failed to send a packet.");
296296
return 0;
297297
}
@@ -535,7 +535,7 @@ static void test_client(void)
535535
ip_port_tcp_s.port = net_htons(ports[random_u32(rng) % NUM_PORTS]);
536536
ip_port_tcp_s.ip = get_loopback();
537537

538-
TCP_Client_Connection *conn = new_tcp_connection(logger, mem, mono_time, rng, ns, &ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, nullptr);
538+
TCP_Client_Connection *conn = new_tcp_connection(logger, mem, mono_time, rng, ns, &ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, nullptr, nullptr);
539539
ck_assert_msg(conn != nullptr, "Failed to create a TCP client connection.");
540540
// TCP sockets might need a moment before they can be written to.
541541
c_sleep(50);
@@ -572,7 +572,7 @@ static void test_client(void)
572572
crypto_new_keypair(rng, f2_public_key, f2_secret_key);
573573
ip_port_tcp_s.port = net_htons(ports[random_u32(rng) % NUM_PORTS]);
574574
TCP_Client_Connection *conn2 = new_tcp_connection(logger, mem, mono_time, rng, ns, &ip_port_tcp_s, self_public_key, f2_public_key,
575-
f2_secret_key, nullptr);
575+
f2_secret_key, nullptr, nullptr);
576576
ck_assert_msg(conn2 != nullptr, "Failed to create a second TCP client connection.");
577577
c_sleep(50);
578578

@@ -670,7 +670,7 @@ static void test_client_invalid(void)
670670
ip_port_tcp_s.port = net_htons(ports[random_u32(rng) % NUM_PORTS]);
671671
ip_port_tcp_s.ip = get_loopback();
672672
TCP_Client_Connection *conn = new_tcp_connection(logger, mem, mono_time, rng, ns, &ip_port_tcp_s,
673-
self_public_key, f_public_key, f_secret_key, nullptr);
673+
self_public_key, f_public_key, f_secret_key, nullptr, nullptr);
674674
ck_assert_msg(conn != nullptr, "Failed to create a TCP client connection.");
675675

676676
// Run the client's main loop but not the server.

auto_tests/netprof_test.c

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/** Auto Tests: basic network profile functionality test (UDP only)
2+
* TODO(JFreegman): test TCP packets as well
3+
*/
4+
5+
#include <stdint.h>
6+
#include <stdio.h>
7+
8+
#include "../toxcore/tox_private.h"
9+
#include "../toxcore/util.h"
10+
11+
#include "auto_test_support.h"
12+
#include "check_compat.h"
13+
14+
#define NUM_TOXES 2
15+
16+
static void test_netprof(AutoTox *autotoxes)
17+
{
18+
// Send some messages to create fake traffic
19+
for (size_t i = 0; i < 256; ++i) {
20+
for (uint32_t j = 0; j < NUM_TOXES; ++j) {
21+
tox_friend_send_message(autotoxes[j].tox, 0, TOX_MESSAGE_TYPE_NORMAL, (const uint8_t *)"test", 4, nullptr);
22+
}
23+
24+
iterate_all_wait(autotoxes, NUM_TOXES, ITERATION_INTERVAL);
25+
}
26+
27+
// idle traffic for a while
28+
for (size_t i = 0; i < 100; ++i) {
29+
iterate_all_wait(autotoxes, NUM_TOXES, ITERATION_INTERVAL);
30+
}
31+
32+
const Tox *tox1 = autotoxes[0].tox;
33+
34+
const unsigned long long UDP_count_sent1 = tox_netprof_get_packet_total_count(tox1, TOX_NETPROF_PACKET_TYPE_UDP,
35+
TOX_NETPROF_DIRECTION_SENT);
36+
const unsigned long long UDP_count_recv1 = tox_netprof_get_packet_total_count(tox1, TOX_NETPROF_PACKET_TYPE_UDP,
37+
TOX_NETPROF_DIRECTION_RECV);
38+
const unsigned long long TCP_count_sent1 = tox_netprof_get_packet_total_count(tox1, TOX_NETPROF_PACKET_TYPE_TCP,
39+
TOX_NETPROF_DIRECTION_SENT);
40+
const unsigned long long TCP_count_recv1 = tox_netprof_get_packet_total_count(tox1, TOX_NETPROF_PACKET_TYPE_TCP,
41+
TOX_NETPROF_DIRECTION_RECV);
42+
43+
const unsigned long long UDP_bytes_sent1 = tox_netprof_get_packet_total_bytes(tox1, TOX_NETPROF_PACKET_TYPE_UDP,
44+
TOX_NETPROF_DIRECTION_SENT);
45+
const unsigned long long UDP_bytes_recv1 = tox_netprof_get_packet_total_bytes(tox1, TOX_NETPROF_PACKET_TYPE_UDP,
46+
TOX_NETPROF_DIRECTION_RECV);
47+
const unsigned long long TCP_bytes_sent1 = tox_netprof_get_packet_total_bytes(tox1, TOX_NETPROF_PACKET_TYPE_TCP,
48+
TOX_NETPROF_DIRECTION_SENT);
49+
const unsigned long long TCP_bytes_recv1 = tox_netprof_get_packet_total_bytes(tox1, TOX_NETPROF_PACKET_TYPE_TCP,
50+
TOX_NETPROF_DIRECTION_RECV);
51+
52+
ck_assert(UDP_count_recv1 > 0 && UDP_count_sent1 > 0);
53+
ck_assert(UDP_bytes_recv1 > 0 && UDP_bytes_sent1 > 0);
54+
55+
(void)TCP_count_sent1;
56+
(void)TCP_bytes_sent1;
57+
(void)TCP_bytes_recv1;
58+
(void)TCP_count_recv1;
59+
60+
unsigned long long total_sent_count = 0;
61+
unsigned long long total_recv_count = 0;
62+
unsigned long long total_sent_bytes = 0;
63+
unsigned long long total_recv_bytes = 0;
64+
65+
// tox1 makes sure the sum value of all packet ID's is equal to the totals
66+
for (size_t i = 0; i < 256; ++i) {
67+
// this id isn't valid for UDP packets but we still want to call the
68+
// functions and make sure they return some non-zero value
69+
if (i == TOX_NETPROF_PACKET_ID_TCP_DATA) {
70+
ck_assert(tox_netprof_get_packet_id_count(tox1, TOX_NETPROF_PACKET_TYPE_UDP, i,
71+
TOX_NETPROF_DIRECTION_SENT) > 0);
72+
ck_assert(tox_netprof_get_packet_id_bytes(tox1, TOX_NETPROF_PACKET_TYPE_UDP, i,
73+
TOX_NETPROF_DIRECTION_SENT) > 0);
74+
ck_assert(tox_netprof_get_packet_id_bytes(tox1, TOX_NETPROF_PACKET_TYPE_UDP, i,
75+
TOX_NETPROF_DIRECTION_SENT) > 0);
76+
ck_assert(tox_netprof_get_packet_id_bytes(tox1, TOX_NETPROF_PACKET_TYPE_UDP, i,
77+
TOX_NETPROF_DIRECTION_RECV) > 0);
78+
continue;
79+
}
80+
81+
total_sent_count += tox_netprof_get_packet_id_count(tox1, TOX_NETPROF_PACKET_TYPE_UDP, i,
82+
TOX_NETPROF_DIRECTION_SENT);
83+
total_recv_count += tox_netprof_get_packet_id_count(tox1, TOX_NETPROF_PACKET_TYPE_UDP, i,
84+
TOX_NETPROF_DIRECTION_RECV);
85+
86+
total_sent_bytes += tox_netprof_get_packet_id_bytes(tox1, TOX_NETPROF_PACKET_TYPE_UDP, i,
87+
TOX_NETPROF_DIRECTION_SENT);
88+
total_recv_bytes += tox_netprof_get_packet_id_bytes(tox1, TOX_NETPROF_PACKET_TYPE_UDP, i,
89+
TOX_NETPROF_DIRECTION_RECV);
90+
}
91+
92+
const unsigned long long total_packets = total_sent_count + total_recv_count;
93+
ck_assert_msg(total_packets == UDP_count_sent1 + UDP_count_recv1,
94+
"%llu does not match %llu\n", total_packets, UDP_count_sent1 + UDP_count_recv1);
95+
96+
ck_assert_msg(total_sent_count == UDP_count_sent1, "%llu does not match %llu\n", total_sent_count, UDP_count_sent1);
97+
ck_assert_msg(total_recv_count == UDP_count_recv1, "%llu does not match %llu\n", total_recv_count, UDP_count_recv1);
98+
99+
100+
const unsigned long long total_bytes = total_sent_bytes + total_recv_bytes;
101+
ck_assert_msg(total_bytes == UDP_bytes_sent1 + UDP_bytes_recv1,
102+
"%llu does not match %llu\n", total_bytes, UDP_bytes_sent1 + UDP_bytes_recv1);
103+
104+
ck_assert_msg(total_sent_bytes == UDP_bytes_sent1, "%llu does not match %llu\n", total_sent_bytes, UDP_bytes_sent1);
105+
ck_assert_msg(total_recv_bytes == UDP_bytes_recv1, "%llu does not match %llu\n", total_recv_bytes, UDP_bytes_recv1);
106+
}
107+
108+
int main(void)
109+
{
110+
setvbuf(stdout, nullptr, _IONBF, 0);
111+
112+
Run_Auto_Options autotox_opts = default_run_auto_options();
113+
autotox_opts.graph = GRAPH_COMPLETE;
114+
115+
run_auto_test(nullptr, NUM_TOXES, test_netprof, 0, &autotox_opts);
116+
117+
return 0;
118+
}
119+
120+
#undef NUM_TOXES

other/bootstrap_node_packets.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static int handle_info_request(void *object, const IP_Port *source, const uint8_
3030
return 1;
3131
}
3232

33-
const Networking_Core *nc = (const Networking_Core *)object;
33+
Networking_Core *nc = (Networking_Core *)object;
3434

3535
uint8_t data[1 + sizeof(bootstrap_version) + MAX_MOTD_LENGTH];
3636
data[0] = BOOTSTRAP_INFO_PACKET_ID;

toxcore/BUILD.bazel

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,16 @@ cc_library(
350350
],
351351
)
352352

353+
cc_library(
354+
name = "net_profile",
355+
srcs = ["net_profile.c"],
356+
hdrs = ["net_profile.h"],
357+
deps = [
358+
":attributes",
359+
":ccompat",
360+
],
361+
)
362+
353363
cc_library(
354364
name = "network",
355365
srcs = ["network.c"],
@@ -369,6 +379,7 @@ cc_library(
369379
":logger",
370380
":mem",
371381
":mono_time",
382+
":net_profile",
372383
":util",
373384
"@libsodium",
374385
"@psocket",
@@ -635,6 +646,7 @@ cc_library(
635646
":crypto_core",
636647
":logger",
637648
":mem",
649+
":net_profile",
638650
":network",
639651
],
640652
)
@@ -662,6 +674,7 @@ cc_library(
662674
":logger",
663675
":mem",
664676
":mono_time",
677+
":net_profile",
665678
":network",
666679
":onion",
667680
":util",
@@ -683,6 +696,7 @@ cc_library(
683696
":logger",
684697
":mem",
685698
":mono_time",
699+
":net_profile",
686700
":network",
687701
":util",
688702
],
@@ -705,6 +719,7 @@ cc_library(
705719
":logger",
706720
":mem",
707721
":mono_time",
722+
":net_profile",
708723
":network",
709724
":onion",
710725
":util",
@@ -742,6 +757,7 @@ cc_library(
742757
":logger",
743758
":mem",
744759
":mono_time",
760+
":net_profile",
745761
":network",
746762
":util",
747763
"@pthread",
@@ -1069,6 +1085,7 @@ cc_library(
10691085
":DHT",
10701086
":Messenger",
10711087
":TCP_client",
1088+
":TCP_server",
10721089
":attributes",
10731090
":ccompat",
10741091
":crypto_core",
@@ -1079,6 +1096,7 @@ cc_library(
10791096
":mem",
10801097
":mono_time",
10811098
":net_crypto",
1099+
":net_profile",
10821100
":network",
10831101
":onion_client",
10841102
":state",

toxcore/LAN_discovery.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ static Broadcast_Info *fetch_broadcast_info(const Memory *mem, const Network *ns
223223
* @retval false on failure to find any valid broadcast target.
224224
*/
225225
non_null()
226-
static bool send_broadcasts(const Networking_Core *net, const Broadcast_Info *broadcast, uint16_t port,
226+
static bool send_broadcasts(Networking_Core *net, const Broadcast_Info *broadcast, uint16_t port,
227227
const uint8_t *data, uint16_t length)
228228
{
229229
if (broadcast->count == 0) {
@@ -352,7 +352,7 @@ bool ip_is_lan(const IP *ip)
352352
return false;
353353
}
354354

355-
bool lan_discovery_send(const Networking_Core *net, const Broadcast_Info *broadcast, const uint8_t *dht_pk,
355+
bool lan_discovery_send(Networking_Core *net, const Broadcast_Info *broadcast, const uint8_t *dht_pk,
356356
uint16_t port)
357357
{
358358
if (broadcast == nullptr) {

toxcore/LAN_discovery.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ typedef struct Broadcast_Info Broadcast_Info;
2626
* @return true on success, false on failure.
2727
*/
2828
non_null()
29-
bool lan_discovery_send(const Networking_Core *net, const Broadcast_Info *broadcast, const uint8_t *dht_pk,
29+
bool lan_discovery_send(Networking_Core *net, const Broadcast_Info *broadcast, const uint8_t *dht_pk,
3030
uint16_t port);
3131

3232
/**

toxcore/Makefile.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ libtoxcore_la_SOURCES = ../third_party/cmp/cmp.c \
7474
../toxcore/ping_array.c \
7575
../toxcore/net_crypto.h \
7676
../toxcore/net_crypto.c \
77+
../toxcore/net_profile.c \
78+
../toxcore/net_profile.h \
7779
../toxcore/friend_requests.h \
7880
../toxcore/friend_requests.c \
7981
../toxcore/LAN_discovery.h \

0 commit comments

Comments
 (0)