Skip to content

Commit b85b91f

Browse files
committed
cleanup: rename getnodes/sendnodes to nodes request/response
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.
1 parent f1991aa commit b85b91f

22 files changed

+368
-368
lines changed

Diff for: CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ set(toxcore_SOURCES
238238
toxcore/events/conference_peer_list_changed.c
239239
toxcore/events/conference_peer_name.c
240240
toxcore/events/conference_title.c
241-
toxcore/events/dht_get_nodes_response.c
241+
toxcore/events/dht_nodes_response.c
242242
toxcore/events/events_alloc.c
243243
toxcore/events/events_alloc.h
244244
toxcore/events/file_chunk_request.c

Diff for: auto_tests/dht_getnodes_api_test.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* This autotest creates a small local DHT and makes sure that each peer can crawl
3-
* the entire DHT using the DHT getnodes api functions.
3+
* the entire DHT using the DHT nodes request/response api functions.
44
*/
55

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

75-
static void getnodes_response_cb(const Tox_Event_Dht_Get_Nodes_Response *event, void *user_data)
75+
static void nodes_response_cb(const Tox_Event_Dht_Nodes_Response *event, void *user_data)
7676
{
7777
ck_assert(user_data != nullptr);
7878

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

82-
const uint8_t *public_key = tox_event_dht_get_nodes_response_get_public_key(event);
83-
const char *ip = (const char *)tox_event_dht_get_nodes_response_get_ip(event);
84-
const uint16_t port = tox_event_dht_get_nodes_response_get_port(event);
82+
const uint8_t *public_key = tox_event_dht_nodes_response_get_public_key(event);
83+
const char *ip = (const char *)tox_event_dht_nodes_response_get_ip(event);
84+
const uint16_t port = tox_event_dht_nodes_response_get_port(event);
8585

8686
if (node_crawled(state->nodes, state->num_nodes, public_key)) {
8787
return;
@@ -101,11 +101,11 @@ static void getnodes_response_cb(const Tox_Event_Dht_Get_Nodes_Response *event,
101101

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

108-
static void test_dht_getnodes(AutoTox *autotoxes)
108+
static void test_dht_nodes_request(AutoTox *autotoxes)
109109
{
110110
ck_assert(NUM_TOXES >= 2);
111111

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

127127
tox_self_get_dht_id(autotoxes[i].tox, public_key_list[i]);
128-
tox_events_callback_dht_get_nodes_response(autotoxes[i].dispatch, getnodes_response_cb);
128+
tox_events_callback_dht_nodes_response(autotoxes[i].dispatch, nodes_response_cb);
129129

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

156-
run_auto_test(nullptr, NUM_TOXES, test_dht_getnodes, sizeof(State), &options);
156+
run_auto_test(nullptr, NUM_TOXES, test_dht_nodes_request, sizeof(State), &options);
157157

158158
return 0;
159159
}

Diff for: toxcore/DHT.c

+46-46
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
/** The timeout after which a node is discarded completely. */
3131
#define KILL_NODE_TIMEOUT (BAD_NODE_TIMEOUT + PING_INTERVAL)
3232

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

3636
#define MAX_PUNCHING_PORTS 48
3737

@@ -46,7 +46,7 @@
4646
#define NAT_PING_REQUEST 0
4747
#define NAT_PING_RESPONSE 1
4848

49-
/** Number of get node requests to send to quickly find close nodes. */
49+
/** Number of node requests to send to quickly find close nodes. */
5050
#define MAX_BOOTSTRAP_TIMES 5
5151

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

69-
/* Time at which the last get_nodes request was sent. */
70-
uint64_t lastgetnode;
71-
/* number of times get_node packets were sent. */
69+
/* Time at which the last nodes request was sent. */
70+
uint64_t last_nodes_request;
71+
/* number of times nodes request packets were sent. */
7272
uint32_t bootstrap_times;
7373

7474
/* Symmetric NAT hole punching stuff. */
@@ -104,7 +104,7 @@ struct DHT {
104104
bool lan_discovery_enabled;
105105

106106
Client_data close_clientlist[LCLIENT_LIST];
107-
uint64_t close_lastgetnodes;
107+
uint64_t close_last_nodes_request;
108108
uint32_t close_bootstrap_times;
109109

110110
/* DHT keypair */
@@ -130,7 +130,7 @@ struct DHT {
130130
Node_format to_bootstrap[MAX_CLOSE_TO_BOOTSTRAP_NODES];
131131
unsigned int num_to_bootstrap;
132132

133-
dht_get_nodes_response_cb *get_nodes_response;
133+
dht_nodes_response_cb *nodes_response_callback;
134134
};
135135

136136
const uint8_t *dht_friend_public_key(const DHT_Friend *dht_friend)
@@ -712,7 +712,7 @@ static void get_close_nodes_inner(
712712
}
713713

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

1138-
/** @brief Check if the node obtained with a get_nodes with public_key should be pinged.
1138+
/** @brief Check if the node obtained from a nodes response with public_key should be pinged.
11391139
*
11401140
* NOTE: for best results call it after addto_lists.
11411141
*
11421142
* return false if the node should not be pinged.
11431143
* return true if it should.
11441144
*/
11451145
non_null()
1146-
static bool ping_node_from_getnodes_ok(DHT *dht, const uint8_t *public_key, const IP_Port *ip_port)
1146+
static bool ping_node_from_nodes_response_ok(DHT *dht, const uint8_t *public_key, const IP_Port *ip_port)
11471147
{
11481148
bool ret = false;
11491149

@@ -1314,7 +1314,7 @@ static void returnedip_ports(DHT *dht, const IP_Port *ip_port, const uint8_t *pu
13141314
}
13151315
}
13161316

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

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

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

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

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

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

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

14151415
non_null()
1416-
static int handle_getnodes(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length, void *userdata)
1416+
static int handle_nodes_request(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length, void *userdata)
14171417
{
14181418
DHT *const dht = (DHT *)object;
14191419

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

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

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

14471447
return 0;
14481448
}
14491449

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

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

14691469
non_null()
1470-
static bool handle_sendnodes_core(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length,
1471-
Node_format *plain_nodes, uint16_t size_plain_nodes, uint32_t *num_nodes_out)
1470+
static bool handle_nodes_response_core(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length,
1471+
Node_format *plain_nodes, uint16_t size_plain_nodes, uint32_t *num_nodes_out)
14721472
{
14731473
DHT *const dht = (DHT *)object;
14741474
const uint32_t cid_size = 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + 1 + sizeof(uint64_t) + CRYPTO_MAC_SIZE;
@@ -1509,7 +1509,7 @@ static bool handle_sendnodes_core(void *object, const IP_Port *source, const uin
15091509
uint64_t ping_id;
15101510
memcpy(&ping_id, plain + 1 + data_size, sizeof(ping_id));
15111511

1512-
if (!sent_getnode_to_node(dht, packet + 1, source, ping_id)) {
1512+
if (!sent_nodes_request_to_node(dht, packet + 1, source, ping_id)) {
15131513
return false;
15141514
}
15151515

@@ -1537,14 +1537,14 @@ static bool handle_sendnodes_core(void *object, const IP_Port *source, const uin
15371537
}
15381538

15391539
non_null()
1540-
static int handle_sendnodes_ipv6(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length,
1540+
static int handle_nodes_response(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length,
15411541
void *userdata)
15421542
{
15431543
DHT *const dht = (DHT *)object;
15441544
Node_format plain_nodes[MAX_SENT_NODES];
15451545
uint32_t num_nodes;
15461546

1547-
if (!handle_sendnodes_core(object, source, packet, length, plain_nodes, MAX_SENT_NODES, &num_nodes)) {
1547+
if (!handle_nodes_response_core(object, source, packet, length, plain_nodes, MAX_SENT_NODES, &num_nodes)) {
15481548
return 1;
15491549
}
15501550

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

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

1560-
if (dht->get_nodes_response != nullptr) {
1561-
dht->get_nodes_response(dht, &plain_nodes[i], userdata);
1560+
if (dht->nodes_response_callback != nullptr) {
1561+
dht->nodes_response_callback(dht, &plain_nodes[i], userdata);
15621562
}
15631563
}
15641564
}
@@ -1771,7 +1771,7 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co
17711771
if (mono_time_is_timeout(dht->mono_time, assoc->last_pinged, PING_INTERVAL)) {
17721772
const IP_Port *target = &assoc->ip_port;
17731773
const uint8_t *target_key = client->public_key;
1774-
dht_getnodes(dht, target, target_key, public_key);
1774+
dht_send_nodes_request(dht, target, target_key, public_key);
17751775
assoc->last_pinged = temp_time;
17761776
}
17771777

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

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

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

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

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

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

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

18351835
dht_friend->num_to_bootstrap = 0;
18361836

1837-
do_ping_and_sendnode_requests(dht, &dht_friend->lastgetnode, dht_friend->public_key, dht_friend->client_list,
1837+
do_ping_and_sendnode_requests(dht, &dht_friend->last_nodes_request, dht_friend->public_key, dht_friend->client_list,
18381838
MAX_FRIEND_CLIENTS, &dht_friend->bootstrap_times, true);
18391839
}
18401840
}
18411841

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

18531853
dht->num_to_bootstrap = 0;
18541854

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

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

1890-
return dht_getnodes(dht, ip_port, public_key, dht->self_public_key);
1890+
return dht_send_nodes_request(dht, ip_port, public_key, dht->self_public_key);
18911891
}
18921892

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

2537-
void dht_callback_get_nodes_response(DHT *dht, dht_get_nodes_response_cb *function)
2537+
void dht_callback_nodes_response(DHT *dht, dht_nodes_response_cb *function)
25382538
{
2539-
dht->get_nodes_response = function;
2539+
dht->nodes_response_callback = function;
25402540
}
25412541

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

2600-
networking_registerhandler(dht->net, NET_PACKET_GET_NODES, &handle_getnodes, dht);
2601-
networking_registerhandler(dht->net, NET_PACKET_SEND_NODES_IPV6, &handle_sendnodes_ipv6, dht);
2600+
networking_registerhandler(dht->net, NET_PACKET_NODES_REQUEST, &handle_nodes_request, dht);
2601+
networking_registerhandler(dht->net, NET_PACKET_NODES_RESPONSE, &handle_nodes_response, dht);
26022602
networking_registerhandler(dht->net, NET_PACKET_CRYPTO, &cryptopacket_handle, dht);
26032603
networking_registerhandler(dht->net, NET_PACKET_LAN_DISCOVERY, &handle_lan_discovery, dht);
26042604
cryptopacket_registerhandler(dht, CRYPTO_PACKET_NAT_PING, &handle_nat_ping, dht);
@@ -2676,8 +2676,8 @@ void kill_dht(DHT *dht)
26762676
return;
26772677
}
26782678

2679-
networking_registerhandler(dht->net, NET_PACKET_GET_NODES, nullptr, nullptr);
2680-
networking_registerhandler(dht->net, NET_PACKET_SEND_NODES_IPV6, nullptr, nullptr);
2679+
networking_registerhandler(dht->net, NET_PACKET_NODES_REQUEST, nullptr, nullptr);
2680+
networking_registerhandler(dht->net, NET_PACKET_NODES_RESPONSE, nullptr, nullptr);
26812681
networking_registerhandler(dht->net, NET_PACKET_CRYPTO, nullptr, nullptr);
26822682
networking_registerhandler(dht->net, NET_PACKET_LAN_DISCOVERY, nullptr, nullptr);
26832683
cryptopacket_registerhandler(dht, CRYPTO_PACKET_NAT_PING, nullptr, nullptr);

0 commit comments

Comments
 (0)