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 Nov 28, 2024
1 parent 35b60cb commit dce3d07
Show file tree
Hide file tree
Showing 17 changed files with 119 additions and 48 deletions.
2 changes: 1 addition & 1 deletion auto_tests/onion_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ static int handle_test_4(void *object, const IP_Port *source, const uint8_t *pac
* Use Onion_Path path to send data of length to dest.
* Maximum length of data is ONION_MAX_DATA_SIZE.
*/
static void send_onion_packet(Networking_Core *net, const Random *rng, const Onion_Path *path, const IP_Port *dest, const uint8_t *data, uint16_t length)
static void send_onion_packet(const Networking_Core *net, const Random *rng, const Onion_Path *path, const IP_Port *dest, const uint8_t *data, uint16_t length)
{
uint8_t packet[ONION_MAX_PACKET_SIZE];
const int len = create_onion_packet(rng, packet, sizeof(packet), path, dest, data, length);
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 = (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 @@ -307,6 +307,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;
}
17 changes: 14 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 @@ -1438,5 +1449,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);
}
}
31 changes: 17 additions & 14 deletions toxcore/net_profile.h
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 @@ -12,23 +12,13 @@
#include <stdint.h>

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

/* The max number of packet ID's (must fit inside one byte) */
#define NET_PROF_MAX_PACKET_IDS 256

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;
typedef struct Net_Profile Net_Profile;

/** Specifies whether the query is for sent or received packets. */
typedef enum Packet_Direction {
Expand Down Expand Up @@ -66,4 +56,17 @@ uint64_t netprof_get_bytes_id(const Net_Profile *profile, uint8_t id, Packet_Dir
nullable(1)
uint64_t netprof_get_bytes_total(const Net_Profile *profile, Packet_Direction dir);

/**
* Returns a new net_profile object. The caller is responsible for freeing the
* returned memory via `netprof_kill`.
*/
non_null()
Net_Profile *netprof_new(const Logger *log, const Memory *mem);

/**
* Kills a net_profile object and frees all associated memory.
*/
nullable(2)
void netprof_kill(const Memory *mem, Net_Profile *net_profile);

#endif /* C_TOXCORE_TOXCORE_NET_PROFILE_H */
Loading

0 comments on commit dce3d07

Please sign in to comment.