summaryrefslogtreecommitdiff
path: root/toxcore/net_crypto.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2015-06-23 22:05:39 -0400
committerirungentoo <irungentoo@gmail.com>2015-06-23 22:05:39 -0400
commit1434b319f3956fa3a62033486851ab3815775901 (patch)
tree67ce6739200b3e88fb94d33e009515dc6af545b0 /toxcore/net_crypto.c
parentf0433595f9c118bd19f1bf310ae65c02fbb2eb22 (diff)
Added code to net_crypto to calculate rtt.
Diffstat (limited to 'toxcore/net_crypto.c')
-rw-r--r--toxcore/net_crypto.c40
1 files changed, 35 insertions, 5 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index 4f4f8283..a826f77a 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -659,7 +659,8 @@ static int generate_request_packet(uint8_t *data, uint16_t length, const Packets
659 * return -1 on failure. 659 * return -1 on failure.
660 * return number of requested packets on success. 660 * return number of requested packets on success.
661 */ 661 */
662static int handle_request_packet(Packets_Array *send_array, const uint8_t *data, uint16_t length) 662static int handle_request_packet(Packets_Array *send_array, const uint8_t *data, uint16_t length,
663 uint64_t *latest_send_time, uint64_t rtt_time)
663{ 664{
664 if (length < 1) 665 if (length < 1)
665 return -1; 666 return -1;
@@ -677,6 +678,7 @@ static int handle_request_packet(Packets_Array *send_array, const uint8_t *data,
677 uint32_t requested = 0; 678 uint32_t requested = 0;
678 679
679 uint64_t temp_time = current_time_monotonic(); 680 uint64_t temp_time = current_time_monotonic();
681 uint64_t l_sent_time = ~0;
680 682
681 for (i = send_array->buffer_start; i != send_array->buffer_end; ++i) { 683 for (i = send_array->buffer_start; i != send_array->buffer_end; ++i) {
682 if (length == 0) 684 if (length == 0)
@@ -688,7 +690,7 @@ static int handle_request_packet(Packets_Array *send_array, const uint8_t *data,
688 if (send_array->buffer[num]) { 690 if (send_array->buffer[num]) {
689 uint64_t sent_time = send_array->buffer[num]->sent_time; 691 uint64_t sent_time = send_array->buffer[num]->sent_time;
690 692
691 if ((sent_time + DEFAULT_PING_CONNECTION) < temp_time) { 693 if ((sent_time + rtt_time) < temp_time) {
692 send_array->buffer[num]->sent_time = 0; 694 send_array->buffer[num]->sent_time = 0;
693 } 695 }
694 } 696 }
@@ -698,6 +700,11 @@ static int handle_request_packet(Packets_Array *send_array, const uint8_t *data,
698 n = 0; 700 n = 0;
699 ++requested; 701 ++requested;
700 } else { 702 } else {
703 uint64_t sent_time = send_array->buffer[num]->sent_time;
704
705 if (l_sent_time < sent_time)
706 l_sent_time = sent_time;
707
701 free(send_array->buffer[num]); 708 free(send_array->buffer[num]);
702 send_array->buffer[num] = NULL; 709 send_array->buffer[num] = NULL;
703 } 710 }
@@ -715,6 +722,9 @@ static int handle_request_packet(Packets_Array *send_array, const uint8_t *data,
715 } 722 }
716 } 723 }
717 724
725 if (*latest_send_time < l_sent_time)
726 *latest_send_time = l_sent_time;
727
718 return requested; 728 return requested;
719} 729}
720 730
@@ -1146,8 +1156,19 @@ static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, con
1146 buffer_start = ntohl(buffer_start); 1156 buffer_start = ntohl(buffer_start);
1147 num = ntohl(num); 1157 num = ntohl(num);
1148 1158
1149 if (buffer_start != conn->send_array.buffer_start && clear_buffer_until(&conn->send_array, buffer_start) != 0) 1159 uint64_t rtt_calc_time = 0;
1150 return -1; 1160
1161 if (buffer_start != conn->send_array.buffer_start) {
1162 Packet_Data *packet_time;
1163
1164 if (get_data_pointer(&conn->send_array, &packet_time, conn->send_array.buffer_start) == 1) {
1165 rtt_calc_time = packet_time->sent_time;
1166 }
1167
1168 if (clear_buffer_until(&conn->send_array, buffer_start) != 0) {
1169 return -1;
1170 }
1171 }
1151 1172
1152 uint8_t *real_data = data + (sizeof(uint32_t) * 2); 1173 uint8_t *real_data = data + (sizeof(uint32_t) * 2);
1153 uint16_t real_length = len - (sizeof(uint32_t) * 2); 1174 uint16_t real_length = len - (sizeof(uint32_t) * 2);
@@ -1174,7 +1195,7 @@ static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, con
1174 } 1195 }
1175 1196
1176 if (real_data[0] == PACKET_ID_REQUEST) { 1197 if (real_data[0] == PACKET_ID_REQUEST) {
1177 int requested = handle_request_packet(&conn->send_array, real_data, real_length); 1198 int requested = handle_request_packet(&conn->send_array, real_data, real_length, &rtt_calc_time, conn->rtt_time);
1178 1199
1179 if (requested == -1) { 1200 if (requested == -1) {
1180 return -1; 1201 return -1;
@@ -1226,6 +1247,13 @@ static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, con
1226 return -1; 1247 return -1;
1227 } 1248 }
1228 1249
1250 if (rtt_calc_time != 0) {
1251 uint64_t rtt_time = current_time_monotonic() - rtt_calc_time;
1252
1253 if (rtt_time < conn->rtt_time)
1254 conn->rtt_time = rtt_time;
1255 }
1256
1229 return 0; 1257 return 0;
1230} 1258}
1231 1259
@@ -1580,6 +1608,7 @@ int accept_crypto_connection(Net_Crypto *c, New_Connection *n_c)
1580 memcpy(conn->dht_public_key, n_c->dht_public_key, crypto_box_PUBLICKEYBYTES); 1608 memcpy(conn->dht_public_key, n_c->dht_public_key, crypto_box_PUBLICKEYBYTES);
1581 conn->packet_send_rate = CRYPTO_PACKET_MIN_RATE; 1609 conn->packet_send_rate = CRYPTO_PACKET_MIN_RATE;
1582 conn->packets_left = CRYPTO_MIN_QUEUE_LENGTH; 1610 conn->packets_left = CRYPTO_MIN_QUEUE_LENGTH;
1611 conn->rtt_time = DEFAULT_PING_CONNECTION;
1583 crypto_connection_add_source(c, crypt_connection_id, n_c->source); 1612 crypto_connection_add_source(c, crypt_connection_id, n_c->source);
1584 return crypt_connection_id; 1613 return crypt_connection_id;
1585} 1614}
@@ -1621,6 +1650,7 @@ int new_crypto_connection(Net_Crypto *c, const uint8_t *real_public_key, const u
1621 conn->status = CRYPTO_CONN_COOKIE_REQUESTING; 1650 conn->status = CRYPTO_CONN_COOKIE_REQUESTING;
1622 conn->packet_send_rate = CRYPTO_PACKET_MIN_RATE; 1651 conn->packet_send_rate = CRYPTO_PACKET_MIN_RATE;
1623 conn->packets_left = CRYPTO_MIN_QUEUE_LENGTH; 1652 conn->packets_left = CRYPTO_MIN_QUEUE_LENGTH;
1653 conn->rtt_time = DEFAULT_PING_CONNECTION;
1624 memcpy(conn->dht_public_key, dht_public_key, crypto_box_PUBLICKEYBYTES); 1654 memcpy(conn->dht_public_key, dht_public_key, crypto_box_PUBLICKEYBYTES);
1625 1655
1626 conn->cookie_request_number = random_64b(); 1656 conn->cookie_request_number = random_64b();