Skip to content

Commit

Permalink
diagnostic: get the ratio of close dht nodes with announce/store support
Browse files Browse the repository at this point in the history
  • Loading branch information
Green-Sky committed Jan 4, 2023
1 parent 88ffd1a commit da3b8e8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
26 changes: 26 additions & 0 deletions toxcore/DHT.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions toxcore/DHT.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
9 changes: 9 additions & 0 deletions toxcore/tox_private.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

7 changes: 7 additions & 0 deletions toxcore/tox_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit da3b8e8

Please sign in to comment.