forked from Green-Sky/tomato
Squashed 'external/toxcore/c-toxcore/' changes from 55752a2e2ef..11ab1d2a723
11ab1d2a723 fix: reduce memory usage in group chats by 75% Significantly reduced the memory usage of groups since all message slots are preallocated for every peer for send and receive buffers of buffer size (hundreds of MiB peak when save contained alot of peers to try to connect to) 4f09f4e147c chore: Fix tsan build by moving it to GitHub CI. 6460c25c9e0 refactor: Use `merge_sort` instead of `qsort` for sorting. c660bbe8c95 test: Fix crypto_test to initialise its plain text buffer. 0204db6184b cleanup: Fix layering check warnings. df2211e1548 refactor: Use tox memory allocator for temporary buffers in crypto. ac812871a2e feat: implement the last 2 missing network struct functions and make use of them 29d1043be0b test: friend request test now tests min/max message sizes 93aafd78c1f fix: friend requests with very long messages are no longer dropped 819aa2b2618 feat: Add option to disable DNS lookups in toxcore. 0ac23cee035 fix: windows use of REUSEADDR 7d2811d302d chore(ci): make bazel server shutdown faster 1dc399ba20d chore: Use vcpkg instead of conan in the MSVC build. 14d823165d9 chore: Migrate to conan 2. bdd17c16787 cleanup: Allocate logger using tox memory allocator. b396c061515 chore(deps): bump third_party/cmp from `2ac6bca` to `52bfcfa` 2e94da60d09 feat(net): add missing connect to network struct 41fb1839c7b chore: Add check to ensure version numbers agree. 934a8301113 chore: Release 0.2.20 3acef4bf044 fix: Add missing free in dht_get_nodes_response event. git-subtree-dir: external/toxcore/c-toxcore git-subtree-split: 11ab1d2a7232eee19b51ce126ccce267d6578903
This commit is contained in:
@ -9,7 +9,6 @@
|
||||
#include "onion_announce.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "DHT.h"
|
||||
@ -23,6 +22,7 @@
|
||||
#include "network.h"
|
||||
#include "onion.h"
|
||||
#include "shared_key_cache.h"
|
||||
#include "sort.h"
|
||||
#include "timed_auth.h"
|
||||
|
||||
#define PING_ID_TIMEOUT ONION_ANNOUNCE_TIMEOUT
|
||||
@ -103,7 +103,7 @@ void onion_announce_entry_set_time(Onion_Announce *onion_a, uint32_t entry, uint
|
||||
* return -1 on failure.
|
||||
* return packet length on success.
|
||||
*/
|
||||
int create_announce_request(const Random *rng, uint8_t *packet, uint16_t max_packet_length, const uint8_t *dest_client_id,
|
||||
int create_announce_request(const Memory *mem, const Random *rng, uint8_t *packet, uint16_t max_packet_length, const uint8_t *dest_client_id,
|
||||
const uint8_t *public_key, const uint8_t *secret_key, const uint8_t *ping_id, const uint8_t *client_id,
|
||||
const uint8_t *data_public_key, uint64_t sendback_data)
|
||||
{
|
||||
@ -122,7 +122,7 @@ int create_announce_request(const Random *rng, uint8_t *packet, uint16_t max_pac
|
||||
packet[0] = NET_PACKET_ANNOUNCE_REQUEST_OLD;
|
||||
random_nonce(rng, packet + 1);
|
||||
|
||||
const int len = encrypt_data(dest_client_id, secret_key, packet + 1, plain, sizeof(plain),
|
||||
const int len = encrypt_data(mem, dest_client_id, secret_key, packet + 1, plain, sizeof(plain),
|
||||
packet + 1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE);
|
||||
|
||||
if ((uint32_t)len + 1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE != ONION_ANNOUNCE_REQUEST_MIN_SIZE) {
|
||||
@ -146,7 +146,7 @@ int create_announce_request(const Random *rng, uint8_t *packet, uint16_t max_pac
|
||||
* return -1 on failure.
|
||||
* return 0 on success.
|
||||
*/
|
||||
int create_data_request(const Random *rng, uint8_t *packet, uint16_t max_packet_length, const uint8_t *public_key,
|
||||
int create_data_request(const Memory *mem, const Random *rng, uint8_t *packet, uint16_t max_packet_length, const uint8_t *public_key,
|
||||
const uint8_t *encrypt_public_key, const uint8_t *nonce, const uint8_t *data, uint16_t length)
|
||||
{
|
||||
if (DATA_REQUEST_MIN_SIZE + length > max_packet_length) {
|
||||
@ -167,7 +167,7 @@ int create_data_request(const Random *rng, uint8_t *packet, uint16_t max_packet_
|
||||
|
||||
memcpy(packet + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE, random_public_key, CRYPTO_PUBLIC_KEY_SIZE);
|
||||
|
||||
const int len = encrypt_data(encrypt_public_key, random_secret_key, packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, data, length,
|
||||
const int len = encrypt_data(mem, encrypt_public_key, random_secret_key, packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, data, length,
|
||||
packet + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE);
|
||||
|
||||
if (1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE + len != DATA_REQUEST_MIN_SIZE +
|
||||
@ -193,14 +193,14 @@ int create_data_request(const Random *rng, uint8_t *packet, uint16_t max_packet_
|
||||
* return 0 on success.
|
||||
*/
|
||||
int send_announce_request(
|
||||
const Logger *log, const Networking_Core *net, const Random *rng,
|
||||
const Logger *log, const Memory *mem, const Networking_Core *net, const Random *rng,
|
||||
const Onion_Path *path, const Node_format *dest,
|
||||
const uint8_t *public_key, const uint8_t *secret_key,
|
||||
const uint8_t *ping_id, const uint8_t *client_id,
|
||||
const uint8_t *data_public_key, uint64_t sendback_data)
|
||||
{
|
||||
uint8_t request[ONION_ANNOUNCE_REQUEST_MIN_SIZE];
|
||||
int len = create_announce_request(rng, request, sizeof(request), dest->public_key, public_key, secret_key, ping_id,
|
||||
int len = create_announce_request(mem, rng, request, sizeof(request), dest->public_key, public_key, secret_key, ping_id,
|
||||
client_id, data_public_key, sendback_data);
|
||||
|
||||
if (len != sizeof(request)) {
|
||||
@ -208,7 +208,7 @@ int send_announce_request(
|
||||
}
|
||||
|
||||
uint8_t packet[ONION_MAX_PACKET_SIZE];
|
||||
len = create_onion_packet(rng, packet, sizeof(packet), path, &dest->ip_port, request, sizeof(request));
|
||||
len = create_onion_packet(mem, rng, packet, sizeof(packet), path, &dest->ip_port, request, sizeof(request));
|
||||
|
||||
if (len == -1) {
|
||||
return -1;
|
||||
@ -238,19 +238,19 @@ int send_announce_request(
|
||||
* return 0 on success.
|
||||
*/
|
||||
int send_data_request(
|
||||
const Logger *log, const Networking_Core *net, const Random *rng, const Onion_Path *path, const IP_Port *dest,
|
||||
const Logger *log, const Memory *mem, const Networking_Core *net, const Random *rng, const Onion_Path *path, const IP_Port *dest,
|
||||
const uint8_t *public_key, const uint8_t *encrypt_public_key, const uint8_t *nonce,
|
||||
const uint8_t *data, uint16_t length)
|
||||
{
|
||||
uint8_t request[ONION_MAX_DATA_SIZE];
|
||||
int len = create_data_request(rng, request, sizeof(request), public_key, encrypt_public_key, nonce, data, length);
|
||||
int len = create_data_request(mem, rng, request, sizeof(request), public_key, encrypt_public_key, nonce, data, length);
|
||||
|
||||
if (len == -1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint8_t packet[ONION_MAX_PACKET_SIZE];
|
||||
len = create_onion_packet(rng, packet, sizeof(packet), path, dest, request, len);
|
||||
len = create_onion_packet(mem, rng, packet, sizeof(packet), path, dest, request, len);
|
||||
|
||||
if (len == -1) {
|
||||
return -1;
|
||||
@ -281,23 +281,17 @@ static int in_entries(const Onion_Announce *onion_a, const uint8_t *public_key)
|
||||
return -1;
|
||||
}
|
||||
|
||||
typedef struct Cmp_Data {
|
||||
typedef struct Onion_Announce_Entry_Cmp {
|
||||
const Memory *mem;
|
||||
const Mono_Time *mono_time;
|
||||
const uint8_t *base_public_key;
|
||||
Onion_Announce_Entry entry;
|
||||
} Cmp_Data;
|
||||
const uint8_t *comp_public_key;
|
||||
} Onion_Announce_Entry_Cmp;
|
||||
|
||||
non_null()
|
||||
static int cmp_entry(const void *a, const void *b)
|
||||
static int onion_announce_entry_cmp(const Onion_Announce_Entry_Cmp *cmp, const Onion_Announce_Entry *entry1, const Onion_Announce_Entry *entry2)
|
||||
{
|
||||
const Cmp_Data *cmp1 = (const Cmp_Data *)a;
|
||||
const Cmp_Data *cmp2 = (const Cmp_Data *)b;
|
||||
const Onion_Announce_Entry entry1 = cmp1->entry;
|
||||
const Onion_Announce_Entry entry2 = cmp2->entry;
|
||||
const uint8_t *cmp_public_key = cmp1->base_public_key;
|
||||
|
||||
const bool t1 = mono_time_is_timeout(cmp1->mono_time, entry1.announce_time, ONION_ANNOUNCE_TIMEOUT);
|
||||
const bool t2 = mono_time_is_timeout(cmp1->mono_time, entry2.announce_time, ONION_ANNOUNCE_TIMEOUT);
|
||||
const bool t1 = mono_time_is_timeout(cmp->mono_time, entry1->announce_time, ONION_ANNOUNCE_TIMEOUT);
|
||||
const bool t2 = mono_time_is_timeout(cmp->mono_time, entry2->announce_time, ONION_ANNOUNCE_TIMEOUT);
|
||||
|
||||
if (t1 && t2) {
|
||||
return 0;
|
||||
@ -311,7 +305,7 @@ static int cmp_entry(const void *a, const void *b)
|
||||
return 1;
|
||||
}
|
||||
|
||||
const int closest = id_closest(cmp_public_key, entry1.public_key, entry2.public_key);
|
||||
const int closest = id_closest(cmp->comp_public_key, entry1->public_key, entry2->public_key);
|
||||
|
||||
if (closest == 1) {
|
||||
return 1;
|
||||
@ -324,32 +318,81 @@ static int cmp_entry(const void *a, const void *b)
|
||||
return 0;
|
||||
}
|
||||
|
||||
non_null()
|
||||
static bool onion_announce_entry_less_handler(const void *object, const void *a, const void *b)
|
||||
{
|
||||
const Onion_Announce_Entry_Cmp *cmp = (const Onion_Announce_Entry_Cmp *)object;
|
||||
const Onion_Announce_Entry *entry1 = (const Onion_Announce_Entry *)a;
|
||||
const Onion_Announce_Entry *entry2 = (const Onion_Announce_Entry *)b;
|
||||
|
||||
return onion_announce_entry_cmp(cmp, entry1, entry2) < 0;
|
||||
}
|
||||
|
||||
non_null()
|
||||
static const void *onion_announce_entry_get_handler(const void *arr, uint32_t index)
|
||||
{
|
||||
const Onion_Announce_Entry *entries = (const Onion_Announce_Entry *)arr;
|
||||
return &entries[index];
|
||||
}
|
||||
|
||||
non_null()
|
||||
static void onion_announce_entry_set_handler(void *arr, uint32_t index, const void *val)
|
||||
{
|
||||
Onion_Announce_Entry *entries = (Onion_Announce_Entry *)arr;
|
||||
const Onion_Announce_Entry *entry = (const Onion_Announce_Entry *)val;
|
||||
entries[index] = *entry;
|
||||
}
|
||||
|
||||
non_null()
|
||||
static void *onion_announce_entry_subarr_handler(void *arr, uint32_t index, uint32_t size)
|
||||
{
|
||||
Onion_Announce_Entry *entries = (Onion_Announce_Entry *)arr;
|
||||
return &entries[index];
|
||||
}
|
||||
|
||||
non_null()
|
||||
static void *onion_announce_entry_alloc_handler(const void *object, uint32_t size)
|
||||
{
|
||||
const Onion_Announce_Entry_Cmp *cmp = (const Onion_Announce_Entry_Cmp *)object;
|
||||
Onion_Announce_Entry *tmp = (Onion_Announce_Entry *)mem_valloc(cmp->mem, size, sizeof(Onion_Announce_Entry));
|
||||
|
||||
if (tmp == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
non_null()
|
||||
static void onion_announce_entry_delete_handler(const void *object, void *arr, uint32_t size)
|
||||
{
|
||||
const Onion_Announce_Entry_Cmp *cmp = (const Onion_Announce_Entry_Cmp *)object;
|
||||
mem_delete(cmp->mem, arr);
|
||||
}
|
||||
|
||||
static const Sort_Funcs onion_announce_entry_cmp_funcs = {
|
||||
onion_announce_entry_less_handler,
|
||||
onion_announce_entry_get_handler,
|
||||
onion_announce_entry_set_handler,
|
||||
onion_announce_entry_subarr_handler,
|
||||
onion_announce_entry_alloc_handler,
|
||||
onion_announce_entry_delete_handler,
|
||||
};
|
||||
|
||||
non_null()
|
||||
static void sort_onion_announce_list(const Memory *mem, const Mono_Time *mono_time,
|
||||
Onion_Announce_Entry *list, unsigned int length,
|
||||
const uint8_t *comp_public_key)
|
||||
{
|
||||
// Pass comp_public_key to qsort with each Client_data entry, so the
|
||||
// Pass comp_public_key to sort with each Onion_Announce_Entry entry, so the
|
||||
// comparison function can use it as the base of comparison.
|
||||
Cmp_Data *cmp_list = (Cmp_Data *)mem_valloc(mem, length, sizeof(Cmp_Data));
|
||||
const Onion_Announce_Entry_Cmp cmp = {
|
||||
mem,
|
||||
mono_time,
|
||||
comp_public_key,
|
||||
};
|
||||
|
||||
if (cmp_list == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < length; ++i) {
|
||||
cmp_list[i].mono_time = mono_time;
|
||||
cmp_list[i].base_public_key = comp_public_key;
|
||||
cmp_list[i].entry = list[i];
|
||||
}
|
||||
|
||||
qsort(cmp_list, length, sizeof(Cmp_Data), cmp_entry);
|
||||
|
||||
for (uint32_t i = 0; i < length; ++i) {
|
||||
list[i] = cmp_list[i].entry;
|
||||
}
|
||||
|
||||
mem_delete(mem, cmp_list);
|
||||
merge_sort(list, length, &cmp, &onion_announce_entry_cmp_funcs);
|
||||
}
|
||||
|
||||
/** @brief add entry to entries list
|
||||
@ -455,7 +498,7 @@ static int handle_announce_request_common(
|
||||
return 1;
|
||||
}
|
||||
|
||||
const int decrypted_len = decrypt_data_symmetric(shared_key, packet + 1,
|
||||
const int decrypted_len = decrypt_data_symmetric(onion_a->mem, shared_key, packet + 1,
|
||||
packet + 1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE, plain_size + CRYPTO_MAC_SIZE, plain);
|
||||
|
||||
if ((uint32_t)decrypted_len != plain_size) {
|
||||
@ -542,7 +585,7 @@ static int handle_announce_request_common(
|
||||
offset += extra_size;
|
||||
|
||||
uint8_t data[ONION_ANNOUNCE_RESPONSE_MAX_SIZE];
|
||||
const int len = encrypt_data_symmetric(shared_key, nonce, response, offset,
|
||||
const int len = encrypt_data_symmetric(onion_a->mem, shared_key, nonce, response, offset,
|
||||
data + 1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + CRYPTO_NONCE_SIZE);
|
||||
|
||||
if (len != offset + CRYPTO_MAC_SIZE) {
|
||||
|
Reference in New Issue
Block a user