Skip to content

Commit c1e00c5

Browse files
committed
Improved the to_ping list.
1 parent 1d91699 commit c1e00c5

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

toxcore/DHT.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,10 @@ static int friend_number(const DHT *dht, const uint8_t *public_key)
473473
return -1;
474474
}
475475

476-
static _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)
476+
/* Add node to the node list making sure only the nodes closest to cmp_pk are in the list.
477+
*/
478+
_Bool add_to_list(Node_format *nodes_list, unsigned int length, const uint8_t *pk, IP_Port ip_port,
479+
const uint8_t *cmp_pk)
478480
{
479481
uint8_t pk_bak[crypto_box_PUBLICKEYBYTES];
480482
IP_Port ip_port_bak;

toxcore/DHT.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,11 @@ int DHT_getfriendip(const DHT *dht, const uint8_t *public_key, IP_Port *ip_port)
302302
*/
303303
int id_closest(const uint8_t *pk, const uint8_t *pk1, const uint8_t *pk2);
304304

305+
/* Add node to the node list making sure only the nodes closest to cmp_pk are in the list.
306+
*/
307+
_Bool add_to_list(Node_format *nodes_list, unsigned int length, const uint8_t *pk, IP_Port ip_port,
308+
const uint8_t *cmp_pk);
309+
305310
/* Get the (maximum MAX_SENT_NODES) closest nodes to public_key we know
306311
* and put them in nodes_list (must be MAX_SENT_NODES big).
307312
*

toxcore/ping.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -287,15 +287,8 @@ int add_to_ping(PING *ping, const uint8_t *public_key, IP_Port ip_port)
287287
}
288288
}
289289

290-
unsigned int r = rand();
291-
292-
for (i = 0; i < MAX_TO_PING; ++i) {
293-
if (id_closest(ping->dht->self_public_key, ping->to_ping[(i + r) % MAX_TO_PING].public_key, public_key) == 2) {
294-
memcpy(ping->to_ping[(i + r) % MAX_TO_PING].public_key, public_key, crypto_box_PUBLICKEYBYTES);
295-
ipport_copy(&ping->to_ping[(i + r) % MAX_TO_PING].ip_port, &ip_port);
296-
return 0;
297-
}
298-
}
290+
if (add_to_list(ping->to_ping, MAX_TO_PING, public_key, ip_port, ping->dht->self_public_key))
291+
return 0;
299292

300293
return -1;
301294
}

0 commit comments

Comments
 (0)