-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdns_fixd.cpp
1501 lines (1227 loc) · 35.4 KB
/
dns_fixd.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 <ctype.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#ifdef WIN32
#include <windows.h>
#else
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <netdb.h>
#define closesocket close
#endif
#include "txall.h"
#include "dnsproto.h"
#include "subnet_api.h"
#include <txall.h>
struct uptick_task {
int ticks;
tx_task_t task;
unsigned int last_ticks;
};
extern "C" int cache_verify(const void *ptr);
static void update_tick(void *up)
{
struct uptick_task *uptick;
unsigned int ticks = tx_ticks;
uptick = (struct uptick_task *)up;
if (ticks != uptick->last_ticks) {
//fprintf(stderr, "tx_getticks: %u %d\n", ticks, uptick->ticks);
uptick->last_ticks = ticks;
}
if (uptick->ticks < 100) {
tx_task_active(&uptick->task, "update_tick");
uptick->ticks++;
return;
}
fprintf(stderr, "all update_tick finish\n");
#if 0
tx_loop_stop(tx_loop_get(&uptick->task));
fprintf(stderr, "stop the loop\n");
#endif
return;
}
struct timer_task {
tx_task_t task;
tx_timer_t timer;
};
static void update_timer(void *up)
{
struct timer_task *ttp;
ttp = (struct timer_task*)up;
tx_timer_reset(&ttp->timer, 50000);
//fprintf(stderr, "update_timer %d\n", tx_ticks);
return;
}
struct dns_udp_context_t {
int sockfd;
tx_aiocb file;
int outfd;
tx_aiocb outgoing;
tx_task_t mark_detect;
tx_timer_t reset_detect;
struct tcpip_info forward;
tx_task_t task;
};
typedef void (*callback_t)(void *);
void query_finish(void *context)
{
}
static char addrbuf[128];
#define ntop6(addr) inet_ntop(AF_INET6, &addr, addrbuf, sizeof(addrbuf))
static const char *inet_4to6(void *v6ptr, const void *v4ptr)
{
uint8_t *v4 = (uint8_t *)v4ptr;
uint8_t *v6 = (uint8_t *)v6ptr;
memset(v6, 0, 10);
v6[10] = 0xff;
v6[11] = 0xff;
v6[12] = v4[0];
v6[13] = v4[1];
v6[14] = v4[2];
v6[15] = v4[3];
return "";
}
struct root_server {
char domain[32];
int ttl;
char ipv4[32];
char ipv6[32];
};
static struct root_server _root_servers[]= {
{"a.root-servers.net", 518400, "198.41.0.4", "2001:503:ba3e::2:30"},
{"b.root-servers.net", 518400, "199.9.14.201", "2001:500:200::b"},
{"c.root-servers.net", 518400, "192.33.4.12", "2001:500:2::c"},
{"d.root-servers.net", 518400, "199.7.91.13", "2001:500:2d::d"},
{"e.root-servers.net", 518400, "192.203.230.10", "2001:500:a8::e"},
{"f.root-servers.net", 518400, "192.5.5.241", "2001:500:2f::f"},
{"g.root-servers.net", 518400, "192.112.36.4", "2001:500:12::d0d"},
{"h.root-servers.net", 518400, "198.97.190.53", "2001:500:1::53"},
{"i.root-servers.net", 518400, "192.36.148.17", "2001:7fe::53"},
{"j.root-servers.net", 518400, "192.58.128.30", "2001:503:c27::2:30"},
{"k.root-servers.net", 518400, "193.0.14.129", "2001:7fd::1"},
{"l.root-servers.net", 518400, "199.7.83.42", "2001:500:9f::42"},
{"m.root-servers.net", 518400, "202.12.27.33", "2001:dc3::35"}
};
#define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
static struct dns_resource _tpl0 = {
.type = NSTYPE_A,
.klass = NSCLASS_INET,
.ttl = 518400,
.len = 4,
.flags = 0,
.domain = "dummy",
.value = {}
};
static struct dns_resource _tpl6 = {
.type = NSTYPE_AAAA,
.klass = NSCLASS_INET,
.ttl = 518400,
.len = 16,
.flags = 0,
.domain = "dummy",
.value = {}
};
struct dns_cache {
int persist;
time_t touch;
struct dns_resource *res;
};
static int _ans_ncache = 0;
static struct dns_cache _ans_cache[10240];
static int nanswer = 0;
static struct dns_resource answsers[10240] = {};
static int answer_cache_bound(const char *domain, int type, int *lowp, int *highp)
{
int cmp = -1, mid = -1;
int low = 0, high = _ans_ncache -1;
struct dns_resource *res;
while (low <= high) {
mid = (low + high) >> 1;
res = _ans_cache[mid].res;
cmp = domain == res->domain? res->type - type: domain - res->domain;
if (cmp == 0)
break;
if (cmp > 0) {
high = mid - 1;
} else if (cmp < 0) {
low = mid + 1;
} else {
assert(0);
}
}
if (highp)
*highp = high;
if (lowp)
*lowp = low;
return cmp == 0? mid: -1;
}
static int answer_cache_lookup_once(const char *domain, int type, struct dns_resource results[], size_t size)
{
int count = 0;
int low, high, mid;
struct dns_resource *res;
const char *dn = domain;
if (dn == NULL) {
LOG_DEBUG("domain not found: %s", domain);
return 0;
}
mid = answer_cache_bound(dn, type, &low, &high);
if (mid == -1) {
return count;
}
for (int o = mid; o <= high; o++) {
res = _ans_cache[o].res;
if (res->type == type && dn == res->domain) {
if (_ans_cache[o].persist ||
_ans_cache[o].touch + res->ttl > time(NULL)) {
assert(count < size);
results[count++] = *res;
}
} else {
/* right bound end */
break;
}
}
for (int o = mid -1; o >= low; o--) {
res = _ans_cache[o].res;
if (res->type == type && dn == res->domain) {
if (_ans_cache[o].persist ||
_ans_cache[o].touch + res->ttl > time(NULL)) {
assert(count < size);
results[count++] = *res;
}
} else {
/* left bound end */
break;
}
}
fprintf(stderr, "answer_cache_lookup_once %s type %d count %d\n", domain, type, count);
return count;
}
static int answer_cache_lookup(const char *domain, int type, struct dns_resource results[], size_t size)
{
int more, count = 0;
const char *dn = cache_get_name(domain);
if (dn == NULL) {
LOG_DEBUG("domain not found: %s", domain);
return 0;
}
count = answer_cache_lookup_once(dn, type, results, size);
if (count > 0 || type == NSTYPE_CNAME) {
return count;
}
const char *alias = dn;
for (int offset = 0; offset < size; offset++) {
count = answer_cache_lookup_once(alias, NSTYPE_CNAME, results + offset, size - offset);
if (count == 0) {
count = offset;
break;
} else {
assert(count == 1);
alias = *(char **)results[offset].value;
}
for (int i = 0; i < offset; i++) {
assert(alias != results[i].domain);
}
}
more = answer_cache_lookup_once(alias, type, results + count, size - count);
LOG_DEBUG("answer_cache_lookup %s type %d count %d\n", domain, type, count + more);
return count + more;
}
int answer_cache_clear(const char *domain, int type)
{
int left, right;
int mid = 0, low = 0, high = 0;
const char *dn = cache_get_name(domain);
struct dns_resource *res;
if (dn == NULL) {
LOG_DEBUG("domain not found: %s", domain);
return 0;
}
mid = answer_cache_bound(dn, type, &low, &high);
if (mid == -1) {
return 0;
}
right = mid + 1;
for (int o = mid; o <= high; o++) {
res = _ans_cache[o].res;
if (res->type == type && dn == res->domain) {
continue;
} else {
right = o;
break;
}
}
left = mid;
for (int o = mid -1; o >= low; o--) {
res = _ans_cache[o].res;
if (res->type == type && dn == res->domain) {
continue;
} else {
left = o + 1;
break;
}
}
memmove(_ans_cache + left, _ans_cache + right, sizeof(_ans_cache[0]) * (_ans_ncache - right));
_ans_ncache = left + (_ans_ncache - right);
return 0;
}
int answer_cache_put(struct dns_resource *res, int persist)
{
int mid = 0, low = 0, high = 0;
const char *dn = cache_get_name(res->domain);
if (dn == NULL) {
LOG_DEBUG("domain not found: %s", res->domain);
return 0;
}
mid = answer_cache_bound(dn, res->type, &low, &high);
if (mid == -1) {
mid = high + 1;
}
assert(nanswer + 1 < ARRAY_SIZE(answsers));
assert(_ans_ncache + 1 < ARRAY_SIZE(_ans_cache));
memmove(_ans_cache + mid + 1, _ans_cache + mid, sizeof(_ans_cache[0]) * (_ans_ncache - mid));
_ans_cache[mid].res = &answsers[nanswer++];
_ans_cache[mid].touch = time(NULL);
_ans_cache[mid].res[0] = res[0];
_ans_cache[mid].persist = persist;
_ans_ncache++;
return 0;
}
static int hold_to_answer(struct dns_resource *res, size_t count, int is_china_domain)
{
int i;
struct dns_resource *f;
cache_put(res, count);
for (i = 0; i < count; i++) {
f = res + i;
switch (f->type) {
case NSTYPE_CNAME:
case NSTYPE_DNAME:
case NSTYPE_NS:
case NSTYPE_A:
case NSTYPE_MX:
case NSTYPE_AAAA:
case NSTYPE_SOA:
case NSTYPE_SRV:
case NSTYPE_PTR:
break;
default:
continue;
}
if (f->domain)
answer_cache_clear(f->domain, f->type);
}
for (i = 0; i < count; i++) {
f = res + i;
switch (f->type) {
case NSTYPE_CNAME:
case NSTYPE_DNAME:
case NSTYPE_NS:
case NSTYPE_A:
case NSTYPE_MX:
case NSTYPE_AAAA:
case NSTYPE_SOA:
case NSTYPE_SRV:
case NSTYPE_PTR:
break;
default:
continue;
}
if (f->domain)
answer_cache_put(f, 0);
}
return 0;
}
static int nauthor = 0;
static struct dns_resource authors[1024];
struct query_context_t {
int refcnt;
int is_china_domain;
struct sockaddr_in6 from;
struct dns_question que;
struct dns_parser parser;
int nworker, iworker;
struct dns_resource worker[260];
void *assioc_uptr;
};
static int _qc_next = 0;
static uint8_t _qc_hold[2048];
static struct query_context_t _query_list[0xfff +1];
int dns_parser_copy(struct dns_parser *dst, struct dns_parser *src)
{
size_t len = dns_build(src, _qc_hold, 2048);
return dns_parse(dst, _qc_hold, len) == NULL;
}
static int do_lookup_nameserver(dns_udp_context_t *up, struct query_context_t *qc, const char * domain);
static int author_cache_lookup(const char *domain, struct dns_resource results[], size_t size)
{
struct dns_resource *res = NULL;
struct dns_resource *kes = NULL;
int ndot = 0, j;
const char *dots[256] = {};
dots[ndot] = domain;
for (j = 0; domain[j]; j++) {
if (domain[j] != '.') {
continue;
}
if (domain + j - 1 > dots[ndot]) {
assert(ndot < 256);
ndot++;
}
dots[ndot] = domain + j + 1;
}
if (domain + j - 1 > dots[ndot]) {
assert(ndot < 256);
ndot++;
}
if (ndot == 0) {
return 0;
}
int count = 0;
for (int i = 0; i < ndot; i++) {
const char *zone = cache_get_name(dots[i]);
if (zone == NULL) {
continue;
}
count = 0;
for (int j = 0; j < nauthor; j++) {
res = &authors[j];
if (res->type == NSTYPE_NS &&
(zone == res->domain)) {
results[count++] = *res;
}
}
if (count) {
goto do_resolved;
}
}
for (int j = 0; j < ARRAY_SIZE(_root_servers); j++) {
struct root_server *rs = &_root_servers[j];
const char *domain = cache_get_name(rs->domain);
assert(domain != NULL);
results[count] = _tpl0;
results[count].domain = cache_get_name("");
results[count].type = NSTYPE_NS;
assert (results[count].domain);
memcpy(results[count++].value, &domain, sizeof(domain));
}
do_resolved:
int newcount;
int nscount = count;
const char *namesever = NULL;
for (int i = 0; i < nscount; i++) {
res = &results[i];
if (res->type != NSTYPE_NS) {
res->ttl = 0;
continue;
}
namesever = *(const char **)res->value;
assert (namesever != NULL);
newcount = answer_cache_lookup(namesever, NSTYPE_A, results + count, size - count);
if (newcount > 0) {
assert(results[count].type != NSTYPE_CNAME);
count += newcount;
res->ttl = 0;
continue;
}
for (int j = 0; j < nauthor; j++) {
kes = &authors[j];
if (kes->type == NSTYPE_A && (namesever == kes->domain)) {
results[count++] = *kes;
res->ttl = 0;
}
if (kes->type == NSTYPE_CNAME && (namesever == kes->domain)) {
results[count++] = *kes;
res->ttl = 0;
assert(0);
}
}
}
return count;
}
static int hold_to_author(struct dns_resource *res, size_t count)
{
int i, j, found;
struct dns_resource *f, *t;
cache_put(res, count);
for (i = 0; i < count; i++) {
f = res + i;
found = 0;
for (j = 0; j < nauthor && found == 0; j++) {
t = &authors[j];
if (t->type == f->type && f->domain == t->domain) {
cache_verify(f->domain);
cache_verify(t->domain);
switch(t->type) {
case NSTYPE_CNAME:
case NSTYPE_NS:
cache_verify(*(char **)f->value);
cache_verify(*(char **)t->value);
found = !memcmp(f->value, t->value, sizeof(void*));
break;
case NSTYPE_A:
found = !memcmp(f->value, t->value, 4);
break;
default:
break;
}
}
}
if (found == 0) {
if (f->type == NSTYPE_CNAME) {
cache_verify(*(char **)f->value);
authors[nauthor ++] = *f;
}
if (f->type == NSTYPE_NS) {
cache_verify(*(char **)f->value);
authors[nauthor ++] = *f;
}
if (f->type == NSTYPE_A)
authors[nauthor ++] = *f;
}
}
return 0;
}
static int do_fetch_resource(dns_udp_context_t *up, int ident, struct dns_question *que, struct in6_addr *server, const char *server_name)
{
uint8_t buf[2048];
struct dns_parser p0 = {};
struct sockaddr_in6 target = {};
p0.head.question = 1;
p0.head.ident = ident;
p0.question[0] = *que;
p0.head.addon = 1;
p0.addon[0].domain = "";
p0.addon[0].type = NSTYPE_OPT;
p0.addon[0].ttl = 0;
p0.addon[0].klass = 1230;
p0.addon[0].len = 0;
size_t len = dns_build(&p0, buf, sizeof(buf));
target.sin6_family = AF_INET6;
target.sin6_port = htons(53);
memcpy(&target.sin6_addr, server, 16);
// inet_4to6(&target.sin6_addr, server);
size_t slen = sendto(up->outfd, buf, len, 0, (struct sockaddr *)&target, sizeof(target));
LOG_DEBUG("do_fetch_resource sendto %s/%s data %d %d %s %d", server_name, ntop6(target.sin6_addr), len, slen, que->domain, que->type);
}
static int dns_query_append(struct query_context_t *qc, struct dns_resource *answer, size_t count)
{
int found;
int i, j, is_china_domain = 0;
struct dns_resource *res, *kes;
for (i = 0; i < count; i++) {
res = answer + i;
if (res->type != NSTYPE_A) continue;
int target = *(int *)res->value;
if (res->type == NSTYPE_A && lookupRoute4(htonl(target)) == 0) {
LOG_DEBUG("china domain detect");
// is_china_domain = 1;
}
found = 0;
assert(qc->nworker < ARRAY_SIZE(qc->worker));
for (j = 0; j < qc->nworker; j++) {
kes = &qc->worker[j];
if (kes->type == res->type &&
strcasecmp(kes->domain, res->domain) == 0) {
found = 1;
break;
}
}
if (found == 0) {
qc->worker[qc->nworker++] = *res;
qc->iworker = qc->nworker - 1;
}
}
// qc->is_china_domain |= is_china_domain;
return 0;
}
struct dns_soa {
const char *name_server;
const char *admin_email;
uint32_t serial;
uint32_t day2;
uint32_t day3;
uint32_t day4;
uint32_t day5;
};
static struct dns_soa _tpl_soa = {
.name_server = "jeff.ns.cloudflare.com",
.admin_email = "dns.cloudflare.com",
.serial = 2322497393,
.day2 = 10000,
.day3 = 2400,
.day4 = 604800,
.day5 = 1800
};
static struct dns_resource _soa = {
.type = NSTYPE_SOA,
.klass = NSCLASS_INET,
.ttl = 3600,
.len = 30,
.domain = "cootail.com",
};
static int do_query_response(dns_udp_context_t *up, struct query_context_t *qc, struct dns_resource *answer, size_t count, int author)
{
int i, len, slen;
uint8_t buf[2048];
struct dns_parser *p = &qc->parser;
p->head.flags |= 0x8000;
// p->head.answer = 0;
// p->head.author = 0;
memcpy(_soa.value, &_tpl_soa, sizeof(_tpl_soa));
const char *lastdn = NULL;
if (p->head.question > 1) {
#if 0
for (i = 1; i < p->head.question; i++) {
struct dns_resource *res = &p->answer[p->head.answer++];
struct dns_question *que0 = &p->question[i -1];
struct dns_question *que1 = &p->question[i];
res->domain = add_domain(p, que0->domain);
res->type = NSTYPE_CNAME;
res->klass = NSCLASS_INET;
res->ttl = 600;
*(const char **)res->value = lastdn = add_domain(p, que1->domain);
}
#endif
if (p->head.answer >= p->head.question)
p->head.answer = p->head.question -1;
p->head.question = 1;
} else {
p->head.answer = 0;
}
if (p->head.answer > 0 && !qc->is_china_domain) {
// p->head.answer = 0;
} else {
lastdn = NULL;
}
if (author == 0) {
struct dns_resource *res;
for (i = 0; i < count; i++) {
res = &answer[i];
// if (res->type != NSTYPE_CNAME)
p->answer[p->head.answer++] = answer[i];
#if 0
if (lastdn && strcasecmp(res->domain, lastdn) == 0) {
res->domain = p->question[0].domain;
}
#endif
}
} else {
struct dns_resource *res;
for (i = 0; i < count; i++) {
res = &p->author[p->head.author];
p->author[p->head.author++] = answer[i];
#if 0
if (lastdn && strcasecmp(res->domain, lastdn) == 0) {
res->domain = p->question[0].domain;
}
#endif
}
}
#if 0
if (qc->is_china_domain) {
p->head.addon = 1;
struct dns_resource *res = &p->addon[0];
res->domain = add_domain(p, "ischina.cn");
res->type = NSTYPE_CNAME;
res->klass = NSCLASS_INET;
res->ttl = 600;
*(const char **)res->value = add_domain(p, "yes.com");
}
#endif
int found = 0;
int type = p->question[0].type;
for (i = 0; found == 0 && i < p->head.answer; i++) {
found = (type == p->answer[i].type);
}
#define NSFLAG_AA 0x0400
#define NSFLAG_RA 0x0080
p->head.flags |= NSFLAG_RA;
for (i = 0; found == 0 && i < p->head.author; i++) {
found = (NSTYPE_SOA == p->author[i].type);
if (found) {
p->author[i] = _soa;
p->head.flags |= NSFLAG_AA;
}
}
if (found == 0) {
assert(p->head.author < ARRAY_SIZE(p->author));
p->author[p->head.author++] = _soa;
p->head.flags |= NSFLAG_AA;
}
len = dns_build(p, buf, sizeof(buf));
if (len == -1)
LOG_DEBUG("dns_build failure");
slen = sendto(up->sockfd, buf, len, 0, (struct sockaddr *)&qc->from, sizeof(qc->from));
LOG_DEBUG("do_query_resource sendto %d %s %s", slen, ntop6(qc->from.sin6_addr), p->question[0].domain);
return 0;
}
static int do_query_resource(dns_udp_context_t *up, struct query_context_t *qc, struct dns_question *que);
static int dns_query_continue(dns_udp_context_t *up, struct query_context_t *qc, struct dns_question *que);
static int do_lookup_update(dns_udp_context_t *up, struct query_context_t *qc, const char *alias)
{
struct dns_parser *p = &qc->parser;
int offset = p->head.question++;
struct dns_question *que = &p->question[offset];
p->question[offset].domain = add_domain(p, alias);
p->question[offset].type = p->question[0].type;
p->question[offset].klass = NSCLASS_INET;
LOG_DEBUG("do_lookup_update: %d cname %s -> %s", offset, p->question[offset -1].domain, alias);
qc->iworker = 0;
qc->nworker = 0;
do_query_resource(up, qc, que);
return 0;
}
static int dns_query_continue(dns_udp_context_t *up, struct query_context_t *qc, struct dns_question *que)
{
struct dns_resource *res = NULL;
struct dns_resource *kes = NULL;
struct dns_resource answer[256] = {};
int count = answer_cache_lookup(que->domain, que->type, answer, 256);
if (count > 0 || qc->is_china_domain) {
struct query_context_t *c = (struct query_context_t *)qc->assioc_uptr;
LOG_DEBUG("answer_cache_lookup: result %d %p %p", count, qc, c);
struct dns_question *que = &qc->parser.question[qc->parser.head.question -1];
int found = 0;
const char *alias = NULL;
for (int i = 0; i < count; i++) {
res = answer + i;
if (que->type == res->type) {
found = 1;
}
if (res->type == NSTYPE_CNAME) {
alias = *(char **)res->value;
qc->parser.answer[qc->parser.head.answer++] = *res;
LOG_DEBUG("%s %s", res->domain, alias);
}
}
if (alias != NULL && found == 0) {
do_lookup_update(up, qc, alias);
return 0;
}
if (qc->assioc_uptr == NULL) {
do_query_response(up, qc, answer, count, 0);
} else if (qc != qc->assioc_uptr) {
qc->refcnt --;
dns_query_append(c, answer, count);
// if (qc->is_china_domain) c->is_china_domain = 1;
dns_query_continue(up, c, &c->parser.question[c->parser.head.question -1]);
}
return count;
}
size_t maxns = 0;
LOG_DEBUG("continue: %s %d", que->domain, que->type);
for (int i = 0; i < qc->nworker; i++) {
res = &qc->worker[i];
if (res->type == NSTYPE_NS &&
strlen(res->domain) > maxns) {
LOG_DEBUG("maxns: %s", res->domain);
maxns = strlen(res->domain);
}
}
LOG_DEBUG("maxns is: %d %s", maxns, que->domain);
int newns = 0;
for (int i = 0; i < nauthor; i++) {
res = &authors[i];
if (res->type != NSTYPE_NS) continue;
const char *left = que->domain;
const char *right = res->domain;
assert(left && right);
int leftlen = strlen(left);
int domainlen = strlen(right);
if (res->domain && domainlen > maxns &&
0 == strcmp(left + leftlen - domainlen, right)) {
LOG_DEBUG("add ns %s %s %d", res->domain, que->domain, maxns);
assert(newns < ARRAY_SIZE(qc->worker));
qc->worker[newns++] = *res;
}
}
if (newns > 0) {
qc->iworker = qc->nworker = newns - 1;
qc->nworker ++;
}
int nscount = qc->nworker;
for (int i = 0; i < nscount; i++) {
res = &qc->worker[i];
if (res->type != NSTYPE_NS) {
continue;
}
if (res->ttl == 0) {
continue;
}
const char *nameserver = *(const char **)res->value;
for (int j = 0; j < nanswer; j++) {
kes = &answsers[j];
if (kes->type == NSTYPE_A && nameserver == kes->domain) {
LOG_DEBUG("add v4 %s %s", res->domain, kes->domain);
int target = *(int *)kes->value;
if (kes->type == NSTYPE_A && lookupRoute4(htonl(target)) == 0) {
LOG_DEBUG("china domain detect");
// qc->is_china_domain = 1;
}
assert(qc->nworker < ARRAY_SIZE(qc->worker));
qc->worker[qc->nworker] = *kes;
qc->iworker = qc->nworker;
qc->nworker ++;
res->ttl = 0;
}
}
if (res->ttl == 0) {
continue;
}
for (int j = 0; j < nauthor; j++) {
kes = &authors[j];
if (kes->type == NSTYPE_A && nameserver == kes->domain) {
LOG_DEBUG("add v4 %s %s", res->domain, kes->domain);
int target = *(int *)kes->value;
if (kes->type == NSTYPE_A && lookupRoute4(htonl(target)) == 0) {
LOG_DEBUG("china domain detect");
// qc->is_china_domain = 1;
}
assert(qc->nworker < ARRAY_SIZE(qc->worker));
qc->worker[qc->nworker] = *kes;
qc->iworker = qc->nworker;
qc->nworker ++;
res->ttl = 0;
}
}
}
if (qc->is_china_domain) {
dns_query_continue(up, qc, que);
return 0;
}
int parallel = 2;
struct in6_addr ipv6_addr;
while (qc->iworker >= 0) {
res = &qc->worker[qc->iworker];
qc->iworker--;
if (res->ttl == 0)
continue;
switch (res->type) {
case NSTYPE_A:
inet_4to6(&ipv6_addr, res->value);
do_fetch_resource(up, qc->parser.head.ident, que, &ipv6_addr, res->domain);
LOG_DEBUG("NSTYPE_A: %s %s\n", res->domain, ntop6(ipv6_addr));
if (parallel-- <= 0) return 0;
res->ttl = 0;
break;
case NSTYPE_AAAA:
do_fetch_resource(up, qc->parser.head.ident, que, (struct in6_addr *)res->value, res->domain);