diff options
Diffstat (limited to 'toxcore/TCP_server.c')
-rw-r--r-- | toxcore/TCP_server.c | 125 |
1 files changed, 106 insertions, 19 deletions
diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index 7487ad4f..47e2ff27 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c | |||
@@ -288,7 +288,7 @@ int read_packet_TCP_secure_connection(sock_t sock, uint16_t *next_packet_length, | |||
288 | /* return 0 if pending data was sent completely | 288 | /* return 0 if pending data was sent completely |
289 | * return -1 if it wasn't | 289 | * return -1 if it wasn't |
290 | */ | 290 | */ |
291 | static int send_pending_data(TCP_Secure_Connection *con) | 291 | static int send_pending_data_nonpriority(TCP_Secure_Connection *con) |
292 | { | 292 | { |
293 | if (con->last_packet_length == 0) { | 293 | if (con->last_packet_length == 0) { |
294 | return 0; | 294 | return 0; |
@@ -306,25 +306,96 @@ static int send_pending_data(TCP_Secure_Connection *con) | |||
306 | return 0; | 306 | return 0; |
307 | } | 307 | } |
308 | 308 | ||
309 | if (len > left) | 309 | con->last_packet_sent += len; |
310 | return -1; | ||
311 | |||
312 | } | ||
313 | |||
314 | /* return 0 if pending data was sent completely | ||
315 | * return -1 if it wasn't | ||
316 | */ | ||
317 | static int send_pending_data(TCP_Secure_Connection *con) | ||
318 | { | ||
319 | /* finish sending current non-priority packet */ | ||
320 | if (send_pending_data_nonpriority(con) == -1) { | ||
310 | return -1; | 321 | return -1; |
322 | } | ||
323 | |||
324 | TCP_Priority_List *p = con->priority_queue_start; | ||
325 | |||
326 | while (p) { | ||
327 | uint16_t left = p->size - p->sent; | ||
328 | int len = send(con->sock, p->data + p->sent, left, MSG_NOSIGNAL); | ||
329 | |||
330 | if (len != left) { | ||
331 | if (len > 0) { | ||
332 | p->sent += len; | ||
333 | } | ||
334 | |||
335 | break; | ||
336 | } | ||
337 | |||
338 | TCP_Priority_List *pp = p; | ||
339 | p = p->next; | ||
340 | free(pp); | ||
341 | } | ||
342 | |||
343 | con->priority_queue_start = p; | ||
344 | |||
345 | if (!p) { | ||
346 | con->priority_queue_end = NULL; | ||
347 | return 0; | ||
348 | } | ||
311 | 349 | ||
312 | con->last_packet_sent += len; | ||
313 | return -1; | 350 | return -1; |
351 | } | ||
352 | |||
353 | /* return 0 on failure (only if malloc fails) | ||
354 | * return 1 on success | ||
355 | */ | ||
356 | static _Bool add_priority(TCP_Secure_Connection *con, const uint8_t *packet, uint16_t size, uint16_t sent) | ||
357 | { | ||
358 | TCP_Priority_List *p = con->priority_queue_end, *new; | ||
359 | new = malloc(sizeof(TCP_Priority_List) + size); | ||
314 | 360 | ||
361 | if (!new) { | ||
362 | return 0; | ||
363 | } | ||
364 | |||
365 | new->next = NULL; | ||
366 | new->size = size; | ||
367 | new->sent = sent; | ||
368 | memcpy(new->data, packet, size); | ||
369 | |||
370 | if (p) { | ||
371 | p->next = new; | ||
372 | } else { | ||
373 | con->priority_queue_start = new; | ||
374 | } | ||
375 | |||
376 | con->priority_queue_end = new; | ||
377 | return 1; | ||
315 | } | 378 | } |
316 | 379 | ||
317 | /* return 1 on success. | 380 | /* return 1 on success. |
318 | * return 0 if could not send packet. | 381 | * return 0 if could not send packet. |
319 | * return -1 on failure (connection must be killed). | 382 | * return -1 on failure (connection must be killed). |
320 | */ | 383 | */ |
321 | static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const uint8_t *data, uint16_t length) | 384 | static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const uint8_t *data, uint16_t length, |
385 | _Bool priority) | ||
322 | { | 386 | { |
323 | if (length + crypto_box_MACBYTES > MAX_PACKET_SIZE) | 387 | if (length + crypto_box_MACBYTES > MAX_PACKET_SIZE) |
324 | return -1; | 388 | return -1; |
325 | 389 | ||
326 | if (send_pending_data(con) == -1) | 390 | _Bool sendpriority = 1; |
327 | return 0; | 391 | |
392 | if (send_pending_data(con) == -1) { | ||
393 | if (priority) { | ||
394 | sendpriority = 0; | ||
395 | } else { | ||
396 | return 0; | ||
397 | } | ||
398 | } | ||
328 | 399 | ||
329 | uint8_t packet[sizeof(uint16_t) + length + crypto_box_MACBYTES]; | 400 | uint8_t packet[sizeof(uint16_t) + length + crypto_box_MACBYTES]; |
330 | 401 | ||
@@ -335,17 +406,33 @@ static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const | |||
335 | if ((unsigned int)len != (sizeof(packet) - sizeof(uint16_t))) | 406 | if ((unsigned int)len != (sizeof(packet) - sizeof(uint16_t))) |
336 | return -1; | 407 | return -1; |
337 | 408 | ||
338 | increment_nonce(con->sent_nonce); | 409 | if (priority) { |
410 | len = sendpriority ? send(con->sock, packet, sizeof(packet), MSG_NOSIGNAL) : 0; | ||
339 | 411 | ||
340 | len = send(con->sock, packet, sizeof(packet), MSG_NOSIGNAL); | 412 | if (len <= 0) { |
413 | len = 0; | ||
414 | } | ||
341 | 415 | ||
342 | if ((unsigned int)len == sizeof(packet)) | 416 | increment_nonce(con->sent_nonce); |
343 | return 1; | 417 | |
418 | if (len == sizeof(packet)) { | ||
419 | return 1; | ||
420 | } | ||
421 | |||
422 | return add_priority(con, packet, sizeof(packet), len); | ||
423 | } | ||
424 | |||
425 | len = send(con->sock, packet, sizeof(packet), MSG_NOSIGNAL); | ||
344 | 426 | ||
345 | if (len <= 0) | 427 | if (len <= 0) |
346 | return 0; | 428 | return 0; |
347 | 429 | ||
348 | memcpy(con->last_packet, packet, length); | 430 | increment_nonce(con->sent_nonce); |
431 | |||
432 | if ((unsigned int)len == sizeof(packet)) | ||
433 | return 1; | ||
434 | |||
435 | memcpy(con->last_packet, packet, sizeof(packet)); | ||
349 | con->last_packet_length = sizeof(packet); | 436 | con->last_packet_length = sizeof(packet); |
350 | con->last_packet_sent = len; | 437 | con->last_packet_sent = len; |
351 | return 1; | 438 | return 1; |
@@ -459,7 +546,7 @@ static int send_routing_response(TCP_Secure_Connection *con, uint8_t rpid, const | |||
459 | data[1] = rpid; | 546 | data[1] = rpid; |
460 | memcpy(data + 2, public_key, crypto_box_PUBLICKEYBYTES); | 547 | memcpy(data + 2, public_key, crypto_box_PUBLICKEYBYTES); |
461 | 548 | ||
462 | return write_packet_TCP_secure_connection(con, data, sizeof(data)); | 549 | return write_packet_TCP_secure_connection(con, data, sizeof(data), 1); |
463 | } | 550 | } |
464 | 551 | ||
465 | /* return 1 on success. | 552 | /* return 1 on success. |
@@ -469,7 +556,7 @@ static int send_routing_response(TCP_Secure_Connection *con, uint8_t rpid, const | |||
469 | static int send_connect_notification(TCP_Secure_Connection *con, uint8_t id) | 556 | static int send_connect_notification(TCP_Secure_Connection *con, uint8_t id) |
470 | { | 557 | { |
471 | uint8_t data[2] = {TCP_PACKET_CONNECTION_NOTIFICATION, id + NUM_RESERVED_PORTS}; | 558 | uint8_t data[2] = {TCP_PACKET_CONNECTION_NOTIFICATION, id + NUM_RESERVED_PORTS}; |
472 | return write_packet_TCP_secure_connection(con, data, sizeof(data)); | 559 | return write_packet_TCP_secure_connection(con, data, sizeof(data), 1); |
473 | } | 560 | } |
474 | 561 | ||
475 | /* return 1 on success. | 562 | /* return 1 on success. |
@@ -479,7 +566,7 @@ static int send_connect_notification(TCP_Secure_Connection *con, uint8_t id) | |||
479 | static int send_disconnect_notification(TCP_Secure_Connection *con, uint8_t id) | 566 | static int send_disconnect_notification(TCP_Secure_Connection *con, uint8_t id) |
480 | { | 567 | { |
481 | uint8_t data[2] = {TCP_PACKET_DISCONNECT_NOTIFICATION, id + NUM_RESERVED_PORTS}; | 568 | uint8_t data[2] = {TCP_PACKET_DISCONNECT_NOTIFICATION, id + NUM_RESERVED_PORTS}; |
482 | return write_packet_TCP_secure_connection(con, data, sizeof(data)); | 569 | return write_packet_TCP_secure_connection(con, data, sizeof(data), 1); |
483 | } | 570 | } |
484 | 571 | ||
485 | /* return 0 on success. | 572 | /* return 0 on success. |
@@ -579,7 +666,7 @@ static int handle_TCP_oob_send(TCP_Server *TCP_server, uint32_t con_id, const ui | |||
579 | memcpy(resp_packet + 1, con->public_key, crypto_box_PUBLICKEYBYTES); | 666 | memcpy(resp_packet + 1, con->public_key, crypto_box_PUBLICKEYBYTES); |
580 | memcpy(resp_packet + 1 + crypto_box_PUBLICKEYBYTES, data, length); | 667 | memcpy(resp_packet + 1 + crypto_box_PUBLICKEYBYTES, data, length); |
581 | write_packet_TCP_secure_connection(&TCP_server->accepted_connection_array[other_index], resp_packet, | 668 | write_packet_TCP_secure_connection(&TCP_server->accepted_connection_array[other_index], resp_packet, |
582 | sizeof(resp_packet)); | 669 | sizeof(resp_packet), 0); |
583 | } | 670 | } |
584 | 671 | ||
585 | return 0; | 672 | return 0; |
@@ -637,7 +724,7 @@ static int handle_onion_recv_1(void *object, IP_Port dest, const uint8_t *data, | |||
637 | memcpy(packet + 1, data, length); | 724 | memcpy(packet + 1, data, length); |
638 | packet[0] = TCP_PACKET_ONION_RESPONSE; | 725 | packet[0] = TCP_PACKET_ONION_RESPONSE; |
639 | 726 | ||
640 | if (write_packet_TCP_secure_connection(con, packet, sizeof(packet)) != 1) | 727 | if (write_packet_TCP_secure_connection(con, packet, sizeof(packet), 0) != 1) |
641 | return 1; | 728 | return 1; |
642 | 729 | ||
643 | return 0; | 730 | return 0; |
@@ -682,7 +769,7 @@ static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, const uint | |||
682 | uint8_t response[1 + sizeof(uint64_t)]; | 769 | uint8_t response[1 + sizeof(uint64_t)]; |
683 | response[0] = TCP_PACKET_PONG; | 770 | response[0] = TCP_PACKET_PONG; |
684 | memcpy(response + 1, data + 1, sizeof(uint64_t)); | 771 | memcpy(response + 1, data + 1, sizeof(uint64_t)); |
685 | write_packet_TCP_secure_connection(con, response, sizeof(response)); | 772 | write_packet_TCP_secure_connection(con, response, sizeof(response), 1); |
686 | return 0; | 773 | return 0; |
687 | } | 774 | } |
688 | 775 | ||
@@ -752,7 +839,7 @@ static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, const uint | |||
752 | uint8_t new_data[length]; | 839 | uint8_t new_data[length]; |
753 | memcpy(new_data, data, length); | 840 | memcpy(new_data, data, length); |
754 | new_data[0] = other_c_id; | 841 | new_data[0] = other_c_id; |
755 | int ret = write_packet_TCP_secure_connection(&TCP_server->accepted_connection_array[index], new_data, length); | 842 | int ret = write_packet_TCP_secure_connection(&TCP_server->accepted_connection_array[index], new_data, length, 0); |
756 | 843 | ||
757 | if (ret == -1) | 844 | if (ret == -1) |
758 | return -1; | 845 | return -1; |
@@ -1058,7 +1145,7 @@ static void do_TCP_confirmed(TCP_Server *TCP_server) | |||
1058 | ++ping_id; | 1145 | ++ping_id; |
1059 | 1146 | ||
1060 | memcpy(ping + 1, &ping_id, sizeof(uint64_t)); | 1147 | memcpy(ping + 1, &ping_id, sizeof(uint64_t)); |
1061 | int ret = write_packet_TCP_secure_connection(conn, ping, sizeof(ping)); | 1148 | int ret = write_packet_TCP_secure_connection(conn, ping, sizeof(ping), 1); |
1062 | 1149 | ||
1063 | if (ret == 1) { | 1150 | if (ret == 1) { |
1064 | conn->last_pinged = unix_time(); | 1151 | conn->last_pinged = unix_time(); |