diff options
author | iphydf <iphydf@users.noreply.github.com> | 2016-09-21 18:57:17 +0100 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2016-09-22 11:22:32 +0100 |
commit | 78d77349e499fe4e62d2187c8c696c6714ba6663 (patch) | |
tree | ab9c04418f4b9273d54d0341e04dc128944b0672 | |
parent | aa7670da5a7824c66ea062a523639e993f3c3163 (diff) |
Make TCP_Server opaque.
We should aim to make as many structures module-private as possible.
-rw-r--r-- | auto_tests/TCP_test.c | 23 | ||||
-rw-r--r-- | toxcore/Messenger.c | 3 | ||||
-rw-r--r-- | toxcore/TCP_server.c | 36 | ||||
-rw-r--r-- | toxcore/TCP_server.h | 27 |
4 files changed, 53 insertions, 36 deletions
diff --git a/auto_tests/TCP_test.c b/auto_tests/TCP_test.c index 2efed98f..7f96ef35 100644 --- a/auto_tests/TCP_test.c +++ b/auto_tests/TCP_test.c | |||
@@ -34,7 +34,7 @@ START_TEST(test_basic) | |||
34 | crypto_box_keypair(self_public_key, self_secret_key); | 34 | crypto_box_keypair(self_public_key, self_secret_key); |
35 | TCP_Server *tcp_s = new_TCP_server(1, NUM_PORTS, ports, self_secret_key, NULL); | 35 | TCP_Server *tcp_s = new_TCP_server(1, NUM_PORTS, ports, self_secret_key, NULL); |
36 | ck_assert_msg(tcp_s != NULL, "Failed to create TCP relay server"); | 36 | ck_assert_msg(tcp_s != NULL, "Failed to create TCP relay server"); |
37 | ck_assert_msg(tcp_s->num_listening_socks == NUM_PORTS, "Failed to bind to all ports"); | 37 | ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind to all ports"); |
38 | 38 | ||
39 | sock_t sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); | 39 | sock_t sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); |
40 | struct sockaddr_in6 addr6_loopback = {0}; | 40 | struct sockaddr_in6 addr6_loopback = {0}; |
@@ -154,7 +154,7 @@ static struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s) | |||
154 | memcpy(handshake, sec_c->public_key, crypto_box_PUBLICKEYBYTES); | 154 | memcpy(handshake, sec_c->public_key, crypto_box_PUBLICKEYBYTES); |
155 | new_nonce(handshake + crypto_box_PUBLICKEYBYTES); | 155 | new_nonce(handshake + crypto_box_PUBLICKEYBYTES); |
156 | 156 | ||
157 | ret = encrypt_data(tcp_s->public_key, f_secret_key, handshake + crypto_box_PUBLICKEYBYTES, handshake_plain, | 157 | ret = encrypt_data(tcp_server_public_key(tcp_s), f_secret_key, handshake + crypto_box_PUBLICKEYBYTES, handshake_plain, |
158 | TCP_HANDSHAKE_PLAIN_SIZE, handshake + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES); | 158 | TCP_HANDSHAKE_PLAIN_SIZE, handshake + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES); |
159 | ck_assert_msg(ret == TCP_CLIENT_HANDSHAKE_SIZE - (crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES), | 159 | ck_assert_msg(ret == TCP_CLIENT_HANDSHAKE_SIZE - (crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES), |
160 | "Encrypt failed."); | 160 | "Encrypt failed."); |
@@ -167,7 +167,7 @@ static struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s) | |||
167 | uint8_t response[TCP_SERVER_HANDSHAKE_SIZE]; | 167 | uint8_t response[TCP_SERVER_HANDSHAKE_SIZE]; |
168 | uint8_t response_plain[TCP_HANDSHAKE_PLAIN_SIZE]; | 168 | uint8_t response_plain[TCP_HANDSHAKE_PLAIN_SIZE]; |
169 | ck_assert_msg(recv(sock, response, TCP_SERVER_HANDSHAKE_SIZE, 0) == TCP_SERVER_HANDSHAKE_SIZE, "recv Failed."); | 169 | ck_assert_msg(recv(sock, response, TCP_SERVER_HANDSHAKE_SIZE, 0) == TCP_SERVER_HANDSHAKE_SIZE, "recv Failed."); |
170 | ret = decrypt_data(tcp_s->public_key, f_secret_key, response, response + crypto_box_NONCEBYTES, | 170 | ret = decrypt_data(tcp_server_public_key(tcp_s), f_secret_key, response, response + crypto_box_NONCEBYTES, |
171 | TCP_SERVER_HANDSHAKE_SIZE - crypto_box_NONCEBYTES, response_plain); | 171 | TCP_SERVER_HANDSHAKE_SIZE - crypto_box_NONCEBYTES, response_plain); |
172 | ck_assert_msg(ret == TCP_HANDSHAKE_PLAIN_SIZE, "Decrypt Failed."); | 172 | ck_assert_msg(ret == TCP_HANDSHAKE_PLAIN_SIZE, "Decrypt Failed."); |
173 | encrypt_precompute(response_plain, t_secret_key, sec_c->shared_key); | 173 | encrypt_precompute(response_plain, t_secret_key, sec_c->shared_key); |
@@ -217,7 +217,7 @@ START_TEST(test_some) | |||
217 | crypto_box_keypair(self_public_key, self_secret_key); | 217 | crypto_box_keypair(self_public_key, self_secret_key); |
218 | TCP_Server *tcp_s = new_TCP_server(1, NUM_PORTS, ports, self_secret_key, NULL); | 218 | TCP_Server *tcp_s = new_TCP_server(1, NUM_PORTS, ports, self_secret_key, NULL); |
219 | ck_assert_msg(tcp_s != NULL, "Failed to create TCP relay server"); | 219 | ck_assert_msg(tcp_s != NULL, "Failed to create TCP relay server"); |
220 | ck_assert_msg(tcp_s->num_listening_socks == NUM_PORTS, "Failed to bind to all ports"); | 220 | ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind to all ports"); |
221 | 221 | ||
222 | struct sec_TCP_con *con1 = new_TCP_con(tcp_s); | 222 | struct sec_TCP_con *con1 = new_TCP_con(tcp_s); |
223 | struct sec_TCP_con *con2 = new_TCP_con(tcp_s); | 223 | struct sec_TCP_con *con2 = new_TCP_con(tcp_s); |
@@ -394,7 +394,7 @@ START_TEST(test_client) | |||
394 | crypto_box_keypair(self_public_key, self_secret_key); | 394 | crypto_box_keypair(self_public_key, self_secret_key); |
395 | TCP_Server *tcp_s = new_TCP_server(1, NUM_PORTS, ports, self_secret_key, NULL); | 395 | TCP_Server *tcp_s = new_TCP_server(1, NUM_PORTS, ports, self_secret_key, NULL); |
396 | ck_assert_msg(tcp_s != NULL, "Failed to create TCP relay server"); | 396 | ck_assert_msg(tcp_s != NULL, "Failed to create TCP relay server"); |
397 | ck_assert_msg(tcp_s->num_listening_socks == NUM_PORTS, "Failed to bind to all ports"); | 397 | ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind to all ports"); |
398 | 398 | ||
399 | uint8_t f_public_key[crypto_box_PUBLICKEYBYTES]; | 399 | uint8_t f_public_key[crypto_box_PUBLICKEYBYTES]; |
400 | uint8_t f_secret_key[crypto_box_SECRETKEYBYTES]; | 400 | uint8_t f_secret_key[crypto_box_SECRETKEYBYTES]; |
@@ -554,7 +554,7 @@ START_TEST(test_tcp_connection) | |||
554 | uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; | 554 | uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; |
555 | crypto_box_keypair(self_public_key, self_secret_key); | 555 | crypto_box_keypair(self_public_key, self_secret_key); |
556 | TCP_Server *tcp_s = new_TCP_server(1, NUM_PORTS, ports, self_secret_key, NULL); | 556 | TCP_Server *tcp_s = new_TCP_server(1, NUM_PORTS, ports, self_secret_key, NULL); |
557 | ck_assert_msg(public_key_cmp(tcp_s->public_key, self_public_key) == 0, "Wrong public key"); | 557 | ck_assert_msg(public_key_cmp(tcp_server_public_key(tcp_s), self_public_key) == 0, "Wrong public key"); |
558 | 558 | ||
559 | TCP_Proxy_Info proxy_info; | 559 | TCP_Proxy_Info proxy_info; |
560 | proxy_info.proxy_type = TCP_PROXY_NONE; | 560 | proxy_info.proxy_type = TCP_PROXY_NONE; |
@@ -574,13 +574,13 @@ START_TEST(test_tcp_connection) | |||
574 | 574 | ||
575 | int connection = new_tcp_connection_to(tc_1, tc_2->self_public_key, 123); | 575 | int connection = new_tcp_connection_to(tc_1, tc_2->self_public_key, 123); |
576 | ck_assert_msg(connection == 0, "Connection id wrong"); | 576 | ck_assert_msg(connection == 0, "Connection id wrong"); |
577 | ck_assert_msg(add_tcp_relay_connection(tc_1, connection, ip_port_tcp_s, tcp_s->public_key) == 0, | 577 | ck_assert_msg(add_tcp_relay_connection(tc_1, connection, ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0, |
578 | "Could not add tcp relay to connection\n"); | 578 | "Could not add tcp relay to connection\n"); |
579 | 579 | ||
580 | ip_port_tcp_s.port = htons(ports[rand() % NUM_PORTS]); | 580 | ip_port_tcp_s.port = htons(ports[rand() % NUM_PORTS]); |
581 | connection = new_tcp_connection_to(tc_2, tc_1->self_public_key, 123); | 581 | connection = new_tcp_connection_to(tc_2, tc_1->self_public_key, 123); |
582 | ck_assert_msg(connection == 0, "Connection id wrong"); | 582 | ck_assert_msg(connection == 0, "Connection id wrong"); |
583 | ck_assert_msg(add_tcp_relay_connection(tc_2, connection, ip_port_tcp_s, tcp_s->public_key) == 0, | 583 | ck_assert_msg(add_tcp_relay_connection(tc_2, connection, ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0, |
584 | "Could not add tcp relay to connection\n"); | 584 | "Could not add tcp relay to connection\n"); |
585 | 585 | ||
586 | ck_assert_msg(new_tcp_connection_to(tc_2, tc_1->self_public_key, 123) == -1, "Managed to readd same connection\n"); | 586 | ck_assert_msg(new_tcp_connection_to(tc_2, tc_1->self_public_key, 123) == -1, "Managed to readd same connection\n"); |
@@ -660,7 +660,7 @@ START_TEST(test_tcp_connection2) | |||
660 | uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; | 660 | uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; |
661 | crypto_box_keypair(self_public_key, self_secret_key); | 661 | crypto_box_keypair(self_public_key, self_secret_key); |
662 | TCP_Server *tcp_s = new_TCP_server(1, NUM_PORTS, ports, self_secret_key, NULL); | 662 | TCP_Server *tcp_s = new_TCP_server(1, NUM_PORTS, ports, self_secret_key, NULL); |
663 | ck_assert_msg(public_key_cmp(tcp_s->public_key, self_public_key) == 0, "Wrong public key"); | 663 | ck_assert_msg(public_key_cmp(tcp_server_public_key(tcp_s), self_public_key) == 0, "Wrong public key"); |
664 | 664 | ||
665 | TCP_Proxy_Info proxy_info; | 665 | TCP_Proxy_Info proxy_info; |
666 | proxy_info.proxy_type = TCP_PROXY_NONE; | 666 | proxy_info.proxy_type = TCP_PROXY_NONE; |
@@ -680,10 +680,11 @@ START_TEST(test_tcp_connection2) | |||
680 | 680 | ||
681 | int connection = new_tcp_connection_to(tc_1, tc_2->self_public_key, 123); | 681 | int connection = new_tcp_connection_to(tc_1, tc_2->self_public_key, 123); |
682 | ck_assert_msg(connection == 0, "Connection id wrong"); | 682 | ck_assert_msg(connection == 0, "Connection id wrong"); |
683 | ck_assert_msg(add_tcp_relay_connection(tc_1, connection, ip_port_tcp_s, tcp_s->public_key) == 0, | 683 | ck_assert_msg(add_tcp_relay_connection(tc_1, connection, ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0, |
684 | "Could not add tcp relay to connection\n"); | 684 | "Could not add tcp relay to connection\n"); |
685 | 685 | ||
686 | ck_assert_msg(add_tcp_relay_global(tc_2, ip_port_tcp_s, tcp_s->public_key) == 0, "Could not add global relay"); | 686 | ck_assert_msg(add_tcp_relay_global(tc_2, ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0, |
687 | "Could not add global relay"); | ||
687 | 688 | ||
688 | c_sleep(50); | 689 | c_sleep(50); |
689 | do_TCP_server(tcp_s); | 690 | do_TCP_server(tcp_s); |
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 59592f05..64f9e92a 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c | |||
@@ -2477,7 +2477,8 @@ void do_messenger(Messenger *m, void *userdata) | |||
2477 | local_ip_port.port = m->options.tcp_server_port; | 2477 | local_ip_port.port = m->options.tcp_server_port; |
2478 | local_ip_port.ip.family = AF_INET; | 2478 | local_ip_port.ip.family = AF_INET; |
2479 | local_ip_port.ip.ip4.uint32 = INADDR_LOOPBACK; | 2479 | local_ip_port.ip.ip4.uint32 = INADDR_LOOPBACK; |
2480 | add_tcp_relay(m->net_crypto, local_ip_port, m->tcp_server->public_key); | 2480 | add_tcp_relay(m->net_crypto, local_ip_port, |
2481 | tcp_server_public_key(m->tcp_server)); | ||
2481 | } | 2482 | } |
2482 | } | 2483 | } |
2483 | 2484 | ||
diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index 58efe51c..7e348fa5 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c | |||
@@ -32,6 +32,42 @@ | |||
32 | #include <sys/ioctl.h> | 32 | #include <sys/ioctl.h> |
33 | #endif | 33 | #endif |
34 | 34 | ||
35 | struct TCP_Server { | ||
36 | Onion *onion; | ||
37 | |||
38 | #ifdef TCP_SERVER_USE_EPOLL | ||
39 | int efd; | ||
40 | uint64_t last_run_pinged; | ||
41 | #endif | ||
42 | sock_t *socks_listening; | ||
43 | unsigned int num_listening_socks; | ||
44 | |||
45 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | ||
46 | uint8_t secret_key[crypto_box_SECRETKEYBYTES]; | ||
47 | TCP_Secure_Connection incomming_connection_queue[MAX_INCOMMING_CONNECTIONS]; | ||
48 | uint16_t incomming_connection_queue_index; | ||
49 | TCP_Secure_Connection unconfirmed_connection_queue[MAX_INCOMMING_CONNECTIONS]; | ||
50 | uint16_t unconfirmed_connection_queue_index; | ||
51 | |||
52 | TCP_Secure_Connection *accepted_connection_array; | ||
53 | uint32_t size_accepted_connections; | ||
54 | uint32_t num_accepted_connections; | ||
55 | |||
56 | uint64_t counter; | ||
57 | |||
58 | BS_LIST accepted_key_list; | ||
59 | }; | ||
60 | |||
61 | const uint8_t *tcp_server_public_key(const TCP_Server *tcp_server) | ||
62 | { | ||
63 | return tcp_server->public_key; | ||
64 | } | ||
65 | |||
66 | size_t tcp_server_listen_count(const TCP_Server *tcp_server) | ||
67 | { | ||
68 | return tcp_server->num_listening_socks; | ||
69 | } | ||
70 | |||
35 | /* return 1 on success | 71 | /* return 1 on success |
36 | * return 0 on failure | 72 | * return 0 on failure |
37 | */ | 73 | */ |
diff --git a/toxcore/TCP_server.h b/toxcore/TCP_server.h index dedb819f..4dcfe126 100644 --- a/toxcore/TCP_server.h +++ b/toxcore/TCP_server.h | |||
@@ -115,31 +115,10 @@ typedef struct TCP_Secure_Connection { | |||
115 | } TCP_Secure_Connection; | 115 | } TCP_Secure_Connection; |
116 | 116 | ||
117 | 117 | ||
118 | typedef struct { | 118 | typedef struct TCP_Server TCP_Server; |
119 | Onion *onion; | ||
120 | 119 | ||
121 | #ifdef TCP_SERVER_USE_EPOLL | 120 | const uint8_t *tcp_server_public_key(const TCP_Server *tcp_server); |
122 | int efd; | 121 | size_t tcp_server_listen_count(const TCP_Server *tcp_server); |
123 | uint64_t last_run_pinged; | ||
124 | #endif | ||
125 | sock_t *socks_listening; | ||
126 | unsigned int num_listening_socks; | ||
127 | |||
128 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | ||
129 | uint8_t secret_key[crypto_box_SECRETKEYBYTES]; | ||
130 | TCP_Secure_Connection incomming_connection_queue[MAX_INCOMMING_CONNECTIONS]; | ||
131 | uint16_t incomming_connection_queue_index; | ||
132 | TCP_Secure_Connection unconfirmed_connection_queue[MAX_INCOMMING_CONNECTIONS]; | ||
133 | uint16_t unconfirmed_connection_queue_index; | ||
134 | |||
135 | TCP_Secure_Connection *accepted_connection_array; | ||
136 | uint32_t size_accepted_connections; | ||
137 | uint32_t num_accepted_connections; | ||
138 | |||
139 | uint64_t counter; | ||
140 | |||
141 | BS_LIST accepted_key_list; | ||
142 | } TCP_Server; | ||
143 | 122 | ||
144 | /* Create new TCP server instance. | 123 | /* Create new TCP server instance. |
145 | */ | 124 | */ |