Files
tomato/auto_tests/scenarios/scenario_dht_nodes_response_api_test.c
Green Sky 565efa4f39 Squashed 'external/toxcore/c-toxcore/' changes from 1828c5356..c9cdae001
c9cdae001 fix(toxav): remove extra copy of video frame on encode
4f6d4546b test: Improve the fake network library.
a2581e700 refactor(toxcore): generate `Friend_Request` and `Dht_Nodes_Response`
2aaa11770 refactor(toxcore): use Tox_Memory in generated events
5c367452b test(toxcore): fix incorrect mutex in tox_scenario_get_time
8f92e710f perf: Add a timed limit of number of cookie requests.
695b6417a test: Add some more simulated network support.
815ae9ce9 test(toxcore): fix thread-safety in scenario framework
6d85c754e test(toxcore): add unit tests for net_crypto
9c22e79cc test(support): add SimulatedEnvironment for deterministic testing
f34fcb195 chore: Update windows Dockerfile to debian stable (trixie).
ece0e8980 fix(group_moderation): allow validating unsorted sanction list signatures
a4fa754d7 refactor: rename struct Packet to struct Net_Packet
d6f330f85 cleanup: Fix some warnings from coverity.
e206bffa2 fix(group_chats): fix sync packets reverting topics
0e4715598 test: Add new scenario testing framework.
668291f44 refactor(toxcore): decouple Network_Funcs from sockaddr via IP_Port
fc4396cef fix: potential division by zero in toxav and unsafe hex parsing
8e8b352ab refactor: Add nullable annotations to struct members.
7740bb421 refactor: decouple net_crypto from DHT
1936d4296 test: add benchmark for toxav audio and video
46bfdc2df fix: correct printf format specifiers for unsigned integers
REVERT: 1828c5356 fix(toxav): remove extra copy of video frame on encode

git-subtree-dir: external/toxcore/c-toxcore
git-subtree-split: c9cdae001341e701fca980c9bb9febfeb95d2902
2026-01-11 14:42:31 +01:00

196 lines
6.9 KiB
C

#include "framework/framework.h"
#include "../../toxcore/tox_private.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NUM_TOXES 30
typedef struct {
uint8_t public_key[TOX_DHT_NODE_PUBLIC_KEY_SIZE];
char ip[TOX_DHT_NODE_IP_STRING_SIZE];
uint16_t port;
} Dht_Node;
typedef struct {
Dht_Node *nodes[NUM_TOXES];
size_t num_nodes;
uint8_t public_key_list[NUM_TOXES][TOX_PUBLIC_KEY_SIZE];
} State;
static bool node_crawled(const State *state, const uint8_t *public_key)
{
for (size_t i = 0; i < state->num_nodes; ++i) {
if (memcmp(state->nodes[i]->public_key, public_key, TOX_DHT_NODE_PUBLIC_KEY_SIZE) == 0) {
return true;
}
}
return false;
}
static void on_dht_nodes_response(const Tox_Event_Dht_Nodes_Response *event, void *user_data)
{
ToxNode *self = (ToxNode *)user_data;
State *state = (State *)tox_node_get_script_ctx(self);
const uint8_t *public_key = tox_event_dht_nodes_response_get_public_key(event);
const char *ip = (const char *)tox_event_dht_nodes_response_get_ip(event);
const uint16_t port = tox_event_dht_nodes_response_get_port(event);
if (node_crawled(state, public_key)) {
return;
}
if (state->num_nodes >= NUM_TOXES) {
return;
}
Dht_Node *node = (Dht_Node *)calloc(1, sizeof(Dht_Node));
ck_assert(node != nullptr);
memcpy(node->public_key, public_key, TOX_DHT_NODE_PUBLIC_KEY_SIZE);
snprintf(node->ip, sizeof(node->ip), "%s", ip);
node->port = port;
state->nodes[state->num_nodes] = node;
++state->num_nodes;
// ask new node to give us their close nodes to every public key
for (size_t i = 0; i < NUM_TOXES; ++i) {
tox_dht_send_nodes_request(tox_node_get_tox(self), public_key, ip, port, state->public_key_list[i], nullptr);
}
}
static void peer_script(ToxNode *self, void *ctx)
{
State *state = (State *)ctx;
tox_events_callback_dht_nodes_response(tox_node_get_dispatch(self), on_dht_nodes_response);
// Initial bootstrap: ask the node we bootstrapped from for all other nodes
// Wait for self connection first
WAIT_UNTIL(tox_node_is_self_connected(self));
// After connecting, we should start receiving responses because we bootstrapped
// but the original test calls tox_dht_send_nodes_request for all nodes.
// In our case, we bootstrap linearly, so Peer-i bootstraps from Peer-(i-1).
// Peer-0 doesn't bootstrap from anyone.
// To kick off crawling, each node can ask its bootstrap source for all nodes.
uint32_t my_index = tox_node_get_index(self);
if (my_index > 0) {
ToxNode *bootstrap_source = tox_scenario_get_node(tox_node_get_scenario(self), my_index - 1);
uint8_t bs_pk[TOX_PUBLIC_KEY_SIZE];
tox_self_get_dht_id(tox_node_get_tox(bootstrap_source), bs_pk);
Tox_Err_Get_Port port_err;
uint16_t bs_port = tox_self_get_udp_port(tox_node_get_tox(bootstrap_source), &port_err);
for (size_t i = 0; i < NUM_TOXES; ++i) {
tox_dht_send_nodes_request(tox_node_get_tox(self), bs_pk, "127.0.0.1", bs_port, state->public_key_list[i], nullptr);
}
}
// Wait until we have crawled all nodes
uint64_t last_log_time = 0;
uint64_t last_request_time = 0;
while (state->num_nodes < NUM_TOXES) {
tox_scenario_yield(self);
uint64_t now = tox_scenario_get_time(tox_node_get_scenario(self));
if (now - last_request_time >= 2000) {
last_request_time = now;
// Retry strategy: ask a random known node for missing nodes
if (state->num_nodes > 0) {
int idx = rand() % state->num_nodes;
const Dht_Node *n = state->nodes[idx];
for (int i = 0; i < NUM_TOXES; ++i) {
if (!node_crawled(state, state->public_key_list[i])) {
tox_dht_send_nodes_request(tox_node_get_tox(self),
n->public_key, n->ip, n->port, state->public_key_list[i], nullptr);
}
}
} else if (my_index > 0) {
// Fallback: if we haven't found anyone yet, ask bootstrap peer again
ToxNode *bootstrap_source = tox_scenario_get_node(tox_node_get_scenario(self), my_index - 1);
uint8_t bs_pk[TOX_PUBLIC_KEY_SIZE];
tox_self_get_dht_id(tox_node_get_tox(bootstrap_source), bs_pk);
Tox_Err_Get_Port port_err;
uint16_t bs_port = tox_self_get_udp_port(tox_node_get_tox(bootstrap_source), &port_err);
for (int i = 0; i < NUM_TOXES; ++i) {
tox_dht_send_nodes_request(tox_node_get_tox(self), bs_pk, "127.0.0.1", bs_port, state->public_key_list[i], nullptr);
}
}
}
if (now - last_log_time >= 5000) {
last_log_time = now;
tox_node_log(self, "Still crawling... found %zu/%d nodes", state->num_nodes, NUM_TOXES);
if (state->num_nodes < NUM_TOXES) {
char missing[256] = {0};
size_t pos = 0;
for (int i = 0; i < NUM_TOXES; ++i) {
if (!node_crawled(state, state->public_key_list[i])) {
pos += snprintf(missing + pos, sizeof(missing) - pos, "%d ", i);
if (pos >= sizeof(missing) - 1) {
break;
}
}
}
tox_node_log(self, "Missing nodes: %s", missing);
}
}
}
tox_node_log(self, "Finished crawling all %u nodes.", (unsigned int)state->num_nodes);
}
int main(int argc, char *argv[])
{
ToxScenario *s = tox_scenario_new(argc, argv, 60000);
State *states = (State *)calloc(NUM_TOXES, sizeof(State));
ToxNode *nodes[NUM_TOXES];
for (uint32_t i = 0; i < NUM_TOXES; ++i) {
char alias[32];
snprintf(alias, sizeof(alias), "Peer-%u", i);
nodes[i] = tox_scenario_add_node(s, alias, peer_script, &states[i], sizeof(State));
}
// Fill the public_key_list for all nodes
uint8_t public_keys[NUM_TOXES][TOX_PUBLIC_KEY_SIZE];
for (uint32_t i = 0; i < NUM_TOXES; ++i) {
tox_self_get_dht_id(tox_node_get_tox(nodes[i]), public_keys[i]);
}
for (uint32_t i = 0; i < NUM_TOXES; ++i) {
memcpy(states[i].public_key_list, public_keys, sizeof(public_keys));
}
// Create a linear graph
for (uint32_t i = 1; i < NUM_TOXES; ++i) {
tox_node_bootstrap(nodes[i], nodes[i - 1]);
}
ToxScenarioStatus res = tox_scenario_run(s);
if (res != TOX_SCENARIO_DONE) {
fprintf(stderr, "Scenario failed with status %u\n", res);
return 1;
}
for (uint32_t i = 0; i < NUM_TOXES; ++i) {
for (size_t j = 0; j < states[i].num_nodes; ++j) {
free(states[i].nodes[j]);
}
}
free(states);
tox_scenario_free(s);
return 0;
}
#undef NUM_TOXES