Skip to content

Commit

Permalink
refactor: Deduplicate a bunch of code in TCP client/server.
Browse files Browse the repository at this point in the history
Also generally a bit of cleanup and better layering.
  • Loading branch information
iphydf committed Jan 16, 2022
1 parent c081a50 commit bb3ae52
Show file tree
Hide file tree
Showing 17 changed files with 520 additions and 599 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ set(toxcore_SOURCES ${toxcore_SOURCES}
set(toxcore_SOURCES ${toxcore_SOURCES}
toxcore/TCP_client.c
toxcore/TCP_client.h
toxcore/TCP_common.c
toxcore/TCP_common.h
toxcore/TCP_connection.c
toxcore/TCP_connection.h
toxcore/TCP_server.c
Expand Down
3 changes: 3 additions & 0 deletions auto_tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ flaky_tests = {
"//c-toxcore/toxcore",
"//c-toxcore/toxcore:DHT_srcs",
"//c-toxcore/toxcore:Messenger",
"//c-toxcore/toxcore:TCP_client",
"//c-toxcore/toxcore:TCP_common",
"//c-toxcore/toxcore:TCP_connection",
"//c-toxcore/toxcore:TCP_server",
"//c-toxcore/toxcore:ccompat",
"//c-toxcore/toxcore:crypto_core",
"//c-toxcore/toxcore:friend_connection",
Expand Down
21 changes: 11 additions & 10 deletions auto_tests/TCP_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "../testing/misc_tools.h"
#include "../toxcore/TCP_client.h"
#include "../toxcore/TCP_common.h"
#include "../toxcore/TCP_server.h"
#include "../toxcore/crypto_core.h"
#include "../toxcore/mono_time.h"
Expand Down Expand Up @@ -241,7 +242,7 @@ static void kill_TCP_con(struct sec_TCP_con *con)
free(con);
}

static int write_packet_TCP_secure_connection(struct sec_TCP_con *con, uint8_t *data, uint16_t length)
static int write_packet_TCP_test_connection(struct sec_TCP_con *con, uint8_t *data, uint16_t length)
{
VLA(uint8_t, packet, sizeof(uint16_t) + length + CRYPTO_MAC_SIZE);

Expand Down Expand Up @@ -290,9 +291,9 @@ START_TEST(test_some)

// Sending wrong public keys to test server response.
memcpy(requ_p + 1, con3->public_key, CRYPTO_PUBLIC_KEY_SIZE);
write_packet_TCP_secure_connection(con1, requ_p, sizeof(requ_p));
write_packet_TCP_test_connection(con1, requ_p, sizeof(requ_p));
memcpy(requ_p + 1, con1->public_key, CRYPTO_PUBLIC_KEY_SIZE);
write_packet_TCP_secure_connection(con3, requ_p, sizeof(requ_p));
write_packet_TCP_test_connection(con3, requ_p, sizeof(requ_p));

do_TCP_server_delay(tcp_s, mono_time, 50);

Expand All @@ -313,9 +314,9 @@ START_TEST(test_some)

uint8_t test_packet[512] = {16, 17, 16, 86, 99, 127, 255, 189, 78}; // What is this packet????

write_packet_TCP_secure_connection(con3, test_packet, sizeof(test_packet));
write_packet_TCP_secure_connection(con3, test_packet, sizeof(test_packet));
write_packet_TCP_secure_connection(con3, test_packet, sizeof(test_packet));
write_packet_TCP_test_connection(con3, test_packet, sizeof(test_packet));
write_packet_TCP_test_connection(con3, test_packet, sizeof(test_packet));
write_packet_TCP_test_connection(con3, test_packet, sizeof(test_packet));

do_TCP_server_delay(tcp_s, mono_time, 50);

Expand All @@ -339,9 +340,9 @@ START_TEST(test_some)
ck_assert_msg(len == sizeof(test_packet), "wrong len %d", len);
ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1],
data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]);
write_packet_TCP_secure_connection(con1, test_packet, sizeof(test_packet));
write_packet_TCP_secure_connection(con1, test_packet, sizeof(test_packet));
write_packet_TCP_secure_connection(con1, test_packet, sizeof(test_packet));
write_packet_TCP_test_connection(con1, test_packet, sizeof(test_packet));
write_packet_TCP_test_connection(con1, test_packet, sizeof(test_packet));
write_packet_TCP_test_connection(con1, test_packet, sizeof(test_packet));
do_TCP_server_delay(tcp_s, mono_time, 50);
len = read_packet_sec_TCP(con3, data, 2 + sizeof(test_packet) + CRYPTO_MAC_SIZE);
ck_assert_msg(len == sizeof(test_packet), "wrong len %d", len);
Expand All @@ -357,7 +358,7 @@ START_TEST(test_some)
data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]);

uint8_t ping_packet[1 + sizeof(uint64_t)] = {TCP_PACKET_PING, 8, 6, 9, 67};
write_packet_TCP_secure_connection(con1, ping_packet, sizeof(ping_packet));
write_packet_TCP_test_connection(con1, ping_packet, sizeof(ping_packet));

do_TCP_server_delay(tcp_s, mono_time, 50);

Expand Down
2 changes: 1 addition & 1 deletion other/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ cc_binary(
"//c-toxcore/testing:misc_tools",
"//c-toxcore/toxcore",
"//c-toxcore/toxcore:DHT",
"//c-toxcore/toxcore:TCP_connection",
"//c-toxcore/toxcore:TCP_server",
"//c-toxcore/toxcore:friend_requests",
"//c-toxcore/toxcore:logger",
"//c-toxcore/toxcore:mono_time",
Expand Down
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ cc_binary(
"//c-toxcore/other:bootstrap_node_packets",
"//c-toxcore/toxcore",
"//c-toxcore/toxcore:DHT",
"//c-toxcore/toxcore:TCP_connection",
"//c-toxcore/toxcore:TCP_server",
"//c-toxcore/toxcore:ccompat",
"//c-toxcore/toxcore:logger",
"//c-toxcore/toxcore:mono_time",
Expand Down
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/docker/tox-bootstrapd.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
058c26e86ce3fcdd5d32850f46434e618bec6b122288fbdbc7cf9a9d23ee1d9c /usr/local/bin/tox-bootstrapd
bf2c94623c4d09e19703ed536caf5dd0de7fc7b06620616631cea273dbae5183 /usr/local/bin/tox-bootstrapd
46 changes: 41 additions & 5 deletions toxcore/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,19 @@ cc_library(
)

cc_library(
name = "TCP_connection",
name = "TCP_common",
srcs = ["TCP_common.c"],
hdrs = ["TCP_common.h"],
visibility = ["//c-toxcore/auto_tests:__pkg__"],
deps = [":network"],
)

cc_library(
name = "TCP_server",
srcs = [
"TCP_client.c",
"TCP_connection.c",
"TCP_server.c",
],
hdrs = [
"TCP_client.h",
"TCP_connection.h",
"TCP_server.h",
],
copts = select({
Expand All @@ -274,6 +278,36 @@ cc_library(
"//c-toxcore/other/bootstrap_daemon:__pkg__",
],
deps = [
":TCP_common",
":crypto_core",
":list",
":mono_time",
":network",
":onion",
],
)

cc_library(
name = "TCP_client",
srcs = ["TCP_client.c"],
hdrs = ["TCP_client.h"],
visibility = ["//c-toxcore/auto_tests:__pkg__"],
deps = [
":TCP_common",
":mono_time",
":network",
],
)

cc_library(
name = "TCP_connection",
srcs = ["TCP_connection.c"],
hdrs = ["TCP_connection.h"],
visibility = ["//c-toxcore/auto_tests:__pkg__"],
deps = [
":DHT",
":TCP_client",
":TCP_common",
":crypto_core",
":list",
":mono_time",
Expand Down Expand Up @@ -301,6 +335,7 @@ cc_library(
deps = [
":DHT",
":TCP_connection",
":list",
":mono_time",
":network",
],
Expand Down Expand Up @@ -386,6 +421,7 @@ cc_library(
"//c-toxcore/toxav:__pkg__",
],
deps = [
":TCP_server",
":friend_requests",
":logger",
":mono_time",
Expand Down
2 changes: 2 additions & 0 deletions toxcore/Makefile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ libtoxcore_la_SOURCES = ../toxcore/ccompat.h \
../toxcore/onion_client.c \
../toxcore/TCP_client.h \
../toxcore/TCP_client.c \
../toxcore/TCP_common.h \
../toxcore/TCP_common.c \
../toxcore/TCP_server.h \
../toxcore/TCP_server.c \
../toxcore/TCP_connection.h \
Expand Down
1 change: 1 addition & 0 deletions toxcore/Messenger.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#ifndef C_TOXCORE_TOXCORE_MESSENGER_H
#define C_TOXCORE_TOXCORE_MESSENGER_H

#include "TCP_server.h"
#include "friend_connection.h"
#include "friend_requests.h"
#include "logger.h"
Expand Down
Loading

0 comments on commit bb3ae52

Please sign in to comment.