diff options
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/Messenger.c | 2 | ||||
-rw-r--r-- | toxcore/net_crypto.c | 47 | ||||
-rw-r--r-- | toxcore/net_crypto.h | 4 |
3 files changed, 39 insertions, 14 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index fc8bd9bb..c877d565 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c | |||
@@ -1835,10 +1835,12 @@ void kill_messenger(Messenger *m) | |||
1835 | kill_net_crypto(m->net_crypto); | 1835 | kill_net_crypto(m->net_crypto); |
1836 | kill_DHT(m->dht); | 1836 | kill_DHT(m->dht); |
1837 | kill_networking(m->net); | 1837 | kill_networking(m->net); |
1838 | |||
1838 | for (i = 0; i < m->numfriends; ++i) { | 1839 | for (i = 0; i < m->numfriends; ++i) { |
1839 | if (m->friendlist[i].statusmessage) | 1840 | if (m->friendlist[i].statusmessage) |
1840 | free(m->friendlist[i].statusmessage); | 1841 | free(m->friendlist[i].statusmessage); |
1841 | } | 1842 | } |
1843 | |||
1842 | free(m->friendlist); | 1844 | free(m->friendlist); |
1843 | free(m); | 1845 | free(m); |
1844 | } | 1846 | } |
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 049e2690..b95791f1 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c | |||
@@ -492,6 +492,23 @@ static int clear_buffer_until(Packets_Array *array, uint32_t number) | |||
492 | return 0; | 492 | return 0; |
493 | } | 493 | } |
494 | 494 | ||
495 | /* Set array buffer end to number. | ||
496 | * | ||
497 | * return -1 on failure. | ||
498 | * return 0 on success. | ||
499 | */ | ||
500 | static int set_buffer_end(Packets_Array *array, uint32_t number) | ||
501 | { | ||
502 | if ((number - array->buffer_start) > CRYPTO_PACKET_BUFFER_SIZE) | ||
503 | return -1; | ||
504 | |||
505 | if ((number - array->buffer_end) > CRYPTO_PACKET_BUFFER_SIZE) | ||
506 | return -1; | ||
507 | |||
508 | array->buffer_end = number; | ||
509 | return 0; | ||
510 | } | ||
511 | |||
495 | /* Create a packet request packet from recv_array and send_buffer_end into | 512 | /* Create a packet request packet from recv_array and send_buffer_end into |
496 | * data of length. | 513 | * data of length. |
497 | * | 514 | * |
@@ -549,7 +566,7 @@ static int generate_request_packet(uint8_t *data, uint16_t length, Packets_Array | |||
549 | * Remove all the packets the other recieved from the array. | 566 | * Remove all the packets the other recieved from the array. |
550 | * | 567 | * |
551 | * return -1 on failure. | 568 | * return -1 on failure. |
552 | * return 0 on success. | 569 | * return number of requested packets on success. |
553 | */ | 570 | */ |
554 | static int handle_request_packet(Packets_Array *send_array, uint8_t *data, uint16_t length) | 571 | static int handle_request_packet(Packets_Array *send_array, uint8_t *data, uint16_t length) |
555 | { | 572 | { |
@@ -566,6 +583,7 @@ static int handle_request_packet(Packets_Array *send_array, uint8_t *data, uint1 | |||
566 | --length; | 583 | --length; |
567 | 584 | ||
568 | uint32_t i, n = 1; | 585 | uint32_t i, n = 1; |
586 | uint32_t requested = 0; | ||
569 | 587 | ||
570 | for (i = send_array->buffer_start; i != send_array->buffer_end; ++i) { | 588 | for (i = send_array->buffer_start; i != send_array->buffer_end; ++i) { |
571 | if (length == 0) | 589 | if (length == 0) |
@@ -581,6 +599,7 @@ static int handle_request_packet(Packets_Array *send_array, uint8_t *data, uint1 | |||
581 | ++data; | 599 | ++data; |
582 | --length; | 600 | --length; |
583 | n = 0; | 601 | n = 0; |
602 | ++requested; | ||
584 | } else { | 603 | } else { |
585 | free(send_array->buffer[num]); | 604 | free(send_array->buffer[num]); |
586 | send_array->buffer[num] = NULL; | 605 | send_array->buffer[num] = NULL; |
@@ -599,7 +618,7 @@ static int handle_request_packet(Packets_Array *send_array, uint8_t *data, uint1 | |||
599 | } | 618 | } |
600 | } | 619 | } |
601 | 620 | ||
602 | return 0; | 621 | return requested; |
603 | } | 622 | } |
604 | 623 | ||
605 | /** END: Array Related functions **/ | 624 | /** END: Array Related functions **/ |
@@ -630,7 +649,6 @@ static int send_data_packet(Net_Crypto *c, int crypt_connection_id, uint8_t *dat | |||
630 | return -1; | 649 | return -1; |
631 | 650 | ||
632 | increment_nonce(conn->sent_nonce); | 651 | increment_nonce(conn->sent_nonce); |
633 | conn->last_data_packet_sent = current_time_monotonic(); //TODO remove this. | ||
634 | return send_packet_to(c, crypt_connection_id, packet, sizeof(packet)); | 652 | return send_packet_to(c, crypt_connection_id, packet, sizeof(packet)); |
635 | } | 653 | } |
636 | 654 | ||
@@ -754,7 +772,7 @@ static int send_request_packet(Net_Crypto *c, int crypt_connection_id) | |||
754 | /* Send up to max num previously requested data packets. | 772 | /* Send up to max num previously requested data packets. |
755 | * | 773 | * |
756 | * return -1 on failure. | 774 | * return -1 on failure. |
757 | * return 0 on success. | 775 | * return number of packets sent on success. |
758 | */ | 776 | */ |
759 | static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint16_t max_num) | 777 | static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint16_t max_num) |
760 | { | 778 | { |
@@ -763,7 +781,7 @@ static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint16 | |||
763 | if (conn == 0) | 781 | if (conn == 0) |
764 | return -1; | 782 | return -1; |
765 | 783 | ||
766 | uint32_t i; | 784 | uint32_t i, num_sent = 0; |
767 | 785 | ||
768 | for (i = 0; i < max_num; ++i) { | 786 | for (i = 0; i < max_num; ++i) { |
769 | Packet_Data *dt; | 787 | Packet_Data *dt; |
@@ -783,9 +801,11 @@ static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint16 | |||
783 | dt->time = current_time_monotonic(); | 801 | dt->time = current_time_monotonic(); |
784 | 802 | ||
785 | if (send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, packet_num, dt->data, | 803 | if (send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, packet_num, dt->data, |
786 | dt->length) != 0) | 804 | dt->length) == 0) |
787 | printf("send_data_packet failed\n"); | 805 | ++num_sent; |
788 | } | 806 | } |
807 | |||
808 | return num_sent; | ||
789 | } | 809 | } |
790 | 810 | ||
791 | 811 | ||
@@ -934,12 +954,14 @@ static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, uin | |||
934 | } | 954 | } |
935 | 955 | ||
936 | if (real_data[0] == PACKET_ID_REQUEST) { | 956 | if (real_data[0] == PACKET_ID_REQUEST) { |
937 | if (handle_request_packet(&conn->send_array, real_data, real_length) != 0) { | 957 | int requested = handle_request_packet(&conn->send_array, real_data, real_length); |
958 | |||
959 | if (requested == -1) { | ||
938 | printf("fail %u %u\n", real_data[0], real_length); | 960 | printf("fail %u %u\n", real_data[0], real_length); |
939 | return -1; | 961 | return -1; |
940 | } | 962 | } |
941 | 963 | ||
942 | //TODO: use num. | 964 | set_buffer_end(&conn->recv_array, num); |
943 | } else { | 965 | } else { |
944 | Packet_Data dt; | 966 | Packet_Data dt; |
945 | dt.time = current_time_monotonic(); | 967 | dt.time = current_time_monotonic(); |
@@ -1476,8 +1498,11 @@ static void send_crypto_packets(Net_Crypto *c) | |||
1476 | } | 1498 | } |
1477 | 1499 | ||
1478 | if (conn->status >= CRYPTO_CONN_NOT_CONFIRMED | 1500 | if (conn->status >= CRYPTO_CONN_NOT_CONFIRMED |
1479 | && (CRYPTO_SEND_PACKET_INTERVAL + conn->last_data_packet_sent) < temp_time) { | 1501 | && (CRYPTO_SEND_PACKET_INTERVAL + conn->last_request_packet_sent) < temp_time) { |
1480 | send_request_packet(c, i); | 1502 | if (send_request_packet(c, i) == 0) { |
1503 | conn->last_request_packet_sent = temp_time; | ||
1504 | } | ||
1505 | |||
1481 | } | 1506 | } |
1482 | 1507 | ||
1483 | //TODO | 1508 | //TODO |
diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index b3964064..b06fcb10 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h | |||
@@ -26,8 +26,6 @@ | |||
26 | 26 | ||
27 | #include "DHT.h" | 27 | #include "DHT.h" |
28 | 28 | ||
29 | #define CRYPTO_HANDSHAKE_TIMEOUT (CONNECTION_TIMEOUT * 2) | ||
30 | |||
31 | #define CRYPTO_CONN_NO_CONNECTION 0 | 29 | #define CRYPTO_CONN_NO_CONNECTION 0 |
32 | #define CRYPTO_CONN_COOKIE_REQUESTING 1 //send cookie request packets | 30 | #define CRYPTO_CONN_COOKIE_REQUESTING 1 //send cookie request packets |
33 | #define CRYPTO_CONN_HANDSHAKE_SENT 2 //send handshake packets | 31 | #define CRYPTO_CONN_HANDSHAKE_SENT 2 //send handshake packets |
@@ -104,7 +102,7 @@ typedef struct { | |||
104 | void *connection_data_callback_object; | 102 | void *connection_data_callback_object; |
105 | int connection_data_callback_id; | 103 | int connection_data_callback_id; |
106 | 104 | ||
107 | uint64_t last_data_packet_sent; | 105 | uint64_t last_request_packet_sent; |
108 | 106 | ||
109 | uint32_t packet_counter; | 107 | uint32_t packet_counter; |
110 | } Crypto_Connection; | 108 | } Crypto_Connection; |