summaryrefslogtreecommitdiff
path: root/toxcore/DHT.c
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2018-02-16 17:16:49 +0000
committeriphydf <iphydf@users.noreply.github.com>2018-02-18 13:01:16 +0000
commit3f73dfa7f1a7fa1afef2492d1834e4c619f4afb3 (patch)
treea8fa188dc9929dbfbb5556b1e58d17d46596ec05 /toxcore/DHT.c
parent841b63434fcb949c48d42a2a8b2b4f69467f3f78 (diff)
Use `const` more in C code.
Diffstat (limited to 'toxcore/DHT.c')
-rw-r--r--toxcore/DHT.c455
1 files changed, 231 insertions, 224 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)
154int id_closest(const uint8_t *pk, const uint8_t *pk1, const uint8_t *pk2) 154int 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
669static int client_or_ip_port_in_list(Logger *log, Client_data *list, uint16_t length, const uint8_t *public_key, 669static 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 */
916static unsigned int store_node_ok(const Client_data *client, const uint8_t *public_key, const uint8_t *comp_public_key) 916static 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
923static void sort_client_list(Client_data *list, unsigned int length, const uint8_t *comp_public_key) 923static 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 */
978static int replace_all(Client_data *list, 978static 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
1046static bool is_pk_in_client_list(Client_data *list, unsigned int client_list_length, const uint8_t *public_key, 1046static 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 */
1079static unsigned int ping_node_from_getnodes_ok(DHT *dht, const uint8_t *public_key, IP_Port ip_port) 1079static 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
1195static bool update_client_data(Client_data *array, size_t size, IP_Port ip_port, const uint8_t *pk) 1195static 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
1344static int handle_getnodes(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata) 1344static 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
1380static 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) 1381static 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,
1407static int handle_sendnodes_core(void *object, IP_Port source, const uint8_t *packet, uint16_t length, 1408static 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
1478static int handle_sendnodes_ipv6(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata) 1479static 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
1505int DHT_addfriend(DHT *dht, const uint8_t *public_key, void (*ip_callback)(void *data, int32_t number, IP_Port), 1506int 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
1561int DHT_delfriend(DHT *dht, const uint8_t *public_key, uint16_t lock_count) 1562int 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
1709static void do_DHT_friends(DHT *dht) 1710static 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 */
1911int route_tofriend(const DHT *dht, const uint8_t *friend_id, const uint8_t *packet, uint16_t length) 1914int 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 */
1966static int routeone_tofriend(DHT *dht, const uint8_t *friend_id, const uint8_t *packet, uint16_t length) 1967static 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
2193static void do_NAT(DHT *dht) 2193static 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)
2354static int handle_hardening(void *object, IP_Port source, const uint8_t *source_pubkey, const uint8_t *packet, 2356static 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)
2529static void do_hardening(DHT *dht) 2532static 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
2589static int cryptopacket_handle(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata) 2592static 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
2704void kill_DHT(DHT *dht) 2710void 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). */
2728uint32_t DHT_size(const DHT *dht) 2734uint32_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(). */
2762void DHT_save(DHT *dht, uint8_t *data) 2770void 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 */
2882int DHT_load(DHT *dht, const uint8_t *data, uint32_t length) 2889int 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 */
2902int DHT_isconnected(const DHT *dht) 2909bool 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 */
2921int DHT_non_lan_connected(const DHT *dht) 2928bool 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}