summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2020-03-19 02:15:54 +0000
committeriphydf <iphydf@users.noreply.github.com>2020-03-22 01:49:04 +0000
commitd5ae583c2f09cb3fa0a9f5bf2bc60a6d5ecbd4d4 (patch)
tree1c598389bc615fc8c2b908ec8bcb7e89cc42b6d7
parentda93f054fe88c1c99ab8814deb8957c1ece77b8d (diff)
Minor cleanup: use `assoc_timeout` function where possible.
This function exists and simplifies some code a little bit. There are lots of places in DHT.c where we have the exact same code, so there is a lot of opportunity to factor out common bits. For now, we just make a minor improvement that's easy to review.
-rw-r--r--toxcore/DHT.c49
1 files changed, 25 insertions, 24 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index fb2e125d..191ebc0e 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -173,6 +173,11 @@ const uint8_t *dht_get_friend_public_key(const DHT *dht, uint32_t friend_num)
173 return dht->friends_list[friend_num].public_key; 173 return dht->friends_list[friend_num].public_key;
174} 174}
175 175
176static bool assoc_timeout(const Mono_Time *mono_time, const IPPTsPng *assoc)
177{
178 return mono_time_is_timeout(mono_time, assoc->timestamp, BAD_NODE_TIMEOUT);
179}
180
176/* Compares pk1 and pk2 with pk. 181/* Compares pk1 and pk2 with pk.
177 * 182 *
178 * return 0 if both are same distance. 183 * return 0 if both are same distance.
@@ -778,6 +783,7 @@ static uint8_t hardening_correct(const Hardening *h)
778{ 783{
779 return h->routes_requests_ok + (h->send_nodes_ok << 1) + (h->testing_requests << 2); 784 return h->routes_requests_ok + (h->send_nodes_ok << 1) + (h->testing_requests << 2);
780} 785}
786
781/* 787/*
782 * helper for get_close_nodes(). argument list is a monster :D 788 * helper for get_close_nodes(). argument list is a monster :D
783 */ 789 */
@@ -799,7 +805,7 @@ static void get_close_nodes_inner(const Mono_Time *mono_time, const uint8_t *pub
799 continue; 805 continue;
800 } 806 }
801 807
802 const IPPTsPng *ipptp = nullptr; 808 const IPPTsPng *ipptp;
803 809
804 if (net_family_is_ipv4(sa_family)) { 810 if (net_family_is_ipv4(sa_family)) {
805 ipptp = &client->assoc4; 811 ipptp = &client->assoc4;
@@ -812,7 +818,7 @@ static void get_close_nodes_inner(const Mono_Time *mono_time, const uint8_t *pub
812 } 818 }
813 819
814 /* node not in a good condition? */ 820 /* node not in a good condition? */
815 if (mono_time_is_timeout(mono_time, ipptp->timestamp, BAD_NODE_TIMEOUT)) { 821 if (assoc_timeout(mono_time, ipptp)) {
816 continue; 822 continue;
817 } 823 }
818 824
@@ -886,11 +892,6 @@ typedef struct DHT_Cmp_data {
886 Client_data entry; 892 Client_data entry;
887} DHT_Cmp_data; 893} DHT_Cmp_data;
888 894
889static bool assoc_timeout(const Mono_Time *mono_time, const IPPTsPng *assoc)
890{
891 return mono_time_is_timeout(mono_time, assoc->timestamp, BAD_NODE_TIMEOUT);
892}
893
894static bool incorrect_hardening(const IPPTsPng *assoc) 895static bool incorrect_hardening(const IPPTsPng *assoc)
895{ 896{
896 return hardening_correct(&assoc->hardening) != HARDENING_ALL_OK; 897 return hardening_correct(&assoc->hardening) != HARDENING_ALL_OK;
@@ -952,8 +953,8 @@ static int cmp_dht_entry(const void *a, const void *b)
952static unsigned int store_node_ok(const Client_data *client, const Mono_Time *mono_time, const uint8_t *public_key, 953static unsigned int store_node_ok(const Client_data *client, const Mono_Time *mono_time, const uint8_t *public_key,
953 const uint8_t *comp_public_key) 954 const uint8_t *comp_public_key)
954{ 955{
955 return (mono_time_is_timeout(mono_time, client->assoc4.timestamp, BAD_NODE_TIMEOUT) 956 return (assoc_timeout(mono_time, &client->assoc4)
956 && mono_time_is_timeout(mono_time, client->assoc6.timestamp, BAD_NODE_TIMEOUT)) 957 && assoc_timeout(mono_time, &client->assoc6))
957 || id_closest(comp_public_key, client->public_key, public_key) == 2; 958 || id_closest(comp_public_key, client->public_key, public_key) == 2;
958} 959}
959 960
@@ -1059,8 +1060,8 @@ static int add_to_close(DHT *dht, const uint8_t *public_key, IP_Port ip_port, bo
1059 * index is left as >= LCLIENT_LENGTH */ 1060 * index is left as >= LCLIENT_LENGTH */
1060 Client_data *const client = &dht->close_clientlist[(index * LCLIENT_NODES) + i]; 1061 Client_data *const client = &dht->close_clientlist[(index * LCLIENT_NODES) + i];
1061 1062
1062 if (!mono_time_is_timeout(dht->mono_time, client->assoc4.timestamp, BAD_NODE_TIMEOUT) || 1063 if (!assoc_timeout(dht->mono_time, &client->assoc4) ||
1063 !mono_time_is_timeout(dht->mono_time, client->assoc6.timestamp, BAD_NODE_TIMEOUT)) { 1064 !assoc_timeout(dht->mono_time, &client->assoc6)) {
1064 continue; 1065 continue;
1065 } 1066 }
1066 1067
@@ -1096,7 +1097,7 @@ static bool is_pk_in_client_list(const Client_data *list, unsigned int client_li
1096 ? &list[index].assoc4 1097 ? &list[index].assoc4
1097 : &list[index].assoc6; 1098 : &list[index].assoc6;
1098 1099
1099 return !mono_time_is_timeout(mono_time, assoc->timestamp, BAD_NODE_TIMEOUT); 1100 return !assoc_timeout(mono_time, assoc);
1100} 1101}
1101 1102
1102static bool is_pk_in_close_list(DHT *dht, const uint8_t *public_key, IP_Port ip_port) 1103static bool is_pk_in_close_list(DHT *dht, const uint8_t *public_key, IP_Port ip_port)
@@ -1675,7 +1676,7 @@ int dht_getfriendip(const DHT *dht, const uint8_t *public_key, IP_Port *ip_port)
1675 for (const IPPTsPng * const *it = assocs; *it; ++it) { 1676 for (const IPPTsPng * const *it = assocs; *it; ++it) {
1676 const IPPTsPng *const assoc = *it; 1677 const IPPTsPng *const assoc = *it;
1677 1678
1678 if (!mono_time_is_timeout(dht->mono_time, assoc->timestamp, BAD_NODE_TIMEOUT)) { 1679 if (!assoc_timeout(dht->mono_time, assoc)) {
1679 *ip_port = assoc->ip_port; 1680 *ip_port = assoc->ip_port;
1680 return 1; 1681 return 1;
1681 } 1682 }
@@ -1716,7 +1717,7 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co
1716 } 1717 }
1717 1718
1718 /* If node is good. */ 1719 /* If node is good. */
1719 if (!mono_time_is_timeout(dht->mono_time, assoc->timestamp, BAD_NODE_TIMEOUT)) { 1720 if (!assoc_timeout(dht->mono_time, assoc)) {
1720 client_list[num_nodes] = client; 1721 client_list[num_nodes] = client;
1721 assoc_list[num_nodes] = assoc; 1722 assoc_list[num_nodes] = assoc;
1722 ++num_nodes; 1723 ++num_nodes;
@@ -1917,8 +1918,8 @@ static int friend_iplist(const DHT *dht, IP_Port *ip_portlist, uint16_t friend_n
1917 } 1918 }
1918 1919
1919 if (id_equal(client->public_key, dht_friend->public_key)) { 1920 if (id_equal(client->public_key, dht_friend->public_key)) {
1920 if (!mono_time_is_timeout(dht->mono_time, client->assoc6.timestamp, BAD_NODE_TIMEOUT) 1921 if (!assoc_timeout(dht->mono_time, &client->assoc6)
1921 || !mono_time_is_timeout(dht->mono_time, client->assoc4.timestamp, BAD_NODE_TIMEOUT)) { 1922 || !assoc_timeout(dht->mono_time, &client->assoc4)) {
1922 return 0; /* direct connectivity */ 1923 return 0; /* direct connectivity */
1923 } 1924 }
1924 } 1925 }
@@ -2388,7 +2389,7 @@ static uint32_t have_nodes_closelist(DHT *dht, Node_format *nodes, uint16_t num)
2388 const IPPTsPng *const temp = get_closelist_IPPTsPng(dht, nodes[i].public_key, nodes[i].ip_port.ip.family); 2389 const IPPTsPng *const temp = get_closelist_IPPTsPng(dht, nodes[i].public_key, nodes[i].ip_port.ip.family);
2389 2390
2390 if (temp) { 2391 if (temp) {
2391 if (!mono_time_is_timeout(dht->mono_time, temp->timestamp, BAD_NODE_TIMEOUT)) { 2392 if (!assoc_timeout(dht->mono_time, temp)) {
2392 ++counter; 2393 ++counter;
2393 } 2394 }
2394 } 2395 }
@@ -2519,11 +2520,11 @@ static uint16_t list_nodes(Client_data *list, size_t length, const Mono_Time *mo
2519 for (size_t i = length; i != 0; --i) { 2520 for (size_t i = length; i != 0; --i) {
2520 const IPPTsPng *assoc = nullptr; 2521 const IPPTsPng *assoc = nullptr;
2521 2522
2522 if (!mono_time_is_timeout(mono_time, list[i - 1].assoc4.timestamp, BAD_NODE_TIMEOUT)) { 2523 if (!assoc_timeout(mono_time, &list[i - 1].assoc4)) {
2523 assoc = &list[i - 1].assoc4; 2524 assoc = &list[i - 1].assoc4;
2524 } 2525 }
2525 2526
2526 if (!mono_time_is_timeout(mono_time, list[i - 1].assoc6.timestamp, BAD_NODE_TIMEOUT)) { 2527 if (!assoc_timeout(mono_time, &list[i - 1].assoc6)) {
2527 if (assoc == nullptr) { 2528 if (assoc == nullptr) {
2528 assoc = &list[i - 1].assoc6; 2529 assoc = &list[i - 1].assoc6;
2529 } else if (random_u08() % 2) { 2530 } else if (random_u08() % 2) {
@@ -2595,7 +2596,7 @@ static void do_hardening(DHT *dht)
2595 sa_family = net_family_ipv6; 2596 sa_family = net_family_ipv6;
2596 } 2597 }
2597 2598
2598 if (mono_time_is_timeout(dht->mono_time, cur_iptspng->timestamp, BAD_NODE_TIMEOUT)) { 2599 if (assoc_timeout(dht->mono_time, cur_iptspng)) {
2599 continue; 2600 continue;
2600 } 2601 }
2601 2602
@@ -2961,8 +2962,8 @@ bool dht_isconnected(const DHT *dht)
2961 for (uint32_t i = 0; i < LCLIENT_LIST; ++i) { 2962 for (uint32_t i = 0; i < LCLIENT_LIST; ++i) {
2962 const Client_data *const client = &dht->close_clientlist[i]; 2963 const Client_data *const client = &dht->close_clientlist[i];
2963 2964
2964 if (!mono_time_is_timeout(dht->mono_time, client->assoc4.timestamp, BAD_NODE_TIMEOUT) || 2965 if (!assoc_timeout(dht->mono_time, &client->assoc4) ||
2965 !mono_time_is_timeout(dht->mono_time, client->assoc6.timestamp, BAD_NODE_TIMEOUT)) { 2966 !assoc_timeout(dht->mono_time, &client->assoc6)) {
2966 return true; 2967 return true;
2967 } 2968 }
2968 } 2969 }
@@ -2978,12 +2979,12 @@ bool dht_non_lan_connected(const DHT *dht)
2978 for (uint32_t i = 0; i < LCLIENT_LIST; ++i) { 2979 for (uint32_t i = 0; i < LCLIENT_LIST; ++i) {
2979 const Client_data *const client = &dht->close_clientlist[i]; 2980 const Client_data *const client = &dht->close_clientlist[i];
2980 2981
2981 if (!mono_time_is_timeout(dht->mono_time, client->assoc4.timestamp, BAD_NODE_TIMEOUT) 2982 if (!assoc_timeout(dht->mono_time, &client->assoc4)
2982 && !ip_is_lan(client->assoc4.ip_port.ip)) { 2983 && !ip_is_lan(client->assoc4.ip_port.ip)) {
2983 return true; 2984 return true;
2984 } 2985 }
2985 2986
2986 if (!mono_time_is_timeout(dht->mono_time, client->assoc6.timestamp, BAD_NODE_TIMEOUT) 2987 if (!assoc_timeout(dht->mono_time, &client->assoc6)
2987 && !ip_is_lan(client->assoc6.ip_port.ip)) { 2988 && !ip_is_lan(client->assoc6.ip_port.ip)) {
2988 return true; 2989 return true;
2989 } 2990 }