Skip to content

Commit

Permalink
cleanup: rename getnodes/sendnodes to nodes request/response
Browse files Browse the repository at this point in the history
This change alignes the naming to be closer to the spec and
make it less ambiguous. This change also changes the naming
of some private/experimental marked APIs.
- tox_callback_dht_nodes_response()
- tox_dht_nodes_request()
- Tox_Event_Dht_Get_Nodes_Response

Also remove the "IPv6" from response.
The non-ipv6 capable response was removed 12 years ago.
  • Loading branch information
Green-Sky committed Feb 4, 2025
1 parent f1991aa commit b85b91f
Show file tree
Hide file tree
Showing 22 changed files with 368 additions and 368 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ set(toxcore_SOURCES
toxcore/events/conference_peer_list_changed.c
toxcore/events/conference_peer_name.c
toxcore/events/conference_title.c
toxcore/events/dht_get_nodes_response.c
toxcore/events/dht_nodes_response.c
toxcore/events/events_alloc.c
toxcore/events/events_alloc.h
toxcore/events/file_chunk_request.c
Expand Down
18 changes: 9 additions & 9 deletions auto_tests/dht_getnodes_api_test.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* This autotest creates a small local DHT and makes sure that each peer can crawl
* the entire DHT using the DHT getnodes api functions.
* the entire DHT using the DHT nodes request/response api functions.
*/

#include <stdio.h>
Expand Down Expand Up @@ -72,16 +72,16 @@ static bool all_nodes_crawled(const AutoTox *autotoxes, uint32_t num_toxes, uint
return true;
}

static void getnodes_response_cb(const Tox_Event_Dht_Get_Nodes_Response *event, void *user_data)
static void nodes_response_cb(const Tox_Event_Dht_Nodes_Response *event, void *user_data)
{
ck_assert(user_data != nullptr);

AutoTox *autotox = (AutoTox *)user_data;
State *state = (State *)autotox->state;

const uint8_t *public_key = tox_event_dht_get_nodes_response_get_public_key(event);
const char *ip = (const char *)tox_event_dht_get_nodes_response_get_ip(event);
const uint16_t port = tox_event_dht_get_nodes_response_get_port(event);
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->nodes, state->num_nodes, public_key)) {
return;
Expand All @@ -101,11 +101,11 @@ static void getnodes_response_cb(const Tox_Event_Dht_Get_Nodes_Response *event,

// ask new node to give us their close nodes to every public key
for (size_t i = 0; i < NUM_TOXES; ++i) {
tox_dht_get_nodes(autotox->tox, public_key, ip, port, state->public_key_list[i], nullptr);
tox_dht_send_nodes_request(autotox->tox, public_key, ip, port, state->public_key_list[i], nullptr);
}
}

static void test_dht_getnodes(AutoTox *autotoxes)
static void test_dht_nodes_request(AutoTox *autotoxes)
{
ck_assert(NUM_TOXES >= 2);

Expand All @@ -125,7 +125,7 @@ static void test_dht_getnodes(AutoTox *autotoxes)
ck_assert(public_key_list[i] != nullptr);

tox_self_get_dht_id(autotoxes[i].tox, public_key_list[i]);
tox_events_callback_dht_get_nodes_response(autotoxes[i].dispatch, getnodes_response_cb);
tox_events_callback_dht_nodes_response(autotoxes[i].dispatch, nodes_response_cb);

printf("Peer %zu dht closenode count total/announce-capable: %d/%d\n",
i,
Expand Down Expand Up @@ -153,7 +153,7 @@ int main(void)
Run_Auto_Options options = default_run_auto_options();
options.graph = GRAPH_LINEAR;

run_auto_test(nullptr, NUM_TOXES, test_dht_getnodes, sizeof(State), &options);
run_auto_test(nullptr, NUM_TOXES, test_dht_nodes_request, sizeof(State), &options);

return 0;
}
Expand Down
92 changes: 46 additions & 46 deletions toxcore/DHT.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
/** The timeout after which a node is discarded completely. */
#define KILL_NODE_TIMEOUT (BAD_NODE_TIMEOUT + PING_INTERVAL)

/** Ping interval in seconds for each random sending of a get nodes request. */
#define GET_NODE_INTERVAL 20
/** Ping interval in seconds for each random sending of a nodes request. */
#define NODES_REQUEST_INTERVAL 20

#define MAX_PUNCHING_PORTS 48

Expand All @@ -46,7 +46,7 @@
#define NAT_PING_REQUEST 0
#define NAT_PING_RESPONSE 1

/** Number of get node requests to send to quickly find close nodes. */
/** Number of node requests to send to quickly find close nodes. */
#define MAX_BOOTSTRAP_TIMES 5

// TODO(sudden6): find out why we need multiple callbacks and if we really need 32
Expand All @@ -66,9 +66,9 @@ struct DHT_Friend {
uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE];
Client_data client_list[MAX_FRIEND_CLIENTS];

/* Time at which the last get_nodes request was sent. */
uint64_t lastgetnode;
/* number of times get_node packets were sent. */
/* Time at which the last nodes request was sent. */
uint64_t last_nodes_request;
/* number of times nodes request packets were sent. */
uint32_t bootstrap_times;

/* Symmetric NAT hole punching stuff. */
Expand Down Expand Up @@ -104,7 +104,7 @@ struct DHT {
bool lan_discovery_enabled;

Client_data close_clientlist[LCLIENT_LIST];
uint64_t close_lastgetnodes;
uint64_t close_last_nodes_request;
uint32_t close_bootstrap_times;

/* DHT keypair */
Expand All @@ -130,7 +130,7 @@ struct DHT {
Node_format to_bootstrap[MAX_CLOSE_TO_BOOTSTRAP_NODES];
unsigned int num_to_bootstrap;

dht_get_nodes_response_cb *get_nodes_response;
dht_nodes_response_cb *nodes_response_callback;
};

const uint8_t *dht_friend_public_key(const DHT_Friend *dht_friend)
Expand Down Expand Up @@ -712,7 +712,7 @@ static void get_close_nodes_inner(
}

/**
* Find MAX_SENT_NODES nodes closest to the public_key for the send nodes request:
* Find MAX_SENT_NODES nodes closest to the public_key for the nodes request:
* put them in the nodes_list and return how many were found.
*
* want_announce: return only nodes which implement the dht announcements protocol.
Expand Down Expand Up @@ -1135,15 +1135,15 @@ static bool is_pk_in_close_list(const DHT *dht, const uint8_t *public_key, const
ip_port);
}

/** @brief Check if the node obtained with a get_nodes with public_key should be pinged.
/** @brief Check if the node obtained from a nodes response with public_key should be pinged.
*
* NOTE: for best results call it after addto_lists.
*
* return false if the node should not be pinged.
* return true if it should.
*/
non_null()
static bool ping_node_from_getnodes_ok(DHT *dht, const uint8_t *public_key, const IP_Port *ip_port)
static bool ping_node_from_nodes_response_ok(DHT *dht, const uint8_t *public_key, const IP_Port *ip_port)
{
bool ret = false;

Expand Down Expand Up @@ -1314,7 +1314,7 @@ static void returnedip_ports(DHT *dht, const IP_Port *ip_port, const uint8_t *pu
}
}

bool dht_getnodes(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key, const uint8_t *client_id)
bool dht_send_nodes_request(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key, const uint8_t *client_id)
{
/* Check if packet is going to be sent to ourself. */
if (pk_equal(public_key, dht->self_public_key)) {
Expand Down Expand Up @@ -1349,21 +1349,21 @@ bool dht_getnodes(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key, c
const uint8_t *shared_key = dht_get_shared_key_sent(dht, public_key);

const int len = dht_create_packet(dht->mem, dht->rng,
dht->self_public_key, shared_key, NET_PACKET_GET_NODES,
dht->self_public_key, shared_key, NET_PACKET_NODES_REQUEST,
plain, sizeof(plain), data, sizeof(data));

if (len != sizeof(data)) {
LOGGER_ERROR(dht->log, "getnodes packet encryption failed");
LOGGER_ERROR(dht->log, "nodes request packet encryption failed");
return false;
}

return sendpacket(dht->net, ip_port, data, len) > 0;
}

/** Send a send nodes response: message for IPv6 nodes */
/** Send a nodes response */
non_null()
static int sendnodes_ipv6(const DHT *dht, const IP_Port *ip_port, const uint8_t *public_key, const uint8_t *client_id,
const uint8_t *sendback_data, uint16_t length, const uint8_t *shared_encryption_key)
static int send_nodes_response(const DHT *dht, const IP_Port *ip_port, const uint8_t *public_key, const uint8_t *client_id,
const uint8_t *sendback_data, uint16_t length, const uint8_t *shared_encryption_key)
{
/* Check if packet is going to be sent to ourself. */
if (pk_equal(public_key, dht->self_public_key)) {
Expand Down Expand Up @@ -1400,7 +1400,7 @@ static int sendnodes_ipv6(const DHT *dht, const IP_Port *ip_port, const uint8_t
VLA(uint8_t, data, data_size);

const int len = dht_create_packet(dht->mem, dht->rng,
dht->self_public_key, shared_encryption_key, NET_PACKET_SEND_NODES_IPV6,
dht->self_public_key, shared_encryption_key, NET_PACKET_NODES_RESPONSE,
plain, 1 + nodes_length + length, data, data_size);

if (len < 0 || (uint32_t)len != data_size) {
Expand All @@ -1413,7 +1413,7 @@ static int sendnodes_ipv6(const DHT *dht, const IP_Port *ip_port, const uint8_t
#define CRYPTO_NODE_SIZE (CRYPTO_PUBLIC_KEY_SIZE + sizeof(uint64_t))

non_null()
static int handle_getnodes(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length, void *userdata)
static int handle_nodes_request(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length, void *userdata)
{
DHT *const dht = (DHT *)object;

Expand All @@ -1440,16 +1440,16 @@ static int handle_getnodes(void *object, const IP_Port *source, const uint8_t *p
return 1;
}

sendnodes_ipv6(dht, source, packet + 1, plain, plain + CRYPTO_PUBLIC_KEY_SIZE, sizeof(uint64_t), shared_key);
send_nodes_response(dht, source, packet + 1, plain, plain + CRYPTO_PUBLIC_KEY_SIZE, sizeof(uint64_t), shared_key);

ping_add(dht->ping, packet + 1, source);

return 0;
}

/** Return true if we sent a getnode packet to the peer associated with the supplied info. */
/** Return true if we sent a nodes request packet to the peer associated with the supplied info. */
non_null()
static bool sent_getnode_to_node(DHT *dht, const uint8_t *public_key, const IP_Port *node_ip_port, uint64_t ping_id)
static bool sent_nodes_request_to_node(DHT *dht, const uint8_t *public_key, const IP_Port *node_ip_port, uint64_t ping_id)
{
uint8_t data[sizeof(Node_format) * 2];

Expand All @@ -1467,8 +1467,8 @@ static bool sent_getnode_to_node(DHT *dht, const uint8_t *public_key, const IP_P
}

non_null()
static bool handle_sendnodes_core(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length,
Node_format *plain_nodes, uint16_t size_plain_nodes, uint32_t *num_nodes_out)
static bool handle_nodes_response_core(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length,
Node_format *plain_nodes, uint16_t size_plain_nodes, uint32_t *num_nodes_out)
{
DHT *const dht = (DHT *)object;
const uint32_t cid_size = 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + 1 + sizeof(uint64_t) + CRYPTO_MAC_SIZE;
Expand Down Expand Up @@ -1509,7 +1509,7 @@ static bool handle_sendnodes_core(void *object, const IP_Port *source, const uin
uint64_t ping_id;
memcpy(&ping_id, plain + 1 + data_size, sizeof(ping_id));

if (!sent_getnode_to_node(dht, packet + 1, source, ping_id)) {
if (!sent_nodes_request_to_node(dht, packet + 1, source, ping_id)) {
return false;
}

Expand Down Expand Up @@ -1537,14 +1537,14 @@ static bool handle_sendnodes_core(void *object, const IP_Port *source, const uin
}

non_null()
static int handle_sendnodes_ipv6(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length,
static int handle_nodes_response(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length,
void *userdata)
{
DHT *const dht = (DHT *)object;
Node_format plain_nodes[MAX_SENT_NODES];
uint32_t num_nodes;

if (!handle_sendnodes_core(object, source, packet, length, plain_nodes, MAX_SENT_NODES, &num_nodes)) {
if (!handle_nodes_response_core(object, source, packet, length, plain_nodes, MAX_SENT_NODES, &num_nodes)) {
return 1;
}

Expand All @@ -1554,11 +1554,11 @@ static int handle_sendnodes_ipv6(void *object, const IP_Port *source, const uint

for (uint32_t i = 0; i < num_nodes; ++i) {
if (ipport_isset(&plain_nodes[i].ip_port)) {
ping_node_from_getnodes_ok(dht, plain_nodes[i].public_key, &plain_nodes[i].ip_port);
ping_node_from_nodes_response_ok(dht, plain_nodes[i].public_key, &plain_nodes[i].ip_port);
returnedip_ports(dht, &plain_nodes[i].ip_port, plain_nodes[i].public_key, packet + 1);

if (dht->get_nodes_response != nullptr) {
dht->get_nodes_response(dht, &plain_nodes[i], userdata);
if (dht->nodes_response_callback != nullptr) {
dht->nodes_response_callback(dht, &plain_nodes[i], userdata);
}
}
}
Expand Down Expand Up @@ -1771,7 +1771,7 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co
if (mono_time_is_timeout(dht->mono_time, assoc->last_pinged, PING_INTERVAL)) {
const IP_Port *target = &assoc->ip_port;
const uint8_t *target_key = client->public_key;
dht_getnodes(dht, target, target_key, public_key);
dht_send_nodes_request(dht, target, target_key, public_key);
assoc->last_pinged = temp_time;
}

Expand All @@ -1796,7 +1796,7 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co
sort_client_list(dht->mem, list, dht->cur_time, list_count, public_key);
}

if (num_nodes > 0 && (mono_time_is_timeout(dht->mono_time, *lastgetnode, GET_NODE_INTERVAL)
if (num_nodes > 0 && (mono_time_is_timeout(dht->mono_time, *lastgetnode, NODES_REQUEST_INTERVAL)
|| *bootstrap_times < MAX_BOOTSTRAP_TIMES)) {
uint32_t rand_node = random_range_u32(dht->rng, num_nodes);

Expand All @@ -1806,7 +1806,7 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co

const IP_Port *target = &assoc_list[rand_node]->ip_port;
const uint8_t *target_key = client_list[rand_node]->public_key;
dht_getnodes(dht, target, target_key, public_key);
dht_send_nodes_request(dht, target, target_key, public_key);

*lastgetnode = temp_time;
++*bootstrap_times;
Expand All @@ -1819,7 +1819,7 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co

/** @brief Ping each client in the "friends" list every PING_INTERVAL seconds.
*
* Send a get nodes request every GET_NODE_INTERVAL seconds to a random good
* Send a nodes request every NODES_REQUEST_INTERVAL seconds to a random good
* node for each "friend" in our "friends" list.
*/
non_null()
Expand All @@ -1829,31 +1829,31 @@ static void do_dht_friends(DHT *dht)
DHT_Friend *const dht_friend = &dht->friends_list[i];

for (size_t j = 0; j < dht_friend->num_to_bootstrap; ++j) {
dht_getnodes(dht, &dht_friend->to_bootstrap[j].ip_port, dht_friend->to_bootstrap[j].public_key, dht_friend->public_key);
dht_send_nodes_request(dht, &dht_friend->to_bootstrap[j].ip_port, dht_friend->to_bootstrap[j].public_key, dht_friend->public_key);
}

dht_friend->num_to_bootstrap = 0;

do_ping_and_sendnode_requests(dht, &dht_friend->lastgetnode, dht_friend->public_key, dht_friend->client_list,
do_ping_and_sendnode_requests(dht, &dht_friend->last_nodes_request, dht_friend->public_key, dht_friend->client_list,
MAX_FRIEND_CLIENTS, &dht_friend->bootstrap_times, true);
}
}

/** @brief Ping each client in the close nodes list every PING_INTERVAL seconds.
*
* Send a get nodes request every GET_NODE_INTERVAL seconds to a random good node in the list.
* Send a nodes request every NODES_REQUEST_INTERVAL seconds to a random good node in the list.
*/
non_null()
static void do_close(DHT *dht)
{
for (size_t i = 0; i < dht->num_to_bootstrap; ++i) {
dht_getnodes(dht, &dht->to_bootstrap[i].ip_port, dht->to_bootstrap[i].public_key, dht->self_public_key);
dht_send_nodes_request(dht, &dht->to_bootstrap[i].ip_port, dht->to_bootstrap[i].public_key, dht->self_public_key);
}

dht->num_to_bootstrap = 0;

const uint8_t not_killed = do_ping_and_sendnode_requests(
dht, &dht->close_lastgetnodes, dht->self_public_key, dht->close_clientlist, LCLIENT_LIST, &dht->close_bootstrap_times,
dht, &dht->close_last_nodes_request, dht->self_public_key, dht->close_clientlist, LCLIENT_LIST, &dht->close_bootstrap_times,
false);

if (not_killed != 0) {
Expand Down Expand Up @@ -1887,7 +1887,7 @@ bool dht_bootstrap(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key)
return true;
}

return dht_getnodes(dht, ip_port, public_key, dht->self_public_key);
return dht_send_nodes_request(dht, ip_port, public_key, dht->self_public_key);
}

bool dht_bootstrap_from_address(DHT *dht, const char *address, bool ipv6enabled, bool dns_enabled,
Expand Down Expand Up @@ -2534,9 +2534,9 @@ static int cryptopacket_handle(void *object, const IP_Port *source, const uint8_
return 1;
}

void dht_callback_get_nodes_response(DHT *dht, dht_get_nodes_response_cb *function)
void dht_callback_nodes_response(DHT *dht, dht_nodes_response_cb *function)
{
dht->get_nodes_response = function;
dht->nodes_response_callback = function;
}

non_null(1, 2, 3) nullable(5)
Expand Down Expand Up @@ -2597,8 +2597,8 @@ DHT *new_dht(const Logger *log, const Memory *mem, const Random *rng, const Netw
return nullptr;
}

networking_registerhandler(dht->net, NET_PACKET_GET_NODES, &handle_getnodes, dht);
networking_registerhandler(dht->net, NET_PACKET_SEND_NODES_IPV6, &handle_sendnodes_ipv6, dht);
networking_registerhandler(dht->net, NET_PACKET_NODES_REQUEST, &handle_nodes_request, dht);
networking_registerhandler(dht->net, NET_PACKET_NODES_RESPONSE, &handle_nodes_response, dht);
networking_registerhandler(dht->net, NET_PACKET_CRYPTO, &cryptopacket_handle, dht);
networking_registerhandler(dht->net, NET_PACKET_LAN_DISCOVERY, &handle_lan_discovery, dht);
cryptopacket_registerhandler(dht, CRYPTO_PACKET_NAT_PING, &handle_nat_ping, dht);
Expand Down Expand Up @@ -2676,8 +2676,8 @@ void kill_dht(DHT *dht)
return;
}

networking_registerhandler(dht->net, NET_PACKET_GET_NODES, nullptr, nullptr);
networking_registerhandler(dht->net, NET_PACKET_SEND_NODES_IPV6, nullptr, nullptr);
networking_registerhandler(dht->net, NET_PACKET_NODES_REQUEST, nullptr, nullptr);
networking_registerhandler(dht->net, NET_PACKET_NODES_RESPONSE, nullptr, nullptr);
networking_registerhandler(dht->net, NET_PACKET_CRYPTO, nullptr, nullptr);
networking_registerhandler(dht->net, NET_PACKET_LAN_DISCOVERY, nullptr, nullptr);
cryptopacket_registerhandler(dht, CRYPTO_PACKET_NAT_PING, nullptr, nullptr);
Expand Down
Loading

0 comments on commit b85b91f

Please sign in to comment.