Skip to content

Commit

Permalink
Add nullability to netprof related functions
Browse files Browse the repository at this point in the history
  • Loading branch information
JFreegman committed Nov 23, 2023
1 parent 8cb3935 commit 38b0e7f
Show file tree
Hide file tree
Showing 18 changed files with 265 additions and 35 deletions.
1 change: 1 addition & 0 deletions auto_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ auto_test(invalid_udp_proxy)
auto_test(lan_discovery)
auto_test(lossless_packet)
auto_test(lossy_packet)
auto_test(netprof)
auto_test(network)
auto_test(onion)
auto_test(overflow_recvq)
Expand Down
4 changes: 2 additions & 2 deletions auto_tests/TCP_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ static IP get_loopback(void)
return ip;
}

static void do_TCP_server_delay(TCP_Server *tcp_s, Mono_Time *mono_time, int delay)
static void do_tcp_server_delay(TCP_Server *tcp_s, Mono_Time *mono_time, int delay)
{
c_sleep(delay);
mono_time_update(mono_time);
do_TCP_server(tcp_s, mono_time);
do_tcp_server(tcp_s, mono_time);
c_sleep(delay);
}
static uint16_t ports[NUM_PORTS] = {13215, 33445, 25643};
Expand Down
9 changes: 6 additions & 3 deletions auto_tests/netprof_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ static void test_netprof(AutoTox *autotoxes)

ck_assert(UDP_count_recv1 > 0 && UDP_count_sent1 > 0);

Check warning on line 53 in auto_tests/netprof_test.c

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

auto_tests/netprof_test.c#L53

MISRA 21.8 rule
ck_assert(UDP_bytes_recv1 > 0 && UDP_bytes_sent1 > 0);

Check warning on line 54 in auto_tests/netprof_test.c

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

auto_tests/netprof_test.c#L54

MISRA 21.8 rule
ck_assert(TCP_count_sent1 == 0 && TCP_count_recv1 == 0);
ck_assert(TCP_bytes_sent1 == 0 && TCP_bytes_recv1 == 0);

(void)TCP_count_sent1;
(void)TCP_bytes_sent1;
(void)TCP_bytes_recv1;
(void)TCP_count_recv1;

unsigned long long total_sent_count = 0;
unsigned long long total_recv_count = 0;
Expand Down Expand Up @@ -107,7 +110,7 @@ int main(void)
{
setvbuf(stdout, nullptr, _IONBF, 0);

Run_Auto_Options autotox_opts = default_run_auto_options;
Run_Auto_Options autotox_opts = default_run_auto_options();
autotox_opts.graph = GRAPH_COMPLETE;

run_auto_test(nullptr, NUM_TOXES, test_netprof, 0, &autotox_opts);
Expand Down
2 changes: 1 addition & 1 deletion auto_tests/onion_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,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(const 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(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
1 change: 1 addition & 0 deletions toxcore/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,7 @@ cc_library(
":logger",
":mem",
":mono_time",
":net_profile",
":network",
"//c-toxcore/toxencryptsave:defines",
],
Expand Down
2 changes: 1 addition & 1 deletion toxcore/LAN_discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ bool ip_is_lan(const IP *ip)
}


bool lan_discovery_send(const Networking_Core *net, const Broadcast_Info *broadcast, const uint8_t *dht_pk,
bool lan_discovery_send(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 @@ -24,7 +24,7 @@ typedef struct Broadcast_Info Broadcast_Info;
* @return true on success, false on failure.
*/
non_null()
bool lan_discovery_send(const Networking_Core *net, const Broadcast_Info *broadcast, const uint8_t *dht_pk,
bool lan_discovery_send(Networking_Core *net, const Broadcast_Info *broadcast, const uint8_t *dht_pk,
uint16_t port);

/**
Expand Down
10 changes: 0 additions & 10 deletions toxcore/TCP_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -670,8 +670,6 @@ static int handle_tcp_client_routing_response(TCP_Client_Connection *conn, const
return -1;
}

netprof_record_packet(conn->con.net_profile, data[0], length, DIR_RECV);

if (data[1] < NUM_RESERVED_PORTS) {
return 0;
}
Expand Down Expand Up @@ -700,8 +698,6 @@ static int handle_tcp_client_connection_notification(TCP_Client_Connection *conn
return -1;
}

netprof_record_packet(conn->con.net_profile, data[0], length, DIR_RECV);

if (data[1] < NUM_RESERVED_PORTS) {
return -1;
}
Expand Down Expand Up @@ -729,8 +725,6 @@ static int handle_tcp_client_disconnect_notification(TCP_Client_Connection *conn
return -1;
}

netprof_record_packet(conn->con.net_profile, data[0], length, DIR_RECV);

if (data[1] < NUM_RESERVED_PORTS) {
return -1;
}
Expand Down Expand Up @@ -762,8 +756,6 @@ static int handle_tcp_client_ping(const Logger *logger, TCP_Client_Connection *c
return -1;
}

netprof_record_packet(conn->con.net_profile, data[0], length, DIR_RECV);

uint64_t ping_id;
memcpy(&ping_id, data + 1, sizeof(uint64_t));
conn->ping_response_id = ping_id;
Expand All @@ -778,8 +770,6 @@ static int handle_tcp_client_pong(TCP_Client_Connection *conn, const uint8_t *da
return -1;
}

netprof_record_packet(conn->con.net_profile, data[0], length, DIR_RECV);

uint64_t ping_id;
memcpy(&ping_id, data + 1, sizeof(uint64_t));

Expand Down
6 changes: 4 additions & 2 deletions toxcore/TCP_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ int send_pending_data_nonpriority(const Logger *logger, TCP_Connection *con)
}

const uint16_t left = con->last_packet_length - con->last_packet_sent;
const int len = net_send(con->ns, logger, con->sock, con->last_packet + con->last_packet_sent, left, &con->ip_port, con->net_profile);
const int len = net_send(con->ns, logger, con->sock, con->last_packet + con->last_packet_sent, left, &con->ip_port,
con->net_profile);

if (len <= 0) {
return -1;
Expand Down Expand Up @@ -157,7 +158,8 @@ int write_packet_tcp_secure_connection(const Logger *logger, TCP_Connection *con
}

if (priority) {
len = sendpriority ? net_send(con->ns, logger, con->sock, packet, SIZEOF_VLA(packet), &con->ip_port, con->net_profile) : 0;
len = sendpriority ? net_send(con->ns, logger, con->sock, packet, SIZEOF_VLA(packet), &con->ip_port,
con->net_profile) : 0;

if (len <= 0) {
len = 0;
Expand Down
6 changes: 4 additions & 2 deletions toxcore/TCP_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,11 @@ void do_tcp_connections(const Logger *logger, TCP_Connections *tcp_c, void *user
nullable(1)
void kill_tcp_connections(TCP_Connections *tcp_c);

/** Returns a pointer to the tcp client net profile associated with `tcp_c`.
* Returns null if `tcp_c` is null.
/** @brief a pointer to the tcp client net profile associated with tcp_c.
*
* @retval null if tcp_c is null.
*/
non_null()
const Net_Profile *tcp_connection_get_client_net_profile(const TCP_Connections *tcp_c);

#endif
10 changes: 6 additions & 4 deletions toxcore/TCP_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,25 @@ const uint8_t *tcp_server_public_key(const TCP_Server *tcp_server);
non_null()
size_t tcp_server_listen_count(const TCP_Server *tcp_server);

/** Create new TCP server instance. */
/** @brief Create new TCP server instance. */
non_null(1, 2, 3, 4, 7, 8) nullable(9, 10)
TCP_Server *new_tcp_server(const Logger *logger, const Memory *mem, const Random *rng, const Network *ns,
bool ipv6_enabled, uint16_t num_sockets, const uint16_t *ports,
const uint8_t *secret_key, Onion *onion, Forwarding *forwarding);

/** Run the TCP_server */
/** @brief Run the TCP_server */
non_null()
void do_tcp_server(TCP_Server *tcp_server, const Mono_Time *mono_time);

/** Kill the TCP server */
/** @brief Kill the TCP server */
nullable(1)
void kill_tcp_server(TCP_Server *tcp_server);

/** Returns a pointer to the net profile associated with `tcp_server`.
/** @brief Returns a pointer to the net profile associated with `tcp_server`.
*
* Returns null if `tcp_server` is null.
*/
nullable(1)
const Net_Profile *tcp_server_get_net_profile(const TCP_Server *tcp_server);


Expand Down
1 change: 1 addition & 0 deletions toxcore/net_crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ void kill_net_crypto(Net_Crypto *c);
* Returns a pointer to the net profile object for the TCP client associated with `c`.
* Returns null if `c` is null or the TCP_Connections associated with `c` is null.
*/
non_null()
const Net_Profile *nc_get_tcp_client_net_profile(const Net_Crypto *c);


Expand Down
7 changes: 6 additions & 1 deletion toxcore/net_profile.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@
* Copyright © 2022 The TokTok team.
*/

/*
/**
* Functions for the network profile.
*/

#include "net_profile.h"

#include <stdlib.h>

#include "ccompat.h"

#define NETPROF_TCP_DATA_PACKET_ID 0x10

/** 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,
Packet_Direction dir)
{
Expand All @@ -30,6 +34,7 @@ static uint64_t netprof_get_packet_count_id_range(const Net_Profile *profile, ui
}

/** Returns the number of sent or received bytes for all ID's between `start_id` and `end_id`. */
nullable(1)
static uint64_t netprof_get_bytes_id_range(const Net_Profile *profile, uint8_t start_id, uint8_t end_id,
Packet_Direction dir)
{
Expand Down
11 changes: 9 additions & 2 deletions toxcore/net_profile.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
* Copyright © 2022 The TokTok team.
*/

/*
/**
* Functions for the network profile.
*/
#ifndef C_TOXCORE_TOXCORE_NET_PROFILE_H
#define C_TOXCORE_TOXCORE_NET_PROFILE_H

#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>

#include "attributes.h"

/* The max number of packet ID's (should always be >= 256) */
#define NET_PROF_MAX_PACKET_IDS 256
Expand Down Expand Up @@ -37,26 +39,31 @@ typedef enum Packet_Direction {
/**
* Records a sent or received packet of type `id` and size `length` to the given profile.
*/
nullable(1)
void netprof_record_packet(Net_Profile *profile, uint8_t id, size_t length, Packet_Direction dir);

/**
* Returns the number of sent or received packets of type `id` for the given profile.
*/
nullable(1)
uint64_t netprof_get_packet_count_id(const Net_Profile *profile, uint8_t id, Packet_Direction dir);

/**
* Returns the total number of sent or received packets for the given profile.
*/
nullable(1)
uint64_t netprof_get_packet_count_total(const Net_Profile *profile, Packet_Direction dir);

/**
* Returns the number of bytes sent or received of packet type `id` for the given profile.
*/
nullable(1)
uint64_t netprof_get_bytes_id(const Net_Profile *profile, uint8_t id, Packet_Direction dir);

/**
* Returns the total number of bytes sent or received for the given profile.
*/
nullable(1)
uint64_t netprof_get_bytes_total(const Net_Profile *profile, Packet_Direction dir);

#endif // C_TOXCORE_TOXCORE_NET_PROFILE_H
8 changes: 7 additions & 1 deletion toxcore/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -802,9 +802,15 @@ static void loglogdata(const Logger *log, const char *message, const uint8_t *bu
}

int net_send(const Network *ns, const Logger *log,
Socket sock, const uint8_t *buf, size_t len, const IP_Port *ip_port)
Socket sock, const uint8_t *buf, size_t len, const IP_Port *ip_port, Net_Profile *net_profile)
{
const int res = ns->funcs->send(ns->obj, sock.sock, buf, len);

if (buf != nullptr && res == len) {
const uint8_t *data = buf;
netprof_record_packet(net_profile, data[0], len, DIR_SENT);
}

loglogdata(log, "T=>", buf, len, ip_port, res);
return res;
}
Expand Down
13 changes: 8 additions & 5 deletions toxcore/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ extern const Socket net_invalid_socket;
* Calls send(sockfd, buf, len, MSG_NOSIGNAL).
*/
non_null(1, 2, 4, 6) nullable(7)
int net_send(const Network *ns, const Logger *log, Socket sock, const uint8_t *buf, size_t len, const IP_Port *ip_port, Net_Profile *net_profile);
int net_send(const Network *ns, const Logger *log, Socket sock, const uint8_t *buf, size_t len, const IP_Port *ip_port,
Net_Profile *net_profile);
/**
* Calls recv(sockfd, buf, len, MSG_NOSIGNAL).
*/
Expand Down Expand Up @@ -511,23 +512,23 @@ typedef struct Packet {
* Function to send a network packet to a given IP/port.
*/
non_null()
int send_packet(const Networking_Core *net, const IP_Port *ip_port, Packet packet);
int send_packet(Networking_Core *net, const IP_Port *ip_port, Packet packet);

/**
* Function to send packet(data) of length length to ip_port.
*
* @deprecated Use send_packet instead.
*/
non_null()
int sendpacket(const Networking_Core *net, const IP_Port *ip_port, const uint8_t *data, uint16_t length);
int sendpacket(Networking_Core *net, const IP_Port *ip_port, const uint8_t *data, uint16_t length);

/** Function to call when packet beginning with byte is received. */
non_null(1) nullable(3, 4)
void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handler_cb *cb, void *object);

/** Call this several times a second. */
non_null(1) nullable(2)
void networking_poll(const Networking_Core *net, void *userdata);
void networking_poll(Networking_Core *net, void *userdata);

/** @brief Connect a socket to the address specified by the ip_port.
*
Expand Down Expand Up @@ -614,9 +615,11 @@ Networking_Core *new_networking_no_udp(const Logger *log, const Memory *mem, con
nullable(1)
void kill_networking(Networking_Core *net);

/** Returns a pointer to the network net_profile object associated with `net`
/** @brief Returns a pointer to the network net_profile object associated with `net`.
*
* Returns null if `net` is null.
*/
non_null()
const Net_Profile *net_get_net_profile(const Networking_Core *net);

#ifdef __cplusplus
Expand Down
Loading

0 comments on commit 38b0e7f

Please sign in to comment.