diff options
author | iphydf <iphydf@users.noreply.github.com> | 2016-08-31 19:12:19 +0100 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2016-08-31 20:04:16 +0100 |
commit | 633da98ae69866efb195e00d9a3a22ace6bada66 (patch) | |
tree | 875535f3d2257c4ea5bb97a553b2f1beab4a1590 /toxcore/DHT.c | |
parent | 6356eb4e4fe407fa7870f2a685d0d08b5c2ec5bb (diff) |
Add braces to all if statements.
Diffstat (limited to 'toxcore/DHT.c')
-rw-r--r-- | toxcore/DHT.c | 416 |
1 files changed, 275 insertions, 141 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 98859856..538cd220 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -81,11 +81,13 @@ int id_closest(const uint8_t *pk, const uint8_t *pk1, const uint8_t *pk2) | |||
81 | distance1 = pk[i] ^ pk1[i]; | 81 | distance1 = pk[i] ^ pk1[i]; |
82 | distance2 = pk[i] ^ pk2[i]; | 82 | distance2 = pk[i] ^ pk2[i]; |
83 | 83 | ||
84 | if (distance1 < distance2) | 84 | if (distance1 < distance2) { |
85 | return 1; | 85 | return 1; |
86 | } | ||
86 | 87 | ||
87 | if (distance1 > distance2) | 88 | if (distance1 > distance2) { |
88 | return 2; | 89 | return 2; |
90 | } | ||
89 | } | 91 | } |
90 | 92 | ||
91 | return 0; | 93 | return 0; |
@@ -98,12 +100,14 @@ static unsigned int bit_by_bit_cmp(const uint8_t *pk1, const uint8_t *pk2) | |||
98 | unsigned int i, j = 0; | 100 | unsigned int i, j = 0; |
99 | 101 | ||
100 | for (i = 0; i < crypto_box_PUBLICKEYBYTES; ++i) { | 102 | for (i = 0; i < crypto_box_PUBLICKEYBYTES; ++i) { |
101 | if (pk1[i] == pk2[i]) | 103 | if (pk1[i] == pk2[i]) { |
102 | continue; | 104 | continue; |
105 | } | ||
103 | 106 | ||
104 | for (j = 0; j < 8; ++j) { | 107 | for (j = 0; j < 8; ++j) { |
105 | if ((pk1[i] & (1 << (7 - j))) != (pk2[i] & (1 << (7 - j)))) | 108 | if ((pk1[i] & (1 << (7 - j))) != (pk2[i] & (1 << (7 - j)))) { |
106 | break; | 109 | break; |
110 | } | ||
107 | } | 111 | } |
108 | 112 | ||
109 | break; | 113 | break; |
@@ -179,10 +183,11 @@ void DHT_get_shared_key_sent(DHT *dht, uint8_t *shared_key, const uint8_t *publi | |||
179 | 183 | ||
180 | void to_net_family(IP *ip) | 184 | void to_net_family(IP *ip) |
181 | { | 185 | { |
182 | if (ip->family == AF_INET) | 186 | if (ip->family == AF_INET) { |
183 | ip->family = TOX_AF_INET; | 187 | ip->family = TOX_AF_INET; |
184 | else if (ip->family == AF_INET6) | 188 | } else if (ip->family == AF_INET6) { |
185 | ip->family = TOX_AF_INET6; | 189 | ip->family = TOX_AF_INET6; |
190 | } | ||
186 | } | 191 | } |
187 | 192 | ||
188 | int to_host_family(IP *ip) | 193 | int to_host_family(IP *ip) |
@@ -253,8 +258,9 @@ int pack_nodes(uint8_t *data, uint16_t length, const Node_format *nodes, uint16_ | |||
253 | if (ipv6 == 0) { | 258 | if (ipv6 == 0) { |
254 | uint32_t size = PACKED_NODE_SIZE_IP4; | 259 | uint32_t size = PACKED_NODE_SIZE_IP4; |
255 | 260 | ||
256 | if (packed_length + size > length) | 261 | if (packed_length + size > length) { |
257 | return -1; | 262 | return -1; |
263 | } | ||
258 | 264 | ||
259 | data[packed_length] = net_family; | 265 | data[packed_length] = net_family; |
260 | memcpy(data + packed_length + 1, &nodes[i].ip_port.ip.ip4, SIZE_IP4); | 266 | memcpy(data + packed_length + 1, &nodes[i].ip_port.ip.ip4, SIZE_IP4); |
@@ -264,8 +270,9 @@ int pack_nodes(uint8_t *data, uint16_t length, const Node_format *nodes, uint16_ | |||
264 | } else if (ipv6 == 1) { | 270 | } else if (ipv6 == 1) { |
265 | uint32_t size = PACKED_NODE_SIZE_IP6; | 271 | uint32_t size = PACKED_NODE_SIZE_IP6; |
266 | 272 | ||
267 | if (packed_length + size > length) | 273 | if (packed_length + size > length) { |
268 | return -1; | 274 | return -1; |
275 | } | ||
269 | 276 | ||
270 | data[packed_length] = net_family; | 277 | data[packed_length] = net_family; |
271 | memcpy(data + packed_length + 1, &nodes[i].ip_port.ip.ip6, SIZE_IP6); | 278 | memcpy(data + packed_length + 1, &nodes[i].ip_port.ip.ip6, SIZE_IP6); |
@@ -300,8 +307,9 @@ int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed | |||
300 | ipv6 = 0; | 307 | ipv6 = 0; |
301 | host_family = AF_INET; | 308 | host_family = AF_INET; |
302 | } else if (data[len_processed] == TOX_TCP_INET) { | 309 | } else if (data[len_processed] == TOX_TCP_INET) { |
303 | if (!tcp_enabled) | 310 | if (!tcp_enabled) { |
304 | return -1; | 311 | return -1; |
312 | } | ||
305 | 313 | ||
306 | ipv6 = 0; | 314 | ipv6 = 0; |
307 | host_family = TCP_INET; | 315 | host_family = TCP_INET; |
@@ -309,8 +317,9 @@ int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed | |||
309 | ipv6 = 1; | 317 | ipv6 = 1; |
310 | host_family = AF_INET6; | 318 | host_family = AF_INET6; |
311 | } else if (data[len_processed] == TOX_TCP_INET6) { | 319 | } else if (data[len_processed] == TOX_TCP_INET6) { |
312 | if (!tcp_enabled) | 320 | if (!tcp_enabled) { |
313 | return -1; | 321 | return -1; |
322 | } | ||
314 | 323 | ||
315 | ipv6 = 1; | 324 | ipv6 = 1; |
316 | host_family = TCP_INET6; | 325 | host_family = TCP_INET6; |
@@ -321,8 +330,9 @@ int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed | |||
321 | if (ipv6 == 0) { | 330 | if (ipv6 == 0) { |
322 | uint32_t size = PACKED_NODE_SIZE_IP4; | 331 | uint32_t size = PACKED_NODE_SIZE_IP4; |
323 | 332 | ||
324 | if (len_processed + size > length) | 333 | if (len_processed + size > length) { |
325 | return -1; | 334 | return -1; |
335 | } | ||
326 | 336 | ||
327 | nodes[num].ip_port.ip.family = host_family; | 337 | nodes[num].ip_port.ip.family = host_family; |
328 | memcpy(&nodes[num].ip_port.ip.ip4, data + len_processed + 1, SIZE_IP4); | 338 | memcpy(&nodes[num].ip_port.ip.ip4, data + len_processed + 1, SIZE_IP4); |
@@ -333,8 +343,9 @@ int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed | |||
333 | } else if (ipv6 == 1) { | 343 | } else if (ipv6 == 1) { |
334 | uint32_t size = PACKED_NODE_SIZE_IP6; | 344 | uint32_t size = PACKED_NODE_SIZE_IP6; |
335 | 345 | ||
336 | if (len_processed + size > length) | 346 | if (len_processed + size > length) { |
337 | return -1; | 347 | return -1; |
348 | } | ||
338 | 349 | ||
339 | nodes[num].ip_port.ip.family = host_family; | 350 | nodes[num].ip_port.ip.family = host_family; |
340 | memcpy(&nodes[num].ip_port.ip.ip6, data + len_processed + 1, SIZE_IP6); | 351 | memcpy(&nodes[num].ip_port.ip.ip6, data + len_processed + 1, SIZE_IP6); |
@@ -347,8 +358,9 @@ int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed | |||
347 | } | 358 | } |
348 | } | 359 | } |
349 | 360 | ||
350 | if (processed_data_len) | 361 | if (processed_data_len) { |
351 | *processed_data_len = len_processed; | 362 | *processed_data_len = len_processed; |
363 | } | ||
352 | 364 | ||
353 | return num; | 365 | return num; |
354 | } | 366 | } |
@@ -369,7 +381,7 @@ static int client_or_ip_port_in_list(Logger *log, Client_data *list, uint16_t le | |||
369 | uint64_t temp_time = unix_time(); | 381 | uint64_t temp_time = unix_time(); |
370 | 382 | ||
371 | /* if public_key is in list, find it and maybe overwrite ip_port */ | 383 | /* if public_key is in list, find it and maybe overwrite ip_port */ |
372 | for (i = 0; i < length; ++i) | 384 | for (i = 0; i < length; ++i) { |
373 | if (id_equal(list[i].public_key, public_key)) { | 385 | if (id_equal(list[i].public_key, public_key)) { |
374 | /* Refresh the client timestamp. */ | 386 | /* Refresh the client timestamp. */ |
375 | if (ip_port.ip.family == AF_INET) { | 387 | if (ip_port.ip.family == AF_INET) { |
@@ -380,8 +392,9 @@ static int client_or_ip_port_in_list(Logger *log, Client_data *list, uint16_t le | |||
380 | ip_ntoa(&ip_port.ip), ntohs(ip_port.port)); | 392 | ip_ntoa(&ip_port.ip), ntohs(ip_port.port)); |
381 | } | 393 | } |
382 | 394 | ||
383 | if (LAN_ip(list[i].assoc4.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0) | 395 | if (LAN_ip(list[i].assoc4.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0) { |
384 | return 1; | 396 | return 1; |
397 | } | ||
385 | 398 | ||
386 | list[i].assoc4.ip_port = ip_port; | 399 | list[i].assoc4.ip_port = ip_port; |
387 | list[i].assoc4.timestamp = temp_time; | 400 | list[i].assoc4.timestamp = temp_time; |
@@ -393,8 +406,9 @@ static int client_or_ip_port_in_list(Logger *log, Client_data *list, uint16_t le | |||
393 | ip_ntoa(&ip_port.ip), ntohs(ip_port.port)); | 406 | ip_ntoa(&ip_port.ip), ntohs(ip_port.port)); |
394 | } | 407 | } |
395 | 408 | ||
396 | if (LAN_ip(list[i].assoc6.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0) | 409 | if (LAN_ip(list[i].assoc6.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0) { |
397 | return 1; | 410 | return 1; |
411 | } | ||
398 | 412 | ||
399 | list[i].assoc6.ip_port = ip_port; | 413 | list[i].assoc6.ip_port = ip_port; |
400 | list[i].assoc6.timestamp = temp_time; | 414 | list[i].assoc6.timestamp = temp_time; |
@@ -402,6 +416,7 @@ static int client_or_ip_port_in_list(Logger *log, Client_data *list, uint16_t le | |||
402 | 416 | ||
403 | return 1; | 417 | return 1; |
404 | } | 418 | } |
419 | } | ||
405 | 420 | ||
406 | /* public_key not in list yet: see if we can find an identical ip_port, in | 421 | /* public_key not in list yet: see if we can find an identical ip_port, in |
407 | * that case we kill the old public_key by overwriting it with the new one | 422 | * that case we kill the old public_key by overwriting it with the new one |
@@ -445,8 +460,9 @@ static int client_in_nodelist(const Node_format *list, uint16_t length, const ui | |||
445 | uint32_t i; | 460 | uint32_t i; |
446 | 461 | ||
447 | for (i = 0; i < length; ++i) { | 462 | for (i = 0; i < length; ++i) { |
448 | if (id_equal(list[i].public_key, public_key)) | 463 | if (id_equal(list[i].public_key, public_key)) { |
449 | return 1; | 464 | return 1; |
465 | } | ||
450 | } | 466 | } |
451 | 467 | ||
452 | return 0; | 468 | return 0; |
@@ -460,8 +476,9 @@ static int friend_number(const DHT *dht, const uint8_t *public_key) | |||
460 | uint32_t i; | 476 | uint32_t i; |
461 | 477 | ||
462 | for (i = 0; i < dht->num_friends; ++i) { | 478 | for (i = 0; i < dht->num_friends; ++i) { |
463 | if (id_equal(dht->friends_list[i].public_key, public_key)) | 479 | if (id_equal(dht->friends_list[i].public_key, public_key)) { |
464 | return i; | 480 | return i; |
481 | } | ||
465 | } | 482 | } |
466 | 483 | ||
467 | return -1; | 484 | return -1; |
@@ -484,8 +501,9 @@ _Bool add_to_list(Node_format *nodes_list, unsigned int length, const uint8_t *p | |||
484 | memcpy(nodes_list[i].public_key, pk, crypto_box_PUBLICKEYBYTES); | 501 | memcpy(nodes_list[i].public_key, pk, crypto_box_PUBLICKEYBYTES); |
485 | nodes_list[i].ip_port = ip_port; | 502 | nodes_list[i].ip_port = ip_port; |
486 | 503 | ||
487 | if (i != (length - 1)) | 504 | if (i != (length - 1)) { |
488 | add_to_list(nodes_list, length, pk_bak, ip_port_bak, cmp_pk); | 505 | add_to_list(nodes_list, length, pk_bak, ip_port_bak, cmp_pk); |
506 | } | ||
489 | 507 | ||
490 | return 1; | 508 | return 1; |
491 | } | 509 | } |
@@ -513,8 +531,9 @@ static void get_close_nodes_inner(const uint8_t *public_key, Node_format *nodes_ | |||
513 | sa_family_t sa_family, const Client_data *client_list, uint32_t client_list_length, | 531 | sa_family_t sa_family, const Client_data *client_list, uint32_t client_list_length, |
514 | uint32_t *num_nodes_ptr, uint8_t is_LAN, uint8_t want_good) | 532 | uint32_t *num_nodes_ptr, uint8_t is_LAN, uint8_t want_good) |
515 | { | 533 | { |
516 | if ((sa_family != AF_INET) && (sa_family != AF_INET6) && (sa_family != 0)) | 534 | if ((sa_family != AF_INET) && (sa_family != AF_INET6) && (sa_family != 0)) { |
517 | return; | 535 | return; |
536 | } | ||
518 | 537 | ||
519 | uint32_t num_nodes = *num_nodes_ptr; | 538 | uint32_t num_nodes = *num_nodes_ptr; |
520 | uint32_t i; | 539 | uint32_t i; |
@@ -523,8 +542,9 @@ static void get_close_nodes_inner(const uint8_t *public_key, Node_format *nodes_ | |||
523 | const Client_data *client = &client_list[i]; | 542 | const Client_data *client = &client_list[i]; |
524 | 543 | ||
525 | /* node already in list? */ | 544 | /* node already in list? */ |
526 | if (client_in_nodelist(nodes_list, MAX_SENT_NODES, client->public_key)) | 545 | if (client_in_nodelist(nodes_list, MAX_SENT_NODES, client->public_key)) { |
527 | continue; | 546 | continue; |
547 | } | ||
528 | 548 | ||
529 | const IPPTsPng *ipptp = NULL; | 549 | const IPPTsPng *ipptp = NULL; |
530 | 550 | ||
@@ -541,16 +561,19 @@ static void get_close_nodes_inner(const uint8_t *public_key, Node_format *nodes_ | |||
541 | } | 561 | } |
542 | 562 | ||
543 | /* node not in a good condition? */ | 563 | /* node not in a good condition? */ |
544 | if (is_timeout(ipptp->timestamp, BAD_NODE_TIMEOUT)) | 564 | if (is_timeout(ipptp->timestamp, BAD_NODE_TIMEOUT)) { |
545 | continue; | 565 | continue; |
566 | } | ||
546 | 567 | ||
547 | /* don't send LAN ips to non LAN peers */ | 568 | /* don't send LAN ips to non LAN peers */ |
548 | if (LAN_ip(ipptp->ip_port.ip) == 0 && !is_LAN) | 569 | if (LAN_ip(ipptp->ip_port.ip) == 0 && !is_LAN) { |
549 | continue; | 570 | continue; |
571 | } | ||
550 | 572 | ||
551 | if (LAN_ip(ipptp->ip_port.ip) != 0 && want_good && hardening_correct(&ipptp->hardening) != HARDENING_ALL_OK | 573 | if (LAN_ip(ipptp->ip_port.ip) != 0 && want_good && hardening_correct(&ipptp->hardening) != HARDENING_ALL_OK |
552 | && !id_equal(public_key, client->public_key)) | 574 | && !id_equal(public_key, client->public_key)) { |
553 | continue; | 575 | continue; |
576 | } | ||
554 | 577 | ||
555 | if (num_nodes < MAX_SENT_NODES) { | 578 | if (num_nodes < MAX_SENT_NODES) { |
556 | memcpy(nodes_list[num_nodes].public_key, | 579 | memcpy(nodes_list[num_nodes].public_key, |
@@ -588,10 +611,11 @@ static int get_somewhat_close_nodes(const DHT *dht, const uint8_t *public_key, N | |||
588 | dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, | 611 | dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, |
589 | &num_nodes, is_LAN, want_good); | 612 | &num_nodes, is_LAN, want_good); |
590 | */ | 613 | */ |
591 | for (i = 0; i < dht->num_friends; ++i) | 614 | for (i = 0; i < dht->num_friends; ++i) { |
592 | get_close_nodes_inner(public_key, nodes_list, sa_family, | 615 | get_close_nodes_inner(public_key, nodes_list, sa_family, |
593 | dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, | 616 | dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, |
594 | &num_nodes, is_LAN, 0); | 617 | &num_nodes, is_LAN, 0); |
618 | } | ||
595 | 619 | ||
596 | return num_nodes; | 620 | return num_nodes; |
597 | } | 621 | } |
@@ -665,14 +689,17 @@ static int cmp_dht_entry(const void *a, const void *b) | |||
665 | int t1 = is_timeout(entry1.assoc4.timestamp, BAD_NODE_TIMEOUT) && is_timeout(entry1.assoc6.timestamp, BAD_NODE_TIMEOUT); | 689 | int t1 = is_timeout(entry1.assoc4.timestamp, BAD_NODE_TIMEOUT) && is_timeout(entry1.assoc6.timestamp, BAD_NODE_TIMEOUT); |
666 | int t2 = is_timeout(entry2.assoc4.timestamp, BAD_NODE_TIMEOUT) && is_timeout(entry2.assoc6.timestamp, BAD_NODE_TIMEOUT); | 690 | int t2 = is_timeout(entry2.assoc4.timestamp, BAD_NODE_TIMEOUT) && is_timeout(entry2.assoc6.timestamp, BAD_NODE_TIMEOUT); |
667 | 691 | ||
668 | if (t1 && t2) | 692 | if (t1 && t2) { |
669 | return 0; | 693 | return 0; |
694 | } | ||
670 | 695 | ||
671 | if (t1) | 696 | if (t1) { |
672 | return -1; | 697 | return -1; |
698 | } | ||
673 | 699 | ||
674 | if (t2) | 700 | if (t2) { |
675 | return 1; | 701 | return 1; |
702 | } | ||
676 | 703 | ||
677 | t1 = hardening_correct(&entry1.assoc4.hardening) != HARDENING_ALL_OK | 704 | t1 = hardening_correct(&entry1.assoc4.hardening) != HARDENING_ALL_OK |
678 | && hardening_correct(&entry1.assoc6.hardening) != HARDENING_ALL_OK; | 705 | && hardening_correct(&entry1.assoc6.hardening) != HARDENING_ALL_OK; |
@@ -680,20 +707,24 @@ static int cmp_dht_entry(const void *a, const void *b) | |||
680 | && hardening_correct(&entry2.assoc6.hardening) != HARDENING_ALL_OK; | 707 | && hardening_correct(&entry2.assoc6.hardening) != HARDENING_ALL_OK; |
681 | 708 | ||
682 | if (t1 != t2) { | 709 | if (t1 != t2) { |
683 | if (t1) | 710 | if (t1) { |
684 | return -1; | 711 | return -1; |
712 | } | ||
685 | 713 | ||
686 | if (t2) | 714 | if (t2) { |
687 | return 1; | 715 | return 1; |
716 | } | ||
688 | } | 717 | } |
689 | 718 | ||
690 | int close = id_closest(cmp_public_key, entry1.public_key, entry2.public_key); | 719 | int close = id_closest(cmp_public_key, entry1.public_key, entry2.public_key); |
691 | 720 | ||
692 | if (close == 1) | 721 | if (close == 1) { |
693 | return 1; | 722 | return 1; |
723 | } | ||
694 | 724 | ||
695 | if (close == 2) | 725 | if (close == 2) { |
696 | return -1; | 726 | return -1; |
727 | } | ||
697 | 728 | ||
698 | return 0; | 729 | return 0; |
699 | } | 730 | } |
@@ -738,8 +769,9 @@ static int replace_all( Client_data *list, | |||
738 | IP_Port ip_port, | 769 | IP_Port ip_port, |
739 | const uint8_t *comp_public_key ) | 770 | const uint8_t *comp_public_key ) |
740 | { | 771 | { |
741 | if ((ip_port.ip.family != AF_INET) && (ip_port.ip.family != AF_INET6)) | 772 | if ((ip_port.ip.family != AF_INET) && (ip_port.ip.family != AF_INET6)) { |
742 | return 0; | 773 | return 0; |
774 | } | ||
743 | 775 | ||
744 | if (store_node_ok(&list[1], public_key, comp_public_key) || store_node_ok(&list[0], public_key, comp_public_key)) { | 776 | if (store_node_ok(&list[1], public_key, comp_public_key) || store_node_ok(&list[0], public_key, comp_public_key)) { |
745 | sort_client_list(list, length, comp_public_key); | 777 | sort_client_list(list, length, comp_public_key); |
@@ -787,8 +819,9 @@ static int add_to_close(DHT *dht, const uint8_t *public_key, IP_Port ip_port, _B | |||
787 | 819 | ||
788 | unsigned int index = bit_by_bit_cmp(public_key, dht->self_public_key); | 820 | unsigned int index = bit_by_bit_cmp(public_key, dht->self_public_key); |
789 | 821 | ||
790 | if (index > LCLIENT_LENGTH) | 822 | if (index > LCLIENT_LENGTH) { |
791 | index = LCLIENT_LENGTH - 1; | 823 | index = LCLIENT_LENGTH - 1; |
824 | } | ||
792 | 825 | ||
793 | for (i = 0; i < LCLIENT_NODES; ++i) { | 826 | for (i = 0; i < LCLIENT_NODES; ++i) { |
794 | Client_data *client = &dht->close_clientlist[(index * LCLIENT_NODES) + i]; | 827 | Client_data *client = &dht->close_clientlist[(index * LCLIENT_NODES) + i]; |
@@ -929,10 +962,12 @@ int addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *public_key) | |||
929 | * to replace the first ip by the second. | 962 | * to replace the first ip by the second. |
930 | */ | 963 | */ |
931 | if (!client_or_ip_port_in_list(dht->log, dht->close_clientlist, LCLIENT_LIST, public_key, ip_port)) { | 964 | if (!client_or_ip_port_in_list(dht->log, dht->close_clientlist, LCLIENT_LIST, public_key, ip_port)) { |
932 | if (add_to_close(dht, public_key, ip_port, 0)) | 965 | if (add_to_close(dht, public_key, ip_port, 0)) { |
933 | used++; | 966 | used++; |
934 | } else | 967 | } |
968 | } else { | ||
935 | used++; | 969 | used++; |
970 | } | ||
936 | 971 | ||
937 | DHT_Friend *friend_foundip = 0; | 972 | DHT_Friend *friend_foundip = 0; |
938 | 973 | ||
@@ -965,9 +1000,10 @@ int addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *public_key) | |||
965 | uint32_t j; | 1000 | uint32_t j; |
966 | 1001 | ||
967 | for (j = 0; j < friend_foundip->lock_count; ++j) { | 1002 | for (j = 0; j < friend_foundip->lock_count; ++j) { |
968 | if (friend_foundip->callbacks[j].ip_callback) | 1003 | if (friend_foundip->callbacks[j].ip_callback) { |
969 | friend_foundip->callbacks[j].ip_callback(friend_foundip->callbacks[j].data, friend_foundip->callbacks[j].number, | 1004 | friend_foundip->callbacks[j].ip_callback(friend_foundip->callbacks[j].data, friend_foundip->callbacks[j].number, |
970 | ip_port); | 1005 | ip_port); |
1006 | } | ||
971 | } | 1007 | } |
972 | } | 1008 | } |
973 | 1009 | ||
@@ -1060,8 +1096,9 @@ static int getnodes(DHT *dht, IP_Port ip_port, const uint8_t *public_key, const | |||
1060 | const Node_format *sendback_node) | 1096 | const Node_format *sendback_node) |
1061 | { | 1097 | { |
1062 | /* Check if packet is going to be sent to ourself. */ | 1098 | /* Check if packet is going to be sent to ourself. */ |
1063 | if (id_equal(public_key, dht->self_public_key)) | 1099 | if (id_equal(public_key, dht->self_public_key)) { |
1064 | return -1; | 1100 | return -1; |
1101 | } | ||
1065 | 1102 | ||
1066 | uint8_t plain_message[sizeof(Node_format) * 2] = {0}; | 1103 | uint8_t plain_message[sizeof(Node_format) * 2] = {0}; |
1067 | 1104 | ||
@@ -1079,8 +1116,9 @@ static int getnodes(DHT *dht, IP_Port ip_port, const uint8_t *public_key, const | |||
1079 | ping_id = ping_array_add(&dht->dht_ping_array, plain_message, sizeof(receiver)); | 1116 | ping_id = ping_array_add(&dht->dht_ping_array, plain_message, sizeof(receiver)); |
1080 | } | 1117 | } |
1081 | 1118 | ||
1082 | if (ping_id == 0) | 1119 | if (ping_id == 0) { |
1083 | return -1; | 1120 | return -1; |
1121 | } | ||
1084 | 1122 | ||
1085 | uint8_t plain[crypto_box_PUBLICKEYBYTES + sizeof(ping_id)]; | 1123 | uint8_t plain[crypto_box_PUBLICKEYBYTES + sizeof(ping_id)]; |
1086 | uint8_t encrypt[sizeof(plain) + crypto_box_MACBYTES]; | 1124 | uint8_t encrypt[sizeof(plain) + crypto_box_MACBYTES]; |
@@ -1101,8 +1139,9 @@ static int getnodes(DHT *dht, IP_Port ip_port, const uint8_t *public_key, const | |||
1101 | sizeof(plain), | 1139 | sizeof(plain), |
1102 | encrypt ); | 1140 | encrypt ); |
1103 | 1141 | ||
1104 | if (len != sizeof(encrypt)) | 1142 | if (len != sizeof(encrypt)) { |
1105 | return -1; | 1143 | return -1; |
1144 | } | ||
1106 | 1145 | ||
1107 | data[0] = NET_PACKET_GET_NODES; | 1146 | data[0] = NET_PACKET_GET_NODES; |
1108 | memcpy(data + 1, dht->self_public_key, crypto_box_PUBLICKEYBYTES); | 1147 | memcpy(data + 1, dht->self_public_key, crypto_box_PUBLICKEYBYTES); |
@@ -1117,11 +1156,13 @@ static int sendnodes_ipv6(const DHT *dht, IP_Port ip_port, const uint8_t *public | |||
1117 | const uint8_t *sendback_data, uint16_t length, const uint8_t *shared_encryption_key) | 1156 | const uint8_t *sendback_data, uint16_t length, const uint8_t *shared_encryption_key) |
1118 | { | 1157 | { |
1119 | /* Check if packet is going to be sent to ourself. */ | 1158 | /* Check if packet is going to be sent to ourself. */ |
1120 | if (id_equal(public_key, dht->self_public_key)) | 1159 | if (id_equal(public_key, dht->self_public_key)) { |
1121 | return -1; | 1160 | return -1; |
1161 | } | ||
1122 | 1162 | ||
1123 | if (length != sizeof(uint64_t)) | 1163 | if (length != sizeof(uint64_t)) { |
1124 | return -1; | 1164 | return -1; |
1165 | } | ||
1125 | 1166 | ||
1126 | size_t Node_format_size = sizeof(Node_format); | 1167 | size_t Node_format_size = sizeof(Node_format); |
1127 | uint8_t data[1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES | 1168 | uint8_t data[1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES |
@@ -1140,8 +1181,9 @@ static int sendnodes_ipv6(const DHT *dht, IP_Port ip_port, const uint8_t *public | |||
1140 | if (num_nodes) { | 1181 | if (num_nodes) { |
1141 | nodes_length = pack_nodes(plain + 1, Node_format_size * MAX_SENT_NODES, nodes_list, num_nodes); | 1182 | nodes_length = pack_nodes(plain + 1, Node_format_size * MAX_SENT_NODES, nodes_list, num_nodes); |
1142 | 1183 | ||
1143 | if (nodes_length <= 0) | 1184 | if (nodes_length <= 0) { |
1144 | return -1; | 1185 | return -1; |
1186 | } | ||
1145 | } | 1187 | } |
1146 | 1188 | ||
1147 | plain[0] = num_nodes; | 1189 | plain[0] = num_nodes; |
@@ -1152,8 +1194,9 @@ static int sendnodes_ipv6(const DHT *dht, IP_Port ip_port, const uint8_t *public | |||
1152 | 1 + nodes_length + length, | 1194 | 1 + nodes_length + length, |
1153 | encrypt ); | 1195 | encrypt ); |
1154 | 1196 | ||
1155 | if (len != 1 + nodes_length + length + crypto_box_MACBYTES) | 1197 | if (len != 1 + nodes_length + length + crypto_box_MACBYTES) { |
1156 | return -1; | 1198 | return -1; |
1199 | } | ||
1157 | 1200 | ||
1158 | data[0] = NET_PACKET_SEND_NODES_IPV6; | 1201 | data[0] = NET_PACKET_SEND_NODES_IPV6; |
1159 | memcpy(data + 1, dht->self_public_key, crypto_box_PUBLICKEYBYTES); | 1202 | memcpy(data + 1, dht->self_public_key, crypto_box_PUBLICKEYBYTES); |
@@ -1166,14 +1209,16 @@ static int sendnodes_ipv6(const DHT *dht, IP_Port ip_port, const uint8_t *public | |||
1166 | static int handle_getnodes(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata) | 1209 | static int handle_getnodes(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata) |
1167 | { | 1210 | { |
1168 | if (length != (1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + sizeof( | 1211 | if (length != (1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + sizeof( |
1169 | uint64_t) + crypto_box_MACBYTES)) | 1212 | uint64_t) + crypto_box_MACBYTES)) { |
1170 | return 1; | 1213 | return 1; |
1214 | } | ||
1171 | 1215 | ||
1172 | DHT *dht = object; | 1216 | DHT *dht = object; |
1173 | 1217 | ||
1174 | /* Check if packet is from ourself. */ | 1218 | /* Check if packet is from ourself. */ |
1175 | if (id_equal(packet + 1, dht->self_public_key)) | 1219 | if (id_equal(packet + 1, dht->self_public_key)) { |
1176 | return 1; | 1220 | return 1; |
1221 | } | ||
1177 | 1222 | ||
1178 | uint8_t plain[crypto_box_PUBLICKEYBYTES + sizeof(uint64_t)]; | 1223 | uint8_t plain[crypto_box_PUBLICKEYBYTES + sizeof(uint64_t)]; |
1179 | uint8_t shared_key[crypto_box_BEFORENMBYTES]; | 1224 | uint8_t shared_key[crypto_box_BEFORENMBYTES]; |
@@ -1185,8 +1230,9 @@ static int handle_getnodes(void *object, IP_Port source, const uint8_t *packet, | |||
1185 | crypto_box_PUBLICKEYBYTES + sizeof(uint64_t) + crypto_box_MACBYTES, | 1230 | crypto_box_PUBLICKEYBYTES + sizeof(uint64_t) + crypto_box_MACBYTES, |
1186 | plain ); | 1231 | plain ); |
1187 | 1232 | ||
1188 | if (len != crypto_box_PUBLICKEYBYTES + sizeof(uint64_t)) | 1233 | if (len != crypto_box_PUBLICKEYBYTES + sizeof(uint64_t)) { |
1189 | return 1; | 1234 | return 1; |
1235 | } | ||
1190 | 1236 | ||
1191 | sendnodes_ipv6(dht, source, packet + 1, plain, plain + crypto_box_PUBLICKEYBYTES, sizeof(uint64_t), shared_key); | 1237 | sendnodes_ipv6(dht, source, packet + 1, plain, plain + crypto_box_PUBLICKEYBYTES, sizeof(uint64_t), shared_key); |
1192 | 1238 | ||
@@ -1212,8 +1258,9 @@ static uint8_t sent_getnode_to_node(DHT *dht, const uint8_t *public_key, IP_Port | |||
1212 | Node_format test; | 1258 | Node_format test; |
1213 | memcpy(&test, data, sizeof(Node_format)); | 1259 | memcpy(&test, data, sizeof(Node_format)); |
1214 | 1260 | ||
1215 | if (!ipport_equal(&test.ip_port, &node_ip_port) || public_key_cmp(test.public_key, public_key) != 0) | 1261 | if (!ipport_equal(&test.ip_port, &node_ip_port) || public_key_cmp(test.public_key, public_key) != 0) { |
1216 | return 0; | 1262 | return 0; |
1263 | } | ||
1217 | 1264 | ||
1218 | return 1; | 1265 | return 1; |
1219 | } | 1266 | } |
@@ -1228,16 +1275,19 @@ static int handle_sendnodes_core(void *object, IP_Port source, const uint8_t *pa | |||
1228 | DHT *dht = object; | 1275 | DHT *dht = object; |
1229 | uint32_t cid_size = 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + 1 + sizeof(uint64_t) + crypto_box_MACBYTES; | 1276 | uint32_t cid_size = 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + 1 + sizeof(uint64_t) + crypto_box_MACBYTES; |
1230 | 1277 | ||
1231 | if (length < cid_size) /* too short */ | 1278 | if (length < cid_size) { /* too short */ |
1232 | return 1; | 1279 | return 1; |
1280 | } | ||
1233 | 1281 | ||
1234 | uint32_t data_size = length - cid_size; | 1282 | uint32_t data_size = length - cid_size; |
1235 | 1283 | ||
1236 | if (data_size == 0) | 1284 | if (data_size == 0) { |
1237 | return 1; | 1285 | return 1; |
1286 | } | ||
1238 | 1287 | ||
1239 | if (data_size > sizeof(Node_format) * MAX_SENT_NODES) /* invalid length */ | 1288 | if (data_size > sizeof(Node_format) * MAX_SENT_NODES) { /* invalid length */ |
1240 | return 1; | 1289 | return 1; |
1290 | } | ||
1241 | 1291 | ||
1242 | uint8_t plain[1 + data_size + sizeof(uint64_t)]; | 1292 | uint8_t plain[1 + data_size + sizeof(uint64_t)]; |
1243 | uint8_t shared_key[crypto_box_BEFORENMBYTES]; | 1293 | uint8_t shared_key[crypto_box_BEFORENMBYTES]; |
@@ -1249,31 +1299,37 @@ static int handle_sendnodes_core(void *object, IP_Port source, const uint8_t *pa | |||
1249 | 1 + data_size + sizeof(uint64_t) + crypto_box_MACBYTES, | 1299 | 1 + data_size + sizeof(uint64_t) + crypto_box_MACBYTES, |
1250 | plain); | 1300 | plain); |
1251 | 1301 | ||
1252 | if ((unsigned int)len != sizeof(plain)) | 1302 | if ((unsigned int)len != sizeof(plain)) { |
1253 | return 1; | 1303 | return 1; |
1304 | } | ||
1254 | 1305 | ||
1255 | if (plain[0] > size_plain_nodes) | 1306 | if (plain[0] > size_plain_nodes) { |
1256 | return 1; | 1307 | return 1; |
1308 | } | ||
1257 | 1309 | ||
1258 | Node_format sendback_node; | 1310 | Node_format sendback_node; |
1259 | 1311 | ||
1260 | uint64_t ping_id; | 1312 | uint64_t ping_id; |
1261 | memcpy(&ping_id, plain + 1 + data_size, sizeof(ping_id)); | 1313 | memcpy(&ping_id, plain + 1 + data_size, sizeof(ping_id)); |
1262 | 1314 | ||
1263 | if (!sent_getnode_to_node(dht, packet + 1, source, ping_id, &sendback_node)) | 1315 | if (!sent_getnode_to_node(dht, packet + 1, source, ping_id, &sendback_node)) { |
1264 | return 1; | 1316 | return 1; |
1317 | } | ||
1265 | 1318 | ||
1266 | uint16_t length_nodes = 0; | 1319 | uint16_t length_nodes = 0; |
1267 | int num_nodes = unpack_nodes(plain_nodes, plain[0], &length_nodes, plain + 1, data_size, 0); | 1320 | int num_nodes = unpack_nodes(plain_nodes, plain[0], &length_nodes, plain + 1, data_size, 0); |
1268 | 1321 | ||
1269 | if (length_nodes != data_size) | 1322 | if (length_nodes != data_size) { |
1270 | return 1; | 1323 | return 1; |
1324 | } | ||
1271 | 1325 | ||
1272 | if (num_nodes != plain[0]) | 1326 | if (num_nodes != plain[0]) { |
1273 | return 1; | 1327 | return 1; |
1328 | } | ||
1274 | 1329 | ||
1275 | if (num_nodes < 0) | 1330 | if (num_nodes < 0) { |
1276 | return 1; | 1331 | return 1; |
1332 | } | ||
1277 | 1333 | ||
1278 | /* store the address the *request* was sent to */ | 1334 | /* store the address the *request* was sent to */ |
1279 | addto_lists(dht, source, packet + 1); | 1335 | addto_lists(dht, source, packet + 1); |
@@ -1290,11 +1346,13 @@ static int handle_sendnodes_ipv6(void *object, IP_Port source, const uint8_t *pa | |||
1290 | Node_format plain_nodes[MAX_SENT_NODES]; | 1346 | Node_format plain_nodes[MAX_SENT_NODES]; |
1291 | uint32_t num_nodes; | 1347 | uint32_t num_nodes; |
1292 | 1348 | ||
1293 | if (handle_sendnodes_core(object, source, packet, length, plain_nodes, MAX_SENT_NODES, &num_nodes)) | 1349 | if (handle_sendnodes_core(object, source, packet, length, plain_nodes, MAX_SENT_NODES, &num_nodes)) { |
1294 | return 1; | 1350 | return 1; |
1351 | } | ||
1295 | 1352 | ||
1296 | if (num_nodes == 0) | 1353 | if (num_nodes == 0) { |
1297 | return 0; | 1354 | return 0; |
1355 | } | ||
1298 | 1356 | ||
1299 | uint32_t i; | 1357 | uint32_t i; |
1300 | 1358 | ||
@@ -1322,8 +1380,9 @@ int DHT_addfriend(DHT *dht, const uint8_t *public_key, void (*ip_callback)(void | |||
1322 | if (friend_num != -1) { /* Is friend already in DHT? */ | 1380 | if (friend_num != -1) { /* Is friend already in DHT? */ |
1323 | DHT_Friend *friend = &dht->friends_list[friend_num]; | 1381 | DHT_Friend *friend = &dht->friends_list[friend_num]; |
1324 | 1382 | ||
1325 | if (friend->lock_count == DHT_FRIEND_MAX_LOCKS) | 1383 | if (friend->lock_count == DHT_FRIEND_MAX_LOCKS) { |
1326 | return -1; | 1384 | return -1; |
1385 | } | ||
1327 | 1386 | ||
1328 | lock_num = friend->lock_count; | 1387 | lock_num = friend->lock_count; |
1329 | ++friend->lock_count; | 1388 | ++friend->lock_count; |
@@ -1331,8 +1390,9 @@ int DHT_addfriend(DHT *dht, const uint8_t *public_key, void (*ip_callback)(void | |||
1331 | friend->callbacks[lock_num].data = data; | 1390 | friend->callbacks[lock_num].data = data; |
1332 | friend->callbacks[lock_num].number = number; | 1391 | friend->callbacks[lock_num].number = number; |
1333 | 1392 | ||
1334 | if (lock_count) | 1393 | if (lock_count) { |
1335 | *lock_count = lock_num + 1; | 1394 | *lock_count = lock_num + 1; |
1395 | } | ||
1336 | 1396 | ||
1337 | return 0; | 1397 | return 0; |
1338 | } | 1398 | } |
@@ -1340,8 +1400,9 @@ int DHT_addfriend(DHT *dht, const uint8_t *public_key, void (*ip_callback)(void | |||
1340 | DHT_Friend *temp; | 1400 | DHT_Friend *temp; |
1341 | temp = realloc(dht->friends_list, sizeof(DHT_Friend) * (dht->num_friends + 1)); | 1401 | temp = realloc(dht->friends_list, sizeof(DHT_Friend) * (dht->num_friends + 1)); |
1342 | 1402 | ||
1343 | if (temp == NULL) | 1403 | if (temp == NULL) { |
1344 | return -1; | 1404 | return -1; |
1405 | } | ||
1345 | 1406 | ||
1346 | dht->friends_list = temp; | 1407 | dht->friends_list = temp; |
1347 | DHT_Friend *friend = &dht->friends_list[dht->num_friends]; | 1408 | DHT_Friend *friend = &dht->friends_list[dht->num_friends]; |
@@ -1357,8 +1418,9 @@ int DHT_addfriend(DHT *dht, const uint8_t *public_key, void (*ip_callback)(void | |||
1357 | friend->callbacks[lock_num].data = data; | 1418 | friend->callbacks[lock_num].data = data; |
1358 | friend->callbacks[lock_num].number = number; | 1419 | friend->callbacks[lock_num].number = number; |
1359 | 1420 | ||
1360 | if (lock_count) | 1421 | if (lock_count) { |
1361 | *lock_count = lock_num + 1; | 1422 | *lock_count = lock_num + 1; |
1423 | } | ||
1362 | 1424 | ||
1363 | friend->num_to_bootstrap = get_close_nodes(dht, friend->public_key, friend->to_bootstrap, 0, 1, 0); | 1425 | friend->num_to_bootstrap = get_close_nodes(dht, friend->public_key, friend->to_bootstrap, 0, 1, 0); |
1364 | 1426 | ||
@@ -1402,8 +1464,9 @@ int DHT_delfriend(DHT *dht, const uint8_t *public_key, uint16_t lock_count) | |||
1402 | 1464 | ||
1403 | temp = realloc(dht->friends_list, sizeof(DHT_Friend) * (dht->num_friends)); | 1465 | temp = realloc(dht->friends_list, sizeof(DHT_Friend) * (dht->num_friends)); |
1404 | 1466 | ||
1405 | if (temp == NULL) | 1467 | if (temp == NULL) { |
1406 | return -1; | 1468 | return -1; |
1469 | } | ||
1407 | 1470 | ||
1408 | dht->friends_list = temp; | 1471 | dht->friends_list = temp; |
1409 | return 0; | 1472 | return 0; |
@@ -1427,11 +1490,12 @@ int DHT_getfriendip(const DHT *dht, const uint8_t *public_key, IP_Port *ip_port) | |||
1427 | IPPTsPng *assoc = NULL; | 1490 | IPPTsPng *assoc = NULL; |
1428 | uint32_t a; | 1491 | uint32_t a; |
1429 | 1492 | ||
1430 | for (a = 0, assoc = &client->assoc6; a < 2; a++, assoc = &client->assoc4) | 1493 | for (a = 0, assoc = &client->assoc6; a < 2; a++, assoc = &client->assoc4) { |
1431 | if (!is_timeout(assoc->timestamp, BAD_NODE_TIMEOUT)) { | 1494 | if (!is_timeout(assoc->timestamp, BAD_NODE_TIMEOUT)) { |
1432 | *ip_port = assoc->ip_port; | 1495 | *ip_port = assoc->ip_port; |
1433 | return 1; | 1496 | return 1; |
1434 | } | 1497 | } |
1498 | } | ||
1435 | } | 1499 | } |
1436 | } | 1500 | } |
1437 | 1501 | ||
@@ -1462,7 +1526,7 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co | |||
1462 | IPPTsPng *assoc; | 1526 | IPPTsPng *assoc; |
1463 | uint32_t a; | 1527 | uint32_t a; |
1464 | 1528 | ||
1465 | for (a = 0, assoc = &client->assoc6; a < 2; a++, assoc = &client->assoc4) | 1529 | for (a = 0, assoc = &client->assoc6; a < 2; a++, assoc = &client->assoc4) { |
1466 | if (!is_timeout(assoc->timestamp, KILL_NODE_TIMEOUT)) { | 1530 | if (!is_timeout(assoc->timestamp, KILL_NODE_TIMEOUT)) { |
1467 | sort = 0; | 1531 | sort = 0; |
1468 | not_kill++; | 1532 | not_kill++; |
@@ -1486,6 +1550,7 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co | |||
1486 | sort_ok = 1; | 1550 | sort_ok = 1; |
1487 | } | 1551 | } |
1488 | } | 1552 | } |
1553 | } | ||
1489 | } | 1554 | } |
1490 | 1555 | ||
1491 | if (sortable && sort_ok) { | 1556 | if (sortable && sort_ok) { |
@@ -1559,9 +1624,11 @@ static void do_Close(DHT *dht) | |||
1559 | Client_data *client = &dht->close_clientlist[i]; | 1624 | Client_data *client = &dht->close_clientlist[i]; |
1560 | IPPTsPng *assoc; | 1625 | IPPTsPng *assoc; |
1561 | 1626 | ||
1562 | for (a = 0, assoc = &client->assoc4; a < 2; a++, assoc = &client->assoc6) | 1627 | for (a = 0, assoc = &client->assoc4; a < 2; a++, assoc = &client->assoc6) { |
1563 | if (assoc->timestamp) | 1628 | if (assoc->timestamp) { |
1564 | assoc->timestamp = badonly; | 1629 | assoc->timestamp = badonly; |
1630 | } | ||
1631 | } | ||
1565 | } | 1632 | } |
1566 | } | 1633 | } |
1567 | } | 1634 | } |
@@ -1610,8 +1677,9 @@ int DHT_bootstrap_from_address(DHT *dht, const char *address, uint8_t ipv6enable | |||
1610 | } | 1677 | } |
1611 | 1678 | ||
1612 | return 1; | 1679 | return 1; |
1613 | } else | 1680 | } else { |
1614 | return 0; | 1681 | return 0; |
1682 | } | ||
1615 | } | 1683 | } |
1616 | 1684 | ||
1617 | /* Send the given packet to node with public_key | 1685 | /* Send the given packet to node with public_key |
@@ -1626,12 +1694,13 @@ int route_packet(const DHT *dht, const uint8_t *public_key, const uint8_t *packe | |||
1626 | if (id_equal(public_key, dht->close_clientlist[i].public_key)) { | 1694 | if (id_equal(public_key, dht->close_clientlist[i].public_key)) { |
1627 | const Client_data *client = &dht->close_clientlist[i]; | 1695 | const Client_data *client = &dht->close_clientlist[i]; |
1628 | 1696 | ||
1629 | if (ip_isset(&client->assoc6.ip_port.ip)) | 1697 | if (ip_isset(&client->assoc6.ip_port.ip)) { |
1630 | return sendpacket(dht->net, client->assoc6.ip_port, packet, length); | 1698 | return sendpacket(dht->net, client->assoc6.ip_port, packet, length); |
1631 | else if (ip_isset(&client->assoc4.ip_port.ip)) | 1699 | } else if (ip_isset(&client->assoc4.ip_port.ip)) { |
1632 | return sendpacket(dht->net, client->assoc4.ip_port, packet, length); | 1700 | return sendpacket(dht->net, client->assoc4.ip_port, packet, length); |
1633 | else | 1701 | } else { |
1634 | break; | 1702 | break; |
1703 | } | ||
1635 | } | 1704 | } |
1636 | } | 1705 | } |
1637 | 1706 | ||
@@ -1647,8 +1716,9 @@ int route_packet(const DHT *dht, const uint8_t *public_key, const uint8_t *packe | |||
1647 | */ | 1716 | */ |
1648 | static int friend_iplist(const DHT *dht, IP_Port *ip_portlist, uint16_t friend_num) | 1717 | static int friend_iplist(const DHT *dht, IP_Port *ip_portlist, uint16_t friend_num) |
1649 | { | 1718 | { |
1650 | if (friend_num >= dht->num_friends) | 1719 | if (friend_num >= dht->num_friends) { |
1651 | return -1; | 1720 | return -1; |
1721 | } | ||
1652 | 1722 | ||
1653 | DHT_Friend *friend = &dht->friends_list[friend_num]; | 1723 | DHT_Friend *friend = &dht->friends_list[friend_num]; |
1654 | Client_data *client; | 1724 | Client_data *client; |
@@ -1672,9 +1742,12 @@ static int friend_iplist(const DHT *dht, IP_Port *ip_portlist, uint16_t friend_n | |||
1672 | ++num_ipv6s; | 1742 | ++num_ipv6s; |
1673 | } | 1743 | } |
1674 | 1744 | ||
1675 | if (id_equal(client->public_key, friend->public_key)) | 1745 | if (id_equal(client->public_key, friend->public_key)) { |
1676 | if (!is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT) || !is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT)) | 1746 | if (!is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT) |
1747 | || !is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT)) { | ||
1677 | return 0; /* direct connectivity */ | 1748 | return 0; /* direct connectivity */ |
1749 | } | ||
1750 | } | ||
1678 | } | 1751 | } |
1679 | 1752 | ||
1680 | #ifdef FRIEND_IPLIST_PAD | 1753 | #ifdef FRIEND_IPLIST_PAD |
@@ -1717,8 +1790,9 @@ int route_tofriend(const DHT *dht, const uint8_t *friend_id, const uint8_t *pack | |||
1717 | { | 1790 | { |
1718 | int num = friend_number(dht, friend_id); | 1791 | int num = friend_number(dht, friend_id); |
1719 | 1792 | ||
1720 | if (num == -1) | 1793 | if (num == -1) { |
1721 | return 0; | 1794 | return 0; |
1795 | } | ||
1722 | 1796 | ||
1723 | uint32_t i, sent = 0; | 1797 | uint32_t i, sent = 0; |
1724 | uint8_t friend_sent[MAX_FRIEND_CLIENTS] = {0}; | 1798 | uint8_t friend_sent[MAX_FRIEND_CLIENTS] = {0}; |
@@ -1726,8 +1800,9 @@ int route_tofriend(const DHT *dht, const uint8_t *friend_id, const uint8_t *pack | |||
1726 | IP_Port ip_list[MAX_FRIEND_CLIENTS]; | 1800 | IP_Port ip_list[MAX_FRIEND_CLIENTS]; |
1727 | int ip_num = friend_iplist(dht, ip_list, num); | 1801 | int ip_num = friend_iplist(dht, ip_list, num); |
1728 | 1802 | ||
1729 | if (ip_num < (MAX_FRIEND_CLIENTS / 4)) | 1803 | if (ip_num < (MAX_FRIEND_CLIENTS / 4)) { |
1730 | return 0; /* Reason for that? */ | 1804 | return 0; /* Reason for that? */ |
1805 | } | ||
1731 | 1806 | ||
1732 | DHT_Friend *friend = &dht->friends_list[num]; | 1807 | DHT_Friend *friend = &dht->friends_list[num]; |
1733 | Client_data *client; | 1808 | Client_data *client; |
@@ -1736,18 +1811,20 @@ int route_tofriend(const DHT *dht, const uint8_t *friend_id, const uint8_t *pack | |||
1736 | * is *usually* good(tm) (bites us in the behind in this case though) */ | 1811 | * is *usually* good(tm) (bites us in the behind in this case though) */ |
1737 | uint32_t a; | 1812 | uint32_t a; |
1738 | 1813 | ||
1739 | for (a = 0; a < 2; a++) | 1814 | for (a = 0; a < 2; a++) { |
1740 | for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { | 1815 | for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { |
1741 | if (friend_sent[i])/* Send one packet per client.*/ | 1816 | if (friend_sent[i]) {/* Send one packet per client.*/ |
1742 | continue; | 1817 | continue; |
1818 | } | ||
1743 | 1819 | ||
1744 | client = &friend->client_list[i]; | 1820 | client = &friend->client_list[i]; |
1745 | IPPTsPng *assoc = NULL; | 1821 | IPPTsPng *assoc = NULL; |
1746 | 1822 | ||
1747 | if (!a) | 1823 | if (!a) { |
1748 | assoc = &client->assoc4; | 1824 | assoc = &client->assoc4; |
1749 | else | 1825 | } else { |
1750 | assoc = &client->assoc6; | 1826 | assoc = &client->assoc6; |
1827 | } | ||
1751 | 1828 | ||
1752 | /* If ip is not zero and node is good. */ | 1829 | /* If ip is not zero and node is good. */ |
1753 | if (ip_isset(&assoc->ret_ip_port.ip) && | 1830 | if (ip_isset(&assoc->ret_ip_port.ip) && |
@@ -1760,6 +1837,7 @@ int route_tofriend(const DHT *dht, const uint8_t *friend_id, const uint8_t *pack | |||
1760 | } | 1837 | } |
1761 | } | 1838 | } |
1762 | } | 1839 | } |
1840 | } | ||
1763 | 1841 | ||
1764 | return sent; | 1842 | return sent; |
1765 | } | 1843 | } |
@@ -1772,8 +1850,9 @@ static int routeone_tofriend(DHT *dht, const uint8_t *friend_id, const uint8_t * | |||
1772 | { | 1850 | { |
1773 | int num = friend_number(dht, friend_id); | 1851 | int num = friend_number(dht, friend_id); |
1774 | 1852 | ||
1775 | if (num == -1) | 1853 | if (num == -1) { |
1776 | return 0; | 1854 | return 0; |
1855 | } | ||
1777 | 1856 | ||
1778 | DHT_Friend *friend = &dht->friends_list[num]; | 1857 | DHT_Friend *friend = &dht->friends_list[num]; |
1779 | Client_data *client; | 1858 | Client_data *client; |
@@ -1786,15 +1865,16 @@ static int routeone_tofriend(DHT *dht, const uint8_t *friend_id, const uint8_t * | |||
1786 | * is *usually* good(tm) (bites us in the behind in this case though) */ | 1865 | * is *usually* good(tm) (bites us in the behind in this case though) */ |
1787 | uint32_t a; | 1866 | uint32_t a; |
1788 | 1867 | ||
1789 | for (a = 0; a < 2; a++) | 1868 | for (a = 0; a < 2; a++) { |
1790 | for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { | 1869 | for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { |
1791 | client = &friend->client_list[i]; | 1870 | client = &friend->client_list[i]; |
1792 | IPPTsPng *assoc = NULL; | 1871 | IPPTsPng *assoc = NULL; |
1793 | 1872 | ||
1794 | if (!a) | 1873 | if (!a) { |
1795 | assoc = &client->assoc4; | 1874 | assoc = &client->assoc4; |
1796 | else | 1875 | } else { |
1797 | assoc = &client->assoc6; | 1876 | assoc = &client->assoc6; |
1877 | } | ||
1798 | 1878 | ||
1799 | /* If ip is not zero and node is good. */ | 1879 | /* If ip is not zero and node is good. */ |
1800 | if (ip_isset(&assoc->ret_ip_port.ip) && !is_timeout(assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { | 1880 | if (ip_isset(&assoc->ret_ip_port.ip) && !is_timeout(assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { |
@@ -1802,14 +1882,17 @@ static int routeone_tofriend(DHT *dht, const uint8_t *friend_id, const uint8_t * | |||
1802 | ++n; | 1882 | ++n; |
1803 | } | 1883 | } |
1804 | } | 1884 | } |
1885 | } | ||
1805 | 1886 | ||
1806 | if (n < 1) | 1887 | if (n < 1) { |
1807 | return 0; | 1888 | return 0; |
1889 | } | ||
1808 | 1890 | ||
1809 | int retval = sendpacket(dht->net, ip_list[rand() % n], packet, length); | 1891 | int retval = sendpacket(dht->net, ip_list[rand() % n], packet, length); |
1810 | 1892 | ||
1811 | if ((unsigned int)retval == length) | 1893 | if ((unsigned int)retval == length) { |
1812 | return 1; | 1894 | return 1; |
1895 | } | ||
1813 | 1896 | ||
1814 | return 0; | 1897 | return 0; |
1815 | } | 1898 | } |
@@ -1830,16 +1913,19 @@ static int send_NATping(DHT *dht, const uint8_t *public_key, uint64_t ping_id, u | |||
1830 | int len = create_request(dht->self_public_key, dht->self_secret_key, packet, public_key, data, | 1913 | int len = create_request(dht->self_public_key, dht->self_secret_key, packet, public_key, data, |
1831 | sizeof(uint64_t) + 1, CRYPTO_PACKET_NAT_PING); | 1914 | sizeof(uint64_t) + 1, CRYPTO_PACKET_NAT_PING); |
1832 | 1915 | ||
1833 | if (len == -1) | 1916 | if (len == -1) { |
1834 | return -1; | 1917 | return -1; |
1918 | } | ||
1835 | 1919 | ||
1836 | if (type == 0) /* If packet is request use many people to route it. */ | 1920 | if (type == 0) { /* If packet is request use many people to route it. */ |
1837 | num = route_tofriend(dht, public_key, packet, len); | 1921 | num = route_tofriend(dht, public_key, packet, len); |
1838 | else if (type == 1) /* If packet is response use only one person to route it */ | 1922 | } else if (type == 1) { /* If packet is response use only one person to route it */ |
1839 | num = routeone_tofriend(dht, public_key, packet, len); | 1923 | num = routeone_tofriend(dht, public_key, packet, len); |
1924 | } | ||
1840 | 1925 | ||
1841 | if (num == 0) | 1926 | if (num == 0) { |
1842 | return -1; | 1927 | return -1; |
1928 | } | ||
1843 | 1929 | ||
1844 | return num; | 1930 | return num; |
1845 | } | 1931 | } |
@@ -1848,8 +1934,9 @@ static int send_NATping(DHT *dht, const uint8_t *public_key, uint64_t ping_id, u | |||
1848 | static int handle_NATping(void *object, IP_Port source, const uint8_t *source_pubkey, const uint8_t *packet, | 1934 | static int handle_NATping(void *object, IP_Port source, const uint8_t *source_pubkey, const uint8_t *packet, |
1849 | uint16_t length) | 1935 | uint16_t length) |
1850 | { | 1936 | { |
1851 | if (length != sizeof(uint64_t) + 1) | 1937 | if (length != sizeof(uint64_t) + 1) { |
1852 | return 1; | 1938 | return 1; |
1939 | } | ||
1853 | 1940 | ||
1854 | DHT *dht = object; | 1941 | DHT *dht = object; |
1855 | uint64_t ping_id; | 1942 | uint64_t ping_id; |
@@ -1857,8 +1944,9 @@ static int handle_NATping(void *object, IP_Port source, const uint8_t *source_pu | |||
1857 | 1944 | ||
1858 | int friendnumber = friend_number(dht, source_pubkey); | 1945 | int friendnumber = friend_number(dht, source_pubkey); |
1859 | 1946 | ||
1860 | if (friendnumber == -1) | 1947 | if (friendnumber == -1) { |
1861 | return 1; | 1948 | return 1; |
1949 | } | ||
1862 | 1950 | ||
1863 | DHT_Friend *friend = &dht->friends_list[friendnumber]; | 1951 | DHT_Friend *friend = &dht->friends_list[friendnumber]; |
1864 | 1952 | ||
@@ -1889,20 +1977,23 @@ static IP NAT_commonip(IP_Port *ip_portlist, uint16_t len, uint16_t min_num) | |||
1889 | IP zero; | 1977 | IP zero; |
1890 | ip_reset(&zero); | 1978 | ip_reset(&zero); |
1891 | 1979 | ||
1892 | if (len > MAX_FRIEND_CLIENTS) | 1980 | if (len > MAX_FRIEND_CLIENTS) { |
1893 | return zero; | 1981 | return zero; |
1982 | } | ||
1894 | 1983 | ||
1895 | uint32_t i, j; | 1984 | uint32_t i, j; |
1896 | uint16_t numbers[MAX_FRIEND_CLIENTS] = {0}; | 1985 | uint16_t numbers[MAX_FRIEND_CLIENTS] = {0}; |
1897 | 1986 | ||
1898 | for (i = 0; i < len; ++i) { | 1987 | for (i = 0; i < len; ++i) { |
1899 | for (j = 0; j < len; ++j) { | 1988 | for (j = 0; j < len; ++j) { |
1900 | if (ip_equal(&ip_portlist[i].ip, &ip_portlist[j].ip)) | 1989 | if (ip_equal(&ip_portlist[i].ip, &ip_portlist[j].ip)) { |
1901 | ++numbers[i]; | 1990 | ++numbers[i]; |
1991 | } | ||
1902 | } | 1992 | } |
1903 | 1993 | ||
1904 | if (numbers[i] >= min_num) | 1994 | if (numbers[i] >= min_num) { |
1905 | return ip_portlist[i].ip; | 1995 | return ip_portlist[i].ip; |
1996 | } | ||
1906 | } | 1997 | } |
1907 | 1998 | ||
1908 | return zero; | 1999 | return zero; |
@@ -1931,16 +2022,18 @@ static uint16_t NAT_getports(uint16_t *portlist, IP_Port *ip_portlist, uint16_t | |||
1931 | 2022 | ||
1932 | static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports, uint16_t friend_num) | 2023 | static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports, uint16_t friend_num) |
1933 | { | 2024 | { |
1934 | if (numports > MAX_FRIEND_CLIENTS || numports == 0) | 2025 | if (numports > MAX_FRIEND_CLIENTS || numports == 0) { |
1935 | return; | 2026 | return; |
2027 | } | ||
1936 | 2028 | ||
1937 | uint32_t i; | 2029 | uint32_t i; |
1938 | uint32_t top = dht->friends_list[friend_num].nat.punching_index + MAX_PUNCHING_PORTS; | 2030 | uint32_t top = dht->friends_list[friend_num].nat.punching_index + MAX_PUNCHING_PORTS; |
1939 | uint16_t firstport = port_list[0]; | 2031 | uint16_t firstport = port_list[0]; |
1940 | 2032 | ||
1941 | for (i = 0; i < numports; ++i) { | 2033 | for (i = 0; i < numports; ++i) { |
1942 | if (firstport != port_list[i]) | 2034 | if (firstport != port_list[i]) { |
1943 | break; | 2035 | break; |
2036 | } | ||
1944 | } | 2037 | } |
1945 | 2038 | ||
1946 | if (i == numports) { /* If all ports are the same, only try that one port. */ | 2039 | if (i == numports) { /* If all ports are the same, only try that one port. */ |
@@ -1988,8 +2081,9 @@ static void do_NAT(DHT *dht) | |||
1988 | int num = friend_iplist(dht, ip_list, i); | 2081 | int num = friend_iplist(dht, ip_list, i); |
1989 | 2082 | ||
1990 | /* If already connected or friend is not online don't try to hole punch. */ | 2083 | /* If already connected or friend is not online don't try to hole punch. */ |
1991 | if (num < MAX_FRIEND_CLIENTS / 2) | 2084 | if (num < MAX_FRIEND_CLIENTS / 2) { |
1992 | continue; | 2085 | continue; |
2086 | } | ||
1993 | 2087 | ||
1994 | if (dht->friends_list[i].nat.NATping_timestamp + PUNCH_INTERVAL < temp_time) { | 2088 | if (dht->friends_list[i].nat.NATping_timestamp + PUNCH_INTERVAL < temp_time) { |
1995 | send_NATping(dht, dht->friends_list[i].public_key, dht->friends_list[i].nat.NATping_id, NAT_PING_REQUEST); | 2089 | send_NATping(dht, dht->friends_list[i].public_key, dht->friends_list[i].nat.NATping_id, NAT_PING_REQUEST); |
@@ -2002,8 +2096,9 @@ static void do_NAT(DHT *dht) | |||
2002 | 2096 | ||
2003 | IP ip = NAT_commonip(ip_list, num, MAX_FRIEND_CLIENTS / 2); | 2097 | IP ip = NAT_commonip(ip_list, num, MAX_FRIEND_CLIENTS / 2); |
2004 | 2098 | ||
2005 | if (!ip_isset(&ip)) | 2099 | if (!ip_isset(&ip)) { |
2006 | continue; | 2100 | continue; |
2101 | } | ||
2007 | 2102 | ||
2008 | uint16_t port_list[MAX_FRIEND_CLIENTS]; | 2103 | uint16_t port_list[MAX_FRIEND_CLIENTS]; |
2009 | uint16_t numports = NAT_getports(port_list, ip_list, num, ip); | 2104 | uint16_t numports = NAT_getports(port_list, ip_list, num, ip); |
@@ -2029,8 +2124,9 @@ static void do_NAT(DHT *dht) | |||
2029 | 2124 | ||
2030 | static int send_hardening_req(DHT *dht, Node_format *sendto, uint8_t type, uint8_t *contents, uint16_t length) | 2125 | static int send_hardening_req(DHT *dht, Node_format *sendto, uint8_t type, uint8_t *contents, uint16_t length) |
2031 | { | 2126 | { |
2032 | if (length > HARDREQ_DATA_SIZE - 1) | 2127 | if (length > HARDREQ_DATA_SIZE - 1) { |
2033 | return -1; | 2128 | return -1; |
2129 | } | ||
2034 | 2130 | ||
2035 | uint8_t packet[MAX_CRYPTO_REQUEST_SIZE]; | 2131 | uint8_t packet[MAX_CRYPTO_REQUEST_SIZE]; |
2036 | uint8_t data[HARDREQ_DATA_SIZE] = {0}; | 2132 | uint8_t data[HARDREQ_DATA_SIZE] = {0}; |
@@ -2039,8 +2135,9 @@ static int send_hardening_req(DHT *dht, Node_format *sendto, uint8_t type, uint8 | |||
2039 | int len = create_request(dht->self_public_key, dht->self_secret_key, packet, sendto->public_key, data, | 2135 | int len = create_request(dht->self_public_key, dht->self_secret_key, packet, sendto->public_key, data, |
2040 | sizeof(data), CRYPTO_PACKET_HARDENING); | 2136 | sizeof(data), CRYPTO_PACKET_HARDENING); |
2041 | 2137 | ||
2042 | if (len == -1) | 2138 | if (len == -1) { |
2043 | return -1; | 2139 | return -1; |
2140 | } | ||
2044 | 2141 | ||
2045 | return sendpacket(dht->net, sendto->ip_port, packet, len); | 2142 | return sendpacket(dht->net, sendto->ip_port, packet, len); |
2046 | } | 2143 | } |
@@ -2058,8 +2155,9 @@ static int send_hardening_getnode_req(DHT *dht, Node_format *dest, Node_format * | |||
2058 | static int send_hardening_getnode_res(const DHT *dht, const Node_format *sendto, const uint8_t *queried_client_id, | 2155 | static int send_hardening_getnode_res(const DHT *dht, const Node_format *sendto, const uint8_t *queried_client_id, |
2059 | const uint8_t *nodes_data, uint16_t nodes_data_length) | 2156 | const uint8_t *nodes_data, uint16_t nodes_data_length) |
2060 | { | 2157 | { |
2061 | if (!ip_isset(&sendto->ip_port.ip)) | 2158 | if (!ip_isset(&sendto->ip_port.ip)) { |
2062 | return -1; | 2159 | return -1; |
2160 | } | ||
2063 | 2161 | ||
2064 | uint8_t packet[MAX_CRYPTO_REQUEST_SIZE]; | 2162 | uint8_t packet[MAX_CRYPTO_REQUEST_SIZE]; |
2065 | uint8_t data[1 + crypto_box_PUBLICKEYBYTES + nodes_data_length]; | 2163 | uint8_t data[1 + crypto_box_PUBLICKEYBYTES + nodes_data_length]; |
@@ -2069,8 +2167,9 @@ static int send_hardening_getnode_res(const DHT *dht, const Node_format *sendto, | |||
2069 | int len = create_request(dht->self_public_key, dht->self_secret_key, packet, sendto->public_key, data, | 2167 | int len = create_request(dht->self_public_key, dht->self_secret_key, packet, sendto->public_key, data, |
2070 | sizeof(data), CRYPTO_PACKET_HARDENING); | 2168 | sizeof(data), CRYPTO_PACKET_HARDENING); |
2071 | 2169 | ||
2072 | if (len == -1) | 2170 | if (len == -1) { |
2073 | return -1; | 2171 | return -1; |
2172 | } | ||
2074 | 2173 | ||
2075 | return sendpacket(dht->net, sendto->ip_port, packet, len); | 2174 | return sendpacket(dht->net, sendto->ip_port, packet, len); |
2076 | } | 2175 | } |
@@ -2081,13 +2180,15 @@ static IPPTsPng *get_closelist_IPPTsPng(DHT *dht, const uint8_t *public_key, sa_ | |||
2081 | uint32_t i; | 2180 | uint32_t i; |
2082 | 2181 | ||
2083 | for (i = 0; i < LCLIENT_LIST; ++i) { | 2182 | for (i = 0; i < LCLIENT_LIST; ++i) { |
2084 | if (public_key_cmp(dht->close_clientlist[i].public_key, public_key) != 0) | 2183 | if (public_key_cmp(dht->close_clientlist[i].public_key, public_key) != 0) { |
2085 | continue; | 2184 | continue; |
2185 | } | ||
2086 | 2186 | ||
2087 | if (sa_family == AF_INET) | 2187 | if (sa_family == AF_INET) { |
2088 | return &dht->close_clientlist[i].assoc4; | 2188 | return &dht->close_clientlist[i].assoc4; |
2089 | else if (sa_family == AF_INET6) | 2189 | } else if (sa_family == AF_INET6) { |
2090 | return &dht->close_clientlist[i].assoc6; | 2190 | return &dht->close_clientlist[i].assoc6; |
2191 | } | ||
2091 | } | 2192 | } |
2092 | 2193 | ||
2093 | return NULL; | 2194 | return NULL; |
@@ -2136,26 +2237,30 @@ static int handle_hardening(void *object, IP_Port source, const uint8_t *source_ | |||
2136 | 2237 | ||
2137 | switch (packet[0]) { | 2238 | switch (packet[0]) { |
2138 | case CHECK_TYPE_GETNODE_REQ: { | 2239 | case CHECK_TYPE_GETNODE_REQ: { |
2139 | if (length != HARDREQ_DATA_SIZE) | 2240 | if (length != HARDREQ_DATA_SIZE) { |
2140 | return 1; | 2241 | return 1; |
2242 | } | ||
2141 | 2243 | ||
2142 | Node_format node, tocheck_node; | 2244 | Node_format node, tocheck_node; |
2143 | node.ip_port = source; | 2245 | node.ip_port = source; |
2144 | memcpy(node.public_key, source_pubkey, crypto_box_PUBLICKEYBYTES); | 2246 | memcpy(node.public_key, source_pubkey, crypto_box_PUBLICKEYBYTES); |
2145 | memcpy(&tocheck_node, packet + 1, sizeof(Node_format)); | 2247 | memcpy(&tocheck_node, packet + 1, sizeof(Node_format)); |
2146 | 2248 | ||
2147 | if (getnodes(dht, tocheck_node.ip_port, tocheck_node.public_key, packet + 1 + sizeof(Node_format), &node) == -1) | 2249 | if (getnodes(dht, tocheck_node.ip_port, tocheck_node.public_key, packet + 1 + sizeof(Node_format), &node) == -1) { |
2148 | return 1; | 2250 | return 1; |
2251 | } | ||
2149 | 2252 | ||
2150 | return 0; | 2253 | return 0; |
2151 | } | 2254 | } |
2152 | 2255 | ||
2153 | case CHECK_TYPE_GETNODE_RES: { | 2256 | case CHECK_TYPE_GETNODE_RES: { |
2154 | if (length <= crypto_box_PUBLICKEYBYTES + 1) | 2257 | if (length <= crypto_box_PUBLICKEYBYTES + 1) { |
2155 | return 1; | 2258 | return 1; |
2259 | } | ||
2156 | 2260 | ||
2157 | if (length > 1 + crypto_box_PUBLICKEYBYTES + sizeof(Node_format) * MAX_SENT_NODES) | 2261 | if (length > 1 + crypto_box_PUBLICKEYBYTES + sizeof(Node_format) * MAX_SENT_NODES) { |
2158 | return 1; | 2262 | return 1; |
2263 | } | ||
2159 | 2264 | ||
2160 | uint16_t length_nodes = length - 1 - crypto_box_PUBLICKEYBYTES; | 2265 | uint16_t length_nodes = length - 1 - crypto_box_PUBLICKEYBYTES; |
2161 | Node_format nodes[MAX_SENT_NODES]; | 2266 | Node_format nodes[MAX_SENT_NODES]; |
@@ -2163,23 +2268,28 @@ static int handle_hardening(void *object, IP_Port source, const uint8_t *source_ | |||
2163 | 2268 | ||
2164 | /* TODO: MAX_SENT_NODES nodes should be returned at all times | 2269 | /* TODO: MAX_SENT_NODES nodes should be returned at all times |
2165 | (right now we have a small network size so it could cause problems for testing and etc..) */ | 2270 | (right now we have a small network size so it could cause problems for testing and etc..) */ |
2166 | if (num_nodes <= 0) | 2271 | if (num_nodes <= 0) { |
2167 | return 1; | 2272 | return 1; |
2273 | } | ||
2168 | 2274 | ||
2169 | /* NOTE: This should work for now but should be changed to something better. */ | 2275 | /* NOTE: This should work for now but should be changed to something better. */ |
2170 | if (have_nodes_closelist(dht, nodes, num_nodes) < (uint32_t)((num_nodes + 2) / 2)) | 2276 | if (have_nodes_closelist(dht, nodes, num_nodes) < (uint32_t)((num_nodes + 2) / 2)) { |
2171 | return 1; | 2277 | return 1; |
2278 | } | ||
2172 | 2279 | ||
2173 | IPPTsPng *temp = get_closelist_IPPTsPng(dht, packet + 1, nodes[0].ip_port.ip.family); | 2280 | IPPTsPng *temp = get_closelist_IPPTsPng(dht, packet + 1, nodes[0].ip_port.ip.family); |
2174 | 2281 | ||
2175 | if (temp == NULL) | 2282 | if (temp == NULL) { |
2176 | return 1; | 2283 | return 1; |
2284 | } | ||
2177 | 2285 | ||
2178 | if (is_timeout(temp->hardening.send_nodes_timestamp, HARDENING_INTERVAL)) | 2286 | if (is_timeout(temp->hardening.send_nodes_timestamp, HARDENING_INTERVAL)) { |
2179 | return 1; | 2287 | return 1; |
2288 | } | ||
2180 | 2289 | ||
2181 | if (public_key_cmp(temp->hardening.send_nodes_pingedid, source_pubkey) != 0) | 2290 | if (public_key_cmp(temp->hardening.send_nodes_pingedid, source_pubkey) != 0) { |
2182 | return 1; | 2291 | return 1; |
2292 | } | ||
2183 | 2293 | ||
2184 | /* If Nodes look good and the request checks out */ | 2294 | /* If Nodes look good and the request checks out */ |
2185 | temp->hardening.send_nodes_ok = 1; | 2295 | temp->hardening.send_nodes_ok = 1; |
@@ -2207,10 +2317,11 @@ Node_format random_node(DHT *dht, sa_family_t sa_family) | |||
2207 | memset(nodes_list, 0, sizeof(nodes_list)); | 2317 | memset(nodes_list, 0, sizeof(nodes_list)); |
2208 | uint32_t num_nodes = get_close_nodes(dht, id, nodes_list, sa_family, 1, 0); | 2318 | uint32_t num_nodes = get_close_nodes(dht, id, nodes_list, sa_family, 1, 0); |
2209 | 2319 | ||
2210 | if (num_nodes == 0) | 2320 | if (num_nodes == 0) { |
2211 | return nodes_list[0]; | 2321 | return nodes_list[0]; |
2212 | else | 2322 | } else { |
2213 | return nodes_list[rand() % num_nodes]; | 2323 | return nodes_list[rand() % num_nodes]; |
2324 | } | ||
2214 | } | 2325 | } |
2215 | 2326 | ||
2216 | /* Put up to max_num nodes in nodes from the closelist. | 2327 | /* Put up to max_num nodes in nodes from the closelist. |
@@ -2219,8 +2330,9 @@ Node_format random_node(DHT *dht, sa_family_t sa_family) | |||
2219 | */ | 2330 | */ |
2220 | uint16_t list_nodes(Client_data *list, unsigned int length, Node_format *nodes, uint16_t max_num) | 2331 | uint16_t list_nodes(Client_data *list, unsigned int length, Node_format *nodes, uint16_t max_num) |
2221 | { | 2332 | { |
2222 | if (max_num == 0) | 2333 | if (max_num == 0) { |
2223 | return 0; | 2334 | return 0; |
2335 | } | ||
2224 | 2336 | ||
2225 | uint16_t count = 0; | 2337 | uint16_t count = 0; |
2226 | 2338 | ||
@@ -2229,14 +2341,16 @@ uint16_t list_nodes(Client_data *list, unsigned int length, Node_format *nodes, | |||
2229 | for (i = length; i != 0; --i) { | 2341 | for (i = length; i != 0; --i) { |
2230 | IPPTsPng *assoc = NULL; | 2342 | IPPTsPng *assoc = NULL; |
2231 | 2343 | ||
2232 | if (!is_timeout(list[i - 1].assoc4.timestamp, BAD_NODE_TIMEOUT)) | 2344 | if (!is_timeout(list[i - 1].assoc4.timestamp, BAD_NODE_TIMEOUT)) { |
2233 | assoc = &list[i - 1].assoc4; | 2345 | assoc = &list[i - 1].assoc4; |
2346 | } | ||
2234 | 2347 | ||
2235 | if (!is_timeout(list[i - 1].assoc6.timestamp, BAD_NODE_TIMEOUT)) { | 2348 | if (!is_timeout(list[i - 1].assoc6.timestamp, BAD_NODE_TIMEOUT)) { |
2236 | if (assoc == NULL) | 2349 | if (assoc == NULL) { |
2237 | assoc = &list[i - 1].assoc6; | 2350 | assoc = &list[i - 1].assoc6; |
2238 | else if (rand() % 2) | 2351 | } else if (rand() % 2) { |
2239 | assoc = &list[i - 1].assoc6; | 2352 | assoc = &list[i - 1].assoc6; |
2353 | } | ||
2240 | } | 2354 | } |
2241 | 2355 | ||
2242 | if (assoc != NULL) { | 2356 | if (assoc != NULL) { |
@@ -2244,8 +2358,9 @@ uint16_t list_nodes(Client_data *list, unsigned int length, Node_format *nodes, | |||
2244 | nodes[count].ip_port = assoc->ip_port; | 2358 | nodes[count].ip_port = assoc->ip_port; |
2245 | ++count; | 2359 | ++count; |
2246 | 2360 | ||
2247 | if (count >= max_num) | 2361 | if (count >= max_num) { |
2248 | return count; | 2362 | return count; |
2363 | } | ||
2249 | } | 2364 | } |
2250 | } | 2365 | } |
2251 | 2366 | ||
@@ -2258,8 +2373,9 @@ uint16_t list_nodes(Client_data *list, unsigned int length, Node_format *nodes, | |||
2258 | */ | 2373 | */ |
2259 | uint16_t randfriends_nodes(DHT *dht, Node_format *nodes, uint16_t max_num) | 2374 | uint16_t randfriends_nodes(DHT *dht, Node_format *nodes, uint16_t max_num) |
2260 | { | 2375 | { |
2261 | if (max_num == 0) | 2376 | if (max_num == 0) { |
2262 | return 0; | 2377 | return 0; |
2378 | } | ||
2263 | 2379 | ||
2264 | uint16_t count = 0; | 2380 | uint16_t count = 0; |
2265 | unsigned int i, r = rand(); | 2381 | unsigned int i, r = rand(); |
@@ -2268,8 +2384,9 @@ uint16_t randfriends_nodes(DHT *dht, Node_format *nodes, uint16_t max_num) | |||
2268 | count += list_nodes(dht->friends_list[(i + r) % DHT_FAKE_FRIEND_NUMBER].client_list, MAX_FRIEND_CLIENTS, nodes + count, | 2384 | count += list_nodes(dht->friends_list[(i + r) % DHT_FAKE_FRIEND_NUMBER].client_list, MAX_FRIEND_CLIENTS, nodes + count, |
2269 | max_num - count); | 2385 | max_num - count); |
2270 | 2386 | ||
2271 | if (count >= max_num) | 2387 | if (count >= max_num) { |
2272 | break; | 2388 | break; |
2389 | } | ||
2273 | } | 2390 | } |
2274 | 2391 | ||
2275 | return count; | 2392 | return count; |
@@ -2301,18 +2418,21 @@ void do_hardening(DHT *dht) | |||
2301 | sa_family = AF_INET6; | 2418 | sa_family = AF_INET6; |
2302 | } | 2419 | } |
2303 | 2420 | ||
2304 | if (is_timeout(cur_iptspng->timestamp, BAD_NODE_TIMEOUT)) | 2421 | if (is_timeout(cur_iptspng->timestamp, BAD_NODE_TIMEOUT)) { |
2305 | continue; | 2422 | continue; |
2423 | } | ||
2306 | 2424 | ||
2307 | if (cur_iptspng->hardening.send_nodes_ok == 0) { | 2425 | if (cur_iptspng->hardening.send_nodes_ok == 0) { |
2308 | if (is_timeout(cur_iptspng->hardening.send_nodes_timestamp, HARDENING_INTERVAL)) { | 2426 | if (is_timeout(cur_iptspng->hardening.send_nodes_timestamp, HARDENING_INTERVAL)) { |
2309 | Node_format rand_node = random_node(dht, sa_family); | 2427 | Node_format rand_node = random_node(dht, sa_family); |
2310 | 2428 | ||
2311 | if (!ipport_isset(&rand_node.ip_port)) | 2429 | if (!ipport_isset(&rand_node.ip_port)) { |
2312 | continue; | 2430 | continue; |
2431 | } | ||
2313 | 2432 | ||
2314 | if (id_equal(public_key, rand_node.public_key)) | 2433 | if (id_equal(public_key, rand_node.public_key)) { |
2315 | continue; | 2434 | continue; |
2435 | } | ||
2316 | 2436 | ||
2317 | Node_format to_test; | 2437 | Node_format to_test; |
2318 | to_test.ip_port = cur_iptspng->ip_port; | 2438 | to_test.ip_port = cur_iptspng->ip_port; |
@@ -2348,8 +2468,9 @@ static int cryptopacket_handle(void *object, IP_Port source, const uint8_t *pack | |||
2348 | 2468 | ||
2349 | if (packet[0] == NET_PACKET_CRYPTO) { | 2469 | if (packet[0] == NET_PACKET_CRYPTO) { |
2350 | if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + crypto_box_MACBYTES || | 2470 | if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + crypto_box_MACBYTES || |
2351 | length > MAX_CRYPTO_REQUEST_SIZE + crypto_box_MACBYTES) | 2471 | length > MAX_CRYPTO_REQUEST_SIZE + crypto_box_MACBYTES) { |
2352 | return 1; | 2472 | return 1; |
2473 | } | ||
2353 | 2474 | ||
2354 | if (public_key_cmp(packet + 1, dht->self_public_key) == 0) { // Check if request is for us. | 2475 | if (public_key_cmp(packet + 1, dht->self_public_key) == 0) { // Check if request is for us. |
2355 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | 2476 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; |
@@ -2357,10 +2478,13 @@ static int cryptopacket_handle(void *object, IP_Port source, const uint8_t *pack | |||
2357 | uint8_t number; | 2478 | uint8_t number; |
2358 | int len = handle_request(dht->self_public_key, dht->self_secret_key, public_key, data, &number, packet, length); | 2479 | int len = handle_request(dht->self_public_key, dht->self_secret_key, public_key, data, &number, packet, length); |
2359 | 2480 | ||
2360 | if (len == -1 || len == 0) | 2481 | if (len == -1 || len == 0) { |
2361 | return 1; | 2482 | return 1; |
2483 | } | ||
2362 | 2484 | ||
2363 | if (!dht->cryptopackethandlers[number].function) return 1; | 2485 | if (!dht->cryptopackethandlers[number].function) { |
2486 | return 1; | ||
2487 | } | ||
2364 | 2488 | ||
2365 | return dht->cryptopackethandlers[number].function(dht->cryptopackethandlers[number].object, source, public_key, | 2489 | return dht->cryptopackethandlers[number].function(dht->cryptopackethandlers[number].object, source, public_key, |
2366 | data, len); | 2490 | data, len); |
@@ -2368,8 +2492,9 @@ static int cryptopacket_handle(void *object, IP_Port source, const uint8_t *pack | |||
2368 | } else { /* If request is not for us, try routing it. */ | 2492 | } else { /* If request is not for us, try routing it. */ |
2369 | int retval = route_packet(dht, packet + 1, packet, length); | 2493 | int retval = route_packet(dht, packet + 1, packet, length); |
2370 | 2494 | ||
2371 | if ((unsigned int)retval == length) | 2495 | if ((unsigned int)retval == length) { |
2372 | return 0; | 2496 | return 0; |
2497 | } | ||
2373 | } | 2498 | } |
2374 | } | 2499 | } |
2375 | 2500 | ||
@@ -2383,13 +2508,15 @@ DHT *new_DHT(Logger *log, Networking_Core *net) | |||
2383 | /* init time */ | 2508 | /* init time */ |
2384 | unix_time_update(); | 2509 | unix_time_update(); |
2385 | 2510 | ||
2386 | if (net == NULL) | 2511 | if (net == NULL) { |
2387 | return NULL; | 2512 | return NULL; |
2513 | } | ||
2388 | 2514 | ||
2389 | DHT *dht = calloc(1, sizeof(DHT)); | 2515 | DHT *dht = calloc(1, sizeof(DHT)); |
2390 | 2516 | ||
2391 | if (dht == NULL) | 2517 | if (dht == NULL) { |
2392 | return NULL; | 2518 | return NULL; |
2519 | } | ||
2393 | 2520 | ||
2394 | dht->log = log; | 2521 | dht->log = log; |
2395 | dht->net = net; | 2522 | dht->net = net; |
@@ -2571,11 +2698,13 @@ void DHT_save(DHT *dht, uint8_t *data) | |||
2571 | /* Start sending packets after DHT loaded_friends_list and loaded_clients_list are set */ | 2698 | /* Start sending packets after DHT loaded_friends_list and loaded_clients_list are set */ |
2572 | int DHT_connect_after_load(DHT *dht) | 2699 | int DHT_connect_after_load(DHT *dht) |
2573 | { | 2700 | { |
2574 | if (dht == NULL) | 2701 | if (dht == NULL) { |
2575 | return -1; | 2702 | return -1; |
2703 | } | ||
2576 | 2704 | ||
2577 | if (!dht->loaded_nodes_list) | 2705 | if (!dht->loaded_nodes_list) { |
2578 | return -1; | 2706 | return -1; |
2707 | } | ||
2579 | 2708 | ||
2580 | /* DHT is connected, stop. */ | 2709 | /* DHT is connected, stop. */ |
2581 | if (DHT_non_lan_connected(dht)) { | 2710 | if (DHT_non_lan_connected(dht)) { |
@@ -2602,8 +2731,9 @@ static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t le | |||
2602 | 2731 | ||
2603 | switch (type) { | 2732 | switch (type) { |
2604 | case DHT_STATE_TYPE_NODES: | 2733 | case DHT_STATE_TYPE_NODES: |
2605 | if (length == 0) | 2734 | if (length == 0) { |
2606 | break; | 2735 | break; |
2736 | } | ||
2607 | 2737 | ||
2608 | { | 2738 | { |
2609 | free(dht->loaded_nodes_list); | 2739 | free(dht->loaded_nodes_list); |
@@ -2647,9 +2777,10 @@ int DHT_load(DHT *dht, const uint8_t *data, uint32_t length) | |||
2647 | uint32_t data32; | 2777 | uint32_t data32; |
2648 | lendian_to_host32(&data32, data); | 2778 | lendian_to_host32(&data32, data); |
2649 | 2779 | ||
2650 | if (data32 == DHT_STATE_COOKIE_GLOBAL) | 2780 | if (data32 == DHT_STATE_COOKIE_GLOBAL) { |
2651 | return load_state(dht_load_state_callback, dht, data + cookie_len, | 2781 | return load_state(dht_load_state_callback, dht, data + cookie_len, |
2652 | length - cookie_len, DHT_STATE_COOKIE_TYPE); | 2782 | length - cookie_len, DHT_STATE_COOKIE_TYPE); |
2783 | } | ||
2653 | } | 2784 | } |
2654 | 2785 | ||
2655 | return -1; | 2786 | return -1; |
@@ -2667,8 +2798,9 @@ int DHT_isconnected(const DHT *dht) | |||
2667 | const Client_data *client = &dht->close_clientlist[i]; | 2798 | const Client_data *client = &dht->close_clientlist[i]; |
2668 | 2799 | ||
2669 | if (!is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) || | 2800 | if (!is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) || |
2670 | !is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT)) | 2801 | !is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT)) { |
2671 | return 1; | 2802 | return 1; |
2803 | } | ||
2672 | } | 2804 | } |
2673 | 2805 | ||
2674 | return 0; | 2806 | return 0; |
@@ -2685,11 +2817,13 @@ int DHT_non_lan_connected(const DHT *dht) | |||
2685 | for (i = 0; i < LCLIENT_LIST; ++i) { | 2817 | for (i = 0; i < LCLIENT_LIST; ++i) { |
2686 | const Client_data *client = &dht->close_clientlist[i]; | 2818 | const Client_data *client = &dht->close_clientlist[i]; |
2687 | 2819 | ||
2688 | if (!is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) && LAN_ip(client->assoc4.ip_port.ip) == -1) | 2820 | if (!is_timeout(client->assoc4.timestamp, BAD_NODE_TIMEOUT) && LAN_ip(client->assoc4.ip_port.ip) == -1) { |
2689 | return 1; | 2821 | return 1; |
2822 | } | ||
2690 | 2823 | ||
2691 | if (!is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT) && LAN_ip(client->assoc6.ip_port.ip) == -1) | 2824 | if (!is_timeout(client->assoc6.timestamp, BAD_NODE_TIMEOUT) && LAN_ip(client->assoc6.ip_port.ip) == -1) { |
2692 | return 1; | 2825 | return 1; |
2826 | } | ||
2693 | 2827 | ||
2694 | } | 2828 | } |
2695 | 2829 | ||