forked from Green-Sky/tomato
Squashed 'external/toxcore/c-toxcore/' changes from 67badf694..82460b212
82460b212 feat: add ngc events 24b54722a fix: Ensure we have allocators available for the error paths. 48dbcfebc cleanup: Remove redundant `-DSODIUM_EXPORT` from definitions. 0cef46ee9 cleanup: Fix a few more clang-tidy warnings. 0c5b918e9 cleanup: Fix a few more clang-tidy warnings. 4d3c97f49 cleanup: Enforce stricter identifier naming using clang-tidy. a549807df refactor: Add `mem` module to allow tests to override allocators. 6133fb153 chore: Add devcontainer setup for codespaces. 620e07ecd chore: Set a timeout for tests started using Conan c0ec33b16 chore: Migrate Windows CI from Appveyor to Azure DevOps 8ed47f3ef fix incorrect documentation a1e245841 docs: Fix doxygen config and remove some redundant comments. b0f633185 chore: Fix the Android CI job 7469a529b fix: Add missing `#include <array>`. 2b1a6b0d2 add missing ngc constants getter declarations and definitions 2e02d5637 chore: Add missing module dependencies. REVERT: 67badf694 feat: add ngc events git-subtree-dir: external/toxcore/c-toxcore git-subtree-split: 82460b2124216af1ac9d63060de310a682a2fd15
This commit is contained in:
@ -106,6 +106,7 @@ struct Onion_Client {
|
||||
const Mono_Time *mono_time;
|
||||
const Logger *logger;
|
||||
const Random *rng;
|
||||
const Memory *mem;
|
||||
|
||||
DHT *dht;
|
||||
Net_Crypto *c;
|
||||
@ -731,12 +732,12 @@ static int onion_client_cmp_entry(const void *a, const void *b)
|
||||
}
|
||||
|
||||
non_null()
|
||||
static void sort_onion_node_list(Onion_Node *list, unsigned int length, const Mono_Time *mono_time,
|
||||
const uint8_t *comp_public_key)
|
||||
static void sort_onion_node_list(const Memory *mem, const Mono_Time *mono_time,
|
||||
Onion_Node *list, unsigned int length, const uint8_t *comp_public_key)
|
||||
{
|
||||
// Pass comp_public_key to qsort with each Client_data entry, so the
|
||||
// comparison function can use it as the base of comparison.
|
||||
Onion_Client_Cmp_Data *cmp_list = (Onion_Client_Cmp_Data *)calloc(length, sizeof(Onion_Client_Cmp_Data));
|
||||
Onion_Client_Cmp_Data *cmp_list = (Onion_Client_Cmp_Data *)mem_valloc(mem, length, sizeof(Onion_Client_Cmp_Data));
|
||||
|
||||
if (cmp_list == nullptr) {
|
||||
return;
|
||||
@ -754,7 +755,7 @@ static void sort_onion_node_list(Onion_Node *list, unsigned int length, const Mo
|
||||
list[i] = cmp_list[i].entry;
|
||||
}
|
||||
|
||||
free(cmp_list);
|
||||
mem_delete(mem, cmp_list);
|
||||
}
|
||||
|
||||
non_null()
|
||||
@ -787,7 +788,7 @@ static int client_add_to_list(Onion_Client *onion_c, uint32_t num, const uint8_t
|
||||
list_length = MAX_ONION_CLIENTS;
|
||||
}
|
||||
|
||||
sort_onion_node_list(node_list, list_length, onion_c->mono_time, reference_id);
|
||||
sort_onion_node_list(onion_c->mem, onion_c->mono_time, node_list, list_length, reference_id);
|
||||
|
||||
int index = -1;
|
||||
bool stored = false;
|
||||
@ -1161,7 +1162,7 @@ static int handle_dhtpk_announce(void *object, const uint8_t *source_pubkey, con
|
||||
onion_c->friends_list[friend_num].dht_pk_callback_number, data + 1 + sizeof(uint64_t), userdata);
|
||||
}
|
||||
|
||||
onion_set_friend_DHT_pubkey(onion_c, friend_num, data + 1 + sizeof(uint64_t));
|
||||
onion_set_friend_dht_pubkey(onion_c, friend_num, data + 1 + sizeof(uint64_t));
|
||||
|
||||
const uint16_t len_nodes = length - DHTPK_DATA_MIN_LENGTH;
|
||||
|
||||
@ -1457,19 +1458,19 @@ int onion_friend_num(const Onion_Client *onion_c, const uint8_t *public_key)
|
||||
|
||||
/** @brief Set the size of the friend list to num.
|
||||
*
|
||||
* @retval -1 if realloc fails.
|
||||
* @retval -1 if mem_vrealloc fails.
|
||||
* @retval 0 if it succeeds.
|
||||
*/
|
||||
non_null()
|
||||
static int realloc_onion_friends(Onion_Client *onion_c, uint32_t num)
|
||||
{
|
||||
if (num == 0) {
|
||||
free(onion_c->friends_list);
|
||||
mem_delete(onion_c->mem, onion_c->friends_list);
|
||||
onion_c->friends_list = nullptr;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Onion_Friend *newonion_friends = (Onion_Friend *)realloc(onion_c->friends_list, num * sizeof(Onion_Friend));
|
||||
Onion_Friend *newonion_friends = (Onion_Friend *)mem_vrealloc(onion_c->mem, onion_c->friends_list, num, sizeof(Onion_Friend));
|
||||
|
||||
if (newonion_friends == nullptr) {
|
||||
return -1;
|
||||
@ -1601,7 +1602,7 @@ int onion_dht_pk_callback(Onion_Client *onion_c, int friend_num,
|
||||
* return -1 on failure.
|
||||
* return 0 on success.
|
||||
*/
|
||||
int onion_set_friend_DHT_pubkey(Onion_Client *onion_c, int friend_num, const uint8_t *dht_key)
|
||||
int onion_set_friend_dht_pubkey(Onion_Client *onion_c, int friend_num, const uint8_t *dht_key)
|
||||
{
|
||||
if ((uint32_t)friend_num >= onion_c->num_friends) {
|
||||
return -1;
|
||||
@ -1628,7 +1629,7 @@ int onion_set_friend_DHT_pubkey(Onion_Client *onion_c, int friend_num, const uin
|
||||
* return 0 on failure (no key copied).
|
||||
* return 1 on success (key copied).
|
||||
*/
|
||||
unsigned int onion_getfriend_DHT_pubkey(const Onion_Client *onion_c, int friend_num, uint8_t *dht_key)
|
||||
unsigned int onion_getfriend_dht_pubkey(const Onion_Client *onion_c, int friend_num, uint8_t *dht_key)
|
||||
{
|
||||
if ((uint32_t)friend_num >= onion_c->num_friends) {
|
||||
return 0;
|
||||
@ -1656,7 +1657,7 @@ int onion_getfriendip(const Onion_Client *onion_c, int friend_num, IP_Port *ip_p
|
||||
{
|
||||
uint8_t dht_public_key[CRYPTO_PUBLIC_KEY_SIZE];
|
||||
|
||||
if (onion_getfriend_DHT_pubkey(onion_c, friend_num, dht_public_key) == 0) {
|
||||
if (onion_getfriend_dht_pubkey(onion_c, friend_num, dht_public_key) == 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -2074,28 +2075,29 @@ void do_onion_client(Onion_Client *onion_c)
|
||||
onion_c->last_run = mono_time_get(onion_c->mono_time);
|
||||
}
|
||||
|
||||
Onion_Client *new_onion_client(const Logger *logger, const Random *rng, const Mono_Time *mono_time, Net_Crypto *c)
|
||||
Onion_Client *new_onion_client(const Logger *logger, const Memory *mem, const Random *rng, const Mono_Time *mono_time, Net_Crypto *c)
|
||||
{
|
||||
if (c == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Onion_Client *onion_c = (Onion_Client *)calloc(1, sizeof(Onion_Client));
|
||||
Onion_Client *onion_c = (Onion_Client *)mem_alloc(mem, sizeof(Onion_Client));
|
||||
|
||||
if (onion_c == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
onion_c->announce_ping_array = ping_array_new(ANNOUNCE_ARRAY_SIZE, ANNOUNCE_TIMEOUT);
|
||||
onion_c->announce_ping_array = ping_array_new(mem, ANNOUNCE_ARRAY_SIZE, ANNOUNCE_TIMEOUT);
|
||||
|
||||
if (onion_c->announce_ping_array == nullptr) {
|
||||
free(onion_c);
|
||||
mem_delete(mem, onion_c);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
onion_c->mono_time = mono_time;
|
||||
onion_c->logger = logger;
|
||||
onion_c->rng = rng;
|
||||
onion_c->mem = mem;
|
||||
onion_c->dht = nc_get_dht(c);
|
||||
onion_c->net = dht_get_net(onion_c->dht);
|
||||
onion_c->c = c;
|
||||
@ -2117,6 +2119,8 @@ void kill_onion_client(Onion_Client *onion_c)
|
||||
return;
|
||||
}
|
||||
|
||||
const Memory *mem = onion_c->mem;
|
||||
|
||||
ping_array_kill(onion_c->announce_ping_array);
|
||||
realloc_onion_friends(onion_c, 0);
|
||||
networking_registerhandler(onion_c->net, NET_PACKET_ANNOUNCE_RESPONSE, nullptr, nullptr);
|
||||
@ -2126,5 +2130,5 @@ void kill_onion_client(Onion_Client *onion_c)
|
||||
cryptopacket_registerhandler(onion_c->dht, CRYPTO_PACKET_DHTPK, nullptr, nullptr);
|
||||
set_onion_packet_tcp_connection_callback(nc_get_tcp_c(onion_c->c), nullptr, nullptr);
|
||||
crypto_memzero(onion_c, sizeof(Onion_Client));
|
||||
free(onion_c);
|
||||
mem_delete(mem, onion_c);
|
||||
}
|
||||
|
Reference in New Issue
Block a user