Squashed 'external/toxcore/c-toxcore/' changes from 640e6cace..e58eb27a8

e58eb27a8 fix(toxav): remove extra copy of video frame on encode Tested and works, but there might be alignment issues and other stuff.
206ea3530 refactor: Explicitly pass dependencies to constructors.
7cefa93cf fix(toxencryptsave): Wipe salt and passkey after usage.
7c3be2342 refactor: Add file/line to tox-bootstrapd logging.
f84e8cdce refactor: Move loglogdata out of network.c.
390f7db06 refactor: Move random and memory OS-specifics to `os_*` files.
REVERT: 640e6cace fix(toxav): remove extra copy of video frame on encode Tested and works, but there might be alignment issues and other stuff.

git-subtree-dir: external/toxcore/c-toxcore
git-subtree-split: e58eb27a84f9fa0cd996868e079f39e90a5c04b6
This commit is contained in:
Green Sky
2025-11-04 21:18:05 +01:00
parent 54c0a3c874
commit 596ea37298
117 changed files with 1409 additions and 697 deletions

View File

@@ -29,4 +29,5 @@ freebsd_task:
-DAUTOTEST=ON \ -DAUTOTEST=ON \
-GNinja -GNinja
cmake --build . --target install 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

View File

@@ -6,6 +6,7 @@ FROM gcr.io/oss-fuzz-base/base-builder:latest
RUN apt-get update \ RUN apt-get update \
&& apt-get -y install --no-install-suggests --no-install-recommends \ && apt-get -y install --no-install-suggests --no-install-recommends \
cmake \ cmake \
ninja-build \
pkg-config \ pkg-config \
&& apt-get clean \ && apt-get clean \
&& rm -rf /var/lib/apt/lists/* && rm -rf /var/lib/apt/lists/*

View File

@@ -18,12 +18,18 @@ cd "$WORK"
ls /usr/local/lib/ ls /usr/local/lib/
# Debug build for asserts # 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_CXX_COMPILER="$CXX" \
-DCMAKE_C_FLAGS="$CFLAGS" \ -DCMAKE_C_FLAGS="$CFLAGS" \
-DCMAKE_CXX_FLAGS="$CXXFLAGS" \ -DCMAKE_CXX_FLAGS="$CXXFLAGS" \
-DBUILD_TOXAV=OFF -DENABLE_SHARED=NO -DBUILD_FUZZ_TESTS=ON \ -DBUILD_TOXAV=OFF \
-DDHT_BOOTSTRAP=OFF -DBOOTSTRAP_DAEMON=OFF "$SRC"/c-toxcore -DENABLE_SHARED=OFF \
-DBUILD_FUZZ_TESTS=ON \
-DDHT_BOOTSTRAP=OFF \
-DBOOTSTRAP_DAEMON=OFF \
"$SRC"/c-toxcore
for TARGET in "${FUZZ_TARGETS[@]}"; do for TARGET in "${FUZZ_TARGETS[@]}"; do
# build fuzzer target # build fuzzer target

View File

@@ -10,6 +10,7 @@ brew update
brew install \ brew install \
libconfig \ libconfig \
libvpx \ libvpx \
ninja \
opus opus
. ".github/scripts/flags-clang.sh" . ".github/scripts/flags-clang.sh"
@@ -25,7 +26,7 @@ add_flag -Werror
add_c_flag -Wno-c11-extensions add_c_flag -Wno-c11-extensions
add_c_flag -Wno-pre-c11-compat add_c_flag -Wno-pre-c11-compat
cmake -B_build -H. \ cmake -GNinja -B_build -H. \
-DCMAKE_C_FLAGS="$C_FLAGS" \ -DCMAKE_C_FLAGS="$C_FLAGS" \
-DCMAKE_CXX_FLAGS="$CXX_FLAGS" \ -DCMAKE_CXX_FLAGS="$CXX_FLAGS" \
-DCMAKE_EXE_LINKER_FLAGS="$LD_FLAGS" \ -DCMAKE_EXE_LINKER_FLAGS="$LD_FLAGS" \
@@ -38,11 +39,10 @@ cmake -B_build -H. \
-DUSE_IPV6=OFF \ -DUSE_IPV6=OFF \
-DAUTOTEST=ON -DAUTOTEST=ON
cd _build # pushd cmake --build _build
make "-j$NPROC" -k install cmake --install _build
# TODO(iphydf): Investigate if we can get lan discovery to work on macos CI. # 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. # 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 --test-dir _build -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 ctest --test-dir _build -j50 --output-on-failure --rerun-failed --repeat until-pass:6 -E lan_discovery
cd - # popd

View File

@@ -309,6 +309,8 @@ set(toxcore_SOURCES
toxcore/mono_time.h toxcore/mono_time.h
toxcore/net_crypto.c toxcore/net_crypto.c
toxcore/net_crypto.h toxcore/net_crypto.h
toxcore/net_log.c
toxcore/net_log.h
toxcore/net_profile.c toxcore/net_profile.c
toxcore/net_profile.h toxcore/net_profile.h
toxcore/network.c toxcore/network.c
@@ -319,6 +321,10 @@ set(toxcore_SOURCES
toxcore/onion_client.c toxcore/onion_client.c
toxcore/onion_client.h toxcore/onion_client.h
toxcore/onion.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.c
toxcore/ping_array.h toxcore/ping_array.h
toxcore/ping.c toxcore/ping.c
@@ -340,6 +346,7 @@ set(toxcore_SOURCES
toxcore/timed_auth.c toxcore/timed_auth.c
toxcore/timed_auth.h toxcore/timed_auth.h
toxcore/tox_api.c toxcore/tox_api.c
toxcore/tox_attributes.h
toxcore/tox.c toxcore/tox.c
toxcore/tox.h toxcore/tox.h
toxcore/tox_dispatch.c toxcore/tox_dispatch.c
@@ -350,12 +357,18 @@ set(toxcore_SOURCES
toxcore/tox_events.h toxcore/tox_events.h
toxcore/tox_log_level.c toxcore/tox_log_level.c
toxcore/tox_log_level.h 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.c
toxcore/tox_options.h toxcore/tox_options.h
toxcore/tox_private.c toxcore/tox_private.c
toxcore/tox_private.h toxcore/tox_private.h
toxcore/tox_pack.c toxcore/tox_pack.c
toxcore/tox_pack.h toxcore/tox_pack.h
toxcore/tox_random.c
toxcore/tox_random.h
toxcore/tox_random_impl.h
toxcore/tox_unpack.c toxcore/tox_unpack.c
toxcore/tox_unpack.h toxcore/tox_unpack.h
toxcore/util.c toxcore/util.c
@@ -379,7 +392,8 @@ if(EXPERIMENTAL_API)
set(toxcore_API_HEADERS ${toxcore_API_HEADERS} set(toxcore_API_HEADERS ${toxcore_API_HEADERS}
${toxcore_SOURCE_DIR}/toxcore/tox_dispatch.h^tox ${toxcore_SOURCE_DIR}/toxcore/tox_dispatch.h^tox
${toxcore_SOURCE_DIR}/toxcore/tox_events.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() endif()
################################################################################ ################################################################################

View File

@@ -10,6 +10,8 @@
#include "../toxcore/mono_time.h" #include "../toxcore/mono_time.h"
#include "../toxcore/net_profile.h" #include "../toxcore/net_profile.h"
#include "../toxcore/network.h" #include "../toxcore/network.h"
#include "../toxcore/os_memory.h"
#include "../toxcore/os_random.h"
#include "auto_test_support.h" #include "auto_test_support.h"
#define NUM_PORTS 3 #define NUM_PORTS 3

View File

@@ -2,12 +2,10 @@
#include <string.h> #include <string.h>
#include "../toxcore/announce.h" #include "../toxcore/announce.h"
#include "../toxcore/tox.h"
#include "../testing/misc_tools.h"
#include "../toxcore/mono_time.h" #include "../toxcore/mono_time.h"
#include "../toxcore/forwarding.h" #include "../toxcore/forwarding.h"
#include "../toxcore/net_crypto.h" #include "../toxcore/os_memory.h"
#include "../toxcore/util.h" #include "../toxcore/os_random.h"
#include "auto_test_support.h" #include "auto_test_support.h"
#include "check_compat.h" #include "check_compat.h"
@@ -66,9 +64,9 @@ static void test_store_data(void)
ck_assert(net != nullptr); ck_assert(net != nullptr);
DHT *dht = new_dht(log, mem, rng, ns, mono_time, net, true, true); DHT *dht = new_dht(log, mem, rng, ns, mono_time, net, true, true);
ck_assert(dht != nullptr); 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); 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); ck_assert(announce != nullptr);
/* Just to prevent CI from complaining that set_synch_offset is unused: */ /* Just to prevent CI from complaining that set_synch_offset is unused: */

View File

@@ -7,6 +7,7 @@
#include <stdint.h> #include <stdint.h>
#include "../toxav/toxav.h" #include "../toxav/toxav.h"
#include "../toxcore/os_random.h"
#include "check_compat.h" #include "check_compat.h"
#define NUM_AV_GROUP_TOX 16 #define NUM_AV_GROUP_TOX 16

View File

@@ -6,6 +6,7 @@
#include <time.h> #include <time.h>
#include <stdint.h> #include <stdint.h>
#include "../toxcore/os_random.h"
#include "../toxcore/util.h" #include "../toxcore/util.h"
#include "check_compat.h" #include "check_compat.h"

View File

@@ -2,9 +2,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "../testing/misc_tools.h"
#include "../toxcore/crypto_core.h" #include "../toxcore/crypto_core.h"
#include "../toxcore/net_crypto.h" #include "../toxcore/net_crypto.h"
#include "../toxcore/os_memory.h"
#include "../toxcore/os_random.h"
#include "check_compat.h" #include "check_compat.h"
static void rand_bytes(const Random *rng, uint8_t *b, size_t blen) static void rand_bytes(const Random *rng, uint8_t *b, size_t blen)

View File

@@ -13,6 +13,10 @@
#include "check_compat.h" #include "check_compat.h"
#define NUM_TOXES 30 #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 { typedef struct Dht_Node {
uint8_t public_key[TOX_DHT_NODE_PUBLIC_KEY_SIZE]; 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)); 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); 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) { for (size_t i = 0; i < NUM_TOXES; ++i) {
State *state = (State *)autotoxes[i].state; State *state = (State *)autotoxes[i].state;

View File

@@ -3,9 +3,9 @@
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
#include "../testing/misc_tools.h"
#include "../toxcore/ccompat.h" #include "../toxcore/ccompat.h"
#include "../toxcore/crypto_core.h" #include "../toxcore/crypto_core.h"
#include "../toxcore/os_random.h"
#include "../toxcore/tox.h" #include "../toxcore/tox.h"
#include "../toxencryptsave/toxencryptsave.h" #include "../toxencryptsave/toxencryptsave.h"
#include "auto_test_support.h" #include "auto_test_support.h"

View File

@@ -8,7 +8,8 @@
#include "../toxcore/mono_time.h" #include "../toxcore/mono_time.h"
#include "../toxcore/forwarding.h" #include "../toxcore/forwarding.h"
#include "../toxcore/net_crypto.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 "auto_test_support.h"
#include "check_compat.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); ck_assert(subtox->tcp_np != nullptr);
const TCP_Proxy_Info inf = {{{{0}}}}; 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); 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); ck_assert(subtox->announce != nullptr);
return subtox; return subtox;

View File

@@ -12,6 +12,7 @@
#include "auto_test_support.h" #include "auto_test_support.h"
#include "check_compat.h" #include "check_compat.h"
#include "../toxcore/os_random.h"
#include "../toxcore/util.h" #include "../toxcore/util.h"
typedef struct State { typedef struct State {

View File

@@ -10,6 +10,7 @@
#include "auto_test_support.h" #include "auto_test_support.h"
#include "../toxcore/os_random.h"
#include "../toxcore/tox.h" #include "../toxcore/tox.h"
#include "../toxcore/util.h" #include "../toxcore/util.h"

View File

@@ -5,14 +5,13 @@
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include "auto_test_support.h" #include "auto_test_support.h"
#include "check_compat.h" #include "check_compat.h"
#include "../toxcore/os_random.h"
#include "../toxcore/tox.h" #include "../toxcore/tox.h"
#include "../toxcore/group_chats.h"
#define NUM_GROUP_TOXES 3 #define NUM_GROUP_TOXES 3

View File

@@ -1,6 +1,7 @@
#include <string.h> #include <string.h>
#include "../toxcore/network.h" #include "../toxcore/network.h"
#include "../toxcore/os_memory.h"
#include "check_compat.h" #include "check_compat.h"
#ifndef USE_IPV6 #ifndef USE_IPV6

View File

@@ -3,10 +3,12 @@
#include "../testing/misc_tools.h" #include "../testing/misc_tools.h"
#include "../toxcore/mono_time.h" #include "../toxcore/mono_time.h"
#include "../toxcore/network.h"
#include "../toxcore/onion.h" #include "../toxcore/onion.h"
#include "../toxcore/onion_announce.h" #include "../toxcore/onion_announce.h"
#include "../toxcore/onion_client.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 "auto_test_support.h"
#include "check_compat.h" #include "check_compat.h"
@@ -237,8 +239,10 @@ static void test_basic(void)
Mono_Time *mono_time2 = mono_time_new(mem, nullptr, nullptr); Mono_Time *mono_time2 = mono_time_new(mem, nullptr, nullptr);
IP ip = get_loopback(); 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)); Networking_Core *net1 = new_networking(log1, mem, ns, &ip, 36567);
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)); 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."); ck_assert_msg((onion1 != nullptr) && (onion2 != nullptr), "Onion failed initializing.");
networking_registerhandler(onion2->net, NET_PACKET_ANNOUNCE_REQUEST, &handle_test_1, onion2); 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); do_onion(mono_time2, onion2);
} while (handled_test_2 == 0); } while (handled_test_2 == 0);
Onion_Announce *onion1_a = new_onion_announce(log1, mem, rng, mono_time1, onion1->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); 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, &handle_test_3, onion1);
networking_registerhandler(onion1->net, NET_PACKET_ANNOUNCE_RESPONSE_OLD, &handle_test_3_old, 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."); 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); 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."); ck_assert_msg((onion3 != nullptr), "Onion failed initializing.");
random_nonce(rng, nonce); random_nonce(rng, nonce);
@@ -359,7 +364,7 @@ static void test_basic(void)
{ {
Onion *onion = onion3; Onion *onion = onion3;
Networking_Core *net = dht_get_net(onion->dht); Networking_Core *net = onion->net;
DHT *dht = onion->dht; DHT *dht = onion->dht;
kill_onion(onion); kill_onion(onion);
kill_dht(dht); kill_dht(dht);
@@ -371,7 +376,7 @@ static void test_basic(void)
{ {
Onion *onion = onion2; Onion *onion = onion2;
Networking_Core *net = dht_get_net(onion->dht); Networking_Core *net = onion->net;
DHT *dht = onion->dht; DHT *dht = onion->dht;
kill_onion(onion); kill_onion(onion);
kill_dht(dht); kill_dht(dht);
@@ -383,7 +388,7 @@ static void test_basic(void)
{ {
Onion *onion = onion1; Onion *onion = onion1;
Networking_Core *net = dht_get_net(onion->dht); Networking_Core *net = onion->net;
DHT *dht = onion->dht; DHT *dht = onion->dht;
kill_onion(onion); kill_onion(onion);
kill_dht(dht); kill_dht(dht);
@@ -396,6 +401,7 @@ static void test_basic(void)
typedef struct { typedef struct {
Logger *log; Logger *log;
Mono_Time *mono_time; Mono_Time *mono_time;
Net_Crypto *nc;
Net_Profile *tcp_np; Net_Profile *tcp_np;
Onion *onion; Onion *onion;
Onion_Announce *onion_a; Onion_Announce *onion_a;
@@ -449,7 +455,7 @@ static Onions *new_onions(const Memory *mem, const Random *rng, uint16_t port, u
return nullptr; 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) { if (!on->onion) {
kill_dht(dht); kill_dht(dht);
@@ -460,7 +466,7 @@ static Onions *new_onions(const Memory *mem, const Random *rng, uint16_t port, u
return nullptr; 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) { if (!on->onion_a) {
kill_onion(on->onion); 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}}}}; 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) { if (!on->onion_c) {
netprof_kill(mem, on->tcp_np); 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) 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; 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_client(on->onion_c);
kill_onion_announce(on->onion_a); kill_onion_announce(on->onion_a);
kill_onion(on->onion); kill_onion(on->onion);
@@ -640,9 +647,9 @@ static void test_announce(void)
printf("adding friend\n"); printf("adding friend\n");
int frnum_f = onion_addfriend(onions[NUM_FIRST]->onion_c, 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, 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_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); onion_dht_pk_callback(onions[NUM_LAST]->onion_c, frnum, &dht_pk_callback, onions[NUM_LAST], NUM_LAST);

View File

@@ -6,13 +6,12 @@
*/ */
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <time.h> #include <time.h>
#include "../testing/misc_tools.h" #include "../testing/misc_tools.h"
#include "../toxcore/friend_connection.h" #include "../toxcore/friend_connection.h"
#include "../toxcore/os_random.h"
#include "../toxcore/tox.h" #include "../toxcore/tox.h"
#include "../toxcore/util.h"
#include "check_compat.h" #include "check_compat.h"
#define TOX_COUNT 2 #define TOX_COUNT 2

View File

@@ -8,6 +8,7 @@
#include "../testing/misc_tools.h" #include "../testing/misc_tools.h"
#include "../toxcore/ccompat.h" #include "../toxcore/ccompat.h"
#include "../toxcore/crypto_core.h" #include "../toxcore/crypto_core.h"
#include "../toxcore/os_random.h"
#include "../toxcore/tox.h" #include "../toxcore/tox.h"
#include "auto_test_support.h" #include "auto_test_support.h"
#include "check_compat.h" #include "check_compat.h"

View File

@@ -2,14 +2,13 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include "../testing/misc_tools.h" #include "../testing/misc_tools.h"
#include "../toxcore/crypto_core.h" #include "../toxcore/crypto_core.h"
#include "../toxcore/os_random.h"
#include "../toxcore/tox.h" #include "../toxcore/tox.h"
#include "../toxcore/util.h"
#include "auto_test_support.h" #include "auto_test_support.h"
#include "check_compat.h" #include "check_compat.h"

View File

@@ -2,14 +2,13 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include "../testing/misc_tools.h" #include "../testing/misc_tools.h"
#include "../toxcore/crypto_core.h" #include "../toxcore/crypto_core.h"
#include "../toxcore/os_random.h"
#include "../toxcore/tox.h" #include "../toxcore/tox.h"
#include "../toxcore/util.h"
#include "auto_test_support.h" #include "auto_test_support.h"
#include "check_compat.h" #include "check_compat.h"

View File

@@ -26,6 +26,8 @@
#include "../toxcore/network.h" #include "../toxcore/network.h"
#include "../toxcore/onion.h" #include "../toxcore/onion.h"
#include "../toxcore/onion_announce.h" #include "../toxcore/onion_announce.h"
#include "../toxcore/os_memory.h"
#include "../toxcore/os_random.h"
#include "../toxcore/tox.h" #include "../toxcore/tox.h"
#define TCP_RELAY_ENABLED #define TCP_RELAY_ENABLED
@@ -157,14 +159,15 @@ int main(int argc, char *argv[])
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr); Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
const uint16_t start_port = PORT; const uint16_t start_port = PORT;
const uint16_t end_port = start_port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM); 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); Networking_Core *net = new_networking_ex(logger, mem, ns, &ip, start_port, end_port, nullptr);
Onion *onion = new_onion(logger, mem, mono_time, rng, dht); DHT *dht = new_dht(logger, mem, rng, ns, mono_time, net, true, true);
Forwarding *forwarding = new_forwarding(logger, mem, rng, mono_time, dht); 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); 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 #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 #endif
if (onion == nullptr || forwarding == nullptr || onion_a == nullptr) { if (onion == nullptr || forwarding == nullptr || onion_a == nullptr) {
@@ -214,7 +217,7 @@ int main(int argc, char *argv[])
fclose(file); fclose(file);
printf("\n"); 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) { if (argc > argvoffset + 3) {
printf("Trying to bootstrap into the network...\n"); printf("Trying to bootstrap into the network...\n");
@@ -258,7 +261,7 @@ int main(int argc, char *argv[])
do_dht(dht); do_dht(dht);
if (mono_time_is_timeout(mono_time, last_lan_discovery, is_waiting_for_dht_connection ? 5 : LAN_DISCOVERY_INTERVAL)) { 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); last_lan_discovery = mono_time_get(mono_time);
} }
@@ -267,7 +270,7 @@ int main(int argc, char *argv[])
#ifdef TCP_RELAY_ENABLED #ifdef TCP_RELAY_ENABLED
do_tcp_server(tcp_s, mono_time); do_tcp_server(tcp_s, mono_time);
#endif #endif
networking_poll(dht_get_net(dht), nullptr); networking_poll(net, nullptr);
c_sleep(1); c_sleep(1);
} }

View File

@@ -24,6 +24,8 @@ CPPCHECK+=("--suppress=knownConditionTrueFalse")
CPPCHECK+=("--suppress=missingIncludeSystem") CPPCHECK+=("--suppress=missingIncludeSystem")
# TODO(iphydf): Maybe fix? # TODO(iphydf): Maybe fix?
CPPCHECK+=("--suppress=signConversion") CPPCHECK+=("--suppress=signConversion")
# We have suppressions in the code for the misra extension.
CPPCHECK+=("--suppress=unmatchedSuppression")
# We use this for VLAs. # We use this for VLAs.
CPPCHECK_CXX+=("--suppress=allocaCalled") CPPCHECK_CXX+=("--suppress=allocaCalled")

View File

@@ -25,6 +25,7 @@ cc_binary(
"//c-toxcore/toxcore:network", "//c-toxcore/toxcore:network",
"//c-toxcore/toxcore:onion", "//c-toxcore/toxcore:onion",
"//c-toxcore/toxcore:onion_announce", "//c-toxcore/toxcore:onion_announce",
"//c-toxcore/toxcore:os_random",
"//c-toxcore/toxcore:tox", "//c-toxcore/toxcore:tox",
"@libconfig", "@libconfig",
], ],

View File

@@ -1,6 +1,6 @@
########################################################### ###########################################################
# Builder image: we compile the code here (static build) # 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",\ RUN ["apk", "--no-cache", "add",\
"clang",\ "clang",\
@@ -26,13 +26,12 @@ COPY other/bootstrap_node_packets.[ch] other/
COPY other/DHT_bootstrap.c other/ COPY other/DHT_bootstrap.c other/
COPY other/pkgconfig other/pkgconfig COPY other/pkgconfig other/pkgconfig
COPY other/rpm other/rpm COPY other/rpm other/rpm
COPY testing/misc_tools.[ch] testing/ COPY testing testing
COPY toxcore toxcore COPY toxcore toxcore
COPY toxencryptsave toxencryptsave COPY toxencryptsave toxencryptsave
COPY third_party third_party COPY third_party third_party
COPY CMakeLists.txt so.version ./ COPY CMakeLists.txt so.version ./
COPY other/bootstrap_daemon/CMakeLists.txt other/bootstrap_daemon/CMakeLists.txt 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. \ RUN CC=clang cmake -B_build -H. \
-GNinja \ -GNinja \
@@ -40,19 +39,19 @@ RUN CC=clang cmake -B_build -H. \
-DCMAKE_UNITY_BUILD=ON \ -DCMAKE_UNITY_BUILD=ON \
-DCMAKE_BUILD_TYPE=Release \ -DCMAKE_BUILD_TYPE=Release \
-DFULLY_STATIC=ON \ -DFULLY_STATIC=ON \
-DMIN_LOGGER_LEVEL=DEBUG \ -DMIN_LOGGER_LEVEL=TRACE \
-DBUILD_TOXAV=OFF \ -DBUILD_TOXAV=OFF \
-DBOOTSTRAP_DAEMON=ON && \ -DBOOTSTRAP_DAEMON=ON \
cmake --build _build --target install && cmake --build _build --target install
# Verify checksum from dev-built binary, so we can be sure Docker Hub doesn't # Verify checksum from dev-built binary, so we can be sure Docker Hub doesn't
# mess with your binaries. # mess with your binaries.
COPY other/bootstrap_daemon/docker/tox-bootstrapd.sha256 other/bootstrap_daemon/docker/ COPY other/bootstrap_daemon/docker/tox-bootstrapd.sha256 other/bootstrap_daemon/docker/
ARG CHECK=sha256sum ARG CHECK=sha256sum
RUN SHA256="$("$CHECK" /usr/local/bin/tox-bootstrapd)" && \ RUN SHA256="$("$CHECK" /usr/local/bin/tox-bootstrapd)" \
("$CHECK" -c other/bootstrap_daemon/docker/tox-bootstrapd.sha256 || \ && ("$CHECK" -c other/bootstrap_daemon/docker/tox-bootstrapd.sha256 || \
(echo "::error file=other/bootstrap_daemon/docker/tox-bootstrapd.sha256,line=1::$SHA256" && \ (echo "::error file=other/bootstrap_daemon/docker/tox-bootstrapd.sha256,line=1::$SHA256" \
false)) && false))
# Remove all the example bootstrap nodes from the config file. # Remove all the example bootstrap nodes from the config file.
COPY other/bootstrap_daemon/tox-bootstrapd.conf other/bootstrap_daemon/ 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 /usr/local/bin/tox-bootstrapd /usr/local/bin/
COPY --from=build /src/c-toxcore/other/bootstrap_daemon/tox-bootstrapd.conf /etc/tox-bootstrapd.conf 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 \ RUN useradd \
--home-dir /var/lib/tox-bootstrapd \
--create-home \
--system --shell /sbin/nologin \ --system --shell /sbin/nologin \
--comment "Account to run the Tox DHT bootstrap daemon" \ --comment "Account to run the Tox DHT bootstrap daemon" \
--user-group tox-bootstrapd && \ --user-group tox-bootstrapd \
chmod 644 /etc/tox-bootstrapd.conf && \ && chmod 644 /etc/tox-bootstrapd.conf \
chmod 700 /var/lib/tox-bootstrapd && chmod 700 /var/lib/tox-bootstrapd
WORKDIR /var/lib/tox-bootstrapd WORKDIR /var/lib/tox-bootstrapd

View File

@@ -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

View File

@@ -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 \
.

View File

@@ -9,7 +9,7 @@
*/ */
#include "command_line_arguments.h" #include "command_line_arguments.h"
#include "global.h" #include "global.h" // IWYU pragma: keep
#include "log.h" #include "log.h"
#include "../../../toxcore/ccompat.h" #include "../../../toxcore/ccompat.h"
@@ -26,7 +26,7 @@ static void print_help(void)
// 2 space indent // 2 space indent
// Make sure all lines fit into 80 columns // Make sure all lines fit into 80 columns
// Make sure options are listed in alphabetical order // 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" "Usage: tox-bootstrapd [OPTION]... --config=FILE_PATH\n"
"\n" "\n"
"Options:\n" "Options:\n"
@@ -43,6 +43,7 @@ static void print_help(void)
" Default option when no --log-backend is\n" " Default option when no --log-backend is\n"
" specified.\n" " specified.\n"
" stdout Writes log messages to stdout/stderr.\n" " stdout Writes log messages to stdout/stderr.\n"
" --trace Enable verbose network trace logging in toxcore.\n"
" --version Print version information.\n"); " --version Print version information.\n");
} }
@@ -51,7 +52,7 @@ Cli_Status handle_command_line_arguments(
bool *run_in_foreground) bool *run_in_foreground)
{ {
if (argc < 2) { 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(); print_help();
return CLI_STATUS_ERROR; return CLI_STATUS_ERROR;
} }
@@ -64,6 +65,7 @@ Cli_Status handle_command_line_arguments(
{"help", no_argument, nullptr, 'h'}, {"help", no_argument, nullptr, 'h'},
{"log-backend", required_argument, nullptr, 'l'}, // optional, defaults to syslog {"log-backend", required_argument, nullptr, 'l'}, // optional, defaults to syslog
{"version", no_argument, nullptr, 'v'}, {"version", no_argument, nullptr, 'v'},
{"trace", no_argument, nullptr, 't'},
{nullptr, 0, nullptr, 0 } {nullptr, 0, nullptr, 0 }
}; };
@@ -99,7 +101,7 @@ Cli_Status handle_command_line_arguments(
*log_backend = LOG_BACKEND_STDOUT; *log_backend = LOG_BACKEND_STDOUT;
log_backend_set = true; log_backend_set = true;
} else { } 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(); print_help();
return CLI_STATUS_ERROR; return CLI_STATUS_ERROR;
} }
@@ -107,16 +109,21 @@ Cli_Status handle_command_line_arguments(
break; break;
case 'v': 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; return CLI_STATUS_DONE;
case 't':
LOG_WRITE(LOG_LEVEL_INFO, "Enabling trace logging in toxcore.\n");
log_enable_trace(true);
break;
case '?': 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(); print_help();
return CLI_STATUS_ERROR; return CLI_STATUS_ERROR;
case ':': 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(); print_help();
return CLI_STATUS_ERROR; return CLI_STATUS_ERROR;
} }
@@ -127,7 +134,7 @@ Cli_Status handle_command_line_arguments(
} }
if (!cfg_file_path_set) { 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(); print_help();
return CLI_STATUS_ERROR; return CLI_STATUS_ERROR;
} }

View File

@@ -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); config_setting_t *ports_array = config_lookup(cfg, NAME_TCP_RELAY_PORTS);
if (ports_array == nullptr) { 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, "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, "Using default '%s':\n", NAME_TCP_RELAY_PORTS);
const uint16_t default_ports[] = {DEFAULT_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); const size_t default_ports_count = sizeof(default_ports) / sizeof(*default_ports);
for (size_t i = 0; i < default_ports_count; ++i) { 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 // Similar procedure to the one of reading config file below
*tcp_relay_ports = (uint16_t *)malloc(default_ports_count * sizeof(uint16_t)); *tcp_relay_ports = (uint16_t *)malloc(default_ports_count * sizeof(uint16_t));
if (*tcp_relay_ports == nullptr) { if (*tcp_relay_ports == nullptr) {
log_write(LOG_LEVEL_ERROR, "Allocation failure.\n"); LOG_WRITE(LOG_LEVEL_ERROR, "Allocation failure.\n");
return; 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 if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT
|| (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_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); (*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
continue; 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) { 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); NAME_TCP_RELAY_PORTS);
return; 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); const int config_port_count = config_setting_length(ports_array);
if (config_port_count == 0) { 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; return;
} }
*tcp_relay_ports = (uint16_t *)malloc(config_port_count * sizeof(uint16_t)); *tcp_relay_ports = (uint16_t *)malloc(config_port_count * sizeof(uint16_t));
if (*tcp_relay_ports == nullptr) { if (*tcp_relay_ports == nullptr) {
log_write(LOG_LEVEL_ERROR, "Allocation failure.\n"); LOG_WRITE(LOG_LEVEL_ERROR, "Allocation failure.\n");
return; return;
} }
@@ -110,12 +110,12 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por
if (elem == nullptr) { 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 // 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; break;
} }
if (config_setting_is_number(elem) == CONFIG_FALSE) { 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; 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 if ((*tcp_relay_ports)[*tcp_relay_port_count] < MIN_ALLOWED_PORT
|| (*tcp_relay_ports)[*tcp_relay_port_count] > MAX_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); (*tcp_relay_ports)[*tcp_relay_port_count], MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
continue; 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. // Read the file. If there is an error, report it and exit.
if (config_read_file(&cfg, cfg_file_path) == CONFIG_FALSE) { 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); config_destroy(&cfg);
return false; return false;
} }
// Get port // Get port
if (config_lookup_int(&cfg, NAME_PORT, port) == CONFIG_FALSE) { 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, "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, "Using default '%s': %d\n", NAME_PORT, DEFAULT_PORT);
*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; const char *tmp_pid_file;
if (config_lookup_string(&cfg, NAME_PID_FILE_PATH, &tmp_pid_file) == CONFIG_FALSE) { 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, "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, "Using default '%s': %s\n", NAME_PID_FILE_PATH, DEFAULT_PID_FILE_PATH);
tmp_pid_file = DEFAULT_PID_FILE_PATH; tmp_pid_file = DEFAULT_PID_FILE_PATH;
} }
const size_t pid_file_path_len = strlen(tmp_pid_file) + 1; const size_t pid_file_path_len = strlen(tmp_pid_file) + 1;
*pid_file_path = (char *)malloc(pid_file_path_len); *pid_file_path = (char *)malloc(pid_file_path_len);
if (*pid_file_path == nullptr) { if (*pid_file_path == nullptr) {
log_write(LOG_LEVEL_ERROR, "Allocation failure.\n"); LOG_WRITE(LOG_LEVEL_ERROR, "Allocation failure.\n");
return false; return false;
} }
memcpy(*pid_file_path, tmp_pid_file, pid_file_path_len); 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; const char *tmp_keys_file;
if (config_lookup_string(&cfg, NAME_KEYS_FILE_PATH, &tmp_keys_file) == CONFIG_FALSE) { 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, "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, "Using default '%s': %s\n", NAME_KEYS_FILE_PATH, DEFAULT_KEYS_FILE_PATH);
tmp_keys_file = DEFAULT_KEYS_FILE_PATH; tmp_keys_file = DEFAULT_KEYS_FILE_PATH;
} }
const size_t keys_file_path_len = strlen(tmp_keys_file) + 1; const size_t keys_file_path_len = strlen(tmp_keys_file) + 1;
*keys_file_path = (char *)malloc(keys_file_path_len); *keys_file_path = (char *)malloc(keys_file_path_len);
if (*keys_file_path == nullptr) { 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); free(*pid_file_path);
*pid_file_path = nullptr; *pid_file_path = nullptr;
return false; return false;
@@ -219,31 +219,31 @@ bool get_general_config(const char *cfg_file_path, char **pid_file_path, char **
// Get IPv6 option // Get IPv6 option
if (tox_config_lookup_bool(&cfg, NAME_ENABLE_IPV6, enable_ipv6) == CONFIG_FALSE) { 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, "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, "Using default '%s': %s\n", NAME_ENABLE_IPV6, DEFAULT_ENABLE_IPV6 ? "true" : "false");
*enable_ipv6 = DEFAULT_ENABLE_IPV6; *enable_ipv6 = DEFAULT_ENABLE_IPV6;
} }
// Get IPv4 fallback option // Get IPv4 fallback option
if (tox_config_lookup_bool(&cfg, NAME_ENABLE_IPV4_FALLBACK, enable_ipv4_fallback) == CONFIG_FALSE) { 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, "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, "Using default '%s': %s\n", NAME_ENABLE_IPV4_FALLBACK,
DEFAULT_ENABLE_IPV4_FALLBACK ? "true" : "false"); DEFAULT_ENABLE_IPV4_FALLBACK ? "true" : "false");
*enable_ipv4_fallback = DEFAULT_ENABLE_IPV4_FALLBACK; *enable_ipv4_fallback = DEFAULT_ENABLE_IPV4_FALLBACK;
} }
// Get LAN discovery option // Get LAN discovery option
if (tox_config_lookup_bool(&cfg, NAME_ENABLE_LAN_DISCOVERY, enable_lan_discovery) == CONFIG_FALSE) { 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, "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, "Using default '%s': %s\n", NAME_ENABLE_LAN_DISCOVERY,
DEFAULT_ENABLE_LAN_DISCOVERY ? "true" : "false"); DEFAULT_ENABLE_LAN_DISCOVERY ? "true" : "false");
*enable_lan_discovery = DEFAULT_ENABLE_LAN_DISCOVERY; *enable_lan_discovery = DEFAULT_ENABLE_LAN_DISCOVERY;
} }
// Get TCP relay option // Get TCP relay option
if (tox_config_lookup_bool(&cfg, NAME_ENABLE_TCP_RELAY, enable_tcp_relay) == CONFIG_FALSE) { 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, "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, "Using default '%s': %s\n", NAME_ENABLE_TCP_RELAY,
DEFAULT_ENABLE_TCP_RELAY ? "true" : "false"); DEFAULT_ENABLE_TCP_RELAY ? "true" : "false");
*enable_tcp_relay = DEFAULT_ENABLE_TCP_RELAY; *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 // Get MOTD option
if (tox_config_lookup_bool(&cfg, NAME_ENABLE_MOTD, enable_motd) == CONFIG_FALSE) { 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, "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, "Using default '%s': %s\n", NAME_ENABLE_MOTD,
DEFAULT_ENABLE_MOTD ? "true" : "false"); DEFAULT_ENABLE_MOTD ? "true" : "false");
*enable_motd = DEFAULT_ENABLE_MOTD; *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; const char *tmp_motd;
if (config_lookup_string(&cfg, NAME_MOTD, &tmp_motd) == CONFIG_FALSE) { 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, "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, "Using default '%s': %s\n", NAME_MOTD, DEFAULT_MOTD);
tmp_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); config_destroy(&cfg);
log_write(LOG_LEVEL_INFO, "Successfully read:\n"); 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_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': %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': %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_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_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_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 // Show info about tcp ports only if tcp relay is enabled
if (*enable_tcp_relay) { if (*enable_tcp_relay) {
if (*tcp_relay_port_count == 0) { 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 { } 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) { 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) { 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; 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; const size_t len = strlen(hex_string) / 2;
uint8_t *ret = (uint8_t *)malloc(len); uint8_t *ret = (uint8_t *)malloc(len);
if (ret == nullptr) { if (ret == nullptr) {
log_write(LOG_LEVEL_ERROR, "Allocation failure.\n"); LOG_WRITE(LOG_LEVEL_ERROR, "Allocation failure.\n");
return nullptr; return nullptr;
} }
@@ -358,7 +358,7 @@ bool bootstrap_from_config(const char *cfg_file_path, DHT *dht, bool enable_ipv6
config_init(&cfg); config_init(&cfg);
if (config_read_file(&cfg, cfg_file_path) == CONFIG_FALSE) { 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); config_destroy(&cfg);
return false; 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); config_setting_t *node_list = config_lookup(&cfg, NAME_BOOTSTRAP_NODES);
if (node_list == nullptr) { 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); NAME_BOOTSTRAP_NODES);
config_destroy(&cfg); config_destroy(&cfg);
return true; return true;
} }
if (config_setting_length(node_list) == 0) { 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); config_destroy(&cfg);
return true; 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 // Check that all settings are present
if (config_setting_lookup_string(node, NAME_PUBLIC_KEY, &bs_public_key) == CONFIG_FALSE) { 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); NAME_PUBLIC_KEY);
goto next; goto next;
} }
if (config_setting_lookup_int(node, NAME_PORT, &bs_port) == CONFIG_FALSE) { 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; goto next;
} }
if (config_setting_lookup_string(node, NAME_ADDRESS, &bs_address) == CONFIG_FALSE) { 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; goto next;
} }
// Process settings // Process settings
if (strlen(bs_public_key) != CRYPTO_PUBLIC_KEY_SIZE * 2) { 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); bs_public_key);
goto next; goto next;
} }
if (bs_port < MIN_ALLOWED_PORT || bs_port > MAX_ALLOWED_PORT) { 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, NAME_PORT,
bs_port, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT); bs_port, MIN_ALLOWED_PORT, MAX_ALLOWED_PORT);
goto next; 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); free(bs_public_key_bin);
if (!address_resolved) { 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; 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: next:
// config_setting_lookup_string() allocates string inside and doesn't allow us to free it directly // config_setting_lookup_string() allocates string inside and doesn't allow us to free it directly

View File

@@ -15,6 +15,12 @@
#define INVALID_BACKEND ((LOG_BACKEND)-1u) #define INVALID_BACKEND ((LOG_BACKEND)-1u)
static LOG_BACKEND current_backend = INVALID_BACKEND; 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) bool log_open(LOG_BACKEND backend)
{ {
@@ -58,22 +64,27 @@ bool log_close(void)
return true; 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) { if (current_backend == INVALID_BACKEND) {
return false; return false;
} }
if (level == LOG_LEVEL_TRACE && !log_toxcore_trace) {
// By default, no trace logging.
return true;
}
va_list args; va_list args;
va_start(args, format); va_start(args, format);
switch (current_backend) { switch (current_backend) {
case LOG_BACKEND_STDOUT: case LOG_BACKEND_STDOUT:
log_backend_stdout_write(level, format, args); log_backend_stdout_write(level, category, file, line, format, args);
break; break;
case LOG_BACKEND_SYSLOG: case LOG_BACKEND_SYSLOG:
log_backend_syslog_write(level, format, args); log_backend_syslog_write(level, category, file, line, format, args);
break; break;
} }

View File

@@ -20,11 +20,18 @@ typedef enum LOG_BACKEND {
} LOG_BACKEND; } LOG_BACKEND;
typedef enum LOG_LEVEL { typedef enum LOG_LEVEL {
LOG_LEVEL_TRACE,
LOG_LEVEL_INFO, LOG_LEVEL_INFO,
LOG_LEVEL_WARNING, LOG_LEVEL_WARNING,
LOG_LEVEL_ERROR LOG_LEVEL_ERROR
} LOG_LEVEL; } 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. * Initializes logger.
* @param backend Specifies which backend to use. * @param backend Specifies which backend to use.
@@ -45,6 +52,13 @@ bool log_close(void);
* @param ... Zero or more arguments, similar to printf function. * @param ... Zero or more arguments, similar to printf function.
* @return true on success, false if log is closed. * @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 #endif // C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_LOG_H

View File

@@ -11,12 +11,15 @@
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <sys/time.h>
#include "../../../toxcore/ccompat.h"
#include "log.h" #include "log.h"
static FILE *log_backend_stdout_level(LOG_LEVEL level) static FILE *log_backend_stdout_level(LOG_LEVEL level)
{ {
switch (level) { switch (level) {
case LOG_LEVEL_TRACE: // intentional fallthrough
case LOG_LEVEL_INFO: case LOG_LEVEL_INFO:
return stdout; return stdout;
@@ -28,8 +31,36 @@ static FILE *log_backend_stdout_level(LOG_LEVEL level)
return stdout; 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); switch (level) {
fflush(log_backend_stdout_level(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);
} }

View File

@@ -15,6 +15,6 @@
#include "../../../toxcore/attributes.h" #include "../../../toxcore/attributes.h"
#include "log.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 #endif // C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_LOG_BACKEND_STDOUT_H

View File

@@ -33,6 +33,9 @@ void log_backend_syslog_close(void)
static int log_backend_syslog_level(LOG_LEVEL level) static int log_backend_syslog_level(LOG_LEVEL level)
{ {
switch (level) { switch (level) {
case LOG_LEVEL_TRACE:
return LOG_DEBUG;
case LOG_LEVEL_INFO: case LOG_LEVEL_INFO:
return LOG_INFO; return LOG_INFO;
@@ -46,7 +49,7 @@ static int log_backend_syslog_level(LOG_LEVEL level)
return LOG_INFO; 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; 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); 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); free(buf);
} }

View File

@@ -17,6 +17,6 @@
void log_backend_syslog_open(void); void log_backend_syslog_open(void);
void log_backend_syslog_close(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 #endif // C_TOXCORE_OTHER_BOOTSTRAP_DAEMON_SRC_LOG_BACKEND_SYSLOG_H

View File

@@ -36,11 +36,12 @@
#include "../../../toxcore/group_announce.h" #include "../../../toxcore/group_announce.h"
#include "../../../toxcore/group_onion_announce.h" #include "../../../toxcore/group_onion_announce.h"
#include "../../../toxcore/logger.h" #include "../../../toxcore/logger.h"
#include "../../../toxcore/mem.h"
#include "../../../toxcore/mono_time.h" #include "../../../toxcore/mono_time.h"
#include "../../../toxcore/network.h" #include "../../../toxcore/network.h"
#include "../../../toxcore/onion.h" #include "../../../toxcore/onion.h"
#include "../../../toxcore/onion_announce.h" #include "../../../toxcore/onion_announce.h"
#include "../../../toxcore/os_memory.h"
#include "../../../toxcore/os_random.h"
// misc // misc
#include "../../bootstrap_node_packets.h" #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]); 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 // 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"); FILE *pid_file = fopen(pid_file_path, "r");
if (pid_file != nullptr) { 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); 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+"); pid_file = fopen(pid_file_path, "a+");
if (pid_file == nullptr) { 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; return CLI_STATUS_ERROR;
} }
@@ -146,26 +147,26 @@ static Cli_Status daemonize(LOG_BACKEND log_backend, char *pid_file_path)
if (pid > 0) { if (pid > 0) {
fprintf(pid_file, "%d", pid); fprintf(pid_file, "%d", pid);
fclose(pid_file); 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; return CLI_STATUS_DONE;
} else { } else {
fclose(pid_file); fclose(pid_file);
} }
if (pid < 0) { 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; return CLI_STATUS_ERROR;
} }
// Create a new SID for the child process // Create a new SID for the child process
if (setsid() < 0) { 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; return CLI_STATUS_ERROR;
} }
// Change the current working directory // Change the current working directory
if ((chdir("/")) < 0) { 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; return CLI_STATUS_ERROR;
} }
@@ -185,6 +186,8 @@ static LOG_LEVEL logger_level_to_log_level(Logger_Level level)
{ {
switch (level) { switch (level) {
case LOGGER_LEVEL_TRACE: case LOGGER_LEVEL_TRACE:
return LOG_LEVEL_TRACE;
case LOGGER_LEVEL_DEBUG: case LOGGER_LEVEL_DEBUG:
case LOGGER_LEVEL_INFO: case LOGGER_LEVEL_INFO:
return LOG_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, static void toxcore_logger_callback(void *context, Logger_Level level, const char *file, uint32_t line,
const char *func, const char *message, void *userdata) 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; static volatile sig_atomic_t caught_signal = 0;
@@ -235,7 +242,7 @@ int main(int argc, char *argv[])
log_open(log_backend); 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 *pid_file_path = nullptr;
char *keys_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, 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)) { &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 { } 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; return 1;
} }
if (start_port < MIN_ALLOWED_PORT || start_port > MAX_ALLOWED_PORT) { 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); MAX_ALLOWED_PORT);
free(motd); free(motd);
free(tcp_relay_ports); free(tcp_relay_ports);
@@ -283,28 +290,26 @@ int main(int argc, char *argv[])
IP ip; IP ip;
ip_init(&ip, enable_ipv6); ip_init(&ip, enable_ipv6);
const Memory *mem = os_memory(); const Tox_Memory *mem = os_memory();
const Random *rng = os_random(); const Tox_Random *rng = os_random();
const Network *ns = os_network(); const Network *ns = os_network();
Logger *logger = logger_new(mem); 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); 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); Networking_Core *net = new_networking_ex(logger, mem, ns, &ip, start_port, end_port, nullptr);
if (net == nullptr) { if (net == nullptr) {
if (enable_ipv6 && enable_ipv4_fallback) { 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; enable_ipv6 = false;
ip_init(&ip, enable_ipv6); ip_init(&ip, enable_ipv6);
net = new_networking_ex(logger, mem, ns, &ip, start_port, end_port, nullptr); net = new_networking_ex(logger, mem, ns, &ip, start_port, end_port, nullptr);
if (net == 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); logger_kill(logger);
free(motd); free(motd);
free(tcp_relay_ports); free(tcp_relay_ports);
@@ -312,7 +317,7 @@ int main(int argc, char *argv[])
return 1; return 1;
} }
} else { } 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); logger_kill(logger);
free(motd); free(motd);
free(tcp_relay_ports); 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); Mono_Time *const mono_time = mono_time_new(mem, nullptr, nullptr);
if (mono_time == 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); kill_networking(net);
logger_kill(logger); logger_kill(logger);
free(motd); 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); DHT *const dht = new_dht(logger, mem, rng, ns, mono_time, net, true, enable_lan_discovery);
if (dht == nullptr) { 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); mono_time_free(mem, mono_time);
kill_networking(net); kill_networking(net);
logger_kill(logger); logger_kill(logger);
@@ -348,10 +353,10 @@ int main(int argc, char *argv[])
return 1; 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) { 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); kill_dht(dht);
mono_time_free(mem, mono_time); mono_time_free(mem, mono_time);
kill_networking(net); kill_networking(net);
@@ -362,10 +367,10 @@ int main(int argc, char *argv[])
return 1; 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) { 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_forwarding(forwarding);
kill_dht(dht); kill_dht(dht);
mono_time_free(mem, mono_time); 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); GC_Announces_List *group_announce = new_gca_list(mem);
if (group_announce == nullptr) { 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_announcements(announce);
kill_forwarding(forwarding); kill_forwarding(forwarding);
kill_dht(dht); kill_dht(dht);
@@ -393,10 +398,10 @@ int main(int argc, char *argv[])
return 1; 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) { 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_gca(group_announce);
kill_announcements(announce); kill_announcements(announce);
kill_forwarding(forwarding); kill_forwarding(forwarding);
@@ -410,10 +415,10 @@ int main(int argc, char *argv[])
return 1; 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) { 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_gca(group_announce);
kill_onion(onion); kill_onion(onion);
kill_announcements(announce); kill_announcements(announce);
@@ -431,11 +436,11 @@ int main(int argc, char *argv[])
gca_onion_init(group_announce, onion_a); gca_onion_init(group_announce, onion_a);
if (enable_motd) { if (enable_motd) {
if (bootstrap_set_callbacks(dht_get_net(dht), DAEMON_VERSION_NUMBER, (uint8_t *)motd, strlen(motd) + 1) == 0) { if (bootstrap_set_callbacks(net, DAEMON_VERSION_NUMBER, (uint8_t *)motd, strlen(motd) + 1) == 0) {
log_write(LOG_LEVEL_INFO, "Set MOTD successfully.\n"); LOG_WRITE(LOG_LEVEL_INFO, "Set MOTD successfully.\n");
free(motd); free(motd);
} else { } 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_onion_announce(onion_a);
kill_gca(group_announce); kill_gca(group_announce);
kill_onion(onion); kill_onion(onion);
@@ -453,10 +458,10 @@ int main(int argc, char *argv[])
} }
if (manage_keys(dht, keys_file_path)) { 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); free(keys_file_path);
} else { } 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_onion_announce(onion_a);
kill_gca(group_announce); kill_gca(group_announce);
kill_onion(onion); kill_onion(onion);
@@ -475,7 +480,7 @@ int main(int argc, char *argv[])
if (enable_tcp_relay) { if (enable_tcp_relay) {
if (tcp_relay_port_count == 0) { 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_onion_announce(onion_a);
kill_gca(group_announce); kill_gca(group_announce);
kill_announcements(announce); kill_announcements(announce);
@@ -496,7 +501,7 @@ int main(int argc, char *argv[])
free(tcp_relay_ports); free(tcp_relay_ports);
if (tcp_server != nullptr) { 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; struct rlimit limit;
@@ -515,14 +520,14 @@ int main(int argc, char *argv[])
} }
if (getrlimit(RLIMIT_NOFILE, &limit) == 0 && limit.rlim_cur < rlim_min) { 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. " "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. " "Consider raising the limit to at least %ju or the recommended %ju. "
"Continuing using the current limit (%ju).\n", "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); (uintmax_t)limit.rlim_cur, (uintmax_t)rlim_min, (uintmax_t)rlim_suggested, (uintmax_t)limit.rlim_cur);
} }
} else { } 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_onion_announce(onion_a);
kill_gca(group_announce); kill_gca(group_announce);
kill_onion(onion); kill_onion(onion);
@@ -537,9 +542,9 @@ int main(int argc, char *argv[])
} }
if (bootstrap_from_config(cfg_file_path, dht, enable_ipv6)) { 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 { } 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_tcp_server(tcp_server);
kill_onion_announce(onion_a); kill_onion_announce(onion_a);
kill_gca(group_announce); kill_gca(group_announce);
@@ -564,7 +569,7 @@ int main(int argc, char *argv[])
if (enable_lan_discovery) { if (enable_lan_discovery) {
broadcast = lan_discovery_init(mem, ns); 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; struct sigaction sa;
@@ -578,11 +583,11 @@ int main(int argc, char *argv[])
sigfillset(&sa.sa_mask); sigfillset(&sa.sa_mask);
if (sigaction(SIGINT, &sa, nullptr) != 0) { 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) { 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) { while (caught_signal == 0) {
@@ -591,7 +596,7 @@ int main(int argc, char *argv[])
do_dht(dht); do_dht(dht);
if (enable_lan_discovery && mono_time_is_timeout(mono_time, last_lan_discovery, LAN_DISCOVERY_INTERVAL)) { 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); 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); 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)) { 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; waiting_for_dht_connection = false;
} }
@@ -613,15 +618,15 @@ int main(int argc, char *argv[])
switch (caught_signal) { switch (caught_signal) {
case SIGINT: 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; break;
case SIGTERM: 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; break;
default: 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); lan_discovery_kill(broadcast);

View File

@@ -1,6 +1,6 @@
# Stage 1 - Compile websockify. # Stage 1 - Compile websockify.
FROM toxchat/bootstrap-node:latest AS tox 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 COPY websockify /work/websockify
RUN cd /work/websockify && go mod download github.com/gorilla/websocket && go install 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 \ #RUN /usr/local/bin/tox-bootstrapd --config /etc/tox-bootstrapd.conf --log-backend stdout \
# && sleep 1 # && sleep 1
COPY tox-bootstrapd.conf /etc/
WORKDIR /web WORKDIR /web
CMD ["/entrypoint.sh"] CMD ["/entrypoint.sh"]

View File

@@ -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 .

View File

@@ -2,5 +2,5 @@
set -eux 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 /usr/local/bin/websockify -l "0.0.0.0:$PORT" -t 127.0.0.1:33445

View File

@@ -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 = ()

View File

@@ -2,5 +2,5 @@
set -eux set -eux
BUILD=autotools BUILD=autotools
other/docker/sources/build other/docker/sources/build.sh
docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" . docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" .

View File

@@ -2,5 +2,5 @@
set -eux set -eux
BUILD=clang-tidy 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" . docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" .

View File

@@ -2,5 +2,5 @@
set -eux set -eux
BUILD=compcert BUILD=compcert
other/docker/sources/build other/docker/sources/build.sh
docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" . docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" .

View File

@@ -5,7 +5,7 @@ set -eux
read -a ci_env <<<"$(bash <(curl -s https://codecov.io/env))" read -a ci_env <<<"$(bash <(curl -s https://codecov.io/env))"
BUILD=coverage 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 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 docker run "${ci_env[@]}" -e CI=true --name toxcore-coverage --rm -t toxchat/c-toxcore:coverage /usr/local/bin/codecov

View File

@@ -2,5 +2,5 @@
set -eux set -eux
BUILD=cppcheck BUILD=cppcheck
other/docker/sources/build other/docker/sources/build.sh
docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" . docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" .

View File

@@ -2,5 +2,5 @@
set -eux set -eux
BUILD=goblint BUILD=goblint
other/docker/sources/build other/docker/sources/build.sh
docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" . docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" .

View File

@@ -2,5 +2,5 @@
set -eux set -eux
BUILD=infer BUILD=infer
other/docker/sources/build other/docker/sources/build.sh
docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" . docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" .

View File

@@ -2,5 +2,5 @@
set -eux set -eux
BUILD=misra BUILD=misra
other/docker/sources/build other/docker/sources/build.sh
docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" . docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" .

View File

@@ -6,7 +6,7 @@ PROJECT_COMMIT_ID="$(git rev-parse HEAD)"
PROJECT_COMMIT_ID_SHORT="$(git rev-parse --short HEAD)" PROJECT_COMMIT_ID_SHORT="$(git rev-parse --short HEAD)"
BUILD=rpm BUILD=rpm
other/docker/sources/build other/docker/sources/build.sh
docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" . \ docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" . \
--build-arg="PROJECT_VERSION=$PROJECT_VERSION" \ --build-arg="PROJECT_VERSION=$PROJECT_VERSION" \
--build-arg="PROJECT_COMMIT_ID=$PROJECT_COMMIT_ID" \ --build-arg="PROJECT_COMMIT_ID=$PROJECT_COMMIT_ID" \

View File

@@ -2,5 +2,5 @@
set -eux set -eux
BUILD=slimcc BUILD=slimcc
other/docker/sources/build other/docker/sources/build.sh
docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" . docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" .

View File

@@ -2,5 +2,5 @@
set -eux set -eux
BUILD=sparse BUILD=sparse
other/docker/sources/build other/docker/sources/build.sh
docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" . docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" .

View File

@@ -2,5 +2,5 @@
set -eux set -eux
BUILD=tcc BUILD=tcc
other/docker/sources/build other/docker/sources/build.sh
docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" . docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" .

View File

@@ -2,5 +2,5 @@
set -eux set -eux
BUILD=tokstyle BUILD=tokstyle
other/docker/sources/build other/docker/sources/build.sh
docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" . docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/$BUILD.Dockerfile" .

View File

@@ -87,6 +87,8 @@ cc_binary(
"//c-toxcore/toxcore:Messenger", "//c-toxcore/toxcore:Messenger",
"//c-toxcore/toxcore:ccompat", "//c-toxcore/toxcore:ccompat",
"//c-toxcore/toxcore:mono_time", "//c-toxcore/toxcore:mono_time",
"//c-toxcore/toxcore:os_memory",
"//c-toxcore/toxcore:os_random",
], ],
) )

View File

@@ -35,6 +35,8 @@
#include "../toxcore/Messenger.h" #include "../toxcore/Messenger.h"
#include "../toxcore/ccompat.h" #include "../toxcore/ccompat.h"
#include "../toxcore/mono_time.h" #include "../toxcore/mono_time.h"
#include "../toxcore/os_memory.h"
#include "../toxcore/os_random.h"
#include "misc_tools.h" #include "misc_tools.h"
static void print_message(Messenger *m, uint32_t friendnumber, unsigned int type, const uint8_t *string, size_t length, static void print_message(Messenger *m, uint32_t friendnumber, unsigned int type, const uint8_t *string, size_t length,

View File

@@ -24,7 +24,9 @@
#include "../../toxcore/crypto_core.h" #include "../../toxcore/crypto_core.h"
#include "../../toxcore/network.h" #include "../../toxcore/network.h"
#include "../../toxcore/tox_memory_impl.h"
#include "../../toxcore/tox_private.h" #include "../../toxcore/tox_private.h"
#include "../../toxcore/tox_random_impl.h"
#include "func_conversion.hh" #include "func_conversion.hh"
// TODO(iphydf): Put this somewhere shared. // TODO(iphydf): Put this somewhere shared.
@@ -33,8 +35,8 @@ struct Network_Addr {
size_t size; size_t size;
}; };
System::System(std::unique_ptr<Tox_System> in_sys, std::unique_ptr<Memory> in_mem, System::System(std::unique_ptr<Tox_System> in_sys, std::unique_ptr<Tox_Memory> in_mem,
std::unique_ptr<Network> in_ns, std::unique_ptr<Random> in_rng) std::unique_ptr<Network> in_ns, std::unique_ptr<Tox_Random> in_rng)
: sys(std::move(in_sys)) : sys(std::move(in_sys))
, mem(std::move(in_mem)) , mem(std::move(in_mem))
, ns(std::move(in_ns)) , 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...)); return report_alloc("tox1", func, size, Func(args...));
} }
static constexpr Memory_Funcs fuzz_memory_funcs = { static constexpr Tox_Memory_Funcs fuzz_memory_funcs = {
/* .malloc = */ /* .malloc = */
![](Fuzz_System *self, uint32_t size) { ![](Fuzz_System *self, uint32_t size) {
return alloc_common<decltype(std::malloc), std::malloc>("malloc", size, self->data, size); return alloc_common<decltype(std::malloc), std::malloc>("malloc", size, self->data, size);
}, },
/* .calloc = */
![](Fuzz_System *self, uint32_t nmemb, uint32_t size) {
return alloc_common<decltype(std::calloc), std::calloc>(
"calloc", nmemb * size, self->data, nmemb, size);
},
/* .realloc = */ /* .realloc = */
![](Fuzz_System *self, void *ptr, uint32_t size) { ![](Fuzz_System *self, void *ptr, uint32_t size) {
return alloc_common<decltype(std::realloc), std::realloc>( return alloc_common<decltype(std::realloc), std::realloc>(
"realloc", size, self->data, ptr, size); "realloc", size, self->data, ptr, size);
}, },
/* .free = */ /* .dealloc = */
![](Fuzz_System *self, void *ptr) { std::free(ptr); }, ![](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 = { static constexpr Tox_Random_Funcs fuzz_random_funcs = {
/* .random_bytes = */ /* .bytes_callback = */
![](Fuzz_System *self, uint8_t *bytes, size_t length) { ![](Fuzz_System *self, uint8_t *bytes, size_t length) {
// Initialize the buffer with zeros in case there's no randomness left. // Initialize the buffer with zeros in case there's no randomness left.
std::fill_n(bytes, length, 0); 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) { ![](Fuzz_System *self, uint32_t upper_bound) {
uint32_t randnum = 0; uint32_t randnum = 0;
if (upper_bound > 0) { if (upper_bound > 0) {
self->rng->funcs->random_bytes( self->rng->funcs->bytes_callback(
self, reinterpret_cast<uint8_t *>(&randnum), sizeof(randnum)); self, reinterpret_cast<uint8_t *>(&randnum), sizeof(randnum));
randnum %= upper_bound; randnum %= upper_bound;
} }
@@ -231,9 +228,9 @@ static constexpr Random_Funcs fuzz_random_funcs = {
Fuzz_System::Fuzz_System(Fuzz_Data &input) Fuzz_System::Fuzz_System(Fuzz_Data &input)
: System{ : System{
std::make_unique<Tox_System>(), std::make_unique<Tox_System>(),
std::make_unique<Memory>(Memory{&fuzz_memory_funcs, this}), std::make_unique<Tox_Memory>(Tox_Memory{&fuzz_memory_funcs, this}),
std::make_unique<Network>(Network{&fuzz_network_funcs, this}), std::make_unique<Network>(Network{&fuzz_network_funcs, this}),
std::make_unique<Random>(Random{&fuzz_random_funcs, this}), std::make_unique<Tox_Random>(Tox_Random{&fuzz_random_funcs, this}),
} }
, data(input) , data(input)
{ {
@@ -244,14 +241,12 @@ Fuzz_System::Fuzz_System(Fuzz_Data &input)
sys->rng = rng.get(); sys->rng = rng.get();
} }
static constexpr Memory_Funcs null_memory_funcs = { static constexpr Tox_Memory_Funcs null_memory_funcs = {
/* .malloc = */ /* .malloc = */
![](Null_System *self, uint32_t size) { return std::malloc(size); }, ![](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 = */ /* .realloc = */
![](Null_System *self, void *ptr, uint32_t size) { return std::realloc(ptr, size); }, ![](Null_System *self, void *ptr, uint32_t size) { return std::realloc(ptr, size); },
/* .free = */ /* .dealloc = */
![](Null_System *self, void *ptr) { std::free(ptr); }, ![](Null_System *self, void *ptr) { std::free(ptr); },
}; };
@@ -304,14 +299,14 @@ static uint64_t simple_rng(uint64_t &seed)
return seed; return seed;
} }
static constexpr Random_Funcs null_random_funcs = { static constexpr Tox_Random_Funcs null_random_funcs = {
/* .random_bytes = */ /* .bytes_callback = */
![](Null_System *self, uint8_t *bytes, size_t length) { ![](Null_System *self, uint8_t *bytes, size_t length) {
for (size_t i = 0; i < length; ++i) { for (size_t i = 0; i < length; ++i) {
bytes[i] = simple_rng(self->seed) & 0xff; bytes[i] = simple_rng(self->seed) & 0xff;
} }
}, },
/* .random_uniform = */ /* .uniform_callback = */
![](Null_System *self, uint32_t upper_bound) { ![](Null_System *self, uint32_t upper_bound) {
return static_cast<uint32_t>(simple_rng(self->seed)) % upper_bound; return static_cast<uint32_t>(simple_rng(self->seed)) % upper_bound;
}, },
@@ -320,9 +315,9 @@ static constexpr Random_Funcs null_random_funcs = {
Null_System::Null_System() Null_System::Null_System()
: System{ : System{
std::make_unique<Tox_System>(), std::make_unique<Tox_System>(),
std::make_unique<Memory>(Memory{&null_memory_funcs, this}), std::make_unique<Tox_Memory>(Tox_Memory{&null_memory_funcs, this}),
std::make_unique<Network>(Network{&null_network_funcs, this}), std::make_unique<Network>(Network{&null_network_funcs, this}),
std::make_unique<Random>(Random{&null_random_funcs, this}), std::make_unique<Tox_Random>(Tox_Random{&null_random_funcs, this}),
} }
{ {
sys->mono_time_callback = [](void *self) { return static_cast<Null_System *>(self)->clock; }; sys->mono_time_callback = [](void *self) { return static_cast<Null_System *>(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 = */ /* .malloc = */
![](Record_System *self, uint32_t size) { ![](Record_System *self, uint32_t size) {
self->push(true); self->push(true);
return report_alloc(self->name_, "malloc", size, std::malloc(size)); 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 = */ /* .realloc = */
![](Record_System *self, void *ptr, uint32_t size) { ![](Record_System *self, void *ptr, uint32_t size) {
self->push(true); self->push(true);
return report_alloc(self->name_, "realloc", size, std::realloc(ptr, size)); return report_alloc(self->name_, "realloc", size, std::realloc(ptr, size));
}, },
/* .free = */ /* .dealloc = */
![](Record_System *self, void *ptr) { std::free(ptr); }, ![](Record_System *self, void *ptr) { std::free(ptr); },
}; };
@@ -448,8 +438,8 @@ static constexpr Network_Funcs record_network_funcs = {
size_t optlen) { return 0; }, size_t optlen) { return 0; },
}; };
static constexpr Random_Funcs record_random_funcs = { static constexpr Tox_Random_Funcs record_random_funcs = {
/* .random_bytes = */ /* .bytes_callback = */
![](Record_System *self, uint8_t *bytes, size_t length) { ![](Record_System *self, uint8_t *bytes, size_t length) {
for (size_t i = 0; i < length; ++i) { for (size_t i = 0; i < length; ++i) {
bytes[i] = simple_rng(self->seed_) & 0xff; 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); "%s: rng: %02x..%02x[%zu]\n", self->name_, bytes[0], bytes[length - 1], length);
} }
}, },
/* .random_uniform = */ /* .uniform_callback = */
fuzz_random_funcs.random_uniform, fuzz_random_funcs.uniform_callback,
}; };
Record_System::Record_System(Global &global, uint64_t seed, const char *name) Record_System::Record_System(Global &global, uint64_t seed, const char *name)
: System{ : System{
std::make_unique<Tox_System>(), std::make_unique<Tox_System>(),
std::make_unique<Memory>(Memory{&record_memory_funcs, this}), std::make_unique<Tox_Memory>(Tox_Memory{&record_memory_funcs, this}),
std::make_unique<Network>(Network{&record_network_funcs, this}), std::make_unique<Network>(Network{&record_network_funcs, this}),
std::make_unique<Random>(Random{&record_random_funcs, this}), std::make_unique<Random>(Random{&record_random_funcs, this}),
} }

View File

@@ -187,9 +187,9 @@ void fuzz_select_target(const uint8_t *data, std::size_t size)
return Fuzz_Target_Selector<Args...>::select(selector, input); return Fuzz_Target_Selector<Args...>::select(selector, input);
} }
struct Memory; struct Tox_Memory;
struct Network; struct Network;
struct Random; struct Tox_Random;
struct System { struct System {
/** @brief Deterministic system clock for this instance. /** @brief Deterministic system clock for this instance.
@@ -205,12 +205,12 @@ struct System {
uint64_t clock = 1000000000; uint64_t clock = 1000000000;
std::unique_ptr<Tox_System> sys; std::unique_ptr<Tox_System> sys;
std::unique_ptr<Memory> mem; std::unique_ptr<Tox_Memory> mem;
std::unique_ptr<Network> ns; std::unique_ptr<Network> ns;
std::unique_ptr<Random> rng; std::unique_ptr<Tox_Random> rng;
System(std::unique_ptr<Tox_System> sys, std::unique_ptr<Memory> mem, System(std::unique_ptr<Tox_System> sys, std::unique_ptr<Tox_Memory> mem,
std::unique_ptr<Network> ns, std::unique_ptr<Random> rng); std::unique_ptr<Network> ns, std::unique_ptr<Tox_Random> rng);
System(System &&); System(System &&);
// Not inline because sizeof of the above 2 structs is not known everywhere. // Not inline because sizeof of the above 2 structs is not known everywhere.

View File

@@ -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( sh_library(
name = "cimple_files", name = "cimple_files",
srcs = glob([ srcs = glob([

View File

@@ -3,6 +3,7 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "../toxcore/crypto_core.h" #include "../toxcore/crypto_core.h"
#include "../toxcore/os_random.h"
namespace { namespace {

View File

@@ -77,7 +77,7 @@ typedef struct DecodeTimeStats {
} DecodeTimeStats; } DecodeTimeStats;
struct ToxAV { struct ToxAV {
const Memory *mem; const struct Tox_Memory *mem;
Logger *log; Logger *log;
Tox *tox; Tox *tox;
MSISession *msi; MSISession *msi;

View File

@@ -39,6 +39,12 @@ cc_library(
visibility = ["//c-toxcore:__subpackages__"], visibility = ["//c-toxcore:__subpackages__"],
) )
cc_library(
name = "tox_attributes",
hdrs = ["tox_attributes.h"],
visibility = ["//c-toxcore:__subpackages__"],
)
cc_library( cc_library(
name = "ccompat", name = "ccompat",
srcs = ["ccompat.c"], srcs = ["ccompat.c"],
@@ -47,6 +53,59 @@ cc_library(
deps = [":attributes"], 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( cc_library(
name = "mem", name = "mem",
srcs = ["mem.c"], srcs = ["mem.c"],
@@ -55,6 +114,7 @@ cc_library(
deps = [ deps = [
":attributes", ":attributes",
":ccompat", ":ccompat",
":tox_memory",
], ],
) )
@@ -65,7 +125,9 @@ cc_library(
hdrs = ["mem_test_util.hh"], hdrs = ["mem_test_util.hh"],
deps = [ deps = [
":mem", ":mem",
":os_memory",
":test_util", ":test_util",
":tox_memory",
], ],
) )
@@ -75,6 +137,7 @@ cc_test(
srcs = ["mem_test.cc"], srcs = ["mem_test.cc"],
deps = [ deps = [
":mem", ":mem",
":os_memory",
"@com_google_googletest//:gtest", "@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main", "@com_google_googletest//:gtest_main",
], ],
@@ -208,6 +271,7 @@ cc_test(
":bin_unpack", ":bin_unpack",
":logger", ":logger",
":mem", ":mem",
":os_memory",
"@com_google_googletest//:gtest", "@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main", "@com_google_googletest//:gtest_main",
], ],
@@ -222,6 +286,7 @@ cc_library(
":attributes", ":attributes",
":ccompat", ":ccompat",
":mem", ":mem",
":tox_random",
":util", ":util",
"@libsodium", "@libsodium",
], ],
@@ -249,6 +314,7 @@ cc_library(
deps = [ deps = [
":crypto_core", ":crypto_core",
":test_util", ":test_util",
":tox_random",
], ],
) )
@@ -285,6 +351,7 @@ cc_test(
deps = [ deps = [
":list", ":list",
":mem", ":mem",
":os_memory",
"@com_google_googletest//:gtest", "@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main", "@com_google_googletest//:gtest_main",
], ],
@@ -372,8 +439,14 @@ cc_library(
cc_library( cc_library(
name = "network", name = "network",
srcs = ["network.c"], srcs = [
hdrs = ["network.h"], "net_log.c",
"network.c",
],
hdrs = [
"net_log.h",
"network.h",
],
visibility = [ visibility = [
"//c-toxcore/auto_tests:__pkg__", "//c-toxcore/auto_tests:__pkg__",
"//c-toxcore/other:__pkg__", "//c-toxcore/other:__pkg__",
@@ -1148,6 +1221,8 @@ cc_library(
":net_profile", ":net_profile",
":network", ":network",
":onion_client", ":onion_client",
":os_memory",
":os_random",
":state", ":state",
":tox_log_level", ":tox_log_level",
":tox_options", ":tox_options",
@@ -1163,6 +1238,7 @@ cc_test(
srcs = ["tox_test.cc"], srcs = ["tox_test.cc"],
deps = [ deps = [
":crypto_core", ":crypto_core",
":os_random",
":tox", ":tox",
":tox_log_level", ":tox_log_level",
":tox_options", ":tox_options",

View File

@@ -25,7 +25,6 @@
#include "shared_key_cache.h" #include "shared_key_cache.h"
#include "sort.h" #include "sort.h"
#include "state.h" #include "state.h"
#include "util.h"
/** The timeout after which a node is discarded completely. */ /** The timeout after which a node is discarded completely. */
#define KILL_NODE_TIMEOUT (BAD_NODE_TIMEOUT + PING_INTERVAL) #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); 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) struct Ping *dht_get_ping(const DHT *dht)
{ {
return dht->ping; 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->hole_punching_enabled = hole_punching_enabled;
dht->lan_discovery_enabled = lan_discovery_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) { if (dht->ping == nullptr) {
LOGGER_ERROR(log, "failed to initialise ping"); LOGGER_ERROR(log, "failed to initialise ping");

View File

@@ -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_public_key(DHT *_Nonnull dht, const uint8_t *_Nonnull key);
void dht_set_self_secret_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); 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_clientlist(const DHT *_Nonnull dht);
const Client_data *_Nonnull dht_get_close_client(const DHT *_Nonnull dht, uint32_t client_num); const Client_data *_Nonnull dht_get_close_client(const DHT *_Nonnull dht, uint32_t client_num);

View File

@@ -100,6 +100,8 @@ libtoxcore_la_SOURCES = ../third_party/cmp/cmp.c \
../toxcore/mono_time.h \ ../toxcore/mono_time.h \
../toxcore/net_crypto.c \ ../toxcore/net_crypto.c \
../toxcore/net_crypto.h \ ../toxcore/net_crypto.h \
../toxcore/net_log.c \
../toxcore/net_log.h \
../toxcore/net_profile.c \ ../toxcore/net_profile.c \
../toxcore/net_profile.h \ ../toxcore/net_profile.h \
../toxcore/network.c \ ../toxcore/network.c \
@@ -110,6 +112,10 @@ libtoxcore_la_SOURCES = ../third_party/cmp/cmp.c \
../toxcore/onion_client.h \ ../toxcore/onion_client.h \
../toxcore/onion.c \ ../toxcore/onion.c \
../toxcore/onion.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.c \
../toxcore/ping_array.h \ ../toxcore/ping_array.h \
../toxcore/ping.c \ ../toxcore/ping.c \
@@ -131,6 +137,7 @@ libtoxcore_la_SOURCES = ../third_party/cmp/cmp.c \
../toxcore/timed_auth.c \ ../toxcore/timed_auth.c \
../toxcore/timed_auth.h \ ../toxcore/timed_auth.h \
../toxcore/tox_api.c \ ../toxcore/tox_api.c \
../toxcore/tox_attributes.h \
../toxcore/tox_dispatch.c \ ../toxcore/tox_dispatch.c \
../toxcore/tox_dispatch.h \ ../toxcore/tox_dispatch.h \
../toxcore/tox_event.c \ ../toxcore/tox_event.c \
@@ -139,12 +146,18 @@ libtoxcore_la_SOURCES = ../third_party/cmp/cmp.c \
../toxcore/tox_events.h \ ../toxcore/tox_events.h \
../toxcore/tox_log_level.c \ ../toxcore/tox_log_level.c \
../toxcore/tox_log_level.h \ ../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.c \
../toxcore/tox_options.h \ ../toxcore/tox_options.h \
../toxcore/tox_pack.c \ ../toxcore/tox_pack.c \
../toxcore/tox_pack.h \ ../toxcore/tox_pack.h \
../toxcore/tox_private.c \ ../toxcore/tox_private.c \
../toxcore/tox_private.h \ ../toxcore/tox_private.h \
../toxcore/tox_random.c \
../toxcore/tox_random.h \
../toxcore/tox_random_impl.h \
../toxcore/tox_struct.h \ ../toxcore/tox_struct.h \
../toxcore/tox_unpack.c \ ../toxcore/tox_unpack.c \
../toxcore/tox_unpack.h \ ../toxcore/tox_unpack.h \

View File

@@ -3443,7 +3443,7 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random *
return nullptr; 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) { if (m->net_crypto == nullptr) {
LOGGER_WARNING(m->log, "net_crypto initialisation failed"); 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) { 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) { 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 { } else {
m->announce = nullptr; m->announce = nullptr;
} }
@@ -3484,11 +3484,11 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random *
m->announce = nullptr; m->announce = nullptr;
} }
m->onion = new_onion(m->log, m->mem, m->mono_time, m->rng, m->dht); 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->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->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) { 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)) || if ((options->dht_announcements_enabled && (m->forwarding == nullptr || m->announce == nullptr)) ||

View File

@@ -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, 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) { if (log == nullptr || mono_time == nullptr || forwarding == nullptr) {
return nullptr; return nullptr;
@@ -630,8 +630,8 @@ Announcements *new_announcements(const Logger *log, const Memory *mem, const Ran
announce->rng = rng; announce->rng = rng;
announce->forwarding = forwarding; announce->forwarding = forwarding;
announce->mono_time = mono_time; announce->mono_time = mono_time;
announce->dht = forwarding_get_dht(forwarding); announce->dht = dht;
announce->net = dht_get_net(announce->dht); announce->net = net;
announce->public_key = dht_get_self_public_key(announce->dht); announce->public_key = dht_get_self_public_key(announce->dht);
announce->secret_key = dht_get_self_secret_key(announce->dht); announce->secret_key = dht_get_self_secret_key(announce->dht);
new_hmac_key(announce->rng, announce->hmac_key); new_hmac_key(announce->rng, announce->hmac_key);

View File

@@ -7,12 +7,14 @@
#include <stdint.h> #include <stdint.h>
#include "DHT.h"
#include "attributes.h" #include "attributes.h"
#include "crypto_core.h" #include "crypto_core.h"
#include "forwarding.h" #include "forwarding.h"
#include "logger.h" #include "logger.h"
#include "mem.h" #include "mem.h"
#include "mono_time.h" #include "mono_time.h"
#include "network.h"
#define MAX_ANNOUNCEMENT_SIZE 512 #define MAX_ANNOUNCEMENT_SIZE 512
@@ -22,7 +24,8 @@ uint8_t announce_response_of_request_type(uint8_t request_type);
typedef struct Announcements Announcements; 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. * @brief If data is stored, run `on_retrieve_callback` on it.

View File

@@ -7,6 +7,7 @@
#include "bin_unpack.h" #include "bin_unpack.h"
#include "logger.h" #include "logger.h"
#include "mem.h" #include "mem.h"
#include "os_memory.h"
namespace { namespace {

View File

@@ -13,6 +13,7 @@
#include "attributes.h" #include "attributes.h"
#include "ccompat.h" #include "ccompat.h"
#include "mem.h" #include "mem.h"
#include "tox_random.h"
#include "util.h" #include "util.h"
static_assert(CRYPTO_PUBLIC_KEY_SIZE == crypto_box_PUBLICKEYBYTES, 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) 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], 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 */ #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) 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);
} }

View File

@@ -17,6 +17,7 @@
#include "attributes.h" #include "attributes.h"
#include "mem.h" #include "mem.h"
#include "tox_random.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -78,44 +79,6 @@ extern "C" {
*/ */
#define CRYPTO_SHA512_SIZE 64 #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. * @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]); 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. * @brief Return a random 8 bit integer.
*/ */

View File

@@ -5,15 +5,16 @@
#include "crypto_core.h" #include "crypto_core.h"
#include "test_util.hh" #include "test_util.hh"
#include "tox_random_impl.h"
Random_Funcs const Random_Class::vtable = { Tox_Random_Funcs const Random_Class::vtable = {
Method<crypto_random_bytes_cb, Random_Class>::invoke<&Random_Class::random_bytes>, Method<tox_random_bytes_cb, Random_Class>::invoke<&Random_Class::random_bytes>,
Method<crypto_random_uniform_cb, Random_Class>::invoke<&Random_Class::random_uniform>, Method<tox_random_uniform_cb, Random_Class>::invoke<&Random_Class::random_uniform>,
}; };
Random_Class::~Random_Class() = default; 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)); std::generate(bytes, &bytes[length], std::ref(lcg));
} }

View File

@@ -8,12 +8,13 @@
#include "crypto_core.h" #include "crypto_core.h"
#include "test_util.hh" #include "test_util.hh"
#include "tox_random_impl.h"
struct Random_Class { struct Random_Class {
static Random_Funcs const vtable; static Tox_Random_Funcs const vtable;
Random const self; 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(Random_Class const &) = default;
Random_Class() Random_Class()
@@ -22,8 +23,8 @@ struct Random_Class {
} }
virtual ~Random_Class(); virtual ~Random_Class();
virtual crypto_random_bytes_cb random_bytes = 0; virtual tox_random_bytes_cb random_bytes = 0;
virtual crypto_random_uniform_cb random_uniform = 0; virtual tox_random_uniform_cb random_uniform = 0;
}; };
/** /**
@@ -35,7 +36,7 @@ struct Random_Class {
class Test_Random : public Random_Class { class Test_Random : public Random_Class {
std::minstd_rand lcg; 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; 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); 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 #endif // C_TOXCORE_TOXCORE_CRYPTO_CORE_TEST_UTIL_H

View File

@@ -6,7 +6,6 @@
#define C_TOXCORE_TOXCORE_EVENTS_EVENTS_ALLOC_H #define C_TOXCORE_TOXCORE_EVENTS_EVENTS_ALLOC_H
#include "../attributes.h" #include "../attributes.h"
#include "../mem.h"
#include "../tox.h" #include "../tox.h"
#include "../tox_events.h" #include "../tox_events.h"
#include "../tox_private.h" #include "../tox_private.h"
@@ -15,17 +14,19 @@
extern "C" { extern "C" {
#endif #endif
struct Tox_Memory;
struct Tox_Events { struct Tox_Events {
Tox_Event *_Nonnull events; Tox_Event *_Nonnull events;
uint32_t events_size; uint32_t events_size;
uint32_t events_capacity; uint32_t events_capacity;
const Memory *_Nonnull mem; const struct Tox_Memory *_Nonnull mem;
}; };
typedef struct Tox_Events_State { typedef struct Tox_Events_State {
Tox_Err_Events_Iterate error; Tox_Err_Events_Iterate error;
const Memory *_Nonnull mem; const struct Tox_Memory *_Nonnull mem;
Tox_Events *_Nonnull events; Tox_Events *_Nonnull events;
} Tox_Events_State; } Tox_Events_State;

View File

@@ -37,11 +37,6 @@ struct Forwarding {
void *forwarded_response_callback_object; void *forwarded_response_callback_object;
}; };
DHT *forwarding_get_dht(const Forwarding *forwarding)
{
return forwarding->dht;
}
#define SENDBACK_TIMEOUT 3600 #define SENDBACK_TIMEOUT 3600
bool send_forward_request(const Networking_Core *net, const IP_Port *forwarder, 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->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) { if (log == nullptr || mono_time == nullptr || dht == nullptr) {
return nullptr; return nullptr;
@@ -365,7 +361,7 @@ Forwarding *_Nullable new_forwarding(const Logger *log, const Memory *mem, const
forwarding->rng = rng; forwarding->rng = rng;
forwarding->mono_time = mono_time; forwarding->mono_time = mono_time;
forwarding->dht = dht; 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_REQUEST, &handle_forward_request, forwarding);
networking_registerhandler(forwarding->net, NET_PACKET_FORWARD_REPLY, &handle_forward_reply, forwarding); networking_registerhandler(forwarding->net, NET_PACKET_FORWARD_REPLY, &handle_forward_reply, forwarding);

View File

@@ -30,8 +30,6 @@ extern "C" {
typedef struct Forwarding Forwarding; 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. * @brief Send data to forwarder for forwarding via chain of dht nodes.
* Destination is last key in the chain. * 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. * sendback.
*/ */
void set_callback_forward_reply(Forwarding *_Nonnull forwarding, forward_reply_cb *_Nullable function, void *_Nullable object); 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); void kill_forwarding(Forwarding *_Nullable forwarding);
#ifdef __cplusplus #ifdef __cplusplus

View File

@@ -71,6 +71,7 @@ struct Friend_Connections {
const Mono_Time *mono_time; const Mono_Time *mono_time;
const Memory *mem; const Memory *mem;
const Logger *logger; const Logger *logger;
Networking_Core *net;
Net_Crypto *net_crypto; Net_Crypto *net_crypto;
DHT *dht; DHT *dht;
Broadcast_Info *broadcast; 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. */ /** Create new friend_connections instance. */
Friend_Connections *new_friend_connections( Friend_Connections *new_friend_connections(
const Logger *logger, const Memory *mem, const Mono_Time *mono_time, const Network *ns, const Logger *logger, const Memory *mem, const Mono_Time *mono_time, const Network *ns,
Onion_Client *onion_c, bool local_discovery_enabled) Onion_Client *onion_c, DHT *dht, Net_Crypto *net_crypto, Networking_Core *net,
bool local_discovery_enabled)
{ {
if (onion_c == nullptr) { if (onion_c == nullptr) {
return nullptr; return nullptr;
@@ -922,8 +924,9 @@ Friend_Connections *new_friend_connections(
temp->mono_time = mono_time; temp->mono_time = mono_time;
temp->mem = mem; temp->mem = mem;
temp->logger = logger; temp->logger = logger;
temp->dht = onion_get_dht(onion_c); temp->dht = dht;
temp->net_crypto = onion_get_net_crypto(onion_c); temp->net = net;
temp->net_crypto = net_crypto;
temp->onion_c = onion_c; temp->onion_c = onion_c;
// Don't include default port in port range // Don't include default port in port range
temp->next_lan_port = TOX_PORTRANGE_FROM + 1; 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; last = last > TOX_PORTRANGE_TO ? TOX_PORTRANGE_TO : last;
// Always send to default port // 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)); net_htons(TOX_PORT_DEFAULT));
// And check some extra ports // And check some extra ports
for (uint16_t port = first; port < last; ++port) { 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 // Don't include default port in port range

View File

@@ -145,7 +145,8 @@ void set_friend_request_callback(Friend_Connections *_Nonnull fr_c, fr_request_c
/** Create new friend_connections instance. */ /** 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, 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. */ /** main friend_connections loop. */
void do_friend_connections(Friend_Connections *_Nonnull fr_c, void *_Nonnull userdata); void do_friend_connections(Friend_Connections *_Nonnull fr_c, void *_Nonnull userdata);

View File

@@ -3,6 +3,7 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "mem.h" #include "mem.h"
#include "os_memory.h"
namespace { namespace {

View File

@@ -5,59 +5,29 @@
#include "mem.h" #include "mem.h"
#include <stdlib.h> #include <string.h>
#include "attributes.h"
#include "ccompat.h" #include "ccompat.h"
#include "tox_memory.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;
}
void *mem_balloc(const Memory *mem, uint32_t size) 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; return ptr;
} }
void *mem_brealloc(const Memory *mem, void *ptr, uint32_t size) 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; return new_ptr;
} }
void *mem_alloc(const Memory *mem, uint32_t size) 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; return ptr;
} }
@@ -69,7 +39,10 @@ void *mem_valloc(const Memory *mem, uint32_t nmemb, uint32_t size)
return nullptr; 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; return ptr;
} }
@@ -81,11 +54,11 @@ void *mem_vrealloc(const Memory *mem, void *ptr, uint32_t nmemb, uint32_t size)
return nullptr; 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; return new_ptr;
} }
void mem_delete(const Memory *mem, void *ptr) void mem_delete(const Memory *mem, void *ptr)
{ {
mem->funcs->free(mem->obj, ptr); tox_memory_dealloc(mem, ptr);
} }

View File

@@ -12,30 +12,13 @@
#include <stdint.h> // uint*_t #include <stdint.h> // uint*_t
#include "attributes.h" #include "attributes.h"
#include "tox_memory.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
typedef void *mem_malloc_cb(void *_Nullable obj, uint32_t size); typedef Tox_Memory Memory;
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);
/** /**
* @brief Allocate an array of a given size for built-in types. * @brief Allocate an array of a given size for built-in types.

View File

@@ -2,6 +2,8 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "os_memory.h"
namespace { namespace {
TEST(Mem, AllocLarge) TEST(Mem, AllocLarge)

View File

@@ -2,28 +2,28 @@
#include <cstdlib> #include <cstdlib>
#include "mem.h"
#include "test_util.hh" #include "test_util.hh"
#include "tox_memory_impl.h"
Memory_Funcs const Memory_Class::vtable = { Tox_Memory_Funcs const Memory_Class::vtable = {
Method<mem_malloc_cb, Memory_Class>::invoke<&Memory_Class::malloc>, Method<tox_memory_malloc_cb, Memory_Class>::invoke<&Memory_Class::malloc>,
Method<mem_calloc_cb, Memory_Class>::invoke<&Memory_Class::calloc>, Method<tox_memory_realloc_cb, Memory_Class>::invoke<&Memory_Class::realloc>,
Method<mem_realloc_cb, Memory_Class>::invoke<&Memory_Class::realloc>, Method<tox_memory_dealloc_cb, Memory_Class>::invoke<&Memory_Class::dealloc>,
Method<mem_free_cb, Memory_Class>::invoke<&Memory_Class::free>,
}; };
Memory_Class::~Memory_Class() = default; Memory_Class::~Memory_Class() = default;
void *Test_Memory::malloc(void *obj, uint32_t size) { return mem->funcs->malloc(mem->obj, size); } void *Test_Memory::malloc(void *obj, uint32_t size)
void *Test_Memory::calloc(void *obj, uint32_t nmemb, 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) 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);
}

View File

@@ -2,13 +2,15 @@
#define C_TOXCORE_TOXCORE_MEM_TEST_UTIL_H #define C_TOXCORE_TOXCORE_MEM_TEST_UTIL_H
#include "mem.h" #include "mem.h"
#include "os_memory.h"
#include "test_util.hh" #include "test_util.hh"
#include "tox_memory_impl.h"
struct Memory_Class { struct Memory_Class {
static Memory_Funcs const vtable; static Tox_Memory_Funcs const vtable;
Memory const self; 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(Memory_Class const &) = default;
Memory_Class() Memory_Class()
@@ -17,10 +19,9 @@ struct Memory_Class {
} }
virtual ~Memory_Class(); virtual ~Memory_Class();
virtual mem_malloc_cb malloc = 0; virtual tox_memory_malloc_cb malloc = 0;
virtual mem_calloc_cb calloc = 0; virtual tox_memory_realloc_cb realloc = 0;
virtual mem_realloc_cb realloc = 0; virtual tox_memory_dealloc_cb dealloc = 0;
virtual mem_free_cb free = 0;
}; };
/** /**
@@ -28,12 +29,11 @@ struct Memory_Class {
* subclassed to override individual (or all) functions. * subclassed to override individual (or all) functions.
*/ */
class Test_Memory : public Memory_Class { 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 *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 *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 #endif // C_TOXCORE_TOXCORE_MEM_TEST_UTIL_H

View File

@@ -137,6 +137,7 @@ struct Net_Crypto {
const Random *rng; const Random *rng;
Mono_Time *mono_time; Mono_Time *mono_time;
const Network *ns; const Network *ns;
Networking_Core *net;
DHT *dht; DHT *dht;
TCP_Connections *tcp_c; TCP_Connections *tcp_c;
@@ -176,11 +177,6 @@ TCP_Connections *nc_get_tcp_c(const Net_Crypto *c)
return c->tcp_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) 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) { 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; 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; 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); crypto_connection_status(c, crypt_connection_id, &direct_connected, nullptr);
if (direct_connected) { 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; 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) 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) { || 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; direct_send_attempt = true;
conn->direct_send_attempt_time = mono_time_get(c->mono_time); 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. * 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, 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) { if (dht == nullptr) {
return 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->rng = rng;
temp->mono_time = mono_time; temp->mono_time = mono_time;
temp->ns = ns; 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); 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; 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(net, 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(net, NET_PACKET_COOKIE_RESPONSE, &udp_handle_packet, temp);
networking_registerhandler(dht_get_net(dht), NET_PACKET_CRYPTO_HS, &udp_handle_packet, temp); networking_registerhandler(net, 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_CRYPTO_DATA, &udp_handle_packet, temp);
bs_list_init(&temp->ip_port_list, mem, sizeof(IP_Port), 8, ipport_cmp_handler); 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); kill_tcp_connections(c->tcp_c);
bs_list_free(&c->ip_port_list); bs_list_free(&c->ip_port_list);
networking_registerhandler(dht_get_net(c->dht), NET_PACKET_COOKIE_REQUEST, nullptr, nullptr); networking_registerhandler(c->net, NET_PACKET_COOKIE_REQUEST, nullptr, nullptr);
networking_registerhandler(dht_get_net(c->dht), NET_PACKET_COOKIE_RESPONSE, nullptr, nullptr); networking_registerhandler(c->net, NET_PACKET_COOKIE_RESPONSE, nullptr, nullptr);
networking_registerhandler(dht_get_net(c->dht), NET_PACKET_CRYPTO_HS, nullptr, nullptr); networking_registerhandler(c->net, 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_CRYPTO_DATA, nullptr, nullptr);
crypto_memzero(c, sizeof(Net_Crypto)); crypto_memzero(c, sizeof(Net_Crypto));
mem_delete(mem, c); mem_delete(mem, c);
} }

View File

@@ -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_public_key(const Net_Crypto *_Nonnull c);
const uint8_t *_Nonnull nc_get_self_secret_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); 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 { typedef struct New_Connection {
IP_Port source; 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. /** @brief Create new instance of Net_Crypto.
* Sets all the global connection variables to their default values. * 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); const TCP_Proxy_Info *_Nonnull proxy_info, Net_Profile *_Nonnull tcp_np);
/** return the optimal interval in ms for running do_net_crypto. */ /** return the optimal interval in ms for running do_net_crypto. */

View File

@@ -76,7 +76,7 @@ void TestNetCrypto(Fuzz_Data &input)
const Ptr<Net_Crypto> net_crypto( const Ptr<Net_Crypto> net_crypto(
new_net_crypto(logger.get(), sys.mem.get(), sys.rng.get(), sys.ns.get(), mono_time.get(), 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); kill_net_crypto);
if (net_crypto == nullptr) { if (net_crypto == nullptr) {
netprof_kill(sys.mem.get(), tcp_np); netprof_kill(sys.mem.get(), tcp_np);

180
toxcore/net_log.c Normal file
View File

@@ -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 "<unknown>";
}
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]);
}
}

27
toxcore/net_log.h Normal file
View File

@@ -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 <stdbool.h> // bool
#include <stdint.h> // 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 */

View File

@@ -86,6 +86,7 @@
#include "ccompat.h" #include "ccompat.h"
#include "logger.h" #include "logger.h"
#include "mem.h" #include "mem.h"
#include "net_log.h"
#include "net_profile.h" #include "net_profile.h"
#include "util.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); 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 "<unknown>";
}
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, 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) 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); 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; 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) Socket sock, uint8_t *buf, size_t len, const IP_Port *ip_port)
{ {
const int res = ns->funcs->recv(ns->obj, sock, buf, len); 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; 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); 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); assert(res <= INT_MAX);
@@ -1151,7 +988,7 @@ static int receivepacket(const Network *_Nonnull ns, const Logger *_Nonnull log,
return -1; 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; return 0;
} }

View File

@@ -702,7 +702,7 @@ void set_callback_handle_recv_1(Onion *onion, onion_recv_1_cb *function, void *o
onion->callback_object = object; 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) { if (dht == nullptr) {
return 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->log = log;
onion->dht = dht; onion->dht = dht;
onion->net = dht_get_net(dht); onion->net = net;
onion->mono_time = mono_time; onion->mono_time = mono_time;
onion->rng = rng; onion->rng = rng;
onion->mem = mem; onion->mem = mem;

View File

@@ -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. */ /** 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); 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); void kill_onion(Onion *_Nullable onion);
#endif /* C_TOXCORE_TOXCORE_ONION_H */ #endif /* C_TOXCORE_TOXCORE_ONION_H */

View File

@@ -680,7 +680,8 @@ static int handle_data_request(void *_Nonnull object, const IP_Port *_Nonnull so
return 0; 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) { if (dht == nullptr) {
return 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->mem = mem;
onion_a->mono_time = mono_time; onion_a->mono_time = mono_time;
onion_a->dht = dht; 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_max_size = 0;
onion_a->extra_data_callback = nullptr; onion_a->extra_data_callback = nullptr;
onion_a->extra_data_object = nullptr; onion_a->extra_data_object = nullptr;

View File

@@ -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); 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); void kill_onion_announce(Onion_Announce *_Nullable onion_a);
#endif /* C_TOXCORE_TOXCORE_ONION_ANNOUNCE_H */ #endif /* C_TOXCORE_TOXCORE_ONION_ANNOUNCE_H */

View File

@@ -203,16 +203,6 @@ bool onion_friend_is_groupchat(const Onion_Friend *const onion_friend)
return onion_friend->is_groupchat; 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. /** @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 * 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_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) { if (c == nullptr) {
return 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->logger = logger;
onion_c->rng = rng; onion_c->rng = rng;
onion_c->mem = mem; onion_c->mem = mem;
onion_c->dht = nc_get_dht(c); onion_c->dht = dht;
onion_c->net = dht_get_net(onion_c->dht); onion_c->net = net;
onion_c->c = c; onion_c->c = c;
new_symmetric_key(rng, onion_c->secret_symmetric_key); new_symmetric_key(rng, onion_c->secret_symmetric_key);
crypto_new_keypair(rng, onion_c->temp_public_key, onion_c->temp_secret_key); crypto_new_keypair(rng, onion_c->temp_public_key, onion_c->temp_secret_key);

View File

@@ -21,7 +21,6 @@
#include "net_crypto.h" #include "net_crypto.h"
#include "network.h" #include "network.h"
#include "onion_announce.h" #include "onion_announce.h"
#include "ping_array.h"
#define MAX_ONION_CLIENTS 8 #define MAX_ONION_CLIENTS 8
#define MAX_ONION_CLIENTS_ANNOUNCE 12 // Number of nodes to announce ourselves to. #define MAX_ONION_CLIENTS_ANNOUNCE 12 // Number of nodes to announce ourselves to.
@@ -66,9 +65,6 @@
typedef struct Onion_Client Onion_Client; 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. /** @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 * 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 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); 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); void kill_onion_client(Onion_Client *_Nullable onion_c);
typedef enum Onion_Connection_Status { typedef enum Onion_Connection_Status {

40
toxcore/os_memory.c Normal file
View File

@@ -0,0 +1,40 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2022-2025 The TokTok team.
*/
#include "os_memory.h"
#include <stdlib.h>
#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;
}

22
toxcore/os_memory.h Normal file
View File

@@ -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 */

43
toxcore/os_random.c Normal file
View File

@@ -0,0 +1,43 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2022-2025 The TokTok team.
*/
#include "os_random.h"
#include <sodium.h>
#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;
}

Some files were not shown because too many files have changed in this diff Show More