Coverage Report

Created: 2025-08-05 10:35

/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.99k
#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
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
0
{
91
0
    uint8_t pk[DHT_PING_SIZE];
92
93
0
    if (pk_equal(public_key, dht_get_self_public_key(ping->dht))) {
94
0
        return 1;
95
0
    }
96
97
0
    uint8_t ping_plain[PING_PLAIN_SIZE];
98
0
    ping_plain[0] = NET_PACKET_PING_RESPONSE;
99
0
    memcpy(ping_plain + 1, &ping_id, sizeof(ping_id));
100
101
0
    pk[0] = NET_PACKET_PING_RESPONSE;
102
0
    pk_copy(pk + 1, dht_get_self_public_key(ping->dht));     // Our pubkey
103
0
    random_nonce(ping->rng, pk + 1 + CRYPTO_PUBLIC_KEY_SIZE); // Generate new nonce
104
105
    // Encrypt ping_id using recipient privkey
106
0
    const int rc = encrypt_data_symmetric(ping->mem, shared_encryption_key,
107
0
                                          pk + 1 + CRYPTO_PUBLIC_KEY_SIZE,
108
0
                                          ping_plain, sizeof(ping_plain),
109
0
                                          pk + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE);
110
111
0
    if (rc != PING_PLAIN_SIZE + CRYPTO_MAC_SIZE) {
112
0
        return 1;
113
0
    }
114
115
0
    return sendpacket(dht_get_net(ping->dht), ipp, pk, sizeof(pk));
116
0
}
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
0
{
120
0
    DHT *dht = (DHT *)object;
121
122
0
    if (length != DHT_PING_SIZE) {
123
0
        return 1;
124
0
    }
125
126
0
    Ping *ping = dht_get_ping(dht);
127
128
0
    if (pk_equal(packet + 1, dht_get_self_public_key(ping->dht))) {
129
0
        return 1;
130
0
    }
131
132
0
    const uint8_t *shared_key = dht_get_shared_key_recv(dht, packet + 1);
133
134
0
    uint8_t ping_plain[PING_PLAIN_SIZE];
135
136
    // Decrypt ping_id
137
0
    const int rc = decrypt_data_symmetric(ping->mem, shared_key,
138
0
                                          packet + 1 + CRYPTO_PUBLIC_KEY_SIZE,
139
0
                                          packet + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE,
140
0
                                          PING_PLAIN_SIZE + CRYPTO_MAC_SIZE,
141
0
                                          ping_plain);
142
143
0
    if (rc != sizeof(ping_plain)) {
144
0
        return 1;
145
0
    }
146
147
0
    if (ping_plain[0] != NET_PACKET_PING_REQUEST) {
148
0
        return 1;
149
0
    }
150
151
0
    uint64_t ping_id;
152
0
    memcpy(&ping_id, ping_plain + 1, sizeof(ping_id));
153
    // Send response
154
0
    ping_send_response(ping, source, packet + 1, ping_id, shared_key);
155
0
    ping_add(ping, packet + 1, source);
156
157
0
    return 0;
158
0
}
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
0
{
162
0
    DHT      *dht = (DHT *)object;
163
0
    int       rc;
164
165
0
    if (length != DHT_PING_SIZE) {
166
0
        return 1;
167
0
    }
168
169
0
    Ping *ping = dht_get_ping(dht);
170
171
0
    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
0
    const uint8_t *shared_key = dht_get_shared_key_sent(ping->dht, packet + 1);
177
178
0
    uint8_t ping_plain[PING_PLAIN_SIZE];
179
    // Decrypt ping_id
180
0
    rc = decrypt_data_symmetric(ping->mem, shared_key,
181
0
                                packet + 1 + CRYPTO_PUBLIC_KEY_SIZE,
182
0
                                packet + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE,
183
0
                                PING_PLAIN_SIZE + CRYPTO_MAC_SIZE,
184
0
                                ping_plain);
185
186
0
    if (rc != sizeof(ping_plain)) {
187
0
        return 1;
188
0
    }
189
190
0
    if (ping_plain[0] != NET_PACKET_PING_RESPONSE) {
191
0
        return 1;
192
0
    }
193
194
0
    uint64_t   ping_id;
195
0
    memcpy(&ping_id, ping_plain + 1, sizeof(ping_id));
196
0
    uint8_t data[PING_DATA_SIZE];
197
198
0
    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
0
    if (!pk_equal(packet + 1, data)) {
203
0
        return 1;
204
0
    }
205
206
0
    IP_Port ipp;
207
0
    memcpy(&ipp, data + CRYPTO_PUBLIC_KEY_SIZE, sizeof(IP_Port));
208
209
0
    if (!ipport_equal(&ipp, source)) {
210
0
        return 1;
211
0
    }
212
213
0
    addto_lists(dht, source, packet + 1);
214
0
    return 0;
215
0
}
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
0
{
224
0
    for (unsigned int i = 0; i < length; ++i) {
225
0
        if (pk_equal(list[i].public_key, public_key)) {
226
0
            const IPPTsPng *ipptp;
227
228
0
            if (net_family_is_ipv4(ip_port->ip.family)) {
229
0
                ipptp = &list[i].assoc4;
230
0
            } else {
231
0
                ipptp = &list[i].assoc6;
232
0
            }
233
234
0
            if (!mono_time_is_timeout(mono_time, ipptp->timestamp, BAD_NODE_TIMEOUT)
235
0
                    && ipport_equal(&ipptp->ip_port, ip_port)) {
236
0
                return true;
237
0
            }
238
0
        }
239
0
    }
240
241
0
    return false;
242
0
}
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
0
{
256
0
    if (!ip_isset(&ip_port->ip)) {
257
0
        return -1;
258
0
    }
259
260
0
    if (!node_addable_to_close_list(ping->dht, public_key, ip_port)) {
261
0
        return -1;
262
0
    }
263
264
0
    if (in_list(dht_get_close_clientlist(ping->dht), LCLIENT_LIST, ping->mono_time, public_key, ip_port)) {
265
0
        return -1;
266
0
    }
267
268
0
    IP_Port temp;
269
270
0
    if (dht_getfriendip(ping->dht, public_key, &temp) == 0) {
271
0
        ping_send_request(ping, ip_port, public_key);
272
0
        return -1;
273
0
    }
274
275
0
    for (unsigned int i = 0; i < MAX_TO_PING; ++i) {
276
0
        if (!ip_isset(&ping->to_ping[i].ip_port.ip)) {
277
0
            memcpy(ping->to_ping[i].public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE);
278
0
            ipport_copy(&ping->to_ping[i].ip_port, ip_port);
279
0
            return 0;
280
0
        }
281
282
0
        if (pk_equal(ping->to_ping[i].public_key, public_key)) {
283
0
            return -1;
284
0
        }
285
0
    }
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
0
{
299
0
    if (!mono_time_is_timeout(ping->mono_time, ping->last_to_ping, TIME_TO_PING)) {
300
0
        return;
301
0
    }
302
303
0
    if (!ip_isset(&ping->to_ping[0].ip_port.ip)) {
304
0
        return;
305
0
    }
306
307
0
    unsigned int i;
308
309
0
    for (i = 0; i < MAX_TO_PING; ++i) {
310
0
        if (!ip_isset(&ping->to_ping[i].ip_port.ip)) {
311
0
            break;
312
0
        }
313
314
0
        if (!node_addable_to_close_list(ping->dht, ping->to_ping[i].public_key, &ping->to_ping[i].ip_port)) {
315
0
            continue;
316
0
        }
317
318
0
        ping_send_request(ping, &ping->to_ping[i].ip_port, ping->to_ping[i].public_key);
319
0
        ip_reset(&ping->to_ping[i].ip_port.ip);
320
0
    }
321
322
0
    if (i != 0) {
323
0
        ping->last_to_ping = mono_time_get(ping->mono_time);
324
0
    }
325
0
}
326
327
Ping *ping_new(const Memory *mem, const Mono_Time *mono_time, const Random *rng, DHT *dht)
328
1.99k
{
329
1.99k
    Ping *ping = (Ping *)mem_alloc(mem, sizeof(Ping));
330
331
1.99k
    if (ping == nullptr) {
332
2
        return nullptr;
333
2
    }
334
335
1.99k
    ping->ping_array = ping_array_new(mem, PING_NUM_MAX, PING_TIMEOUT);
336
337
1.99k
    if (ping->ping_array == nullptr) {
338
4
        mem_delete(mem, ping);
339
4
        return nullptr;
340
4
    }
341
342
1.98k
    ping->mono_time = mono_time;
343
1.98k
    ping->rng = rng;
344
1.98k
    ping->mem = mem;
345
1.98k
    ping->dht = dht;
346
1.98k
    networking_registerhandler(dht_get_net(ping->dht), NET_PACKET_PING_REQUEST, &handle_ping_request, dht);
347
1.98k
    networking_registerhandler(dht_get_net(ping->dht), NET_PACKET_PING_RESPONSE, &handle_ping_response, dht);
348
349
1.98k
    return ping;
350
1.99k
}
351
352
void ping_kill(const Memory *mem, Ping *ping)
353
1.99k
{
354
1.99k
    if (ping == nullptr) {
355
6
        return;
356
6
    }
357
358
1.98k
    networking_registerhandler(dht_get_net(ping->dht), NET_PACKET_PING_REQUEST, nullptr, nullptr);
359
1.98k
    networking_registerhandler(dht_get_net(ping->dht), NET_PACKET_PING_RESPONSE, nullptr, nullptr);
360
1.98k
    ping_array_kill(ping->ping_array);
361
362
1.98k
    mem_delete(mem, ping);
363
1.98k
}