summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2018-07-09 15:17:00 +0000
committeriphydf <iphydf@users.noreply.github.com>2018-07-12 09:32:46 +0000
commitcbda01021c561bd061cb03a1c1bab58199ac2307 (patch)
tree537e8397e84d3ccd4e16753101ded1bf676392b2
parent37f8f566d53a3e2603467aa0bbe1e29581ddd561 (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.
-rw-r--r--auto_tests/monolith_test.cc2
-rw-r--r--toxcore/DHT.c159
-rw-r--r--toxcore/DHT.h46
3 files changed, 109 insertions, 98 deletions
diff --git a/auto_tests/monolith_test.cc b/auto_tests/monolith_test.cc
index 7511decf..53b4982e 100644
--- a/auto_tests/monolith_test.cc
+++ b/auto_tests/monolith_test.cc
@@ -164,7 +164,7 @@ int main(int argc, char *argv[]) {
164#if defined(__x86_64__) && defined(__LP64__) 164#if defined(__x86_64__) && defined(__LP64__)
165 // toxcore/DHT 165 // toxcore/DHT
166 CHECK_SIZE(Client_data, 496); 166 CHECK_SIZE(Client_data, 496);
167 CHECK_SIZE(Cryptopacket_Handles, 16); 167 CHECK_SIZE(Cryptopacket_Handler, 16);
168 CHECK_SIZE(DHT, 676528); 168 CHECK_SIZE(DHT, 676528);
169 CHECK_SIZE(DHT_Friend, 5104); 169 CHECK_SIZE(DHT_Friend, 5104);
170 CHECK_SIZE(Hardening, 144); 170 CHECK_SIZE(Hardening, 144);
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]) 64typedef struct DHT_Friend_Callback {
65 dht_ip_cb *ip_callback;
66 void *data;
67 int32_t number;
68} DHT_Friend_Callback;
65 69
66struct DHT_Friend { 70struct 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
89typedef struct Cryptopacket_Handler {
90 cryptopacket_handler_cb *function;
91 void *object;
92} Cryptopacket_Handler;
93
89struct DHT { 94struct 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
630static uint32_t index_of_client_pk(const Client_data *array, uint32_t size, const uint8_t *pk) 637static 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
635static uint32_t index_of_friend_pk(const DHT_Friend *array, uint32_t size, const uint8_t *pk) 642static 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
640static uint32_t index_of_node_pk(const Node_format *array, uint32_t size, const uint8_t *pk) 647static 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
888typedef struct { 895typedef 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
900static bool assoc_timeout(const IPPTsPng *assoc)
901{
902 return is_timeout(assoc->timestamp, BAD_NODE_TIMEOUT);
903}
904
905static bool incorrect_hardening(const IPPTsPng *assoc)
906{
907 return hardening_correct(&assoc->hardening) != HARDENING_ALL_OK;
908}
909
893static int cmp_dht_entry(const void *a, const void *b) 910static 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
1543int dht_addfriend(DHT *dht, const uint8_t *public_key, void (*ip_callback)(void *data, int32_t number, IP_Port), 1556int 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 */
2127static IP NAT_commonip(IP_Port *ip_portlist, uint16_t len, uint16_t min_num) 2142static 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 */
2159static uint16_t NAT_getports(uint16_t *portlist, IP_Port *ip_portlist, uint16_t len, IP ip) 2174static 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
2281enum { 2296typedef 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
2291static int send_hardening_req(DHT *dht, Node_format *sendto, uint8_t type, uint8_t *contents, uint16_t length) 2306static 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
2628void cryptopacket_registerhandler(DHT *dht, uint8_t byte, cryptopacket_handler_callback cb, void *object) 2643void 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;
diff --git a/toxcore/DHT.h b/toxcore/DHT.h
index 15ff44e5..0852827c 100644
--- a/toxcore/DHT.h
+++ b/toxcore/DHT.h
@@ -64,10 +64,10 @@
64 64
65#define MAX_CRYPTO_REQUEST_SIZE 1024 65#define MAX_CRYPTO_REQUEST_SIZE 1024
66 66
67#define CRYPTO_PACKET_FRIEND_REQ 32 /* Friend request crypto packet ID. */ 67#define CRYPTO_PACKET_FRIEND_REQ 32 // Friend request crypto packet ID.
68#define CRYPTO_PACKET_HARDENING 48 /* Hardening crypto packet ID. */ 68#define CRYPTO_PACKET_HARDENING 48 // Hardening crypto packet ID.
69#define CRYPTO_PACKET_DHTPK 156 69#define CRYPTO_PACKET_DHTPK 156
70#define CRYPTO_PACKET_NAT_PING 254 /* NAT ping crypto packet ID. */ 70#define CRYPTO_PACKET_NAT_PING 254 // NAT ping crypto packet ID.
71 71
72/* Create a request to peer. 72/* Create a request to peer.
73 * send_public_key and send_secret_key are the pub/secret keys of the sender. 73 * send_public_key and send_secret_key are the pub/secret keys of the sender.
@@ -89,12 +89,12 @@ int create_request(const uint8_t *send_public_key, const uint8_t *send_secret_ke
89int handle_request(const uint8_t *self_public_key, const uint8_t *self_secret_key, uint8_t *public_key, uint8_t *data, 89int handle_request(const uint8_t *self_public_key, const uint8_t *self_secret_key, uint8_t *public_key, uint8_t *data,
90 uint8_t *request_id, const uint8_t *packet, uint16_t length); 90 uint8_t *request_id, const uint8_t *packet, uint16_t length);
91 91
92typedef struct { 92typedef struct IPPTs {
93 IP_Port ip_port; 93 IP_Port ip_port;
94 uint64_t timestamp; 94 uint64_t timestamp;
95} IPPTs; 95} IPPTs;
96 96
97typedef struct { 97typedef struct Hardening {
98 /* Node routes request correctly (true (1) or false/didn't check (0)) */ 98 /* Node routes request correctly (true (1) or false/didn't check (0)) */
99 uint8_t routes_requests_ok; 99 uint8_t routes_requests_ok;
100 /* Time which we last checked this.*/ 100 /* Time which we last checked this.*/
@@ -112,7 +112,7 @@ typedef struct {
112 uint8_t testing_pingedid[CRYPTO_PUBLIC_KEY_SIZE]; 112 uint8_t testing_pingedid[CRYPTO_PUBLIC_KEY_SIZE];
113} Hardening; 113} Hardening;
114 114
115typedef struct { 115typedef struct IPPTsPng {
116 IP_Port ip_port; 116 IP_Port ip_port;
117 uint64_t timestamp; 117 uint64_t timestamp;
118 uint64_t last_pinged; 118 uint64_t last_pinged;
@@ -123,7 +123,7 @@ typedef struct {
123 uint64_t ret_timestamp; 123 uint64_t ret_timestamp;
124} IPPTsPng; 124} IPPTsPng;
125 125
126typedef struct { 126typedef struct Client_data {
127 uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; 127 uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE];
128 IPPTsPng assoc4; 128 IPPTsPng assoc4;
129 IPPTsPng assoc6; 129 IPPTsPng assoc6;
@@ -131,7 +131,7 @@ typedef struct {
131 131
132/*----------------------------------------------------------------------------------*/ 132/*----------------------------------------------------------------------------------*/
133 133
134typedef struct { 134typedef struct NAT {
135 /* 1 if currently hole punching, otherwise 0 */ 135 /* 1 if currently hole punching, otherwise 0 */
136 uint8_t hole_punching; 136 uint8_t hole_punching;
137 uint32_t punching_index; 137 uint32_t punching_index;
@@ -139,18 +139,17 @@ typedef struct {
139 uint32_t punching_index2; 139 uint32_t punching_index2;
140 140
141 uint64_t punching_timestamp; 141 uint64_t punching_timestamp;
142 uint64_t recvNATping_timestamp; 142 uint64_t recv_nat_ping_timestamp;
143 uint64_t NATping_id; 143 uint64_t nat_ping_id;
144 uint64_t NATping_timestamp; 144 uint64_t nat_ping_timestamp;
145} NAT; 145} NAT;
146 146
147#define DHT_FRIEND_MAX_LOCKS 32 147#define DHT_FRIEND_MAX_LOCKS 32
148 148
149typedef struct { 149typedef struct Node_format {
150 uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; 150 uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE];
151 IP_Port ip_port; 151 IP_Port ip_port;
152} 152} Node_format;
153Node_format;
154 153
155typedef struct DHT_Friend DHT_Friend; 154typedef struct DHT_Friend DHT_Friend;
156 155
@@ -199,7 +198,7 @@ int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed
199#define MAX_KEYS_PER_SLOT 4 198#define MAX_KEYS_PER_SLOT 4
200#define KEYS_TIMEOUT 600 199#define KEYS_TIMEOUT 600
201 200
202typedef struct { 201typedef struct Shared_Key {
203 uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; 202 uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE];
204 uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]; 203 uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE];
205 uint32_t times_requested; 204 uint32_t times_requested;
@@ -207,19 +206,14 @@ typedef struct {
207 uint64_t time_last_requested; 206 uint64_t time_last_requested;
208} Shared_Key; 207} Shared_Key;
209 208
210typedef struct { 209typedef struct Shared_Keys {
211 Shared_Key keys[256 * MAX_KEYS_PER_SLOT]; 210 Shared_Key keys[256 * MAX_KEYS_PER_SLOT];
212} Shared_Keys; 211} Shared_Keys;
213 212
214/*----------------------------------------------------------------------------------*/ 213/*----------------------------------------------------------------------------------*/
215 214
216typedef int (*cryptopacket_handler_callback)(void *object, IP_Port ip_port, const uint8_t *source_pubkey, 215typedef int cryptopacket_handler_cb(void *object, IP_Port ip_port, const uint8_t *source_pubkey,
217 const uint8_t *data, uint16_t len, void *userdata); 216 const uint8_t *data, uint16_t len, void *userdata);
218
219typedef struct {
220 cryptopacket_handler_callback function;
221 void *object;
222} Cryptopacket_Handles;
223 217
224#define DHT_DEFINED 218#define DHT_DEFINED
225typedef struct DHT DHT; 219typedef struct DHT DHT;
@@ -261,6 +255,8 @@ void dht_get_shared_key_sent(DHT *dht, uint8_t *shared_key, const uint8_t *publi
261 255
262void dht_getnodes(DHT *dht, const IP_Port *from_ipp, const uint8_t *from_id, const uint8_t *which_id); 256void dht_getnodes(DHT *dht, const IP_Port *from_ipp, const uint8_t *from_id, const uint8_t *which_id);
263 257
258typedef void dht_ip_cb(void *object, int32_t number, IP_Port ip_port);
259
264/* Add a new friend to the friends list. 260/* Add a new friend to the friends list.
265 * public_key must be CRYPTO_PUBLIC_KEY_SIZE bytes long. 261 * public_key must be CRYPTO_PUBLIC_KEY_SIZE bytes long.
266 * 262 *
@@ -273,7 +269,7 @@ void dht_getnodes(DHT *dht, const IP_Port *from_ipp, const uint8_t *from_id, con
273 * return 0 if success. 269 * return 0 if success.
274 * return -1 if failure (friends list is full). 270 * return -1 if failure (friends list is full).
275 */ 271 */
276int dht_addfriend(DHT *dht, const uint8_t *public_key, void (*ip_callback)(void *data, int32_t number, IP_Port), 272int dht_addfriend(DHT *dht, const uint8_t *public_key, dht_ip_cb *ip_callback,
277 void *data, int32_t number, uint16_t *lock_count); 273 void *data, int32_t number, uint16_t *lock_count);
278 274
279/* Delete a friend from the friends list. 275/* Delete a friend from the friends list.
@@ -387,7 +383,7 @@ int route_tofriend(const DHT *dht, const uint8_t *friend_id, const uint8_t *pack
387 383
388/* Function to handle crypto packets. 384/* Function to handle crypto packets.
389 */ 385 */
390void cryptopacket_registerhandler(DHT *dht, uint8_t byte, cryptopacket_handler_callback cb, void *object); 386void cryptopacket_registerhandler(DHT *dht, uint8_t byte, cryptopacket_handler_cb *cb, void *object);
391 387
392/* SAVE/LOAD functions */ 388/* SAVE/LOAD functions */
393 389