Coverage Report

Created: 2026-10-24 08:16

/work/toxcore/ping.c
Line
Count
Source (jump to first uncovered line)
1
/* SPDX-License-Identifier: GPL-3.0-or-later
2
 * Copyright © 2016-2026 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
2.06k
#define PING_NUM_MAX 512
24
25
/** Maximum newly announced nodes to ping per TIME_TO_PING seconds. */
26
179k
#define MAX_TO_PING 32
27
28
/** Ping newly announced nodes to ping per TIME_TO_PING seconds*/
29
6.55k
#define TIME_TO_PING 2
30
31
struct Ping {
32
    const Mono_Time *_Nonnull mono_time;
33
    const Random *_Nonnull rng;
34
    const Memory *_Nonnull mem;
35
    DHT *_Nonnull dht;
36
    Networking_Core *_Nonnull net;
37
38
    Ping_Array  *_Nonnull ping_array;
39
    Node_format to_ping[MAX_TO_PING];
40
    uint64_t    last_to_ping;
41
};
42
43
29.2k
#define PING_PLAIN_SIZE (1 + sizeof(uint64_t))
44
9.76k
#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
4.88k
{
49
4.88k
    uint8_t   pk[DHT_PING_SIZE];
50
4.88k
    int       rc;
51
4.88k
    uint64_t  ping_id;
52
53
4.88k
    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
4.88k
    const uint8_t *shared_key = dht_get_shared_key_sent(ping->dht, public_key);
59
    // Generate random ping_id.
60
4.88k
    uint8_t data[PING_DATA_SIZE];
61
4.88k
    pk_copy(data, public_key);
62
4.88k
    memcpy(data + CRYPTO_PUBLIC_KEY_SIZE, ipp, sizeof(IP_Port));
63
4.88k
    ping_id = ping_array_add(ping->ping_array, ping->mono_time, ping->rng, data, sizeof(data));
64
65
4.88k
    if (ping_id == 0) {
66
0
        return;
67
0
    }
68
69
4.88k
    uint8_t ping_plain[PING_PLAIN_SIZE];
70
4.88k
    ping_plain[0] = NET_PACKET_PING_REQUEST;
71
4.88k
    memcpy(ping_plain + 1, &ping_id, sizeof(ping_id));
72
73
4.88k
    pk[0] = NET_PACKET_PING_REQUEST;
74
4.88k
    pk_copy(pk + 1, dht_get_self_public_key(ping->dht));     // Our pubkey
75
4.88k
    random_nonce(ping->rng, pk + 1 + CRYPTO_PUBLIC_KEY_SIZE); // Generate new nonce
76
77
4.88k
    rc = encrypt_data_symmetric(ping->mem, shared_key,
78
4.88k
                                pk + 1 + CRYPTO_PUBLIC_KEY_SIZE,
79
4.88k
                                ping_plain, sizeof(ping_plain),
80
4.88k
                                pk + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE);
81
82
4.88k
    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
4.88k
    sendpacket(ping->net, ipp, pk, sizeof(pk));
88
4.88k
}
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
4.88k
{
92
4.88k
    uint8_t pk[DHT_PING_SIZE];
93
94
4.88k
    if (pk_equal(public_key, dht_get_self_public_key(ping->dht))) {
95
0
        return 1;
96
0
    }
97
98
4.88k
    uint8_t ping_plain[PING_PLAIN_SIZE];
99
4.88k
    ping_plain[0] = NET_PACKET_PING_RESPONSE;
100
4.88k
    memcpy(ping_plain + 1, &ping_id, sizeof(ping_id));
101
102
4.88k
    pk[0] = NET_PACKET_PING_RESPONSE;
103
4.88k
    pk_copy(pk + 1, dht_get_self_public_key(ping->dht));     // Our pubkey
104
4.88k
    random_nonce(ping->rng, pk + 1 + CRYPTO_PUBLIC_KEY_SIZE); // Generate new nonce
105
106
    // Encrypt ping_id using recipient privkey
107
4.88k
    const int rc = encrypt_data_symmetric(ping->mem, shared_encryption_key,
108
4.88k
                                          pk + 1 + CRYPTO_PUBLIC_KEY_SIZE,
109
4.88k
                                          ping_plain, sizeof(ping_plain),
110
4.88k
                                          pk + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE);
111
112
4.88k
    if (rc != PING_PLAIN_SIZE + CRYPTO_MAC_SIZE) {
113
0
        return 1;
114
0
    }
115
116
4.88k
    return sendpacket(ping->net, ipp, pk, sizeof(pk));
117
4.88k
}
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
4.88k
{
121
4.88k
    DHT *dht = (DHT *)object;
122
123
4.88k
    if (length != DHT_PING_SIZE) {
124
0
        return 1;
125
0
    }
126
127
4.88k
    Ping *ping = dht_get_ping(dht);
128
129
4.88k
    if (pk_equal(packet + 1, dht_get_self_public_key(ping->dht))) {
130
0
        return 1;
131
0
    }
132
133
4.88k
    const uint8_t *shared_key = dht_get_shared_key_recv(dht, packet + 1);
134
135
4.88k
    uint8_t ping_plain[PING_PLAIN_SIZE];
136
137
    // Decrypt ping_id
138
4.88k
    const int rc = decrypt_data_symmetric(ping->mem, shared_key,
139
4.88k
                                          packet + 1 + CRYPTO_PUBLIC_KEY_SIZE,
140
4.88k
                                          packet + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE,
141
4.88k
                                          PING_PLAIN_SIZE + CRYPTO_MAC_SIZE,
142
4.88k
                                          ping_plain);
143
144
4.88k
    if (rc != sizeof(ping_plain)) {
145
0
        return 1;
146
0
    }
147
148
4.88k
    if (ping_plain[0] != NET_PACKET_PING_REQUEST) {
149
0
        return 1;
150
0
    }
151
152
4.88k
    uint64_t ping_id;
153
4.88k
    memcpy(&ping_id, ping_plain + 1, sizeof(ping_id));
154
    // Send response
155
4.88k
    ping_send_response(ping, source, packet + 1, ping_id, shared_key);
156
4.88k
    ping_add(ping, packet + 1, source);
157
158
4.88k
    return 0;
159
4.88k
}
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
4.88k
{
163
4.88k
    DHT      *dht = (DHT *)object;
164
4.88k
    int       rc;
165
166
4.88k
    if (length != DHT_PING_SIZE) {
167
0
        return 1;
168
0
    }
169
170
4.88k
    Ping *ping = dht_get_ping(dht);
171
172
4.88k
    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
4.88k
    const uint8_t *shared_key = dht_get_shared_key_sent(ping->dht, packet + 1);
178
179
4.88k
    uint8_t ping_plain[PING_PLAIN_SIZE];
180
    // Decrypt ping_id
181
4.88k
    rc = decrypt_data_symmetric(ping->mem, shared_key,
182
4.88k
                                packet + 1 + CRYPTO_PUBLIC_KEY_SIZE,
183
4.88k
                                packet + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE,
184
4.88k
                                PING_PLAIN_SIZE + CRYPTO_MAC_SIZE,
185
4.88k
                                ping_plain);
186
187
4.88k
    if (rc != sizeof(ping_plain)) {
188
0
        return 1;
189
0
    }
190
191
4.88k
    if (ping_plain[0] != NET_PACKET_PING_RESPONSE) {
192
0
        return 1;
193
0
    }
194
195
4.88k
    uint64_t   ping_id;
196
4.88k
    memcpy(&ping_id, ping_plain + 1, sizeof(ping_id));
197
4.88k
    uint8_t data[PING_DATA_SIZE];
198
199
4.88k
    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
4.88k
    if (!pk_equal(packet + 1, data)) {
204
0
        return 1;
205
0
    }
206
207
4.88k
    IP_Port ipp;
208
4.88k
    memcpy(&ipp, data + CRYPTO_PUBLIC_KEY_SIZE, sizeof(IP_Port));
209
210
4.88k
    if (!ipport_equal(&ipp, source)) {
211
0
        return 1;
212
0
    }
213
214
4.88k
    addto_lists(dht, source, packet + 1);
215
4.88k
    return 0;
216
4.88k
}
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
49.9k
{
225
22.5M
    for (unsigned int i = 0; i < length; ++i) {
226
22.5M
        if (pk_equal(list[i].public_key, public_key)) {
227
28.2k
            const IPPTsPng *ipptp;
228
229
28.2k
            if (net_family_is_ipv4(ip_port->ip.family)) {
230
28.2k
                ipptp = &list[i].assoc4;
231
18.4E
            } else {
232
18.4E
                ipptp = &list[i].assoc6;
233
18.4E
            }
234
235
28.2k
            if (!mono_time_is_timeout(mono_time, ipptp->timestamp, BAD_NODE_TIMEOUT)
236
28.2k
                    && ipport_equal(&ipptp->ip_port, ip_port)) {
237
28.2k
                return true;
238
28.2k
            }
239
28.2k
        }
240
22.5M
    }
241
242
21.6k
    return false;
243
49.9k
}
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
69.1k
{
257
69.1k
    if (!ip_isset(&ip_port->ip)) {
258
0
        return -1;
259
0
    }
260
261
69.1k
    if (!node_addable_to_close_list(ping->dht, public_key, ip_port)) {
262
19.1k
        return -1;
263
19.1k
    }
264
265
49.9k
    if (in_list(dht_get_close_clientlist(ping->dht), LCLIENT_LIST, ping->mono_time, public_key, ip_port)) {
266
28.2k
        return -1;
267
28.2k
    }
268
269
21.6k
    IP_Port temp;
270
271
21.6k
    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
171k
    for (unsigned int i = 0; i < MAX_TO_PING; ++i) {
277
171k
        if (!ip_isset(&ping->to_ping[i].ip_port.ip)) {
278
5.44k
            memcpy(ping->to_ping[i].public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE);
279
5.44k
            ipport_copy(&ping->to_ping[i].ip_port, ip_port);
280
5.44k
            return 0;
281
5.44k
        }
282
283
166k
        if (pk_equal(ping->to_ping[i].public_key, public_key)) {
284
16.2k
            return -1;
285
16.2k
        }
286
166k
    }
287
288
6
    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
6
    return -1;
293
6
}
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
6.55k
{
300
6.55k
    if (!mono_time_is_timeout(ping->mono_time, ping->last_to_ping, TIME_TO_PING)) {
301
1.42k
        return;
302
1.42k
    }
303
304
5.13k
    if (!ip_isset(&ping->to_ping[0].ip_port.ip)) {
305
3.57k
        return;
306
3.57k
    }
307
308
1.56k
    unsigned int i;
309
310
7.69k
    for (i = 0; i < MAX_TO_PING; ++i) {
311
7.69k
        if (!ip_isset(&ping->to_ping[i].ip_port.ip)) {
312
1.56k
            break;
313
1.56k
        }
314
315
6.12k
        if (!node_addable_to_close_list(ping->dht, ping->to_ping[i].public_key, &ping->to_ping[i].ip_port)) {
316
1.24k
            continue;
317
1.24k
        }
318
319
4.88k
        ping_send_request(ping, &ping->to_ping[i].ip_port, ping->to_ping[i].public_key);
320
4.88k
        ip_reset(&ping->to_ping[i].ip_port.ip);
321
4.88k
    }
322
323
1.56k
    if (i != 0) {
324
1.56k
        ping->last_to_ping = mono_time_get(ping->mono_time);
325
1.56k
    }
326
1.56k
}
327
328
Ping *ping_new(const Memory *mem, const Mono_Time *mono_time, const Random *rng, DHT *dht, Networking_Core *net)
329
2.06k
{
330
2.06k
    Ping *ping = (Ping *)mem_alloc(mem, sizeof(Ping));
331
332
2.06k
    if (ping == nullptr) {
333
4
        return nullptr;
334
4
    }
335
336
2.06k
    Ping_Array *const ping_array = ping_array_new(mem, PING_NUM_MAX, PING_TIMEOUT);
337
338
2.06k
    if (ping_array == nullptr) {
339
8
        mem_delete(mem, ping);
340
8
        return nullptr;
341
8
    }
342
2.05k
    ping->ping_array = ping_array;
343
344
2.05k
    ping->mono_time = mono_time;
345
2.05k
    ping->rng = rng;
346
2.05k
    ping->mem = mem;
347
2.05k
    ping->dht = dht;
348
2.05k
    ping->net = net;
349
2.05k
    networking_registerhandler(ping->net, NET_PACKET_PING_REQUEST, &handle_ping_request, dht);
350
2.05k
    networking_registerhandler(ping->net, NET_PACKET_PING_RESPONSE, &handle_ping_response, dht);
351
352
2.05k
    return ping;
353
2.06k
}
354
355
void ping_kill(const Memory *mem, Ping *ping)
356
2.04k
{
357
2.04k
    if (ping == nullptr) {
358
12
        return;
359
12
    }
360
361
2.03k
    networking_registerhandler(ping->net, NET_PACKET_PING_REQUEST, nullptr, nullptr);
362
2.03k
    networking_registerhandler(ping->net, NET_PACKET_PING_RESPONSE, nullptr, nullptr);
363
2.03k
    ping_array_kill(ping->ping_array);
364
365
2.03k
    mem_delete(mem, ping);
366
2.03k
}