summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-07-09 20:11:01 -0400
committerirungentoo <irungentoo@gmail.com>2014-07-09 20:11:01 -0400
commit422ff3b77b3023c930161b992866ec54c419dbcc (patch)
tree2713556df07bf3d84607c122bee579bc21324034 /toxcore
parentbd4c142e38afd7c03a23721a3e8261e15aacf973 (diff)
TCP should be a bit more solid.
When a TCP ping request is recieved, try to send the response until success instead of just dropping it if sending the response fails on the first try.
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/TCP_client.c24
-rw-r--r--toxcore/TCP_client.h1
2 files changed, 21 insertions, 4 deletions
diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c
index 087188f7..82720ae8 100644
--- a/toxcore/TCP_client.c
+++ b/toxcore/TCP_client.c
@@ -197,6 +197,8 @@ void routing_status_handler(TCP_Client_Connection *con, int (*status_callback)(v
197 con->status_callback_object = object; 197 con->status_callback_object = object;
198} 198}
199 199
200static int send_ping_response(TCP_Client_Connection *con);
201
200/* return 1 on success. 202/* return 1 on success.
201 * return 0 if could not send packet. 203 * return 0 if could not send packet.
202 * return -1 on failure. 204 * return -1 on failure.
@@ -209,6 +211,9 @@ int send_data(TCP_Client_Connection *con, uint8_t con_id, const uint8_t *data, u
209 if (con->connections[con_id].status != 2) 211 if (con->connections[con_id].status != 2)
210 return -1; 212 return -1;
211 213
214 if (send_ping_response(con) == 0)
215 return 0;
216
212 uint8_t packet[1 + length]; 217 uint8_t packet[1 + length];
213 packet[0] = con_id + NUM_RESERVED_PORTS; 218 packet[0] = con_id + NUM_RESERVED_PORTS;
214 memcpy(packet + 1, data, length); 219 memcpy(packet + 1, data, length);
@@ -293,12 +298,21 @@ static int send_ping_request(TCP_Client_Connection *con, uint64_t ping_id)
293 * return 0 if could not send packet. 298 * return 0 if could not send packet.
294 * return -1 on failure (connection must be killed). 299 * return -1 on failure (connection must be killed).
295 */ 300 */
296static int send_ping_response(TCP_Client_Connection *con, uint64_t ping_id) 301static int send_ping_response(TCP_Client_Connection *con)
297{ 302{
303 if (!con->ping_response_id)
304 return 1;
305
298 uint8_t packet[1 + sizeof(uint64_t)]; 306 uint8_t packet[1 + sizeof(uint64_t)];
299 packet[0] = TCP_PACKET_PONG; 307 packet[0] = TCP_PACKET_PONG;
300 memcpy(packet + 1, &ping_id, sizeof(uint64_t)); 308 memcpy(packet + 1, &con->ping_response_id, sizeof(uint64_t));
301 return write_packet_TCP_secure_connection(con, packet, sizeof(packet)); 309 int ret;
310
311 if ((ret = write_packet_TCP_secure_connection(con, packet, sizeof(packet))) == 1) {
312 con->ping_response_id = 0;
313 }
314
315 return ret;
302} 316}
303 317
304/* return 1 on success. 318/* return 1 on success.
@@ -468,7 +482,8 @@ static int handle_TCP_packet(TCP_Client_Connection *conn, const uint8_t *data, u
468 482
469 uint64_t ping_id; 483 uint64_t ping_id;
470 memcpy(&ping_id, data + 1, sizeof(uint64_t)); 484 memcpy(&ping_id, data + 1, sizeof(uint64_t));
471 send_ping_response(conn, ping_id); 485 conn->ping_response_id = ping_id;
486 send_ping_response(conn);
472 return 0; 487 return 0;
473 } 488 }
474 489
@@ -523,6 +538,7 @@ static int handle_TCP_packet(TCP_Client_Connection *conn, const uint8_t *data, u
523static int do_confirmed_TCP(TCP_Client_Connection *conn) 538static int do_confirmed_TCP(TCP_Client_Connection *conn)
524{ 539{
525 send_pending_data(conn); 540 send_pending_data(conn);
541 send_ping_response(conn);
526 uint8_t packet[MAX_PACKET_SIZE]; 542 uint8_t packet[MAX_PACKET_SIZE];
527 int len; 543 int len;
528 544
diff --git a/toxcore/TCP_client.h b/toxcore/TCP_client.h
index 2622b4f7..e6d232ed 100644
--- a/toxcore/TCP_client.h
+++ b/toxcore/TCP_client.h
@@ -57,6 +57,7 @@ typedef struct {
57 uint64_t last_pinged; 57 uint64_t last_pinged;
58 uint64_t ping_id; 58 uint64_t ping_id;
59 59
60 uint64_t ping_response_id;
60 void *net_crypto_pointer; 61 void *net_crypto_pointer;
61 uint32_t net_crypto_location; 62 uint32_t net_crypto_location;
62 struct { 63 struct {