diff options
author | iphydf <iphydf@users.noreply.github.com> | 2018-02-16 17:16:49 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2018-02-18 13:01:16 +0000 |
commit | 3f73dfa7f1a7fa1afef2492d1834e4c619f4afb3 (patch) | |
tree | a8fa188dc9929dbfbb5556b1e58d17d46596ec05 /toxcore | |
parent | 841b63434fcb949c48d42a2a8b2b4f69467f3f78 (diff) |
Use `const` more in C code.
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/DHT.c | 455 | ||||
-rw-r--r-- | toxcore/DHT.h | 14 | ||||
-rw-r--r-- | toxcore/friend_connection.c | 226 | ||||
-rw-r--r-- | toxcore/friend_requests.c | 27 |
4 files changed, 348 insertions, 374 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 1b0302b3..10ea659e 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -154,8 +154,8 @@ const uint8_t *dht_get_friend_public_key(const DHT *dht, uint32_t friend_num) | |||
154 | int id_closest(const uint8_t *pk, const uint8_t *pk1, const uint8_t *pk2) | 154 | int id_closest(const uint8_t *pk, const uint8_t *pk1, const uint8_t *pk2) |
155 | { | 155 | { |
156 | for (size_t i = 0; i < CRYPTO_PUBLIC_KEY_SIZE; ++i) { | 156 | for (size_t i = 0; i < CRYPTO_PUBLIC_KEY_SIZE; ++i) { |
157 | uint8_t distance1 = pk[i] ^ pk1[i]; | 157 | const uint8_t distance1 = pk[i] ^ pk1[i]; |
158 | uint8_t distance2 = pk[i] ^ pk2[i]; | 158 | const uint8_t distance2 = pk[i] ^ pk2[i]; |
159 | 159 | ||
160 | if (distance1 < distance2) { | 160 | if (distance1 < distance2) { |
161 | return 1; | 161 | return 1; |
@@ -182,7 +182,7 @@ static unsigned int bit_by_bit_cmp(const uint8_t *pk1, const uint8_t *pk2) | |||
182 | } | 182 | } |
183 | 183 | ||
184 | for (j = 0; j < 8; ++j) { | 184 | for (j = 0; j < 8; ++j) { |
185 | uint8_t mask = 1 << (7 - j); | 185 | const uint8_t mask = 1 << (7 - j); |
186 | 186 | ||
187 | if ((pk1[i] & mask) != (pk2[i] & mask)) { | 187 | if ((pk1[i] & mask) != (pk2[i] & mask)) { |
188 | break; | 188 | break; |
@@ -207,8 +207,8 @@ void get_shared_key(Shared_Keys *shared_keys, uint8_t *shared_key, const uint8_t | |||
207 | uint32_t curr = 0; | 207 | uint32_t curr = 0; |
208 | 208 | ||
209 | for (uint32_t i = 0; i < MAX_KEYS_PER_SLOT; ++i) { | 209 | for (uint32_t i = 0; i < MAX_KEYS_PER_SLOT; ++i) { |
210 | int index = public_key[30] * MAX_KEYS_PER_SLOT + i; | 210 | const int index = public_key[30] * MAX_KEYS_PER_SLOT + i; |
211 | Shared_Key *key = &shared_keys->keys[index]; | 211 | Shared_Key *const key = &shared_keys->keys[index]; |
212 | 212 | ||
213 | if (key->stored) { | 213 | if (key->stored) { |
214 | if (id_equal(public_key, key->public_key)) { | 214 | if (id_equal(public_key, key->public_key)) { |
@@ -235,8 +235,8 @@ void get_shared_key(Shared_Keys *shared_keys, uint8_t *shared_key, const uint8_t | |||
235 | 235 | ||
236 | encrypt_precompute(public_key, secret_key, shared_key); | 236 | encrypt_precompute(public_key, secret_key, shared_key); |
237 | 237 | ||
238 | if (num != (uint32_t)~0) { | 238 | if (num != UINT32_MAX) { |
239 | Shared_Key *key = &shared_keys->keys[curr]; | 239 | Shared_Key *const key = &shared_keys->keys[curr]; |
240 | key->stored = 1; | 240 | key->stored = 1; |
241 | key->times_requested = 1; | 241 | key->times_requested = 1; |
242 | memcpy(key->public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); | 242 | memcpy(key->public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); |
@@ -284,13 +284,13 @@ int create_request(const uint8_t *send_public_key, const uint8_t *send_secret_ke | |||
284 | return -1; | 284 | return -1; |
285 | } | 285 | } |
286 | 286 | ||
287 | uint8_t *nonce = packet + 1 + CRYPTO_PUBLIC_KEY_SIZE * 2; | 287 | uint8_t *const nonce = packet + 1 + CRYPTO_PUBLIC_KEY_SIZE * 2; |
288 | random_nonce(nonce); | 288 | random_nonce(nonce); |
289 | uint8_t temp[MAX_CRYPTO_REQUEST_SIZE]; | 289 | uint8_t temp[MAX_CRYPTO_REQUEST_SIZE]; |
290 | memcpy(temp + 1, data, length); | 290 | memcpy(temp + 1, data, length); |
291 | temp[0] = request_id; | 291 | temp[0] = request_id; |
292 | int len = encrypt_data(recv_public_key, send_secret_key, nonce, temp, length + 1, | 292 | const int len = encrypt_data(recv_public_key, send_secret_key, nonce, temp, length + 1, |
293 | CRYPTO_SIZE + packet); | 293 | CRYPTO_SIZE + packet); |
294 | 294 | ||
295 | if (len == -1) { | 295 | if (len == -1) { |
296 | crypto_memzero(temp, MAX_CRYPTO_REQUEST_SIZE); | 296 | crypto_memzero(temp, MAX_CRYPTO_REQUEST_SIZE); |
@@ -327,7 +327,7 @@ int handle_request(const uint8_t *self_public_key, const uint8_t *self_secret_ke | |||
327 | } | 327 | } |
328 | 328 | ||
329 | memcpy(public_key, packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, CRYPTO_PUBLIC_KEY_SIZE); | 329 | memcpy(public_key, packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, CRYPTO_PUBLIC_KEY_SIZE); |
330 | const uint8_t *nonce = packet + 1 + CRYPTO_PUBLIC_KEY_SIZE * 2; | 330 | const uint8_t *const nonce = packet + 1 + CRYPTO_PUBLIC_KEY_SIZE * 2; |
331 | uint8_t temp[MAX_CRYPTO_REQUEST_SIZE]; | 331 | uint8_t temp[MAX_CRYPTO_REQUEST_SIZE]; |
332 | int len1 = decrypt_data(public_key, self_secret_key, nonce, | 332 | int len1 = decrypt_data(public_key, self_secret_key, nonce, |
333 | packet + CRYPTO_SIZE, length - CRYPTO_SIZE, temp); | 333 | packet + CRYPTO_SIZE, length - CRYPTO_SIZE, temp); |
@@ -399,7 +399,7 @@ int pack_ip_port(uint8_t *data, uint16_t length, const IP_Port *ip_port) | |||
399 | } | 399 | } |
400 | 400 | ||
401 | if (is_ipv4) { | 401 | if (is_ipv4) { |
402 | uint32_t size = 1 + SIZE_IP4 + sizeof(uint16_t); | 402 | const uint32_t size = 1 + SIZE_IP4 + sizeof(uint16_t); |
403 | 403 | ||
404 | if (size > length) { | 404 | if (size > length) { |
405 | return -1; | 405 | return -1; |
@@ -410,7 +410,7 @@ int pack_ip_port(uint8_t *data, uint16_t length, const IP_Port *ip_port) | |||
410 | memcpy(data + 1 + SIZE_IP4, &ip_port->port, sizeof(uint16_t)); | 410 | memcpy(data + 1 + SIZE_IP4, &ip_port->port, sizeof(uint16_t)); |
411 | return size; | 411 | return size; |
412 | } else { | 412 | } else { |
413 | uint32_t size = 1 + SIZE_IP6 + sizeof(uint16_t); | 413 | const uint32_t size = 1 + SIZE_IP6 + sizeof(uint16_t); |
414 | 414 | ||
415 | if (size > length) { | 415 | if (size > length) { |
416 | return -1; | 416 | return -1; |
@@ -431,7 +431,7 @@ static int DHT_create_packet(const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE], | |||
431 | 431 | ||
432 | random_nonce(nonce); | 432 | random_nonce(nonce); |
433 | 433 | ||
434 | int encrypted_length = encrypt_data_symmetric(shared_key, nonce, plain, plain_length, encrypted); | 434 | const int encrypted_length = encrypt_data_symmetric(shared_key, nonce, plain, plain_length, encrypted); |
435 | 435 | ||
436 | if (encrypted_length == -1) { | 436 | if (encrypted_length == -1) { |
437 | return -1; | 437 | return -1; |
@@ -484,7 +484,7 @@ int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, uint8 | |||
484 | } | 484 | } |
485 | 485 | ||
486 | if (is_ipv4) { | 486 | if (is_ipv4) { |
487 | uint32_t size = 1 + SIZE_IP4 + sizeof(uint16_t); | 487 | const uint32_t size = 1 + SIZE_IP4 + sizeof(uint16_t); |
488 | 488 | ||
489 | if (size > length) { | 489 | if (size > length) { |
490 | return -1; | 490 | return -1; |
@@ -495,7 +495,7 @@ int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, uint8 | |||
495 | memcpy(&ip_port->port, data + 1 + SIZE_IP4, sizeof(uint16_t)); | 495 | memcpy(&ip_port->port, data + 1 + SIZE_IP4, sizeof(uint16_t)); |
496 | return size; | 496 | return size; |
497 | } else { | 497 | } else { |
498 | uint32_t size = 1 + SIZE_IP6 + sizeof(uint16_t); | 498 | const uint32_t size = 1 + SIZE_IP6 + sizeof(uint16_t); |
499 | 499 | ||
500 | if (size > length) { | 500 | if (size > length) { |
501 | return -1; | 501 | return -1; |
@@ -518,7 +518,7 @@ int pack_nodes(uint8_t *data, uint16_t length, const Node_format *nodes, uint16_ | |||
518 | uint32_t packed_length = 0; | 518 | uint32_t packed_length = 0; |
519 | 519 | ||
520 | for (uint32_t i = 0; i < number && packed_length < length; ++i) { | 520 | for (uint32_t i = 0; i < number && packed_length < length; ++i) { |
521 | int ipp_size = pack_ip_port(data + packed_length, length - packed_length, &nodes[i].ip_port); | 521 | const int ipp_size = pack_ip_port(data + packed_length, length - packed_length, &nodes[i].ip_port); |
522 | 522 | ||
523 | if (ipp_size == -1) { | 523 | if (ipp_size == -1) { |
524 | return -1; | 524 | return -1; |
@@ -553,7 +553,7 @@ int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed | |||
553 | uint32_t num = 0, len_processed = 0; | 553 | uint32_t num = 0, len_processed = 0; |
554 | 554 | ||
555 | while (num < max_num_nodes && len_processed < length) { | 555 | while (num < max_num_nodes && len_processed < length) { |
556 | int ipp_size = unpack_ip_port(&nodes[num].ip_port, data + len_processed, length - len_processed, tcp_enabled); | 556 | const int ipp_size = unpack_ip_port(&nodes[num].ip_port, data + len_processed, length - len_processed, tcp_enabled); |
557 | 557 | ||
558 | if (ipp_size == -1) { | 558 | if (ipp_size == -1) { |
559 | return -1; | 559 | return -1; |
@@ -669,7 +669,7 @@ static void update_client(Logger *log, int index, Client_data *client, IP_Port i | |||
669 | static int client_or_ip_port_in_list(Logger *log, Client_data *list, uint16_t length, const uint8_t *public_key, | 669 | static int client_or_ip_port_in_list(Logger *log, Client_data *list, uint16_t length, const uint8_t *public_key, |
670 | IP_Port ip_port) | 670 | IP_Port ip_port) |
671 | { | 671 | { |
672 | uint64_t temp_time = unix_time(); | 672 | const uint64_t temp_time = unix_time(); |
673 | uint32_t index = index_of_client_pk(list, length, public_key); | 673 | uint32_t index = index_of_client_pk(list, length, public_key); |
674 | 674 | ||
675 | /* if public_key is in list, find it and maybe overwrite ip_port */ | 675 | /* if public_key is in list, find it and maybe overwrite ip_port */ |
@@ -763,7 +763,7 @@ static void get_close_nodes_inner(const uint8_t *public_key, Node_format *nodes_ | |||
763 | uint32_t num_nodes = *num_nodes_ptr; | 763 | uint32_t num_nodes = *num_nodes_ptr; |
764 | 764 | ||
765 | for (uint32_t i = 0; i < client_list_length; i++) { | 765 | for (uint32_t i = 0; i < client_list_length; i++) { |
766 | const Client_data *client = &client_list[i]; | 766 | const Client_data *const client = &client_list[i]; |
767 | 767 | ||
768 | /* node already in list? */ | 768 | /* node already in list? */ |
769 | if (index_of_node_pk(nodes_list, MAX_SENT_NODES, client->public_key) != UINT32_MAX) { | 769 | if (index_of_node_pk(nodes_list, MAX_SENT_NODES, client->public_key) != UINT32_MAX) { |
@@ -861,8 +861,8 @@ static int cmp_dht_entry(const void *a, const void *b) | |||
861 | DHT_Cmp_data cmp1, cmp2; | 861 | DHT_Cmp_data cmp1, cmp2; |
862 | memcpy(&cmp1, a, sizeof(DHT_Cmp_data)); | 862 | memcpy(&cmp1, a, sizeof(DHT_Cmp_data)); |
863 | memcpy(&cmp2, b, sizeof(DHT_Cmp_data)); | 863 | memcpy(&cmp2, b, sizeof(DHT_Cmp_data)); |
864 | Client_data entry1 = cmp1.entry; | 864 | const Client_data entry1 = cmp1.entry; |
865 | Client_data entry2 = cmp2.entry; | 865 | const Client_data entry2 = cmp2.entry; |
866 | const uint8_t *cmp_public_key = cmp1.base_public_key; | 866 | const uint8_t *cmp_public_key = cmp1.base_public_key; |
867 | 867 | ||
868 | #define ASSOC_TIMEOUT(assoc) is_timeout((assoc).timestamp, BAD_NODE_TIMEOUT) | 868 | #define ASSOC_TIMEOUT(assoc) is_timeout((assoc).timestamp, BAD_NODE_TIMEOUT) |
@@ -895,7 +895,7 @@ static int cmp_dht_entry(const void *a, const void *b) | |||
895 | return 1; | 895 | return 1; |
896 | } | 896 | } |
897 | 897 | ||
898 | int close = id_closest(cmp_public_key, entry1.public_key, entry2.public_key); | 898 | const int close = id_closest(cmp_public_key, entry1.public_key, entry2.public_key); |
899 | 899 | ||
900 | if (close == 1) { | 900 | if (close == 1) { |
901 | return 1; | 901 | return 1; |
@@ -915,9 +915,9 @@ static int cmp_dht_entry(const void *a, const void *b) | |||
915 | */ | 915 | */ |
916 | static unsigned int store_node_ok(const Client_data *client, const uint8_t *public_key, const uint8_t *comp_public_key) | 916 | static unsigned int store_node_ok(const Client_data *client, const uint8_t *public_key, const uint8_t *comp_public_key) |
917 | { | 917 | { |
918 | return is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) && | 918 | return (is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) |
919 | is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT) || | 919 | && is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT)) |
920 | id_closest(comp_public_key, client->public_key, public_key) == 2; | 920 | || id_closest(comp_public_key, client->public_key, public_key) == 2; |
921 | } | 921 | } |
922 | 922 | ||
923 | static void sort_client_list(Client_data *list, unsigned int length, const uint8_t *comp_public_key) | 923 | static void sort_client_list(Client_data *list, unsigned int length, const uint8_t *comp_public_key) |
@@ -974,29 +974,29 @@ static void update_client_with_reset(Client_data *client, const IP_Port *ip_port | |||
974 | * and all nodes in the list are closer to comp_public_key | 974 | * and all nodes in the list are closer to comp_public_key |
975 | * than public_key. | 975 | * than public_key. |
976 | * | 976 | * |
977 | * returns True(1) when the item was stored, False(0) otherwise */ | 977 | * returns true when the item was stored, false otherwise */ |
978 | static int replace_all(Client_data *list, | 978 | static bool replace_all(Client_data *list, |
979 | uint16_t length, | 979 | uint16_t length, |
980 | const uint8_t *public_key, | 980 | const uint8_t *public_key, |
981 | IP_Port ip_port, | 981 | IP_Port ip_port, |
982 | const uint8_t *comp_public_key) | 982 | const uint8_t *comp_public_key) |
983 | { | 983 | { |
984 | if ((ip_port.ip.family != TOX_AF_INET) && (ip_port.ip.family != TOX_AF_INET6)) { | 984 | if ((ip_port.ip.family != TOX_AF_INET) && (ip_port.ip.family != TOX_AF_INET6)) { |
985 | return 0; | 985 | return false; |
986 | } | 986 | } |
987 | 987 | ||
988 | if (!store_node_ok(&list[1], public_key, comp_public_key) && | 988 | if (!store_node_ok(&list[1], public_key, comp_public_key) && |
989 | !store_node_ok(&list[0], public_key, comp_public_key)) { | 989 | !store_node_ok(&list[0], public_key, comp_public_key)) { |
990 | return 0; | 990 | return false; |
991 | } | 991 | } |
992 | 992 | ||
993 | sort_client_list(list, length, comp_public_key); | 993 | sort_client_list(list, length, comp_public_key); |
994 | 994 | ||
995 | Client_data *client = &list[0]; | 995 | Client_data *const client = &list[0]; |
996 | id_copy(client->public_key, public_key); | 996 | id_copy(client->public_key, public_key); |
997 | 997 | ||
998 | update_client_with_reset(client, &ip_port); | 998 | update_client_with_reset(client, &ip_port); |
999 | return 1; | 999 | return true; |
1000 | } | 1000 | } |
1001 | 1001 | ||
1002 | /* Add node to close list. | 1002 | /* Add node to close list. |
@@ -1017,7 +1017,7 @@ static int add_to_close(DHT *dht, const uint8_t *public_key, IP_Port ip_port, bo | |||
1017 | for (uint32_t i = 0; i < LCLIENT_NODES; ++i) { | 1017 | for (uint32_t i = 0; i < LCLIENT_NODES; ++i) { |
1018 | /* TODO(iphydf): write bounds checking test to catch the case that | 1018 | /* TODO(iphydf): write bounds checking test to catch the case that |
1019 | * index is left as >= LCLIENT_LENGTH */ | 1019 | * index is left as >= LCLIENT_LENGTH */ |
1020 | Client_data *client = &dht->close_clientlist[(index * LCLIENT_NODES) + i]; | 1020 | Client_data *const client = &dht->close_clientlist[(index * LCLIENT_NODES) + i]; |
1021 | 1021 | ||
1022 | if (!is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) || | 1022 | if (!is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) || |
1023 | !is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT)) { | 1023 | !is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT)) { |
@@ -1043,18 +1043,18 @@ bool node_addable_to_close_list(DHT *dht, const uint8_t *public_key, IP_Port ip_ | |||
1043 | return add_to_close(dht, public_key, ip_port, 1) == 0; | 1043 | return add_to_close(dht, public_key, ip_port, 1) == 0; |
1044 | } | 1044 | } |
1045 | 1045 | ||
1046 | static bool is_pk_in_client_list(Client_data *list, unsigned int client_list_length, const uint8_t *public_key, | 1046 | static bool is_pk_in_client_list(const Client_data *list, unsigned int client_list_length, const uint8_t *public_key, |
1047 | IP_Port ip_port) | 1047 | IP_Port ip_port) |
1048 | { | 1048 | { |
1049 | uint32_t index = index_of_client_pk(list, client_list_length, public_key); | 1049 | const uint32_t index = index_of_client_pk(list, client_list_length, public_key); |
1050 | 1050 | ||
1051 | if (index == UINT32_MAX) { | 1051 | if (index == UINT32_MAX) { |
1052 | return 0; | 1052 | return 0; |
1053 | } | 1053 | } |
1054 | 1054 | ||
1055 | const IPPTsPng *assoc = ip_port.ip.family == TOX_AF_INET ? | 1055 | const IPPTsPng *assoc = ip_port.ip.family == TOX_AF_INET |
1056 | &list[index].assoc4 : | 1056 | ? &list[index].assoc4 |
1057 | &list[index].assoc6; | 1057 | : &list[index].assoc6; |
1058 | 1058 | ||
1059 | return !is_timeout(assoc->timestamp, BAD_NODE_TIMEOUT); | 1059 | return !is_timeout(assoc->timestamp, BAD_NODE_TIMEOUT); |
1060 | } | 1060 | } |
@@ -1073,20 +1073,20 @@ static bool is_pk_in_close_list(DHT *dht, const uint8_t *public_key, IP_Port ip_ | |||
1073 | /* Check if the node obtained with a get_nodes with public_key should be pinged. | 1073 | /* Check if the node obtained with a get_nodes with public_key should be pinged. |
1074 | * NOTE: for best results call it after addto_lists; | 1074 | * NOTE: for best results call it after addto_lists; |
1075 | * | 1075 | * |
1076 | * return 0 if the node should not be pinged. | 1076 | * return false if the node should not be pinged. |
1077 | * return 1 if it should. | 1077 | * return true if it should. |
1078 | */ | 1078 | */ |
1079 | static unsigned int ping_node_from_getnodes_ok(DHT *dht, const uint8_t *public_key, IP_Port ip_port) | 1079 | static bool ping_node_from_getnodes_ok(DHT *dht, const uint8_t *public_key, IP_Port ip_port) |
1080 | { | 1080 | { |
1081 | bool ret = 0; | 1081 | bool ret = false; |
1082 | 1082 | ||
1083 | if (add_to_close(dht, public_key, ip_port, 1) == 0) { | 1083 | if (add_to_close(dht, public_key, ip_port, 1) == 0) { |
1084 | ret = 1; | 1084 | ret = true; |
1085 | } | 1085 | } |
1086 | 1086 | ||
1087 | unsigned int *num = &dht->num_to_bootstrap; | 1087 | unsigned int *const num = &dht->num_to_bootstrap; |
1088 | uint32_t index = index_of_node_pk(dht->to_bootstrap, *num, public_key); | 1088 | const uint32_t index = index_of_node_pk(dht->to_bootstrap, *num, public_key); |
1089 | bool in_close_list = is_pk_in_close_list(dht, public_key, ip_port); | 1089 | const bool in_close_list = is_pk_in_close_list(dht, public_key, ip_port); |
1090 | 1090 | ||
1091 | if (ret && index == UINT32_MAX && !in_close_list) { | 1091 | if (ret && index == UINT32_MAX && !in_close_list) { |
1092 | if (*num < MAX_CLOSE_TO_BOOTSTRAP_NODES) { | 1092 | if (*num < MAX_CLOSE_TO_BOOTSTRAP_NODES) { |
@@ -1100,25 +1100,25 @@ static unsigned int ping_node_from_getnodes_ok(DHT *dht, const uint8_t *public_k | |||
1100 | } | 1100 | } |
1101 | 1101 | ||
1102 | for (uint32_t i = 0; i < dht->num_friends; ++i) { | 1102 | for (uint32_t i = 0; i < dht->num_friends; ++i) { |
1103 | bool store_ok = 0; | ||
1104 | |||
1105 | DHT_Friend *dht_friend = &dht->friends_list[i]; | 1103 | DHT_Friend *dht_friend = &dht->friends_list[i]; |
1106 | 1104 | ||
1105 | bool store_ok = false; | ||
1106 | |||
1107 | if (store_node_ok(&dht_friend->client_list[1], public_key, dht_friend->public_key)) { | 1107 | if (store_node_ok(&dht_friend->client_list[1], public_key, dht_friend->public_key)) { |
1108 | store_ok = 1; | 1108 | store_ok = true; |
1109 | } | 1109 | } |
1110 | 1110 | ||
1111 | if (store_node_ok(&dht_friend->client_list[0], public_key, dht_friend->public_key)) { | 1111 | if (store_node_ok(&dht_friend->client_list[0], public_key, dht_friend->public_key)) { |
1112 | store_ok = 1; | 1112 | store_ok = true; |
1113 | } | 1113 | } |
1114 | 1114 | ||
1115 | unsigned int *friend_num = &dht_friend->num_to_bootstrap; | 1115 | unsigned int *const friend_num = &dht_friend->num_to_bootstrap; |
1116 | const uint32_t index = index_of_node_pk(dht_friend->to_bootstrap, *friend_num, public_key); | 1116 | const uint32_t index = index_of_node_pk(dht_friend->to_bootstrap, *friend_num, public_key); |
1117 | const bool pk_in_list = is_pk_in_client_list(dht_friend->client_list, MAX_FRIEND_CLIENTS, public_key, ip_port); | 1117 | const bool pk_in_list = is_pk_in_client_list(dht_friend->client_list, MAX_FRIEND_CLIENTS, public_key, ip_port); |
1118 | 1118 | ||
1119 | if (store_ok && index == UINT32_MAX && !pk_in_list) { | 1119 | if (store_ok && index == UINT32_MAX && !pk_in_list) { |
1120 | if (*friend_num < MAX_SENT_NODES) { | 1120 | if (*friend_num < MAX_SENT_NODES) { |
1121 | Node_format *format = &dht_friend->to_bootstrap[*friend_num]; | 1121 | Node_format *const format = &dht_friend->to_bootstrap[*friend_num]; |
1122 | memcpy(format->public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); | 1122 | memcpy(format->public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); |
1123 | format->ip_port = ip_port; | 1123 | format->ip_port = ip_port; |
1124 | ++*friend_num; | 1124 | ++*friend_num; |
@@ -1126,7 +1126,7 @@ static unsigned int ping_node_from_getnodes_ok(DHT *dht, const uint8_t *public_k | |||
1126 | add_to_list(dht_friend->to_bootstrap, MAX_SENT_NODES, public_key, ip_port, dht_friend->public_key); | 1126 | add_to_list(dht_friend->to_bootstrap, MAX_SENT_NODES, public_key, ip_port, dht_friend->public_key); |
1127 | } | 1127 | } |
1128 | 1128 | ||
1129 | ret = 1; | 1129 | ret = true; |
1130 | } | 1130 | } |
1131 | } | 1131 | } |
1132 | 1132 | ||
@@ -1194,14 +1194,14 @@ uint32_t addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *public_key) | |||
1194 | 1194 | ||
1195 | static bool update_client_data(Client_data *array, size_t size, IP_Port ip_port, const uint8_t *pk) | 1195 | static bool update_client_data(Client_data *array, size_t size, IP_Port ip_port, const uint8_t *pk) |
1196 | { | 1196 | { |
1197 | uint64_t temp_time = unix_time(); | 1197 | const uint64_t temp_time = unix_time(); |
1198 | uint32_t index = index_of_client_pk(array, size, pk); | 1198 | const uint32_t index = index_of_client_pk(array, size, pk); |
1199 | 1199 | ||
1200 | if (index == UINT32_MAX) { | 1200 | if (index == UINT32_MAX) { |
1201 | return false; | 1201 | return false; |
1202 | } | 1202 | } |
1203 | 1203 | ||
1204 | Client_data *data = &array[index]; | 1204 | Client_data *const data = &array[index]; |
1205 | IPPTsPng *assoc; | 1205 | IPPTsPng *assoc; |
1206 | 1206 | ||
1207 | if (ip_port.ip.family == TOX_AF_INET) { | 1207 | if (ip_port.ip.family == TOX_AF_INET) { |
@@ -1235,7 +1235,7 @@ static void returnedip_ports(DHT *dht, IP_Port ip_port, const uint8_t *public_ke | |||
1235 | 1235 | ||
1236 | for (uint32_t i = 0; i < dht->num_friends; ++i) { | 1236 | for (uint32_t i = 0; i < dht->num_friends; ++i) { |
1237 | if (id_equal(public_key, dht->friends_list[i].public_key)) { | 1237 | if (id_equal(public_key, dht->friends_list[i].public_key)) { |
1238 | Client_data *client_list = dht->friends_list[i].client_list; | 1238 | Client_data *const client_list = dht->friends_list[i].client_list; |
1239 | 1239 | ||
1240 | if (update_client_data(client_list, MAX_FRIEND_CLIENTS, ip_port, nodepublic_key)) { | 1240 | if (update_client_data(client_list, MAX_FRIEND_CLIENTS, ip_port, nodepublic_key)) { |
1241 | return; | 1241 | return; |
@@ -1283,8 +1283,8 @@ static int getnodes(DHT *dht, IP_Port ip_port, const uint8_t *public_key, const | |||
1283 | uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]; | 1283 | uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]; |
1284 | DHT_get_shared_key_sent(dht, shared_key, public_key); | 1284 | DHT_get_shared_key_sent(dht, shared_key, public_key); |
1285 | 1285 | ||
1286 | int len = DHT_create_packet(dht->self_public_key, shared_key, NET_PACKET_GET_NODES, | 1286 | const int len = DHT_create_packet(dht->self_public_key, shared_key, NET_PACKET_GET_NODES, |
1287 | plain, sizeof(plain), data); | 1287 | plain, sizeof(plain), data); |
1288 | 1288 | ||
1289 | if (len != sizeof(data)) { | 1289 | if (len != sizeof(data)) { |
1290 | return -1; | 1290 | return -1; |
@@ -1306,17 +1306,17 @@ static int sendnodes_ipv6(const DHT *dht, IP_Port ip_port, const uint8_t *public | |||
1306 | return -1; | 1306 | return -1; |
1307 | } | 1307 | } |
1308 | 1308 | ||
1309 | size_t Node_format_size = sizeof(Node_format); | 1309 | const size_t node_format_size = sizeof(Node_format); |
1310 | 1310 | ||
1311 | Node_format nodes_list[MAX_SENT_NODES]; | 1311 | Node_format nodes_list[MAX_SENT_NODES]; |
1312 | uint32_t num_nodes = get_close_nodes(dht, client_id, nodes_list, 0, ip_is_lan(ip_port.ip) == 0, 1); | 1312 | const uint32_t num_nodes = get_close_nodes(dht, client_id, nodes_list, 0, ip_is_lan(ip_port.ip) == 0, 1); |
1313 | 1313 | ||
1314 | VLA(uint8_t, plain, 1 + Node_format_size * MAX_SENT_NODES + length); | 1314 | VLA(uint8_t, plain, 1 + node_format_size * MAX_SENT_NODES + length); |
1315 | 1315 | ||
1316 | int nodes_length = 0; | 1316 | int nodes_length = 0; |
1317 | 1317 | ||
1318 | if (num_nodes) { | 1318 | if (num_nodes) { |
1319 | nodes_length = pack_nodes(plain + 1, Node_format_size * MAX_SENT_NODES, nodes_list, num_nodes); | 1319 | nodes_length = pack_nodes(plain + 1, node_format_size * MAX_SENT_NODES, nodes_list, num_nodes); |
1320 | 1320 | ||
1321 | if (nodes_length <= 0) { | 1321 | if (nodes_length <= 0) { |
1322 | return -1; | 1322 | return -1; |
@@ -1329,8 +1329,8 @@ static int sendnodes_ipv6(const DHT *dht, IP_Port ip_port, const uint8_t *public | |||
1329 | const uint32_t crypto_size = 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + CRYPTO_MAC_SIZE; | 1329 | const uint32_t crypto_size = 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + CRYPTO_MAC_SIZE; |
1330 | VLA(uint8_t, data, 1 + nodes_length + length + crypto_size); | 1330 | VLA(uint8_t, data, 1 + nodes_length + length + crypto_size); |
1331 | 1331 | ||
1332 | int len = DHT_create_packet(dht->self_public_key, shared_encryption_key, NET_PACKET_SEND_NODES_IPV6, | 1332 | const int len = DHT_create_packet(dht->self_public_key, shared_encryption_key, NET_PACKET_SEND_NODES_IPV6, |
1333 | plain, 1 + nodes_length + length, data); | 1333 | plain, 1 + nodes_length + length, data); |
1334 | 1334 | ||
1335 | if (len != SIZEOF_VLA(data)) { | 1335 | if (len != SIZEOF_VLA(data)) { |
1336 | return -1; | 1336 | return -1; |
@@ -1344,41 +1344,42 @@ static int sendnodes_ipv6(const DHT *dht, IP_Port ip_port, const uint8_t *public | |||
1344 | static int handle_getnodes(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata) | 1344 | static int handle_getnodes(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata) |
1345 | { | 1345 | { |
1346 | if (length != (CRYPTO_SIZE + CRYPTO_MAC_SIZE + sizeof(uint64_t))) { | 1346 | if (length != (CRYPTO_SIZE + CRYPTO_MAC_SIZE + sizeof(uint64_t))) { |
1347 | return 1; | 1347 | return true; |
1348 | } | 1348 | } |
1349 | 1349 | ||
1350 | DHT *dht = (DHT *)object; | 1350 | DHT *const dht = (DHT *)object; |
1351 | 1351 | ||
1352 | /* Check if packet is from ourself. */ | 1352 | /* Check if packet is from ourself. */ |
1353 | if (id_equal(packet + 1, dht->self_public_key)) { | 1353 | if (id_equal(packet + 1, dht->self_public_key)) { |
1354 | return 1; | 1354 | return true; |
1355 | } | 1355 | } |
1356 | 1356 | ||
1357 | uint8_t plain[CRYPTO_NODE_SIZE]; | 1357 | uint8_t plain[CRYPTO_NODE_SIZE]; |
1358 | uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]; | 1358 | uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]; |
1359 | 1359 | ||
1360 | DHT_get_shared_key_recv(dht, shared_key, packet + 1); | 1360 | DHT_get_shared_key_recv(dht, shared_key, packet + 1); |
1361 | int len = decrypt_data_symmetric( | 1361 | const int len = decrypt_data_symmetric( |
1362 | shared_key, | 1362 | shared_key, |
1363 | packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, | 1363 | packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, |
1364 | packet + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE, | 1364 | packet + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE, |
1365 | CRYPTO_NODE_SIZE + CRYPTO_MAC_SIZE, | 1365 | CRYPTO_NODE_SIZE + CRYPTO_MAC_SIZE, |
1366 | plain); | 1366 | plain); |
1367 | 1367 | ||
1368 | if (len != CRYPTO_NODE_SIZE) { | 1368 | if (len != CRYPTO_NODE_SIZE) { |
1369 | return 1; | 1369 | return true; |
1370 | } | 1370 | } |
1371 | 1371 | ||
1372 | sendnodes_ipv6(dht, source, packet + 1, plain, plain + CRYPTO_PUBLIC_KEY_SIZE, sizeof(uint64_t), shared_key); | 1372 | sendnodes_ipv6(dht, source, packet + 1, plain, plain + CRYPTO_PUBLIC_KEY_SIZE, sizeof(uint64_t), shared_key); |
1373 | 1373 | ||
1374 | ping_add(dht->ping, packet + 1, source); | 1374 | ping_add(dht->ping, packet + 1, source); |
1375 | 1375 | ||
1376 | return 0; | 1376 | return false; |
1377 | } | 1377 | } |
1378 | /* return 0 if no | 1378 | |
1379 | return 1 if yes */ | 1379 | /* return false if no |
1380 | static uint8_t sent_getnode_to_node(DHT *dht, const uint8_t *public_key, IP_Port node_ip_port, uint64_t ping_id, | 1380 | return true if yes */ |
1381 | Node_format *sendback_node) | 1381 | static bool sent_getnode_to_node(DHT *dht, const uint8_t *public_key, IP_Port node_ip_port, uint64_t ping_id, |
1382 | Node_format *sendback_node) | ||
1382 | { | 1383 | { |
1383 | uint8_t data[sizeof(Node_format) * 2]; | 1384 | uint8_t data[sizeof(Node_format) * 2]; |
1384 | 1385 | ||
@@ -1387,17 +1388,17 @@ static uint8_t sent_getnode_to_node(DHT *dht, const uint8_t *public_key, IP_Port | |||
1387 | } else if (ping_array_check(dht->dht_harden_ping_array, data, sizeof(data), ping_id) == sizeof(data)) { | 1388 | } else if (ping_array_check(dht->dht_harden_ping_array, data, sizeof(data), ping_id) == sizeof(data)) { |
1388 | memcpy(sendback_node, data + sizeof(Node_format), sizeof(Node_format)); | 1389 | memcpy(sendback_node, data + sizeof(Node_format), sizeof(Node_format)); |
1389 | } else { | 1390 | } else { |
1390 | return 0; | 1391 | return false; |
1391 | } | 1392 | } |
1392 | 1393 | ||
1393 | Node_format test; | 1394 | Node_format test; |
1394 | memcpy(&test, data, sizeof(Node_format)); | 1395 | memcpy(&test, data, sizeof(Node_format)); |
1395 | 1396 | ||
1396 | if (!ipport_equal(&test.ip_port, &node_ip_port) || !id_equal(test.public_key, public_key)) { | 1397 | if (!ipport_equal(&test.ip_port, &node_ip_port) || !id_equal(test.public_key, public_key)) { |
1397 | return 0; | 1398 | return false; |
1398 | } | 1399 | } |
1399 | 1400 | ||
1400 | return 1; | 1401 | return true; |
1401 | } | 1402 | } |
1402 | 1403 | ||
1403 | /* Function is needed in following functions. */ | 1404 | /* Function is needed in following functions. */ |
@@ -1407,14 +1408,14 @@ static int send_hardening_getnode_res(const DHT *dht, const Node_format *sendto, | |||
1407 | static int handle_sendnodes_core(void *object, IP_Port source, const uint8_t *packet, uint16_t length, | 1408 | static int handle_sendnodes_core(void *object, IP_Port source, const uint8_t *packet, uint16_t length, |
1408 | Node_format *plain_nodes, uint16_t size_plain_nodes, uint32_t *num_nodes_out) | 1409 | Node_format *plain_nodes, uint16_t size_plain_nodes, uint32_t *num_nodes_out) |
1409 | { | 1410 | { |
1410 | DHT *dht = (DHT *)object; | 1411 | DHT *const dht = (DHT *)object; |
1411 | uint32_t cid_size = 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + 1 + sizeof(uint64_t) + CRYPTO_MAC_SIZE; | 1412 | const uint32_t cid_size = 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + 1 + sizeof(uint64_t) + CRYPTO_MAC_SIZE; |
1412 | 1413 | ||
1413 | if (length < cid_size) { /* too short */ | 1414 | if (length < cid_size) { /* too short */ |
1414 | return 1; | 1415 | return 1; |
1415 | } | 1416 | } |
1416 | 1417 | ||
1417 | uint32_t data_size = length - cid_size; | 1418 | const uint32_t data_size = length - cid_size; |
1418 | 1419 | ||
1419 | if (data_size == 0) { | 1420 | if (data_size == 0) { |
1420 | return 1; | 1421 | return 1; |
@@ -1427,12 +1428,12 @@ static int handle_sendnodes_core(void *object, IP_Port source, const uint8_t *pa | |||
1427 | VLA(uint8_t, plain, 1 + data_size + sizeof(uint64_t)); | 1428 | VLA(uint8_t, plain, 1 + data_size + sizeof(uint64_t)); |
1428 | uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]; | 1429 | uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]; |
1429 | DHT_get_shared_key_sent(dht, shared_key, packet + 1); | 1430 | DHT_get_shared_key_sent(dht, shared_key, packet + 1); |
1430 | int len = decrypt_data_symmetric( | 1431 | const int len = decrypt_data_symmetric( |
1431 | shared_key, | 1432 | shared_key, |
1432 | packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, | 1433 | packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, |
1433 | packet + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE, | 1434 | packet + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE, |
1434 | 1 + data_size + sizeof(uint64_t) + CRYPTO_MAC_SIZE, | 1435 | 1 + data_size + sizeof(uint64_t) + CRYPTO_MAC_SIZE, |
1435 | plain); | 1436 | plain); |
1436 | 1437 | ||
1437 | if ((unsigned int)len != SIZEOF_VLA(plain)) { | 1438 | if ((unsigned int)len != SIZEOF_VLA(plain)) { |
1438 | return 1; | 1439 | return 1; |
@@ -1452,7 +1453,7 @@ static int handle_sendnodes_core(void *object, IP_Port source, const uint8_t *pa | |||
1452 | } | 1453 | } |
1453 | 1454 | ||
1454 | uint16_t length_nodes = 0; | 1455 | uint16_t length_nodes = 0; |
1455 | int num_nodes = unpack_nodes(plain_nodes, plain[0], &length_nodes, plain + 1, data_size, 0); | 1456 | const int num_nodes = unpack_nodes(plain_nodes, plain[0], &length_nodes, plain + 1, data_size, 0); |
1456 | 1457 | ||
1457 | if (length_nodes != data_size) { | 1458 | if (length_nodes != data_size) { |
1458 | return 1; | 1459 | return 1; |
@@ -1477,7 +1478,7 @@ static int handle_sendnodes_core(void *object, IP_Port source, const uint8_t *pa | |||
1477 | 1478 | ||
1478 | static int handle_sendnodes_ipv6(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata) | 1479 | static int handle_sendnodes_ipv6(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata) |
1479 | { | 1480 | { |
1480 | DHT *dht = (DHT *)object; | 1481 | DHT *const dht = (DHT *)object; |
1481 | Node_format plain_nodes[MAX_SENT_NODES]; | 1482 | Node_format plain_nodes[MAX_SENT_NODES]; |
1482 | uint32_t num_nodes; | 1483 | uint32_t num_nodes; |
1483 | 1484 | ||
@@ -1505,12 +1506,12 @@ static int handle_sendnodes_ipv6(void *object, IP_Port source, const uint8_t *pa | |||
1505 | int DHT_addfriend(DHT *dht, const uint8_t *public_key, void (*ip_callback)(void *data, int32_t number, IP_Port), | 1506 | int DHT_addfriend(DHT *dht, const uint8_t *public_key, void (*ip_callback)(void *data, int32_t number, IP_Port), |
1506 | void *data, int32_t number, uint16_t *lock_count) | 1507 | void *data, int32_t number, uint16_t *lock_count) |
1507 | { | 1508 | { |
1508 | uint32_t friend_num = index_of_friend_pk(dht->friends_list, dht->num_friends, public_key); | 1509 | const uint32_t friend_num = index_of_friend_pk(dht->friends_list, dht->num_friends, public_key); |
1509 | 1510 | ||
1510 | uint16_t lock_num; | 1511 | uint16_t lock_num; |
1511 | 1512 | ||
1512 | if (friend_num != UINT32_MAX) { /* Is friend already in DHT? */ | 1513 | if (friend_num != UINT32_MAX) { /* Is friend already in DHT? */ |
1513 | DHT_Friend *dht_friend = &dht->friends_list[friend_num]; | 1514 | DHT_Friend *const dht_friend = &dht->friends_list[friend_num]; |
1514 | 1515 | ||
1515 | if (dht_friend->lock_count == DHT_FRIEND_MAX_LOCKS) { | 1516 | if (dht_friend->lock_count == DHT_FRIEND_MAX_LOCKS) { |
1516 | return -1; | 1517 | return -1; |
@@ -1529,14 +1530,14 @@ int DHT_addfriend(DHT *dht, const uint8_t *public_key, void (*ip_callback)(void | |||
1529 | return 0; | 1530 | return 0; |
1530 | } | 1531 | } |
1531 | 1532 | ||
1532 | DHT_Friend *temp = (DHT_Friend *)realloc(dht->friends_list, sizeof(DHT_Friend) * (dht->num_friends + 1)); | 1533 | DHT_Friend *const temp = (DHT_Friend *)realloc(dht->friends_list, sizeof(DHT_Friend) * (dht->num_friends + 1)); |
1533 | 1534 | ||
1534 | if (temp == nullptr) { | 1535 | if (temp == nullptr) { |
1535 | return -1; | 1536 | return -1; |
1536 | } | 1537 | } |
1537 | 1538 | ||
1538 | dht->friends_list = temp; | 1539 | dht->friends_list = temp; |
1539 | DHT_Friend *dht_friend = &dht->friends_list[dht->num_friends]; | 1540 | DHT_Friend *const dht_friend = &dht->friends_list[dht->num_friends]; |
1540 | memset(dht_friend, 0, sizeof(DHT_Friend)); | 1541 | memset(dht_friend, 0, sizeof(DHT_Friend)); |
1541 | memcpy(dht_friend->public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); | 1542 | memcpy(dht_friend->public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); |
1542 | 1543 | ||
@@ -1560,13 +1561,13 @@ int DHT_addfriend(DHT *dht, const uint8_t *public_key, void (*ip_callback)(void | |||
1560 | 1561 | ||
1561 | int DHT_delfriend(DHT *dht, const uint8_t *public_key, uint16_t lock_count) | 1562 | int DHT_delfriend(DHT *dht, const uint8_t *public_key, uint16_t lock_count) |
1562 | { | 1563 | { |
1563 | uint32_t friend_num = index_of_friend_pk(dht->friends_list, dht->num_friends, public_key); | 1564 | const uint32_t friend_num = index_of_friend_pk(dht->friends_list, dht->num_friends, public_key); |
1564 | 1565 | ||
1565 | if (friend_num == UINT32_MAX) { | 1566 | if (friend_num == UINT32_MAX) { |
1566 | return -1; | 1567 | return -1; |
1567 | } | 1568 | } |
1568 | 1569 | ||
1569 | DHT_Friend *dht_friend = &dht->friends_list[friend_num]; | 1570 | DHT_Friend *const dht_friend = &dht->friends_list[friend_num]; |
1570 | --dht_friend->lock_count; | 1571 | --dht_friend->lock_count; |
1571 | 1572 | ||
1572 | if (dht_friend->lock_count && lock_count) { /* DHT friend is still in use.*/ | 1573 | if (dht_friend->lock_count && lock_count) { /* DHT friend is still in use.*/ |
@@ -1591,7 +1592,7 @@ int DHT_delfriend(DHT *dht, const uint8_t *public_key, uint16_t lock_count) | |||
1591 | return 0; | 1592 | return 0; |
1592 | } | 1593 | } |
1593 | 1594 | ||
1594 | DHT_Friend *temp = (DHT_Friend *)realloc(dht->friends_list, sizeof(DHT_Friend) * (dht->num_friends)); | 1595 | DHT_Friend *const temp = (DHT_Friend *)realloc(dht->friends_list, sizeof(DHT_Friend) * (dht->num_friends)); |
1595 | 1596 | ||
1596 | if (temp == nullptr) { | 1597 | if (temp == nullptr) { |
1597 | return -1; | 1598 | return -1; |
@@ -1607,24 +1608,24 @@ int DHT_getfriendip(const DHT *dht, const uint8_t *public_key, IP_Port *ip_port) | |||
1607 | ip_reset(&ip_port->ip); | 1608 | ip_reset(&ip_port->ip); |
1608 | ip_port->port = 0; | 1609 | ip_port->port = 0; |
1609 | 1610 | ||
1610 | uint32_t friend_index = index_of_friend_pk(dht->friends_list, dht->num_friends, public_key); | 1611 | const uint32_t friend_index = index_of_friend_pk(dht->friends_list, dht->num_friends, public_key); |
1611 | 1612 | ||
1612 | if (friend_index == UINT32_MAX) { | 1613 | if (friend_index == UINT32_MAX) { |
1613 | return -1; | 1614 | return -1; |
1614 | } | 1615 | } |
1615 | 1616 | ||
1616 | DHT_Friend *frnd = &dht->friends_list[friend_index]; | 1617 | DHT_Friend *const frnd = &dht->friends_list[friend_index]; |
1617 | uint32_t client_index = index_of_client_pk(frnd->client_list, MAX_FRIEND_CLIENTS, public_key); | 1618 | const uint32_t client_index = index_of_client_pk(frnd->client_list, MAX_FRIEND_CLIENTS, public_key); |
1618 | 1619 | ||
1619 | if (client_index == -1) { | 1620 | if (client_index == -1) { |
1620 | return 0; | 1621 | return 0; |
1621 | } | 1622 | } |
1622 | 1623 | ||
1623 | Client_data *client = &frnd->client_list[client_index]; | 1624 | const Client_data *const client = &frnd->client_list[client_index]; |
1624 | IPPTsPng *assocs[ASSOC_COUNT] = { &client->assoc6, &client->assoc4 }; | 1625 | const IPPTsPng *const assocs[ASSOC_COUNT] = { &client->assoc6, &client->assoc4 }; |
1625 | 1626 | ||
1626 | for (size_t i = 0; i < ASSOC_COUNT; i++) { | 1627 | for (size_t i = 0; i < ASSOC_COUNT; i++) { |
1627 | IPPTsPng *assoc = assocs[i]; | 1628 | const IPPTsPng *const assoc = assocs[i]; |
1628 | 1629 | ||
1629 | if (!is_timeout(assoc->timestamp, BAD_NODE_TIMEOUT)) { | 1630 | if (!is_timeout(assoc->timestamp, BAD_NODE_TIMEOUT)) { |
1630 | *ip_port = assoc->ip_port; | 1631 | *ip_port = assoc->ip_port; |
@@ -1640,13 +1641,13 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co | |||
1640 | Client_data *list, uint32_t list_count, uint32_t *bootstrap_times, bool sortable) | 1641 | Client_data *list, uint32_t list_count, uint32_t *bootstrap_times, bool sortable) |
1641 | { | 1642 | { |
1642 | uint8_t not_kill = 0; | 1643 | uint8_t not_kill = 0; |
1643 | uint64_t temp_time = unix_time(); | 1644 | const uint64_t temp_time = unix_time(); |
1644 | 1645 | ||
1645 | uint32_t num_nodes = 0; | 1646 | uint32_t num_nodes = 0; |
1646 | VLA(Client_data *, client_list, list_count * 2); | 1647 | VLA(Client_data *, client_list, list_count * 2); |
1647 | VLA(IPPTsPng *, assoc_list, list_count * 2); | 1648 | VLA(IPPTsPng *, assoc_list, list_count * 2); |
1648 | unsigned int sort = 0; | 1649 | unsigned int sort = 0; |
1649 | bool sort_ok = 0; | 1650 | bool sort_ok = false; |
1650 | 1651 | ||
1651 | for (uint32_t i = 0; i < list_count; i++) { | 1652 | for (uint32_t i = 0; i < list_count; i++) { |
1652 | /* If node is not dead. */ | 1653 | /* If node is not dead. */ |
@@ -1677,7 +1678,7 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co | |||
1677 | 1678 | ||
1678 | /* Timed out should be at beginning, if they are not, sort the list. */ | 1679 | /* Timed out should be at beginning, if they are not, sort the list. */ |
1679 | if (sort > 1 && sort < (((i + 1) * 2) - 1)) { | 1680 | if (sort > 1 && sort < (((i + 1) * 2) - 1)) { |
1680 | sort_ok = 1; | 1681 | sort_ok = true; |
1681 | } | 1682 | } |
1682 | } | 1683 | } |
1683 | } | 1684 | } |
@@ -1688,7 +1689,7 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co | |||
1688 | } | 1689 | } |
1689 | 1690 | ||
1690 | if ((num_nodes != 0) && (is_timeout(*lastgetnode, GET_NODE_INTERVAL) || *bootstrap_times < MAX_BOOTSTRAP_TIMES)) { | 1691 | if ((num_nodes != 0) && (is_timeout(*lastgetnode, GET_NODE_INTERVAL) || *bootstrap_times < MAX_BOOTSTRAP_TIMES)) { |
1691 | uint32_t rand_node = rand() % (num_nodes); | 1692 | uint32_t rand_node = rand() % num_nodes; |
1692 | 1693 | ||
1693 | if ((num_nodes - 1) != rand_node) { | 1694 | if ((num_nodes - 1) != rand_node) { |
1694 | rand_node += rand() % (num_nodes - (rand_node + 1)); | 1695 | rand_node += rand() % (num_nodes - (rand_node + 1)); |
@@ -1709,7 +1710,7 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co | |||
1709 | static void do_DHT_friends(DHT *dht) | 1710 | static void do_DHT_friends(DHT *dht) |
1710 | { | 1711 | { |
1711 | for (size_t i = 0; i < dht->num_friends; ++i) { | 1712 | for (size_t i = 0; i < dht->num_friends; ++i) { |
1712 | DHT_Friend *dht_friend = &dht->friends_list[i]; | 1713 | DHT_Friend *const dht_friend = &dht->friends_list[i]; |
1713 | 1714 | ||
1714 | for (size_t j = 0; j < dht_friend->num_to_bootstrap; ++j) { | 1715 | for (size_t j = 0; j < dht_friend->num_to_bootstrap; ++j) { |
1715 | getnodes(dht, dht_friend->to_bootstrap[j].ip_port, dht_friend->to_bootstrap[j].public_key, dht_friend->public_key, | 1716 | getnodes(dht, dht_friend->to_bootstrap[j].ip_port, dht_friend->to_bootstrap[j].public_key, dht_friend->public_key, |
@@ -1735,29 +1736,32 @@ static void do_Close(DHT *dht) | |||
1735 | 1736 | ||
1736 | dht->num_to_bootstrap = 0; | 1737 | dht->num_to_bootstrap = 0; |
1737 | 1738 | ||
1738 | uint8_t not_killed = do_ping_and_sendnode_requests(dht, &dht->close_lastgetnodes, dht->self_public_key, | 1739 | uint8_t not_killed = do_ping_and_sendnode_requests( |
1739 | dht->close_clientlist, LCLIENT_LIST, &dht->close_bootstrap_times, 0); | 1740 | dht, &dht->close_lastgetnodes, dht->self_public_key, dht->close_clientlist, LCLIENT_LIST, &dht->close_bootstrap_times, |
1741 | 0); | ||
1740 | 1742 | ||
1741 | if (!not_killed) { | 1743 | if (not_killed != 0) { |
1742 | /* all existing nodes are at least KILL_NODE_TIMEOUT, | 1744 | return; |
1743 | * which means we are mute, as we only send packets to | 1745 | } |
1744 | * nodes NOT in KILL_NODE_TIMEOUT | ||
1745 | * | ||
1746 | * so: reset all nodes to be BAD_NODE_TIMEOUT, but not | ||
1747 | * KILL_NODE_TIMEOUT, so we at least keep trying pings */ | ||
1748 | uint64_t badonly = unix_time() - BAD_NODE_TIMEOUT; | ||
1749 | 1746 | ||
1750 | for (size_t i = 0; i < LCLIENT_LIST; i++) { | 1747 | /* all existing nodes are at least KILL_NODE_TIMEOUT, |
1751 | Client_data *client = &dht->close_clientlist[i]; | 1748 | * which means we are mute, as we only send packets to |
1749 | * nodes NOT in KILL_NODE_TIMEOUT | ||
1750 | * | ||
1751 | * so: reset all nodes to be BAD_NODE_TIMEOUT, but not | ||
1752 | * KILL_NODE_TIMEOUT, so we at least keep trying pings */ | ||
1753 | const uint64_t badonly = unix_time() - BAD_NODE_TIMEOUT; | ||
1752 | 1754 | ||
1753 | IPPTsPng *assocs[ASSOC_COUNT] = { &client->assoc6, &client->assoc4 }; | 1755 | for (size_t i = 0; i < LCLIENT_LIST; i++) { |
1756 | Client_data *const client = &dht->close_clientlist[i]; | ||
1754 | 1757 | ||
1755 | for (size_t j = 0; j < ASSOC_COUNT; j++) { | 1758 | IPPTsPng *const assocs[ASSOC_COUNT] = { &client->assoc6, &client->assoc4 }; |
1756 | IPPTsPng *assoc = assocs[j]; | ||
1757 | 1759 | ||
1758 | if (assoc->timestamp) { | 1760 | for (size_t j = 0; j < ASSOC_COUNT; j++) { |
1759 | assoc->timestamp = badonly; | 1761 | IPPTsPng *const assoc = assocs[j]; |
1760 | } | 1762 | |
1763 | if (assoc->timestamp) { | ||
1764 | assoc->timestamp = badonly; | ||
1761 | } | 1765 | } |
1762 | } | 1766 | } |
1763 | } | 1767 | } |
@@ -1810,11 +1814,11 @@ int route_packet(const DHT *dht, const uint8_t *public_key, const uint8_t *packe | |||
1810 | { | 1814 | { |
1811 | for (uint32_t i = 0; i < LCLIENT_LIST; ++i) { | 1815 | for (uint32_t i = 0; i < LCLIENT_LIST; ++i) { |
1812 | if (id_equal(public_key, dht->close_clientlist[i].public_key)) { | 1816 | if (id_equal(public_key, dht->close_clientlist[i].public_key)) { |
1813 | const Client_data *client = &dht->close_clientlist[i]; | 1817 | const Client_data *const client = &dht->close_clientlist[i]; |
1814 | const IPPTsPng *assocs[ASSOC_COUNT] = { &client->assoc6, &client->assoc4 }; | 1818 | const IPPTsPng *const assocs[ASSOC_COUNT] = { &client->assoc6, &client->assoc4 }; |
1815 | 1819 | ||
1816 | for (size_t j = 0; j < ASSOC_COUNT; j++) { | 1820 | for (size_t j = 0; j < ASSOC_COUNT; j++) { |
1817 | const IPPTsPng *assoc = assocs[j]; | 1821 | const IPPTsPng *const assoc = assocs[j]; |
1818 | 1822 | ||
1819 | if (ip_isset(&assoc->ip_port.ip)) { | 1823 | if (ip_isset(&assoc->ip_port.ip)) { |
1820 | return sendpacket(dht->net, assoc->ip_port, packet, length); | 1824 | return sendpacket(dht->net, assoc->ip_port, packet, length); |
@@ -1841,15 +1845,14 @@ static int friend_iplist(const DHT *dht, IP_Port *ip_portlist, uint16_t friend_n | |||
1841 | return -1; | 1845 | return -1; |
1842 | } | 1846 | } |
1843 | 1847 | ||
1844 | DHT_Friend *dht_friend = &dht->friends_list[friend_num]; | 1848 | const DHT_Friend *const dht_friend = &dht->friends_list[friend_num]; |
1845 | Client_data *client; | ||
1846 | IP_Port ipv4s[MAX_FRIEND_CLIENTS]; | 1849 | IP_Port ipv4s[MAX_FRIEND_CLIENTS]; |
1847 | int num_ipv4s = 0; | 1850 | int num_ipv4s = 0; |
1848 | IP_Port ipv6s[MAX_FRIEND_CLIENTS]; | 1851 | IP_Port ipv6s[MAX_FRIEND_CLIENTS]; |
1849 | int num_ipv6s = 0; | 1852 | int num_ipv6s = 0; |
1850 | 1853 | ||
1851 | for (size_t i = 0; i < MAX_FRIEND_CLIENTS; ++i) { | 1854 | for (size_t i = 0; i < MAX_FRIEND_CLIENTS; ++i) { |
1852 | client = &dht_friend->client_list[i]; | 1855 | const Client_data *const client = &dht_friend->client_list[i]; |
1853 | 1856 | ||
1854 | /* If ip is not zero and node is good. */ | 1857 | /* If ip is not zero and node is good. */ |
1855 | if (ip_isset(&client->assoc4.ret_ip_port.ip) && !is_timeout(client->assoc4.ret_timestamp, BAD_NODE_TIMEOUT)) { | 1858 | if (ip_isset(&client->assoc4.ret_ip_port.ip) && !is_timeout(client->assoc4.ret_timestamp, BAD_NODE_TIMEOUT)) { |
@@ -1910,7 +1913,7 @@ static int friend_iplist(const DHT *dht, IP_Port *ip_portlist, uint16_t friend_n | |||
1910 | */ | 1913 | */ |
1911 | int route_tofriend(const DHT *dht, const uint8_t *friend_id, const uint8_t *packet, uint16_t length) | 1914 | int route_tofriend(const DHT *dht, const uint8_t *friend_id, const uint8_t *packet, uint16_t length) |
1912 | { | 1915 | { |
1913 | uint32_t num = index_of_friend_pk(dht->friends_list, dht->num_friends, friend_id); | 1916 | const uint32_t num = index_of_friend_pk(dht->friends_list, dht->num_friends, friend_id); |
1914 | 1917 | ||
1915 | if (num == UINT32_MAX) { | 1918 | if (num == UINT32_MAX) { |
1916 | return 0; | 1919 | return 0; |
@@ -1920,14 +1923,13 @@ int route_tofriend(const DHT *dht, const uint8_t *friend_id, const uint8_t *pack | |||
1920 | uint8_t friend_sent[MAX_FRIEND_CLIENTS] = {0}; | 1923 | uint8_t friend_sent[MAX_FRIEND_CLIENTS] = {0}; |
1921 | 1924 | ||
1922 | IP_Port ip_list[MAX_FRIEND_CLIENTS]; | 1925 | IP_Port ip_list[MAX_FRIEND_CLIENTS]; |
1923 | int ip_num = friend_iplist(dht, ip_list, num); | 1926 | const int ip_num = friend_iplist(dht, ip_list, num); |
1924 | 1927 | ||
1925 | if (ip_num < (MAX_FRIEND_CLIENTS / 4)) { | 1928 | if (ip_num < (MAX_FRIEND_CLIENTS / 4)) { |
1926 | return 0; /* Reason for that? */ | 1929 | return 0; /* Reason for that? */ |
1927 | } | 1930 | } |
1928 | 1931 | ||
1929 | DHT_Friend *dht_friend = &dht->friends_list[num]; | 1932 | const DHT_Friend *const dht_friend = &dht->friends_list[num]; |
1930 | Client_data *client; | ||
1931 | 1933 | ||
1932 | /* extra legwork, because having the outside allocating the space for us | 1934 | /* extra legwork, because having the outside allocating the space for us |
1933 | * is *usually* good(tm) (bites us in the behind in this case though) */ | 1935 | * is *usually* good(tm) (bites us in the behind in this case though) */ |
@@ -1937,16 +1939,15 @@ int route_tofriend(const DHT *dht, const uint8_t *friend_id, const uint8_t *pack | |||
1937 | continue; | 1939 | continue; |
1938 | } | 1940 | } |
1939 | 1941 | ||
1940 | client = &dht_friend->client_list[i]; | 1942 | const Client_data *const client = &dht_friend->client_list[i]; |
1941 | 1943 | const IPPTsPng *const assocs[ASSOC_COUNT] = { &client->assoc4, &client->assoc6 }; | |
1942 | const IPPTsPng *assocs[ASSOC_COUNT] = { &client->assoc4, &client->assoc6 }; | ||
1943 | 1944 | ||
1944 | for (size_t j = 0; j < ASSOC_COUNT; j++) { | 1945 | for (size_t j = 0; j < ASSOC_COUNT; j++) { |
1945 | const IPPTsPng *assoc = assocs[j]; | 1946 | const IPPTsPng *const assoc = assocs[j]; |
1946 | 1947 | ||
1947 | /* If ip is not zero and node is good. */ | 1948 | /* If ip is not zero and node is good. */ |
1948 | if (ip_isset(&assoc->ret_ip_port.ip) && !is_timeout(assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { | 1949 | if (ip_isset(&assoc->ret_ip_port.ip) && !is_timeout(assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { |
1949 | int retval = sendpacket(dht->net, assoc->ip_port, packet, length); | 1950 | const int retval = sendpacket(dht->net, assoc->ip_port, packet, length); |
1950 | 1951 | ||
1951 | if ((unsigned int)retval == length) { | 1952 | if ((unsigned int)retval == length) { |
1952 | ++sent; | 1953 | ++sent; |
@@ -1965,14 +1966,13 @@ int route_tofriend(const DHT *dht, const uint8_t *friend_id, const uint8_t *pack | |||
1965 | */ | 1966 | */ |
1966 | static int routeone_tofriend(DHT *dht, const uint8_t *friend_id, const uint8_t *packet, uint16_t length) | 1967 | static int routeone_tofriend(DHT *dht, const uint8_t *friend_id, const uint8_t *packet, uint16_t length) |
1967 | { | 1968 | { |
1968 | uint32_t num = index_of_friend_pk(dht->friends_list, dht->num_friends, friend_id); | 1969 | const uint32_t num = index_of_friend_pk(dht->friends_list, dht->num_friends, friend_id); |
1969 | 1970 | ||
1970 | if (num == UINT32_MAX) { | 1971 | if (num == UINT32_MAX) { |
1971 | return 0; | 1972 | return 0; |
1972 | } | 1973 | } |
1973 | 1974 | ||
1974 | DHT_Friend *dht_friend = &dht->friends_list[num]; | 1975 | const DHT_Friend *const dht_friend = &dht->friends_list[num]; |
1975 | Client_data *client; | ||
1976 | 1976 | ||
1977 | IP_Port ip_list[MAX_FRIEND_CLIENTS * 2]; | 1977 | IP_Port ip_list[MAX_FRIEND_CLIENTS * 2]; |
1978 | int n = 0; | 1978 | int n = 0; |
@@ -1981,9 +1981,8 @@ static int routeone_tofriend(DHT *dht, const uint8_t *friend_id, const uint8_t * | |||
1981 | * is *usually* good(tm) (bites us in the behind in this case though) */ | 1981 | * is *usually* good(tm) (bites us in the behind in this case though) */ |
1982 | 1982 | ||
1983 | for (uint32_t i = 0; i < MAX_FRIEND_CLIENTS; ++i) { | 1983 | for (uint32_t i = 0; i < MAX_FRIEND_CLIENTS; ++i) { |
1984 | client = &dht_friend->client_list[i]; | 1984 | const Client_data *const client = &dht_friend->client_list[i]; |
1985 | 1985 | const IPPTsPng *const assocs[ASSOC_COUNT] = { &client->assoc4, &client->assoc6 }; | |
1986 | const IPPTsPng *assocs[ASSOC_COUNT] = { &client->assoc4, &client->assoc6 }; | ||
1987 | 1986 | ||
1988 | for (size_t j = 0; j < ASSOC_COUNT; j++) { | 1987 | for (size_t j = 0; j < ASSOC_COUNT; j++) { |
1989 | const IPPTsPng *assoc = assocs[j]; | 1988 | const IPPTsPng *assoc = assocs[j]; |
@@ -2000,7 +1999,7 @@ static int routeone_tofriend(DHT *dht, const uint8_t *friend_id, const uint8_t * | |||
2000 | return 0; | 1999 | return 0; |
2001 | } | 2000 | } |
2002 | 2001 | ||
2003 | int retval = sendpacket(dht->net, ip_list[rand() % n], packet, length); | 2002 | const int retval = sendpacket(dht->net, ip_list[rand() % n], packet, length); |
2004 | 2003 | ||
2005 | if ((unsigned int)retval == length) { | 2004 | if ((unsigned int)retval == length) { |
2006 | return 1; | 2005 | return 1; |
@@ -2022,8 +2021,9 @@ static int send_NATping(DHT *dht, const uint8_t *public_key, uint64_t ping_id, u | |||
2022 | data[0] = type; | 2021 | data[0] = type; |
2023 | memcpy(data + 1, &ping_id, sizeof(uint64_t)); | 2022 | memcpy(data + 1, &ping_id, sizeof(uint64_t)); |
2024 | /* 254 is NAT ping request packet id */ | 2023 | /* 254 is NAT ping request packet id */ |
2025 | int len = create_request(dht->self_public_key, dht->self_secret_key, packet, public_key, data, | 2024 | const int len = create_request( |
2026 | sizeof(uint64_t) + 1, CRYPTO_PACKET_NAT_PING); | 2025 | dht->self_public_key, dht->self_secret_key, packet, public_key, data, |
2026 | sizeof(uint64_t) + 1, CRYPTO_PACKET_NAT_PING); | ||
2027 | 2027 | ||
2028 | if (len == -1) { | 2028 | if (len == -1) { |
2029 | return -1; | 2029 | return -1; |
@@ -2050,7 +2050,7 @@ static int handle_NATping(void *object, IP_Port source, const uint8_t *source_pu | |||
2050 | return 1; | 2050 | return 1; |
2051 | } | 2051 | } |
2052 | 2052 | ||
2053 | DHT *dht = (DHT *)object; | 2053 | DHT *const dht = (DHT *)object; |
2054 | uint64_t ping_id; | 2054 | uint64_t ping_id; |
2055 | memcpy(&ping_id, packet + 1, sizeof(uint64_t)); | 2055 | memcpy(&ping_id, packet + 1, sizeof(uint64_t)); |
2056 | 2056 | ||
@@ -2060,7 +2060,7 @@ static int handle_NATping(void *object, IP_Port source, const uint8_t *source_pu | |||
2060 | return 1; | 2060 | return 1; |
2061 | } | 2061 | } |
2062 | 2062 | ||
2063 | DHT_Friend *dht_friend = &dht->friends_list[friendnumber]; | 2063 | DHT_Friend *const dht_friend = &dht->friends_list[friendnumber]; |
2064 | 2064 | ||
2065 | if (packet[0] == NAT_PING_REQUEST) { | 2065 | if (packet[0] == NAT_PING_REQUEST) { |
2066 | /* 1 is reply */ | 2066 | /* 1 is reply */ |
@@ -2142,7 +2142,7 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports, | |||
2142 | return; | 2142 | return; |
2143 | } | 2143 | } |
2144 | 2144 | ||
2145 | uint16_t first_port = port_list[0]; | 2145 | const uint16_t first_port = port_list[0]; |
2146 | uint32_t i; | 2146 | uint32_t i; |
2147 | 2147 | ||
2148 | for (i = 0; i < numports; ++i) { | 2148 | for (i = 0; i < numports; ++i) { |
@@ -2159,11 +2159,11 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports, | |||
2159 | } else { | 2159 | } else { |
2160 | for (i = 0; i < MAX_PUNCHING_PORTS; ++i) { | 2160 | for (i = 0; i < MAX_PUNCHING_PORTS; ++i) { |
2161 | /* TODO(irungentoo): Improve port guessing algorithm. */ | 2161 | /* TODO(irungentoo): Improve port guessing algorithm. */ |
2162 | uint32_t it = i + dht->friends_list[friend_num].nat.punching_index; | 2162 | const uint32_t it = i + dht->friends_list[friend_num].nat.punching_index; |
2163 | int8_t sign = (it % 2) ? -1 : 1; | 2163 | const int8_t sign = (it % 2) ? -1 : 1; |
2164 | uint32_t delta = sign * (it / (2 * numports)); | 2164 | const uint32_t delta = sign * (it / (2 * numports)); |
2165 | uint32_t index = (it / 2) % numports; | 2165 | const uint32_t index = (it / 2) % numports; |
2166 | uint16_t port = port_list[index] + delta; | 2166 | const uint16_t port = port_list[index] + delta; |
2167 | IP_Port pinging; | 2167 | IP_Port pinging; |
2168 | ip_copy(&pinging.ip, &ip); | 2168 | ip_copy(&pinging.ip, &ip); |
2169 | pinging.port = net_htons(port); | 2169 | pinging.port = net_htons(port); |
@@ -2174,7 +2174,7 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports, | |||
2174 | } | 2174 | } |
2175 | 2175 | ||
2176 | if (dht->friends_list[friend_num].nat.tries > MAX_NORMAL_PUNCHING_TRIES) { | 2176 | if (dht->friends_list[friend_num].nat.tries > MAX_NORMAL_PUNCHING_TRIES) { |
2177 | uint16_t port = 1024; | 2177 | const uint16_t port = 1024; |
2178 | IP_Port pinging; | 2178 | IP_Port pinging; |
2179 | ip_copy(&pinging.ip, &ip); | 2179 | ip_copy(&pinging.ip, &ip); |
2180 | 2180 | ||
@@ -2192,11 +2192,11 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports, | |||
2192 | 2192 | ||
2193 | static void do_NAT(DHT *dht) | 2193 | static void do_NAT(DHT *dht) |
2194 | { | 2194 | { |
2195 | uint64_t temp_time = unix_time(); | 2195 | const uint64_t temp_time = unix_time(); |
2196 | 2196 | ||
2197 | for (uint32_t i = 0; i < dht->num_friends; ++i) { | 2197 | for (uint32_t i = 0; i < dht->num_friends; ++i) { |
2198 | IP_Port ip_list[MAX_FRIEND_CLIENTS]; | 2198 | IP_Port ip_list[MAX_FRIEND_CLIENTS]; |
2199 | int num = friend_iplist(dht, ip_list, i); | 2199 | const int num = friend_iplist(dht, ip_list, i); |
2200 | 2200 | ||
2201 | /* If already connected or friend is not online don't try to hole punch. */ | 2201 | /* If already connected or friend is not online don't try to hole punch. */ |
2202 | if (num < MAX_FRIEND_CLIENTS / 2) { | 2202 | if (num < MAX_FRIEND_CLIENTS / 2) { |
@@ -2212,7 +2212,7 @@ static void do_NAT(DHT *dht) | |||
2212 | dht->friends_list[i].nat.punching_timestamp + PUNCH_INTERVAL < temp_time && | 2212 | dht->friends_list[i].nat.punching_timestamp + PUNCH_INTERVAL < temp_time && |
2213 | dht->friends_list[i].nat.recvNATping_timestamp + PUNCH_INTERVAL * 2 >= temp_time) { | 2213 | dht->friends_list[i].nat.recvNATping_timestamp + PUNCH_INTERVAL * 2 >= temp_time) { |
2214 | 2214 | ||
2215 | IP ip = NAT_commonip(ip_list, num, MAX_FRIEND_CLIENTS / 2); | 2215 | const IP ip = NAT_commonip(ip_list, num, MAX_FRIEND_CLIENTS / 2); |
2216 | 2216 | ||
2217 | if (!ip_isset(&ip)) { | 2217 | if (!ip_isset(&ip)) { |
2218 | continue; | 2218 | continue; |
@@ -2225,7 +2225,7 @@ static void do_NAT(DHT *dht) | |||
2225 | } | 2225 | } |
2226 | 2226 | ||
2227 | uint16_t port_list[MAX_FRIEND_CLIENTS]; | 2227 | uint16_t port_list[MAX_FRIEND_CLIENTS]; |
2228 | uint16_t numports = NAT_getports(port_list, ip_list, num, ip); | 2228 | const uint16_t numports = NAT_getports(port_list, ip_list, num, ip); |
2229 | punch_holes(dht, ip, port_list, numports, i); | 2229 | punch_holes(dht, ip, port_list, numports, i); |
2230 | 2230 | ||
2231 | dht->friends_list[i].nat.punching_timestamp = temp_time; | 2231 | dht->friends_list[i].nat.punching_timestamp = temp_time; |
@@ -2257,8 +2257,9 @@ static int send_hardening_req(DHT *dht, Node_format *sendto, uint8_t type, uint8 | |||
2257 | uint8_t data[HARDREQ_DATA_SIZE] = {0}; | 2257 | uint8_t data[HARDREQ_DATA_SIZE] = {0}; |
2258 | data[0] = type; | 2258 | data[0] = type; |
2259 | memcpy(data + 1, contents, length); | 2259 | memcpy(data + 1, contents, length); |
2260 | int len = create_request(dht->self_public_key, dht->self_secret_key, packet, sendto->public_key, data, | 2260 | const int len = create_request( |
2261 | sizeof(data), CRYPTO_PACKET_HARDENING); | 2261 | dht->self_public_key, dht->self_secret_key, packet, sendto->public_key, |
2262 | data, sizeof(data), CRYPTO_PACKET_HARDENING); | ||
2262 | 2263 | ||
2263 | if (len == -1) { | 2264 | if (len == -1) { |
2264 | return -1; | 2265 | return -1; |
@@ -2290,8 +2291,9 @@ static int send_hardening_getnode_res(const DHT *dht, const Node_format *sendto, | |||
2290 | data[0] = CHECK_TYPE_GETNODE_RES; | 2291 | data[0] = CHECK_TYPE_GETNODE_RES; |
2291 | memcpy(data + 1, queried_client_id, CRYPTO_PUBLIC_KEY_SIZE); | 2292 | memcpy(data + 1, queried_client_id, CRYPTO_PUBLIC_KEY_SIZE); |
2292 | memcpy(data + 1 + CRYPTO_PUBLIC_KEY_SIZE, nodes_data, nodes_data_length); | 2293 | memcpy(data + 1 + CRYPTO_PUBLIC_KEY_SIZE, nodes_data, nodes_data_length); |
2293 | int len = create_request(dht->self_public_key, dht->self_secret_key, packet, sendto->public_key, data, | 2294 | const int len = create_request( |
2294 | SIZEOF_VLA(data), CRYPTO_PACKET_HARDENING); | 2295 | dht->self_public_key, dht->self_secret_key, packet, sendto->public_key, |
2296 | data, SIZEOF_VLA(data), CRYPTO_PACKET_HARDENING); | ||
2295 | 2297 | ||
2296 | if (len == -1) { | 2298 | if (len == -1) { |
2297 | return -1; | 2299 | return -1; |
@@ -2334,7 +2336,7 @@ static uint32_t have_nodes_closelist(DHT *dht, Node_format *nodes, uint16_t num) | |||
2334 | continue; | 2336 | continue; |
2335 | } | 2337 | } |
2336 | 2338 | ||
2337 | IPPTsPng *temp = get_closelist_IPPTsPng(dht, nodes[i].public_key, nodes[i].ip_port.ip.family); | 2339 | const IPPTsPng *const temp = get_closelist_IPPTsPng(dht, nodes[i].public_key, nodes[i].ip_port.ip.family); |
2338 | 2340 | ||
2339 | if (temp) { | 2341 | if (temp) { |
2340 | if (!is_timeout(temp->timestamp, BAD_NODE_TIMEOUT)) { | 2342 | if (!is_timeout(temp->timestamp, BAD_NODE_TIMEOUT)) { |
@@ -2354,7 +2356,7 @@ static uint32_t have_nodes_closelist(DHT *dht, Node_format *nodes, uint16_t num) | |||
2354 | static int handle_hardening(void *object, IP_Port source, const uint8_t *source_pubkey, const uint8_t *packet, | 2356 | static int handle_hardening(void *object, IP_Port source, const uint8_t *source_pubkey, const uint8_t *packet, |
2355 | uint16_t length, void *userdata) | 2357 | uint16_t length, void *userdata) |
2356 | { | 2358 | { |
2357 | DHT *dht = (DHT *)object; | 2359 | DHT *const dht = (DHT *)object; |
2358 | 2360 | ||
2359 | if (length < 2) { | 2361 | if (length < 2) { |
2360 | return 1; | 2362 | return 1; |
@@ -2389,7 +2391,8 @@ static int handle_hardening(void *object, IP_Port source, const uint8_t *source_ | |||
2389 | 2391 | ||
2390 | uint16_t length_nodes = length - 1 - CRYPTO_PUBLIC_KEY_SIZE; | 2392 | uint16_t length_nodes = length - 1 - CRYPTO_PUBLIC_KEY_SIZE; |
2391 | Node_format nodes[MAX_SENT_NODES]; | 2393 | Node_format nodes[MAX_SENT_NODES]; |
2392 | int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, nullptr, packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, length_nodes, 0); | 2394 | const int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, nullptr, packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, |
2395 | length_nodes, 0); | ||
2393 | 2396 | ||
2394 | /* TODO(irungentoo): MAX_SENT_NODES nodes should be returned at all times | 2397 | /* TODO(irungentoo): MAX_SENT_NODES nodes should be returned at all times |
2395 | (right now we have a small network size so it could cause problems for testing and etc..) */ | 2398 | (right now we have a small network size so it could cause problems for testing and etc..) */ |
@@ -2402,7 +2405,7 @@ static int handle_hardening(void *object, IP_Port source, const uint8_t *source_ | |||
2402 | return 1; | 2405 | return 1; |
2403 | } | 2406 | } |
2404 | 2407 | ||
2405 | IPPTsPng *temp = get_closelist_IPPTsPng(dht, packet + 1, nodes[0].ip_port.ip.family); | 2408 | IPPTsPng *const temp = get_closelist_IPPTsPng(dht, packet + 1, nodes[0].ip_port.ip.family); |
2406 | 2409 | ||
2407 | if (temp == nullptr) { | 2410 | if (temp == nullptr) { |
2408 | return 1; | 2411 | return 1; |
@@ -2434,13 +2437,13 @@ static Node_format random_node(DHT *dht, Family sa_family) | |||
2434 | uint8_t id[CRYPTO_PUBLIC_KEY_SIZE]; | 2437 | uint8_t id[CRYPTO_PUBLIC_KEY_SIZE]; |
2435 | 2438 | ||
2436 | for (uint32_t i = 0; i < CRYPTO_PUBLIC_KEY_SIZE / 4; ++i) { /* populate the id with pseudorandom bytes.*/ | 2439 | for (uint32_t i = 0; i < CRYPTO_PUBLIC_KEY_SIZE / 4; ++i) { /* populate the id with pseudorandom bytes.*/ |
2437 | uint32_t t = rand(); | 2440 | const uint32_t t = rand(); |
2438 | memcpy(id + i * sizeof(t), &t, sizeof(t)); | 2441 | memcpy(id + i * sizeof(t), &t, sizeof(t)); |
2439 | } | 2442 | } |
2440 | 2443 | ||
2441 | Node_format nodes_list[MAX_SENT_NODES]; | 2444 | Node_format nodes_list[MAX_SENT_NODES]; |
2442 | memset(nodes_list, 0, sizeof(nodes_list)); | 2445 | memset(nodes_list, 0, sizeof(nodes_list)); |
2443 | uint32_t num_nodes = get_close_nodes(dht, id, nodes_list, sa_family, 1, 0); | 2446 | const uint32_t num_nodes = get_close_nodes(dht, id, nodes_list, sa_family, 1, 0); |
2444 | 2447 | ||
2445 | if (num_nodes == 0) { | 2448 | if (num_nodes == 0) { |
2446 | return nodes_list[0]; | 2449 | return nodes_list[0]; |
@@ -2463,7 +2466,7 @@ static uint16_t list_nodes(Client_data *list, size_t length, Node_format *nodes, | |||
2463 | uint16_t count = 0; | 2466 | uint16_t count = 0; |
2464 | 2467 | ||
2465 | for (size_t i = length; i != 0; --i) { | 2468 | for (size_t i = length; i != 0; --i) { |
2466 | IPPTsPng *assoc = nullptr; | 2469 | const IPPTsPng *assoc = nullptr; |
2467 | 2470 | ||
2468 | if (!is_timeout(list[i - 1].assoc4.timestamp, BAD_NODE_TIMEOUT)) { | 2471 | if (!is_timeout(list[i - 1].assoc4.timestamp, BAD_NODE_TIMEOUT)) { |
2469 | assoc = &list[i - 1].assoc4; | 2472 | assoc = &list[i - 1].assoc4; |
@@ -2502,7 +2505,7 @@ uint16_t randfriends_nodes(DHT *dht, Node_format *nodes, uint16_t max_num) | |||
2502 | } | 2505 | } |
2503 | 2506 | ||
2504 | uint16_t count = 0; | 2507 | uint16_t count = 0; |
2505 | unsigned int r = rand(); | 2508 | const unsigned int r = rand(); |
2506 | 2509 | ||
2507 | for (size_t i = 0; i < DHT_FAKE_FRIEND_NUMBER; ++i) { | 2510 | for (size_t i = 0; i < DHT_FAKE_FRIEND_NUMBER; ++i) { |
2508 | count += list_nodes(dht->friends_list[(i + r) % DHT_FAKE_FRIEND_NUMBER].client_list, MAX_FRIEND_CLIENTS, nodes + count, | 2511 | count += list_nodes(dht->friends_list[(i + r) % DHT_FAKE_FRIEND_NUMBER].client_list, MAX_FRIEND_CLIENTS, nodes + count, |
@@ -2529,9 +2532,9 @@ uint16_t closelist_nodes(DHT *dht, Node_format *nodes, uint16_t max_num) | |||
2529 | static void do_hardening(DHT *dht) | 2532 | static void do_hardening(DHT *dht) |
2530 | { | 2533 | { |
2531 | for (uint32_t i = 0; i < LCLIENT_LIST * 2; ++i) { | 2534 | for (uint32_t i = 0; i < LCLIENT_LIST * 2; ++i) { |
2532 | IPPTsPng *cur_iptspng; | 2535 | IPPTsPng *cur_iptspng; |
2533 | Family sa_family; | 2536 | Family sa_family; |
2534 | uint8_t *public_key = dht->close_clientlist[i / 2].public_key; | 2537 | const uint8_t *const public_key = dht->close_clientlist[i / 2].public_key; |
2535 | 2538 | ||
2536 | if (i % 2 == 0) { | 2539 | if (i % 2 == 0) { |
2537 | cur_iptspng = &dht->close_clientlist[i / 2].assoc4; | 2540 | cur_iptspng = &dht->close_clientlist[i / 2].assoc4; |
@@ -2588,7 +2591,7 @@ void cryptopacket_registerhandler(DHT *dht, uint8_t byte, cryptopacket_handler_c | |||
2588 | 2591 | ||
2589 | static int cryptopacket_handle(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata) | 2592 | static int cryptopacket_handle(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata) |
2590 | { | 2593 | { |
2591 | DHT *dht = (DHT *)object; | 2594 | DHT *const dht = (DHT *)object; |
2592 | 2595 | ||
2593 | assert(packet[0] == NET_PACKET_CRYPTO); | 2596 | assert(packet[0] == NET_PACKET_CRYPTO); |
2594 | 2597 | ||
@@ -2602,7 +2605,8 @@ static int cryptopacket_handle(void *object, IP_Port source, const uint8_t *pack | |||
2602 | uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; | 2605 | uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; |
2603 | uint8_t data[MAX_CRYPTO_REQUEST_SIZE]; | 2606 | uint8_t data[MAX_CRYPTO_REQUEST_SIZE]; |
2604 | uint8_t number; | 2607 | uint8_t number; |
2605 | int len = handle_request(dht->self_public_key, dht->self_secret_key, public_key, data, &number, packet, length); | 2608 | const int len = handle_request(dht->self_public_key, dht->self_secret_key, public_key, |
2609 | data, &number, packet, length); | ||
2606 | 2610 | ||
2607 | if (len == -1 || len == 0) { | 2611 | if (len == -1 || len == 0) { |
2608 | return 1; | 2612 | return 1; |
@@ -2612,12 +2616,13 @@ static int cryptopacket_handle(void *object, IP_Port source, const uint8_t *pack | |||
2612 | return 1; | 2616 | return 1; |
2613 | } | 2617 | } |
2614 | 2618 | ||
2615 | return dht->cryptopackethandlers[number].function(dht->cryptopackethandlers[number].object, source, public_key, | 2619 | return dht->cryptopackethandlers[number].function( |
2616 | data, len, userdata); | 2620 | dht->cryptopackethandlers[number].object, source, public_key, |
2621 | data, len, userdata); | ||
2617 | } | 2622 | } |
2618 | 2623 | ||
2619 | /* If request is not for us, try routing it. */ | 2624 | /* If request is not for us, try routing it. */ |
2620 | int retval = route_packet(dht, packet + 1, packet, length); | 2625 | const int retval = route_packet(dht, packet + 1, packet, length); |
2621 | 2626 | ||
2622 | if ((unsigned int)retval == length) { | 2627 | if ((unsigned int)retval == length) { |
2623 | return 0; | 2628 | return 0; |
@@ -2637,7 +2642,7 @@ DHT *new_DHT(Logger *log, Networking_Core *net, bool holepunching_enabled) | |||
2637 | return nullptr; | 2642 | return nullptr; |
2638 | } | 2643 | } |
2639 | 2644 | ||
2640 | DHT *dht = (DHT *)calloc(1, sizeof(DHT)); | 2645 | DHT *const dht = (DHT *)calloc(1, sizeof(DHT)); |
2641 | 2646 | ||
2642 | if (dht == nullptr) { | 2647 | if (dht == nullptr) { |
2643 | return nullptr; | 2648 | return nullptr; |
@@ -2701,6 +2706,7 @@ void do_DHT(DHT *dht) | |||
2701 | #endif | 2706 | #endif |
2702 | dht->last_run = unix_time(); | 2707 | dht->last_run = unix_time(); |
2703 | } | 2708 | } |
2709 | |||
2704 | void kill_DHT(DHT *dht) | 2710 | void kill_DHT(DHT *dht) |
2705 | { | 2711 | { |
2706 | networking_registerhandler(dht->net, NET_PACKET_GET_NODES, nullptr, nullptr); | 2712 | networking_registerhandler(dht->net, NET_PACKET_GET_NODES, nullptr, nullptr); |
@@ -2727,7 +2733,8 @@ void kill_DHT(DHT *dht) | |||
2727 | /* Get the size of the DHT (for saving). */ | 2733 | /* Get the size of the DHT (for saving). */ |
2728 | uint32_t DHT_size(const DHT *dht) | 2734 | uint32_t DHT_size(const DHT *dht) |
2729 | { | 2735 | { |
2730 | uint32_t numv4 = 0, numv6 = 0; | 2736 | uint32_t numv4 = 0; |
2737 | uint32_t numv6 = 0; | ||
2731 | 2738 | ||
2732 | for (uint32_t i = 0; i < LCLIENT_LIST; ++i) { | 2739 | for (uint32_t i = 0; i < LCLIENT_LIST; ++i) { |
2733 | numv4 += (dht->close_clientlist[i].assoc4.timestamp != 0); | 2740 | numv4 += (dht->close_clientlist[i].assoc4.timestamp != 0); |
@@ -2735,7 +2742,7 @@ uint32_t DHT_size(const DHT *dht) | |||
2735 | } | 2742 | } |
2736 | 2743 | ||
2737 | for (uint32_t i = 0; i < DHT_FAKE_FRIEND_NUMBER && i < dht->num_friends; ++i) { | 2744 | for (uint32_t i = 0; i < DHT_FAKE_FRIEND_NUMBER && i < dht->num_friends; ++i) { |
2738 | DHT_Friend *fr = &dht->friends_list[i]; | 2745 | const DHT_Friend *const fr = &dht->friends_list[i]; |
2739 | 2746 | ||
2740 | for (uint32_t j = 0; j < MAX_FRIEND_CLIENTS; ++j) { | 2747 | for (uint32_t j = 0; j < MAX_FRIEND_CLIENTS; ++j) { |
2741 | numv4 += (fr->client_list[j].assoc4.timestamp != 0); | 2748 | numv4 += (fr->client_list[j].assoc4.timestamp != 0); |
@@ -2743,7 +2750,8 @@ uint32_t DHT_size(const DHT *dht) | |||
2743 | } | 2750 | } |
2744 | } | 2751 | } |
2745 | 2752 | ||
2746 | uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2; | 2753 | const uint32_t size32 = sizeof(uint32_t); |
2754 | const uint32_t sizesubhead = size32 * 2; | ||
2747 | 2755 | ||
2748 | return size32 + sizesubhead + (packed_node_size(TOX_AF_INET) * numv4) + (packed_node_size(TOX_AF_INET6) * numv6); | 2756 | return size32 + sizesubhead + (packed_node_size(TOX_AF_INET) * numv4) + (packed_node_size(TOX_AF_INET6) * numv6); |
2749 | } | 2757 | } |
@@ -2759,12 +2767,12 @@ static uint8_t *DHT_save_subheader(uint8_t *data, uint32_t len, uint16_t type) | |||
2759 | 2767 | ||
2760 | 2768 | ||
2761 | /* Save the DHT in data where data is an array of size DHT_size(). */ | 2769 | /* Save the DHT in data where data is an array of size DHT_size(). */ |
2762 | void DHT_save(DHT *dht, uint8_t *data) | 2770 | void DHT_save(const DHT *dht, uint8_t *data) |
2763 | { | 2771 | { |
2764 | host_to_lendian32(data, DHT_STATE_COOKIE_GLOBAL); | 2772 | host_to_lendian32(data, DHT_STATE_COOKIE_GLOBAL); |
2765 | data += sizeof(uint32_t); | 2773 | data += sizeof(uint32_t); |
2766 | 2774 | ||
2767 | uint8_t *old_data = data; | 2775 | uint8_t *const old_data = data; |
2768 | 2776 | ||
2769 | /* get right offset. we write the actual header later. */ | 2777 | /* get right offset. we write the actual header later. */ |
2770 | data = DHT_save_subheader(data, 0, 0); | 2778 | data = DHT_save_subheader(data, 0, 0); |
@@ -2788,7 +2796,7 @@ void DHT_save(DHT *dht, uint8_t *data) | |||
2788 | } | 2796 | } |
2789 | 2797 | ||
2790 | for (uint32_t i = 0; i < DHT_FAKE_FRIEND_NUMBER && i < dht->num_friends; ++i) { | 2798 | for (uint32_t i = 0; i < DHT_FAKE_FRIEND_NUMBER && i < dht->num_friends; ++i) { |
2791 | DHT_Friend *fr = &dht->friends_list[i]; | 2799 | const DHT_Friend *const fr = &dht->friends_list[i]; |
2792 | 2800 | ||
2793 | for (uint32_t j = 0; j < MAX_FRIEND_CLIENTS; ++j) { | 2801 | for (uint32_t j = 0; j < MAX_FRIEND_CLIENTS; ++j) { |
2794 | if (fr->client_list[j].assoc4.timestamp != 0) { | 2802 | if (fr->client_list[j].assoc4.timestamp != 0) { |
@@ -2831,7 +2839,7 @@ int DHT_connect_after_load(DHT *dht) | |||
2831 | } | 2839 | } |
2832 | 2840 | ||
2833 | for (uint32_t i = 0; i < dht->loaded_num_nodes && i < SAVE_BOOTSTAP_FREQUENCY; ++i) { | 2841 | for (uint32_t i = 0; i < dht->loaded_num_nodes && i < SAVE_BOOTSTAP_FREQUENCY; ++i) { |
2834 | unsigned int index = dht->loaded_nodes_index % dht->loaded_num_nodes; | 2842 | const unsigned int index = dht->loaded_nodes_index % dht->loaded_num_nodes; |
2835 | DHT_bootstrap(dht, dht->loaded_nodes_list[index].ip_port, dht->loaded_nodes_list[index].public_key); | 2843 | DHT_bootstrap(dht, dht->loaded_nodes_list[index].ip_port, dht->loaded_nodes_list[index].public_key); |
2836 | ++dht->loaded_nodes_index; | 2844 | ++dht->loaded_nodes_index; |
2837 | } | 2845 | } |
@@ -2844,26 +2852,25 @@ static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t le | |||
2844 | DHT *dht = (DHT *)outer; | 2852 | DHT *dht = (DHT *)outer; |
2845 | 2853 | ||
2846 | switch (type) { | 2854 | switch (type) { |
2847 | case DHT_STATE_TYPE_NODES: | 2855 | case DHT_STATE_TYPE_NODES: { |
2848 | if (length == 0) { | 2856 | if (length == 0) { |
2849 | break; | 2857 | break; |
2850 | } | 2858 | } |
2851 | 2859 | ||
2852 | { | 2860 | free(dht->loaded_nodes_list); |
2853 | free(dht->loaded_nodes_list); | 2861 | // Copy to loaded_clients_list |
2854 | // Copy to loaded_clients_list | 2862 | dht->loaded_nodes_list = (Node_format *)calloc(MAX_SAVED_DHT_NODES, sizeof(Node_format)); |
2855 | dht->loaded_nodes_list = (Node_format *)calloc(MAX_SAVED_DHT_NODES, sizeof(Node_format)); | ||
2856 | 2863 | ||
2857 | int num = unpack_nodes(dht->loaded_nodes_list, MAX_SAVED_DHT_NODES, nullptr, data, length, 0); | 2864 | const int num = unpack_nodes(dht->loaded_nodes_list, MAX_SAVED_DHT_NODES, nullptr, data, length, 0); |
2858 | 2865 | ||
2859 | if (num > 0) { | 2866 | if (num > 0) { |
2860 | dht->loaded_num_nodes = num; | 2867 | dht->loaded_num_nodes = num; |
2861 | } else { | 2868 | } else { |
2862 | dht->loaded_num_nodes = 0; | 2869 | dht->loaded_num_nodes = 0; |
2863 | } | 2870 | } |
2864 | } /* localize declarations */ | ||
2865 | 2871 | ||
2866 | break; | 2872 | break; |
2873 | } | ||
2867 | 2874 | ||
2868 | default: | 2875 | default: |
2869 | LOGGER_ERROR(dht->log, "Load state (DHT): contains unrecognized part (len %u, type %u)\n", | 2876 | LOGGER_ERROR(dht->log, "Load state (DHT): contains unrecognized part (len %u, type %u)\n", |
@@ -2881,7 +2888,7 @@ static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t le | |||
2881 | */ | 2888 | */ |
2882 | int DHT_load(DHT *dht, const uint8_t *data, uint32_t length) | 2889 | int DHT_load(DHT *dht, const uint8_t *data, uint32_t length) |
2883 | { | 2890 | { |
2884 | uint32_t cookie_len = sizeof(uint32_t); | 2891 | const uint32_t cookie_len = sizeof(uint32_t); |
2885 | 2892 | ||
2886 | if (length > cookie_len) { | 2893 | if (length > cookie_len) { |
2887 | uint32_t data32; | 2894 | uint32_t data32; |
@@ -2896,43 +2903,43 @@ int DHT_load(DHT *dht, const uint8_t *data, uint32_t length) | |||
2896 | return -1; | 2903 | return -1; |
2897 | } | 2904 | } |
2898 | 2905 | ||
2899 | /* return 0 if we are not connected to the DHT. | 2906 | /* return false if we are not connected to the DHT. |
2900 | * return 1 if we are. | 2907 | * return true if we are. |
2901 | */ | 2908 | */ |
2902 | int DHT_isconnected(const DHT *dht) | 2909 | bool DHT_isconnected(const DHT *dht) |
2903 | { | 2910 | { |
2904 | unix_time_update(); | 2911 | unix_time_update(); |
2905 | 2912 | ||
2906 | for (uint32_t i = 0; i < LCLIENT_LIST; ++i) { | 2913 | for (uint32_t i = 0; i < LCLIENT_LIST; ++i) { |
2907 | const Client_data *client = &dht->close_clientlist[i]; | 2914 | const Client_data *const client = &dht->close_clientlist[i]; |
2908 | 2915 | ||
2909 | if (!is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) || | 2916 | if (!is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) || |
2910 | !is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT)) { | 2917 | !is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT)) { |
2911 | return 1; | 2918 | return true; |
2912 | } | 2919 | } |
2913 | } | 2920 | } |
2914 | 2921 | ||
2915 | return 0; | 2922 | return false; |
2916 | } | 2923 | } |
2917 | 2924 | ||
2918 | /* return 0 if we are not connected or only connected to lan peers with the DHT. | 2925 | /* return false if we are not connected or only connected to lan peers with the DHT. |
2919 | * return 1 if we are. | 2926 | * return true if we are. |
2920 | */ | 2927 | */ |
2921 | int DHT_non_lan_connected(const DHT *dht) | 2928 | bool DHT_non_lan_connected(const DHT *dht) |
2922 | { | 2929 | { |
2923 | unix_time_update(); | 2930 | unix_time_update(); |
2924 | 2931 | ||
2925 | for (uint32_t i = 0; i < LCLIENT_LIST; ++i) { | 2932 | for (uint32_t i = 0; i < LCLIENT_LIST; ++i) { |
2926 | const Client_data *client = &dht->close_clientlist[i]; | 2933 | const Client_data *const client = &dht->close_clientlist[i]; |
2927 | 2934 | ||
2928 | if (!is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) && ip_is_lan(client->assoc4.ip_port.ip) == -1) { | 2935 | if (!is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) && ip_is_lan(client->assoc4.ip_port.ip) == -1) { |
2929 | return 1; | 2936 | return true; |
2930 | } | 2937 | } |
2931 | 2938 | ||
2932 | if (!is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT) && ip_is_lan(client->assoc6.ip_port.ip) == -1) { | 2939 | if (!is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT) && ip_is_lan(client->assoc6.ip_port.ip) == -1) { |
2933 | return 1; | 2940 | return true; |
2934 | } | 2941 | } |
2935 | } | 2942 | } |
2936 | 2943 | ||
2937 | return 0; | 2944 | return false; |
2938 | } | 2945 | } |
diff --git a/toxcore/DHT.h b/toxcore/DHT.h index 808470fc..284aa8c0 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h | |||
@@ -413,7 +413,7 @@ void cryptopacket_registerhandler(DHT *dht, uint8_t byte, cryptopacket_handler_c | |||
413 | uint32_t DHT_size(const DHT *dht); | 413 | uint32_t DHT_size(const DHT *dht); |
414 | 414 | ||
415 | /* Save the DHT in data where data is an array of size DHT_size(). */ | 415 | /* Save the DHT in data where data is an array of size DHT_size(). */ |
416 | void DHT_save(DHT *dht, uint8_t *data); | 416 | void DHT_save(const DHT *dht, uint8_t *data); |
417 | 417 | ||
418 | /* Load the DHT from data of size size. | 418 | /* Load the DHT from data of size size. |
419 | * | 419 | * |
@@ -427,15 +427,15 @@ DHT *new_DHT(Logger *log, Networking_Core *net, bool holepunching_enabled); | |||
427 | 427 | ||
428 | void kill_DHT(DHT *dht); | 428 | void kill_DHT(DHT *dht); |
429 | 429 | ||
430 | /* return 0 if we are not connected to the DHT. | 430 | /* return false if we are not connected to the DHT. |
431 | * return 1 if we are. | 431 | * return true if we are. |
432 | */ | 432 | */ |
433 | int DHT_isconnected(const DHT *dht); | 433 | bool DHT_isconnected(const DHT *dht); |
434 | 434 | ||
435 | /* return 0 if we are not connected or only connected to lan peers with the DHT. | 435 | /* return false if we are not connected or only connected to lan peers with the DHT. |
436 | * return 1 if we are. | 436 | * return true if we are. |
437 | */ | 437 | */ |
438 | int DHT_non_lan_connected(const DHT *dht); | 438 | bool DHT_non_lan_connected(const DHT *dht); |
439 | 439 | ||
440 | 440 | ||
441 | uint32_t addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *public_key); | 441 | uint32_t addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *public_key); |
diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c index 9381262b..6f5685fb 100644 --- a/toxcore/friend_connection.c +++ b/toxcore/friend_connection.c | |||
@@ -88,48 +88,38 @@ Net_Crypto *friendconn_net_crypto(const Friend_Connections *fr_c) | |||
88 | } | 88 | } |
89 | 89 | ||
90 | 90 | ||
91 | /* return 1 if the friendcon_id is not valid. | 91 | /* return true if the friendcon_id is valid. |
92 | * return 0 if the friendcon_id is valid. | 92 | * return false if the friendcon_id is not valid. |
93 | */ | 93 | */ |
94 | static uint8_t friendconn_id_not_valid(const Friend_Connections *fr_c, int friendcon_id) | 94 | static bool friendconn_id_valid(const Friend_Connections *fr_c, int friendcon_id) |
95 | { | 95 | { |
96 | if ((unsigned int)friendcon_id >= fr_c->num_cons) { | 96 | return (unsigned int)friendcon_id < fr_c->num_cons && |
97 | return 1; | 97 | fr_c->conns != nullptr && |
98 | } | 98 | fr_c->conns[friendcon_id].status != FRIENDCONN_STATUS_NONE; |
99 | |||
100 | if (fr_c->conns == nullptr) { | ||
101 | return 1; | ||
102 | } | ||
103 | |||
104 | if (fr_c->conns[friendcon_id].status == FRIENDCONN_STATUS_NONE) { | ||
105 | return 1; | ||
106 | } | ||
107 | |||
108 | return 0; | ||
109 | } | 99 | } |
110 | 100 | ||
111 | 101 | ||
112 | /* Set the size of the friend connections list to num. | 102 | /* Set the size of the friend connections list to num. |
113 | * | 103 | * |
114 | * return -1 if realloc fails. | 104 | * return false if realloc fails. |
115 | * return 0 if it succeeds. | 105 | * return true if it succeeds. |
116 | */ | 106 | */ |
117 | static int realloc_friendconns(Friend_Connections *fr_c, uint32_t num) | 107 | static bool realloc_friendconns(Friend_Connections *fr_c, uint32_t num) |
118 | { | 108 | { |
119 | if (num == 0) { | 109 | if (num == 0) { |
120 | free(fr_c->conns); | 110 | free(fr_c->conns); |
121 | fr_c->conns = nullptr; | 111 | fr_c->conns = nullptr; |
122 | return 0; | 112 | return true; |
123 | } | 113 | } |
124 | 114 | ||
125 | Friend_Conn *newgroup_cons = (Friend_Conn *)realloc(fr_c->conns, num * sizeof(Friend_Conn)); | 115 | Friend_Conn *newgroup_cons = (Friend_Conn *)realloc(fr_c->conns, num * sizeof(Friend_Conn)); |
126 | 116 | ||
127 | if (newgroup_cons == nullptr) { | 117 | if (newgroup_cons == nullptr) { |
128 | return -1; | 118 | return false; |
129 | } | 119 | } |
130 | 120 | ||
131 | fr_c->conns = newgroup_cons; | 121 | fr_c->conns = newgroup_cons; |
132 | return 0; | 122 | return true; |
133 | } | 123 | } |
134 | 124 | ||
135 | /* Create a new empty friend connection. | 125 | /* Create a new empty friend connection. |
@@ -139,22 +129,20 @@ static int realloc_friendconns(Friend_Connections *fr_c, uint32_t num) | |||
139 | */ | 129 | */ |
140 | static int create_friend_conn(Friend_Connections *fr_c) | 130 | static int create_friend_conn(Friend_Connections *fr_c) |
141 | { | 131 | { |
142 | uint32_t i; | 132 | for (uint32_t i = 0; i < fr_c->num_cons; ++i) { |
143 | |||
144 | for (i = 0; i < fr_c->num_cons; ++i) { | ||
145 | if (fr_c->conns[i].status == FRIENDCONN_STATUS_NONE) { | 133 | if (fr_c->conns[i].status == FRIENDCONN_STATUS_NONE) { |
146 | return i; | 134 | return i; |
147 | } | 135 | } |
148 | } | 136 | } |
149 | 137 | ||
150 | int id = -1; | 138 | if (!realloc_friendconns(fr_c, fr_c->num_cons + 1)) { |
151 | 139 | return -1; | |
152 | if (realloc_friendconns(fr_c, fr_c->num_cons + 1) == 0) { | ||
153 | id = fr_c->num_cons; | ||
154 | ++fr_c->num_cons; | ||
155 | memset(&fr_c->conns[id], 0, sizeof(Friend_Conn)); | ||
156 | } | 140 | } |
157 | 141 | ||
142 | const int id = fr_c->num_cons; | ||
143 | ++fr_c->num_cons; | ||
144 | memset(&fr_c->conns[id], 0, sizeof(Friend_Conn)); | ||
145 | |||
158 | return id; | 146 | return id; |
159 | } | 147 | } |
160 | 148 | ||
@@ -165,13 +153,14 @@ static int create_friend_conn(Friend_Connections *fr_c) | |||
165 | */ | 153 | */ |
166 | static int wipe_friend_conn(Friend_Connections *fr_c, int friendcon_id) | 154 | static int wipe_friend_conn(Friend_Connections *fr_c, int friendcon_id) |
167 | { | 155 | { |
168 | if (friendconn_id_not_valid(fr_c, friendcon_id)) { | 156 | if (!friendconn_id_valid(fr_c, friendcon_id)) { |
169 | return -1; | 157 | return -1; |
170 | } | 158 | } |
171 | 159 | ||
172 | uint32_t i; | ||
173 | memset(&fr_c->conns[friendcon_id], 0, sizeof(Friend_Conn)); | 160 | memset(&fr_c->conns[friendcon_id], 0, sizeof(Friend_Conn)); |
174 | 161 | ||
162 | uint32_t i; | ||
163 | |||
175 | for (i = fr_c->num_cons; i != 0; --i) { | 164 | for (i = fr_c->num_cons; i != 0; --i) { |
176 | if (fr_c->conns[i - 1].status != FRIENDCONN_STATUS_NONE) { | 165 | if (fr_c->conns[i - 1].status != FRIENDCONN_STATUS_NONE) { |
177 | break; | 166 | break; |
@@ -188,7 +177,7 @@ static int wipe_friend_conn(Friend_Connections *fr_c, int friendcon_id) | |||
188 | 177 | ||
189 | static Friend_Conn *get_conn(const Friend_Connections *fr_c, int friendcon_id) | 178 | static Friend_Conn *get_conn(const Friend_Connections *fr_c, int friendcon_id) |
190 | { | 179 | { |
191 | if (friendconn_id_not_valid(fr_c, friendcon_id)) { | 180 | if (!friendconn_id_valid(fr_c, friendcon_id)) { |
192 | return nullptr; | 181 | return nullptr; |
193 | } | 182 | } |
194 | 183 | ||
@@ -200,9 +189,7 @@ static Friend_Conn *get_conn(const Friend_Connections *fr_c, int friendcon_id) | |||
200 | */ | 189 | */ |
201 | int getfriend_conn_id_pk(Friend_Connections *fr_c, const uint8_t *real_pk) | 190 | int getfriend_conn_id_pk(Friend_Connections *fr_c, const uint8_t *real_pk) |
202 | { | 191 | { |
203 | uint32_t i; | 192 | for (uint32_t i = 0; i < fr_c->num_cons; ++i) { |
204 | |||
205 | for (i = 0; i < fr_c->num_cons; ++i) { | ||
206 | Friend_Conn *friend_con = get_conn(fr_c, i); | 193 | Friend_Conn *friend_con = get_conn(fr_c, i); |
207 | 194 | ||
208 | if (friend_con) { | 195 | if (friend_con) { |
@@ -222,7 +209,7 @@ int getfriend_conn_id_pk(Friend_Connections *fr_c, const uint8_t *real_pk) | |||
222 | */ | 209 | */ |
223 | int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, IP_Port ip_port, const uint8_t *public_key) | 210 | int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, IP_Port ip_port, const uint8_t *public_key) |
224 | { | 211 | { |
225 | Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); | 212 | Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id); |
226 | 213 | ||
227 | if (!friend_con) { | 214 | if (!friend_con) { |
228 | return -1; | 215 | return -1; |
@@ -237,11 +224,9 @@ int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, IP_Port ip_ | |||
237 | } | 224 | } |
238 | } | 225 | } |
239 | 226 | ||
240 | unsigned int i; | 227 | const uint16_t index = friend_con->tcp_relay_counter % FRIEND_MAX_STORED_TCP_RELAYS; |
241 | 228 | ||
242 | uint16_t index = friend_con->tcp_relay_counter % FRIEND_MAX_STORED_TCP_RELAYS; | 229 | for (unsigned i = 0; i < FRIEND_MAX_STORED_TCP_RELAYS; ++i) { |
243 | |||
244 | for (i = 0; i < FRIEND_MAX_STORED_TCP_RELAYS; ++i) { | ||
245 | if (friend_con->tcp_relays[i].ip_port.ip.family != 0 | 230 | if (friend_con->tcp_relays[i].ip_port.ip.family != 0 |
246 | && public_key_cmp(friend_con->tcp_relays[i].public_key, public_key) == 0) { | 231 | && public_key_cmp(friend_con->tcp_relays[i].public_key, public_key) == 0) { |
247 | memset(&friend_con->tcp_relays[i], 0, sizeof(Node_format)); | 232 | memset(&friend_con->tcp_relays[i], 0, sizeof(Node_format)); |
@@ -258,16 +243,14 @@ int friend_add_tcp_relay(Friend_Connections *fr_c, int friendcon_id, IP_Port ip_ | |||
258 | /* Connect to number saved relays for friend. */ | 243 | /* Connect to number saved relays for friend. */ |
259 | static void connect_to_saved_tcp_relays(Friend_Connections *fr_c, int friendcon_id, unsigned int number) | 244 | static void connect_to_saved_tcp_relays(Friend_Connections *fr_c, int friendcon_id, unsigned int number) |
260 | { | 245 | { |
261 | Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); | 246 | const Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id); |
262 | 247 | ||
263 | if (!friend_con) { | 248 | if (!friend_con) { |
264 | return; | 249 | return; |
265 | } | 250 | } |
266 | 251 | ||
267 | unsigned int i; | 252 | for (unsigned i = 0; (i < FRIEND_MAX_STORED_TCP_RELAYS) && (number != 0); ++i) { |
268 | 253 | const uint16_t index = (friend_con->tcp_relay_counter - (i + 1)) % FRIEND_MAX_STORED_TCP_RELAYS; | |
269 | for (i = 0; (i < FRIEND_MAX_STORED_TCP_RELAYS) && (number != 0); ++i) { | ||
270 | uint16_t index = (friend_con->tcp_relay_counter - (i + 1)) % FRIEND_MAX_STORED_TCP_RELAYS; | ||
271 | 254 | ||
272 | if (friend_con->tcp_relays[index].ip_port.ip.family) { | 255 | if (friend_con->tcp_relays[index].ip_port.ip.family) { |
273 | if (add_tcp_relay_peer(fr_c->net_crypto, friend_con->crypt_connection_id, friend_con->tcp_relays[index].ip_port, | 256 | if (add_tcp_relay_peer(fr_c->net_crypto, friend_con->crypt_connection_id, friend_con->tcp_relays[index].ip_port, |
@@ -280,7 +263,7 @@ static void connect_to_saved_tcp_relays(Friend_Connections *fr_c, int friendcon_ | |||
280 | 263 | ||
281 | static unsigned int send_relays(Friend_Connections *fr_c, int friendcon_id) | 264 | static unsigned int send_relays(Friend_Connections *fr_c, int friendcon_id) |
282 | { | 265 | { |
283 | Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); | 266 | Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id); |
284 | 267 | ||
285 | if (!friend_con) { | 268 | if (!friend_con) { |
286 | return 0; | 269 | return 0; |
@@ -288,19 +271,16 @@ static unsigned int send_relays(Friend_Connections *fr_c, int friendcon_id) | |||
288 | 271 | ||
289 | Node_format nodes[MAX_SHARED_RELAYS]; | 272 | Node_format nodes[MAX_SHARED_RELAYS]; |
290 | uint8_t data[1024]; | 273 | uint8_t data[1024]; |
291 | int n, length; | ||
292 | 274 | ||
293 | n = copy_connected_tcp_relays(fr_c->net_crypto, nodes, MAX_SHARED_RELAYS); | 275 | const int n = copy_connected_tcp_relays(fr_c->net_crypto, nodes, MAX_SHARED_RELAYS); |
294 | 276 | ||
295 | int i; | 277 | for (int i = 0; i < n; ++i) { |
296 | |||
297 | for (i = 0; i < n; ++i) { | ||
298 | /* Associated the relays being sent with this connection. | 278 | /* Associated the relays being sent with this connection. |
299 | On receiving the peer will do the same which will establish the connection. */ | 279 | On receiving the peer will do the same which will establish the connection. */ |
300 | friend_add_tcp_relay(fr_c, friendcon_id, nodes[i].ip_port, nodes[i].public_key); | 280 | friend_add_tcp_relay(fr_c, friendcon_id, nodes[i].ip_port, nodes[i].public_key); |
301 | } | 281 | } |
302 | 282 | ||
303 | length = pack_nodes(data + 1, sizeof(data) - 1, nodes, n); | 283 | int length = pack_nodes(data + 1, sizeof(data) - 1, nodes, n); |
304 | 284 | ||
305 | if (length <= 0) { | 285 | if (length <= 0) { |
306 | return 0; | 286 | return 0; |
@@ -321,7 +301,7 @@ static unsigned int send_relays(Friend_Connections *fr_c, int friendcon_id) | |||
321 | static int tcp_relay_node_callback(void *object, uint32_t number, IP_Port ip_port, const uint8_t *public_key) | 301 | static int tcp_relay_node_callback(void *object, uint32_t number, IP_Port ip_port, const uint8_t *public_key) |
322 | { | 302 | { |
323 | Friend_Connections *fr_c = (Friend_Connections *)object; | 303 | Friend_Connections *fr_c = (Friend_Connections *)object; |
324 | Friend_Conn *friend_con = get_conn(fr_c, number); | 304 | const Friend_Conn *friend_con = get_conn(fr_c, number); |
325 | 305 | ||
326 | if (!friend_con) { | 306 | if (!friend_con) { |
327 | return -1; | 307 | return -1; |
@@ -338,8 +318,8 @@ static int friend_new_connection(Friend_Connections *fr_c, int friendcon_id); | |||
338 | /* Callback for DHT ip_port changes. */ | 318 | /* Callback for DHT ip_port changes. */ |
339 | static void dht_ip_callback(void *object, int32_t number, IP_Port ip_port) | 319 | static void dht_ip_callback(void *object, int32_t number, IP_Port ip_port) |
340 | { | 320 | { |
341 | Friend_Connections *fr_c = (Friend_Connections *)object; | 321 | Friend_Connections *const fr_c = (Friend_Connections *)object; |
342 | Friend_Conn *friend_con = get_conn(fr_c, number); | 322 | Friend_Conn *const friend_con = get_conn(fr_c, number); |
343 | 323 | ||
344 | if (!friend_con) { | 324 | if (!friend_con) { |
345 | return; | 325 | return; |
@@ -361,7 +341,7 @@ static void dht_ip_callback(void *object, int32_t number, IP_Port ip_port) | |||
361 | 341 | ||
362 | static void change_dht_pk(Friend_Connections *fr_c, int friendcon_id, const uint8_t *dht_public_key) | 342 | static void change_dht_pk(Friend_Connections *fr_c, int friendcon_id, const uint8_t *dht_public_key) |
363 | { | 343 | { |
364 | Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); | 344 | Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id); |
365 | 345 | ||
366 | if (!friend_con) { | 346 | if (!friend_con) { |
367 | return; | 347 | return; |
@@ -384,8 +364,8 @@ static void change_dht_pk(Friend_Connections *fr_c, int friendcon_id, const uint | |||
384 | 364 | ||
385 | static int handle_status(void *object, int number, uint8_t status, void *userdata) | 365 | static int handle_status(void *object, int number, uint8_t status, void *userdata) |
386 | { | 366 | { |
387 | Friend_Connections *fr_c = (Friend_Connections *)object; | 367 | Friend_Connections *const fr_c = (Friend_Connections *)object; |
388 | Friend_Conn *friend_con = get_conn(fr_c, number); | 368 | Friend_Conn *const friend_con = get_conn(fr_c, number); |
389 | 369 | ||
390 | if (!friend_con) { | 370 | if (!friend_con) { |
391 | return -1; | 371 | return -1; |
@@ -429,8 +409,8 @@ static int handle_status(void *object, int number, uint8_t status, void *userdat | |||
429 | /* Callback for dht public key changes. */ | 409 | /* Callback for dht public key changes. */ |
430 | static void dht_pk_callback(void *object, int32_t number, const uint8_t *dht_public_key, void *userdata) | 410 | static void dht_pk_callback(void *object, int32_t number, const uint8_t *dht_public_key, void *userdata) |
431 | { | 411 | { |
432 | Friend_Connections *fr_c = (Friend_Connections *)object; | 412 | Friend_Connections *const fr_c = (Friend_Connections *)object; |
433 | Friend_Conn *friend_con = get_conn(fr_c, number); | 413 | Friend_Conn *const friend_con = get_conn(fr_c, number); |
434 | 414 | ||
435 | if (!friend_con) { | 415 | if (!friend_con) { |
436 | return; | 416 | return; |
@@ -459,7 +439,7 @@ static int handle_packet(void *object, int number, const uint8_t *data, uint16_t | |||
459 | return -1; | 439 | return -1; |
460 | } | 440 | } |
461 | 441 | ||
462 | Friend_Connections *fr_c = (Friend_Connections *)object; | 442 | Friend_Connections *const fr_c = (Friend_Connections *)object; |
463 | Friend_Conn *friend_con = get_conn(fr_c, number); | 443 | Friend_Conn *friend_con = get_conn(fr_c, number); |
464 | 444 | ||
465 | if (!friend_con) { | 445 | if (!friend_con) { |
@@ -481,24 +461,20 @@ static int handle_packet(void *object, int number, const uint8_t *data, uint16_t | |||
481 | 461 | ||
482 | if (data[0] == PACKET_ID_SHARE_RELAYS) { | 462 | if (data[0] == PACKET_ID_SHARE_RELAYS) { |
483 | Node_format nodes[MAX_SHARED_RELAYS]; | 463 | Node_format nodes[MAX_SHARED_RELAYS]; |
484 | int n; | 464 | const int n = unpack_nodes(nodes, MAX_SHARED_RELAYS, nullptr, data + 1, length - 1, 1); |
485 | 465 | ||
486 | if ((n = unpack_nodes(nodes, MAX_SHARED_RELAYS, nullptr, data + 1, length - 1, 1)) == -1) { | 466 | if (n == -1) { |
487 | return -1; | 467 | return -1; |
488 | } | 468 | } |
489 | 469 | ||
490 | int j; | 470 | for (int j = 0; j < n; j++) { |
491 | |||
492 | for (j = 0; j < n; j++) { | ||
493 | friend_add_tcp_relay(fr_c, number, nodes[j].ip_port, nodes[j].public_key); | 471 | friend_add_tcp_relay(fr_c, number, nodes[j].ip_port, nodes[j].public_key); |
494 | } | 472 | } |
495 | 473 | ||
496 | return 0; | 474 | return 0; |
497 | } | 475 | } |
498 | 476 | ||
499 | unsigned int i; | 477 | for (unsigned i = 0; i < MAX_FRIEND_CONNECTION_CALLBACKS; ++i) { |
500 | |||
501 | for (i = 0; i < MAX_FRIEND_CONNECTION_CALLBACKS; ++i) { | ||
502 | if (friend_con->callbacks[i].data_callback) { | 478 | if (friend_con->callbacks[i].data_callback) { |
503 | friend_con->callbacks[i].data_callback( | 479 | friend_con->callbacks[i].data_callback( |
504 | friend_con->callbacks[i].callback_object, | 480 | friend_con->callbacks[i].callback_object, |
@@ -521,16 +497,14 @@ static int handle_lossy_packet(void *object, int number, const uint8_t *data, ui | |||
521 | return -1; | 497 | return -1; |
522 | } | 498 | } |
523 | 499 | ||
524 | Friend_Connections *fr_c = (Friend_Connections *)object; | 500 | Friend_Connections *const fr_c = (Friend_Connections *)object; |
525 | Friend_Conn *friend_con = get_conn(fr_c, number); | 501 | const Friend_Conn *friend_con = get_conn(fr_c, number); |
526 | 502 | ||
527 | if (!friend_con) { | 503 | if (!friend_con) { |
528 | return -1; | 504 | return -1; |
529 | } | 505 | } |
530 | 506 | ||
531 | unsigned int i; | 507 | for (unsigned i = 0; i < MAX_FRIEND_CONNECTION_CALLBACKS; ++i) { |
532 | |||
533 | for (i = 0; i < MAX_FRIEND_CONNECTION_CALLBACKS; ++i) { | ||
534 | if (friend_con->callbacks[i].lossy_data_callback) { | 508 | if (friend_con->callbacks[i].lossy_data_callback) { |
535 | friend_con->callbacks[i].lossy_data_callback( | 509 | friend_con->callbacks[i].lossy_data_callback( |
536 | friend_con->callbacks[i].callback_object, | 510 | friend_con->callbacks[i].callback_object, |
@@ -549,48 +523,47 @@ static int handle_lossy_packet(void *object, int number, const uint8_t *data, ui | |||
549 | 523 | ||
550 | static int handle_new_connections(void *object, New_Connection *n_c) | 524 | static int handle_new_connections(void *object, New_Connection *n_c) |
551 | { | 525 | { |
552 | Friend_Connections *fr_c = (Friend_Connections *)object; | 526 | Friend_Connections *const fr_c = (Friend_Connections *)object; |
553 | int friendcon_id = getfriend_conn_id_pk(fr_c, n_c->public_key); | 527 | const int friendcon_id = getfriend_conn_id_pk(fr_c, n_c->public_key); |
554 | Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); | 528 | Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id); |
555 | 529 | ||
556 | if (friend_con) { | 530 | if (!friend_con) { |
557 | 531 | return -1; | |
558 | if (friend_con->crypt_connection_id != -1) { | 532 | } |
559 | return -1; | ||
560 | } | ||
561 | 533 | ||
562 | int id = accept_crypto_connection(fr_c->net_crypto, n_c); | 534 | if (friend_con->crypt_connection_id != -1) { |
535 | return -1; | ||
536 | } | ||
563 | 537 | ||
564 | if (id == -1) { | 538 | const int id = accept_crypto_connection(fr_c->net_crypto, n_c); |
565 | return -1; | ||
566 | } | ||
567 | 539 | ||
568 | connection_status_handler(fr_c->net_crypto, id, &handle_status, fr_c, friendcon_id); | 540 | if (id == -1) { |
569 | connection_data_handler(fr_c->net_crypto, id, &handle_packet, fr_c, friendcon_id); | 541 | return -1; |
570 | connection_lossy_data_handler(fr_c->net_crypto, id, &handle_lossy_packet, fr_c, friendcon_id); | 542 | } |
571 | friend_con->crypt_connection_id = id; | ||
572 | 543 | ||
573 | if (n_c->source.ip.family != TOX_AF_INET && n_c->source.ip.family != TOX_AF_INET6) { | 544 | connection_status_handler(fr_c->net_crypto, id, &handle_status, fr_c, friendcon_id); |
574 | set_direct_ip_port(fr_c->net_crypto, friend_con->crypt_connection_id, friend_con->dht_ip_port, 0); | 545 | connection_data_handler(fr_c->net_crypto, id, &handle_packet, fr_c, friendcon_id); |
575 | } else { | 546 | connection_lossy_data_handler(fr_c->net_crypto, id, &handle_lossy_packet, fr_c, friendcon_id); |
576 | friend_con->dht_ip_port = n_c->source; | 547 | friend_con->crypt_connection_id = id; |
577 | friend_con->dht_ip_port_lastrecv = unix_time(); | ||
578 | } | ||
579 | 548 | ||
580 | if (public_key_cmp(friend_con->dht_temp_pk, n_c->dht_public_key) != 0) { | 549 | if (n_c->source.ip.family != TOX_AF_INET && n_c->source.ip.family != TOX_AF_INET6) { |
581 | change_dht_pk(fr_c, friendcon_id, n_c->dht_public_key); | 550 | set_direct_ip_port(fr_c->net_crypto, friend_con->crypt_connection_id, friend_con->dht_ip_port, 0); |
582 | } | 551 | } else { |
552 | friend_con->dht_ip_port = n_c->source; | ||
553 | friend_con->dht_ip_port_lastrecv = unix_time(); | ||
554 | } | ||
583 | 555 | ||
584 | nc_dht_pk_callback(fr_c->net_crypto, id, &dht_pk_callback, fr_c, friendcon_id); | 556 | if (public_key_cmp(friend_con->dht_temp_pk, n_c->dht_public_key) != 0) { |
585 | return 0; | 557 | change_dht_pk(fr_c, friendcon_id, n_c->dht_public_key); |
586 | } | 558 | } |
587 | 559 | ||
588 | return -1; | 560 | nc_dht_pk_callback(fr_c->net_crypto, id, &dht_pk_callback, fr_c, friendcon_id); |
561 | return 0; | ||
589 | } | 562 | } |
590 | 563 | ||
591 | static int friend_new_connection(Friend_Connections *fr_c, int friendcon_id) | 564 | static int friend_new_connection(Friend_Connections *fr_c, int friendcon_id) |
592 | { | 565 | { |
593 | Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); | 566 | Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id); |
594 | 567 | ||
595 | if (!friend_con) { | 568 | if (!friend_con) { |
596 | return -1; | 569 | return -1; |
@@ -605,7 +578,7 @@ static int friend_new_connection(Friend_Connections *fr_c, int friendcon_id) | |||
605 | return -1; | 578 | return -1; |
606 | } | 579 | } |
607 | 580 | ||
608 | int id = new_crypto_connection(fr_c->net_crypto, friend_con->real_public_key, friend_con->dht_temp_pk); | 581 | const int id = new_crypto_connection(fr_c->net_crypto, friend_con->real_public_key, friend_con->dht_temp_pk); |
609 | 582 | ||
610 | if (id == -1) { | 583 | if (id == -1) { |
611 | return -1; | 584 | return -1; |
@@ -622,14 +595,14 @@ static int friend_new_connection(Friend_Connections *fr_c, int friendcon_id) | |||
622 | 595 | ||
623 | static int send_ping(const Friend_Connections *fr_c, int friendcon_id) | 596 | static int send_ping(const Friend_Connections *fr_c, int friendcon_id) |
624 | { | 597 | { |
625 | Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); | 598 | Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id); |
626 | 599 | ||
627 | if (!friend_con) { | 600 | if (!friend_con) { |
628 | return -1; | 601 | return -1; |
629 | } | 602 | } |
630 | 603 | ||
631 | uint8_t ping = PACKET_ID_ALIVE; | 604 | const uint8_t ping = PACKET_ID_ALIVE; |
632 | int64_t ret = write_cryptpacket(fr_c->net_crypto, friend_con->crypt_connection_id, &ping, sizeof(ping), 0); | 605 | const int64_t ret = write_cryptpacket(fr_c->net_crypto, friend_con->crypt_connection_id, &ping, sizeof(ping), 0); |
633 | 606 | ||
634 | if (ret != -1) { | 607 | if (ret != -1) { |
635 | friend_con->ping_lastsent = unix_time(); | 608 | friend_con->ping_lastsent = unix_time(); |
@@ -646,7 +619,7 @@ static int send_ping(const Friend_Connections *fr_c, int friendcon_id) | |||
646 | */ | 619 | */ |
647 | int friend_connection_lock(Friend_Connections *fr_c, int friendcon_id) | 620 | int friend_connection_lock(Friend_Connections *fr_c, int friendcon_id) |
648 | { | 621 | { |
649 | Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); | 622 | Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id); |
650 | 623 | ||
651 | if (!friend_con) { | 624 | if (!friend_con) { |
652 | return -1; | 625 | return -1; |
@@ -662,7 +635,7 @@ int friend_connection_lock(Friend_Connections *fr_c, int friendcon_id) | |||
662 | */ | 635 | */ |
663 | unsigned int friend_con_connected(Friend_Connections *fr_c, int friendcon_id) | 636 | unsigned int friend_con_connected(Friend_Connections *fr_c, int friendcon_id) |
664 | { | 637 | { |
665 | Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); | 638 | const Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id); |
666 | 639 | ||
667 | if (!friend_con) { | 640 | if (!friend_con) { |
668 | return 0; | 641 | return 0; |
@@ -678,7 +651,7 @@ unsigned int friend_con_connected(Friend_Connections *fr_c, int friendcon_id) | |||
678 | */ | 651 | */ |
679 | int get_friendcon_public_keys(uint8_t *real_pk, uint8_t *dht_temp_pk, Friend_Connections *fr_c, int friendcon_id) | 652 | int get_friendcon_public_keys(uint8_t *real_pk, uint8_t *dht_temp_pk, Friend_Connections *fr_c, int friendcon_id) |
680 | { | 653 | { |
681 | Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); | 654 | const Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id); |
682 | 655 | ||
683 | if (!friend_con) { | 656 | if (!friend_con) { |
684 | return -1; | 657 | return -1; |
@@ -714,7 +687,7 @@ int friend_connection_callbacks(Friend_Connections *fr_c, int friendcon_id, unsi | |||
714 | int (*lossy_data_callback)(void *object, int id, const uint8_t *data, uint16_t length, void *userdata), | 687 | int (*lossy_data_callback)(void *object, int id, const uint8_t *data, uint16_t length, void *userdata), |
715 | void *object, int number) | 688 | void *object, int number) |
716 | { | 689 | { |
717 | Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); | 690 | Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id); |
718 | 691 | ||
719 | if (!friend_con) { | 692 | if (!friend_con) { |
720 | return -1; | 693 | return -1; |
@@ -741,7 +714,7 @@ int friend_connection_callbacks(Friend_Connections *fr_c, int friendcon_id, unsi | |||
741 | */ | 714 | */ |
742 | int friend_connection_crypt_connection_id(Friend_Connections *fr_c, int friendcon_id) | 715 | int friend_connection_crypt_connection_id(Friend_Connections *fr_c, int friendcon_id) |
743 | { | 716 | { |
744 | Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); | 717 | const Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id); |
745 | 718 | ||
746 | if (!friend_con) { | 719 | if (!friend_con) { |
747 | return -1; | 720 | return -1; |
@@ -771,13 +744,13 @@ int new_friend_connection(Friend_Connections *fr_c, const uint8_t *real_public_k | |||
771 | return -1; | 744 | return -1; |
772 | } | 745 | } |
773 | 746 | ||
774 | int32_t onion_friendnum = onion_addfriend(fr_c->onion_c, real_public_key); | 747 | const int32_t onion_friendnum = onion_addfriend(fr_c->onion_c, real_public_key); |
775 | 748 | ||
776 | if (onion_friendnum == -1) { | 749 | if (onion_friendnum == -1) { |
777 | return -1; | 750 | return -1; |
778 | } | 751 | } |
779 | 752 | ||
780 | Friend_Conn *friend_con = &fr_c->conns[friendcon_id]; | 753 | Friend_Conn *const friend_con = &fr_c->conns[friendcon_id]; |
781 | 754 | ||
782 | friend_con->crypt_connection_id = -1; | 755 | friend_con->crypt_connection_id = -1; |
783 | friend_con->status = FRIENDCONN_STATUS_CONNECTING; | 756 | friend_con->status = FRIENDCONN_STATUS_CONNECTING; |
@@ -797,7 +770,7 @@ int new_friend_connection(Friend_Connections *fr_c, const uint8_t *real_public_k | |||
797 | */ | 770 | */ |
798 | int kill_friend_connection(Friend_Connections *fr_c, int friendcon_id) | 771 | int kill_friend_connection(Friend_Connections *fr_c, int friendcon_id) |
799 | { | 772 | { |
800 | Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); | 773 | Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id); |
801 | 774 | ||
802 | if (!friend_con) { | 775 | if (!friend_con) { |
803 | return -1; | 776 | return -1; |
@@ -844,7 +817,7 @@ int send_friend_request_packet(Friend_Connections *fr_c, int friendcon_id, uint3 | |||
844 | return -1; | 817 | return -1; |
845 | } | 818 | } |
846 | 819 | ||
847 | Friend_Conn *friend_con = get_conn(fr_c, friendcon_id); | 820 | const Friend_Conn *const friend_con = get_conn(fr_c, friendcon_id); |
848 | 821 | ||
849 | if (!friend_con) { | 822 | if (!friend_con) { |
850 | return -1; | 823 | return -1; |
@@ -860,7 +833,7 @@ int send_friend_request_packet(Friend_Connections *fr_c, int friendcon_id, uint3 | |||
860 | } | 833 | } |
861 | 834 | ||
862 | packet[0] = CRYPTO_PACKET_FRIEND_REQ; | 835 | packet[0] = CRYPTO_PACKET_FRIEND_REQ; |
863 | int num = send_onion_data(fr_c->onion_c, friend_con->onion_friendnum, packet, SIZEOF_VLA(packet)); | 836 | const int num = send_onion_data(fr_c->onion_c, friend_con->onion_friendnum, packet, SIZEOF_VLA(packet)); |
864 | 837 | ||
865 | if (num <= 0) { | 838 | if (num <= 0) { |
866 | return -1; | 839 | return -1; |
@@ -872,11 +845,11 @@ int send_friend_request_packet(Friend_Connections *fr_c, int friendcon_id, uint3 | |||
872 | /* Create new friend_connections instance. */ | 845 | /* Create new friend_connections instance. */ |
873 | Friend_Connections *new_friend_connections(Onion_Client *onion_c, bool local_discovery_enabled) | 846 | Friend_Connections *new_friend_connections(Onion_Client *onion_c, bool local_discovery_enabled) |
874 | { | 847 | { |
875 | if (!onion_c) { | 848 | if (onion_c == nullptr) { |
876 | return nullptr; | 849 | return nullptr; |
877 | } | 850 | } |
878 | 851 | ||
879 | Friend_Connections *temp = (Friend_Connections *)calloc(1, sizeof(Friend_Connections)); | 852 | Friend_Connections *const temp = (Friend_Connections *)calloc(1, sizeof(Friend_Connections)); |
880 | 853 | ||
881 | if (temp == nullptr) { | 854 | if (temp == nullptr) { |
882 | return nullptr; | 855 | return nullptr; |
@@ -923,11 +896,10 @@ static void LANdiscovery(Friend_Connections *fr_c) | |||
923 | /* main friend_connections loop. */ | 896 | /* main friend_connections loop. */ |
924 | void do_friend_connections(Friend_Connections *fr_c, void *userdata) | 897 | void do_friend_connections(Friend_Connections *fr_c, void *userdata) |
925 | { | 898 | { |
926 | uint32_t i; | 899 | const uint64_t temp_time = unix_time(); |
927 | uint64_t temp_time = unix_time(); | ||
928 | 900 | ||
929 | for (i = 0; i < fr_c->num_cons; ++i) { | 901 | for (uint32_t i = 0; i < fr_c->num_cons; ++i) { |
930 | Friend_Conn *friend_con = get_conn(fr_c, i); | 902 | Friend_Conn *const friend_con = get_conn(fr_c, i); |
931 | 903 | ||
932 | if (friend_con) { | 904 | if (friend_con) { |
933 | if (friend_con->status == FRIENDCONN_STATUS_CONNECTING) { | 905 | if (friend_con->status == FRIENDCONN_STATUS_CONNECTING) { |
@@ -980,9 +952,7 @@ void kill_friend_connections(Friend_Connections *fr_c) | |||
980 | return; | 952 | return; |
981 | } | 953 | } |
982 | 954 | ||
983 | uint32_t i; | 955 | for (uint32_t i = 0; i < fr_c->num_cons; ++i) { |
984 | |||
985 | for (i = 0; i < fr_c->num_cons; ++i) { | ||
986 | kill_friend_connection(fr_c, i); | 956 | kill_friend_connection(fr_c, i); |
987 | } | 957 | } |
988 | 958 | ||
diff --git a/toxcore/friend_requests.c b/toxcore/friend_requests.c index 4a06654d..13c89eae 100644 --- a/toxcore/friend_requests.c +++ b/toxcore/friend_requests.c | |||
@@ -67,6 +67,7 @@ void callback_friendrequest(Friend_Requests *fr, void (*function)(void *, const | |||
67 | fr->handle_friendrequest_isset = 1; | 67 | fr->handle_friendrequest_isset = 1; |
68 | fr->handle_friendrequest_object = object; | 68 | fr->handle_friendrequest_object = object; |
69 | } | 69 | } |
70 | |||
70 | /* Set the function used to check if a friend request should be displayed to the user or not. */ | 71 | /* Set the function used to check if a friend request should be displayed to the user or not. */ |
71 | void set_filter_function(Friend_Requests *fr, int (*function)(const uint8_t *, void *), void *userdata) | 72 | void set_filter_function(Friend_Requests *fr, int (*function)(const uint8_t *, void *), void *userdata) |
72 | { | 73 | { |
@@ -87,20 +88,18 @@ static void addto_receivedlist(Friend_Requests *fr, const uint8_t *real_pk) | |||
87 | 88 | ||
88 | /* Check if a friend request was already received. | 89 | /* Check if a friend request was already received. |
89 | * | 90 | * |
90 | * return 0 if it did not. | 91 | * return false if it did not. |
91 | * return 1 if it did. | 92 | * return true if it did. |
92 | */ | 93 | */ |
93 | static int request_received(Friend_Requests *fr, const uint8_t *real_pk) | 94 | static bool request_received(const Friend_Requests *fr, const uint8_t *real_pk) |
94 | { | 95 | { |
95 | uint32_t i; | 96 | for (uint32_t i = 0; i < MAX_RECEIVED_STORED; ++i) { |
96 | |||
97 | for (i = 0; i < MAX_RECEIVED_STORED; ++i) { | ||
98 | if (id_equal(fr->received_requests[i], real_pk)) { | 97 | if (id_equal(fr->received_requests[i], real_pk)) { |
99 | return 1; | 98 | return true; |
100 | } | 99 | } |
101 | } | 100 | } |
102 | 101 | ||
103 | return 0; | 102 | return false; |
104 | } | 103 | } |
105 | 104 | ||
106 | /* Remove real pk from received_requests list. | 105 | /* Remove real pk from received_requests list. |
@@ -110,9 +109,7 @@ static int request_received(Friend_Requests *fr, const uint8_t *real_pk) | |||
110 | */ | 109 | */ |
111 | int remove_request_received(Friend_Requests *fr, const uint8_t *real_pk) | 110 | int remove_request_received(Friend_Requests *fr, const uint8_t *real_pk) |
112 | { | 111 | { |
113 | uint32_t i; | 112 | for (uint32_t i = 0; i < MAX_RECEIVED_STORED; ++i) { |
114 | |||
115 | for (i = 0; i < MAX_RECEIVED_STORED; ++i) { | ||
116 | if (id_equal(fr->received_requests[i], real_pk)) { | 113 | if (id_equal(fr->received_requests[i], real_pk)) { |
117 | crypto_memzero(fr->received_requests[i], CRYPTO_PUBLIC_KEY_SIZE); | 114 | crypto_memzero(fr->received_requests[i], CRYPTO_PUBLIC_KEY_SIZE); |
118 | return 0; | 115 | return 0; |
@@ -126,7 +123,7 @@ int remove_request_received(Friend_Requests *fr, const uint8_t *real_pk) | |||
126 | static int friendreq_handlepacket(void *object, const uint8_t *source_pubkey, const uint8_t *packet, uint16_t length, | 123 | static int friendreq_handlepacket(void *object, const uint8_t *source_pubkey, const uint8_t *packet, uint16_t length, |
127 | void *userdata) | 124 | void *userdata) |
128 | { | 125 | { |
129 | Friend_Requests *fr = (Friend_Requests *)object; | 126 | Friend_Requests *const fr = (Friend_Requests *)object; |
130 | 127 | ||
131 | if (length <= 1 + sizeof(fr->nospam) || length > ONION_CLIENT_MAX_DATA_SIZE) { | 128 | if (length <= 1 + sizeof(fr->nospam) || length > ONION_CLIENT_MAX_DATA_SIZE) { |
132 | return 1; | 129 | return 1; |
@@ -148,19 +145,19 @@ static int friendreq_handlepacket(void *object, const uint8_t *source_pubkey, co | |||
148 | } | 145 | } |
149 | 146 | ||
150 | if (fr->filter_function) { | 147 | if (fr->filter_function) { |
151 | if ((*fr->filter_function)(source_pubkey, fr->filter_function_userdata) != 0) { | 148 | if (fr->filter_function(source_pubkey, fr->filter_function_userdata) != 0) { |
152 | return 1; | 149 | return 1; |
153 | } | 150 | } |
154 | } | 151 | } |
155 | 152 | ||
156 | addto_receivedlist(fr, source_pubkey); | 153 | addto_receivedlist(fr, source_pubkey); |
157 | 154 | ||
158 | uint32_t message_len = length - sizeof(fr->nospam); | 155 | const uint32_t message_len = length - sizeof(fr->nospam); |
159 | VLA(uint8_t, message, message_len + 1); | 156 | VLA(uint8_t, message, message_len + 1); |
160 | memcpy(message, packet + sizeof(fr->nospam), message_len); | 157 | memcpy(message, packet + sizeof(fr->nospam), message_len); |
161 | message[SIZEOF_VLA(message) - 1] = 0; /* Be sure the message is null terminated. */ | 158 | message[SIZEOF_VLA(message) - 1] = 0; /* Be sure the message is null terminated. */ |
162 | 159 | ||
163 | (*fr->handle_friendrequest)(fr->handle_friendrequest_object, source_pubkey, message, message_len, userdata); | 160 | fr->handle_friendrequest(fr->handle_friendrequest_object, source_pubkey, message, message_len, userdata); |
164 | return 0; | 161 | return 0; |
165 | } | 162 | } |
166 | 163 | ||