diff options
Diffstat (limited to 'toxcore/net_crypto.c')
-rw-r--r-- | toxcore/net_crypto.c | 159 |
1 files changed, 95 insertions, 64 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 8d598469..e0319f34 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c | |||
@@ -32,7 +32,7 @@ | |||
32 | #include "util.h" | 32 | #include "util.h" |
33 | #include "math.h" | 33 | #include "math.h" |
34 | 34 | ||
35 | static uint8_t crypt_connection_id_not_valid(Net_Crypto *c, int crypt_connection_id) | 35 | static uint8_t crypt_connection_id_not_valid(const Net_Crypto *c, int crypt_connection_id) |
36 | { | 36 | { |
37 | return (uint32_t)crypt_connection_id >= c->crypto_connections_length; | 37 | return (uint32_t)crypt_connection_id >= c->crypto_connections_length; |
38 | } | 38 | } |
@@ -70,7 +70,7 @@ static int is_alive(uint8_t status) | |||
70 | * return -1 on failure. | 70 | * return -1 on failure. |
71 | * return COOKIE_REQUEST_LENGTH on success. | 71 | * return COOKIE_REQUEST_LENGTH on success. |
72 | */ | 72 | */ |
73 | static int create_cookie_request(Net_Crypto *c, uint8_t *packet, uint8_t *dht_public_key, uint64_t number, | 73 | static int create_cookie_request(const Net_Crypto *c, uint8_t *packet, uint8_t *dht_public_key, uint64_t number, |
74 | uint8_t *shared_key) | 74 | uint8_t *shared_key) |
75 | { | 75 | { |
76 | uint8_t plain[COOKIE_REQUEST_PLAIN_LENGTH]; | 76 | uint8_t plain[COOKIE_REQUEST_PLAIN_LENGTH]; |
@@ -100,7 +100,7 @@ static int create_cookie_request(Net_Crypto *c, uint8_t *packet, uint8_t *dht_pu | |||
100 | * return -1 on failure. | 100 | * return -1 on failure. |
101 | * return 0 on success. | 101 | * return 0 on success. |
102 | */ | 102 | */ |
103 | static int create_cookie(uint8_t *cookie, uint8_t *bytes, uint8_t *encryption_key) | 103 | static int create_cookie(uint8_t *cookie, const uint8_t *bytes, const uint8_t *encryption_key) |
104 | { | 104 | { |
105 | uint8_t contents[COOKIE_CONTENTS_LENGTH]; | 105 | uint8_t contents[COOKIE_CONTENTS_LENGTH]; |
106 | uint64_t temp_time = unix_time(); | 106 | uint64_t temp_time = unix_time(); |
@@ -148,8 +148,8 @@ static int open_cookie(uint8_t *bytes, const uint8_t *cookie, const uint8_t *enc | |||
148 | * return -1 on failure. | 148 | * return -1 on failure. |
149 | * return COOKIE_RESPONSE_LENGTH on success. | 149 | * return COOKIE_RESPONSE_LENGTH on success. |
150 | */ | 150 | */ |
151 | static int create_cookie_response(Net_Crypto *c, uint8_t *packet, uint8_t *request_plain, uint8_t *shared_key, | 151 | static int create_cookie_response(const Net_Crypto *c, uint8_t *packet, const uint8_t *request_plain, |
152 | uint8_t *dht_public_key) | 152 | const uint8_t *shared_key, const uint8_t *dht_public_key) |
153 | { | 153 | { |
154 | uint8_t cookie_plain[COOKIE_DATA_LENGTH]; | 154 | uint8_t cookie_plain[COOKIE_DATA_LENGTH]; |
155 | memcpy(cookie_plain, request_plain, crypto_box_PUBLICKEYBYTES); | 155 | memcpy(cookie_plain, request_plain, crypto_box_PUBLICKEYBYTES); |
@@ -177,8 +177,8 @@ static int create_cookie_response(Net_Crypto *c, uint8_t *packet, uint8_t *reque | |||
177 | * return -1 on failure. | 177 | * return -1 on failure. |
178 | * return 0 on success. | 178 | * return 0 on success. |
179 | */ | 179 | */ |
180 | static int handle_cookie_request(Net_Crypto *c, uint8_t *request_plain, uint8_t *shared_key, uint8_t *dht_public_key, | 180 | static int handle_cookie_request(const Net_Crypto *c, uint8_t *request_plain, uint8_t *shared_key, |
181 | const uint8_t *packet, uint16_t length) | 181 | uint8_t *dht_public_key, const uint8_t *packet, uint16_t length) |
182 | { | 182 | { |
183 | if (length != COOKIE_REQUEST_LENGTH) | 183 | if (length != COOKIE_REQUEST_LENGTH) |
184 | return -1; | 184 | return -1; |
@@ -220,8 +220,8 @@ static int udp_handle_cookie_request(void *object, IP_Port source, const uint8_t | |||
220 | 220 | ||
221 | /* Handle the cookie request packet (for TCP) | 221 | /* Handle the cookie request packet (for TCP) |
222 | */ | 222 | */ |
223 | static int tcp_handle_cookie_request(Net_Crypto *c, TCP_Client_Connection *TCP_con, uint8_t conn_id, uint8_t *packet, | 223 | static int tcp_handle_cookie_request(const Net_Crypto *c, TCP_Client_Connection *TCP_con, uint8_t conn_id, |
224 | uint32_t length) | 224 | const uint8_t *packet, uint32_t length) |
225 | { | 225 | { |
226 | uint8_t request_plain[COOKIE_REQUEST_PLAIN_LENGTH]; | 226 | uint8_t request_plain[COOKIE_REQUEST_PLAIN_LENGTH]; |
227 | uint8_t shared_key[crypto_box_BEFORENMBYTES]; | 227 | uint8_t shared_key[crypto_box_BEFORENMBYTES]; |
@@ -243,8 +243,8 @@ static int tcp_handle_cookie_request(Net_Crypto *c, TCP_Client_Connection *TCP_c | |||
243 | 243 | ||
244 | /* Handle the cookie request packet (for TCP oob packets) | 244 | /* Handle the cookie request packet (for TCP oob packets) |
245 | */ | 245 | */ |
246 | static int tcp_oob_handle_cookie_request(Net_Crypto *c, TCP_Client_Connection *TCP_con, uint8_t *dht_public_key, | 246 | static int tcp_oob_handle_cookie_request(const Net_Crypto *c, TCP_Client_Connection *TCP_con, |
247 | uint8_t *packet, uint32_t length) | 247 | const uint8_t *dht_public_key, const uint8_t *packet, uint32_t length) |
248 | { | 248 | { |
249 | uint8_t request_plain[COOKIE_REQUEST_PLAIN_LENGTH]; | 249 | uint8_t request_plain[COOKIE_REQUEST_PLAIN_LENGTH]; |
250 | uint8_t shared_key[crypto_box_BEFORENMBYTES]; | 250 | uint8_t shared_key[crypto_box_BEFORENMBYTES]; |
@@ -302,8 +302,8 @@ static int handle_cookie_response(uint8_t *cookie, uint64_t *number, const uint8 | |||
302 | * return -1 on failure. | 302 | * return -1 on failure. |
303 | * return HANDSHAKE_PACKET_LENGTH on success. | 303 | * return HANDSHAKE_PACKET_LENGTH on success. |
304 | */ | 304 | */ |
305 | static int create_crypto_handshake(Net_Crypto *c, uint8_t *packet, uint8_t *cookie, uint8_t *nonce, uint8_t *session_pk, | 305 | static int create_crypto_handshake(const Net_Crypto *c, uint8_t *packet, const uint8_t *cookie, const uint8_t *nonce, |
306 | uint8_t *peer_real_pk, uint8_t *peer_dht_pubkey) | 306 | const uint8_t *session_pk, const uint8_t *peer_real_pk, const uint8_t *peer_dht_pubkey) |
307 | { | 307 | { |
308 | uint8_t plain[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + crypto_hash_sha512_BYTES + COOKIE_LENGTH]; | 308 | uint8_t plain[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + crypto_hash_sha512_BYTES + COOKIE_LENGTH]; |
309 | memcpy(plain, nonce, crypto_box_NONCEBYTES); | 309 | memcpy(plain, nonce, crypto_box_NONCEBYTES); |
@@ -348,8 +348,8 @@ static int create_crypto_handshake(Net_Crypto *c, uint8_t *packet, uint8_t *cook | |||
348 | * return -1 on failure. | 348 | * return -1 on failure. |
349 | * return 0 on success. | 349 | * return 0 on success. |
350 | */ | 350 | */ |
351 | static int handle_crypto_handshake(Net_Crypto *c, uint8_t *nonce, uint8_t *session_pk, uint8_t *peer_real_pk, | 351 | static int handle_crypto_handshake(const Net_Crypto *c, uint8_t *nonce, uint8_t *session_pk, uint8_t *peer_real_pk, |
352 | uint8_t *dht_public_key, uint8_t *cookie, const uint8_t *packet, uint32_t length, uint8_t *expected_real_pk) | 352 | uint8_t *dht_public_key, uint8_t *cookie, const uint8_t *packet, uint32_t length, const uint8_t *expected_real_pk) |
353 | { | 353 | { |
354 | if (length != HANDSHAKE_PACKET_LENGTH) | 354 | if (length != HANDSHAKE_PACKET_LENGTH) |
355 | return -1; | 355 | return -1; |
@@ -386,7 +386,7 @@ static int handle_crypto_handshake(Net_Crypto *c, uint8_t *nonce, uint8_t *sessi | |||
386 | } | 386 | } |
387 | 387 | ||
388 | 388 | ||
389 | static Crypto_Connection *get_crypto_connection(Net_Crypto *c, int crypt_connection_id) | 389 | static Crypto_Connection *get_crypto_connection(const Net_Crypto *c, int crypt_connection_id) |
390 | { | 390 | { |
391 | if (crypt_connection_id_not_valid(c, crypt_connection_id)) | 391 | if (crypt_connection_id_not_valid(c, crypt_connection_id)) |
392 | return 0; | 392 | return 0; |
@@ -400,7 +400,7 @@ static Crypto_Connection *get_crypto_connection(Net_Crypto *c, int crypt_connect | |||
400 | * return -1 on failure. | 400 | * return -1 on failure. |
401 | * return 0 on success. | 401 | * return 0 on success. |
402 | */ | 402 | */ |
403 | static int send_packet_to(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint16_t length) | 403 | static int send_packet_to(const Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length) |
404 | { | 404 | { |
405 | //TODO TCP, etc... | 405 | //TODO TCP, etc... |
406 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 406 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
@@ -456,7 +456,7 @@ static int send_packet_to(Net_Crypto *c, int crypt_connection_id, uint8_t *data, | |||
456 | /* Return number of packets in array | 456 | /* Return number of packets in array |
457 | * Note that holes are counted too. | 457 | * Note that holes are counted too. |
458 | */ | 458 | */ |
459 | static uint32_t num_packets_array(Packets_Array *array) | 459 | static uint32_t num_packets_array(const Packets_Array *array) |
460 | { | 460 | { |
461 | return array->buffer_end - array->buffer_start; | 461 | return array->buffer_end - array->buffer_start; |
462 | } | 462 | } |
@@ -466,7 +466,7 @@ static uint32_t num_packets_array(Packets_Array *array) | |||
466 | * return -1 on failure. | 466 | * return -1 on failure. |
467 | * return 0 on success. | 467 | * return 0 on success. |
468 | */ | 468 | */ |
469 | static int add_data_to_buffer(Packets_Array *array, uint32_t number, Packet_Data *data) | 469 | static int add_data_to_buffer(Packets_Array *array, uint32_t number, const Packet_Data *data) |
470 | { | 470 | { |
471 | if (number - array->buffer_start > CRYPTO_PACKET_BUFFER_SIZE) | 471 | if (number - array->buffer_start > CRYPTO_PACKET_BUFFER_SIZE) |
472 | return -1; | 472 | return -1; |
@@ -496,7 +496,7 @@ static int add_data_to_buffer(Packets_Array *array, uint32_t number, Packet_Data | |||
496 | * return 0 if data at number is empty. | 496 | * return 0 if data at number is empty. |
497 | * return 1 if data pointer was put in data. | 497 | * return 1 if data pointer was put in data. |
498 | */ | 498 | */ |
499 | static int get_data_pointer(Packets_Array *array, Packet_Data **data, uint32_t number) | 499 | static int get_data_pointer(const Packets_Array *array, Packet_Data **data, uint32_t number) |
500 | { | 500 | { |
501 | uint32_t num_spots = array->buffer_end - array->buffer_start; | 501 | uint32_t num_spots = array->buffer_end - array->buffer_start; |
502 | 502 | ||
@@ -517,7 +517,7 @@ static int get_data_pointer(Packets_Array *array, Packet_Data **data, uint32_t n | |||
517 | * return -1 on failure. | 517 | * return -1 on failure. |
518 | * return packet number on success. | 518 | * return packet number on success. |
519 | */ | 519 | */ |
520 | static int64_t add_data_end_of_buffer(Packets_Array *array, Packet_Data *data) | 520 | static int64_t add_data_end_of_buffer(Packets_Array *array, const Packet_Data *data) |
521 | { | 521 | { |
522 | if (num_packets_array(array) >= CRYPTO_PACKET_BUFFER_SIZE) | 522 | if (num_packets_array(array) >= CRYPTO_PACKET_BUFFER_SIZE) |
523 | return -1; | 523 | return -1; |
@@ -607,7 +607,7 @@ static int set_buffer_end(Packets_Array *array, uint32_t number) | |||
607 | * return -1 on failure. | 607 | * return -1 on failure. |
608 | * return length of packet on success. | 608 | * return length of packet on success. |
609 | */ | 609 | */ |
610 | static int generate_request_packet(uint8_t *data, uint16_t length, Packets_Array *recv_array) | 610 | static int generate_request_packet(uint8_t *data, uint16_t length, const Packets_Array *recv_array) |
611 | { | 611 | { |
612 | if (length == 0) | 612 | if (length == 0) |
613 | return -1; | 613 | return -1; |
@@ -656,7 +656,7 @@ static int generate_request_packet(uint8_t *data, uint16_t length, Packets_Array | |||
656 | * return -1 on failure. | 656 | * return -1 on failure. |
657 | * return number of requested packets on success. | 657 | * return number of requested packets on success. |
658 | */ | 658 | */ |
659 | static int handle_request_packet(Packets_Array *send_array, uint8_t *data, uint16_t length) | 659 | static int handle_request_packet(Packets_Array *send_array, const uint8_t *data, uint16_t length) |
660 | { | 660 | { |
661 | if (length < 1) | 661 | if (length < 1) |
662 | return -1; | 662 | return -1; |
@@ -718,7 +718,7 @@ static int handle_request_packet(Packets_Array *send_array, uint8_t *data, uint1 | |||
718 | * return -1 on failure. | 718 | * return -1 on failure. |
719 | * return 0 on success. | 719 | * return 0 on success. |
720 | */ | 720 | */ |
721 | static int send_data_packet(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint16_t length) | 721 | static int send_data_packet(const Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length) |
722 | { | 722 | { |
723 | if (length == 0 || length + (1 + sizeof(uint16_t) + crypto_box_MACBYTES) > MAX_CRYPTO_PACKET_SIZE) | 723 | if (length == 0 || length + (1 + sizeof(uint16_t) + crypto_box_MACBYTES) > MAX_CRYPTO_PACKET_SIZE) |
724 | return -1; | 724 | return -1; |
@@ -750,8 +750,8 @@ static int send_data_packet(Net_Crypto *c, int crypt_connection_id, uint8_t *dat | |||
750 | * return -1 on failure. | 750 | * return -1 on failure. |
751 | * return 0 on success. | 751 | * return 0 on success. |
752 | */ | 752 | */ |
753 | static int send_data_packet_helper(Net_Crypto *c, int crypt_connection_id, uint32_t buffer_start, uint32_t num, | 753 | static int send_data_packet_helper(const Net_Crypto *c, int crypt_connection_id, uint32_t buffer_start, uint32_t num, |
754 | uint8_t *data, uint32_t length) | 754 | const uint8_t *data, uint32_t length) |
755 | { | 755 | { |
756 | if (length == 0 || length > MAX_CRYPTO_DATA_SIZE) | 756 | if (length == 0 || length > MAX_CRYPTO_DATA_SIZE) |
757 | return -1; | 757 | return -1; |
@@ -771,7 +771,7 @@ static int send_data_packet_helper(Net_Crypto *c, int crypt_connection_id, uint3 | |||
771 | /* return -1 if data could not be put in packet queue. | 771 | /* return -1 if data could not be put in packet queue. |
772 | * return positive packet number if data was put into the queue. | 772 | * return positive packet number if data was put into the queue. |
773 | */ | 773 | */ |
774 | static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length) | 774 | static int64_t send_lossless_packet(const Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint32_t length) |
775 | { | 775 | { |
776 | if (length == 0 || length > MAX_CRYPTO_DATA_SIZE) | 776 | if (length == 0 || length > MAX_CRYPTO_DATA_SIZE) |
777 | return -1; | 777 | return -1; |
@@ -781,8 +781,29 @@ static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, uint | |||
781 | if (conn == 0) | 781 | if (conn == 0) |
782 | return -1; | 782 | return -1; |
783 | 783 | ||
784 | uint64_t temp_time = current_time_monotonic(); | ||
785 | |||
786 | /* If last packet send failed, try to send packet again. | ||
787 | If sending it fails we won't be able to send the new packet. */ | ||
788 | if (conn->maximum_speed_reached) { | ||
789 | Packet_Data *dt = NULL; | ||
790 | uint32_t packet_num = conn->send_array.buffer_end - 1; | ||
791 | int ret = get_data_pointer(&conn->send_array, &dt, packet_num); | ||
792 | |||
793 | if (ret == 1) { | ||
794 | if (!dt->time) { | ||
795 | if (send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, packet_num, dt->data, | ||
796 | dt->length) != 0) { | ||
797 | return -1; | ||
798 | } | ||
799 | |||
800 | dt->time = temp_time; | ||
801 | } | ||
802 | } | ||
803 | } | ||
804 | |||
784 | Packet_Data dt; | 805 | Packet_Data dt; |
785 | dt.time = current_time_monotonic(); | 806 | dt.time = temp_time; |
786 | dt.length = length; | 807 | dt.length = length; |
787 | memcpy(dt.data, data, length); | 808 | memcpy(dt.data, data, length); |
788 | int64_t packet_num = add_data_end_of_buffer(&conn->send_array, &dt); | 809 | int64_t packet_num = add_data_end_of_buffer(&conn->send_array, &dt); |
@@ -790,8 +811,15 @@ static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, uint | |||
790 | if (packet_num == -1) | 811 | if (packet_num == -1) |
791 | return -1; | 812 | return -1; |
792 | 813 | ||
793 | if (send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, packet_num, data, length) != 0) | 814 | conn->maximum_speed_reached = 0; |
815 | |||
816 | if (send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, packet_num, data, length) != 0) { | ||
817 | Packet_Data *dt1 = NULL; | ||
818 | get_data_pointer(&conn->send_array, &dt1, packet_num); | ||
819 | dt1->time = 0; | ||
820 | conn->maximum_speed_reached = 1; | ||
794 | fprintf(stderr, "send_data_packet failed\n"); | 821 | fprintf(stderr, "send_data_packet failed\n"); |
822 | } | ||
795 | 823 | ||
796 | return packet_num; | 824 | return packet_num; |
797 | } | 825 | } |
@@ -799,7 +827,7 @@ static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, uint | |||
799 | /* Get the lowest 2 bytes from the nonce and convert | 827 | /* Get the lowest 2 bytes from the nonce and convert |
800 | * them to host byte format before returning them. | 828 | * them to host byte format before returning them. |
801 | */ | 829 | */ |
802 | static uint16_t get_nonce_uint16(uint8_t *nonce) | 830 | static uint16_t get_nonce_uint16(const uint8_t *nonce) |
803 | { | 831 | { |
804 | uint16_t num; | 832 | uint16_t num; |
805 | memcpy(&num, nonce + (crypto_box_NONCEBYTES - sizeof(uint16_t)), sizeof(uint16_t)); | 833 | memcpy(&num, nonce + (crypto_box_NONCEBYTES - sizeof(uint16_t)), sizeof(uint16_t)); |
@@ -815,7 +843,7 @@ static uint16_t get_nonce_uint16(uint8_t *nonce) | |||
815 | * return -1 on failure. | 843 | * return -1 on failure. |
816 | * return length of data on success. | 844 | * return length of data on success. |
817 | */ | 845 | */ |
818 | static int handle_data_packet(Net_Crypto *c, int crypt_connection_id, uint8_t *data, const uint8_t *packet, | 846 | static int handle_data_packet(const Net_Crypto *c, int crypt_connection_id, uint8_t *data, const uint8_t *packet, |
819 | uint16_t length) | 847 | uint16_t length) |
820 | { | 848 | { |
821 | if (length <= (1 + sizeof(uint16_t) + crypto_box_MACBYTES) || length > MAX_CRYPTO_PACKET_SIZE) | 849 | if (length <= (1 + sizeof(uint16_t) + crypto_box_MACBYTES) || length > MAX_CRYPTO_PACKET_SIZE) |
@@ -852,7 +880,7 @@ static int handle_data_packet(Net_Crypto *c, int crypt_connection_id, uint8_t *d | |||
852 | * return -1 on failure. | 880 | * return -1 on failure. |
853 | * return 0 on success. | 881 | * return 0 on success. |
854 | */ | 882 | */ |
855 | static int send_request_packet(Net_Crypto *c, int crypt_connection_id) | 883 | static int send_request_packet(const Net_Crypto *c, int crypt_connection_id) |
856 | { | 884 | { |
857 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 885 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
858 | 886 | ||
@@ -874,7 +902,7 @@ static int send_request_packet(Net_Crypto *c, int crypt_connection_id) | |||
874 | * return -1 on failure. | 902 | * return -1 on failure. |
875 | * return number of packets sent on success. | 903 | * return number of packets sent on success. |
876 | */ | 904 | */ |
877 | static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint16_t max_num) | 905 | static int send_requested_packets(const Net_Crypto *c, int crypt_connection_id, uint16_t max_num) |
878 | { | 906 | { |
879 | if (max_num == 0) | 907 | if (max_num == 0) |
880 | return -1; | 908 | return -1; |
@@ -920,7 +948,7 @@ static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint16 | |||
920 | * return -1 on failure. | 948 | * return -1 on failure. |
921 | * return 0 on success. | 949 | * return 0 on success. |
922 | */ | 950 | */ |
923 | static int new_temp_packet(Net_Crypto *c, int crypt_connection_id, uint8_t *packet, uint16_t length) | 951 | static int new_temp_packet(const Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length) |
924 | { | 952 | { |
925 | if (length == 0 || length > MAX_CRYPTO_PACKET_SIZE) | 953 | if (length == 0 || length > MAX_CRYPTO_PACKET_SIZE) |
926 | return -1; | 954 | return -1; |
@@ -951,7 +979,7 @@ static int new_temp_packet(Net_Crypto *c, int crypt_connection_id, uint8_t *pack | |||
951 | * return -1 on failure. | 979 | * return -1 on failure. |
952 | * return 0 on success. | 980 | * return 0 on success. |
953 | */ | 981 | */ |
954 | static int clear_temp_packet(Net_Crypto *c, int crypt_connection_id) | 982 | static int clear_temp_packet(const Net_Crypto *c, int crypt_connection_id) |
955 | { | 983 | { |
956 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 984 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
957 | 985 | ||
@@ -974,7 +1002,7 @@ static int clear_temp_packet(Net_Crypto *c, int crypt_connection_id) | |||
974 | * return -1 on failure. | 1002 | * return -1 on failure. |
975 | * return 0 on success. | 1003 | * return 0 on success. |
976 | */ | 1004 | */ |
977 | static int send_temp_packet(Net_Crypto *c, int crypt_connection_id) | 1005 | static int send_temp_packet(const Net_Crypto *c, int crypt_connection_id) |
978 | { | 1006 | { |
979 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 1007 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
980 | 1008 | ||
@@ -998,7 +1026,8 @@ static int send_temp_packet(Net_Crypto *c, int crypt_connection_id) | |||
998 | * return -1 on failure. | 1026 | * return -1 on failure. |
999 | * return 0 on success. | 1027 | * return 0 on success. |
1000 | */ | 1028 | */ |
1001 | static int create_send_handshake(Net_Crypto *c, int crypt_connection_id, uint8_t *cookie, uint8_t *dht_public_key) | 1029 | static int create_send_handshake(const Net_Crypto *c, int crypt_connection_id, const uint8_t *cookie, |
1030 | const uint8_t *dht_public_key) | ||
1002 | { | 1031 | { |
1003 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 1032 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
1004 | 1033 | ||
@@ -1023,7 +1052,7 @@ static int create_send_handshake(Net_Crypto *c, int crypt_connection_id, uint8_t | |||
1023 | * return -1 on failure. | 1052 | * return -1 on failure. |
1024 | * return 0 on success. | 1053 | * return 0 on success. |
1025 | */ | 1054 | */ |
1026 | static int send_kill_packet(Net_Crypto *c, int crypt_connection_id) | 1055 | static int send_kill_packet(const Net_Crypto *c, int crypt_connection_id) |
1027 | { | 1056 | { |
1028 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 1057 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
1029 | 1058 | ||
@@ -1040,7 +1069,8 @@ static int send_kill_packet(Net_Crypto *c, int crypt_connection_id) | |||
1040 | * return -1 on failure. | 1069 | * return -1 on failure. |
1041 | * return 0 on success. | 1070 | * return 0 on success. |
1042 | */ | 1071 | */ |
1043 | static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length) | 1072 | static int handle_data_packet_helper(const Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, |
1073 | uint16_t length) | ||
1044 | { | 1074 | { |
1045 | if (length > MAX_CRYPTO_PACKET_SIZE || length <= CRYPTO_DATA_PACKET_MIN_SIZE) | 1075 | if (length > MAX_CRYPTO_PACKET_SIZE || length <= CRYPTO_DATA_PACKET_MIN_SIZE) |
1046 | return -1; | 1076 | return -1; |
@@ -1289,7 +1319,7 @@ static int wipe_crypto_connection(Net_Crypto *c, int crypt_connection_id) | |||
1289 | * return -1 if there are no connections like we are looking for. | 1319 | * return -1 if there are no connections like we are looking for. |
1290 | * return id if it found it. | 1320 | * return id if it found it. |
1291 | */ | 1321 | */ |
1292 | static int getcryptconnection_id(Net_Crypto *c, uint8_t *public_key) | 1322 | static int getcryptconnection_id(const Net_Crypto *c, const uint8_t *public_key) |
1293 | { | 1323 | { |
1294 | uint32_t i; | 1324 | uint32_t i; |
1295 | 1325 | ||
@@ -1307,7 +1337,7 @@ static int getcryptconnection_id(Net_Crypto *c, uint8_t *public_key) | |||
1307 | * return -1 if there are no connections like we are looking for. | 1337 | * return -1 if there are no connections like we are looking for. |
1308 | * return id if it found it. | 1338 | * return id if it found it. |
1309 | */ | 1339 | */ |
1310 | static int getcryptconnection_id_dht_pubkey(Net_Crypto *c, uint8_t *dht_public_key) | 1340 | static int getcryptconnection_id_dht_pubkey(const Net_Crypto *c, const uint8_t *dht_public_key) |
1311 | { | 1341 | { |
1312 | uint32_t i; | 1342 | uint32_t i; |
1313 | 1343 | ||
@@ -1465,7 +1495,7 @@ int accept_crypto_connection(Net_Crypto *c, New_Connection *n_c) | |||
1465 | * return -1 on failure. | 1495 | * return -1 on failure. |
1466 | * return connection id on success. | 1496 | * return connection id on success. |
1467 | */ | 1497 | */ |
1468 | int new_crypto_connection(Net_Crypto *c, uint8_t *real_public_key) | 1498 | int new_crypto_connection(Net_Crypto *c, const uint8_t *real_public_key) |
1469 | { | 1499 | { |
1470 | int crypt_connection_id = getcryptconnection_id(c, real_public_key); | 1500 | int crypt_connection_id = getcryptconnection_id(c, real_public_key); |
1471 | 1501 | ||
@@ -1495,7 +1525,7 @@ int new_crypto_connection(Net_Crypto *c, uint8_t *real_public_key) | |||
1495 | * return -1 on failure. | 1525 | * return -1 on failure. |
1496 | * return 0 on success. | 1526 | * return 0 on success. |
1497 | */ | 1527 | */ |
1498 | static int disconnect_peer_tcp(Net_Crypto *c, int crypt_connection_id) | 1528 | static int disconnect_peer_tcp(const Net_Crypto *c, int crypt_connection_id) |
1499 | { | 1529 | { |
1500 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 1530 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
1501 | 1531 | ||
@@ -1520,7 +1550,7 @@ static int disconnect_peer_tcp(Net_Crypto *c, int crypt_connection_id) | |||
1520 | * return -1 on failure. | 1550 | * return -1 on failure. |
1521 | * return 0 on success. | 1551 | * return 0 on success. |
1522 | */ | 1552 | */ |
1523 | static int connect_peer_tcp(Net_Crypto *c, int crypt_connection_id) | 1553 | static int connect_peer_tcp(const Net_Crypto *c, int crypt_connection_id) |
1524 | { | 1554 | { |
1525 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 1555 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
1526 | 1556 | ||
@@ -1545,7 +1575,7 @@ static int connect_peer_tcp(Net_Crypto *c, int crypt_connection_id) | |||
1545 | * return 0 on failure (no key copied). | 1575 | * return 0 on failure (no key copied). |
1546 | * return timestamp on success (key copied). | 1576 | * return timestamp on success (key copied). |
1547 | */ | 1577 | */ |
1548 | uint64_t get_connection_dht_key(Net_Crypto *c, int crypt_connection_id, uint8_t *dht_public_key) | 1578 | uint64_t get_connection_dht_key(const Net_Crypto *c, int crypt_connection_id, uint8_t *dht_public_key) |
1549 | { | 1579 | { |
1550 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 1580 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
1551 | 1581 | ||
@@ -1567,7 +1597,8 @@ uint64_t get_connection_dht_key(Net_Crypto *c, int crypt_connection_id, uint8_t | |||
1567 | * return -1 on failure. | 1597 | * return -1 on failure. |
1568 | * return 0 on success. | 1598 | * return 0 on success. |
1569 | */ | 1599 | */ |
1570 | int set_connection_dht_public_key(Net_Crypto *c, int crypt_connection_id, uint8_t *dht_public_key, uint64_t timestamp) | 1600 | int set_connection_dht_public_key(const Net_Crypto *c, int crypt_connection_id, const uint8_t *dht_public_key, |
1601 | uint64_t timestamp) | ||
1571 | { | 1602 | { |
1572 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 1603 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
1573 | 1604 | ||
@@ -1628,7 +1659,7 @@ int set_direct_ip_port(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port) | |||
1628 | return -1; | 1659 | return -1; |
1629 | } | 1660 | } |
1630 | 1661 | ||
1631 | static int tcp_response_callback(void *object, uint8_t connection_id, uint8_t *public_key) | 1662 | static int tcp_response_callback(void *object, uint8_t connection_id, const uint8_t *public_key) |
1632 | { | 1663 | { |
1633 | TCP_Client_Connection *TCP_con = object; | 1664 | TCP_Client_Connection *TCP_con = object; |
1634 | Net_Crypto *c = TCP_con->net_crypto_pointer; | 1665 | Net_Crypto *c = TCP_con->net_crypto_pointer; |
@@ -1695,7 +1726,7 @@ static int tcp_status_callback(void *object, uint32_t number, uint8_t connection | |||
1695 | return 0; | 1726 | return 0; |
1696 | } | 1727 | } |
1697 | 1728 | ||
1698 | static int tcp_data_callback(void *object, uint32_t number, uint8_t connection_id, uint8_t *data, uint16_t length) | 1729 | static int tcp_data_callback(void *object, uint32_t number, uint8_t connection_id, const uint8_t *data, uint16_t length) |
1699 | { | 1730 | { |
1700 | 1731 | ||
1701 | if (length == 0) | 1732 | if (length == 0) |
@@ -1720,7 +1751,7 @@ static int tcp_data_callback(void *object, uint32_t number, uint8_t connection_i | |||
1720 | return 0; | 1751 | return 0; |
1721 | } | 1752 | } |
1722 | 1753 | ||
1723 | static int tcp_oob_callback(void *object, uint8_t *public_key, uint8_t *data, uint16_t length) | 1754 | static int tcp_oob_callback(void *object, const uint8_t *public_key, const uint8_t *data, uint16_t length) |
1724 | { | 1755 | { |
1725 | if (length == 0 || length > MAX_CRYPTO_PACKET_SIZE) | 1756 | if (length == 0 || length > MAX_CRYPTO_PACKET_SIZE) |
1726 | return -1; | 1757 | return -1; |
@@ -1762,7 +1793,7 @@ static int tcp_oob_callback(void *object, uint8_t *public_key, uint8_t *data, ui | |||
1762 | * return -1 if it can't. | 1793 | * return -1 if it can't. |
1763 | * return 0 if it can. | 1794 | * return 0 if it can. |
1764 | */ | 1795 | */ |
1765 | static int tcp_connection_check(Net_Crypto *c, uint8_t *public_key) | 1796 | static int tcp_connection_check(const Net_Crypto *c, const uint8_t *public_key) |
1766 | { | 1797 | { |
1767 | uint32_t i; | 1798 | uint32_t i; |
1768 | 1799 | ||
@@ -1797,7 +1828,7 @@ static int tcp_connection_check(Net_Crypto *c, uint8_t *public_key) | |||
1797 | * return 0 if it was added. | 1828 | * return 0 if it was added. |
1798 | * return -1 if it wasn't. | 1829 | * return -1 if it wasn't. |
1799 | */ | 1830 | */ |
1800 | int add_tcp_relay_peer(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port, uint8_t *public_key) | 1831 | int add_tcp_relay_peer(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port, const uint8_t *public_key) |
1801 | { | 1832 | { |
1802 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 1833 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
1803 | 1834 | ||
@@ -1850,7 +1881,7 @@ int add_tcp_relay_peer(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port, | |||
1850 | * return 0 if it was added. | 1881 | * return 0 if it was added. |
1851 | * return -1 if it wasn't. | 1882 | * return -1 if it wasn't. |
1852 | */ | 1883 | */ |
1853 | int add_tcp_relay(Net_Crypto *c, IP_Port ip_port, uint8_t *public_key) | 1884 | int add_tcp_relay(Net_Crypto *c, IP_Port ip_port, const uint8_t *public_key) |
1854 | { | 1885 | { |
1855 | if (ip_port.ip.family == TCP_INET) { | 1886 | if (ip_port.ip.family == TCP_INET) { |
1856 | ip_port.ip.family = AF_INET; | 1887 | ip_port.ip.family = AF_INET; |
@@ -1882,7 +1913,7 @@ int add_tcp_relay(Net_Crypto *c, IP_Port ip_port, uint8_t *public_key) | |||
1882 | * return number of relays copied to tcp_relays on success. | 1913 | * return number of relays copied to tcp_relays on success. |
1883 | * return 0 on failure. | 1914 | * return 0 on failure. |
1884 | */ | 1915 | */ |
1885 | unsigned int copy_connected_tcp_relays(Net_Crypto *c, Node_format *tcp_relays, uint16_t num) | 1916 | unsigned int copy_connected_tcp_relays(const Net_Crypto *c, Node_format *tcp_relays, uint16_t num) |
1886 | { | 1917 | { |
1887 | if (num == 0) | 1918 | if (num == 0) |
1888 | return 0; | 1919 | return 0; |
@@ -2046,8 +2077,8 @@ static void clear_disconnected_tcp(Net_Crypto *c) | |||
2046 | * return -1 on failure. | 2077 | * return -1 on failure. |
2047 | * return 0 on success. | 2078 | * return 0 on success. |
2048 | */ | 2079 | */ |
2049 | int connection_status_handler(Net_Crypto *c, int crypt_connection_id, int (*connection_status_callback)(void *object, | 2080 | int connection_status_handler(const Net_Crypto *c, int crypt_connection_id, |
2050 | int id, uint8_t status), void *object, int id) | 2081 | int (*connection_status_callback)(void *object, int id, uint8_t status), void *object, int id) |
2051 | { | 2082 | { |
2052 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 2083 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
2053 | 2084 | ||
@@ -2068,7 +2099,7 @@ int connection_status_handler(Net_Crypto *c, int crypt_connection_id, int (*conn | |||
2068 | * return -1 on failure. | 2099 | * return -1 on failure. |
2069 | * return 0 on success. | 2100 | * return 0 on success. |
2070 | */ | 2101 | */ |
2071 | int connection_data_handler(Net_Crypto *c, int crypt_connection_id, int (*connection_data_callback)(void *object, | 2102 | int connection_data_handler(const Net_Crypto *c, int crypt_connection_id, int (*connection_data_callback)(void *object, |
2072 | int id, uint8_t *data, uint16_t length), void *object, int id) | 2103 | int id, uint8_t *data, uint16_t length), void *object, int id) |
2073 | { | 2104 | { |
2074 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 2105 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
@@ -2091,7 +2122,7 @@ int connection_data_handler(Net_Crypto *c, int crypt_connection_id, int (*connec | |||
2091 | * return 0 on success. | 2122 | * return 0 on success. |
2092 | */ | 2123 | */ |
2093 | int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id, | 2124 | int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id, |
2094 | int (*connection_lossy_data_callback)(void *object, int id, uint8_t *data, uint16_t length), void *object, int id) | 2125 | int (*connection_lossy_data_callback)(void *object, int id, const uint8_t *data, uint16_t length), void *object, int id) |
2095 | { | 2126 | { |
2096 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 2127 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
2097 | 2128 | ||
@@ -2109,7 +2140,7 @@ int connection_lossy_data_handler(Net_Crypto *c, int crypt_connection_id, | |||
2109 | * return -1 on failure. | 2140 | * return -1 on failure. |
2110 | * return connection id on success. | 2141 | * return connection id on success. |
2111 | */ | 2142 | */ |
2112 | static int crypto_id_ip_port(Net_Crypto *c, IP_Port ip_port) | 2143 | static int crypto_id_ip_port(const Net_Crypto *c, IP_Port ip_port) |
2113 | { | 2144 | { |
2114 | return bs_list_find(&c->ip_port_list, &ip_port); | 2145 | return bs_list_find(&c->ip_port_list, &ip_port); |
2115 | } | 2146 | } |
@@ -2345,7 +2376,7 @@ static void send_crypto_packets(Net_Crypto *c) | |||
2345 | /* returns the number of packet slots left in the sendbuffer. | 2376 | /* returns the number of packet slots left in the sendbuffer. |
2346 | * return 0 if failure. | 2377 | * return 0 if failure. |
2347 | */ | 2378 | */ |
2348 | uint32_t crypto_num_free_sendqueue_slots(Net_Crypto *c, int crypt_connection_id) | 2379 | uint32_t crypto_num_free_sendqueue_slots(const Net_Crypto *c, int crypt_connection_id) |
2349 | { | 2380 | { |
2350 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 2381 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
2351 | 2382 | ||
@@ -2362,7 +2393,7 @@ uint32_t crypto_num_free_sendqueue_slots(Net_Crypto *c, int crypt_connection_id) | |||
2362 | * | 2393 | * |
2363 | * The first byte of data must be in the CRYPTO_RESERVED_PACKETS to PACKET_ID_LOSSY_RANGE_START range. | 2394 | * The first byte of data must be in the CRYPTO_RESERVED_PACKETS to PACKET_ID_LOSSY_RANGE_START range. |
2364 | */ | 2395 | */ |
2365 | int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length) | 2396 | int64_t write_cryptpacket(const Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint32_t length) |
2366 | { | 2397 | { |
2367 | if (length == 0) | 2398 | if (length == 0) |
2368 | return -1; | 2399 | return -1; |
@@ -2399,7 +2430,7 @@ int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, | |||
2399 | * | 2430 | * |
2400 | * Sends a lossy cryptopacket. (first byte must in the PACKET_ID_LOSSY_RANGE_*) | 2431 | * Sends a lossy cryptopacket. (first byte must in the PACKET_ID_LOSSY_RANGE_*) |
2401 | */ | 2432 | */ |
2402 | int send_lossy_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length) | 2433 | int send_lossy_cryptpacket(const Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint32_t length) |
2403 | { | 2434 | { |
2404 | if (length == 0 || length > MAX_CRYPTO_DATA_SIZE) | 2435 | if (length == 0 || length > MAX_CRYPTO_DATA_SIZE) |
2405 | return -1; | 2436 | return -1; |
@@ -2441,7 +2472,7 @@ int crypto_kill(Net_Crypto *c, int crypt_connection_id) | |||
2441 | * | 2472 | * |
2442 | * sets direct_connected to 1 if connection connects directly to other, 0 if it isn't. | 2473 | * sets direct_connected to 1 if connection connects directly to other, 0 if it isn't. |
2443 | */ | 2474 | */ |
2444 | unsigned int crypto_connection_status(Net_Crypto *c, int crypt_connection_id, uint8_t *direct_connected) | 2475 | unsigned int crypto_connection_status(const Net_Crypto *c, int crypt_connection_id, uint8_t *direct_connected) |
2445 | { | 2476 | { |
2446 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | 2477 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); |
2447 | 2478 | ||
@@ -2464,7 +2495,7 @@ void new_keys(Net_Crypto *c) | |||
2464 | /* Save the public and private keys to the keys array. | 2495 | /* Save the public and private keys to the keys array. |
2465 | * Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES. | 2496 | * Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES. |
2466 | */ | 2497 | */ |
2467 | void save_keys(Net_Crypto *c, uint8_t *keys) | 2498 | void save_keys(const Net_Crypto *c, uint8_t *keys) |
2468 | { | 2499 | { |
2469 | memcpy(keys, c->self_public_key, crypto_box_PUBLICKEYBYTES); | 2500 | memcpy(keys, c->self_public_key, crypto_box_PUBLICKEYBYTES); |
2470 | memcpy(keys + crypto_box_PUBLICKEYBYTES, c->self_secret_key, crypto_box_SECRETKEYBYTES); | 2501 | memcpy(keys + crypto_box_PUBLICKEYBYTES, c->self_secret_key, crypto_box_SECRETKEYBYTES); |
@@ -2552,7 +2583,7 @@ static void kill_timedout(Net_Crypto *c) | |||
2552 | 2583 | ||
2553 | /* return the optimal interval in ms for running do_net_crypto. | 2584 | /* return the optimal interval in ms for running do_net_crypto. |
2554 | */ | 2585 | */ |
2555 | uint32_t crypto_run_interval(Net_Crypto *c) | 2586 | uint32_t crypto_run_interval(const Net_Crypto *c) |
2556 | { | 2587 | { |
2557 | return c->current_sleep_time; | 2588 | return c->current_sleep_time; |
2558 | } | 2589 | } |