Skip to content

Commit 8099d82

Browse files
committed
diagnostic: get the number of close dht nodes with announce/store support
1 parent d01c116 commit 8099d82

File tree

6 files changed

+86
-1
lines changed

6 files changed

+86
-1
lines changed

auto_tests/dht_getnodes_api_test.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ static void test_dht_getnodes(AutoTox *autotoxes)
122122

123123
tox_self_get_dht_id(autotoxes[i].tox, public_key_list[i]);
124124
tox_callback_dht_get_nodes_response(autotoxes[i].tox, getnodes_response_cb);
125+
126+
printf("Peer %zu dht closenode count total/annouce-capable: %d/%d\n",
127+
i,
128+
tox_dht_get_num_closelist(autotoxes[i].tox),
129+
tox_dht_get_num_closelist_announce_capable(autotoxes[i].tox)
130+
);
125131
}
126132

127133
while (!all_nodes_crawled(autotoxes, NUM_TOXES, public_key_list)) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
c71f87c6ff30393d748bbdc118248eff90a4874cfa015b3113534f2333154555 /usr/local/bin/tox-bootstrapd
1+
9bec65f2a3093ebb49c3751dfad267482bc80d4b29ef9171f11d5ba53058d713 /usr/local/bin/tox-bootstrapd

toxcore/DHT.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2965,6 +2965,34 @@ bool dht_non_lan_connected(const DHT *dht)
29652965
return false;
29662966
}
29672967

2968+
uint16_t dht_get_num_closelist(const DHT *dht) {
2969+
uint16_t num_valid_close_clients = 0;
2970+
for (uint32_t i = 0; i < LCLIENT_LIST; ++i) {
2971+
const Client_data *const client = dht_get_close_client(dht, i);
2972+
2973+
// check if client is valid
2974+
if (!(assoc_timeout(dht->cur_time, &client->assoc4) && assoc_timeout(dht->cur_time, &client->assoc6))) {
2975+
++num_valid_close_clients;
2976+
}
2977+
}
2978+
2979+
return num_valid_close_clients;
2980+
}
2981+
2982+
uint16_t dht_get_num_closelist_announce_capable(const DHT *dht) {
2983+
uint16_t num_valid_close_clients_with_cap = 0;
2984+
for (uint32_t i = 0; i < LCLIENT_LIST; ++i) {
2985+
const Client_data *const client = dht_get_close_client(dht, i);
2986+
2987+
// check if client is valid
2988+
if (!(assoc_timeout(dht->cur_time, &client->assoc4) && assoc_timeout(dht->cur_time, &client->assoc6)) && client->announce_node) {
2989+
++num_valid_close_clients_with_cap;
2990+
}
2991+
}
2992+
2993+
return num_valid_close_clients_with_cap;
2994+
}
2995+
29682996
unsigned int ipport_self_copy(const DHT *dht, IP_Port *dest)
29692997
{
29702998
ipport_reset(dest);

toxcore/DHT.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,24 @@ bool dht_isconnected(const DHT *dht);
515515
non_null()
516516
bool dht_non_lan_connected(const DHT *dht);
517517

518+
/**
519+
* This function returns the ratio of close dht nodes that are known to support announce/store.
520+
* This function returns the number of DHT nodes in the closelist.
521+
*
522+
* @return number
523+
*/
524+
non_null()
525+
uint16_t dht_get_num_closelist(const DHT *dht);
526+
527+
/**
528+
* This function returns the number of DHT nodes in the closelist,
529+
* that are capable to store annouce data (introduced in version 0.2.18).
530+
*
531+
* @return number
532+
*/
533+
non_null()
534+
uint16_t dht_get_num_closelist_announce_capable(const DHT *dht);
535+
518536
/** @brief Attempt to add client with ip_port and public_key to the friends client list
519537
* and close_clientlist.
520538
*

toxcore/tox_private.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,20 @@ bool tox_dht_get_nodes(const Tox *tox, const uint8_t *public_key, const char *ip
149149

150150
return true;
151151
}
152+
153+
uint16_t tox_dht_get_num_closelist(const Tox *tox) {
154+
tox_lock(tox);
155+
const uint16_t num_total = dht_get_num_closelist(tox->m->dht);
156+
tox_unlock(tox);
157+
158+
return num_total;
159+
}
160+
161+
uint16_t tox_dht_get_num_closelist_announce_capable(const Tox *tox){
162+
tox_lock(tox);
163+
const uint16_t num_cap = dht_get_num_closelist_announce_capable(tox->m->dht);
164+
tox_unlock(tox);
165+
166+
return num_cap;
167+
}
168+

toxcore/tox_private.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,22 @@ typedef enum Tox_Err_Dht_Get_Nodes {
140140
bool tox_dht_get_nodes(const Tox *tox, const uint8_t *public_key, const char *ip, uint16_t port,
141141
const uint8_t *target_public_key, Tox_Err_Dht_Get_Nodes *error);
142142

143+
/**
144+
* This function returns the ratio of close dht nodes that are known to support announce/store.
145+
* This function returns the number of DHT nodes in the closelist.
146+
*
147+
* @return number
148+
*/
149+
uint16_t tox_dht_get_num_closelist(const Tox *tox);
150+
151+
/**
152+
* This function returns the number of DHT nodes in the closelist,
153+
* that are capable to store annouce data (introduced in version 0.2.18).
154+
*
155+
* @return number
156+
*/
157+
uint16_t tox_dht_get_num_closelist_announce_capable(const Tox *tox);
158+
143159
#ifdef __cplusplus
144160
}
145161
#endif

0 commit comments

Comments
 (0)