-
Notifications
You must be signed in to change notification settings - Fork 74
/
kad.c
643 lines (541 loc) · 16.5 KB
/
kad.c
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
#define _GNU_SOURCE
#include <sys/time.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include "log.h"
#include "main.h"
#include "utils.h"
#include "conf.h"
#include "utils.h"
#include "net.h"
#include "searches.h"
#include "announces.h"
#ifdef BOB
#include "ext-bob.h"
#endif
// include dht.c instead of dht.h to access private vars
#include "dht.c"
/*
* The interface that is used to interact with the DHT.
*/
// Next time to do DHT maintenance
static time_t g_dht_maintenance = 0;
static int g_dht_socket4 = -1;
static int g_dht_socket6 = -1;
/*
* Put an address and port into a sockaddr_storages struct.
* Both addr and port are in network byte order.
*/
void to_addr(IP *out_addr, const void *in_addr, size_t len, uint16_t port)
{
memset(out_addr, '\0', sizeof(IP));
if (len == 4) {
IP4 *a = (IP4 *) out_addr;
a->sin_family = AF_INET;
a->sin_port = port;
memcpy(&a->sin_addr.s_addr, in_addr, 4);
}
if (len == 16) {
IP6 *a = (IP6 *) out_addr;
a->sin6_family = AF_INET6;
a->sin6_port = port;
memcpy(&a->sin6_addr.s6_addr, in_addr, 16);
}
}
typedef struct {
uint8_t addr[16];
uint16_t port;
} dht_addr6_t;
typedef struct {
uint8_t addr[4];
uint16_t port;
} dht_addr4_t;
// This callback is called when a search result arrives or a search completes
void dht_callback_func(void *closure, int event, const uint8_t *info_hash, const void *data, size_t data_len)
{
struct search_t *search;
dht_addr4_t *data4;
dht_addr6_t *data6;
IP addr;
size_t i;
search = searches_find_by_id(info_hash);
if (search == NULL) {
return;
}
switch (event) {
case DHT_EVENT_VALUES:
data4 = (dht_addr4_t *) data;
for(i = 0; i < (data_len / sizeof(dht_addr4_t)); ++i) {
to_addr(&addr, &data4[i].addr, 4, data4[i].port);
searches_add_addr(search, &addr);
}
break;
case DHT_EVENT_VALUES6:
data6 = (dht_addr6_t *) data;
for(i = 0; i < (data_len / sizeof(dht_addr6_t)); ++i) {
to_addr(&addr, &data6[i].addr, 16, data6[i].port);
searches_add_addr(search, &addr);
}
break;
case DHT_EVENT_SEARCH_DONE:
case DHT_EVENT_SEARCH_DONE6:
// Ignore..
break;
}
}
#if 0
/*
* Lookup in values we announce ourselves.
* Useful for networks of only one node, also faster.
*/
void kad_lookup_own_announcements(struct search_t *search)
{
struct announcement_t* value;
int af;
IP addr;
// 127.0.0.1
const uint32_t inaddr_loopback = htonl(INADDR_LOOPBACK);
af = gconf->af;
value = announces_find(search->id);
if (value) {
if (af == AF_UNSPEC || af == AF_INET6) {
to_addr(&addr, &in6addr_loopback, 16, htons(value->port)); // ::1
log_debug("KAD: Address found in own announcements: %s", str_addr(&addr));
searches_add_addr(search, &addr);
}
if (af == AF_UNSPEC || af == AF_INET) {
to_addr(&addr, &inaddr_loopback, 4, htons(value->port)); // 127.0.0.1
log_debug("KAD: Address found in own announcements: %s", str_addr(&addr));
searches_add_addr(search, &addr);
}
}
}
#endif
// Handle incoming packets and pass them to the DHT code
void dht_handler(int rc, int sock)
{
uint8_t buf[1500];
uint32_t buflen;
IP from;
socklen_t fromlen;
time_t time_wait = 0;
if (rc > 0) {
// Check which socket received the data
fromlen = sizeof(from);
buflen = recvfrom(sock, buf, sizeof(buf) - 1, 0, (struct sockaddr*) &from, &fromlen);
if (buflen <= 0 || buflen >= sizeof(buf)) {
return;
}
// The DHT code expects the message to be null-terminated.
buf[buflen] = '\0';
} else {
buflen = 0;
}
#ifdef BOB
// Hook up BOB extension on the DHT socket
if (bob_handler(sock, buf, buflen, &from)) {
return;
}
#endif
if (buflen > 0) {
// Handle incoming data
rc = dht_periodic(buf, buflen, (struct sockaddr*) &from, fromlen, &time_wait, dht_callback_func, NULL);
if (rc < 0 && errno != EINTR) {
if (rc == EINVAL || rc == EFAULT) {
log_error("KAD: Error calling dht_periodic.");
exit(1);
}
g_dht_maintenance = time_now_sec() + 1;
} else {
g_dht_maintenance = time_now_sec() + time_wait;
}
} else if (g_dht_maintenance <= time_now_sec()) {
// Do a maintenance call
rc = dht_periodic(NULL, 0, NULL, 0, &time_wait, dht_callback_func, NULL);
// Wait for the next maintenance call
g_dht_maintenance = time_now_sec() + time_wait;
log_debug("KAD: Next maintenance call in %u seconds.", (unsigned int) time_wait);
} else {
rc = 0;
}
if (rc < 0) {
if (errno == EINTR) {
return;
} else if (rc == EINVAL || rc == EFAULT) {
log_error("KAD: Error using select: %s", strerror(errno));
return;
} else {
g_dht_maintenance = time_now_sec() + 1;
}
}
}
/*
* Kademlia needs dht_blacklisted/dht_hash/dht_random_bytes functions to be present.
*/
int dht_sendto(int sockfd, const void *buf, int len, int flags, const struct sockaddr *to, int tolen)
{
return sendto(sockfd, buf, len, flags, to, tolen);
}
int dht_blacklisted(const struct sockaddr *sa, int salen)
{
return 0;
}
// Hashing for the DHT - implementation does not matter for interoperability
void dht_hash(void *hash_return, int hash_size,
const void *v1, int len1,
const void *v2, int len2,
const void *v3, int len3)
{
union {
uint8_t data[8];
uint16_t num4[4];
uint32_t num2[2];
uint64_t num1[1];
} hash;
assert(len1 == 8);
memcpy(&hash.data, v1, 8);
assert(len2 == 4 || len2 == 16);
if (len2 == 4) {
const uint32_t d2 = *((uint32_t*) v2);
hash.num2[0] ^= d2;
hash.num2[1] ^= d2;
} else {
hash.num1[0] ^= *((uint64_t*) v2);
hash.num1[0] ^= *((uint64_t*) v2 + 8);
}
assert(len3 == 2);
const uint16_t d3 = *((uint16_t*) v3);
hash.num4[0] ^= d3;
hash.num4[1] ^= d3;
hash.num4[2] ^= d3;
hash.num4[3] ^= d3;
assert(hash_size == 8);
memcpy(hash_return, &hash.data, 8);
}
int dht_random_bytes(void *buf, size_t size)
{
return bytes_random(buf, size);
}
int kad_setup(void)
{
uint8_t node_id[SHA1_BIN_LENGTH];
#ifdef DEBUG
// Let the DHT output debug text
dht_debug = stdout;
#endif
bytes_random(node_id, SHA1_BIN_LENGTH);
g_dht_socket4 = net_bind("KAD", "0.0.0.0", gconf->dht_port, gconf->dht_ifname, IPPROTO_UDP);
g_dht_socket6 = net_bind("KAD", "::", gconf->dht_port, gconf->dht_ifname, IPPROTO_UDP);
if (g_dht_socket4 >= 0) {
net_add_handler(g_dht_socket4, &dht_handler);
}
if (g_dht_socket6 >= 0) {
net_add_handler(g_dht_socket6, &dht_handler);
}
if (g_dht_socket4 < 0 && g_dht_socket6 < 0) {
return EXIT_FAILURE;
}
// Init the DHT. Also set the sockets into non-blocking mode.
if (dht_init(g_dht_socket4, g_dht_socket6, node_id, (uint8_t*) "KN\0\0") < 0) {
log_error("KAD: Failed to initialize the DHT.");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
void kad_free(void)
{
dht_uninit();
}
int kad_count_bucket(const struct bucket *bucket, int good)
{
struct node *node;
int count;
count = 0;
while (bucket) {
if (good) {
node = bucket->nodes;
while (node) {
count += node_good(node) ? 1 : 0;
node = node->next;
}
} else {
count += bucket->count;
}
bucket = bucket->next;
}
return count;
}
int kad_count_nodes(bool good)
{
// Count nodes in IPv4 and IPv6 buckets
return kad_count_bucket(buckets, good) + kad_count_bucket(buckets6, good);
}
void kad_status(FILE *fp)
{
struct storage *strg = storage;
struct search *srch = searches;
struct announcement_t *announcement = announces_get();
int numsearches4_active = 0;
int numsearches4_done = 0;
int numsearches6_active = 0;
int numsearches6_done = 0;
int numstorage = 0;
int numstorage_peers = 0;
int numannounces = 0;
// Count searches
while (srch) {
if (srch->af == AF_INET6) {
if (srch->done) {
numsearches6_done += 1;
} else {
numsearches6_active += 1;
}
} else {
if (srch->done) {
numsearches4_done += 1;
} else {
numsearches4_active += 1;
}
}
srch = srch->next;
}
// Count storage and peers
while (strg) {
numstorage_peers += strg->numpeers;
numstorage += 1;
strg = strg->next;
}
while (announcement) {
numannounces += 1;
announcement = announcement->next;
}
// Use dht data structure!
int nodes4 = kad_count_bucket(buckets, false);
int nodes6 = kad_count_bucket(buckets6, false);
int nodes4_good = kad_count_bucket(buckets, true);
int nodes6_good = kad_count_bucket(buckets6, true);
fprintf(
fp,
"%s\n"
"DHT id: %s\n"
"DHT listen on: %s / device: %s / port: %d\n"
"DHT nodes: %d IPv4 (%d good), %d IPv6 (%d good)\n"
"DHT storage: %d entries with %d addresses\n"
"DHT searches: %d IPv4 (%d done), %d IPv6 active (%d done)\n"
"DHT announcements: %d\n"
"DHT blocklist: %d\n",
kadnode_version_str,
str_id(myid),
str_af(gconf->af), gconf->dht_ifname ? gconf->dht_ifname : "<any>", gconf->dht_port,
nodes4, nodes4_good, nodes6, nodes6_good,
numstorage, numstorage_peers,
numsearches4_active, numsearches4_done, numsearches6_active, numsearches6_done,
numannounces,
(next_blacklisted % DHT_MAX_BLACKLISTED)
);
}
bool kad_ping(const IP* addr)
{
return dht_ping_node((struct sockaddr *)addr, addr_len(addr)) >= 0;
}
/*
* Find nodes that are near the given id and announce to them
* that this node can satisfy the given id on the given port.
*/
bool kad_announce_once(const uint8_t id[], int port)
{
if (port < 1 || port > 65535) {
log_debug("KAD: Invalid port for announcement: %d", port);
return false;
}
dht_search(id, port, AF_INET, dht_callback_func, NULL);
dht_search(id, port, AF_INET6, dht_callback_func, NULL);
return true;
}
/*
* Add a new value to the announcement list or refresh an announcement.
*/
bool kad_announce(const char query[], time_t lifetime)
{
char hostname[QUERY_MAX_SIZE];
// Remove .p2p suffix and convert to lowercase
if (!query_sanitize(hostname, sizeof(hostname), query)) {
return false;
}
return announces_add(NULL, hostname, lifetime) ? true : false;
}
// Lookup known nodes that are nearest to the given id
const struct search_t *kad_lookup(const char query[])
{
char hostname[QUERY_MAX_SIZE];
struct search_t *search;
// Remove .p2p suffix and convert to lowercase
if (!query_sanitize(hostname, sizeof(hostname), query)) {
log_debug("KAD: query_sanitize error");
return NULL;
}
log_debug("KAD: Lookup identifier: %s", hostname);
// Find existing or create new search
search = searches_start(hostname);
if (search == NULL) {
// Failed to create a new search
log_debug("KAD: searches_start error");
return NULL;
}
// Start DHT search if search was just started/restarted
if (search->start_time == time_now_sec()) {
#if 0
// Search own announces
kad_lookup_own_announcements(search);
#endif
log_debug("KAD: Start DHT search");
// Start a new DHT search
dht_search(search->id, 0, AF_INET, dht_callback_func, NULL);
dht_search(search->id, 0, AF_INET6, dht_callback_func, NULL);
}
return search;
}
#if 0
/*
* Lookup the address of the node whose node id matches id.
* The lookup will be performed on the results of kad_lookup().
* The port in the returned address refers to the kad instance.
*/
bool kad_lookup_node(const char query[], IP *addr_return)
{
uint8_t id[SHA1_BIN_LENGTH];
struct search *sr;
int i;
bool rc;
if (!bytes_from_base16hex(id, query, SHA1_HEX_LENGTH) {
return false;
}
rc = true;
sr = searches;
while (sr) {
if (sr->af == gconf->af && id_equal(sr->id, id)) {
for (i = 0; i < sr->numnodes; ++i) {
if (id_equal(sr->nodes[i].id, id)) {
memcpy(addr_return, &sr->nodes[i].ss, sizeof(IP));
rc = false;
goto done;
}
}
break;
}
sr = sr->next;
}
done:;
return rc;
}
#endif
bool kad_block(const IP* addr)
{
blacklist_node(NULL, (struct sockaddr *) addr, sizeof(IP));
return true;
}
// Export known peers; the maximum is 400 nodes
int kad_export_peers(FILE *fp)
{
IP4 addr4[200];
IP6 addr6[200];
int num6 = ARRAY_SIZE(addr4);
int num4 = ARRAY_SIZE(addr6);
dht_get_nodes(addr4, &num4, addr6, &num6);
for (size_t i = 0; i < num4; i++) {
#ifdef __CYGWIN__
fprintf(fp, "%s\r\n", str_addr((IP*) &addr4[i]));
#else
fprintf(fp, "%s\n", str_addr((IP*) &addr4[i]));
#endif
}
for (size_t i = 0; i < num6; i++) {
#ifdef __CYGWIN__
fprintf(fp, "%s\r\n", str_addr((IP*) &addr6[i]));
#else
fprintf(fp, "%s\n", str_addr((IP*) &addr6[i]));
#endif
}
return num4 + num6;
}
// Print buckets (leaf/finger table)
void kad_print_buckets(FILE* fp)
{
size_t i, j;
struct bucket *b = (gconf->af == AF_INET) ? buckets : buckets6;
for (j = 0; b; ++j) {
fprintf(fp, " bucket: %s\n", str_id(b->first));
struct node * n = b->nodes;
for (i = 0; n; ++i) {
fprintf(fp, " id: %s\n", str_id(n->id));
fprintf(fp, " address: %s\n", str_addr(&n->ss));
fprintf(fp, " pinged: %d\n", n->pinged);
n = n->next;
}
fprintf(fp, " Found %u nodes.\n", (unsigned) i);
b = b->next;
}
fprintf(fp, "Found %u buckets.\n", (unsigned) j);
}
// Print announced ids we have received
void kad_print_storage(FILE *fp)
{
size_t i, j;
struct storage *s = storage;
for (i = 0; s; ++i) {
fprintf(fp, " id: %s\n", str_id(s->id));
for (j = 0; j < s->numpeers; ++j) {
struct peer *p = &s->peers[j];
fprintf(fp, " peer: %s\n", str_addr2(&p->ip[0], p->len, p->port));
}
fprintf(fp, " Found %u peers.\n", (unsigned) j);
s = s->next;
}
fprintf(fp, " Found %u stored hashes from received announcements.\n", (unsigned) i);
}
// Print searches
void kad_print_searches(FILE *fp)
{
struct search *s;
int i;
int j;
s = searches;
for (j = 0; s; ++j) {
fprintf(fp, " DHT-Search: %s\n", str_id(s->id));
fprintf(fp, " af: %s\n", (s->af == AF_INET) ? "AF_INET" : "AF_INET6");
fprintf(fp, " port: %hu\n", s->port);
//fprintf(fp, " done: %d\n", s->done);
for (i = 0; i < s->numnodes; ++i) {
struct search_node *sn = &s->nodes[i];
fprintf(fp, " Node: %s\n", str_id(sn->id));
fprintf(fp, " addr: %s\n", str_addr(&sn->ss));
fprintf(fp, " pinged: %d\n", sn->pinged);
fprintf(fp, " replied: %d\n", sn->replied);
fprintf(fp, " acked: %d\n", sn->acked);
}
fprintf(fp, " Found %d nodes.\n", i);
s = s->next;
}
fprintf(fp, " Found %d searches.\n", j);
}
void kad_print_blocklist(FILE *fp)
{
size_t i;
for (i = 0; i < (next_blacklisted % DHT_MAX_BLACKLISTED); i++) {
fprintf(fp, " %s\n", str_addr(&blacklist[i]));
}
fprintf(fp, " Found %u blocked addresses.\n", (unsigned) i);
}
void kad_print_constants(FILE *fp)
{
fprintf(fp, "DHT_SEARCH_EXPIRE_TIME: %d\n", DHT_SEARCH_EXPIRE_TIME);
fprintf(fp, "DHT_MAX_SEARCHES: %d\n", DHT_MAX_SEARCHES);
// Maximum number of announced hashes we track
fprintf(fp, "DHT_MAX_HASHES: %d\n", DHT_MAX_HASHES);
// Maximum number of peers for each announced hash we track
fprintf(fp, "DHT_MAX_PEERS: %d\n", DHT_MAX_PEERS);
// Maximum number of blocked nodes
fprintf(fp, "DHT_MAX_BLACKLISTED: %d\n", DHT_MAX_BLACKLISTED);
}