Skip to content

Commit 271bd9b

Browse files
committed
refactor: Move some OS-specifics into tox_system.
1 parent 944cc83 commit 271bd9b

File tree

174 files changed

+3270
-1503
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

174 files changed

+3270
-1503
lines changed

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
_build
2+
_install

.github/scripts/flags.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ add_flag -O3 -march=native
2929
# Warn on non-ISO C.
3030
add_c_flag -pedantic
3131
add_c_flag -std=c99
32-
add_cxx_flag -std=c++11
32+
add_cxx_flag -std=c++17
3333

3434
add_flag -g3
3535
add_flag -ftrapv

BUILD.bazel

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,49 @@ project()
88
genrule(
99
name = "public_headers",
1010
srcs = [
11+
"toxav.h",
12+
"tox.h",
13+
"toxencryptsave.h",
1114
"//c-toxcore/toxav:toxav.h",
1215
"//c-toxcore/toxcore:tox.h",
16+
"//c-toxcore/toxcore:tox_attributes.h",
17+
"//c-toxcore/toxcore:tox_logger.h",
18+
"//c-toxcore/toxcore:tox_memory.h",
19+
"//c-toxcore/toxcore:tox_network.h",
20+
"//c-toxcore/toxcore:tox_random.h",
21+
"//c-toxcore/toxcore:tox_system.h",
22+
"//c-toxcore/toxcore:tox_time.h",
1323
"//c-toxcore/toxencryptsave:toxencryptsave.h",
1424
],
1525
outs = [
1626
"tox/toxav.h",
1727
"tox/tox.h",
1828
"tox/toxencryptsave.h",
29+
"tox/toxav/toxav.h",
30+
"tox/toxcore/tox.h",
31+
"tox/toxcore/tox_attributes.h",
32+
"tox/toxcore/tox_logger.h",
33+
"tox/toxcore/tox_memory.h",
34+
"tox/toxcore/tox_network.h",
35+
"tox/toxcore/tox_random.h",
36+
"tox/toxcore/tox_system.h",
37+
"tox/toxcore/tox_time.h",
38+
"tox/toxencryptsave/toxencryptsave.h",
1939
],
2040
cmd = """
21-
cp $(location //c-toxcore/toxav:toxav.h) $(GENDIR)/c-toxcore/tox/toxav.h
22-
cp $(location //c-toxcore/toxcore:tox.h) $(GENDIR)/c-toxcore/tox/tox.h
23-
cp $(location //c-toxcore/toxencryptsave:toxencryptsave.h) $(GENDIR)/c-toxcore/tox/toxencryptsave.h
41+
cp $(location toxav.h) $(GENDIR)/c-toxcore/tox/toxav.h
42+
cp $(location tox.h) $(GENDIR)/c-toxcore/tox/tox.h
43+
cp $(location toxencryptsave.h) $(GENDIR)/c-toxcore/tox/toxencryptsave.h
44+
cp $(location //c-toxcore/toxav:toxav.h) $(GENDIR)/c-toxcore/tox/toxav/toxav.h
45+
cp $(location //c-toxcore/toxcore:tox.h) $(GENDIR)/c-toxcore/tox/toxcore/tox.h
46+
cp $(location //c-toxcore/toxcore:tox_attributes.h) $(GENDIR)/c-toxcore/tox/toxcore/tox_attributes.h
47+
cp $(location //c-toxcore/toxcore:tox_logger.h) $(GENDIR)/c-toxcore/tox/toxcore/tox_logger.h
48+
cp $(location //c-toxcore/toxcore:tox_memory.h) $(GENDIR)/c-toxcore/tox/toxcore/tox_memory.h
49+
cp $(location //c-toxcore/toxcore:tox_network.h) $(GENDIR)/c-toxcore/tox/toxcore/tox_network.h
50+
cp $(location //c-toxcore/toxcore:tox_random.h) $(GENDIR)/c-toxcore/tox/toxcore/tox_random.h
51+
cp $(location //c-toxcore/toxcore:tox_system.h) $(GENDIR)/c-toxcore/tox/toxcore/tox_system.h
52+
cp $(location //c-toxcore/toxcore:tox_time.h) $(GENDIR)/c-toxcore/tox/toxcore/tox_time.h
53+
cp $(location //c-toxcore/toxencryptsave:toxencryptsave.h) $(GENDIR)/c-toxcore/tox/toxencryptsave/toxencryptsave.h
2454
""",
2555
visibility = ["//visibility:public"],
2656
)

CMakeLists.txt

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,17 @@ set(toxcore_SOURCES
282282
toxcore/onion_client.c
283283
toxcore/onion_client.h
284284
toxcore/onion.h
285+
toxcore/os_logger.c
286+
toxcore/os_logger.h
287+
toxcore/os_memory.c
288+
toxcore/os_memory.h
289+
toxcore/os_network.c
290+
toxcore/os_network.h
291+
toxcore/os_network_impl.h
292+
toxcore/os_random.c
293+
toxcore/os_random.h
294+
toxcore/os_system.c
295+
toxcore/os_system.h
285296
toxcore/ping_array.c
286297
toxcore/ping_array.h
287298
toxcore/ping.c
@@ -302,11 +313,29 @@ set(toxcore_SOURCES
302313
toxcore/timed_auth.h
303314
toxcore/tox_api.c
304315
toxcore/tox.c
316+
toxcore/tox.h
305317
toxcore/tox_dispatch.c
306318
toxcore/tox_dispatch.h
307319
toxcore/tox_events.c
308320
toxcore/tox_events.h
309-
toxcore/tox.h
321+
toxcore/tox_logger.c
322+
toxcore/tox_logger.h
323+
toxcore/tox_logger_impl.h
324+
toxcore/tox_memory.c
325+
toxcore/tox_memory.h
326+
toxcore/tox_memory_impl.h
327+
toxcore/tox_network.c
328+
toxcore/tox_network.h
329+
toxcore/tox_network_impl.h
330+
toxcore/tox_random.c
331+
toxcore/tox_random.h
332+
toxcore/tox_random_impl.h
333+
toxcore/tox_system.c
334+
toxcore/tox_system.h
335+
toxcore/tox_system_impl.h
336+
toxcore/tox_time.c
337+
toxcore/tox_time.h
338+
toxcore/tox_time_impl.h
310339
toxcore/tox_private.c
311340
toxcore/tox_private.h
312341
toxcore/tox_unpack.c
@@ -316,9 +345,16 @@ set(toxcore_SOURCES
316345
set(toxcore_LINK_MODULES ${toxcore_LINK_MODULES} ${LIBSODIUM_LIBRARIES})
317346
set(toxcore_PKGCONFIG_REQUIRES ${toxcore_PKGCONFIG_REQUIRES} libsodium)
318347
set(toxcore_API_HEADERS
348+
${toxcore_SOURCE_DIR}/tox.h^tox
319349
${toxcore_SOURCE_DIR}/toxcore/tox.h^tox
350+
${toxcore_SOURCE_DIR}/toxcore/tox_dispatch.h^tox
320351
${toxcore_SOURCE_DIR}/toxcore/tox_events.h^tox
321-
${toxcore_SOURCE_DIR}/toxcore/tox_dispatch.h^tox)
352+
${toxcore_SOURCE_DIR}/toxcore/tox_logger.h^tox
353+
${toxcore_SOURCE_DIR}/toxcore/tox_memory.h^tox
354+
${toxcore_SOURCE_DIR}/toxcore/tox_network.h^tox
355+
${toxcore_SOURCE_DIR}/toxcore/tox_random.h^tox
356+
${toxcore_SOURCE_DIR}/toxcore/tox_system.h^tox
357+
${toxcore_SOURCE_DIR}/toxcore/tox_time.h^tox)
322358

323359
################################################################################
324360
#
@@ -346,6 +382,7 @@ if(BUILD_TOXAV)
346382
toxav/video.c
347383
toxav/video.h)
348384
set(toxcore_API_HEADERS ${toxcore_API_HEADERS}
385+
${toxcore_SOURCE_DIR}/toxav.h^toxav
349386
${toxcore_SOURCE_DIR}/toxav/toxav.h^toxav)
350387

351388
set(toxcore_LINK_MODULES ${toxcore_LINK_MODULES} ${OPUS_LIBRARIES} ${VPX_LIBRARIES})
@@ -362,6 +399,7 @@ set(toxcore_SOURCES ${toxcore_SOURCES}
362399
toxencryptsave/toxencryptsave.c
363400
toxencryptsave/toxencryptsave.h)
364401
set(toxcore_API_HEADERS ${toxcore_API_HEADERS}
402+
${toxcore_SOURCE_DIR}/toxencryptsave.h^tox
365403
${toxcore_SOURCE_DIR}/toxencryptsave/toxencryptsave.h^tox)
366404

367405
################################################################################

auto_tests/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ cc_library(
1919
"//c-toxcore/toxcore:Messenger",
2020
"//c-toxcore/toxcore:mono_time",
2121
"//c-toxcore/toxcore:tox",
22+
"//c-toxcore/toxcore:tox_time",
2223
],
2324
)
2425

@@ -61,9 +62,13 @@ flaky_tests = {
6162
"//c-toxcore/toxcore:onion",
6263
"//c-toxcore/toxcore:onion_announce",
6364
"//c-toxcore/toxcore:onion_client",
65+
"//c-toxcore/toxcore:os_memory",
66+
"//c-toxcore/toxcore:os_network",
67+
"//c-toxcore/toxcore:os_random",
6468
"//c-toxcore/toxcore:tox",
6569
"//c-toxcore/toxcore:tox_dispatch",
6670
"//c-toxcore/toxcore:tox_events",
71+
"//c-toxcore/toxcore:tox_time",
6772
"//c-toxcore/toxcore:util",
6873
"//c-toxcore/toxencryptsave",
6974
"@libsodium",

auto_tests/TCP_test.c

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#include "../toxcore/TCP_server.h"
99
#include "../toxcore/crypto_core.h"
1010
#include "../toxcore/mono_time.h"
11+
#include "../toxcore/os_random.h"
12+
#include "../toxcore/os_network.h"
13+
#include "../toxcore/os_memory.h"
1114
#include "../toxcore/util.h"
1215
#include "auto_test_support.h"
1316

@@ -45,15 +48,15 @@ static uint16_t ports[NUM_PORTS] = {13215, 33445, 25643};
4548

4649
static void test_basic(void)
4750
{
48-
const Random *rng = system_random();
51+
const Random *rng = os_random();
4952
ck_assert(rng != nullptr);
50-
const Network *ns = system_network();
53+
const Network *ns = os_network();
5154
ck_assert(ns != nullptr);
52-
const Memory *mem = system_memory();
55+
const Memory *mem = os_memory();
5356
ck_assert(mem != nullptr);
5457

55-
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
56-
Logger *logger = logger_new();
58+
Mono_Time *mono_time = mono_time_new(mem, nullptr);
59+
Logger *logger = logger_new(mem);
5760
logger_callback_log(logger, print_debug_logger, nullptr, nullptr);
5861

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

104107
// Encrypting handshake
105108
int ret = encrypt_data(self_public_key, f_secret_key, handshake + CRYPTO_PUBLIC_KEY_SIZE, handshake_plain,
106-
TCP_HANDSHAKE_PLAIN_SIZE, handshake + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE);
109+
TCP_HANDSHAKE_PLAIN_SIZE, handshake + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE, mem);
107110
ck_assert_msg(ret == TCP_CLIENT_HANDSHAKE_SIZE - (CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE),
108111
"encrypt_data() call failed.");
109112

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

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

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

@@ -229,7 +232,7 @@ static struct sec_TCP_con *new_TCP_con(const Logger *logger, const Memory *mem,
229232
random_nonce(rng, handshake + CRYPTO_PUBLIC_KEY_SIZE);
230233

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

@@ -249,7 +252,7 @@ static struct sec_TCP_con *new_TCP_con(const Logger *logger, const Memory *mem,
249252
ck_assert_msg(net_recv(sec_c->ns, logger, sock, response, TCP_SERVER_HANDSHAKE_SIZE, &localhost) == TCP_SERVER_HANDSHAKE_SIZE,
250253
"Failed to receive server handshake response.");
251254
ret = decrypt_data(tcp_server_public_key(tcp_s), f_secret_key, response, response + CRYPTO_NONCE_SIZE,
252-
TCP_SERVER_HANDSHAKE_SIZE - CRYPTO_NONCE_SIZE, response_plain);
255+
TCP_SERVER_HANDSHAKE_SIZE - CRYPTO_NONCE_SIZE, response_plain, mem);
253256
ck_assert_msg(ret == TCP_HANDSHAKE_PLAIN_SIZE, "Failed to decrypt server handshake response.");
254257
encrypt_precompute(response_plain, t_secret_key, sec_c->shared_key);
255258
memcpy(sec_c->recv_nonce, response_plain + CRYPTO_SHARED_KEY_SIZE, CRYPTO_NONCE_SIZE);
@@ -270,7 +273,7 @@ static int write_packet_TCP_test_connection(const Logger *logger, struct sec_TCP
270273

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

275278
if ((unsigned int)len != (SIZEOF_VLA(packet) - sizeof(uint16_t))) {
276279
return -1;
@@ -295,23 +298,23 @@ static int read_packet_sec_TCP(const Logger *logger, struct sec_TCP_con *con, ui
295298

296299
int rlen = net_recv(con->ns, logger, con->sock, data, length, &localhost);
297300
ck_assert_msg(rlen == length, "Did not receive packet of correct length. Wanted %i, instead got %i", length, rlen);
298-
rlen = decrypt_data_symmetric(con->shared_key, con->recv_nonce, data + 2, length - 2, data);
301+
rlen = decrypt_data_symmetric(con->shared_key, con->recv_nonce, data + 2, length - 2, data, con->mem);
299302
ck_assert_msg(rlen != -1, "Failed to decrypt a received packet from the Relay server.");
300303
increment_nonce(con->recv_nonce);
301304
return rlen;
302305
}
303306

304307
static void test_some(void)
305308
{
306-
const Random *rng = system_random();
309+
const Random *rng = os_random();
307310
ck_assert(rng != nullptr);
308-
const Network *ns = system_network();
311+
const Network *ns = os_network();
309312
ck_assert(ns != nullptr);
310-
const Memory *mem = system_memory();
313+
const Memory *mem = os_memory();
311314
ck_assert(mem != nullptr);
312315

313-
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
314-
Logger *logger = logger_new();
316+
Mono_Time *mono_time = mono_time_new(mem, nullptr);
317+
Logger *logger = logger_new(mem);
315318

316319
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
317320
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
@@ -498,15 +501,15 @@ static int oob_data_callback(void *object, const uint8_t *public_key, const uint
498501

499502
static void test_client(void)
500503
{
501-
const Random *rng = system_random();
504+
const Random *rng = os_random();
502505
ck_assert(rng != nullptr);
503-
const Network *ns = system_network();
506+
const Network *ns = os_network();
504507
ck_assert(ns != nullptr);
505-
const Memory *mem = system_memory();
508+
const Memory *mem = os_memory();
506509
ck_assert(mem != nullptr);
507510

508-
Logger *logger = logger_new();
509-
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
511+
Logger *logger = logger_new(mem);
512+
Mono_Time *mono_time = mono_time_new(mem, nullptr);
510513

511514
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
512515
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
@@ -632,15 +635,15 @@ static void test_client(void)
632635
// Test how the client handles servers that don't respond.
633636
static void test_client_invalid(void)
634637
{
635-
const Random *rng = system_random();
638+
const Random *rng = os_random();
636639
ck_assert(rng != nullptr);
637-
const Network *ns = system_network();
640+
const Network *ns = os_network();
638641
ck_assert(ns != nullptr);
639-
const Memory *mem = system_memory();
642+
const Memory *mem = os_memory();
640643
ck_assert(mem != nullptr);
641644

642-
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
643-
Logger *logger = logger_new();
645+
Mono_Time *mono_time = mono_time_new(mem, nullptr);
646+
Logger *logger = logger_new(mem);
644647

645648
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
646649
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
@@ -711,15 +714,15 @@ static int tcp_data_callback(void *object, int id, const uint8_t *data, uint16_t
711714

712715
static void test_tcp_connection(void)
713716
{
714-
const Random *rng = system_random();
717+
const Random *rng = os_random();
715718
ck_assert(rng != nullptr);
716-
const Network *ns = system_network();
719+
const Network *ns = os_network();
717720
ck_assert(ns != nullptr);
718-
const Memory *mem = system_memory();
721+
const Memory *mem = os_memory();
719722
ck_assert(mem != nullptr);
720723

721-
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
722-
Logger *logger = logger_new();
724+
Mono_Time *mono_time = mono_time_new(mem, nullptr);
725+
Logger *logger = logger_new(mem);
723726

724727
tcp_data_callback_called = 0;
725728
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
@@ -824,15 +827,15 @@ static int tcp_oobdata_callback(void *object, const uint8_t *public_key, unsigne
824827

825828
static void test_tcp_connection2(void)
826829
{
827-
const Random *rng = system_random();
830+
const Random *rng = os_random();
828831
ck_assert(rng != nullptr);
829-
const Network *ns = system_network();
832+
const Network *ns = os_network();
830833
ck_assert(ns != nullptr);
831-
const Memory *mem = system_memory();
834+
const Memory *mem = os_memory();
832835
ck_assert(mem != nullptr);
833836

834-
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
835-
Logger *logger = logger_new();
837+
Mono_Time *mono_time = mono_time_new(mem, nullptr);
838+
Logger *logger = logger_new(mem);
836839

837840
tcp_oobdata_callback_called = 0;
838841
tcp_data_callback_called = 0;

0 commit comments

Comments
 (0)