Skip to content

Commit

Permalink
cleanup: Heap allocate network profile objects
Browse files Browse the repository at this point in the history
  • Loading branch information
JFreegman committed Dec 6, 2024
1 parent de48b20 commit 86652db
Show file tree
Hide file tree
Showing 16 changed files with 136 additions and 65 deletions.
40 changes: 20 additions & 20 deletions auto_tests/netprof_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ static void test_netprof(AutoTox *autotoxes)

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

const unsigned long long UDP_count_sent1 = tox_netprof_get_packet_total_count(tox1, TOX_NETPROF_PACKET_TYPE_UDP,
const uint64_t UDP_count_sent1 = tox_netprof_get_packet_total_count(tox1, TOX_NETPROF_PACKET_TYPE_UDP,
TOX_NETPROF_DIRECTION_SENT);
const unsigned long long UDP_count_recv1 = tox_netprof_get_packet_total_count(tox1, TOX_NETPROF_PACKET_TYPE_UDP,
const uint64_t UDP_count_recv1 = tox_netprof_get_packet_total_count(tox1, TOX_NETPROF_PACKET_TYPE_UDP,
TOX_NETPROF_DIRECTION_RECV);
const unsigned long long TCP_count_sent1 = tox_netprof_get_packet_total_count(tox1, TOX_NETPROF_PACKET_TYPE_TCP,
const uint64_t TCP_count_sent1 = tox_netprof_get_packet_total_count(tox1, TOX_NETPROF_PACKET_TYPE_TCP,
TOX_NETPROF_DIRECTION_SENT);
const unsigned long long TCP_count_recv1 = tox_netprof_get_packet_total_count(tox1, TOX_NETPROF_PACKET_TYPE_TCP,
const uint64_t TCP_count_recv1 = tox_netprof_get_packet_total_count(tox1, TOX_NETPROF_PACKET_TYPE_TCP,
TOX_NETPROF_DIRECTION_RECV);

const unsigned long long UDP_bytes_sent1 = tox_netprof_get_packet_total_bytes(tox1, TOX_NETPROF_PACKET_TYPE_UDP,
const uint64_t UDP_bytes_sent1 = tox_netprof_get_packet_total_bytes(tox1, TOX_NETPROF_PACKET_TYPE_UDP,
TOX_NETPROF_DIRECTION_SENT);
const unsigned long long UDP_bytes_recv1 = tox_netprof_get_packet_total_bytes(tox1, TOX_NETPROF_PACKET_TYPE_UDP,
const uint64_t UDP_bytes_recv1 = tox_netprof_get_packet_total_bytes(tox1, TOX_NETPROF_PACKET_TYPE_UDP,
TOX_NETPROF_DIRECTION_RECV);
const unsigned long long TCP_bytes_sent1 = tox_netprof_get_packet_total_bytes(tox1, TOX_NETPROF_PACKET_TYPE_TCP,
const uint64_t TCP_bytes_sent1 = tox_netprof_get_packet_total_bytes(tox1, TOX_NETPROF_PACKET_TYPE_TCP,
TOX_NETPROF_DIRECTION_SENT);
const unsigned long long TCP_bytes_recv1 = tox_netprof_get_packet_total_bytes(tox1, TOX_NETPROF_PACKET_TYPE_TCP,
const uint64_t TCP_bytes_recv1 = tox_netprof_get_packet_total_bytes(tox1, TOX_NETPROF_PACKET_TYPE_TCP,
TOX_NETPROF_DIRECTION_RECV);

ck_assert(UDP_count_recv1 > 0 && UDP_count_sent1 > 0);
Expand All @@ -57,10 +57,10 @@ static void test_netprof(AutoTox *autotoxes)
(void)TCP_bytes_recv1;
(void)TCP_count_recv1;

unsigned long long total_sent_count = 0;
unsigned long long total_recv_count = 0;
unsigned long long total_sent_bytes = 0;
unsigned long long total_recv_bytes = 0;
uint64_t total_sent_count = 0;
uint64_t total_recv_count = 0;
uint64_t total_sent_bytes = 0;
uint64_t total_recv_bytes = 0;

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

const unsigned long long total_packets = total_sent_count + total_recv_count;
const uint64_t total_packets = total_sent_count + total_recv_count;
ck_assert_msg(total_packets == UDP_count_sent1 + UDP_count_recv1,
"%llu does not match %llu\n", total_packets, UDP_count_sent1 + UDP_count_recv1);
"%lu does not match %lu\n", total_packets, UDP_count_sent1 + UDP_count_recv1);

ck_assert_msg(total_sent_count == UDP_count_sent1, "%llu does not match %llu\n", total_sent_count, UDP_count_sent1);
ck_assert_msg(total_recv_count == UDP_count_recv1, "%llu does not match %llu\n", total_recv_count, UDP_count_recv1);
ck_assert_msg(total_sent_count == UDP_count_sent1, "%lu does not match %lu\n", total_sent_count, UDP_count_sent1);
ck_assert_msg(total_recv_count == UDP_count_recv1, "%lu does not match %lu\n", total_recv_count, UDP_count_recv1);


const unsigned long long total_bytes = total_sent_bytes + total_recv_bytes;
const uint64_t total_bytes = total_sent_bytes + total_recv_bytes;
ck_assert_msg(total_bytes == UDP_bytes_sent1 + UDP_bytes_recv1,
"%llu does not match %llu\n", total_bytes, UDP_bytes_sent1 + UDP_bytes_recv1);
"%lu does not match %lu\n", total_bytes, UDP_bytes_sent1 + UDP_bytes_recv1);

ck_assert_msg(total_sent_bytes == UDP_bytes_sent1, "%llu does not match %llu\n", total_sent_bytes, UDP_bytes_sent1);
ck_assert_msg(total_recv_bytes == UDP_bytes_recv1, "%llu does not match %llu\n", total_recv_bytes, UDP_bytes_recv1);
ck_assert_msg(total_sent_bytes == UDP_bytes_sent1, "%lu does not match %lu\n", total_sent_bytes, UDP_bytes_sent1);
ck_assert_msg(total_recv_bytes == UDP_bytes_recv1, "%lu does not match %lu\n", total_recv_bytes, UDP_bytes_recv1);
}

int main(void)
Expand Down
2 changes: 1 addition & 1 deletion other/bootstrap_node_packets.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static int handle_info_request(void *object, const IP_Port *source, const uint8_
return 1;
}

Networking_Core *nc = (Networking_Core *)object;
const Networking_Core *nc = (const Networking_Core *)object;

uint8_t data[1 + sizeof(bootstrap_version) + MAX_MOTD_LENGTH];
data[0] = BOOTSTRAP_INFO_PACKET_ID;
Expand Down
2 changes: 2 additions & 0 deletions toxcore/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,8 @@ cc_library(
deps = [
":attributes",
":ccompat",
":logger",
":mem",
],
)

Expand Down
4 changes: 2 additions & 2 deletions toxcore/LAN_discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns)
* @retval false on failure to find any valid broadcast target.
*/
non_null()
static bool send_broadcasts(Networking_Core *net, const Broadcast_Info *broadcast, uint16_t port,
static bool send_broadcasts(const Networking_Core *net, const Broadcast_Info *broadcast, uint16_t port,
const uint8_t *data, uint16_t length)
{
if (broadcast->count == 0) {
Expand Down Expand Up @@ -339,7 +339,7 @@ bool ip_is_lan(const IP *ip)
return false;
}

bool lan_discovery_send(Networking_Core *net, const Broadcast_Info *broadcast, const uint8_t *dht_pk,
bool lan_discovery_send(const Networking_Core *net, const Broadcast_Info *broadcast, const uint8_t *dht_pk,
uint16_t port)
{
if (broadcast == nullptr) {
Expand Down
2 changes: 1 addition & 1 deletion toxcore/LAN_discovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ typedef struct Broadcast_Info Broadcast_Info;
* @return true on success, false on failure.
*/
non_null()
bool lan_discovery_send(Networking_Core *net, const Broadcast_Info *broadcast, const uint8_t *dht_pk,
bool lan_discovery_send(const Networking_Core *net, const Broadcast_Info *broadcast, const uint8_t *dht_pk,
uint16_t port);

/**
Expand Down
19 changes: 14 additions & 5 deletions toxcore/TCP_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ struct TCP_Connections {
uint16_t onion_num_conns;

/* Network profile for all TCP client packets. */
Net_Profile net_profile;
Net_Profile *net_profile;
};

static const TCP_Connection_to empty_tcp_connection_to = {0};
Expand Down Expand Up @@ -933,7 +933,7 @@ static int reconnect_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connec
memcpy(relay_pk, tcp_con_public_key(tcp_con->connection), CRYPTO_PUBLIC_KEY_SIZE);
kill_tcp_connection(tcp_con->connection);
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,
&tcp_c->net_profile);
tcp_c->net_profile);

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

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

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

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

if (tcp_con->connection == nullptr) {
return -1;
Expand Down Expand Up @@ -1614,6 +1614,14 @@ 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->logger = logger;
temp->mem = mem;
temp->rng = rng;
Expand Down Expand Up @@ -1728,6 +1736,7 @@ 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);
Expand All @@ -1739,5 +1748,5 @@ const Net_Profile *tcp_connection_get_client_net_profile(const TCP_Connections *
return nullptr;
}

return &tcp_c->net_profile;
return tcp_c->net_profile;
}
18 changes: 15 additions & 3 deletions toxcore/TCP_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ struct TCP_Server {
BS_List accepted_key_list;

/* Network profile for all TCP server packets. */
Net_Profile net_profile;
Net_Profile *net_profile;
};

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

return index;
}
Expand Down Expand Up @@ -975,6 +975,14 @@ TCP_Server *new_tcp_server(const Logger *logger, const Memory *mem, const Random
return nullptr;
}

Net_Profile *np = netprof_new(logger, mem);

if (np == nullptr) {
mem_delete(mem, temp);
return nullptr;
}

temp->net_profile = np;
temp->logger = logger;
temp->mem = mem;
temp->ns = ns;
Expand All @@ -984,6 +992,7 @@ TCP_Server *new_tcp_server(const Logger *logger, const Memory *mem, const Random

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

if (temp->efd == -1) {
LOGGER_ERROR(logger, "epoll initialisation failed");
netprof_kill(mem, temp->net_profile);
mem_delete(mem, socks_listening);
mem_delete(mem, temp);
return nullptr;
Expand Down Expand Up @@ -1028,6 +1038,7 @@ TCP_Server *new_tcp_server(const Logger *logger, const Memory *mem, const Random
}

if (temp->num_listening_socks == 0) {
netprof_kill(mem, temp->net_profile);
mem_delete(mem, temp->socks_listening);
mem_delete(mem, temp);
return nullptr;
Expand Down Expand Up @@ -1428,6 +1439,7 @@ void kill_tcp_server(TCP_Server *tcp_server)

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

netprof_kill(tcp_server->mem, tcp_server->net_profile);
mem_delete(tcp_server->mem, tcp_server->socks_listening);
mem_delete(tcp_server->mem, tcp_server);
}
Expand All @@ -1438,5 +1450,5 @@ const Net_Profile *tcp_server_get_net_profile(const TCP_Server *tcp_server)
return nullptr;
}

return &tcp_server->net_profile;
return tcp_server->net_profile;
}
4 changes: 2 additions & 2 deletions toxcore/forwarding.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ DHT *forwarding_get_dht(const Forwarding *forwarding)

#define SENDBACK_TIMEOUT 3600

bool send_forward_request(Networking_Core *net, const IP_Port *forwarder,
bool send_forward_request(const Networking_Core *net, const IP_Port *forwarder,
const uint8_t *chain_keys, uint16_t chain_length,
const uint8_t *data, uint16_t data_length)
{
Expand Down Expand Up @@ -321,7 +321,7 @@ static int handle_forwarding(void *object, const IP_Port *source, const uint8_t
}
}

bool forward_reply(Networking_Core *net, const IP_Port *forwarder,
bool forward_reply(const Networking_Core *net, const IP_Port *forwarder,
const uint8_t *sendback, uint16_t sendback_length,
const uint8_t *data, uint16_t length)
{
Expand Down
4 changes: 2 additions & 2 deletions toxcore/forwarding.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ DHT *forwarding_get_dht(const Forwarding *forwarding);
* @return true on success, false otherwise.
*/
non_null()
bool send_forward_request(Networking_Core *net, const IP_Port *forwarder,
bool send_forward_request(const Networking_Core *net, const IP_Port *forwarder,
const uint8_t *chain_keys, uint16_t chain_length,
const uint8_t *data, uint16_t data_length);

Expand Down Expand Up @@ -79,7 +79,7 @@ bool create_forward_chain_packet(const uint8_t *chain_keys, uint16_t chain_lengt
* @return true on success, false otherwise.
*/
non_null()
bool forward_reply(Networking_Core *net, const IP_Port *forwarder,
bool forward_reply(const Networking_Core *net, const IP_Port *forwarder,
const uint8_t *sendback, uint16_t sendback_length,
const uint8_t *data, uint16_t length);

Expand Down
38 changes: 37 additions & 1 deletion toxcore/net_profile.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2023 The TokTok team.
* Copyright © 2023-2024 The TokTok team.
*/

/**
Expand All @@ -11,10 +11,27 @@
#include <stdint.h>

#include "attributes.h"
#include "logger.h"
#include "mem.h"

#include "ccompat.h"

#define NETPROF_TCP_DATA_PACKET_ID 0x10

typedef struct Net_Profile {
uint64_t packets_recv[NET_PROF_MAX_PACKET_IDS];
uint64_t packets_sent[NET_PROF_MAX_PACKET_IDS];

uint64_t total_packets_recv;
uint64_t total_packets_sent;

uint64_t bytes_recv[NET_PROF_MAX_PACKET_IDS];
uint64_t bytes_sent[NET_PROF_MAX_PACKET_IDS];

uint64_t total_bytes_recv;
uint64_t total_bytes_sent;
} Net_Profile;

/** Returns the number of sent or received packets for all ID's between `start_id` and `end_id`. */
nullable(1)
static uint64_t netprof_get_packet_count_id_range(const Net_Profile *profile, uint8_t start_id, uint8_t end_id,
Expand Down Expand Up @@ -119,3 +136,22 @@ uint64_t netprof_get_bytes_total(const Net_Profile *profile, Packet_Direction di

return dir == PACKET_DIRECTION_SEND ? profile->total_bytes_sent : profile->total_bytes_recv;
}

Net_Profile *netprof_new(const Logger *log, const Memory *mem)
{
Net_Profile *np = (Net_Profile *)mem_alloc(mem, sizeof(Net_Profile));

if (np == nullptr) {
LOGGER_ERROR(log, "failed to allocate memory for net profiler");
return nullptr;
}

return np;
}

void netprof_kill(const Memory *mem, Net_Profile *net_profile)
{
if (net_profile != nullptr) {
mem_delete(mem, net_profile);
}
}
Loading

0 comments on commit 86652db

Please sign in to comment.