diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 75907645d6f..ec380c54d7e 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -2965,6 +2965,34 @@ bool dht_non_lan_connected(const DHT *dht) return false; } +uint16_t dht_get_num_closelist(const DHT *dht) { + uint16_t num_valid_close_clients = 0u; + for (size_t i = 0u; i < LCLIENT_LIST; i++) { + const Client_data *const client = dht_get_close_client(dht, i); + + // check if client is valid + if (!(assoc_timeout(dht->cur_time, &client->assoc4) && assoc_timeout(dht->cur_time, &client->assoc6))) { + num_valid_close_clients++; + } + } + + return num_valid_close_clients; +} + +uint16_t dht_get_num_closelist_announce_capable(const DHT *dht) { + uint16_t num_valid_close_clients_with_cap = 0u; + for (size_t i = 0u; i < LCLIENT_LIST; i++) { + const Client_data *const client = dht_get_close_client(dht, i); + + // check if client is valid + if (!(assoc_timeout(dht->cur_time, &client->assoc4) && assoc_timeout(dht->cur_time, &client->assoc6)) && client->announce_node) { + num_valid_close_clients_with_cap++; + } + } + + return num_valid_close_clients_with_cap; +} + unsigned int ipport_self_copy(const DHT *dht, IP_Port *dest) { ipport_reset(dest); diff --git a/toxcore/DHT.h b/toxcore/DHT.h index d4fc33dc4d5..6021958fb05 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h @@ -515,6 +515,24 @@ bool dht_isconnected(const DHT *dht); non_null() bool dht_non_lan_connected(const DHT *dht); +/** + * This function returns the ratio of close dht nodes that are known to support announce/store. + * This function returns the number of DHT nodes in the closelist. + * + * @return number + */ +non_null() +uint16_t dht_get_num_closelist(const DHT *dht); + +/** + * This function returns the number of DHT nodes in the closelist, + * that are capable to store annouce data (introduced in version 0.2.18). + * + * @return number + */ +non_null() +uint16_t dht_get_num_closelist_announce_capable(const DHT *dht); + /** @brief Attempt to add client with ip_port and public_key to the friends client list * and close_clientlist. * diff --git a/toxcore/tox_private.c b/toxcore/tox_private.c index 72a93358a5b..ba62cd41929 100644 --- a/toxcore/tox_private.c +++ b/toxcore/tox_private.c @@ -149,3 +149,20 @@ bool tox_dht_get_nodes(const Tox *tox, const uint8_t *public_key, const char *ip return true; } + +uint16_t tox_dht_get_num_closelist(const Tox *tox) { + tox_lock(tox); + uint16_t num_total = dht_get_num_closelist(tox->m->dht); + tox_unlock(tox); + + return num_total; +} + +uint16_t tox_dht_get_num_closelist_announce_capable(const Tox *tox){ + tox_lock(tox); + uint16_t num_cap = dht_get_num_closelist_announce_capable(tox->m->dht); + tox_unlock(tox); + + return num_cap; +} + diff --git a/toxcore/tox_private.h b/toxcore/tox_private.h index c82357170e6..1503293614f 100644 --- a/toxcore/tox_private.h +++ b/toxcore/tox_private.h @@ -140,6 +140,22 @@ typedef enum Tox_Err_Dht_Get_Nodes { bool tox_dht_get_nodes(const Tox *tox, const uint8_t *public_key, const char *ip, uint16_t port, const uint8_t *target_public_key, Tox_Err_Dht_Get_Nodes *error); +/** + * This function returns the ratio of close dht nodes that are known to support announce/store. + * This function returns the number of DHT nodes in the closelist. + * + * @return number + */ +uint16_t tox_dht_get_num_closelist(const Tox *tox); + +/** + * This function returns the number of DHT nodes in the closelist, + * that are capable to store annouce data (introduced in version 0.2.18). + * + * @return number + */ +uint16_t tox_dht_get_num_closelist_announce_capable(const Tox *tox); + #ifdef __cplusplus } #endif