summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2015-12-08 20:04:27 -0500
committerirungentoo <irungentoo@gmail.com>2015-12-08 20:04:27 -0500
commit4a6837acc705c260724d68e1e5efba095d84788f (patch)
treec1f114ae3296c6873a99f2f04b490891eeaf5478
parent30acc33ef673b357768acd3f438fe675cd60b5f6 (diff)
Improve likelyhood of the closest nodes being in our lists.
-rw-r--r--toxcore/DHT.c54
1 files changed, 51 insertions, 3 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 459a6a78..0540125c 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -1324,6 +1324,43 @@ int DHT_getfriendip(const DHT *dht, const uint8_t *public_key, IP_Port *ip_port)
1324 return -1; 1324 return -1;
1325} 1325}
1326 1326
1327static void divide_by_2(uint8_t *public_key)
1328{
1329 unsigned int i;
1330 _Bool one = 0;
1331
1332 for (i = 0; i < crypto_box_PUBLICKEYBYTES; ++i) {
1333 _Bool temp = 0;
1334
1335 if (public_key[i] & (1)) {
1336 temp = 1;
1337 }
1338
1339 public_key[i] >>= 1;
1340
1341 if (one)
1342 public_key[i] += (1 << 7);
1343
1344 one = temp;
1345 }
1346}
1347
1348static void find_midpoint(uint8_t *out, const uint8_t *top, const uint8_t *bot)
1349{
1350 unsigned int i;
1351 _Bool one = 0;
1352
1353 for (i = 0; i < crypto_box_PUBLICKEYBYTES; ++i) {
1354 out[i] = top[i] ^ bot[i];
1355 }
1356
1357 divide_by_2(out);
1358
1359 for (i = 0; i < crypto_box_PUBLICKEYBYTES; ++i) {
1360 out[i] ^= bot[i];
1361 }
1362}
1363
1327/* returns number of nodes not in kill-timeout */ 1364/* returns number of nodes not in kill-timeout */
1328static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, const uint8_t *public_key, 1365static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, const uint8_t *public_key,
1329 Client_data *list, uint32_t list_count, uint32_t *bootstrap_times) 1366 Client_data *list, uint32_t list_count, uint32_t *bootstrap_times)
@@ -1361,9 +1398,20 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co
1361 } 1398 }
1362 1399
1363 if ((num_nodes != 0) && (is_timeout(*lastgetnode, GET_NODE_INTERVAL) || *bootstrap_times < MAX_BOOTSTRAP_TIMES)) { 1400 if ((num_nodes != 0) && (is_timeout(*lastgetnode, GET_NODE_INTERVAL) || *bootstrap_times < MAX_BOOTSTRAP_TIMES)) {
1364 uint32_t rand_node = rand() % num_nodes; 1401 uint32_t rand_node = rand() % (num_nodes * 2);
1365 getnodes(dht, assoc_list[rand_node]->ip_port, client_list[rand_node]->public_key, 1402
1366 public_key, NULL); 1403 if (rand_node >= num_nodes) {
1404 rand_node = rand_node % num_nodes;
1405 uint8_t get_pk[crypto_box_PUBLICKEYBYTES];
1406
1407 if (memcmp(client_list[rand_node]->public_key, public_key, crypto_box_PUBLICKEYBYTES) != 0) {
1408 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);
1410 }
1411 } else {
1412 getnodes(dht, assoc_list[rand_node]->ip_port, client_list[rand_node]->public_key, public_key, NULL);
1413 }
1414
1367 *lastgetnode = temp_time; 1415 *lastgetnode = temp_time;
1368 ++*bootstrap_times; 1416 ++*bootstrap_times;
1369 } 1417 }