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