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 } uint64_t size_recv; +uint8_t num; void write_file(Tox *m, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata) { if (*((uint32_t *)userdata) != 974536) return; uint8_t *f_data = malloc(length); - memset(f_data, 6, length); + memset(f_data, num, length); + ++num; - if (memcmp(f_data, data, length) == 0) + if (memcmp(f_data, data, length) == 0) { size_recv += length; + } else { + printf("FILE_CORRUPTED\n"); + } } START_TEST(test_few_clients) @@ -228,8 +233,8 @@ START_TEST(test_few_clients) ck_assert_msg(fnum != -1, "tox_new_file_sender fail"); int fpiece_size = tox_file_data_size(tox2, 0); uint8_t *f_data = malloc(fpiece_size); - memset(f_data, 6, fpiece_size); - + uint8_t num = 0; + memset(f_data, num, fpiece_size); while (1) { file_sent = 0; tox_do(tox1); @@ -242,6 +247,8 @@ START_TEST(test_few_clients) sendf_ok = 0; tox_file_send_control(tox2, 0, 0, fnum, TOX_FILECONTROL_FINISHED, NULL, 0); } + ++num; + memset(f_data, num, fpiece_size); totalf_size -= fpiece_size; } 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) kill_net_crypto(m->net_crypto); kill_DHT(m->dht); kill_networking(m->net); + for (i = 0; i < m->numfriends; ++i) { if (m->friendlist[i].statusmessage) free(m->friendlist[i].statusmessage); } + free(m->friendlist); free(m); } 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) return 0; } +/* Set array buffer end to number. + * + * return -1 on failure. + * return 0 on success. + */ +static int set_buffer_end(Packets_Array *array, uint32_t number) +{ + if ((number - array->buffer_start) > CRYPTO_PACKET_BUFFER_SIZE) + return -1; + + if ((number - array->buffer_end) > CRYPTO_PACKET_BUFFER_SIZE) + return -1; + + array->buffer_end = number; + return 0; +} + /* Create a packet request packet from recv_array and send_buffer_end into * data of length. * @@ -549,7 +566,7 @@ static int generate_request_packet(uint8_t *data, uint16_t length, Packets_Array * Remove all the packets the other recieved from the array. * * return -1 on failure. - * return 0 on success. + * return number of requested packets on success. */ static int handle_request_packet(Packets_Array *send_array, uint8_t *data, uint16_t length) { @@ -566,6 +583,7 @@ static int handle_request_packet(Packets_Array *send_array, uint8_t *data, uint1 --length; uint32_t i, n = 1; + uint32_t requested = 0; for (i = send_array->buffer_start; i != send_array->buffer_end; ++i) { if (length == 0) @@ -581,6 +599,7 @@ static int handle_request_packet(Packets_Array *send_array, uint8_t *data, uint1 ++data; --length; n = 0; + ++requested; } else { free(send_array->buffer[num]); send_array->buffer[num] = NULL; @@ -599,7 +618,7 @@ static int handle_request_packet(Packets_Array *send_array, uint8_t *data, uint1 } } - return 0; + return requested; } /** END: Array Related functions **/ @@ -630,7 +649,6 @@ static int send_data_packet(Net_Crypto *c, int crypt_connection_id, uint8_t *dat return -1; increment_nonce(conn->sent_nonce); - conn->last_data_packet_sent = current_time_monotonic(); //TODO remove this. return send_packet_to(c, crypt_connection_id, packet, sizeof(packet)); } @@ -754,7 +772,7 @@ static int send_request_packet(Net_Crypto *c, int crypt_connection_id) /* Send up to max num previously requested data packets. * * return -1 on failure. - * return 0 on success. + * return number of packets sent on success. */ static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint16_t max_num) { @@ -763,7 +781,7 @@ static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint16 if (conn == 0) return -1; - uint32_t i; + uint32_t i, num_sent = 0; for (i = 0; i < max_num; ++i) { Packet_Data *dt; @@ -783,9 +801,11 @@ static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint16 dt->time = current_time_monotonic(); if (send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, packet_num, dt->data, - dt->length) != 0) - printf("send_data_packet failed\n"); + dt->length) == 0) + ++num_sent; } + + return num_sent; } @@ -934,12 +954,14 @@ static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, uin } if (real_data[0] == PACKET_ID_REQUEST) { - if (handle_request_packet(&conn->send_array, real_data, real_length) != 0) { + int requested = handle_request_packet(&conn->send_array, real_data, real_length); + + if (requested == -1) { printf("fail %u %u\n", real_data[0], real_length); return -1; } - //TODO: use num. + set_buffer_end(&conn->recv_array, num); } else { Packet_Data dt; dt.time = current_time_monotonic(); @@ -1476,8 +1498,11 @@ static void send_crypto_packets(Net_Crypto *c) } if (conn->status >= CRYPTO_CONN_NOT_CONFIRMED - && (CRYPTO_SEND_PACKET_INTERVAL + conn->last_data_packet_sent) < temp_time) { - send_request_packet(c, i); + && (CRYPTO_SEND_PACKET_INTERVAL + conn->last_request_packet_sent) < temp_time) { + if (send_request_packet(c, i) == 0) { + conn->last_request_packet_sent = temp_time; + } + } //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 @@ #include "DHT.h" -#define CRYPTO_HANDSHAKE_TIMEOUT (CONNECTION_TIMEOUT * 2) - #define CRYPTO_CONN_NO_CONNECTION 0 #define CRYPTO_CONN_COOKIE_REQUESTING 1 //send cookie request packets #define CRYPTO_CONN_HANDSHAKE_SENT 2 //send handshake packets @@ -104,7 +102,7 @@ typedef struct { void *connection_data_callback_object; int connection_data_callback_id; - uint64_t last_data_packet_sent; + uint64_t last_request_packet_sent; uint32_t packet_counter; } Crypto_Connection;