diff options
author | iphydf <iphydf@users.noreply.github.com> | 2018-07-09 15:17:00 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2018-07-12 09:32:46 +0000 |
commit | cbda01021c561bd061cb03a1c1bab58199ac2307 (patch) | |
tree | 537e8397e84d3ccd4e16753101ded1bf676392b2 /toxcore/DHT.c | |
parent | 37f8f566d53a3e2603467aa0bbe1e29581ddd561 (diff) |
Fix style in DHT.c.
* Removed `ARRAY_SIZE` and use NULL markers for end of array, instead.
The alternative is + size, but for these arrays, NULL markers made
sense, since they are arrays of non-null pointers.
* Made `INDEX_OF_PK` a self-contained macro, not dependent upon the
naming inside its call site. This is a minor change but makes the code
more local and reviews easier.
* No nested structs.
* Use only named function types ending in `_cb` for callbacks.
* Replaced two macros with functions.
* `++i` instead of `i++`.
* struct member names start with lowercase letters.
* It takes a bit of work to support `/**/` comments in preprocessor
macros, so I've decided not to support these. If a macro is complex
enough to need comments inside it, it's too complex. `//` comments are
allowed at the end of macro definitions.
* Callback typedefs must name their parameters.
Diffstat (limited to 'toxcore/DHT.c')
-rw-r--r-- | toxcore/DHT.c | 159 |
1 files changed, 87 insertions, 72 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 2572b28a..3fd3829e 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -61,7 +61,11 @@ | |||
61 | /* Number of get node requests to send to quickly find close nodes. */ | 61 | /* Number of get node requests to send to quickly find close nodes. */ |
62 | #define MAX_BOOTSTRAP_TIMES 5 | 62 | #define MAX_BOOTSTRAP_TIMES 5 |
63 | 63 | ||
64 | #define ARRAY_SIZE(ARR) (sizeof (ARR) / sizeof (ARR)[0]) | 64 | typedef struct DHT_Friend_Callback { |
65 | dht_ip_cb *ip_callback; | ||
66 | void *data; | ||
67 | int32_t number; | ||
68 | } DHT_Friend_Callback; | ||
65 | 69 | ||
66 | struct DHT_Friend { | 70 | struct DHT_Friend { |
67 | uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; | 71 | uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; |
@@ -76,16 +80,17 @@ struct DHT_Friend { | |||
76 | NAT nat; | 80 | NAT nat; |
77 | 81 | ||
78 | uint16_t lock_count; | 82 | uint16_t lock_count; |
79 | struct { | 83 | DHT_Friend_Callback callbacks[DHT_FRIEND_MAX_LOCKS]; |
80 | void (*ip_callback)(void *, int32_t, IP_Port); | ||
81 | void *data; | ||
82 | int32_t number; | ||
83 | } callbacks[DHT_FRIEND_MAX_LOCKS]; | ||
84 | 84 | ||
85 | Node_format to_bootstrap[MAX_SENT_NODES]; | 85 | Node_format to_bootstrap[MAX_SENT_NODES]; |
86 | unsigned int num_to_bootstrap; | 86 | unsigned int num_to_bootstrap; |
87 | }; | 87 | }; |
88 | 88 | ||
89 | typedef struct Cryptopacket_Handler { | ||
90 | cryptopacket_handler_cb *function; | ||
91 | void *object; | ||
92 | } Cryptopacket_Handler; | ||
93 | |||
89 | struct DHT { | 94 | struct DHT { |
90 | const Logger *log; | 95 | const Logger *log; |
91 | Networking_Core *net; | 96 | Networking_Core *net; |
@@ -115,7 +120,7 @@ struct DHT { | |||
115 | Ping_Array *dht_harden_ping_array; | 120 | Ping_Array *dht_harden_ping_array; |
116 | uint64_t last_run; | 121 | uint64_t last_run; |
117 | 122 | ||
118 | Cryptopacket_Handles cryptopackethandlers[256]; | 123 | Cryptopacket_Handler cryptopackethandlers[256]; |
119 | 124 | ||
120 | Node_format to_bootstrap[MAX_CLOSE_TO_BOOTSTRAP_NODES]; | 125 | Node_format to_bootstrap[MAX_CLOSE_TO_BOOTSTRAP_NODES]; |
121 | unsigned int num_to_bootstrap; | 126 | unsigned int num_to_bootstrap; |
@@ -618,28 +623,30 @@ int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed | |||
618 | * | 623 | * |
619 | * return index or UINT32_MAX if not found. | 624 | * return index or UINT32_MAX if not found. |
620 | */ | 625 | */ |
621 | #define INDEX_OF_PK \ | 626 | #define INDEX_OF_PK(array, size, pk) \ |
622 | for (uint32_t i = 0; i < size; i++) { \ | 627 | do { \ |
623 | if (id_equal(array[i].public_key, pk)) { \ | 628 | for (uint32_t i = 0; i < size; ++i) { \ |
624 | return i; \ | 629 | if (id_equal(array[i].public_key, pk)) { \ |
625 | } \ | 630 | return i; \ |
626 | } \ | 631 | } \ |
627 | \ | 632 | } \ |
628 | return UINT32_MAX; | 633 | \ |
634 | return UINT32_MAX; \ | ||
635 | } while (0) | ||
629 | 636 | ||
630 | static uint32_t index_of_client_pk(const Client_data *array, uint32_t size, const uint8_t *pk) | 637 | static uint32_t index_of_client_pk(const Client_data *array, uint32_t size, const uint8_t *pk) |
631 | { | 638 | { |
632 | INDEX_OF_PK | 639 | INDEX_OF_PK(array, size, pk); |
633 | } | 640 | } |
634 | 641 | ||
635 | static uint32_t index_of_friend_pk(const DHT_Friend *array, uint32_t size, const uint8_t *pk) | 642 | static uint32_t index_of_friend_pk(const DHT_Friend *array, uint32_t size, const uint8_t *pk) |
636 | { | 643 | { |
637 | INDEX_OF_PK | 644 | INDEX_OF_PK(array, size, pk); |
638 | } | 645 | } |
639 | 646 | ||
640 | static uint32_t index_of_node_pk(const Node_format *array, uint32_t size, const uint8_t *pk) | 647 | static uint32_t index_of_node_pk(const Node_format *array, uint32_t size, const uint8_t *pk) |
641 | { | 648 | { |
642 | INDEX_OF_PK | 649 | INDEX_OF_PK(array, size, pk); |
643 | } | 650 | } |
644 | 651 | ||
645 | /* Find index of Client_data with ip_port equal to param ip_port. | 652 | /* Find index of Client_data with ip_port equal to param ip_port. |
@@ -796,7 +803,7 @@ static void get_close_nodes_inner(const uint8_t *public_key, Node_format *nodes_ | |||
796 | 803 | ||
797 | uint32_t num_nodes = *num_nodes_ptr; | 804 | uint32_t num_nodes = *num_nodes_ptr; |
798 | 805 | ||
799 | for (uint32_t i = 0; i < client_list_length; i++) { | 806 | for (uint32_t i = 0; i < client_list_length; ++i) { |
800 | const Client_data *const client = &client_list[i]; | 807 | const Client_data *const client = &client_list[i]; |
801 | 808 | ||
802 | /* node already in list? */ | 809 | /* node already in list? */ |
@@ -834,7 +841,7 @@ static void get_close_nodes_inner(const uint8_t *public_key, Node_format *nodes_ | |||
834 | if (num_nodes < MAX_SENT_NODES) { | 841 | if (num_nodes < MAX_SENT_NODES) { |
835 | memcpy(nodes_list[num_nodes].public_key, client->public_key, CRYPTO_PUBLIC_KEY_SIZE); | 842 | memcpy(nodes_list[num_nodes].public_key, client->public_key, CRYPTO_PUBLIC_KEY_SIZE); |
836 | nodes_list[num_nodes].ip_port = ipptp->ip_port; | 843 | nodes_list[num_nodes].ip_port = ipptp->ip_port; |
837 | num_nodes++; | 844 | ++num_nodes; |
838 | } else { | 845 | } else { |
839 | add_to_list(nodes_list, MAX_SENT_NODES, client->public_key, ipptp->ip_port, public_key); | 846 | add_to_list(nodes_list, MAX_SENT_NODES, client->public_key, ipptp->ip_port, public_key); |
840 | } | 847 | } |
@@ -885,11 +892,21 @@ int get_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *node | |||
885 | return get_somewhat_close_nodes(dht, public_key, nodes_list, sa_family, is_LAN, want_good); | 892 | return get_somewhat_close_nodes(dht, public_key, nodes_list, sa_family, is_LAN, want_good); |
886 | } | 893 | } |
887 | 894 | ||
888 | typedef struct { | 895 | typedef struct DHT_Cmp_data { |
889 | const uint8_t *base_public_key; | 896 | const uint8_t *base_public_key; |
890 | Client_data entry; | 897 | Client_data entry; |
891 | } DHT_Cmp_data; | 898 | } DHT_Cmp_data; |
892 | 899 | ||
900 | static bool assoc_timeout(const IPPTsPng *assoc) | ||
901 | { | ||
902 | return is_timeout(assoc->timestamp, BAD_NODE_TIMEOUT); | ||
903 | } | ||
904 | |||
905 | static bool incorrect_hardening(const IPPTsPng *assoc) | ||
906 | { | ||
907 | return hardening_correct(&assoc->hardening) != HARDENING_ALL_OK; | ||
908 | } | ||
909 | |||
893 | static int cmp_dht_entry(const void *a, const void *b) | 910 | static int cmp_dht_entry(const void *a, const void *b) |
894 | { | 911 | { |
895 | DHT_Cmp_data cmp1, cmp2; | 912 | DHT_Cmp_data cmp1, cmp2; |
@@ -899,10 +916,8 @@ static int cmp_dht_entry(const void *a, const void *b) | |||
899 | const Client_data entry2 = cmp2.entry; | 916 | const Client_data entry2 = cmp2.entry; |
900 | const uint8_t *cmp_public_key = cmp1.base_public_key; | 917 | const uint8_t *cmp_public_key = cmp1.base_public_key; |
901 | 918 | ||
902 | #define ASSOC_TIMEOUT(assoc) is_timeout((assoc).timestamp, BAD_NODE_TIMEOUT) | 919 | bool t1 = assoc_timeout(&entry1.assoc4) && assoc_timeout(&entry1.assoc6); |
903 | 920 | bool t2 = assoc_timeout(&entry2.assoc4) && assoc_timeout(&entry2.assoc6); | |
904 | bool t1 = ASSOC_TIMEOUT(entry1.assoc4) && ASSOC_TIMEOUT(entry1.assoc6); | ||
905 | bool t2 = ASSOC_TIMEOUT(entry2.assoc4) && ASSOC_TIMEOUT(entry2.assoc6); | ||
906 | 921 | ||
907 | if (t1 && t2) { | 922 | if (t1 && t2) { |
908 | return 0; | 923 | return 0; |
@@ -916,10 +931,8 @@ static int cmp_dht_entry(const void *a, const void *b) | |||
916 | return 1; | 931 | return 1; |
917 | } | 932 | } |
918 | 933 | ||
919 | #define INCORRECT_HARDENING(assoc) hardening_correct(&(assoc).hardening) != HARDENING_ALL_OK | 934 | t1 = incorrect_hardening(&entry1.assoc4) && incorrect_hardening(&entry1.assoc6); |
920 | 935 | t2 = incorrect_hardening(&entry2.assoc4) && incorrect_hardening(&entry2.assoc6); | |
921 | t1 = INCORRECT_HARDENING(entry1.assoc4) && INCORRECT_HARDENING(entry1.assoc6); | ||
922 | t2 = INCORRECT_HARDENING(entry2.assoc4) && INCORRECT_HARDENING(entry2.assoc6); | ||
923 | 936 | ||
924 | if (t1 && !t2) { | 937 | if (t1 && !t2) { |
925 | return -1; | 938 | return -1; |
@@ -960,14 +973,14 @@ static void sort_client_list(Client_data *list, unsigned int length, const uint8 | |||
960 | // comparison function can use it as the base of comparison. | 973 | // comparison function can use it as the base of comparison. |
961 | VLA(DHT_Cmp_data, cmp_list, length); | 974 | VLA(DHT_Cmp_data, cmp_list, length); |
962 | 975 | ||
963 | for (uint32_t i = 0; i < length; i++) { | 976 | for (uint32_t i = 0; i < length; ++i) { |
964 | cmp_list[i].base_public_key = comp_public_key; | 977 | cmp_list[i].base_public_key = comp_public_key; |
965 | cmp_list[i].entry = list[i]; | 978 | cmp_list[i].entry = list[i]; |
966 | } | 979 | } |
967 | 980 | ||
968 | qsort(cmp_list, length, sizeof(DHT_Cmp_data), cmp_dht_entry); | 981 | qsort(cmp_list, length, sizeof(DHT_Cmp_data), cmp_dht_entry); |
969 | 982 | ||
970 | for (uint32_t i = 0; i < length; i++) { | 983 | for (uint32_t i = 0; i < length; ++i) { |
971 | list[i] = cmp_list[i].entry; | 984 | list[i] = cmp_list[i].entry; |
972 | } | 985 | } |
973 | } | 986 | } |
@@ -1192,7 +1205,7 @@ uint32_t addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *public_key) | |||
1192 | 1205 | ||
1193 | /* add_to_close should be called only if !in_list (don't extract to variable) */ | 1206 | /* add_to_close should be called only if !in_list (don't extract to variable) */ |
1194 | if (in_close_list || add_to_close(dht, public_key, ip_port, 0)) { | 1207 | if (in_close_list || add_to_close(dht, public_key, ip_port, 0)) { |
1195 | used++; | 1208 | ++used; |
1196 | } | 1209 | } |
1197 | 1210 | ||
1198 | DHT_Friend *friend_foundip = nullptr; | 1211 | DHT_Friend *friend_foundip = nullptr; |
@@ -1210,7 +1223,7 @@ uint32_t addto_lists(DHT *dht, IP_Port ip_port, const uint8_t *public_key) | |||
1210 | friend_foundip = dht_friend; | 1223 | friend_foundip = dht_friend; |
1211 | } | 1224 | } |
1212 | 1225 | ||
1213 | used++; | 1226 | ++used; |
1214 | } | 1227 | } |
1215 | } | 1228 | } |
1216 | 1229 | ||
@@ -1527,7 +1540,7 @@ static int handle_sendnodes_ipv6(void *object, IP_Port source, const uint8_t *pa | |||
1527 | return 0; | 1540 | return 0; |
1528 | } | 1541 | } |
1529 | 1542 | ||
1530 | for (uint32_t i = 0; i < num_nodes; i++) { | 1543 | for (uint32_t i = 0; i < num_nodes; ++i) { |
1531 | if (ipport_isset(&plain_nodes[i].ip_port)) { | 1544 | if (ipport_isset(&plain_nodes[i].ip_port)) { |
1532 | ping_node_from_getnodes_ok(dht, plain_nodes[i].public_key, plain_nodes[i].ip_port); | 1545 | ping_node_from_getnodes_ok(dht, plain_nodes[i].public_key, plain_nodes[i].ip_port); |
1533 | returnedip_ports(dht, plain_nodes[i].ip_port, plain_nodes[i].public_key, packet + 1); | 1546 | returnedip_ports(dht, plain_nodes[i].ip_port, plain_nodes[i].public_key, packet + 1); |
@@ -1540,7 +1553,7 @@ static int handle_sendnodes_ipv6(void *object, IP_Port source, const uint8_t *pa | |||
1540 | /*----------------------------------------------------------------------------------*/ | 1553 | /*----------------------------------------------------------------------------------*/ |
1541 | /*------------------------END of packet handling functions--------------------------*/ | 1554 | /*------------------------END of packet handling functions--------------------------*/ |
1542 | 1555 | ||
1543 | int dht_addfriend(DHT *dht, const uint8_t *public_key, void (*ip_callback)(void *data, int32_t number, IP_Port), | 1556 | int dht_addfriend(DHT *dht, const uint8_t *public_key, dht_ip_cb *ip_callback, |
1544 | void *data, int32_t number, uint16_t *lock_count) | 1557 | void *data, int32_t number, uint16_t *lock_count) |
1545 | { | 1558 | { |
1546 | const uint32_t friend_num = index_of_friend_pk(dht->friends_list, dht->num_friends, public_key); | 1559 | const uint32_t friend_num = index_of_friend_pk(dht->friends_list, dht->num_friends, public_key); |
@@ -1578,7 +1591,7 @@ int dht_addfriend(DHT *dht, const uint8_t *public_key, void (*ip_callback)(void | |||
1578 | memset(dht_friend, 0, sizeof(DHT_Friend)); | 1591 | memset(dht_friend, 0, sizeof(DHT_Friend)); |
1579 | memcpy(dht_friend->public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); | 1592 | memcpy(dht_friend->public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); |
1580 | 1593 | ||
1581 | dht_friend->nat.NATping_id = random_u64(); | 1594 | dht_friend->nat.nat_ping_id = random_u64(); |
1582 | ++dht->num_friends; | 1595 | ++dht->num_friends; |
1583 | 1596 | ||
1584 | lock_num = dht_friend->lock_count; | 1597 | lock_num = dht_friend->lock_count; |
@@ -1660,10 +1673,10 @@ int dht_getfriendip(const DHT *dht, const uint8_t *public_key, IP_Port *ip_port) | |||
1660 | } | 1673 | } |
1661 | 1674 | ||
1662 | const Client_data *const client = &frnd->client_list[client_index]; | 1675 | const Client_data *const client = &frnd->client_list[client_index]; |
1663 | const IPPTsPng *const assocs[] = { &client->assoc6, &client->assoc4 }; | 1676 | const IPPTsPng *const assocs[] = { &client->assoc6, &client->assoc4, nullptr }; |
1664 | 1677 | ||
1665 | for (size_t i = 0; i < ARRAY_SIZE(assocs); i++) { | 1678 | for (const IPPTsPng * const *it = assocs; *it; ++it) { |
1666 | const IPPTsPng *const assoc = assocs[i]; | 1679 | const IPPTsPng *const assoc = *it; |
1667 | 1680 | ||
1668 | if (!is_timeout(assoc->timestamp, BAD_NODE_TIMEOUT)) { | 1681 | if (!is_timeout(assoc->timestamp, BAD_NODE_TIMEOUT)) { |
1669 | *ip_port = assoc->ip_port; | 1682 | *ip_port = assoc->ip_port; |
@@ -1687,18 +1700,20 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co | |||
1687 | unsigned int sort = 0; | 1700 | unsigned int sort = 0; |
1688 | bool sort_ok = false; | 1701 | bool sort_ok = false; |
1689 | 1702 | ||
1690 | for (uint32_t i = 0; i < list_count; i++) { | 1703 | for (uint32_t i = 0; i < list_count; ++i) { |
1691 | /* If node is not dead. */ | 1704 | /* If node is not dead. */ |
1692 | Client_data *client = &list[i]; | 1705 | Client_data *client = &list[i]; |
1693 | 1706 | ||
1694 | IPPTsPng *assocs[] = { &client->assoc6, &client->assoc4 }; | 1707 | IPPTsPng *const assocs[] = { &client->assoc6, &client->assoc4, nullptr }; |
1708 | |||
1709 | uint32_t j = 0; | ||
1695 | 1710 | ||
1696 | for (size_t j = 0; j < ARRAY_SIZE(assocs); j++) { | 1711 | for (IPPTsPng * const *it = assocs; *it; ++it, ++j) { |
1697 | IPPTsPng *assoc = assocs[j]; | 1712 | IPPTsPng *const assoc = *it; |
1698 | 1713 | ||
1699 | if (!is_timeout(assoc->timestamp, KILL_NODE_TIMEOUT)) { | 1714 | if (!is_timeout(assoc->timestamp, KILL_NODE_TIMEOUT)) { |
1700 | sort = 0; | 1715 | sort = 0; |
1701 | not_kill++; | 1716 | ++not_kill; |
1702 | 1717 | ||
1703 | if (is_timeout(assoc->last_pinged, PING_INTERVAL)) { | 1718 | if (is_timeout(assoc->last_pinged, PING_INTERVAL)) { |
1704 | getnodes(dht, assoc->ip_port, client->public_key, public_key, nullptr); | 1719 | getnodes(dht, assoc->ip_port, client->public_key, public_key, nullptr); |
@@ -1790,13 +1805,13 @@ static void do_Close(DHT *dht) | |||
1790 | * KILL_NODE_TIMEOUT, so we at least keep trying pings */ | 1805 | * KILL_NODE_TIMEOUT, so we at least keep trying pings */ |
1791 | const uint64_t badonly = unix_time() - BAD_NODE_TIMEOUT; | 1806 | const uint64_t badonly = unix_time() - BAD_NODE_TIMEOUT; |
1792 | 1807 | ||
1793 | for (size_t i = 0; i < LCLIENT_LIST; i++) { | 1808 | for (size_t i = 0; i < LCLIENT_LIST; ++i) { |
1794 | Client_data *const client = &dht->close_clientlist[i]; | 1809 | Client_data *const client = &dht->close_clientlist[i]; |
1795 | 1810 | ||
1796 | IPPTsPng *const assocs[] = { &client->assoc6, &client->assoc4 }; | 1811 | IPPTsPng *const assocs[] = { &client->assoc6, &client->assoc4, nullptr }; |
1797 | 1812 | ||
1798 | for (size_t j = 0; j < ARRAY_SIZE(assocs); j++) { | 1813 | for (IPPTsPng * const *it = assocs; *it; ++it) { |
1799 | IPPTsPng *const assoc = assocs[j]; | 1814 | IPPTsPng *const assoc = *it; |
1800 | 1815 | ||
1801 | if (assoc->timestamp) { | 1816 | if (assoc->timestamp) { |
1802 | assoc->timestamp = badonly; | 1817 | assoc->timestamp = badonly; |
@@ -1853,10 +1868,10 @@ int route_packet(const DHT *dht, const uint8_t *public_key, const uint8_t *packe | |||
1853 | for (uint32_t i = 0; i < LCLIENT_LIST; ++i) { | 1868 | for (uint32_t i = 0; i < LCLIENT_LIST; ++i) { |
1854 | if (id_equal(public_key, dht->close_clientlist[i].public_key)) { | 1869 | if (id_equal(public_key, dht->close_clientlist[i].public_key)) { |
1855 | const Client_data *const client = &dht->close_clientlist[i]; | 1870 | const Client_data *const client = &dht->close_clientlist[i]; |
1856 | const IPPTsPng *const assocs[] = { &client->assoc6, &client->assoc4 }; | 1871 | const IPPTsPng *const assocs[] = { &client->assoc6, &client->assoc4, nullptr }; |
1857 | 1872 | ||
1858 | for (size_t j = 0; j < ARRAY_SIZE(assocs); j++) { | 1873 | for (const IPPTsPng * const *it = assocs; *it; ++it) { |
1859 | const IPPTsPng *const assoc = assocs[j]; | 1874 | const IPPTsPng *const assoc = *it; |
1860 | 1875 | ||
1861 | if (ip_isset(&assoc->ip_port.ip)) { | 1876 | if (ip_isset(&assoc->ip_port.ip)) { |
1862 | return sendpacket(dht->net, assoc->ip_port, packet, length); | 1877 | return sendpacket(dht->net, assoc->ip_port, packet, length); |
@@ -1978,10 +1993,10 @@ int route_tofriend(const DHT *dht, const uint8_t *friend_id, const uint8_t *pack | |||
1978 | } | 1993 | } |
1979 | 1994 | ||
1980 | const Client_data *const client = &dht_friend->client_list[i]; | 1995 | const Client_data *const client = &dht_friend->client_list[i]; |
1981 | const IPPTsPng *const assocs[] = { &client->assoc4, &client->assoc6 }; | 1996 | const IPPTsPng *const assocs[] = { &client->assoc4, &client->assoc6, nullptr }; |
1982 | 1997 | ||
1983 | for (size_t j = 0; j < ARRAY_SIZE(assocs); j++) { | 1998 | for (const IPPTsPng * const *it = assocs; *it; ++it) { |
1984 | const IPPTsPng *const assoc = assocs[j]; | 1999 | const IPPTsPng *const assoc = *it; |
1985 | 2000 | ||
1986 | /* If ip is not zero and node is good. */ | 2001 | /* If ip is not zero and node is good. */ |
1987 | if (ip_isset(&assoc->ret_ip_port.ip) && !is_timeout(assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { | 2002 | if (ip_isset(&assoc->ret_ip_port.ip) && !is_timeout(assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { |
@@ -2020,10 +2035,10 @@ static int routeone_tofriend(DHT *dht, const uint8_t *friend_id, const uint8_t * | |||
2020 | 2035 | ||
2021 | for (uint32_t i = 0; i < MAX_FRIEND_CLIENTS; ++i) { | 2036 | for (uint32_t i = 0; i < MAX_FRIEND_CLIENTS; ++i) { |
2022 | const Client_data *const client = &dht_friend->client_list[i]; | 2037 | const Client_data *const client = &dht_friend->client_list[i]; |
2023 | const IPPTsPng *const assocs[] = { &client->assoc4, &client->assoc6 }; | 2038 | const IPPTsPng *const assocs[] = { &client->assoc4, &client->assoc6, nullptr }; |
2024 | 2039 | ||
2025 | for (size_t j = 0; j < ARRAY_SIZE(assocs); j++) { | 2040 | for (const IPPTsPng * const *it = assocs; *it; ++it) { |
2026 | const IPPTsPng *assoc = assocs[j]; | 2041 | const IPPTsPng *const assoc = *it; |
2027 | 2042 | ||
2028 | /* If ip is not zero and node is good. */ | 2043 | /* If ip is not zero and node is good. */ |
2029 | if (ip_isset(&assoc->ret_ip_port.ip) && !is_timeout(assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { | 2044 | if (ip_isset(&assoc->ret_ip_port.ip) && !is_timeout(assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { |
@@ -2103,13 +2118,13 @@ static int handle_NATping(void *object, IP_Port source, const uint8_t *source_pu | |||
2103 | if (packet[0] == NAT_PING_REQUEST) { | 2118 | if (packet[0] == NAT_PING_REQUEST) { |
2104 | /* 1 is reply */ | 2119 | /* 1 is reply */ |
2105 | send_NATping(dht, source_pubkey, ping_id, NAT_PING_RESPONSE); | 2120 | send_NATping(dht, source_pubkey, ping_id, NAT_PING_RESPONSE); |
2106 | dht_friend->nat.recvNATping_timestamp = unix_time(); | 2121 | dht_friend->nat.recv_nat_ping_timestamp = unix_time(); |
2107 | return 0; | 2122 | return 0; |
2108 | } | 2123 | } |
2109 | 2124 | ||
2110 | if (packet[0] == NAT_PING_RESPONSE) { | 2125 | if (packet[0] == NAT_PING_RESPONSE) { |
2111 | if (dht_friend->nat.NATping_id == ping_id) { | 2126 | if (dht_friend->nat.nat_ping_id == ping_id) { |
2112 | dht_friend->nat.NATping_id = random_u64(); | 2127 | dht_friend->nat.nat_ping_id = random_u64(); |
2113 | dht_friend->nat.hole_punching = 1; | 2128 | dht_friend->nat.hole_punching = 1; |
2114 | return 0; | 2129 | return 0; |
2115 | } | 2130 | } |
@@ -2124,7 +2139,7 @@ static int handle_NATping(void *object, IP_Port source, const uint8_t *source_pu | |||
2124 | * | 2139 | * |
2125 | * return ip of 0 if failure. | 2140 | * return ip of 0 if failure. |
2126 | */ | 2141 | */ |
2127 | static IP NAT_commonip(IP_Port *ip_portlist, uint16_t len, uint16_t min_num) | 2142 | static IP nat_commonip(IP_Port *ip_portlist, uint16_t len, uint16_t min_num) |
2128 | { | 2143 | { |
2129 | IP zero; | 2144 | IP zero; |
2130 | ip_reset(&zero); | 2145 | ip_reset(&zero); |
@@ -2156,7 +2171,7 @@ static IP NAT_commonip(IP_Port *ip_portlist, uint16_t len, uint16_t min_num) | |||
2156 | * | 2171 | * |
2157 | * return number of ports and puts the list of ports in portlist. | 2172 | * return number of ports and puts the list of ports in portlist. |
2158 | */ | 2173 | */ |
2159 | static uint16_t NAT_getports(uint16_t *portlist, IP_Port *ip_portlist, uint16_t len, IP ip) | 2174 | static uint16_t nat_getports(uint16_t *portlist, IP_Port *ip_portlist, uint16_t len, IP ip) |
2160 | { | 2175 | { |
2161 | uint16_t num = 0; | 2176 | uint16_t num = 0; |
2162 | 2177 | ||
@@ -2241,16 +2256,16 @@ static void do_NAT(DHT *dht) | |||
2241 | continue; | 2256 | continue; |
2242 | } | 2257 | } |
2243 | 2258 | ||
2244 | if (dht->friends_list[i].nat.NATping_timestamp + PUNCH_INTERVAL < temp_time) { | 2259 | if (dht->friends_list[i].nat.nat_ping_timestamp + PUNCH_INTERVAL < temp_time) { |
2245 | send_NATping(dht, dht->friends_list[i].public_key, dht->friends_list[i].nat.NATping_id, NAT_PING_REQUEST); | 2260 | send_NATping(dht, dht->friends_list[i].public_key, dht->friends_list[i].nat.nat_ping_id, NAT_PING_REQUEST); |
2246 | dht->friends_list[i].nat.NATping_timestamp = temp_time; | 2261 | dht->friends_list[i].nat.nat_ping_timestamp = temp_time; |
2247 | } | 2262 | } |
2248 | 2263 | ||
2249 | if (dht->friends_list[i].nat.hole_punching == 1 && | 2264 | if (dht->friends_list[i].nat.hole_punching == 1 && |
2250 | dht->friends_list[i].nat.punching_timestamp + PUNCH_INTERVAL < temp_time && | 2265 | dht->friends_list[i].nat.punching_timestamp + PUNCH_INTERVAL < temp_time && |
2251 | dht->friends_list[i].nat.recvNATping_timestamp + PUNCH_INTERVAL * 2 >= temp_time) { | 2266 | dht->friends_list[i].nat.recv_nat_ping_timestamp + PUNCH_INTERVAL * 2 >= temp_time) { |
2252 | 2267 | ||
2253 | const IP ip = NAT_commonip(ip_list, num, MAX_FRIEND_CLIENTS / 2); | 2268 | const IP ip = nat_commonip(ip_list, num, MAX_FRIEND_CLIENTS / 2); |
2254 | 2269 | ||
2255 | if (!ip_isset(&ip)) { | 2270 | if (!ip_isset(&ip)) { |
2256 | continue; | 2271 | continue; |
@@ -2263,7 +2278,7 @@ static void do_NAT(DHT *dht) | |||
2263 | } | 2278 | } |
2264 | 2279 | ||
2265 | uint16_t port_list[MAX_FRIEND_CLIENTS]; | 2280 | uint16_t port_list[MAX_FRIEND_CLIENTS]; |
2266 | const uint16_t numports = NAT_getports(port_list, ip_list, num, ip); | 2281 | const uint16_t numports = nat_getports(port_list, ip_list, num, ip); |
2267 | punch_holes(dht, ip, port_list, numports, i); | 2282 | punch_holes(dht, ip, port_list, numports, i); |
2268 | 2283 | ||
2269 | dht->friends_list[i].nat.punching_timestamp = temp_time; | 2284 | dht->friends_list[i].nat.punching_timestamp = temp_time; |
@@ -2276,16 +2291,16 @@ static void do_NAT(DHT *dht) | |||
2276 | /*-----------------------END OF NAT PUNCHING FUNCTIONS------------------------------*/ | 2291 | /*-----------------------END OF NAT PUNCHING FUNCTIONS------------------------------*/ |
2277 | 2292 | ||
2278 | #define DHT_HARDENING 0 | 2293 | #define DHT_HARDENING 0 |
2279 | #define HARDREQ_DATA_SIZE 384 /* Attempt to prevent amplification/other attacks*/ | 2294 | #define HARDREQ_DATA_SIZE 384 // Attempt to prevent amplification/other attacks |
2280 | 2295 | ||
2281 | enum { | 2296 | typedef enum Check_Type { |
2282 | CHECK_TYPE_ROUTE_REQ = 0, | 2297 | CHECK_TYPE_ROUTE_REQ = 0, |
2283 | CHECK_TYPE_ROUTE_RES = 1, | 2298 | CHECK_TYPE_ROUTE_RES = 1, |
2284 | CHECK_TYPE_GETNODE_REQ = 2, | 2299 | CHECK_TYPE_GETNODE_REQ = 2, |
2285 | CHECK_TYPE_GETNODE_RES = 3, | 2300 | CHECK_TYPE_GETNODE_RES = 3, |
2286 | CHECK_TYPE_TEST_REQ = 4, | 2301 | CHECK_TYPE_TEST_REQ = 4, |
2287 | CHECK_TYPE_TEST_RES = 5, | 2302 | CHECK_TYPE_TEST_RES = 5, |
2288 | }; | 2303 | } Check_Type; |
2289 | 2304 | ||
2290 | #if DHT_HARDENING | 2305 | #if DHT_HARDENING |
2291 | static int send_hardening_req(DHT *dht, Node_format *sendto, uint8_t type, uint8_t *contents, uint16_t length) | 2306 | static int send_hardening_req(DHT *dht, Node_format *sendto, uint8_t type, uint8_t *contents, uint16_t length) |
@@ -2625,7 +2640,7 @@ static void do_hardening(DHT *dht) | |||
2625 | 2640 | ||
2626 | /*----------------------------------------------------------------------------------*/ | 2641 | /*----------------------------------------------------------------------------------*/ |
2627 | 2642 | ||
2628 | void cryptopacket_registerhandler(DHT *dht, uint8_t byte, cryptopacket_handler_callback cb, void *object) | 2643 | void cryptopacket_registerhandler(DHT *dht, uint8_t byte, cryptopacket_handler_cb *cb, void *object) |
2629 | { | 2644 | { |
2630 | dht->cryptopackethandlers[byte].function = cb; | 2645 | dht->cryptopackethandlers[byte].function = cb; |
2631 | dht->cryptopackethandlers[byte].object = object; | 2646 | dht->cryptopackethandlers[byte].object = object; |