From 8aaa5fe996e44a0c68c069b8e70649264f5e8a56 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Fri, 28 Mar 2014 22:37:42 -0400 Subject: TCP server almost ready. --- auto_tests/TCP_test.c | 40 +++++++++++++++++++++++++++++++++++++--- docs/TCP_Network.txt | 11 +++++++++++ toxcore/TCP_server.c | 38 ++++++++++++++++++++++++++++---------- toxcore/TCP_server.h | 2 ++ toxcore/onion.c | 7 ++++++- 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) ck_assert_msg(data[1] == 16, "connection not refused %u", data[1]); ck_assert_msg(memcmp(data + 2, con1->public_key, crypto_box_PUBLICKEYBYTES) == 0, "key in packet wrong"); - uint8_t test_packet[1024] = {16}; + uint8_t test_packet[512] = {16, 17, 16, 86, 99, 127, 255, 189, 78}; 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)); + c_sleep(50); do_TCP_server(tcp_s); c_sleep(50); len = read_packet_sec_TCP(con1, data, 2 + 2 + crypto_box_MACBYTES); ck_assert_msg(len == 2, "wrong len %u", len); ck_assert_msg(data[0] == 2, "wrong packet id %u", data[0]); ck_assert_msg(data[1] == 16, "wrong peer id %u", data[1]); + len = read_packet_sec_TCP(con3, data, 2 + 2 + crypto_box_MACBYTES); + ck_assert_msg(len == 2, "wrong len %u", len); + ck_assert_msg(data[0] == 2, "wrong packet id %u", data[0]); + ck_assert_msg(data[1] == 16, "wrong peer id %u", data[1]); len = read_packet_sec_TCP(con1, data, 2 + sizeof(test_packet) + crypto_box_MACBYTES); - ck_assert_msg(len == 1024, "wrong len %u", len); - ck_assert_msg(memcmp(data, test_packet, sizeof(test_packet)) == 0, "packet is wrong"); + ck_assert_msg(len == sizeof(test_packet), "wrong len %u", 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]); + len = read_packet_sec_TCP(con1, data, 2 + sizeof(test_packet) + crypto_box_MACBYTES); + ck_assert_msg(len == sizeof(test_packet), "wrong len %u", 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]); + len = read_packet_sec_TCP(con1, data, 2 + sizeof(test_packet) + crypto_box_MACBYTES); + ck_assert_msg(len == sizeof(test_packet), "wrong len %u", 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)); + c_sleep(50); + do_TCP_server(tcp_s); + c_sleep(50); + len = read_packet_sec_TCP(con3, data, 2 + sizeof(test_packet) + crypto_box_MACBYTES); + ck_assert_msg(len == sizeof(test_packet), "wrong len %u", 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]); + len = read_packet_sec_TCP(con3, data, 2 + sizeof(test_packet) + crypto_box_MACBYTES); + ck_assert_msg(len == sizeof(test_packet), "wrong len %u", 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]); + len = read_packet_sec_TCP(con3, data, 2 + sizeof(test_packet) + crypto_box_MACBYTES); + ck_assert_msg(len == sizeof(test_packet), "wrong len %u", 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]); } END_TEST 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: [uint8_t id (2)][uint8_t (packet id of connection that got connected)] 3 - Disconnect notification: [uint8_t id (3)][uint8_t (packet id of connection that got disconnected)] +4 - ping packet +[uint8_t id (4)][uint64_t ping_id] +5 - ping response (pong) +[uint8_t id (5)][uint64_t ping_id] 8 - onion packet (same format as initial onion packet (See: Prevent tracking.txt) but packet id is 8 instead of 128) 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 was one received normally via UDP, he must also assure himself that any responses must be sent to the proper client. +Ping responses must have the same ping_id as the request. + +If the server recieves a ping packet he must respond with a ping response. + +The server will send a ping packet to clients every 30 seconds, they have 30 +seconds to respond, if they don't the connection is deleted. + Client: 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 return disconnect_conection_index(TCP_server, con, data[1] - NUM_RESERVED_PORTS); } - case TCP_PACKET_ONION_REQUEST: { + case TCP_PACKET_PING: { + if (length != 1 + sizeof(uint64_t)) + return -1; + + break; + } + + case TCP_PACKET_PONG: { + if (length != 1 + sizeof(uint64_t)) + return -1; break; } + case TCP_PACKET_ONION_REQUEST: { + //if (length <= 1 + crypto_box_NONCEBYTES + ONION_SEND_BASE*2) + // return -1; + + //TODO onion_send_1(Onion *onion, data + 1 + crypto_box_NONCEBYTES, length - (1 + crypto_box_NONCEBYTES), IP_Port source, data + 1); + return 0; + } + case TCP_PACKET_ONION_RESPONSE: { break; @@ -638,7 +655,7 @@ static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, uint8_t *d return 0; uint32_t index = con->connections[con_id].index; - uint8_t other_con_id = con->connections[con_id].other_id; + uint8_t other_con_id = con->connections[con_id].other_id + NUM_RESERVED_PORTS; uint8_t new_data[length]; memcpy(new_data, data, length); new_data[0] = other_con_id; @@ -847,18 +864,19 @@ static void do_TCP_confirmed(TCP_Server *TCP_server) send_pending_data(conn); uint8_t packet[MAX_PACKET_SIZE]; - int len = read_packet_TCP_secure_connection(conn, packet, sizeof(packet)); + int len; + + while ((len = read_packet_TCP_secure_connection(conn, packet, sizeof(packet)))) { + if (len == -1) { + kill_TCP_connection(conn); + del_accepted(TCP_server, i); + break; + } - if (len == 0) { - continue; - } else if (len == -1) { - kill_TCP_connection(conn); - del_accepted(TCP_server, i); - continue; - } else { if (handle_TCP_packet(TCP_server, i, packet, len) == -1) { kill_TCP_connection(conn); del_accepted(TCP_server, i); + break; } } } 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 @@ #define TCP_PACKET_ROUTING_RESPONSE 1 #define TCP_PACKET_CONNECTION_NOTIFICATION 2 #define TCP_PACKET_DISCONNECT_NOTIFICATION 3 +#define TCP_PACKET_PING 4 +#define TCP_PACKET_PONG 5 #define TCP_PACKET_ONION_REQUEST 8 #define TCP_PACKET_ONION_RESPONSE 9 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 if ((uint32_t)len != length - (1 + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + crypto_box_MACBYTES)) return 1; + return onion_send_1(onion, plain, len, source, packet + 1); +} + +int onion_send_1(Onion *onion, uint8_t *plain, uint32_t len, IP_Port source, uint8_t *nonce) +{ IP_Port send_to; memcpy(&send_to, plain, sizeof(IP_Port)); to_host_family(&send_to.ip); uint8_t data[MAX_ONION_SIZE]; data[0] = NET_PACKET_ONION_SEND_1; - memcpy(data + 1, packet + 1, crypto_box_NONCEBYTES); + memcpy(data + 1, nonce, crypto_box_NONCEBYTES); memcpy(data + 1 + crypto_box_NONCEBYTES, plain + sizeof(IP_Port), len - sizeof(IP_Port)); uint32_t data_len = 1 + crypto_box_NONCEBYTES + (len - sizeof(IP_Port)); 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 */ int send_onion_response(Networking_Core *net, IP_Port dest, uint8_t *data, uint32_t length, uint8_t *ret); +/* Function to handle/send received decrypted versions of the packet sent with send_onion_packet. + * + * return 0 on success. + * return 1 on failure. + * + * Used to handle these packets that are received in a non traditional way (by TCP for example). + */ +int onion_send_1(Onion *onion, uint8_t *plain, uint32_t len, IP_Port source, uint8_t *nonce); + Onion *new_onion(DHT *dht); void kill_onion(Onion *onion); -- cgit v1.2.3