Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: get the number of close dht nodes with announce/store support #2357

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions auto_tests/dht_getnodes_api_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ static void test_dht_getnodes(AutoTox *autotoxes)

tox_self_get_dht_id(autotoxes[i].tox, public_key_list[i]);
tox_callback_dht_get_nodes_response(autotoxes[i].tox, getnodes_response_cb);

printf("Peer %zu dht closenode count total/annouce-capable: %d/%d\n",
i,
tox_dht_get_num_closelist(autotoxes[i].tox),
tox_dht_get_num_closelist_announce_capable(autotoxes[i].tox)
);
}

while (!all_nodes_crawled(autotoxes, NUM_TOXES, public_key_list)) {
Expand Down
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/docker/tox-bootstrapd.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
c71f87c6ff30393d748bbdc118248eff90a4874cfa015b3113534f2333154555 /usr/local/bin/tox-bootstrapd
9bec65f2a3093ebb49c3751dfad267482bc80d4b29ef9171f11d5ba53058d713 /usr/local/bin/tox-bootstrapd
28 changes: 28 additions & 0 deletions toxcore/DHT.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = 0;
for (uint32_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))) {
++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 = 0;
for (uint32_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)) && 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);
Expand Down
18 changes: 18 additions & 0 deletions toxcore/DHT.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
17 changes: 17 additions & 0 deletions toxcore/tox_private.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
const 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);
const uint16_t num_cap = dht_get_num_closelist_announce_capable(tox->m->dht);
tox_unlock(tox);

return num_cap;
}

16 changes: 16 additions & 0 deletions toxcore/tox_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Green-Sky marked this conversation as resolved.
Show resolved Hide resolved
* 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
Expand Down