forked from h31h31/H31DHTDEMO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dht.cpp
2854 lines (2487 loc) · 80.5 KB
/
dht.cpp
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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "stdafx.h"
//#define WIN32
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
//#include <unistd.h>
#include <fcntl.h>
//#include <sys/time.h>
#include "winsock2.h"
#include "ws2tcpip.h"
#include "dht.h"
#if WIN32
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#endif
#ifndef HAVE_MEMMEM
#ifdef __GLIBC__
#define HAVE_MEMMEM
#endif
#endif
#ifndef MSG_CONFIRM
#define MSG_CONFIRM 0
#endif
#ifdef WIN32
int gettimeofday(struct timeval *tp, void *tzp)
{
time_t clock;
struct tm tm;
SYSTEMTIME wtm;
GetLocalTime(&wtm);
tm.tm_year = wtm.wYear - 1900;
tm.tm_mon = wtm.wMonth - 1;
tm.tm_mday = wtm.wDay;
tm.tm_hour = wtm.wHour;
tm.tm_min = wtm.wMinute;
tm.tm_sec = wtm.wSecond;
tm. tm_isdst = -1;
clock = mktime(&tm);
tp->tv_sec = clock;
tp->tv_usec = wtm.wMilliseconds * 1000;
return (0);
}
#endif
#define EAFNOSUPPORT WSAEAFNOSUPPORT
static int set_nonblocking(int fd, int nonblocking)
{
int rc;
unsigned long mode = !!nonblocking;
rc = ioctlsocket(fd, FIONBIO, &mode);
if(rc != 0)
errno = WSAGetLastError();
return (rc == 0 ? 0 : -1);
}
extern const char *inet_ntop(int, const void *, char *, socklen_t)
{
return NULL;
}
/* We set sin_family to 0 to mark unused slots. */
#if AF_INET == 0 || AF_INET6 == 0
#error You lose
#endif
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
/* nothing */
#elif defined(__GNUC__)
#define inline __inline
#if (__GNUC__ >= 3)
#define restrict __restrict
#else
#define restrict /**/
#endif
#else
#define inline /**/
#define restrict /**/
#endif
#define MAX(x, y) ((x) >= (y) ? (x) : (y))
#define MIN(x, y) ((x) <= (y) ? (x) : (y))
struct node {
unsigned char id[20];
struct sockaddr_storage ss;
int sslen;
time_t time; /* time of last message received */
time_t reply_time; /* time of last correct reply received */
time_t pinged_time; /* time of last request */
int pinged; /* how many requests we sent since last reply */
struct node *next;
};
struct bucket {
int af;
unsigned char first[20];
int count; /* number of nodes */
int time; /* time of last reply in this bucket */
struct node *nodes;
struct sockaddr_storage cached; /* the address of a likely candidate */
int cachedlen;
struct bucket *next;
};
struct search_node {
unsigned char id[20];
struct sockaddr_storage ss;
int sslen;
time_t request_time; /* the time of the last unanswered request */
time_t reply_time; /* the time of the last reply */
int pinged;
unsigned char token[40];
int token_len;
int replied; /* whether we have received a reply */
int acked; /* whether they acked our announcement */
};
/* When performing a search, we search for up to SEARCH_NODES closest nodes
to the destination, and use the additional ones to backtrack if any of
the target 8 turn out to be dead. */
#define SEARCH_NODES 14
struct search {
unsigned short tid;
int af;
time_t step_time; /* the time of the last search_step */
unsigned char id[20];
unsigned short port; /* 0 for pure searches */
int done;
struct search_node nodes[SEARCH_NODES];
int numnodes;
struct search *next;
};
struct peer {
time_t time;
unsigned char ip[16];
unsigned short len;
unsigned short port;
};
/* The maximum number of peers we store for a given hash. */
#ifndef DHT_MAX_PEERS
#define DHT_MAX_PEERS 2048
#endif
/* The maximum number of hashes we're willing to track. */
#ifndef DHT_MAX_HASHES
#define DHT_MAX_HASHES 16384
#endif
/* The maximum number of searches we keep data about. */
#ifndef DHT_MAX_SEARCHES
#define DHT_MAX_SEARCHES 1024
#endif
/* The time after which we consider a search to be expirable. */
#ifndef DHT_SEARCH_EXPIRE_TIME
#define DHT_SEARCH_EXPIRE_TIME (62 * 60)
#endif
struct storage {
unsigned char id[20];
int numpeers, maxpeers;
struct peer *peers;
struct storage *next;
};
static void flush_search_node(struct search_node *n, struct search *sr);
static int send_ping(const struct sockaddr *sa, int salen,const unsigned char *tid, int tid_len);
static int send_pong(const struct sockaddr *sa, int salen,const unsigned char *tid, int tid_len);
static int send_nodes_peers(const struct sockaddr *sa, int salen,const unsigned char *tid, int tid_len,const unsigned char *nodes, int nodes_len,const unsigned char *nodes6, int nodes6_len,int af, struct storage *st,const unsigned char *token, int token_len);
static int send_closest_nodes(const struct sockaddr *sa, int salen,const unsigned char *tid, int tid_len,const unsigned char *id, int want,int af, struct storage *st,const unsigned char *token, int token_len);
static int send_announce_peer(const struct sockaddr *sa, int salen,unsigned char *tid, int tid_len,unsigned char *infohas, unsigned short port,unsigned char *token, int token_len, int confirm);
static int send_peer_announced(const struct sockaddr *sa, int salen,unsigned char *tid, int tid_len);
static int send_error(const struct sockaddr *sa, int salen,unsigned char *tid, int tid_len,int code, const char *message);
#define ERROR 0
#define REPLY 1
#define PING 2
#define FIND_NODE 3
#define GET_PEERS 4
#define ANNOUNCE_PEER 5
#define WANT4 1
#define WANT6 2
static int parse_message(const unsigned char *buf, int buflen,
unsigned char *tid_return, int *tid_len,
unsigned char *id_return,
unsigned char *info_hash_return,
unsigned char *target_return,
unsigned short *port_return,
unsigned char *token_return, int *token_len,
unsigned char *nodes_return, int *nodes_len,
unsigned char *nodes6_return, int *nodes6_len,
unsigned char *values_return, int *values_len,
unsigned char *values6_return, int *values6_len,
int *want_return);
static const unsigned char zeroes[20] = {0};
static const unsigned char ones[20] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF
};
static const unsigned char v4prefix[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0};
static int dht_socket = -1;
static int dht_socket6 = -1;
static time_t search_time;
static time_t confirm_nodes_time;
static time_t rotate_secrets_time;
static unsigned char myid[20];
static int have_v = 0;
static unsigned char my_v[9];
static unsigned char secret[8];
static unsigned char oldsecret[8];
static struct bucket *buckets = NULL;
static struct bucket *buckets6 = NULL;
static struct storage *mystorage;
static int numstorage;
static struct search *searches = NULL;
static int numsearches;
static unsigned short search_id;
/* The maximum number of nodes that we snub. There is probably little
reason to increase this value. */
#ifndef DHT_MAX_BLACKLISTED
#define DHT_MAX_BLACKLISTED 10
#endif
static struct sockaddr_storage blacklist[DHT_MAX_BLACKLISTED];
int next_blacklisted;
static struct timeval nowTime;
static time_t mybucket_grow_time, mybucket6_grow_time;
static time_t expire_stuff_time;
#define MAX_TOKEN_BUCKET_TOKENS 400
static time_t token_bucket_time;
static int token_bucket_tokens;
#ifdef __GNUC__
__attribute__ ((format (printf, 1, 2)))
#endif
static void debug_printable(const unsigned char *buf, int buflen)
{
//int i;
//if(dht_debug) {
// for(i = 0; i < buflen; i++)
// putc(buf[i] >= 32 && buf[i] <= 126 ? buf[i] : '.', dht_debug);
//}
}
static void print_hex(FILE *f, const unsigned char *buf, int buflen)
{
int i;
for(i = 0; i < buflen; i++)
fprintf(f, "%02x", buf[i]);
}
static int is_martian(const struct sockaddr *sa)
{
switch(sa->sa_family) {
case AF_INET: {
struct sockaddr_in *sin = (struct sockaddr_in*)sa;
const unsigned char *address = (const unsigned char*)&sin->sin_addr;
return sin->sin_port == 0 ||
(address[0] == 0) ||
(address[0] == 127) ||
((address[0] & 0xE0) == 0xE0);
}
case AF_INET6: {
struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)sa;
const unsigned char *address = (const unsigned char*)&sin6->sin6_addr;
return sin6->sin6_port == 0 ||
(address[0] == 0xFF) ||
(address[0] == 0xFE && (address[1] & 0xC0) == 0x80) ||
(memcmp(address, zeroes, 15) == 0 &&
(address[15] == 0 || address[15] == 1)) ||
(memcmp(address, v4prefix, 12) == 0);
}
default:
return 0;
}
}
/* Forget about the ``XOR-metric''. An id is just a path from the
root of the tree, so bits are numbered from the start. */
static int id_cmp(const unsigned char *restrict id1, const unsigned char *restrict id2)
{
/* Memcmp is guaranteed to perform an unsigned comparison. */
return memcmp(id1, id2, 20);
}
/* Find the lowest 1 bit in an id. */
static int lowbit(const unsigned char *id)
{
int i, j;
for(i = 19; i >= 0; i--)
if(id[i] != 0)
break;
if(i < 0)
return -1;
for(j = 7; j >= 0; j--)
if((id[i] & (0x80 >> j)) != 0)
break;
return 8 * i + j;
}
/* Find how many bits two ids have in common. */
static int common_bits(const unsigned char *id1, const unsigned char *id2)
{
int i, j;
unsigned char xor;
for(i = 0; i < 20; i++) {
if(id1[i] != id2[i])
break;
}
if(i == 20)
return 160;
xor = id1[i] ^ id2[i];
j = 0;
while((xor & 0x80) == 0) {
xor <<= 1;
j++;
}
return 8 * i + j;
}
/* Determine whether id1 or id2 is closer to ref */
static int xorcmp(const unsigned char *id1, const unsigned char *id2,
const unsigned char *ref)
{
int i;
for(i = 0; i < 20; i++) {
unsigned char xor1, xor2;
if(id1[i] == id2[i])
continue;
xor1 = id1[i] ^ ref[i];
xor2 = id2[i] ^ ref[i];
if(xor1 < xor2)
return -1;
else
return 1;
}
return 0;
}
/* We keep buckets in a sorted linked list. A bucket b ranges from
b->first inclusive up to b->next->first exclusive. */
static int in_bucket(const unsigned char *id, struct bucket *b)
{
return id_cmp(b->first, id) <= 0 &&
(b->next == NULL || id_cmp(id, b->next->first) < 0);
}
static struct bucket *find_bucket(unsigned const char *id, int af)
{
struct bucket *b = af == AF_INET ? buckets : buckets6;
if(b == NULL)
return NULL;
while(1) {
if(b->next == NULL)
return b;
if(id_cmp(id, b->next->first) < 0)
return b;
b = b->next;
}
}
static struct bucket *previous_bucket(struct bucket *b)
{
struct bucket *p = b->af == AF_INET ? buckets : buckets6;
if(b == p)
return NULL;
while(1) {
if(p->next == NULL)
return NULL;
if(p->next == b)
return p;
p = p->next;
}
}
/* Every bucket contains an unordered list of nodes. */
static struct node *find_node(const unsigned char *id, int af)
{
struct bucket *b = find_bucket(id, af);
struct node *n;
if(b == NULL)
return NULL;
n = b->nodes;
while(n) {
if(id_cmp(n->id, id) == 0)
return n;
n = n->next;
}
return NULL;
}
/* Return a random node in a bucket. */
static struct node *random_node(struct bucket *b)
{
struct node *n;
int nn;
if(b->count == 0)
return NULL;
nn = random() % b->count;
n = b->nodes;
while(nn > 0 && n) {
n = n->next;
nn--;
}
return n;
}
/* Return the middle id of a bucket. */
static int bucket_middle(struct bucket *b, unsigned char *id_return)
{
int bit1 = lowbit(b->first);
int bit2 = b->next ? lowbit(b->next->first) : -1;
int bit = MAX(bit1, bit2) + 1;
if(bit >= 160)
return -1;
memcpy(id_return, b->first, 20);
id_return[bit / 8] |= (0x80 >> (bit % 8));
return 1;
}
/* Return a random id within a bucket. */
static int bucket_random(struct bucket *b, unsigned char *id_return)
{
int bit1 = lowbit(b->first);
int bit2 = b->next ? lowbit(b->next->first) : -1;
int bit = MAX(bit1, bit2) + 1;
int i;
if(bit >= 160) {
memcpy(id_return, b->first, 20);
return 1;
}
memcpy(id_return, b->first, bit / 8);
id_return[bit / 8] = b->first[bit / 8] & (0xFF00 >> (bit % 8));
id_return[bit / 8] |= random() & 0xFF >> (bit % 8);
for(i = bit / 8 + 1; i < 20; i++)
id_return[i] = random() & 0xFF;
return 1;
}
/* Insert a new node into a bucket. */
static struct node * insert_node(struct node *node)
{
struct bucket *b = find_bucket(node->id, node->ss.ss_family);
if(b == NULL)
return NULL;
node->next = b->nodes;
b->nodes = node;
b->count++;
return node;
}
/* This is our definition of a known-good node. */
static int node_good(struct node *node)
{
return node->pinged <= 2 &&node->reply_time >= nowTime.tv_sec - 7200 &&node->time >= nowTime.tv_sec - 900;
}
/* Our transaction-ids are 4-bytes long, with the first two bytes identi-
fying the kind of request, and the remaining two a sequence number in
host order. */
void make_tid(unsigned char *tid_return, const char *prefix, unsigned short seqno)
{
tid_return[0] = prefix[0] & 0xFF;
tid_return[1] = prefix[1] & 0xFF;
memcpy(tid_return + 2, &seqno, 2);
}
static int tid_match(const unsigned char *tid, const char *prefix,unsigned short *seqno_return)
{
if(tid[0] == (prefix[0] & 0xFF) && tid[1] == (prefix[1] & 0xFF)) {
if(seqno_return)
memcpy(seqno_return, tid + 2, 2);
return 1;
} else
return 0;
}
/* Every bucket caches the address of a likely node. Ping it. */
static int send_cached_ping(struct bucket *b)
{
unsigned char tid[4];
int rc;
/* We set family to 0 when there's no cached node. */
if(b->cached.ss_family == 0)
return 0;
_dout("Sending ping to cached node.\n");
make_tid(tid, "pn", 0);
rc = send_ping((struct sockaddr*)&b->cached, b->cachedlen, tid, 4);
b->cached.ss_family = 0;
b->cachedlen = 0;
return rc;
}
/* Called whenever we send a request to a node, increases the ping count
and, if that reaches 3, sends a ping to a new candidate. */
static void pinged(struct node *n, struct bucket *b)
{
n->pinged++;
n->pinged_time = nowTime.tv_sec;
if(n->pinged >= 3)
send_cached_ping(b ? b : find_bucket(n->id, n->ss.ss_family));
}
/* The internal blacklist is an LRU cache of nodes that have sent
incorrect messages. */
static void blacklist_node(const unsigned char *id, const struct sockaddr *sa, int salen)
{
int i;
_dout("Blacklisting broken node.\n");
if(id) {
struct node *n;
struct search *sr;
/* Make the node easy to discard. */
n = find_node(id, sa->sa_family);
if(n) {
n->pinged = 3;
pinged(n, NULL);
}
/* Discard it from any searches in progress. */
sr = searches;
while(sr) {
for(i = 0; i < sr->numnodes; i++)
if(id_cmp(sr->nodes[i].id, id) == 0)
flush_search_node(&sr->nodes[i], sr);
sr = sr->next;
}
}
/* And make sure we don't hear from it again. */
memcpy(&blacklist[next_blacklisted], sa, salen);
next_blacklisted = (next_blacklisted + 1) % DHT_MAX_BLACKLISTED;
}
static int node_blacklisted(const struct sockaddr *sa, int salen)
{
int i;
if((unsigned)salen > sizeof(struct sockaddr_storage))
abort();
if(dht_blacklisted(sa, salen))
return 1;
for(i = 0; i < DHT_MAX_BLACKLISTED; i++) {
if(memcmp(&blacklist[i], sa, salen) == 0)
return 1;
}
return 0;
}
/* Split a bucket into two equal parts. */
static struct bucket * split_bucket(struct bucket *b)
{
struct bucket *newBucket;
struct node *nodes;
int rc;
unsigned char new_id[20];
rc = bucket_middle(b, new_id);
if(rc < 0)
return NULL;
newBucket = (bucket *)calloc(1, sizeof(struct bucket));
if(newBucket == NULL)
return NULL;
newBucket->af = b->af;
send_cached_ping(b);
memcpy(newBucket->first, new_id, 20);
newBucket->time = b->time;
nodes = b->nodes;
b->nodes = NULL;
b->count = 0;
newBucket->next = b->next;
b->next = newBucket;
while(nodes)
{
struct node *n;
n = nodes;
nodes = nodes->next;
insert_node(n);
}
return b;
}
/* We just learnt about a node, not necessarily a new one. Confirm is 1 if the node sent a message, 2 if it sent us a reply. */
static struct node *new_node(const unsigned char *id, const struct sockaddr *sa, int salen,int confirm)
{
struct bucket *b = find_bucket(id, sa->sa_family);
struct node *n;
int mybucket, split;
if(b == NULL)
return NULL;
if(id_cmp(id, myid) == 0)
return NULL;
if(is_martian(sa) || node_blacklisted(sa, salen))
return NULL;
mybucket = in_bucket(myid, b);
if(confirm == 2)
b->time = nowTime.tv_sec;
n = b->nodes;
while(n)
{
if(id_cmp(n->id, id) == 0)
{
if(confirm || n->time < nowTime.tv_sec - 15 * 60)
{
/* Known node. Update stuff. */
memcpy((struct sockaddr*)&n->ss, sa, salen);
if(confirm)
n->time = nowTime.tv_sec;
if(confirm >= 2)
{
n->reply_time = nowTime.tv_sec;
n->pinged = 0;
n->pinged_time = 0;
}
}
return n;
}
n = n->next;
}
/* New node. */
if(mybucket)
{
if(sa->sa_family == AF_INET)
mybucket_grow_time = nowTime.tv_sec;
else
mybucket6_grow_time = nowTime.tv_sec;
}
/* First, try to get rid of a known-bad node. */
n = b->nodes;
while(n)
{
if(n->pinged >= 3 && n->pinged_time < nowTime.tv_sec - 15)
{
memcpy(n->id, id, 20);
memcpy((struct sockaddr*)&n->ss, sa, salen);
n->time = confirm ? nowTime.tv_sec : 0;
n->reply_time = confirm >= 2 ? nowTime.tv_sec : 0;
n->pinged_time = 0;
n->pinged = 0;
return n;
}
n = n->next;
}
if(b->count >= 8)
{
/* Bucket full. Ping a dubious node */
int dubious = 0;
n = b->nodes;
while(n)
{
/* Pick the first dubious node that we haven't pinged in the last 15 seconds. This gives nodes the time to reply, but
tends to concentrate on the same nodes, so that we get rid of bad nodes fast. */
if(!node_good(n))
{
dubious = 1;
if(n->pinged_time < nowTime.tv_sec - 15)
{
unsigned char tid[4];
_dout("Sending ping to dubious node.\n");
make_tid(tid, "pn", 0);
send_ping((struct sockaddr*)&n->ss, n->sslen,tid, 4);
n->pinged++;
n->pinged_time = nowTime.tv_sec;
break;
}
}
n = n->next;
}
split = 0;
if(mybucket)
{
if(!dubious)
split = 1;
/* If there's only one bucket, split eagerly. This is incorrect unless there's more than 8 nodes in the DHT. */
else if(b->af == AF_INET && buckets->next == NULL)
split = 1;
else if(b->af == AF_INET6 && buckets6->next == NULL)
split = 1;
}
if(split)
{
struct sockaddr_in* tempip=(struct sockaddr_in *)sa;
_dout("Splitting new node ip:[%s] port[%d] id:[%s]\n",inet_ntoa(tempip->sin_addr),tempip->sin_port,id);
//_dout("Splitting.\n");
b = split_bucket(b);
return new_node(id, sa, salen, confirm);
}
/* No space for this node. Cache it away for later. */
if(confirm || b->cached.ss_family == 0)
{
memcpy(&b->cached, sa, salen);
b->cachedlen = salen;
}
return NULL;
}
/* Create a new node. */
n = (node *)calloc(1, sizeof(struct node));
if(n == NULL)
return NULL;
memcpy(n->id, id, 20);
memcpy(&n->ss, sa, salen);
n->sslen = salen;
n->time = confirm ? nowTime.tv_sec : 0;
n->reply_time = confirm >= 2 ? nowTime.tv_sec : 0;
n->next = b->nodes;
b->nodes = n;
b->count++;
return n;
}
/* Called periodically to purge known-bad nodes. Note that we're very
conservative here: broken nodes in the table don't do much harm, we'll
recover as soon as we find better ones. */
static int expire_buckets(struct bucket *b)
{
while(b) {
struct node *n, *p;
int changed = 0;
while(b->nodes && b->nodes->pinged >= 4) {
n = b->nodes;
b->nodes = n->next;
b->count--;
changed = 1;
free(n);
}
p = b->nodes;
while(p) {
while(p->next && p->next->pinged >= 4) {
n = p->next;
p->next = n->next;
b->count--;
changed = 1;
free(n);
}
p = p->next;
}
if(changed)
send_cached_ping(b);
b = b->next;
}
expire_stuff_time = nowTime.tv_sec + 120 + random() % 240;
return 1;
}
/* While a search is in progress, we don't necessarily keep the nodes being
walked in the main bucket table. A search in progress is identified by
a unique transaction id, a short (and hence small enough to fit in the
transaction id of the protocol packets). */
static struct search *find_search(unsigned short tid, int af)
{
struct search *sr = searches;
while(sr) {
if(sr->tid == tid && sr->af == af)
return sr;
sr = sr->next;
}
return NULL;
}
/* A search contains a list of nodes, sorted by decreasing distance to the
target. We just got a new candidate, insert it at the right spot or
discard it. */
static int insert_search_node(unsigned char *id,const struct sockaddr *sa, int salen,struct search *sr, int replied,unsigned char *token, int token_len)
{
struct search_node *n;
int i, j;
if(sa->sa_family != sr->af) {
_dout("Attempted to insert node in the wrong family.\n");
return 0;
}
for(i = 0; i < sr->numnodes; i++) {
if(id_cmp(id, sr->nodes[i].id) == 0) {
n = &sr->nodes[i];
goto found;
}
if(xorcmp(id, sr->nodes[i].id, sr->id) < 0)
break;
}
if(i == SEARCH_NODES)
return 0;
if(sr->numnodes < SEARCH_NODES)
sr->numnodes++;
for(j = sr->numnodes - 1; j > i; j--) {
sr->nodes[j] = sr->nodes[j - 1];
}
n = &sr->nodes[i];
memset(n, 0, sizeof(struct search_node));
memcpy(n->id, id, 20);
//struct sockaddr_in* tempip=(struct sockaddr_in *)sa;
//_dout("insert search node ip:[%s] port[%d] id:[%s]\n",inet_ntoa(tempip->sin_addr),tempip->sin_port,n->id);
found:
memcpy(&n->ss, sa, salen);
n->sslen = salen;
if(replied) {
n->replied = 1;
n->reply_time = nowTime.tv_sec;
n->request_time = 0;
n->pinged = 0;
}
if(token) {
if(token_len >= 40)
{
_dout("Eek! Overlong token.\n");
}
else
{
memcpy(n->token, token, token_len);
n->token_len = token_len;
}
}
return 1;
}
static void flush_search_node(struct search_node *n, struct search *sr)
{
int i = n - sr->nodes, j;
for(j = i; j < sr->numnodes - 1; j++)
sr->nodes[j] = sr->nodes[j + 1];
sr->numnodes--;
}
static void expire_searches(void)
{
struct search *sr = searches, *previous = NULL;
while(sr) {
struct search *next = sr->next;
if(sr->step_time < nowTime.tv_sec - DHT_SEARCH_EXPIRE_TIME) {
if(previous)
previous->next = next;
else
searches = next;
free(sr);
numsearches--;
} else {
previous = sr;
}
sr = next;
}
}
/* This must always return 0 or 1, never -1, not even on failure (see below). */
static int search_send_get_peers(struct search *sr, struct search_node *n)
{
struct node *node;
unsigned char tid[4];
if(n == NULL) {
int i;
for(i = 0; i < sr->numnodes; i++) {
if(sr->nodes[i].pinged < 3 && !sr->nodes[i].replied &&
sr->nodes[i].request_time < nowTime.tv_sec - 15)
n = &sr->nodes[i];
}
}
if(!n || n->pinged >= 3 || n->replied ||
n->request_time >= nowTime.tv_sec - 15)
return 0;
_dout("Sending get_peers.\n");
make_tid(tid, "gp", sr->tid);
send_get_peers((struct sockaddr*)&n->ss, n->sslen, tid, 4, sr->id, -1,n->reply_time >= nowTime.tv_sec - 15);
n->pinged++;
n->request_time = nowTime.tv_sec;
/* If the node happens to be in our main routing table, mark it as pinged. */
node = find_node(n->id, n->ss.ss_family);
if(node) pinged(node, NULL);
return 1;
}
/* When a search is in progress, we periodically call search_step to send further requests. */
static void search_step(struct search *sr, dht_callback *callback, void *closure)
{
int i, j;
int all_done = 1;
/* Check if the first 8 live nodes have replied. */
j = 0;
for(i = 0; i < sr->numnodes && j < 8; i++) {
struct search_node *n = &sr->nodes[i];
if(n->pinged >= 3)
continue;
if(!n->replied) {
all_done = 0;
break;
}
j++;
}
if(all_done)
{
if(sr->port == 0)
{