diff options
author | irungentoo <irungentoo@gmail.com> | 2015-12-09 08:59:10 -0500 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2015-12-09 08:59:10 -0500 |
commit | deb0b358a5c6c2d05b4b6574fe864d5b078cf5f4 (patch) | |
tree | c130fc8875a6a3b7c8d1825ef649576ba77ec510 | |
parent | 4a6837acc705c260724d68e1e5efba095d84788f (diff) |
DHT fix.
Improve DHT distance function.
-rw-r--r-- | toxcore/DHT.c | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 0540125c..6ff7e295 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -75,12 +75,37 @@ int id_closest(const uint8_t *pk, const uint8_t *pk1, const uint8_t *pk2) | |||
75 | { | 75 | { |
76 | size_t i; | 76 | size_t i; |
77 | uint8_t distance1, distance2; | 77 | uint8_t distance1, distance2; |
78 | _Bool d1_abs = 0, d2_abs = 0; | ||
78 | 79 | ||
79 | for (i = 0; i < crypto_box_PUBLICKEYBYTES; ++i) { | 80 | for (i = 0; i < crypto_box_PUBLICKEYBYTES; ++i) { |
80 | 81 | ||
81 | distance1 = pk[i] ^ pk1[i]; | 82 | distance1 = pk[i] ^ pk1[i]; |
82 | distance2 = pk[i] ^ pk2[i]; | 83 | distance2 = pk[i] ^ pk2[i]; |
83 | 84 | ||
85 | if (!i) { | ||
86 | if (distance1 & (1 << 7)) { | ||
87 | d1_abs = 1; | ||
88 | } | ||
89 | |||
90 | if (distance2 & (1 << 7)) { | ||
91 | d2_abs = 1; | ||
92 | } | ||
93 | } | ||
94 | |||
95 | if (d1_abs) | ||
96 | distance1 = ~distance1; | ||
97 | |||
98 | if (d2_abs) | ||
99 | distance2 = ~distance2; | ||
100 | |||
101 | if (i == (crypto_box_PUBLICKEYBYTES - 1)) { | ||
102 | if (d1_abs) | ||
103 | ++distance1; | ||
104 | |||
105 | if (d2_abs) | ||
106 | ++distance2; | ||
107 | } | ||
108 | |||
84 | if (distance1 < distance2) | 109 | if (distance1 < distance2) |
85 | return 1; | 110 | return 1; |
86 | 111 | ||
@@ -1402,9 +1427,9 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co | |||
1402 | 1427 | ||
1403 | if (rand_node >= num_nodes) { | 1428 | if (rand_node >= num_nodes) { |
1404 | rand_node = rand_node % num_nodes; | 1429 | rand_node = rand_node % num_nodes; |
1405 | uint8_t get_pk[crypto_box_PUBLICKEYBYTES]; | ||
1406 | 1430 | ||
1407 | if (memcmp(client_list[rand_node]->public_key, public_key, crypto_box_PUBLICKEYBYTES) != 0) { | 1431 | if (memcmp(client_list[rand_node]->public_key, public_key, crypto_box_PUBLICKEYBYTES) != 0) { |
1432 | uint8_t get_pk[crypto_box_PUBLICKEYBYTES]; | ||
1408 | find_midpoint(get_pk, client_list[rand_node]->public_key, public_key); | 1433 | find_midpoint(get_pk, client_list[rand_node]->public_key, public_key); |
1409 | getnodes(dht, assoc_list[rand_node]->ip_port, client_list[rand_node]->public_key, get_pk, NULL); | 1434 | getnodes(dht, assoc_list[rand_node]->ip_port, client_list[rand_node]->public_key, get_pk, NULL); |
1410 | } | 1435 | } |