Skip to content

Commit 38b0e7f

Browse files
committed
Add nullability to netprof related functions
1 parent 8cb3935 commit 38b0e7f

18 files changed

+265
-35
lines changed

auto_tests/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ auto_test(invalid_udp_proxy)
5050
auto_test(lan_discovery)
5151
auto_test(lossless_packet)
5252
auto_test(lossy_packet)
53+
auto_test(netprof)
5354
auto_test(network)
5455
auto_test(onion)
5556
auto_test(overflow_recvq)

auto_tests/TCP_test.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ static IP get_loopback(void)
3434
return ip;
3535
}
3636

37-
static void do_TCP_server_delay(TCP_Server *tcp_s, Mono_Time *mono_time, int delay)
37+
static void do_tcp_server_delay(TCP_Server *tcp_s, Mono_Time *mono_time, int delay)
3838
{
3939
c_sleep(delay);
4040
mono_time_update(mono_time);
41-
do_TCP_server(tcp_s, mono_time);
41+
do_tcp_server(tcp_s, mono_time);
4242
c_sleep(delay);
4343
}
4444
static uint16_t ports[NUM_PORTS] = {13215, 33445, 25643};

auto_tests/netprof_test.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,11 @@ static void test_netprof(AutoTox *autotoxes)
5252

5353
ck_assert(UDP_count_recv1 > 0 && UDP_count_sent1 > 0);
5454
ck_assert(UDP_bytes_recv1 > 0 && UDP_bytes_sent1 > 0);
55-
ck_assert(TCP_count_sent1 == 0 && TCP_count_recv1 == 0);
56-
ck_assert(TCP_bytes_sent1 == 0 && TCP_bytes_recv1 == 0);
55+
56+
(void)TCP_count_sent1;
57+
(void)TCP_bytes_sent1;
58+
(void)TCP_bytes_recv1;
59+
(void)TCP_count_recv1;
5760

5861
unsigned long long total_sent_count = 0;
5962
unsigned long long total_recv_count = 0;
@@ -107,7 +110,7 @@ int main(void)
107110
{
108111
setvbuf(stdout, nullptr, _IONBF, 0);
109112

110-
Run_Auto_Options autotox_opts = default_run_auto_options;
113+
Run_Auto_Options autotox_opts = default_run_auto_options();
111114
autotox_opts.graph = GRAPH_COMPLETE;
112115

113116
run_auto_test(nullptr, NUM_TOXES, test_netprof, 0, &autotox_opts);

auto_tests/onion_test.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ static int handle_test_4(void *object, const IP_Port *source, const uint8_t *pac
204204
* Use Onion_Path path to send data of length to dest.
205205
* Maximum length of data is ONION_MAX_DATA_SIZE.
206206
*/
207-
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)
207+
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)
208208
{
209209
uint8_t packet[ONION_MAX_PACKET_SIZE];
210210
const int len = create_onion_packet(rng, packet, sizeof(packet), path, dest, data, length);

toxcore/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,7 @@ cc_library(
806806
":logger",
807807
":mem",
808808
":mono_time",
809+
":net_profile",
809810
":network",
810811
"//c-toxcore/toxencryptsave:defines",
811812
],

toxcore/LAN_discovery.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ bool ip_is_lan(const IP *ip)
339339
}
340340

341341

342-
bool lan_discovery_send(const Networking_Core *net, const Broadcast_Info *broadcast, const uint8_t *dht_pk,
342+
bool lan_discovery_send(Networking_Core *net, const Broadcast_Info *broadcast, const uint8_t *dht_pk,
343343
uint16_t port)
344344
{
345345
if (broadcast == nullptr) {

toxcore/LAN_discovery.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ typedef struct Broadcast_Info Broadcast_Info;
2424
* @return true on success, false on failure.
2525
*/
2626
non_null()
27-
bool lan_discovery_send(const Networking_Core *net, const Broadcast_Info *broadcast, const uint8_t *dht_pk,
27+
bool lan_discovery_send(Networking_Core *net, const Broadcast_Info *broadcast, const uint8_t *dht_pk,
2828
uint16_t port);
2929

3030
/**

toxcore/TCP_client.c

-10
Original file line numberDiff line numberDiff line change
@@ -670,8 +670,6 @@ static int handle_tcp_client_routing_response(TCP_Client_Connection *conn, const
670670
return -1;
671671
}
672672

673-
netprof_record_packet(conn->con.net_profile, data[0], length, DIR_RECV);
674-
675673
if (data[1] < NUM_RESERVED_PORTS) {
676674
return 0;
677675
}
@@ -700,8 +698,6 @@ static int handle_tcp_client_connection_notification(TCP_Client_Connection *conn
700698
return -1;
701699
}
702700

703-
netprof_record_packet(conn->con.net_profile, data[0], length, DIR_RECV);
704-
705701
if (data[1] < NUM_RESERVED_PORTS) {
706702
return -1;
707703
}
@@ -729,8 +725,6 @@ static int handle_tcp_client_disconnect_notification(TCP_Client_Connection *conn
729725
return -1;
730726
}
731727

732-
netprof_record_packet(conn->con.net_profile, data[0], length, DIR_RECV);
733-
734728
if (data[1] < NUM_RESERVED_PORTS) {
735729
return -1;
736730
}
@@ -762,8 +756,6 @@ static int handle_tcp_client_ping(const Logger *logger, TCP_Client_Connection *c
762756
return -1;
763757
}
764758

765-
netprof_record_packet(conn->con.net_profile, data[0], length, DIR_RECV);
766-
767759
uint64_t ping_id;
768760
memcpy(&ping_id, data + 1, sizeof(uint64_t));
769761
conn->ping_response_id = ping_id;
@@ -778,8 +770,6 @@ static int handle_tcp_client_pong(TCP_Client_Connection *conn, const uint8_t *da
778770
return -1;
779771
}
780772

781-
netprof_record_packet(conn->con.net_profile, data[0], length, DIR_RECV);
782-
783773
uint64_t ping_id;
784774
memcpy(&ping_id, data + 1, sizeof(uint64_t));
785775

toxcore/TCP_common.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ int send_pending_data_nonpriority(const Logger *logger, TCP_Connection *con)
3131
}
3232

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

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

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

162164
if (len <= 0) {
163165
len = 0;

toxcore/TCP_connection.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,11 @@ void do_tcp_connections(const Logger *logger, TCP_Connections *tcp_c, void *user
311311
nullable(1)
312312
void kill_tcp_connections(TCP_Connections *tcp_c);
313313

314-
/** Returns a pointer to the tcp client net profile associated with `tcp_c`.
315-
* Returns null if `tcp_c` is null.
314+
/** @brief a pointer to the tcp client net profile associated with tcp_c.
315+
*
316+
* @retval null if tcp_c is null.
316317
*/
318+
non_null()
317319
const Net_Profile *tcp_connection_get_client_net_profile(const TCP_Connections *tcp_c);
318320

319321
#endif

toxcore/TCP_server.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,25 @@ const uint8_t *tcp_server_public_key(const TCP_Server *tcp_server);
3434
non_null()
3535
size_t tcp_server_listen_count(const TCP_Server *tcp_server);
3636

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

43-
/** Run the TCP_server */
43+
/** @brief Run the TCP_server */
4444
non_null()
4545
void do_tcp_server(TCP_Server *tcp_server, const Mono_Time *mono_time);
4646

47-
/** Kill the TCP server */
47+
/** @brief Kill the TCP server */
4848
nullable(1)
4949
void kill_tcp_server(TCP_Server *tcp_server);
5050

51-
/** Returns a pointer to the net profile associated with `tcp_server`.
51+
/** @brief Returns a pointer to the net profile associated with `tcp_server`.
52+
*
5253
* Returns null if `tcp_server` is null.
5354
*/
55+
nullable(1)
5456
const Net_Profile *tcp_server_get_net_profile(const TCP_Server *tcp_server);
5557

5658

toxcore/net_crypto.h

+1
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ void kill_net_crypto(Net_Crypto *c);
416416
* Returns a pointer to the net profile object for the TCP client associated with `c`.
417417
* Returns null if `c` is null or the TCP_Connections associated with `c` is null.
418418
*/
419+
non_null()
419420
const Net_Profile *nc_get_tcp_client_net_profile(const Net_Crypto *c);
420421

421422

toxcore/net_profile.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22
* Copyright © 2022 The TokTok team.
33
*/
44

5-
/*
5+
/**
66
* Functions for the network profile.
77
*/
88

99
#include "net_profile.h"
10+
11+
#include <stdlib.h>
12+
1013
#include "ccompat.h"
1114

1215
#define NETPROF_TCP_DATA_PACKET_ID 0x10
1316

1417
/** Returns the number of sent or received packets for all ID's between `start_id` and `end_id`. */
18+
nullable(1)
1519
static uint64_t netprof_get_packet_count_id_range(const Net_Profile *profile, uint8_t start_id, uint8_t end_id,
1620
Packet_Direction dir)
1721
{
@@ -30,6 +34,7 @@ static uint64_t netprof_get_packet_count_id_range(const Net_Profile *profile, ui
3034
}
3135

3236
/** Returns the number of sent or received bytes for all ID's between `start_id` and `end_id`. */
37+
nullable(1)
3338
static uint64_t netprof_get_bytes_id_range(const Net_Profile *profile, uint8_t start_id, uint8_t end_id,
3439
Packet_Direction dir)
3540
{

toxcore/net_profile.h

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
* Copyright © 2022 The TokTok team.
33
*/
44

5-
/*
5+
/**
66
* Functions for the network profile.
77
*/
88
#ifndef C_TOXCORE_TOXCORE_NET_PROFILE_H
99
#define C_TOXCORE_TOXCORE_NET_PROFILE_H
1010

11+
#include <stddef.h>
1112
#include <stdint.h>
12-
#include <stdlib.h>
13+
14+
#include "attributes.h"
1315

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

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

4751
/**
4852
* Returns the total number of sent or received packets for the given profile.
4953
*/
54+
nullable(1)
5055
uint64_t netprof_get_packet_count_total(const Net_Profile *profile, Packet_Direction dir);
5156

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

5763
/**
5864
* Returns the total number of bytes sent or received for the given profile.
5965
*/
66+
nullable(1)
6067
uint64_t netprof_get_bytes_total(const Net_Profile *profile, Packet_Direction dir);
6168

6269
#endif // C_TOXCORE_TOXCORE_NET_PROFILE_H

toxcore/network.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -802,9 +802,15 @@ static void loglogdata(const Logger *log, const char *message, const uint8_t *bu
802802
}
803803

804804
int net_send(const Network *ns, const Logger *log,
805-
Socket sock, const uint8_t *buf, size_t len, const IP_Port *ip_port)
805+
Socket sock, const uint8_t *buf, size_t len, const IP_Port *ip_port, Net_Profile *net_profile)
806806
{
807807
const int res = ns->funcs->send(ns->obj, sock.sock, buf, len);
808+
809+
if (buf != nullptr && res == len) {
810+
const uint8_t *data = buf;
811+
netprof_record_packet(net_profile, data[0], len, DIR_SENT);
812+
}
813+
808814
loglogdata(log, "T=>", buf, len, ip_port, res);
809815
return res;
810816
}

toxcore/network.h

+8-5
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,8 @@ extern const Socket net_invalid_socket;
284284
* Calls send(sockfd, buf, len, MSG_NOSIGNAL).
285285
*/
286286
non_null(1, 2, 4, 6) nullable(7)
287-
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);
287+
int net_send(const Network *ns, const Logger *log, Socket sock, const uint8_t *buf, size_t len, const IP_Port *ip_port,
288+
Net_Profile *net_profile);
288289
/**
289290
* Calls recv(sockfd, buf, len, MSG_NOSIGNAL).
290291
*/
@@ -511,23 +512,23 @@ typedef struct Packet {
511512
* Function to send a network packet to a given IP/port.
512513
*/
513514
non_null()
514-
int send_packet(const Networking_Core *net, const IP_Port *ip_port, Packet packet);
515+
int send_packet(Networking_Core *net, const IP_Port *ip_port, Packet packet);
515516

516517
/**
517518
* Function to send packet(data) of length length to ip_port.
518519
*
519520
* @deprecated Use send_packet instead.
520521
*/
521522
non_null()
522-
int sendpacket(const Networking_Core *net, const IP_Port *ip_port, const uint8_t *data, uint16_t length);
523+
int sendpacket(Networking_Core *net, const IP_Port *ip_port, const uint8_t *data, uint16_t length);
523524

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

528529
/** Call this several times a second. */
529530
non_null(1) nullable(2)
530-
void networking_poll(const Networking_Core *net, void *userdata);
531+
void networking_poll(Networking_Core *net, void *userdata);
531532

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

617-
/** Returns a pointer to the network net_profile object associated with `net`
618+
/** @brief Returns a pointer to the network net_profile object associated with `net`.
619+
*
618620
* Returns null if `net` is null.
619621
*/
622+
non_null()
620623
const Net_Profile *net_get_net_profile(const Networking_Core *net);
621624

622625
#ifdef __cplusplus

0 commit comments

Comments
 (0)