diff options
-rw-r--r-- | auto_tests/tox_test.c | 15 | ||||
-rw-r--r-- | toxcore/Messenger.c | 2 | ||||
-rw-r--r-- | toxcore/net_crypto.c | 47 | ||||
-rw-r--r-- | toxcore/net_crypto.h | 4 |
4 files changed, 50 insertions, 18 deletions
diff --git a/auto_tests/tox_test.c b/auto_tests/tox_test.c index 13c4fc7b..5723d1c3 100644 --- a/auto_tests/tox_test.c +++ b/auto_tests/tox_test.c | |||
@@ -96,16 +96,21 @@ void file_print_control(Tox *m, int friendnumber, uint8_t send_recieve, uint8_t | |||
96 | } | 96 | } |
97 | 97 | ||
98 | uint64_t size_recv; | 98 | uint64_t size_recv; |
99 | uint8_t num; | ||
99 | void write_file(Tox *m, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata) | 100 | void write_file(Tox *m, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata) |
100 | { | 101 | { |
101 | if (*((uint32_t *)userdata) != 974536) | 102 | if (*((uint32_t *)userdata) != 974536) |
102 | return; | 103 | return; |
103 | 104 | ||
104 | uint8_t *f_data = malloc(length); | 105 | uint8_t *f_data = malloc(length); |
105 | memset(f_data, 6, length); | 106 | memset(f_data, num, length); |
107 | ++num; | ||
106 | 108 | ||
107 | if (memcmp(f_data, data, length) == 0) | 109 | if (memcmp(f_data, data, length) == 0) { |
108 | size_recv += length; | 110 | size_recv += length; |
111 | } else { | ||
112 | printf("FILE_CORRUPTED\n"); | ||
113 | } | ||
109 | } | 114 | } |
110 | 115 | ||
111 | START_TEST(test_few_clients) | 116 | START_TEST(test_few_clients) |
@@ -228,8 +233,8 @@ START_TEST(test_few_clients) | |||
228 | ck_assert_msg(fnum != -1, "tox_new_file_sender fail"); | 233 | ck_assert_msg(fnum != -1, "tox_new_file_sender fail"); |
229 | int fpiece_size = tox_file_data_size(tox2, 0); | 234 | int fpiece_size = tox_file_data_size(tox2, 0); |
230 | uint8_t *f_data = malloc(fpiece_size); | 235 | uint8_t *f_data = malloc(fpiece_size); |
231 | memset(f_data, 6, fpiece_size); | 236 | uint8_t num = 0; |
232 | 237 | memset(f_data, num, fpiece_size); | |
233 | while (1) { | 238 | while (1) { |
234 | file_sent = 0; | 239 | file_sent = 0; |
235 | tox_do(tox1); | 240 | tox_do(tox1); |
@@ -242,6 +247,8 @@ START_TEST(test_few_clients) | |||
242 | sendf_ok = 0; | 247 | sendf_ok = 0; |
243 | tox_file_send_control(tox2, 0, 0, fnum, TOX_FILECONTROL_FINISHED, NULL, 0); | 248 | tox_file_send_control(tox2, 0, 0, fnum, TOX_FILECONTROL_FINISHED, NULL, 0); |
244 | } | 249 | } |
250 | ++num; | ||
251 | memset(f_data, num, fpiece_size); | ||
245 | 252 | ||
246 | totalf_size -= fpiece_size; | 253 | totalf_size -= fpiece_size; |
247 | } | 254 | } |
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; |