Squashed 'external/toxcore/c-toxcore/' changes from 8f0d505f9a..6d634674a9

6d634674a9 cleanup: Remove old type-ordered event getters.
d1d48d1dfc feat: add ngc events
994ffecc6b refactor: Make event dispatch ordered by receive time.
812f931d5f fix: Make sure there's enough space for CONSUME1 in fuzzers.
50f1b30fa9 test: Add fuzz tests to the coverage run.
df76f5cf47 chore: Move from gcov to llvm source-based coverage.
072e3beb3f fix: issues with packet broadcast error reporting
6b6718e4d2 cleanup: Make group packet entry creation less error-prone
5b9c420ce1 refactor: packet broadcast functions now return errors
af4cb31028 refactor: Use `operator==` for equality tests of `Node_format`.
9592d590cf refactor(test): Slightly nicer C++ interface to tox Random.
c66e10fb7a refactor: Minor refactoring of get_close_nodes functions.
ebc9643862 fix: don't pass garbage data buffer to packet send functions
32b68cffca cleanup: Some more test cleanups, removing overly smart code.
0426624dcb refactor: Assign malloc return to a local variable first.
afc38f2458 test: Add more unit tests for `add_to_list`.
05ce5c1ab9 test: Add "infer" CI check to github, remove from circle.
REVERT: 8f0d505f9a feat: add ngc events
REVERT: 9b8216e70c refactor: Make event dispatch ordered by receive time.

git-subtree-dir: external/toxcore/c-toxcore
git-subtree-split: 6d634674a929edb0ab70689dcbcb195b3547be13
This commit is contained in:
2024-01-12 21:30:48 +01:00
parent 9ace11a0e2
commit 8eb4892b49
126 changed files with 1556 additions and 2484 deletions

View File

@ -85,30 +85,48 @@ void gcc_set_recv_message_id(GC_Connection *gconn, uint64_t id)
}
/** @brief Puts packet data in array_entry.
*
* Requires an empty array entry to be passed, and must not modify the passed
* array entry on error.
*
* Return true on success.
*/
non_null(1, 2) nullable(3)
static bool create_array_entry(const Mono_Time *mono_time, GC_Message_Array_Entry *array_entry, const uint8_t *data,
uint16_t length, uint8_t packet_type, uint64_t message_id)
non_null(1, 2, 3) nullable(4)
static bool create_array_entry(const Logger *log, const Mono_Time *mono_time, GC_Message_Array_Entry *array_entry,
const uint8_t *data, uint16_t length, uint8_t packet_type, uint64_t message_id)
{
if (length > 0) {
if (!array_entry_is_empty(array_entry)) {
LOGGER_WARNING(log, "Failed to create array entry; entry is not empty.");
return false;
}
if (length == 0) {
if (data != nullptr) {
LOGGER_FATAL(log, "Got non-null data with zero length (type %d)", packet_type); // should never happen
return false;
}
array_entry->data = nullptr;
array_entry->data_length = 0;
} else {
if (data == nullptr) {
LOGGER_FATAL(log, "Got null data with non-zero length (type %u)", packet_type); // should never happen
return false;
}
array_entry->data = (uint8_t *)malloc(sizeof(uint8_t) * length);
uint8_t *entry_data = (uint8_t *)malloc(length);
if (array_entry->data == nullptr) {
if (entry_data == nullptr) {
return false;
}
memcpy(array_entry->data, data, length);
memcpy(entry_data, data, length);
array_entry->data = entry_data;
array_entry->data_length = length;
}
const uint64_t tm = mono_time_get(mono_time);
array_entry->data_length = length;
array_entry->packet_type = packet_type;
array_entry->message_id = message_id;
array_entry->time_added = tm;
@ -119,7 +137,7 @@ static bool create_array_entry(const Mono_Time *mono_time, GC_Message_Array_Entr
/** @brief Adds data of length to gconn's send_array.
*
* Returns true on success and increments gconn's send_message_id.
* Returns true and increments gconn's send_message_id on success.
*/
non_null(1, 2, 3) nullable(4)
static bool add_to_send_array(const Logger *log, const Mono_Time *mono_time, GC_Connection *gconn, const uint8_t *data,
@ -134,13 +152,7 @@ static bool add_to_send_array(const Logger *log, const Mono_Time *mono_time, GC_
const uint16_t idx = gcc_get_array_index(gconn->send_message_id);
GC_Message_Array_Entry *array_entry = &gconn->send_array[idx];
if (!array_entry_is_empty(array_entry)) {
LOGGER_DEBUG(log, "Send array entry isn't empty");
return false;
}
if (!create_array_entry(mono_time, array_entry, data, length, packet_type, gconn->send_message_id)) {
LOGGER_WARNING(log, "Failed to create array entry");
if (!create_array_entry(log, mono_time, array_entry, data, length, packet_type, gconn->send_message_id)) {
return false;
}
@ -159,8 +171,15 @@ int gcc_send_lossless_packet(const GC_Chat *chat, GC_Connection *gconn, const ui
return -1;
}
if (!gcc_encrypt_and_send_lossless_packet(chat, gconn, data, length, message_id, packet_type)) {
LOGGER_DEBUG(chat->log, "Failed to send payload: (type: 0x%02x, length: %d)", packet_type, length);
// If the packet fails to wrap/encrypt, we remove it from the send array, since trying to-resend
// the same bad packet probably won't help much. Otherwise we don't care if it doesn't successfully
// send through the wire as it will keep retrying until the connection times out.
if (gcc_encrypt_and_send_lossless_packet(chat, gconn, data, length, message_id, packet_type) == -1) {
const uint16_t idx = gcc_get_array_index(message_id);
GC_Message_Array_Entry *array_entry = &gconn->send_array[idx];
clear_array_entry(array_entry);
gconn->send_message_id = message_id;
LOGGER_ERROR(chat->log, "Failed to encrypt payload: (type: 0x%02x, length: %d)", packet_type, length);
return -2;
}
@ -331,17 +350,7 @@ static bool store_in_recv_array(const Logger *log, const Mono_Time *mono_time, G
const uint16_t idx = gcc_get_array_index(message_id);
GC_Message_Array_Entry *ary_entry = &gconn->recv_array[idx];
if (!array_entry_is_empty(ary_entry)) {
LOGGER_DEBUG(log, "Recv array is not empty");
return false;
}
if (!create_array_entry(mono_time, ary_entry, data, length, packet_type, message_id)) {
LOGGER_WARNING(log, "Failed to create array entry");
return false;
}
return true;
return create_array_entry(log, mono_time, ary_entry, data, length, packet_type, message_id);
}
/**
@ -392,10 +401,9 @@ static uint16_t reassemble_packet(const Logger *log, GC_Connection *gconn, uint8
return 0;
}
assert(*payload == nullptr);
*payload = (uint8_t *)malloc(packet_length);
uint8_t *tmp_payload = (uint8_t *)malloc(packet_length);
if (*payload == nullptr) {
if (tmp_payload == nullptr) {
LOGGER_ERROR(log, "Failed to allocate %u bytes for payload buffer", packet_length);
return 0;
}
@ -409,12 +417,15 @@ static uint16_t reassemble_packet(const Logger *log, GC_Connection *gconn, uint8
entry = &gconn->recv_array[i];
assert(processed + entry->data_length <= packet_length);
memcpy(*payload + processed, entry->data, entry->data_length);
memcpy(tmp_payload + processed, entry->data, entry->data_length);
processed += entry->data_length;
clear_array_entry(entry);
}
assert(*payload == nullptr);
*payload = tmp_payload;
return processed;
}
@ -612,7 +623,7 @@ bool gcc_send_packet(const GC_Chat *chat, const GC_Connection *gconn, const uint
return ret == 0 || direct_send_attempt;
}
bool gcc_encrypt_and_send_lossless_packet(const GC_Chat *chat, const GC_Connection *gconn, const uint8_t *data,
int gcc_encrypt_and_send_lossless_packet(const GC_Chat *chat, const GC_Connection *gconn, const uint8_t *data,
uint16_t length, uint64_t message_id, uint8_t packet_type)
{
const uint16_t packet_size = gc_get_wrapped_packet_size(length, NET_PACKET_GC_LOSSLESS);
@ -620,7 +631,7 @@ bool gcc_encrypt_and_send_lossless_packet(const GC_Chat *chat, const GC_Connecti
if (packet == nullptr) {
LOGGER_ERROR(chat->log, "Failed to allocate memory for packet buffer");
return false;
return -1;
}
const int enc_len = group_packet_wrap(
@ -630,18 +641,18 @@ bool gcc_encrypt_and_send_lossless_packet(const GC_Chat *chat, const GC_Connecti
if (enc_len < 0) {
LOGGER_ERROR(chat->log, "Failed to wrap packet (type: 0x%02x, error: %d)", packet_type, enc_len);
free(packet);
return false;
return -1;
}
if (!gcc_send_packet(chat, gconn, packet, (uint16_t)enc_len)) {
LOGGER_DEBUG(chat->log, "Failed to send packet (type: 0x%02x, enc_len: %d)", packet_type, enc_len);
free(packet);
return false;
return -2;
}
free(packet);
return true;
return 0;
}
void gcc_make_session_shared_key(GC_Connection *gconn, const uint8_t *sender_pk)