diff --git a/external/toxcore/c-toxcore/.circleci/config.yml b/external/toxcore/c-toxcore/.circleci/config.yml index 2fd8c05..264139a 100644 --- a/external/toxcore/c-toxcore/.circleci/config.yml +++ b/external/toxcore/c-toxcore/.circleci/config.yml @@ -6,7 +6,8 @@ workflows: circleci: jobs: - bazel-asan - - bazel-msan + - bazel-dbg + - bazel-opt - clang-analyze - cpplint - static-analysis @@ -24,15 +25,25 @@ jobs: - run: .circleci/bazel-test //c-toxcore/... - bazel-msan: + bazel-dbg: working_directory: /tmp/cirrus-ci-build docker: - - image: toxchat/toktok-stack:latest-msan + - image: toxchat/toktok-stack:latest-debug steps: - checkout - run: .circleci/bazel-test - //c-toxcore/auto_tests:lossless_packet_test + //c-toxcore/... + + bazel-opt: + working_directory: /tmp/cirrus-ci-build + docker: + - image: toxchat/toktok-stack:latest-release + + steps: + - checkout + - run: .circleci/bazel-test + //c-toxcore/... static-analysis: working_directory: ~/work diff --git a/external/toxcore/c-toxcore/.cirrus.yml b/external/toxcore/c-toxcore/.cirrus.yml index cd54173..d434f09 100644 --- a/external/toxcore/c-toxcore/.cirrus.yml +++ b/external/toxcore/c-toxcore/.cirrus.yml @@ -1,18 +1,16 @@ --- bazel-opt_task: - timeout_in: 5m + timeout_in: 10m container: image: toxchat/toktok-stack:latest-release - cpu: 2 + cpu: 8 memory: 2G configure_script: - git submodule update --init --recursive - /src/workspace/tools/inject-repo c-toxcore test_all_script: - - cd /src/workspace && bazel - --max_idle_secs=5 + - cd /src/workspace && tools/retry 5 bazel test -k - --remote_cache=http://$CIRRUS_HTTP_CACHE_HOST --build_tag_filters=-haskell --test_tag_filters=-haskell -- @@ -20,25 +18,38 @@ bazel-opt_task: -//c-toxcore/auto_tests:tcp_relay_test # Cirrus doesn't allow external network connections. bazel-dbg_task: - timeout_in: 5m + timeout_in: 10m container: image: toxchat/toktok-stack:latest-debug - cpu: 2 + cpu: 8 memory: 2G configure_script: - git submodule update --init --recursive - /src/workspace/tools/inject-repo c-toxcore test_all_script: - - cd /src/workspace && bazel - --max_idle_secs=5 + - cd /src/workspace && tools/retry 5 bazel test -k - --remote_cache=http://$CIRRUS_HTTP_CACHE_HOST --build_tag_filters=-haskell --test_tag_filters=-haskell -- //c-toxcore/... -//c-toxcore/auto_tests:tcp_relay_test # Cirrus doesn't allow external network connections. +bazel-msan_task: + timeout_in: 10m + container: + image: toxchat/toktok-stack:latest-msan + cpu: 4 + memory: 2G + configure_script: + - git submodule update --init --recursive + - /src/workspace/tools/inject-repo c-toxcore + test_all_script: + - cd /src/workspace && tools/retry 5 bazel + test -k + -- + //c-toxcore/auto_tests:lossless_packet_test + cimple_task: timeout_in: 5m container: @@ -50,9 +61,7 @@ cimple_task: - /src/workspace/tools/inject-repo c-toxcore test_all_script: - cd /src/workspace && bazel - --max_idle_secs=5 test -k - --remote_cache=http://$CIRRUS_HTTP_CACHE_HOST --build_tag_filters=haskell --test_tag_filters=haskell -- @@ -71,6 +80,7 @@ freebsd_task: libconfig libsodium libvpx + ninja opus pkgconf - git submodule update --init --recursive @@ -84,6 +94,7 @@ freebsd_task: -DNON_HERMETIC_TESTS=OFF \ -DTEST_TIMEOUT_SECONDS=50 \ -DUSE_IPV6=OFF \ - -DAUTOTEST=ON + -DAUTOTEST=ON \ + -GNinja cmake --build . --target install - ctest -j50 --output-on-failure --rerun-failed --repeat until-pass:6 + ctest -j50 --output-on-failure --rerun-failed --repeat until-pass:3 || ctest -j50 --output-on-failure --rerun-failed --repeat until-pass:3 diff --git a/external/toxcore/c-toxcore/.github/scripts/cmake-freebsd b/external/toxcore/c-toxcore/.github/scripts/cmake-freebsd index 1301175..568241e 100755 --- a/external/toxcore/c-toxcore/.github/scripts/cmake-freebsd +++ b/external/toxcore/c-toxcore/.github/scripts/cmake-freebsd @@ -42,6 +42,7 @@ RUN "cmake -B_build -Hc-toxcore \ -DCMAKE_EXE_LINKER_FLAGS='$LD_FLAGS' \ -DCMAKE_SHARED_LINKER_FLAGS='$LD_FLAGS' \ -DCMAKE_INSTALL_PREFIX:PATH='_install' \ + -DENABLE_SHARED=OFF \ -DMIN_LOGGER_LEVEL=TRACE \ -DMUST_BUILD_TOXAV=ON \ -DNON_HERMETIC_TESTS=ON \ diff --git a/external/toxcore/c-toxcore/.github/workflows/ci.yml b/external/toxcore/c-toxcore/.github/workflows/ci.yml index 99cbe7b..0b832d3 100644 --- a/external/toxcore/c-toxcore/.github/workflows/ci.yml +++ b/external/toxcore/c-toxcore/.github/workflows/ci.yml @@ -127,6 +127,7 @@ jobs: libopus libsodium libvpx + ninja pkg-config run: | @@ -138,7 +139,8 @@ jobs: -DNON_HERMETIC_TESTS=ON \ -DTEST_TIMEOUT_SECONDS=90 \ -DUSE_IPV6=OFF \ - -DAUTOTEST=ON + -DAUTOTEST=ON \ + -GNinja cmake --build . --target install ctest -j50 --output-on-failure --rerun-failed --repeat until-pass:6 @@ -163,6 +165,7 @@ jobs: libconfig libsodium libvpx + ninja opus pkgconf @@ -175,7 +178,8 @@ jobs: -DNON_HERMETIC_TESTS=ON \ -DTEST_TIMEOUT_SECONDS=50 \ -DUSE_IPV6=OFF \ - -DAUTOTEST=ON + -DAUTOTEST=ON \ + -GNinja cmake --build . --target install ctest -j50 --output-on-failure --rerun-failed --repeat until-pass:6 diff --git a/external/toxcore/c-toxcore/.gitignore b/external/toxcore/c-toxcore/.gitignore index 33b7971..33d31c0 100644 --- a/external/toxcore/c-toxcore/.gitignore +++ b/external/toxcore/c-toxcore/.gitignore @@ -36,6 +36,7 @@ testing/data # Vim *.swp +*.nvimlog # Object files *.o diff --git a/external/toxcore/c-toxcore/BUILD.bazel b/external/toxcore/c-toxcore/BUILD.bazel index db5f569..481b46a 100644 --- a/external/toxcore/c-toxcore/BUILD.bazel +++ b/external/toxcore/c-toxcore/BUILD.bazel @@ -8,18 +8,24 @@ genrule( srcs = [ "//c-toxcore/toxav:toxav.h", "//c-toxcore/toxcore:tox.h", + "//c-toxcore/toxcore:tox_dispatch.h", + "//c-toxcore/toxcore:tox_events.h", "//c-toxcore/toxcore:tox_private.h", "//c-toxcore/toxencryptsave:toxencryptsave.h", ], outs = [ "tox/toxav.h", "tox/tox.h", + "tox/tox_dispatch.h", + "tox/tox_events.h", "tox/tox_private.h", "tox/toxencryptsave.h", ], cmd = """ cp $(location //c-toxcore/toxav:toxav.h) $(GENDIR)/c-toxcore/tox/toxav.h cp $(location //c-toxcore/toxcore:tox.h) $(GENDIR)/c-toxcore/tox/tox.h + cp $(location //c-toxcore/toxcore:tox_dispatch.h) $(GENDIR)/c-toxcore/tox/tox_dispatch.h + cp $(location //c-toxcore/toxcore:tox_events.h) $(GENDIR)/c-toxcore/tox/tox_events.h cp $(location //c-toxcore/toxcore:tox_private.h) $(GENDIR)/c-toxcore/tox/tox_private.h cp $(location //c-toxcore/toxencryptsave:toxencryptsave.h) $(GENDIR)/c-toxcore/tox/toxencryptsave.h """, diff --git a/external/toxcore/c-toxcore/CMakeLists.txt b/external/toxcore/c-toxcore/CMakeLists.txt index 4d6cf22..9237d21 100644 --- a/external/toxcore/c-toxcore/CMakeLists.txt +++ b/external/toxcore/c-toxcore/CMakeLists.txt @@ -309,6 +309,8 @@ set(toxcore_SOURCES toxcore/mono_time.h toxcore/net_crypto.c toxcore/net_crypto.h + toxcore/net_profile.c + toxcore/net_profile.h toxcore/network.c toxcore/network.h toxcore/onion_announce.c diff --git a/external/toxcore/c-toxcore/auto_tests/CMakeLists.txt b/external/toxcore/c-toxcore/auto_tests/CMakeLists.txt index 8413e1d..996846b 100644 --- a/external/toxcore/c-toxcore/auto_tests/CMakeLists.txt +++ b/external/toxcore/c-toxcore/auto_tests/CMakeLists.txt @@ -71,6 +71,7 @@ auto_test(invalid_udp_proxy) auto_test(lan_discovery) auto_test(lossless_packet) auto_test(lossy_packet) +auto_test(netprof) auto_test(network) auto_test(onion) auto_test(overflow_recvq) diff --git a/external/toxcore/c-toxcore/auto_tests/TCP_test.c b/external/toxcore/c-toxcore/auto_tests/TCP_test.c index b59fa94..1cf7748 100644 --- a/external/toxcore/c-toxcore/auto_tests/TCP_test.c +++ b/external/toxcore/c-toxcore/auto_tests/TCP_test.c @@ -8,7 +8,7 @@ #include "../toxcore/TCP_server.h" #include "../toxcore/crypto_core.h" #include "../toxcore/mono_time.h" -#include "../toxcore/util.h" +#include "../toxcore/network.h" #include "auto_test_support.h" #define NUM_PORTS 3 @@ -74,8 +74,9 @@ static void test_basic(void) for (uint8_t i = 0; i < NUM_PORTS; i++) { sock = net_socket(ns, net_family_ipv6(), TOX_SOCK_STREAM, TOX_PROTO_TCP); localhost.port = net_htons(ports[i]); - bool ret = net_connect(ns, mem, logger, sock, &localhost); - ck_assert_msg(ret, "Failed to connect to created TCP relay server on port %d (%d).", ports[i], errno); + Net_Err_Connect err; + bool ret = net_connect(ns, mem, logger, sock, &localhost, &err); + ck_assert_msg(ret, "Failed to connect to created TCP relay server on port %d (%d, %s).", ports[i], errno, net_err_connect_to_string(err)); // Leave open one connection for the next test. if (i + 1 < NUM_PORTS) { @@ -111,12 +112,12 @@ static void test_basic(void) // Sending the handshake ck_assert_msg(net_send(ns, logger, sock, handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1, - &localhost) == TCP_CLIENT_HANDSHAKE_SIZE - 1, + &localhost, nullptr) == TCP_CLIENT_HANDSHAKE_SIZE - 1, "An attempt to send the initial handshake minus last byte failed."); do_tcp_server_delay(tcp_s, mono_time, 50); - ck_assert_msg(net_send(ns, logger, sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1, &localhost) == 1, + ck_assert_msg(net_send(ns, logger, sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1, &localhost, nullptr) == 1, "The attempt to send the last byte of handshake failed."); free(handshake); @@ -155,7 +156,7 @@ static void test_basic(void) msg_length = sizeof(r_req) - i; } - ck_assert_msg(net_send(ns, logger, sock, r_req + i, msg_length, &localhost) == msg_length, + ck_assert_msg(net_send(ns, logger, sock, r_req + i, msg_length, &localhost, nullptr) == msg_length, "Failed to send request after completing the handshake."); i += msg_length; @@ -165,7 +166,9 @@ static void test_basic(void) } // Receiving the second response and verifying its validity - uint8_t packet_resp[4096]; + const size_t max_packet_size = 4096; + uint8_t *packet_resp = (uint8_t *)malloc(max_packet_size); + ck_assert(packet_resp != nullptr); int recv_data_len = net_recv(ns, logger, sock, packet_resp, 2 + 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE, &localhost); ck_assert_msg(recv_data_len == 2 + 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE, "Failed to receive server response to request. %d", recv_data_len); @@ -173,7 +176,8 @@ static void test_basic(void) ck_assert_msg(net_ntohs(size) == 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE, "Wrong packet size for request response."); - uint8_t packet_resp_plain[4096]; + uint8_t *packet_resp_plain = (uint8_t *)malloc(max_packet_size); + ck_assert(packet_resp_plain != nullptr); ret = decrypt_data_symmetric(mem, f_shared_key, f_nonce_r, packet_resp + 2, recv_data_len - 2, packet_resp_plain); ck_assert_msg(ret != -1, "Failed to decrypt the TCP server's response."); increment_nonce(f_nonce_r); @@ -183,6 +187,9 @@ static void test_basic(void) ck_assert_msg(packet_resp_plain[1] == 0, "Server did not refuse the connection."); ck_assert_msg(pk_equal(packet_resp_plain + 2, f_public_key), "Server sent the wrong public key."); + free(packet_resp_plain); + free(packet_resp); + // Closing connections. kill_sock(ns, sock); kill_tcp_server(tcp_s); @@ -213,8 +220,9 @@ static struct sec_TCP_con *new_tcp_con(const Logger *logger, const Memory *mem, localhost.ip = get_loopback(); localhost.port = net_htons(ports[random_u32(rng) % NUM_PORTS]); - bool ok = net_connect(ns, mem, logger, sock, &localhost); - ck_assert_msg(ok, "Failed to connect to the test TCP relay server."); + Net_Err_Connect err; + bool ok = net_connect(ns, mem, logger, sock, &localhost, &err); + ck_assert_msg(ok, "Failed to connect to the test TCP relay server: %s.", net_err_connect_to_string(err)); uint8_t f_secret_key[CRYPTO_SECRET_KEY_SIZE]; crypto_new_keypair(rng, sec_c->public_key, f_secret_key); @@ -234,12 +242,12 @@ static struct sec_TCP_con *new_tcp_con(const Logger *logger, const Memory *mem, "Failed to encrypt the outgoing handshake."); ck_assert_msg(net_send(ns, logger, sock, handshake, TCP_CLIENT_HANDSHAKE_SIZE - 1, - &localhost) == TCP_CLIENT_HANDSHAKE_SIZE - 1, + &localhost, nullptr) == TCP_CLIENT_HANDSHAKE_SIZE - 1, "Failed to send the first portion of the handshake to the TCP relay server."); do_tcp_server_delay(tcp_s, mono_time, 50); - ck_assert_msg(net_send(ns, logger, sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1, &localhost) == 1, + ck_assert_msg(net_send(ns, logger, sock, handshake + (TCP_CLIENT_HANDSHAKE_SIZE - 1), 1, &localhost, nullptr) == 1, "Failed to send last byte of handshake."); do_tcp_server_delay(tcp_s, mono_time, 50); @@ -283,7 +291,7 @@ static int write_packet_tcp_test_connection(const Logger *logger, struct sec_TCP localhost.ip = get_loopback(); localhost.port = 0; - ck_assert_msg(net_send(con->ns, logger, con->sock, packet, packet_size, &localhost) == packet_size, + ck_assert_msg(net_send(con->ns, logger, con->sock, packet, packet_size, &localhost, nullptr) == packet_size, "Failed to send a packet."); return 0; } @@ -337,7 +345,8 @@ static void test_some(void) do_tcp_server_delay(tcp_s, mono_time, 50); // Testing response from connection 1 - uint8_t data[2048]; + const size_t max_packet_size = 4096; + uint8_t *data = (uint8_t *)malloc(max_packet_size); int len = read_packet_sec_tcp(logger, con1, data, 2 + 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE); ck_assert_msg(len == 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE, "Wrong response packet length of %d.", len); ck_assert_msg(data[0] == TCP_PACKET_ROUTING_RESPONSE, "Wrong response packet id of %d.", data[0]); @@ -351,7 +360,7 @@ static void test_some(void) ck_assert_msg(data[1] == 16, "Server didn't refuse connection using wrong public key."); ck_assert_msg(pk_equal(data + 2, con1->public_key), "Key in response packet wrong."); - uint8_t test_packet[512] = {16, 17, 16, 86, 99, 127, 255, 189, 78}; // What is this packet???? + const uint8_t test_packet[512] = {16, 17, 16, 86, 99, 127, 255, 189, 78}; // What is this packet???? write_packet_tcp_test_connection(logger, con3, test_packet, sizeof(test_packet)); write_packet_tcp_test_connection(logger, con3, test_packet, sizeof(test_packet)); @@ -406,6 +415,8 @@ static void test_some(void) ck_assert_msg(data[0] == TCP_PACKET_PONG, "wrong packet id %u", data[0]); ck_assert_msg(memcmp(ping_packet + 1, data + 1, sizeof(uint64_t)) == 0, "wrong packet data"); + free(data); + // Kill off the connections kill_tcp_server(tcp_s); kill_tcp_con(con1); @@ -524,7 +535,8 @@ static void test_client(void) ip_port_tcp_s.port = net_htons(ports[random_u32(rng) % NUM_PORTS]); ip_port_tcp_s.ip = get_loopback(); - TCP_Client_Connection *conn = new_tcp_connection(logger, mem, mono_time, rng, ns, &ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, nullptr); + TCP_Client_Connection *conn = new_tcp_connection(logger, mem, mono_time, rng, ns, &ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, nullptr, nullptr); + ck_assert_msg(conn != nullptr, "Failed to create a TCP client connection."); // TCP sockets might need a moment before they can be written to. c_sleep(50); do_tcp_connection(logger, mono_time, conn, nullptr); @@ -560,7 +572,8 @@ static void test_client(void) crypto_new_keypair(rng, f2_public_key, f2_secret_key); ip_port_tcp_s.port = net_htons(ports[random_u32(rng) % NUM_PORTS]); TCP_Client_Connection *conn2 = new_tcp_connection(logger, mem, mono_time, rng, ns, &ip_port_tcp_s, self_public_key, f2_public_key, - f2_secret_key, nullptr); + f2_secret_key, nullptr, nullptr); + ck_assert_msg(conn2 != nullptr, "Failed to create a second TCP client connection."); c_sleep(50); // The client should call this function (defined earlier) during the routing process. @@ -657,7 +670,8 @@ static void test_client_invalid(void) ip_port_tcp_s.port = net_htons(ports[random_u32(rng) % NUM_PORTS]); ip_port_tcp_s.ip = get_loopback(); TCP_Client_Connection *conn = new_tcp_connection(logger, mem, mono_time, rng, ns, &ip_port_tcp_s, - self_public_key, f_public_key, f_secret_key, nullptr); + self_public_key, f_public_key, f_secret_key, nullptr, nullptr); + ck_assert_msg(conn != nullptr, "Failed to create a TCP client connection."); // Run the client's main loop but not the server. mono_time_update(mono_time); @@ -734,10 +748,12 @@ static void test_tcp_connection(void) proxy_info.proxy_type = TCP_PROXY_NONE; crypto_new_keypair(rng, self_public_key, self_secret_key); TCP_Connections *tc_1 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info); + ck_assert_msg(tc_1 != nullptr, "Failed to create TCP connections"); ck_assert_msg(pk_equal(tcp_connections_public_key(tc_1), self_public_key), "Wrong public key"); crypto_new_keypair(rng, self_public_key, self_secret_key); TCP_Connections *tc_2 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info); + ck_assert_msg(tc_2 != nullptr, "Failed to create TCP connections"); ck_assert_msg(pk_equal(tcp_connections_public_key(tc_2), self_public_key), "Wrong public key"); IP_Port ip_port_tcp_s; @@ -849,10 +865,12 @@ static void test_tcp_connection2(void) proxy_info.proxy_type = TCP_PROXY_NONE; crypto_new_keypair(rng, self_public_key, self_secret_key); TCP_Connections *tc_1 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info); + ck_assert_msg(tc_1 != nullptr, "Failed to create TCP connections"); ck_assert_msg(pk_equal(tcp_connections_public_key(tc_1), self_public_key), "Wrong public key"); crypto_new_keypair(rng, self_public_key, self_secret_key); TCP_Connections *tc_2 = new_tcp_connections(logger, mem, rng, ns, mono_time, self_secret_key, &proxy_info); + ck_assert_msg(tc_2 != nullptr, "Failed to create TCP connections"); ck_assert_msg(pk_equal(tcp_connections_public_key(tc_2), self_public_key), "Wrong public key"); IP_Port ip_port_tcp_s; diff --git a/external/toxcore/c-toxcore/auto_tests/announce_test.c b/external/toxcore/c-toxcore/auto_tests/announce_test.c index ef93095..7e387c2 100644 --- a/external/toxcore/c-toxcore/auto_tests/announce_test.c +++ b/external/toxcore/c-toxcore/auto_tests/announce_test.c @@ -66,7 +66,7 @@ static void test_store_data(void) ck_assert(net != nullptr); DHT *dht = new_dht(log, mem, rng, ns, mono_time, net, true, true); ck_assert(dht != nullptr); - Forwarding *forwarding = new_forwarding(log, rng, mono_time, dht); + Forwarding *forwarding = new_forwarding(log, mem, rng, mono_time, dht); ck_assert(forwarding != nullptr); Announcements *announce = new_announcements(log, mem, rng, mono_time, forwarding); ck_assert(announce != nullptr); diff --git a/external/toxcore/c-toxcore/auto_tests/auto_test_support.c b/external/toxcore/c-toxcore/auto_tests/auto_test_support.c index 213e705..483e313 100644 --- a/external/toxcore/c-toxcore/auto_tests/auto_test_support.c +++ b/external/toxcore/c-toxcore/auto_tests/auto_test_support.c @@ -444,9 +444,9 @@ void print_debug_log(Tox *m, Tox_Log_Level level, const char *file, uint32_t lin } } -void print_debug_logger(void *context, Logger_Level level, const char *file, int line, const char *func, const char *message, void *userdata) +void print_debug_logger(void *context, Logger_Level level, const char *file, uint32_t line, const char *func, const char *message, void *userdata) { - print_debug_log(nullptr, (Tox_Log_Level) level, file, (uint32_t) line, func, message, userdata); + print_debug_log(nullptr, (Tox_Log_Level) level, file, line, func, message, userdata); } Tox *tox_new_log_lan(struct Tox_Options *options, Tox_Err_New *err, void *log_user_data, bool lan_discovery) diff --git a/external/toxcore/c-toxcore/auto_tests/auto_test_support.h b/external/toxcore/c-toxcore/auto_tests/auto_test_support.h index b9132e1..cd883e2 100644 --- a/external/toxcore/c-toxcore/auto_tests/auto_test_support.h +++ b/external/toxcore/c-toxcore/auto_tests/auto_test_support.h @@ -60,7 +60,7 @@ void print_debug_log(Tox *m, Tox_Log_Level level, const char *file, uint32_t lin const char *message, void *user_data); // Use this function when setting the log callback on a Logger object -void print_debug_logger(void *context, Logger_Level level, const char *file, int line, +void print_debug_logger(void *context, Logger_Level level, const char *file, uint32_t line, const char *func, const char *message, void *userdata); Tox *tox_new_log(struct Tox_Options *options, Tox_Err_New *err, void *log_user_data); diff --git a/external/toxcore/c-toxcore/auto_tests/file_saving_test.c b/external/toxcore/c-toxcore/auto_tests/file_saving_test.c index 58a1b6d..9198920 100644 --- a/external/toxcore/c-toxcore/auto_tests/file_saving_test.c +++ b/external/toxcore/c-toxcore/auto_tests/file_saving_test.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2016 Tox project. */ diff --git a/external/toxcore/c-toxcore/auto_tests/forwarding_test.c b/external/toxcore/c-toxcore/auto_tests/forwarding_test.c index 3ba85ed..a1b8d6d 100644 --- a/external/toxcore/c-toxcore/auto_tests/forwarding_test.c +++ b/external/toxcore/c-toxcore/auto_tests/forwarding_test.c @@ -129,7 +129,7 @@ static Forwarding_Subtox *new_forwarding_subtox(const Memory *mem, bool no_udp, const TCP_Proxy_Info inf = {{{{0}}}}; subtox->c = new_net_crypto(subtox->log, mem, rng, ns, subtox->mono_time, subtox->dht, &inf); - subtox->forwarding = new_forwarding(subtox->log, rng, subtox->mono_time, subtox->dht); + subtox->forwarding = new_forwarding(subtox->log, mem, rng, subtox->mono_time, subtox->dht); ck_assert(subtox->forwarding != nullptr); subtox->announce = new_announcements(subtox->log, mem, rng, subtox->mono_time, subtox->forwarding); diff --git a/external/toxcore/c-toxcore/auto_tests/group_general_test.c b/external/toxcore/c-toxcore/auto_tests/group_general_test.c index a4a4a0f..9e953e5 100644 --- a/external/toxcore/c-toxcore/auto_tests/group_general_test.c +++ b/external/toxcore/c-toxcore/auto_tests/group_general_test.c @@ -392,9 +392,9 @@ static void group_announce_test(AutoTox *autotoxes) ck_assert(s_err == TOX_ERR_GROUP_SELF_STATUS_SET_OK); fprintf(stderr, "Peer 0 reconnecting...\n"); - Tox_Err_Group_Reconnect r_err; - tox_group_reconnect(tox0, groupnumber, &r_err); - ck_assert(r_err == TOX_ERR_GROUP_RECONNECT_OK); + Tox_Err_Group_Join err_rejoin; + tox_group_join(tox0, chat_id, (const uint8_t *)PEER0_NICK, PEER0_NICK_LEN, nullptr, 0, &err_rejoin); + ck_assert(err_rejoin == TOX_ERR_GROUP_JOIN_OK); while (state1->peer_joined_count != 2 && state0->self_joined_count == 2) { iterate_all_wait(autotoxes, NUM_GROUP_TOXES, ITERATION_INTERVAL); diff --git a/external/toxcore/c-toxcore/auto_tests/netprof_test.c b/external/toxcore/c-toxcore/auto_tests/netprof_test.c new file mode 100644 index 0000000..6b1fcd8 --- /dev/null +++ b/external/toxcore/c-toxcore/auto_tests/netprof_test.c @@ -0,0 +1,134 @@ +/** Auto Tests: basic network profile functionality test (UDP only) + */ + +#include +#include +#include + +#include "../toxcore/tox_private.h" +#include "../toxcore/util.h" + +#include "auto_test_support.h" +#include "check_compat.h" + +#define NUM_TOXES 2 + +static void test_netprof(AutoTox *autotoxes) +{ + // Send some messages to create fake traffic + for (size_t i = 0; i < 256; ++i) { + for (uint32_t j = 0; j < NUM_TOXES; ++j) { + tox_friend_send_message(autotoxes[j].tox, 0, TOX_MESSAGE_TYPE_NORMAL, (const uint8_t *)"test", 4, nullptr); + } + + iterate_all_wait(autotoxes, NUM_TOXES, ITERATION_INTERVAL); + } + + // idle traffic for a while + for (size_t i = 0; i < 100; ++i) { + iterate_all_wait(autotoxes, NUM_TOXES, ITERATION_INTERVAL); + } + + const Tox *tox1 = autotoxes[0].tox; + + const uint64_t UDP_count_sent1 = tox_netprof_get_packet_total_count(tox1, TOX_NETPROF_PACKET_TYPE_UDP, + TOX_NETPROF_DIRECTION_SENT); + const uint64_t UDP_count_recv1 = tox_netprof_get_packet_total_count(tox1, TOX_NETPROF_PACKET_TYPE_UDP, + TOX_NETPROF_DIRECTION_RECV); + const uint64_t TCP_count_sent1 = tox_netprof_get_packet_total_count(tox1, TOX_NETPROF_PACKET_TYPE_TCP, + TOX_NETPROF_DIRECTION_SENT); + const uint64_t TCP_count_recv1 = tox_netprof_get_packet_total_count(tox1, TOX_NETPROF_PACKET_TYPE_TCP, + TOX_NETPROF_DIRECTION_RECV); + + const uint64_t UDP_bytes_sent1 = tox_netprof_get_packet_total_bytes(tox1, TOX_NETPROF_PACKET_TYPE_UDP, + TOX_NETPROF_DIRECTION_SENT); + const uint64_t UDP_bytes_recv1 = tox_netprof_get_packet_total_bytes(tox1, TOX_NETPROF_PACKET_TYPE_UDP, + TOX_NETPROF_DIRECTION_RECV); + const uint64_t TCP_bytes_sent1 = tox_netprof_get_packet_total_bytes(tox1, TOX_NETPROF_PACKET_TYPE_TCP, + TOX_NETPROF_DIRECTION_SENT); + const uint64_t TCP_bytes_recv1 = tox_netprof_get_packet_total_bytes(tox1, TOX_NETPROF_PACKET_TYPE_TCP, + TOX_NETPROF_DIRECTION_RECV); + + ck_assert(UDP_count_recv1 > 0 && UDP_count_sent1 > 0); + ck_assert(UDP_bytes_recv1 > 0 && UDP_bytes_sent1 > 0); + + (void)TCP_count_sent1; + (void)TCP_bytes_sent1; + (void)TCP_bytes_recv1; + (void)TCP_count_recv1; + + uint64_t total_sent_count = 0; + uint64_t total_recv_count = 0; + uint64_t total_sent_bytes = 0; + uint64_t total_recv_bytes = 0; + + // tox1 makes sure the sum value of all packet ID's is equal to the totals + for (size_t i = 0; i < 256; ++i) { + // this id isn't valid for UDP packets but we still want to call the + // functions and make sure they return some non-zero value + if (i == TOX_NETPROF_PACKET_ID_TCP_DATA) { + ck_assert(tox_netprof_get_packet_id_count(tox1, TOX_NETPROF_PACKET_TYPE_UDP, i, + TOX_NETPROF_DIRECTION_SENT) > 0); + ck_assert(tox_netprof_get_packet_id_bytes(tox1, TOX_NETPROF_PACKET_TYPE_UDP, i, + TOX_NETPROF_DIRECTION_SENT) > 0); + ck_assert(tox_netprof_get_packet_id_bytes(tox1, TOX_NETPROF_PACKET_TYPE_UDP, i, + TOX_NETPROF_DIRECTION_SENT) > 0); + ck_assert(tox_netprof_get_packet_id_bytes(tox1, TOX_NETPROF_PACKET_TYPE_UDP, i, + TOX_NETPROF_DIRECTION_RECV) > 0); + continue; + } + + total_sent_count += tox_netprof_get_packet_id_count(tox1, TOX_NETPROF_PACKET_TYPE_UDP, i, + TOX_NETPROF_DIRECTION_SENT); + total_recv_count += tox_netprof_get_packet_id_count(tox1, TOX_NETPROF_PACKET_TYPE_UDP, i, + TOX_NETPROF_DIRECTION_RECV); + + total_sent_bytes += tox_netprof_get_packet_id_bytes(tox1, TOX_NETPROF_PACKET_TYPE_UDP, i, + TOX_NETPROF_DIRECTION_SENT); + total_recv_bytes += tox_netprof_get_packet_id_bytes(tox1, TOX_NETPROF_PACKET_TYPE_UDP, i, + TOX_NETPROF_DIRECTION_RECV); + } + + const uint64_t total_packets = total_sent_count + total_recv_count; + ck_assert_msg(total_packets == UDP_count_sent1 + UDP_count_recv1, + "%" PRIu64 "does not match %" PRIu64 "\n", total_packets, UDP_count_sent1 + UDP_count_recv1); + + ck_assert_msg(total_sent_count == UDP_count_sent1, "%" PRIu64 " does not match %" PRIu64 "\n", total_sent_count, UDP_count_sent1); + ck_assert_msg(total_recv_count == UDP_count_recv1, "%" PRIu64 " does not match %" PRIu64"\n", total_recv_count, UDP_count_recv1); + + + const uint64_t total_bytes = total_sent_bytes + total_recv_bytes; + ck_assert_msg(total_bytes == UDP_bytes_sent1 + UDP_bytes_recv1, + "%" PRIu64 "does not match %" PRIu64 "\n", total_bytes, UDP_bytes_sent1 + UDP_bytes_recv1); + + ck_assert_msg(total_sent_bytes == UDP_bytes_sent1, "%" PRIu64 " does not match %" PRIu64 "\n", total_sent_bytes, UDP_bytes_sent1); + ck_assert_msg(total_recv_bytes == UDP_bytes_recv1, "%" PRIu64 " does not match %" PRIu64 "\n", total_recv_bytes, UDP_bytes_recv1); +} + +int main(void) +{ + setvbuf(stdout, nullptr, _IONBF, 0); + + Tox_Err_Options_New options_err; + struct Tox_Options *tox_opts = tox_options_new(&options_err); + + ck_assert_msg(options_err == TOX_ERR_OPTIONS_NEW_OK, "Failed to initialize tox options: %d\n", options_err); + + tox_options_default(tox_opts); + tox_options_set_udp_enabled(tox_opts, true); + + Run_Auto_Options autotox_opts = default_run_auto_options(); + autotox_opts.graph = GRAPH_COMPLETE; + + run_auto_test(tox_opts, NUM_TOXES, test_netprof, 0, &autotox_opts); + + // TODO(Jfreegman): uncomment this when TCP autotests are fixed + // tox_options_set_udp_enabled(tox_opts, false); + // run_auto_test(tox_opts, NUM_TOXES, test_netprof, 0, &autotox_opts); + + tox_options_free(tox_opts); + + return 0; +} + +#undef NUM_TOXES diff --git a/external/toxcore/c-toxcore/auto_tests/proxy_test.c b/external/toxcore/c-toxcore/auto_tests/proxy_test.c index d20c14f..66d6bc9 100644 --- a/external/toxcore/c-toxcore/auto_tests/proxy_test.c +++ b/external/toxcore/c-toxcore/auto_tests/proxy_test.c @@ -1,9 +1,9 @@ /* Tests that we can send messages to friends. */ +#include #include #include -#include #include "auto_test_support.h" diff --git a/external/toxcore/c-toxcore/auto_tests/save_compatibility_test.c b/external/toxcore/c-toxcore/auto_tests/save_compatibility_test.c index 75dd4f2..6267f66 100644 --- a/external/toxcore/c-toxcore/auto_tests/save_compatibility_test.c +++ b/external/toxcore/c-toxcore/auto_tests/save_compatibility_test.c @@ -71,22 +71,22 @@ static uint8_t *read_save(const char *save_path, size_t *length) static void test_save_compatibility(const char *save_path) { - struct Tox_Options options = {0}; - tox_options_default(&options); + Tox_Options *options = tox_options_new(nullptr); + ck_assert(options != nullptr); size_t size = 0; uint8_t *save_data = read_save(save_path, &size); ck_assert_msg(save_data != nullptr, "error while reading save file '%s'", save_path); - options.savedata_data = save_data; - options.savedata_length = size; - options.savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE; + tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE); + tox_options_set_savedata_data(options, save_data, size); size_t index = 0; Tox_Err_New err; - Tox *tox = tox_new_log(&options, &err, &index); + Tox *tox = tox_new_log(options, &err, &index); ck_assert_msg(tox, "failed to create tox, error number: %d", err); + tox_options_free(options); free(save_data); const size_t name_size = tox_self_get_name_size(tox); @@ -145,7 +145,10 @@ static bool is_little_endian(void) // cppcheck-suppress constParameter int main(int argc, char *argv[]) { - char base_path[4096] = {0}; + const size_t base_path_size = 4096; + char *base_path = (char *)malloc(base_path_size); + ck_assert(base_path != nullptr); + memset(base_path, 0, 4096); if (argc <= 1) { const char *srcdir = getenv("srcdir"); @@ -154,21 +157,29 @@ int main(int argc, char *argv[]) srcdir = "."; } - snprintf(base_path, sizeof(base_path), "%s", srcdir); + snprintf(base_path, base_path_size, "%s", srcdir); } else { - snprintf(base_path, sizeof(base_path), "%s", argv[1]); + snprintf(base_path, base_path_size, "%s", argv[1]); base_path[strrchr(base_path, '/') - base_path] = '\0'; } if (is_little_endian()) { - char save_path[4096 + sizeof(LOADED_SAVE_FILE_LITTLE)]; - snprintf(save_path, sizeof(save_path), "%s/%s", base_path, LOADED_SAVE_FILE_LITTLE); + const size_t save_path_size = 4096 + sizeof(LOADED_SAVE_FILE_LITTLE); + char *save_path = (char *)malloc(save_path_size); + ck_assert(save_path != nullptr); + snprintf(save_path, save_path_size, "%s/%s", base_path, LOADED_SAVE_FILE_LITTLE); test_save_compatibility(save_path); + free(save_path); } else { - char save_path[4096 + sizeof(LOADED_SAVE_FILE_BIG)]; - snprintf(save_path, sizeof(save_path), "%s/%s", base_path, LOADED_SAVE_FILE_BIG); + const size_t save_path_size = 4096 + sizeof(LOADED_SAVE_FILE_BIG); + char *save_path = (char *)malloc(save_path_size); + ck_assert(save_path != nullptr); + snprintf(save_path, save_path_size, "%s/%s", base_path, LOADED_SAVE_FILE_BIG); test_save_compatibility(save_path); + free(save_path); } + free(base_path); + return 0; } diff --git a/external/toxcore/c-toxcore/auto_tests/tcp_relay_test.c b/external/toxcore/c-toxcore/auto_tests/tcp_relay_test.c index b098f5c..48c24ed 100644 --- a/external/toxcore/c-toxcore/auto_tests/tcp_relay_test.c +++ b/external/toxcore/c-toxcore/auto_tests/tcp_relay_test.c @@ -5,12 +5,19 @@ #include "auto_test_support.h" +#ifndef USE_IPV6 +#define USE_IPV6 1 +#endif + int main(void) { setvbuf(stdout, nullptr, _IONBF, 0); struct Tox_Options *opts = tox_options_new(nullptr); tox_options_set_udp_enabled(opts, false); +#if !USE_IPV6 + tox_options_set_ipv6_enabled(opts, false); +#endif Tox *tox_tcp = tox_new_log(opts, nullptr, nullptr); tox_options_free(opts); diff --git a/external/toxcore/c-toxcore/other/DHT_bootstrap.c b/external/toxcore/c-toxcore/other/DHT_bootstrap.c index c4511aa..629ec60 100644 --- a/external/toxcore/c-toxcore/other/DHT_bootstrap.c +++ b/external/toxcore/c-toxcore/other/DHT_bootstrap.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ @@ -117,10 +117,10 @@ static const char *strlevel(Logger_Level level) } } -static void print_log(void *context, Logger_Level level, const char *file, int line, +static void print_log(void *context, Logger_Level level, const char *file, uint32_t line, const char *func, const char *message, void *userdata) { - fprintf(stderr, "[%s] %s:%d(%s) %s\n", strlevel(level), file, line, func, message); + fprintf(stderr, "[%s] %s:%u(%s) %s\n", strlevel(level), file, line, func, message); } int main(int argc, char *argv[]) @@ -159,8 +159,8 @@ int main(int argc, char *argv[]) const uint16_t end_port = start_port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM); DHT *dht = new_dht(logger, mem, rng, ns, mono_time, new_networking_ex(logger, mem, ns, &ip, start_port, end_port, nullptr), true, true); Onion *onion = new_onion(logger, mem, mono_time, rng, dht); - Forwarding *forwarding = new_forwarding(logger, rng, mono_time, dht); - GC_Announces_List *gc_announces_list = new_gca_list(); + Forwarding *forwarding = new_forwarding(logger, mem, rng, mono_time, dht); + GC_Announces_List *gc_announces_list = new_gca_list(mem); Onion_Announce *onion_a = new_onion_announce(logger, mem, rng, mono_time, dht); #ifdef DHT_NODE_EXTRA_PACKETS @@ -245,7 +245,7 @@ int main(int argc, char *argv[]) bool is_waiting_for_dht_connection = true; uint64_t last_lan_discovery = 0; - const Broadcast_Info *broadcast = lan_discovery_init(ns); + const Broadcast_Info *broadcast = lan_discovery_init(mem, ns); while (true) { mono_time_update(mono_time); diff --git a/external/toxcore/c-toxcore/other/bootstrap_daemon/src/command_line_arguments.c b/external/toxcore/c-toxcore/other/bootstrap_daemon/src/command_line_arguments.c index 3b798ef..8181210 100644 --- a/external/toxcore/c-toxcore/other/bootstrap_daemon/src/command_line_arguments.c +++ b/external/toxcore/c-toxcore/other/bootstrap_daemon/src/command_line_arguments.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2024 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2015-2016 Tox project. */ diff --git a/external/toxcore/c-toxcore/other/bootstrap_daemon/src/command_line_arguments.h b/external/toxcore/c-toxcore/other/bootstrap_daemon/src/command_line_arguments.h index 3be3953..8134efc 100644 --- a/external/toxcore/c-toxcore/other/bootstrap_daemon/src/command_line_arguments.h +++ b/external/toxcore/c-toxcore/other/bootstrap_daemon/src/command_line_arguments.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2015-2016 Tox project. */ diff --git a/external/toxcore/c-toxcore/other/bootstrap_daemon/src/config.c b/external/toxcore/c-toxcore/other/bootstrap_daemon/src/config.c index 19779c5..43ff151 100644 --- a/external/toxcore/c-toxcore/other/bootstrap_daemon/src/config.c +++ b/external/toxcore/c-toxcore/other/bootstrap_daemon/src/config.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2024 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2014-2016 Tox project. */ diff --git a/external/toxcore/c-toxcore/other/bootstrap_daemon/src/config.h b/external/toxcore/c-toxcore/other/bootstrap_daemon/src/config.h index 677dde5..8102036 100644 --- a/external/toxcore/c-toxcore/other/bootstrap_daemon/src/config.h +++ b/external/toxcore/c-toxcore/other/bootstrap_daemon/src/config.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2024 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2014-2016 Tox project. */ diff --git a/external/toxcore/c-toxcore/other/bootstrap_daemon/src/config_defaults.h b/external/toxcore/c-toxcore/other/bootstrap_daemon/src/config_defaults.h index 68f444f..a4cdc65 100644 --- a/external/toxcore/c-toxcore/other/bootstrap_daemon/src/config_defaults.h +++ b/external/toxcore/c-toxcore/other/bootstrap_daemon/src/config_defaults.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2024 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2014-2016 Tox project. */ diff --git a/external/toxcore/c-toxcore/other/bootstrap_daemon/src/global.h b/external/toxcore/c-toxcore/other/bootstrap_daemon/src/global.h index b154e6e..035f4a7 100644 --- a/external/toxcore/c-toxcore/other/bootstrap_daemon/src/global.h +++ b/external/toxcore/c-toxcore/other/bootstrap_daemon/src/global.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2014-2016 Tox project. */ diff --git a/external/toxcore/c-toxcore/other/bootstrap_daemon/src/log.c b/external/toxcore/c-toxcore/other/bootstrap_daemon/src/log.c index 5dde7b5..48d7047 100644 --- a/external/toxcore/c-toxcore/other/bootstrap_daemon/src/log.c +++ b/external/toxcore/c-toxcore/other/bootstrap_daemon/src/log.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2015-2016 Tox project. */ diff --git a/external/toxcore/c-toxcore/other/bootstrap_daemon/src/log.h b/external/toxcore/c-toxcore/other/bootstrap_daemon/src/log.h index 7912625..ba812fa 100644 --- a/external/toxcore/c-toxcore/other/bootstrap_daemon/src/log.h +++ b/external/toxcore/c-toxcore/other/bootstrap_daemon/src/log.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2015-2016 Tox project. */ diff --git a/external/toxcore/c-toxcore/other/bootstrap_daemon/src/log_backend_stdout.c b/external/toxcore/c-toxcore/other/bootstrap_daemon/src/log_backend_stdout.c index 854c767..2ef7bf7 100644 --- a/external/toxcore/c-toxcore/other/bootstrap_daemon/src/log_backend_stdout.c +++ b/external/toxcore/c-toxcore/other/bootstrap_daemon/src/log_backend_stdout.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2015-2016 Tox project. */ diff --git a/external/toxcore/c-toxcore/other/bootstrap_daemon/src/log_backend_stdout.h b/external/toxcore/c-toxcore/other/bootstrap_daemon/src/log_backend_stdout.h index 131d48d..bb6d2cc 100644 --- a/external/toxcore/c-toxcore/other/bootstrap_daemon/src/log_backend_stdout.h +++ b/external/toxcore/c-toxcore/other/bootstrap_daemon/src/log_backend_stdout.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2015-2016 Tox project. */ diff --git a/external/toxcore/c-toxcore/other/bootstrap_daemon/src/log_backend_syslog.c b/external/toxcore/c-toxcore/other/bootstrap_daemon/src/log_backend_syslog.c index c5fccef..de6d228 100644 --- a/external/toxcore/c-toxcore/other/bootstrap_daemon/src/log_backend_syslog.c +++ b/external/toxcore/c-toxcore/other/bootstrap_daemon/src/log_backend_syslog.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2015-2016 Tox project. */ diff --git a/external/toxcore/c-toxcore/other/bootstrap_daemon/src/log_backend_syslog.h b/external/toxcore/c-toxcore/other/bootstrap_daemon/src/log_backend_syslog.h index 8e919f0..1538c92 100644 --- a/external/toxcore/c-toxcore/other/bootstrap_daemon/src/log_backend_syslog.h +++ b/external/toxcore/c-toxcore/other/bootstrap_daemon/src/log_backend_syslog.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2015-2016 Tox project. */ diff --git a/external/toxcore/c-toxcore/other/bootstrap_daemon/src/tox-bootstrapd.c b/external/toxcore/c-toxcore/other/bootstrap_daemon/src/tox-bootstrapd.c index e29afe5..09a54df 100644 --- a/external/toxcore/c-toxcore/other/bootstrap_daemon/src/tox-bootstrapd.c +++ b/external/toxcore/c-toxcore/other/bootstrap_daemon/src/tox-bootstrapd.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2024 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2014-2016 Tox project. */ @@ -200,10 +200,10 @@ static LOG_LEVEL logger_level_to_log_level(Logger_Level level) } } -static void toxcore_logger_callback(void *context, Logger_Level level, const char *file, int line, +static void toxcore_logger_callback(void *context, Logger_Level level, const char *file, uint32_t line, const char *func, const char *message, void *userdata) { - log_write(logger_level_to_log_level(level), "%s:%d(%s) %s\n", file, line, func, message); + log_write(logger_level_to_log_level(level), "%s:%u(%s) %s\n", file, line, func, message); } static volatile sig_atomic_t caught_signal = 0; @@ -348,7 +348,7 @@ int main(int argc, char *argv[]) return 1; } - Forwarding *forwarding = new_forwarding(logger, rng, mono_time, dht); + Forwarding *forwarding = new_forwarding(logger, mem, rng, mono_time, dht); if (forwarding == nullptr) { log_write(LOG_LEVEL_ERROR, "Couldn't initialize forwarding. Exiting.\n"); @@ -377,7 +377,7 @@ int main(int argc, char *argv[]) return 1; } - GC_Announces_List *group_announce = new_gca_list(); + GC_Announces_List *group_announce = new_gca_list(mem); if (group_announce == nullptr) { log_write(LOG_LEVEL_ERROR, "Couldn't initialize group announces. Exiting.\n"); @@ -563,7 +563,7 @@ int main(int argc, char *argv[]) Broadcast_Info *broadcast = nullptr; if (enable_lan_discovery) { - broadcast = lan_discovery_init(ns); + broadcast = lan_discovery_init(mem, ns); log_write(LOG_LEVEL_INFO, "Initialized LAN discovery successfully.\n"); } diff --git a/external/toxcore/c-toxcore/other/bootstrap_daemon/websocket/websockify/go.mod b/external/toxcore/c-toxcore/other/bootstrap_daemon/websocket/websockify/go.mod index 541af4e..aa291f6 100644 --- a/external/toxcore/c-toxcore/other/bootstrap_daemon/websocket/websockify/go.mod +++ b/external/toxcore/c-toxcore/other/bootstrap_daemon/websocket/websockify/go.mod @@ -1,7 +1,7 @@ module github.com/TokTok/c-toxcore/other/bootstrap_daemon/websocket/websockify -go 1.17 +go 1.20 -require github.com/gorilla/websocket v1.5.1 +require github.com/gorilla/websocket v1.5.3 -require golang.org/x/net v0.23.0 // indirect +require golang.org/x/net v0.33.0 // indirect diff --git a/external/toxcore/c-toxcore/other/bootstrap_daemon/websocket/websockify/websockify.go b/external/toxcore/c-toxcore/other/bootstrap_daemon/websocket/websockify/websockify.go index 898af4e..fb3c9c5 100644 --- a/external/toxcore/c-toxcore/other/bootstrap_daemon/websocket/websockify/websockify.go +++ b/external/toxcore/c-toxcore/other/bootstrap_daemon/websocket/websockify/websockify.go @@ -9,7 +9,7 @@ // - Proper error handling in general. // - Support both websocket and regular GET requests on /. // -// Copyright 2022 The TokTok team. +// Copyright 2022-2025 The TokTok team. // Copyright 2021 Michael.liu. // See LICENSE for licensing conditions. diff --git a/external/toxcore/c-toxcore/other/bootstrap_node_packets.c b/external/toxcore/c-toxcore/other/bootstrap_node_packets.c index c250b25..2ca711a 100644 --- a/external/toxcore/c-toxcore/other/bootstrap_node_packets.c +++ b/external/toxcore/c-toxcore/other/bootstrap_node_packets.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ diff --git a/external/toxcore/c-toxcore/other/bootstrap_node_packets.h b/external/toxcore/c-toxcore/other/bootstrap_node_packets.h index c5bda21..9b515fc 100644 --- a/external/toxcore/c-toxcore/other/bootstrap_node_packets.h +++ b/external/toxcore/c-toxcore/other/bootstrap_node_packets.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2015 Tox project. */ diff --git a/external/toxcore/c-toxcore/other/event_tooling/generate_event_c.cpp b/external/toxcore/c-toxcore/other/event_tooling/generate_event_c.cpp index 9ac3d48..51aeca1 100644 --- a/external/toxcore/c-toxcore/other/event_tooling/generate_event_c.cpp +++ b/external/toxcore/c-toxcore/other/event_tooling/generate_event_c.cpp @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPL-3.0-or-later -// Copyright © 2023-2024 The TokTok team. +// Copyright © 2023-2025 The TokTok team. // this file can be used to generate event.c files // requires c++17 @@ -156,7 +156,7 @@ void generate_event_impl(const std::string& event_name, const std::vector +#include +#include + +// ./decrypt_save +int main(int argc, char *argv[]) +{ + if (argc != 4) { + printf("Usage: %s \n", argv[0]); + return 1; + } + FILE *fp = fopen(argv[2], "rb"); + if (!fp) { + printf("Could not open %s\n", argv[2]); + return 1; + } + fseek(fp, 0, SEEK_END); + size_t len = ftell(fp); + fseek(fp, 0, SEEK_SET); + uint8_t *data = (uint8_t *)malloc(len); + if (!data) { + printf("Could not allocate memory\n"); + fclose(fp); + return 1; + } + + if (fread(data, 1, len, fp) != len) { + printf("Could not read %s\n", argv[2]); + free(data); + fclose(fp); + return 1; + } + fclose(fp); + + uint8_t *plaintext = (uint8_t *)malloc(len); + if (!plaintext) { + printf("Could not allocate memory\n"); + free(data); + return 1; + } + Tox_Err_Decryption error; + if (!tox_pass_decrypt(data, len, (uint8_t *)argv[1], strlen(argv[1]), plaintext, &error)) { + printf("Could not decrypt: %s\n", tox_err_decryption_to_string(error)); + free(data); + free(plaintext); + return 1; + } + fp = fopen(argv[3], "wb"); + if (!fp) { + printf("Could not open %s\n", argv[3]); + free(data); + free(plaintext); + return 1; + } + if (fwrite(plaintext, 1, len - TOX_PASS_ENCRYPTION_EXTRA_LENGTH, fp) != len - TOX_PASS_ENCRYPTION_EXTRA_LENGTH) { + printf("Could not write %s\n", argv[3]); + free(data); + free(plaintext); + fclose(fp); + return 1; + } + free(data); + free(plaintext); + fclose(fp); + + return 0; +} diff --git a/external/toxcore/c-toxcore/testing/fuzzing/func_conversion.hh b/external/toxcore/c-toxcore/testing/fuzzing/func_conversion.hh index 8ab9103..bfc08be 100644 --- a/external/toxcore/c-toxcore/testing/fuzzing/func_conversion.hh +++ b/external/toxcore/c-toxcore/testing/fuzzing/func_conversion.hh @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2022-2025 The TokTok team. */ #ifndef C_TOXCORE_TESTING_FUZZING_FUNC_CONVERSION_H diff --git a/external/toxcore/c-toxcore/testing/fuzzing/fuzz_support.cc b/external/toxcore/c-toxcore/testing/fuzzing/fuzz_support.cc index a98967a..7f94970 100644 --- a/external/toxcore/c-toxcore/testing/fuzzing/fuzz_support.cc +++ b/external/toxcore/c-toxcore/testing/fuzzing/fuzz_support.cc @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2021-2022 The TokTok team. + * Copyright © 2021-2025 The TokTok team. */ #include "fuzz_support.hh" diff --git a/external/toxcore/c-toxcore/testing/fuzzing/fuzz_support.hh b/external/toxcore/c-toxcore/testing/fuzzing/fuzz_support.hh index 6a370f3..c9eca0a 100644 --- a/external/toxcore/c-toxcore/testing/fuzzing/fuzz_support.hh +++ b/external/toxcore/c-toxcore/testing/fuzzing/fuzz_support.hh @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2021-2022 The TokTok team. + * Copyright © 2021-2025 The TokTok team. */ #ifndef C_TOXCORE_TESTING_FUZZING_FUZZ_SUPPORT_H diff --git a/external/toxcore/c-toxcore/testing/fuzzing/fuzz_tox.hh b/external/toxcore/c-toxcore/testing/fuzzing/fuzz_tox.hh index c19bb1b..2c23edb 100644 --- a/external/toxcore/c-toxcore/testing/fuzzing/fuzz_tox.hh +++ b/external/toxcore/c-toxcore/testing/fuzzing/fuzz_tox.hh @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022-2024 The TokTok team. + * Copyright © 2022-2025 The TokTok team. */ #ifndef C_TOXCORE_TESTING_FUZZING_FUZZ_TOX_H diff --git a/external/toxcore/c-toxcore/testing/misc_tools.c b/external/toxcore/c-toxcore/testing/misc_tools.c index 6352f47..7b0d5f8 100644 --- a/external/toxcore/c-toxcore/testing/misc_tools.c +++ b/external/toxcore/c-toxcore/testing/misc_tools.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ diff --git a/external/toxcore/c-toxcore/toxav/audio.c b/external/toxcore/c-toxcore/toxav/audio.c index ab11cc0..59c8a71 100644 --- a/external/toxcore/c-toxcore/toxav/audio.c +++ b/external/toxcore/c-toxcore/toxav/audio.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013-2015 Tox project. */ #include "audio.h" @@ -195,7 +195,7 @@ void ac_iterate(ACSession *ac) free(temp_audio_buffer); } -int ac_queue_message(Mono_Time *mono_time, void *cs, struct RTPMessage *msg) +int ac_queue_message(const Mono_Time *mono_time, void *cs, struct RTPMessage *msg) { ACSession *ac = (ACSession *)cs; diff --git a/external/toxcore/c-toxcore/toxav/audio.h b/external/toxcore/c-toxcore/toxav/audio.h index 3ae4251..ce8b3af 100644 --- a/external/toxcore/c-toxcore/toxav/audio.h +++ b/external/toxcore/c-toxcore/toxav/audio.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013-2015 Tox project. */ #ifndef C_TOXCORE_TOXAV_AUDIO_H @@ -67,7 +67,7 @@ ACSession *ac_new(Mono_Time *mono_time, const Logger *log, ToxAV *av, uint32_t f toxav_audio_receive_frame_cb *cb, void *cb_data); void ac_kill(ACSession *ac); void ac_iterate(ACSession *ac); -int ac_queue_message(Mono_Time *mono_time, void *cs, struct RTPMessage *msg); +int ac_queue_message(const Mono_Time *mono_time, void *cs, struct RTPMessage *msg); int ac_reconfigure_encoder(ACSession *ac, uint32_t bit_rate, uint32_t sampling_rate, uint8_t channels); #endif /* C_TOXCORE_TOXAV_AUDIO_H */ diff --git a/external/toxcore/c-toxcore/toxav/bwcontroller.c b/external/toxcore/c-toxcore/toxav/bwcontroller.c index 8dacc92..bb6b614 100644 --- a/external/toxcore/c-toxcore/toxav/bwcontroller.c +++ b/external/toxcore/c-toxcore/toxav/bwcontroller.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013-2015 Tox project. */ #include "bwcontroller.h" diff --git a/external/toxcore/c-toxcore/toxav/bwcontroller.h b/external/toxcore/c-toxcore/toxav/bwcontroller.h index 0a8f618..7e3ddd3 100644 --- a/external/toxcore/c-toxcore/toxav/bwcontroller.h +++ b/external/toxcore/c-toxcore/toxav/bwcontroller.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013-2015 Tox project. */ #ifndef C_TOXCORE_TOXAV_BWCONTROLLER_H diff --git a/external/toxcore/c-toxcore/toxav/groupav.c b/external/toxcore/c-toxcore/toxav/groupav.c index 14ca9bb..b937d02 100644 --- a/external/toxcore/c-toxcore/toxav/groupav.c +++ b/external/toxcore/c-toxcore/toxav/groupav.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2014 Tox project. */ #include "groupav.h" diff --git a/external/toxcore/c-toxcore/toxav/groupav.h b/external/toxcore/c-toxcore/toxav/groupav.h index 6777803..c60bdba 100644 --- a/external/toxcore/c-toxcore/toxav/groupav.h +++ b/external/toxcore/c-toxcore/toxav/groupav.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2014 Tox project. */ #ifndef C_TOXCORE_TOXAV_GROUPAV_H diff --git a/external/toxcore/c-toxcore/toxav/msi.c b/external/toxcore/c-toxcore/toxav/msi.c index 5173621..a0bbe8d 100644 --- a/external/toxcore/c-toxcore/toxav/msi.c +++ b/external/toxcore/c-toxcore/toxav/msi.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013-2015 Tox project. */ #include "msi.h" diff --git a/external/toxcore/c-toxcore/toxav/msi.h b/external/toxcore/c-toxcore/toxav/msi.h index 88d0157..72a0a1a 100644 --- a/external/toxcore/c-toxcore/toxav/msi.h +++ b/external/toxcore/c-toxcore/toxav/msi.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013-2015 Tox project. */ #ifndef C_TOXCORE_TOXAV_MSI_H diff --git a/external/toxcore/c-toxcore/toxav/ring_buffer.c b/external/toxcore/c-toxcore/toxav/ring_buffer.c index 9fd6b4e..186fa53 100644 --- a/external/toxcore/c-toxcore/toxav/ring_buffer.c +++ b/external/toxcore/c-toxcore/toxav/ring_buffer.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. * Copyright © 2013 plutooo */ diff --git a/external/toxcore/c-toxcore/toxav/ring_buffer.h b/external/toxcore/c-toxcore/toxav/ring_buffer.h index d89e25e..d4208be 100644 --- a/external/toxcore/c-toxcore/toxav/ring_buffer.h +++ b/external/toxcore/c-toxcore/toxav/ring_buffer.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. * Copyright © 2013 plutooo */ diff --git a/external/toxcore/c-toxcore/toxav/rtp.c b/external/toxcore/c-toxcore/toxav/rtp.c index 32b2545..dd955d0 100644 --- a/external/toxcore/c-toxcore/toxav/rtp.c +++ b/external/toxcore/c-toxcore/toxav/rtp.c @@ -1,11 +1,10 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013-2015 Tox project. */ #include "rtp.h" #include -#include #include #include @@ -768,22 +767,35 @@ void rtp_stop_receiving(Tox *tox) tox_callback_friend_lossy_packet_per_pktid(tox, nullptr, RTP_TYPE_VIDEO); } +/** + * Log the neterror error if any. + * + * @param error the error from rtp_send_custom_lossy_packet. + * @param rdata_size The package length to be shown in the log. + */ +static void rtp_report_error_maybe(const Logger *log, Tox_Err_Friend_Custom_Packet error, uint16_t rdata_size) +{ + if (error != TOX_ERR_FRIEND_CUSTOM_PACKET_OK) { + char *netstrerror = net_new_strerror(net_error()); + const char *toxerror = tox_err_friend_custom_packet_to_string(error); + LOGGER_WARNING(log, "RTP send failed (len: %u)! tox error: %s net error: %s", + rdata_size, toxerror, netstrerror); + net_kill_strerror(netstrerror); + } +} + static void rtp_send_piece(const Logger *log, Tox *tox, uint32_t friend_number, const struct RTPHeader *header, const uint8_t *data, uint8_t *rdata, uint16_t length) { rtp_header_pack(rdata + 1, header); memcpy(rdata + 1 + RTP_HEADER_SIZE, data, length); - Tox_Err_Friend_Custom_Packet error; - tox_friend_send_lossy_packet(tox, friend_number, - rdata, length + RTP_HEADER_SIZE + 1, &error); + const uint16_t rdata_size = length + RTP_HEADER_SIZE + 1; - if (error != TOX_ERR_FRIEND_CUSTOM_PACKET_OK) { - char *netstrerror = net_new_strerror(net_error()); - LOGGER_WARNING(log, "RTP send failed (len: %d)! tox error: %d, net error: %s", - length + RTP_HEADER_SIZE + 1, error, netstrerror); - net_kill_strerror(netstrerror); - } + Tox_Err_Friend_Custom_Packet error; + tox_friend_send_lossy_packet(tox, friend_number, rdata, rdata_size, &error); + + rtp_report_error_maybe(log, error, rdata_size); } static struct RTPHeader rtp_default_header(const RTPSession *session, uint32_t length, bool is_keyframe) @@ -811,7 +823,7 @@ static struct RTPHeader rtp_default_header(const RTPSession *session, uint32_t l header.ma = 0; header.pt = session->payload_type % 128; header.sequnum = session->sequnum; - Mono_Time *mt = toxav_get_av_mono_time(session->toxav); + const Mono_Time *mt = toxav_get_av_mono_time(session->toxav); if (mt != nullptr) { header.timestamp = current_time_monotonic(mt); } else { diff --git a/external/toxcore/c-toxcore/toxav/rtp.h b/external/toxcore/c-toxcore/toxav/rtp.h index 1e2e2e8..1668a54 100644 --- a/external/toxcore/c-toxcore/toxav/rtp.h +++ b/external/toxcore/c-toxcore/toxav/rtp.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013-2015 Tox project. */ #ifndef C_TOXCORE_TOXAV_RTP_H @@ -147,7 +147,7 @@ struct RTPWorkBufferList { #define DISMISS_FIRST_LOST_VIDEO_PACKET_COUNT 10 -typedef int rtp_m_cb(Mono_Time *mono_time, void *cs, struct RTPMessage *msg); +typedef int rtp_m_cb(const Mono_Time *mono_time, void *cs, struct RTPMessage *msg); /** * RTP control session. diff --git a/external/toxcore/c-toxcore/toxav/toxav.c b/external/toxcore/c-toxcore/toxav/toxav.c index dcb4597..b69ce15 100644 --- a/external/toxcore/c-toxcore/toxav/toxav.c +++ b/external/toxcore/c-toxcore/toxav/toxav.c @@ -1,11 +1,10 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013-2015 Tox project. */ #include "toxav.h" #include -#include #include #include #include @@ -21,7 +20,7 @@ #include "../toxcore/network.h" #include "../toxcore/tox.h" #include "../toxcore/tox_private.h" -#include "../toxcore/tox_struct.h" +#include "../toxcore/tox_struct.h" // IWYU pragma: keep #include "../toxcore/util.h" // TODO(zoff99): don't hardcode this, let the application choose it @@ -342,7 +341,7 @@ uint32_t toxav_iteration_interval(const ToxAV *av) * @param frame_time the duration of the current frame in ms * @param start_time the timestamp when decoding of this frame started */ -static void calc_interval(ToxAV *av, DecodeTimeStats *stats, int32_t frame_time, uint64_t start_time) +static void calc_interval(const ToxAV *av, DecodeTimeStats *stats, int32_t frame_time, uint64_t start_time) { stats->interval = frame_time < stats->average ? 0 : (frame_time - stats->average); stats->total += current_time_monotonic(av->toxav_mono_time) - start_time; diff --git a/external/toxcore/c-toxcore/toxav/toxav.h b/external/toxcore/c-toxcore/toxav/toxav.h index a9e3485..b77bf06 100644 --- a/external/toxcore/c-toxcore/toxav/toxav.h +++ b/external/toxcore/c-toxcore/toxav/toxav.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2024 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013-2015 Tox project. */ diff --git a/external/toxcore/c-toxcore/toxav/toxav_hacks.h b/external/toxcore/c-toxcore/toxav/toxav_hacks.h index 2b0382d..7b38143 100644 --- a/external/toxcore/c-toxcore/toxav/toxav_hacks.h +++ b/external/toxcore/c-toxcore/toxav/toxav_hacks.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013-2015 Tox project. */ #ifndef C_TOXCORE_TOXAV_HACKS_H diff --git a/external/toxcore/c-toxcore/toxav/toxav_old.c b/external/toxcore/c-toxcore/toxav/toxav_old.c index 364cb3c..223f5c4 100644 --- a/external/toxcore/c-toxcore/toxav/toxav_old.c +++ b/external/toxcore/c-toxcore/toxav/toxav_old.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013-2015 Tox project. */ diff --git a/external/toxcore/c-toxcore/toxav/video.c b/external/toxcore/c-toxcore/toxav/video.c index 4d1d5a9..52d9829 100644 --- a/external/toxcore/c-toxcore/toxav/video.c +++ b/external/toxcore/c-toxcore/toxav/video.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013-2015 Tox project. */ #include "video.h" @@ -8,14 +8,12 @@ #include #include -#include "msi.h" #include "ring_buffer.h" #include "rtp.h" #include "../toxcore/ccompat.h" #include "../toxcore/logger.h" #include "../toxcore/mono_time.h" -#include "../toxcore/network.h" /** * Soft deadline the decoder should attempt to meet, in "us" (microseconds). @@ -143,7 +141,7 @@ static void vc_init_encoder_cfg(const Logger *log, vpx_codec_enc_cfg_t *cfg, int #endif /* 0 */ } -VCSession *vc_new(const Logger *log, Mono_Time *mono_time, ToxAV *av, uint32_t friend_number, +VCSession *vc_new(const Logger *log, const Mono_Time *mono_time, ToxAV *av, uint32_t friend_number, toxav_video_receive_frame_cb *cb, void *cb_data) { VCSession *vc = (VCSession *)calloc(1, sizeof(VCSession)); @@ -346,7 +344,7 @@ void vc_iterate(VCSession *vc) } } -int vc_queue_message(Mono_Time *mono_time, void *cs, struct RTPMessage *msg) +int vc_queue_message(const Mono_Time *mono_time, void *cs, struct RTPMessage *msg) { VCSession *vc = (VCSession *)cs; diff --git a/external/toxcore/c-toxcore/toxav/video.h b/external/toxcore/c-toxcore/toxav/video.h index 7bd54fa..c8ecf17 100644 --- a/external/toxcore/c-toxcore/toxav/video.h +++ b/external/toxcore/c-toxcore/toxav/video.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013-2015 Tox project. */ #ifndef C_TOXCORE_TOXAV_VIDEO_H @@ -44,11 +44,11 @@ typedef struct VCSession { const Logger *log; } VCSession; -VCSession *vc_new(const Logger *log, Mono_Time *mono_time, ToxAV *av, uint32_t friend_number, +VCSession *vc_new(const Logger *log, const Mono_Time *mono_time, ToxAV *av, uint32_t friend_number, toxav_video_receive_frame_cb *cb, void *cb_data); void vc_kill(VCSession *vc); void vc_iterate(VCSession *vc); -int vc_queue_message(Mono_Time *mono_time, void *cs, struct RTPMessage *msg); +int vc_queue_message(const Mono_Time *mono_time, void *cs, struct RTPMessage *msg); int vc_reconfigure_encoder(VCSession *vc, uint32_t bit_rate, uint16_t width, uint16_t height, int16_t kf_max_dist); #endif /* C_TOXCORE_TOXAV_VIDEO_H */ diff --git a/external/toxcore/c-toxcore/toxcore/BUILD.bazel b/external/toxcore/c-toxcore/toxcore/BUILD.bazel index 63cc08a..8bbd870 100644 --- a/external/toxcore/c-toxcore/toxcore/BUILD.bazel +++ b/external/toxcore/c-toxcore/toxcore/BUILD.bazel @@ -4,6 +4,8 @@ load("@rules_fuzzing//fuzzing:cc_defs.bzl", "cc_fuzz_test") exports_files( srcs = [ "tox.h", + "tox_dispatch.h", + "tox_events.h", "tox_private.h", ], visibility = ["//c-toxcore:__subpackages__"], @@ -268,6 +270,7 @@ cc_library( deps = [ ":attributes", ":ccompat", + ":mem", ], ) @@ -277,6 +280,7 @@ cc_test( srcs = ["list_test.cc"], deps = [ ":list", + ":mem", "@com_google_googletest//:gtest", "@com_google_googletest//:gtest_main", ], @@ -346,6 +350,18 @@ cc_library( ], ) +cc_library( + name = "net_profile", + srcs = ["net_profile.c"], + hdrs = ["net_profile.h"], + deps = [ + ":attributes", + ":ccompat", + ":logger", + ":mem", + ], +) + cc_library( name = "network", srcs = ["network.c"], @@ -365,6 +381,7 @@ cc_library( ":logger", ":mem", ":mono_time", + ":net_profile", ":util", "@libsodium", "@psocket", @@ -450,6 +467,7 @@ cc_library( ":attributes", ":ccompat", ":crypto_core", + ":mem", ":network", ":util", "@psocket", @@ -575,6 +593,7 @@ cc_library( ":ccompat", ":crypto_core", ":logger", + ":mem", ":mono_time", ":network", ":timed_auth", @@ -629,6 +648,7 @@ cc_library( ":crypto_core", ":logger", ":mem", + ":net_profile", ":network", ], ) @@ -656,6 +676,7 @@ cc_library( ":logger", ":mem", ":mono_time", + ":net_profile", ":network", ":onion", ":util", @@ -677,6 +698,7 @@ cc_library( ":logger", ":mem", ":mono_time", + ":net_profile", ":network", ":util", ], @@ -699,6 +721,7 @@ cc_library( ":logger", ":mem", ":mono_time", + ":net_profile", ":network", ":onion", ":util", @@ -736,6 +759,7 @@ cc_library( ":logger", ":mem", ":mono_time", + ":net_profile", ":network", ":util", "@pthread", @@ -785,6 +809,7 @@ cc_library( ":ccompat", ":crypto_core", ":logger", + ":mem", ":mono_time", ":network", ":util", @@ -886,6 +911,7 @@ cc_library( ":ccompat", ":crypto_core", ":logger", + ":mem", ":mono_time", ":net_crypto", ":network", @@ -910,6 +936,7 @@ cc_library( ":ccompat", ":crypto_core", ":friend_connection", + ":mem", ":network", ":onion", ":onion_announce", @@ -1060,6 +1087,7 @@ cc_library( ":DHT", ":Messenger", ":TCP_client", + ":TCP_server", ":attributes", ":ccompat", ":crypto_core", @@ -1070,6 +1098,7 @@ cc_library( ":mem", ":mono_time", ":net_crypto", + ":net_profile", ":network", ":onion_client", ":state", diff --git a/external/toxcore/c-toxcore/toxcore/DHT.c b/external/toxcore/c-toxcore/toxcore/DHT.c index da4ab11..630b6c4 100644 --- a/external/toxcore/c-toxcore/toxcore/DHT.c +++ b/external/toxcore/c-toxcore/toxcore/DHT.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ diff --git a/external/toxcore/c-toxcore/toxcore/DHT.h b/external/toxcore/c-toxcore/toxcore/DHT.h index f42670b..8e0e5e7 100644 --- a/external/toxcore/c-toxcore/toxcore/DHT.h +++ b/external/toxcore/c-toxcore/toxcore/DHT.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ diff --git a/external/toxcore/c-toxcore/toxcore/LAN_discovery.c b/external/toxcore/c-toxcore/toxcore/LAN_discovery.c index aead975..fcc3d47 100644 --- a/external/toxcore/c-toxcore/toxcore/LAN_discovery.c +++ b/external/toxcore/c-toxcore/toxcore/LAN_discovery.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ @@ -8,8 +8,6 @@ */ #include "LAN_discovery.h" -#include - #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) // The mingw32/64 Windows library warns about including winsock2.h after // windows.h even though with the above it's a valid thing to do. So, to make @@ -41,11 +39,14 @@ #include "attributes.h" #include "ccompat.h" #include "crypto_core.h" +#include "mem.h" #include "network.h" #define MAX_INTERFACES 16 struct Broadcast_Info { + const Memory *mem; + uint32_t count; IP ips[MAX_INTERFACES]; }; @@ -53,29 +54,31 @@ struct Broadcast_Info { #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) non_null() -static Broadcast_Info *fetch_broadcast_info(const Network *ns) +static Broadcast_Info *fetch_broadcast_info(const Memory *mem, const Network *ns) { - Broadcast_Info *broadcast = (Broadcast_Info *)calloc(1, sizeof(Broadcast_Info)); + Broadcast_Info *broadcast = (Broadcast_Info *)mem_alloc(mem, sizeof(Broadcast_Info)); if (broadcast == nullptr) { return nullptr; } - IP_ADAPTER_INFO *adapter_info = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO)); + broadcast->mem = mem; + + IP_ADAPTER_INFO *adapter_info = (IP_ADAPTER_INFO *)mem_balloc(mem, sizeof(IP_ADAPTER_INFO)); if (adapter_info == nullptr) { - free(broadcast); + mem_delete(mem, broadcast); return nullptr; } unsigned long out_buf_len = sizeof(IP_ADAPTER_INFO); if (GetAdaptersInfo(adapter_info, &out_buf_len) == ERROR_BUFFER_OVERFLOW) { - free(adapter_info); - IP_ADAPTER_INFO *new_adapter_info = (IP_ADAPTER_INFO *)malloc(out_buf_len); + mem_delete(mem, adapter_info); + IP_ADAPTER_INFO *new_adapter_info = (IP_ADAPTER_INFO *)mem_balloc(mem, out_buf_len); if (new_adapter_info == nullptr) { - free(broadcast); + mem_delete(mem, broadcast); return nullptr; } @@ -113,7 +116,7 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns) } if (adapter_info != nullptr) { - free(adapter_info); + mem_delete(mem, adapter_info); } return broadcast; @@ -122,14 +125,19 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns) #elif !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && (defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__)) non_null() -static Broadcast_Info *fetch_broadcast_info(const Network *ns) +static bool ip4_is_local(const IP4 *ip4); + +non_null() +static Broadcast_Info *fetch_broadcast_info(const Memory *mem, const Network *ns) { - Broadcast_Info *broadcast = (Broadcast_Info *)calloc(1, sizeof(Broadcast_Info)); + Broadcast_Info *broadcast = (Broadcast_Info *)mem_alloc(mem, sizeof(Broadcast_Info)); if (broadcast == nullptr) { return nullptr; } + broadcast->mem = mem; + /* Not sure how many platforms this will run on, * so it's wrapped in `__linux__` for now. * Definitely won't work like this on Windows... @@ -137,7 +145,7 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns) const Socket sock = net_socket(ns, net_family_ipv4(), TOX_SOCK_STREAM, 0); if (!sock_valid(sock)) { - free(broadcast); + mem_delete(mem, broadcast); return nullptr; } @@ -150,7 +158,7 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns) if (ioctl(net_socket_to_native(sock), SIOCGIFCONF, &ifc) < 0) { kill_sock(ns, sock); - free(broadcast); + mem_delete(mem, broadcast); return nullptr; } @@ -162,7 +170,8 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns) const int n = ifc.ifc_len / sizeof(struct ifreq); for (int i = 0; i < n; ++i) { - /* there are interfaces with are incapable of broadcast */ + /* there are interfaces with are incapable of broadcast + * on Linux, `lo` has no broadcast address, but this function returns `>=0` */ if (ioctl(net_socket_to_native(sock), SIOCGIFBRDADDR, &i_faces[i]) < 0) { continue; } @@ -172,7 +181,7 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns) continue; } - const struct sockaddr_in *sock4 = (const struct sockaddr_in *)(void *)&i_faces[i].ifr_broadaddr; + const struct sockaddr_in *broadaddr4 = (const struct sockaddr_in *)(void *)&i_faces[i].ifr_broadaddr; if (broadcast->count >= MAX_INTERFACES) { break; @@ -180,10 +189,27 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns) IP *ip = &broadcast->ips[broadcast->count]; ip->family = net_family_ipv4(); - ip->ip.v4.uint32 = sock4->sin_addr.s_addr; + ip->ip.v4.uint32 = broadaddr4->sin_addr.s_addr; + // if no broadcast address if (ip->ip.v4.uint32 == 0) { - continue; + if (ioctl(net_socket_to_native(sock), SIOCGIFADDR, &i_faces[i]) < 0) { + continue; + } + + const struct sockaddr_in *addr4 = (const struct sockaddr_in *)(void *)&i_faces[i].ifr_addr; + + + IP4 ip4_staging; + ip4_staging.uint32 = addr4->sin_addr.s_addr; + + if (ip4_is_local(&ip4_staging)) { + // this is 127.x.x.x + ip->ip.v4.uint32 = ip4_staging.uint32; + } else { + // give up. + continue; + } } ++broadcast->count; @@ -197,9 +223,17 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns) #else // TODO(irungentoo): Other platforms? non_null() -static Broadcast_Info *fetch_broadcast_info(const Network *ns) +static Broadcast_Info *fetch_broadcast_info(const Memory *mem, const Network *ns) { - return (Broadcast_Info *)calloc(1, sizeof(Broadcast_Info)); + Broadcast_Info *broadcast = (Broadcast_Info *)mem_alloc(mem, sizeof(Broadcast_Info)); + + if (broadcast == nullptr) { + return nullptr; + } + + broadcast->mem = mem; + + return broadcast; } #endif /* platforms */ @@ -375,12 +409,16 @@ bool lan_discovery_send(const Networking_Core *net, const Broadcast_Info *broadc return res; } -Broadcast_Info *lan_discovery_init(const Network *ns) +Broadcast_Info *lan_discovery_init(const Memory *mem, const Network *ns) { - return fetch_broadcast_info(ns); + return fetch_broadcast_info(mem, ns); } void lan_discovery_kill(Broadcast_Info *broadcast) { - free(broadcast); + if (broadcast == nullptr) { + return; + } + + mem_delete(broadcast->mem, broadcast); } diff --git a/external/toxcore/c-toxcore/toxcore/LAN_discovery.h b/external/toxcore/c-toxcore/toxcore/LAN_discovery.h index 94e5d35..c18ce9e 100644 --- a/external/toxcore/c-toxcore/toxcore/LAN_discovery.h +++ b/external/toxcore/c-toxcore/toxcore/LAN_discovery.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ @@ -10,6 +10,7 @@ #define C_TOXCORE_TOXCORE_LAN_DISCOVERY_H #include "attributes.h" +#include "mem.h" #include "network.h" /** @@ -32,7 +33,7 @@ bool lan_discovery_send(const Networking_Core *net, const Broadcast_Info *broadc * Discovers broadcast devices and IP addresses. */ non_null() -Broadcast_Info *lan_discovery_init(const Network *ns); +Broadcast_Info *lan_discovery_init(const Memory *mem, const Network *ns); /** * Free all resources associated with the broadcast info. diff --git a/external/toxcore/c-toxcore/toxcore/Makefile.inc b/external/toxcore/c-toxcore/toxcore/Makefile.inc index d4d64bc..2ee4a72 100644 --- a/external/toxcore/c-toxcore/toxcore/Makefile.inc +++ b/external/toxcore/c-toxcore/toxcore/Makefile.inc @@ -74,6 +74,8 @@ libtoxcore_la_SOURCES = ../third_party/cmp/cmp.c \ ../toxcore/ping_array.c \ ../toxcore/net_crypto.h \ ../toxcore/net_crypto.c \ + ../toxcore/net_profile.c \ + ../toxcore/net_profile.h \ ../toxcore/friend_requests.h \ ../toxcore/friend_requests.c \ ../toxcore/LAN_discovery.h \ diff --git a/external/toxcore/c-toxcore/toxcore/Messenger.c b/external/toxcore/c-toxcore/toxcore/Messenger.c index d2a8449..e7f8ee9 100644 --- a/external/toxcore/c-toxcore/toxcore/Messenger.c +++ b/external/toxcore/c-toxcore/toxcore/Messenger.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ @@ -137,7 +137,7 @@ void getaddress(const Messenger *m, uint8_t *address) } non_null() -static bool send_online_packet(Messenger *m, int friendcon_id) +static bool send_online_packet(const Messenger *m, int friendcon_id) { 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, @@ -145,7 +145,7 @@ static bool send_online_packet(Messenger *m, int friendcon_id) } non_null() -static bool send_offline_packet(Messenger *m, int friendcon_id) +static bool send_offline_packet(const Messenger *m, int friendcon_id) { 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, @@ -2515,7 +2515,7 @@ static bool self_announce_group(const Messenger *m, GC_Chat *chat, Onion_Friend return false; } - if (gca_add_announce(m->mono_time, m->group_announce, &announce) == nullptr) { + if (gca_add_announce(m->mem, m->mono_time, m->group_announce, &announce) == nullptr) { onion_friend_set_gc_data(onion_friend, nullptr, 0); return false; } @@ -3475,7 +3475,7 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random * m->rng = rng; m->ns = ns; - m->fr = friendreq_new(); + m->fr = friendreq_new(mem); if (m->fr == nullptr) { mem_delete(mem, m); @@ -3544,7 +3544,7 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random * return nullptr; } - m->group_announce = new_gca_list(); + m->group_announce = new_gca_list(m->mem); if (m->group_announce == nullptr) { LOGGER_WARNING(m->log, "DHT group chats initialisation failed"); @@ -3559,7 +3559,7 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random * } if (options->dht_announcements_enabled) { - m->forwarding = new_forwarding(m->log, m->rng, m->mono_time, m->dht); + m->forwarding = new_forwarding(m->log, m->mem, m->rng, m->mono_time, m->dht); if (m->forwarding != nullptr) { m->announce = new_announcements(m->log, m->mem, m->rng, m->mono_time, m->forwarding); } else { @@ -3574,7 +3574,7 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random * m->onion_a = new_onion_announce(m->log, m->mem, m->rng, m->mono_time, m->dht); m->onion_c = new_onion_client(m->log, m->mem, m->rng, m->mono_time, m->net_crypto); if (m->onion_c != nullptr) { - m->fr_c = new_friend_connections(m->log, m->mono_time, m->ns, m->onion_c, options->local_discovery_enabled); + m->fr_c = new_friend_connections(m->log, m->mem, m->mono_time, m->ns, m->onion_c, options->local_discovery_enabled); } if ((options->dht_announcements_enabled && (m->forwarding == nullptr || m->announce == nullptr)) || diff --git a/external/toxcore/c-toxcore/toxcore/Messenger.h b/external/toxcore/c-toxcore/toxcore/Messenger.h index be6fb5f..d9bdaf3 100644 --- a/external/toxcore/c-toxcore/toxcore/Messenger.h +++ b/external/toxcore/c-toxcore/toxcore/Messenger.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ diff --git a/external/toxcore/c-toxcore/toxcore/TCP_client.c b/external/toxcore/c-toxcore/toxcore/TCP_client.c index de857bf..1982412 100644 --- a/external/toxcore/c-toxcore/toxcore/TCP_client.c +++ b/external/toxcore/c-toxcore/toxcore/TCP_client.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2014 Tox project. */ @@ -20,6 +20,7 @@ #include "logger.h" #include "mem.h" #include "mono_time.h" +#include "net_profile.h" #include "network.h" #include "util.h" @@ -109,11 +110,23 @@ void tcp_con_set_custom_uint(TCP_Client_Connection *con, uint32_t value) non_null() static bool connect_sock_to(const Network *ns, const Logger *logger, const Memory *mem, Socket sock, const IP_Port *ip_port, const TCP_Proxy_Info *proxy_info) { + Net_Err_Connect err; if (proxy_info->proxy_type != TCP_PROXY_NONE) { - return net_connect(ns, mem, logger, sock, &proxy_info->ip_port); + net_connect(ns, mem, logger, sock, &proxy_info->ip_port, &err); } else { - return net_connect(ns, mem, logger, sock, ip_port); + net_connect(ns, mem, logger, sock, ip_port, &err); } + switch (err) { + case NET_ERR_CONNECT_OK: + case NET_ERR_CONNECT_FAILED: { + /* nonblocking socket, connect will never return success */ + return true; + } + case NET_ERR_CONNECT_INVALID_FAMILY: + return false; + } + LOGGER_ERROR(logger, "unexpected error code %s from net_connect", net_err_connect_to_string(err)); + return false; } /** @@ -582,7 +595,7 @@ void forwarding_handler(TCP_Client_Connection *con, forwarded_response_cb *forwa TCP_Client_Connection *new_tcp_connection( const Logger *logger, const Memory *mem, const Mono_Time *mono_time, const Random *rng, const Network *ns, const IP_Port *ip_port, const uint8_t *public_key, const uint8_t *self_public_key, const uint8_t *self_secret_key, - const TCP_Proxy_Info *proxy_info) + const TCP_Proxy_Info *proxy_info, Net_Profile *net_profile) { assert(logger != nullptr); assert(mem != nullptr); @@ -591,6 +604,7 @@ TCP_Client_Connection *new_tcp_connection( assert(ns != nullptr); if (!net_family_is_ipv4(ip_port->ip.family) && !net_family_is_ipv6(ip_port->ip.family)) { + LOGGER_ERROR(logger, "Invalid IP family: %d", ip_port->ip.family.value); return nullptr; } @@ -609,15 +623,26 @@ TCP_Client_Connection *new_tcp_connection( const Socket sock = net_socket(ns, family, TOX_SOCK_STREAM, TOX_PROTO_TCP); if (!sock_valid(sock)) { + LOGGER_ERROR(logger, "Failed to create TCP socket with family %d", family.value); return nullptr; } if (!set_socket_nosigpipe(ns, sock)) { + LOGGER_ERROR(logger, "Failed to set TCP socket to ignore SIGPIPE"); kill_sock(ns, sock); return nullptr; } - if (!(set_socket_nonblock(ns, sock) && connect_sock_to(ns, logger, mem, sock, ip_port, proxy_info))) { + if (!set_socket_nonblock(ns, sock)) { + LOGGER_ERROR(logger, "Failed to set TCP socket to non-blocking"); + kill_sock(ns, sock); + return nullptr; + } + + if (!connect_sock_to(ns, logger, mem, sock, ip_port, proxy_info)) { + Ip_Ntoa ip_ntoa; + LOGGER_WARNING(logger, "Failed to connect TCP socket to %s:%u", + net_ip_ntoa(&ip_port->ip, &ip_ntoa), net_ntohs(ip_port->port)); kill_sock(ns, sock); return nullptr; } @@ -625,6 +650,7 @@ TCP_Client_Connection *new_tcp_connection( TCP_Client_Connection *temp = (TCP_Client_Connection *)mem_alloc(mem, sizeof(TCP_Client_Connection)); if (temp == nullptr) { + LOGGER_ERROR(logger, "Failed to allocate memory for TCP_Client_Connection"); kill_sock(ns, sock); return nullptr; } @@ -634,6 +660,7 @@ TCP_Client_Connection *new_tcp_connection( temp->con.rng = rng; temp->con.sock = sock; temp->con.ip_port = *ip_port; + temp->con.net_profile = net_profile; memcpy(temp->public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); memcpy(temp->self_public_key, self_public_key, CRYPTO_PUBLIC_KEY_SIZE); encrypt_precompute(temp->public_key, self_secret_key, temp->con.shared_key); @@ -657,6 +684,7 @@ TCP_Client_Connection *new_tcp_connection( temp->status = TCP_CLIENT_CONNECTING; if (generate_handshake(temp) == -1) { + LOGGER_ERROR(logger, "Failed to generate handshake"); kill_sock(ns, sock); mem_delete(mem, temp); return nullptr; @@ -819,6 +847,8 @@ static int handle_tcp_client_packet(const Logger *logger, TCP_Client_Connection return -1; } + netprof_record_packet(conn->con.net_profile, data[0], length, PACKET_DIRECTION_RECV); + switch (data[0]) { case TCP_PACKET_ROUTING_RESPONSE: return handle_tcp_client_routing_response(conn, data, length); diff --git a/external/toxcore/c-toxcore/toxcore/TCP_client.h b/external/toxcore/c-toxcore/toxcore/TCP_client.h index ea2654b..3ed6e8c 100644 --- a/external/toxcore/c-toxcore/toxcore/TCP_client.h +++ b/external/toxcore/c-toxcore/toxcore/TCP_client.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2014 Tox project. */ @@ -15,6 +15,7 @@ #include "logger.h" #include "mem.h" #include "mono_time.h" +#include "net_profile.h" #include "network.h" #define TCP_CONNECTION_TIMEOUT 10 @@ -60,11 +61,11 @@ non_null() void tcp_con_set_custom_uint(TCP_Client_Connection *con, uint32_t value); /** Create new TCP connection to ip_port/public_key */ -non_null(1, 2, 3, 4, 5, 6, 7, 8, 9) nullable(10) +non_null(1, 2, 3, 4, 5, 6, 7, 8, 9) nullable(10, 11) TCP_Client_Connection *new_tcp_connection( const Logger *logger, const Memory *mem, const Mono_Time *mono_time, const Random *rng, const Network *ns, const IP_Port *ip_port, const uint8_t *public_key, const uint8_t *self_public_key, const uint8_t *self_secret_key, - const TCP_Proxy_Info *proxy_info); + const TCP_Proxy_Info *proxy_info, Net_Profile *net_profile); /** Run the TCP connection */ non_null(1, 2, 3) nullable(4) diff --git a/external/toxcore/c-toxcore/toxcore/TCP_common.c b/external/toxcore/c-toxcore/toxcore/TCP_common.c index 6e0ac42..e9f67dc 100644 --- a/external/toxcore/c-toxcore/toxcore/TCP_common.c +++ b/external/toxcore/c-toxcore/toxcore/TCP_common.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2014 Tox project. */ @@ -35,7 +35,8 @@ int send_pending_data_nonpriority(const Logger *logger, TCP_Connection *con) } const uint16_t left = con->last_packet_length - con->last_packet_sent; - const int len = net_send(con->ns, logger, con->sock, con->last_packet + con->last_packet_sent, left, &con->ip_port); + const int len = net_send(con->ns, logger, con->sock, con->last_packet + con->last_packet_sent, left, &con->ip_port, + con->net_profile); if (len <= 0) { return -1; @@ -66,7 +67,7 @@ int send_pending_data(const Logger *logger, TCP_Connection *con) while (p != nullptr) { const uint16_t left = p->size - p->sent; - const int len = net_send(con->ns, logger, con->sock, p->data + p->sent, left, &con->ip_port); + const int len = net_send(con->ns, logger, con->sock, p->data + p->sent, left, &con->ip_port, con->net_profile); if (len != left) { if (len > 0) { @@ -164,7 +165,8 @@ int write_packet_tcp_secure_connection(const Logger *logger, TCP_Connection *con } if (priority) { - len = sendpriority ? net_send(con->ns, logger, con->sock, packet, packet_size, &con->ip_port) : 0; + len = sendpriority ? net_send(con->ns, logger, con->sock, packet, packet_size, &con->ip_port, + con->net_profile) : 0; if (len <= 0) { len = 0; @@ -179,7 +181,7 @@ int write_packet_tcp_secure_connection(const Logger *logger, TCP_Connection *con return add_priority(con, packet, packet_size, len) ? 1 : 0; } - len = net_send(con->ns, logger, con->sock, packet, packet_size, &con->ip_port); + len = net_send(con->ns, logger, con->sock, packet, packet_size, &con->ip_port, con->net_profile); if (len <= 0) { return 0; diff --git a/external/toxcore/c-toxcore/toxcore/TCP_common.h b/external/toxcore/c-toxcore/toxcore/TCP_common.h index 9fa1366..dd07fd1 100644 --- a/external/toxcore/c-toxcore/toxcore/TCP_common.h +++ b/external/toxcore/c-toxcore/toxcore/TCP_common.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2014 Tox project. */ @@ -10,6 +10,7 @@ #include "crypto_core.h" #include "logger.h" #include "mem.h" +#include "net_profile.h" #include "network.h" typedef struct TCP_Priority_List TCP_Priority_List; @@ -66,6 +67,10 @@ typedef struct TCP_Connection { TCP_Priority_List *priority_queue_start; TCP_Priority_List *priority_queue_end; + + // This is a shared pointer to the parent's respective Net_Profile object + // (either TCP_Server for TCP server packets or TCP_Connections for TCP client packets). + Net_Profile *net_profile; } TCP_Connection; /** diff --git a/external/toxcore/c-toxcore/toxcore/TCP_connection.c b/external/toxcore/c-toxcore/toxcore/TCP_connection.c index c7161a9..20ac3ce 100644 --- a/external/toxcore/c-toxcore/toxcore/TCP_connection.c +++ b/external/toxcore/c-toxcore/toxcore/TCP_connection.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2015 Tox project. */ @@ -20,6 +20,7 @@ #include "logger.h" #include "mem.h" #include "mono_time.h" +#include "net_profile.h" #include "network.h" #include "util.h" @@ -56,6 +57,9 @@ struct TCP_Connections { bool onion_status; uint16_t onion_num_conns; + + /* Network profile for all TCP client packets. */ + Net_Profile *net_profile; }; static const TCP_Connection_to empty_tcp_connection_to = {0}; @@ -928,7 +932,8 @@ static int reconnect_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connec uint8_t relay_pk[CRYPTO_PUBLIC_KEY_SIZE]; memcpy(relay_pk, tcp_con_public_key(tcp_con->connection), CRYPTO_PUBLIC_KEY_SIZE); kill_tcp_connection(tcp_con->connection); - tcp_con->connection = new_tcp_connection(tcp_c->logger, tcp_c->mem, tcp_c->mono_time, tcp_c->rng, tcp_c->ns, &ip_port, relay_pk, tcp_c->self_public_key, tcp_c->self_secret_key, &tcp_c->proxy_info); + tcp_con->connection = new_tcp_connection(tcp_c->logger, tcp_c->mem, tcp_c->mono_time, tcp_c->rng, tcp_c->ns, &ip_port, relay_pk, tcp_c->self_public_key, tcp_c->self_secret_key, &tcp_c->proxy_info, + tcp_c->net_profile); if (tcp_con->connection == nullptr) { kill_tcp_relay_connection(tcp_c, tcp_connections_number); @@ -1017,7 +1022,7 @@ static int unsleep_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connecti tcp_con->connection = new_tcp_connection( tcp_c->logger, tcp_c->mem, tcp_c->mono_time, tcp_c->rng, tcp_c->ns, &tcp_con->ip_port, - tcp_con->relay_pk, tcp_c->self_public_key, tcp_c->self_secret_key, &tcp_c->proxy_info); + tcp_con->relay_pk, tcp_c->self_public_key, tcp_c->self_secret_key, &tcp_c->proxy_info, tcp_c->net_profile); if (tcp_con->connection == nullptr) { kill_tcp_relay_connection(tcp_c, tcp_connections_number); @@ -1315,7 +1320,7 @@ static int add_tcp_relay_instance(TCP_Connections *tcp_c, const IP_Port *ip_port tcp_con->connection = new_tcp_connection( tcp_c->logger, tcp_c->mem, tcp_c->mono_time, tcp_c->rng, tcp_c->ns, &ipp_copy, - relay_pk, tcp_c->self_public_key, tcp_c->self_secret_key, &tcp_c->proxy_info); + relay_pk, tcp_c->self_public_key, tcp_c->self_secret_key, &tcp_c->proxy_info, tcp_c->net_profile); if (tcp_con->connection == nullptr) { return -1; @@ -1609,6 +1614,14 @@ TCP_Connections *new_tcp_connections(const Logger *logger, const Memory *mem, co return nullptr; } + Net_Profile *np = netprof_new(logger, mem); + + if (np == nullptr) { + mem_delete(mem, temp); + return nullptr; + } + + temp->net_profile = np; temp->logger = logger; temp->mem = mem; temp->rng = rng; @@ -1723,7 +1736,17 @@ void kill_tcp_connections(TCP_Connections *tcp_c) crypto_memzero(tcp_c->self_secret_key, sizeof(tcp_c->self_secret_key)); + netprof_kill(tcp_c->mem, tcp_c->net_profile); mem_delete(tcp_c->mem, tcp_c->tcp_connections); mem_delete(tcp_c->mem, tcp_c->connections); mem_delete(tcp_c->mem, tcp_c); } + +const Net_Profile *tcp_connection_get_client_net_profile(const TCP_Connections *tcp_c) +{ + if (tcp_c == nullptr) { + return nullptr; + } + + return tcp_c->net_profile; +} diff --git a/external/toxcore/c-toxcore/toxcore/TCP_connection.h b/external/toxcore/c-toxcore/toxcore/TCP_connection.h index 2d35919..33388d5 100644 --- a/external/toxcore/c-toxcore/toxcore/TCP_connection.h +++ b/external/toxcore/c-toxcore/toxcore/TCP_connection.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2015 Tox project. */ @@ -21,6 +21,7 @@ #include "logger.h" #include "mem.h" #include "mono_time.h" +#include "net_profile.h" #include "network.h" #define TCP_CONN_NONE 0 @@ -317,4 +318,11 @@ void do_tcp_connections(const Logger *logger, TCP_Connections *tcp_c, void *user nullable(1) void kill_tcp_connections(TCP_Connections *tcp_c); +/** @brief a pointer to the tcp client net profile associated with tcp_c. + * + * @retval null if tcp_c is null. + */ +non_null() +const Net_Profile *tcp_connection_get_client_net_profile(const TCP_Connections *tcp_c); + #endif /* C_TOXCORE_TOXCORE_TCP_CONNECTION_H */ diff --git a/external/toxcore/c-toxcore/toxcore/TCP_server.c b/external/toxcore/c-toxcore/toxcore/TCP_server.c index ec686f8..f5b32d9 100644 --- a/external/toxcore/c-toxcore/toxcore/TCP_server.c +++ b/external/toxcore/c-toxcore/toxcore/TCP_server.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2014 Tox project. */ @@ -27,6 +27,7 @@ #include "logger.h" #include "mem.h" #include "mono_time.h" +#include "net_profile.h" #include "network.h" #include "onion.h" @@ -91,6 +92,9 @@ struct TCP_Server { uint64_t counter; BS_List accepted_key_list; + + /* Network profile for all TCP server packets. */ + Net_Profile *net_profile; }; static_assert(sizeof(TCP_Server) < 7 * 1024 * 1024, @@ -236,6 +240,7 @@ static int add_accepted(TCP_Server *tcp_server, const Mono_Time *mono_time, TCP_ tcp_server->accepted_connection_array[index].identifier = ++tcp_server->counter; tcp_server->accepted_connection_array[index].last_pinged = mono_time_get(mono_time); tcp_server->accepted_connection_array[index].ping_id = 0; + tcp_server->accepted_connection_array[index].con.net_profile = tcp_server->net_profile; return index; } @@ -357,7 +362,7 @@ static int handle_tcp_handshake(const Logger *logger, TCP_Secure_Connection *con const IP_Port ipp = {{{0}}}; - if (TCP_SERVER_HANDSHAKE_SIZE != net_send(con->con.ns, logger, con->con.sock, response, TCP_SERVER_HANDSHAKE_SIZE, &ipp)) { + if (TCP_SERVER_HANDSHAKE_SIZE != net_send(con->con.ns, logger, con->con.sock, response, TCP_SERVER_HANDSHAKE_SIZE, &ipp, con->con.net_profile)) { crypto_memzero(shared_key, sizeof(shared_key)); return -1; } @@ -680,6 +685,7 @@ static int handle_tcp_packet(TCP_Server *tcp_server, uint32_t con_id, const uint } TCP_Secure_Connection *const con = &tcp_server->accepted_connection_array[con_id]; + netprof_record_packet(con->con.net_profile, data[0], length, PACKET_DIRECTION_RECV); switch (data[0]) { case TCP_PACKET_ROUTING_REQUEST: { @@ -969,6 +975,14 @@ TCP_Server *new_tcp_server(const Logger *logger, const Memory *mem, const Random return nullptr; } + Net_Profile *np = netprof_new(logger, mem); + + if (np == nullptr) { + mem_delete(mem, temp); + return nullptr; + } + + temp->net_profile = np; temp->logger = logger; temp->mem = mem; temp->ns = ns; @@ -978,6 +992,7 @@ TCP_Server *new_tcp_server(const Logger *logger, const Memory *mem, const Random if (socks_listening == nullptr) { LOGGER_ERROR(logger, "socket allocation failed"); + netprof_kill(mem, temp->net_profile); mem_delete(mem, temp); return nullptr; } @@ -989,6 +1004,7 @@ TCP_Server *new_tcp_server(const Logger *logger, const Memory *mem, const Random if (temp->efd == -1) { LOGGER_ERROR(logger, "epoll initialisation failed"); + netprof_kill(mem, temp->net_profile); mem_delete(mem, socks_listening); mem_delete(mem, temp); return nullptr; @@ -1022,6 +1038,7 @@ TCP_Server *new_tcp_server(const Logger *logger, const Memory *mem, const Random } if (temp->num_listening_socks == 0) { + netprof_kill(mem, temp->net_profile); mem_delete(mem, temp->socks_listening); mem_delete(mem, temp); return nullptr; @@ -1040,7 +1057,7 @@ TCP_Server *new_tcp_server(const Logger *logger, const Memory *mem, const Random memcpy(temp->secret_key, secret_key, CRYPTO_SECRET_KEY_SIZE); crypto_derive_public_key(temp->public_key, temp->secret_key); - bs_list_init(&temp->accepted_key_list, CRYPTO_PUBLIC_KEY_SIZE, 8, memcmp); + bs_list_init(&temp->accepted_key_list, mem, CRYPTO_PUBLIC_KEY_SIZE, 8, memcmp); return temp; } @@ -1422,6 +1439,16 @@ void kill_tcp_server(TCP_Server *tcp_server) crypto_memzero(tcp_server->secret_key, sizeof(tcp_server->secret_key)); + netprof_kill(tcp_server->mem, tcp_server->net_profile); mem_delete(tcp_server->mem, tcp_server->socks_listening); mem_delete(tcp_server->mem, tcp_server); } + +const Net_Profile *tcp_server_get_net_profile(const TCP_Server *tcp_server) +{ + if (tcp_server == nullptr) { + return nullptr; + } + + return tcp_server->net_profile; +} diff --git a/external/toxcore/c-toxcore/toxcore/TCP_server.h b/external/toxcore/c-toxcore/toxcore/TCP_server.h index 1d3933a..bd4c98b 100644 --- a/external/toxcore/c-toxcore/toxcore/TCP_server.h +++ b/external/toxcore/c-toxcore/toxcore/TCP_server.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2014 Tox project. */ @@ -15,6 +15,7 @@ #include "logger.h" #include "mem.h" #include "mono_time.h" +#include "net_profile.h" #include "network.h" #include "onion.h" @@ -52,4 +53,11 @@ void do_tcp_server(TCP_Server *tcp_server, const Mono_Time *mono_time); nullable(1) void kill_tcp_server(TCP_Server *tcp_server); +/** @brief Returns a pointer to the net profile associated with `tcp_server`. + * + * Returns null if `tcp_server` is null. + */ +nullable(1) +const Net_Profile *tcp_server_get_net_profile(const TCP_Server *tcp_server); + #endif /* C_TOXCORE_TOXCORE_TCP_SERVER_H */ diff --git a/external/toxcore/c-toxcore/toxcore/announce.c b/external/toxcore/c-toxcore/toxcore/announce.c index 02a378f..dc34ca0 100644 --- a/external/toxcore/c-toxcore/toxcore/announce.c +++ b/external/toxcore/c-toxcore/toxcore/announce.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2020-2021 The TokTok team. + * Copyright © 2020-2025 The TokTok team. */ /** @@ -9,7 +9,6 @@ #include "announce.h" #include -#include #include #include "DHT.h" @@ -239,9 +238,9 @@ bool announce_store_data(Announcements *announce, const uint8_t *data_public_key if (length > 0) { assert(data != nullptr); - free(entry->data); + mem_delete(announce->mem, entry->data); - uint8_t *entry_data = (uint8_t *)malloc(length); + uint8_t *entry_data = (uint8_t *)mem_balloc(announce->mem, length); if (entry_data == nullptr) { entry->data = nullptr; // TODO(iphydf): Is this necessary? @@ -651,7 +650,7 @@ Announcements *new_announcements(const Logger *log, const Memory *mem, const Ran return nullptr; } - Announcements *announce = (Announcements *)calloc(1, sizeof(Announcements)); + Announcements *announce = (Announcements *)mem_alloc(mem, sizeof(Announcements)); if (announce == nullptr) { return nullptr; @@ -669,7 +668,7 @@ Announcements *new_announcements(const Logger *log, const Memory *mem, const Ran new_hmac_key(announce->rng, announce->hmac_key); announce->shared_keys = shared_key_cache_new(log, mono_time, mem, announce->secret_key, KEYS_TIMEOUT, MAX_KEYS_PER_SLOT); if (announce->shared_keys == nullptr) { - free(announce); + mem_delete(announce->mem, announce); return nullptr; } @@ -700,8 +699,8 @@ void kill_announcements(Announcements *announce) shared_key_cache_free(announce->shared_keys); for (uint32_t i = 0; i < ANNOUNCE_BUCKETS * ANNOUNCE_BUCKET_SIZE; ++i) { - free(announce->entries[i].data); + mem_delete(announce->mem, announce->entries[i].data); } - free(announce); + mem_delete(announce->mem, announce); } diff --git a/external/toxcore/c-toxcore/toxcore/announce.h b/external/toxcore/c-toxcore/toxcore/announce.h index bd05088..c139745 100644 --- a/external/toxcore/c-toxcore/toxcore/announce.h +++ b/external/toxcore/c-toxcore/toxcore/announce.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2020-2021 The TokTok team. + * Copyright © 2020-2025 The TokTok team. */ #ifndef C_TOXCORE_TOXCORE_ANNOUNCE_H diff --git a/external/toxcore/c-toxcore/toxcore/attributes.h b/external/toxcore/c-toxcore/toxcore/attributes.h index 087efe2..009dfa3 100644 --- a/external/toxcore/c-toxcore/toxcore/attributes.h +++ b/external/toxcore/c-toxcore/toxcore/attributes.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2022-2025 The TokTok team. */ /** diff --git a/external/toxcore/c-toxcore/toxcore/bin_pack.c b/external/toxcore/c-toxcore/toxcore/bin_pack.c index 56bdc9e..82e9d20 100644 --- a/external/toxcore/c-toxcore/toxcore/bin_pack.c +++ b/external/toxcore/c-toxcore/toxcore/bin_pack.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2022-2025 The TokTok team. */ #include "bin_pack.h" diff --git a/external/toxcore/c-toxcore/toxcore/bin_pack.h b/external/toxcore/c-toxcore/toxcore/bin_pack.h index ce6926a..4b2927f 100644 --- a/external/toxcore/c-toxcore/toxcore/bin_pack.h +++ b/external/toxcore/c-toxcore/toxcore/bin_pack.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2022-2025 The TokTok team. */ #ifndef C_TOXCORE_TOXCORE_BIN_PACK_H #define C_TOXCORE_TOXCORE_BIN_PACK_H diff --git a/external/toxcore/c-toxcore/toxcore/bin_unpack.c b/external/toxcore/c-toxcore/toxcore/bin_unpack.c index d6b1c52..82b7f8d 100644 --- a/external/toxcore/c-toxcore/toxcore/bin_unpack.c +++ b/external/toxcore/c-toxcore/toxcore/bin_unpack.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2022-2025 The TokTok team. */ #include "bin_unpack.h" diff --git a/external/toxcore/c-toxcore/toxcore/bin_unpack.h b/external/toxcore/c-toxcore/toxcore/bin_unpack.h index 0554bd1..3557476 100644 --- a/external/toxcore/c-toxcore/toxcore/bin_unpack.h +++ b/external/toxcore/c-toxcore/toxcore/bin_unpack.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2022-2025 The TokTok team. */ #ifndef C_TOXCORE_TOXCORE_BIN_UNPACK_H diff --git a/external/toxcore/c-toxcore/toxcore/ccompat.c b/external/toxcore/c-toxcore/toxcore/ccompat.c index 6e46897..3ac18a2 100644 --- a/external/toxcore/c-toxcore/toxcore/ccompat.c +++ b/external/toxcore/c-toxcore/toxcore/ccompat.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2022-2025 The TokTok team. */ #include "ccompat.h" diff --git a/external/toxcore/c-toxcore/toxcore/ccompat.h b/external/toxcore/c-toxcore/toxcore/ccompat.h index 47ab9ed..df68413 100644 --- a/external/toxcore/c-toxcore/toxcore/ccompat.h +++ b/external/toxcore/c-toxcore/toxcore/ccompat.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2021 The TokTok team. + * Copyright © 2016-2025 The TokTok team. */ /** diff --git a/external/toxcore/c-toxcore/toxcore/crypto_core.c b/external/toxcore/c-toxcore/toxcore/crypto_core.c index 9a460a2..8a6aa38 100644 --- a/external/toxcore/c-toxcore/toxcore/crypto_core.c +++ b/external/toxcore/c-toxcore/toxcore/crypto_core.c @@ -1,12 +1,11 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2024 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ #include "crypto_core.h" #include -#include #include #include diff --git a/external/toxcore/c-toxcore/toxcore/crypto_core.h b/external/toxcore/c-toxcore/toxcore/crypto_core.h index 3d405ed..558118e 100644 --- a/external/toxcore/c-toxcore/toxcore/crypto_core.h +++ b/external/toxcore/c-toxcore/toxcore/crypto_core.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2024 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ diff --git a/external/toxcore/c-toxcore/toxcore/crypto_core_pack.c b/external/toxcore/c-toxcore/toxcore/crypto_core_pack.c index a173d39..4c08fc7 100644 --- a/external/toxcore/c-toxcore/toxcore/crypto_core_pack.c +++ b/external/toxcore/c-toxcore/toxcore/crypto_core_pack.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2024 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ diff --git a/external/toxcore/c-toxcore/toxcore/crypto_core_pack.h b/external/toxcore/c-toxcore/toxcore/crypto_core_pack.h index 1efefff..85b47ff 100644 --- a/external/toxcore/c-toxcore/toxcore/crypto_core_pack.h +++ b/external/toxcore/c-toxcore/toxcore/crypto_core_pack.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2024 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ diff --git a/external/toxcore/c-toxcore/toxcore/crypto_core_test.cc b/external/toxcore/c-toxcore/toxcore/crypto_core_test.cc index 6203afb..b18f0c1 100644 --- a/external/toxcore/c-toxcore/toxcore/crypto_core_test.cc +++ b/external/toxcore/c-toxcore/toxcore/crypto_core_test.cc @@ -8,7 +8,6 @@ #include "crypto_core_test_util.hh" #include "mem_test_util.hh" -#include "util.h" namespace { diff --git a/external/toxcore/c-toxcore/toxcore/events/conference_connected.c b/external/toxcore/c-toxcore/toxcore/events/conference_connected.c index 8aeb172..d19f019 100644 --- a/external/toxcore/c-toxcore/toxcore/events/conference_connected.c +++ b/external/toxcore/c-toxcore/toxcore/events/conference_connected.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/external/toxcore/c-toxcore/toxcore/events/conference_invite.c b/external/toxcore/c-toxcore/toxcore/events/conference_invite.c index c9b8d14..04206e7 100644 --- a/external/toxcore/c-toxcore/toxcore/events/conference_invite.c +++ b/external/toxcore/c-toxcore/toxcore/events/conference_invite.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_pack.h" #include "../tox_unpack.h" diff --git a/external/toxcore/c-toxcore/toxcore/events/conference_message.c b/external/toxcore/c-toxcore/toxcore/events/conference_message.c index e509827..7cef201 100644 --- a/external/toxcore/c-toxcore/toxcore/events/conference_message.c +++ b/external/toxcore/c-toxcore/toxcore/events/conference_message.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_pack.h" #include "../tox_unpack.h" diff --git a/external/toxcore/c-toxcore/toxcore/events/conference_peer_list_changed.c b/external/toxcore/c-toxcore/toxcore/events/conference_peer_list_changed.c index f7efd3f..6874420 100644 --- a/external/toxcore/c-toxcore/toxcore/events/conference_peer_list_changed.c +++ b/external/toxcore/c-toxcore/toxcore/events/conference_peer_list_changed.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/external/toxcore/c-toxcore/toxcore/events/conference_peer_name.c b/external/toxcore/c-toxcore/toxcore/events/conference_peer_name.c index 9300656..36de6ba 100644 --- a/external/toxcore/c-toxcore/toxcore/events/conference_peer_name.c +++ b/external/toxcore/c-toxcore/toxcore/events/conference_peer_name.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/external/toxcore/c-toxcore/toxcore/events/conference_title.c b/external/toxcore/c-toxcore/toxcore/events/conference_title.c index d8e2690..cd92ba1 100644 --- a/external/toxcore/c-toxcore/toxcore/events/conference_title.c +++ b/external/toxcore/c-toxcore/toxcore/events/conference_title.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/external/toxcore/c-toxcore/toxcore/events/dht_get_nodes_response.c b/external/toxcore/c-toxcore/toxcore/events/dht_get_nodes_response.c index f637b9c..918b97d 100644 --- a/external/toxcore/c-toxcore/toxcore/events/dht_get_nodes_response.c +++ b/external/toxcore/c-toxcore/toxcore/events/dht_get_nodes_response.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2022-2025 The TokTok team. */ #include "events_alloc.h" @@ -13,6 +13,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_private.h" diff --git a/external/toxcore/c-toxcore/toxcore/events/events_alloc.c b/external/toxcore/c-toxcore/toxcore/events/events_alloc.c index 24eebb3..18405ea 100644 --- a/external/toxcore/c-toxcore/toxcore/events/events_alloc.c +++ b/external/toxcore/c-toxcore/toxcore/events/events_alloc.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2022-2025 The TokTok team. */ #include "events_alloc.h" diff --git a/external/toxcore/c-toxcore/toxcore/events/events_alloc.h b/external/toxcore/c-toxcore/toxcore/events/events_alloc.h index a40db97..7477559 100644 --- a/external/toxcore/c-toxcore/toxcore/events/events_alloc.h +++ b/external/toxcore/c-toxcore/toxcore/events/events_alloc.h @@ -1,16 +1,13 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2022-2025 The TokTok team. */ #ifndef C_TOXCORE_TOXCORE_EVENTS_EVENTS_ALLOC_H #define C_TOXCORE_TOXCORE_EVENTS_EVENTS_ALLOC_H #include "../attributes.h" -#include "../bin_pack.h" -#include "../bin_unpack.h" #include "../mem.h" #include "../tox.h" -#include "../tox_event.h" #include "../tox_events.h" #include "../tox_private.h" diff --git a/external/toxcore/c-toxcore/toxcore/events/file_chunk_request.c b/external/toxcore/c-toxcore/toxcore/events/file_chunk_request.c index 23e5f5b..d59b7a5 100644 --- a/external/toxcore/c-toxcore/toxcore/events/file_chunk_request.c +++ b/external/toxcore/c-toxcore/toxcore/events/file_chunk_request.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/external/toxcore/c-toxcore/toxcore/events/file_recv.c b/external/toxcore/c-toxcore/toxcore/events/file_recv.c index 389ce59..6ba2fde 100644 --- a/external/toxcore/c-toxcore/toxcore/events/file_recv.c +++ b/external/toxcore/c-toxcore/toxcore/events/file_recv.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/external/toxcore/c-toxcore/toxcore/events/file_recv_chunk.c b/external/toxcore/c-toxcore/toxcore/events/file_recv_chunk.c index 619011a..cb4487e 100644 --- a/external/toxcore/c-toxcore/toxcore/events/file_recv_chunk.c +++ b/external/toxcore/c-toxcore/toxcore/events/file_recv_chunk.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/external/toxcore/c-toxcore/toxcore/events/file_recv_control.c b/external/toxcore/c-toxcore/toxcore/events/file_recv_control.c index 3c575c0..72de182 100644 --- a/external/toxcore/c-toxcore/toxcore/events/file_recv_control.c +++ b/external/toxcore/c-toxcore/toxcore/events/file_recv_control.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_pack.h" #include "../tox_unpack.h" diff --git a/external/toxcore/c-toxcore/toxcore/events/friend_connection_status.c b/external/toxcore/c-toxcore/toxcore/events/friend_connection_status.c index 68c4869..d57980d 100644 --- a/external/toxcore/c-toxcore/toxcore/events/friend_connection_status.c +++ b/external/toxcore/c-toxcore/toxcore/events/friend_connection_status.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_pack.h" #include "../tox_unpack.h" diff --git a/external/toxcore/c-toxcore/toxcore/events/friend_lossless_packet.c b/external/toxcore/c-toxcore/toxcore/events/friend_lossless_packet.c index b783e33..098c5ab 100644 --- a/external/toxcore/c-toxcore/toxcore/events/friend_lossless_packet.c +++ b/external/toxcore/c-toxcore/toxcore/events/friend_lossless_packet.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/external/toxcore/c-toxcore/toxcore/events/friend_lossy_packet.c b/external/toxcore/c-toxcore/toxcore/events/friend_lossy_packet.c index e3a6ad5..016fa5d 100644 --- a/external/toxcore/c-toxcore/toxcore/events/friend_lossy_packet.c +++ b/external/toxcore/c-toxcore/toxcore/events/friend_lossy_packet.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/external/toxcore/c-toxcore/toxcore/events/friend_message.c b/external/toxcore/c-toxcore/toxcore/events/friend_message.c index dfd1dae..cedc3b5 100644 --- a/external/toxcore/c-toxcore/toxcore/events/friend_message.c +++ b/external/toxcore/c-toxcore/toxcore/events/friend_message.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_pack.h" #include "../tox_unpack.h" diff --git a/external/toxcore/c-toxcore/toxcore/events/friend_name.c b/external/toxcore/c-toxcore/toxcore/events/friend_name.c index b5a5129..9f8de0c 100644 --- a/external/toxcore/c-toxcore/toxcore/events/friend_name.c +++ b/external/toxcore/c-toxcore/toxcore/events/friend_name.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/external/toxcore/c-toxcore/toxcore/events/friend_read_receipt.c b/external/toxcore/c-toxcore/toxcore/events/friend_read_receipt.c index 88808b5..4ba4267 100644 --- a/external/toxcore/c-toxcore/toxcore/events/friend_read_receipt.c +++ b/external/toxcore/c-toxcore/toxcore/events/friend_read_receipt.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/external/toxcore/c-toxcore/toxcore/events/friend_request.c b/external/toxcore/c-toxcore/toxcore/events/friend_request.c index 6cd8ac6..631c236 100644 --- a/external/toxcore/c-toxcore/toxcore/events/friend_request.c +++ b/external/toxcore/c-toxcore/toxcore/events/friend_request.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2022-2025 The TokTok team. */ #include "events_alloc.h" @@ -13,6 +13,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_private.h" diff --git a/external/toxcore/c-toxcore/toxcore/events/friend_status.c b/external/toxcore/c-toxcore/toxcore/events/friend_status.c index 5af2d45..209eed8 100644 --- a/external/toxcore/c-toxcore/toxcore/events/friend_status.c +++ b/external/toxcore/c-toxcore/toxcore/events/friend_status.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_pack.h" #include "../tox_unpack.h" diff --git a/external/toxcore/c-toxcore/toxcore/events/friend_status_message.c b/external/toxcore/c-toxcore/toxcore/events/friend_status_message.c index 178342f..2c6d0fb 100644 --- a/external/toxcore/c-toxcore/toxcore/events/friend_status_message.c +++ b/external/toxcore/c-toxcore/toxcore/events/friend_status_message.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/external/toxcore/c-toxcore/toxcore/events/friend_typing.c b/external/toxcore/c-toxcore/toxcore/events/friend_typing.c index 96c7195..cb2cfac 100644 --- a/external/toxcore/c-toxcore/toxcore/events/friend_typing.c +++ b/external/toxcore/c-toxcore/toxcore/events/friend_typing.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/external/toxcore/c-toxcore/toxcore/events/group_custom_packet.c b/external/toxcore/c-toxcore/toxcore/events/group_custom_packet.c index eb4d49d..337d601 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_custom_packet.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_custom_packet.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/external/toxcore/c-toxcore/toxcore/events/group_custom_private_packet.c b/external/toxcore/c-toxcore/toxcore/events/group_custom_private_packet.c index f7c1634..9408a8d 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_custom_private_packet.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_custom_private_packet.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/external/toxcore/c-toxcore/toxcore/events/group_invite.c b/external/toxcore/c-toxcore/toxcore/events/group_invite.c index 6372eb5..3385b89 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_invite.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_invite.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/external/toxcore/c-toxcore/toxcore/events/group_join_fail.c b/external/toxcore/c-toxcore/toxcore/events/group_join_fail.c index 7b95454..8fdefb7 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_join_fail.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_join_fail.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_pack.h" #include "../tox_unpack.h" diff --git a/external/toxcore/c-toxcore/toxcore/events/group_message.c b/external/toxcore/c-toxcore/toxcore/events/group_message.c index 9ed07e4..dfb2bc9 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_message.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_message.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_pack.h" #include "../tox_unpack.h" diff --git a/external/toxcore/c-toxcore/toxcore/events/group_moderation.c b/external/toxcore/c-toxcore/toxcore/events/group_moderation.c index eb8d3ba..9e2b87e 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_moderation.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_moderation.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_pack.h" #include "../tox_unpack.h" diff --git a/external/toxcore/c-toxcore/toxcore/events/group_password.c b/external/toxcore/c-toxcore/toxcore/events/group_password.c index 14bda0f..33b64a0 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_password.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_password.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/external/toxcore/c-toxcore/toxcore/events/group_peer_exit.c b/external/toxcore/c-toxcore/toxcore/events/group_peer_exit.c index 405b4b6..a09f96b 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_peer_exit.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_peer_exit.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_pack.h" #include "../tox_unpack.h" diff --git a/external/toxcore/c-toxcore/toxcore/events/group_peer_join.c b/external/toxcore/c-toxcore/toxcore/events/group_peer_join.c index 8dc34be..d450564 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_peer_join.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_peer_join.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/external/toxcore/c-toxcore/toxcore/events/group_peer_limit.c b/external/toxcore/c-toxcore/toxcore/events/group_peer_limit.c index 157827c..65c44e7 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_peer_limit.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_peer_limit.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/external/toxcore/c-toxcore/toxcore/events/group_peer_name.c b/external/toxcore/c-toxcore/toxcore/events/group_peer_name.c index 063a6ab..d653ff5 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_peer_name.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_peer_name.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/external/toxcore/c-toxcore/toxcore/events/group_peer_status.c b/external/toxcore/c-toxcore/toxcore/events/group_peer_status.c index d646cae..3dc56df 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_peer_status.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_peer_status.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_pack.h" #include "../tox_unpack.h" diff --git a/external/toxcore/c-toxcore/toxcore/events/group_privacy_state.c b/external/toxcore/c-toxcore/toxcore/events/group_privacy_state.c index 3fa081c..fde9e1d 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_privacy_state.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_privacy_state.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_pack.h" #include "../tox_unpack.h" diff --git a/external/toxcore/c-toxcore/toxcore/events/group_private_message.c b/external/toxcore/c-toxcore/toxcore/events/group_private_message.c index 9b7f95a..24dd1db 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_private_message.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_private_message.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_pack.h" #include "../tox_unpack.h" diff --git a/external/toxcore/c-toxcore/toxcore/events/group_self_join.c b/external/toxcore/c-toxcore/toxcore/events/group_self_join.c index 6cc3080..0703863 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_self_join.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_self_join.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/external/toxcore/c-toxcore/toxcore/events/group_topic.c b/external/toxcore/c-toxcore/toxcore/events/group_topic.c index 7588d43..f9d1a7b 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_topic.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_topic.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -14,6 +14,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" /***************************************************** diff --git a/external/toxcore/c-toxcore/toxcore/events/group_topic_lock.c b/external/toxcore/c-toxcore/toxcore/events/group_topic_lock.c index 431ff38..a21acea 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_topic_lock.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_topic_lock.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_pack.h" #include "../tox_unpack.h" diff --git a/external/toxcore/c-toxcore/toxcore/events/group_voice_state.c b/external/toxcore/c-toxcore/toxcore/events/group_voice_state.c index d9c592b..ebc78dc 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_voice_state.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_voice_state.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_pack.h" #include "../tox_unpack.h" diff --git a/external/toxcore/c-toxcore/toxcore/events/self_connection_status.c b/external/toxcore/c-toxcore/toxcore/events/self_connection_status.c index b79d70b..f66daff 100644 --- a/external/toxcore/c-toxcore/toxcore/events/self_connection_status.c +++ b/external/toxcore/c-toxcore/toxcore/events/self_connection_status.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "events_alloc.h" @@ -12,6 +12,7 @@ #include "../ccompat.h" #include "../mem.h" #include "../tox.h" +#include "../tox_event.h" #include "../tox_events.h" #include "../tox_pack.h" #include "../tox_unpack.h" diff --git a/external/toxcore/c-toxcore/toxcore/forwarding.c b/external/toxcore/c-toxcore/toxcore/forwarding.c index 18ff320..6d608b5 100644 --- a/external/toxcore/c-toxcore/toxcore/forwarding.c +++ b/external/toxcore/c-toxcore/toxcore/forwarding.c @@ -1,11 +1,10 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2019-2022 The TokTok team. + * Copyright © 2019-2025 The TokTok team. */ #include "forwarding.h" #include -#include #include #include "DHT.h" @@ -13,12 +12,14 @@ #include "ccompat.h" #include "crypto_core.h" #include "logger.h" +#include "mem.h" #include "mono_time.h" #include "network.h" #include "timed_auth.h" struct Forwarding { const Logger *log; + const Memory *mem; const Random *rng; DHT *dht; const Mono_Time *mono_time; @@ -357,19 +358,20 @@ void set_callback_forward_reply(Forwarding *forwarding, forward_reply_cb *functi forwarding->forward_reply_callback_object = object; } -Forwarding *new_forwarding(const Logger *log, const Random *rng, const Mono_Time *mono_time, DHT *dht) +Forwarding *new_forwarding(const Logger *log, const Memory *mem, const Random *rng, const Mono_Time *mono_time, DHT *dht) { if (log == nullptr || mono_time == nullptr || dht == nullptr) { return nullptr; } - Forwarding *forwarding = (Forwarding *)calloc(1, sizeof(Forwarding)); + Forwarding *forwarding = (Forwarding *)mem_alloc(mem, sizeof(Forwarding)); if (forwarding == nullptr) { return nullptr; } forwarding->log = log; + forwarding->mem = mem; forwarding->rng = rng; forwarding->mono_time = mono_time; forwarding->dht = dht; @@ -396,5 +398,5 @@ void kill_forwarding(Forwarding *forwarding) crypto_memzero(forwarding->hmac_key, CRYPTO_HMAC_KEY_SIZE); - free(forwarding); + mem_delete(forwarding->mem, forwarding); } diff --git a/external/toxcore/c-toxcore/toxcore/forwarding.h b/external/toxcore/c-toxcore/toxcore/forwarding.h index bd0ef09..b4431f5 100644 --- a/external/toxcore/c-toxcore/toxcore/forwarding.h +++ b/external/toxcore/c-toxcore/toxcore/forwarding.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2019-2022 The TokTok team. + * Copyright © 2019-2025 The TokTok team. */ #ifndef C_TOXCORE_TOXCORE_FORWARDING_H @@ -9,6 +9,7 @@ #include "attributes.h" #include "crypto_core.h" #include "logger.h" +#include "mem.h" #include "mono_time.h" #include "network.h" @@ -116,7 +117,7 @@ non_null(1) nullable(2, 3) void set_callback_forward_reply(Forwarding *forwarding, forward_reply_cb *function, void *object); non_null() -Forwarding *new_forwarding(const Logger *log, const Random *rng, const Mono_Time *mono_time, DHT *dht); +Forwarding *new_forwarding(const Logger *log, const Memory *mem, const Random *rng, const Mono_Time *mono_time, DHT *dht); nullable(1) void kill_forwarding(Forwarding *forwarding); diff --git a/external/toxcore/c-toxcore/toxcore/friend_connection.c b/external/toxcore/c-toxcore/toxcore/friend_connection.c index 9c44288..78c6d4a 100644 --- a/external/toxcore/c-toxcore/toxcore/friend_connection.c +++ b/external/toxcore/c-toxcore/toxcore/friend_connection.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2014 Tox project. */ @@ -8,7 +8,6 @@ */ #include "friend_connection.h" -#include #include #include "DHT.h" @@ -18,6 +17,7 @@ #include "ccompat.h" #include "crypto_core.h" #include "logger.h" +#include "mem.h" #include "mono_time.h" #include "net_crypto.h" #include "network.h" @@ -69,6 +69,7 @@ static const Friend_Conn empty_friend_conn = {0}; struct Friend_Connections { const Mono_Time *mono_time; + const Memory *mem; const Logger *logger; Net_Crypto *net_crypto; DHT *dht; @@ -126,12 +127,12 @@ non_null() static bool realloc_friendconns(Friend_Connections *fr_c, uint32_t num) { if (num == 0) { - free(fr_c->conns); + mem_delete(fr_c->mem, fr_c->conns); fr_c->conns = nullptr; return true; } - Friend_Conn *newgroup_cons = (Friend_Conn *)realloc(fr_c->conns, num * sizeof(Friend_Conn)); + Friend_Conn *newgroup_cons = (Friend_Conn *)mem_vrealloc(fr_c->mem, fr_c->conns, num, sizeof(Friend_Conn)); if (newgroup_cons == nullptr) { return false; @@ -912,14 +913,14 @@ int send_friend_request_packet(Friend_Connections *fr_c, int friendcon_id, uint3 /** Create new friend_connections instance. */ Friend_Connections *new_friend_connections( - const Logger *logger, const Mono_Time *mono_time, const Network *ns, + const Logger *logger, const Memory *mem, const Mono_Time *mono_time, const Network *ns, Onion_Client *onion_c, bool local_discovery_enabled) { if (onion_c == nullptr) { return nullptr; } - Friend_Connections *const temp = (Friend_Connections *)calloc(1, sizeof(Friend_Connections)); + Friend_Connections *const temp = (Friend_Connections *)mem_alloc(mem, sizeof(Friend_Connections)); if (temp == nullptr) { return nullptr; @@ -928,7 +929,7 @@ Friend_Connections *new_friend_connections( temp->local_discovery_enabled = local_discovery_enabled; if (temp->local_discovery_enabled) { - temp->broadcast = lan_discovery_init(ns); + temp->broadcast = lan_discovery_init(mem, ns); if (temp->broadcast == nullptr) { LOGGER_ERROR(logger, "could not initialise LAN discovery"); @@ -937,6 +938,7 @@ Friend_Connections *new_friend_connections( } temp->mono_time = mono_time; + temp->mem = mem; temp->logger = logger; temp->dht = onion_get_dht(onion_c); temp->net_crypto = onion_get_net_crypto(onion_c); @@ -1038,9 +1040,9 @@ void kill_friend_connections(Friend_Connections *fr_c) // there might be allocated NONE connections if (fr_c->conns != nullptr) { - free(fr_c->conns); + mem_delete(fr_c->mem, fr_c->conns); } lan_discovery_kill(fr_c->broadcast); - free(fr_c); + mem_delete(fr_c->mem, fr_c); } diff --git a/external/toxcore/c-toxcore/toxcore/friend_connection.h b/external/toxcore/c-toxcore/toxcore/friend_connection.h index bbd4454..8c3c84f 100644 --- a/external/toxcore/c-toxcore/toxcore/friend_connection.h +++ b/external/toxcore/c-toxcore/toxcore/friend_connection.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2014 Tox project. */ @@ -12,9 +12,9 @@ #include #include "DHT.h" -#include "LAN_discovery.h" #include "attributes.h" #include "logger.h" +#include "mem.h" #include "mono_time.h" #include "net_crypto.h" #include "network.h" @@ -161,7 +161,7 @@ void set_friend_request_callback(Friend_Connections *fr_c, fr_request_cb *fr_req /** Create new friend_connections instance. */ non_null() Friend_Connections *new_friend_connections( - const Logger *logger, const Mono_Time *mono_time, const Network *ns, + const Logger *logger, const Memory *mem, const Mono_Time *mono_time, const Network *ns, Onion_Client *onion_c, bool local_discovery_enabled); /** main friend_connections loop. */ diff --git a/external/toxcore/c-toxcore/toxcore/friend_requests.c b/external/toxcore/c-toxcore/toxcore/friend_requests.c index 8b91544..e28ce7d 100644 --- a/external/toxcore/c-toxcore/toxcore/friend_requests.c +++ b/external/toxcore/c-toxcore/toxcore/friend_requests.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ @@ -8,18 +8,22 @@ */ #include "friend_requests.h" -#include #include #include "attributes.h" #include "ccompat.h" #include "crypto_core.h" #include "friend_connection.h" +#include "mem.h" #include "network.h" #include "onion.h" #include "onion_announce.h" #include "onion_client.h" +static_assert(ONION_CLIENT_MAX_DATA_SIZE <= MAX_DATA_REQUEST_SIZE, "ONION_CLIENT_MAX_DATA_SIZE is too big"); +static_assert(MAX_DATA_REQUEST_SIZE <= ONION_MAX_DATA_SIZE, "MAX_DATA_REQUEST_SIZE is too big"); +static_assert(SIZE_IPPORT <= ONION_SEND_BASE, "IP_Port does not fit in the onion packet"); + /** * NOTE: The following is just a temporary fix for the multiple friend requests received at the same time problem. * TODO(irungentoo): Make this better (This will most likely tie in with the way we will handle spam). @@ -32,6 +36,8 @@ struct Received_Requests { }; struct Friend_Requests { + const Memory *mem; + uint32_t nospam; fr_friend_request_cb *handle_friendrequest; uint8_t handle_friendrequest_isset; @@ -164,12 +170,24 @@ void friendreq_init(Friend_Requests *fr, Friend_Connections *fr_c) set_friend_request_callback(fr_c, &friendreq_handlepacket, fr); } -Friend_Requests *friendreq_new(void) +Friend_Requests *friendreq_new(const Memory *mem) { - return (Friend_Requests *)calloc(1, sizeof(Friend_Requests)); + Friend_Requests *fr = (Friend_Requests *)mem_alloc(mem, sizeof(Friend_Requests)); + + if (fr == nullptr) { + return nullptr; + } + + fr->mem = mem; + + return fr; } void friendreq_kill(Friend_Requests *fr) { - free(fr); + if (fr == nullptr) { + return; + } + + mem_delete(fr->mem, fr); } diff --git a/external/toxcore/c-toxcore/toxcore/friend_requests.h b/external/toxcore/c-toxcore/toxcore/friend_requests.h index 4f3ed0a..c7bae81 100644 --- a/external/toxcore/c-toxcore/toxcore/friend_requests.h +++ b/external/toxcore/c-toxcore/toxcore/friend_requests.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2014 Tox project. */ @@ -13,6 +13,7 @@ #include "attributes.h" #include "friend_connection.h" +#include "mem.h" // TODO(Jfreegman): This should be the maximum size that an onion client can handle. #define MAX_FRIEND_REQUEST_DATA_SIZE (ONION_CLIENT_MAX_DATA_SIZE - 100) @@ -50,7 +51,8 @@ void set_filter_function(Friend_Requests *fr, filter_function_cb *function, void non_null() void friendreq_init(Friend_Requests *fr, Friend_Connections *fr_c); -Friend_Requests *friendreq_new(void); +non_null() +Friend_Requests *friendreq_new(const Memory *mem); nullable(1) void friendreq_kill(Friend_Requests *fr); diff --git a/external/toxcore/c-toxcore/toxcore/group.c b/external/toxcore/c-toxcore/toxcore/group.c index 9d2783d..dbc3876 100644 --- a/external/toxcore/c-toxcore/toxcore/group.c +++ b/external/toxcore/c-toxcore/toxcore/group.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2014 Tox project. */ @@ -9,7 +9,6 @@ #include "group.h" #include -#include // calloc, free #include #include "DHT.h" @@ -149,6 +148,7 @@ typedef struct Group_c { } Group_c; struct Group_Chats { + const Memory *mem; const Mono_Time *mono_time; Messenger *m; @@ -248,19 +248,19 @@ static bool is_groupnumber_valid(const Group_Chats *g_c, uint32_t groupnumber) /** @brief Set the size of the groupchat list to num. * - * @retval false if realloc fails. + * @retval false if mem_vrealloc fails. * @retval true if it succeeds. */ non_null() static bool realloc_conferences(Group_Chats *g_c, uint16_t num) { if (num == 0) { - free(g_c->chats); + mem_delete(g_c->mem, g_c->chats); g_c->chats = nullptr; return true; } - Group_c *newgroup_chats = (Group_c *)realloc(g_c->chats, num * sizeof(Group_c)); + Group_c *newgroup_chats = (Group_c *)mem_vrealloc(g_c->mem, g_c->chats, num, sizeof(Group_c)); if (newgroup_chats == nullptr) { return false; @@ -302,10 +302,10 @@ static int32_t create_group_chat(Group_Chats *g_c) } non_null() -static void wipe_group_c(Group_c *g) +static void wipe_group_c(const Memory *mem, Group_c *g) { - free(g->frozen); - free(g->group); + mem_delete(mem, g->frozen); + mem_delete(mem, g->group); crypto_memzero(g, sizeof(Group_c)); } @@ -320,7 +320,7 @@ static bool wipe_group_chat(Group_Chats *g_c, uint32_t groupnumber) return false; } - wipe_group_c(&g_c->chats[groupnumber]); + wipe_group_c(g_c->mem, &g_c->chats[groupnumber]); uint16_t i; @@ -669,7 +669,7 @@ static int get_frozen_index(const Group_c *g, uint16_t peer_number) } non_null() -static bool delete_frozen(Group_c *g, uint32_t frozen_index) +static bool delete_frozen(const Memory *mem, Group_c *g, uint32_t frozen_index) { if (frozen_index >= g->numfrozen) { return false; @@ -678,14 +678,14 @@ static bool delete_frozen(Group_c *g, uint32_t frozen_index) --g->numfrozen; if (g->numfrozen == 0) { - free(g->frozen); + mem_delete(mem, g->frozen); g->frozen = nullptr; } else { if (g->numfrozen != frozen_index) { g->frozen[frozen_index] = g->frozen[g->numfrozen]; } - Group_Peer *const frozen_temp = (Group_Peer *)realloc(g->frozen, g->numfrozen * sizeof(Group_Peer)); + Group_Peer *const frozen_temp = (Group_Peer *)mem_vrealloc(mem, g->frozen, g->numfrozen, sizeof(Group_Peer)); if (frozen_temp == nullptr) { return false; @@ -726,7 +726,7 @@ static int note_peer_active(Group_Chats *g_c, uint32_t groupnumber, uint16_t pee /* Now thaw the peer */ - Group_Peer *temp = (Group_Peer *)realloc(g->group, (g->numpeers + 1) * sizeof(Group_Peer)); + Group_Peer *temp = (Group_Peer *)mem_vrealloc(g_c->mem, g->group, g->numpeers + 1, sizeof(Group_Peer)); if (temp == nullptr) { return -1; @@ -743,7 +743,7 @@ static int note_peer_active(Group_Chats *g_c, uint32_t groupnumber, uint16_t pee ++g->numpeers; - delete_frozen(g, frozen_index); + delete_frozen(g_c->mem, g, frozen_index); if (g_c->peer_list_changed_callback != nullptr) { g_c->peer_list_changed_callback(g_c->m, groupnumber, userdata); @@ -779,7 +779,7 @@ static void delete_any_peer_with_pk(Group_Chats *g_c, uint32_t groupnumber, cons const int frozen_index = frozen_in_group(g, real_pk); if (frozen_index >= 0) { - delete_frozen(g, frozen_index); + delete_frozen(g_c->mem, g, frozen_index); } } @@ -839,7 +839,7 @@ static int addpeer(Group_Chats *g_c, uint32_t groupnumber, const uint8_t *real_p delete_any_peer_with_pk(g_c, groupnumber, real_pk, userdata); - Group_Peer *temp = (Group_Peer *)realloc(g->group, (g->numpeers + 1) * sizeof(Group_Peer)); + Group_Peer *temp = (Group_Peer *)mem_vrealloc(g_c->mem, g->group, g->numpeers + 1, sizeof(Group_Peer)); if (temp == nullptr) { return -1; @@ -930,14 +930,14 @@ static bool delpeer(Group_Chats *g_c, uint32_t groupnumber, int peer_index, void void *peer_object = g->group[peer_index].object; if (g->numpeers == 0) { - free(g->group); + mem_delete(g_c->mem, g->group); g->group = nullptr; } else { if (g->numpeers != (uint32_t)peer_index) { g->group[peer_index] = g->group[g->numpeers]; } - Group_Peer *temp = (Group_Peer *)realloc(g->group, g->numpeers * sizeof(Group_Peer)); + Group_Peer *temp = (Group_Peer *)mem_vrealloc(g_c->mem, g->group, g->numpeers, sizeof(Group_Peer)); if (temp == nullptr) { return false; @@ -1034,7 +1034,7 @@ static bool delete_old_frozen(Group_c *g, const Memory *mem) } if (g->maxfrozen == 0) { - free(g->frozen); + mem_delete(mem, g->frozen); g->frozen = nullptr; g->numfrozen = 0; return true; @@ -1042,7 +1042,7 @@ static bool delete_old_frozen(Group_c *g, const Memory *mem) merge_sort(g->frozen, g->numfrozen, mem, &group_peer_cmp_funcs); - Group_Peer *temp = (Group_Peer *)realloc(g->frozen, g->maxfrozen * sizeof(Group_Peer)); + Group_Peer *temp = (Group_Peer *)mem_vrealloc(mem, g->frozen, g->maxfrozen, sizeof(Group_Peer)); if (temp == nullptr) { return false; @@ -1067,7 +1067,7 @@ static bool freeze_peer(Group_Chats *g_c, uint32_t groupnumber, int peer_index, return false; } - Group_Peer *temp = (Group_Peer *)realloc(g->frozen, (g->numfrozen + 1) * sizeof(Group_Peer)); + Group_Peer *temp = (Group_Peer *)mem_vrealloc(g_c->mem, g->frozen, g->numfrozen + 1, sizeof(Group_Peer)); if (temp == nullptr) { return false; @@ -1085,7 +1085,7 @@ static bool freeze_peer(Group_Chats *g_c, uint32_t groupnumber, int peer_index, ++g->numfrozen; - delete_old_frozen(g, g_c->m->mem); + delete_old_frozen(g, g_c->mem); return true; } @@ -1572,7 +1572,7 @@ int group_set_max_frozen(const Group_Chats *g_c, uint32_t groupnumber, uint32_t } g->maxfrozen = maxfrozen; - delete_old_frozen(g, g_c->m->mem); + delete_old_frozen(g, g_c->mem); return 0; } @@ -3665,7 +3665,7 @@ static uint32_t load_group(Group_c *g, const Group_Chats *g_c, const uint8_t *da } // This is inefficient, but allows us to check data consistency before allocating memory - Group_Peer *tmp_frozen = (Group_Peer *)realloc(g->frozen, (j + 1) * sizeof(Group_Peer)); + Group_Peer *tmp_frozen = (Group_Peer *)mem_vrealloc(g_c->mem, g->frozen, j + 1, sizeof(Group_Peer)); if (tmp_frozen == nullptr) { // Memory allocation failure @@ -3805,18 +3805,19 @@ bool conferences_load_state_section(Group_Chats *g_c, const uint8_t *data, uint3 } /** Create new groupchat instance. */ -Group_Chats *new_groupchats(const Mono_Time *mono_time, Messenger *m) +Group_Chats *new_groupchats(const Mono_Time *mono_time, const Memory *mem, Messenger *m) { if (m == nullptr) { return nullptr; } - Group_Chats *temp = (Group_Chats *)calloc(1, sizeof(Group_Chats)); + Group_Chats *temp = (Group_Chats *)mem_alloc(mem, sizeof(Group_Chats)); if (temp == nullptr) { return nullptr; } + temp->mem = mem; temp->mono_time = mono_time; temp->m = m; temp->fr_c = m->fr_c; @@ -3867,7 +3868,7 @@ void kill_groupchats(Group_Chats *g_c) m_callback_conference_invite(g_c->m, nullptr); set_global_status_callback(g_c->m->fr_c, nullptr, nullptr); g_c->m->conferences_object = nullptr; - free(g_c); + mem_delete(g_c->mem, g_c); } /** diff --git a/external/toxcore/c-toxcore/toxcore/group.h b/external/toxcore/c-toxcore/toxcore/group.h index 5ce0327..706428f 100644 --- a/external/toxcore/c-toxcore/toxcore/group.h +++ b/external/toxcore/c-toxcore/toxcore/group.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2014 Tox project. */ @@ -15,6 +15,7 @@ #include "Messenger.h" #include "attributes.h" #include "crypto_core.h" +#include "mem.h" #include "mono_time.h" #include "state.h" @@ -390,7 +391,7 @@ bool conferences_load_state_section( /** Create new groupchat instance. */ non_null() -Group_Chats *new_groupchats(const Mono_Time *mono_time, Messenger *m); +Group_Chats *new_groupchats(const Mono_Time *mono_time, const Memory *mem, Messenger *m); /** main groupchats loop. */ non_null(1) nullable(2) diff --git a/external/toxcore/c-toxcore/toxcore/group_announce.c b/external/toxcore/c-toxcore/toxcore/group_announce.c index ee08319..caa7019 100644 --- a/external/toxcore/c-toxcore/toxcore/group_announce.c +++ b/external/toxcore/c-toxcore/toxcore/group_announce.c @@ -5,7 +5,6 @@ #include "group_announce.h" -#include #include #include "DHT.h" @@ -13,6 +12,7 @@ #include "ccompat.h" #include "crypto_core.h" #include "logger.h" +#include "mem.h" #include "mono_time.h" #include "network.h" @@ -36,7 +36,7 @@ static void remove_announces(GC_Announces_List *gc_announces_list, GC_Announces announces->next_announce->prev_announce = announces->prev_announce; } - free(announces); + mem_delete(gc_announces_list->mem, announces); } /** @@ -343,10 +343,11 @@ int gca_unpack_announces_list(const Logger *log, const uint8_t *data, uint16_t l non_null() static GC_Announces *gca_new_announces( + const Memory *mem, GC_Announces_List *gc_announces_list, const GC_Public_Announce *public_announce) { - GC_Announces *announces = (GC_Announces *)calloc(1, sizeof(GC_Announces)); + GC_Announces *announces = (GC_Announces *)mem_alloc(mem, sizeof(GC_Announces)); if (announces == nullptr) { return nullptr; @@ -366,7 +367,7 @@ static GC_Announces *gca_new_announces( return announces; } -GC_Peer_Announce *gca_add_announce(const Mono_Time *mono_time, GC_Announces_List *gc_announces_list, +GC_Peer_Announce *gca_add_announce(const Memory *mem, const Mono_Time *mono_time, GC_Announces_List *gc_announces_list, const GC_Public_Announce *public_announce) { if (gc_announces_list == nullptr || public_announce == nullptr) { @@ -377,7 +378,7 @@ GC_Peer_Announce *gca_add_announce(const Mono_Time *mono_time, GC_Announces_List // No entry for this chat_id exists so we create one if (announces == nullptr) { - announces = gca_new_announces(gc_announces_list, public_announce); + announces = gca_new_announces(mem, gc_announces_list, public_announce); if (announces == nullptr) { return nullptr; @@ -410,9 +411,17 @@ bool gca_is_valid_announce(const GC_Announce *announce) return announce->tcp_relays_count > 0 || announce->ip_port_is_set; } -GC_Announces_List *new_gca_list(void) +GC_Announces_List *new_gca_list(const Memory *mem) { - return (GC_Announces_List *)calloc(1, sizeof(GC_Announces_List)); + GC_Announces_List *announces_list = (GC_Announces_List *)mem_alloc(mem, sizeof(GC_Announces_List)); + + if (announces_list == nullptr) { + return nullptr; + } + + announces_list->mem = mem; + + return announces_list; } void kill_gca(GC_Announces_List *announces_list) @@ -425,11 +434,11 @@ void kill_gca(GC_Announces_List *announces_list) while (root != nullptr) { GC_Announces *next = root->next_announce; - free(root); + mem_delete(announces_list->mem, root); root = next; } - free(announces_list); + mem_delete(announces_list->mem, announces_list); } /* How long we save a peer's announce before we consider it stale and remove it. */ diff --git a/external/toxcore/c-toxcore/toxcore/group_announce.h b/external/toxcore/c-toxcore/toxcore/group_announce.h index 72f2cfc..2e043a0 100644 --- a/external/toxcore/c-toxcore/toxcore/group_announce.h +++ b/external/toxcore/c-toxcore/toxcore/group_announce.h @@ -16,6 +16,7 @@ #include "attributes.h" #include "crypto_core.h" #include "logger.h" +#include "mem.h" #include "mono_time.h" #include "network.h" @@ -79,6 +80,8 @@ struct GC_Announces { /* A list of all announces. */ struct GC_Announces_List { + const Memory *mem; + GC_Announces *root_announces; uint64_t last_timeout_check; }; @@ -87,7 +90,8 @@ struct GC_Announces_List { * * The caller is responsible for freeing the memory with `kill_gca`. */ -GC_Announces_List *new_gca_list(void); +non_null() +GC_Announces_List *new_gca_list(const Memory *mem); /** @brief Frees all dynamically allocated memory associated with `announces_list`. */ nullable(1) @@ -135,7 +139,7 @@ int gca_get_announces(const GC_Announces_List *gc_announces_list, GC_Announce *g * @retval null on failure. */ non_null() -GC_Peer_Announce *gca_add_announce(const Mono_Time *mono_time, GC_Announces_List *gc_announces_list, +GC_Peer_Announce *gca_add_announce(const Memory *mem, const Mono_Time *mono_time, GC_Announces_List *gc_announces_list, const GC_Public_Announce *public_announce); /** @brief Packs an announce into a data buffer. diff --git a/external/toxcore/c-toxcore/toxcore/group_announce_fuzz_test.cc b/external/toxcore/c-toxcore/toxcore/group_announce_fuzz_test.cc index 9f5655a..154e486 100644 --- a/external/toxcore/c-toxcore/toxcore/group_announce_fuzz_test.cc +++ b/external/toxcore/c-toxcore/toxcore/group_announce_fuzz_test.cc @@ -60,7 +60,8 @@ void TestDoGca(Fuzz_Data &input) mem, [](void *user_data) { return *static_cast(user_data); }, &clock), [mem](Mono_Time *ptr) { mono_time_free(mem, ptr); }); assert(mono_time != nullptr); - std::unique_ptr gca(new_gca_list(), kill_gca); + std::unique_ptr gca( + new_gca_list(mem), kill_gca); assert(gca != nullptr); while (!input.empty()) { @@ -72,7 +73,7 @@ void TestDoGca(Fuzz_Data &input) CONSUME_OR_RETURN(const uint8_t *data, input, length); GC_Public_Announce public_announce; if (gca_unpack_public_announce(logger.get(), data, length, &public_announce) != -1) { - gca_add_announce(mono_time.get(), gca.get(), &public_announce); + gca_add_announce(mem, mono_time.get(), gca.get(), &public_announce); } break; } diff --git a/external/toxcore/c-toxcore/toxcore/group_announce_test.cc b/external/toxcore/c-toxcore/toxcore/group_announce_test.cc index f1027f3..1b37fd3 100644 --- a/external/toxcore/c-toxcore/toxcore/group_announce_test.cc +++ b/external/toxcore/c-toxcore/toxcore/group_announce_test.cc @@ -27,7 +27,7 @@ protected: mono_time_set_current_time_callback( mono_time_, [](void *user_data) { return *static_cast(user_data); }, &clock_); - gca_ = new_gca_list(); + gca_ = new_gca_list(mem_); ASSERT_NE(gca_, nullptr); } @@ -54,10 +54,10 @@ TEST_F(Announces, CanBeCreatedAndDeleted) { GC_Public_Announce ann{}; ann.chat_public_key[0] = 0x88; - ASSERT_NE(gca_add_announce(mono_time_, gca_, &ann), nullptr); + ASSERT_NE(gca_add_announce(mem_, mono_time_, gca_, &ann), nullptr); #ifndef _DEBUG - ASSERT_EQ(gca_add_announce(mono_time_, gca_, nullptr), nullptr); - ASSERT_EQ(gca_add_announce(mono_time_, nullptr, &ann), nullptr); + ASSERT_EQ(gca_add_announce(mem_, mono_time_, gca_, nullptr), nullptr); + ASSERT_EQ(gca_add_announce(mem_, mono_time_, nullptr, &ann), nullptr); #endif } @@ -67,7 +67,7 @@ TEST_F(Announces, AnnouncesCanTimeOut) ASSERT_EQ(gca_->root_announces, nullptr); GC_Public_Announce ann{}; ann.chat_public_key[0] = 0xae; - ASSERT_NE(gca_add_announce(mono_time_, gca_, &ann), nullptr); + ASSERT_NE(gca_add_announce(mem_, mono_time_, gca_, &ann), nullptr); ASSERT_NE(gca_->root_announces, nullptr); ASSERT_EQ(gca_->root_announces->chat_id[0], 0xae); @@ -95,9 +95,9 @@ TEST_F(Announces, AnnouncesGetAndCleanup) ann2.chat_public_key[0] = 0x92; ann2.base_announce.peer_public_key[0] = 0x7c; - ASSERT_NE(gca_add_announce(mono_time_, gca_, &ann1), nullptr); - ASSERT_NE(gca_add_announce(mono_time_, gca_, &ann2), nullptr); - ASSERT_NE(gca_add_announce(mono_time_, gca_, &ann2), nullptr); + ASSERT_NE(gca_add_announce(mem_, mono_time_, gca_, &ann1), nullptr); + ASSERT_NE(gca_add_announce(mem_, mono_time_, gca_, &ann2), nullptr); + ASSERT_NE(gca_add_announce(mem_, mono_time_, gca_, &ann2), nullptr); uint8_t empty_pk[ENC_PUBLIC_KEY_SIZE] = {0}; diff --git a/external/toxcore/c-toxcore/toxcore/group_chats.c b/external/toxcore/c-toxcore/toxcore/group_chats.c index 5e0b788..a364ac9 100644 --- a/external/toxcore/c-toxcore/toxcore/group_chats.c +++ b/external/toxcore/c-toxcore/toxcore/group_chats.c @@ -12,7 +12,6 @@ #include #include -#include #include #include "DHT.h" @@ -1486,7 +1485,7 @@ static int group_packet_unwrap(const Logger *log, const Memory *mem, const GC_Co return -1; } - uint8_t *plain = (uint8_t *)malloc(length); + uint8_t *plain = (uint8_t *)mem_balloc(mem, length); if (plain == nullptr) { LOGGER_ERROR(log, "Failed to allocate memory for plain data buffer"); @@ -1497,7 +1496,7 @@ static int group_packet_unwrap(const Logger *log, const Memory *mem, const GC_Co length - CRYPTO_NONCE_SIZE, plain); if (plain_len <= 0) { - free(plain); + mem_delete(mem, plain); return plain_len == 0 ? -3 : -2; } @@ -1511,7 +1510,7 @@ static int group_packet_unwrap(const Logger *log, const Memory *mem, const GC_Co --plain_len; if (plain_len < min_plain_len) { - free(plain); + mem_delete(mem, plain); return -3; } } @@ -1528,7 +1527,7 @@ static int group_packet_unwrap(const Logger *log, const Memory *mem, const GC_Co memcpy(data, real_plain + header_len, plain_len); - free(plain); + mem_delete(mem, plain); return plain_len; } @@ -1554,7 +1553,7 @@ int group_packet_wrap( return -1; } - uint8_t *plain = (uint8_t *)malloc(packet_size); + uint8_t *plain = (uint8_t *)mem_balloc(mem, packet_size); if (plain == nullptr) { return -1; @@ -1582,20 +1581,20 @@ int group_packet_wrap( const uint16_t plain_len = padding_len + enc_header_len + length; const uint16_t encrypt_buf_size = plain_len + CRYPTO_MAC_SIZE; - uint8_t *encrypt = (uint8_t *)malloc(encrypt_buf_size); + uint8_t *encrypt = (uint8_t *)mem_balloc(mem, encrypt_buf_size); if (encrypt == nullptr) { - free(plain); + mem_delete(mem, plain); return -2; } const int enc_len = encrypt_data_symmetric(mem, shared_key, nonce, plain, plain_len, encrypt); - free(plain); + mem_delete(mem, plain); if (enc_len != encrypt_buf_size) { LOGGER_ERROR(log, "encryption failed. packet type: 0x%02x, enc_len: %d", gp_packet_type, enc_len); - free(encrypt); + mem_delete(mem, encrypt); return -3; } @@ -1604,7 +1603,7 @@ int group_packet_wrap( memcpy(packet + 1 + ENC_PUBLIC_KEY_SIZE, nonce, CRYPTO_NONCE_SIZE); memcpy(packet + 1 + ENC_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE, encrypt, enc_len); - free(encrypt); + mem_delete(mem, encrypt); return 1 + ENC_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + enc_len; } @@ -1628,7 +1627,7 @@ static bool send_lossy_group_packet(const GC_Chat *chat, const GC_Connection *gc } const uint16_t packet_size = gc_get_wrapped_packet_size(length, NET_PACKET_GC_LOSSY); - uint8_t *packet = (uint8_t *)malloc(packet_size); + uint8_t *packet = (uint8_t *)mem_balloc(chat->mem, packet_size); if (packet == nullptr) { return false; @@ -1640,13 +1639,13 @@ static bool send_lossy_group_packet(const GC_Chat *chat, const GC_Connection *gc if (len < 0) { LOGGER_ERROR(chat->log, "Failed to encrypt packet (type: 0x%02x, error: %d)", packet_type, len); - free(packet); + mem_delete(chat->mem, packet); return false; } const bool ret = gcc_send_packet(chat, gconn, packet, (uint16_t)len); - free(packet); + mem_delete(chat->mem, packet); return ret; } @@ -1884,7 +1883,7 @@ static bool sync_response_send_peers(GC_Chat *chat, GC_Connection *gconn, uint32 return true; } - uint8_t *response = (uint8_t *)malloc(MAX_GC_PACKET_CHUNK_SIZE); + uint8_t *response = (uint8_t *)mem_balloc(chat->mem, MAX_GC_PACKET_CHUNK_SIZE); if (response == nullptr) { return false; @@ -1924,7 +1923,7 @@ static bool sync_response_send_peers(GC_Chat *chat, GC_Connection *gconn, uint32 ++num_announces; } - free(response); + mem_delete(chat->mem, response); if (num_announces == 0) { // we send an empty sync response even if we didn't send any peers as an acknowledgement @@ -2388,7 +2387,7 @@ static bool send_gc_broadcast_message(const GC_Chat *chat, const uint8_t *data, return false; } - uint8_t *packet = (uint8_t *)malloc(length + GC_BROADCAST_ENC_HEADER_SIZE); + uint8_t *packet = (uint8_t *)mem_balloc(chat->mem, length + GC_BROADCAST_ENC_HEADER_SIZE); if (packet == nullptr) { return false; @@ -2398,7 +2397,7 @@ static bool send_gc_broadcast_message(const GC_Chat *chat, const uint8_t *data, const bool ret = send_gc_lossless_packet_all_peers(chat, packet, packet_len, GP_BROADCAST); - free(packet); + mem_delete(chat->mem, packet); return ret; } @@ -2632,7 +2631,7 @@ void gc_get_chat_id(const GC_Chat *chat, uint8_t *dest) non_null() static bool send_self_to_peer(const GC_Chat *chat, GC_Connection *gconn) { - GC_Peer *self = (GC_Peer *)calloc(1, sizeof(GC_Peer)); + GC_Peer *self = (GC_Peer *)mem_alloc(chat->mem, sizeof(GC_Peer)); if (self == nullptr) { return false; @@ -2641,10 +2640,10 @@ static bool send_self_to_peer(const GC_Chat *chat, GC_Connection *gconn) copy_self(chat, self); const uint16_t data_size = PACKED_GC_PEER_SIZE + sizeof(uint16_t) + MAX_GC_PASSWORD_SIZE; - uint8_t *data = (uint8_t *)malloc(data_size); + uint8_t *data = (uint8_t *)mem_balloc(chat->mem, data_size); if (data == nullptr) { - free(self); + mem_delete(chat->mem, self); return false; } @@ -2661,17 +2660,17 @@ static bool send_self_to_peer(const GC_Chat *chat, GC_Connection *gconn) const int packed_len = pack_gc_peer(data + length, data_size - length, self); length += packed_len; - free(self); + mem_delete(chat->mem, self); if (packed_len <= 0) { LOGGER_DEBUG(chat->log, "pack_gc_peer failed in handle_gc_peer_info_request_request %d", packed_len); - free(data); + mem_delete(chat->mem, data); return false; } const bool ret = send_lossless_group_packet(chat, gconn, data, length, GP_PEER_INFO_RESPONSE); - free(data); + mem_delete(chat->mem, data); return ret; } @@ -2737,7 +2736,7 @@ static bool send_gc_peer_exchange(const GC_Chat *chat, GC_Connection *gconn) * Return -5 if supplied group password is invalid. * Return -6 if we fail to add the peer to the peer list. * Return -7 if peer's role cannot be validated. - * Return -8 if malloc fails. + * Return -8 if memory allocation fails. */ non_null(1, 2, 4) nullable(6) static int handle_gc_peer_info_response(const GC_Session *c, GC_Chat *chat, uint32_t peer_number, @@ -2782,7 +2781,7 @@ static int handle_gc_peer_info_response(const GC_Session *c, GC_Chat *chat, uint return -1; } - GC_Peer *peer_info = (GC_Peer *)calloc(1, sizeof(GC_Peer)); + GC_Peer *peer_info = (GC_Peer *)mem_alloc(chat->mem, sizeof(GC_Peer)); if (peer_info == nullptr) { return -8; @@ -2790,17 +2789,17 @@ static int handle_gc_peer_info_response(const GC_Session *c, GC_Chat *chat, uint if (unpack_gc_peer(peer_info, data + unpacked_len, length - unpacked_len) == -1) { LOGGER_ERROR(chat->log, "unpack_gc_peer() failed"); - free(peer_info); + mem_delete(chat->mem, peer_info); return -6; } if (peer_update(chat, peer_info, peer_number) == -1) { LOGGER_WARNING(chat->log, "peer_update() failed"); - free(peer_info); + mem_delete(chat->mem, peer_info); return -6; } - free(peer_info); + mem_delete(chat->mem, peer_info); const bool was_confirmed = gconn->confirmed; gconn->confirmed = true; @@ -3182,7 +3181,7 @@ static int handle_gc_sanctions_list(const GC_Session *c, GC_Chat *chat, const ui Mod_Sanction_Creds creds; - Mod_Sanction *sanctions = (Mod_Sanction *)calloc(num_sanctions, sizeof(Mod_Sanction)); + Mod_Sanction *sanctions = (Mod_Sanction *)mem_valloc(chat->mem, num_sanctions, sizeof(Mod_Sanction)); if (sanctions == nullptr) { return -1; @@ -3193,25 +3192,25 @@ static int handle_gc_sanctions_list(const GC_Session *c, GC_Chat *chat, const ui if (unpacked_num != num_sanctions) { LOGGER_WARNING(chat->log, "Failed to unpack sanctions list: %d", unpacked_num); - free(sanctions); + mem_delete(chat->mem, sanctions); return handle_gc_sanctions_list_error(chat); } if (!sanctions_list_check_integrity(&chat->moderation, &creds, sanctions, num_sanctions)) { LOGGER_WARNING(chat->log, "Sanctions list failed integrity check"); - free(sanctions); + mem_delete(chat->mem, sanctions); return handle_gc_sanctions_list_error(chat); } if (creds.version < chat->moderation.sanctions_creds.version) { - free(sanctions); + mem_delete(chat->mem, sanctions); return 0; } // this may occur if two mods change the sanctions list at the exact same time if (creds.version == chat->moderation.sanctions_creds.version && creds.checksum <= chat->moderation.sanctions_creds.checksum) { - free(sanctions); + mem_delete(chat->mem, sanctions); return 0; } @@ -3248,7 +3247,7 @@ static int make_gc_mod_list_packet(const GC_Chat *chat, uint8_t *data, uint32_t const uint16_t length = sizeof(uint16_t) + mod_list_size; if (mod_list_size > 0) { - uint8_t *packed_mod_list = (uint8_t *)malloc(mod_list_size); + uint8_t *packed_mod_list = (uint8_t *)mem_balloc(chat->mem, mod_list_size); if (packed_mod_list == nullptr) { return -1; @@ -3257,7 +3256,7 @@ static int make_gc_mod_list_packet(const GC_Chat *chat, uint8_t *data, uint32_t mod_list_pack(&chat->moderation, packed_mod_list); memcpy(data + sizeof(uint16_t), packed_mod_list, mod_list_size); - free(packed_mod_list); + mem_delete(chat->mem, packed_mod_list); } return length; @@ -3272,7 +3271,7 @@ static bool send_peer_mod_list(const GC_Chat *chat, GC_Connection *gconn) { const uint16_t mod_list_size = chat->moderation.num_mods * MOD_LIST_ENTRY_SIZE; const uint16_t length = sizeof(uint16_t) + mod_list_size; - uint8_t *packet = (uint8_t *)malloc(length); + uint8_t *packet = (uint8_t *)mem_balloc(chat->mem, length); if (packet == nullptr) { return false; @@ -3281,13 +3280,13 @@ static bool send_peer_mod_list(const GC_Chat *chat, GC_Connection *gconn) const int packet_len = make_gc_mod_list_packet(chat, packet, length, mod_list_size); if (packet_len != length) { - free(packet); + mem_delete(chat->mem, packet); return false; } const bool ret = send_lossless_group_packet(chat, gconn, packet, length, GP_MOD_LIST); - free(packet); + mem_delete(chat->mem, packet); return ret; } @@ -3331,7 +3330,7 @@ static bool send_peer_sanctions_list(const GC_Chat *chat, GC_Connection *gconn) const uint16_t packet_size = MOD_SANCTION_PACKED_SIZE * chat->moderation.num_sanctions + sizeof(uint16_t) + MOD_SANCTIONS_CREDS_SIZE; - uint8_t *packet = (uint8_t *)malloc(packet_size); + uint8_t *packet = (uint8_t *)mem_balloc(chat->mem, packet_size); if (packet == nullptr) { return false; @@ -3340,13 +3339,13 @@ static bool send_peer_sanctions_list(const GC_Chat *chat, GC_Connection *gconn) const int packet_len = make_gc_sanctions_list_packet(chat, packet, packet_size); if (packet_len == -1) { - free(packet); + mem_delete(chat->mem, packet); return false; } const bool ret = send_lossless_group_packet(chat, gconn, packet, (uint16_t)packet_len, GP_SANCTIONS_LIST); - free(packet); + mem_delete(chat->mem, packet); return ret; } @@ -3361,7 +3360,7 @@ static bool broadcast_gc_sanctions_list(const GC_Chat *chat) const uint16_t packet_size = MOD_SANCTION_PACKED_SIZE * chat->moderation.num_sanctions + sizeof(uint16_t) + MOD_SANCTIONS_CREDS_SIZE; - uint8_t *packet = (uint8_t *)malloc(packet_size); + uint8_t *packet = (uint8_t *)mem_balloc(chat->mem, packet_size); if (packet == nullptr) { return false; @@ -3370,13 +3369,13 @@ static bool broadcast_gc_sanctions_list(const GC_Chat *chat) const int packet_len = make_gc_sanctions_list_packet(chat, packet, packet_size); if (packet_len == -1) { - free(packet); + mem_delete(chat->mem, packet); return false; } const bool ret = send_gc_lossless_packet_all_peers(chat, packet, (uint16_t)packet_len, GP_SANCTIONS_LIST); - free(packet); + mem_delete(chat->mem, packet); return ret; } @@ -3407,7 +3406,7 @@ static bool broadcast_gc_mod_list(const GC_Chat *chat) { const uint16_t mod_list_size = chat->moderation.num_mods * MOD_LIST_ENTRY_SIZE; const uint16_t length = sizeof(uint16_t) + mod_list_size; - uint8_t *packet = (uint8_t *)malloc(length); + uint8_t *packet = (uint8_t *)mem_balloc(chat->mem, length); if (packet == nullptr) { return false; @@ -3416,13 +3415,13 @@ static bool broadcast_gc_mod_list(const GC_Chat *chat) const int packet_len = make_gc_mod_list_packet(chat, packet, length, mod_list_size); if (packet_len != length) { - free(packet); + mem_delete(chat->mem, packet); return false; } const bool ret = send_gc_lossless_packet_all_peers(chat, packet, length, GP_MOD_LIST); - free(packet); + mem_delete(chat->mem, packet); return ret; } @@ -3700,7 +3699,7 @@ non_null() static bool send_peer_topic(const GC_Chat *chat, GC_Connection *gconn) { const uint16_t packet_buf_size = SIGNATURE_SIZE + chat->topic_info.length + GC_MIN_PACKED_TOPIC_INFO_SIZE; - uint8_t *packet = (uint8_t *)malloc(packet_buf_size); + uint8_t *packet = (uint8_t *)mem_balloc(chat->mem, packet_buf_size); if (packet == nullptr) { return false; @@ -3709,16 +3708,16 @@ static bool send_peer_topic(const GC_Chat *chat, GC_Connection *gconn) const int packet_len = make_gc_topic_packet(chat, packet, packet_buf_size); if (packet_len != packet_buf_size) { - free(packet); + mem_delete(chat->mem, packet); return false; } if (!send_lossless_group_packet(chat, gconn, packet, packet_buf_size, GP_TOPIC)) { - free(packet); + mem_delete(chat->mem, packet); return false; } - free(packet); + mem_delete(chat->mem, packet); return true; } @@ -3768,7 +3767,7 @@ non_null() static bool broadcast_gc_topic(const GC_Chat *chat) { const uint16_t packet_buf_size = SIGNATURE_SIZE + chat->topic_info.length + GC_MIN_PACKED_TOPIC_INFO_SIZE; - uint8_t *packet = (uint8_t *)malloc(packet_buf_size); + uint8_t *packet = (uint8_t *)mem_balloc(chat->mem, packet_buf_size); if (packet == nullptr) { return false; @@ -3777,13 +3776,13 @@ static bool broadcast_gc_topic(const GC_Chat *chat) const int packet_len = make_gc_topic_packet(chat, packet, packet_buf_size); if (packet_len != packet_buf_size) { - free(packet); + mem_delete(chat->mem, packet); return false; } const bool ret = send_gc_lossless_packet_all_peers(chat, packet, packet_buf_size, GP_TOPIC); - free(packet); + mem_delete(chat->mem, packet); return ret; } @@ -3833,7 +3832,7 @@ int gc_set_topic(GC_Chat *chat, const uint8_t *topic, uint16_t length) chat->topic_info.checksum = get_gc_topic_checksum(&chat->topic_info); const uint16_t packet_buf_size = length + GC_MIN_PACKED_TOPIC_INFO_SIZE; - uint8_t *packed_topic = (uint8_t *)malloc(packet_buf_size); + uint8_t *packed_topic = (uint8_t *)mem_balloc(chat->mem, packet_buf_size); if (packed_topic == nullptr) { return -3; @@ -3860,13 +3859,13 @@ int gc_set_topic(GC_Chat *chat, const uint8_t *topic, uint16_t length) chat->topic_prev_checksum = old_topic_info.checksum; chat->topic_time_set = mono_time_get(chat->mono_time); - free(packed_topic); + mem_delete(chat->mem, packed_topic); return 0; ON_ERROR: chat->topic_info = old_topic_info; memcpy(chat->topic_sig, old_topic_sig, SIGNATURE_SIZE); - free(packed_topic); + mem_delete(chat->mem, packed_topic); return err; } @@ -4117,24 +4116,24 @@ int gc_founder_set_password(GC_Chat *chat, const uint8_t *password, uint16_t pas } const uint16_t oldlen = chat->shared_state.password_length; - uint8_t *oldpasswd = memdup(chat->shared_state.password, oldlen); + uint8_t *oldpasswd = memdup(chat->mem, chat->shared_state.password, oldlen); if (oldpasswd == nullptr && oldlen > 0) { return -4; } if (!set_gc_password_local(chat, password, password_length)) { - free(oldpasswd); + mem_delete(chat->mem, oldpasswd); return -2; } if (!sign_gc_shared_state(chat)) { set_gc_password_local(chat, oldpasswd, oldlen); - free(oldpasswd); + mem_delete(chat->mem, oldpasswd); return -2; } - free(oldpasswd); + mem_delete(chat->mem, oldpasswd); if (!broadcast_gc_shared_state(chat)) { return -3; @@ -4259,7 +4258,7 @@ non_null() static bool send_gc_set_mod(const GC_Chat *chat, const GC_Connection *gconn, bool add_mod) { const uint16_t length = 1 + SIG_PUBLIC_KEY_SIZE; - uint8_t *data = (uint8_t *)malloc(length); + uint8_t *data = (uint8_t *)mem_balloc(chat->mem, length); if (data == nullptr) { return false; @@ -4270,11 +4269,11 @@ static bool send_gc_set_mod(const GC_Chat *chat, const GC_Connection *gconn, boo memcpy(data + 1, get_sig_pk(&gconn->addr.public_key), SIG_PUBLIC_KEY_SIZE); if (!send_gc_broadcast_message(chat, data, length, GM_SET_MOD)) { - free(data); + mem_delete(chat->mem, data); return false; } - free(data); + mem_delete(chat->mem, data); return true; } @@ -4462,7 +4461,7 @@ static bool send_gc_set_observer(const GC_Chat *chat, const Extended_Public_Key const uint8_t *sanction_data, uint16_t length, bool add_obs) { const uint16_t packet_len = 1 + ENC_PUBLIC_KEY_SIZE + SIG_PUBLIC_KEY_SIZE + length; - uint8_t *packet = (uint8_t *)malloc(packet_len); + uint8_t *packet = (uint8_t *)mem_balloc(chat->mem, packet_len); if (packet == nullptr) { return false; @@ -4475,11 +4474,11 @@ static bool send_gc_set_observer(const GC_Chat *chat, const Extended_Public_Key memcpy(packet + 1 + ENC_PUBLIC_KEY_SIZE + SIG_PUBLIC_KEY_SIZE, sanction_data, length); if (!send_gc_broadcast_message(chat, packet, packet_len, GM_SET_OBSERVER)) { - free(packet); + mem_delete(chat->mem, packet); return false; } - free(packet); + mem_delete(chat->mem, packet); return true; } @@ -4896,7 +4895,7 @@ int gc_send_message(const GC_Chat *chat, const uint8_t *message, uint16_t length const uint8_t packet_type = type == GC_MESSAGE_TYPE_NORMAL ? GM_PLAIN_MESSAGE : GM_ACTION_MESSAGE; const uint16_t length_raw = length + GC_MESSAGE_PSEUDO_ID_SIZE; - uint8_t *message_raw = (uint8_t *)malloc(length_raw); + uint8_t *message_raw = (uint8_t *)mem_balloc(chat->mem, length_raw); if (message_raw == nullptr) { return -5; @@ -4908,11 +4907,11 @@ int gc_send_message(const GC_Chat *chat, const uint8_t *message, uint16_t length memcpy(message_raw + GC_MESSAGE_PSEUDO_ID_SIZE, message, length); if (!send_gc_broadcast_message(chat, message_raw, length_raw, packet_type)) { - free(message_raw); + mem_delete(chat->mem, message_raw); return -5; } - free(message_raw); + mem_delete(chat->mem, message_raw); if (message_id != nullptr) { *message_id = pseudo_msg_id; @@ -4984,7 +4983,7 @@ int gc_send_private_message(const GC_Chat *chat, GC_Peer_Id peer_id, uint8_t typ } const uint16_t raw_length = 1 + length + GC_MESSAGE_PSEUDO_ID_SIZE; - uint8_t *message_with_type = (uint8_t *)malloc(raw_length); + uint8_t *message_with_type = (uint8_t *)mem_balloc(chat->mem, raw_length); if (message_with_type == nullptr) { return -6; @@ -4997,23 +4996,23 @@ int gc_send_private_message(const GC_Chat *chat, GC_Peer_Id peer_id, uint8_t typ memcpy(message_with_type + 1 + GC_MESSAGE_PSEUDO_ID_SIZE, message, length); - uint8_t *packet = (uint8_t *)malloc(raw_length + GC_BROADCAST_ENC_HEADER_SIZE); + uint8_t *packet = (uint8_t *)mem_balloc(chat->mem, raw_length + GC_BROADCAST_ENC_HEADER_SIZE); if (packet == nullptr) { - free(message_with_type); + mem_delete(chat->mem, message_with_type); return -6; } const uint16_t packet_len = make_gc_broadcast_header(message_with_type, raw_length, packet, GM_PRIVATE_MESSAGE); - free(message_with_type); + mem_delete(chat->mem, message_with_type); if (!send_lossless_group_packet(chat, gconn, packet, packet_len, GP_BROADCAST)) { - free(packet); + mem_delete(chat->mem, packet); return -6; } - free(packet); + mem_delete(chat->mem, packet); if (message_id != nullptr) { *message_id = pseudo_msg_id; @@ -5331,7 +5330,7 @@ static int handle_gc_message_ack(const GC_Chat *chat, GC_Connection *gconn, cons const Group_Message_Ack_Type type = (Group_Message_Ack_Type) data[0]; if (type == GR_ACK_RECV) { - if (!gcc_handle_ack(chat->log, gconn, message_id)) { + if (!gcc_handle_ack(chat->log, chat->mem, gconn, message_id)) { return -2; } @@ -5535,7 +5534,7 @@ static int unwrap_group_handshake_packet(const Logger *log, const Memory *mem, c * * Return length of encrypted packet on success. * Return -1 if packet size is invalid. - * Return -2 on malloc failure. + * Return -2 on memory allocation failure. * Return -3 if encryption fails. */ non_null() @@ -5553,7 +5552,7 @@ static int wrap_group_handshake_packet( random_nonce(rng, nonce); const size_t encrypt_buf_size = length + CRYPTO_MAC_SIZE; - uint8_t *encrypt = (uint8_t *)malloc(encrypt_buf_size); + uint8_t *encrypt = (uint8_t *)mem_balloc(mem, encrypt_buf_size); if (encrypt == nullptr) { return -2; @@ -5563,7 +5562,7 @@ static int wrap_group_handshake_packet( if (enc_len < 0 || (size_t)enc_len != encrypt_buf_size) { LOGGER_ERROR(log, "Failed to encrypt group handshake packet (len: %d)", enc_len); - free(encrypt); + mem_delete(mem, encrypt); return -3; } @@ -5573,7 +5572,7 @@ static int wrap_group_handshake_packet( memcpy(packet + 1 + ENC_PUBLIC_KEY_SIZE + ENC_PUBLIC_KEY_SIZE, nonce, CRYPTO_NONCE_SIZE); memcpy(packet + 1 + ENC_PUBLIC_KEY_SIZE + ENC_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE, encrypt, enc_len); - free(encrypt); + mem_delete(mem, encrypt); return 1 + ENC_PUBLIC_KEY_SIZE + ENC_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + enc_len; } @@ -5946,7 +5945,7 @@ static int handle_gc_handshake_packet(GC_Chat *chat, const uint8_t *sender_pk, c } const size_t data_buf_size = length - CRYPTO_NONCE_SIZE - CRYPTO_MAC_SIZE; - uint8_t *data = (uint8_t *)malloc(data_buf_size); + uint8_t *data = (uint8_t *)mem_balloc(chat->mem, data_buf_size); if (data == nullptr) { return -1; @@ -5957,7 +5956,7 @@ static int handle_gc_handshake_packet(GC_Chat *chat, const uint8_t *sender_pk, c if (plain_len < GC_MIN_HS_PACKET_PAYLOAD_SIZE) { LOGGER_DEBUG(chat->log, "Failed to unwrap handshake packet (probably a stale request using an old key)"); - free(data); + mem_delete(chat->mem, data); return -1; } @@ -5973,11 +5972,11 @@ static int handle_gc_handshake_packet(GC_Chat *chat, const uint8_t *sender_pk, c } else if (handshake_type == GH_RESPONSE) { peer_number = handle_gc_handshake_response(chat, sender_pk, real_data, real_len); } else { - free(data); + mem_delete(chat->mem, data); return -1; } - free(data); + mem_delete(chat->mem, data); GC_Connection *gconn = get_gc_connection(chat, peer_number); @@ -6172,7 +6171,7 @@ static bool handle_gc_lossless_packet(const GC_Session *c, GC_Chat *chat, const return true; } - uint8_t *data = (uint8_t *)malloc(length); + uint8_t *data = (uint8_t *)mem_balloc(chat->mem, length); if (data == nullptr) { LOGGER_DEBUG(chat->log, "Failed to allocate memory for packet data buffer"); @@ -6188,13 +6187,13 @@ static bool handle_gc_lossless_packet(const GC_Session *c, GC_Chat *chat, const Ip_Ntoa ip_str; LOGGER_DEBUG(chat->log, "Failed to unwrap lossless packet from %s:%d: %d", net_ip_ntoa(&gconn->addr.ip_port.ip, &ip_str), net_ntohs(gconn->addr.ip_port.port), len); - free(data); + mem_delete(chat->mem, data); return false; } if (!gconn->handshaked && (packet_type != GP_HS_RESPONSE_ACK && packet_type != GP_INVITE_REQUEST)) { LOGGER_DEBUG(chat->log, "Got lossless packet type 0x%02x from unconfirmed peer", packet_type); - free(data); + mem_delete(chat->mem, data); return false; } @@ -6204,28 +6203,27 @@ static bool handle_gc_lossless_packet(const GC_Session *c, GC_Chat *chat, const if (message_id == 3 && is_invite_packet && gconn->received_message_id <= 1) { // we missed initial handshake request. Drop this packet and wait for another handshake request. LOGGER_DEBUG(chat->log, "Missed handshake packet, type: 0x%02x", packet_type); - free(data); + mem_delete(chat->mem, data); return false; } - const int lossless_ret = gcc_handle_received_message(chat->log, chat->mono_time, gconn, data, (uint16_t) len, - packet_type, message_id, direct_conn); + const int lossless_ret = gcc_handle_received_message(chat->log, chat->mem, chat->mono_time, gconn, data, (uint16_t) len, packet_type, message_id, direct_conn); if (packet_type == GP_INVITE_REQUEST && !gconn->handshaked) { // Both peers sent request at same time - free(data); + mem_delete(chat->mem, data); return true; } if (lossless_ret < 0) { LOGGER_DEBUG(chat->log, "failed to handle packet %llu (type: 0x%02x, id: %llu)", (unsigned long long)message_id, packet_type, (unsigned long long)message_id); - free(data); + mem_delete(chat->mem, data); return false; } /* Duplicate packet */ if (lossless_ret == 0) { - free(data); + mem_delete(chat->mem, data); return gc_send_message_ack(chat, gconn, message_id, GR_ACK_RECV); } @@ -6233,7 +6231,7 @@ static bool handle_gc_lossless_packet(const GC_Session *c, GC_Chat *chat, const if (lossless_ret == 1) { LOGGER_TRACE(chat->log, "received out of order packet from peer %u. expected %llu, got %llu", peer_number, (unsigned long long)gconn->received_message_id + 1, (unsigned long long)message_id); - free(data); + mem_delete(chat->mem, data); return gc_send_message_ack(chat, gconn, gconn->received_message_id + 1, GR_ACK_REQ); } @@ -6241,13 +6239,13 @@ static bool handle_gc_lossless_packet(const GC_Session *c, GC_Chat *chat, const if (lossless_ret == 3) { const bool frag_ret = handle_gc_packet_fragment(c, chat, peer_number, gconn, data, (uint16_t)len, packet_type, message_id, userdata); - free(data); + mem_delete(chat->mem, data); return frag_ret; } const bool ret = handle_gc_lossless_helper(c, chat, peer_number, data, (uint16_t)len, packet_type, userdata); - free(data); + mem_delete(chat->mem, data); if (!ret) { return false; @@ -6326,7 +6324,7 @@ static bool handle_gc_lossy_packet(const GC_Session *c, GC_Chat *chat, const uin return false; } - uint8_t *data = (uint8_t *)malloc(length); + uint8_t *data = (uint8_t *)mem_balloc(chat->mem, length); if (data == nullptr) { LOGGER_ERROR(chat->log, "Failed to allocate memory for packet buffer"); @@ -6341,13 +6339,13 @@ static bool handle_gc_lossy_packet(const GC_Session *c, GC_Chat *chat, const uin Ip_Ntoa ip_str; LOGGER_DEBUG(chat->log, "Failed to unwrap lossy packet from %s:%d: %d", net_ip_ntoa(&gconn->addr.ip_port.ip, &ip_str), net_ntohs(gconn->addr.ip_port.port), len); - free(data); + mem_delete(chat->mem, data); return false; } const int ret = handle_gc_lossy_packet_decoded(c, chat, gconn, peer, packet_type, data, (uint16_t)len, userdata); - free(data); + mem_delete(chat->mem, data); if (ret < 0) { LOGGER_DEBUG(chat->log, "Lossy packet handle error %d: type: 0x%02x, peernumber %d", ret, packet_type, @@ -6721,7 +6719,7 @@ static bool peer_delete(const GC_Session *c, GC_Chat *chat, uint32_t peer_number assert(nick_length <= MAX_GC_NICK_SIZE); memcpy(nick, peer->nick, nick_length); - gcc_peer_cleanup(&peer->gconn); + gcc_peer_cleanup(chat->mem, &peer->gconn); --chat->numpeers; @@ -6733,7 +6731,7 @@ static bool peer_delete(const GC_Session *c, GC_Chat *chat, uint32_t peer_number 0 }; - GC_Peer *tmp_group = (GC_Peer *)realloc(chat->group, chat->numpeers * sizeof(GC_Peer)); + GC_Peer *tmp_group = (GC_Peer *)mem_vrealloc(chat->mem, chat->group, chat->numpeers, sizeof(GC_Peer)); if (tmp_group == nullptr) { return false; @@ -6809,8 +6807,8 @@ int peer_add(GC_Chat *chat, const IP_Port *ipp, const uint8_t *public_key) } } - GC_Message_Array_Entry *send = (GC_Message_Array_Entry *)calloc(GCC_BUFFER_SIZE, sizeof(GC_Message_Array_Entry)); - GC_Message_Array_Entry *recv = (GC_Message_Array_Entry *)calloc(GCC_BUFFER_SIZE, sizeof(GC_Message_Array_Entry)); + GC_Message_Array_Entry *send = (GC_Message_Array_Entry *)mem_valloc(chat->mem, GCC_BUFFER_SIZE, sizeof(GC_Message_Array_Entry)); + GC_Message_Array_Entry *recv = (GC_Message_Array_Entry *)mem_valloc(chat->mem, GCC_BUFFER_SIZE, sizeof(GC_Message_Array_Entry)); if (send == nullptr || recv == nullptr) { LOGGER_ERROR(chat->log, "Failed to allocate memory for gconn buffers"); @@ -6819,22 +6817,22 @@ int peer_add(GC_Chat *chat, const IP_Port *ipp, const uint8_t *public_key) kill_tcp_connection_to(chat->tcp_conn, tcp_connection_num); } - free(send); - free(recv); + mem_delete(chat->mem, send); + mem_delete(chat->mem, recv); return -1; } - GC_Peer *tmp_group = (GC_Peer *)realloc(chat->group, (chat->numpeers + 1) * sizeof(GC_Peer)); + GC_Peer *tmp_group = (GC_Peer *)mem_vrealloc(chat->mem, chat->group, chat->numpeers + 1, sizeof(GC_Peer)); if (tmp_group == nullptr) { - LOGGER_ERROR(chat->log, "Failed to allocate memory for group realloc"); + LOGGER_ERROR(chat->log, "Failed to allocate memory for group mem_vrealloc"); if (tcp_connection_num != -1) { kill_tcp_connection_to(chat->tcp_conn, tcp_connection_num); } - free(send); - free(recv); + mem_delete(chat->mem, send); + mem_delete(chat->mem, recv); return -1; } @@ -7057,7 +7055,7 @@ non_null() static bool ping_peer(const GC_Chat *chat, const GC_Connection *gconn) { const uint16_t buf_size = GC_PING_PACKET_MIN_DATA_SIZE + sizeof(IP_Port); - uint8_t *data = (uint8_t *)malloc(buf_size); + uint8_t *data = (uint8_t *)mem_balloc(chat->mem, buf_size); if (data == nullptr) { return false; @@ -7103,11 +7101,11 @@ static bool ping_peer(const GC_Chat *chat, const GC_Connection *gconn) } if (!send_lossy_group_packet(chat, gconn, data, packed_len, GP_PING)) { - free(data); + mem_delete(chat->mem, data); return false; } - free(data); + mem_delete(chat->mem, data); return true; } @@ -7320,15 +7318,15 @@ void do_gc(GC_Session *c, void *userdata) * Return true on success. */ non_null() -static bool realloc_groupchats(GC_Session *c, uint32_t n) +static bool realloc_groupchats(const Memory *mem, GC_Session *c, uint32_t n) { if (n == 0) { - free(c->chats); + mem_delete(mem, c->chats); c->chats = nullptr; return true; } - GC_Chat *temp = (GC_Chat *)realloc(c->chats, n * sizeof(GC_Chat)); + GC_Chat *temp = (GC_Chat *)mem_vrealloc(mem, c->chats, n, sizeof(GC_Chat)); if (temp == nullptr) { return false; @@ -7339,7 +7337,7 @@ static bool realloc_groupchats(GC_Session *c, uint32_t n) } non_null() -static int get_new_group_index(GC_Session *c) +static int get_new_group_index(const Memory *mem, GC_Session *c) { if (c == nullptr) { return -1; @@ -7351,7 +7349,7 @@ static int get_new_group_index(GC_Session *c) } } - if (!realloc_groupchats(c, c->chats_index + 1)) { + if (!realloc_groupchats(mem, c, c->chats_index + 1)) { return -1; } @@ -7379,7 +7377,7 @@ static void add_tcp_relays_to_chat(const GC_Session *c, GC_Chat *chat) return; } - Node_format *tcp_relays = (Node_format *)calloc(num_relays, sizeof(Node_format)); + Node_format *tcp_relays = (Node_format *)mem_valloc(chat->mem, num_relays, sizeof(Node_format)); if (tcp_relays == nullptr) { return; @@ -7391,7 +7389,7 @@ static void add_tcp_relays_to_chat(const GC_Session *c, GC_Chat *chat) add_tcp_relay_global(chat->tcp_conn, &tcp_relays[i].ip_port, tcp_relays[i].public_key); } - free(tcp_relays); + mem_delete(chat->mem, tcp_relays); } non_null() @@ -7461,7 +7459,7 @@ non_null() static bool create_new_chat_ext_keypair(GC_Chat *chat); non_null() -static int create_new_group(GC_Session *c, const uint8_t *nick, size_t nick_length, bool founder, +static int create_new_group(const Memory *mem, GC_Session *c, const uint8_t *nick, size_t nick_length, bool founder, const Group_Privacy_State privacy_state) { if (nick == nullptr || nick_length == 0) { @@ -7472,7 +7470,7 @@ static int create_new_group(GC_Session *c, const uint8_t *nick, size_t nick_leng return -1; } - const int group_number = get_new_group_index(c); + const int group_number = get_new_group_index(mem, c); if (group_number == -1) { return -1; @@ -7611,7 +7609,7 @@ void gc_group_save(const GC_Chat *chat, Bin_Pack *bp) int gc_group_load(GC_Session *c, Bin_Unpack *bu) { - const int group_number = get_new_group_index(c); + const int group_number = get_new_group_index(c->messenger->mem, c); if (group_number < 0) { return -1; @@ -7681,7 +7679,7 @@ int gc_group_add(GC_Session *c, Group_Privacy_State privacy_state, return -2; } - const int group_number = create_new_group(c, nick, nick_length, true, privacy_state); + const int group_number = create_new_group(c->messenger->mem, c, nick, nick_length, true, privacy_state); if (group_number == -1) { return -3; @@ -7733,10 +7731,52 @@ int gc_group_add(GC_Session *c, Group_Privacy_State privacy_state, return group_number; } +int gc_rejoin_group(GC_Session *c, GC_Chat *chat, const uint8_t *passwd, uint16_t passwd_len) +{ + if (c == nullptr) { + LOGGER_ERROR(chat->log, "NULL group session pointer."); + return -1; + } + + if (passwd != nullptr && passwd_len > 0) { + if (!set_gc_password_local(chat, passwd, passwd_len)) { + LOGGER_WARNING(chat->log, "Failed to set new password during reconnect."); + } + } + + chat->time_connected = 0; + + if (group_can_handle_packets(chat)) { + send_gc_self_exit(chat, nullptr, 0); + } + + for (uint32_t i = 1; i < chat->numpeers; ++i) { + GC_Connection *gconn = get_gc_connection(chat, i); + assert(gconn != nullptr); + + gcc_mark_for_deletion(gconn, chat->tcp_conn, GC_EXIT_TYPE_SELF_DISCONNECTED, nullptr, 0); + } + + if (is_public_chat(chat)) { + kill_group_friend_connection(c, chat); + + if (!m_create_group_connection(c->messenger, chat)) { + LOGGER_WARNING(chat->log, "Failed to create new messenger connection for group"); + return -1; + } + + chat->update_self_announces = true; + } + + chat->connection_state = CS_CONNECTING; + + return 0; +} + int gc_group_join(GC_Session *c, const uint8_t *chat_id, const uint8_t *nick, size_t nick_length, const uint8_t *passwd, uint16_t passwd_len) { - if (chat_id == nullptr || group_exists(c, chat_id) || getfriend_id(c->messenger, chat_id) != -1) { + if (chat_id == nullptr) { return -2; } @@ -7748,7 +7788,15 @@ int gc_group_join(GC_Session *c, const uint8_t *chat_id, const uint8_t *nick, si return -4; } - const int group_number = create_new_group(c, nick, nick_length, false, GI_PUBLIC); + GC_Chat *existing_group = gc_get_group_by_public_key(c, chat_id); + + // If we're already in the group we try to reconnect to it + if (existing_group != nullptr) { + const int ret = gc_rejoin_group(c, existing_group, passwd, passwd_len); + return ret != 0 ? -6 : ret; + } + + const int group_number = create_new_group(c->messenger->mem, c, nick, nick_length, false, GI_PUBLIC); if (group_number == -1) { return -1; @@ -7806,41 +7854,6 @@ bool gc_disconnect_from_group(const GC_Session *c, GC_Chat *chat) return true; } -int gc_rejoin_group(GC_Session *c, GC_Chat *chat) -{ - if (c == nullptr || chat == nullptr) { - return -1; - } - - chat->time_connected = 0; - - if (group_can_handle_packets(chat)) { - send_gc_self_exit(chat, nullptr, 0); - } - - for (uint32_t i = 1; i < chat->numpeers; ++i) { - GC_Connection *gconn = get_gc_connection(chat, i); - assert(gconn != nullptr); - - gcc_mark_for_deletion(gconn, chat->tcp_conn, GC_EXIT_TYPE_SELF_DISCONNECTED, nullptr, 0); - } - - if (is_public_chat(chat)) { - kill_group_friend_connection(c, chat); - - if (!m_create_group_connection(c->messenger, chat)) { - LOGGER_WARNING(chat->log, "Failed to create new messenger connection for group"); - return -2; - } - - chat->update_self_announces = true; - } - - chat->connection_state = CS_CONNECTING; - - return 0; -} - bool group_not_added(const GC_Session *c, const uint8_t *chat_id, uint32_t length) { if (length < CHAT_ID_SIZE) { @@ -7861,7 +7874,7 @@ int gc_invite_friend(const GC_Session *c, GC_Chat *chat, int32_t friend_number, assert(group_name_length <= MAX_GC_GROUP_NAME_SIZE); - uint8_t *packet = (uint8_t *)malloc(2 + CHAT_ID_SIZE + ENC_PUBLIC_KEY_SIZE + group_name_length); + uint8_t *packet = (uint8_t *)mem_balloc(chat->mem, 2 + CHAT_ID_SIZE + ENC_PUBLIC_KEY_SIZE + group_name_length); if (packet == nullptr) { return -1; @@ -7882,11 +7895,11 @@ int gc_invite_friend(const GC_Session *c, GC_Chat *chat, int32_t friend_number, assert(length <= MAX_GC_PACKET_SIZE); if (!callback(c->messenger, friend_number, packet, length)) { - free(packet); + mem_delete(chat->mem, packet); return -2; } - free(packet); + mem_delete(chat->mem, packet); chat->saved_invites[chat->saved_invites_index] = friend_number; chat->saved_invites_index = (chat->saved_invites_index + 1) % MAX_GC_SAVED_INVITES; @@ -7955,7 +7968,7 @@ static bool send_gc_invite_confirmed_packet(const Messenger *m, const GC_Chat *c } const uint16_t packet_length = 2 + length; - uint8_t *packet = (uint8_t *)malloc(packet_length); + uint8_t *packet = (uint8_t *)mem_balloc(chat->mem, packet_length); if (packet == nullptr) { return false; @@ -7967,11 +7980,11 @@ static bool send_gc_invite_confirmed_packet(const Messenger *m, const GC_Chat *c memcpy(packet + 2, data, length); if (!send_group_invite_packet(m, friend_number, packet, packet_length)) { - free(packet); + mem_delete(chat->mem, packet); return false; } - free(packet); + mem_delete(chat->mem, packet); return true; } @@ -8184,7 +8197,7 @@ int gc_accept_invite(GC_Session *c, int32_t friend_number, const uint8_t *data, const uint8_t *chat_id = data; const uint8_t *invite_chat_pk = data + CHAT_ID_SIZE; - const int group_number = create_new_group(c, nick, nick_length, false, GI_PUBLIC); + const int group_number = create_new_group(c->messenger->mem, c, nick, nick_length, false, GI_PUBLIC); if (group_number == -1) { return -2; @@ -8233,7 +8246,7 @@ GC_Session *new_dht_groupchats(Messenger *m) return nullptr; } - GC_Session *c = (GC_Session *)calloc(1, sizeof(GC_Session)); + GC_Session *c = (GC_Session *)mem_alloc(m->mem, sizeof(GC_Session)); if (c == nullptr) { return nullptr; @@ -8264,7 +8277,7 @@ static void group_cleanup(const GC_Session *c, GC_Chat *chat) gcc_cleanup(chat); if (chat->group != nullptr) { - free(chat->group); + mem_delete(chat->mem, chat->group); chat->group = nullptr; } @@ -8299,7 +8312,7 @@ static void group_delete(GC_Session *c, GC_Chat *chat) if (c->chats_index != i) { c->chats_index = i; - if (!realloc_groupchats(c, c->chats_index)) { + if (!realloc_groupchats(c->messenger->mem, c, c->chats_index)) { LOGGER_ERROR(c->messenger->log, "Failed to reallocate groupchats array"); } } @@ -8342,8 +8355,8 @@ void kill_dht_groupchats(GC_Session *c) networking_registerhandler(c->messenger->net, NET_PACKET_GC_HANDSHAKE, nullptr, nullptr); onion_group_announce_register(c->messenger->onion_c, nullptr, nullptr); - free(c->chats); - free(c); + mem_delete(c->messenger->mem, c->chats); + mem_delete(c->messenger->mem, c); } bool gc_group_is_valid(const GC_Chat *chat) @@ -8409,19 +8422,7 @@ GC_Chat *gc_get_group_by_public_key(const GC_Session *c, const uint8_t *public_k /** Return True if chat_id exists in the session chat array */ static bool group_exists(const GC_Session *c, const uint8_t *chat_id) { - for (uint32_t i = 0; i < c->chats_index; ++i) { - const GC_Chat *chat = &c->chats[i]; - - if (chat->connection_state == CS_NONE) { - continue; - } - - if (memcmp(get_chat_id(&chat->chat_public_key), chat_id, CHAT_ID_SIZE) == 0) { - return true; - } - } - - return false; + return gc_get_group_by_public_key(c, chat_id) != nullptr; } /** Creates a new 32-byte session encryption keypair and puts the results in `public_key` and `secret_key`. */ diff --git a/external/toxcore/c-toxcore/toxcore/group_chats.h b/external/toxcore/c-toxcore/toxcore/group_chats.h index dfd2bd1..481dee1 100644 --- a/external/toxcore/c-toxcore/toxcore/group_chats.h +++ b/external/toxcore/c-toxcore/toxcore/group_chats.h @@ -650,7 +650,7 @@ int gc_group_add(GC_Session *c, Group_Privacy_State privacy_state, const uint8_t * * Return group_number on success. * Return -1 if the group object fails to initialize. - * Return -2 if chat_id is NULL or a group with chat_id already exists in the chats array. + * Return -2 if chat_id is NULL. * Return -3 if nick is too long. * Return -4 if nick is empty or nick length is zero. * Return -5 if there is an error setting the group password. @@ -676,8 +676,8 @@ bool gc_disconnect_from_group(const GC_Session *c, GC_Chat *chat); * Returns -1 if the group handler object or chat object is null. * Returns -2 if the Messenger friend connection fails to initialize. */ -non_null() -int gc_rejoin_group(GC_Session *c, GC_Chat *chat); +non_null(1, 2) nullable(3) +int gc_rejoin_group(GC_Session *c, GC_Chat *chat, const uint8_t *passwd, uint16_t passwd_len); /** @brief Joins a group using the invite data received in a friend's group invite. * diff --git a/external/toxcore/c-toxcore/toxcore/group_common.h b/external/toxcore/c-toxcore/toxcore/group_common.h index 0e965bb..4c2e98a 100644 --- a/external/toxcore/c-toxcore/toxcore/group_common.h +++ b/external/toxcore/c-toxcore/toxcore/group_common.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2022 The TokTok team. + * Copyright © 2016-2025 The TokTok team. */ /** diff --git a/external/toxcore/c-toxcore/toxcore/group_connection.c b/external/toxcore/c-toxcore/toxcore/group_connection.c index 6a2fedb..79c9829 100644 --- a/external/toxcore/c-toxcore/toxcore/group_connection.c +++ b/external/toxcore/c-toxcore/toxcore/group_connection.c @@ -11,7 +11,6 @@ #include #include -#include #include #include "DHT.h" @@ -22,6 +21,7 @@ #include "group_chats.h" #include "group_common.h" #include "logger.h" +#include "mem.h" #include "mono_time.h" #include "network.h" #include "util.h" @@ -39,9 +39,9 @@ static bool array_entry_is_empty(const GC_Message_Array_Entry *array_entry) /** @brief Clears an array entry. */ non_null() -static void clear_array_entry(GC_Message_Array_Entry *const array_entry) +static void clear_array_entry(const Memory *mem, GC_Message_Array_Entry *const array_entry) { - free(array_entry->data); + mem_delete(mem, array_entry->data); *array_entry = (GC_Message_Array_Entry) { nullptr @@ -54,14 +54,14 @@ static void clear_array_entry(GC_Message_Array_Entry *const array_entry) * to `start_id`. */ non_null() -static void clear_send_queue_id_range(GC_Connection *gconn, uint64_t start_id, uint64_t end_id) +static void clear_send_queue_id_range(const Memory *mem, GC_Connection *gconn, uint64_t start_id, uint64_t end_id) { const uint16_t start_idx = gcc_get_array_index(start_id); const uint16_t end_idx = gcc_get_array_index(end_id); for (uint16_t i = start_idx; i != end_idx; i = (i + 1) % GCC_BUFFER_SIZE) { GC_Message_Array_Entry *entry = &gconn->send_array[i]; - clear_array_entry(entry); + clear_array_entry(mem, entry); } gconn->send_message_id = start_id; @@ -90,8 +90,8 @@ void gcc_set_recv_message_id(GC_Connection *gconn, uint64_t id) * * Return true on success. */ -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, +non_null(1, 2, 3, 4) nullable(5) +static bool create_array_entry(const Logger *log, const Memory *mem, 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 (!array_entry_is_empty(array_entry)) { @@ -109,7 +109,7 @@ static bool create_array_entry(const Logger *log, const Mono_Time *mono_time, GC return false; } - uint8_t *entry_data = (uint8_t *)malloc(length); + uint8_t *entry_data = (uint8_t *)mem_balloc(mem, length); if (entry_data == nullptr) { return false; @@ -134,9 +134,9 @@ static bool create_array_entry(const Logger *log, const Mono_Time *mono_time, GC * * 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, - uint16_t length, uint8_t packet_type) +non_null(1, 2, 3, 4) nullable(5) +static bool add_to_send_array(const Logger *log, const Memory *mem, const Mono_Time *mono_time, GC_Connection *gconn, + const uint8_t *data, uint16_t length, uint8_t packet_type) { /* check if send_array is full */ if ((gconn->send_message_id % GCC_BUFFER_SIZE) == (uint16_t)(gconn->send_array_start - 1)) { @@ -147,7 +147,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 (!create_array_entry(log, mono_time, array_entry, data, length, packet_type, gconn->send_message_id)) { + if (!create_array_entry(log, mem, mono_time, array_entry, data, length, packet_type, gconn->send_message_id)) { return false; } @@ -161,7 +161,7 @@ int gcc_send_lossless_packet(const GC_Chat *chat, GC_Connection *gconn, const ui { const uint64_t message_id = gconn->send_message_id; - if (!add_to_send_array(chat->log, chat->mono_time, gconn, data, length, packet_type)) { + if (!add_to_send_array(chat->log, chat->mem, chat->mono_time, gconn, data, length, packet_type)) { LOGGER_WARNING(chat->log, "Failed to add payload to send array: (type: 0x%02x, length: %d)", packet_type, length); return -1; } @@ -172,7 +172,7 @@ int gcc_send_lossless_packet(const GC_Chat *chat, GC_Connection *gconn, const ui 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); + clear_array_entry(chat->mem, 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; @@ -196,7 +196,7 @@ bool gcc_send_lossless_packet_fragments(const GC_Chat *chat, GC_Connection *gcon chunk[0] = packet_type; memcpy(chunk + 1, data, MAX_GC_PACKET_CHUNK_SIZE - 1); - if (!add_to_send_array(chat->log, chat->mono_time, gconn, chunk, MAX_GC_PACKET_CHUNK_SIZE, GP_FRAGMENT)) { + if (!add_to_send_array(chat->log, chat->mem, chat->mono_time, gconn, chunk, MAX_GC_PACKET_CHUNK_SIZE, GP_FRAGMENT)) { return false; } @@ -209,15 +209,15 @@ bool gcc_send_lossless_packet_fragments(const GC_Chat *chat, GC_Connection *gcon memcpy(chunk, data + processed, chunk_len); processed += chunk_len; - if (!add_to_send_array(chat->log, chat->mono_time, gconn, chunk, chunk_len, GP_FRAGMENT)) { - clear_send_queue_id_range(gconn, start_id, gconn->send_message_id); + if (!add_to_send_array(chat->log, chat->mem, chat->mono_time, gconn, chunk, chunk_len, GP_FRAGMENT)) { + clear_send_queue_id_range(chat->mem, gconn, start_id, gconn->send_message_id); return false; } } // empty packet signals the end of the sequence - if (!add_to_send_array(chat->log, chat->mono_time, gconn, nullptr, 0, GP_FRAGMENT)) { - clear_send_queue_id_range(gconn, start_id, gconn->send_message_id); + if (!add_to_send_array(chat->log, chat->mem, chat->mono_time, gconn, nullptr, 0, GP_FRAGMENT)) { + clear_send_queue_id_range(chat->mem, gconn, start_id, gconn->send_message_id); return false; } @@ -241,7 +241,7 @@ bool gcc_send_lossless_packet_fragments(const GC_Chat *chat, GC_Connection *gcon return true; } -bool gcc_handle_ack(const Logger *log, GC_Connection *gconn, uint64_t message_id) +bool gcc_handle_ack(const Logger *log, const Memory *mem, GC_Connection *gconn, uint64_t message_id) { uint16_t idx = gcc_get_array_index(message_id); GC_Message_Array_Entry *array_entry = &gconn->send_array[idx]; @@ -255,7 +255,7 @@ bool gcc_handle_ack(const Logger *log, GC_Connection *gconn, uint64_t message_id return false; } - clear_array_entry(array_entry); + clear_array_entry(mem, array_entry); /* Put send_array_start in proper position */ if (idx == gconn->send_array_start) { @@ -336,15 +336,15 @@ int gcc_save_tcp_relay(const Random *rng, GC_Connection *gconn, const Node_forma * * Return true on success. */ -non_null(1, 2, 3) nullable(4) -static bool store_in_recv_array(const Logger *log, const Mono_Time *mono_time, GC_Connection *gconn, - const uint8_t *data, +non_null(1, 2, 3, 4) nullable(5) +static bool store_in_recv_array(const Logger *log, const Memory *mem, const Mono_Time *mono_time, + GC_Connection *gconn, const uint8_t *data, uint16_t length, uint8_t packet_type, uint64_t message_id) { const uint16_t idx = gcc_get_array_index(message_id); GC_Message_Array_Entry *ary_entry = &gconn->recv_array[idx]; - return create_array_entry(log, mono_time, ary_entry, data, length, packet_type, message_id); + return create_array_entry(log, mem, mono_time, ary_entry, data, length, packet_type, message_id); } /** @@ -358,8 +358,8 @@ static bool store_in_recv_array(const Logger *log, const Mono_Time *mono_time, G * Return the length of the fully reassembled packet on success. * Return 0 on failure. */ -non_null(1, 3) nullable(2) -static uint16_t reassemble_packet(const Logger *log, GC_Connection *gconn, uint8_t **payload, uint64_t message_id) +non_null(1, 2, 4) nullable(3) +static uint16_t reassemble_packet(const Logger *log, const Memory *mem, GC_Connection *gconn, uint8_t **payload, uint64_t message_id) { uint16_t end_idx = gcc_get_array_index(message_id - 1); uint16_t start_idx = end_idx; @@ -395,7 +395,7 @@ static uint16_t reassemble_packet(const Logger *log, GC_Connection *gconn, uint8 return 0; } - uint8_t *tmp_payload = (uint8_t *)malloc(packet_length); + uint8_t *tmp_payload = (uint8_t *)mem_balloc(mem, packet_length); if (tmp_payload == nullptr) { LOGGER_ERROR(log, "Failed to allocate %u bytes for payload buffer", packet_length); @@ -414,7 +414,7 @@ static uint16_t reassemble_packet(const Logger *log, GC_Connection *gconn, uint8 memcpy(tmp_payload + processed, entry->data, entry->data_length); processed += entry->data_length; - clear_array_entry(entry); + clear_array_entry(mem, entry); } assert(*payload == nullptr); @@ -428,7 +428,7 @@ int gcc_handle_packet_fragment(const GC_Session *c, GC_Chat *chat, uint32_t peer uint64_t message_id, void *userdata) { if (length > 0) { - if (!store_in_recv_array(chat->log, chat->mono_time, gconn, chunk, length, packet_type, message_id)) { + if (!store_in_recv_array(chat->log, chat->mem, chat->mono_time, gconn, chunk, length, packet_type, message_id)) { return -1; } @@ -442,15 +442,15 @@ int gcc_handle_packet_fragment(const GC_Session *c, GC_Chat *chat, uint32_t peer memcpy(sender_pk, get_enc_key(&gconn->addr.public_key), ENC_PUBLIC_KEY_SIZE); uint8_t *payload = nullptr; - const uint16_t processed_len = reassemble_packet(chat->log, gconn, &payload, message_id); + const uint16_t processed_len = reassemble_packet(chat->log, chat->mem, gconn, &payload, message_id); if (processed_len == 0) { - free(payload); + mem_delete(chat->mem, payload); return -1; } if (!handle_gc_lossless_helper(c, chat, peer_number, payload + 1, processed_len - 1, payload[0], userdata)) { - free(payload); + mem_delete(chat->mem, payload); return -1; } @@ -459,19 +459,19 @@ int gcc_handle_packet_fragment(const GC_Session *c, GC_Chat *chat, uint32_t peer gconn = get_gc_connection(chat, peer_number); if (gconn == nullptr) { - free(payload); + mem_delete(chat->mem, payload); return 0; } gcc_set_recv_message_id(gconn, gconn->received_message_id + 1); gconn->last_chunk_id = 0; - free(payload); + mem_delete(chat->mem, payload); return 0; } -int gcc_handle_received_message(const Logger *log, const Mono_Time *mono_time, GC_Connection *gconn, +int gcc_handle_received_message(const Logger *log, const Memory *mem, const Mono_Time *mono_time, GC_Connection *gconn, const uint8_t *data, uint16_t length, uint8_t packet_type, uint64_t message_id, bool direct_conn) { @@ -490,7 +490,7 @@ int gcc_handle_received_message(const Logger *log, const Mono_Time *mono_time, G /* we're missing an older message from this peer so we store it in recv_array */ if (message_id > gconn->received_message_id + 1) { - if (!store_in_recv_array(log, mono_time, gconn, data, length, packet_type, message_id)) { + if (!store_in_recv_array(log, mem, mono_time, gconn, data, length, packet_type, message_id)) { return -1; } @@ -522,7 +522,7 @@ static bool process_recv_array_entry(const GC_Session *c, GC_Chat *chat, GC_Conn peer_number = get_peer_number_of_enc_pk(chat, sender_pk, false); gconn = get_gc_connection(chat, peer_number); - clear_array_entry(array_entry); + clear_array_entry(chat->mem, array_entry); if (gconn == nullptr) { return true; @@ -621,7 +621,7 @@ int gcc_encrypt_and_send_lossless_packet(const GC_Chat *chat, const GC_Connectio 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); - uint8_t *packet = (uint8_t *)malloc(packet_size); + uint8_t *packet = (uint8_t *)mem_balloc(chat->mem, packet_size); if (packet == nullptr) { LOGGER_ERROR(chat->log, "Failed to allocate memory for packet buffer"); @@ -634,17 +634,17 @@ int gcc_encrypt_and_send_lossless_packet(const GC_Chat *chat, const GC_Connectio if (enc_len < 0) { LOGGER_ERROR(chat->log, "Failed to wrap packet (type: 0x%02x, error: %d)", packet_type, enc_len); - free(packet); + mem_delete(chat->mem, packet); 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); + mem_delete(chat->mem, packet); return -2; } - free(packet); + mem_delete(chat->mem, packet); return 0; } @@ -686,15 +686,15 @@ void gcc_mark_for_deletion(GC_Connection *gconn, TCP_Connections *tcp_conn, Grou } } -void gcc_peer_cleanup(GC_Connection *gconn) +void gcc_peer_cleanup(const Memory *mem, GC_Connection *gconn) { for (size_t i = 0; i < GCC_BUFFER_SIZE; ++i) { - free(gconn->send_array[i].data); - free(gconn->recv_array[i].data); + mem_delete(mem, gconn->send_array[i].data); + mem_delete(mem, gconn->recv_array[i].data); } - free(gconn->recv_array); - free(gconn->send_array); + mem_delete(mem, gconn->recv_array); + mem_delete(mem, gconn->send_array); crypto_memunlock(gconn->session_secret_key, sizeof(gconn->session_secret_key)); crypto_memunlock(gconn->session_shared_key, sizeof(gconn->session_shared_key)); @@ -707,6 +707,6 @@ void gcc_cleanup(const GC_Chat *chat) GC_Connection *gconn = get_gc_connection(chat, i); assert(gconn != nullptr); - gcc_peer_cleanup(gconn); + gcc_peer_cleanup(chat->mem, gconn); } } diff --git a/external/toxcore/c-toxcore/toxcore/group_connection.h b/external/toxcore/c-toxcore/toxcore/group_connection.h index 4a9ccad..e165ac5 100644 --- a/external/toxcore/c-toxcore/toxcore/group_connection.h +++ b/external/toxcore/c-toxcore/toxcore/group_connection.h @@ -16,6 +16,7 @@ #include "crypto_core.h" #include "group_common.h" #include "logger.h" +#include "mem.h" #include "mono_time.h" #include "network.h" @@ -35,8 +36,8 @@ void gcc_mark_for_deletion(GC_Connection *gconn, TCP_Connections *tcp_conn, Grou * Return 0 if message is a duplicate. * Return -1 on failure */ -non_null(1, 2, 3) nullable(4) -int gcc_handle_received_message(const Logger *log, const Mono_Time *mono_time, GC_Connection *gconn, +non_null(1, 2, 3, 4) nullable(5) +int gcc_handle_received_message(const Logger *log, const Memory *mem, const Mono_Time *mono_time, GC_Connection *gconn, const uint8_t *data, uint16_t length, uint8_t packet_type, uint64_t message_id, bool direct_conn); @@ -63,7 +64,7 @@ uint16_t gcc_get_array_index(uint64_t message_id); * Return true on success. */ non_null() -bool gcc_handle_ack(const Logger *log, GC_Connection *gconn, uint64_t message_id); +bool gcc_handle_ack(const Logger *log, const Memory *mem, GC_Connection *gconn, uint64_t message_id); /** @brief Sets the send_message_id and send_array_start for `gconn` to `id`. * @@ -188,7 +189,7 @@ int gcc_encrypt_and_send_lossless_packet(const GC_Chat *chat, const GC_Connectio /** @brief Called when a peer leaves the group. */ non_null() -void gcc_peer_cleanup(GC_Connection *gconn); +void gcc_peer_cleanup(const Memory *mem, GC_Connection *gconn); /** @brief Called on group exit. */ non_null() diff --git a/external/toxcore/c-toxcore/toxcore/group_moderation.c b/external/toxcore/c-toxcore/toxcore/group_moderation.c index 3cb69e9..acc17bd 100644 --- a/external/toxcore/c-toxcore/toxcore/group_moderation.c +++ b/external/toxcore/c-toxcore/toxcore/group_moderation.c @@ -11,7 +11,6 @@ #include -#include #include #include @@ -20,6 +19,7 @@ #include "ccompat.h" #include "crypto_core.h" #include "logger.h" +#include "mem.h" #include "network.h" #include "util.h" @@ -51,7 +51,7 @@ int mod_list_unpack(Moderation *moderation, const uint8_t *data, uint16_t length return 0; } - uint8_t **tmp_list = (uint8_t **)calloc(num_mods, sizeof(uint8_t *)); + uint8_t **tmp_list = (uint8_t **)mem_valloc(moderation->mem, num_mods, sizeof(uint8_t *)); if (tmp_list == nullptr) { return -1; @@ -60,7 +60,7 @@ int mod_list_unpack(Moderation *moderation, const uint8_t *data, uint16_t length uint16_t unpacked_len = 0; for (uint16_t i = 0; i < num_mods; ++i) { - uint8_t *entry = (uint8_t *)malloc(MOD_LIST_ENTRY_SIZE); + uint8_t *entry = (uint8_t *)mem_balloc(moderation->mem, MOD_LIST_ENTRY_SIZE); if (entry == nullptr) { free_uint8_t_pointer_array(moderation->mem, tmp_list, i); @@ -102,7 +102,7 @@ bool mod_list_make_hash(const Moderation *moderation, uint8_t *hash) assert(data_buf_size > 0); - uint8_t *data = (uint8_t *)malloc(data_buf_size); + uint8_t *data = (uint8_t *)mem_balloc(moderation->mem, data_buf_size); if (data == nullptr) { return false; @@ -112,7 +112,7 @@ bool mod_list_make_hash(const Moderation *moderation, uint8_t *hash) mod_list_get_data_hash(hash, data, data_buf_size); - free(data); + mem_delete(moderation->mem, data); return true; } @@ -166,10 +166,10 @@ bool mod_list_remove_index(Moderation *moderation, uint16_t index) MOD_LIST_ENTRY_SIZE); } - free(moderation->mod_list[moderation->num_mods]); + mem_delete(moderation->mem, moderation->mod_list[moderation->num_mods]); moderation->mod_list[moderation->num_mods] = nullptr; - uint8_t **tmp_list = (uint8_t **)realloc(moderation->mod_list, moderation->num_mods * sizeof(uint8_t *)); + uint8_t **tmp_list = (uint8_t **)mem_vrealloc(moderation->mem, moderation->mod_list, moderation->num_mods, sizeof(uint8_t *)); if (tmp_list == nullptr) { return false; @@ -203,7 +203,7 @@ bool mod_list_add_entry(Moderation *moderation, const uint8_t *mod_data) return false; } - uint8_t **tmp_list = (uint8_t **)realloc(moderation->mod_list, (moderation->num_mods + 1) * sizeof(uint8_t *)); + uint8_t **tmp_list = (uint8_t **)mem_vrealloc(moderation->mem, moderation->mod_list, moderation->num_mods + 1, sizeof(uint8_t *)); if (tmp_list == nullptr) { return false; @@ -211,7 +211,7 @@ bool mod_list_add_entry(Moderation *moderation, const uint8_t *mod_data) moderation->mod_list = tmp_list; - uint8_t *entry = (uint8_t *)malloc(MOD_LIST_ENTRY_SIZE); + uint8_t *entry = (uint8_t *)mem_balloc(moderation->mem, MOD_LIST_ENTRY_SIZE); if (entry == nullptr) { return false; @@ -405,8 +405,8 @@ int sanctions_list_unpack(Mod_Sanction *sanctions, Mod_Sanction_Creds *creds, ui * * Return true on success. */ -non_null(4) nullable(1) -static bool sanctions_list_make_hash(const Mod_Sanction *sanctions, uint32_t new_version, uint16_t num_sanctions, +non_null(1, 5) nullable(2) +static bool sanctions_list_make_hash(const Memory *mem, const Mod_Sanction *sanctions, uint32_t new_version, uint16_t num_sanctions, uint8_t *hash) { if (num_sanctions == 0 || sanctions == nullptr) { @@ -422,7 +422,7 @@ static bool sanctions_list_make_hash(const Mod_Sanction *sanctions, uint32_t new return false; } - uint8_t *data = (uint8_t *)malloc(data_buf_size); + uint8_t *data = (uint8_t *)mem_balloc(mem, data_buf_size); if (data == nullptr) { return false; @@ -435,7 +435,7 @@ static bool sanctions_list_make_hash(const Mod_Sanction *sanctions, uint32_t new memcpy(&data[sig_data_size], &new_version, sizeof(uint32_t)); crypto_sha256(hash, data, data_buf_size); - free(data); + mem_delete(mem, data); return true; } @@ -492,7 +492,7 @@ bool sanctions_list_make_creds(Moderation *moderation) uint8_t hash[MOD_SANCTION_HASH_SIZE]; - if (!sanctions_list_make_hash(moderation->sanctions, moderation->sanctions_creds.version, + if (!sanctions_list_make_hash(moderation->mem, moderation->sanctions, moderation->sanctions_creds.version, moderation->num_sanctions, hash)) { moderation->sanctions_creds = old_creds; return false; @@ -533,7 +533,7 @@ static bool sanctions_creds_validate(const Moderation *moderation, const Mod_San uint8_t hash[MOD_SANCTION_HASH_SIZE]; - if (!sanctions_list_make_hash(sanctions, creds->version, num_sanctions, hash)) { + if (!sanctions_list_make_hash(moderation->mem, sanctions, creds->version, num_sanctions, hash)) { return false; } @@ -612,9 +612,9 @@ static bool sanctions_apply_new(Moderation *moderation, Mod_Sanction *new_sancti * memory returned by this function. */ non_null() -static Mod_Sanction *sanctions_list_copy(const Mod_Sanction *sanctions, uint16_t num_sanctions) +static Mod_Sanction *sanctions_list_copy(const Memory *mem, const Mod_Sanction *sanctions, uint16_t num_sanctions) { - Mod_Sanction *copy = (Mod_Sanction *)calloc(num_sanctions, sizeof(Mod_Sanction)); + Mod_Sanction *copy = (Mod_Sanction *)mem_valloc(mem, num_sanctions, sizeof(Mod_Sanction)); if (copy == nullptr) { return nullptr; @@ -655,7 +655,7 @@ static bool sanctions_list_remove_index(Moderation *moderation, uint16_t index, } /* Operate on a copy of the list in case something goes wrong. */ - Mod_Sanction *sanctions_copy = sanctions_list_copy(moderation->sanctions, moderation->num_sanctions); + Mod_Sanction *sanctions_copy = sanctions_list_copy(moderation->mem, moderation->sanctions, moderation->num_sanctions); if (sanctions_copy == nullptr) { return false; @@ -665,15 +665,15 @@ static bool sanctions_list_remove_index(Moderation *moderation, uint16_t index, sanctions_copy[index] = sanctions_copy[new_num]; } - Mod_Sanction *new_list = (Mod_Sanction *)realloc(sanctions_copy, new_num * sizeof(Mod_Sanction)); + Mod_Sanction *new_list = (Mod_Sanction *)mem_vrealloc(moderation->mem, sanctions_copy, new_num, sizeof(Mod_Sanction)); if (new_list == nullptr) { - free(sanctions_copy); + mem_delete(moderation->mem, sanctions_copy); return false; } if (!sanctions_apply_new(moderation, new_list, creds, new_num)) { - free(new_list); + mem_delete(moderation->mem, new_list); return false; } @@ -753,7 +753,7 @@ bool sanctions_list_add_entry(Moderation *moderation, const Mod_Sanction *sancti Mod_Sanction *sanctions_copy = nullptr; if (moderation->num_sanctions > 0) { - sanctions_copy = sanctions_list_copy(moderation->sanctions, moderation->num_sanctions); + sanctions_copy = sanctions_list_copy(moderation->mem, moderation->sanctions, moderation->num_sanctions); if (sanctions_copy == nullptr) { return false; @@ -761,17 +761,17 @@ bool sanctions_list_add_entry(Moderation *moderation, const Mod_Sanction *sancti } const uint16_t index = moderation->num_sanctions; - Mod_Sanction *new_list = (Mod_Sanction *)realloc(sanctions_copy, (index + 1) * sizeof(Mod_Sanction)); + Mod_Sanction *new_list = (Mod_Sanction *)mem_vrealloc(moderation->mem, sanctions_copy, index + 1, sizeof(Mod_Sanction)); if (new_list == nullptr) { - free(sanctions_copy); + mem_delete(moderation->mem, sanctions_copy); return false; } new_list[index] = *sanction; if (!sanctions_apply_new(moderation, new_list, creds, index + 1)) { - free(new_list); + mem_delete(moderation->mem, new_list); return false; } @@ -864,7 +864,7 @@ uint16_t sanctions_list_replace_sig(Moderation *moderation, const uint8_t *publi void sanctions_list_cleanup(Moderation *moderation) { - free(moderation->sanctions); + mem_delete(moderation->mem, moderation->sanctions); moderation->sanctions = nullptr; moderation->num_sanctions = 0; diff --git a/external/toxcore/c-toxcore/toxcore/group_onion_announce.c b/external/toxcore/c-toxcore/toxcore/group_onion_announce.c index 6d68013..5a19935 100644 --- a/external/toxcore/c-toxcore/toxcore/group_onion_announce.c +++ b/external/toxcore/c-toxcore/toxcore/group_onion_announce.c @@ -20,12 +20,16 @@ #include "onion_announce.h" #include "timed_auth.h" +static_assert(TIMED_AUTH_SIZE <= ONION_ANNOUNCE_REQUEST_MAX_SIZE, + "Timed auth does not fit into the onion packet"); +static_assert(PACKED_NODE_SIZE_IP6 <= GCA_ANNOUNCE_MAX_SIZE, + "IP6 does not fit into the GC_Announce"); static_assert(GCA_ANNOUNCE_MAX_SIZE <= ONION_MAX_EXTRA_DATA_SIZE, "GC_Announce does not fit into the onion packet extra data"); static pack_extra_data_cb pack_group_announces; non_null() -static int pack_group_announces(void *object, const Logger *logger, const Mono_Time *mono_time, +static int pack_group_announces(void *object, const Logger *logger, const Memory *mem, const Mono_Time *mono_time, uint8_t num_nodes, uint8_t *plain, uint16_t plain_size, uint8_t *response, uint16_t response_size, uint16_t offset) { @@ -38,7 +42,7 @@ static int pack_group_announces(void *object, const Logger *logger, const Mono_T return -1; } - const GC_Peer_Announce *new_announce = gca_add_announce(mono_time, gc_announces_list, &public_announce); + const GC_Peer_Announce *new_announce = gca_add_announce(mem, mono_time, gc_announces_list, &public_announce); if (new_announce == nullptr) { LOGGER_ERROR(logger, "Failed to add group announce"); diff --git a/external/toxcore/c-toxcore/toxcore/group_onion_announce.h b/external/toxcore/c-toxcore/toxcore/group_onion_announce.h index bc9edb1..8abff63 100644 --- a/external/toxcore/c-toxcore/toxcore/group_onion_announce.h +++ b/external/toxcore/c-toxcore/toxcore/group_onion_announce.h @@ -17,9 +17,9 @@ void gca_onion_init(GC_Announces_List *group_announce, Onion_Announce *onion_a); non_null() int create_gca_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, - const uint8_t *gc_data, uint16_t gc_data_length); + 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, const uint8_t *gc_data, uint16_t gc_data_length); #endif /* C_TOXCORE_TOXCORE_GROUP_ONION_ANNOUNCE_H */ diff --git a/external/toxcore/c-toxcore/toxcore/group_pack.c b/external/toxcore/c-toxcore/toxcore/group_pack.c index c20d6a1..877df74 100644 --- a/external/toxcore/c-toxcore/toxcore/group_pack.c +++ b/external/toxcore/c-toxcore/toxcore/group_pack.c @@ -10,7 +10,6 @@ #include "group_pack.h" #include -#include #include #include "DHT.h" @@ -23,9 +22,13 @@ #include "group_common.h" #include "group_moderation.h" #include "logger.h" +#include "mem.h" #include "network.h" #include "util.h" +static_assert(GC_SAVED_PEER_SIZE >= sizeof(IP_Port), + "GC_SAVED_PEER_SIZE cannot contain IP_Port"); + bool group_privacy_state_from_int(uint8_t value, Group_Privacy_State *out_enum) { switch (value) { @@ -180,7 +183,7 @@ static bool load_unpack_mod_list(GC_Chat *chat, Bin_Unpack *bu) chat->moderation.num_mods = MOD_MAX_NUM_MODERATORS; } - uint8_t *packed_mod_list = (uint8_t *)malloc(chat->moderation.num_mods * MOD_LIST_ENTRY_SIZE); + uint8_t *packed_mod_list = (uint8_t *)mem_balloc(chat->mem, chat->moderation.num_mods * MOD_LIST_ENTRY_SIZE); if (packed_mod_list == nullptr) { LOGGER_ERROR(chat->log, "Failed to allocate memory for packed mod list"); @@ -191,17 +194,17 @@ static bool load_unpack_mod_list(GC_Chat *chat, Bin_Unpack *bu) if (!bin_unpack_bin_fixed(bu, packed_mod_list, packed_size)) { LOGGER_ERROR(chat->log, "Failed to unpack mod list binary data"); - free(packed_mod_list); + mem_delete(chat->mem, packed_mod_list); return false; } if (mod_list_unpack(&chat->moderation, packed_mod_list, packed_size, chat->moderation.num_mods) == -1) { LOGGER_ERROR(chat->log, "Failed to unpack mod list info"); - free(packed_mod_list); + mem_delete(chat->mem, packed_mod_list); return false; } - free(packed_mod_list); + mem_delete(chat->mem, packed_mod_list); return true; } @@ -299,7 +302,7 @@ static bool load_unpack_saved_peers(GC_Chat *chat, Bin_Unpack *bu) return true; } - uint8_t *saved_peers = (uint8_t *)malloc(saved_peers_size * GC_SAVED_PEER_SIZE); + uint8_t *saved_peers = (uint8_t *)mem_balloc(chat->mem, saved_peers_size * GC_SAVED_PEER_SIZE); if (saved_peers == nullptr) { LOGGER_ERROR(chat->log, "Failed to allocate memory for saved peer list"); @@ -308,7 +311,7 @@ static bool load_unpack_saved_peers(GC_Chat *chat, Bin_Unpack *bu) if (!bin_unpack_bin_fixed(bu, saved_peers, saved_peers_size)) { LOGGER_ERROR(chat->log, "Failed to unpack saved peers binary data"); - free(saved_peers); + mem_delete(chat->mem, saved_peers); return false; } @@ -316,7 +319,7 @@ static bool load_unpack_saved_peers(GC_Chat *chat, Bin_Unpack *bu) LOGGER_ERROR(chat->log, "Failed to unpack saved peers"); // recoverable error } - free(saved_peers); + mem_delete(chat->mem, saved_peers); return true; } @@ -390,7 +393,7 @@ static void save_pack_mod_list(const GC_Chat *chat, Bin_Pack *bp) return; } - uint8_t *packed_mod_list = (uint8_t *)malloc(num_mods * MOD_LIST_ENTRY_SIZE); + uint8_t *packed_mod_list = (uint8_t *)mem_balloc(chat->mem, num_mods * MOD_LIST_ENTRY_SIZE); // we can still recover without the mod list if (packed_mod_list == nullptr) { @@ -408,7 +411,7 @@ static void save_pack_mod_list(const GC_Chat *chat, Bin_Pack *bp) bin_pack_bin(bp, packed_mod_list, packed_size); // 2 - free(packed_mod_list); + mem_delete(chat->mem, packed_mod_list); } non_null() @@ -445,7 +448,7 @@ static void save_pack_saved_peers(const GC_Chat *chat, Bin_Pack *bp) { bin_pack_array(bp, 2); - uint8_t *saved_peers = (uint8_t *)malloc(GC_MAX_SAVED_PEERS * GC_SAVED_PEER_SIZE); + uint8_t *saved_peers = (uint8_t *)mem_balloc(chat->mem, GC_MAX_SAVED_PEERS * GC_SAVED_PEER_SIZE); // we can still recover without the saved peers list if (saved_peers == nullptr) { @@ -466,13 +469,13 @@ static void save_pack_saved_peers(const GC_Chat *chat, Bin_Pack *bp) if (packed_size == 0) { bin_pack_nil(bp); // 2 - free(saved_peers); + mem_delete(chat->mem, saved_peers); return; } bin_pack_bin(bp, saved_peers, packed_size); // 2 - free(saved_peers); + mem_delete(chat->mem, saved_peers); } void gc_save_pack_group(const GC_Chat *chat, Bin_Pack *bp) diff --git a/external/toxcore/c-toxcore/toxcore/list.c b/external/toxcore/c-toxcore/toxcore/list.c index bf3db24..689ad0a 100644 --- a/external/toxcore/c-toxcore/toxcore/list.c +++ b/external/toxcore/c-toxcore/toxcore/list.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2014 Tox project. */ @@ -12,11 +12,11 @@ #include #include -#include #include #include "attributes.h" #include "ccompat.h" +#include "mem.h" /** * Basically, the elements in the list are placed in order so that they can be searched for easily @@ -115,7 +115,7 @@ static bool resize(BS_List *list, uint32_t new_size) return true; } - uint8_t *data = (uint8_t *)realloc(list->data, list->element_size * new_size); + uint8_t *data = (uint8_t *)mem_brealloc(list->mem, list->data, new_size * list->element_size); if (data == nullptr) { return false; @@ -123,7 +123,7 @@ static bool resize(BS_List *list, uint32_t new_size) list->data = data; - int *ids = (int *)realloc(list->ids, new_size * sizeof(int)); + int *ids = (int *)mem_vrealloc(list->mem, list->ids, new_size, sizeof(int)); if (ids == nullptr) { return false; @@ -134,8 +134,10 @@ static bool resize(BS_List *list, uint32_t new_size) return true; } -int bs_list_init(BS_List *list, uint32_t element_size, uint32_t initial_capacity, bs_list_cmp_cb *cmp_callback) +int bs_list_init(BS_List *list, const Memory *mem, uint32_t element_size, uint32_t initial_capacity, bs_list_cmp_cb *cmp_callback) { + list->mem = mem; + // set initial values list->n = 0; list->element_size = element_size; @@ -162,10 +164,10 @@ void bs_list_free(BS_List *list) } // free both arrays - free(list->data); + mem_delete(list->mem, list->data); list->data = nullptr; - free(list->ids); + mem_delete(list->mem, list->ids); list->ids = nullptr; } diff --git a/external/toxcore/c-toxcore/toxcore/list.h b/external/toxcore/c-toxcore/toxcore/list.h index 1dc66d0..ae0ea32 100644 --- a/external/toxcore/c-toxcore/toxcore/list.h +++ b/external/toxcore/c-toxcore/toxcore/list.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2014 Tox project. */ @@ -16,6 +16,7 @@ #include #include "attributes.h" +#include "mem.h" #ifdef __cplusplus extern "C" { @@ -24,6 +25,8 @@ extern "C" { typedef int bs_list_cmp_cb(const void *a, const void *b, size_t size); typedef struct BS_List { + const Memory *mem; + uint32_t n; // number of elements uint32_t capacity; // number of elements memory is allocated for uint32_t element_size; // size of the elements @@ -41,7 +44,7 @@ typedef struct BS_List { * @retval 0 failure */ non_null() -int bs_list_init(BS_List *list, uint32_t element_size, uint32_t initial_capacity, bs_list_cmp_cb *cmp_callback); +int bs_list_init(BS_List *list, const Memory *mem, uint32_t element_size, uint32_t initial_capacity, bs_list_cmp_cb *cmp_callback); /** Free a list initiated with list_init */ nullable(1) diff --git a/external/toxcore/c-toxcore/toxcore/list_test.cc b/external/toxcore/c-toxcore/toxcore/list_test.cc index 0d8f646..794c5e2 100644 --- a/external/toxcore/c-toxcore/toxcore/list_test.cc +++ b/external/toxcore/c-toxcore/toxcore/list_test.cc @@ -2,26 +2,31 @@ #include +#include "mem.h" + namespace { TEST(List, CreateAndDestroyWithNonZeroSize) { + const Memory *mem = os_memory(); BS_List list; - bs_list_init(&list, sizeof(int), 10, memcmp); + bs_list_init(&list, mem, sizeof(int), 10, memcmp); bs_list_free(&list); } TEST(List, CreateAndDestroyWithZeroSize) { + const Memory *mem = os_memory(); BS_List list; - bs_list_init(&list, sizeof(int), 0, memcmp); + bs_list_init(&list, mem, sizeof(int), 0, memcmp); bs_list_free(&list); } TEST(List, DeleteFromEmptyList) { + const Memory *mem = os_memory(); BS_List list; - bs_list_init(&list, sizeof(int), 0, memcmp); + bs_list_init(&list, mem, sizeof(int), 0, memcmp); const uint8_t data[sizeof(int)] = {0}; bs_list_remove(&list, data, 0); bs_list_free(&list); diff --git a/external/toxcore/c-toxcore/toxcore/logger.c b/external/toxcore/c-toxcore/toxcore/logger.c index 69d7f50..b97ef8e 100644 --- a/external/toxcore/c-toxcore/toxcore/logger.c +++ b/external/toxcore/c-toxcore/toxcore/logger.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013-2015 Tox project. */ @@ -8,6 +8,7 @@ */ #include "logger.h" +#include #include #include #include @@ -52,12 +53,13 @@ void logger_kill(Logger *log) void logger_callback_log(Logger *log, logger_cb *function, void *context, void *userdata) { + assert(log != nullptr); log->callback = function; log->context = context; log->userdata = userdata; } -void logger_write(const Logger *log, Logger_Level level, const char *file, int line, const char *func, +void logger_write(const Logger *log, Logger_Level level, const char *file, uint32_t line, const char *func, const char *format, ...) { if (log == nullptr) { diff --git a/external/toxcore/c-toxcore/toxcore/logger.h b/external/toxcore/c-toxcore/toxcore/logger.h index 1c7c007..893c21f 100644 --- a/external/toxcore/c-toxcore/toxcore/logger.h +++ b/external/toxcore/c-toxcore/toxcore/logger.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ @@ -33,7 +33,7 @@ typedef enum Logger_Level { typedef struct Logger Logger; -typedef void logger_cb(void *context, Logger_Level level, const char *file, int line, +typedef void logger_cb(void *context, Logger_Level level, const char *file, uint32_t line, const char *func, const char *message, void *userdata); /** @@ -66,7 +66,7 @@ void logger_callback_log(Logger *log, logger_cb *function, void *context, void * */ non_null(3, 5, 6) nullable(1) GNU_PRINTF(6, 7) void logger_write( - const Logger *log, Logger_Level level, const char *file, int line, const char *func, + const Logger *log, Logger_Level level, const char *file, uint32_t line, const char *func, const char *format, ...); /* @brief Terminate the program with a signal. */ diff --git a/external/toxcore/c-toxcore/toxcore/mem.c b/external/toxcore/c-toxcore/toxcore/mem.c index bddc335..32e7eec 100644 --- a/external/toxcore/c-toxcore/toxcore/mem.c +++ b/external/toxcore/c-toxcore/toxcore/mem.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ @@ -53,6 +53,12 @@ void *mem_balloc(const Memory *mem, uint32_t size) return ptr; } +void *mem_brealloc(const Memory *mem, void *ptr, uint32_t size) +{ + void *const new_ptr = mem->funcs->realloc(mem->obj, ptr, size); + return new_ptr; +} + void *mem_alloc(const Memory *mem, uint32_t size) { void *const ptr = mem->funcs->calloc(mem->obj, 1, size); diff --git a/external/toxcore/c-toxcore/toxcore/mem.h b/external/toxcore/c-toxcore/toxcore/mem.h index 7a96b6d..6c36027 100644 --- a/external/toxcore/c-toxcore/toxcore/mem.h +++ b/external/toxcore/c-toxcore/toxcore/mem.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ @@ -45,6 +45,14 @@ const Memory *os_memory(void); */ non_null() void *mem_balloc(const Memory *mem, uint32_t size); +/** + * @brief Resize an array of a given size for built-in types. + * + * If used for a type other than byte-sized types, `size` needs to be manually + * multiplied by the element size. + */ +non_null(1) nullable(2) void *mem_brealloc(const Memory *mem, void *ptr, uint32_t size); + /** * @brief Allocate a single object. * diff --git a/external/toxcore/c-toxcore/toxcore/mono_time.c b/external/toxcore/c-toxcore/toxcore/mono_time.c index 124f94f..8a3044c 100644 --- a/external/toxcore/c-toxcore/toxcore/mono_time.c +++ b/external/toxcore/c-toxcore/toxcore/mono_time.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2023 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2014 Tox project. */ #ifndef _XOPEN_SOURCE @@ -218,7 +218,7 @@ void mono_time_set_current_time_callback(Mono_Time *mono_time, * The starting point is unspecified and in particular is likely not comparable * to the return value of `mono_time_get_ms()`. */ -uint64_t current_time_monotonic(Mono_Time *mono_time) +uint64_t current_time_monotonic(const Mono_Time *mono_time) { return mono_time->current_time_callback(mono_time->user_data); } diff --git a/external/toxcore/c-toxcore/toxcore/mono_time.h b/external/toxcore/c-toxcore/toxcore/mono_time.h index d0f2b7a..e23c1ba 100644 --- a/external/toxcore/c-toxcore/toxcore/mono_time.h +++ b/external/toxcore/c-toxcore/toxcore/mono_time.h @@ -87,7 +87,7 @@ bool mono_time_is_timeout(const Mono_Time *mono_time, uint64_t timestamp, uint64 * to the return value of `mono_time_get_ms()`. */ non_null() -uint64_t current_time_monotonic(Mono_Time *mono_time); +uint64_t current_time_monotonic(const Mono_Time *mono_time); /** * Override implementation of `current_time_monotonic()` (for tests). diff --git a/external/toxcore/c-toxcore/toxcore/net_crypto.c b/external/toxcore/c-toxcore/toxcore/net_crypto.c index c17ff6c..b960241 100644 --- a/external/toxcore/c-toxcore/toxcore/net_crypto.c +++ b/external/toxcore/c-toxcore/toxcore/net_crypto.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ @@ -10,6 +10,7 @@ */ #include "net_crypto.h" +#include #include #include "DHT.h" @@ -23,6 +24,7 @@ #include "logger.h" #include "mem.h" #include "mono_time.h" +#include "net_profile.h" #include "network.h" #include "util.h" @@ -673,7 +675,7 @@ static IP_Port return_ip_port_connection(const Net_Crypto *c, int crypt_connecti * @retval 0 on success. */ non_null() -static int send_packet_to(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length) +static int send_packet_to(const Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length) { // TODO(irungentoo): TCP, etc... Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); @@ -981,7 +983,7 @@ static int generate_request_packet(uint8_t *data, uint16_t length, const Packets * @return number of requested packets on success. */ non_null() -static int handle_request_packet(const Memory *mem, Mono_Time *mono_time, Packets_Array *send_array, +static int handle_request_packet(const Memory *mem, const Mono_Time *mono_time, Packets_Array *send_array, const uint8_t *data, uint16_t length, uint64_t *latest_send_time, uint64_t rtt_time) { @@ -1064,7 +1066,7 @@ static int handle_request_packet(const Memory *mem, Mono_Time *mono_time, Packet * @retval 0 on success. */ non_null() -static int send_data_packet(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length) +static int send_data_packet(const Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length) { const uint16_t max_length = MAX_CRYPTO_PACKET_SIZE - (1 + sizeof(uint16_t) + CRYPTO_MAC_SIZE); @@ -1102,7 +1104,7 @@ static int send_data_packet(Net_Crypto *c, int crypt_connection_id, const uint8_ * @retval 0 on success. */ non_null() -static int send_data_packet_helper(Net_Crypto *c, int crypt_connection_id, uint32_t buffer_start, uint32_t num, +static int send_data_packet_helper(const Net_Crypto *c, int crypt_connection_id, uint32_t buffer_start, uint32_t num, const uint8_t *data, uint16_t length) { if (length == 0 || length > MAX_CRYPTO_DATA_SIZE) { @@ -1124,7 +1126,7 @@ static int send_data_packet_helper(Net_Crypto *c, int crypt_connection_id, uint3 } non_null() -static int reset_max_speed_reached(Net_Crypto *c, int crypt_connection_id) +static int reset_max_speed_reached(const Net_Crypto *c, int crypt_connection_id) { Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); @@ -1159,7 +1161,7 @@ static int reset_max_speed_reached(Net_Crypto *c, int crypt_connection_id) * @return positive packet number if data was put into the queue. */ non_null() -static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length, +static int64_t send_lossless_packet(const Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length, bool congestion_control) { if (length == 0 || length > MAX_CRYPTO_DATA_SIZE) { @@ -1275,7 +1277,7 @@ static int handle_data_packet(const Net_Crypto *c, int crypt_connection_id, uint * @retval 0 on success. */ non_null() -static int send_request_packet(Net_Crypto *c, int crypt_connection_id) +static int send_request_packet(const Net_Crypto *c, int crypt_connection_id) { const Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); @@ -1300,7 +1302,7 @@ static int send_request_packet(Net_Crypto *c, int crypt_connection_id) * @return number of packets sent on success. */ non_null() -static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint32_t max_num) +static int send_requested_packets(const Net_Crypto *c, int crypt_connection_id, uint32_t max_num) { if (max_num == 0) { return -1; @@ -1414,7 +1416,7 @@ static int clear_temp_packet(const Net_Crypto *c, int crypt_connection_id) * @retval 0 on success. */ non_null() -static int send_temp_packet(Net_Crypto *c, int crypt_connection_id) +static int send_temp_packet(const Net_Crypto *c, int crypt_connection_id) { Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); @@ -1442,7 +1444,7 @@ static int send_temp_packet(Net_Crypto *c, int crypt_connection_id) * @retval 0 on success. */ non_null() -static int create_send_handshake(Net_Crypto *c, int crypt_connection_id, const uint8_t *cookie, +static int create_send_handshake(const Net_Crypto *c, int crypt_connection_id, const uint8_t *cookie, const uint8_t *dht_public_key) { const Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); @@ -1472,7 +1474,7 @@ static int create_send_handshake(Net_Crypto *c, int crypt_connection_id, const u * @retval 0 on success. */ non_null() -static int send_kill_packet(Net_Crypto *c, int crypt_connection_id) +static int send_kill_packet(const Net_Crypto *c, int crypt_connection_id) { const Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); @@ -1647,7 +1649,7 @@ static int handle_data_packet_core(Net_Crypto *c, int crypt_connection_id, const } non_null() -static int handle_packet_cookie_response(Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length) +static int handle_packet_cookie_response(const Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length) { Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); @@ -1679,7 +1681,7 @@ static int handle_packet_cookie_response(Net_Crypto *c, int crypt_connection_id, } non_null(1, 3) nullable(5) -static int handle_packet_crypto_hs(Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length, +static int handle_packet_crypto_hs(const Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length, void *userdata) { Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); @@ -2757,7 +2759,7 @@ static void send_crypto_packets(Net_Crypto *c) * @retval 1 if max speed was reached for this connection (no more data can be physically through the pipe). * @retval 0 if it wasn't reached. */ -bool max_speed_reached(Net_Crypto *c, int crypt_connection_id) +bool max_speed_reached(const Net_Crypto *c, int crypt_connection_id) { return reset_max_speed_reached(c, crypt_connection_id) != 0; } @@ -2792,7 +2794,7 @@ uint32_t crypto_num_free_sendqueue_slots(const Net_Crypto *c, int crypt_connecti * * congestion_control: should congestion control apply to this packet? */ -int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length, +int64_t write_cryptpacket(const Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length, bool congestion_control) { if (length == 0) { @@ -2878,7 +2880,7 @@ int cryptpacket_received(const Net_Crypto *c, int crypt_connection_id, uint32_t * * The first byte of data must be in the PACKET_ID_RANGE_LOSSY. */ -int send_lossy_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length) +int send_lossy_cryptpacket(const Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length) { if (length == 0 || length > MAX_CRYPTO_DATA_SIZE) { return -1; @@ -3026,7 +3028,7 @@ Net_Crypto *new_net_crypto(const Logger *log, const Memory *mem, const Random *r networking_registerhandler(dht_get_net(dht), NET_PACKET_CRYPTO_HS, &udp_handle_packet, temp); networking_registerhandler(dht_get_net(dht), NET_PACKET_CRYPTO_DATA, &udp_handle_packet, temp); - bs_list_init(&temp->ip_port_list, sizeof(IP_Port), 8, ipport_cmp_handler); + bs_list_init(&temp->ip_port_list, mem, sizeof(IP_Port), 8, ipport_cmp_handler); return temp; } @@ -3096,3 +3098,18 @@ void kill_net_crypto(Net_Crypto *c) crypto_memzero(c, sizeof(Net_Crypto)); mem_delete(mem, c); } + +const Net_Profile *nc_get_tcp_client_net_profile(const Net_Crypto *c) +{ + if (c == nullptr) { + return nullptr; + } + + const TCP_Connections *tcp_c = nc_get_tcp_c(c); + + if (tcp_c == nullptr) { + return nullptr; + } + + return tcp_connection_get_client_net_profile(tcp_c); +} diff --git a/external/toxcore/c-toxcore/toxcore/net_crypto.h b/external/toxcore/c-toxcore/toxcore/net_crypto.h index 8368d46..c21ac7d 100644 --- a/external/toxcore/c-toxcore/toxcore/net_crypto.h +++ b/external/toxcore/c-toxcore/toxcore/net_crypto.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ @@ -9,10 +9,7 @@ #ifndef C_TOXCORE_TOXCORE_NET_CRYPTO_H #define C_TOXCORE_TOXCORE_NET_CRYPTO_H -#include - #include "DHT.h" -#include "LAN_discovery.h" #include "TCP_client.h" #include "TCP_connection.h" #include "attributes.h" @@ -20,6 +17,7 @@ #include "logger.h" #include "mem.h" #include "mono_time.h" +#include "net_profile.h" #include "network.h" /*** Crypto payloads. */ @@ -242,7 +240,7 @@ uint32_t crypto_num_free_sendqueue_slots(const Net_Crypto *c, int crypt_connecti * @retval 0 if it wasn't reached. */ non_null() -bool max_speed_reached(Net_Crypto *c, int crypt_connection_id); +bool max_speed_reached(const Net_Crypto *c, int crypt_connection_id); /** @brief Sends a lossless cryptopacket. * @@ -254,7 +252,7 @@ bool max_speed_reached(Net_Crypto *c, int crypt_connection_id); * congestion_control: should congestion control apply to this packet? */ non_null() -int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, +int64_t write_cryptpacket(const Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length, bool congestion_control); /** @brief Check if packet_number was received by the other side. @@ -282,7 +280,7 @@ int cryptpacket_received(const Net_Crypto *c, int crypt_connection_id, uint32_t * The first byte of data must be in the PACKET_ID_RANGE_LOSSY. */ non_null() -int send_lossy_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length); +int send_lossy_cryptpacket(const Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length); /** @brief Add a tcp relay, associating it to a crypt_connection_id. * @@ -417,4 +415,11 @@ void do_net_crypto(Net_Crypto *c, void *userdata); nullable(1) void kill_net_crypto(Net_Crypto *c); +/** + * Returns a pointer to the net profile object for the TCP client associated with `c`. + * Returns null if `c` is null or the TCP_Connections associated with `c` is null. + */ +non_null() +const Net_Profile *nc_get_tcp_client_net_profile(const Net_Crypto *c); + #endif /* C_TOXCORE_TOXCORE_NET_CRYPTO_H */ diff --git a/external/toxcore/c-toxcore/toxcore/net_profile.c b/external/toxcore/c-toxcore/toxcore/net_profile.c new file mode 100644 index 0000000..bbff79c --- /dev/null +++ b/external/toxcore/c-toxcore/toxcore/net_profile.c @@ -0,0 +1,157 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2023-2024 The TokTok team. + */ + +/** + * Functions for the network profile. + */ + +#include "net_profile.h" + +#include + +#include "attributes.h" +#include "logger.h" +#include "mem.h" + +#include "ccompat.h" + +#define NETPROF_TCP_DATA_PACKET_ID 0x10 + +typedef struct Net_Profile { + uint64_t packets_recv[NET_PROF_MAX_PACKET_IDS]; + uint64_t packets_sent[NET_PROF_MAX_PACKET_IDS]; + + uint64_t total_packets_recv; + uint64_t total_packets_sent; + + uint64_t bytes_recv[NET_PROF_MAX_PACKET_IDS]; + uint64_t bytes_sent[NET_PROF_MAX_PACKET_IDS]; + + uint64_t total_bytes_recv; + uint64_t total_bytes_sent; +} Net_Profile; + +/** Returns the number of sent or received packets for all ID's between `start_id` and `end_id`. */ +nullable(1) +static uint64_t netprof_get_packet_count_id_range(const Net_Profile *profile, uint8_t start_id, uint8_t end_id, + Packet_Direction dir) +{ + if (profile == nullptr) { + return 0; + } + + const uint64_t *arr = dir == PACKET_DIRECTION_SEND ? profile->packets_sent : profile->packets_recv; + uint64_t count = 0; + + for (size_t i = start_id; i <= end_id; ++i) { + count += arr[i]; + } + + return count; +} + +/** Returns the number of sent or received bytes for all ID's between `start_id` and `end_id`. */ +nullable(1) +static uint64_t netprof_get_bytes_id_range(const Net_Profile *profile, uint8_t start_id, uint8_t end_id, + Packet_Direction dir) +{ + if (profile == nullptr) { + return 0; + } + + const uint64_t *arr = dir == PACKET_DIRECTION_SEND ? profile->bytes_sent : profile->bytes_recv; + uint64_t bytes = 0; + + for (size_t i = start_id; i <= end_id; ++i) { + bytes += arr[i]; + } + + return bytes; +} + +void netprof_record_packet(Net_Profile *profile, uint8_t id, size_t length, Packet_Direction dir) +{ + if (profile == nullptr) { + return; + } + + if (dir == PACKET_DIRECTION_SEND) { + ++profile->total_packets_sent; + ++profile->packets_sent[id]; + + profile->total_bytes_sent += length; + profile->bytes_sent[id] += length; + } else { + ++profile->total_packets_recv; + ++profile->packets_recv[id]; + + profile->total_bytes_recv += length; + profile->bytes_recv[id] += length; + } +} + +uint64_t netprof_get_packet_count_id(const Net_Profile *profile, uint8_t id, Packet_Direction dir) +{ + if (profile == nullptr) { + return 0; + } + + // Special case - TCP data packets can have any ID between 0x10 and 0xff + if (id == NETPROF_TCP_DATA_PACKET_ID) { + return netprof_get_packet_count_id_range(profile, id, UINT8_MAX, dir); + } + + return dir == PACKET_DIRECTION_SEND ? profile->packets_sent[id] : profile->packets_recv[id]; +} + +uint64_t netprof_get_packet_count_total(const Net_Profile *profile, Packet_Direction dir) +{ + if (profile == nullptr) { + return 0; + } + + return dir == PACKET_DIRECTION_SEND ? profile->total_packets_sent : profile->total_packets_recv; +} + +uint64_t netprof_get_bytes_id(const Net_Profile *profile, uint8_t id, Packet_Direction dir) +{ + if (profile == nullptr) { + return 0; + } + + // Special case - TCP data packets can have any ID between 0x10 and 0xff + if (id == NETPROF_TCP_DATA_PACKET_ID) { + return netprof_get_bytes_id_range(profile, id, 0xff, dir); + } + + return dir == PACKET_DIRECTION_SEND ? profile->bytes_sent[id] : profile->bytes_recv[id]; +} + +uint64_t netprof_get_bytes_total(const Net_Profile *profile, Packet_Direction dir) +{ + if (profile == nullptr) { + return 0; + } + + return dir == PACKET_DIRECTION_SEND ? profile->total_bytes_sent : profile->total_bytes_recv; +} + +Net_Profile *netprof_new(const Logger *log, const Memory *mem) +{ + Net_Profile *np = (Net_Profile *)mem_alloc(mem, sizeof(Net_Profile)); + + if (np == nullptr) { + LOGGER_ERROR(log, "failed to allocate memory for net profiler"); + return nullptr; + } + + return np; +} + +void netprof_kill(const Memory *mem, Net_Profile *net_profile) +{ + if (net_profile != nullptr) { + mem_delete(mem, net_profile); + } +} diff --git a/external/toxcore/c-toxcore/toxcore/net_profile.h b/external/toxcore/c-toxcore/toxcore/net_profile.h new file mode 100644 index 0000000..a2f66a1 --- /dev/null +++ b/external/toxcore/c-toxcore/toxcore/net_profile.h @@ -0,0 +1,73 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2023-2024 The TokTok team. + */ + +/** + * Functions for the network profile. + */ +#ifndef C_TOXCORE_TOXCORE_NET_PROFILE_H +#define C_TOXCORE_TOXCORE_NET_PROFILE_H + +#include +#include + +#include "attributes.h" +#include "logger.h" +#include "mem.h" + +/* The max number of packet ID's (must fit inside one byte) */ +#define NET_PROF_MAX_PACKET_IDS 256 + +/* If passed to a netprof function as a nullptr the function will have no effect. */ +typedef struct Net_Profile Net_Profile; + +/** Specifies whether the query is for sent or received packets. */ +typedef enum Packet_Direction { + PACKET_DIRECTION_SEND, + PACKET_DIRECTION_RECV, +} Packet_Direction; + +/** + * Records a sent or received packet of type `id` and size `length` to the given profile. + */ +nullable(1) +void netprof_record_packet(Net_Profile *profile, uint8_t id, size_t length, Packet_Direction dir); + +/** + * Returns the number of sent or received packets of type `id` for the given profile. + */ +nullable(1) +uint64_t netprof_get_packet_count_id(const Net_Profile *profile, uint8_t id, Packet_Direction dir); + +/** + * Returns the total number of sent or received packets for the given profile. + */ +nullable(1) +uint64_t netprof_get_packet_count_total(const Net_Profile *profile, Packet_Direction dir); + +/** + * Returns the number of bytes sent or received of packet type `id` for the given profile. + */ +nullable(1) +uint64_t netprof_get_bytes_id(const Net_Profile *profile, uint8_t id, Packet_Direction dir); + +/** + * Returns the total number of bytes sent or received for the given profile. + */ +nullable(1) +uint64_t netprof_get_bytes_total(const Net_Profile *profile, Packet_Direction dir); + +/** + * Returns a new net_profile object. The caller is responsible for freeing the + * returned memory via `netprof_kill`. + */ +non_null() +Net_Profile *netprof_new(const Logger *log, const Memory *mem); + +/** + * Kills a net_profile object and frees all associated memory. + */ +non_null(1) nullable(2) +void netprof_kill(const Memory *mem, Net_Profile *net_profile); + +#endif /* C_TOXCORE_TOXCORE_NET_PROFILE_H */ diff --git a/external/toxcore/c-toxcore/toxcore/network.c b/external/toxcore/c-toxcore/toxcore/network.c index 03bea7e..396d56b 100644 --- a/external/toxcore/c-toxcore/toxcore/network.c +++ b/external/toxcore/c-toxcore/toxcore/network.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2023 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ @@ -86,6 +86,7 @@ #include "ccompat.h" #include "logger.h" #include "mem.h" +#include "net_profile.h" #include "util.h" // Disable MSG_NOSIGNAL on systems not supporting it, e.g. Windows, FreeBSD @@ -907,9 +908,14 @@ static void loglogdata(const Logger *log, const char *message, const uint8_t *bu } int net_send(const Network *ns, const Logger *log, - Socket sock, const uint8_t *buf, size_t len, const IP_Port *ip_port) + Socket sock, const uint8_t *buf, size_t len, const IP_Port *ip_port, Net_Profile *net_profile) { const int res = ns->funcs->send(ns->obj, sock, buf, len); + + if (res > 0) { + netprof_record_packet(net_profile, buf[0], res, PACKET_DIRECTION_SEND); + } + loglogdata(log, "T=>", buf, len, ip_port, res); return res; } @@ -1013,6 +1019,8 @@ struct Networking_Core { uint16_t port; /* Our UDP socket. */ Socket sock; + + Net_Profile *udp_net_profile; }; Family net_family(const Networking_Core *net) @@ -1098,6 +1106,11 @@ int send_packet(const Networking_Core *net, const IP_Port *ip_port, Packet packe loglogdata(net->log, "O=>", packet.data, packet.length, ip_port, res); assert(res <= INT_MAX); + + if (res == packet.length && packet.data != nullptr) { + netprof_record_packet(net->udp_net_profile, packet.data[0], packet.length, PACKET_DIRECTION_SEND); + } + return (int)res; } @@ -1202,6 +1215,8 @@ void networking_poll(const Networking_Core *net, void *userdata) continue; } + netprof_record_packet(net->udp_net_profile, data[0], length, PACKET_DIRECTION_RECV); + const Packet_Handler *const handler = &net->packethandlers[data[0]]; if (handler->function == nullptr) { @@ -1262,6 +1277,14 @@ Networking_Core *new_networking_ex( return nullptr; } + Net_Profile *np = netprof_new(log, mem); + + if (np == nullptr) { + free(temp); + return nullptr; + } + + temp->udp_net_profile = np; temp->ns = ns; temp->log = log; temp->mem = mem; @@ -1278,6 +1301,7 @@ Networking_Core *new_networking_ex( char *strerror = net_new_strerror(neterror); LOGGER_ERROR(log, "failed to get a socket?! %d, %s", neterror, strerror); net_kill_strerror(strerror); + netprof_kill(mem, temp->udp_net_profile); mem_delete(mem, temp); if (error != nullptr) { @@ -1485,6 +1509,7 @@ void kill_networking(Networking_Core *net) kill_sock(net->ns, net->sock); } + netprof_kill(net->mem, net->udp_net_profile); mem_delete(net->mem, net); } @@ -2015,7 +2040,21 @@ bool addr_resolve_or_parse_ip(const Network *ns, const Memory *mem, const char * return addr_parse_ip(address, to); } -bool net_connect(const Network *ns, const Memory *mem, const Logger *log, Socket sock, const IP_Port *ip_port) +const char *net_err_connect_to_string(Net_Err_Connect err) +{ + switch (err) { + case NET_ERR_CONNECT_OK: + return "NET_ERR_CONNECT_OK"; + case NET_ERR_CONNECT_INVALID_FAMILY: + return "NET_ERR_CONNECT_INVALID_FAMILY"; + case NET_ERR_CONNECT_FAILED: + return "NET_ERR_CONNECT_FAILED"; + } + + return ""; +} + +bool net_connect(const Network *ns, const Memory *mem, const Logger *log, Socket sock, const IP_Port *ip_port, Net_Err_Connect *err) { Network_Addr addr = {{0}}; @@ -2037,11 +2076,13 @@ bool net_connect(const Network *ns, const Memory *mem, const Logger *log, Socket Ip_Ntoa ip_str; LOGGER_ERROR(log, "cannot connect to %s:%d which is neither IPv4 nor IPv6", net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port)); + *err = NET_ERR_CONNECT_INVALID_FAMILY; return false; } #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION if ((true)) { + *err = NET_ERR_CONNECT_OK; return true; } #endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */ @@ -2060,10 +2101,12 @@ bool net_connect(const Network *ns, const Memory *mem, const Logger *log, Socket LOGGER_WARNING(log, "failed to connect to %s:%d: %d (%s)", net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port), error, net_strerror); net_kill_strerror(net_strerror); + *err = NET_ERR_CONNECT_FAILED; return false; } } + *err = NET_ERR_CONNECT_OK; return true; } @@ -2393,3 +2436,12 @@ void net_kill_strerror(char *strerror) free(strerror); #endif /* OS_WIN32 */ } + +const Net_Profile *net_get_net_profile(const Networking_Core *net) +{ + if (net == nullptr) { + return nullptr; + } + + return net->udp_net_profile; +} diff --git a/external/toxcore/c-toxcore/toxcore/network.h b/external/toxcore/c-toxcore/toxcore/network.h index 84c2433..41d7eaf 100644 --- a/external/toxcore/c-toxcore/toxcore/network.h +++ b/external/toxcore/c-toxcore/toxcore/network.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ @@ -17,6 +17,7 @@ #include "bin_pack.h" #include "logger.h" #include "mem.h" +#include "net_profile.h" #ifdef __cplusplus extern "C" { @@ -236,8 +237,9 @@ Socket net_invalid_socket(void); /** * Calls send(sockfd, buf, len, MSG_NOSIGNAL). */ -non_null() -int net_send(const Network *ns, const Logger *log, Socket sock, const uint8_t *buf, size_t len, const IP_Port *ip_port); +non_null(1, 2, 4, 6) nullable(7) +int net_send(const Network *ns, const Logger *log, Socket sock, const uint8_t *buf, size_t len, const IP_Port *ip_port, + Net_Profile *net_profile); /** * Calls recv(sockfd, buf, len, MSG_NOSIGNAL). */ @@ -499,13 +501,22 @@ void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handl non_null(1) nullable(2) void networking_poll(const Networking_Core *net, void *userdata); +typedef enum Net_Err_Connect { + NET_ERR_CONNECT_OK, + NET_ERR_CONNECT_INVALID_FAMILY, + NET_ERR_CONNECT_FAILED, +} Net_Err_Connect; + +const char *net_err_connect_to_string(Net_Err_Connect err); + /** @brief Connect a socket to the address specified by the ip_port. * - * Return true on success. - * Return false on failure. + * @param[out] err Set to NET_ERR_CONNECT_OK on success, otherwise an error code. + * + * @retval true on success, false on failure. */ non_null() -bool net_connect(const Network *ns, const Memory *mem, const Logger *log, Socket sock, const IP_Port *ip_port); +bool net_connect(const Network *ns, const Memory *mem, const Logger *log, Socket sock, const IP_Port *ip_port, Net_Err_Connect *err); /** @brief High-level getaddrinfo implementation. * @@ -614,6 +625,13 @@ Networking_Core *new_networking_no_udp(const Logger *log, const Memory *mem, con nullable(1) void kill_networking(Networking_Core *net); +/** @brief Returns a pointer to the network net_profile object associated with `net`. + * + * Returns null if `net` is null. + */ +non_null() +const Net_Profile *net_get_net_profile(const Networking_Core *net); + #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/external/toxcore/c-toxcore/toxcore/onion.c b/external/toxcore/c-toxcore/toxcore/onion.c index 722d610..f948be5 100644 --- a/external/toxcore/c-toxcore/toxcore/onion.c +++ b/external/toxcore/c-toxcore/toxcore/onion.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ diff --git a/external/toxcore/c-toxcore/toxcore/onion.h b/external/toxcore/c-toxcore/toxcore/onion.h index ef8fdb7..bbc3f22 100644 --- a/external/toxcore/c-toxcore/toxcore/onion.h +++ b/external/toxcore/c-toxcore/toxcore/onion.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ diff --git a/external/toxcore/c-toxcore/toxcore/onion_announce.c b/external/toxcore/c-toxcore/toxcore/onion_announce.c index c7a6b38..50e2e00 100644 --- a/external/toxcore/c-toxcore/toxcore/onion_announce.c +++ b/external/toxcore/c-toxcore/toxcore/onion_announce.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ @@ -24,6 +24,7 @@ #include "shared_key_cache.h" #include "sort.h" #include "timed_auth.h" +#include "util.h" #define PING_ID_TIMEOUT ONION_ANNOUNCE_TIMEOUT @@ -506,11 +507,17 @@ static int handle_announce_request_common( return 1; } - const uint16_t ping_id_data_len = CRYPTO_PUBLIC_KEY_SIZE + sizeof(*source); - uint8_t ping_id_data[CRYPTO_PUBLIC_KEY_SIZE + sizeof(*source)]; + const uint16_t ping_id_data_len = CRYPTO_PUBLIC_KEY_SIZE + SIZE_IPPORT; + uint8_t ping_id_data[CRYPTO_PUBLIC_KEY_SIZE + SIZE_IPPORT]; memcpy(ping_id_data, packet_public_key, CRYPTO_PUBLIC_KEY_SIZE); - memcpy(ping_id_data + CRYPTO_PUBLIC_KEY_SIZE, source, sizeof(*source)); - + const int packed_len = pack_ip_port(onion_a->log, &ping_id_data[CRYPTO_PUBLIC_KEY_SIZE], SIZE_IPPORT, source); + if (packed_len < 0) { + LOGGER_ERROR(onion_a->log, "failed to pack IP/Port"); + mem_delete(onion_a->mem, plain); + return 1; + } + assert(packed_len <= SIZE_IPPORT); + memzero(&ping_id_data[CRYPTO_PUBLIC_KEY_SIZE + packed_len], SIZE_IPPORT - packed_len); const uint8_t *data_public_key = plain + ONION_PING_ID_SIZE + CRYPTO_PUBLIC_KEY_SIZE; int index; @@ -553,7 +560,7 @@ static int handle_announce_request_common( int nodes_length = 0; if (num_nodes != 0) { - nodes_length = pack_nodes(onion_a->log, response + nodes_offset, sizeof(nodes_list), nodes_list, + nodes_length = pack_nodes(onion_a->log, &response[nodes_offset], num_nodes * PACKED_NODE_SIZE_IP6, nodes_list, (uint16_t)num_nodes); if (nodes_length <= 0) { @@ -572,7 +579,7 @@ static int handle_announce_request_common( const int extra_size = pack_extra_data_callback == nullptr ? 0 : pack_extra_data_callback(onion_a->extra_data_object, - onion_a->log, onion_a->mono_time, num_nodes, + onion_a->log, onion_a->mem, onion_a->mono_time, num_nodes, plain + ONION_MINIMAL_SIZE, length - ANNOUNCE_REQUEST_MIN_SIZE_RECV, response, response_size, offset); diff --git a/external/toxcore/c-toxcore/toxcore/onion_announce.h b/external/toxcore/c-toxcore/toxcore/onion_announce.h index da0e190..d883439 100644 --- a/external/toxcore/c-toxcore/toxcore/onion_announce.h +++ b/external/toxcore/c-toxcore/toxcore/onion_announce.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ @@ -129,7 +129,7 @@ int send_data_request( const uint8_t *public_key, const uint8_t *encrypt_public_key, const uint8_t *nonce, const uint8_t *data, uint16_t length); -typedef int pack_extra_data_cb(void *object, const Logger *logger, const Mono_Time *mono_time, +typedef int pack_extra_data_cb(void *object, const Logger *logger, const Memory *mem, const Mono_Time *mono_time, uint8_t num_nodes, uint8_t *plain, uint16_t plain_size, uint8_t *response, uint16_t response_size, uint16_t offset); diff --git a/external/toxcore/c-toxcore/toxcore/onion_client.c b/external/toxcore/c-toxcore/toxcore/onion_client.c index 45546cb..7f76c83 100644 --- a/external/toxcore/c-toxcore/toxcore/onion_client.c +++ b/external/toxcore/c-toxcore/toxcore/onion_client.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ @@ -576,11 +576,17 @@ non_null() static int new_sendback(Onion_Client *onion_c, uint32_t num, const uint8_t *public_key, const IP_Port *ip_port, uint32_t path_num, uint64_t *sendback) { - uint8_t data[sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE + sizeof(IP_Port) + sizeof(uint32_t)]; + uint8_t data[sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE + SIZE_IPPORT + sizeof(uint32_t)]; memcpy(data, &num, sizeof(uint32_t)); - memcpy(data + sizeof(uint32_t), public_key, CRYPTO_PUBLIC_KEY_SIZE); - memcpy(data + sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE, ip_port, sizeof(IP_Port)); - memcpy(data + sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE + sizeof(IP_Port), &path_num, sizeof(uint32_t)); + memcpy(&data[sizeof(uint32_t)], public_key, CRYPTO_PUBLIC_KEY_SIZE); + const int packed_len = pack_ip_port(onion_c->logger, &data[sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE], SIZE_IPPORT, ip_port); + if (packed_len < 0) { + LOGGER_ERROR(onion_c->logger, "failed to pack IP/port"); + return -1; + } + assert(packed_len <= SIZE_IPPORT); + memzero(&data[sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE + packed_len], SIZE_IPPORT - packed_len); + memcpy(&data[sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE + SIZE_IPPORT], &path_num, sizeof(uint32_t)); *sendback = ping_array_add(onion_c->announce_ping_array, onion_c->mono_time, onion_c->rng, data, sizeof(data)); if (*sendback == 0) { @@ -607,15 +613,15 @@ static uint32_t check_sendback(Onion_Client *onion_c, const uint8_t *sendback, u { uint64_t sback; memcpy(&sback, sendback, sizeof(uint64_t)); - uint8_t data[sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE + sizeof(IP_Port) + sizeof(uint32_t)]; + uint8_t data[sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE + SIZE_IPPORT + sizeof(uint32_t)]; if (ping_array_check(onion_c->announce_ping_array, onion_c->mono_time, data, sizeof(data), sback) != sizeof(data)) { return -1; } memcpy(ret_pubkey, data + sizeof(uint32_t), CRYPTO_PUBLIC_KEY_SIZE); - memcpy(ret_ip_port, data + sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE, sizeof(IP_Port)); - memcpy(path_num, data + sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE + sizeof(IP_Port), sizeof(uint32_t)); + unpack_ip_port(ret_ip_port, data + sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE, SIZE_IPPORT, false); + memcpy(path_num, data + sizeof(uint32_t) + CRYPTO_PUBLIC_KEY_SIZE + SIZE_IPPORT, sizeof(uint32_t)); uint32_t num; memcpy(&num, data, sizeof(uint32_t)); diff --git a/external/toxcore/c-toxcore/toxcore/onion_client.h b/external/toxcore/c-toxcore/toxcore/onion_client.h index 92be110..5999f15 100644 --- a/external/toxcore/c-toxcore/toxcore/onion_client.h +++ b/external/toxcore/c-toxcore/toxcore/onion_client.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ diff --git a/external/toxcore/c-toxcore/toxcore/ping.c b/external/toxcore/c-toxcore/toxcore/ping.c index d3dc92d..bfb3c92 100644 --- a/external/toxcore/c-toxcore/toxcore/ping.c +++ b/external/toxcore/c-toxcore/toxcore/ping.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. * Copyright © 2013 plutooo */ diff --git a/external/toxcore/c-toxcore/toxcore/ping.h b/external/toxcore/c-toxcore/toxcore/ping.h index b0339ec..98ae7bd 100644 --- a/external/toxcore/c-toxcore/toxcore/ping.h +++ b/external/toxcore/c-toxcore/toxcore/ping.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. * Copyright © 2013 plutooo */ diff --git a/external/toxcore/c-toxcore/toxcore/ping_array.c b/external/toxcore/c-toxcore/toxcore/ping_array.c index c40215e..91b9064 100644 --- a/external/toxcore/c-toxcore/toxcore/ping_array.c +++ b/external/toxcore/c-toxcore/toxcore/ping_array.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2014 Tox project. */ diff --git a/external/toxcore/c-toxcore/toxcore/ping_array.h b/external/toxcore/c-toxcore/toxcore/ping_array.h index 3e50e66..2d1f9f5 100644 --- a/external/toxcore/c-toxcore/toxcore/ping_array.h +++ b/external/toxcore/c-toxcore/toxcore/ping_array.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ diff --git a/external/toxcore/c-toxcore/toxcore/shared_key_cache.c b/external/toxcore/c-toxcore/toxcore/shared_key_cache.c index 80d74ae..64906b5 100644 --- a/external/toxcore/c-toxcore/toxcore/shared_key_cache.c +++ b/external/toxcore/c-toxcore/toxcore/shared_key_cache.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2022-2025 The TokTok team. */ #include "shared_key_cache.h" diff --git a/external/toxcore/c-toxcore/toxcore/shared_key_cache.h b/external/toxcore/c-toxcore/toxcore/shared_key_cache.h index f6e84c3..6929532 100644 --- a/external/toxcore/c-toxcore/toxcore/shared_key_cache.h +++ b/external/toxcore/c-toxcore/toxcore/shared_key_cache.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2022-2025 The TokTok team. */ #ifndef C_TOXCORE_TOXCORE_SHARED_KEY_CACHE_H diff --git a/external/toxcore/c-toxcore/toxcore/sort.c b/external/toxcore/c-toxcore/toxcore/sort.c index 45b2ffa..9bd83d6 100644 --- a/external/toxcore/c-toxcore/toxcore/sort.c +++ b/external/toxcore/c-toxcore/toxcore/sort.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "sort.h" diff --git a/external/toxcore/c-toxcore/toxcore/sort.h b/external/toxcore/c-toxcore/toxcore/sort.h index c84d776..b95ca8b 100644 --- a/external/toxcore/c-toxcore/toxcore/sort.h +++ b/external/toxcore/c-toxcore/toxcore/sort.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #ifndef C_TOXCORE_TOXCORE_SORT_H diff --git a/external/toxcore/c-toxcore/toxcore/sort_bench.cc b/external/toxcore/c-toxcore/toxcore/sort_bench.cc index 0e75bc9..dbb872e 100644 --- a/external/toxcore/c-toxcore/toxcore/sort_bench.cc +++ b/external/toxcore/c-toxcore/toxcore/sort_bench.cc @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include diff --git a/external/toxcore/c-toxcore/toxcore/sort_test.cc b/external/toxcore/c-toxcore/toxcore/sort_test.cc index c8af334..2a08e7e 100644 --- a/external/toxcore/c-toxcore/toxcore/sort_test.cc +++ b/external/toxcore/c-toxcore/toxcore/sort_test.cc @@ -1,11 +1,12 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #include "sort.h" #include +#include // generate, sort #include #include diff --git a/external/toxcore/c-toxcore/toxcore/sort_test_util.hh b/external/toxcore/c-toxcore/toxcore/sort_test_util.hh index 8013290..52b2aeb 100644 --- a/external/toxcore/c-toxcore/toxcore/sort_test_util.hh +++ b/external/toxcore/c-toxcore/toxcore/sort_test_util.hh @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2023-2024 The TokTok team. + * Copyright © 2023-2025 The TokTok team. */ #ifndef C_TOXCORE_TOXCORE_SORT_TEST_UTIL_H diff --git a/external/toxcore/c-toxcore/toxcore/timed_auth.c b/external/toxcore/c-toxcore/toxcore/timed_auth.c index 8750032..45afe07 100644 --- a/external/toxcore/c-toxcore/toxcore/timed_auth.c +++ b/external/toxcore/c-toxcore/toxcore/timed_auth.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2019-2021 The TokTok team. + * Copyright © 2019-2025 The TokTok team. */ #include "timed_auth.h" diff --git a/external/toxcore/c-toxcore/toxcore/timed_auth.h b/external/toxcore/c-toxcore/toxcore/timed_auth.h index bb19570..ccef4b3 100644 --- a/external/toxcore/c-toxcore/toxcore/timed_auth.h +++ b/external/toxcore/c-toxcore/toxcore/timed_auth.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2019-2021 The TokTok team. + * Copyright © 2019-2025 The TokTok team. */ #ifndef C_TOXCORE_TOXCORE_TIMED_AUTH_H #define C_TOXCORE_TOXCORE_TIMED_AUTH_H diff --git a/external/toxcore/c-toxcore/toxcore/tox.c b/external/toxcore/c-toxcore/toxcore/tox.c index 4918e7b..7349928 100644 --- a/external/toxcore/c-toxcore/toxcore/tox.c +++ b/external/toxcore/c-toxcore/toxcore/tox.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ @@ -33,7 +33,7 @@ #include "onion_client.h" #include "state.h" #include "tox_private.h" -#include "tox_struct.h" +#include "tox_struct.h" // IWYU pragma: keep #include "util.h" #include "../toxencryptsave/defines.h" @@ -79,7 +79,7 @@ struct Tox_Userdata { static logger_cb tox_log_handler; non_null(1, 3, 5, 6) nullable(7) -static void tox_log_handler(void *context, Logger_Level level, const char *file, int line, const char *func, +static void tox_log_handler(void *context, Logger_Level level, const char *file, uint32_t line, const char *func, const char *message, void *userdata) { Tox *tox = (Tox *)context; @@ -929,7 +929,7 @@ static Tox *tox_new_system(const struct Tox_Options *options, Tox_Err_New *error return nullptr; } - tox->m->conferences_object = new_groupchats(tox->mono_time, tox->m); + tox->m->conferences_object = new_groupchats(tox->mono_time, sys->mem, tox->m); if (tox->m->conferences_object == nullptr) { kill_messenger(tox->m); @@ -3249,7 +3249,7 @@ bool tox_group_reconnect(Tox *tox, uint32_t group_number, Tox_Err_Group_Reconnec return false; } - const int ret = gc_rejoin_group(tox->m->group_handler, chat); + const int ret = gc_rejoin_group(tox->m->group_handler, chat, nullptr, 0); tox_unlock(tox); switch (ret) { diff --git a/external/toxcore/c-toxcore/toxcore/tox.h b/external/toxcore/c-toxcore/toxcore/tox.h index 101437b..dddaf16 100644 --- a/external/toxcore/c-toxcore/toxcore/tox.h +++ b/external/toxcore/c-toxcore/toxcore/tox.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2024 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ @@ -87,6 +87,13 @@ * If any other thread calls tox_self_set_name while this thread is allocating * memory, the length may have become invalid, and the call to * tox_self_get_name may cause undefined behaviour. + * + * @section deprecations + * + * Some functions and types are deprecated. We recommend compiling with + * `-DTOX_HIDE_DEPRECATED` to hide them. They will be removed in the next major + * version of Tox (and since we're in major version 0, that means the next + * minor version). */ #ifndef C_TOXCORE_TOXCORE_TOX_H #define C_TOXCORE_TOXCORE_TOX_H @@ -512,6 +519,7 @@ typedef void tox_log_cb(Tox *tox, Tox_Log_Level level, const char *file, uint32_ * private) in v0.3.0. */ typedef struct Tox_Options Tox_Options; +#ifndef TOX_HIDE_DEPRECATED struct Tox_Options { /** @@ -687,6 +695,7 @@ struct Tox_Options { */ bool experimental_disable_dns; }; +#endif /* TOX_HIDE_DEPRECATED */ bool tox_options_get_ipv6_enabled(const Tox_Options *options); @@ -3165,6 +3174,7 @@ const char *tox_err_conference_by_id_to_string(Tox_Err_Conference_By_Id value); Tox_Conference_Number tox_conference_by_id( const Tox *tox, const uint8_t id[TOX_CONFERENCE_ID_SIZE], Tox_Err_Conference_By_Id *error); +#ifndef TOX_HIDE_DEPRECATED /** * @brief Get the conference unique ID. * @@ -3179,6 +3189,7 @@ Tox_Conference_Number tox_conference_by_id( */ bool tox_conference_get_uid( const Tox *tox, Tox_Conference_Number conference_number, uint8_t uid[TOX_CONFERENCE_UID_SIZE]); +#endif /* TOX_HIDE_DEPRECATED */ typedef enum Tox_Err_Conference_By_Uid { @@ -3201,6 +3212,7 @@ typedef enum Tox_Err_Conference_By_Uid { const char *tox_err_conference_by_uid_to_string(Tox_Err_Conference_By_Uid value); +#ifndef TOX_HIDE_DEPRECATED /** * @brief Return the conference number associated with the specified uid. * @@ -3213,6 +3225,7 @@ const char *tox_err_conference_by_uid_to_string(Tox_Err_Conference_By_Uid value) */ Tox_Conference_Number tox_conference_by_uid( const Tox *tox, const uint8_t uid[TOX_CONFERENCE_UID_SIZE], Tox_Err_Conference_By_Uid *error); +#endif /* TOX_HIDE_DEPRECATED */ /** @} */ @@ -3685,9 +3698,7 @@ typedef enum Tox_Err_Group_Join { TOX_ERR_GROUP_JOIN_INIT, /** - * The chat_id pointer is set to NULL or a group with chat_id already - * exists. This usually happens if the client attempts to create multiple - * sessions for the same group. + * The chat_id pointer is set to NULL. */ TOX_ERR_GROUP_JOIN_BAD_CHAT_ID, @@ -3717,12 +3728,15 @@ typedef enum Tox_Err_Group_Join { const char *tox_err_group_join_to_string(Tox_Err_Group_Join value); /** - * Joins a group chat with specified Chat ID. + * Joins a group chat with specified Chat ID or reconnects to an existing group. * * This function creates a new group chat object, adds it to the chats array, * and sends a DHT announcement to find peers in the group associated with * chat_id. Once a peer has been found a join attempt will be initiated. * + * If a group with the specified Chat ID already exists, this function will attempt + * to reconnect to the group. + * * @param chat_id The Chat ID of the group you wish to join. This must be * TOX_GROUP_CHAT_ID_SIZE bytes. * @param password The password required to join the group. Set to NULL if no @@ -3817,6 +3831,7 @@ typedef enum Tox_Err_Group_Reconnect { const char *tox_err_group_reconnect_to_string(Tox_Err_Group_Reconnect value); +#ifndef TOX_HIDE_DEPRECATED /** * Reconnects to a group. * @@ -3827,8 +3842,11 @@ const char *tox_err_group_reconnect_to_string(Tox_Err_Group_Reconnect value); * @param group_number The group number of the group we wish to reconnect to. * * @return true on success. + * + * @deprecated Use `tox_group_join` instead. */ bool tox_group_reconnect(Tox *tox, Tox_Group_Number group_number, Tox_Err_Group_Reconnect *error); +#endif /* TOX_HIDE_DEPRECATED */ typedef enum Tox_Err_Group_Leave { @@ -5742,6 +5760,7 @@ void tox_callback_group_moderation(Tox *tox, tox_group_moderation_cb *callback); //!TOKSTYLE- #ifndef DOXYGEN_IGNORE +#ifndef TOX_HIDE_DEPRECATED typedef Tox_Err_Options_New TOX_ERR_OPTIONS_NEW; typedef Tox_Err_New TOX_ERR_NEW; typedef Tox_Err_Bootstrap TOX_ERR_BOOTSTRAP; @@ -5781,6 +5800,7 @@ typedef Tox_Connection TOX_CONNECTION; typedef Tox_File_Control TOX_FILE_CONTROL; typedef Tox_Conference_Type TOX_CONFERENCE_TYPE; typedef enum Tox_File_Kind TOX_FILE_KIND; +#endif /* TOX_HIDE_DEPRECATED */ #endif //!TOKSTYLE+ diff --git a/external/toxcore/c-toxcore/toxcore/tox_api.c b/external/toxcore/c-toxcore/toxcore/tox_api.c index b979e4e..7988018 100644 --- a/external/toxcore/c-toxcore/toxcore/tox_api.c +++ b/external/toxcore/c-toxcore/toxcore/tox_api.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2021 The TokTok team. + * Copyright © 2016-2025 The TokTok team. */ #include "tox.h" @@ -1722,3 +1722,120 @@ const char *tox_group_mod_event_to_string(Tox_Group_Mod_Event value) return ""; } +const char *tox_netprof_packet_id_to_string(Tox_Netprof_Packet_Id value) +{ + switch (value) { + case TOX_NETPROF_PACKET_ID_ZERO: + return "TOX_NETPROF_PACKET_ID_ZERO"; + case TOX_NETPROF_PACKET_ID_ONE: + return "TOX_NETPROF_PACKET_ID_ONE"; + case TOX_NETPROF_PACKET_ID_TWO: + return "TOX_NETPROF_PACKET_ID_TWO"; + case TOX_NETPROF_PACKET_ID_TCP_DISCONNECT: + return "TOX_NETPROF_PACKET_ID_TCP_DISCONNECT"; + case TOX_NETPROF_PACKET_ID_FOUR: + return "TOX_NETPROF_PACKET_ID_FOUR"; + case TOX_NETPROF_PACKET_ID_TCP_PONG: + return "TOX_NETPROF_PACKET_ID_TCP_PONG"; + case TOX_NETPROF_PACKET_ID_TCP_OOB_SEND: + return "TOX_NETPROF_PACKET_ID_TCP_OOB_SEND"; + case TOX_NETPROF_PACKET_ID_TCP_OOB_RECV: + return "TOX_NETPROF_PACKET_ID_TCP_OOB_RECV"; + case TOX_NETPROF_PACKET_ID_TCP_ONION_REQUEST: + return "TOX_NETPROF_PACKET_ID_TCP_ONION_REQUEST"; + case TOX_NETPROF_PACKET_ID_TCP_ONION_RESPONSE: + return "TOX_NETPROF_PACKET_ID_TCP_ONION_RESPONSE"; + case TOX_NETPROF_PACKET_ID_TCP_DATA: + return "TOX_NETPROF_PACKET_ID_TCP_DATA"; + case TOX_NETPROF_PACKET_ID_COOKIE_REQUEST: + return "TOX_NETPROF_PACKET_ID_COOKIE_REQUEST"; + case TOX_NETPROF_PACKET_ID_COOKIE_RESPONSE: + return "TOX_NETPROF_PACKET_ID_COOKIE_RESPONSE"; + case TOX_NETPROF_PACKET_ID_CRYPTO_HS: + return "TOX_NETPROF_PACKET_ID_CRYPTO_HS"; + case TOX_NETPROF_PACKET_ID_CRYPTO_DATA: + return "TOX_NETPROF_PACKET_ID_CRYPTO_DATA"; + case TOX_NETPROF_PACKET_ID_CRYPTO: + return "TOX_NETPROF_PACKET_ID_CRYPTO"; + case TOX_NETPROF_PACKET_ID_LAN_DISCOVERY: + return "TOX_NETPROF_PACKET_ID_LAN_DISCOVERY"; + case TOX_NETPROF_PACKET_ID_GC_HANDSHAKE: + return "TOX_NETPROF_PACKET_ID_GC_HANDSHAKE"; + case TOX_NETPROF_PACKET_ID_GC_LOSSLESS: + return "TOX_NETPROF_PACKET_ID_GC_LOSSLESS"; + case TOX_NETPROF_PACKET_ID_GC_LOSSY: + return "TOX_NETPROF_PACKET_ID_GC_LOSSY"; + case TOX_NETPROF_PACKET_ID_ONION_SEND_INITIAL: + return "TOX_NETPROF_PACKET_ID_ONION_SEND_INITIAL"; + case TOX_NETPROF_PACKET_ID_ONION_SEND_1: + return "TOX_NETPROF_PACKET_ID_ONION_SEND_1"; + case TOX_NETPROF_PACKET_ID_ONION_SEND_2: + return "TOX_NETPROF_PACKET_ID_ONION_SEND_2"; + case TOX_NETPROF_PACKET_ID_ANNOUNCE_REQUEST_OLD: + return "TOX_NETPROF_PACKET_ID_ANNOUNCE_REQUEST_OLD"; + case TOX_NETPROF_PACKET_ID_ANNOUNCE_RESPONSE_OLD: + return "TOX_NETPROF_PACKET_ID_ANNOUNCE_RESPONSE_OLD"; + case TOX_NETPROF_PACKET_ID_ONION_DATA_REQUEST: + return "TOX_NETPROF_PACKET_ID_ONION_DATA_REQUEST"; + case TOX_NETPROF_PACKET_ID_ONION_DATA_RESPONSE: + return "TOX_NETPROF_PACKET_ID_ONION_DATA_RESPONSE"; + case TOX_NETPROF_PACKET_ID_ANNOUNCE_REQUEST: + return "TOX_NETPROF_PACKET_ID_ANNOUNCE_REQUEST"; + case TOX_NETPROF_PACKET_ID_ANNOUNCE_RESPONSE: + return "TOX_NETPROF_PACKET_ID_ANNOUNCE_RESPONSE"; + case TOX_NETPROF_PACKET_ID_ONION_RECV_3: + return "TOX_NETPROF_PACKET_ID_ONION_RECV_3"; + case TOX_NETPROF_PACKET_ID_ONION_RECV_2: + return "TOX_NETPROF_PACKET_ID_ONION_RECV_2"; + case TOX_NETPROF_PACKET_ID_ONION_RECV_1: + return "TOX_NETPROF_PACKET_ID_ONION_RECV_1"; + case TOX_NETPROF_PACKET_ID_FORWARD_REQUEST: + return "TOX_NETPROF_PACKET_ID_FORWARD_REQUEST"; + case TOX_NETPROF_PACKET_ID_FORWARDING: + return "TOX_NETPROF_PACKET_ID_FORWARDING"; + case TOX_NETPROF_PACKET_ID_FORWARD_REPLY: + return "TOX_NETPROF_PACKET_ID_FORWARD_REPLY"; + case TOX_NETPROF_PACKET_ID_DATA_SEARCH_REQUEST: + return "TOX_NETPROF_PACKET_ID_DATA_SEARCH_REQUEST"; + case TOX_NETPROF_PACKET_ID_DATA_SEARCH_RESPONSE: + return "TOX_NETPROF_PACKET_ID_DATA_SEARCH_RESPONSE"; + case TOX_NETPROF_PACKET_ID_DATA_RETRIEVE_REQUEST: + return "TOX_NETPROF_PACKET_ID_DATA_RETRIEVE_REQUEST"; + case TOX_NETPROF_PACKET_ID_DATA_RETRIEVE_RESPONSE: + return "TOX_NETPROF_PACKET_ID_DATA_RETRIEVE_RESPONSE"; + case TOX_NETPROF_PACKET_ID_STORE_ANNOUNCE_REQUEST: + return "TOX_NETPROF_PACKET_ID_STORE_ANNOUNCE_REQUEST"; + case TOX_NETPROF_PACKET_ID_STORE_ANNOUNCE_RESPONSE: + return "TOX_NETPROF_PACKET_ID_STORE_ANNOUNCE_RESPONSE"; + case TOX_NETPROF_PACKET_ID_BOOTSTRAP_INFO: + return "TOX_NETPROF_PACKET_ID_BOOTSTRAP_INFO"; + } + + return ""; +} +const char *tox_netprof_packet_type_to_string(Tox_Netprof_Packet_Type value) +{ + switch (value) { + case TOX_NETPROF_PACKET_TYPE_TCP_CLIENT: + return "TOX_NETPROF_PACKET_TYPE_TCP_CLIENT"; + case TOX_NETPROF_PACKET_TYPE_TCP_SERVER: + return "TOX_NETPROF_PACKET_TYPE_TCP_SERVER"; + case TOX_NETPROF_PACKET_TYPE_TCP: + return "TOX_NETPROF_PACKET_TYPE_TCP"; + case TOX_NETPROF_PACKET_TYPE_UDP: + return "TOX_NETPROF_PACKET_TYPE_UDP"; + } + + return ""; +} +const char *tox_netprof_direction_to_string(Tox_Netprof_Direction value) +{ + switch (value) { + case TOX_NETPROF_DIRECTION_SENT: + return "TOX_NETPROF_DIRECTION_SENT"; + case TOX_NETPROF_DIRECTION_RECV: + return "TOX_NETPROF_DIRECTION_RECV"; + } + + return ""; +} diff --git a/external/toxcore/c-toxcore/toxcore/tox_dispatch.c b/external/toxcore/c-toxcore/toxcore/tox_dispatch.c index 5383925..dde768e 100644 --- a/external/toxcore/c-toxcore/toxcore/tox_dispatch.c +++ b/external/toxcore/c-toxcore/toxcore/tox_dispatch.c @@ -1,15 +1,15 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2022-2025 The TokTok team. */ #include "tox_dispatch.h" +#include #include #include "attributes.h" #include "ccompat.h" #include "events/events_alloc.h" // IWYU pragma: keep -#include "tox.h" #include "tox_event.h" #include "tox_events.h" diff --git a/external/toxcore/c-toxcore/toxcore/tox_dispatch.h b/external/toxcore/c-toxcore/toxcore/tox_dispatch.h index 29ac66a..01770ee 100644 --- a/external/toxcore/c-toxcore/toxcore/tox_dispatch.h +++ b/external/toxcore/c-toxcore/toxcore/tox_dispatch.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2022-2025 The TokTok team. */ /** diff --git a/external/toxcore/c-toxcore/toxcore/tox_event.c b/external/toxcore/c-toxcore/toxcore/tox_event.c index f702d62..10cabe1 100644 --- a/external/toxcore/c-toxcore/toxcore/tox_event.c +++ b/external/toxcore/c-toxcore/toxcore/tox_event.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2022-2025 The TokTok team. */ #include "tox_event.h" diff --git a/external/toxcore/c-toxcore/toxcore/tox_event.h b/external/toxcore/c-toxcore/toxcore/tox_event.h index fa98aae..293939c 100644 --- a/external/toxcore/c-toxcore/toxcore/tox_event.h +++ b/external/toxcore/c-toxcore/toxcore/tox_event.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2022-2025 The TokTok team. */ #ifndef C_TOXCORE_TOXCORE_TOX_EVENT_H diff --git a/external/toxcore/c-toxcore/toxcore/tox_events.c b/external/toxcore/c-toxcore/toxcore/tox_events.c index a5d995c..0f2b1f2 100644 --- a/external/toxcore/c-toxcore/toxcore/tox_events.c +++ b/external/toxcore/c-toxcore/toxcore/tox_events.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022-2024 The TokTok team. + * Copyright © 2022-2025 The TokTok team. */ #include "tox_events.h" diff --git a/external/toxcore/c-toxcore/toxcore/tox_events.h b/external/toxcore/c-toxcore/toxcore/tox_events.h index 6bbf13e..5452790 100644 --- a/external/toxcore/c-toxcore/toxcore/tox_events.h +++ b/external/toxcore/c-toxcore/toxcore/tox_events.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022-2024 The TokTok team. + * Copyright © 2022-2025 The TokTok team. */ /** diff --git a/external/toxcore/c-toxcore/toxcore/tox_pack.c b/external/toxcore/c-toxcore/toxcore/tox_pack.c index 3d584ac..63995c8 100644 --- a/external/toxcore/c-toxcore/toxcore/tox_pack.c +++ b/external/toxcore/c-toxcore/toxcore/tox_pack.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2022-2025 The TokTok team. */ #include "tox_pack.h" diff --git a/external/toxcore/c-toxcore/toxcore/tox_pack.h b/external/toxcore/c-toxcore/toxcore/tox_pack.h index ea8605d..895cd23 100644 --- a/external/toxcore/c-toxcore/toxcore/tox_pack.h +++ b/external/toxcore/c-toxcore/toxcore/tox_pack.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2022-2025 The TokTok team. */ #ifndef C_TOXCORE_TOXCORE_TOX_PACK_H diff --git a/external/toxcore/c-toxcore/toxcore/tox_private.c b/external/toxcore/c-toxcore/toxcore/tox_private.c index ff6aa6e..d7e13ac 100644 --- a/external/toxcore/c-toxcore/toxcore/tox_private.c +++ b/external/toxcore/c-toxcore/toxcore/tox_private.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2022 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ @@ -11,13 +11,16 @@ #include #include "DHT.h" +#include "TCP_server.h" #include "attributes.h" #include "ccompat.h" #include "crypto_core.h" #include "group_chats.h" #include "group_common.h" +#include "logger.h" #include "mem.h" #include "net_crypto.h" +#include "net_profile.h" #include "network.h" #include "tox.h" #include "tox_struct.h" @@ -226,3 +229,199 @@ bool tox_group_peer_get_ip_address(const Tox *tox, uint32_t group_number, uint32 SET_ERROR_PARAMETER(error, TOX_ERR_GROUP_PEER_QUERY_OK); return true; } + +uint64_t tox_netprof_get_packet_id_count(const Tox *tox, Tox_Netprof_Packet_Type type, uint8_t id, + Tox_Netprof_Direction direction) +{ + assert(tox != nullptr); + + tox_lock(tox); + + const Net_Profile *tcp_c_profile = nc_get_tcp_client_net_profile(tox->m->net_crypto); + const Net_Profile *tcp_s_profile = tcp_server_get_net_profile(tox->m->tcp_server); + + const Packet_Direction dir = (Packet_Direction) direction; + + uint64_t count = 0; + + switch (type) { + case TOX_NETPROF_PACKET_TYPE_TCP_CLIENT: { + count = netprof_get_packet_count_id(tcp_c_profile, id, dir); + break; + } + + case TOX_NETPROF_PACKET_TYPE_TCP_SERVER: { + count = netprof_get_packet_count_id(tcp_s_profile, id, dir); + break; + } + + case TOX_NETPROF_PACKET_TYPE_TCP: { + const uint64_t tcp_c_count = netprof_get_packet_count_id(tcp_c_profile, id, dir); + const uint64_t tcp_s_count = netprof_get_packet_count_id(tcp_s_profile, id, dir); + count = tcp_c_count + tcp_s_count; + break; + } + + case TOX_NETPROF_PACKET_TYPE_UDP: { + const Net_Profile *udp_profile = net_get_net_profile(tox->m->net); + count = netprof_get_packet_count_id(udp_profile, id, dir); + break; + } + + default: { + LOGGER_ERROR(tox->m->log, "invalid packet type: %d", type); + break; + } + } + + tox_unlock(tox); + + return count; +} + +uint64_t tox_netprof_get_packet_total_count(const Tox *tox, Tox_Netprof_Packet_Type type, + Tox_Netprof_Direction direction) +{ + assert(tox != nullptr); + + tox_lock(tox); + + const Net_Profile *tcp_c_profile = nc_get_tcp_client_net_profile(tox->m->net_crypto); + const Net_Profile *tcp_s_profile = tcp_server_get_net_profile(tox->m->tcp_server); + + const Packet_Direction dir = (Packet_Direction) direction; + + uint64_t count = 0; + + switch (type) { + case TOX_NETPROF_PACKET_TYPE_TCP_CLIENT: { + count = netprof_get_packet_count_total(tcp_c_profile, dir); + break; + } + + case TOX_NETPROF_PACKET_TYPE_TCP_SERVER: { + count = netprof_get_packet_count_total(tcp_s_profile, dir); + break; + } + + case TOX_NETPROF_PACKET_TYPE_TCP: { + const uint64_t tcp_c_count = netprof_get_packet_count_total(tcp_c_profile, dir); + const uint64_t tcp_s_count = netprof_get_packet_count_total(tcp_s_profile, dir); + count = tcp_c_count + tcp_s_count; + break; + } + + case TOX_NETPROF_PACKET_TYPE_UDP: { + const Net_Profile *udp_profile = net_get_net_profile(tox->m->net); + count = netprof_get_packet_count_total(udp_profile, dir); + break; + } + + default: { + LOGGER_ERROR(tox->m->log, "invalid packet type: %d", type); + break; + } + } + + tox_unlock(tox); + + return count; +} + +uint64_t tox_netprof_get_packet_id_bytes(const Tox *tox, Tox_Netprof_Packet_Type type, uint8_t id, + Tox_Netprof_Direction direction) +{ + assert(tox != nullptr); + + tox_lock(tox); + + const Net_Profile *tcp_c_profile = nc_get_tcp_client_net_profile(tox->m->net_crypto); + const Net_Profile *tcp_s_profile = tcp_server_get_net_profile(tox->m->tcp_server); + + const Packet_Direction dir = (Packet_Direction) direction; + + uint64_t bytes = 0; + + switch (type) { + case TOX_NETPROF_PACKET_TYPE_TCP_CLIENT: { + bytes = netprof_get_bytes_id(tcp_c_profile, id, dir); + break; + } + + case TOX_NETPROF_PACKET_TYPE_TCP_SERVER: { + bytes = netprof_get_bytes_id(tcp_s_profile, id, dir); + break; + } + + case TOX_NETPROF_PACKET_TYPE_TCP: { + const uint64_t tcp_c_bytes = netprof_get_bytes_id(tcp_c_profile, id, dir); + const uint64_t tcp_s_bytes = netprof_get_bytes_id(tcp_s_profile, id, dir); + bytes = tcp_c_bytes + tcp_s_bytes; + break; + } + + case TOX_NETPROF_PACKET_TYPE_UDP: { + const Net_Profile *udp_profile = net_get_net_profile(tox->m->net); + bytes = netprof_get_bytes_id(udp_profile, id, dir); + break; + } + + default: { + LOGGER_ERROR(tox->m->log, "invalid packet type: %d", type); + break; + } + } + + tox_unlock(tox); + + return bytes; +} + +uint64_t tox_netprof_get_packet_total_bytes(const Tox *tox, Tox_Netprof_Packet_Type type, + Tox_Netprof_Direction direction) +{ + assert(tox != nullptr); + + tox_lock(tox); + + const Net_Profile *tcp_c_profile = nc_get_tcp_client_net_profile(tox->m->net_crypto); + const Net_Profile *tcp_s_profile = tcp_server_get_net_profile(tox->m->tcp_server); + + const Packet_Direction dir = (Packet_Direction) direction; + + uint64_t bytes = 0; + + switch (type) { + case TOX_NETPROF_PACKET_TYPE_TCP_CLIENT: { + bytes = netprof_get_bytes_total(tcp_c_profile, dir); + break; + } + + case TOX_NETPROF_PACKET_TYPE_TCP_SERVER: { + bytes = netprof_get_bytes_total(tcp_s_profile, dir); + break; + } + + case TOX_NETPROF_PACKET_TYPE_TCP: { + const uint64_t tcp_c_bytes = netprof_get_bytes_total(tcp_c_profile, dir); + const uint64_t tcp_s_bytes = netprof_get_bytes_total(tcp_s_profile, dir); + bytes = tcp_c_bytes + tcp_s_bytes; + break; + } + + case TOX_NETPROF_PACKET_TYPE_UDP: { + const Net_Profile *udp_profile = net_get_net_profile(tox->m->net); + bytes = netprof_get_bytes_total(udp_profile, dir); + break; + } + + default: { + LOGGER_ERROR(tox->m->log, "invalid packet type: %d", type); + break; + } + } + + tox_unlock(tox); + + return bytes; +} diff --git a/external/toxcore/c-toxcore/toxcore/tox_private.h b/external/toxcore/c-toxcore/toxcore/tox_private.h index c872bfe..cc8a085 100644 --- a/external/toxcore/c-toxcore/toxcore/tox_private.h +++ b/external/toxcore/c-toxcore/toxcore/tox_private.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2024 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ @@ -169,6 +169,267 @@ uint16_t tox_dht_get_num_closelist(const Tox *tox); */ uint16_t tox_dht_get_num_closelist_announce_capable(const Tox *tox); +/******************************************************************************* + * + * :: Network profiler + * + ******************************************************************************/ + + +/** + * Represents all of the network packet identifiers that Toxcore uses. + * + * Notes: + * - Some packet ID's have different purposes depending on the + * packet type. These ID's are given numeral names. + * + * - Queries for invalid packet ID's return undefined results. For example, + * querying a TCP-exclusive packet ID for UDP, or querying an ID that + * doesn't exist in this enum. + */ +typedef enum Tox_Netprof_Packet_Id { + /** + * Ping request packet (UDP). + * Routing request (TCP). + */ + TOX_NETPROF_PACKET_ID_ZERO = 0x00, + + /** + * Ping response packet (UDP). + * Routing response (TCP). + */ + TOX_NETPROF_PACKET_ID_ONE = 0x01, + + /** + * Get nodes request packet (UDP). + * Connection notification (TCP). + */ + TOX_NETPROF_PACKET_ID_TWO = 0x02, + + /** + * TCP disconnect notification. + */ + TOX_NETPROF_PACKET_ID_TCP_DISCONNECT = 0x03, + + /** + * Send nodes response packet (UDP). + * Ping packet (TCP). + */ + TOX_NETPROF_PACKET_ID_FOUR = 0x04, + + /** + * TCP pong packet. + */ + TOX_NETPROF_PACKET_ID_TCP_PONG = 0x05, + + /** + * TCP out-of-band send packet. + */ + TOX_NETPROF_PACKET_ID_TCP_OOB_SEND = 0x06, + + /** + * TCP out-of-band receive packet. + */ + TOX_NETPROF_PACKET_ID_TCP_OOB_RECV = 0x07, + + /** + * TCP onion request packet. + */ + TOX_NETPROF_PACKET_ID_TCP_ONION_REQUEST = 0x08, + + /** + * TCP onion response packet. + */ + TOX_NETPROF_PACKET_ID_TCP_ONION_RESPONSE = 0x09, + + /** + * TCP data packet. + */ + TOX_NETPROF_PACKET_ID_TCP_DATA = 0x10, + + /** + * Cookie request packet. + */ + TOX_NETPROF_PACKET_ID_COOKIE_REQUEST = 0x18, + + /** + * Cookie response packet. + */ + TOX_NETPROF_PACKET_ID_COOKIE_RESPONSE = 0x19, + + /** + * Crypto handshake packet. + */ + TOX_NETPROF_PACKET_ID_CRYPTO_HS = 0x1a, + + /** + * Crypto data packet. + */ + TOX_NETPROF_PACKET_ID_CRYPTO_DATA = 0x1b, + + /** + * Encrypted data packet. + */ + TOX_NETPROF_PACKET_ID_CRYPTO = 0x20, + + /** + * LAN discovery packet. + */ + TOX_NETPROF_PACKET_ID_LAN_DISCOVERY = 0x21, + + /** + * DHT groupchat packets. + */ + TOX_NETPROF_PACKET_ID_GC_HANDSHAKE = 0x5a, + TOX_NETPROF_PACKET_ID_GC_LOSSLESS = 0x5b, + TOX_NETPROF_PACKET_ID_GC_LOSSY = 0x5c, + + /** + * Onion send packets. + */ + TOX_NETPROF_PACKET_ID_ONION_SEND_INITIAL = 0x80, + TOX_NETPROF_PACKET_ID_ONION_SEND_1 = 0x81, + TOX_NETPROF_PACKET_ID_ONION_SEND_2 = 0x82, + + /** + * DHT announce request packet (deprecated). + */ + TOX_NETPROF_PACKET_ID_ANNOUNCE_REQUEST_OLD = 0x83, + + /** + * DHT announce response packet (deprecated). + */ + TOX_NETPROF_PACKET_ID_ANNOUNCE_RESPONSE_OLD = 0x84, + + /** + * Onion data request packet. + */ + TOX_NETPROF_PACKET_ID_ONION_DATA_REQUEST = 0x85, + + /** + * Onion data response packet. + */ + TOX_NETPROF_PACKET_ID_ONION_DATA_RESPONSE = 0x86, + + /** + * DHT announce request packet. + */ + TOX_NETPROF_PACKET_ID_ANNOUNCE_REQUEST = 0x87, + + /** + * DHT announce response packet. + */ + TOX_NETPROF_PACKET_ID_ANNOUNCE_RESPONSE = 0x88, + + /** + * Onion receive packets. + */ + TOX_NETPROF_PACKET_ID_ONION_RECV_3 = 0x8c, + TOX_NETPROF_PACKET_ID_ONION_RECV_2 = 0x8d, + TOX_NETPROF_PACKET_ID_ONION_RECV_1 = 0x8e, + + TOX_NETPROF_PACKET_ID_FORWARD_REQUEST = 0x90, + TOX_NETPROF_PACKET_ID_FORWARDING = 0x91, + TOX_NETPROF_PACKET_ID_FORWARD_REPLY = 0x92, + + TOX_NETPROF_PACKET_ID_DATA_SEARCH_REQUEST = 0x93, + TOX_NETPROF_PACKET_ID_DATA_SEARCH_RESPONSE = 0x94, + TOX_NETPROF_PACKET_ID_DATA_RETRIEVE_REQUEST = 0x95, + TOX_NETPROF_PACKET_ID_DATA_RETRIEVE_RESPONSE = 0x96, + TOX_NETPROF_PACKET_ID_STORE_ANNOUNCE_REQUEST = 0x97, + TOX_NETPROF_PACKET_ID_STORE_ANNOUNCE_RESPONSE = 0x98, + + /** + * Bootstrap info packet. + */ + TOX_NETPROF_PACKET_ID_BOOTSTRAP_INFO = 0xf0, +} Tox_Netprof_Packet_Id; + +const char *tox_netprof_packet_id_to_string(Tox_Netprof_Packet_Id value); + +/** + * Specifies the packet type for a given query. + */ +typedef enum Tox_Netprof_Packet_Type { + /** + * TCP client packets. + */ + TOX_NETPROF_PACKET_TYPE_TCP_CLIENT, + + /** + * TCP server packets. + */ + TOX_NETPROF_PACKET_TYPE_TCP_SERVER, + + /** + * Combined TCP server and TCP client packets. + */ + TOX_NETPROF_PACKET_TYPE_TCP, + + /** + * UDP packets. + */ + TOX_NETPROF_PACKET_TYPE_UDP, +} Tox_Netprof_Packet_Type; + +const char *tox_netprof_packet_type_to_string(Tox_Netprof_Packet_Type value); + +/** + * Specifies the packet direction for a given query. + */ +typedef enum Tox_Netprof_Direction { + /** + * Outbound packets. + */ + TOX_NETPROF_DIRECTION_SENT, + + /** + * Inbound packets. + */ + TOX_NETPROF_DIRECTION_RECV, +} Tox_Netprof_Direction; + +const char *tox_netprof_direction_to_string(Tox_Netprof_Direction value); + +/** + * Return the number of packets sent or received for a specific packet ID. + * + * @param type The types of packets being queried. + * @param id The packet ID being queried. + * @param direction The packet direction. + */ +uint64_t tox_netprof_get_packet_id_count(const Tox *tox, Tox_Netprof_Packet_Type type, uint8_t id, + Tox_Netprof_Direction direction); + +/** + * Return the total number of packets sent or received. + * + * @param type The types of packets being queried. + * @param direction The packet direction. + */ +uint64_t tox_netprof_get_packet_total_count(const Tox *tox, Tox_Netprof_Packet_Type type, + Tox_Netprof_Direction direction); + +/** + * Return the number of bytes sent or received for a specific packet ID. + * + * @param type The types of packets being queried. + * @param id The packet ID being queried. + * @param direction The packet direction. + */ +uint64_t tox_netprof_get_packet_id_bytes(const Tox *tox, Tox_Netprof_Packet_Type type, uint8_t id, + Tox_Netprof_Direction direction); + +/** + * Return the total number of bytes sent or received. + * + * @param type The types of packets being queried. + * @param direction The packet direction. + */ +uint64_t tox_netprof_get_packet_total_bytes(const Tox *tox, Tox_Netprof_Packet_Type type, + Tox_Netprof_Direction direction); + + /******************************************************************************* * * :: DHT groupchat queries. diff --git a/external/toxcore/c-toxcore/toxcore/tox_struct.h b/external/toxcore/c-toxcore/toxcore/tox_struct.h index bd42fcc..88f74ea 100644 --- a/external/toxcore/c-toxcore/toxcore/tox_struct.h +++ b/external/toxcore/c-toxcore/toxcore/tox_struct.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2022 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ diff --git a/external/toxcore/c-toxcore/toxcore/tox_unpack.c b/external/toxcore/c-toxcore/toxcore/tox_unpack.c index 5caef02..6b31029 100644 --- a/external/toxcore/c-toxcore/toxcore/tox_unpack.c +++ b/external/toxcore/c-toxcore/toxcore/tox_unpack.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2022-2025 The TokTok team. */ #include "tox_unpack.h" diff --git a/external/toxcore/c-toxcore/toxcore/tox_unpack.h b/external/toxcore/c-toxcore/toxcore/tox_unpack.h index 828d294..0909237 100644 --- a/external/toxcore/c-toxcore/toxcore/tox_unpack.h +++ b/external/toxcore/c-toxcore/toxcore/tox_unpack.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2022-2025 The TokTok team. */ #ifndef C_TOXCORE_TOXCORE_TOX_UNPACK_H diff --git a/external/toxcore/c-toxcore/toxcore/util.c b/external/toxcore/c-toxcore/toxcore/util.c index 85d29a4..455e513 100644 --- a/external/toxcore/c-toxcore/toxcore/util.c +++ b/external/toxcore/c-toxcore/toxcore/util.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2024 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. * Copyright © 2013 plutooo */ @@ -13,10 +13,8 @@ #include "util.h" -#include #include -#include "attributes.h" #include "ccompat.h" #include "mem.h" @@ -82,13 +80,13 @@ bool memeq(const uint8_t *a, size_t a_size, const uint8_t *b, size_t b_size) return a_size == b_size && memcmp(a, b, a_size) == 0; } -uint8_t *memdup(const uint8_t *data, size_t data_size) +uint8_t *memdup(const Memory *mem, const uint8_t *data, size_t data_size) { if (data == nullptr || data_size == 0) { return nullptr; } - uint8_t *copy = (uint8_t *)malloc(data_size); + uint8_t *copy = (uint8_t *)mem_balloc(mem, data_size); if (copy != nullptr) { memcpy(copy, data, data_size); diff --git a/external/toxcore/c-toxcore/toxcore/util.h b/external/toxcore/c-toxcore/toxcore/util.h index 5be74a8..9be7213 100644 --- a/external/toxcore/c-toxcore/toxcore/util.h +++ b/external/toxcore/c-toxcore/toxcore/util.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. * Copyright © 2013 plutooo */ @@ -46,7 +46,7 @@ non_null() bool memeq(const uint8_t *a, size_t a_size, const uint8_t *b, size_t * * @return nullptr on allocation failure or if the input data was nullptr or data_size was 0. */ -nullable(1) uint8_t *memdup(const uint8_t *data, size_t data_size); +non_null(1) nullable(2) uint8_t *memdup(const Memory *mem, const uint8_t *data, size_t data_size); /** * @brief Set all bytes in `data` to 0. @@ -79,7 +79,12 @@ uint16_t min_u16(uint16_t a, uint16_t b); uint32_t min_u32(uint32_t a, uint32_t b); uint64_t min_u64(uint64_t a, uint64_t b); -// Comparison function: return -1 if ab. +/** + * Comparison function: + * @retval -1 if `a < b` + * @retval 0 if `a == b` + * @retval 1 if `a > b` + */ int cmp_uint(uint64_t a, uint64_t b); /** @brief Returns a 32-bit hash of key of size len */ diff --git a/external/toxcore/c-toxcore/toxcore/util_test.cc b/external/toxcore/c-toxcore/toxcore/util_test.cc index ccdd979..0a80ff5 100644 --- a/external/toxcore/c-toxcore/toxcore/util_test.cc +++ b/external/toxcore/c-toxcore/toxcore/util_test.cc @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2024 The TokTok team. + * Copyright © 2016-2025 The TokTok team. */ #include "util.h" diff --git a/external/toxcore/c-toxcore/toxencryptsave/defines.h b/external/toxcore/c-toxcore/toxencryptsave/defines.h index ac80cc7..ca66f4d 100644 --- a/external/toxcore/c-toxcore/toxencryptsave/defines.h +++ b/external/toxcore/c-toxcore/toxencryptsave/defines.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2021 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ diff --git a/external/toxcore/c-toxcore/toxencryptsave/toxencryptsave.c b/external/toxcore/c-toxcore/toxencryptsave/toxencryptsave.c index 4e43b17..63bda86 100644 --- a/external/toxcore/c-toxcore/toxencryptsave/toxencryptsave.c +++ b/external/toxcore/c-toxcore/toxencryptsave/toxencryptsave.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2018 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013 Tox project. */ @@ -395,3 +395,63 @@ bool tox_is_data_encrypted(const uint8_t data[TOX_PASS_ENCRYPTION_EXTRA_LENGTH]) { return memcmp(data, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) == 0; } + +const char *tox_err_key_derivation_to_string(Tox_Err_Key_Derivation error) +{ + switch (error) { + case TOX_ERR_KEY_DERIVATION_OK: + return "TOX_ERR_KEY_DERIVATION_OK"; + case TOX_ERR_KEY_DERIVATION_NULL: + return "TOX_ERR_KEY_DERIVATION_NULL"; + case TOX_ERR_KEY_DERIVATION_FAILED: + return "TOX_ERR_KEY_DERIVATION_FAILED"; + } + return ""; +} + +const char *tox_err_encryption_to_string(Tox_Err_Encryption error) +{ + switch (error) { + case TOX_ERR_ENCRYPTION_OK: + return "TOX_ERR_ENCRYPTION_OK"; + case TOX_ERR_ENCRYPTION_NULL: + return "TOX_ERR_ENCRYPTION_NULL"; + case TOX_ERR_ENCRYPTION_KEY_DERIVATION_FAILED: + return "TOX_ERR_ENCRYPTION_KEY_DERIVATION_FAILED"; + case TOX_ERR_ENCRYPTION_FAILED: + return "TOX_ERR_ENCRYPTION_FAILED"; + } + return ""; +} + +const char *tox_err_decryption_to_string(Tox_Err_Decryption error) +{ + switch (error) { + case TOX_ERR_DECRYPTION_OK: + return "TOX_ERR_DECRYPTION_OK"; + case TOX_ERR_DECRYPTION_NULL: + return "TOX_ERR_DECRYPTION_NULL"; + case TOX_ERR_DECRYPTION_INVALID_LENGTH: + return "TOX_ERR_DECRYPTION_INVALID_LENGTH"; + case TOX_ERR_DECRYPTION_BAD_FORMAT: + return "TOX_ERR_DECRYPTION_BAD_FORMAT"; + case TOX_ERR_DECRYPTION_KEY_DERIVATION_FAILED: + return "TOX_ERR_DECRYPTION_KEY_DERIVATION_FAILED"; + case TOX_ERR_DECRYPTION_FAILED: + return "TOX_ERR_DECRYPTION_FAILED"; + } + return ""; +} + +const char *tox_err_get_salt_to_string(Tox_Err_Get_Salt error) +{ + switch (error) { + case TOX_ERR_GET_SALT_OK: + return "TOX_ERR_GET_SALT_OK"; + case TOX_ERR_GET_SALT_NULL: + return "TOX_ERR_GET_SALT_NULL"; + case TOX_ERR_GET_SALT_BAD_FORMAT: + return "TOX_ERR_GET_SALT_BAD_FORMAT"; + } + return ""; +} diff --git a/external/toxcore/c-toxcore/toxencryptsave/toxencryptsave.h b/external/toxcore/c-toxcore/toxencryptsave/toxencryptsave.h index e4cf116..f70ff8b 100644 --- a/external/toxcore/c-toxcore/toxencryptsave/toxencryptsave.h +++ b/external/toxcore/c-toxcore/toxencryptsave/toxencryptsave.h @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2024 The TokTok team. + * Copyright © 2016-2025 The TokTok team. * Copyright © 2013-2016 Tox Developers. */ @@ -88,6 +88,8 @@ typedef enum Tox_Err_Key_Derivation { } Tox_Err_Key_Derivation; +const char *tox_err_key_derivation_to_string(Tox_Err_Key_Derivation error); + typedef enum Tox_Err_Encryption { /** @@ -114,6 +116,8 @@ typedef enum Tox_Err_Encryption { } Tox_Err_Encryption; +const char *tox_err_encryption_to_string(Tox_Err_Encryption error); + typedef enum Tox_Err_Decryption { /** @@ -152,6 +156,8 @@ typedef enum Tox_Err_Decryption { } Tox_Err_Decryption; +const char *tox_err_decryption_to_string(Tox_Err_Decryption error); + /******************************************************************************* * * BEGIN PART 1 @@ -313,6 +319,8 @@ typedef enum Tox_Err_Get_Salt { } Tox_Err_Get_Salt; +const char *tox_err_get_salt_to_string(Tox_Err_Get_Salt error); + /** * Retrieves the salt used to encrypt the given data. *