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 044ae63 commit 4ce02c0
Show file tree
Hide file tree
Showing 17 changed files with 522 additions and 605 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 @@ -240,7 +241,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, const uint8_t *data, uint16_t length)
static int write_packet_TCP_test_connection(struct sec_TCP_con *con, const uint8_t *data, uint16_t length)
{
VLA(uint8_t, packet, sizeof(uint16_t) + length + CRYPTO_MAC_SIZE);

Expand Down Expand Up @@ -289,9 +290,9 @@ static void test_some(void)

// 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 @@ -312,9 +313,9 @@ static void test_some(void)

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 @@ -338,9 +339,9 @@ static void test_some(void)
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 @@ -356,7 +357,7 @@ static void test_some(void)
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 @@ -19,7 +19,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 @@
7be4fcdc11f74036b97de9569eb06f82296f193db3e18e671171f3bc059f95a6 /usr/local/bin/tox-bootstrapd
fb46c678adbe48e846286d9cb45b560e26f51cb7eccb99378c57e66c6c49732b /usr/local/bin/tox-bootstrapd
54 changes: 43 additions & 11 deletions toxcore/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -253,17 +253,17 @@ cc_library(
)

cc_library(
name = "TCP_connection",
srcs = [
"TCP_client.c",
"TCP_connection.c",
"TCP_server.c",
],
hdrs = [
"TCP_client.h",
"TCP_connection.h",
"TCP_server.h",
],
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_server.c"],
hdrs = ["TCP_server.h"],
copts = select({
"//tools/config:linux": ["-DTCP_SERVER_USE_EPOLL=1"],
"//conditions:default": [],
Expand All @@ -274,6 +274,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 +331,7 @@ cc_library(
deps = [
":DHT",
":TCP_connection",
":list",
":mono_time",
":network",
],
Expand Down Expand Up @@ -386,6 +417,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 4ce02c0

Please sign in to comment.