Coverage Report

Created: 2025-10-08 19:34

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