diff --git a/toxcore/DHT.c b/toxcore/DHT.c index ffb89e1eebb..9995e835af6 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -3060,6 +3060,32 @@ bool dht_non_lan_connected(const DHT *dht) return false; } +float dht_get_announce_capability_ratio(const DHT *dht) { + float num_valid_close_clients = 0.f; + float num_valid_close_clients_with_cap = 0.f; + for (size_t i = 0; 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)) { + continue; + } + + num_valid_close_clients += 1.f; + + if (client->announce_node) { + num_valid_close_clients_with_cap += 1.f; + } + } + + if (num_valid_close_clients == 0.f) { + return 0.f; + } + + return num_valid_close_clients_with_cap / num_valid_close_clients; +} + /** @brief Copies our own ip_port structure to `dest`. * * WAN addresses take priority over LAN addresses. diff --git a/toxcore/DHT.h b/toxcore/DHT.h index 86ac4f9d406..eaef7abc52f 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h @@ -514,6 +514,14 @@ 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. + * + * @return ratio + */ +non_null() +float dht_get_announce_capability_ratio(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 847e96d4262..48986f7386d 100644 --- a/toxcore/tox_private.c +++ b/toxcore/tox_private.c @@ -147,3 +147,12 @@ bool tox_dht_get_nodes(const Tox *tox, const uint8_t *public_key, const char *ip return true; } + +float tox_dht_get_announce_capability_ratio(const Tox *tox) { + tox_lock(tox); + float ret = dht_get_announce_capability_ratio(tox->m->dht); + tox_unlock(tox); + + return ret; +} + diff --git a/toxcore/tox_private.h b/toxcore/tox_private.h index 71d7a976018..f0b91be2fa8 100644 --- a/toxcore/tox_private.h +++ b/toxcore/tox_private.h @@ -139,6 +139,13 @@ 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. + * + * @return ratio + */ +float tox_dht_get_announce_capability_ratio(const Tox *tox); + #ifdef __cplusplus } #endif