summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2015-12-11 15:34:04 -0500
committerirungentoo <irungentoo@gmail.com>2015-12-11 15:34:04 -0500
commitae801b3257ccd2c13b18c013619674bee229dcae (patch)
tree61a3f0f4bc7d6ecdd1b31aa7cee56fcafab0c6eb
parent1986a2e492cc45d442414e5bd39d219a97ae0f25 (diff)
DHT fix.
Return only closest nodes in get nodes packet.
-rw-r--r--toxcore/DHT.c44
1 files changed, 25 insertions, 19 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index b2fe59f4..ae1ef8b1 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -473,6 +473,30 @@ static int friend_number(const DHT *dht, const uint8_t *public_key)
473 return -1; 473 return -1;
474} 474}
475 475
476static _Bool add_to_list(Node_format *nodes_list, unsigned int length, const uint8_t *pk, IP_Port ip_port,
477 const uint8_t *cmp_pk)
478{
479 uint8_t pk_bak[crypto_box_PUBLICKEYBYTES];
480 IP_Port ip_port_bak;
481
482 unsigned int i;
483
484 for (i = 0; i < length; ++i) {
485 if (id_closest(cmp_pk, nodes_list[i].public_key, pk) == 2) {
486 memcpy(pk_bak, nodes_list[i].public_key, crypto_box_PUBLICKEYBYTES);
487 ip_port_bak = nodes_list[i].ip_port;
488 memcpy(nodes_list[i].public_key, pk, crypto_box_PUBLICKEYBYTES);
489
490 if (i != (length - 1))
491 add_to_list(nodes_list, length, pk_bak, ip_port_bak, cmp_pk);
492
493 return 1;
494 }
495 }
496
497 return 0;
498}
499
476/*TODO: change this to 7 when done*/ 500/*TODO: change this to 7 when done*/
477#define HARDENING_ALL_OK 2 501#define HARDENING_ALL_OK 2
478/* return 0 if not. 502/* return 0 if not.
@@ -540,25 +564,7 @@ static void get_close_nodes_inner(const uint8_t *public_key, Node_format *nodes_
540 nodes_list[num_nodes].ip_port = ipptp->ip_port; 564 nodes_list[num_nodes].ip_port = ipptp->ip_port;
541 num_nodes++; 565 num_nodes++;
542 } else { 566 } else {
543 /* see if node_list contains a public_key that's "further away" 567 add_to_list(nodes_list, MAX_SENT_NODES, client->public_key, ipptp->ip_port, public_key);
544 * compared to the one we're looking at at the moment, if there
545 * is, replace it
546 */
547 for (j = 0; j < MAX_SENT_NODES; ++j) {
548 closest = id_closest( public_key,
549 nodes_list[j].public_key,
550 client->public_key );
551
552 /* second public_key is closer than current: change to it */
553 if (closest == 2) {
554 memcpy( nodes_list[j].public_key,
555 client->public_key,
556 crypto_box_PUBLICKEYBYTES);
557
558 nodes_list[j].ip_port = ipptp->ip_port;
559 break;
560 }
561 }
562 } 568 }
563 } 569 }
564 570