Skip to content

Commit

Permalink
refactor: Move some OS-specifics into tox_system.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Mar 25, 2024
1 parent 0ec4978 commit cfbef98
Show file tree
Hide file tree
Showing 255 changed files with 4,356 additions and 2,235 deletions.
61 changes: 52 additions & 9 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,75 @@ load("//tools/project:build_defs.bzl", "project")

project(license = "gpl3-https")

genrule(
name = "toxcore_headers",
srcs = ["//c-toxcore/toxcore:public"],
outs = [
"tox/toxcore/os_log.h",
"tox/toxcore/os_memory.h",
"tox/toxcore/os_network.h",
"tox/toxcore/os_random.h",
"tox/toxcore/os_system.h",
"tox/toxcore/tox.h",
"tox/toxcore/tox_attributes.h",
"tox/toxcore/tox_dispatch.h",
"tox/toxcore/tox_events.h",
"tox/toxcore/tox_log.h",
"tox/toxcore/tox_log_impl.h",
"tox/toxcore/tox_memory.h",
"tox/toxcore/tox_memory_impl.h",
"tox/toxcore/tox_network.h",
"tox/toxcore/tox_network_impl.h",
"tox/toxcore/tox_options.h",
"tox/toxcore/tox_random.h",
"tox/toxcore/tox_random_impl.h",
"tox/toxcore/tox_system.h",
"tox/toxcore/tox_system_impl.h",
"tox/toxcore/tox_time.h",
"tox/toxcore/tox_time_impl.h",
],
cmd = "cp $(locations //c-toxcore/toxcore:public) $(GENDIR)/c-toxcore/tox/toxcore/",
visibility = ["//visibility:public"],
)

genrule(
name = "public_headers",
srcs = [
"tox.h",
"toxav.h",
"toxencryptsave.h",
"//c-toxcore/toxav:toxav.h",
"//c-toxcore/toxcore:tox.h",
"//c-toxcore/toxcore:tox_private.h",
"//c-toxcore/toxencryptsave:toxencryptsave.h",
],
outs = [
"tox/toxav.h",
"tox/tox.h",
"tox/tox_private.h",
"tox/toxav.h",
"tox/toxav/toxav.h",
"tox/toxencryptsave.h",
"tox/toxencryptsave/toxencryptsave.h",
],
cmd = """
cp $(location //c-toxcore/toxav:toxav.h) $(GENDIR)/c-toxcore/tox/toxav.h
cp $(location //c-toxcore/toxcore:tox.h) $(GENDIR)/c-toxcore/tox/tox.h
cp $(location //c-toxcore/toxcore:tox_private.h) $(GENDIR)/c-toxcore/tox/tox_private.h
cp $(location //c-toxcore/toxencryptsave:toxencryptsave.h) $(GENDIR)/c-toxcore/tox/toxencryptsave.h
cp $(location toxav.h) $(GENDIR)/c-toxcore/tox/toxav.h
cp $(location tox.h) $(GENDIR)/c-toxcore/tox/tox.h
cp $(location toxencryptsave.h) $(GENDIR)/c-toxcore/tox/toxencryptsave.h
cp $(location //c-toxcore/toxav:toxav.h) $(GENDIR)/c-toxcore/tox/toxav/toxav.h
cp $(location //c-toxcore/toxencryptsave:toxencryptsave.h) $(GENDIR)/c-toxcore/tox/toxencryptsave/toxencryptsave.h
""",
visibility = ["//visibility:public"],
)

filegroup(
name = "public",
srcs = [
":public_headers",
":toxcore_headers",
],
visibility = ["//visibility:public"],
)

cc_library(
name = "c-toxcore",
hdrs = [":public_headers"],
hdrs = [":public"],
includes = ["."],
visibility = ["//visibility:public"],
deps = [
Expand Down
59 changes: 53 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ endif()
set_source_files_properties(
toxcore/mono_time.c
toxcore/network.c
toxcore/os_time.c
toxcore/tox.c
toxcore/util.c
PROPERTIES SKIP_UNITY_BUILD_INCLUSION TRUE)
Expand Down Expand Up @@ -314,6 +315,19 @@ set(toxcore_SOURCES
toxcore/onion_client.c
toxcore/onion_client.h
toxcore/onion.h
toxcore/os_log.c
toxcore/os_log.h
toxcore/os_memory.c
toxcore/os_memory.h
toxcore/os_network.c
toxcore/os_network.h
toxcore/os_network_impl.h
toxcore/os_random.c
toxcore/os_random.h
toxcore/os_system.c
toxcore/os_system.h
toxcore/os_time.c
toxcore/os_time.h
toxcore/ping_array.c
toxcore/ping_array.h
toxcore/ping.c
Expand All @@ -334,13 +348,33 @@ set(toxcore_SOURCES
toxcore/timed_auth.h
toxcore/tox_api.c
toxcore/tox.c
toxcore/tox.h
toxcore/tox_dispatch.c
toxcore/tox_dispatch.h
toxcore/tox_event.c
toxcore/tox_event.h
toxcore/tox_events.c
toxcore/tox_events.h
toxcore/tox.h
toxcore/tox_log.c
toxcore/tox_log.h
toxcore/tox_log_impl.h
toxcore/tox_memory.c
toxcore/tox_memory.h
toxcore/tox_memory_impl.h
toxcore/tox_network.c
toxcore/tox_network.h
toxcore/tox_network_impl.h
toxcore/tox_options.c
toxcore/tox_options.h
toxcore/tox_random.c
toxcore/tox_random.h
toxcore/tox_random_impl.h
toxcore/tox_system.c
toxcore/tox_system.h
toxcore/tox_system_impl.h
toxcore/tox_time.c
toxcore/tox_time.h
toxcore/tox_time_impl.h
toxcore/tox_private.c
toxcore/tox_private.h
toxcore/tox_pack.c
Expand All @@ -359,13 +393,24 @@ else()
endif()
set(toxcore_PKGCONFIG_REQUIRES ${toxcore_PKGCONFIG_REQUIRES} libsodium)
set(toxcore_API_HEADERS
${toxcore_SOURCE_DIR}/tox.h^tox
${toxcore_SOURCE_DIR}/toxcore/os_log.h^os
${toxcore_SOURCE_DIR}/toxcore/os_memory.h^os
${toxcore_SOURCE_DIR}/toxcore/os_network.h^os
${toxcore_SOURCE_DIR}/toxcore/os_random.h^os
${toxcore_SOURCE_DIR}/toxcore/os_system.h^os
${toxcore_SOURCE_DIR}/toxcore/tox.h^tox
${toxcore_SOURCE_DIR}/toxcore/tox_attributes.h^tox
${toxcore_SOURCE_DIR}/toxcore/tox_dispatch.h^tox
${toxcore_SOURCE_DIR}/toxcore/tox_events.h^tox
${toxcore_SOURCE_DIR}/toxcore/tox_dispatch.h^tox)
if(EXPERIMENTAL_API)
set(toxcore_API_HEADERS ${toxcore_API_HEADERS}
${toxcore_SOURCE_DIR}/toxcore/tox_private.h^tox)
endif()
${toxcore_SOURCE_DIR}/toxcore/tox_log.h^tox
${toxcore_SOURCE_DIR}/toxcore/tox_memory.h^tox
${toxcore_SOURCE_DIR}/toxcore/tox_network.h^tox
${toxcore_SOURCE_DIR}/toxcore/tox_options.h^tox
${toxcore_SOURCE_DIR}/toxcore/tox_random.h^tox
${toxcore_SOURCE_DIR}/toxcore/tox_system.h^tox
${toxcore_SOURCE_DIR}/toxcore/tox_system_impl.h^tox
${toxcore_SOURCE_DIR}/toxcore/tox_time.h^tox)

################################################################################
#
Expand Down Expand Up @@ -393,6 +438,7 @@ if(BUILD_TOXAV)
toxav/video.c
toxav/video.h)
set(toxcore_API_HEADERS ${toxcore_API_HEADERS}
${toxcore_SOURCE_DIR}/toxav.h^toxav
${toxcore_SOURCE_DIR}/toxav/toxav.h^toxav)

if(MSVC)
Expand All @@ -416,6 +462,7 @@ set(toxcore_SOURCES ${toxcore_SOURCES}
toxencryptsave/toxencryptsave.c
toxencryptsave/toxencryptsave.h)
set(toxcore_API_HEADERS ${toxcore_API_HEADERS}
${toxcore_SOURCE_DIR}/toxencryptsave.h^tox
${toxcore_SOURCE_DIR}/toxencryptsave/toxencryptsave.h^tox)

################################################################################
Expand Down
5 changes: 5 additions & 0 deletions auto_tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ cc_library(
"//c-toxcore/toxcore:tox",
"//c-toxcore/toxcore:tox_dispatch",
"//c-toxcore/toxcore:tox_events",
"//c-toxcore/toxcore:tox_time",
],
)

Expand Down Expand Up @@ -75,9 +76,13 @@ extra_data = {
"//c-toxcore/toxcore:onion",
"//c-toxcore/toxcore:onion_announce",
"//c-toxcore/toxcore:onion_client",
"//c-toxcore/toxcore:os_memory",
"//c-toxcore/toxcore:os_network",
"//c-toxcore/toxcore:os_random",
"//c-toxcore/toxcore:tox",
"//c-toxcore/toxcore:tox_dispatch",
"//c-toxcore/toxcore:tox_events",
"//c-toxcore/toxcore:tox_time",
"//c-toxcore/toxcore:util",
"//c-toxcore/toxencryptsave",
"@libsodium",
Expand Down
43 changes: 23 additions & 20 deletions auto_tests/TCP_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#include "../toxcore/TCP_server.h"
#include "../toxcore/crypto_core.h"
#include "../toxcore/mono_time.h"
#include "../toxcore/os_random.h"
#include "../toxcore/os_network.h"
#include "../toxcore/os_memory.h"
#include "../toxcore/util.h"
#include "auto_test_support.h"

Expand Down Expand Up @@ -52,8 +55,8 @@ static void test_basic(void)
const Memory *mem = os_memory();
ck_assert(mem != nullptr);

Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
Logger *logger = logger_new();
Mono_Time *mono_time = mono_time_new(mem, nullptr);
Logger *logger = logger_new(mem);
logger_callback_log(logger, print_debug_logger, nullptr, nullptr);

// Attempt to create a new TCP_Server instance.
Expand Down Expand Up @@ -103,7 +106,7 @@ static void test_basic(void)

// Encrypting handshake
int ret = encrypt_data(self_public_key, f_secret_key, handshake + CRYPTO_PUBLIC_KEY_SIZE, handshake_plain,
TCP_HANDSHAKE_PLAIN_SIZE, handshake + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE);
TCP_HANDSHAKE_PLAIN_SIZE, handshake + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE, mem);
ck_assert_msg(ret == TCP_CLIENT_HANDSHAKE_SIZE - (CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE),
"encrypt_data() call failed.");

Expand All @@ -129,7 +132,7 @@ static void test_basic(void)
ck_assert_msg(net_recv(ns, logger, sock, response, TCP_SERVER_HANDSHAKE_SIZE, &localhost) == TCP_SERVER_HANDSHAKE_SIZE,
"Could/did not receive a server response to the initial handshake.");
ret = decrypt_data(self_public_key, f_secret_key, response, response + CRYPTO_NONCE_SIZE,
TCP_SERVER_HANDSHAKE_SIZE - CRYPTO_NONCE_SIZE, response_plain);
TCP_SERVER_HANDSHAKE_SIZE - CRYPTO_NONCE_SIZE, response_plain, mem);
ck_assert_msg(ret == TCP_HANDSHAKE_PLAIN_SIZE, "Failed to decrypt handshake response.");
uint8_t f_nonce_r[CRYPTO_NONCE_SIZE];
uint8_t f_shared_key[CRYPTO_SHARED_KEY_SIZE];
Expand All @@ -143,7 +146,7 @@ static void test_basic(void)
uint8_t r_req[2 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE];
uint16_t size = 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE;
size = net_htons(size);
encrypt_data_symmetric(f_shared_key, f_nonce, r_req_p, 1 + CRYPTO_PUBLIC_KEY_SIZE, r_req + 2);
encrypt_data_symmetric(f_shared_key, f_nonce, r_req_p, 1 + CRYPTO_PUBLIC_KEY_SIZE, r_req + 2, mem);
increment_nonce(f_nonce);
memcpy(r_req, &size, 2);

Expand Down Expand Up @@ -174,7 +177,7 @@ static void test_basic(void)
"Wrong packet size for request response.");

uint8_t packet_resp_plain[4096];
ret = decrypt_data_symmetric(f_shared_key, f_nonce_r, packet_resp + 2, recv_data_len - 2, packet_resp_plain);
ret = decrypt_data_symmetric(f_shared_key, f_nonce_r, packet_resp + 2, recv_data_len - 2, packet_resp_plain, mem);
ck_assert_msg(ret != -1, "Failed to decrypt the TCP server's response.");
increment_nonce(f_nonce_r);

Expand Down Expand Up @@ -229,7 +232,7 @@ static struct sec_TCP_con *new_tcp_con(const Logger *logger, const Memory *mem,
random_nonce(rng, handshake + CRYPTO_PUBLIC_KEY_SIZE);

int ret = encrypt_data(tcp_server_public_key(tcp_s), f_secret_key, handshake + CRYPTO_PUBLIC_KEY_SIZE, handshake_plain,
TCP_HANDSHAKE_PLAIN_SIZE, handshake + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE);
TCP_HANDSHAKE_PLAIN_SIZE, handshake + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE, mem);
ck_assert_msg(ret == TCP_CLIENT_HANDSHAKE_SIZE - (CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE),
"Failed to encrypt the outgoing handshake.");

Expand All @@ -249,7 +252,7 @@ static struct sec_TCP_con *new_tcp_con(const Logger *logger, const Memory *mem,
ck_assert_msg(net_recv(sec_c->ns, logger, sock, response, TCP_SERVER_HANDSHAKE_SIZE, &localhost) == TCP_SERVER_HANDSHAKE_SIZE,
"Failed to receive server handshake response.");
ret = decrypt_data(tcp_server_public_key(tcp_s), f_secret_key, response, response + CRYPTO_NONCE_SIZE,
TCP_SERVER_HANDSHAKE_SIZE - CRYPTO_NONCE_SIZE, response_plain);
TCP_SERVER_HANDSHAKE_SIZE - CRYPTO_NONCE_SIZE, response_plain, mem);
ck_assert_msg(ret == TCP_HANDSHAKE_PLAIN_SIZE, "Failed to decrypt server handshake response.");
encrypt_precompute(response_plain, t_secret_key, sec_c->shared_key);
memcpy(sec_c->recv_nonce, response_plain + CRYPTO_SHARED_KEY_SIZE, CRYPTO_NONCE_SIZE);
Expand All @@ -271,7 +274,7 @@ static int write_packet_tcp_test_connection(const Logger *logger, struct sec_TCP

uint16_t c_length = net_htons(length + CRYPTO_MAC_SIZE);
memcpy(packet, &c_length, sizeof(uint16_t));
int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t));
int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t), con->mem);

if ((unsigned int)len != (packet_size - sizeof(uint16_t))) {
return -1;
Expand All @@ -296,7 +299,7 @@ static int read_packet_sec_tcp(const Logger *logger, struct sec_TCP_con *con, ui

int rlen = net_recv(con->ns, logger, con->sock, data, length, &localhost);
ck_assert_msg(rlen == length, "Did not receive packet of correct length. Wanted %i, instead got %i", length, rlen);
rlen = decrypt_data_symmetric(con->shared_key, con->recv_nonce, data + 2, length - 2, data);
rlen = decrypt_data_symmetric(con->shared_key, con->recv_nonce, data + 2, length - 2, data, con->mem);
ck_assert_msg(rlen != -1, "Failed to decrypt a received packet from the Relay server.");
increment_nonce(con->recv_nonce);
return rlen;
Expand All @@ -311,8 +314,8 @@ static void test_some(void)
const Memory *mem = os_memory();
ck_assert(mem != nullptr);

Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
Logger *logger = logger_new();
Mono_Time *mono_time = mono_time_new(mem, nullptr);
Logger *logger = logger_new(mem);

uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
Expand Down Expand Up @@ -506,8 +509,8 @@ static void test_client(void)
const Memory *mem = os_memory();
ck_assert(mem != nullptr);

Logger *logger = logger_new();
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
Logger *logger = logger_new(mem);
Mono_Time *mono_time = mono_time_new(mem, nullptr);

uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
Expand Down Expand Up @@ -642,8 +645,8 @@ static void test_client_invalid(void)
const Memory *mem = os_memory();
ck_assert(mem != nullptr);

Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
Logger *logger = logger_new();
Mono_Time *mono_time = mono_time_new(mem, nullptr);
Logger *logger = logger_new(mem);

uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
Expand Down Expand Up @@ -720,8 +723,8 @@ static void test_tcp_connection(void)
const Memory *mem = os_memory();
ck_assert(mem != nullptr);

Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
Logger *logger = logger_new();
Mono_Time *mono_time = mono_time_new(mem, nullptr);
Logger *logger = logger_new(mem);

tcp_data_callback_called = 0;
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
Expand Down Expand Up @@ -833,8 +836,8 @@ static void test_tcp_connection2(void)
const Memory *mem = os_memory();
ck_assert(mem != nullptr);

Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
Logger *logger = logger_new();
Mono_Time *mono_time = mono_time_new(mem, nullptr);
Logger *logger = logger_new(mem);

tcp_oobdata_callback_called = 0;
tcp_data_callback_called = 0;
Expand Down
9 changes: 6 additions & 3 deletions auto_tests/announce_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include "../toxcore/mono_time.h"
#include "../toxcore/forwarding.h"
#include "../toxcore/net_crypto.h"
#include "../toxcore/os_memory.h"
#include "../toxcore/os_network.h"
#include "../toxcore/os_random.h"
#include "../toxcore/util.h"
#include "auto_test_support.h"
#include "check_compat.h"
Expand Down Expand Up @@ -57,16 +60,16 @@ static void test_store_data(void)
const Memory *mem = os_memory();
ck_assert(mem != nullptr);

Logger *log = logger_new();
Logger *log = logger_new(mem);
ck_assert(log != nullptr);
logger_callback_log(log, print_debug_logger, nullptr, nullptr);
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
Mono_Time *mono_time = mono_time_new(mem, nullptr);
ck_assert(mono_time != nullptr);
Networking_Core *net = new_networking_no_udp(log, mem, ns);
ck_assert(net != nullptr);
DHT *dht = new_dht(log, mem, rng, ns, mono_time, net, true, true);
ck_assert(dht != nullptr);
Forwarding *forwarding = new_forwarding(log, rng, mono_time, dht);
Forwarding *forwarding = new_forwarding(log, mem, rng, mono_time, dht);
ck_assert(forwarding != nullptr);
Announcements *announce = new_announcements(log, mem, rng, mono_time, forwarding);
ck_assert(announce != nullptr);
Expand Down
Loading

0 comments on commit cfbef98

Please sign in to comment.