diff options
author | irungentoo <irungentoo@gmail.com> | 2014-03-28 22:37:42 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2014-03-28 22:37:42 -0400 |
commit | 8aaa5fe996e44a0c68c069b8e70649264f5e8a56 (patch) | |
tree | 09a7a2c680425d772bdf2d2b6a0499a1ead3a00d | |
parent | a11667355491b6a0b18111e0c6ef16f816521214 (diff) |
TCP server almost ready.
-rw-r--r-- | auto_tests/TCP_test.c | 40 | ||||
-rw-r--r-- | docs/TCP_Network.txt | 11 | ||||
-rw-r--r-- | toxcore/TCP_server.c | 38 | ||||
-rw-r--r-- | toxcore/TCP_server.h | 2 | ||||
-rw-r--r-- | toxcore/onion.c | 7 | ||||
-rw-r--r-- | toxcore/onion.h | 9 |
6 files changed, 93 insertions, 14 deletions
diff --git a/auto_tests/TCP_test.c b/auto_tests/TCP_test.c index 989867b4..6e46492c 100644 --- a/auto_tests/TCP_test.c +++ b/auto_tests/TCP_test.c | |||
@@ -229,17 +229,51 @@ START_TEST(test_some) | |||
229 | ck_assert_msg(data[1] == 16, "connection not refused %u", data[1]); | 229 | ck_assert_msg(data[1] == 16, "connection not refused %u", data[1]); |
230 | ck_assert_msg(memcmp(data + 2, con1->public_key, crypto_box_PUBLICKEYBYTES) == 0, "key in packet wrong"); | 230 | ck_assert_msg(memcmp(data + 2, con1->public_key, crypto_box_PUBLICKEYBYTES) == 0, "key in packet wrong"); |
231 | 231 | ||
232 | uint8_t test_packet[1024] = {16}; | 232 | uint8_t test_packet[512] = {16, 17, 16, 86, 99, 127, 255, 189, 78}; |
233 | write_packet_TCP_secure_connection(con3, test_packet, sizeof(test_packet)); | 233 | write_packet_TCP_secure_connection(con3, test_packet, sizeof(test_packet)); |
234 | write_packet_TCP_secure_connection(con3, test_packet, sizeof(test_packet)); | ||
235 | write_packet_TCP_secure_connection(con3, test_packet, sizeof(test_packet)); | ||
236 | c_sleep(50); | ||
234 | do_TCP_server(tcp_s); | 237 | do_TCP_server(tcp_s); |
235 | c_sleep(50); | 238 | c_sleep(50); |
236 | len = read_packet_sec_TCP(con1, data, 2 + 2 + crypto_box_MACBYTES); | 239 | len = read_packet_sec_TCP(con1, data, 2 + 2 + crypto_box_MACBYTES); |
237 | ck_assert_msg(len == 2, "wrong len %u", len); | 240 | ck_assert_msg(len == 2, "wrong len %u", len); |
238 | ck_assert_msg(data[0] == 2, "wrong packet id %u", data[0]); | 241 | ck_assert_msg(data[0] == 2, "wrong packet id %u", data[0]); |
239 | ck_assert_msg(data[1] == 16, "wrong peer id %u", data[1]); | 242 | ck_assert_msg(data[1] == 16, "wrong peer id %u", data[1]); |
243 | len = read_packet_sec_TCP(con3, data, 2 + 2 + crypto_box_MACBYTES); | ||
244 | ck_assert_msg(len == 2, "wrong len %u", len); | ||
245 | ck_assert_msg(data[0] == 2, "wrong packet id %u", data[0]); | ||
246 | ck_assert_msg(data[1] == 16, "wrong peer id %u", data[1]); | ||
240 | len = read_packet_sec_TCP(con1, data, 2 + sizeof(test_packet) + crypto_box_MACBYTES); | 247 | len = read_packet_sec_TCP(con1, data, 2 + sizeof(test_packet) + crypto_box_MACBYTES); |
241 | ck_assert_msg(len == 1024, "wrong len %u", len); | 248 | ck_assert_msg(len == sizeof(test_packet), "wrong len %u", len); |
242 | ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong"); | 249 | ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1], |
250 | data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]); | ||
251 | len = read_packet_sec_TCP(con1, data, 2 + sizeof(test_packet) + crypto_box_MACBYTES); | ||
252 | ck_assert_msg(len == sizeof(test_packet), "wrong len %u", len); | ||
253 | ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1], | ||
254 | data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]); | ||
255 | len = read_packet_sec_TCP(con1, data, 2 + sizeof(test_packet) + crypto_box_MACBYTES); | ||
256 | ck_assert_msg(len == sizeof(test_packet), "wrong len %u", len); | ||
257 | ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1], | ||
258 | data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]); | ||
259 | write_packet_TCP_secure_connection(con1, test_packet, sizeof(test_packet)); | ||
260 | write_packet_TCP_secure_connection(con1, test_packet, sizeof(test_packet)); | ||
261 | write_packet_TCP_secure_connection(con1, test_packet, sizeof(test_packet)); | ||
262 | c_sleep(50); | ||
263 | do_TCP_server(tcp_s); | ||
264 | c_sleep(50); | ||
265 | len = read_packet_sec_TCP(con3, data, 2 + sizeof(test_packet) + crypto_box_MACBYTES); | ||
266 | ck_assert_msg(len == sizeof(test_packet), "wrong len %u", len); | ||
267 | ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1], | ||
268 | data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]); | ||
269 | len = read_packet_sec_TCP(con3, data, 2 + sizeof(test_packet) + crypto_box_MACBYTES); | ||
270 | ck_assert_msg(len == sizeof(test_packet), "wrong len %u", len); | ||
271 | ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1], | ||
272 | data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]); | ||
273 | len = read_packet_sec_TCP(con3, data, 2 + sizeof(test_packet) + crypto_box_MACBYTES); | ||
274 | ck_assert_msg(len == sizeof(test_packet), "wrong len %u", len); | ||
275 | ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong %u %u %u %u", data[0], data[1], | ||
276 | data[sizeof(test_packet) - 2], data[sizeof(test_packet) - 1]); | ||
243 | } | 277 | } |
244 | END_TEST | 278 | END_TEST |
245 | 279 | ||
diff --git a/docs/TCP_Network.txt b/docs/TCP_Network.txt index 80cdfbdb..ee1bcbde 100644 --- a/docs/TCP_Network.txt +++ b/docs/TCP_Network.txt | |||
@@ -100,6 +100,10 @@ special ids and packets: | |||
100 | [uint8_t id (2)][uint8_t (packet id of connection that got connected)] | 100 | [uint8_t id (2)][uint8_t (packet id of connection that got connected)] |
101 | 3 - Disconnect notification: | 101 | 3 - Disconnect notification: |
102 | [uint8_t id (3)][uint8_t (packet id of connection that got disconnected)] | 102 | [uint8_t id (3)][uint8_t (packet id of connection that got disconnected)] |
103 | 4 - ping packet | ||
104 | [uint8_t id (4)][uint64_t ping_id] | ||
105 | 5 - ping response (pong) | ||
106 | [uint8_t id (5)][uint64_t ping_id] | ||
103 | 8 - onion packet (same format as initial onion packet (See: Prevent | 107 | 8 - onion packet (same format as initial onion packet (See: Prevent |
104 | tracking.txt) but packet id is 8 instead of 128) | 108 | tracking.txt) but packet id is 8 instead of 128) |
105 | 9 - onion packet response (same format as onion packet with id 142 but id is 9 | 109 | 9 - onion packet response (same format as onion packet with id 142 but id is 9 |
@@ -129,6 +133,13 @@ If the server receives an onion packet he handles it the same as he would if it | |||
129 | was one received normally via UDP, he must also assure himself that any | 133 | was one received normally via UDP, he must also assure himself that any |
130 | responses must be sent to the proper client. | 134 | responses must be sent to the proper client. |
131 | 135 | ||
136 | Ping responses must have the same ping_id as the request. | ||
137 | |||
138 | If the server recieves a ping packet he must respond with a ping response. | ||
139 | |||
140 | The server will send a ping packet to clients every 30 seconds, they have 30 | ||
141 | seconds to respond, if they don't the connection is deleted. | ||
142 | |||
132 | Client: | 143 | Client: |
133 | 144 | ||
134 | Implementation details coming soon. | 145 | Implementation details coming soon. |
diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index 620ff51b..70e6ede6 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c | |||
@@ -612,11 +612,28 @@ static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, uint8_t *d | |||
612 | return disconnect_conection_index(TCP_server, con, data[1] - NUM_RESERVED_PORTS); | 612 | return disconnect_conection_index(TCP_server, con, data[1] - NUM_RESERVED_PORTS); |
613 | } | 613 | } |
614 | 614 | ||
615 | case TCP_PACKET_ONION_REQUEST: { | 615 | case TCP_PACKET_PING: { |
616 | if (length != 1 + sizeof(uint64_t)) | ||
617 | return -1; | ||
618 | |||
619 | break; | ||
620 | } | ||
621 | |||
622 | case TCP_PACKET_PONG: { | ||
623 | if (length != 1 + sizeof(uint64_t)) | ||
624 | return -1; | ||
616 | 625 | ||
617 | break; | 626 | break; |
618 | } | 627 | } |
619 | 628 | ||
629 | case TCP_PACKET_ONION_REQUEST: { | ||
630 | //if (length <= 1 + crypto_box_NONCEBYTES + ONION_SEND_BASE*2) | ||
631 | // return -1; | ||
632 | |||
633 | //TODO onion_send_1(Onion *onion, data + 1 + crypto_box_NONCEBYTES, length - (1 + crypto_box_NONCEBYTES), IP_Port source, data + 1); | ||
634 | return 0; | ||
635 | } | ||
636 | |||
620 | case TCP_PACKET_ONION_RESPONSE: { | 637 | case TCP_PACKET_ONION_RESPONSE: { |
621 | 638 | ||
622 | break; | 639 | break; |
@@ -638,7 +655,7 @@ static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, uint8_t *d | |||
638 | return 0; | 655 | return 0; |
639 | 656 | ||
640 | uint32_t index = con->connections[con_id].index; | 657 | uint32_t index = con->connections[con_id].index; |
641 | uint8_t other_con_id = con->connections[con_id].other_id; | 658 | uint8_t other_con_id = con->connections[con_id].other_id + NUM_RESERVED_PORTS; |
642 | uint8_t new_data[length]; | 659 | uint8_t new_data[length]; |
643 | memcpy(new_data, data, length); | 660 | memcpy(new_data, data, length); |
644 | new_data[0] = other_con_id; | 661 | new_data[0] = other_con_id; |
@@ -847,18 +864,19 @@ static void do_TCP_confirmed(TCP_Server *TCP_server) | |||
847 | 864 | ||
848 | send_pending_data(conn); | 865 | send_pending_data(conn); |
849 | uint8_t packet[MAX_PACKET_SIZE]; | 866 | uint8_t packet[MAX_PACKET_SIZE]; |
850 | int len = read_packet_TCP_secure_connection(conn, packet, sizeof(packet)); | 867 | int len; |
868 | |||
869 | while ((len = read_packet_TCP_secure_connection(conn, packet, sizeof(packet)))) { | ||
870 | if (len == -1) { | ||
871 | kill_TCP_connection(conn); | ||
872 | del_accepted(TCP_server, i); | ||
873 | break; | ||
874 | } | ||
851 | 875 | ||
852 | if (len == 0) { | ||
853 | continue; | ||
854 | } else if (len == -1) { | ||
855 | kill_TCP_connection(conn); | ||
856 | del_accepted(TCP_server, i); | ||
857 | continue; | ||
858 | } else { | ||
859 | if (handle_TCP_packet(TCP_server, i, packet, len) == -1) { | 876 | if (handle_TCP_packet(TCP_server, i, packet, len) == -1) { |
860 | kill_TCP_connection(conn); | 877 | kill_TCP_connection(conn); |
861 | del_accepted(TCP_server, i); | 878 | del_accepted(TCP_server, i); |
879 | break; | ||
862 | } | 880 | } |
863 | } | 881 | } |
864 | } | 882 | } |
diff --git a/toxcore/TCP_server.h b/toxcore/TCP_server.h index 9bbfbe17..4814d407 100644 --- a/toxcore/TCP_server.h +++ b/toxcore/TCP_server.h | |||
@@ -39,6 +39,8 @@ | |||
39 | #define TCP_PACKET_ROUTING_RESPONSE 1 | 39 | #define TCP_PACKET_ROUTING_RESPONSE 1 |
40 | #define TCP_PACKET_CONNECTION_NOTIFICATION 2 | 40 | #define TCP_PACKET_CONNECTION_NOTIFICATION 2 |
41 | #define TCP_PACKET_DISCONNECT_NOTIFICATION 3 | 41 | #define TCP_PACKET_DISCONNECT_NOTIFICATION 3 |
42 | #define TCP_PACKET_PING 4 | ||
43 | #define TCP_PACKET_PONG 5 | ||
42 | #define TCP_PACKET_ONION_REQUEST 8 | 44 | #define TCP_PACKET_ONION_REQUEST 8 |
43 | #define TCP_PACKET_ONION_RESPONSE 9 | 45 | #define TCP_PACKET_ONION_RESPONSE 9 |
44 | 46 | ||
diff --git a/toxcore/onion.c b/toxcore/onion.c index 292f9309..6bc6ab07 100644 --- a/toxcore/onion.c +++ b/toxcore/onion.c | |||
@@ -181,13 +181,18 @@ static int handle_send_initial(void *object, IP_Port source, uint8_t *packet, ui | |||
181 | if ((uint32_t)len != length - (1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + crypto_box_MACBYTES)) | 181 | if ((uint32_t)len != length - (1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + crypto_box_MACBYTES)) |
182 | return 1; | 182 | return 1; |
183 | 183 | ||
184 | return onion_send_1(onion, plain, len, source, packet + 1); | ||
185 | } | ||
186 | |||
187 | int onion_send_1(Onion *onion, uint8_t *plain, uint32_t len, IP_Port source, uint8_t *nonce) | ||
188 | { | ||
184 | IP_Port send_to; | 189 | IP_Port send_to; |
185 | memcpy(&send_to, plain, sizeof(IP_Port)); | 190 | memcpy(&send_to, plain, sizeof(IP_Port)); |
186 | to_host_family(&send_to.ip); | 191 | to_host_family(&send_to.ip); |
187 | 192 | ||
188 | uint8_t data[MAX_ONION_SIZE]; | 193 | uint8_t data[MAX_ONION_SIZE]; |
189 | data[0] = NET_PACKET_ONION_SEND_1; | 194 | data[0] = NET_PACKET_ONION_SEND_1; |
190 | memcpy(data + 1, packet + 1, crypto_box_NONCEBYTES); | 195 | memcpy(data + 1, nonce, crypto_box_NONCEBYTES); |
191 | memcpy(data + 1 + crypto_box_NONCEBYTES, plain + sizeof(IP_Port), len - sizeof(IP_Port)); | 196 | memcpy(data + 1 + crypto_box_NONCEBYTES, plain + sizeof(IP_Port), len - sizeof(IP_Port)); |
192 | uint32_t data_len = 1 + crypto_box_NONCEBYTES + (len - sizeof(IP_Port)); | 197 | uint32_t data_len = 1 + crypto_box_NONCEBYTES + (len - sizeof(IP_Port)); |
193 | uint8_t *ret_part = data + data_len; | 198 | uint8_t *ret_part = data + data_len; |
diff --git a/toxcore/onion.h b/toxcore/onion.h index daaec91c..9577f019 100644 --- a/toxcore/onion.h +++ b/toxcore/onion.h | |||
@@ -86,6 +86,15 @@ int send_onion_packet(Networking_Core *net, Onion_Path *path, IP_Port dest, uint | |||
86 | */ | 86 | */ |
87 | int send_onion_response(Networking_Core *net, IP_Port dest, uint8_t *data, uint32_t length, uint8_t *ret); | 87 | int send_onion_response(Networking_Core *net, IP_Port dest, uint8_t *data, uint32_t length, uint8_t *ret); |
88 | 88 | ||
89 | /* Function to handle/send received decrypted versions of the packet sent with send_onion_packet. | ||
90 | * | ||
91 | * return 0 on success. | ||
92 | * return 1 on failure. | ||
93 | * | ||
94 | * Used to handle these packets that are received in a non traditional way (by TCP for example). | ||
95 | */ | ||
96 | int onion_send_1(Onion *onion, uint8_t *plain, uint32_t len, IP_Port source, uint8_t *nonce); | ||
97 | |||
89 | Onion *new_onion(DHT *dht); | 98 | Onion *new_onion(DHT *dht); |
90 | 99 | ||
91 | void kill_onion(Onion *onion); | 100 | void kill_onion(Onion *onion); |