summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-03-30 20:56:59 -0400
committerirungentoo <irungentoo@gmail.com>2014-03-30 20:56:59 -0400
commit27a7bf5b1e65f715ae2a7f8c76fa8761be389857 (patch)
treee4cf3b9d78cc6acfaa1a9786c29316e864f8f616
parent98cba889a70c62847da9127832e3aece8722e235 (diff)
TCP server now sends/handles ping packets.
-rw-r--r--docs/TCP_Network.txt4
-rw-r--r--toxcore/TCP_server.c48
-rw-r--r--toxcore/TCP_server.h7
3 files changed, 55 insertions, 4 deletions
diff --git a/docs/TCP_Network.txt b/docs/TCP_Network.txt
index ee1bcbde..775ccc88 100644
--- a/docs/TCP_Network.txt
+++ b/docs/TCP_Network.txt
@@ -101,9 +101,9 @@ special ids and packets:
1013 - Disconnect notification: 1013 - 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)]
1034 - ping packet 1034 - ping packet
104[uint8_t id (4)][uint64_t ping_id] 104[uint8_t id (4)][uint64_t ping_id (0 is invalid)]
1055 - ping response (pong) 1055 - ping response (pong)
106[uint8_t id (5)][uint64_t ping_id] 106[uint8_t id (5)][uint64_t ping_id (0 is invalid)]
1078 - onion packet (same format as initial onion packet (See: Prevent 1078 - onion packet (same format as initial onion packet (See: Prevent
108tracking.txt) but packet id is 8 instead of 128) 108tracking.txt) but packet id is 8 instead of 128)
1099 - onion packet response (same format as onion packet with id 142 but id is 9 1099 - onion packet response (same format as onion packet with id 142 but id is 9
diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c
index 727c1336..d44147a6 100644
--- a/toxcore/TCP_server.c
+++ b/toxcore/TCP_server.c
@@ -26,6 +26,8 @@
26#include <sys/ioctl.h> 26#include <sys/ioctl.h>
27#endif 27#endif
28 28
29#include "util.h"
30
29/* return 1 if valid 31/* return 1 if valid
30 * return 0 if not valid 32 * return 0 if not valid
31 */ 33 */
@@ -168,6 +170,8 @@ static int add_accepted(TCP_Server *TCP_server, TCP_Secure_Connection *con)
168 TCP_server->accepted_connection_array[index].status = TCP_STATUS_CONFIRMED; 170 TCP_server->accepted_connection_array[index].status = TCP_STATUS_CONFIRMED;
169 ++TCP_server->num_accepted_connections; 171 ++TCP_server->num_accepted_connections;
170 TCP_server->accepted_connection_array[index].identifier = ++TCP_server->counter; 172 TCP_server->accepted_connection_array[index].identifier = ++TCP_server->counter;
173 TCP_server->accepted_connection_array[index].last_pinged = unix_time();
174 TCP_server->accepted_connection_array[index].ping_id = 0;
171 return index; 175 return index;
172} 176}
173 177
@@ -640,14 +644,29 @@ static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, uint8_t *d
640 if (length != 1 + sizeof(uint64_t)) 644 if (length != 1 + sizeof(uint64_t))
641 return -1; 645 return -1;
642 646
643 break; 647 uint8_t response[1 + sizeof(uint64_t)];
648 response[0] = TCP_PACKET_PONG;
649 memcpy(response + 1, data + 1, sizeof(uint64_t));
650 write_packet_TCP_secure_connection(con, response, sizeof(response));
651 return 0;
644 } 652 }
645 653
646 case TCP_PACKET_PONG: { 654 case TCP_PACKET_PONG: {
647 if (length != 1 + sizeof(uint64_t)) 655 if (length != 1 + sizeof(uint64_t))
648 return -1; 656 return -1;
649 657
650 break; 658 uint64_t ping_id;
659 memcpy(&ping_id, data + 1, sizeof(uint64_t));
660
661 if (ping_id) {
662 if (ping_id == con->ping_id) {
663 con->ping_id = 0;
664 }
665
666 return 0;
667 } else {
668 return -1;
669 }
651 } 670 }
652 671
653 case TCP_PACKET_ONION_REQUEST: { 672 case TCP_PACKET_ONION_REQUEST: {
@@ -898,6 +917,29 @@ static void do_TCP_confirmed(TCP_Server *TCP_server)
898 if (conn->status != TCP_STATUS_CONFIRMED) 917 if (conn->status != TCP_STATUS_CONFIRMED)
899 continue; 918 continue;
900 919
920 if (is_timeout(conn->last_pinged, TCP_PING_FREQUENCY)) {
921 uint8_t ping[1 + sizeof(uint64_t)];
922 ping[0] = TCP_PACKET_PING;
923 uint64_t ping_id = random_64b();
924
925 if (!ping_id)
926 ++ping_id;
927
928 memcpy(ping + 1, &ping_id, sizeof(uint64_t));
929 int ret = write_packet_TCP_secure_connection(conn, ping, sizeof(ping));
930
931 if (ret == 1) {
932 conn->last_pinged = unix_time();
933 conn->ping_id = ping_id;
934 }
935 }
936
937 if (conn->ping_id && is_timeout(conn->last_pinged, TCP_PING_TIMEOUT)) {
938 kill_TCP_connection(conn);
939 del_accepted(TCP_server, i);
940 continue;
941 }
942
901 send_pending_data(conn); 943 send_pending_data(conn);
902 uint8_t packet[MAX_PACKET_SIZE]; 944 uint8_t packet[MAX_PACKET_SIZE];
903 int len; 945 int len;
@@ -920,6 +962,8 @@ static void do_TCP_confirmed(TCP_Server *TCP_server)
920 962
921void do_TCP_server(TCP_Server *TCP_server) 963void do_TCP_server(TCP_Server *TCP_server)
922{ 964{
965 unix_time_update();
966
923 do_TCP_accept_new(TCP_server); 967 do_TCP_accept_new(TCP_server);
924 do_TCP_incomming(TCP_server); 968 do_TCP_incomming(TCP_server);
925 do_TCP_unconfirmed(TCP_server); 969 do_TCP_unconfirmed(TCP_server);
diff --git a/toxcore/TCP_server.h b/toxcore/TCP_server.h
index 7461fe5d..8cd18438 100644
--- a/toxcore/TCP_server.h
+++ b/toxcore/TCP_server.h
@@ -49,6 +49,10 @@
49 49
50#define TCP_ONION_FAMILY (AF_INET6 + 1) 50#define TCP_ONION_FAMILY (AF_INET6 + 1)
51 51
52/* frequency to ping connected nodes and timeout in seconds */
53#define TCP_PING_FREQUENCY 30
54#define TCP_PING_TIMEOUT 20
55
52enum { 56enum {
53 TCP_STATUS_NO_STATUS, 57 TCP_STATUS_NO_STATUS,
54 TCP_STATUS_CONNECTED, 58 TCP_STATUS_CONNECTED,
@@ -75,6 +79,9 @@ typedef struct TCP_Secure_Connection {
75 uint16_t last_packet_sent; 79 uint16_t last_packet_sent;
76 80
77 uint64_t identifier; 81 uint64_t identifier;
82
83 uint64_t last_pinged;
84 uint64_t ping_id;
78} TCP_Secure_Connection; 85} TCP_Secure_Connection;
79 86
80 87