diff options
Diffstat (limited to 'toxcore/TCP_client.c')
-rw-r--r-- | toxcore/TCP_client.c | 24 |
1 files changed, 20 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 | ||
200 | static 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 | */ |
296 | static int send_ping_response(TCP_Client_Connection *con, uint64_t ping_id) | 301 | static 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 | |||
523 | static int do_confirmed_TCP(TCP_Client_Connection *conn) | 538 | static 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 | ||