diff --git a/external/toxcore/c-toxcore/.cirrus.yml b/external/toxcore/c-toxcore/.cirrus.yml index e772275a2..5f1d55a71 100644 --- a/external/toxcore/c-toxcore/.cirrus.yml +++ b/external/toxcore/c-toxcore/.cirrus.yml @@ -29,4 +29,5 @@ freebsd_task: -DAUTOTEST=ON \ -GNinja cmake --build . --target install - ctest -j50 --output-on-failure --rerun-failed --repeat until-pass:3 || ctest -j50 --output-on-failure --rerun-failed --repeat until-pass:3 + ctest -j50 --output-on-failure --rerun-failed --repeat until-pass:3 || + ctest -j50 --output-on-failure --rerun-failed --repeat until-pass:6 diff --git a/external/toxcore/c-toxcore/.clusterfuzzlite/Dockerfile b/external/toxcore/c-toxcore/.clusterfuzzlite/Dockerfile index 4fa7632d6..6a70d0d3d 100644 --- a/external/toxcore/c-toxcore/.clusterfuzzlite/Dockerfile +++ b/external/toxcore/c-toxcore/.clusterfuzzlite/Dockerfile @@ -6,6 +6,7 @@ FROM gcr.io/oss-fuzz-base/base-builder:latest RUN apt-get update \ && apt-get -y install --no-install-suggests --no-install-recommends \ cmake \ + ninja-build \ pkg-config \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* diff --git a/external/toxcore/c-toxcore/.clusterfuzzlite/build.sh b/external/toxcore/c-toxcore/.clusterfuzzlite/build.sh index 9496f15dc..609b2df1a 100644 --- a/external/toxcore/c-toxcore/.clusterfuzzlite/build.sh +++ b/external/toxcore/c-toxcore/.clusterfuzzlite/build.sh @@ -18,12 +18,18 @@ cd "$WORK" ls /usr/local/lib/ # Debug build for asserts -cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER="$CC" \ +cmake -GNinja \ + -DCMAKE_BUILD_TYPE=Debug \ + -DCMAKE_C_COMPILER="$CC" \ -DCMAKE_CXX_COMPILER="$CXX" \ -DCMAKE_C_FLAGS="$CFLAGS" \ -DCMAKE_CXX_FLAGS="$CXXFLAGS" \ - -DBUILD_TOXAV=OFF -DENABLE_SHARED=NO -DBUILD_FUZZ_TESTS=ON \ - -DDHT_BOOTSTRAP=OFF -DBOOTSTRAP_DAEMON=OFF "$SRC"/c-toxcore + -DBUILD_TOXAV=OFF \ + -DENABLE_SHARED=OFF \ + -DBUILD_FUZZ_TESTS=ON \ + -DDHT_BOOTSTRAP=OFF \ + -DBOOTSTRAP_DAEMON=OFF \ + "$SRC"/c-toxcore for TARGET in "${FUZZ_TARGETS[@]}"; do # build fuzzer target diff --git a/external/toxcore/c-toxcore/.github/scripts/cmake-osx b/external/toxcore/c-toxcore/.github/scripts/cmake-osx index 5a44006dc..6bbffefe5 100755 --- a/external/toxcore/c-toxcore/.github/scripts/cmake-osx +++ b/external/toxcore/c-toxcore/.github/scripts/cmake-osx @@ -10,6 +10,7 @@ brew update brew install \ libconfig \ libvpx \ + ninja \ opus . ".github/scripts/flags-clang.sh" @@ -25,7 +26,7 @@ add_flag -Werror add_c_flag -Wno-c11-extensions add_c_flag -Wno-pre-c11-compat -cmake -B_build -H. \ +cmake -GNinja -B_build -H. \ -DCMAKE_C_FLAGS="$C_FLAGS" \ -DCMAKE_CXX_FLAGS="$CXX_FLAGS" \ -DCMAKE_EXE_LINKER_FLAGS="$LD_FLAGS" \ @@ -38,11 +39,10 @@ cmake -B_build -H. \ -DUSE_IPV6=OFF \ -DAUTOTEST=ON -cd _build # pushd -make "-j$NPROC" -k install +cmake --build _build +cmake --install _build # TODO(iphydf): Investigate if we can get lan discovery to work on macos CI. # It works, but CI doesn't let us press the "allow broadcast" button. -ctest -j50 --output-on-failure --rerun-failed --repeat until-pass:6 -E lan_discovery || - ctest -j50 --output-on-failure --rerun-failed --repeat until-pass:6 -E lan_discovery -cd - # popd +ctest --test-dir _build -j50 --output-on-failure --rerun-failed --repeat until-pass:6 -E lan_discovery || + ctest --test-dir _build -j50 --output-on-failure --rerun-failed --repeat until-pass:6 -E lan_discovery diff --git a/external/toxcore/c-toxcore/CMakeLists.txt b/external/toxcore/c-toxcore/CMakeLists.txt index 337ad7162..4064bb22a 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_log.c + toxcore/net_log.h toxcore/net_profile.c toxcore/net_profile.h toxcore/network.c @@ -319,6 +321,10 @@ set(toxcore_SOURCES toxcore/onion_client.c toxcore/onion_client.h toxcore/onion.h + toxcore/os_memory.c + toxcore/os_memory.h + toxcore/os_random.c + toxcore/os_random.h toxcore/ping_array.c toxcore/ping_array.h toxcore/ping.c @@ -340,6 +346,7 @@ set(toxcore_SOURCES toxcore/timed_auth.c toxcore/timed_auth.h toxcore/tox_api.c + toxcore/tox_attributes.h toxcore/tox.c toxcore/tox.h toxcore/tox_dispatch.c @@ -350,12 +357,18 @@ set(toxcore_SOURCES toxcore/tox_events.h toxcore/tox_log_level.c toxcore/tox_log_level.h + toxcore/tox_memory.c + toxcore/tox_memory.h + toxcore/tox_memory_impl.h toxcore/tox_options.c toxcore/tox_options.h toxcore/tox_private.c toxcore/tox_private.h toxcore/tox_pack.c toxcore/tox_pack.h + toxcore/tox_random.c + toxcore/tox_random.h + toxcore/tox_random_impl.h toxcore/tox_unpack.c toxcore/tox_unpack.h toxcore/util.c @@ -379,7 +392,8 @@ if(EXPERIMENTAL_API) set(toxcore_API_HEADERS ${toxcore_API_HEADERS} ${toxcore_SOURCE_DIR}/toxcore/tox_dispatch.h^tox ${toxcore_SOURCE_DIR}/toxcore/tox_events.h^tox - ${toxcore_SOURCE_DIR}/toxcore/tox_private.h^tox) + ${toxcore_SOURCE_DIR}/toxcore/tox_private.h^tox + ${toxcore_SOURCE_DIR}/toxcore/tox_random.h^tox) endif() ################################################################################ diff --git a/external/toxcore/c-toxcore/auto_tests/TCP_test.c b/external/toxcore/c-toxcore/auto_tests/TCP_test.c index a7c2c7aa2..af10fcbb0 100644 --- a/external/toxcore/c-toxcore/auto_tests/TCP_test.c +++ b/external/toxcore/c-toxcore/auto_tests/TCP_test.c @@ -10,6 +10,8 @@ #include "../toxcore/mono_time.h" #include "../toxcore/net_profile.h" #include "../toxcore/network.h" +#include "../toxcore/os_memory.h" +#include "../toxcore/os_random.h" #include "auto_test_support.h" #define NUM_PORTS 3 diff --git a/external/toxcore/c-toxcore/auto_tests/announce_test.c b/external/toxcore/c-toxcore/auto_tests/announce_test.c index 7e387c273..008af93d6 100644 --- a/external/toxcore/c-toxcore/auto_tests/announce_test.c +++ b/external/toxcore/c-toxcore/auto_tests/announce_test.c @@ -2,12 +2,10 @@ #include #include "../toxcore/announce.h" -#include "../toxcore/tox.h" -#include "../testing/misc_tools.h" #include "../toxcore/mono_time.h" #include "../toxcore/forwarding.h" -#include "../toxcore/net_crypto.h" -#include "../toxcore/util.h" +#include "../toxcore/os_memory.h" +#include "../toxcore/os_random.h" #include "auto_test_support.h" #include "check_compat.h" @@ -66,9 +64,9 @@ 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, mem, rng, mono_time, dht); + Forwarding *forwarding = new_forwarding(log, mem, rng, mono_time, dht, net); ck_assert(forwarding != nullptr); - Announcements *announce = new_announcements(log, mem, rng, mono_time, forwarding); + Announcements *announce = new_announcements(log, mem, rng, mono_time, forwarding, dht, net); ck_assert(announce != nullptr); /* Just to prevent CI from complaining that set_synch_offset is unused: */ diff --git a/external/toxcore/c-toxcore/auto_tests/conference_av_test.c b/external/toxcore/c-toxcore/auto_tests/conference_av_test.c index 5cf709ede..53c686b25 100644 --- a/external/toxcore/c-toxcore/auto_tests/conference_av_test.c +++ b/external/toxcore/c-toxcore/auto_tests/conference_av_test.c @@ -7,6 +7,7 @@ #include #include "../toxav/toxav.h" +#include "../toxcore/os_random.h" #include "check_compat.h" #define NUM_AV_GROUP_TOX 16 diff --git a/external/toxcore/c-toxcore/auto_tests/conference_test.c b/external/toxcore/c-toxcore/auto_tests/conference_test.c index efce44de9..62e9bf61d 100644 --- a/external/toxcore/c-toxcore/auto_tests/conference_test.c +++ b/external/toxcore/c-toxcore/auto_tests/conference_test.c @@ -6,6 +6,7 @@ #include #include +#include "../toxcore/os_random.h" #include "../toxcore/util.h" #include "check_compat.h" diff --git a/external/toxcore/c-toxcore/auto_tests/crypto_test.c b/external/toxcore/c-toxcore/auto_tests/crypto_test.c index 75d94ea1b..c03e5dc9d 100644 --- a/external/toxcore/c-toxcore/auto_tests/crypto_test.c +++ b/external/toxcore/c-toxcore/auto_tests/crypto_test.c @@ -2,9 +2,10 @@ #include #include -#include "../testing/misc_tools.h" #include "../toxcore/crypto_core.h" #include "../toxcore/net_crypto.h" +#include "../toxcore/os_memory.h" +#include "../toxcore/os_random.h" #include "check_compat.h" static void rand_bytes(const Random *rng, uint8_t *b, size_t blen) diff --git a/external/toxcore/c-toxcore/auto_tests/dht_nodes_response_api_test.c b/external/toxcore/c-toxcore/auto_tests/dht_nodes_response_api_test.c index 5f6e80be4..8acc111f8 100644 --- a/external/toxcore/c-toxcore/auto_tests/dht_nodes_response_api_test.c +++ b/external/toxcore/c-toxcore/auto_tests/dht_nodes_response_api_test.c @@ -13,6 +13,10 @@ #include "check_compat.h" #define NUM_TOXES 30 +// Maximum number of iterations to wait for all nodes to be crawled. 5 should +// be enough. We pick 10 in case things are slow. This makes the test take +// less time in case it completely fails, so we can retry it. +#define MAX_ITERATIONS 10 typedef struct Dht_Node { uint8_t public_key[TOX_DHT_NODE_PUBLIC_KEY_SIZE]; @@ -132,9 +136,15 @@ static void test_dht_nodes_request(AutoTox *autotoxes) tox_dht_get_num_closelist_announce_capable(autotoxes[i].tox)); } - while (!all_nodes_crawled(autotoxes, NUM_TOXES, public_key_list)) { + bool success = false; + for (size_t i = 0; i < MAX_ITERATIONS; ++i) { + if (all_nodes_crawled(autotoxes, NUM_TOXES, public_key_list)) { + success = true; + break; + } iterate_all_wait(autotoxes, NUM_TOXES, ITERATION_INTERVAL); } + ck_assert_msg(success, "Failed to crawl all nodes within %d iterations", MAX_ITERATIONS); for (size_t i = 0; i < NUM_TOXES; ++i) { State *state = (State *)autotoxes[i].state; diff --git a/external/toxcore/c-toxcore/auto_tests/encryptsave_test.c b/external/toxcore/c-toxcore/auto_tests/encryptsave_test.c index 5a2f52993..07b797dcd 100644 --- a/external/toxcore/c-toxcore/auto_tests/encryptsave_test.c +++ b/external/toxcore/c-toxcore/auto_tests/encryptsave_test.c @@ -3,9 +3,9 @@ #include #include -#include "../testing/misc_tools.h" #include "../toxcore/ccompat.h" #include "../toxcore/crypto_core.h" +#include "../toxcore/os_random.h" #include "../toxcore/tox.h" #include "../toxencryptsave/toxencryptsave.h" #include "auto_test_support.h" diff --git a/external/toxcore/c-toxcore/auto_tests/forwarding_test.c b/external/toxcore/c-toxcore/auto_tests/forwarding_test.c index 8172271ce..3e5c30710 100644 --- a/external/toxcore/c-toxcore/auto_tests/forwarding_test.c +++ b/external/toxcore/c-toxcore/auto_tests/forwarding_test.c @@ -8,7 +8,8 @@ #include "../toxcore/mono_time.h" #include "../toxcore/forwarding.h" #include "../toxcore/net_crypto.h" -#include "../toxcore/util.h" +#include "../toxcore/os_memory.h" +#include "../toxcore/os_random.h" #include "auto_test_support.h" #include "check_compat.h" @@ -131,12 +132,12 @@ static Forwarding_Subtox *new_forwarding_subtox(const Memory *mem, bool no_udp, ck_assert(subtox->tcp_np != nullptr); const TCP_Proxy_Info inf = {{{{0}}}}; - subtox->c = new_net_crypto(subtox->log, mem, rng, ns, subtox->mono_time, subtox->dht, &inf, subtox->tcp_np); + subtox->c = new_net_crypto(subtox->log, mem, rng, ns, subtox->mono_time, subtox->net, subtox->dht, &inf, subtox->tcp_np); - subtox->forwarding = new_forwarding(subtox->log, mem, rng, subtox->mono_time, subtox->dht); + subtox->forwarding = new_forwarding(subtox->log, mem, rng, subtox->mono_time, subtox->dht, subtox->net); ck_assert(subtox->forwarding != nullptr); - subtox->announce = new_announcements(subtox->log, mem, rng, subtox->mono_time, subtox->forwarding); + subtox->announce = new_announcements(subtox->log, mem, rng, subtox->mono_time, subtox->forwarding, subtox->dht, subtox->net); ck_assert(subtox->announce != nullptr); return subtox; diff --git a/external/toxcore/c-toxcore/auto_tests/group_message_test.c b/external/toxcore/c-toxcore/auto_tests/group_message_test.c index 09ed34fd8..4b8c1d1d1 100644 --- a/external/toxcore/c-toxcore/auto_tests/group_message_test.c +++ b/external/toxcore/c-toxcore/auto_tests/group_message_test.c @@ -12,6 +12,7 @@ #include "auto_test_support.h" #include "check_compat.h" +#include "../toxcore/os_random.h" #include "../toxcore/util.h" typedef struct State { diff --git a/external/toxcore/c-toxcore/auto_tests/group_sync_test.c b/external/toxcore/c-toxcore/auto_tests/group_sync_test.c index e94eb0c0a..cdd154aaf 100644 --- a/external/toxcore/c-toxcore/auto_tests/group_sync_test.c +++ b/external/toxcore/c-toxcore/auto_tests/group_sync_test.c @@ -10,6 +10,7 @@ #include "auto_test_support.h" +#include "../toxcore/os_random.h" #include "../toxcore/tox.h" #include "../toxcore/util.h" diff --git a/external/toxcore/c-toxcore/auto_tests/group_topic_test.c b/external/toxcore/c-toxcore/auto_tests/group_topic_test.c index b561847ef..44b765472 100644 --- a/external/toxcore/c-toxcore/auto_tests/group_topic_test.c +++ b/external/toxcore/c-toxcore/auto_tests/group_topic_test.c @@ -5,14 +5,13 @@ #include #include -#include #include #include "auto_test_support.h" #include "check_compat.h" +#include "../toxcore/os_random.h" #include "../toxcore/tox.h" -#include "../toxcore/group_chats.h" #define NUM_GROUP_TOXES 3 diff --git a/external/toxcore/c-toxcore/auto_tests/network_test.c b/external/toxcore/c-toxcore/auto_tests/network_test.c index 72a9408c0..7079120dc 100644 --- a/external/toxcore/c-toxcore/auto_tests/network_test.c +++ b/external/toxcore/c-toxcore/auto_tests/network_test.c @@ -1,6 +1,7 @@ #include #include "../toxcore/network.h" +#include "../toxcore/os_memory.h" #include "check_compat.h" #ifndef USE_IPV6 diff --git a/external/toxcore/c-toxcore/auto_tests/onion_test.c b/external/toxcore/c-toxcore/auto_tests/onion_test.c index 46c70d9e1..f4cbeff7b 100644 --- a/external/toxcore/c-toxcore/auto_tests/onion_test.c +++ b/external/toxcore/c-toxcore/auto_tests/onion_test.c @@ -3,10 +3,12 @@ #include "../testing/misc_tools.h" #include "../toxcore/mono_time.h" +#include "../toxcore/network.h" #include "../toxcore/onion.h" #include "../toxcore/onion_announce.h" #include "../toxcore/onion_client.h" -#include "../toxcore/util.h" +#include "../toxcore/os_memory.h" +#include "../toxcore/os_random.h" #include "auto_test_support.h" #include "check_compat.h" @@ -237,8 +239,10 @@ static void test_basic(void) Mono_Time *mono_time2 = mono_time_new(mem, nullptr, nullptr); IP ip = get_loopback(); - Onion *onion1 = new_onion(log1, mem, mono_time1, rng, new_dht(log1, mem, rng, ns, mono_time1, new_networking(log1, mem, ns, &ip, 36567), true, false)); - Onion *onion2 = new_onion(log2, mem, mono_time2, rng, new_dht(log2, mem, rng, ns, mono_time2, new_networking(log2, mem, ns, &ip, 36568), true, false)); + Networking_Core *net1 = new_networking(log1, mem, ns, &ip, 36567); + Onion *onion1 = new_onion(log1, mem, mono_time1, rng, new_dht(log1, mem, rng, ns, mono_time1, net1, true, false), net1); + Networking_Core *net2 = new_networking(log2, mem, ns, &ip, 36568); + Onion *onion2 = new_onion(log2, mem, mono_time2, rng, new_dht(log2, mem, rng, ns, mono_time2, net2, true, false), net2); ck_assert_msg((onion1 != nullptr) && (onion2 != nullptr), "Onion failed initializing."); networking_registerhandler(onion2->net, NET_PACKET_ANNOUNCE_REQUEST, &handle_test_1, onion2); @@ -281,8 +285,8 @@ static void test_basic(void) do_onion(mono_time2, onion2); } while (handled_test_2 == 0); - Onion_Announce *onion1_a = new_onion_announce(log1, mem, rng, mono_time1, onion1->dht); - Onion_Announce *onion2_a = new_onion_announce(log2, mem, rng, mono_time2, onion2->dht); + Onion_Announce *onion1_a = new_onion_announce(log1, mem, rng, mono_time1, onion1->dht, onion1->net); + Onion_Announce *onion2_a = new_onion_announce(log2, mem, rng, mono_time2, onion2->dht, onion2->net); networking_registerhandler(onion1->net, NET_PACKET_ANNOUNCE_RESPONSE, &handle_test_3, onion1); networking_registerhandler(onion1->net, NET_PACKET_ANNOUNCE_RESPONSE_OLD, &handle_test_3_old, onion1); ck_assert_msg((onion1_a != nullptr) && (onion2_a != nullptr), "Onion_Announce failed initializing."); @@ -334,7 +338,8 @@ static void test_basic(void) Mono_Time *mono_time3 = mono_time_new(mem, nullptr, nullptr); - Onion *onion3 = new_onion(log3, mem, mono_time3, rng, new_dht(log3, mem, rng, ns, mono_time3, new_networking(log3, mem, ns, &ip, 36569), true, false)); + Networking_Core *net3 = new_networking(log3, mem, ns, &ip, 36569); + Onion *onion3 = new_onion(log3, mem, mono_time3, rng, new_dht(log3, mem, rng, ns, mono_time3, net3, true, false), net3); ck_assert_msg((onion3 != nullptr), "Onion failed initializing."); random_nonce(rng, nonce); @@ -359,7 +364,7 @@ static void test_basic(void) { Onion *onion = onion3; - Networking_Core *net = dht_get_net(onion->dht); + Networking_Core *net = onion->net; DHT *dht = onion->dht; kill_onion(onion); kill_dht(dht); @@ -371,7 +376,7 @@ static void test_basic(void) { Onion *onion = onion2; - Networking_Core *net = dht_get_net(onion->dht); + Networking_Core *net = onion->net; DHT *dht = onion->dht; kill_onion(onion); kill_dht(dht); @@ -383,7 +388,7 @@ static void test_basic(void) { Onion *onion = onion1; - Networking_Core *net = dht_get_net(onion->dht); + Networking_Core *net = onion->net; DHT *dht = onion->dht; kill_onion(onion); kill_dht(dht); @@ -396,6 +401,7 @@ static void test_basic(void) typedef struct { Logger *log; Mono_Time *mono_time; + Net_Crypto *nc; Net_Profile *tcp_np; Onion *onion; Onion_Announce *onion_a; @@ -449,7 +455,7 @@ static Onions *new_onions(const Memory *mem, const Random *rng, uint16_t port, u return nullptr; } - on->onion = new_onion(on->log, mem, on->mono_time, rng, dht); + on->onion = new_onion(on->log, mem, on->mono_time, rng, dht, net); if (!on->onion) { kill_dht(dht); @@ -460,7 +466,7 @@ static Onions *new_onions(const Memory *mem, const Random *rng, uint16_t port, u return nullptr; } - on->onion_a = new_onion_announce(on->log, mem, rng, on->mono_time, dht); + on->onion_a = new_onion_announce(on->log, mem, rng, on->mono_time, dht, net); if (!on->onion_a) { kill_onion(on->onion); @@ -486,7 +492,8 @@ static Onions *new_onions(const Memory *mem, const Random *rng, uint16_t port, u } TCP_Proxy_Info inf = {{{{0}}}}; - on->onion_c = new_onion_client(on->log, mem, rng, on->mono_time, new_net_crypto(on->log, mem, rng, ns, on->mono_time, dht, &inf, on->tcp_np)); + on->nc = new_net_crypto(on->log, mem, rng, ns, on->mono_time, net, dht, &inf, on->tcp_np); + on->onion_c = new_onion_client(on->log, mem, rng, on->mono_time, on->nc, dht, net); if (!on->onion_c) { netprof_kill(mem, on->tcp_np); @@ -514,9 +521,9 @@ static void do_onions(Onions *on) static void kill_onions(const Memory *mem, Onions *on) { - Networking_Core *net = dht_get_net(on->onion->dht); + Networking_Core *net = on->onion->net; DHT *dht = on->onion->dht; - Net_Crypto *c = onion_get_net_crypto(on->onion_c); + Net_Crypto *c = on->nc; kill_onion_client(on->onion_c); kill_onion_announce(on->onion_a); kill_onion(on->onion); @@ -640,9 +647,9 @@ static void test_announce(void) printf("adding friend\n"); int frnum_f = onion_addfriend(onions[NUM_FIRST]->onion_c, - nc_get_self_public_key(onion_get_net_crypto(onions[NUM_LAST]->onion_c))); + nc_get_self_public_key(onions[NUM_LAST]->nc)); int frnum = onion_addfriend(onions[NUM_LAST]->onion_c, - nc_get_self_public_key(onion_get_net_crypto(onions[NUM_FIRST]->onion_c))); + nc_get_self_public_key(onions[NUM_FIRST]->nc)); onion_dht_pk_callback(onions[NUM_FIRST]->onion_c, frnum_f, &dht_pk_callback, onions[NUM_FIRST], NUM_FIRST); onion_dht_pk_callback(onions[NUM_LAST]->onion_c, frnum, &dht_pk_callback, onions[NUM_LAST], NUM_LAST); diff --git a/external/toxcore/c-toxcore/auto_tests/reconnect_test.c b/external/toxcore/c-toxcore/auto_tests/reconnect_test.c index 8a4b31c49..4661ec8f1 100644 --- a/external/toxcore/c-toxcore/auto_tests/reconnect_test.c +++ b/external/toxcore/c-toxcore/auto_tests/reconnect_test.c @@ -6,13 +6,12 @@ */ #include -#include #include #include "../testing/misc_tools.h" #include "../toxcore/friend_connection.h" +#include "../toxcore/os_random.h" #include "../toxcore/tox.h" -#include "../toxcore/util.h" #include "check_compat.h" #define TOX_COUNT 2 diff --git a/external/toxcore/c-toxcore/auto_tests/save_friend_test.c b/external/toxcore/c-toxcore/auto_tests/save_friend_test.c index 339690f2b..5b82c6347 100644 --- a/external/toxcore/c-toxcore/auto_tests/save_friend_test.c +++ b/external/toxcore/c-toxcore/auto_tests/save_friend_test.c @@ -8,6 +8,7 @@ #include "../testing/misc_tools.h" #include "../toxcore/ccompat.h" #include "../toxcore/crypto_core.h" +#include "../toxcore/os_random.h" #include "../toxcore/tox.h" #include "auto_test_support.h" #include "check_compat.h" diff --git a/external/toxcore/c-toxcore/auto_tests/tox_many_tcp_test.c b/external/toxcore/c-toxcore/auto_tests/tox_many_tcp_test.c index fb4505d34..217beb37a 100644 --- a/external/toxcore/c-toxcore/auto_tests/tox_many_tcp_test.c +++ b/external/toxcore/c-toxcore/auto_tests/tox_many_tcp_test.c @@ -2,14 +2,13 @@ */ #include -#include #include #include #include "../testing/misc_tools.h" #include "../toxcore/crypto_core.h" +#include "../toxcore/os_random.h" #include "../toxcore/tox.h" -#include "../toxcore/util.h" #include "auto_test_support.h" #include "check_compat.h" diff --git a/external/toxcore/c-toxcore/auto_tests/tox_many_test.c b/external/toxcore/c-toxcore/auto_tests/tox_many_test.c index 41666119a..63e2ccac3 100644 --- a/external/toxcore/c-toxcore/auto_tests/tox_many_test.c +++ b/external/toxcore/c-toxcore/auto_tests/tox_many_test.c @@ -2,14 +2,13 @@ */ #include -#include #include #include #include "../testing/misc_tools.h" #include "../toxcore/crypto_core.h" +#include "../toxcore/os_random.h" #include "../toxcore/tox.h" -#include "../toxcore/util.h" #include "auto_test_support.h" #include "check_compat.h" diff --git a/external/toxcore/c-toxcore/other/DHT_bootstrap.c b/external/toxcore/c-toxcore/other/DHT_bootstrap.c index 0d1d6fbe6..547e32411 100644 --- a/external/toxcore/c-toxcore/other/DHT_bootstrap.c +++ b/external/toxcore/c-toxcore/other/DHT_bootstrap.c @@ -26,6 +26,8 @@ #include "../toxcore/network.h" #include "../toxcore/onion.h" #include "../toxcore/onion_announce.h" +#include "../toxcore/os_memory.h" +#include "../toxcore/os_random.h" #include "../toxcore/tox.h" #define TCP_RELAY_ENABLED @@ -157,14 +159,15 @@ int main(int argc, char *argv[]) Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr); const uint16_t start_port = PORT; 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, mem, rng, mono_time, dht); + Networking_Core *net = new_networking_ex(logger, mem, ns, &ip, start_port, end_port, nullptr); + DHT *dht = new_dht(logger, mem, rng, ns, mono_time, net, true, true); + Onion *onion = new_onion(logger, mem, mono_time, rng, dht, net); + Forwarding *forwarding = new_forwarding(logger, mem, rng, mono_time, dht, net); GC_Announces_List *gc_announces_list = new_gca_list(mem); - Onion_Announce *onion_a = new_onion_announce(logger, mem, rng, mono_time, dht); + Onion_Announce *onion_a = new_onion_announce(logger, mem, rng, mono_time, dht, net); #ifdef DHT_NODE_EXTRA_PACKETS - bootstrap_set_callbacks(dht_get_net(dht), (uint32_t)DAEMON_VERSION_NUMBER, (const uint8_t *) motd_str, strlen(motd_str) + 1); + bootstrap_set_callbacks(net, (uint32_t)DAEMON_VERSION_NUMBER, (const uint8_t *) motd_str, strlen(motd_str) + 1); #endif if (onion == nullptr || forwarding == nullptr || onion_a == nullptr) { @@ -214,7 +217,7 @@ int main(int argc, char *argv[]) fclose(file); printf("\n"); - printf("Port: %u\n", net_ntohs(net_port(dht_get_net(dht)))); + printf("Port: %u\n", net_ntohs(net_port(net))); if (argc > argvoffset + 3) { printf("Trying to bootstrap into the network...\n"); @@ -258,7 +261,7 @@ int main(int argc, char *argv[]) do_dht(dht); if (mono_time_is_timeout(mono_time, last_lan_discovery, is_waiting_for_dht_connection ? 5 : LAN_DISCOVERY_INTERVAL)) { - lan_discovery_send(dht_get_net(dht), broadcast, dht_get_self_public_key(dht), net_htons(PORT)); + lan_discovery_send(net, broadcast, dht_get_self_public_key(dht), net_htons(PORT)); last_lan_discovery = mono_time_get(mono_time); } @@ -267,7 +270,7 @@ int main(int argc, char *argv[]) #ifdef TCP_RELAY_ENABLED do_tcp_server(tcp_s, mono_time); #endif - networking_poll(dht_get_net(dht), nullptr); + networking_poll(net, nullptr); c_sleep(1); } diff --git a/external/toxcore/c-toxcore/other/analysis/run-cppcheck b/external/toxcore/c-toxcore/other/analysis/run-cppcheck index 0e586ba81..e861851dc 100755 --- a/external/toxcore/c-toxcore/other/analysis/run-cppcheck +++ b/external/toxcore/c-toxcore/other/analysis/run-cppcheck @@ -24,6 +24,8 @@ CPPCHECK+=("--suppress=knownConditionTrueFalse") CPPCHECK+=("--suppress=missingIncludeSystem") # TODO(iphydf): Maybe fix? CPPCHECK+=("--suppress=signConversion") +# We have suppressions in the code for the misra extension. +CPPCHECK+=("--suppress=unmatchedSuppression") # We use this for VLAs. CPPCHECK_CXX+=("--suppress=allocaCalled") diff --git a/external/toxcore/c-toxcore/other/bootstrap_daemon/BUILD.bazel b/external/toxcore/c-toxcore/other/bootstrap_daemon/BUILD.bazel index 4c6566054..35b09ef27 100644 --- a/external/toxcore/c-toxcore/other/bootstrap_daemon/BUILD.bazel +++ b/external/toxcore/c-toxcore/other/bootstrap_daemon/BUILD.bazel @@ -25,6 +25,7 @@ cc_binary( "//c-toxcore/toxcore:network", "//c-toxcore/toxcore:onion", "//c-toxcore/toxcore:onion_announce", + "//c-toxcore/toxcore:os_random", "//c-toxcore/toxcore:tox", "@libconfig", ], diff --git a/external/toxcore/c-toxcore/other/bootstrap_daemon/docker/Dockerfile b/external/toxcore/c-toxcore/other/bootstrap_daemon/docker/Dockerfile index 3d150e02f..f3f8bf816 100644 --- a/external/toxcore/c-toxcore/other/bootstrap_daemon/docker/Dockerfile +++ b/external/toxcore/c-toxcore/other/bootstrap_daemon/docker/Dockerfile @@ -1,6 +1,6 @@ ########################################################### # Builder image: we compile the code here (static build) -FROM alpine:3.19.0 AS build +FROM alpine:3.21.0 AS build RUN ["apk", "--no-cache", "add",\ "clang",\ @@ -26,33 +26,32 @@ COPY other/bootstrap_node_packets.[ch] other/ COPY other/DHT_bootstrap.c other/ COPY other/pkgconfig other/pkgconfig COPY other/rpm other/rpm -COPY testing/misc_tools.[ch] testing/ +COPY testing testing COPY toxcore toxcore COPY toxencryptsave toxencryptsave COPY third_party third_party COPY CMakeLists.txt so.version ./ COPY other/bootstrap_daemon/CMakeLists.txt other/bootstrap_daemon/CMakeLists.txt -COPY testing/CMakeLists.txt testing/CMakeLists.txt RUN CC=clang cmake -B_build -H. \ - -GNinja \ - -DCMAKE_C_FLAGS="-DTCP_SERVER_USE_EPOLL -fsanitize=alignment,return,returns-nonnull-attribute,vla-bound,unreachable,float-cast-overflow,null -fsanitize-trap=all -fstack-protector-all" \ - -DCMAKE_UNITY_BUILD=ON \ - -DCMAKE_BUILD_TYPE=Release \ - -DFULLY_STATIC=ON \ - -DMIN_LOGGER_LEVEL=DEBUG \ - -DBUILD_TOXAV=OFF \ - -DBOOTSTRAP_DAEMON=ON && \ - cmake --build _build --target install + -GNinja \ + -DCMAKE_C_FLAGS="-DTCP_SERVER_USE_EPOLL -fsanitize=alignment,return,returns-nonnull-attribute,vla-bound,unreachable,float-cast-overflow,null -fsanitize-trap=all -fstack-protector-all" \ + -DCMAKE_UNITY_BUILD=ON \ + -DCMAKE_BUILD_TYPE=Release \ + -DFULLY_STATIC=ON \ + -DMIN_LOGGER_LEVEL=TRACE \ + -DBUILD_TOXAV=OFF \ + -DBOOTSTRAP_DAEMON=ON \ + && cmake --build _build --target install # Verify checksum from dev-built binary, so we can be sure Docker Hub doesn't # mess with your binaries. COPY other/bootstrap_daemon/docker/tox-bootstrapd.sha256 other/bootstrap_daemon/docker/ ARG CHECK=sha256sum -RUN SHA256="$("$CHECK" /usr/local/bin/tox-bootstrapd)" && \ - ("$CHECK" -c other/bootstrap_daemon/docker/tox-bootstrapd.sha256 || \ - (echo "::error file=other/bootstrap_daemon/docker/tox-bootstrapd.sha256,line=1::$SHA256" && \ - false)) +RUN SHA256="$("$CHECK" /usr/local/bin/tox-bootstrapd)" \ + && ("$CHECK" -c other/bootstrap_daemon/docker/tox-bootstrapd.sha256 || \ + (echo "::error file=other/bootstrap_daemon/docker/tox-bootstrapd.sha256,line=1::$SHA256" \ + && false)) # Remove all the example bootstrap nodes from the config file. COPY other/bootstrap_daemon/tox-bootstrapd.conf other/bootstrap_daemon/ @@ -69,12 +68,14 @@ FROM debian:bookworm-slim COPY --from=build /usr/local/bin/tox-bootstrapd /usr/local/bin/ COPY --from=build /src/c-toxcore/other/bootstrap_daemon/tox-bootstrapd.conf /etc/tox-bootstrapd.conf -RUN useradd --home-dir /var/lib/tox-bootstrapd --create-home \ - --system --shell /sbin/nologin \ - --comment "Account to run the Tox DHT bootstrap daemon" \ - --user-group tox-bootstrapd && \ - chmod 644 /etc/tox-bootstrapd.conf && \ - chmod 700 /var/lib/tox-bootstrapd +RUN useradd \ + --home-dir /var/lib/tox-bootstrapd \ + --create-home \ + --system --shell /sbin/nologin \ + --comment "Account to run the Tox DHT bootstrap daemon" \ + --user-group tox-bootstrapd \ + && chmod 644 /etc/tox-bootstrapd.conf \ + && chmod 700 /var/lib/tox-bootstrapd WORKDIR /var/lib/tox-bootstrapd diff --git a/external/toxcore/c-toxcore/other/bootstrap_daemon/docker/Dockerfile.dockerignore b/external/toxcore/c-toxcore/other/bootstrap_daemon/docker/Dockerfile.dockerignore new file mode 100644 index 000000000..369d49483 --- /dev/null +++ b/external/toxcore/c-toxcore/other/bootstrap_daemon/docker/Dockerfile.dockerignore @@ -0,0 +1,22 @@ +# Very selectively add files to the image, because we may have random stuff +# lying around. In particular, we don't need to rebuild the docker image when +# toxav changes or the Dockerfile changes down from the build. +**/* +!cmake/* +!other/bootstrap_daemon/bash-completion/* +!other/bootstrap_daemon/docker/get-nodes.py +!other/bootstrap_daemon/docker/tox-bootstrapd.sha256 +!other/bootstrap_daemon/src/* +!other/bootstrap_daemon/tox-bootstrapd.conf +!other/bootstrap_node_packets.[ch] +!other/DHT_bootstrap.c +!other/pkgconfig/* +!other/rpm/* +!testing/misc_tools.[ch] +!toxcore/**/* +!toxencryptsave/**/* +!third_party/cmp/cmp.[ch] +!CMakeLists.txt +!so.version +!other/bootstrap_daemon/CMakeLists.txt +!testing/CMakeLists.txt diff --git a/external/toxcore/c-toxcore/other/bootstrap_daemon/docker/build.sh b/external/toxcore/c-toxcore/other/bootstrap_daemon/docker/build.sh new file mode 100755 index 000000000..2d489623d --- /dev/null +++ b/external/toxcore/c-toxcore/other/bootstrap_daemon/docker/build.sh @@ -0,0 +1,12 @@ +#!/bin/sh + +set -eux -o pipefail + +GIT_ROOT="$(git rev-parse --show-toplevel)" +cd "$GIT_ROOT" + +docker build \ + -t toxchat/bootstrap-node \ + -f other/bootstrap_daemon/docker/Dockerfile \ + --build-arg CHECK=true \ + . 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 8181210d9..907567e5d 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 @@ -9,7 +9,7 @@ */ #include "command_line_arguments.h" -#include "global.h" +#include "global.h" // IWYU pragma: keep #include "log.h" #include "../../../toxcore/ccompat.h" @@ -26,7 +26,7 @@ static void print_help(void) // 2 space indent // Make sure all lines fit into 80 columns // Make sure options are listed in alphabetical order - log_write(LOG_LEVEL_INFO, + LOG_WRITE(LOG_LEVEL_INFO, "Usage: tox-bootstrapd [OPTION]... --config=FILE_PATH\n" "\n" "Options:\n" @@ -43,6 +43,7 @@ static void print_help(void) " Default option when no --log-backend is\n" " specified.\n" " stdout Writes log messages to stdout/stderr.\n" + " --trace Enable verbose network trace logging in toxcore.\n" " --version Print version information.\n"); } @@ -51,7 +52,7 @@ Cli_Status handle_command_line_arguments( bool *run_in_foreground) { if (argc < 2) { - log_write(LOG_LEVEL_ERROR, "Error: No arguments provided.\n\n"); + LOG_WRITE(LOG_LEVEL_ERROR, "Error: No arguments provided.\n\n"); print_help(); return CLI_STATUS_ERROR; } @@ -64,6 +65,7 @@ Cli_Status handle_command_line_arguments( {"help", no_argument, nullptr, 'h'}, {"log-backend", required_argument, nullptr, 'l'}, // optional, defaults to syslog {"version", no_argument, nullptr, 'v'}, + {"trace", no_argument, nullptr, 't'}, {nullptr, 0, nullptr, 0 } }; @@ -99,7 +101,7 @@ Cli_Status handle_command_line_arguments( *log_backend = LOG_BACKEND_STDOUT; log_backend_set = true; } else { - log_write(LOG_LEVEL_ERROR, "Error: Invalid BACKEND value for --log-backend option passed: %s\n\n", optarg); + LOG_WRITE(LOG_LEVEL_ERROR, "Error: Invalid BACKEND value for --log-backend option passed: %s\n\n", optarg); print_help(); return CLI_STATUS_ERROR; } @@ -107,16 +109,21 @@ Cli_Status handle_command_line_arguments( break; case 'v': - log_write(LOG_LEVEL_INFO, "Version: %lu\n", DAEMON_VERSION_NUMBER); + LOG_WRITE(LOG_LEVEL_INFO, "Version: %lu\n", DAEMON_VERSION_NUMBER); return CLI_STATUS_DONE; + case 't': + LOG_WRITE(LOG_LEVEL_INFO, "Enabling trace logging in toxcore.\n"); + log_enable_trace(true); + break; + case '?': - log_write(LOG_LEVEL_ERROR, "Error: Unrecognized option %s\n\n", argv[optind - 1]); + LOG_WRITE(LOG_LEVEL_ERROR, "Error: Unrecognized option %s\n\n", argv[optind - 1]); print_help(); return CLI_STATUS_ERROR; case ':': - log_write(LOG_LEVEL_ERROR, "Error: No argument provided for option %s\n\n", argv[optind - 1]); + LOG_WRITE(LOG_LEVEL_ERROR, "Error: No argument provided for option %s\n\n", argv[optind - 1]); print_help(); return CLI_STATUS_ERROR; } @@ -127,7 +134,7 @@ Cli_Status handle_command_line_arguments( } if (!cfg_file_path_set) { - log_write(LOG_LEVEL_ERROR, "Error: The required --config option wasn't specified\n\n"); + LOG_WRITE(LOG_LEVEL_ERROR, "Error: The required --config option wasn't specified\n\n"); print_help(); return CLI_STATUS_ERROR; } 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 43ff1511f..1ba697d4d 100644 --- a/external/toxcore/c-toxcore/other/bootstrap_daemon/src/config.c +++ b/external/toxcore/c-toxcore/other/bootstrap_daemon/src/config.c @@ -41,8 +41,8 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por config_setting_t *ports_array = config_lookup(cfg, NAME_TCP_RELAY_PORTS); if (ports_array == nullptr) { - log_write(LOG_LEVEL_WARNING, "No '%s' setting in the configuration file.\n", NAME_TCP_RELAY_PORTS); - log_write(LOG_LEVEL_WARNING, "Using default '%s':\n", NAME_TCP_RELAY_PORTS); + LOG_WRITE(LOG_LEVEL_WARNING, "No '%s' setting in the configuration file.\n", NAME_TCP_RELAY_PORTS); + LOG_WRITE(LOG_LEVEL_WARNING, "Using default '%s':\n", NAME_TCP_RELAY_PORTS); const uint16_t default_ports[] = {DEFAULT_TCP_RELAY_PORTS}; @@ -53,13 +53,13 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por const size_t default_ports_count = sizeof(default_ports) / sizeof(*default_ports); for (size_t i = 0; i < default_ports_count; ++i) { - log_write(LOG_LEVEL_INFO, "Port #%zu: %u\n", i, default_ports[i]); + LOG_WRITE(LOG_LEVEL_INFO, "Port #%zu: %u\n", i, default_ports[i]); } // Similar procedure to the one of reading config file below *tcp_relay_ports = (uint16_t *)malloc(default_ports_count * sizeof(uint16_t)); if (*tcp_relay_ports == nullptr) { - log_write(LOG_LEVEL_ERROR, "Allocation failure.\n"); + LOG_WRITE(LOG_LEVEL_ERROR, "Allocation failure.\n"); return; } @@ -69,7 +69,7 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT || (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) { - log_write(LOG_LEVEL_WARNING, "Port #%zu: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i, + LOG_WRITE(LOG_LEVEL_WARNING, "Port #%zu: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i, (*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); continue; } @@ -87,7 +87,7 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por } if (config_setting_is_array(ports_array) == CONFIG_FALSE) { - log_write(LOG_LEVEL_ERROR, "'%s' setting should be an array. Array syntax: 'setting = [value1, value2, ...]'.\n", + LOG_WRITE(LOG_LEVEL_ERROR, "'%s' setting should be an array. Array syntax: 'setting = [value1, value2, ...]'.\n", NAME_TCP_RELAY_PORTS); return; } @@ -95,13 +95,13 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por const int config_port_count = config_setting_length(ports_array); if (config_port_count == 0) { - log_write(LOG_LEVEL_ERROR, "'%s' is empty.\n", NAME_TCP_RELAY_PORTS); + LOG_WRITE(LOG_LEVEL_ERROR, "'%s' is empty.\n", NAME_TCP_RELAY_PORTS); return; } *tcp_relay_ports = (uint16_t *)malloc(config_port_count * sizeof(uint16_t)); if (*tcp_relay_ports == nullptr) { - log_write(LOG_LEVEL_ERROR, "Allocation failure.\n"); + LOG_WRITE(LOG_LEVEL_ERROR, "Allocation failure.\n"); return; } @@ -110,12 +110,12 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por if (elem == nullptr) { // It's NULL if `ports_array` is not an array (we have that check earlier) or if `i` is out of range, which should not be - log_write(LOG_LEVEL_WARNING, "Port #%d: Something went wrong while parsing the port. Stopping reading ports.\n", i); + LOG_WRITE(LOG_LEVEL_WARNING, "Port #%d: Something went wrong while parsing the port. Stopping reading ports.\n", i); break; } if (config_setting_is_number(elem) == CONFIG_FALSE) { - log_write(LOG_LEVEL_WARNING, "Port #%d: Not a number. Skipping.\n", i); + LOG_WRITE(LOG_LEVEL_WARNING, "Port #%d: Not a number. Skipping.\n", i); continue; } @@ -123,7 +123,7 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT || (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_ALLOWED_PORT) { - log_write(LOG_LEVEL_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i, + LOG_WRITE(LOG_LEVEL_WARNING, "Port #%d: Invalid port: %u, should be in [%d, %d]. Skipping.\n", i, (*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); continue; } @@ -169,15 +169,15 @@ bool get_general_config(const char *cfg_file_path, char **pid_file_path, char ** // Read the file. If there is an error, report it and exit. if (config_read_file(&cfg, cfg_file_path) == CONFIG_FALSE) { - log_write(LOG_LEVEL_ERROR, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); + LOG_WRITE(LOG_LEVEL_ERROR, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); config_destroy(&cfg); return false; } // Get port if (config_lookup_int(&cfg, NAME_PORT, port) == CONFIG_FALSE) { - log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_PORT); - log_write(LOG_LEVEL_WARNING, "Using default '%s': %d\n", NAME_PORT, DEFAULT_PORT); + LOG_WRITE(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_PORT); + LOG_WRITE(LOG_LEVEL_WARNING, "Using default '%s': %d\n", NAME_PORT, DEFAULT_PORT); *port = DEFAULT_PORT; } @@ -185,15 +185,15 @@ bool get_general_config(const char *cfg_file_path, char **pid_file_path, char ** const char *tmp_pid_file; if (config_lookup_string(&cfg, NAME_PID_FILE_PATH, &tmp_pid_file) == CONFIG_FALSE) { - log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_PID_FILE_PATH); - log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_PID_FILE_PATH, DEFAULT_PID_FILE_PATH); + LOG_WRITE(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_PID_FILE_PATH); + LOG_WRITE(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_PID_FILE_PATH, DEFAULT_PID_FILE_PATH); tmp_pid_file = DEFAULT_PID_FILE_PATH; } const size_t pid_file_path_len = strlen(tmp_pid_file) + 1; *pid_file_path = (char *)malloc(pid_file_path_len); if (*pid_file_path == nullptr) { - log_write(LOG_LEVEL_ERROR, "Allocation failure.\n"); + LOG_WRITE(LOG_LEVEL_ERROR, "Allocation failure.\n"); return false; } memcpy(*pid_file_path, tmp_pid_file, pid_file_path_len); @@ -202,15 +202,15 @@ bool get_general_config(const char *cfg_file_path, char **pid_file_path, char ** const char *tmp_keys_file; if (config_lookup_string(&cfg, NAME_KEYS_FILE_PATH, &tmp_keys_file) == CONFIG_FALSE) { - log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_KEYS_FILE_PATH); - log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_KEYS_FILE_PATH, DEFAULT_KEYS_FILE_PATH); + LOG_WRITE(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_KEYS_FILE_PATH); + LOG_WRITE(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_KEYS_FILE_PATH, DEFAULT_KEYS_FILE_PATH); tmp_keys_file = DEFAULT_KEYS_FILE_PATH; } const size_t keys_file_path_len = strlen(tmp_keys_file) + 1; *keys_file_path = (char *)malloc(keys_file_path_len); if (*keys_file_path == nullptr) { - log_write(LOG_LEVEL_ERROR, "Allocation failure.\n"); + LOG_WRITE(LOG_LEVEL_ERROR, "Allocation failure.\n"); free(*pid_file_path); *pid_file_path = nullptr; return false; @@ -219,31 +219,31 @@ bool get_general_config(const char *cfg_file_path, char **pid_file_path, char ** // Get IPv6 option if (tox_config_lookup_bool(&cfg, NAME_ENABLE_IPV6, enable_ipv6) == CONFIG_FALSE) { - log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV6); - log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_IPV6, DEFAULT_ENABLE_IPV6 ? "true" : "false"); + LOG_WRITE(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV6); + LOG_WRITE(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_IPV6, DEFAULT_ENABLE_IPV6 ? "true" : "false"); *enable_ipv6 = DEFAULT_ENABLE_IPV6; } // Get IPv4 fallback option if (tox_config_lookup_bool(&cfg, NAME_ENABLE_IPV4_FALLBACK, enable_ipv4_fallback) == CONFIG_FALSE) { - log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV4_FALLBACK); - log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_IPV4_FALLBACK, + LOG_WRITE(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_IPV4_FALLBACK); + LOG_WRITE(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_IPV4_FALLBACK, DEFAULT_ENABLE_IPV4_FALLBACK ? "true" : "false"); *enable_ipv4_fallback = DEFAULT_ENABLE_IPV4_FALLBACK; } // Get LAN discovery option if (tox_config_lookup_bool(&cfg, NAME_ENABLE_LAN_DISCOVERY, enable_lan_discovery) == CONFIG_FALSE) { - log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_LAN_DISCOVERY); - log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, + LOG_WRITE(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_LAN_DISCOVERY); + LOG_WRITE(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, DEFAULT_ENABLE_LAN_DISCOVERY ? "true" : "false"); *enable_lan_discovery = DEFAULT_ENABLE_LAN_DISCOVERY; } // Get TCP relay option if (tox_config_lookup_bool(&cfg, NAME_ENABLE_TCP_RELAY, enable_tcp_relay) == CONFIG_FALSE) { - log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_TCP_RELAY); - log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_TCP_RELAY, + LOG_WRITE(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_TCP_RELAY); + LOG_WRITE(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_TCP_RELAY, DEFAULT_ENABLE_TCP_RELAY ? "true" : "false"); *enable_tcp_relay = DEFAULT_ENABLE_TCP_RELAY; } @@ -256,8 +256,8 @@ bool get_general_config(const char *cfg_file_path, char **pid_file_path, char ** // Get MOTD option if (tox_config_lookup_bool(&cfg, NAME_ENABLE_MOTD, enable_motd) == CONFIG_FALSE) { - log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_MOTD); - log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_MOTD, + LOG_WRITE(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_ENABLE_MOTD); + LOG_WRITE(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_ENABLE_MOTD, DEFAULT_ENABLE_MOTD ? "true" : "false"); *enable_motd = DEFAULT_ENABLE_MOTD; } @@ -267,8 +267,8 @@ bool get_general_config(const char *cfg_file_path, char **pid_file_path, char ** const char *tmp_motd; if (config_lookup_string(&cfg, NAME_MOTD, &tmp_motd) == CONFIG_FALSE) { - log_write(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_MOTD); - log_write(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_MOTD, DEFAULT_MOTD); + LOG_WRITE(LOG_LEVEL_WARNING, "No '%s' setting in configuration file.\n", NAME_MOTD); + LOG_WRITE(LOG_LEVEL_WARNING, "Using default '%s': %s\n", NAME_MOTD, DEFAULT_MOTD); tmp_motd = DEFAULT_MOTD; } @@ -280,33 +280,33 @@ bool get_general_config(const char *cfg_file_path, char **pid_file_path, char ** config_destroy(&cfg); - log_write(LOG_LEVEL_INFO, "Successfully read:\n"); - log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_PID_FILE_PATH, *pid_file_path); - log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_KEYS_FILE_PATH, *keys_file_path); - log_write(LOG_LEVEL_INFO, "'%s': %d\n", NAME_PORT, *port); - log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_IPV6, *enable_ipv6 ? "true" : "false"); - log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_IPV4_FALLBACK, *enable_ipv4_fallback ? "true" : "false"); - log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, *enable_lan_discovery ? "true" : "false"); + LOG_WRITE(LOG_LEVEL_INFO, "Successfully read:\n"); + LOG_WRITE(LOG_LEVEL_INFO, "'%s': %s\n", NAME_PID_FILE_PATH, *pid_file_path); + LOG_WRITE(LOG_LEVEL_INFO, "'%s': %s\n", NAME_KEYS_FILE_PATH, *keys_file_path); + LOG_WRITE(LOG_LEVEL_INFO, "'%s': %d\n", NAME_PORT, *port); + LOG_WRITE(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_IPV6, *enable_ipv6 ? "true" : "false"); + LOG_WRITE(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_IPV4_FALLBACK, *enable_ipv4_fallback ? "true" : "false"); + LOG_WRITE(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_LAN_DISCOVERY, *enable_lan_discovery ? "true" : "false"); - log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_TCP_RELAY, *enable_tcp_relay ? "true" : "false"); + LOG_WRITE(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_TCP_RELAY, *enable_tcp_relay ? "true" : "false"); // Show info about tcp ports only if tcp relay is enabled if (*enable_tcp_relay) { if (*tcp_relay_port_count == 0) { - log_write(LOG_LEVEL_ERROR, "No TCP ports could be read.\n"); + LOG_WRITE(LOG_LEVEL_ERROR, "No TCP ports could be read.\n"); } else { - log_write(LOG_LEVEL_INFO, "Read %d TCP ports:\n", *tcp_relay_port_count); + LOG_WRITE(LOG_LEVEL_INFO, "Read %d TCP ports:\n", *tcp_relay_port_count); for (int i = 0; i < *tcp_relay_port_count; ++i) { - log_write(LOG_LEVEL_INFO, "Port #%d: %u\n", i, (*tcp_relay_ports)[i]); + LOG_WRITE(LOG_LEVEL_INFO, "Port #%d: %u\n", i, (*tcp_relay_ports)[i]); } } } - log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_MOTD, *enable_motd ? "true" : "false"); + LOG_WRITE(LOG_LEVEL_INFO, "'%s': %s\n", NAME_ENABLE_MOTD, *enable_motd ? "true" : "false"); if (*enable_motd) { - log_write(LOG_LEVEL_INFO, "'%s': %s\n", NAME_MOTD, *motd); + LOG_WRITE(LOG_LEVEL_INFO, "'%s': %s\n", NAME_MOTD, *motd); } return true; @@ -330,7 +330,7 @@ static uint8_t *bootstrap_hex_string_to_bin(const char *hex_string) const size_t len = strlen(hex_string) / 2; uint8_t *ret = (uint8_t *)malloc(len); if (ret == nullptr) { - log_write(LOG_LEVEL_ERROR, "Allocation failure.\n"); + LOG_WRITE(LOG_LEVEL_ERROR, "Allocation failure.\n"); return nullptr; } @@ -358,7 +358,7 @@ bool bootstrap_from_config(const char *cfg_file_path, DHT *dht, bool enable_ipv6 config_init(&cfg); if (config_read_file(&cfg, cfg_file_path) == CONFIG_FALSE) { - log_write(LOG_LEVEL_ERROR, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); + LOG_WRITE(LOG_LEVEL_ERROR, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); config_destroy(&cfg); return false; } @@ -366,14 +366,14 @@ bool bootstrap_from_config(const char *cfg_file_path, DHT *dht, bool enable_ipv6 config_setting_t *node_list = config_lookup(&cfg, NAME_BOOTSTRAP_NODES); if (node_list == nullptr) { - log_write(LOG_LEVEL_WARNING, "No '%s' setting in the configuration file. Skipping bootstrapping.\n", + LOG_WRITE(LOG_LEVEL_WARNING, "No '%s' setting in the configuration file. Skipping bootstrapping.\n", NAME_BOOTSTRAP_NODES); config_destroy(&cfg); return true; } if (config_setting_length(node_list) == 0) { - log_write(LOG_LEVEL_WARNING, "No bootstrap nodes found. Skipping bootstrapping.\n"); + LOG_WRITE(LOG_LEVEL_WARNING, "No bootstrap nodes found. Skipping bootstrapping.\n"); config_destroy(&cfg); return true; } @@ -402,30 +402,30 @@ bool bootstrap_from_config(const char *cfg_file_path, DHT *dht, bool enable_ipv6 // Check that all settings are present if (config_setting_lookup_string(node, NAME_PUBLIC_KEY, &bs_public_key) == CONFIG_FALSE) { - log_write(LOG_LEVEL_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, + LOG_WRITE(LOG_LEVEL_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_PUBLIC_KEY); goto next; } if (config_setting_lookup_int(node, NAME_PORT, &bs_port) == CONFIG_FALSE) { - log_write(LOG_LEVEL_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_PORT); + LOG_WRITE(LOG_LEVEL_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_PORT); goto next; } if (config_setting_lookup_string(node, NAME_ADDRESS, &bs_address) == CONFIG_FALSE) { - log_write(LOG_LEVEL_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_ADDRESS); + LOG_WRITE(LOG_LEVEL_WARNING, "Bootstrap node #%d: Couldn't find '%s' setting. Skipping the node.\n", i, NAME_ADDRESS); goto next; } // Process settings if (strlen(bs_public_key) != CRYPTO_PUBLIC_KEY_SIZE * 2) { - log_write(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_PUBLIC_KEY, + LOG_WRITE(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_PUBLIC_KEY, bs_public_key); goto next; } if (bs_port < MIN_ALLOWED_PORT || bs_port > MAX_ALLOWED_PORT) { - log_write(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %d, should be in [%d, %d]. Skipping the node.\n", i, + LOG_WRITE(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %d, should be in [%d, %d]. Skipping the node.\n", i, NAME_PORT, bs_port, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); goto next; @@ -437,11 +437,11 @@ bool bootstrap_from_config(const char *cfg_file_path, DHT *dht, bool enable_ipv6 free(bs_public_key_bin); if (!address_resolved) { - log_write(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_ADDRESS, bs_address); + LOG_WRITE(LOG_LEVEL_WARNING, "Bootstrap node #%d: Invalid '%s': %s. Skipping the node.\n", i, NAME_ADDRESS, bs_address); goto next; } - log_write(LOG_LEVEL_INFO, "Successfully added bootstrap node #%d: %s:%d %s\n", i, bs_address, bs_port, bs_public_key); + LOG_WRITE(LOG_LEVEL_INFO, "Successfully added bootstrap node #%d: %s:%d %s\n", i, bs_address, bs_port, bs_public_key); next: // config_setting_lookup_string() allocates string inside and doesn't allow us to free it directly 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 48d70470a..57c8de352 100644 --- a/external/toxcore/c-toxcore/other/bootstrap_daemon/src/log.c +++ b/external/toxcore/c-toxcore/other/bootstrap_daemon/src/log.c @@ -15,6 +15,12 @@ #define INVALID_BACKEND ((LOG_BACKEND)-1u) static LOG_BACKEND current_backend = INVALID_BACKEND; +static bool log_toxcore_trace = false; + +void log_enable_trace(bool enable) +{ + log_toxcore_trace = enable; +} bool log_open(LOG_BACKEND backend) { @@ -58,22 +64,27 @@ bool log_close(void) return true; } -bool log_write(LOG_LEVEL level, const char *format, ...) +bool log_write(LOG_LEVEL level, const char *category, const char *file, int line, const char *format, ...) { if (current_backend == INVALID_BACKEND) { return false; } + if (level == LOG_LEVEL_TRACE && !log_toxcore_trace) { + // By default, no trace logging. + return true; + } + va_list args; va_start(args, format); switch (current_backend) { case LOG_BACKEND_STDOUT: - log_backend_stdout_write(level, format, args); + log_backend_stdout_write(level, category, file, line, format, args); break; case LOG_BACKEND_SYSLOG: - log_backend_syslog_write(level, format, args); + log_backend_syslog_write(level, category, file, line, format, args); break; } 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 ba812fab0..55aad042a 100644 --- a/external/toxcore/c-toxcore/other/bootstrap_daemon/src/log.h +++ b/external/toxcore/c-toxcore/other/bootstrap_daemon/src/log.h @@ -20,11 +20,18 @@ typedef enum LOG_BACKEND { } LOG_BACKEND; typedef enum LOG_LEVEL { + LOG_LEVEL_TRACE, LOG_LEVEL_INFO, LOG_LEVEL_WARNING, LOG_LEVEL_ERROR } LOG_LEVEL; +/** + * Enables or disables logging of trace messages from toxcore. + * @param enable true to enable, false to disable. + */ +void log_enable_trace(bool enable); + /** * Initializes logger. * @param backend Specifies which backend to use. @@ -45,6 +52,13 @@ bool log_close(void); * @param ... Zero or more arguments, similar to printf function. * @return true on success, false if log is closed. */ -bool log_write(LOG_LEVEL level, const char *format, ...) GNU_PRINTF(2, 3); +bool log_write(LOG_LEVEL level, const char *category, const char *file, int line, const char *format, ...) GNU_PRINTF(5, 6); + +enum { + LOG_PATH_PREFIX = sizeof(__FILE__) - sizeof("log.h") +}; + +#define LOG_WRITEC(level, category, ...) log_write(level, category, &__FILE__[LOG_PATH_PREFIX], __LINE__, __VA_ARGS__) +#define LOG_WRITE(level, ...) LOG_WRITEC(level, "tox.bootstrap", __VA_ARGS__) #endif // C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_LOG_H 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 2ef7bf75e..d0126e186 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 @@ -11,12 +11,15 @@ #include #include +#include +#include "../../../toxcore/ccompat.h" #include "log.h" static FILE *log_backend_stdout_level(LOG_LEVEL level) { switch (level) { + case LOG_LEVEL_TRACE: // intentional fallthrough case LOG_LEVEL_INFO: return stdout; @@ -28,8 +31,36 @@ static FILE *log_backend_stdout_level(LOG_LEVEL level) return stdout; } -void log_backend_stdout_write(LOG_LEVEL level, const char *format, va_list args) +static const char *log_level_string(LOG_LEVEL level) { - vfprintf(log_backend_stdout_level(level), format, args); - fflush(log_backend_stdout_level(level)); + switch (level) { + case LOG_LEVEL_TRACE: + return "Debug"; + + case LOG_LEVEL_INFO: + return "Info"; + + case LOG_LEVEL_WARNING: + return "Warning"; + + case LOG_LEVEL_ERROR: + return "Critical"; // Qt-compatible. + } + + return "Debug"; // Just in case. Shouldn't happen. +} + +// Output bootstrap node log messages in the standard Tox log format: +// [15:02:46.433 UTC] (tox.bootstrap) config.c:444 : Info: Successfully added bootstrap node ... +void log_backend_stdout_write(LOG_LEVEL level, const char *category, const char *file, int line, const char *format, va_list args) +{ + struct timeval tv = {0}; + gettimeofday(&tv, nullptr); + + FILE *stream = log_backend_stdout_level(level); + fprintf(stream, "[%02d:%02d:%02d.%03d UTC] (%s) %s:%d : %s: ", + (int)(tv.tv_sec / 3600 % 24), (int)(tv.tv_sec / 60 % 60), (int)(tv.tv_sec % 60), (int)(tv.tv_usec / 1000), + category, file, line, log_level_string(level)); + vfprintf(stream, format, args); + fflush(stream); } 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 bb6d2cc45..c8d0f2667 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 @@ -15,6 +15,6 @@ #include "../../../toxcore/attributes.h" #include "log.h" -void log_backend_stdout_write(LOG_LEVEL level, const char *format, va_list args) GNU_PRINTF(2, 0); +void log_backend_stdout_write(LOG_LEVEL level, const char *category, const char *file, int line, const char *format, va_list args) GNU_PRINTF(5, 0); #endif // C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_LOG_BACKEND_STDOUT_H 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 de6d22876..33622e016 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 @@ -33,6 +33,9 @@ void log_backend_syslog_close(void) static int log_backend_syslog_level(LOG_LEVEL level) { switch (level) { + case LOG_LEVEL_TRACE: + return LOG_DEBUG; + case LOG_LEVEL_INFO: return LOG_INFO; @@ -46,7 +49,7 @@ static int log_backend_syslog_level(LOG_LEVEL level) return LOG_INFO; } -void log_backend_syslog_write(LOG_LEVEL level, const char *format, va_list args) +void log_backend_syslog_write(LOG_LEVEL level, const char *category, const char *file, int line, const char *format, va_list args) { va_list args2; @@ -66,6 +69,6 @@ void log_backend_syslog_write(LOG_LEVEL level, const char *format, va_list args) } vsnprintf(buf, size + 1, format, args); - syslog(log_backend_syslog_level(level), "%s", buf); + syslog(log_backend_syslog_level(level), "(%s) %s", category, buf); free(buf); } 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 1538c92d9..f3230816f 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 @@ -17,6 +17,6 @@ void log_backend_syslog_open(void); void log_backend_syslog_close(void); -void log_backend_syslog_write(LOG_LEVEL level, const char *format, va_list args) GNU_PRINTF(2, 0); +void log_backend_syslog_write(LOG_LEVEL level, const char *category, const char *file, int line, const char *format, va_list args) GNU_PRINTF(5, 0); #endif // C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_LOG_BACKEND_SYSLOG_H 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 6f80e9d64..e5e24a22c 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 @@ -36,11 +36,12 @@ #include "../../../toxcore/group_announce.h" #include "../../../toxcore/group_onion_announce.h" #include "../../../toxcore/logger.h" -#include "../../../toxcore/mem.h" #include "../../../toxcore/mono_time.h" #include "../../../toxcore/network.h" #include "../../../toxcore/onion.h" #include "../../../toxcore/onion_announce.h" +#include "../../../toxcore/os_memory.h" +#include "../../../toxcore/os_random.h" // misc #include "../../bootstrap_node_packets.h" @@ -116,7 +117,7 @@ static void print_public_key(const uint8_t *public_key) index += snprintf(buffer + index, sizeof(buffer) - index, "%02X", public_key[i]); } - log_write(LOG_LEVEL_INFO, "Public Key: %s\n", buffer); + LOG_WRITE(LOG_LEVEL_INFO, "Public Key: %s\n", buffer); } // Demonizes the process, appending PID to the PID file and closing file descriptors based on log backend @@ -128,7 +129,7 @@ static Cli_Status daemonize(LOG_BACKEND log_backend, char *pid_file_path) FILE *pid_file = fopen(pid_file_path, "r"); if (pid_file != nullptr) { - log_write(LOG_LEVEL_WARNING, "Another instance of the daemon is already running, PID file %s exists.\n", pid_file_path); + LOG_WRITE(LOG_LEVEL_WARNING, "Another instance of the daemon is already running, PID file %s exists.\n", pid_file_path); fclose(pid_file); } @@ -136,7 +137,7 @@ static Cli_Status daemonize(LOG_BACKEND log_backend, char *pid_file_path) pid_file = fopen(pid_file_path, "a+"); if (pid_file == nullptr) { - log_write(LOG_LEVEL_ERROR, "Couldn't open the PID file for writing: %s. Exiting.\n", pid_file_path); + LOG_WRITE(LOG_LEVEL_ERROR, "Couldn't open the PID file for writing: %s. Exiting.\n", pid_file_path); return CLI_STATUS_ERROR; } @@ -146,26 +147,26 @@ static Cli_Status daemonize(LOG_BACKEND log_backend, char *pid_file_path) if (pid > 0) { fprintf(pid_file, "%d", pid); fclose(pid_file); - log_write(LOG_LEVEL_INFO, "Forked successfully: PID: %d.\n", pid); + LOG_WRITE(LOG_LEVEL_INFO, "Forked successfully: PID: %d.\n", pid); return CLI_STATUS_DONE; } else { fclose(pid_file); } if (pid < 0) { - log_write(LOG_LEVEL_ERROR, "Forking failed. Exiting.\n"); + LOG_WRITE(LOG_LEVEL_ERROR, "Forking failed. Exiting.\n"); return CLI_STATUS_ERROR; } // Create a new SID for the child process if (setsid() < 0) { - log_write(LOG_LEVEL_ERROR, "SID creation failure. Exiting.\n"); + LOG_WRITE(LOG_LEVEL_ERROR, "SID creation failure. Exiting.\n"); return CLI_STATUS_ERROR; } // Change the current working directory if ((chdir("/")) < 0) { - log_write(LOG_LEVEL_ERROR, "Couldn't change working directory to '/'. Exiting.\n"); + LOG_WRITE(LOG_LEVEL_ERROR, "Couldn't change working directory to '/'. Exiting.\n"); return CLI_STATUS_ERROR; } @@ -185,6 +186,8 @@ static LOG_LEVEL logger_level_to_log_level(Logger_Level level) { switch (level) { case LOGGER_LEVEL_TRACE: + return LOG_LEVEL_TRACE; + case LOGGER_LEVEL_DEBUG: case LOGGER_LEVEL_INFO: return LOG_LEVEL_INFO; @@ -203,7 +206,11 @@ static LOG_LEVEL logger_level_to_log_level(Logger_Level level) 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:%u(%s) %s\n", file, line, func, message); + const char *category = "tox.core"; + if (level == LOGGER_LEVEL_TRACE) { + category = "tox.trace"; + } + log_write(logger_level_to_log_level(level), category, file, line, "%s\n", message); } static volatile sig_atomic_t caught_signal = 0; @@ -235,7 +242,7 @@ int main(int argc, char *argv[]) log_open(log_backend); - log_write(LOG_LEVEL_INFO, "Running \"%s\" version %lu.\n", DAEMON_NAME, DAEMON_VERSION_NUMBER); + LOG_WRITE(LOG_LEVEL_INFO, "Running \"%s\" version %lu.\n", DAEMON_NAME, DAEMON_VERSION_NUMBER); char *pid_file_path = nullptr; char *keys_file_path = nullptr; @@ -251,14 +258,14 @@ int main(int argc, char *argv[]) if (get_general_config(cfg_file_path, &pid_file_path, &keys_file_path, &start_port, &enable_ipv6, &enable_ipv4_fallback, &enable_lan_discovery, &enable_tcp_relay, &tcp_relay_ports, &tcp_relay_port_count, &enable_motd, &motd)) { - log_write(LOG_LEVEL_INFO, "General config read successfully\n"); + LOG_WRITE(LOG_LEVEL_INFO, "General config read successfully\n"); } else { - log_write(LOG_LEVEL_ERROR, "Couldn't read config file: %s. Exiting.\n", cfg_file_path); + LOG_WRITE(LOG_LEVEL_ERROR, "Couldn't read config file: %s. Exiting.\n", cfg_file_path); return 1; } if (start_port < MIN_ALLOWED_PORT || start_port > MAX_ALLOWED_PORT) { - log_write(LOG_LEVEL_ERROR, "Invalid port: %d, should be in [%d, %d]. Exiting.\n", start_port, MIN_ALLOWED_PORT, + LOG_WRITE(LOG_LEVEL_ERROR, "Invalid port: %d, should be in [%d, %d]. Exiting.\n", start_port, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); free(motd); free(tcp_relay_ports); @@ -283,28 +290,26 @@ int main(int argc, char *argv[]) IP ip; ip_init(&ip, enable_ipv6); - const Memory *mem = os_memory(); - const Random *rng = os_random(); + const Tox_Memory *mem = os_memory(); + const Tox_Random *rng = os_random(); const Network *ns = os_network(); Logger *logger = logger_new(mem); - if (MIN_LOGGER_LEVEL <= LOGGER_LEVEL_DEBUG) { - logger_callback_log(logger, toxcore_logger_callback, nullptr, nullptr); - } + logger_callback_log(logger, toxcore_logger_callback, nullptr, nullptr); const uint16_t end_port = start_port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM); Networking_Core *net = new_networking_ex(logger, mem, ns, &ip, start_port, end_port, nullptr); if (net == nullptr) { if (enable_ipv6 && enable_ipv4_fallback) { - log_write(LOG_LEVEL_WARNING, "Couldn't initialize IPv6 networking. Falling back to using IPv4.\n"); + LOG_WRITE(LOG_LEVEL_WARNING, "Couldn't initialize IPv6 networking. Falling back to using IPv4.\n"); enable_ipv6 = false; ip_init(&ip, enable_ipv6); net = new_networking_ex(logger, mem, ns, &ip, start_port, end_port, nullptr); if (net == nullptr) { - log_write(LOG_LEVEL_ERROR, "Couldn't fallback to IPv4. Exiting.\n"); + LOG_WRITE(LOG_LEVEL_ERROR, "Couldn't fallback to IPv4. Exiting.\n"); logger_kill(logger); free(motd); free(tcp_relay_ports); @@ -312,7 +317,7 @@ int main(int argc, char *argv[]) return 1; } } else { - log_write(LOG_LEVEL_ERROR, "Couldn't initialize networking. Exiting.\n"); + LOG_WRITE(LOG_LEVEL_ERROR, "Couldn't initialize networking. Exiting.\n"); logger_kill(logger); free(motd); free(tcp_relay_ports); @@ -324,7 +329,7 @@ int main(int argc, char *argv[]) Mono_Time *const mono_time = mono_time_new(mem, nullptr, nullptr); if (mono_time == nullptr) { - log_write(LOG_LEVEL_ERROR, "Couldn't initialize monotonic timer. Exiting.\n"); + LOG_WRITE(LOG_LEVEL_ERROR, "Couldn't initialize monotonic timer. Exiting.\n"); kill_networking(net); logger_kill(logger); free(motd); @@ -338,7 +343,7 @@ int main(int argc, char *argv[]) DHT *const dht = new_dht(logger, mem, rng, ns, mono_time, net, true, enable_lan_discovery); if (dht == nullptr) { - log_write(LOG_LEVEL_ERROR, "Couldn't initialize Tox DHT instance. Exiting.\n"); + LOG_WRITE(LOG_LEVEL_ERROR, "Couldn't initialize Tox DHT instance. Exiting.\n"); mono_time_free(mem, mono_time); kill_networking(net); logger_kill(logger); @@ -348,10 +353,10 @@ int main(int argc, char *argv[]) return 1; } - Forwarding *forwarding = new_forwarding(logger, mem, rng, mono_time, dht); + Forwarding *forwarding = new_forwarding(logger, mem, rng, mono_time, dht, net); if (forwarding == nullptr) { - log_write(LOG_LEVEL_ERROR, "Couldn't initialize forwarding. Exiting.\n"); + LOG_WRITE(LOG_LEVEL_ERROR, "Couldn't initialize forwarding. Exiting.\n"); kill_dht(dht); mono_time_free(mem, mono_time); kill_networking(net); @@ -362,10 +367,10 @@ int main(int argc, char *argv[]) return 1; } - Announcements *announce = new_announcements(logger, mem, rng, mono_time, forwarding); + Announcements *announce = new_announcements(logger, mem, rng, mono_time, forwarding, dht, net); if (announce == nullptr) { - log_write(LOG_LEVEL_ERROR, "Couldn't initialize DHT announcements. Exiting.\n"); + LOG_WRITE(LOG_LEVEL_ERROR, "Couldn't initialize DHT announcements. Exiting.\n"); kill_forwarding(forwarding); kill_dht(dht); mono_time_free(mem, mono_time); @@ -380,7 +385,7 @@ int main(int argc, char *argv[]) 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"); + LOG_WRITE(LOG_LEVEL_ERROR, "Couldn't initialize group announces. Exiting.\n"); kill_announcements(announce); kill_forwarding(forwarding); kill_dht(dht); @@ -393,10 +398,10 @@ int main(int argc, char *argv[]) return 1; } - Onion *onion = new_onion(logger, mem, mono_time, rng, dht); + Onion *onion = new_onion(logger, mem, mono_time, rng, dht, net); if (onion == nullptr) { - log_write(LOG_LEVEL_ERROR, "Couldn't initialize Tox Onion. Exiting.\n"); + LOG_WRITE(LOG_LEVEL_ERROR, "Couldn't initialize Tox Onion. Exiting.\n"); kill_gca(group_announce); kill_announcements(announce); kill_forwarding(forwarding); @@ -410,10 +415,10 @@ int main(int argc, char *argv[]) return 1; } - Onion_Announce *onion_a = new_onion_announce(logger, mem, rng, mono_time, dht); + Onion_Announce *onion_a = new_onion_announce(logger, mem, rng, mono_time, dht, net); if (onion_a == nullptr) { - log_write(LOG_LEVEL_ERROR, "Couldn't initialize Tox Onion Announce. Exiting.\n"); + LOG_WRITE(LOG_LEVEL_ERROR, "Couldn't initialize Tox Onion Announce. Exiting.\n"); kill_gca(group_announce); kill_onion(onion); kill_announcements(announce); @@ -431,11 +436,11 @@ int main(int argc, char *argv[]) gca_onion_init(group_announce, onion_a); if (enable_motd) { - if (bootstrap_set_callbacks(dht_get_net(dht), DAEMON_VERSION_NUMBER, (uint8_t *)motd, strlen(motd) + 1) == 0) { - log_write(LOG_LEVEL_INFO, "Set MOTD successfully.\n"); + if (bootstrap_set_callbacks(net, DAEMON_VERSION_NUMBER, (uint8_t *)motd, strlen(motd) + 1) == 0) { + LOG_WRITE(LOG_LEVEL_INFO, "Set MOTD successfully.\n"); free(motd); } else { - log_write(LOG_LEVEL_ERROR, "Couldn't set MOTD: %s. Exiting.\n", motd); + LOG_WRITE(LOG_LEVEL_ERROR, "Couldn't set MOTD: %s. Exiting.\n", motd); kill_onion_announce(onion_a); kill_gca(group_announce); kill_onion(onion); @@ -453,10 +458,10 @@ int main(int argc, char *argv[]) } if (manage_keys(dht, keys_file_path)) { - log_write(LOG_LEVEL_INFO, "Keys are managed successfully.\n"); + LOG_WRITE(LOG_LEVEL_INFO, "Keys are managed successfully.\n"); free(keys_file_path); } else { - log_write(LOG_LEVEL_ERROR, "Couldn't read/write: %s. Exiting.\n", keys_file_path); + LOG_WRITE(LOG_LEVEL_ERROR, "Couldn't read/write: %s. Exiting.\n", keys_file_path); kill_onion_announce(onion_a); kill_gca(group_announce); kill_onion(onion); @@ -475,7 +480,7 @@ int main(int argc, char *argv[]) if (enable_tcp_relay) { if (tcp_relay_port_count == 0) { - log_write(LOG_LEVEL_ERROR, "No TCP relay ports read. Exiting.\n"); + LOG_WRITE(LOG_LEVEL_ERROR, "No TCP relay ports read. Exiting.\n"); kill_onion_announce(onion_a); kill_gca(group_announce); kill_announcements(announce); @@ -496,7 +501,7 @@ int main(int argc, char *argv[]) free(tcp_relay_ports); if (tcp_server != nullptr) { - log_write(LOG_LEVEL_INFO, "Initialized Tox TCP server successfully.\n"); + LOG_WRITE(LOG_LEVEL_INFO, "Initialized Tox TCP server successfully.\n"); struct rlimit limit; @@ -515,14 +520,14 @@ int main(int argc, char *argv[]) } if (getrlimit(RLIMIT_NOFILE, &limit) == 0 && limit.rlim_cur < rlim_min) { - log_write(LOG_LEVEL_WARNING, + LOG_WRITE(LOG_LEVEL_WARNING, "Current limit on the number of files this process can open (%ju) is rather low for the proper functioning of the TCP server. " "Consider raising the limit to at least %ju or the recommended %ju. " "Continuing using the current limit (%ju).\n", (uintmax_t)limit.rlim_cur, (uintmax_t)rlim_min, (uintmax_t)rlim_suggested, (uintmax_t)limit.rlim_cur); } } else { - log_write(LOG_LEVEL_ERROR, "Couldn't initialize Tox TCP server. Exiting.\n"); + LOG_WRITE(LOG_LEVEL_ERROR, "Couldn't initialize Tox TCP server. Exiting.\n"); kill_onion_announce(onion_a); kill_gca(group_announce); kill_onion(onion); @@ -537,9 +542,9 @@ int main(int argc, char *argv[]) } if (bootstrap_from_config(cfg_file_path, dht, enable_ipv6)) { - log_write(LOG_LEVEL_INFO, "List of bootstrap nodes read successfully.\n"); + LOG_WRITE(LOG_LEVEL_INFO, "List of bootstrap nodes read successfully.\n"); } else { - log_write(LOG_LEVEL_ERROR, "Couldn't read list of bootstrap nodes in %s. Exiting.\n", cfg_file_path); + LOG_WRITE(LOG_LEVEL_ERROR, "Couldn't read list of bootstrap nodes in %s. Exiting.\n", cfg_file_path); kill_tcp_server(tcp_server); kill_onion_announce(onion_a); kill_gca(group_announce); @@ -564,7 +569,7 @@ int main(int argc, char *argv[]) if (enable_lan_discovery) { broadcast = lan_discovery_init(mem, ns); - log_write(LOG_LEVEL_INFO, "Initialized LAN discovery successfully.\n"); + LOG_WRITE(LOG_LEVEL_INFO, "Initialized LAN discovery successfully.\n"); } struct sigaction sa; @@ -578,11 +583,11 @@ int main(int argc, char *argv[]) sigfillset(&sa.sa_mask); if (sigaction(SIGINT, &sa, nullptr) != 0) { - log_write(LOG_LEVEL_WARNING, "Couldn't set signal handler for SIGINT. Continuing without the signal handler set.\n"); + LOG_WRITE(LOG_LEVEL_WARNING, "Couldn't set signal handler for SIGINT. Continuing without the signal handler set.\n"); } if (sigaction(SIGTERM, &sa, nullptr) != 0) { - log_write(LOG_LEVEL_WARNING, "Couldn't set signal handler for SIGTERM. Continuing without the signal handler set.\n"); + LOG_WRITE(LOG_LEVEL_WARNING, "Couldn't set signal handler for SIGTERM. Continuing without the signal handler set.\n"); } while (caught_signal == 0) { @@ -591,7 +596,7 @@ int main(int argc, char *argv[]) do_dht(dht); if (enable_lan_discovery && mono_time_is_timeout(mono_time, last_lan_discovery, LAN_DISCOVERY_INTERVAL)) { - lan_discovery_send(dht_get_net(dht), broadcast, dht_get_self_public_key(dht), net_htons_port); + lan_discovery_send(net, broadcast, dht_get_self_public_key(dht), net_htons_port); last_lan_discovery = mono_time_get(mono_time); } @@ -601,10 +606,10 @@ int main(int argc, char *argv[]) do_tcp_server(tcp_server, mono_time); } - networking_poll(dht_get_net(dht), nullptr); + networking_poll(net, nullptr); if (waiting_for_dht_connection && dht_isconnected(dht)) { - log_write(LOG_LEVEL_INFO, "Connected to another bootstrap node successfully.\n"); + LOG_WRITE(LOG_LEVEL_INFO, "Connected to another bootstrap node successfully.\n"); waiting_for_dht_connection = false; } @@ -613,15 +618,15 @@ int main(int argc, char *argv[]) switch (caught_signal) { case SIGINT: - log_write(LOG_LEVEL_INFO, "Received SIGINT (%d) signal. Exiting.\n", SIGINT); + LOG_WRITE(LOG_LEVEL_INFO, "Received SIGINT (%d) signal. Exiting.\n", SIGINT); break; case SIGTERM: - log_write(LOG_LEVEL_INFO, "Received SIGTERM (%d) signal. Exiting.\n", SIGTERM); + LOG_WRITE(LOG_LEVEL_INFO, "Received SIGTERM (%d) signal. Exiting.\n", SIGTERM); break; default: - log_write(LOG_LEVEL_INFO, "Received (%ld) signal. Exiting.\n", (long)caught_signal); + LOG_WRITE(LOG_LEVEL_INFO, "Received (%ld) signal. Exiting.\n", (long)caught_signal); } lan_discovery_kill(broadcast); diff --git a/external/toxcore/c-toxcore/other/bootstrap_daemon/websocket/Dockerfile b/external/toxcore/c-toxcore/other/bootstrap_daemon/websocket/Dockerfile index 5e48c7e16..7e6d855bd 100644 --- a/external/toxcore/c-toxcore/other/bootstrap_daemon/websocket/Dockerfile +++ b/external/toxcore/c-toxcore/other/bootstrap_daemon/websocket/Dockerfile @@ -1,6 +1,6 @@ # Stage 1 - Compile websockify. FROM toxchat/bootstrap-node:latest AS tox -FROM golang:1.17-alpine AS websockify +FROM golang:1.23-alpine3.21 AS websockify COPY websockify /work/websockify RUN cd /work/websockify && go mod download github.com/gorilla/websocket && go install @@ -25,5 +25,7 @@ USER tox #RUN /usr/local/bin/tox-bootstrapd --config /etc/tox-bootstrapd.conf --log-backend stdout \ # && sleep 1 +COPY tox-bootstrapd.conf /etc/ + WORKDIR /web CMD ["/entrypoint.sh"] diff --git a/external/toxcore/c-toxcore/other/bootstrap_daemon/websocket/build.sh b/external/toxcore/c-toxcore/other/bootstrap_daemon/websocket/build.sh new file mode 100755 index 000000000..39379366c --- /dev/null +++ b/external/toxcore/c-toxcore/other/bootstrap_daemon/websocket/build.sh @@ -0,0 +1,12 @@ +#!/bin/sh + +set -eux -o pipefail + +GIT_ROOT="$(git rev-parse --show-toplevel)" + +# Build bootstrap daemon first. +"$GIT_ROOT/other/bootstrap_daemon/docker/build.sh" + +# Build websocket server. +cd "$GIT_ROOT/other/bootstrap_daemon/websocket" +docker build -t toxchat/bootstrap-node:latest-websocket . diff --git a/external/toxcore/c-toxcore/other/bootstrap_daemon/websocket/entrypoint.sh b/external/toxcore/c-toxcore/other/bootstrap_daemon/websocket/entrypoint.sh index 7284bdb3b..4f215f918 100755 --- a/external/toxcore/c-toxcore/other/bootstrap_daemon/websocket/entrypoint.sh +++ b/external/toxcore/c-toxcore/other/bootstrap_daemon/websocket/entrypoint.sh @@ -2,5 +2,5 @@ set -eux -/usr/local/bin/tox-bootstrapd --config /etc/tox-bootstrapd.conf --log-backend stdout +/usr/local/bin/tox-bootstrapd --config /etc/tox-bootstrapd.conf --log-backend stdout --trace /usr/local/bin/websockify -l "0.0.0.0:$PORT" -t 127.0.0.1:33445 diff --git a/external/toxcore/c-toxcore/other/bootstrap_daemon/websocket/tox-bootstrapd.conf b/external/toxcore/c-toxcore/other/bootstrap_daemon/websocket/tox-bootstrapd.conf new file mode 100644 index 000000000..e1892df2c --- /dev/null +++ b/external/toxcore/c-toxcore/other/bootstrap_daemon/websocket/tox-bootstrapd.conf @@ -0,0 +1,21 @@ +# Tox WebSocket bootstrap daemon configuration file. + +port = 33445 + +keys_file_path = "/var/lib/tox-bootstrapd/keys" + +pid_file_path = "/var/run/tox-bootstrapd/tox-bootstrapd.pid" + +enable_ipv6 = false +enable_ipv4_fallback = true +enable_lan_discovery = false + +enable_tcp_relay = true +tcp_relay_ports = [443, 3389, 33445] + +enable_motd = true + +motd = "tox-bootstrapd" + +# No bootstrap nodes for now, since none of them support WebSocket. +bootstrap_nodes = () diff --git a/external/toxcore/c-toxcore/other/docker/autotools/run b/external/toxcore/c-toxcore/other/docker/autotools/run index 300465b00..c92013fe3 100755 --- a/external/toxcore/c-toxcore/other/docker/autotools/run +++ b/external/toxcore/c-toxcore/other/docker/autotools/run @@ -2,5 +2,5 @@ set -eux BUILD=autotools -other/docker/sources/build +other/docker/sources/build.sh docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" . diff --git a/external/toxcore/c-toxcore/other/docker/clang-tidy/run b/external/toxcore/c-toxcore/other/docker/clang-tidy/run index ab830de0b..9d3abf2a1 100755 --- a/external/toxcore/c-toxcore/other/docker/clang-tidy/run +++ b/external/toxcore/c-toxcore/other/docker/clang-tidy/run @@ -2,5 +2,5 @@ set -eux BUILD=clang-tidy -other/docker/sources/build +other/docker/sources/build.sh docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" . diff --git a/external/toxcore/c-toxcore/other/docker/compcert/run b/external/toxcore/c-toxcore/other/docker/compcert/run index 885f811ae..bf11d09a6 100755 --- a/external/toxcore/c-toxcore/other/docker/compcert/run +++ b/external/toxcore/c-toxcore/other/docker/compcert/run @@ -2,5 +2,5 @@ set -eux BUILD=compcert -other/docker/sources/build +other/docker/sources/build.sh docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" . diff --git a/external/toxcore/c-toxcore/other/docker/coverage/run b/external/toxcore/c-toxcore/other/docker/coverage/run index e4f513391..aa23abfb3 100755 --- a/external/toxcore/c-toxcore/other/docker/coverage/run +++ b/external/toxcore/c-toxcore/other/docker/coverage/run @@ -5,7 +5,7 @@ set -eux read -a ci_env <<<"$(bash <(curl -s https://codecov.io/env))" BUILD=coverage -other/docker/sources/build +other/docker/sources/build.sh docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" . docker run "${ci_env[@]}" -e CI=true --name toxcore-coverage --rm -t toxchat/c-toxcore:coverage /usr/local/bin/codecov diff --git a/external/toxcore/c-toxcore/other/docker/cppcheck/run b/external/toxcore/c-toxcore/other/docker/cppcheck/run index 4cc18bac9..b7853fd56 100755 --- a/external/toxcore/c-toxcore/other/docker/cppcheck/run +++ b/external/toxcore/c-toxcore/other/docker/cppcheck/run @@ -2,5 +2,5 @@ set -eux BUILD=cppcheck -other/docker/sources/build +other/docker/sources/build.sh docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" . diff --git a/external/toxcore/c-toxcore/other/docker/goblint/run b/external/toxcore/c-toxcore/other/docker/goblint/run index f2bbceea7..8c97cb51b 100755 --- a/external/toxcore/c-toxcore/other/docker/goblint/run +++ b/external/toxcore/c-toxcore/other/docker/goblint/run @@ -2,5 +2,5 @@ set -eux BUILD=goblint -other/docker/sources/build +other/docker/sources/build.sh docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" . diff --git a/external/toxcore/c-toxcore/other/docker/infer/run b/external/toxcore/c-toxcore/other/docker/infer/run index f605d1406..a7ae62987 100755 --- a/external/toxcore/c-toxcore/other/docker/infer/run +++ b/external/toxcore/c-toxcore/other/docker/infer/run @@ -2,5 +2,5 @@ set -eux BUILD=infer -other/docker/sources/build +other/docker/sources/build.sh docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" . diff --git a/external/toxcore/c-toxcore/other/docker/misra/run b/external/toxcore/c-toxcore/other/docker/misra/run index a2692a37a..1dec98b66 100755 --- a/external/toxcore/c-toxcore/other/docker/misra/run +++ b/external/toxcore/c-toxcore/other/docker/misra/run @@ -2,5 +2,5 @@ set -eux BUILD=misra -other/docker/sources/build +other/docker/sources/build.sh docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" . diff --git a/external/toxcore/c-toxcore/other/docker/rpm/run b/external/toxcore/c-toxcore/other/docker/rpm/run index ea71cc358..1f40925d0 100755 --- a/external/toxcore/c-toxcore/other/docker/rpm/run +++ b/external/toxcore/c-toxcore/other/docker/rpm/run @@ -6,7 +6,7 @@ PROJECT_COMMIT_ID="$(git rev-parse HEAD)" PROJECT_COMMIT_ID_SHORT="$(git rev-parse --short HEAD)" BUILD=rpm -other/docker/sources/build +other/docker/sources/build.sh docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" . \ --build-arg="PROJECT_VERSION=$PROJECT_VERSION" \ --build-arg="PROJECT_COMMIT_ID=$PROJECT_COMMIT_ID" \ diff --git a/external/toxcore/c-toxcore/other/docker/slimcc/run b/external/toxcore/c-toxcore/other/docker/slimcc/run index 82d711204..e30b329cf 100755 --- a/external/toxcore/c-toxcore/other/docker/slimcc/run +++ b/external/toxcore/c-toxcore/other/docker/slimcc/run @@ -2,5 +2,5 @@ set -eux BUILD=slimcc -other/docker/sources/build +other/docker/sources/build.sh docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" . diff --git a/external/toxcore/c-toxcore/other/docker/sources/build b/external/toxcore/c-toxcore/other/docker/sources/build.sh similarity index 100% rename from external/toxcore/c-toxcore/other/docker/sources/build rename to external/toxcore/c-toxcore/other/docker/sources/build.sh diff --git a/external/toxcore/c-toxcore/other/docker/sparse/run b/external/toxcore/c-toxcore/other/docker/sparse/run index 6b87e91b8..640735510 100755 --- a/external/toxcore/c-toxcore/other/docker/sparse/run +++ b/external/toxcore/c-toxcore/other/docker/sparse/run @@ -2,5 +2,5 @@ set -eux BUILD=sparse -other/docker/sources/build +other/docker/sources/build.sh docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" . diff --git a/external/toxcore/c-toxcore/other/docker/tcc/run b/external/toxcore/c-toxcore/other/docker/tcc/run index 63412b55f..e9e29ff6d 100755 --- a/external/toxcore/c-toxcore/other/docker/tcc/run +++ b/external/toxcore/c-toxcore/other/docker/tcc/run @@ -2,5 +2,5 @@ set -eux BUILD=tcc -other/docker/sources/build +other/docker/sources/build.sh docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" . diff --git a/external/toxcore/c-toxcore/other/docker/tokstyle/run b/external/toxcore/c-toxcore/other/docker/tokstyle/run index 1d92173d6..847cd3223 100755 --- a/external/toxcore/c-toxcore/other/docker/tokstyle/run +++ b/external/toxcore/c-toxcore/other/docker/tokstyle/run @@ -2,5 +2,5 @@ set -eux BUILD=tokstyle -other/docker/sources/build +other/docker/sources/build.sh docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" . diff --git a/external/toxcore/c-toxcore/testing/BUILD.bazel b/external/toxcore/c-toxcore/testing/BUILD.bazel index b8c1a2ba5..5269d7363 100644 --- a/external/toxcore/c-toxcore/testing/BUILD.bazel +++ b/external/toxcore/c-toxcore/testing/BUILD.bazel @@ -87,6 +87,8 @@ cc_binary( "//c-toxcore/toxcore:Messenger", "//c-toxcore/toxcore:ccompat", "//c-toxcore/toxcore:mono_time", + "//c-toxcore/toxcore:os_memory", + "//c-toxcore/toxcore:os_random", ], ) diff --git a/external/toxcore/c-toxcore/testing/Messenger_test.c b/external/toxcore/c-toxcore/testing/Messenger_test.c index 9e6484e86..bbb2a4179 100644 --- a/external/toxcore/c-toxcore/testing/Messenger_test.c +++ b/external/toxcore/c-toxcore/testing/Messenger_test.c @@ -35,6 +35,8 @@ #include "../toxcore/Messenger.h" #include "../toxcore/ccompat.h" #include "../toxcore/mono_time.h" +#include "../toxcore/os_memory.h" +#include "../toxcore/os_random.h" #include "misc_tools.h" static void print_message(Messenger *m, uint32_t friendnumber, unsigned int type, const uint8_t *string, size_t length, diff --git a/external/toxcore/c-toxcore/testing/fuzzing/fuzz_support.cc b/external/toxcore/c-toxcore/testing/fuzzing/fuzz_support.cc index 904d54249..6e43f871b 100644 --- a/external/toxcore/c-toxcore/testing/fuzzing/fuzz_support.cc +++ b/external/toxcore/c-toxcore/testing/fuzzing/fuzz_support.cc @@ -24,7 +24,9 @@ #include "../../toxcore/crypto_core.h" #include "../../toxcore/network.h" +#include "../../toxcore/tox_memory_impl.h" #include "../../toxcore/tox_private.h" +#include "../../toxcore/tox_random_impl.h" #include "func_conversion.hh" // TODO(iphydf): Put this somewhere shared. @@ -33,8 +35,8 @@ struct Network_Addr { size_t size; }; -System::System(std::unique_ptr in_sys, std::unique_ptr in_mem, - std::unique_ptr in_ns, std::unique_ptr in_rng) +System::System(std::unique_ptr in_sys, std::unique_ptr in_mem, + std::unique_ptr in_ns, std::unique_ptr in_rng) : sys(std::move(in_sys)) , mem(std::move(in_mem)) , ns(std::move(in_ns)) @@ -94,22 +96,17 @@ static void *alloc_common(const char *func, std::size_t size, Fuzz_Data &data, A return report_alloc("tox1", func, size, Func(args...)); } -static constexpr Memory_Funcs fuzz_memory_funcs = { +static constexpr Tox_Memory_Funcs fuzz_memory_funcs = { /* .malloc = */ ![](Fuzz_System *self, uint32_t size) { return alloc_common("malloc", size, self->data, size); }, - /* .calloc = */ - ![](Fuzz_System *self, uint32_t nmemb, uint32_t size) { - return alloc_common( - "calloc", nmemb * size, self->data, nmemb, size); - }, /* .realloc = */ ![](Fuzz_System *self, void *ptr, uint32_t size) { return alloc_common( "realloc", size, self->data, ptr, size); }, - /* .free = */ + /* .dealloc = */ ![](Fuzz_System *self, void *ptr) { std::free(ptr); }, }; @@ -172,8 +169,8 @@ static constexpr Network_Funcs fuzz_network_funcs = { }, }; -static constexpr Random_Funcs fuzz_random_funcs = { - /* .random_bytes = */ +static constexpr Tox_Random_Funcs fuzz_random_funcs = { + /* .bytes_callback = */ ![](Fuzz_System *self, uint8_t *bytes, size_t length) { // Initialize the buffer with zeros in case there's no randomness left. std::fill_n(bytes, length, 0); @@ -216,11 +213,11 @@ static constexpr Random_Funcs fuzz_random_funcs = { } } }, - /* .random_uniform = */ + /* .uniform_callback = */ ![](Fuzz_System *self, uint32_t upper_bound) { uint32_t randnum = 0; if (upper_bound > 0) { - self->rng->funcs->random_bytes( + self->rng->funcs->bytes_callback( self, reinterpret_cast(&randnum), sizeof(randnum)); randnum %= upper_bound; } @@ -231,9 +228,9 @@ static constexpr Random_Funcs fuzz_random_funcs = { Fuzz_System::Fuzz_System(Fuzz_Data &input) : System{ std::make_unique(), - std::make_unique(Memory{&fuzz_memory_funcs, this}), + std::make_unique(Tox_Memory{&fuzz_memory_funcs, this}), std::make_unique(Network{&fuzz_network_funcs, this}), - std::make_unique(Random{&fuzz_random_funcs, this}), + std::make_unique(Tox_Random{&fuzz_random_funcs, this}), } , data(input) { @@ -244,14 +241,12 @@ Fuzz_System::Fuzz_System(Fuzz_Data &input) sys->rng = rng.get(); } -static constexpr Memory_Funcs null_memory_funcs = { +static constexpr Tox_Memory_Funcs null_memory_funcs = { /* .malloc = */ ![](Null_System *self, uint32_t size) { return std::malloc(size); }, - /* .calloc = */ - ![](Null_System *self, uint32_t nmemb, uint32_t size) { return std::calloc(nmemb, size); }, /* .realloc = */ ![](Null_System *self, void *ptr, uint32_t size) { return std::realloc(ptr, size); }, - /* .free = */ + /* .dealloc = */ ![](Null_System *self, void *ptr) { std::free(ptr); }, }; @@ -304,14 +299,14 @@ static uint64_t simple_rng(uint64_t &seed) return seed; } -static constexpr Random_Funcs null_random_funcs = { - /* .random_bytes = */ +static constexpr Tox_Random_Funcs null_random_funcs = { + /* .bytes_callback = */ ![](Null_System *self, uint8_t *bytes, size_t length) { for (size_t i = 0; i < length; ++i) { bytes[i] = simple_rng(self->seed) & 0xff; } }, - /* .random_uniform = */ + /* .uniform_callback = */ ![](Null_System *self, uint32_t upper_bound) { return static_cast(simple_rng(self->seed)) % upper_bound; }, @@ -320,9 +315,9 @@ static constexpr Random_Funcs null_random_funcs = { Null_System::Null_System() : System{ std::make_unique(), - std::make_unique(Memory{&null_memory_funcs, this}), + std::make_unique(Tox_Memory{&null_memory_funcs, this}), std::make_unique(Network{&null_network_funcs, this}), - std::make_unique(Random{&null_random_funcs, this}), + std::make_unique(Tox_Random{&null_random_funcs, this}), } { sys->mono_time_callback = [](void *self) { return static_cast(self)->clock; }; @@ -342,23 +337,18 @@ static uint16_t get_port(const Network_Addr *addr) } } -static constexpr Memory_Funcs record_memory_funcs = { +static constexpr Tox_Memory_Funcs record_memory_funcs = { /* .malloc = */ ![](Record_System *self, uint32_t size) { self->push(true); return report_alloc(self->name_, "malloc", size, std::malloc(size)); }, - /* .calloc = */ - ![](Record_System *self, uint32_t nmemb, uint32_t size) { - self->push(true); - return report_alloc(self->name_, "calloc", nmemb * size, std::calloc(nmemb, size)); - }, /* .realloc = */ ![](Record_System *self, void *ptr, uint32_t size) { self->push(true); return report_alloc(self->name_, "realloc", size, std::realloc(ptr, size)); }, - /* .free = */ + /* .dealloc = */ ![](Record_System *self, void *ptr) { std::free(ptr); }, }; @@ -448,8 +438,8 @@ static constexpr Network_Funcs record_network_funcs = { size_t optlen) { return 0; }, }; -static constexpr Random_Funcs record_random_funcs = { - /* .random_bytes = */ +static constexpr Tox_Random_Funcs record_random_funcs = { + /* .bytes_callback = */ ![](Record_System *self, uint8_t *bytes, size_t length) { for (size_t i = 0; i < length; ++i) { bytes[i] = simple_rng(self->seed_) & 0xff; @@ -460,14 +450,14 @@ static constexpr Random_Funcs record_random_funcs = { "%s: rng: %02x..%02x[%zu]\n", self->name_, bytes[0], bytes[length - 1], length); } }, - /* .random_uniform = */ - fuzz_random_funcs.random_uniform, + /* .uniform_callback = */ + fuzz_random_funcs.uniform_callback, }; Record_System::Record_System(Global &global, uint64_t seed, const char *name) : System{ std::make_unique(), - std::make_unique(Memory{&record_memory_funcs, this}), + std::make_unique(Tox_Memory{&record_memory_funcs, this}), std::make_unique(Network{&record_network_funcs, this}), std::make_unique(Random{&record_random_funcs, this}), } diff --git a/external/toxcore/c-toxcore/testing/fuzzing/fuzz_support.hh b/external/toxcore/c-toxcore/testing/fuzzing/fuzz_support.hh index 1e03abf5b..cff634b5e 100644 --- a/external/toxcore/c-toxcore/testing/fuzzing/fuzz_support.hh +++ b/external/toxcore/c-toxcore/testing/fuzzing/fuzz_support.hh @@ -187,9 +187,9 @@ void fuzz_select_target(const uint8_t *data, std::size_t size) return Fuzz_Target_Selector::select(selector, input); } -struct Memory; +struct Tox_Memory; struct Network; -struct Random; +struct Tox_Random; struct System { /** @brief Deterministic system clock for this instance. @@ -205,12 +205,12 @@ struct System { uint64_t clock = 1000000000; std::unique_ptr sys; - std::unique_ptr mem; + std::unique_ptr mem; std::unique_ptr ns; - std::unique_ptr rng; + std::unique_ptr rng; - System(std::unique_ptr sys, std::unique_ptr mem, - std::unique_ptr ns, std::unique_ptr rng); + System(std::unique_ptr sys, std::unique_ptr mem, + std::unique_ptr ns, std::unique_ptr rng); System(System &&); // Not inline because sizeof of the above 2 structs is not known everywhere. diff --git a/external/toxcore/c-toxcore/toxav/BUILD.bazel b/external/toxcore/c-toxcore/toxav/BUILD.bazel index 90fb40d71..f213c5148 100644 --- a/external/toxcore/c-toxcore/toxav/BUILD.bazel +++ b/external/toxcore/c-toxcore/toxav/BUILD.bazel @@ -68,6 +68,19 @@ cc_library( ], ) +cc_test( + name = "rtp_test", + size = "small", + srcs = ["rtp_test.cc"], + deps = [ + ":toxav", + "//c-toxcore/toxcore:crypto_core", + "//c-toxcore/toxcore:os_random", + "@com_google_googletest//:gtest", + "@com_google_googletest//:gtest_main", + ], +) + sh_library( name = "cimple_files", srcs = glob([ diff --git a/external/toxcore/c-toxcore/toxav/rtp_test.cc b/external/toxcore/c-toxcore/toxav/rtp_test.cc index 674bc4b46..24e524480 100644 --- a/external/toxcore/c-toxcore/toxav/rtp_test.cc +++ b/external/toxcore/c-toxcore/toxav/rtp_test.cc @@ -3,6 +3,7 @@ #include #include "../toxcore/crypto_core.h" +#include "../toxcore/os_random.h" namespace { diff --git a/external/toxcore/c-toxcore/toxav/toxav.c b/external/toxcore/c-toxcore/toxav/toxav.c index d11d96c58..f7132bc35 100644 --- a/external/toxcore/c-toxcore/toxav/toxav.c +++ b/external/toxcore/c-toxcore/toxav/toxav.c @@ -77,7 +77,7 @@ typedef struct DecodeTimeStats { } DecodeTimeStats; struct ToxAV { - const Memory *mem; + const struct Tox_Memory *mem; Logger *log; Tox *tox; MSISession *msi; diff --git a/external/toxcore/c-toxcore/toxcore/BUILD.bazel b/external/toxcore/c-toxcore/toxcore/BUILD.bazel index 580d8884f..eb67f237c 100644 --- a/external/toxcore/c-toxcore/toxcore/BUILD.bazel +++ b/external/toxcore/c-toxcore/toxcore/BUILD.bazel @@ -39,6 +39,12 @@ cc_library( visibility = ["//c-toxcore:__subpackages__"], ) +cc_library( + name = "tox_attributes", + hdrs = ["tox_attributes.h"], + visibility = ["//c-toxcore:__subpackages__"], +) + cc_library( name = "ccompat", srcs = ["ccompat.c"], @@ -47,6 +53,59 @@ cc_library( deps = [":attributes"], ) +cc_library( + name = "tox_memory", + srcs = ["tox_memory.c"], + hdrs = [ + "tox_memory.h", + "tox_memory_impl.h", + ], + visibility = ["//c-toxcore:__subpackages__"], + deps = [ + ":ccompat", + ":tox_attributes", + ], +) + +cc_library( + name = "os_memory", + srcs = ["os_memory.c"], + hdrs = ["os_memory.h"], + visibility = ["//c-toxcore:__subpackages__"], + deps = [ + ":attributes", + ":tox_memory", + ], +) + +cc_library( + name = "tox_random", + srcs = ["tox_random.c"], + hdrs = [ + "tox_random.h", + "tox_random_impl.h", + ], + visibility = ["//c-toxcore:__subpackages__"], + deps = [ + ":ccompat", + ":tox_attributes", + ":tox_memory", + ], +) + +cc_library( + name = "os_random", + srcs = ["os_random.c"], + hdrs = ["os_random.h"], + visibility = ["//c-toxcore:__subpackages__"], + deps = [ + ":attributes", + ":ccompat", + ":tox_random", + "@libsodium", + ], +) + cc_library( name = "mem", srcs = ["mem.c"], @@ -55,6 +114,7 @@ cc_library( deps = [ ":attributes", ":ccompat", + ":tox_memory", ], ) @@ -65,7 +125,9 @@ cc_library( hdrs = ["mem_test_util.hh"], deps = [ ":mem", + ":os_memory", ":test_util", + ":tox_memory", ], ) @@ -75,6 +137,7 @@ cc_test( srcs = ["mem_test.cc"], deps = [ ":mem", + ":os_memory", "@com_google_googletest//:gtest", "@com_google_googletest//:gtest_main", ], @@ -208,6 +271,7 @@ cc_test( ":bin_unpack", ":logger", ":mem", + ":os_memory", "@com_google_googletest//:gtest", "@com_google_googletest//:gtest_main", ], @@ -222,6 +286,7 @@ cc_library( ":attributes", ":ccompat", ":mem", + ":tox_random", ":util", "@libsodium", ], @@ -249,6 +314,7 @@ cc_library( deps = [ ":crypto_core", ":test_util", + ":tox_random", ], ) @@ -285,6 +351,7 @@ cc_test( deps = [ ":list", ":mem", + ":os_memory", "@com_google_googletest//:gtest", "@com_google_googletest//:gtest_main", ], @@ -372,8 +439,14 @@ cc_library( cc_library( name = "network", - srcs = ["network.c"], - hdrs = ["network.h"], + srcs = [ + "net_log.c", + "network.c", + ], + hdrs = [ + "net_log.h", + "network.h", + ], visibility = [ "//c-toxcore/auto_tests:__pkg__", "//c-toxcore/other:__pkg__", @@ -1148,6 +1221,8 @@ cc_library( ":net_profile", ":network", ":onion_client", + ":os_memory", + ":os_random", ":state", ":tox_log_level", ":tox_options", @@ -1163,6 +1238,7 @@ cc_test( srcs = ["tox_test.cc"], deps = [ ":crypto_core", + ":os_random", ":tox", ":tox_log_level", ":tox_options", diff --git a/external/toxcore/c-toxcore/toxcore/DHT.c b/external/toxcore/c-toxcore/toxcore/DHT.c index 025de9678..58341bdf8 100644 --- a/external/toxcore/c-toxcore/toxcore/DHT.c +++ b/external/toxcore/c-toxcore/toxcore/DHT.c @@ -25,7 +25,6 @@ #include "shared_key_cache.h" #include "sort.h" #include "state.h" -#include "util.h" /** The timeout after which a node is discarded completely. */ #define KILL_NODE_TIMEOUT (BAD_NODE_TIMEOUT + PING_INTERVAL) @@ -161,10 +160,6 @@ void dht_set_self_secret_key(DHT *dht, const uint8_t *key) memcpy(dht->self_secret_key, key, CRYPTO_SECRET_KEY_SIZE); } -Networking_Core *dht_get_net(const DHT *dht) -{ - return dht->net; -} struct Ping *dht_get_ping(const DHT *dht) { return dht->ping; @@ -2509,7 +2504,7 @@ DHT *new_dht(const Logger *log, const Memory *mem, const Random *rng, const Netw dht->hole_punching_enabled = hole_punching_enabled; dht->lan_discovery_enabled = lan_discovery_enabled; - dht->ping = ping_new(mem, mono_time, rng, dht); + dht->ping = ping_new(mem, mono_time, rng, dht, net); if (dht->ping == nullptr) { LOGGER_ERROR(log, "failed to initialise ping"); diff --git a/external/toxcore/c-toxcore/toxcore/DHT.h b/external/toxcore/c-toxcore/toxcore/DHT.h index 43ddbadb0..6279053f2 100644 --- a/external/toxcore/c-toxcore/toxcore/DHT.h +++ b/external/toxcore/c-toxcore/toxcore/DHT.h @@ -222,7 +222,6 @@ const uint8_t *_Nonnull dht_get_self_secret_key(const DHT *_Nonnull dht); void dht_set_self_public_key(DHT *_Nonnull dht, const uint8_t *_Nonnull key); void dht_set_self_secret_key(DHT *_Nonnull dht, const uint8_t *_Nonnull key); -Networking_Core *_Nonnull dht_get_net(const DHT *_Nonnull dht); struct Ping *_Nonnull dht_get_ping(const DHT *_Nonnull dht); const Client_data *_Nonnull dht_get_close_clientlist(const DHT *_Nonnull dht); const Client_data *_Nonnull dht_get_close_client(const DHT *_Nonnull dht, uint32_t client_num); diff --git a/external/toxcore/c-toxcore/toxcore/Makefile.inc b/external/toxcore/c-toxcore/toxcore/Makefile.inc index de9227b6d..ba373f573 100644 --- a/external/toxcore/c-toxcore/toxcore/Makefile.inc +++ b/external/toxcore/c-toxcore/toxcore/Makefile.inc @@ -100,6 +100,8 @@ libtoxcore_la_SOURCES = ../third_party/cmp/cmp.c \ ../toxcore/mono_time.h \ ../toxcore/net_crypto.c \ ../toxcore/net_crypto.h \ + ../toxcore/net_log.c \ + ../toxcore/net_log.h \ ../toxcore/net_profile.c \ ../toxcore/net_profile.h \ ../toxcore/network.c \ @@ -110,6 +112,10 @@ libtoxcore_la_SOURCES = ../third_party/cmp/cmp.c \ ../toxcore/onion_client.h \ ../toxcore/onion.c \ ../toxcore/onion.h \ + ../toxcore/os_memory.c \ + ../toxcore/os_memory.h \ + ../toxcore/os_random.c \ + ../toxcore/os_random.h \ ../toxcore/ping_array.c \ ../toxcore/ping_array.h \ ../toxcore/ping.c \ @@ -131,6 +137,7 @@ libtoxcore_la_SOURCES = ../third_party/cmp/cmp.c \ ../toxcore/timed_auth.c \ ../toxcore/timed_auth.h \ ../toxcore/tox_api.c \ + ../toxcore/tox_attributes.h \ ../toxcore/tox_dispatch.c \ ../toxcore/tox_dispatch.h \ ../toxcore/tox_event.c \ @@ -139,12 +146,18 @@ libtoxcore_la_SOURCES = ../third_party/cmp/cmp.c \ ../toxcore/tox_events.h \ ../toxcore/tox_log_level.c \ ../toxcore/tox_log_level.h \ + ../toxcore/tox_memory.c \ + ../toxcore/tox_memory.h \ + ../toxcore/tox_memory_impl.h \ ../toxcore/tox_options.c \ ../toxcore/tox_options.h \ ../toxcore/tox_pack.c \ ../toxcore/tox_pack.h \ ../toxcore/tox_private.c \ ../toxcore/tox_private.h \ + ../toxcore/tox_random.c \ + ../toxcore/tox_random.h \ + ../toxcore/tox_random_impl.h \ ../toxcore/tox_struct.h \ ../toxcore/tox_unpack.c \ ../toxcore/tox_unpack.h \ diff --git a/external/toxcore/c-toxcore/toxcore/Messenger.c b/external/toxcore/c-toxcore/toxcore/Messenger.c index a04e1f639..8902311fc 100644 --- a/external/toxcore/c-toxcore/toxcore/Messenger.c +++ b/external/toxcore/c-toxcore/toxcore/Messenger.c @@ -3443,7 +3443,7 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random * return nullptr; } - m->net_crypto = new_net_crypto(m->log, m->mem, m->rng, m->ns, m->mono_time, m->dht, &options->proxy_info, m->tcp_np); + m->net_crypto = new_net_crypto(m->log, m->mem, m->rng, m->ns, m->mono_time, m->net, m->dht, &options->proxy_info, m->tcp_np); if (m->net_crypto == nullptr) { LOGGER_WARNING(m->log, "net_crypto initialisation failed"); @@ -3473,9 +3473,9 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random * } if (options->dht_announcements_enabled) { - m->forwarding = new_forwarding(m->log, m->mem, m->rng, m->mono_time, m->dht); + m->forwarding = new_forwarding(m->log, m->mem, m->rng, m->mono_time, m->dht, m->net); if (m->forwarding != nullptr) { - m->announce = new_announcements(m->log, m->mem, m->rng, m->mono_time, m->forwarding); + m->announce = new_announcements(m->log, m->mem, m->rng, m->mono_time, m->forwarding, m->dht, m->net); } else { m->announce = nullptr; } @@ -3484,11 +3484,11 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random * m->announce = nullptr; } - m->onion = new_onion(m->log, m->mem, m->mono_time, m->rng, m->dht); - 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); + m->onion = new_onion(m->log, m->mem, m->mono_time, m->rng, m->dht, m->net); + m->onion_a = new_onion_announce(m->log, m->mem, m->rng, m->mono_time, m->dht, m->net); + m->onion_c = new_onion_client(m->log, m->mem, m->rng, m->mono_time, m->net_crypto, m->dht, m->net); if (m->onion_c != nullptr) { - m->fr_c = new_friend_connections(m->log, m->mem, 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, m->dht, m->net_crypto, m->net, options->local_discovery_enabled); } if ((options->dht_announcements_enabled && (m->forwarding == nullptr || m->announce == nullptr)) || diff --git a/external/toxcore/c-toxcore/toxcore/announce.c b/external/toxcore/c-toxcore/toxcore/announce.c index b8dfb5662..382e01880 100644 --- a/external/toxcore/c-toxcore/toxcore/announce.c +++ b/external/toxcore/c-toxcore/toxcore/announce.c @@ -613,7 +613,7 @@ static int handle_dht_announce_request( } Announcements *new_announcements(const Logger *log, const Memory *mem, const Random *rng, const Mono_Time *mono_time, - Forwarding *forwarding) + Forwarding *forwarding, DHT *dht, Networking_Core *net) { if (log == nullptr || mono_time == nullptr || forwarding == nullptr) { return nullptr; @@ -630,8 +630,8 @@ Announcements *new_announcements(const Logger *log, const Memory *mem, const Ran announce->rng = rng; announce->forwarding = forwarding; announce->mono_time = mono_time; - announce->dht = forwarding_get_dht(forwarding); - announce->net = dht_get_net(announce->dht); + announce->dht = dht; + announce->net = net; announce->public_key = dht_get_self_public_key(announce->dht); announce->secret_key = dht_get_self_secret_key(announce->dht); new_hmac_key(announce->rng, announce->hmac_key); diff --git a/external/toxcore/c-toxcore/toxcore/announce.h b/external/toxcore/c-toxcore/toxcore/announce.h index 27b2f7855..2c1183c4a 100644 --- a/external/toxcore/c-toxcore/toxcore/announce.h +++ b/external/toxcore/c-toxcore/toxcore/announce.h @@ -7,12 +7,14 @@ #include +#include "DHT.h" #include "attributes.h" #include "crypto_core.h" #include "forwarding.h" #include "logger.h" #include "mem.h" #include "mono_time.h" +#include "network.h" #define MAX_ANNOUNCEMENT_SIZE 512 @@ -22,7 +24,8 @@ uint8_t announce_response_of_request_type(uint8_t request_type); typedef struct Announcements Announcements; -Announcements *_Nullable new_announcements(const Logger *_Nonnull log, const Memory *_Nonnull mem, const Random *_Nonnull rng, const Mono_Time *_Nonnull mono_time, Forwarding *_Nonnull forwarding); +Announcements *_Nullable new_announcements(const Logger *_Nonnull log, const Memory *_Nonnull mem, const Random *_Nonnull rng, const Mono_Time *_Nonnull mono_time, Forwarding *_Nonnull forwarding, + DHT *_Nonnull dht, Networking_Core *_Nonnull net); /** * @brief If data is stored, run `on_retrieve_callback` on it. diff --git a/external/toxcore/c-toxcore/toxcore/bin_pack_test.cc b/external/toxcore/c-toxcore/toxcore/bin_pack_test.cc index c46719c3a..b17cfa80c 100644 --- a/external/toxcore/c-toxcore/toxcore/bin_pack_test.cc +++ b/external/toxcore/c-toxcore/toxcore/bin_pack_test.cc @@ -7,6 +7,7 @@ #include "bin_unpack.h" #include "logger.h" #include "mem.h" +#include "os_memory.h" namespace { diff --git a/external/toxcore/c-toxcore/toxcore/crypto_core.c b/external/toxcore/c-toxcore/toxcore/crypto_core.c index 9f140aca1..49019c918 100644 --- a/external/toxcore/c-toxcore/toxcore/crypto_core.c +++ b/external/toxcore/c-toxcore/toxcore/crypto_core.c @@ -13,6 +13,7 @@ #include "attributes.h" #include "ccompat.h" #include "mem.h" +#include "tox_random.h" #include "util.h" static_assert(CRYPTO_PUBLIC_KEY_SIZE == crypto_box_PUBLICKEYBYTES, @@ -204,7 +205,7 @@ uint64_t random_u64(const Random *rng) uint32_t random_range_u32(const Random *rng, uint32_t upper_bound) { - return rng->funcs->random_uniform(rng->obj, upper_bound); + return tox_random_uniform(rng, upper_bound); } bool crypto_signature_create(uint8_t signature[CRYPTO_SIGNATURE_SIZE], @@ -493,39 +494,7 @@ void crypto_sha512(uint8_t hash[CRYPTO_SHA512_SIZE], const uint8_t *data, size_t #endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */ } -static void sys_random_bytes(void *_Nonnull obj, uint8_t *_Nonnull bytes, size_t length) -{ - randombytes(bytes, length); -} - -static uint32_t sys_random_uniform(void *_Nonnull obj, uint32_t upper_bound) -{ - return randombytes_uniform(upper_bound); -} - -static const Random_Funcs os_random_funcs = { - sys_random_bytes, - sys_random_uniform, -}; - -static const Random os_random_obj = {&os_random_funcs}; - -const Random *os_random(void) -{ -#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION - if ((true)) { - return nullptr; - } -#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */ - // It is safe to call this function more than once and from different - // threads -- subsequent calls won't have any effects. - if (sodium_init() == -1) { - return nullptr; - } - return &os_random_obj; -} - void random_bytes(const Random *rng, uint8_t *bytes, size_t length) { - rng->funcs->random_bytes(rng->obj, bytes, length); + tox_random_bytes(rng, bytes, length); } diff --git a/external/toxcore/c-toxcore/toxcore/crypto_core.h b/external/toxcore/c-toxcore/toxcore/crypto_core.h index 82b31e393..99b976ae1 100644 --- a/external/toxcore/c-toxcore/toxcore/crypto_core.h +++ b/external/toxcore/c-toxcore/toxcore/crypto_core.h @@ -17,6 +17,7 @@ #include "attributes.h" #include "mem.h" +#include "tox_random.h" #ifdef __cplusplus extern "C" { @@ -78,44 +79,6 @@ extern "C" { */ #define CRYPTO_SHA512_SIZE 64 -/** @brief Fill a byte array with random bytes. - * - * This is the key generator callback and as such must be a cryptographically - * secure pseudo-random number generator (CSPRNG). The security of Tox heavily - * depends on the security of this RNG. - */ -typedef void crypto_random_bytes_cb(void *_Nullable obj, uint8_t *_Nonnull bytes, size_t length); - -/** @brief Generate a random integer between 0 and @p upper_bound. - * - * Should produce a uniform random distribution, but Tox security does not - * depend on this being correct. In principle, it could even be a non-CSPRNG. - */ -typedef uint32_t crypto_random_uniform_cb(void *_Nullable obj, uint32_t upper_bound); - -/** @brief Virtual function table for Random. */ -typedef struct Random_Funcs { - crypto_random_bytes_cb *_Nullable random_bytes; - crypto_random_uniform_cb *_Nullable random_uniform; -} Random_Funcs; - -/** @brief Random number generator object. - * - * Can be used by test code and fuzzers to make toxcore behave in specific - * well-defined (non-random) ways. Production code ought to use libsodium's - * CSPRNG and use `os_random` below. - */ -typedef struct Random { - const Random_Funcs *_Nullable funcs; - void *_Nullable obj; -} Random; - -/** @brief System random number generator. - * - * Uses libsodium's CSPRNG (on Linux, `/dev/urandom`). - */ -const Random *_Nullable os_random(void); - /** * @brief The number of bytes in an encryption public key used by DHT group chats. */ @@ -227,6 +190,11 @@ bool crypto_sha512_eq(const uint8_t cksum1[_Nonnull CRYPTO_SHA512_SIZE], const u */ bool crypto_sha256_eq(const uint8_t cksum1[_Nonnull CRYPTO_SHA256_SIZE], const uint8_t cksum2[_Nonnull CRYPTO_SHA256_SIZE]); +/** + * @brief Shorter internal name for the RNG type. + */ +typedef Tox_Random Random; + /** * @brief Return a random 8 bit integer. */ diff --git a/external/toxcore/c-toxcore/toxcore/crypto_core_test_util.cc b/external/toxcore/c-toxcore/toxcore/crypto_core_test_util.cc index b48871a53..19d8ff949 100644 --- a/external/toxcore/c-toxcore/toxcore/crypto_core_test_util.cc +++ b/external/toxcore/c-toxcore/toxcore/crypto_core_test_util.cc @@ -5,15 +5,16 @@ #include "crypto_core.h" #include "test_util.hh" +#include "tox_random_impl.h" -Random_Funcs const Random_Class::vtable = { - Method::invoke<&Random_Class::random_bytes>, - Method::invoke<&Random_Class::random_uniform>, +Tox_Random_Funcs const Random_Class::vtable = { + Method::invoke<&Random_Class::random_bytes>, + Method::invoke<&Random_Class::random_uniform>, }; Random_Class::~Random_Class() = default; -void Test_Random::random_bytes(void *obj, uint8_t *bytes, size_t length) +void Test_Random::random_bytes(void *obj, uint8_t *bytes, uint32_t length) { std::generate(bytes, &bytes[length], std::ref(lcg)); } diff --git a/external/toxcore/c-toxcore/toxcore/crypto_core_test_util.hh b/external/toxcore/c-toxcore/toxcore/crypto_core_test_util.hh index 91a9d68ee..53b9b07d1 100644 --- a/external/toxcore/c-toxcore/toxcore/crypto_core_test_util.hh +++ b/external/toxcore/c-toxcore/toxcore/crypto_core_test_util.hh @@ -8,12 +8,13 @@ #include "crypto_core.h" #include "test_util.hh" +#include "tox_random_impl.h" struct Random_Class { - static Random_Funcs const vtable; - Random const self; + static Tox_Random_Funcs const vtable; + Tox_Random const self; - operator Random const *() const { return &self; } + operator Tox_Random const *() const { return &self; } Random_Class(Random_Class const &) = default; Random_Class() @@ -22,8 +23,8 @@ struct Random_Class { } virtual ~Random_Class(); - virtual crypto_random_bytes_cb random_bytes = 0; - virtual crypto_random_uniform_cb random_uniform = 0; + virtual tox_random_bytes_cb random_bytes = 0; + virtual tox_random_uniform_cb random_uniform = 0; }; /** @@ -35,7 +36,7 @@ struct Random_Class { class Test_Random : public Random_Class { std::minstd_rand lcg; - void random_bytes(void *obj, uint8_t *bytes, size_t length) override; + void random_bytes(void *obj, uint8_t *bytes, uint32_t length) override; uint32_t random_uniform(void *obj, uint32_t upper_bound) override; }; @@ -83,6 +84,6 @@ inline bool operator==(PublicKey::Base const &pk1, PublicKey const &pk2) std::ostream &operator<<(std::ostream &out, PublicKey const &pk); -PublicKey random_pk(const Random *rng); +PublicKey random_pk(const Tox_Random *rng); #endif // C_TOXCORE_TOXCORE_CRYPTO_CORE_TEST_UTIL_H diff --git a/external/toxcore/c-toxcore/toxcore/events/events_alloc.h b/external/toxcore/c-toxcore/toxcore/events/events_alloc.h index ce0518380..b131002de 100644 --- a/external/toxcore/c-toxcore/toxcore/events/events_alloc.h +++ b/external/toxcore/c-toxcore/toxcore/events/events_alloc.h @@ -6,7 +6,6 @@ #define C_TOXCORE_TOXCORE_EVENTS_EVENTS_ALLOC_H #include "../attributes.h" -#include "../mem.h" #include "../tox.h" #include "../tox_events.h" #include "../tox_private.h" @@ -15,17 +14,19 @@ extern "C" { #endif +struct Tox_Memory; + struct Tox_Events { Tox_Event *_Nonnull events; uint32_t events_size; uint32_t events_capacity; - const Memory *_Nonnull mem; + const struct Tox_Memory *_Nonnull mem; }; typedef struct Tox_Events_State { Tox_Err_Events_Iterate error; - const Memory *_Nonnull mem; + const struct Tox_Memory *_Nonnull mem; Tox_Events *_Nonnull events; } Tox_Events_State; diff --git a/external/toxcore/c-toxcore/toxcore/forwarding.c b/external/toxcore/c-toxcore/toxcore/forwarding.c index 4c7cf9117..5b29c8e76 100644 --- a/external/toxcore/c-toxcore/toxcore/forwarding.c +++ b/external/toxcore/c-toxcore/toxcore/forwarding.c @@ -37,11 +37,6 @@ struct Forwarding { void *forwarded_response_callback_object; }; -DHT *forwarding_get_dht(const Forwarding *forwarding) -{ - return forwarding->dht; -} - #define SENDBACK_TIMEOUT 3600 bool send_forward_request(const Networking_Core *net, const IP_Port *forwarder, @@ -348,7 +343,8 @@ void set_callback_forward_reply(Forwarding *forwarding, forward_reply_cb *functi forwarding->forward_reply_callback_object = object; } -Forwarding *_Nullable new_forwarding(const Logger *log, const Memory *mem, const Random *rng, const Mono_Time *mono_time, DHT *dht) +Forwarding *_Nullable new_forwarding(const Logger *log, const Memory *mem, const Random *rng, const Mono_Time *mono_time, DHT *dht, + Networking_Core *net) { if (log == nullptr || mono_time == nullptr || dht == nullptr) { return nullptr; @@ -365,7 +361,7 @@ Forwarding *_Nullable new_forwarding(const Logger *log, const Memory *mem, const forwarding->rng = rng; forwarding->mono_time = mono_time; forwarding->dht = dht; - forwarding->net = dht_get_net(dht); + forwarding->net = net; networking_registerhandler(forwarding->net, NET_PACKET_FORWARD_REQUEST, &handle_forward_request, forwarding); networking_registerhandler(forwarding->net, NET_PACKET_FORWARD_REPLY, &handle_forward_reply, forwarding); diff --git a/external/toxcore/c-toxcore/toxcore/forwarding.h b/external/toxcore/c-toxcore/toxcore/forwarding.h index ac533b3ab..8cef884a4 100644 --- a/external/toxcore/c-toxcore/toxcore/forwarding.h +++ b/external/toxcore/c-toxcore/toxcore/forwarding.h @@ -30,8 +30,6 @@ extern "C" { typedef struct Forwarding Forwarding; -DHT *_Nonnull forwarding_get_dht(const Forwarding *_Nonnull forwarding); - /** * @brief Send data to forwarder for forwarding via chain of dht nodes. * Destination is last key in the chain. @@ -100,7 +98,8 @@ typedef bool forward_reply_cb(void *_Nullable object, const uint8_t *_Nullable s * sendback. */ void set_callback_forward_reply(Forwarding *_Nonnull forwarding, forward_reply_cb *_Nullable function, void *_Nullable object); -Forwarding *_Nullable new_forwarding(const Logger *_Nonnull log, const Memory *_Nonnull mem, const Random *_Nonnull rng, const Mono_Time *_Nonnull mono_time, DHT *_Nonnull dht); +Forwarding *_Nullable new_forwarding(const Logger *_Nonnull log, const Memory *_Nonnull mem, const Random *_Nonnull rng, const Mono_Time *_Nonnull mono_time, DHT *_Nonnull dht, + Networking_Core *_Nonnull net); void kill_forwarding(Forwarding *_Nullable forwarding); #ifdef __cplusplus diff --git a/external/toxcore/c-toxcore/toxcore/friend_connection.c b/external/toxcore/c-toxcore/toxcore/friend_connection.c index e3464e5e6..bab78f874 100644 --- a/external/toxcore/c-toxcore/toxcore/friend_connection.c +++ b/external/toxcore/c-toxcore/toxcore/friend_connection.c @@ -71,6 +71,7 @@ struct Friend_Connections { const Mono_Time *mono_time; const Memory *mem; const Logger *logger; + Networking_Core *net; Net_Crypto *net_crypto; DHT *dht; Broadcast_Info *broadcast; @@ -896,7 +897,8 @@ 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 Memory *mem, const Mono_Time *mono_time, const Network *ns, - Onion_Client *onion_c, bool local_discovery_enabled) + Onion_Client *onion_c, DHT *dht, Net_Crypto *net_crypto, Networking_Core *net, + bool local_discovery_enabled) { if (onion_c == nullptr) { return nullptr; @@ -922,8 +924,9 @@ 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); + temp->dht = dht; + temp->net = net; + temp->net_crypto = net_crypto; temp->onion_c = onion_c; // Don't include default port in port range temp->next_lan_port = TOX_PORTRANGE_FROM + 1; @@ -942,12 +945,12 @@ static void lan_discovery(Friend_Connections *_Nonnull fr_c) last = last > TOX_PORTRANGE_TO ? TOX_PORTRANGE_TO : last; // Always send to default port - lan_discovery_send(dht_get_net(fr_c->dht), fr_c->broadcast, dht_get_self_public_key(fr_c->dht), + lan_discovery_send(fr_c->net, fr_c->broadcast, dht_get_self_public_key(fr_c->dht), net_htons(TOX_PORT_DEFAULT)); // And check some extra ports for (uint16_t port = first; port < last; ++port) { - lan_discovery_send(dht_get_net(fr_c->dht), fr_c->broadcast, dht_get_self_public_key(fr_c->dht), net_htons(port)); + lan_discovery_send(fr_c->net, fr_c->broadcast, dht_get_self_public_key(fr_c->dht), net_htons(port)); } // Don't include default port in port range diff --git a/external/toxcore/c-toxcore/toxcore/friend_connection.h b/external/toxcore/c-toxcore/toxcore/friend_connection.h index 8ab02ee2a..a6d09f0e2 100644 --- a/external/toxcore/c-toxcore/toxcore/friend_connection.h +++ b/external/toxcore/c-toxcore/toxcore/friend_connection.h @@ -145,7 +145,8 @@ void set_friend_request_callback(Friend_Connections *_Nonnull fr_c, fr_request_c /** Create new friend_connections instance. */ Friend_Connections *_Nullable new_friend_connections(const Logger *_Nonnull logger, const Memory *_Nonnull mem, const Mono_Time *_Nonnull mono_time, const Network *_Nonnull ns, - Onion_Client *_Nonnull onion_c, bool local_discovery_enabled); + Onion_Client *_Nonnull onion_c, DHT *_Nonnull dht, Net_Crypto *_Nonnull net_crypto, Networking_Core *_Nonnull net, + bool local_discovery_enabled); /** main friend_connections loop. */ void do_friend_connections(Friend_Connections *_Nonnull fr_c, void *_Nonnull userdata); diff --git a/external/toxcore/c-toxcore/toxcore/list_test.cc b/external/toxcore/c-toxcore/toxcore/list_test.cc index 794c5e233..7cf3efe1b 100644 --- a/external/toxcore/c-toxcore/toxcore/list_test.cc +++ b/external/toxcore/c-toxcore/toxcore/list_test.cc @@ -3,6 +3,7 @@ #include #include "mem.h" +#include "os_memory.h" namespace { diff --git a/external/toxcore/c-toxcore/toxcore/mem.c b/external/toxcore/c-toxcore/toxcore/mem.c index 31aa4c9e5..ce33fb4a0 100644 --- a/external/toxcore/c-toxcore/toxcore/mem.c +++ b/external/toxcore/c-toxcore/toxcore/mem.c @@ -5,59 +5,29 @@ #include "mem.h" -#include +#include -#include "attributes.h" #include "ccompat.h" - -static void *sys_malloc(void *_Nullable obj, uint32_t size) -{ - return malloc(size); -} - -static void *sys_calloc(void *_Nullable obj, uint32_t nmemb, uint32_t size) -{ - return calloc(nmemb, size); -} - -static void *sys_realloc(void *_Nullable obj, void *_Nullable ptr, uint32_t size) -{ - return realloc(ptr, size); -} - -static void sys_free(void *_Nullable obj, void *_Nullable ptr) -{ - free(ptr); -} - -static const Memory_Funcs os_memory_funcs = { - sys_malloc, - sys_calloc, - sys_realloc, - sys_free, -}; -static const Memory os_memory_obj = {&os_memory_funcs}; - -const Memory *os_memory(void) -{ - return &os_memory_obj; -} +#include "tox_memory.h" void *mem_balloc(const Memory *mem, uint32_t size) { - void *const ptr = mem->funcs->malloc(mem->obj, size); + void *const ptr = tox_memory_malloc(mem, 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); + void *const new_ptr = tox_memory_realloc(mem, ptr, size); return new_ptr; } void *mem_alloc(const Memory *mem, uint32_t size) { - void *const ptr = mem->funcs->calloc(mem->obj, 1, size); + void *const ptr = tox_memory_malloc(mem, size); + if (ptr != nullptr) { + memset(ptr, 0, size); + } return ptr; } @@ -69,7 +39,10 @@ void *mem_valloc(const Memory *mem, uint32_t nmemb, uint32_t size) return nullptr; } - void *const ptr = mem->funcs->calloc(mem->obj, nmemb, size); + void *const ptr = tox_memory_malloc(mem, bytes); + if (ptr != nullptr) { + memset(ptr, 0, bytes); + } return ptr; } @@ -81,11 +54,11 @@ void *mem_vrealloc(const Memory *mem, void *ptr, uint32_t nmemb, uint32_t size) return nullptr; } - void *const new_ptr = mem->funcs->realloc(mem->obj, ptr, bytes); + void *const new_ptr = tox_memory_realloc(mem, ptr, bytes); return new_ptr; } void mem_delete(const Memory *mem, void *ptr) { - mem->funcs->free(mem->obj, ptr); + tox_memory_dealloc(mem, ptr); } diff --git a/external/toxcore/c-toxcore/toxcore/mem.h b/external/toxcore/c-toxcore/toxcore/mem.h index a8a4eff99..6784525bd 100644 --- a/external/toxcore/c-toxcore/toxcore/mem.h +++ b/external/toxcore/c-toxcore/toxcore/mem.h @@ -12,30 +12,13 @@ #include // uint*_t #include "attributes.h" +#include "tox_memory.h" #ifdef __cplusplus extern "C" { #endif -typedef void *mem_malloc_cb(void *_Nullable obj, uint32_t size); -typedef void *mem_calloc_cb(void *_Nullable obj, uint32_t nmemb, uint32_t size); -typedef void *mem_realloc_cb(void *_Nullable obj, void *_Nullable ptr, uint32_t size); -typedef void mem_free_cb(void *_Nullable obj, void *_Nullable ptr); - -/** @brief Functions wrapping standard C memory allocation functions. */ -typedef struct Memory_Funcs { - mem_malloc_cb *_Nullable malloc; - mem_calloc_cb *_Nullable calloc; - mem_realloc_cb *_Nullable realloc; - mem_free_cb *_Nullable free; -} Memory_Funcs; - -typedef struct Memory { - const Memory_Funcs *_Nullable funcs; - void *_Nullable obj; -} Memory; - -const Memory *_Nullable os_memory(void); +typedef Tox_Memory Memory; /** * @brief Allocate an array of a given size for built-in types. diff --git a/external/toxcore/c-toxcore/toxcore/mem_test.cc b/external/toxcore/c-toxcore/toxcore/mem_test.cc index 5b649e211..c0e88831b 100644 --- a/external/toxcore/c-toxcore/toxcore/mem_test.cc +++ b/external/toxcore/c-toxcore/toxcore/mem_test.cc @@ -2,6 +2,8 @@ #include +#include "os_memory.h" + namespace { TEST(Mem, AllocLarge) diff --git a/external/toxcore/c-toxcore/toxcore/mem_test_util.cc b/external/toxcore/c-toxcore/toxcore/mem_test_util.cc index 02ade8fc6..e62887103 100644 --- a/external/toxcore/c-toxcore/toxcore/mem_test_util.cc +++ b/external/toxcore/c-toxcore/toxcore/mem_test_util.cc @@ -2,28 +2,28 @@ #include -#include "mem.h" #include "test_util.hh" +#include "tox_memory_impl.h" -Memory_Funcs const Memory_Class::vtable = { - Method::invoke<&Memory_Class::malloc>, - Method::invoke<&Memory_Class::calloc>, - Method::invoke<&Memory_Class::realloc>, - Method::invoke<&Memory_Class::free>, +Tox_Memory_Funcs const Memory_Class::vtable = { + Method::invoke<&Memory_Class::malloc>, + Method::invoke<&Memory_Class::realloc>, + Method::invoke<&Memory_Class::dealloc>, }; Memory_Class::~Memory_Class() = default; -void *Test_Memory::malloc(void *obj, uint32_t size) { return mem->funcs->malloc(mem->obj, size); } - -void *Test_Memory::calloc(void *obj, uint32_t nmemb, uint32_t size) +void *Test_Memory::malloc(void *obj, uint32_t size) { - return mem->funcs->calloc(mem->obj, nmemb, size); + return mem->funcs->malloc_callback(mem->user_data, size); } void *Test_Memory::realloc(void *obj, void *ptr, uint32_t size) { - return mem->funcs->realloc(mem->obj, ptr, size); + return mem->funcs->realloc_callback(mem->user_data, ptr, size); } -void Test_Memory::free(void *obj, void *ptr) { return mem->funcs->free(mem->obj, ptr); } +void Test_Memory::dealloc(void *obj, void *ptr) +{ + return mem->funcs->dealloc_callback(mem->user_data, ptr); +} diff --git a/external/toxcore/c-toxcore/toxcore/mem_test_util.hh b/external/toxcore/c-toxcore/toxcore/mem_test_util.hh index 03094eb57..7a02a22e3 100644 --- a/external/toxcore/c-toxcore/toxcore/mem_test_util.hh +++ b/external/toxcore/c-toxcore/toxcore/mem_test_util.hh @@ -2,13 +2,15 @@ #define C_TOXCORE_TOXCORE_MEM_TEST_UTIL_H #include "mem.h" +#include "os_memory.h" #include "test_util.hh" +#include "tox_memory_impl.h" struct Memory_Class { - static Memory_Funcs const vtable; - Memory const self; + static Tox_Memory_Funcs const vtable; + Tox_Memory const self; - operator Memory const *() const { return &self; } + operator Tox_Memory const *() const { return &self; } Memory_Class(Memory_Class const &) = default; Memory_Class() @@ -17,10 +19,9 @@ struct Memory_Class { } virtual ~Memory_Class(); - virtual mem_malloc_cb malloc = 0; - virtual mem_calloc_cb calloc = 0; - virtual mem_realloc_cb realloc = 0; - virtual mem_free_cb free = 0; + virtual tox_memory_malloc_cb malloc = 0; + virtual tox_memory_realloc_cb realloc = 0; + virtual tox_memory_dealloc_cb dealloc = 0; }; /** @@ -28,12 +29,11 @@ struct Memory_Class { * subclassed to override individual (or all) functions. */ class Test_Memory : public Memory_Class { - const Memory *mem = REQUIRE_NOT_NULL(os_memory()); + const Tox_Memory *mem = REQUIRE_NOT_NULL(os_memory()); void *malloc(void *obj, uint32_t size) override; - void *calloc(void *obj, uint32_t nmemb, uint32_t size) override; void *realloc(void *obj, void *ptr, uint32_t size) override; - void free(void *obj, void *ptr) override; + void dealloc(void *obj, void *ptr) override; }; #endif // C_TOXCORE_TOXCORE_MEM_TEST_UTIL_H diff --git a/external/toxcore/c-toxcore/toxcore/net_crypto.c b/external/toxcore/c-toxcore/toxcore/net_crypto.c index 6815a13ab..59ca4601b 100644 --- a/external/toxcore/c-toxcore/toxcore/net_crypto.c +++ b/external/toxcore/c-toxcore/toxcore/net_crypto.c @@ -137,6 +137,7 @@ struct Net_Crypto { const Random *rng; Mono_Time *mono_time; const Network *ns; + Networking_Core *net; DHT *dht; TCP_Connections *tcp_c; @@ -176,11 +177,6 @@ TCP_Connections *nc_get_tcp_c(const Net_Crypto *c) return c->tcp_c; } -DHT *nc_get_dht(const Net_Crypto *c) -{ - return c->dht; -} - static bool crypt_connection_id_is_valid(const Net_Crypto *_Nonnull c, int crypt_connection_id) { if ((uint32_t)crypt_connection_id >= c->crypto_connections_length) { @@ -366,7 +362,7 @@ static int udp_handle_cookie_request(void *_Nonnull object, const IP_Port *_Nonn return 1; } - if ((uint32_t)sendpacket(dht_get_net(c->dht), source, data, sizeof(data)) != sizeof(data)) { + if ((uint32_t)sendpacket(c->net, source, data, sizeof(data)) != sizeof(data)) { return 1; } @@ -676,7 +672,7 @@ static int send_packet_to(const Net_Crypto *_Nonnull c, int crypt_connection_id, crypto_connection_status(c, crypt_connection_id, &direct_connected, nullptr); if (direct_connected) { - if ((uint32_t)sendpacket(dht_get_net(c->dht), &ip_port, data, length) == length) { + if ((uint32_t)sendpacket(c->net, &ip_port, data, length) == length) { return 0; } @@ -689,7 +685,7 @@ static int send_packet_to(const Net_Crypto *_Nonnull c, int crypt_connection_id, if ((((UDP_DIRECT_TIMEOUT / 2) + conn->direct_send_attempt_time) < current_time && length < 96) || data[0] == NET_PACKET_COOKIE_REQUEST || data[0] == NET_PACKET_CRYPTO_HS) { - if ((uint32_t)sendpacket(dht_get_net(c->dht), &ip_port, data, length) == length) { + if ((uint32_t)sendpacket(c->net, &ip_port, data, length) == length) { direct_send_attempt = true; conn->direct_send_attempt_time = mono_time_get(c->mono_time); } @@ -2915,7 +2911,7 @@ void load_secret_key(Net_Crypto *c, const uint8_t *sk) * Sets all the global connection variables to their default values. */ Net_Crypto *new_net_crypto(const Logger *log, const Memory *mem, const Random *rng, const Network *ns, - Mono_Time *mono_time, DHT *dht, const TCP_Proxy_Info *proxy_info, Net_Profile *tcp_np) + Mono_Time *mono_time, Networking_Core *net, DHT *dht, const TCP_Proxy_Info *proxy_info, Net_Profile *tcp_np) { if (dht == nullptr) { return nullptr; @@ -2932,6 +2928,7 @@ Net_Crypto *new_net_crypto(const Logger *log, const Memory *mem, const Random *r temp->rng = rng; temp->mono_time = mono_time; temp->ns = ns; + temp->net = net; temp->tcp_c = new_tcp_connections(log, mem, rng, ns, mono_time, dht_get_self_secret_key(dht), proxy_info, tcp_np); @@ -2950,10 +2947,10 @@ Net_Crypto *new_net_crypto(const Logger *log, const Memory *mem, const Random *r temp->current_sleep_time = CRYPTO_SEND_PACKET_INTERVAL; - networking_registerhandler(dht_get_net(dht), NET_PACKET_COOKIE_REQUEST, &udp_handle_cookie_request, temp); - networking_registerhandler(dht_get_net(dht), NET_PACKET_COOKIE_RESPONSE, &udp_handle_packet, temp); - 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); + networking_registerhandler(net, NET_PACKET_COOKIE_REQUEST, &udp_handle_cookie_request, temp); + networking_registerhandler(net, NET_PACKET_COOKIE_RESPONSE, &udp_handle_packet, temp); + networking_registerhandler(net, NET_PACKET_CRYPTO_HS, &udp_handle_packet, temp); + networking_registerhandler(net, NET_PACKET_CRYPTO_DATA, &udp_handle_packet, temp); bs_list_init(&temp->ip_port_list, mem, sizeof(IP_Port), 8, ipport_cmp_handler); @@ -3016,10 +3013,10 @@ void kill_net_crypto(Net_Crypto *c) kill_tcp_connections(c->tcp_c); bs_list_free(&c->ip_port_list); - networking_registerhandler(dht_get_net(c->dht), NET_PACKET_COOKIE_REQUEST, nullptr, nullptr); - networking_registerhandler(dht_get_net(c->dht), NET_PACKET_COOKIE_RESPONSE, nullptr, nullptr); - networking_registerhandler(dht_get_net(c->dht), NET_PACKET_CRYPTO_HS, nullptr, nullptr); - networking_registerhandler(dht_get_net(c->dht), NET_PACKET_CRYPTO_DATA, nullptr, nullptr); + networking_registerhandler(c->net, NET_PACKET_COOKIE_REQUEST, nullptr, nullptr); + networking_registerhandler(c->net, NET_PACKET_COOKIE_RESPONSE, nullptr, nullptr); + networking_registerhandler(c->net, NET_PACKET_CRYPTO_HS, nullptr, nullptr); + networking_registerhandler(c->net, NET_PACKET_CRYPTO_DATA, nullptr, nullptr); crypto_memzero(c, sizeof(Net_Crypto)); mem_delete(mem, c); } diff --git a/external/toxcore/c-toxcore/toxcore/net_crypto.h b/external/toxcore/c-toxcore/toxcore/net_crypto.h index dc93d6585..f3270ad8a 100644 --- a/external/toxcore/c-toxcore/toxcore/net_crypto.h +++ b/external/toxcore/c-toxcore/toxcore/net_crypto.h @@ -126,7 +126,6 @@ typedef struct Net_Crypto Net_Crypto; const uint8_t *_Nonnull nc_get_self_public_key(const Net_Crypto *_Nonnull c); const uint8_t *_Nonnull nc_get_self_secret_key(const Net_Crypto *_Nonnull c); TCP_Connections *_Nonnull nc_get_tcp_c(const Net_Crypto *_Nonnull c); -DHT *_Nonnull nc_get_dht(const Net_Crypto *_Nonnull c); typedef struct New_Connection { IP_Port source; @@ -369,7 +368,8 @@ void load_secret_key(Net_Crypto *_Nonnull c, const uint8_t *_Nonnull sk); /** @brief Create new instance of Net_Crypto. * Sets all the global connection variables to their default values. */ -Net_Crypto *_Nullable new_net_crypto(const Logger *_Nonnull log, const Memory *_Nonnull mem, const Random *_Nonnull rng, const Network *_Nonnull ns, Mono_Time *_Nonnull mono_time, DHT *_Nonnull dht, +Net_Crypto *_Nullable new_net_crypto(const Logger *_Nonnull log, const Memory *_Nonnull mem, const Random *_Nonnull rng, const Network *_Nonnull ns, Mono_Time *_Nonnull mono_time, + Networking_Core *_Nonnull net, DHT *_Nonnull dht, const TCP_Proxy_Info *_Nonnull proxy_info, Net_Profile *_Nonnull tcp_np); /** return the optimal interval in ms for running do_net_crypto. */ diff --git a/external/toxcore/c-toxcore/toxcore/net_crypto_fuzz_test.cc b/external/toxcore/c-toxcore/toxcore/net_crypto_fuzz_test.cc index 96ee86fca..01ff12c24 100644 --- a/external/toxcore/c-toxcore/toxcore/net_crypto_fuzz_test.cc +++ b/external/toxcore/c-toxcore/toxcore/net_crypto_fuzz_test.cc @@ -76,7 +76,7 @@ void TestNetCrypto(Fuzz_Data &input) const Ptr net_crypto( new_net_crypto(logger.get(), sys.mem.get(), sys.rng.get(), sys.ns.get(), mono_time.get(), - dht.get(), &proxy_info, tcp_np), + net.get(), dht.get(), &proxy_info, tcp_np), kill_net_crypto); if (net_crypto == nullptr) { netprof_kill(sys.mem.get(), tcp_np); diff --git a/external/toxcore/c-toxcore/toxcore/net_log.c b/external/toxcore/c-toxcore/toxcore/net_log.c new file mode 100644 index 000000000..c83bfe351 --- /dev/null +++ b/external/toxcore/c-toxcore/toxcore/net_log.c @@ -0,0 +1,180 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2016-2025 The TokTok team. + * Copyright © 2013 Tox project. + */ + +/** + * Network traffic logging function. + */ + +#include "net_log.h" + +#include "attributes.h" +#include "logger.h" +#include "network.h" +#include "util.h" + +static uint32_t data_0(uint16_t buflen, const uint8_t *_Nonnull buffer) +{ + uint32_t data = 0; + + if (buflen > 4) { + net_unpack_u32(buffer + 1, &data); + } + + return data; +} +static uint32_t data_1(uint16_t buflen, const uint8_t *_Nonnull buffer) +{ + uint32_t data = 0; + + if (buflen > 8) { + net_unpack_u32(buffer + 5, &data); + } + + return data; +} + +static const char *net_packet_type_name(Net_Packet_Type type) +{ + switch (type) { + case NET_PACKET_PING_REQUEST: + return "PING_REQUEST"; + + case NET_PACKET_PING_RESPONSE: + return "PING_RESPONSE"; + + case NET_PACKET_NODES_REQUEST: + return "NODES_REQUEST"; + + case NET_PACKET_NODES_RESPONSE: + return "NODES_RESPONSE"; + + case NET_PACKET_COOKIE_REQUEST: + return "COOKIE_REQUEST"; + + case NET_PACKET_COOKIE_RESPONSE: + return "COOKIE_RESPONSE"; + + case NET_PACKET_CRYPTO_HS: + return "CRYPTO_HS"; + + case NET_PACKET_CRYPTO_DATA: + return "CRYPTO_DATA"; + + case NET_PACKET_CRYPTO: + return "CRYPTO"; + + case NET_PACKET_GC_HANDSHAKE: + return "GC_HANDSHAKE"; + + case NET_PACKET_GC_LOSSLESS: + return "GC_LOSSLESS"; + + case NET_PACKET_GC_LOSSY: + return "GC_LOSSY"; + + case NET_PACKET_LAN_DISCOVERY: + return "LAN_DISCOVERY"; + + case NET_PACKET_ONION_SEND_INITIAL: + return "ONION_SEND_INITIAL"; + + case NET_PACKET_ONION_SEND_1: + return "ONION_SEND_1"; + + case NET_PACKET_ONION_SEND_2: + return "ONION_SEND_2"; + + case NET_PACKET_ANNOUNCE_REQUEST_OLD: + return "ANNOUNCE_REQUEST_OLD"; + + case NET_PACKET_ANNOUNCE_RESPONSE_OLD: + return "ANNOUNCE_RESPONSE_OLD"; + + case NET_PACKET_ONION_DATA_REQUEST: + return "ONION_DATA_REQUEST"; + + case NET_PACKET_ONION_DATA_RESPONSE: + return "ONION_DATA_RESPONSE"; + + case NET_PACKET_ANNOUNCE_REQUEST: + return "ANNOUNCE_REQUEST"; + + case NET_PACKET_ANNOUNCE_RESPONSE: + return "ANNOUNCE_RESPONSE"; + + case NET_PACKET_ONION_RECV_3: + return "ONION_RECV_3"; + + case NET_PACKET_ONION_RECV_2: + return "ONION_RECV_2"; + + case NET_PACKET_ONION_RECV_1: + return "ONION_RECV_1"; + + case NET_PACKET_FORWARD_REQUEST: + return "FORWARD_REQUEST"; + + case NET_PACKET_FORWARDING: + return "FORWARDING"; + + case NET_PACKET_FORWARD_REPLY: + return "FORWARD_REPLY"; + + case NET_PACKET_DATA_SEARCH_REQUEST: + return "DATA_SEARCH_REQUEST"; + + case NET_PACKET_DATA_SEARCH_RESPONSE: + return "DATA_SEARCH_RESPONSE"; + + case NET_PACKET_DATA_RETRIEVE_REQUEST: + return "DATA_RETRIEVE_REQUEST"; + + case NET_PACKET_DATA_RETRIEVE_RESPONSE: + return "DATA_RETRIEVE_RESPONSE"; + + case NET_PACKET_STORE_ANNOUNCE_REQUEST: + return "STORE_ANNOUNCE_REQUEST"; + + case NET_PACKET_STORE_ANNOUNCE_RESPONSE: + return "STORE_ANNOUNCE_RESPONSE"; + + case BOOTSTRAP_INFO_PACKET_ID: + return "BOOTSTRAP_INFO"; + + case NET_PACKET_MAX: + return "MAX"; + } + + return ""; +} + +void net_log_data(const Logger *log, const char *message, const uint8_t *buffer, + uint16_t buflen, const IP_Port *ip_port, long res) +{ + if (res < 0) { /* Windows doesn't necessarily know `%zu` */ + Ip_Ntoa ip_str; + const int error = net_error(); + Net_Strerror error_str; + LOGGER_TRACE(log, "[%02x = %-21s] %s %3u%c %s:%u (%d: %s) | %08x%08x...%02x", + buffer[0], net_packet_type_name((Net_Packet_Type)buffer[0]), message, + min_u16(buflen, 999), 'E', + net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port), error, + net_strerror(error, &error_str), data_0(buflen, buffer), data_1(buflen, buffer), buffer[buflen - 1]); + } else if ((res > 0) && ((size_t)res <= buflen)) { + Ip_Ntoa ip_str; + LOGGER_TRACE(log, "[%02x = %-21s] %s %3u%c %s:%u (%d: %s) | %08x%08x...%02x", + buffer[0], net_packet_type_name((Net_Packet_Type)buffer[0]), message, + min_u16(res, 999), (size_t)res < buflen ? '<' : '=', + net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port), 0, "OK", + data_0(buflen, buffer), data_1(buflen, buffer), buffer[buflen - 1]); + } else { /* empty or overwrite */ + Ip_Ntoa ip_str; + LOGGER_TRACE(log, "[%02x = %-21s] %s %ld%c%u %s:%u (%d: %s) | %08x%08x...%02x", + buffer[0], net_packet_type_name((Net_Packet_Type)buffer[0]), message, + res, res == 0 ? '!' : '>', buflen, + net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port), 0, "OK", + data_0(buflen, buffer), data_1(buflen, buffer), buffer[buflen - 1]); + } +} diff --git a/external/toxcore/c-toxcore/toxcore/net_log.h b/external/toxcore/c-toxcore/toxcore/net_log.h new file mode 100644 index 000000000..6aec3df6f --- /dev/null +++ b/external/toxcore/c-toxcore/toxcore/net_log.h @@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2016-2025 The TokTok team. + * Copyright © 2013 Tox project. + */ + +#ifndef C_TOXCORE_TOXCORE_NET_LOG_H +#define C_TOXCORE_TOXCORE_NET_LOG_H + +#include // bool +#include // uint*_t + +#include "attributes.h" +#include "logger.h" +#include "network.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void net_log_data(const Logger *_Nonnull log, const char *_Nonnull message, const uint8_t *_Nonnull buffer, + uint16_t buflen, const IP_Port *_Nonnull ip_port, long res); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* C_TOXCORE_TOXCORE_NET_LOG_H */ diff --git a/external/toxcore/c-toxcore/toxcore/network.c b/external/toxcore/c-toxcore/toxcore/network.c index 00e8c87b4..7a205783e 100644 --- a/external/toxcore/c-toxcore/toxcore/network.c +++ b/external/toxcore/c-toxcore/toxcore/network.c @@ -86,6 +86,7 @@ #include "ccompat.h" #include "logger.h" #include "mem.h" +#include "net_log.h" #include "net_profile.h" #include "util.h" @@ -714,170 +715,6 @@ static int net_getsockopt(const Network *_Nonnull ns, Socket sock, int level, in return ns->funcs->getsockopt(ns->obj, sock, level, optname, optval, optlen); } -static uint32_t data_0(uint16_t buflen, const uint8_t *_Nonnull buffer) -{ - uint32_t data = 0; - - if (buflen > 4) { - net_unpack_u32(buffer + 1, &data); - } - - return data; -} -static uint32_t data_1(uint16_t buflen, const uint8_t *_Nonnull buffer) -{ - uint32_t data = 0; - - if (buflen > 8) { - net_unpack_u32(buffer + 5, &data); - } - - return data; -} - -static const char *net_packet_type_name(Net_Packet_Type type) -{ - switch (type) { - case NET_PACKET_PING_REQUEST: - return "PING_REQUEST"; - - case NET_PACKET_PING_RESPONSE: - return "PING_RESPONSE"; - - case NET_PACKET_NODES_REQUEST: - return "NODES_REQUEST"; - - case NET_PACKET_NODES_RESPONSE: - return "NODES_RESPONSE"; - - case NET_PACKET_COOKIE_REQUEST: - return "COOKIE_REQUEST"; - - case NET_PACKET_COOKIE_RESPONSE: - return "COOKIE_RESPONSE"; - - case NET_PACKET_CRYPTO_HS: - return "CRYPTO_HS"; - - case NET_PACKET_CRYPTO_DATA: - return "CRYPTO_DATA"; - - case NET_PACKET_CRYPTO: - return "CRYPTO"; - - case NET_PACKET_GC_HANDSHAKE: - return "GC_HANDSHAKE"; - - case NET_PACKET_GC_LOSSLESS: - return "GC_LOSSLESS"; - - case NET_PACKET_GC_LOSSY: - return "GC_LOSSY"; - - case NET_PACKET_LAN_DISCOVERY: - return "LAN_DISCOVERY"; - - case NET_PACKET_ONION_SEND_INITIAL: - return "ONION_SEND_INITIAL"; - - case NET_PACKET_ONION_SEND_1: - return "ONION_SEND_1"; - - case NET_PACKET_ONION_SEND_2: - return "ONION_SEND_2"; - - case NET_PACKET_ANNOUNCE_REQUEST_OLD: - return "ANNOUNCE_REQUEST_OLD"; - - case NET_PACKET_ANNOUNCE_RESPONSE_OLD: - return "ANNOUNCE_RESPONSE_OLD"; - - case NET_PACKET_ONION_DATA_REQUEST: - return "ONION_DATA_REQUEST"; - - case NET_PACKET_ONION_DATA_RESPONSE: - return "ONION_DATA_RESPONSE"; - - case NET_PACKET_ANNOUNCE_REQUEST: - return "ANNOUNCE_REQUEST"; - - case NET_PACKET_ANNOUNCE_RESPONSE: - return "ANNOUNCE_RESPONSE"; - - case NET_PACKET_ONION_RECV_3: - return "ONION_RECV_3"; - - case NET_PACKET_ONION_RECV_2: - return "ONION_RECV_2"; - - case NET_PACKET_ONION_RECV_1: - return "ONION_RECV_1"; - - case NET_PACKET_FORWARD_REQUEST: - return "FORWARD_REQUEST"; - - case NET_PACKET_FORWARDING: - return "FORWARDING"; - - case NET_PACKET_FORWARD_REPLY: - return "FORWARD_REPLY"; - - case NET_PACKET_DATA_SEARCH_REQUEST: - return "DATA_SEARCH_REQUEST"; - - case NET_PACKET_DATA_SEARCH_RESPONSE: - return "DATA_SEARCH_RESPONSE"; - - case NET_PACKET_DATA_RETRIEVE_REQUEST: - return "DATA_RETRIEVE_REQUEST"; - - case NET_PACKET_DATA_RETRIEVE_RESPONSE: - return "DATA_RETRIEVE_RESPONSE"; - - case NET_PACKET_STORE_ANNOUNCE_REQUEST: - return "STORE_ANNOUNCE_REQUEST"; - - case NET_PACKET_STORE_ANNOUNCE_RESPONSE: - return "STORE_ANNOUNCE_RESPONSE"; - - case BOOTSTRAP_INFO_PACKET_ID: - return "BOOTSTRAP_INFO"; - - case NET_PACKET_MAX: - return "MAX"; - } - - return ""; -} - -static void loglogdata(const Logger *_Nonnull log, const char *_Nonnull message, const uint8_t *_Nonnull buffer, uint16_t buflen, const IP_Port *_Nonnull ip_port, long res) -{ - if (res < 0) { /* Windows doesn't necessarily know `%zu` */ - Ip_Ntoa ip_str; - const int error = net_error(); - Net_Strerror error_str; - LOGGER_TRACE(log, "[%02x = %-21s] %s %3u%c %s:%u (%u: %s) | %08x%08x...%02x", - buffer[0], net_packet_type_name((Net_Packet_Type)buffer[0]), message, - min_u16(buflen, 999), 'E', - net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port), (unsigned int)error, - net_strerror(error, &error_str), data_0(buflen, buffer), data_1(buflen, buffer), buffer[buflen - 1]); - } else if ((res > 0) && ((size_t)res <= buflen)) { - Ip_Ntoa ip_str; - LOGGER_TRACE(log, "[%02x = %-21s] %s %3u%c %s:%u (%u: %s) | %08x%08x...%02x", - buffer[0], net_packet_type_name((Net_Packet_Type)buffer[0]), message, - min_u16(res, 999), (size_t)res < buflen ? '<' : '=', - net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port), (unsigned int)0, "OK", - data_0(buflen, buffer), data_1(buflen, buffer), buffer[buflen - 1]); - } else { /* empty or overwrite */ - Ip_Ntoa ip_str; - LOGGER_TRACE(log, "[%02x = %-21s] %s %lu%c%u %s:%u (%u: %s) | %08x%08x...%02x", - buffer[0], net_packet_type_name((Net_Packet_Type)buffer[0]), message, - (unsigned long)res, res == 0 ? '!' : '>', buflen, - net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port), (unsigned int)0, "OK", - data_0(buflen, buffer), data_1(buflen, buffer), buffer[buflen - 1]); - } -} - 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) { @@ -887,7 +724,7 @@ int net_send(const Network *ns, const Logger *log, netprof_record_packet(net_profile, buf[0], res, PACKET_DIRECTION_SEND); } - loglogdata(log, "T=>", buf, len, ip_port, res); + net_log_data(log, "T=>", buf, len, ip_port, res); return res; } @@ -900,7 +737,7 @@ int net_recv(const Network *ns, const Logger *log, Socket sock, uint8_t *buf, size_t len, const IP_Port *ip_port) { const int res = ns->funcs->recv(ns->obj, sock, buf, len); - loglogdata(log, "=>T", buf, len, ip_port, res); + net_log_data(log, "=>T", buf, len, ip_port, res); return res; } @@ -1068,7 +905,7 @@ int send_packet(const Networking_Core *net, const IP_Port *ip_port, Packet packe } const long res = net_sendto(net->ns, net->sock, packet.data, packet.length, &addr, &ipp_copy); - loglogdata(net->log, "O=>", packet.data, packet.length, ip_port, res); + net_log_data(net->log, "O=>", packet.data, packet.length, ip_port, res); assert(res <= INT_MAX); @@ -1151,7 +988,7 @@ static int receivepacket(const Network *_Nonnull ns, const Logger *_Nonnull log, return -1; } - loglogdata(log, "=>O", data, MAX_UDP_PACKET_SIZE, ip_port, *length); + net_log_data(log, "=>O", data, MAX_UDP_PACKET_SIZE, ip_port, *length); return 0; } diff --git a/external/toxcore/c-toxcore/toxcore/onion.c b/external/toxcore/c-toxcore/toxcore/onion.c index f5738dfb8..9452bd2c4 100644 --- a/external/toxcore/c-toxcore/toxcore/onion.c +++ b/external/toxcore/c-toxcore/toxcore/onion.c @@ -702,7 +702,7 @@ void set_callback_handle_recv_1(Onion *onion, onion_recv_1_cb *function, void *o onion->callback_object = object; } -Onion *new_onion(const Logger *log, const Memory *mem, const Mono_Time *mono_time, const Random *rng, DHT *dht) +Onion *new_onion(const Logger *log, const Memory *mem, const Mono_Time *mono_time, const Random *rng, DHT *dht, Networking_Core *net) { if (dht == nullptr) { return nullptr; @@ -716,7 +716,7 @@ Onion *new_onion(const Logger *log, const Memory *mem, const Mono_Time *mono_tim onion->log = log; onion->dht = dht; - onion->net = dht_get_net(dht); + onion->net = net; onion->mono_time = mono_time; onion->rng = rng; onion->mem = mem; diff --git a/external/toxcore/c-toxcore/toxcore/onion.h b/external/toxcore/c-toxcore/toxcore/onion.h index 355a41f6b..6c967ba8c 100644 --- a/external/toxcore/c-toxcore/toxcore/onion.h +++ b/external/toxcore/c-toxcore/toxcore/onion.h @@ -140,7 +140,7 @@ int onion_send_1(const Onion *_Nonnull onion, const uint8_t *_Nonnull plain, uin /** Set the callback to be called when the dest ip_port doesn't have TOX_AF_INET6 or TOX_AF_INET as the family. */ void set_callback_handle_recv_1(Onion *_Nonnull onion, onion_recv_1_cb *_Nullable function, void *_Nullable object); -Onion *_Nullable new_onion(const Logger *_Nonnull log, const Memory *_Nonnull mem, const Mono_Time *_Nonnull mono_time, const Random *_Nonnull rng, DHT *_Nonnull dht); +Onion *_Nullable new_onion(const Logger *_Nonnull log, const Memory *_Nonnull mem, const Mono_Time *_Nonnull mono_time, const Random *_Nonnull rng, DHT *_Nonnull dht, Networking_Core *_Nonnull net); void kill_onion(Onion *_Nullable onion); #endif /* C_TOXCORE_TOXCORE_ONION_H */ diff --git a/external/toxcore/c-toxcore/toxcore/onion_announce.c b/external/toxcore/c-toxcore/toxcore/onion_announce.c index 6c80aa96d..f752506cd 100644 --- a/external/toxcore/c-toxcore/toxcore/onion_announce.c +++ b/external/toxcore/c-toxcore/toxcore/onion_announce.c @@ -680,7 +680,8 @@ static int handle_data_request(void *_Nonnull object, const IP_Port *_Nonnull so return 0; } -Onion_Announce *new_onion_announce(const Logger *log, const Memory *mem, const Random *rng, const Mono_Time *mono_time, DHT *dht) +Onion_Announce *new_onion_announce(const Logger *log, const Memory *mem, const Random *rng, const Mono_Time *mono_time, DHT *dht, + Networking_Core *net) { if (dht == nullptr) { return nullptr; @@ -697,7 +698,7 @@ Onion_Announce *new_onion_announce(const Logger *log, const Memory *mem, const R onion_a->mem = mem; onion_a->mono_time = mono_time; onion_a->dht = dht; - onion_a->net = dht_get_net(dht); + onion_a->net = net; onion_a->extra_data_max_size = 0; onion_a->extra_data_callback = nullptr; onion_a->extra_data_object = nullptr; diff --git a/external/toxcore/c-toxcore/toxcore/onion_announce.h b/external/toxcore/c-toxcore/toxcore/onion_announce.h index a7e0f0658..fca6798c3 100644 --- a/external/toxcore/c-toxcore/toxcore/onion_announce.h +++ b/external/toxcore/c-toxcore/toxcore/onion_announce.h @@ -124,7 +124,8 @@ typedef int pack_extra_data_cb(void *_Nonnull object, const Logger *_Nonnull log void onion_announce_extra_data_callback(Onion_Announce *_Nonnull onion_a, uint16_t extra_data_max_size, pack_extra_data_cb *_Nonnull extra_data_callback, void *_Nonnull extra_data_object); -Onion_Announce *_Nullable new_onion_announce(const Logger *_Nonnull log, const Memory *_Nonnull mem, const Random *_Nonnull rng, const Mono_Time *_Nonnull mono_time, DHT *_Nonnull dht); +Onion_Announce *_Nullable new_onion_announce(const Logger *_Nonnull log, const Memory *_Nonnull mem, const Random *_Nonnull rng, const Mono_Time *_Nonnull mono_time, DHT *_Nonnull dht, + Networking_Core *_Nonnull net); void kill_onion_announce(Onion_Announce *_Nullable onion_a); #endif /* C_TOXCORE_TOXCORE_ONION_ANNOUNCE_H */ diff --git a/external/toxcore/c-toxcore/toxcore/onion_client.c b/external/toxcore/c-toxcore/toxcore/onion_client.c index 4c50925b9..b47a6522a 100644 --- a/external/toxcore/c-toxcore/toxcore/onion_client.c +++ b/external/toxcore/c-toxcore/toxcore/onion_client.c @@ -203,16 +203,6 @@ bool onion_friend_is_groupchat(const Onion_Friend *const onion_friend) return onion_friend->is_groupchat; } -DHT *onion_get_dht(const Onion_Client *onion_c) -{ - return onion_c->dht; -} - -Net_Crypto *onion_get_net_crypto(const Onion_Client *onion_c) -{ - return onion_c->c; -} - /** @brief Add a node to the path_nodes bootstrap array. * * If a node with the given public key was already in the bootstrap array, this function has no @@ -2169,7 +2159,8 @@ void do_onion_client(Onion_Client *onion_c) onion_c->last_run = mono_time_get(onion_c->mono_time); } -Onion_Client *new_onion_client(const Logger *logger, const Memory *mem, const Random *rng, const Mono_Time *mono_time, Net_Crypto *c) +Onion_Client *new_onion_client(const Logger *logger, const Memory *mem, const Random *rng, const Mono_Time *mono_time, Net_Crypto *c, + DHT *dht, Networking_Core *net) { if (c == nullptr) { return nullptr; @@ -2192,8 +2183,8 @@ Onion_Client *new_onion_client(const Logger *logger, const Memory *mem, const Ra onion_c->logger = logger; onion_c->rng = rng; onion_c->mem = mem; - onion_c->dht = nc_get_dht(c); - onion_c->net = dht_get_net(onion_c->dht); + onion_c->dht = dht; + onion_c->net = net; onion_c->c = c; new_symmetric_key(rng, onion_c->secret_symmetric_key); crypto_new_keypair(rng, onion_c->temp_public_key, onion_c->temp_secret_key); diff --git a/external/toxcore/c-toxcore/toxcore/onion_client.h b/external/toxcore/c-toxcore/toxcore/onion_client.h index bdcaf3553..a61e9ac77 100644 --- a/external/toxcore/c-toxcore/toxcore/onion_client.h +++ b/external/toxcore/c-toxcore/toxcore/onion_client.h @@ -21,7 +21,6 @@ #include "net_crypto.h" #include "network.h" #include "onion_announce.h" -#include "ping_array.h" #define MAX_ONION_CLIENTS 8 #define MAX_ONION_CLIENTS_ANNOUNCE 12 // Number of nodes to announce ourselves to. @@ -66,9 +65,6 @@ typedef struct Onion_Client Onion_Client; -DHT *_Nonnull onion_get_dht(const Onion_Client *_Nonnull onion_c); -Net_Crypto *_Nonnull onion_get_net_crypto(const Onion_Client *_Nonnull onion_c); - /** @brief Add a node to the path_nodes bootstrap array. * * If a node with the given public key was already in the bootstrap array, this function has no @@ -195,7 +191,8 @@ typedef bool onion_group_announce_cb(Onion_Client *_Nonnull onion_c, uint32_t se void onion_group_announce_register(Onion_Client *_Nonnull onion_c, onion_group_announce_cb *_Nullable func, void *_Nullable user_data); void do_onion_client(Onion_Client *_Nonnull onion_c); -Onion_Client *_Nullable new_onion_client(const Logger *_Nonnull logger, const Memory *_Nonnull mem, const Random *_Nonnull rng, const Mono_Time *_Nonnull mono_time, Net_Crypto *_Nonnull c); +Onion_Client *_Nullable new_onion_client(const Logger *_Nonnull logger, const Memory *_Nonnull mem, const Random *_Nonnull rng, const Mono_Time *_Nonnull mono_time, Net_Crypto *_Nonnull c, + DHT *_Nonnull dht, Networking_Core *_Nonnull net); void kill_onion_client(Onion_Client *_Nullable onion_c); typedef enum Onion_Connection_Status { diff --git a/external/toxcore/c-toxcore/toxcore/os_memory.c b/external/toxcore/c-toxcore/toxcore/os_memory.c new file mode 100644 index 000000000..7275a45bc --- /dev/null +++ b/external/toxcore/c-toxcore/toxcore/os_memory.c @@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2022-2025 The TokTok team. + */ +#include "os_memory.h" + +#include + +#include "attributes.h" +#include "tox_memory.h" +#include "tox_memory_impl.h" // IWYU pragma: keep + +static void *os_malloc(void *_Nonnull self, uint32_t size) +{ + // cppcheck-suppress misra-c2012-21.3 + return malloc(size); +} + +static void *os_realloc(void *_Nonnull self, void *_Nullable ptr, uint32_t size) +{ + // cppcheck-suppress misra-c2012-21.3 + return realloc(ptr, size); +} + +static void os_dealloc(void *_Nonnull self, void *_Nullable ptr) +{ + // cppcheck-suppress misra-c2012-21.3 + free(ptr); +} + +static const Tox_Memory_Funcs os_memory_funcs = { + os_malloc, + os_realloc, + os_dealloc, +}; +const Tox_Memory os_memory_obj = {&os_memory_funcs}; + +const Tox_Memory *os_memory(void) +{ + return &os_memory_obj; +} diff --git a/external/toxcore/c-toxcore/toxcore/os_memory.h b/external/toxcore/c-toxcore/toxcore/os_memory.h new file mode 100644 index 000000000..cfbe6a2c7 --- /dev/null +++ b/external/toxcore/c-toxcore/toxcore/os_memory.h @@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2022-2025 The TokTok team. + */ + +#ifndef C_TOXCORE_TOXCORE_OS_MEMORY_H +#define C_TOXCORE_TOXCORE_OS_MEMORY_H + +#include "tox_memory.h" + +#ifdef __cplusplus +extern "C" { +#endif + +extern const Tox_Memory os_memory_obj; + +const Tox_Memory *os_memory(void); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* C_TOXCORE_TOXCORE_OS_MEMORY_H */ diff --git a/external/toxcore/c-toxcore/toxcore/os_random.c b/external/toxcore/c-toxcore/toxcore/os_random.c new file mode 100644 index 000000000..ee71ce30b --- /dev/null +++ b/external/toxcore/c-toxcore/toxcore/os_random.c @@ -0,0 +1,43 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2022-2025 The TokTok team. + */ +#include "os_random.h" + +#include + +#include "attributes.h" +#include "ccompat.h" +#include "tox_random.h" +#include "tox_random_impl.h" + +static void os_random_bytes(void *_Nonnull self, uint8_t *_Nonnull bytes, uint32_t length) +{ + randombytes(bytes, length); +} + +static uint32_t os_random_uniform(void *_Nonnull self, uint32_t upper_bound) +{ + return randombytes_uniform(upper_bound); +} + +static const Tox_Random_Funcs os_random_funcs = { + os_random_bytes, + os_random_uniform, +}; + +const Tox_Random os_random_obj = {&os_random_funcs}; + +const Tox_Random *os_random(void) +{ +#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION + if ((true)) { + return nullptr; + } +#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */ + // It is safe to call this function more than once and from different + // threads -- subsequent calls won't have any effects. + if (sodium_init() == -1) { + return nullptr; + } + return &os_random_obj; +} diff --git a/external/toxcore/c-toxcore/toxcore/os_random.h b/external/toxcore/c-toxcore/toxcore/os_random.h new file mode 100644 index 000000000..a670b8eeb --- /dev/null +++ b/external/toxcore/c-toxcore/toxcore/os_random.h @@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2022-2025 The TokTok team. + */ + +#ifndef C_TOXCORE_TOXCORE_OS_RANDOM_H +#define C_TOXCORE_TOXCORE_OS_RANDOM_H + +#include "tox_random.h" + +#ifdef __cplusplus +extern "C" { +#endif + +extern const Tox_Random os_random_obj; + +/** @brief System random number generator. + * + * Uses libsodium's CSPRNG (on Linux, `/dev/urandom`). + */ +const Tox_Random *os_random(void); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* C_TOXCORE_TOXCORE_OS_RANDOM_H */ diff --git a/external/toxcore/c-toxcore/toxcore/ping.c b/external/toxcore/c-toxcore/toxcore/ping.c index 13e443353..910a0a6f3 100644 --- a/external/toxcore/c-toxcore/toxcore/ping.c +++ b/external/toxcore/c-toxcore/toxcore/ping.c @@ -33,6 +33,7 @@ struct Ping { const Random *rng; const Memory *mem; DHT *dht; + Networking_Core *net; Ping_Array *ping_array; Node_format to_ping[MAX_TO_PING]; @@ -83,7 +84,7 @@ void ping_send_request(Ping *ping, const IP_Port *ipp, const uint8_t *public_key } // We never check this return value and failures in sendpacket are already logged - sendpacket(dht_get_net(ping->dht), ipp, pk, sizeof(pk)); + sendpacket(ping->net, ipp, pk, sizeof(pk)); } static int ping_send_response(const Ping *_Nonnull ping, const IP_Port *_Nonnull ipp, const uint8_t *_Nonnull public_key, uint64_t ping_id, const uint8_t *_Nonnull shared_encryption_key) @@ -112,7 +113,7 @@ static int ping_send_response(const Ping *_Nonnull ping, const IP_Port *_Nonnull return 1; } - return sendpacket(dht_get_net(ping->dht), ipp, pk, sizeof(pk)); + return sendpacket(ping->net, ipp, pk, sizeof(pk)); } static int handle_ping_request(void *_Nonnull object, const IP_Port *_Nonnull source, const uint8_t *_Nonnull packet, uint16_t length, void *_Nonnull userdata) @@ -324,7 +325,7 @@ void ping_iterate(Ping *ping) } } -Ping *ping_new(const Memory *mem, const Mono_Time *mono_time, const Random *rng, DHT *dht) +Ping *ping_new(const Memory *mem, const Mono_Time *mono_time, const Random *rng, DHT *dht, Networking_Core *net) { Ping *ping = (Ping *)mem_alloc(mem, sizeof(Ping)); @@ -343,8 +344,9 @@ Ping *ping_new(const Memory *mem, const Mono_Time *mono_time, const Random *rng, ping->rng = rng; ping->mem = mem; ping->dht = dht; - networking_registerhandler(dht_get_net(ping->dht), NET_PACKET_PING_REQUEST, &handle_ping_request, dht); - networking_registerhandler(dht_get_net(ping->dht), NET_PACKET_PING_RESPONSE, &handle_ping_response, dht); + ping->net = net; + networking_registerhandler(ping->net, NET_PACKET_PING_REQUEST, &handle_ping_request, dht); + networking_registerhandler(ping->net, NET_PACKET_PING_RESPONSE, &handle_ping_response, dht); return ping; } @@ -355,8 +357,8 @@ void ping_kill(const Memory *mem, Ping *ping) return; } - networking_registerhandler(dht_get_net(ping->dht), NET_PACKET_PING_REQUEST, nullptr, nullptr); - networking_registerhandler(dht_get_net(ping->dht), NET_PACKET_PING_RESPONSE, nullptr, nullptr); + networking_registerhandler(ping->net, NET_PACKET_PING_REQUEST, nullptr, nullptr); + networking_registerhandler(ping->net, NET_PACKET_PING_RESPONSE, nullptr, nullptr); ping_array_kill(ping->ping_array); mem_delete(mem, ping); diff --git a/external/toxcore/c-toxcore/toxcore/ping.h b/external/toxcore/c-toxcore/toxcore/ping.h index 90e9cfa8e..0ed705b75 100644 --- a/external/toxcore/c-toxcore/toxcore/ping.h +++ b/external/toxcore/c-toxcore/toxcore/ping.h @@ -21,7 +21,7 @@ typedef struct Ping Ping; -Ping *_Nullable ping_new(const Memory *_Nonnull mem, const Mono_Time *_Nonnull mono_time, const Random *_Nonnull rng, DHT *_Nonnull dht); +Ping *_Nullable ping_new(const Memory *_Nonnull mem, const Mono_Time *_Nonnull mono_time, const Random *_Nonnull rng, DHT *_Nonnull dht, Networking_Core *_Nonnull net); void ping_kill(const Memory *_Nonnull mem, Ping *_Nullable ping); /** @brief Add nodes to the to_ping list. diff --git a/external/toxcore/c-toxcore/toxcore/sort_test_util.hh b/external/toxcore/c-toxcore/toxcore/sort_test_util.hh index 52b2aeb54..3e8a2c838 100644 --- a/external/toxcore/c-toxcore/toxcore/sort_test_util.hh +++ b/external/toxcore/c-toxcore/toxcore/sort_test_util.hh @@ -9,7 +9,7 @@ #include "sort.h" -struct Memory; +struct Tox_Memory; template constexpr Sort_Funcs sort_funcs() @@ -41,7 +41,7 @@ constexpr Sort_Funcs sort_funcs() // A realistic test case where we have a struct with some stuff and an expensive value we compare. struct Some_Type { - const Memory *mem; + const Tox_Memory *mem; std::array compare_value; const char *name; diff --git a/external/toxcore/c-toxcore/toxcore/tox_attributes.h b/external/toxcore/c-toxcore/toxcore/tox_attributes.h new file mode 100644 index 000000000..fa646db0c --- /dev/null +++ b/external/toxcore/c-toxcore/toxcore/tox_attributes.h @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2022-2025 The TokTok team. + */ + +/** + * nonnull attributes for GCC/Clang and Cimple. + * + * This file is a modified version of c-toxcore/toxcore/attributes.h with a + * `tox_` prefix added to the macros to avoid conflicts with client code. + */ +#ifndef C_TOXCORE_TOXCORE_TOX_ATTRIBUTES_H +#define C_TOXCORE_TOXCORE_TOX_ATTRIBUTES_H + +/* No declarations here. */ + +//!TOKSTYLE- + +#ifndef __clang__ +#define _Nonnull +#define _Nullable +#endif + +#ifdef SPARSE +#define tox_bitwise __attribute__((bitwise)) +#else +#define tox_bitwise +#endif + +//!TOKSTYLE+ + +#endif /* C_TOXCORE_TOXCORE_TOX_ATTRIBUTES_H */ diff --git a/external/toxcore/c-toxcore/toxcore/tox_memory.c b/external/toxcore/c-toxcore/toxcore/tox_memory.c new file mode 100644 index 000000000..a2d6ad8a8 --- /dev/null +++ b/external/toxcore/c-toxcore/toxcore/tox_memory.c @@ -0,0 +1,60 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2016-2025 The TokTok team. + * Copyright © 2013 Tox project. + */ +#include "tox_memory.h" + +#include + +#include "ccompat.h" +#include "tox_memory_impl.h" // IWYU pragma: keep + +Tox_Memory *tox_memory_new(const Tox_Memory_Funcs *funcs, void *user_data) +{ + const Tox_Memory bootstrap = {funcs, user_data}; + + Tox_Memory *mem = (Tox_Memory *)tox_memory_alloc(&bootstrap, sizeof(Tox_Memory)); + + if (mem == nullptr) { + return nullptr; + } + + *mem = bootstrap; + + return mem; +} + +void tox_memory_free(Tox_Memory *mem) +{ + if (mem == nullptr) { + return; + } + + tox_memory_dealloc(mem, mem); +} + +void *tox_memory_malloc(const Tox_Memory *mem, uint32_t size) +{ + void *const ptr = mem->funcs->malloc_callback(mem->user_data, size); + return ptr; +} + +void *tox_memory_alloc(const Tox_Memory *mem, uint32_t size) +{ + void *const ptr = tox_memory_malloc(mem, size); + if (ptr != nullptr) { + memset(ptr, 0, size); + } + return ptr; +} + +void *tox_memory_realloc(const Tox_Memory *mem, void *ptr, uint32_t size) +{ + void *const new_ptr = mem->funcs->realloc_callback(mem->user_data, ptr, size); + return new_ptr; +} + +void tox_memory_dealloc(const Tox_Memory *mem, void *ptr) +{ + mem->funcs->dealloc_callback(mem->user_data, ptr); +} diff --git a/external/toxcore/c-toxcore/toxcore/tox_memory.h b/external/toxcore/c-toxcore/toxcore/tox_memory.h new file mode 100644 index 000000000..c872bf4da --- /dev/null +++ b/external/toxcore/c-toxcore/toxcore/tox_memory.h @@ -0,0 +1,75 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2016-2025 The TokTok team. + * Copyright © 2013 Tox project. + */ + +/** + * Memory allocation and deallocation functions. + */ +#ifndef C_TOXCORE_TOXCORE_TOX_MEMORY_H +#define C_TOXCORE_TOXCORE_TOX_MEMORY_H + +#include // uint*_t + +#include "tox_attributes.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** @brief Functions wrapping standard C memory allocation functions. */ +typedef struct Tox_Memory_Funcs Tox_Memory_Funcs; + +/** + * @brief A dynamic memory allocator. + */ +typedef struct Tox_Memory Tox_Memory; + +/** + * @brief Allocates a new allocator using itself to allocate its own memory. + * + * The passed `user_data` is stored and passed to allocator callbacks. It must + * outlive the `Tox_Memory` object, since it may be used by the callback invoked + * in `tox_memory_free`. + * + * @return NULL if allocation fails. + */ +Tox_Memory *_Nullable tox_memory_new(const Tox_Memory_Funcs *_Nonnull funcs, void *_Nullable user_data); + +/** + * @brief Destroys the allocator using its own deallocation function. + * + * The stored `user_data` will not be deallocated. + */ +void tox_memory_free(Tox_Memory *_Nullable mem); + +/** + * @brief Allocate an array of a given size for built-in types. + * + * The array will not be initialised. Supported built-in types are + * `uint8_t`, `int8_t`, and `int16_t`. + */ +void *_Nullable tox_memory_malloc(const Tox_Memory *_Nonnull mem, uint32_t size); + +/** + * @brief Allocate a single zero-initialised object. + * + * Always use as `(T *)tox_memory_alloc(mem, sizeof(T))`. Unlike `calloc`, this + * does not support allocating arrays. Use `malloc` and `memset` for that. + * + * @param mem The memory allocator. + * @param size Size in bytes of each element. + */ +void *_Nullable tox_memory_alloc(const Tox_Memory *_Nonnull mem, uint32_t size); + +/** @brief Resize a memory chunk vector. */ +void *_Nullable tox_memory_realloc(const Tox_Memory *_Nonnull mem, void *_Nullable ptr, uint32_t size); + +/** @brief Free an array, object, or object vector. */ +void tox_memory_dealloc(const Tox_Memory *_Nonnull mem, void *_Nullable ptr); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* C_TOXCORE_TOXCORE_TOX_MEMORY_H */ diff --git a/external/toxcore/c-toxcore/toxcore/tox_memory_impl.h b/external/toxcore/c-toxcore/toxcore/tox_memory_impl.h new file mode 100644 index 000000000..789c57f95 --- /dev/null +++ b/external/toxcore/c-toxcore/toxcore/tox_memory_impl.h @@ -0,0 +1,49 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2016-2025 The TokTok team. + * Copyright © 2013 Tox project. + */ + +/** + * Datatypes, functions and includes for the core networking. + */ +#ifndef C_TOXCORE_TOXCORE_TOX_MEMORY_IMPL_H +#define C_TOXCORE_TOXCORE_TOX_MEMORY_IMPL_H + +#include // uint*_t + +#include "tox_memory.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** @brief Allocate a byte array, similar to malloc. */ +typedef void *tox_memory_malloc_cb(void *self, uint32_t size); +/** @brief Reallocate a byte array, similar to realloc. */ +typedef void *tox_memory_realloc_cb(void *self, void *ptr, uint32_t size); +/** + * @brief Deallocate a byte or object array, similar to free. + * + * Note that `tox_memory_free` will use this callback to deallocate itself, so + * once the deallocation is done, the allocator data structures can no longer be + * referenced. + */ +typedef void tox_memory_dealloc_cb(void *self, void *ptr); + +/** @brief Functions wrapping standard C memory allocation functions. */ +struct Tox_Memory_Funcs { + tox_memory_malloc_cb *malloc_callback; + tox_memory_realloc_cb *realloc_callback; + tox_memory_dealloc_cb *dealloc_callback; +}; + +struct Tox_Memory { + const Tox_Memory_Funcs *funcs; + void *user_data; +}; + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* C_TOXCORE_TOXCORE_TOX_MEMORY_IMPL_H */ diff --git a/external/toxcore/c-toxcore/toxcore/tox_private.c b/external/toxcore/c-toxcore/toxcore/tox_private.c index e87072e5c..12aabc87e 100644 --- a/external/toxcore/c-toxcore/toxcore/tox_private.c +++ b/external/toxcore/c-toxcore/toxcore/tox_private.c @@ -22,6 +22,8 @@ #include "net_crypto.h" #include "net_profile.h" #include "network.h" +#include "os_memory.h" +#include "os_random.h" #include "tox.h" #include "tox_struct.h" // IWYU pragma: keep diff --git a/external/toxcore/c-toxcore/toxcore/tox_private.h b/external/toxcore/c-toxcore/toxcore/tox_private.h index 4cc3edd00..89a292eff 100644 --- a/external/toxcore/c-toxcore/toxcore/tox_private.h +++ b/external/toxcore/c-toxcore/toxcore/tox_private.h @@ -22,9 +22,9 @@ typedef uint64_t tox_mono_time_cb(void *user_data); typedef struct Tox_System { tox_mono_time_cb *mono_time_callback; void *mono_time_user_data; - const struct Random *rng; + const struct Tox_Random *rng; const struct Network *ns; - const struct Memory *mem; + const struct Tox_Memory *mem; } Tox_System; Tox_System tox_default_system(void); diff --git a/external/toxcore/c-toxcore/toxcore/tox_random.c b/external/toxcore/c-toxcore/toxcore/tox_random.c new file mode 100644 index 000000000..8f1432d43 --- /dev/null +++ b/external/toxcore/c-toxcore/toxcore/tox_random.c @@ -0,0 +1,42 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2022-2025 The TokTok team. + */ +#include "tox_random.h" + +#include "ccompat.h" +#include "tox_memory.h" +#include "tox_random_impl.h" + +Tox_Random *tox_random_new(const Tox_Random_Funcs *funcs, void *user_data, const Tox_Memory *mem) +{ + Tox_Random *rng = (Tox_Random *)tox_memory_alloc(mem, sizeof(Tox_Random)); + + if (rng == nullptr) { + return nullptr; + } + + rng->funcs = funcs; + rng->user_data = user_data; + + rng->mem = mem; + + return rng; +} + +void tox_random_free(Tox_Random *rng) +{ + if (rng == nullptr || rng->mem == nullptr) { + return; + } + tox_memory_dealloc(rng->mem, rng); +} + +void tox_random_bytes(const Tox_Random *rng, uint8_t *bytes, uint32_t length) +{ + rng->funcs->bytes_callback(rng->user_data, bytes, length); +} + +uint32_t tox_random_uniform(const Tox_Random *rng, uint32_t upper_bound) +{ + return rng->funcs->uniform_callback(rng->user_data, upper_bound); +} diff --git a/external/toxcore/c-toxcore/toxcore/tox_random.h b/external/toxcore/c-toxcore/toxcore/tox_random.h new file mode 100644 index 000000000..3339a41cd --- /dev/null +++ b/external/toxcore/c-toxcore/toxcore/tox_random.h @@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2022-2025 The TokTok team. + */ + +#ifndef C_TOXCORE_TOXCORE_TOX_RANDOM_H +#define C_TOXCORE_TOXCORE_TOX_RANDOM_H + +#include +#include + +#include "tox_attributes.h" +#include "tox_memory.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct Tox_Random_Funcs Tox_Random_Funcs; + +typedef struct Tox_Random Tox_Random; + +Tox_Random *_Nullable tox_random_new(const Tox_Random_Funcs *_Nonnull funcs, void *_Nullable user_data, const Tox_Memory *_Nonnull mem); + +void tox_random_free(Tox_Random *_Nullable rng); + +void tox_random_bytes(const Tox_Random *_Nonnull rng, uint8_t *_Nonnull bytes, uint32_t length); +uint32_t tox_random_uniform(const Tox_Random *_Nonnull rng, uint32_t upper_bound); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* C_TOXCORE_TOXCORE_TOX_RANDOM_H */ diff --git a/external/toxcore/c-toxcore/toxcore/tox_random_impl.h b/external/toxcore/c-toxcore/toxcore/tox_random_impl.h new file mode 100644 index 000000000..b7469015a --- /dev/null +++ b/external/toxcore/c-toxcore/toxcore/tox_random_impl.h @@ -0,0 +1,53 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2022-2025 The TokTok team. + */ + +#ifndef C_TOXCORE_TOXCORE_TOX_RANDOM_IMPL_H +#define C_TOXCORE_TOXCORE_TOX_RANDOM_IMPL_H + +#include "tox_memory.h" +#include "tox_random.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** @brief Fill a byte array with random bytes. + * + * This is the key generator callback and as such must be a cryptographically + * secure pseudo-random number generator (CSPRNG). The security of Tox heavily + * depends on the security of this RNG. + */ +typedef void tox_random_bytes_cb(void *self, uint8_t *bytes, uint32_t length); + +/** @brief Generate a random integer between 0 and @p upper_bound. + * + * Should produce a uniform random distribution, but Tox security does not + * depend on this being correct. In principle, it could even be a non-CSPRNG. + */ +typedef uint32_t tox_random_uniform_cb(void *self, uint32_t upper_bound); + +/** @brief Virtual function table for Random. */ +struct Tox_Random_Funcs { + tox_random_bytes_cb *bytes_callback; + tox_random_uniform_cb *uniform_callback; +}; + +/** @brief Random number generator object. + * + * Can be used by test code and fuzzers to make toxcore behave in specific + * well-defined (non-random) ways. Production code ought to use libsodium's + * CSPRNG and use `os_random` below. + */ +struct Tox_Random { + const Tox_Random_Funcs *funcs; + void *user_data; + + const Tox_Memory *mem; +}; + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* C_TOXCORE_TOXCORE_TOX_RANDOM_IMPL_H */ diff --git a/external/toxcore/c-toxcore/toxcore/tox_test.cc b/external/toxcore/c-toxcore/toxcore/tox_test.cc index 6d3c25ca0..394829626 100644 --- a/external/toxcore/c-toxcore/toxcore/tox_test.cc +++ b/external/toxcore/c-toxcore/toxcore/tox_test.cc @@ -6,6 +6,7 @@ #include #include "crypto_core.h" +#include "os_random.h" #include "tox_log_level.h" #include "tox_options.h" #include "tox_private.h" diff --git a/external/toxcore/c-toxcore/toxencryptsave/BUILD.bazel b/external/toxcore/c-toxcore/toxencryptsave/BUILD.bazel index 917d9c007..b2c46a376 100644 --- a/external/toxcore/c-toxcore/toxencryptsave/BUILD.bazel +++ b/external/toxcore/c-toxcore/toxencryptsave/BUILD.bazel @@ -20,6 +20,8 @@ cc_library( ":defines", "//c-toxcore/toxcore:ccompat", "//c-toxcore/toxcore:crypto_core", + "//c-toxcore/toxcore:os_memory", + "//c-toxcore/toxcore:os_random", "@libsodium", ], ) @@ -34,6 +36,8 @@ cc_library( deps = [ "//c-toxcore/toxcore:ccompat", "//c-toxcore/toxcore:crypto_core", + "//c-toxcore/toxcore:os_memory", + "//c-toxcore/toxcore:os_random", "@libsodium", ], ) diff --git a/external/toxcore/c-toxcore/toxencryptsave/toxencryptsave.c b/external/toxcore/c-toxcore/toxencryptsave/toxencryptsave.c index 63bda8605..5efc642d6 100644 --- a/external/toxcore/c-toxcore/toxencryptsave/toxencryptsave.c +++ b/external/toxcore/c-toxcore/toxencryptsave/toxencryptsave.c @@ -15,6 +15,9 @@ #include "../toxcore/ccompat.h" #include "../toxcore/crypto_core.h" +#include "../toxcore/mem.h" +#include "../toxcore/os_memory.h" +#include "../toxcore/os_random.h" #include "defines.h" static_assert(TOX_PASS_SALT_LENGTH == crypto_pwhash_scryptsalsa208sha256_SALTBYTES, @@ -52,6 +55,13 @@ struct Tox_Pass_Key { void tox_pass_key_free(Tox_Pass_Key *key) { + if (key == NULL) { + return; + } + + /* wipe sensitive state */ + crypto_memzero(key->salt, TOX_PASS_SALT_LENGTH); + crypto_memzero(key->key, TOX_PASS_KEY_LENGTH); free(key); } @@ -198,9 +208,10 @@ Tox_Pass_Key *tox_pass_key_derive_with_salt( bool tox_pass_key_encrypt(const Tox_Pass_Key *key, const uint8_t plaintext[], size_t plaintext_len, uint8_t ciphertext[], Tox_Err_Encryption *error) { + const Memory *mem = os_memory(); const Random *rng = os_random(); - if (rng == nullptr) { + if (mem == nullptr || rng == nullptr) { SET_ERROR_PARAMETER(error, TOX_ERR_ENCRYPTION_FAILED); return false; } @@ -231,7 +242,7 @@ bool tox_pass_key_encrypt(const Tox_Pass_Key *key, const uint8_t plaintext[], si ciphertext += crypto_box_NONCEBYTES; /* now encrypt */ - const int32_t encrypted_len = encrypt_data_symmetric(os_memory(), key->key, nonce, plaintext, plaintext_len, ciphertext); + const int32_t encrypted_len = encrypt_data_symmetric(mem, key->key, nonce, plaintext, plaintext_len, ciphertext); if (encrypted_len < 0 || (size_t)encrypted_len != plaintext_len + crypto_box_MACBYTES) { SET_ERROR_PARAMETER(error, TOX_ERR_ENCRYPTION_FAILED); return false; @@ -291,6 +302,13 @@ bool tox_pass_encrypt(const uint8_t plaintext[], size_t plaintext_len, const uin bool tox_pass_key_decrypt(const Tox_Pass_Key *key, const uint8_t ciphertext[], size_t ciphertext_len, uint8_t plaintext[], Tox_Err_Decryption *error) { + const Memory *mem = os_memory(); + + if (mem == nullptr) { + SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_FAILED); + return false; + } + if (ciphertext_len <= TOX_PASS_ENCRYPTION_EXTRA_LENGTH) { SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_INVALID_LENGTH); return false; @@ -316,7 +334,7 @@ bool tox_pass_key_decrypt(const Tox_Pass_Key *key, const uint8_t ciphertext[], s ciphertext += crypto_box_NONCEBYTES; /* decrypt the ciphertext */ - const int32_t decrypted_len = decrypt_data_symmetric(os_memory(), key->key, nonce, ciphertext, decrypt_length + crypto_box_MACBYTES, plaintext); + const int32_t decrypted_len = decrypt_data_symmetric(mem, key->key, nonce, ciphertext, decrypt_length + crypto_box_MACBYTES, plaintext); if (decrypted_len < 0 || (size_t)decrypted_len != decrypt_length) { SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_FAILED); return false; diff --git a/external/toxcore/c-toxcore/toxencryptsave/toxencryptsave.h b/external/toxcore/c-toxcore/toxencryptsave/toxencryptsave.h index f70ff8b4d..131bf6cb3 100644 --- a/external/toxcore/c-toxcore/toxencryptsave/toxencryptsave.h +++ b/external/toxcore/c-toxcore/toxencryptsave/toxencryptsave.h @@ -226,10 +226,7 @@ bool tox_pass_decrypt(const uint8_t ciphertext[], size_t ciphertext_len, const u * using tox_pass_key_derive or tox_pass_key_derive_with_salt and must be * deallocated using tox_pass_key_free. */ -#ifndef TOX_PASS_KEY_DEFINED -#define TOX_PASS_KEY_DEFINED typedef struct Tox_Pass_Key Tox_Pass_Key; -#endif /* TOX_PASS_KEY_DEFINED */ /** * Deallocate a Tox_Pass_Key. This function behaves like `free()`, so NULL is an