Skip to content

Commit 52d915e

Browse files
committed
cleanup: Heap allocate network profile objects
1 parent 80fabd4 commit 52d915e

File tree

16 files changed

+136
-65
lines changed

16 files changed

+136
-65
lines changed

auto_tests/netprof_test.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,22 @@ static void test_netprof(AutoTox *autotoxes)
3131

3232
const Tox *tox1 = autotoxes[0].tox;
3333

34-
const unsigned long long UDP_count_sent1 = tox_netprof_get_packet_total_count(tox1, TOX_NETPROF_PACKET_TYPE_UDP,
34+
const uint64_t UDP_count_sent1 = tox_netprof_get_packet_total_count(tox1, TOX_NETPROF_PACKET_TYPE_UDP,
3535
TOX_NETPROF_DIRECTION_SENT);
36-
const unsigned long long UDP_count_recv1 = tox_netprof_get_packet_total_count(tox1, TOX_NETPROF_PACKET_TYPE_UDP,
36+
const uint64_t UDP_count_recv1 = tox_netprof_get_packet_total_count(tox1, TOX_NETPROF_PACKET_TYPE_UDP,
3737
TOX_NETPROF_DIRECTION_RECV);
38-
const unsigned long long TCP_count_sent1 = tox_netprof_get_packet_total_count(tox1, TOX_NETPROF_PACKET_TYPE_TCP,
38+
const uint64_t TCP_count_sent1 = tox_netprof_get_packet_total_count(tox1, TOX_NETPROF_PACKET_TYPE_TCP,
3939
TOX_NETPROF_DIRECTION_SENT);
40-
const unsigned long long TCP_count_recv1 = tox_netprof_get_packet_total_count(tox1, TOX_NETPROF_PACKET_TYPE_TCP,
40+
const uint64_t TCP_count_recv1 = tox_netprof_get_packet_total_count(tox1, TOX_NETPROF_PACKET_TYPE_TCP,
4141
TOX_NETPROF_DIRECTION_RECV);
4242

43-
const unsigned long long UDP_bytes_sent1 = tox_netprof_get_packet_total_bytes(tox1, TOX_NETPROF_PACKET_TYPE_UDP,
43+
const uint64_t UDP_bytes_sent1 = tox_netprof_get_packet_total_bytes(tox1, TOX_NETPROF_PACKET_TYPE_UDP,
4444
TOX_NETPROF_DIRECTION_SENT);
45-
const unsigned long long UDP_bytes_recv1 = tox_netprof_get_packet_total_bytes(tox1, TOX_NETPROF_PACKET_TYPE_UDP,
45+
const uint64_t UDP_bytes_recv1 = tox_netprof_get_packet_total_bytes(tox1, TOX_NETPROF_PACKET_TYPE_UDP,
4646
TOX_NETPROF_DIRECTION_RECV);
47-
const unsigned long long TCP_bytes_sent1 = tox_netprof_get_packet_total_bytes(tox1, TOX_NETPROF_PACKET_TYPE_TCP,
47+
const uint64_t TCP_bytes_sent1 = tox_netprof_get_packet_total_bytes(tox1, TOX_NETPROF_PACKET_TYPE_TCP,
4848
TOX_NETPROF_DIRECTION_SENT);
49-
const unsigned long long TCP_bytes_recv1 = tox_netprof_get_packet_total_bytes(tox1, TOX_NETPROF_PACKET_TYPE_TCP,
49+
const uint64_t TCP_bytes_recv1 = tox_netprof_get_packet_total_bytes(tox1, TOX_NETPROF_PACKET_TYPE_TCP,
5050
TOX_NETPROF_DIRECTION_RECV);
5151

5252
ck_assert(UDP_count_recv1 > 0 && UDP_count_sent1 > 0);
@@ -57,10 +57,10 @@ static void test_netprof(AutoTox *autotoxes)
5757
(void)TCP_bytes_recv1;
5858
(void)TCP_count_recv1;
5959

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;
60+
uint64_t total_sent_count = 0;
61+
uint64_t total_recv_count = 0;
62+
uint64_t total_sent_bytes = 0;
63+
uint64_t total_recv_bytes = 0;
6464

6565
// tox1 makes sure the sum value of all packet ID's is equal to the totals
6666
for (size_t i = 0; i < 256; ++i) {
@@ -89,20 +89,20 @@ static void test_netprof(AutoTox *autotoxes)
8989
TOX_NETPROF_DIRECTION_RECV);
9090
}
9191

92-
const unsigned long long total_packets = total_sent_count + total_recv_count;
92+
const uint64_t total_packets = total_sent_count + total_recv_count;
9393
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);
94+
"%lu does not match %lu\n", total_packets, UDP_count_sent1 + UDP_count_recv1);
9595

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);
96+
ck_assert_msg(total_sent_count == UDP_count_sent1, "%lu does not match %lu\n", total_sent_count, UDP_count_sent1);
97+
ck_assert_msg(total_recv_count == UDP_count_recv1, "%lu does not match %lu\n", total_recv_count, UDP_count_recv1);
9898

9999

100-
const unsigned long long total_bytes = total_sent_bytes + total_recv_bytes;
100+
const uint64_t total_bytes = total_sent_bytes + total_recv_bytes;
101101
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);
102+
"%lu does not match %lu\n", total_bytes, UDP_bytes_sent1 + UDP_bytes_recv1);
103103

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);
104+
ck_assert_msg(total_sent_bytes == UDP_bytes_sent1, "%lu does not match %lu\n", total_sent_bytes, UDP_bytes_sent1);
105+
ck_assert_msg(total_recv_bytes == UDP_bytes_recv1, "%lu does not match %lu\n", total_recv_bytes, UDP_bytes_recv1);
106106
}
107107

108108
int main(void)

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-
Networking_Core *nc = (Networking_Core *)object;
33+
const Networking_Core *nc = (const 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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ cc_library(
357357
deps = [
358358
":attributes",
359359
":ccompat",
360+
":logger",
361+
":mem",
360362
],
361363
)
362364

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(Networking_Core *net, const Broadcast_Info *broadcast, uint16_t port,
226+
static bool send_broadcasts(const 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(Networking_Core *net, const Broadcast_Info *broadcast, const uint8_t *dht_pk,
355+
bool lan_discovery_send(const 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(Networking_Core *net, const Broadcast_Info *broadcast, const uint8_t *dht_pk,
29+
bool lan_discovery_send(const Networking_Core *net, const Broadcast_Info *broadcast, const uint8_t *dht_pk,
3030
uint16_t port);
3131

3232
/**

toxcore/TCP_connection.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ struct TCP_Connections {
5959
uint16_t onion_num_conns;
6060

6161
/* Network profile for all TCP client packets. */
62-
Net_Profile net_profile;
62+
Net_Profile *net_profile;
6363
};
6464

6565
static const TCP_Connection_to empty_tcp_connection_to = {0};
@@ -933,7 +933,7 @@ static int reconnect_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connec
933933
memcpy(relay_pk, tcp_con_public_key(tcp_con->connection), CRYPTO_PUBLIC_KEY_SIZE);
934934
kill_tcp_connection(tcp_con->connection);
935935
tcp_con->connection = new_tcp_connection(tcp_c->logger, tcp_c->mem, tcp_c->mono_time, tcp_c->rng, tcp_c->ns, &ip_port, relay_pk, tcp_c->self_public_key, tcp_c->self_secret_key, &tcp_c->proxy_info,
936-
&tcp_c->net_profile);
936+
tcp_c->net_profile);
937937

938938
if (tcp_con->connection == nullptr) {
939939
kill_tcp_relay_connection(tcp_c, tcp_connections_number);
@@ -1022,7 +1022,7 @@ static int unsleep_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connecti
10221022

10231023
tcp_con->connection = new_tcp_connection(
10241024
tcp_c->logger, tcp_c->mem, tcp_c->mono_time, tcp_c->rng, tcp_c->ns, &tcp_con->ip_port,
1025-
tcp_con->relay_pk, tcp_c->self_public_key, tcp_c->self_secret_key, &tcp_c->proxy_info, &tcp_c->net_profile);
1025+
tcp_con->relay_pk, tcp_c->self_public_key, tcp_c->self_secret_key, &tcp_c->proxy_info, tcp_c->net_profile);
10261026

10271027
if (tcp_con->connection == nullptr) {
10281028
kill_tcp_relay_connection(tcp_c, tcp_connections_number);
@@ -1320,7 +1320,7 @@ static int add_tcp_relay_instance(TCP_Connections *tcp_c, const IP_Port *ip_port
13201320

13211321
tcp_con->connection = new_tcp_connection(
13221322
tcp_c->logger, tcp_c->mem, tcp_c->mono_time, tcp_c->rng, tcp_c->ns, &ipp_copy,
1323-
relay_pk, tcp_c->self_public_key, tcp_c->self_secret_key, &tcp_c->proxy_info, &tcp_c->net_profile);
1323+
relay_pk, tcp_c->self_public_key, tcp_c->self_secret_key, &tcp_c->proxy_info, tcp_c->net_profile);
13241324

13251325
if (tcp_con->connection == nullptr) {
13261326
return -1;
@@ -1614,6 +1614,14 @@ 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;
16171625
temp->logger = logger;
16181626
temp->mem = mem;
16191627
temp->rng = rng;
@@ -1728,6 +1736,7 @@ void kill_tcp_connections(TCP_Connections *tcp_c)
17281736

17291737
crypto_memzero(tcp_c->self_secret_key, sizeof(tcp_c->self_secret_key));
17301738

1739+
netprof_kill(tcp_c->mem, tcp_c->net_profile);
17311740
mem_delete(tcp_c->mem, tcp_c->tcp_connections);
17321741
mem_delete(tcp_c->mem, tcp_c->connections);
17331742
mem_delete(tcp_c->mem, tcp_c);
@@ -1739,5 +1748,5 @@ const Net_Profile *tcp_connection_get_client_net_profile(const TCP_Connections *
17391748
return nullptr;
17401749
}
17411750

1742-
return &tcp_c->net_profile;
1751+
return tcp_c->net_profile;
17431752
}

toxcore/TCP_server.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ struct TCP_Server {
9494
BS_List accepted_key_list;
9595

9696
/* Network profile for all TCP server packets. */
97-
Net_Profile net_profile;
97+
Net_Profile *net_profile;
9898
};
9999

100100
static_assert(sizeof(TCP_Server) < 7 * 1024 * 1024,
@@ -240,7 +240,7 @@ static int add_accepted(TCP_Server *tcp_server, const Mono_Time *mono_time, TCP_
240240
tcp_server->accepted_connection_array[index].identifier = ++tcp_server->counter;
241241
tcp_server->accepted_connection_array[index].last_pinged = mono_time_get(mono_time);
242242
tcp_server->accepted_connection_array[index].ping_id = 0;
243-
tcp_server->accepted_connection_array[index].con.net_profile = &tcp_server->net_profile;
243+
tcp_server->accepted_connection_array[index].con.net_profile = tcp_server->net_profile;
244244

245245
return index;
246246
}
@@ -975,6 +975,14 @@ TCP_Server *new_tcp_server(const Logger *logger, const Memory *mem, const Random
975975
return nullptr;
976976
}
977977

978+
Net_Profile *np = netprof_new(logger, mem);
979+
980+
if (np == nullptr) {
981+
mem_delete(mem, temp);
982+
return nullptr;
983+
}
984+
985+
temp->net_profile = np;
978986
temp->logger = logger;
979987
temp->mem = mem;
980988
temp->ns = ns;
@@ -984,6 +992,7 @@ TCP_Server *new_tcp_server(const Logger *logger, const Memory *mem, const Random
984992

985993
if (socks_listening == nullptr) {
986994
LOGGER_ERROR(logger, "socket allocation failed");
995+
netprof_kill(mem, temp->net_profile);
987996
mem_delete(mem, temp);
988997
return nullptr;
989998
}
@@ -995,6 +1004,7 @@ TCP_Server *new_tcp_server(const Logger *logger, const Memory *mem, const Random
9951004

9961005
if (temp->efd == -1) {
9971006
LOGGER_ERROR(logger, "epoll initialisation failed");
1007+
netprof_kill(mem, temp->net_profile);
9981008
mem_delete(mem, socks_listening);
9991009
mem_delete(mem, temp);
10001010
return nullptr;
@@ -1028,6 +1038,7 @@ TCP_Server *new_tcp_server(const Logger *logger, const Memory *mem, const Random
10281038
}
10291039

10301040
if (temp->num_listening_socks == 0) {
1041+
netprof_kill(mem, temp->net_profile);
10311042
mem_delete(mem, temp->socks_listening);
10321043
mem_delete(mem, temp);
10331044
return nullptr;
@@ -1428,6 +1439,7 @@ void kill_tcp_server(TCP_Server *tcp_server)
14281439

14291440
crypto_memzero(tcp_server->secret_key, sizeof(tcp_server->secret_key));
14301441

1442+
netprof_kill(tcp_server->mem, tcp_server->net_profile);
14311443
mem_delete(tcp_server->mem, tcp_server->socks_listening);
14321444
mem_delete(tcp_server->mem, tcp_server);
14331445
}
@@ -1438,5 +1450,5 @@ const Net_Profile *tcp_server_get_net_profile(const TCP_Server *tcp_server)
14381450
return nullptr;
14391451
}
14401452

1441-
return &tcp_server->net_profile;
1453+
return tcp_server->net_profile;
14421454
}

toxcore/forwarding.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ DHT *forwarding_get_dht(const Forwarding *forwarding)
4444

4545
#define SENDBACK_TIMEOUT 3600
4646

47-
bool send_forward_request(Networking_Core *net, const IP_Port *forwarder,
47+
bool send_forward_request(const Networking_Core *net, const IP_Port *forwarder,
4848
const uint8_t *chain_keys, uint16_t chain_length,
4949
const uint8_t *data, uint16_t data_length)
5050
{
@@ -322,7 +322,7 @@ static int handle_forwarding(void *object, const IP_Port *source, const uint8_t
322322
}
323323
}
324324

325-
bool forward_reply(Networking_Core *net, const IP_Port *forwarder,
325+
bool forward_reply(const Networking_Core *net, const IP_Port *forwarder,
326326
const uint8_t *sendback, uint16_t sendback_length,
327327
const uint8_t *data, uint16_t length)
328328
{

toxcore/forwarding.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ DHT *forwarding_get_dht(const Forwarding *forwarding);
4646
* @return true on success, false otherwise.
4747
*/
4848
non_null()
49-
bool send_forward_request(Networking_Core *net, const IP_Port *forwarder,
49+
bool send_forward_request(const Networking_Core *net, const IP_Port *forwarder,
5050
const uint8_t *chain_keys, uint16_t chain_length,
5151
const uint8_t *data, uint16_t data_length);
5252

@@ -80,7 +80,7 @@ bool create_forward_chain_packet(const uint8_t *chain_keys, uint16_t chain_lengt
8080
* @return true on success, false otherwise.
8181
*/
8282
non_null()
83-
bool forward_reply(Networking_Core *net, const IP_Port *forwarder,
83+
bool forward_reply(const Networking_Core *net, const IP_Port *forwarder,
8484
const uint8_t *sendback, uint16_t sendback_length,
8585
const uint8_t *data, uint16_t length);
8686

toxcore/net_profile.c

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* SPDX-License-Identifier: GPL-3.0-or-later
2-
* Copyright © 2023 The TokTok team.
2+
* Copyright © 2023-2024 The TokTok team.
33
*/
44

55
/**
@@ -11,10 +11,27 @@
1111
#include <stdint.h>
1212

1313
#include "attributes.h"
14+
#include "logger.h"
15+
#include "mem.h"
16+
1417
#include "ccompat.h"
1518

1619
#define NETPROF_TCP_DATA_PACKET_ID 0x10
1720

21+
typedef struct Net_Profile {
22+
uint64_t packets_recv[NET_PROF_MAX_PACKET_IDS];
23+
uint64_t packets_sent[NET_PROF_MAX_PACKET_IDS];
24+
25+
uint64_t total_packets_recv;
26+
uint64_t total_packets_sent;
27+
28+
uint64_t bytes_recv[NET_PROF_MAX_PACKET_IDS];
29+
uint64_t bytes_sent[NET_PROF_MAX_PACKET_IDS];
30+
31+
uint64_t total_bytes_recv;
32+
uint64_t total_bytes_sent;
33+
} Net_Profile;
34+
1835
/** Returns the number of sent or received packets for all ID's between `start_id` and `end_id`. */
1936
nullable(1)
2037
static uint64_t netprof_get_packet_count_id_range(const Net_Profile *profile, uint8_t start_id, uint8_t end_id,
@@ -119,3 +136,22 @@ uint64_t netprof_get_bytes_total(const Net_Profile *profile, Packet_Direction di
119136

120137
return dir == PACKET_DIRECTION_SEND ? profile->total_bytes_sent : profile->total_bytes_recv;
121138
}
139+
140+
Net_Profile *netprof_new(const Logger *log, const Memory *mem)
141+
{
142+
Net_Profile *np = (Net_Profile *)mem_alloc(mem, sizeof(Net_Profile));
143+
144+
if (np == nullptr) {
145+
LOGGER_ERROR(log, "failed to allocate memory for net profiler");
146+
return nullptr;
147+
}
148+
149+
return np;
150+
}
151+
152+
void netprof_kill(const Memory *mem, Net_Profile *net_profile)
153+
{
154+
if (net_profile != nullptr) {
155+
mem_delete(mem, net_profile);
156+
}
157+
}

0 commit comments

Comments
 (0)