diff options
author | iphydf <iphydf@users.noreply.github.com> | 2018-06-23 17:16:28 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2018-06-24 19:17:32 +0000 |
commit | 8e00294b3cb9808ce55160240454359638178275 (patch) | |
tree | 095a7708b3fe3566951ebe7b068ee66a8beed3c1 /toxcore/net_crypto.c | |
parent | 5a8790eab0ed9421445adce40526462b9e6742c3 (diff) |
Add Logger to various net_crypto functions.
In preparation for adding log statements.
Also, fix an uninitialised variable warning in cppcheck.
Diffstat (limited to 'toxcore/net_crypto.c')
-rw-r--r-- | toxcore/net_crypto.c | 63 |
1 files changed, 32 insertions, 31 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index fbf41552..f82cef69 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c | |||
@@ -130,7 +130,7 @@ typedef struct { | |||
130 | } Crypto_Connection; | 130 | } Crypto_Connection; |
131 | 131 | ||
132 | struct Net_Crypto { | 132 | struct Net_Crypto { |
133 | Logger *log; | 133 | const Logger *log; |
134 | 134 | ||
135 | DHT *dht; | 135 | DHT *dht; |
136 | TCP_Connections *tcp_c; | 136 | TCP_Connections *tcp_c; |
@@ -245,7 +245,7 @@ static int create_cookie_request(const Net_Crypto *c, uint8_t *packet, uint8_t * | |||
245 | * return -1 on failure. | 245 | * return -1 on failure. |
246 | * return 0 on success. | 246 | * return 0 on success. |
247 | */ | 247 | */ |
248 | static int create_cookie(uint8_t *cookie, const uint8_t *bytes, const uint8_t *encryption_key) | 248 | static int create_cookie(const Logger *log, uint8_t *cookie, const uint8_t *bytes, const uint8_t *encryption_key) |
249 | { | 249 | { |
250 | uint8_t contents[COOKIE_CONTENTS_LENGTH]; | 250 | uint8_t contents[COOKIE_CONTENTS_LENGTH]; |
251 | uint64_t temp_time = unix_time(); | 251 | uint64_t temp_time = unix_time(); |
@@ -266,7 +266,7 @@ static int create_cookie(uint8_t *cookie, const uint8_t *bytes, const uint8_t *e | |||
266 | * return -1 on failure. | 266 | * return -1 on failure. |
267 | * return 0 on success. | 267 | * return 0 on success. |
268 | */ | 268 | */ |
269 | static int open_cookie(uint8_t *bytes, const uint8_t *cookie, const uint8_t *encryption_key) | 269 | static int open_cookie(const Logger *log, uint8_t *bytes, const uint8_t *cookie, const uint8_t *encryption_key) |
270 | { | 270 | { |
271 | uint8_t contents[COOKIE_CONTENTS_LENGTH]; | 271 | uint8_t contents[COOKIE_CONTENTS_LENGTH]; |
272 | int len = decrypt_data_symmetric(encryption_key, cookie, cookie + CRYPTO_NONCE_SIZE, | 272 | int len = decrypt_data_symmetric(encryption_key, cookie, cookie + CRYPTO_NONCE_SIZE, |
@@ -304,7 +304,7 @@ static int create_cookie_response(const Net_Crypto *c, uint8_t *packet, const ui | |||
304 | memcpy(cookie_plain + CRYPTO_PUBLIC_KEY_SIZE, dht_public_key, CRYPTO_PUBLIC_KEY_SIZE); | 304 | memcpy(cookie_plain + CRYPTO_PUBLIC_KEY_SIZE, dht_public_key, CRYPTO_PUBLIC_KEY_SIZE); |
305 | uint8_t plain[COOKIE_LENGTH + sizeof(uint64_t)]; | 305 | uint8_t plain[COOKIE_LENGTH + sizeof(uint64_t)]; |
306 | 306 | ||
307 | if (create_cookie(plain, cookie_plain, c->secret_symmetric_key) != 0) { | 307 | if (create_cookie(c->log, plain, cookie_plain, c->secret_symmetric_key) != 0) { |
308 | return -1; | 308 | return -1; |
309 | } | 309 | } |
310 | 310 | ||
@@ -431,7 +431,8 @@ static int tcp_oob_handle_cookie_request(const Net_Crypto *c, unsigned int tcp_c | |||
431 | * return -1 on failure. | 431 | * return -1 on failure. |
432 | * return COOKIE_LENGTH on success. | 432 | * return COOKIE_LENGTH on success. |
433 | */ | 433 | */ |
434 | static int handle_cookie_response(uint8_t *cookie, uint64_t *number, const uint8_t *packet, uint16_t length, | 434 | static int handle_cookie_response(const Logger *log, uint8_t *cookie, uint64_t *number, |
435 | const uint8_t *packet, uint16_t length, | ||
435 | const uint8_t *shared_key) | 436 | const uint8_t *shared_key) |
436 | { | 437 | { |
437 | if (length != COOKIE_RESPONSE_LENGTH) { | 438 | if (length != COOKIE_RESPONSE_LENGTH) { |
@@ -471,7 +472,7 @@ static int create_crypto_handshake(const Net_Crypto *c, uint8_t *packet, const u | |||
471 | memcpy(cookie_plain, peer_real_pk, CRYPTO_PUBLIC_KEY_SIZE); | 472 | memcpy(cookie_plain, peer_real_pk, CRYPTO_PUBLIC_KEY_SIZE); |
472 | memcpy(cookie_plain + CRYPTO_PUBLIC_KEY_SIZE, peer_dht_pubkey, CRYPTO_PUBLIC_KEY_SIZE); | 473 | memcpy(cookie_plain + CRYPTO_PUBLIC_KEY_SIZE, peer_dht_pubkey, CRYPTO_PUBLIC_KEY_SIZE); |
473 | 474 | ||
474 | if (create_cookie(plain + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_SHA512_SIZE, cookie_plain, | 475 | if (create_cookie(c->log, plain + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_SHA512_SIZE, cookie_plain, |
475 | c->secret_symmetric_key) != 0) { | 476 | c->secret_symmetric_key) != 0) { |
476 | return -1; | 477 | return -1; |
477 | } | 478 | } |
@@ -517,7 +518,7 @@ static int handle_crypto_handshake(const Net_Crypto *c, uint8_t *nonce, uint8_t | |||
517 | 518 | ||
518 | uint8_t cookie_plain[COOKIE_DATA_LENGTH]; | 519 | uint8_t cookie_plain[COOKIE_DATA_LENGTH]; |
519 | 520 | ||
520 | if (open_cookie(cookie_plain, packet + 1, c->secret_symmetric_key) != 0) { | 521 | if (open_cookie(c->log, cookie_plain, packet + 1, c->secret_symmetric_key) != 0) { |
521 | return -1; | 522 | return -1; |
522 | } | 523 | } |
523 | 524 | ||
@@ -724,7 +725,7 @@ static uint32_t num_packets_array(const Packets_Array *array) | |||
724 | * return -1 on failure. | 725 | * return -1 on failure. |
725 | * return 0 on success. | 726 | * return 0 on success. |
726 | */ | 727 | */ |
727 | static int add_data_to_buffer(Packets_Array *array, uint32_t number, const Packet_Data *data) | 728 | static int add_data_to_buffer(const Logger *log, Packets_Array *array, uint32_t number, const Packet_Data *data) |
728 | { | 729 | { |
729 | if (number - array->buffer_start > CRYPTO_PACKET_BUFFER_SIZE) { | 730 | if (number - array->buffer_start > CRYPTO_PACKET_BUFFER_SIZE) { |
730 | return -1; | 731 | return -1; |
@@ -758,7 +759,7 @@ static int add_data_to_buffer(Packets_Array *array, uint32_t number, const Packe | |||
758 | * return 0 if data at number is empty. | 759 | * return 0 if data at number is empty. |
759 | * return 1 if data pointer was put in data. | 760 | * return 1 if data pointer was put in data. |
760 | */ | 761 | */ |
761 | static int get_data_pointer(const Packets_Array *array, Packet_Data **data, uint32_t number) | 762 | static int get_data_pointer(const Logger *log, const Packets_Array *array, Packet_Data **data, uint32_t number) |
762 | { | 763 | { |
763 | uint32_t num_spots = array->buffer_end - array->buffer_start; | 764 | uint32_t num_spots = array->buffer_end - array->buffer_start; |
764 | 765 | ||
@@ -781,7 +782,7 @@ static int get_data_pointer(const Packets_Array *array, Packet_Data **data, uint | |||
781 | * return -1 on failure. | 782 | * return -1 on failure. |
782 | * return packet number on success. | 783 | * return packet number on success. |
783 | */ | 784 | */ |
784 | static int64_t add_data_end_of_buffer(Packets_Array *array, const Packet_Data *data) | 785 | static int64_t add_data_end_of_buffer(const Logger *log, Packets_Array *array, const Packet_Data *data) |
785 | { | 786 | { |
786 | if (num_packets_array(array) >= CRYPTO_PACKET_BUFFER_SIZE) { | 787 | if (num_packets_array(array) >= CRYPTO_PACKET_BUFFER_SIZE) { |
787 | return -1; | 788 | return -1; |
@@ -800,12 +801,12 @@ static int64_t add_data_end_of_buffer(Packets_Array *array, const Packet_Data *d | |||
800 | return id; | 801 | return id; |
801 | } | 802 | } |
802 | 803 | ||
803 | /* Read data from begginning of array. | 804 | /* Read data from beginning of array. |
804 | * | 805 | * |
805 | * return -1 on failure. | 806 | * return -1 on failure. |
806 | * return packet number on success. | 807 | * return packet number on success. |
807 | */ | 808 | */ |
808 | static int64_t read_data_beg_buffer(Packets_Array *array, Packet_Data *data) | 809 | static int64_t read_data_beg_buffer(const Logger *log, Packets_Array *array, Packet_Data *data) |
809 | { | 810 | { |
810 | if (array->buffer_end == array->buffer_start) { | 811 | if (array->buffer_end == array->buffer_start) { |
811 | return -1; | 812 | return -1; |
@@ -830,7 +831,7 @@ static int64_t read_data_beg_buffer(Packets_Array *array, Packet_Data *data) | |||
830 | * return -1 on failure. | 831 | * return -1 on failure. |
831 | * return 0 on success | 832 | * return 0 on success |
832 | */ | 833 | */ |
833 | static int clear_buffer_until(Packets_Array *array, uint32_t number) | 834 | static int clear_buffer_until(const Logger *log, Packets_Array *array, uint32_t number) |
834 | { | 835 | { |
835 | uint32_t num_spots = array->buffer_end - array->buffer_start; | 836 | uint32_t num_spots = array->buffer_end - array->buffer_start; |
836 | 837 | ||
@@ -875,7 +876,7 @@ static int clear_buffer(Packets_Array *array) | |||
875 | * return -1 on failure. | 876 | * return -1 on failure. |
876 | * return 0 on success. | 877 | * return 0 on success. |
877 | */ | 878 | */ |
878 | static int set_buffer_end(Packets_Array *array, uint32_t number) | 879 | static int set_buffer_end(const Logger *log, Packets_Array *array, uint32_t number) |
879 | { | 880 | { |
880 | if ((number - array->buffer_start) > CRYPTO_PACKET_BUFFER_SIZE) { | 881 | if ((number - array->buffer_start) > CRYPTO_PACKET_BUFFER_SIZE) { |
881 | return -1; | 882 | return -1; |
@@ -895,7 +896,7 @@ static int set_buffer_end(Packets_Array *array, uint32_t number) | |||
895 | * return -1 on failure. | 896 | * return -1 on failure. |
896 | * return length of packet on success. | 897 | * return length of packet on success. |
897 | */ | 898 | */ |
898 | static int generate_request_packet(uint8_t *data, uint16_t length, const Packets_Array *recv_array) | 899 | static int generate_request_packet(const Logger *log, uint8_t *data, uint16_t length, const Packets_Array *recv_array) |
899 | { | 900 | { |
900 | if (length == 0) { | 901 | if (length == 0) { |
901 | return -1; | 902 | return -1; |
@@ -948,7 +949,7 @@ static int generate_request_packet(uint8_t *data, uint16_t length, const Packets | |||
948 | * return -1 on failure. | 949 | * return -1 on failure. |
949 | * return number of requested packets on success. | 950 | * return number of requested packets on success. |
950 | */ | 951 | */ |
951 | static int handle_request_packet(Packets_Array *send_array, const uint8_t *data, uint16_t length, | 952 | static int handle_request_packet(const Logger *log, Packets_Array *send_array, const uint8_t *data, uint16_t length, |
952 | uint64_t *latest_send_time, uint64_t rtt_time) | 953 | uint64_t *latest_send_time, uint64_t rtt_time) |
953 | { | 954 | { |
954 | if (length < 1) { | 955 | if (length < 1) { |
@@ -1101,7 +1102,7 @@ static int reset_max_speed_reached(Net_Crypto *c, int crypt_connection_id) | |||
1101 | if (conn->maximum_speed_reached) { | 1102 | if (conn->maximum_speed_reached) { |
1102 | Packet_Data *dt = nullptr; | 1103 | Packet_Data *dt = nullptr; |
1103 | uint32_t packet_num = conn->send_array.buffer_end - 1; | 1104 | uint32_t packet_num = conn->send_array.buffer_end - 1; |
1104 | int ret = get_data_pointer(&conn->send_array, &dt, packet_num); | 1105 | int ret = get_data_pointer(c->log, &conn->send_array, &dt, packet_num); |
1105 | 1106 | ||
1106 | uint8_t send_failed = 0; | 1107 | uint8_t send_failed = 0; |
1107 | 1108 | ||
@@ -1155,7 +1156,7 @@ static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, cons | |||
1155 | dt.length = length; | 1156 | dt.length = length; |
1156 | memcpy(dt.data, data, length); | 1157 | memcpy(dt.data, data, length); |
1157 | pthread_mutex_lock(&conn->mutex); | 1158 | pthread_mutex_lock(&conn->mutex); |
1158 | int64_t packet_num = add_data_end_of_buffer(&conn->send_array, &dt); | 1159 | int64_t packet_num = add_data_end_of_buffer(c->log, &conn->send_array, &dt); |
1159 | pthread_mutex_unlock(&conn->mutex); | 1160 | pthread_mutex_unlock(&conn->mutex); |
1160 | 1161 | ||
1161 | if (packet_num == -1) { | 1162 | if (packet_num == -1) { |
@@ -1169,7 +1170,7 @@ static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, cons | |||
1169 | if (send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, packet_num, data, length) == 0) { | 1170 | if (send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, packet_num, data, length) == 0) { |
1170 | Packet_Data *dt1 = nullptr; | 1171 | Packet_Data *dt1 = nullptr; |
1171 | 1172 | ||
1172 | if (get_data_pointer(&conn->send_array, &dt1, packet_num) == 1) { | 1173 | if (get_data_pointer(c->log, &conn->send_array, &dt1, packet_num) == 1) { |
1173 | dt1->sent_time = current_time_monotonic(); | 1174 | dt1->sent_time = current_time_monotonic(); |
1174 | } | 1175 | } |
1175 | } else { | 1176 | } else { |
@@ -1248,7 +1249,7 @@ static int send_request_packet(Net_Crypto *c, int crypt_connection_id) | |||
1248 | } | 1249 | } |
1249 | 1250 | ||
1250 | uint8_t data[MAX_CRYPTO_DATA_SIZE]; | 1251 | uint8_t data[MAX_CRYPTO_DATA_SIZE]; |
1251 | int len = generate_request_packet(data, sizeof(data), &conn->recv_array); | 1252 | int len = generate_request_packet(c->log, data, sizeof(data), &conn->recv_array); |
1252 | 1253 | ||
1253 | if (len == -1) { | 1254 | if (len == -1) { |
1254 | return -1; | 1255 | return -1; |
@@ -1281,7 +1282,7 @@ static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint32 | |||
1281 | for (i = 0; i < array_size; ++i) { | 1282 | for (i = 0; i < array_size; ++i) { |
1282 | Packet_Data *dt; | 1283 | Packet_Data *dt; |
1283 | uint32_t packet_num = (i + conn->send_array.buffer_start); | 1284 | uint32_t packet_num = (i + conn->send_array.buffer_start); |
1284 | int ret = get_data_pointer(&conn->send_array, &dt, packet_num); | 1285 | int ret = get_data_pointer(c->log, &conn->send_array, &dt, packet_num); |
1285 | 1286 | ||
1286 | if (ret == -1) { | 1287 | if (ret == -1) { |
1287 | return -1; | 1288 | return -1; |
@@ -1496,11 +1497,11 @@ static int handle_data_packet_core(Net_Crypto *c, int crypt_connection_id, const | |||
1496 | if (buffer_start != conn->send_array.buffer_start) { | 1497 | if (buffer_start != conn->send_array.buffer_start) { |
1497 | Packet_Data *packet_time; | 1498 | Packet_Data *packet_time; |
1498 | 1499 | ||
1499 | if (get_data_pointer(&conn->send_array, &packet_time, conn->send_array.buffer_start) == 1) { | 1500 | if (get_data_pointer(c->log, &conn->send_array, &packet_time, conn->send_array.buffer_start) == 1) { |
1500 | rtt_calc_time = packet_time->sent_time; | 1501 | rtt_calc_time = packet_time->sent_time; |
1501 | } | 1502 | } |
1502 | 1503 | ||
1503 | if (clear_buffer_until(&conn->send_array, buffer_start) != 0) { | 1504 | if (clear_buffer_until(c->log, &conn->send_array, buffer_start) != 0) { |
1504 | return -1; | 1505 | return -1; |
1505 | } | 1506 | } |
1506 | } | 1507 | } |
@@ -1541,25 +1542,25 @@ static int handle_data_packet_core(Net_Crypto *c, int crypt_connection_id, const | |||
1541 | rtt_time = DEFAULT_TCP_PING_CONNECTION; | 1542 | rtt_time = DEFAULT_TCP_PING_CONNECTION; |
1542 | } | 1543 | } |
1543 | 1544 | ||
1544 | int requested = handle_request_packet(&conn->send_array, real_data, real_length, &rtt_calc_time, rtt_time); | 1545 | int requested = handle_request_packet(c->log, &conn->send_array, real_data, real_length, &rtt_calc_time, rtt_time); |
1545 | 1546 | ||
1546 | if (requested == -1) { | 1547 | if (requested == -1) { |
1547 | return -1; | 1548 | return -1; |
1548 | } | 1549 | } |
1549 | 1550 | ||
1550 | set_buffer_end(&conn->recv_array, num); | 1551 | set_buffer_end(c->log, &conn->recv_array, num); |
1551 | } else if (real_data[0] >= CRYPTO_RESERVED_PACKETS && real_data[0] < PACKET_ID_LOSSY_RANGE_START) { | 1552 | } else if (real_data[0] >= CRYPTO_RESERVED_PACKETS && real_data[0] < PACKET_ID_LOSSY_RANGE_START) { |
1552 | Packet_Data dt; | 1553 | Packet_Data dt = {0}; |
1553 | dt.length = real_length; | 1554 | dt.length = real_length; |
1554 | memcpy(dt.data, real_data, real_length); | 1555 | memcpy(dt.data, real_data, real_length); |
1555 | 1556 | ||
1556 | if (add_data_to_buffer(&conn->recv_array, num, &dt) != 0) { | 1557 | if (add_data_to_buffer(c->log, &conn->recv_array, num, &dt) != 0) { |
1557 | return -1; | 1558 | return -1; |
1558 | } | 1559 | } |
1559 | 1560 | ||
1560 | while (1) { | 1561 | while (1) { |
1561 | pthread_mutex_lock(&conn->mutex); | 1562 | pthread_mutex_lock(&conn->mutex); |
1562 | int ret = read_data_beg_buffer(&conn->recv_array, &dt); | 1563 | int ret = read_data_beg_buffer(c->log, &conn->recv_array, &dt); |
1563 | pthread_mutex_unlock(&conn->mutex); | 1564 | pthread_mutex_unlock(&conn->mutex); |
1564 | 1565 | ||
1565 | if (ret == -1) { | 1566 | if (ret == -1) { |
@@ -1584,7 +1585,7 @@ static int handle_data_packet_core(Net_Crypto *c, int crypt_connection_id, const | |||
1584 | } else if (real_data[0] >= PACKET_ID_LOSSY_RANGE_START && | 1585 | } else if (real_data[0] >= PACKET_ID_LOSSY_RANGE_START && |
1585 | real_data[0] < (PACKET_ID_LOSSY_RANGE_START + PACKET_ID_LOSSY_RANGE_SIZE)) { | 1586 | real_data[0] < (PACKET_ID_LOSSY_RANGE_START + PACKET_ID_LOSSY_RANGE_SIZE)) { |
1586 | 1587 | ||
1587 | set_buffer_end(&conn->recv_array, num); | 1588 | set_buffer_end(c->log, &conn->recv_array, num); |
1588 | 1589 | ||
1589 | if (conn->connection_lossy_data_callback) { | 1590 | if (conn->connection_lossy_data_callback) { |
1590 | conn->connection_lossy_data_callback(conn->connection_lossy_data_callback_object, | 1591 | conn->connection_lossy_data_callback(conn->connection_lossy_data_callback_object, |
@@ -1632,7 +1633,7 @@ static int handle_packet_connection(Net_Crypto *c, int crypt_connection_id, cons | |||
1632 | uint8_t cookie[COOKIE_LENGTH]; | 1633 | uint8_t cookie[COOKIE_LENGTH]; |
1633 | uint64_t number; | 1634 | uint64_t number; |
1634 | 1635 | ||
1635 | if (handle_cookie_response(cookie, &number, packet, length, conn->shared_key) != sizeof(cookie)) { | 1636 | if (handle_cookie_response(c->log, cookie, &number, packet, length, conn->shared_key) != sizeof(cookie)) { |
1636 | return -1; | 1637 | return -1; |
1637 | } | 1638 | } |
1638 | 1639 | ||
@@ -2928,7 +2929,7 @@ void load_secret_key(Net_Crypto *c, const uint8_t *sk) | |||
2928 | /* Run this to (re)initialize net_crypto. | 2929 | /* Run this to (re)initialize net_crypto. |
2929 | * Sets all the global connection variables to their default values. | 2930 | * Sets all the global connection variables to their default values. |
2930 | */ | 2931 | */ |
2931 | Net_Crypto *new_net_crypto(Logger *log, DHT *dht, TCP_Proxy_Info *proxy_info) | 2932 | Net_Crypto *new_net_crypto(const Logger *log, DHT *dht, TCP_Proxy_Info *proxy_info) |
2932 | { | 2933 | { |
2933 | unix_time_update(); | 2934 | unix_time_update(); |
2934 | 2935 | ||