summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/TCP_client.c37
-rw-r--r--toxcore/TCP_client.h2
2 files changed, 23 insertions, 16 deletions
diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c
index 82720ae8..bc1b7ff4 100644
--- a/toxcore/TCP_client.c
+++ b/toxcore/TCP_client.c
@@ -198,6 +198,7 @@ void routing_status_handler(TCP_Client_Connection *con, int (*status_callback)(v
198} 198}
199 199
200static int send_ping_response(TCP_Client_Connection *con); 200static int send_ping_response(TCP_Client_Connection *con);
201static int send_ping_request(TCP_Client_Connection *con);
201 202
202/* return 1 on success. 203/* return 1 on success.
203 * return 0 if could not send packet. 204 * return 0 if could not send packet.
@@ -211,7 +212,7 @@ int send_data(TCP_Client_Connection *con, uint8_t con_id, const uint8_t *data, u
211 if (con->connections[con_id].status != 2) 212 if (con->connections[con_id].status != 2)
212 return -1; 213 return -1;
213 214
214 if (send_ping_response(con) == 0) 215 if (send_ping_response(con) == 0 || send_ping_request(con) == 0)
215 return 0; 216 return 0;
216 217
217 uint8_t packet[1 + length]; 218 uint8_t packet[1 + length];
@@ -286,12 +287,21 @@ static int send_disconnect_notification(TCP_Client_Connection *con, uint8_t id)
286 * return 0 if could not send packet. 287 * return 0 if could not send packet.
287 * return -1 on failure (connection must be killed). 288 * return -1 on failure (connection must be killed).
288 */ 289 */
289static int send_ping_request(TCP_Client_Connection *con, uint64_t ping_id) 290static int send_ping_request(TCP_Client_Connection *con)
290{ 291{
292 if (!con->ping_request_id)
293 return 1;
294
291 uint8_t packet[1 + sizeof(uint64_t)]; 295 uint8_t packet[1 + sizeof(uint64_t)];
292 packet[0] = TCP_PACKET_PING; 296 packet[0] = TCP_PACKET_PING;
293 memcpy(packet + 1, &ping_id, sizeof(uint64_t)); 297 memcpy(packet + 1, &con->ping_request_id, sizeof(uint64_t));
294 return write_packet_TCP_secure_connection(con, packet, sizeof(packet)); 298 int ret;
299
300 if ((ret = write_packet_TCP_secure_connection(con, packet, sizeof(packet))) == 1) {
301 con->ping_request_id = 0;
302 }
303
304 return ret;
295} 305}
296 306
297/* return 1 on success. 307/* return 1 on success.
@@ -441,7 +451,7 @@ static int handle_TCP_packet(TCP_Client_Connection *conn, const uint8_t *data, u
441 uint8_t con_id = data[1] - NUM_RESERVED_PORTS; 451 uint8_t con_id = data[1] - NUM_RESERVED_PORTS;
442 452
443 if (conn->connections[con_id].status != 1) 453 if (conn->connections[con_id].status != 1)
444 return -1; 454 return 0;
445 455
446 conn->connections[con_id].status = 2; 456 conn->connections[con_id].status = 2;
447 457
@@ -465,7 +475,7 @@ static int handle_TCP_packet(TCP_Client_Connection *conn, const uint8_t *data, u
465 return 0; 475 return 0;
466 476
467 if (conn->connections[con_id].status != 2) 477 if (conn->connections[con_id].status != 2)
468 return -1; 478 return 0;
469 479
470 conn->connections[con_id].status = 1; 480 conn->connections[con_id].status = 1;
471 481
@@ -539,6 +549,8 @@ static int do_confirmed_TCP(TCP_Client_Connection *conn)
539{ 549{
540 send_pending_data(conn); 550 send_pending_data(conn);
541 send_ping_response(conn); 551 send_ping_response(conn);
552 send_ping_request(conn);
553
542 uint8_t packet[MAX_PACKET_SIZE]; 554 uint8_t packet[MAX_PACKET_SIZE];
543 int len; 555 int len;
544 556
@@ -548,16 +560,9 @@ static int do_confirmed_TCP(TCP_Client_Connection *conn)
548 if (!ping_id) 560 if (!ping_id)
549 ++ping_id; 561 ++ping_id;
550 562
551 int ret = send_ping_request(conn, ping_id); 563 conn->ping_request_id = conn->ping_id = ping_id;
552 564 send_ping_request(conn);
553 if (ret == 1) { 565 conn->last_pinged = unix_time();
554 conn->last_pinged = unix_time();
555 conn->ping_id = ping_id;
556 } else {
557 if (is_timeout(conn->last_pinged, TCP_PING_FREQUENCY + TCP_PING_TIMEOUT)) {
558 conn->status = TCP_CLIENT_DISCONNECTED;
559 }
560 }
561 } 566 }
562 567
563 if (conn->ping_id && is_timeout(conn->last_pinged, TCP_PING_TIMEOUT)) { 568 if (conn->ping_id && is_timeout(conn->last_pinged, TCP_PING_TIMEOUT)) {
diff --git a/toxcore/TCP_client.h b/toxcore/TCP_client.h
index e6d232ed..f57f79e6 100644
--- a/toxcore/TCP_client.h
+++ b/toxcore/TCP_client.h
@@ -58,6 +58,8 @@ typedef struct {
58 uint64_t ping_id; 58 uint64_t ping_id;
59 59
60 uint64_t ping_response_id; 60 uint64_t ping_response_id;
61 uint64_t ping_request_id;
62
61 void *net_crypto_pointer; 63 void *net_crypto_pointer;
62 uint32_t net_crypto_location; 64 uint32_t net_crypto_location;
63 struct { 65 struct {