Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make TCP_Server opaque. #156

Merged
merged 1 commit into from
Sep 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions auto_tests/TCP_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ START_TEST(test_basic)
crypto_box_keypair(self_public_key, self_secret_key);
TCP_Server *tcp_s = new_TCP_server(1, NUM_PORTS, ports, self_secret_key, NULL);
ck_assert_msg(tcp_s != NULL, "Failed to create TCP relay server");
ck_assert_msg(tcp_s->num_listening_socks == NUM_PORTS, "Failed to bind to all ports");
ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind to all ports");

sock_t sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
struct sockaddr_in6 addr6_loopback = {0};
Expand Down Expand Up @@ -154,7 +154,7 @@ static struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s)
memcpy(handshake, sec_c->public_key, crypto_box_PUBLICKEYBYTES);
new_nonce(handshake + crypto_box_PUBLICKEYBYTES);

ret = encrypt_data(tcp_s->public_key, f_secret_key, handshake + crypto_box_PUBLICKEYBYTES, handshake_plain,
ret = encrypt_data(tcp_server_public_key(tcp_s), f_secret_key, handshake + crypto_box_PUBLICKEYBYTES, handshake_plain,
TCP_HANDSHAKE_PLAIN_SIZE, handshake + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES);
ck_assert_msg(ret == TCP_CLIENT_HANDSHAKE_SIZE - (crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES),
"Encrypt failed.");
Expand All @@ -167,7 +167,7 @@ static struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s)
uint8_t response[TCP_SERVER_HANDSHAKE_SIZE];
uint8_t response_plain[TCP_HANDSHAKE_PLAIN_SIZE];
ck_assert_msg(recv(sock, response, TCP_SERVER_HANDSHAKE_SIZE, 0) == TCP_SERVER_HANDSHAKE_SIZE, "recv Failed.");
ret = decrypt_data(tcp_s->public_key, f_secret_key, response, response + crypto_box_NONCEBYTES,
ret = decrypt_data(tcp_server_public_key(tcp_s), f_secret_key, response, response + crypto_box_NONCEBYTES,
TCP_SERVER_HANDSHAKE_SIZE - crypto_box_NONCEBYTES, response_plain);
ck_assert_msg(ret == TCP_HANDSHAKE_PLAIN_SIZE, "Decrypt Failed.");
encrypt_precompute(response_plain, t_secret_key, sec_c->shared_key);
Expand Down Expand Up @@ -217,7 +217,7 @@ START_TEST(test_some)
crypto_box_keypair(self_public_key, self_secret_key);
TCP_Server *tcp_s = new_TCP_server(1, NUM_PORTS, ports, self_secret_key, NULL);
ck_assert_msg(tcp_s != NULL, "Failed to create TCP relay server");
ck_assert_msg(tcp_s->num_listening_socks == NUM_PORTS, "Failed to bind to all ports");
ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind to all ports");

struct sec_TCP_con *con1 = new_TCP_con(tcp_s);
struct sec_TCP_con *con2 = new_TCP_con(tcp_s);
Expand Down Expand Up @@ -394,7 +394,7 @@ START_TEST(test_client)
crypto_box_keypair(self_public_key, self_secret_key);
TCP_Server *tcp_s = new_TCP_server(1, NUM_PORTS, ports, self_secret_key, NULL);
ck_assert_msg(tcp_s != NULL, "Failed to create TCP relay server");
ck_assert_msg(tcp_s->num_listening_socks == NUM_PORTS, "Failed to bind to all ports");
ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind to all ports");

uint8_t f_public_key[crypto_box_PUBLICKEYBYTES];
uint8_t f_secret_key[crypto_box_SECRETKEYBYTES];
Expand Down Expand Up @@ -554,7 +554,7 @@ START_TEST(test_tcp_connection)
uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
crypto_box_keypair(self_public_key, self_secret_key);
TCP_Server *tcp_s = new_TCP_server(1, NUM_PORTS, ports, self_secret_key, NULL);
ck_assert_msg(public_key_cmp(tcp_s->public_key, self_public_key) == 0, "Wrong public key");
ck_assert_msg(public_key_cmp(tcp_server_public_key(tcp_s), self_public_key) == 0, "Wrong public key");

TCP_Proxy_Info proxy_info;
proxy_info.proxy_type = TCP_PROXY_NONE;
Expand All @@ -574,13 +574,13 @@ START_TEST(test_tcp_connection)

int connection = new_tcp_connection_to(tc_1, tc_2->self_public_key, 123);
ck_assert_msg(connection == 0, "Connection id wrong");
ck_assert_msg(add_tcp_relay_connection(tc_1, connection, ip_port_tcp_s, tcp_s->public_key) == 0,
ck_assert_msg(add_tcp_relay_connection(tc_1, connection, ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0,
"Could not add tcp relay to connection\n");

ip_port_tcp_s.port = htons(ports[rand() % NUM_PORTS]);
connection = new_tcp_connection_to(tc_2, tc_1->self_public_key, 123);
ck_assert_msg(connection == 0, "Connection id wrong");
ck_assert_msg(add_tcp_relay_connection(tc_2, connection, ip_port_tcp_s, tcp_s->public_key) == 0,
ck_assert_msg(add_tcp_relay_connection(tc_2, connection, ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0,
"Could not add tcp relay to connection\n");

ck_assert_msg(new_tcp_connection_to(tc_2, tc_1->self_public_key, 123) == -1, "Managed to readd same connection\n");
Expand Down Expand Up @@ -660,7 +660,7 @@ START_TEST(test_tcp_connection2)
uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
crypto_box_keypair(self_public_key, self_secret_key);
TCP_Server *tcp_s = new_TCP_server(1, NUM_PORTS, ports, self_secret_key, NULL);
ck_assert_msg(public_key_cmp(tcp_s->public_key, self_public_key) == 0, "Wrong public key");
ck_assert_msg(public_key_cmp(tcp_server_public_key(tcp_s), self_public_key) == 0, "Wrong public key");

TCP_Proxy_Info proxy_info;
proxy_info.proxy_type = TCP_PROXY_NONE;
Expand All @@ -680,10 +680,11 @@ START_TEST(test_tcp_connection2)

int connection = new_tcp_connection_to(tc_1, tc_2->self_public_key, 123);
ck_assert_msg(connection == 0, "Connection id wrong");
ck_assert_msg(add_tcp_relay_connection(tc_1, connection, ip_port_tcp_s, tcp_s->public_key) == 0,
ck_assert_msg(add_tcp_relay_connection(tc_1, connection, ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0,
"Could not add tcp relay to connection\n");

ck_assert_msg(add_tcp_relay_global(tc_2, ip_port_tcp_s, tcp_s->public_key) == 0, "Could not add global relay");
ck_assert_msg(add_tcp_relay_global(tc_2, ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0,
"Could not add global relay");

c_sleep(50);
do_TCP_server(tcp_s);
Expand Down
3 changes: 2 additions & 1 deletion toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -2477,7 +2477,8 @@ void do_messenger(Messenger *m, void *userdata)
local_ip_port.port = m->options.tcp_server_port;
local_ip_port.ip.family = AF_INET;
local_ip_port.ip.ip4.uint32 = INADDR_LOOPBACK;
add_tcp_relay(m->net_crypto, local_ip_port, m->tcp_server->public_key);
add_tcp_relay(m->net_crypto, local_ip_port,
tcp_server_public_key(m->tcp_server));
}
}

Expand Down
36 changes: 36 additions & 0 deletions toxcore/TCP_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,42 @@
#include <sys/ioctl.h>
#endif

struct TCP_Server {
Onion *onion;

#ifdef TCP_SERVER_USE_EPOLL
int efd;
uint64_t last_run_pinged;
#endif
sock_t *socks_listening;
unsigned int num_listening_socks;

uint8_t public_key[crypto_box_PUBLICKEYBYTES];
uint8_t secret_key[crypto_box_SECRETKEYBYTES];
TCP_Secure_Connection incomming_connection_queue[MAX_INCOMMING_CONNECTIONS];
uint16_t incomming_connection_queue_index;
TCP_Secure_Connection unconfirmed_connection_queue[MAX_INCOMMING_CONNECTIONS];
uint16_t unconfirmed_connection_queue_index;

TCP_Secure_Connection *accepted_connection_array;
uint32_t size_accepted_connections;
uint32_t num_accepted_connections;

uint64_t counter;

BS_LIST accepted_key_list;
};

const uint8_t *tcp_server_public_key(const TCP_Server *tcp_server)
{
return tcp_server->public_key;
}

size_t tcp_server_listen_count(const TCP_Server *tcp_server)
{
return tcp_server->num_listening_socks;
}

/* return 1 on success
* return 0 on failure
*/
Expand Down
27 changes: 3 additions & 24 deletions toxcore/TCP_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,31 +115,10 @@ typedef struct TCP_Secure_Connection {
} TCP_Secure_Connection;


typedef struct {
Onion *onion;
typedef struct TCP_Server TCP_Server;

#ifdef TCP_SERVER_USE_EPOLL
int efd;
uint64_t last_run_pinged;
#endif
sock_t *socks_listening;
unsigned int num_listening_socks;

uint8_t public_key[crypto_box_PUBLICKEYBYTES];
uint8_t secret_key[crypto_box_SECRETKEYBYTES];
TCP_Secure_Connection incomming_connection_queue[MAX_INCOMMING_CONNECTIONS];
uint16_t incomming_connection_queue_index;
TCP_Secure_Connection unconfirmed_connection_queue[MAX_INCOMMING_CONNECTIONS];
uint16_t unconfirmed_connection_queue_index;

TCP_Secure_Connection *accepted_connection_array;
uint32_t size_accepted_connections;
uint32_t num_accepted_connections;

uint64_t counter;

BS_LIST accepted_key_list;
} TCP_Server;
const uint8_t *tcp_server_public_key(const TCP_Server *tcp_server);
size_t tcp_server_listen_count(const TCP_Server *tcp_server);

/* Create new TCP server instance.
*/
Expand Down