From 27218c2dbfc266011f11f07c53a9d70415afa9b6 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Wed, 26 Oct 2022 18:27:54 +0200 Subject: [PATCH] diagnostic: get the ratio of close dht nodes with announce/store support --- toxcore/DHT.c | 25 +++++++++++++++++++++++++ toxcore/DHT.h | 8 ++++++++ toxcore/tox_private.c | 9 +++++++++ toxcore/tox_private.h | 7 +++++++ 4 files changed, 49 insertions(+) diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 75907645d6f..480c18265c5 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -2965,6 +2965,31 @@ 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; +} + 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..17027728dff 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h @@ -515,6 +515,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 72a93358a5b..28ae5749674 100644 --- a/toxcore/tox_private.c +++ b/toxcore/tox_private.c @@ -149,3 +149,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 c82357170e6..ca51b999f94 100644 --- a/toxcore/tox_private.h +++ b/toxcore/tox_private.h @@ -140,6 +140,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