summaryrefslogtreecommitdiff
path: root/toxcore/net_crypto.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-05-09 21:46:37 -0400
committerirungentoo <irungentoo@gmail.com>2014-05-09 21:46:37 -0400
commitb0731f54cd6c891283565e0082ad06481c274415 (patch)
treee7b051a91f812f9e0b1c92a336ef2a6b11f2be93 /toxcore/net_crypto.c
parenta26ced5fcb7a9a8bf6b4795c95b04277c14b84f2 (diff)
Fixed small issues.
Added set_buffer_end(). File transfer test now checks if pieces arrive correctly.
Diffstat (limited to 'toxcore/net_crypto.c')
-rw-r--r--toxcore/net_crypto.c47
1 files changed, 36 insertions, 11 deletions
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 */
500static 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 */
554static int handle_request_packet(Packets_Array *send_array, uint8_t *data, uint16_t length) 571static 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 */
759static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint16_t max_num) 777static 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