/src/c-toxcore/toxcore/ping.c
Line | Count | Source |
1 | | /* SPDX-License-Identifier: GPL-3.0-or-later |
2 | | * Copyright © 2016-2025 The TokTok team. |
3 | | * Copyright © 2013 Tox project. |
4 | | * Copyright © 2013 plutooo |
5 | | */ |
6 | | |
7 | | /** |
8 | | * Buffered pinging using cyclic arrays. |
9 | | */ |
10 | | #include "ping.h" |
11 | | |
12 | | #include <string.h> |
13 | | |
14 | | #include "DHT.h" |
15 | | #include "attributes.h" |
16 | | #include "ccompat.h" |
17 | | #include "crypto_core.h" |
18 | | #include "mem.h" |
19 | | #include "mono_time.h" |
20 | | #include "network.h" |
21 | | #include "ping_array.h" |
22 | | |
23 | 1.38k | #define PING_NUM_MAX 512 |
24 | | |
25 | | /** Maximum newly announced nodes to ping per TIME_TO_PING seconds. */ |
26 | 0 | #define MAX_TO_PING 32 |
27 | | |
28 | | /** Ping newly announced nodes to ping per TIME_TO_PING seconds*/ |
29 | 0 | #define TIME_TO_PING 2 |
30 | | |
31 | | struct Ping { |
32 | | const Mono_Time *mono_time; |
33 | | const Random *rng; |
34 | | const Memory *mem; |
35 | | DHT *dht; |
36 | | Networking_Core *net; |
37 | | |
38 | | Ping_Array *ping_array; |
39 | | Node_format to_ping[MAX_TO_PING]; |
40 | | uint64_t last_to_ping; |
41 | | }; |
42 | | |
43 | 0 | #define PING_PLAIN_SIZE (1 + sizeof(uint64_t)) |
44 | 0 | #define DHT_PING_SIZE (1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + PING_PLAIN_SIZE + CRYPTO_MAC_SIZE) |
45 | | #define PING_DATA_SIZE (CRYPTO_PUBLIC_KEY_SIZE + sizeof(IP_Port)) |
46 | | |
47 | | void ping_send_request(Ping *ping, const IP_Port *ipp, const uint8_t *public_key) |
48 | 0 | { |
49 | 0 | uint8_t pk[DHT_PING_SIZE]; |
50 | 0 | int rc; |
51 | 0 | uint64_t ping_id; |
52 | |
|
53 | 0 | if (pk_equal(public_key, dht_get_self_public_key(ping->dht))) { |
54 | 0 | return; |
55 | 0 | } |
56 | | |
57 | | // generate key to encrypt ping_id with recipient privkey |
58 | 0 | const uint8_t *shared_key = dht_get_shared_key_sent(ping->dht, public_key); |
59 | | // Generate random ping_id. |
60 | 0 | uint8_t data[PING_DATA_SIZE]; |
61 | 0 | pk_copy(data, public_key); |
62 | 0 | memcpy(data + CRYPTO_PUBLIC_KEY_SIZE, ipp, sizeof(IP_Port)); |
63 | 0 | ping_id = ping_array_add(ping->ping_array, ping->mono_time, ping->rng, data, sizeof(data)); |
64 | |
|
65 | 0 | if (ping_id == 0) { |
66 | 0 | return; |
67 | 0 | } |
68 | | |
69 | 0 | uint8_t ping_plain[PING_PLAIN_SIZE]; |
70 | 0 | ping_plain[0] = NET_PACKET_PING_REQUEST; |
71 | 0 | memcpy(ping_plain + 1, &ping_id, sizeof(ping_id)); |
72 | |
|
73 | 0 | pk[0] = NET_PACKET_PING_REQUEST; |
74 | 0 | pk_copy(pk + 1, dht_get_self_public_key(ping->dht)); // Our pubkey |
75 | 0 | random_nonce(ping->rng, pk + 1 + CRYPTO_PUBLIC_KEY_SIZE); // Generate new nonce |
76 | |
|
77 | 0 | rc = encrypt_data_symmetric(ping->mem, shared_key, |
78 | 0 | pk + 1 + CRYPTO_PUBLIC_KEY_SIZE, |
79 | 0 | ping_plain, sizeof(ping_plain), |
80 | 0 | pk + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE); |
81 | |
|
82 | 0 | if (rc != PING_PLAIN_SIZE + CRYPTO_MAC_SIZE) { |
83 | 0 | return; |
84 | 0 | } |
85 | | |
86 | | // We never check this return value and failures in sendpacket are already logged |
87 | 0 | sendpacket(ping->net, ipp, pk, sizeof(pk)); |
88 | 0 | } |
89 | | |
90 | | static int ping_send_response(const Ping *_Nonnull ping, const IP_Port *_Nonnull ipp, const uint8_t *_Nonnull public_key, uint64_t ping_id, const uint8_t *_Nonnull shared_encryption_key) |
91 | 0 | { |
92 | 0 | uint8_t pk[DHT_PING_SIZE]; |
93 | |
|
94 | 0 | if (pk_equal(public_key, dht_get_self_public_key(ping->dht))) { |
95 | 0 | return 1; |
96 | 0 | } |
97 | | |
98 | 0 | uint8_t ping_plain[PING_PLAIN_SIZE]; |
99 | 0 | ping_plain[0] = NET_PACKET_PING_RESPONSE; |
100 | 0 | memcpy(ping_plain + 1, &ping_id, sizeof(ping_id)); |
101 | |
|
102 | 0 | pk[0] = NET_PACKET_PING_RESPONSE; |
103 | 0 | pk_copy(pk + 1, dht_get_self_public_key(ping->dht)); // Our pubkey |
104 | 0 | random_nonce(ping->rng, pk + 1 + CRYPTO_PUBLIC_KEY_SIZE); // Generate new nonce |
105 | | |
106 | | // Encrypt ping_id using recipient privkey |
107 | 0 | const int rc = encrypt_data_symmetric(ping->mem, shared_encryption_key, |
108 | 0 | pk + 1 + CRYPTO_PUBLIC_KEY_SIZE, |
109 | 0 | ping_plain, sizeof(ping_plain), |
110 | 0 | pk + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE); |
111 | |
|
112 | 0 | if (rc != PING_PLAIN_SIZE + CRYPTO_MAC_SIZE) { |
113 | 0 | return 1; |
114 | 0 | } |
115 | | |
116 | 0 | return sendpacket(ping->net, ipp, pk, sizeof(pk)); |
117 | 0 | } |
118 | | |
119 | | static int handle_ping_request(void *_Nonnull object, const IP_Port *_Nonnull source, const uint8_t *_Nonnull packet, uint16_t length, void *_Nonnull userdata) |
120 | 0 | { |
121 | 0 | DHT *dht = (DHT *)object; |
122 | |
|
123 | 0 | if (length != DHT_PING_SIZE) { |
124 | 0 | return 1; |
125 | 0 | } |
126 | | |
127 | 0 | Ping *ping = dht_get_ping(dht); |
128 | |
|
129 | 0 | if (pk_equal(packet + 1, dht_get_self_public_key(ping->dht))) { |
130 | 0 | return 1; |
131 | 0 | } |
132 | | |
133 | 0 | const uint8_t *shared_key = dht_get_shared_key_recv(dht, packet + 1); |
134 | |
|
135 | 0 | uint8_t ping_plain[PING_PLAIN_SIZE]; |
136 | | |
137 | | // Decrypt ping_id |
138 | 0 | const int rc = decrypt_data_symmetric(ping->mem, shared_key, |
139 | 0 | packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, |
140 | 0 | packet + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE, |
141 | 0 | PING_PLAIN_SIZE + CRYPTO_MAC_SIZE, |
142 | 0 | ping_plain); |
143 | |
|
144 | 0 | if (rc != sizeof(ping_plain)) { |
145 | 0 | return 1; |
146 | 0 | } |
147 | | |
148 | 0 | if (ping_plain[0] != NET_PACKET_PING_REQUEST) { |
149 | 0 | return 1; |
150 | 0 | } |
151 | | |
152 | 0 | uint64_t ping_id; |
153 | 0 | memcpy(&ping_id, ping_plain + 1, sizeof(ping_id)); |
154 | | // Send response |
155 | 0 | ping_send_response(ping, source, packet + 1, ping_id, shared_key); |
156 | 0 | ping_add(ping, packet + 1, source); |
157 | |
|
158 | 0 | return 0; |
159 | 0 | } |
160 | | |
161 | | static int handle_ping_response(void *_Nonnull object, const IP_Port *_Nonnull source, const uint8_t *_Nonnull packet, uint16_t length, void *_Nonnull userdata) |
162 | 0 | { |
163 | 0 | DHT *dht = (DHT *)object; |
164 | 0 | int rc; |
165 | |
|
166 | 0 | if (length != DHT_PING_SIZE) { |
167 | 0 | return 1; |
168 | 0 | } |
169 | | |
170 | 0 | Ping *ping = dht_get_ping(dht); |
171 | |
|
172 | 0 | if (pk_equal(packet + 1, dht_get_self_public_key(ping->dht))) { |
173 | 0 | return 1; |
174 | 0 | } |
175 | | |
176 | | // generate key to encrypt ping_id with recipient privkey |
177 | 0 | const uint8_t *shared_key = dht_get_shared_key_sent(ping->dht, packet + 1); |
178 | |
|
179 | 0 | uint8_t ping_plain[PING_PLAIN_SIZE]; |
180 | | // Decrypt ping_id |
181 | 0 | rc = decrypt_data_symmetric(ping->mem, shared_key, |
182 | 0 | packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, |
183 | 0 | packet + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE, |
184 | 0 | PING_PLAIN_SIZE + CRYPTO_MAC_SIZE, |
185 | 0 | ping_plain); |
186 | |
|
187 | 0 | if (rc != sizeof(ping_plain)) { |
188 | 0 | return 1; |
189 | 0 | } |
190 | | |
191 | 0 | if (ping_plain[0] != NET_PACKET_PING_RESPONSE) { |
192 | 0 | return 1; |
193 | 0 | } |
194 | | |
195 | 0 | uint64_t ping_id; |
196 | 0 | memcpy(&ping_id, ping_plain + 1, sizeof(ping_id)); |
197 | 0 | uint8_t data[PING_DATA_SIZE]; |
198 | |
|
199 | 0 | if (ping_array_check(ping->ping_array, ping->mono_time, data, sizeof(data), ping_id) != sizeof(data)) { |
200 | 0 | return 1; |
201 | 0 | } |
202 | | |
203 | 0 | if (!pk_equal(packet + 1, data)) { |
204 | 0 | return 1; |
205 | 0 | } |
206 | | |
207 | 0 | IP_Port ipp; |
208 | 0 | memcpy(&ipp, data + CRYPTO_PUBLIC_KEY_SIZE, sizeof(IP_Port)); |
209 | |
|
210 | 0 | if (!ipport_equal(&ipp, source)) { |
211 | 0 | return 1; |
212 | 0 | } |
213 | | |
214 | 0 | addto_lists(dht, source, packet + 1); |
215 | 0 | return 0; |
216 | 0 | } |
217 | | |
218 | | /** @brief Check if public_key with ip_port is in the list. |
219 | | * |
220 | | * return true if it is. |
221 | | * return false if it isn't. |
222 | | */ |
223 | | static bool in_list(const Client_data *_Nonnull list, uint16_t length, const Mono_Time *_Nonnull mono_time, const uint8_t *_Nonnull public_key, const IP_Port *_Nonnull ip_port) |
224 | 0 | { |
225 | 0 | for (unsigned int i = 0; i < length; ++i) { |
226 | 0 | if (pk_equal(list[i].public_key, public_key)) { |
227 | 0 | const IPPTsPng *ipptp; |
228 | |
|
229 | 0 | if (net_family_is_ipv4(ip_port->ip.family)) { |
230 | 0 | ipptp = &list[i].assoc4; |
231 | 0 | } else { |
232 | 0 | ipptp = &list[i].assoc6; |
233 | 0 | } |
234 | |
|
235 | 0 | if (!mono_time_is_timeout(mono_time, ipptp->timestamp, BAD_NODE_TIMEOUT) |
236 | 0 | && ipport_equal(&ipptp->ip_port, ip_port)) { |
237 | 0 | return true; |
238 | 0 | } |
239 | 0 | } |
240 | 0 | } |
241 | | |
242 | 0 | return false; |
243 | 0 | } |
244 | | |
245 | | /** @brief Add nodes to the to_ping list. |
246 | | * All nodes in this list are pinged every TIME_TO_PING seconds |
247 | | * and are then removed from the list. |
248 | | * If the list is full the nodes farthest from our public_key are replaced. |
249 | | * The purpose of this list is to enable quick integration of new nodes into the |
250 | | * network while preventing amplification attacks. |
251 | | * |
252 | | * @retval 0 if node was added. |
253 | | * @retval -1 if node was not added. |
254 | | */ |
255 | | int32_t ping_add(Ping *ping, const uint8_t *public_key, const IP_Port *ip_port) |
256 | 0 | { |
257 | 0 | if (!ip_isset(&ip_port->ip)) { |
258 | 0 | return -1; |
259 | 0 | } |
260 | | |
261 | 0 | if (!node_addable_to_close_list(ping->dht, public_key, ip_port)) { |
262 | 0 | return -1; |
263 | 0 | } |
264 | | |
265 | 0 | if (in_list(dht_get_close_clientlist(ping->dht), LCLIENT_LIST, ping->mono_time, public_key, ip_port)) { |
266 | 0 | return -1; |
267 | 0 | } |
268 | | |
269 | 0 | IP_Port temp; |
270 | |
|
271 | 0 | if (dht_getfriendip(ping->dht, public_key, &temp) == 0) { |
272 | 0 | ping_send_request(ping, ip_port, public_key); |
273 | 0 | return -1; |
274 | 0 | } |
275 | | |
276 | 0 | for (unsigned int i = 0; i < MAX_TO_PING; ++i) { |
277 | 0 | if (!ip_isset(&ping->to_ping[i].ip_port.ip)) { |
278 | 0 | memcpy(ping->to_ping[i].public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); |
279 | 0 | ipport_copy(&ping->to_ping[i].ip_port, ip_port); |
280 | 0 | return 0; |
281 | 0 | } |
282 | | |
283 | 0 | if (pk_equal(ping->to_ping[i].public_key, public_key)) { |
284 | 0 | return -1; |
285 | 0 | } |
286 | 0 | } |
287 | | |
288 | 0 | if (add_to_list(ping->to_ping, MAX_TO_PING, public_key, ip_port, dht_get_self_public_key(ping->dht))) { |
289 | 0 | return 0; |
290 | 0 | } |
291 | | |
292 | 0 | return -1; |
293 | 0 | } |
294 | | |
295 | | /** @brief Ping all the valid nodes in the to_ping list every TIME_TO_PING seconds. |
296 | | * This function must be run at least once every TIME_TO_PING seconds. |
297 | | */ |
298 | | void ping_iterate(Ping *ping) |
299 | 0 | { |
300 | 0 | if (!mono_time_is_timeout(ping->mono_time, ping->last_to_ping, TIME_TO_PING)) { |
301 | 0 | return; |
302 | 0 | } |
303 | | |
304 | 0 | if (!ip_isset(&ping->to_ping[0].ip_port.ip)) { |
305 | 0 | return; |
306 | 0 | } |
307 | | |
308 | 0 | unsigned int i; |
309 | |
|
310 | 0 | for (i = 0; i < MAX_TO_PING; ++i) { |
311 | 0 | if (!ip_isset(&ping->to_ping[i].ip_port.ip)) { |
312 | 0 | break; |
313 | 0 | } |
314 | | |
315 | 0 | if (!node_addable_to_close_list(ping->dht, ping->to_ping[i].public_key, &ping->to_ping[i].ip_port)) { |
316 | 0 | continue; |
317 | 0 | } |
318 | | |
319 | 0 | ping_send_request(ping, &ping->to_ping[i].ip_port, ping->to_ping[i].public_key); |
320 | 0 | ip_reset(&ping->to_ping[i].ip_port.ip); |
321 | 0 | } |
322 | |
|
323 | 0 | if (i != 0) { |
324 | 0 | ping->last_to_ping = mono_time_get(ping->mono_time); |
325 | 0 | } |
326 | 0 | } |
327 | | |
328 | | Ping *ping_new(const Memory *mem, const Mono_Time *mono_time, const Random *rng, DHT *dht, Networking_Core *net) |
329 | 1.39k | { |
330 | 1.39k | Ping *ping = (Ping *)mem_alloc(mem, sizeof(Ping)); |
331 | | |
332 | 1.39k | if (ping == nullptr) { |
333 | 2 | return nullptr; |
334 | 2 | } |
335 | | |
336 | 1.38k | ping->ping_array = ping_array_new(mem, PING_NUM_MAX, PING_TIMEOUT); |
337 | | |
338 | 1.38k | if (ping->ping_array == nullptr) { |
339 | 4 | mem_delete(mem, ping); |
340 | 4 | return nullptr; |
341 | 4 | } |
342 | | |
343 | 1.38k | ping->mono_time = mono_time; |
344 | 1.38k | ping->rng = rng; |
345 | 1.38k | ping->mem = mem; |
346 | 1.38k | ping->dht = dht; |
347 | 1.38k | ping->net = net; |
348 | 1.38k | networking_registerhandler(ping->net, NET_PACKET_PING_REQUEST, &handle_ping_request, dht); |
349 | 1.38k | networking_registerhandler(ping->net, NET_PACKET_PING_RESPONSE, &handle_ping_response, dht); |
350 | | |
351 | 1.38k | return ping; |
352 | 1.38k | } |
353 | | |
354 | | void ping_kill(const Memory *mem, Ping *ping) |
355 | 1.39k | { |
356 | 1.39k | if (ping == nullptr) { |
357 | 6 | return; |
358 | 6 | } |
359 | | |
360 | 1.38k | networking_registerhandler(ping->net, NET_PACKET_PING_REQUEST, nullptr, nullptr); |
361 | 1.38k | networking_registerhandler(ping->net, NET_PACKET_PING_RESPONSE, nullptr, nullptr); |
362 | 1.38k | ping_array_kill(ping->ping_array); |
363 | | |
364 | 1.38k | mem_delete(mem, ping); |
365 | 1.38k | } |