From c1e00c5d3940fc590d099692b848a106c28633ad Mon Sep 17 00:00:00 2001 From: irungentoo Date: Wed, 16 Dec 2015 11:28:30 -0500 Subject: Improved the to_ping list. --- toxcore/DHT.c | 6 ++++-- toxcore/DHT.h | 5 +++++ toxcore/ping.c | 11 ++--------- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/toxcore/DHT.c b/toxcore/DHT.c index a958938d..c4d1aa4c 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -473,8 +473,10 @@ static int friend_number(const DHT *dht, const uint8_t *public_key) return -1; } -static _Bool add_to_list(Node_format *nodes_list, unsigned int length, const uint8_t *pk, IP_Port ip_port, - const uint8_t *cmp_pk) +/* Add node to the node list making sure only the nodes closest to cmp_pk are in the list. + */ +_Bool add_to_list(Node_format *nodes_list, unsigned int length, const uint8_t *pk, IP_Port ip_port, + const uint8_t *cmp_pk) { uint8_t pk_bak[crypto_box_PUBLICKEYBYTES]; IP_Port ip_port_bak; diff --git a/toxcore/DHT.h b/toxcore/DHT.h index 4ae99049..048bfcf4 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h @@ -302,6 +302,11 @@ int DHT_getfriendip(const DHT *dht, const uint8_t *public_key, IP_Port *ip_port) */ int id_closest(const uint8_t *pk, const uint8_t *pk1, const uint8_t *pk2); +/* Add node to the node list making sure only the nodes closest to cmp_pk are in the list. + */ +_Bool add_to_list(Node_format *nodes_list, unsigned int length, const uint8_t *pk, IP_Port ip_port, + const uint8_t *cmp_pk); + /* Get the (maximum MAX_SENT_NODES) closest nodes to public_key we know * and put them in nodes_list (must be MAX_SENT_NODES big). * diff --git a/toxcore/ping.c b/toxcore/ping.c index 17fb3e9e..6a480f6f 100644 --- a/toxcore/ping.c +++ b/toxcore/ping.c @@ -287,15 +287,8 @@ int add_to_ping(PING *ping, const uint8_t *public_key, IP_Port ip_port) } } - unsigned int r = rand(); - - for (i = 0; i < MAX_TO_PING; ++i) { - if (id_closest(ping->dht->self_public_key, ping->to_ping[(i + r) % MAX_TO_PING].public_key, public_key) == 2) { - memcpy(ping->to_ping[(i + r) % MAX_TO_PING].public_key, public_key, crypto_box_PUBLICKEYBYTES); - ipport_copy(&ping->to_ping[(i + r) % MAX_TO_PING].ip_port, &ip_port); - return 0; - } - } + if (add_to_list(ping->to_ping, MAX_TO_PING, public_key, ip_port, ping->dht->self_public_key)) + return 0; return -1; } -- cgit v1.2.3