Squashed 'external/toxcore/c-toxcore/' changes from 1828c5356..c9cdae001

c9cdae001 fix(toxav): remove extra copy of video frame on encode
4f6d4546b test: Improve the fake network library.
a2581e700 refactor(toxcore): generate `Friend_Request` and `Dht_Nodes_Response`
2aaa11770 refactor(toxcore): use Tox_Memory in generated events
5c367452b test(toxcore): fix incorrect mutex in tox_scenario_get_time
8f92e710f perf: Add a timed limit of number of cookie requests.
695b6417a test: Add some more simulated network support.
815ae9ce9 test(toxcore): fix thread-safety in scenario framework
6d85c754e test(toxcore): add unit tests for net_crypto
9c22e79cc test(support): add SimulatedEnvironment for deterministic testing
f34fcb195 chore: Update windows Dockerfile to debian stable (trixie).
ece0e8980 fix(group_moderation): allow validating unsorted sanction list signatures
a4fa754d7 refactor: rename struct Packet to struct Net_Packet
d6f330f85 cleanup: Fix some warnings from coverity.
e206bffa2 fix(group_chats): fix sync packets reverting topics
0e4715598 test: Add new scenario testing framework.
668291f44 refactor(toxcore): decouple Network_Funcs from sockaddr via IP_Port
fc4396cef fix: potential division by zero in toxav and unsafe hex parsing
8e8b352ab refactor: Add nullable annotations to struct members.
7740bb421 refactor: decouple net_crypto from DHT
1936d4296 test: add benchmark for toxav audio and video
46bfdc2df fix: correct printf format specifiers for unsigned integers
REVERT: 1828c5356 fix(toxav): remove extra copy of video frame on encode

git-subtree-dir: external/toxcore/c-toxcore
git-subtree-split: c9cdae001341e701fca980c9bb9febfeb95d2902
This commit is contained in:
Green Sky
2026-01-11 14:42:31 +01:00
parent e95f2cbb1c
commit 565efa4f39
328 changed files with 19057 additions and 13982 deletions

View File

@@ -25,33 +25,33 @@
#include "util.h"
struct TCP_Connections {
const Logger *logger;
const Memory *mem;
const Random *rng;
Mono_Time *mono_time;
const Network *ns;
DHT *dht;
const Logger *_Nonnull logger;
const Memory *_Nonnull mem;
const Random *_Nonnull rng;
Mono_Time *_Nonnull mono_time;
const Network *_Nonnull ns;
DHT *_Nonnull dht;
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
TCP_Connection_to *connections;
TCP_Connection_to *_Nullable connections;
uint32_t connections_length; /* Length of connections array. */
TCP_con *tcp_connections;
TCP_con *_Nullable tcp_connections;
uint32_t tcp_connections_length; /* Length of tcp_connections array. */
tcp_data_cb *tcp_data_callback;
void *tcp_data_callback_object;
tcp_data_cb *_Nullable tcp_data_callback;
void *_Nullable tcp_data_callback_object;
tcp_oob_cb *tcp_oob_callback;
void *tcp_oob_callback_object;
tcp_oob_cb *_Nullable tcp_oob_callback;
void *_Nullable tcp_oob_callback_object;
tcp_onion_cb *tcp_onion_callback;
void *tcp_onion_callback_object;
tcp_onion_cb *_Nullable tcp_onion_callback;
void *_Nullable tcp_onion_callback_object;
forwarded_response_cb *tcp_forwarded_response_callback;
void *tcp_forwarded_response_callback_object;
forwarded_response_cb *_Nullable tcp_forwarded_response_callback;
void *_Nullable tcp_forwarded_response_callback_object;
TCP_Proxy_Info proxy_info;
@@ -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 *_Nullable net_profile;
};
static const TCP_Connection_to empty_tcp_connection_to = {0};
@@ -80,7 +80,7 @@ uint32_t tcp_connections_count(const TCP_Connections *tcp_c)
* @retval -1 if mem_vrealloc fails.
* @retval 0 if it succeeds.
*/
static int realloc_tcp_connection_to(const Memory *_Nonnull mem, TCP_Connection_to *_Nullable *_Nonnull array, size_t num)
static int realloc_tcp_connection_to(const Memory *_Nonnull mem, TCP_Connection_to *_Nullable *array, size_t num)
{
if (num == 0) {
mem_delete(mem, *array);
@@ -100,7 +100,7 @@ static int realloc_tcp_connection_to(const Memory *_Nonnull mem, TCP_Connection_
return 0;
}
static int realloc_tcp_con(const Memory *_Nonnull mem, TCP_con *_Nullable *_Nonnull array, size_t num)
static int realloc_tcp_con(const Memory *_Nonnull mem, TCP_con *_Nullable *array, size_t num)
{
if (num == 0) {
mem_delete(mem, *array);
@@ -331,6 +331,10 @@ int send_packet_tcp_connection(const TCP_Connections *tcp_c, int connections_num
continue;
}
if (tcp_con->connection == nullptr) {
LOGGER_ERROR(tcp_c->logger, "TCP connection is null for connection number %u", tcp_con_num);
continue;
}
ret = send_data(tcp_c->logger, tcp_con->connection, connection_id, packet, length);
if (ret == 0) {
@@ -366,6 +370,10 @@ int send_packet_tcp_connection(const TCP_Connections *tcp_c, int connections_num
continue;
}
if (tcp_con->connection == nullptr) {
LOGGER_ERROR(tcp_c->logger, "TCP connection is null for connection number %u", tcp_con_num);
continue;
}
if (send_oob_packet(tcp_c->logger, tcp_con->connection, con_to->public_key, packet, length) == 1) {
sent_any = true;
}
@@ -406,6 +414,10 @@ int get_random_tcp_onion_conn_number(const TCP_Connections *tcp_c)
static int get_conn_number_by_ip_port(const TCP_Connections *_Nonnull tcp_c, const IP_Port *_Nonnull ip_port)
{
for (uint32_t i = 0; i < tcp_c->tcp_connections_length; ++i) {
if (tcp_c->tcp_connections[i].connection == nullptr) {
LOGGER_ERROR(tcp_c->logger, "TCP connection is null for connection index %u", i);
continue;
}
const IP_Port conn_ip_port = tcp_con_ip_port(tcp_c->tcp_connections[i].connection);
if (ipport_equal(ip_port, &conn_ip_port) &&
@@ -430,6 +442,10 @@ bool tcp_get_random_conn_ip_port(const TCP_Connections *tcp_c, IP_Port *ip_port)
return false;
}
if (tcp_c->tcp_connections[index].connection == nullptr) {
LOGGER_ERROR(tcp_c->logger, "TCP connection is null for connection index %d", index);
return false;
}
*ip_port = tcp_con_ip_port(tcp_c->tcp_connections[index].connection);
return true;
}
@@ -447,6 +463,10 @@ int tcp_send_onion_request(TCP_Connections *tcp_c, uint32_t tcp_connections_numb
}
if (tcp_c->tcp_connections[tcp_connections_number].status == TCP_CONN_CONNECTED) {
if (tcp_c->tcp_connections[tcp_connections_number].connection == nullptr) {
LOGGER_ERROR(tcp_c->logger, "TCP connection is null for connection number %u", tcp_connections_number);
return -1;
}
const int ret = send_onion_request(tcp_c->logger, tcp_c->tcp_connections[tcp_connections_number].connection, data,
length);
@@ -477,6 +497,10 @@ int tcp_send_forward_request(const Logger *logger, TCP_Connections *tcp_c, const
}
if (chain_length == 0) {
if (tcp_c->tcp_connections[index].connection == nullptr) {
LOGGER_ERROR(tcp_c->logger, "TCP connection is null for connection index %d", index);
return -1;
}
return send_forward_request_tcp(logger, tcp_c->tcp_connections[index].connection, dht_node, data,
data_length) == 1 ? 0 : -1;
}
@@ -484,6 +508,10 @@ int tcp_send_forward_request(const Logger *logger, TCP_Connections *tcp_c, const
const uint16_t len = forward_chain_packet_size(chain_length, data_length);
VLA(uint8_t, packet, len);
if (tcp_c->tcp_connections[index].connection == nullptr) {
LOGGER_ERROR(tcp_c->logger, "TCP connection is null for connection index %d", index);
return -1;
}
return create_forward_chain_packet(chain_keys, chain_length, data, data_length, packet)
&& send_forward_request_tcp(logger, tcp_c->tcp_connections[index].connection, dht_node, packet, len) == 1 ? 0 : -1;
}
@@ -506,6 +534,10 @@ int tcp_send_oob_packet(const TCP_Connections *tcp_c, unsigned int tcp_connectio
return -1;
}
if (tcp_con->connection == nullptr) {
LOGGER_ERROR(tcp_c->logger, "TCP connection is null for tcp_con");
return -1;
}
const int ret = send_oob_packet(tcp_c->logger, tcp_con->connection, public_key, packet, length);
if (ret == 1) {
@@ -623,6 +655,10 @@ static int find_tcp_connection_relay(const TCP_Connections *tcp_c, const uint8_t
return i;
}
} else {
if (tcp_con->connection == nullptr) {
LOGGER_ERROR(tcp_c->logger, "TCP connection is null for tcp_con");
return -1;
}
if (pk_equal(tcp_con_public_key(tcp_con->connection), relay_pk)) {
return i;
}
@@ -690,6 +726,10 @@ int kill_tcp_connection_to(TCP_Connections *tcp_c, int connections_number)
}
if (tcp_con->status == TCP_CONN_CONNECTED) {
if (tcp_con->connection == nullptr) {
LOGGER_ERROR(tcp_c->logger, "TCP connection is null for tcp_con");
continue;
}
send_disconnect_request(tcp_c->logger, tcp_con->connection, con_to->connections[i].connection_id);
}
@@ -907,6 +947,10 @@ static int reconnect_tcp_relay_connection(TCP_Connections *_Nonnull tcp_c, int t
return -1;
}
if (tcp_con->connection == nullptr) {
LOGGER_ERROR(tcp_c->logger, "TCP connection is null for tcp_con");
return -1;
}
const IP_Port ip_port = tcp_con_ip_port(tcp_con->connection);
uint8_t relay_pk[CRYPTO_PUBLIC_KEY_SIZE];
memcpy(relay_pk, tcp_con_public_key(tcp_con->connection), CRYPTO_PUBLIC_KEY_SIZE);
@@ -957,6 +1001,10 @@ static int sleep_tcp_relay_connection(TCP_Connections *_Nonnull tcp_c, int tcp_c
return -1;
}
if (tcp_con->connection == nullptr) {
LOGGER_ERROR(tcp_c->logger, "TCP connection is null for tcp_con");
return -1;
}
tcp_con->ip_port = tcp_con_ip_port(tcp_con->connection);
memcpy(tcp_con->relay_pk, tcp_con_public_key(tcp_con->connection), CRYPTO_PUBLIC_KEY_SIZE);
@@ -1032,6 +1080,10 @@ static int send_tcp_relay_routing_request(const TCP_Connections *_Nonnull tcp_c,
return -1;
}
if (tcp_con->connection == nullptr) {
LOGGER_ERROR(tcp_c->logger, "TCP connection is null for tcp_con");
return -1;
}
if (send_routing_request(tcp_c->logger, tcp_con->connection, public_key) != 1) {
return -1;
}
@@ -1067,6 +1119,10 @@ static int tcp_response_callback(void *_Nonnull object, uint8_t connection_id, c
return -1;
}
if (tcp_con->connection == nullptr) {
LOGGER_ERROR(tcp_c->logger, "TCP connection is null for tcp_con");
return -1;
}
set_tcp_connection_number(tcp_con->connection, connection_id, connections_number);
return 0;
@@ -1427,6 +1483,10 @@ static bool copy_tcp_relay_conn(const TCP_Connections *_Nonnull tcp_c, Node_form
return false;
}
if (tcp_con->connection == nullptr) {
LOGGER_ERROR(tcp_c->logger, "TCP connection is null for tcp_con");
return false;
}
memcpy(tcp_relay->public_key, tcp_con_public_key(tcp_con->connection), CRYPTO_PUBLIC_KEY_SIZE);
tcp_relay->ip_port = tcp_con_ip_port(tcp_con->connection);
@@ -1600,6 +1660,10 @@ static void do_tcp_conns(const Logger *_Nonnull logger, TCP_Connections *_Nonnul
}
if (tcp_con->status != TCP_CONN_SLEEPING) {
if (tcp_con->connection == nullptr) {
LOGGER_ERROR(logger, "TCP connection is null for tcp_con");
continue;
}
do_tcp_connection(logger, tcp_c->mono_time, tcp_con->connection, userdata);
/* callbacks can change TCP connection address. */
@@ -1608,6 +1672,10 @@ static void do_tcp_conns(const Logger *_Nonnull logger, TCP_Connections *_Nonnul
// Make sure the TCP connection wasn't dropped in any of the callbacks.
assert(tcp_con != nullptr);
if (tcp_con->connection == nullptr) {
LOGGER_ERROR(logger, "TCP connection is null for tcp_con");
continue;
}
if (tcp_con_status(tcp_con->connection) == TCP_CLIENT_DISCONNECTED) {
if (tcp_con->status == TCP_CONN_CONNECTED) {
reconnect_tcp_relay_connection(tcp_c, i);
@@ -1618,6 +1686,10 @@ static void do_tcp_conns(const Logger *_Nonnull logger, TCP_Connections *_Nonnul
continue;
}
if (tcp_con->connection == nullptr) {
LOGGER_ERROR(logger, "TCP connection is null for tcp_con");
continue;
}
if (tcp_con->status == TCP_CONN_VALID && tcp_con_status(tcp_con->connection) == TCP_CLIENT_CONFIRMED) {
tcp_relay_on_online(tcp_c, i);
}