summaryrefslogtreecommitdiff
path: root/toxcore/net_crypto.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/net_crypto.c')
-rw-r--r--toxcore/net_crypto.c93
1 files changed, 64 insertions, 29 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index 0ed855bb..6d4f6a9b 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -676,6 +676,8 @@ static int handle_request_packet(Packets_Array *send_array, const uint8_t *data,
676 uint32_t i, n = 1; 676 uint32_t i, n = 1;
677 uint32_t requested = 0; 677 uint32_t requested = 0;
678 678
679 uint64_t temp_time = current_time_monotonic();
680
679 for (i = send_array->buffer_start; i != send_array->buffer_end; ++i) { 681 for (i = send_array->buffer_start; i != send_array->buffer_end; ++i) {
680 if (length == 0) 682 if (length == 0)
681 break; 683 break;
@@ -684,7 +686,11 @@ static int handle_request_packet(Packets_Array *send_array, const uint8_t *data,
684 686
685 if (n == data[0]) { 687 if (n == data[0]) {
686 if (send_array->buffer[num]) { 688 if (send_array->buffer[num]) {
687 send_array->buffer[num]->sent = 0; 689 uint64_t sent_time = send_array->buffer[num]->sent_time;
690
691 if ((sent_time + DEFAULT_PING_CONNECTION) < temp_time) {
692 send_array->buffer[num]->sent_time = 0;
693 }
688 } 694 }
689 695
690 ++data; 696 ++data;
@@ -788,12 +794,12 @@ static int reset_max_speed_reached(Net_Crypto *c, int crypt_connection_id)
788 uint8_t send_failed = 0; 794 uint8_t send_failed = 0;
789 795
790 if (ret == 1) { 796 if (ret == 1) {
791 if (!dt->sent) { 797 if (!dt->sent_time) {
792 if (send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, packet_num, dt->data, 798 if (send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, packet_num, dt->data,
793 dt->length) != 0) { 799 dt->length) != 0) {
794 send_failed = 1; 800 send_failed = 1;
795 } else { 801 } else {
796 dt->sent = 1; 802 dt->sent_time = current_time_monotonic();
797 } 803 }
798 } 804 }
799 } 805 }
@@ -831,7 +837,7 @@ static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, cons
831 } 837 }
832 838
833 Packet_Data dt; 839 Packet_Data dt;
834 dt.sent = 0; 840 dt.sent_time = 0;
835 dt.length = length; 841 dt.length = length;
836 memcpy(dt.data, data, length); 842 memcpy(dt.data, data, length);
837 pthread_mutex_lock(&conn->mutex); 843 pthread_mutex_lock(&conn->mutex);
@@ -849,7 +855,7 @@ static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, cons
849 Packet_Data *dt1 = NULL; 855 Packet_Data *dt1 = NULL;
850 856
851 if (get_data_pointer(&conn->send_array, &dt1, packet_num) == 1) 857 if (get_data_pointer(&conn->send_array, &dt1, packet_num) == 1)
852 dt1->sent = 1; 858 dt1->sent_time = current_time_monotonic();
853 } else { 859 } else {
854 conn->maximum_speed_reached = 1; 860 conn->maximum_speed_reached = 1;
855 LOGGER_ERROR("send_data_packet failed\n"); 861 LOGGER_ERROR("send_data_packet failed\n");
@@ -936,7 +942,7 @@ static int send_request_packet(Net_Crypto *c, int crypt_connection_id)
936 * return -1 on failure. 942 * return -1 on failure.
937 * return number of packets sent on success. 943 * return number of packets sent on success.
938 */ 944 */
939static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint16_t max_num) 945static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint32_t max_num)
940{ 946{
941 if (max_num == 0) 947 if (max_num == 0)
942 return -1; 948 return -1;
@@ -946,6 +952,7 @@ static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint16
946 if (conn == 0) 952 if (conn == 0)
947 return -1; 953 return -1;
948 954
955 uint64_t temp_time = current_time_monotonic();
949 uint32_t i, num_sent = 0, array_size = num_packets_array(&conn->send_array); 956 uint32_t i, num_sent = 0, array_size = num_packets_array(&conn->send_array);
950 957
951 for (i = 0; i < array_size; ++i) { 958 for (i = 0; i < array_size; ++i) {
@@ -959,13 +966,13 @@ static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint16
959 continue; 966 continue;
960 } 967 }
961 968
962 if (dt->sent) { 969 if (dt->sent_time) {
963 continue; 970 continue;
964 } 971 }
965 972
966 if (send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, packet_num, dt->data, 973 if (send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, packet_num, dt->data,
967 dt->length) == 0) { 974 dt->length) == 0) {
968 dt->sent = 1; 975 dt->sent_time = temp_time;
969 ++num_sent; 976 ++num_sent;
970 } 977 }
971 978
@@ -1439,10 +1446,10 @@ static int crypto_connection_add_source(Net_Crypto *c, int crypt_connection_id,
1439 1446
1440 if (source.ip.family == AF_INET || source.ip.family == AF_INET6) { 1447 if (source.ip.family == AF_INET || source.ip.family == AF_INET6) {
1441 if (!ipport_equal(&source, &conn->ip_port)) { 1448 if (!ipport_equal(&source, &conn->ip_port)) {
1442 if (!bs_list_add(&c->ip_port_list, &source, crypt_connection_id)) 1449 if (!bs_list_add(&c->ip_port_list, (uint8_t *)&source, crypt_connection_id))
1443 return -1; 1450 return -1;
1444 1451
1445 bs_list_remove(&c->ip_port_list, &conn->ip_port, crypt_connection_id); 1452 bs_list_remove(&c->ip_port_list, (uint8_t *)&conn->ip_port, crypt_connection_id);
1446 conn->ip_port = source; 1453 conn->ip_port = source;
1447 } 1454 }
1448 1455
@@ -1547,12 +1554,13 @@ int accept_crypto_connection(Net_Crypto *c, New_Connection *n_c)
1547 return -1; 1554 return -1;
1548 1555
1549 pthread_mutex_lock(&c->tcp_mutex); 1556 pthread_mutex_lock(&c->tcp_mutex);
1550 conn->connection_number_tcp = new_tcp_connection_to(c->tcp_c, n_c->dht_public_key, crypt_connection_id); 1557 int connection_number_tcp = new_tcp_connection_to(c->tcp_c, n_c->dht_public_key, crypt_connection_id);
1551 pthread_mutex_unlock(&c->tcp_mutex); 1558 pthread_mutex_unlock(&c->tcp_mutex);
1552 1559
1553 if (conn->connection_number_tcp == -1) 1560 if (connection_number_tcp == -1)
1554 return -1; 1561 return -1;
1555 1562
1563 conn->connection_number_tcp = connection_number_tcp;
1556 memcpy(conn->public_key, n_c->public_key, crypto_box_PUBLICKEYBYTES); 1564 memcpy(conn->public_key, n_c->public_key, crypto_box_PUBLICKEYBYTES);
1557 memcpy(conn->recv_nonce, n_c->recv_nonce, crypto_box_NONCEBYTES); 1565 memcpy(conn->recv_nonce, n_c->recv_nonce, crypto_box_NONCEBYTES);
1558 memcpy(conn->peersessionpublic_key, n_c->peersessionpublic_key, crypto_box_PUBLICKEYBYTES); 1566 memcpy(conn->peersessionpublic_key, n_c->peersessionpublic_key, crypto_box_PUBLICKEYBYTES);
@@ -1600,12 +1608,13 @@ int new_crypto_connection(Net_Crypto *c, const uint8_t *real_public_key, const u
1600 return -1; 1608 return -1;
1601 1609
1602 pthread_mutex_lock(&c->tcp_mutex); 1610 pthread_mutex_lock(&c->tcp_mutex);
1603 conn->connection_number_tcp = new_tcp_connection_to(c->tcp_c, dht_public_key, crypt_connection_id); 1611 int connection_number_tcp = new_tcp_connection_to(c->tcp_c, dht_public_key, crypt_connection_id);
1604 pthread_mutex_unlock(&c->tcp_mutex); 1612 pthread_mutex_unlock(&c->tcp_mutex);
1605 1613
1606 if (conn->connection_number_tcp == -1) 1614 if (connection_number_tcp == -1)
1607 return -1; 1615 return -1;
1608 1616
1617 conn->connection_number_tcp = connection_number_tcp;
1609 memcpy(conn->public_key, real_public_key, crypto_box_PUBLICKEYBYTES); 1618 memcpy(conn->public_key, real_public_key, crypto_box_PUBLICKEYBYTES);
1610 random_nonce(conn->sent_nonce); 1619 random_nonce(conn->sent_nonce);
1611 crypto_box_keypair(conn->sessionpublic_key, conn->sessionsecret_key); 1620 crypto_box_keypair(conn->sessionpublic_key, conn->sessionsecret_key);
@@ -1632,10 +1641,12 @@ int new_crypto_connection(Net_Crypto *c, const uint8_t *real_public_key, const u
1632 1641
1633/* Set the direct ip of the crypto connection. 1642/* Set the direct ip of the crypto connection.
1634 * 1643 *
1644 * Connected is 0 if we are not sure we are connected to that person, 1 if we are sure.
1645 *
1635 * return -1 on failure. 1646 * return -1 on failure.
1636 * return 0 on success. 1647 * return 0 on success.
1637 */ 1648 */
1638int set_direct_ip_port(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port) 1649int set_direct_ip_port(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port, _Bool connected)
1639{ 1650{
1640 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 1651 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
1641 1652
@@ -1656,10 +1667,16 @@ int set_direct_ip_port(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port)
1656 return -1; 1667 return -1;
1657 } 1668 }
1658 1669
1659 if (bs_list_add(&c->ip_port_list, &ip_port, crypt_connection_id)) { 1670 if (bs_list_add(&c->ip_port_list, (uint8_t *)&ip_port, crypt_connection_id)) {
1660 bs_list_remove(&c->ip_port_list, &conn->ip_port, crypt_connection_id); 1671 bs_list_remove(&c->ip_port_list, (uint8_t *)&conn->ip_port, crypt_connection_id);
1661 conn->ip_port = ip_port; 1672 conn->ip_port = ip_port;
1662 conn->direct_lastrecv_time = 0; 1673
1674 if (connected) {
1675 conn->direct_lastrecv_time = unix_time();
1676 } else {
1677 conn->direct_lastrecv_time = 0;
1678 }
1679
1663 return 0; 1680 return 0;
1664 } 1681 }
1665 } 1682 }
@@ -1931,7 +1948,7 @@ int nc_dht_pk_callback(Net_Crypto *c, int crypt_connection_id, void (*function)(
1931 */ 1948 */
1932static int crypto_id_ip_port(const Net_Crypto *c, IP_Port ip_port) 1949static int crypto_id_ip_port(const Net_Crypto *c, IP_Port ip_port)
1933{ 1950{
1934 return bs_list_find(&c->ip_port_list, &ip_port); 1951 return bs_list_find(&c->ip_port_list, (uint8_t *)&ip_port);
1935} 1952}
1936 1953
1937#define CRYPTO_MIN_PACKET_SIZE (1 + sizeof(uint16_t) + crypto_box_MACBYTES) 1954#define CRYPTO_MIN_PACKET_SIZE (1 + sizeof(uint16_t) + crypto_box_MACBYTES)
@@ -1983,7 +2000,14 @@ static int udp_handle_packet(void *object, IP_Port source, const uint8_t *packet
1983/* Ratio of recv queue size / recv packet rate (in seconds) times 2000/* Ratio of recv queue size / recv packet rate (in seconds) times
1984 * the number of ms between request packets to send at that ratio 2001 * the number of ms between request packets to send at that ratio
1985 */ 2002 */
1986#define REQUEST_PACKETS_COMPARE_CONSTANT (0.5 * 100.0) 2003#define REQUEST_PACKETS_COMPARE_CONSTANT (0.125 * 100.0)
2004
2005/* Multiplier for maximum allowed resends. */
2006#define PACKET_RESEND_MULTIPLIER 2
2007
2008/* Timeout for increasing speed after congestion event (in ms). */
2009#define CONGESTION_EVENT_TIMEOUT 4000
2010
1987static void send_crypto_packets(Net_Crypto *c) 2011static void send_crypto_packets(Net_Crypto *c)
1988{ 2012{
1989 uint32_t i; 2013 uint32_t i;
@@ -2061,7 +2085,11 @@ static void send_crypto_packets(Net_Crypto *c)
2061 double min_speed = 1000.0 * (((double)(total_sent)) / ((double)(CONGESTION_QUEUE_ARRAY_SIZE) * 2085 double min_speed = 1000.0 * (((double)(total_sent)) / ((double)(CONGESTION_QUEUE_ARRAY_SIZE) *
2062 PACKET_COUNTER_AVERAGE_INTERVAL)); 2086 PACKET_COUNTER_AVERAGE_INTERVAL));
2063 2087
2064 conn->packet_send_rate = min_speed * 1.2; 2088 if (conn->last_congestion_event + CONGESTION_EVENT_TIMEOUT < temp_time) {
2089 conn->packet_send_rate = min_speed * 1.2;
2090 } else {
2091 conn->packet_send_rate = min_speed;
2092 }
2065 2093
2066 if (conn->packet_send_rate < CRYPTO_PACKET_MIN_RATE) { 2094 if (conn->packet_send_rate < CRYPTO_PACKET_MIN_RATE) {
2067 conn->packet_send_rate = CRYPTO_PACKET_MIN_RATE; 2095 conn->packet_send_rate = CRYPTO_PACKET_MIN_RATE;
@@ -2084,10 +2112,15 @@ static void send_crypto_packets(Net_Crypto *c)
2084 conn->last_packets_left_set = temp_time; 2112 conn->last_packets_left_set = temp_time;
2085 } 2113 }
2086 2114
2087 int ret = send_requested_packets(c, i, conn->packets_left); 2115 int ret = send_requested_packets(c, i, conn->packets_left * PACKET_RESEND_MULTIPLIER);
2088 2116
2089 if (ret != -1) { 2117 if (ret != -1) {
2090 conn->packets_left -= ret; 2118 if (ret < conn->packets_left) {
2119 conn->packets_left -= ret;
2120 } else {
2121 conn->last_congestion_event = temp_time;
2122 conn->packets_left = 0;
2123 }
2091 } 2124 }
2092 2125
2093 if (conn->packet_send_rate > CRYPTO_PACKET_MIN_RATE * 1.5) { 2126 if (conn->packet_send_rate > CRYPTO_PACKET_MIN_RATE * 1.5) {
@@ -2280,7 +2313,7 @@ int crypto_kill(Net_Crypto *c, int crypt_connection_id)
2280 kill_tcp_connection_to(c->tcp_c, conn->connection_number_tcp); 2313 kill_tcp_connection_to(c->tcp_c, conn->connection_number_tcp);
2281 pthread_mutex_unlock(&c->tcp_mutex); 2314 pthread_mutex_unlock(&c->tcp_mutex);
2282 2315
2283 bs_list_remove(&c->ip_port_list, &conn->ip_port, crypt_connection_id); 2316 bs_list_remove(&c->ip_port_list, (uint8_t *)&conn->ip_port, crypt_connection_id);
2284 clear_temp_packet(c, crypt_connection_id); 2317 clear_temp_packet(c, crypt_connection_id);
2285 clear_buffer(&conn->send_array); 2318 clear_buffer(&conn->send_array);
2286 clear_buffer(&conn->recv_array); 2319 clear_buffer(&conn->recv_array);
@@ -2326,6 +2359,8 @@ void new_keys(Net_Crypto *c)
2326 2359
2327/* Save the public and private keys to the keys array. 2360/* Save the public and private keys to the keys array.
2328 * Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES. 2361 * Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES.
2362 *
2363 * TODO: Save only secret key.
2329 */ 2364 */
2330void save_keys(const Net_Crypto *c, uint8_t *keys) 2365void save_keys(const Net_Crypto *c, uint8_t *keys)
2331{ 2366{
@@ -2333,13 +2368,13 @@ void save_keys(const Net_Crypto *c, uint8_t *keys)
2333 memcpy(keys + crypto_box_PUBLICKEYBYTES, c->self_secret_key, crypto_box_SECRETKEYBYTES); 2368 memcpy(keys + crypto_box_PUBLICKEYBYTES, c->self_secret_key, crypto_box_SECRETKEYBYTES);
2334} 2369}
2335 2370
2336/* Load the public and private keys from the keys array. 2371/* Load the secret key.
2337 * Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES. 2372 * Length must be crypto_box_SECRETKEYBYTES.
2338 */ 2373 */
2339void load_keys(Net_Crypto *c, const uint8_t *keys) 2374void load_secret_key(Net_Crypto *c, const uint8_t *sk)
2340{ 2375{
2341 memcpy(c->self_public_key, keys, crypto_box_PUBLICKEYBYTES); 2376 memcpy(c->self_secret_key, sk, crypto_box_SECRETKEYBYTES);
2342 memcpy(c->self_secret_key, keys + crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES); 2377 crypto_scalarmult_curve25519_base(c->self_public_key, c->self_secret_key);
2343} 2378}
2344 2379
2345/* Run this to (re)initialize net_crypto. 2380/* Run this to (re)initialize net_crypto.