Squashed 'external/toxcore/c-toxcore/' changes from e2c01e457b..b03b571272

b03b571272 fix: flaky tcp test This only fixes the symptoms, not the real problem. Sometimes or consistently on some platforms a socket might need a moment before it can be written to.
32e67ab4c2 cleanup: use typedef for private message ID's in callback
7b1db6adc1 feat: add message IDs to private group messages
99e0bcc27d refactor: Observers/ignored peers can now send and receive custom packets
b3c3c49d26 fix: Disable IPv6 in Windows cross-compilation tests
e742deddff feat: Check hashes of Windows dependencies when cross-compiling
dfb9a0b02b fix: Test the current Windows Dockerfile, not an old Dockerhub image
14de93ccec chore: Use WineHQ's Wine as Debian Bookworm's crashes
ed37616249 docs: Update the Windows cross-compilation section
9bb79c174f cleanup: Remove a couple of unnecessary misc_tools dependencies
19475adb70 chore: Statically link OpenMP into the cracker fun util on Windows
1be311e51f feat: Build the fun utils when cross-compiling to Windows
88133f8446 chore: Strip Windows binaries
3cc0ae7535 refactor: Copy over all of the required static dependencies
c4fa8f7fb1 feat: Generate .def, .exp and .lib files when building for Windows
74bbac5363 feat: Let CMake create the dll instead of doing so ourselves
246642e9ae feat: Harden Windows cross-compilation
8d431c0d11 chore: Bump Windows build dependency versions
e519f7998b fix: Remove unnecessary wsock32 dependency on Windows
ed2b60c217 chore: Use a specific non-broken slimcc version.
d7f21010a1 chore: Update github actions.
e71a68b7f2 docs: Update the list of CMake options
77e08876ff chore: Remove mod and founder from group API naming scheme
12bc042767 docs: add the experimental api build option to INSTALL.md
e1fa5cae96 refactor: Rename Queries to Query to align with other enums.
be82a3ea30 fix: Correct type for conference offline peer numbers.
0627c36716 test: Add pkgsrc build.
92578afe4b test: Add FreeBSD VM action on GitHub.
52ece0f57b test: Build toxcore on NetBSD (VM).
3fe8ee2c11 chore: Only install tox_private.h on request.
9a8dfa06ab fix: save_compatibility_test failing on big-endian systems
86f5e55578 fix: Don't serve files from websockify.
710eb674a5 fix: Correctly pass extended public keys to group moderation code.
021db7031c refactor: Use `struct`s for extended public/secret keys.
a1e999fd80 chore: Compile libsodium reference implementation with compcert.
fbe3c19cf5 cleanup: correct a few nullable annotations
623e3ee5c3 cleanup: Don't use `memcpy` to cast arbitrary `struct`s to `uint8_t[]`.
c71567dc18 fix: Pass array, not array pointer, to `memcmp`.
9b46a08144 cleanup: Never pass `void*` directly to `memcpy`.
5d7b7a7bbc refactor: Use tox rng to seed the keypair generation.
961891d568 cleanup: Small improvements found by PVS Studio.
8201019f0d chore: Disable NGC saving by default, enable through Tox_Options.
5dd9ee3f65 cleanup: Replace pointer arithmetic with explicit `&arr[i]`.
ca4606d49d refactor: Use strong typedef for NGC peer id.
442213b722 cleanup: Simplify custom packet length check in NGC.
08d3393def fix: Correct a few potential null derefs in bootstrap daemon.
b9877b32b0 fix: Add missing memunlock of local variable when it goes out of scope.
dab5fe44b9 fix: Zero out stack-allocated secret key before return.
f058103299 refactor: Make prune_gc_sanctions_list more obviously correct.
3ba7a0dec9 docs: Add static analysis tool list to README.
8d0811a0f3 docs: Run prettier-markdown on markdown files.
969e3a2bfc refactor: Fix network test not using the strong typedef
93c83fbc7c refactor: Use strong typedef instead of struct for `Socket`.
9fe18b176f fix: Fix some false positive from PVS Studio.
7c44379ccb cleanup: Check that WINXP macro exists before comparing it.
5c93231bef refactor: Make tox mutex non-recursive.
aacff73939 docs: Fix up doxyfile.
d55fc85ff5 docs: Add more documentation to crypto_core.
5bdaaaedb6 refactor: Remove `Tox *` from `tox_dispatch`.
e202341e76 refactor: Don't rely on tox_dispatch passing tox in tests.
34df938f52 chore: Use C++ mode for clang-tidy.
8b05296a78 chore: Check that both gtest and gmock exist for tests.
42010660e1 test: Add slimcc compiler compatibility test.
b473630321 chore: Add some comments to the astyle config.
b7404f24f6 cleanup: Remove implicit bool conversions.
4e2dba4d9f chore: Reformat sources with astyle.
4359e3a6bc chore: Rename C++ headers to .hh suffixes.
0c05566e58 cleanup: Further `#include` cleanups.
8d29935b7a chore: Only check the bootstrap daemon checksum on release.
f70e588bc6 cleanup: Add more `const` where possible.
511bfe39c8 cleanup: Use Bazel modules to enforce proper `#include` hygiene.
1710a0d091 refactor: Move pack/unpack `IP_Port` from DHT into network module.
a975943564 chore: Really fix coverage docker image build.
c08409390f chore: Fix post-submit coverage image.
39aadf8922 fix: Don't use `memcmp` to compare `IP_Port`s.
d94246a906 fix: partially fix a bug that prevented group part messages from sending.
eeaa039222 chore: Fix rpm build; add a CI check for it.
8328449c1a chore: Speed up docker builds a bit by reducing layer count.
d6d67d56f3 cleanup: Add `const` where possible in auto tests.
6aa9e6850d cleanup: Minor cleanup of event unpack code.
bdf460a3a9 refactor: Rename `system_{memory,...}` to `os_{memory,...}`.
203e1af81e fix: a few off by one errors in group autotests
5c093c4888 cleanup: Remove all uses of `SIZEOF_VLA`.
662c2140f3 test: Add goblint static analyser.
8f07755834 cleanup: Use `memzero(x, s)` instead of `memset(x, 0, s)`.
a7258e40cf cleanup: Use explicit 0 instead of `PACKET_ID_PADDING`.
6370d0f15d cleanup: Expand the `Tox_Options` accessor macros.
14a1a0b9bd cleanup: Remove plan9 support.
a05dccad13 test: Add a simple new/delete test for Tox.
1cdcf938b9 cleanup: Add comment after every `#endif`.
ba99d4dc4b test: Fix comment I broke in the events test PR.
e07248debb refactor: Migrate auto_tests to new events API.
bdd42b5452 refactor: Add common msgpack array packer with callback.
3c659f5288 cleanup: Rename group to conference in groupav documentation.
89957be230 cleanup: Ensure handler params are named after callback params.
c650d9d345 refactor: Pass `this` pointer as first param to s11n callbacks.
e7fb91ddb8 refactor: Allow NULL pointers for byte arrays in events.
5e2c8cabc1 cleanup: make some improvements to group moderation test
259de4867e cleanup: Remove `bin_pack_{new,free}`.
21a8ff5895 cleanup: skip a do_gc iteration before removing peers marked for deletion
16809dc36e feat: Add dht_get_nodes_response event to the events system.

git-subtree-dir: external/toxcore/c-toxcore
git-subtree-split: b03b5712720de9a9901ea12fd741f177327a7021
This commit is contained in:
2024-03-07 23:12:55 +01:00
parent 61accfe184
commit aae086cc65
358 changed files with 8093 additions and 5229 deletions

View File

@ -18,6 +18,7 @@
#include "TCP_connection.h"
#include "TCP_server.h"
#include "announce.h"
#include "attributes.h"
#include "bin_pack.h"
#include "bin_unpack.h"
#include "ccompat.h"
@ -138,25 +139,25 @@ void getaddress(const Messenger *m, uint8_t *address)
non_null()
static bool send_online_packet(Messenger *m, int friendcon_id)
{
uint8_t packet = PACKET_ID_ONLINE;
return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, friendcon_id), &packet,
const uint8_t packet[1] = {PACKET_ID_ONLINE};
return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, friendcon_id), packet,
sizeof(packet), false) != -1;
}
non_null()
static bool send_offline_packet(Messenger *m, int friendcon_id)
{
uint8_t packet = PACKET_ID_OFFLINE;
return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, friendcon_id), &packet,
const uint8_t packet[1] = {PACKET_ID_OFFLINE};
return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, friendcon_id), packet,
sizeof(packet), false) != -1;
}
non_null(1) nullable(4)
static int m_handle_status(void *object, int i, bool status, void *userdata);
static int m_handle_status(void *object, int friendcon_id, bool status, void *userdata);
non_null(1, 3) nullable(5)
static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t len, void *userdata);
static int m_handle_packet(void *object, int friendcon_id, const uint8_t *data, uint16_t length, void *userdata);
non_null(1, 3) nullable(5)
static int m_handle_lossy_packet(void *object, int friend_num, const uint8_t *packet, uint16_t length,
static int m_handle_lossy_packet(void *object, int friendcon_id, const uint8_t *data, uint16_t length,
void *userdata);
non_null()
@ -403,7 +404,7 @@ bool m_create_group_connection(Messenger *m, GC_Chat *chat)
const int onion_friend_number = friend_conn_get_onion_friendnum(connection);
Onion_Friend *onion_friend = onion_get_friend(m->onion_c, (uint16_t)onion_friend_number);
onion_friend_set_gc_public_key(onion_friend, get_chat_id(chat->chat_public_key));
onion_friend_set_gc_public_key(onion_friend, get_chat_id(&chat->chat_public_key));
onion_friend_set_gc_data(onion_friend, nullptr, 0);
return true;
@ -472,7 +473,7 @@ int m_delfriend(Messenger *m, int32_t friendnumber)
}
if (m->friend_connectionstatuschange_internal != nullptr) {
m->friend_connectionstatuschange_internal(m, friendnumber, 0, m->friend_connectionstatuschange_internal_userdata);
m->friend_connectionstatuschange_internal(m, friendnumber, false, m->friend_connectionstatuschange_internal_userdata);
}
clear_receipts(m, friendnumber);
@ -591,7 +592,7 @@ int m_send_message_generic(Messenger *m, int32_t friendnumber, uint8_t type, con
memcpy(packet + 1, message, length);
const int64_t packet_num = write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
m->friendlist[friendnumber].friendcon_id), packet, length + 1, false);
m->friendlist[friendnumber].friendcon_id), packet, length + 1, false);
if (packet_num == -1) {
return -4;
@ -857,7 +858,7 @@ int m_copy_statusmessage(const Messenger *m, int32_t friendnumber, uint8_t *buf,
const uint32_t msglen = min_u32(maxlen, m->friendlist[friendnumber].statusmessage_length);
memcpy(buf, m->friendlist[friendnumber].statusmessage, msglen);
memset(buf + msglen, 0, maxlen - msglen);
memzero(buf + msglen, maxlen - msglen);
return msglen;
}
@ -1097,7 +1098,6 @@ static void set_friend_status(Messenger *m, int32_t friendnumber, uint8_t status
/*** CONFERENCES */
/** @brief Set the callback for conference invites. */
void m_callback_conference_invite(Messenger *m, m_conference_invite_cb *function)
{
@ -1120,20 +1120,17 @@ bool send_conference_invite_packet(const Messenger *m, int32_t friendnumber, con
return write_cryptpacket_id(m, friendnumber, PACKET_ID_INVITE_CONFERENCE, data, length, false);
}
/** @brief Send a group invite packet.
*
* @retval true if success
*/
bool send_group_invite_packet(const Messenger *m, uint32_t friendnumber, const uint8_t *data, uint16_t length)
bool send_group_invite_packet(const Messenger *m, uint32_t friendnumber, const uint8_t *packet, uint16_t length)
{
return write_cryptpacket_id(m, friendnumber, PACKET_ID_INVITE_GROUPCHAT, data, length, false);
return write_cryptpacket_id(m, friendnumber, PACKET_ID_INVITE_GROUPCHAT, packet, length, false);
}
/*** FILE SENDING */
/** @brief Set the callback for file send requests. */
void callback_file_sendrequest(Messenger *m, m_file_recv_cb *function)
{
@ -1195,8 +1192,8 @@ int file_get_id(const Messenger *m, int32_t friendnumber, uint32_t filenumber, u
file_number = temp_filenum;
const struct File_Transfers *const ft = inbound
? &m->friendlist[friendnumber].file_receiving[file_number]
: &m->friendlist[friendnumber].file_sending[file_number];
? &m->friendlist[friendnumber].file_receiving[file_number]
: &m->friendlist[friendnumber].file_sending[file_number];
if (ft->status == FILESTATUS_NONE) {
return -2;
@ -1223,7 +1220,8 @@ static bool file_sendrequest(const Messenger *m, int32_t friendnumber, uint8_t f
return false;
}
VLA(uint8_t, packet, 1 + sizeof(file_type) + sizeof(filesize) + FILE_ID_LENGTH + filename_length);
const uint16_t packet_size = 1 + sizeof(file_type) + sizeof(filesize) + FILE_ID_LENGTH + filename_length;
VLA(uint8_t, packet, packet_size);
packet[0] = filenumber;
file_type = net_htonl(file_type);
memcpy(packet + 1, &file_type, sizeof(file_type));
@ -1234,7 +1232,7 @@ static bool file_sendrequest(const Messenger *m, int32_t friendnumber, uint8_t f
memcpy(packet + 1 + sizeof(file_type) + sizeof(filesize) + FILE_ID_LENGTH, filename, filename_length);
}
return write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_SENDREQUEST, packet, SIZEOF_VLA(packet), false);
return write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_SENDREQUEST, packet, packet_size, false);
}
/** @brief Send a file send request.
@ -1301,7 +1299,8 @@ static bool send_file_control_packet(const Messenger *m, int32_t friendnumber, b
return false;
}
VLA(uint8_t, packet, 3 + data_length);
const uint16_t packet_size = 3 + data_length;
VLA(uint8_t, packet, packet_size);
packet[0] = inbound ? 1 : 0;
packet[1] = filenumber;
@ -1311,7 +1310,7 @@ static bool send_file_control_packet(const Messenger *m, int32_t friendnumber, b
memcpy(packet + 3, data, data_length);
}
return write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_CONTROL, packet, SIZEOF_VLA(packet), false);
return write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_CONTROL, packet, packet_size, false);
}
/** @brief Send a file control request.
@ -1501,7 +1500,8 @@ static int64_t send_file_data_packet(const Messenger *m, int32_t friendnumber, u
return -1;
}
VLA(uint8_t, packet, 2 + length);
const uint16_t packet_size = 2 + length;
VLA(uint8_t, packet, packet_size);
packet[0] = PACKET_ID_FILE_DATA;
packet[1] = filenumber;
@ -1510,7 +1510,7 @@ static int64_t send_file_data_packet(const Messenger *m, int32_t friendnumber, u
}
return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
m->friendlist[friendnumber].friendcon_id), packet, SIZEOF_VLA(packet), true);
m->friendlist[friendnumber].friendcon_id), packet, packet_size, true);
}
#define MAX_FILE_DATA_SIZE (MAX_CRYPTO_DATA_SIZE - 2)
@ -1712,7 +1712,6 @@ static void do_reqchunk_filecb(Messenger *m, int32_t friendnumber, void *userdat
}
}
/** @brief Run this when the friend disconnects.
* Kill all current file transfers.
*/
@ -1873,28 +1872,28 @@ bool m_msi_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data,
return write_cryptpacket_id(m, friendnumber, PACKET_ID_MSI, data, length, false);
}
static int m_handle_lossy_packet(void *object, int friend_num, const uint8_t *packet, uint16_t length,
static int m_handle_lossy_packet(void *object, int friendcon_id, const uint8_t *data, uint16_t length,
void *userdata)
{
Messenger *m = (Messenger *)object;
if (!m_friend_exists(m, friend_num)) {
if (!m_friend_exists(m, friendcon_id)) {
return 1;
}
if (packet[0] <= PACKET_ID_RANGE_LOSSY_AV_END) {
if (data[0] <= PACKET_ID_RANGE_LOSSY_AV_END) {
const RTP_Packet_Handler *const ph =
&m->friendlist[friend_num].lossy_rtp_packethandlers[packet[0] % PACKET_ID_RANGE_LOSSY_AV_SIZE];
&m->friendlist[friendcon_id].lossy_rtp_packethandlers[data[0] % PACKET_ID_RANGE_LOSSY_AV_SIZE];
if (ph->function != nullptr) {
return ph->function(m, friend_num, packet, length, ph->object);
return ph->function(m, friendcon_id, data, length, ph->object);
}
return 1;
}
if (m->lossy_packethandler != nullptr) {
m->lossy_packethandler(m, friend_num, packet[0], packet, length, userdata);
m->lossy_packethandler(m, friendcon_id, data[0], data, length, userdata);
}
return 1;
@ -1921,7 +1920,6 @@ int m_callback_rtp_packet(Messenger *m, int32_t friendnumber, uint8_t byte, m_lo
return 0;
}
/** @brief High level function to send custom lossy packets.
*
* TODO(oxij): this name is confusing, because this function sends both av and custom lossy packets.
@ -2020,11 +2018,11 @@ int send_custom_lossless_packet(const Messenger *m, int32_t friendnumber, const
/** Function to filter out some friend requests*/
non_null()
static int friend_already_added(const uint8_t *real_pk, void *data)
static int friend_already_added(void *object, const uint8_t *public_key)
{
const Messenger *m = (const Messenger *)data;
const Messenger *m = (const Messenger *)object;
if (getfriend_id(m, real_pk) == -1) {
if (getfriend_id(m, public_key) == -1) {
return 0;
}
@ -2034,16 +2032,16 @@ static int friend_already_added(const uint8_t *real_pk, void *data)
/** @brief Check for and handle a timed-out friend request.
*
* If the request has timed-out then the friend status is set back to FRIEND_ADDED.
* @param i friendlist index of the timed-out friend
* @param friendcon_id friendlist index of the timed-out friend
* @param t time
*/
non_null(1) nullable(4)
static void check_friend_request_timed_out(Messenger *m, uint32_t i, uint64_t t, void *userdata)
static void check_friend_request_timed_out(Messenger *m, uint32_t friendcon_id, uint64_t t, void *userdata)
{
Friend *f = &m->friendlist[i];
Friend *f = &m->friendlist[friendcon_id];
if (f->friendrequest_lastsent + f->friendrequest_timeout < t) {
set_friend_status(m, i, FRIEND_ADDED, userdata);
set_friend_status(m, friendcon_id, FRIEND_ADDED, userdata);
/* Double the default timeout every time if friendrequest is assumed
* to have been sent unsuccessfully.
*/
@ -2052,15 +2050,15 @@ static void check_friend_request_timed_out(Messenger *m, uint32_t i, uint64_t t,
}
non_null(1) nullable(4)
static int m_handle_status(void *object, int i, bool status, void *userdata)
static int m_handle_status(void *object, int friendcon_id, bool status, void *userdata)
{
Messenger *m = (Messenger *)object;
if (status) { /* Went online. */
send_online_packet(m, m->friendlist[i].friendcon_id);
send_online_packet(m, m->friendlist[friendcon_id].friendcon_id);
} else { /* Went offline. */
if (m->friendlist[i].status == FRIEND_ONLINE) {
set_friend_status(m, i, FRIEND_CONFIRMED, userdata);
if (m->friendlist[friendcon_id].status == FRIEND_ONLINE) {
set_friend_status(m, friendcon_id, FRIEND_CONFIRMED, userdata);
}
}
@ -2068,17 +2066,17 @@ static int m_handle_status(void *object, int i, bool status, void *userdata)
}
non_null(1, 3) nullable(5)
static int m_handle_packet_offline(Messenger *m, const int i, const uint8_t *data, const uint16_t data_length, void *userdata)
static int m_handle_packet_offline(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
{
if (data_length == 0) {
set_friend_status(m, i, FRIEND_CONFIRMED, userdata);
set_friend_status(m, friendcon_id, FRIEND_CONFIRMED, userdata);
}
return 0;
}
non_null(1, 3) nullable(5)
static int m_handle_packet_nickname(Messenger *m, const int i, const uint8_t *data, const uint16_t data_length, void *userdata)
static int m_handle_packet_nickname(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
{
if (data_length > MAX_NAME_LENGTH) {
return 0;
@ -2091,17 +2089,17 @@ static int m_handle_packet_nickname(Messenger *m, const int i, const uint8_t *da
/* inform of namechange before we overwrite the old name */
if (m->friend_namechange != nullptr) {
m->friend_namechange(m, i, data_terminated, data_length, userdata);
m->friend_namechange(m, friendcon_id, data_terminated, data_length, userdata);
}
memcpy(m->friendlist[i].name, data_terminated, data_length);
m->friendlist[i].name_length = data_length;
memcpy(m->friendlist[friendcon_id].name, data_terminated, data_length);
m->friendlist[friendcon_id].name_length = data_length;
return 0;
}
non_null(1, 3) nullable(5)
static int m_handle_packet_statusmessage(Messenger *m, const int i, const uint8_t *data, const uint16_t data_length, void *userdata)
static int m_handle_packet_statusmessage(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
{
if (data_length > MAX_STATUSMESSAGE_LENGTH) {
return 0;
@ -2113,16 +2111,16 @@ static int m_handle_packet_statusmessage(Messenger *m, const int i, const uint8_
data_terminated[data_length] = 0;
if (m->friend_statusmessagechange != nullptr) {
m->friend_statusmessagechange(m, i, data_terminated, data_length, userdata);
m->friend_statusmessagechange(m, friendcon_id, data_terminated, data_length, userdata);
}
set_friend_statusmessage(m, i, data_terminated, data_length);
set_friend_statusmessage(m, friendcon_id, data_terminated, data_length);
return 0;
}
non_null(1, 3) nullable(5)
static int m_handle_packet_userstatus(Messenger *m, const int i, const uint8_t *data, const uint16_t data_length, void *userdata)
static int m_handle_packet_userstatus(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
{
if (data_length != 1) {
return 0;
@ -2134,16 +2132,16 @@ static int m_handle_packet_userstatus(Messenger *m, const int i, const uint8_t *
}
if (m->friend_userstatuschange != nullptr) {
m->friend_userstatuschange(m, i, status, userdata);
m->friend_userstatuschange(m, friendcon_id, status, userdata);
}
set_friend_userstatus(m, i, status);
set_friend_userstatus(m, friendcon_id, status);
return 0;
}
non_null(1, 3) nullable(5)
static int m_handle_packet_typing(Messenger *m, const int i, const uint8_t *data, const uint16_t data_length, void *userdata)
static int m_handle_packet_typing(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
{
if (data_length != 1) {
return 0;
@ -2151,17 +2149,17 @@ static int m_handle_packet_typing(Messenger *m, const int i, const uint8_t *data
const bool typing = data[0] != 0;
set_friend_typing(m, i, typing);
set_friend_typing(m, friendcon_id, typing);
if (m->friend_typingchange != nullptr) {
m->friend_typingchange(m, i, typing, userdata);
m->friend_typingchange(m, friendcon_id, typing, userdata);
}
return 0;
}
non_null(1, 3) nullable(6)
static int m_handle_packet_message(Messenger *m, const int i, const uint8_t *data, const uint16_t data_length, const Message_Type message_type, void *userdata)
static int m_handle_packet_message(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, const Message_Type message_type, void *userdata)
{
if (data_length == 0) {
return 0;
@ -2176,28 +2174,28 @@ static int m_handle_packet_message(Messenger *m, const int i, const uint8_t *dat
message_terminated[message_length] = 0;
if (m->friend_message != nullptr) {
m->friend_message(m, i, message_type, message_terminated, message_length, userdata);
m->friend_message(m, friendcon_id, message_type, message_terminated, message_length, userdata);
}
return 0;
}
non_null(1, 3) nullable(5)
static int m_handle_packet_invite_conference(Messenger *m, const int i, const uint8_t *data, const uint16_t data_length, void *userdata)
static int m_handle_packet_invite_conference(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
{
if (data_length == 0) {
return 0;
}
if (m->conference_invite != nullptr) {
m->conference_invite(m, i, data, data_length, userdata);
m->conference_invite(m, friendcon_id, data, data_length, userdata);
}
return 0;
}
non_null(1, 3) nullable(5)
static int m_handle_packet_file_sendrequest(Messenger *m, const int i, const uint8_t *data, const uint16_t data_length, void *userdata)
static int m_handle_packet_file_sendrequest(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
{
const unsigned int head_length = 1 + sizeof(uint32_t) + sizeof(uint64_t) + FILE_ID_LENGTH;
@ -2213,7 +2211,7 @@ static int m_handle_packet_file_sendrequest(Messenger *m, const int i, const uin
return 0;
}
#endif
#endif /* UINT8_MAX >= MAX_CONCURRENT_FILE_PIPES */
uint64_t filesize;
uint32_t file_type;
@ -2227,7 +2225,7 @@ static int m_handle_packet_file_sendrequest(Messenger *m, const int i, const uin
file_type = net_ntohl(file_type);
net_unpack_u64(data + 1 + sizeof(uint32_t), &filesize);
struct File_Transfers *ft = &m->friendlist[i].file_receiving[filenumber];
struct File_Transfers *ft = &m->friendlist[friendcon_id].file_receiving[filenumber];
if (ft->status != FILESTATUS_NONE) {
return 0;
@ -2254,7 +2252,7 @@ static int m_handle_packet_file_sendrequest(Messenger *m, const int i, const uin
real_filenumber <<= 16;
if (m->file_sendrequest != nullptr) {
m->file_sendrequest(m, i, real_filenumber, file_type, filesize, filename, filename_length,
m->file_sendrequest(m, friendcon_id, real_filenumber, file_type, filesize, filename, filename_length,
userdata);
}
@ -2262,7 +2260,7 @@ static int m_handle_packet_file_sendrequest(Messenger *m, const int i, const uin
}
non_null(1, 3) nullable(5)
static int m_handle_packet_file_control(Messenger *m, const int i, const uint8_t *data, const uint16_t data_length, void *userdata)
static int m_handle_packet_file_control(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
{
if (data_length < 3) {
return 0;
@ -2281,9 +2279,9 @@ static int m_handle_packet_file_control(Messenger *m, const int i, const uint8_t
return 0;
}
#endif
#endif /* UINT8_MAX >= MAX_CONCURRENT_FILE_PIPES */
if (handle_filecontrol(m, i, outbound, filenumber, control_type, data + 3, data_length - 3, userdata) == -1) {
if (handle_filecontrol(m, friendcon_id, outbound, filenumber, control_type, data + 3, data_length - 3, userdata) == -1) {
// TODO(iphydf): Do something different here? Right now, this
// check is pointless.
return 0;
@ -2293,7 +2291,7 @@ static int m_handle_packet_file_control(Messenger *m, const int i, const uint8_t
}
non_null(1, 3) nullable(5)
static int m_handle_packet_file_data(Messenger *m, const int i, const uint8_t *data, const uint16_t data_length, void *userdata)
static int m_handle_packet_file_data(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
{
if (data_length < 1) {
return 0;
@ -2307,9 +2305,9 @@ static int m_handle_packet_file_data(Messenger *m, const int i, const uint8_t *d
return 0;
}
#endif
#endif /* UINT8_MAX >= MAX_CONCURRENT_FILE_PIPES */
struct File_Transfers *ft = &m->friendlist[i].file_receiving[filenumber];
struct File_Transfers *ft = &m->friendlist[friendcon_id].file_receiving[filenumber];
if (ft->status != FILESTATUS_TRANSFERRING) {
return 0;
@ -2334,7 +2332,7 @@ static int m_handle_packet_file_data(Messenger *m, const int i, const uint8_t *d
}
if (m->file_filedata != nullptr) {
m->file_filedata(m, i, real_filenumber, position, file_data, file_data_length, userdata);
m->file_filedata(m, friendcon_id, real_filenumber, position, file_data, file_data_length, userdata);
}
ft->transferred += file_data_length;
@ -2346,7 +2344,7 @@ static int m_handle_packet_file_data(Messenger *m, const int i, const uint8_t *d
/* Full file received. */
if (m->file_filedata != nullptr) {
m->file_filedata(m, i, real_filenumber, position, file_data, file_data_length, userdata);
m->file_filedata(m, friendcon_id, real_filenumber, position, file_data, file_data_length, userdata);
}
}
@ -2359,21 +2357,21 @@ static int m_handle_packet_file_data(Messenger *m, const int i, const uint8_t *d
}
non_null(1, 3) nullable(5)
static int m_handle_packet_msi(Messenger *m, const int i, const uint8_t *data, const uint16_t data_length, void *userdata)
static int m_handle_packet_msi(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
{
if (data_length == 0) {
return 0;
}
if (m->msi_packet != nullptr) {
m->msi_packet(m, i, data, data_length, m->msi_packet_userdata);
m->msi_packet(m, friendcon_id, data, data_length, m->msi_packet_userdata);
}
return 0;
}
non_null(1, 3) nullable(5)
static int m_handle_packet_invite_groupchat(Messenger *m, const int i, const uint8_t *data, const uint16_t data_length, void *userdata)
static int m_handle_packet_invite_groupchat(Messenger *m, const int friendcon_id, const uint8_t *data, const uint16_t data_length, void *userdata)
{
// first two bytes are messenger packet type and group invite type
if (data_length < 2 + GC_JOIN_DATA_LENGTH) {
@ -2386,35 +2384,35 @@ static int m_handle_packet_invite_groupchat(Messenger *m, const int i, const uin
if (m->group_invite != nullptr && data[1] == GROUP_INVITE && data_length != 2 + GC_JOIN_DATA_LENGTH) {
if (group_not_added(m->group_handler, join_data, join_data_len)) {
m->group_invite(m, i, join_data, GC_JOIN_DATA_LENGTH,
m->group_invite(m, friendcon_id, join_data, GC_JOIN_DATA_LENGTH,
join_data + GC_JOIN_DATA_LENGTH, join_data_len - GC_JOIN_DATA_LENGTH, userdata);
}
} else if (invite_type == GROUP_INVITE_ACCEPTED) {
handle_gc_invite_accepted_packet(m->group_handler, i, join_data, join_data_len);
handle_gc_invite_accepted_packet(m->group_handler, friendcon_id, join_data, join_data_len);
} else if (invite_type == GROUP_INVITE_CONFIRMATION) {
handle_gc_invite_confirmed_packet(m->group_handler, i, join_data, join_data_len);
handle_gc_invite_confirmed_packet(m->group_handler, friendcon_id, join_data, join_data_len);
}
return 0;
}
non_null(1, 3) nullable(5)
static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t len, void *userdata)
static int m_handle_packet(void *object, int friendcon_id, const uint8_t *data, uint16_t length, void *userdata)
{
Messenger *m = (Messenger *)object;
if (len == 0) {
if (length == 0) {
return -1;
}
const uint8_t packet_id = temp[0];
const uint8_t *data = temp + 1;
const uint16_t data_length = len - 1;
const uint8_t packet_id = data[0];
const uint8_t *payload = data + 1;
const uint16_t payload_length = length - 1;
if (m->friendlist[i].status != FRIEND_ONLINE) {
if (packet_id == PACKET_ID_ONLINE && len == 1) {
set_friend_status(m, i, FRIEND_ONLINE, userdata);
send_online_packet(m, m->friendlist[i].friendcon_id);
if (m->friendlist[friendcon_id].status != FRIEND_ONLINE) {
if (packet_id == PACKET_ID_ONLINE && length == 1) {
set_friend_status(m, friendcon_id, FRIEND_ONLINE, userdata);
send_online_packet(m, m->friendlist[friendcon_id].friendcon_id);
} else {
return -1;
}
@ -2423,34 +2421,34 @@ static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t le
switch (packet_id) {
// TODO(Green-Sky): now all return 0 on error AND success, make errors errors?
case PACKET_ID_OFFLINE:
return m_handle_packet_offline(m, i, data, data_length, userdata);
return m_handle_packet_offline(m, friendcon_id, payload, payload_length, userdata);
case PACKET_ID_NICKNAME:
return m_handle_packet_nickname(m, i, data, data_length, userdata);
return m_handle_packet_nickname(m, friendcon_id, payload, payload_length, userdata);
case PACKET_ID_STATUSMESSAGE:
return m_handle_packet_statusmessage(m, i, data, data_length, userdata);
return m_handle_packet_statusmessage(m, friendcon_id, payload, payload_length, userdata);
case PACKET_ID_USERSTATUS:
return m_handle_packet_userstatus(m, i, data, data_length, userdata);
return m_handle_packet_userstatus(m, friendcon_id, payload, payload_length, userdata);
case PACKET_ID_TYPING:
return m_handle_packet_typing(m, i, data, data_length, userdata);
return m_handle_packet_typing(m, friendcon_id, payload, payload_length, userdata);
case PACKET_ID_MESSAGE:
return m_handle_packet_message(m, i, data, data_length, MESSAGE_NORMAL, userdata);
return m_handle_packet_message(m, friendcon_id, payload, payload_length, MESSAGE_NORMAL, userdata);
case PACKET_ID_ACTION:
return m_handle_packet_message(m, i, data, data_length, MESSAGE_ACTION, userdata);
return m_handle_packet_message(m, friendcon_id, payload, payload_length, MESSAGE_ACTION, userdata);
case PACKET_ID_INVITE_CONFERENCE:
return m_handle_packet_invite_conference(m, i, data, data_length, userdata);
return m_handle_packet_invite_conference(m, friendcon_id, payload, payload_length, userdata);
case PACKET_ID_FILE_SENDREQUEST:
return m_handle_packet_file_sendrequest(m, i, data, data_length, userdata);
return m_handle_packet_file_sendrequest(m, friendcon_id, payload, payload_length, userdata);
case PACKET_ID_FILE_CONTROL:
return m_handle_packet_file_control(m, i, data, data_length, userdata);
return m_handle_packet_file_control(m, friendcon_id, payload, payload_length, userdata);
case PACKET_ID_FILE_DATA:
return m_handle_packet_file_data(m, i, data, data_length, userdata);
return m_handle_packet_file_data(m, friendcon_id, payload, payload_length, userdata);
case PACKET_ID_MSI:
return m_handle_packet_msi(m, i, data, data_length, userdata);
return m_handle_packet_msi(m, friendcon_id, payload, payload_length, userdata);
case PACKET_ID_INVITE_GROUPCHAT:
return m_handle_packet_invite_groupchat(m, i, data, data_length, userdata);
return m_handle_packet_invite_groupchat(m, friendcon_id, payload, payload_length, userdata);
}
return handle_custom_lossless_packet(object, i, temp, len, userdata);
return handle_custom_lossless_packet(object, friendcon_id, data, length, userdata);
}
non_null(1) nullable(2)
@ -2461,8 +2459,8 @@ static void do_friends(Messenger *m, void *userdata)
for (uint32_t i = 0; i < m->numfriends; ++i) {
if (m->friendlist[i].status == FRIEND_ADDED) {
const int fr = send_friend_request_packet(m->fr_c, m->friendlist[i].friendcon_id, m->friendlist[i].friendrequest_nospam,
m->friendlist[i].info,
m->friendlist[i].info_size);
m->friendlist[i].info,
m->friendlist[i].info_size);
if (fr >= 0) {
set_friend_status(m, i, FRIEND_REQUESTED, userdata);
@ -2528,7 +2526,6 @@ static void m_connection_status_callback(Messenger *m, void *userdata)
}
}
#define DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS 60UL
#define IDSTRING_LEN (CRYPTO_PUBLIC_KEY_SIZE * 2 + 1)
@ -2591,14 +2588,14 @@ static bool self_announce_group(const Messenger *m, GC_Chat *chat, Onion_Friend
}
announce.base_announce.tcp_relays_count = (uint8_t)tcp_num;
announce.base_announce.ip_port_is_set = (uint8_t)(ip_port_is_set ? 1 : 0);
announce.base_announce.ip_port_is_set = ip_port_is_set;
if (ip_port_is_set) {
memcpy(&announce.base_announce.ip_port, &chat->self_ip_port, sizeof(IP_Port));
}
memcpy(announce.base_announce.peer_public_key, chat->self_public_key, ENC_PUBLIC_KEY_SIZE);
memcpy(announce.chat_public_key, get_chat_id(chat->chat_public_key), ENC_PUBLIC_KEY_SIZE);
memcpy(announce.base_announce.peer_public_key, chat->self_public_key.enc, ENC_PUBLIC_KEY_SIZE);
memcpy(announce.chat_public_key, get_chat_id(&chat->chat_public_key), ENC_PUBLIC_KEY_SIZE);
uint8_t gc_data[GCA_MAX_DATA_LENGTH];
const int length = gca_pack_public_announce(m->log, gc_data, GCA_MAX_DATA_LENGTH, &announce);
@ -2620,7 +2617,7 @@ static bool self_announce_group(const Messenger *m, GC_Chat *chat, Onion_Friend
if (tcp_num > 0) {
pk_copy(chat->announced_tcp_relay_pk, announce.base_announce.tcp_relays[0].public_key);
} else {
memset(chat->announced_tcp_relay_pk, 0, sizeof(chat->announced_tcp_relay_pk));
memzero(chat->announced_tcp_relay_pk, sizeof(chat->announced_tcp_relay_pk));
}
LOGGER_DEBUG(chat->log, "Published group announce. TCP relays: %d, UDP status: %d", tcp_num,
@ -2721,7 +2718,6 @@ void do_messenger(Messenger *m, void *userdata)
}
}
/* dht contains additional "friends" (requests) */
const uint32_t num_dhtfriends = dht_get_num_friends(m->dht);
VLA(int32_t, m2dht, num_dhtfriends);
@ -2889,7 +2885,6 @@ static uint8_t *friend_save(const struct Saved_Friend *temp, uint8_t *data)
return data;
}
non_null()
static const uint8_t *friend_load(struct Saved_Friend *temp, const uint8_t *data)
{
@ -2927,7 +2922,6 @@ static const uint8_t *friend_load(struct Saved_Friend *temp, const uint8_t *data
return data;
}
non_null()
static uint32_t m_state_plugins_size(const Messenger *m)
{
@ -2956,7 +2950,7 @@ bool m_register_state_plugin(Messenger *m, State_Type type, m_state_size_cb *siz
{
const uint32_t new_length = m->options.state_plugins_length + 1;
Messenger_State_Plugin *temp = (Messenger_State_Plugin *)mem_vrealloc(
m->mem, m->options.state_plugins, new_length, sizeof(Messenger_State_Plugin));
m->mem, m->options.state_plugins, new_length, sizeof(Messenger_State_Plugin));
if (temp == nullptr) {
return false;
@ -3117,7 +3111,7 @@ static uint8_t *friends_list_save(const Messenger *m, uint8_t *data)
assert(next_data - cur_data == friend_size());
#ifdef __LP64__
assert(memcmp(cur_data, &temp, friend_size()) == 0);
#endif
#endif /* __LP64__ */
cur_data = next_data;
++num;
}
@ -3191,7 +3185,7 @@ static void pack_groupchats(const GC_Session *c, Bin_Pack *bp)
}
non_null()
static bool pack_groupchats_handler(Bin_Pack *bp, const Logger *log, const void *obj)
static bool pack_groupchats_handler(const void *obj, const Logger *logger, Bin_Pack *bp)
{
const GC_Session *session = (const GC_Session *)obj;
pack_groupchats(session, bp);
@ -3201,8 +3195,8 @@ static bool pack_groupchats_handler(Bin_Pack *bp, const Logger *log, const void
non_null()
static uint32_t saved_groups_size(const Messenger *m)
{
const GC_Session *c = m->group_handler;
return bin_pack_obj_size(pack_groupchats_handler, m->log, c);
const GC_Session *session = m->group_handler;
return bin_pack_obj_size(pack_groupchats_handler, session, m->log);
}
non_null()
@ -3224,7 +3218,7 @@ static uint8_t *groups_save(const Messenger *m, uint8_t *data)
data = state_write_section_header(data, STATE_COOKIE_TYPE, len, STATE_TYPE_GROUPS);
if (!bin_pack_obj(pack_groupchats_handler, m->log, c, data, len)) {
if (!bin_pack_obj(pack_groupchats_handler, c, m->log, data, len)) {
LOGGER_FATAL(m->log, "failed to pack group chats into buffer of length %u", len);
return data;
}
@ -3237,22 +3231,17 @@ static uint8_t *groups_save(const Messenger *m, uint8_t *data)
}
non_null()
static State_Load_Status groups_load(Messenger *m, const uint8_t *data, uint32_t length)
static bool handle_groups_load(void *obj, Bin_Unpack *bu)
{
Bin_Unpack *bu = bin_unpack_new(data, length);
if (bu == nullptr) {
LOGGER_ERROR(m->log, "failed to allocate binary unpacker");
return STATE_LOAD_STATUS_ERROR;
}
Messenger *m = (Messenger *)obj;
uint32_t num_groups;
if (!bin_unpack_array(bu, &num_groups)) {
LOGGER_ERROR(m->log, "msgpack failed to unpack groupchats array: expected array");
bin_unpack_free(bu);
return STATE_LOAD_STATUS_ERROR;
return false;
}
LOGGER_DEBUG(m->log, "Loading %u groups (length %u)", num_groups, length);
LOGGER_DEBUG(m->log, "Loading %u groups", num_groups);
for (uint32_t i = 0; i < num_groups; ++i) {
const int group_number = gc_group_load(m->group_handler, bu);
@ -3266,7 +3255,16 @@ static State_Load_Status groups_load(Messenger *m, const uint8_t *data, uint32_t
LOGGER_DEBUG(m->log, "Successfully loaded %u groups", gc_count_groups(m->group_handler));
bin_unpack_free(bu);
return true;
}
non_null()
static State_Load_Status groups_load(Messenger *m, const uint8_t *data, uint32_t length)
{
if (!bin_unpack_obj(handle_groups_load, m, data, length)) {
LOGGER_ERROR(m->log, "msgpack failed to unpack groupchats array");
return STATE_LOAD_STATUS_ERROR;
}
return STATE_LOAD_STATUS_CONTINUE;
}
@ -3412,10 +3410,9 @@ static uint32_t path_node_size(const Messenger *m)
non_null()
static uint8_t *save_path_nodes(const Messenger *m, uint8_t *data)
{
Node_format nodes[NUM_SAVED_PATH_NODES];
Node_format nodes[NUM_SAVED_PATH_NODES] = {{{0}}};
uint8_t *temp_data = data;
data = state_write_section_header(data, STATE_COOKIE_TYPE, 0, STATE_TYPE_PATH_NODE);
memset(nodes, 0, sizeof(nodes));
const unsigned int num = onion_backup_nodes(m->onion_c, nodes, NUM_SAVED_PATH_NODES);
const int l = pack_nodes(m->log, data, NUM_SAVED_PATH_NODES * packed_node_size(net_family_tcp_ipv6()), nodes, num);
@ -3457,7 +3454,9 @@ static void m_register_default_plugins(Messenger *m)
m_register_state_plugin(m, STATE_TYPE_STATUSMESSAGE, status_message_size, load_status_message,
save_status_message);
m_register_state_plugin(m, STATE_TYPE_STATUS, status_size, load_status, save_status);
m_register_state_plugin(m, STATE_TYPE_GROUPS, saved_groups_size, groups_load, groups_save);
if (m->options.groups_persistence_enabled) {
m_register_state_plugin(m, STATE_TYPE_GROUPS, saved_groups_size, groups_load, groups_save);
}
m_register_state_plugin(m, STATE_TYPE_TCP_RELAY, tcp_relay_size, load_tcp_relays, save_tcp_relays);
m_register_state_plugin(m, STATE_TYPE_PATH_NODE, path_node_size, load_path_nodes, save_path_nodes);
}