Improved the to_ping list.

This commit is contained in:
irungentoo 2015-12-16 11:28:30 -05:00
parent 1d91699e8e
commit c1e00c5d39
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98
3 changed files with 11 additions and 11 deletions

View File

@ -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;

View File

@ -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).
*

View File

@ -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;
}