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