/
coredns.c
1303 lines (1204 loc) · 35.3 KB
/
coredns.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
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
/*
* coredns.c -- part of dns.mod
* This file contains all core functions needed for the eggdrop dns module.
* Many of them are only minimaly modified from the original source.
*
* Modified/written by Fabian Knittel <fknittel@gmx.de>
* IPv6 support added by pseudo <pseudo@egg6.net>
*/
/*
* Portions Copyright (C) 1999 - 2019 Eggheads Development Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
* Borrowed from mtr -- a network diagnostic tool
* Copyright (C) 1997,1998 Matt Kimball <mkimball@xmission.com>
* Released under the GPL, as above.
*
* Non-blocking DNS portion --
* Copyright (C) 1998 Simon Kirby <sim@neato.org>
* Released under the GPL, as above.
*/
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>
#undef answer /* before resolv.h because it could collide with src/mod/module.h
(dietlibc) */
#include <resolv.h>
#include <errno.h>
/* OpenBSD */
#ifndef NS_GET16
#define NS_GET16 GETSHORT
#endif
#ifndef NS_GET32
#define NS_GET32 GETLONG
#endif
/* Defines */
#define BASH_SIZE 8192 /* Size of hash tables */
#define HOSTNAMELEN 255 /* From RFC */
#define RES_ERR "DNS Resolver error: "
#define RES_MSG "DNS Resolver: "
#define RES_WRN "DNS Resolver warning: "
#define MAX_PACKETSIZE (PACKETSZ)
#define MAX_DOMAINLEN (MAXDNAME)
/* Macros */
#define nonull(s) (s) ? s : nullstring
#define BASH_MODULO(x) ((x) & 8191) /* Modulo for hash table size */
/* Non-blocking nameserver interface routines */
#ifdef DEBUG_DNS
# define RESPONSECODES_COUNT 6
static char *responsecodes[RESPONSECODES_COUNT + 1] = {
"no error",
"format error in query",
"server failure",
"queried domain name does not exist",
"requested query type not implemented",
"refused by name server",
"unknown error",
};
# ifdef IPV6
# define RESOURCETYPES_COUNT 29
# else
# define RESOURCETYPES_COUNT 17
# endif
static const char *resourcetypes[RESOURCETYPES_COUNT + 1] = {
"unknown type",
"A: host address",
"NS: authoritative name server",
"MD: mail destination (OBSOLETE)",
"MF: mail forwarder (OBSOLETE)",
"CNAME: name alias",
"SOA: authority record",
"MB: mailbox domain name (EXPERIMENTAL)",
"MG: mail group member (EXPERIMENTAL)",
"MR: mail rename domain name (EXPERIMENTAL)",
"NULL: NULL RR (EXPERIMENTAL)",
"WKS: well known service description",
"PTR: domain name pointer",
"HINFO: host information",
"MINFO: mailbox or mail list information",
"MX: mail exchange",
"TXT: text string",
# ifdef IPV6
"RP: responsible person",
"AFSDB: AFS cell database",
"X25: X_25 calling address",
"ISDN: ISDN calling address",
"RT: router",
"NSAP: NSAP address",
"NSAP_PTR: reverse NSAP lookup (deprecated)",
"SIG: security signature",
"KEY: security key",
"PX: X.400 mail mapping",
"GPOS: geographical position (withdrawn)",
"AAAA: IP6 Address",
# endif /* IPV6 */
"unknown type",
};
# define CLASSTYPES_COUNT 5
static const char *classtypes[CLASSTYPES_COUNT + 1] = {
"unknown class",
"IN: the Internet",
"CS: CSNET (OBSOLETE)",
"CH: CHAOS",
"HS: Hesiod [Dyer 87]",
"unknown class"
};
#endif /* DEBUG_DNS */
typedef struct {
uint16_t id; /* Packet id */
uint8_t databyte_a;
/* rd:1 recursion desired
* tc:1 truncated message
* aa:1 authoritative answer
* opcode:4 purpose of message
* qr:1 response flag
*/
uint8_t databyte_b;
/* rcode:4 response code
* unassigned:2 unassigned bits
* pr:1 primary server required (non standard)
* ra:1 recursion available
*/
uint16_t qdcount; /* Query record count */
uint16_t ancount; /* Answer record count */
uint16_t nscount; /* Authority reference record count */
uint16_t arcount; /* Resource reference record count */
} packetheader;
#ifndef HFIXEDSZ
#define HFIXEDSZ (sizeof(packetheader))
#endif
/*
* Byte order independent macros for packetheader
*/
#define getheader_rd(x) (x->databyte_a & 1)
#define getheader_tc(x) ((x->databyte_a >> 1) & 1)
#define getheader_aa(x) ((x->databyte_a >> 2) & 1)
#define getheader_opcode(x) ((x->databyte_a >> 3) & 15)
#define getheader_qr(x) (x->databyte_a >> 7)
#define getheader_rcode(x) (x->databyte_b & 15)
#define getheader_pr(x) ((x->databyte_b >> 6) & 1)
#define getheader_ra(x) (x->databyte_b >> 7)
static uint32_t resrecvbuf[(MAX_PACKETSIZE + 7) >> 2]; /* MUST BE DWORD ALIGNED */
static struct resolve *idbash[BASH_SIZE];
static struct resolve *ipbash[BASH_SIZE];
#ifdef IPV6
static struct resolve *ip6bash[BASH_SIZE];
#endif
static struct resolve *hostbash[BASH_SIZE];
static struct resolve *expireresolves = NULL;
static IP localhost;
static long idseed = 0xdeadbeef;
static long aseed;
static int resfd;
static char tempstring[512];
static char namestring[1024 + 1];
static char stackstring[1024 + 1];
#ifdef DEBUG_DNS
static char sendstring[1024 + 1];
#endif /* DEBUG_DNS */
static const char nullstring[] = "";
#ifdef res_ninit
#define MY_RES_INIT() res_ninit(&myres);
#define RES_MKQUERY(a, b, c, d, e, f, g, h, i) \
res_nmkquery(&myres, a, b, c, d, e, f, g, h, i)
struct __res_state myres;
#else
#define MY_RES_INIT() res_init();
#define RES_MKQUERY(a, b, c, d, e, f, g, h, i) \
res_mkquery(a, b, c, d, e, f, g, h, i)
#define myres _res
#endif
/*
* Miscellaneous helper functions
*/
#ifdef DEBUG_DNS
/* Displays the time difference passed in signeddiff.
*/
static char *strtdiff(char *d, long signeddiff)
{
uint32_t diff;
uint32_t seconds, minutes, hours;
long day;
if ((diff = labs(signeddiff))) {
seconds = diff % 60;
diff /= 60;
minutes = diff % 60;
diff /= 60;
hours = diff % 24;
day = signeddiff / (60 * 60 * 24);
if (day)
sprintf(d, "%lid", day);
else
*d = '\0';
if (hours)
sprintf(d + strlen(d), "%uh", hours);
if (minutes)
sprintf(d + strlen(d), "%um", minutes);
if (seconds)
sprintf(d + strlen(d), "%us", seconds);
} else
sprintf(d, "0s");
return d;
}
#endif /* DEBUG_DNS */
/* Allocate memory to hold one resolve request structure.
*/
static struct resolve *allocresolve()
{
struct resolve *rp;
rp = nmalloc(sizeof *rp);
egg_bzero(rp, sizeof(struct resolve));
return rp;
}
/*
* Hash and linked-list related functions
*/
/* Return the hash bucket number for id.
*/
static uint32_t getidbash(uint16_t id)
{
return (uint32_t) BASH_MODULO(id);
}
/* Return the hash bucket number for ip.
*/
static uint32_t getipbash(IP ip)
{
return (uint32_t) BASH_MODULO(ip);
}
#ifdef IPV6
static unsigned long getip6bash(struct in6_addr *ip6) {
uint32_t x, y;
memcpy(&x, ip6->s6_addr , sizeof x);
memcpy(&y, ip6->s6_addr + 12, sizeof y);
x ^= y;
return (unsigned long) BASH_MODULO(x);
}
#endif
/* Return the hash bucket number for host.
*/
static uint32_t gethostbash(char *host)
{
uint32_t bashvalue = 0;
for (; *host; host++) {
bashvalue ^= *host;
bashvalue += (*host >> 1) + (bashvalue >> 1);
}
return BASH_MODULO(bashvalue);
}
/* Insert request structure addrp into the id hash table.
*/
static void linkresolveid(struct resolve *addrp)
{
struct resolve *rp;
uint32_t bashnum;
bashnum = getidbash(addrp->id);
rp = idbash[bashnum];
if (rp) {
while ((rp->nextid) && (addrp->id > rp->nextid->id))
rp = rp->nextid;
while ((rp->previousid) && (addrp->id < rp->previousid->id))
rp = rp->previousid;
if (rp->id < addrp->id) {
addrp->previousid = rp;
addrp->nextid = rp->nextid;
if (rp->nextid)
rp->nextid->previousid = addrp;
rp->nextid = addrp;
} else if (rp->id > addrp->id) {
addrp->previousid = rp->previousid;
addrp->nextid = rp;
if (rp->previousid)
rp->previousid->nextid = addrp;
rp->previousid = addrp;
} else /* Trying to add the same id! */
return;
} else
addrp->nextid = addrp->previousid = NULL;
idbash[bashnum] = addrp;
}
/* Remove request structure rp from the id hash table.
*/
static void unlinkresolveid(struct resolve *rp)
{
uint32_t bashnum;
bashnum = getidbash(rp->id);
if (idbash[bashnum] == rp) {
if (rp->previousid)
idbash[bashnum] = rp->previousid;
else
idbash[bashnum] = rp->nextid;
}
if (rp->nextid)
rp->nextid->previousid = rp->previousid;
if (rp->previousid)
rp->previousid->nextid = rp->nextid;
}
/* Insert request structure addrp into the host hash table.
*/
static void linkresolvehost(struct resolve *addrp)
{
struct resolve *rp;
uint32_t bashnum;
int ret;
bashnum = gethostbash(addrp->hostn);
rp = hostbash[bashnum];
if (rp) {
while ((rp->nexthost) &&
(strcasecmp(addrp->hostn, rp->nexthost->hostn) < 0))
rp = rp->nexthost;
while ((rp->previoushost) &&
(strcasecmp(addrp->hostn, rp->previoushost->hostn) > 0))
rp = rp->previoushost;
ret = strcasecmp(addrp->hostn, rp->hostn);
if (ret < 0) {
addrp->previoushost = rp;
addrp->nexthost = rp->nexthost;
if (rp->nexthost)
rp->nexthost->previoushost = addrp;
rp->nexthost = addrp;
} else if (ret > 0) {
addrp->previoushost = rp->previoushost;
addrp->nexthost = rp;
if (rp->previoushost)
rp->previoushost->nexthost = addrp;
rp->previoushost = addrp;
} else /* Trying to add the same host! */
return;
} else
addrp->nexthost = addrp->previoushost = NULL;
hostbash[bashnum] = addrp;
}
/* Remove request structure rp from the host hash table.
*/
static void unlinkresolvehost(struct resolve *rp)
{
uint32_t bashnum;
bashnum = gethostbash(rp->hostn);
if (hostbash[bashnum] == rp) {
if (rp->previoushost)
hostbash[bashnum] = rp->previoushost;
else
hostbash[bashnum] = rp->nexthost;
}
if (rp->nexthost)
rp->nexthost->previoushost = rp->previoushost;
if (rp->previoushost)
rp->previoushost->nexthost = rp->nexthost;
nfree(rp->hostn);
}
/* Insert request structure addrp into the ip hash table.
*/
static void linkresolveip(struct resolve *addrp)
{
struct resolve *rp;
uint32_t bashnum;
bashnum = getipbash(addrp->ip);
rp = ipbash[bashnum];
if (rp) {
while ((rp->nextip) && (addrp->ip > rp->nextip->ip))
rp = rp->nextip;
while ((rp->previousip) && (addrp->ip < rp->previousip->ip))
rp = rp->previousip;
if (rp->ip < addrp->ip) {
addrp->previousip = rp;
addrp->nextip = rp->nextip;
if (rp->nextip)
rp->nextip->previousip = addrp;
rp->nextip = addrp;
} else if (rp->ip > addrp->ip) {
addrp->previousip = rp->previousip;
addrp->nextip = rp;
if (rp->previousip)
rp->previousip->nextip = addrp;
rp->previousip = addrp;
} else /* Trying to add the same ip! */
return;
} else
addrp->nextip = addrp->previousip = NULL;
ipbash[bashnum] = addrp;
}
#ifdef IPV6
static void linkresolveip6(struct resolve *addrp){
struct resolve *rp;
unsigned long bashnum;
bashnum = getip6bash(&addrp->sockname.addr.s6.sin6_addr);
rp = ip6bash[bashnum];
if (rp){
while ((rp->nextip) &&
(addrp->sockname.addr.s6.sin6_addr.s6_addr[15] >
rp->nextip->sockname.addr.s6.sin6_addr.s6_addr[15]))
rp = rp->nextip;
while ((rp->previousip) &&
(addrp->sockname.addr.s6.sin6_addr.s6_addr[15] <
rp->previousip->sockname.addr.s6.sin6_addr.s6_addr[15]))
rp = rp->previousip;
if (rp->sockname.addr.s6.sin6_addr.s6_addr[15] <
addrp->sockname.addr.s6.sin6_addr.s6_addr[15]) {
addrp->previousip = rp;
addrp->nextip = rp->nextip;
if (rp->nextip)
rp->nextip->previousip = addrp;
rp->nextip = addrp;
} else {
addrp->previousip = rp->previousip;
addrp->nextip = rp;
if (rp->previousip)
rp->previousip->nextip = addrp;
rp->previousip = addrp;
}
} else
addrp->nextip = addrp->previousip = NULL;
ip6bash[bashnum] = addrp;
}
static void unlinkresolveip6(struct resolve *rp)
{
uint32_t bashnum;
bashnum = getip6bash(&rp->sockname.addr.s6.sin6_addr);
if (ip6bash[bashnum] == rp) {
if (rp->previousip)
ip6bash[bashnum] = rp->previousip;
else
ip6bash[bashnum] = rp->nextip;
}
if (rp->nextip)
rp->nextip->previousip = rp->previousip;
if (rp->previousip)
rp->previousip->nextip = rp->nextip;
}
#endif
/* Remove request structure rp from the ip hash table.
*/
static void unlinkresolveip(struct resolve *rp)
{
uint32_t bashnum;
bashnum = getipbash(rp->ip);
if (ipbash[bashnum] == rp) {
if (rp->previousip)
ipbash[bashnum] = rp->previousip;
else
ipbash[bashnum] = rp->nextip;
}
if (rp->nextip)
rp->nextip->previousip = rp->previousip;
if (rp->previousip)
rp->previousip->nextip = rp->nextip;
}
/* Add request structure rp to the expireresolves list. Entries are sorted
* by expire time.
*/
static void linkresolve(struct resolve *rp)
{
struct resolve *irp;
if (expireresolves) {
irp = expireresolves;
while ((irp->next) && (rp->expiretime >= irp->expiretime))
irp = irp->next;
if (rp->expiretime >= irp->expiretime) {
rp->next = NULL;
rp->previous = irp;
irp->next = rp;
} else {
rp->previous = irp->previous;
rp->next = irp;
if (irp->previous)
irp->previous->next = rp;
else
expireresolves = rp;
irp->previous = rp;
}
} else {
rp->next = NULL;
rp->previous = NULL;
expireresolves = rp;
}
}
/* Remove request structure rp from the expireresolves list.
*/
static void untieresolve(struct resolve *rp)
{
if (rp->previous)
rp->previous->next = rp->next;
else
expireresolves = rp->next;
if (rp->next)
rp->next->previous = rp->previous;
}
/* Remove request structure rp from all lists and hash tables and
* then delete and free the structure
*/
static void unlinkresolve(struct resolve *rp)
{
untieresolve(rp); /* Not really needed. Left in to be on the
* safe side. */
unlinkresolveid(rp);
#ifdef IPV6
if (rp->sockname.family == AF_INET6)
unlinkresolveip6(rp);
else
#endif
unlinkresolveip(rp);
if (rp->hostn)
unlinkresolvehost(rp);
nfree(rp);
}
/* Find request structure using the id.
*/
static struct resolve *findid(uint16_t id)
{
struct resolve *rp;
int bashnum;
bashnum = getidbash(id);
rp = idbash[bashnum];
if (rp) {
while ((rp->nextid) && (id >= rp->nextid->id))
rp = rp->nextid;
while ((rp->previousid) && (id <= rp->previousid->id))
rp = rp->previousid;
if (id == rp->id) {
idbash[bashnum] = rp;
return rp;
} else
return NULL;
}
return rp; /* NULL */
}
/* Find request structure using the host.
*/
static struct resolve *findhost(char *hostn)
{
struct resolve *rp;
int bashnum;
bashnum = gethostbash(hostn);
rp = hostbash[bashnum];
if (rp) {
while ((rp->nexthost) &&
(strcasecmp(hostn, rp->nexthost->hostn) >= 0))
rp = rp->nexthost;
while ((rp->previoushost) &&
(strcasecmp(hostn, rp->previoushost->hostn) <= 0))
rp = rp->previoushost;
if (strcasecmp(hostn, rp->hostn))
return NULL;
else {
hostbash[bashnum] = rp;
return rp;
}
}
return rp; /* NULL */
}
/* Find request structure using the ip.
*/
#ifdef IPV6
static struct resolve *findip6(struct in6_addr *ip6)
{
struct resolve *rp;
unsigned long bashnum;
bashnum = getip6bash(ip6);
rp = ip6bash[bashnum];
if (rp){
while ((rp->nextip) && (ip6->s6_addr[15] >=
(rp->nextip->sockname.addr.s6.sin6_addr.s6_addr[15])))
rp = rp->nextip;
while ((rp->previousip) && (ip6->s6_addr[15] <=
(rp->previousip->sockname.addr.s6.sin6_addr.s6_addr[15])))
rp = rp->previousip;
if (IN6_ARE_ADDR_EQUAL(ip6, &rp->sockname.addr.s6.sin6_addr)) {
ip6bash[bashnum] = rp;
return rp;
} else return NULL;
}
return rp; /* NULL */
}
#endif
static struct resolve *findip(IP ip)
{
struct resolve *rp;
uint32_t bashnum;
bashnum = getipbash(ip);
rp = ipbash[bashnum];
if (rp) {
while ((rp->nextip) && (ip >= rp->nextip->ip))
rp = rp->nextip;
while ((rp->previousip) && (ip <= rp->previousip->ip))
rp = rp->previousip;
if (ip == rp->ip) {
ipbash[bashnum] = rp;
return rp;
} else
return NULL;
}
return rp; /* NULL */
}
void ptrstring4(IP *ip, char *buf, size_t sz)
{
egg_snprintf(buf, sz, "%u.%u.%u.%u.in-addr.arpa",
((uint8_t *) ip)[3],
((uint8_t *) ip)[2],
((uint8_t *) ip)[1],
((uint8_t *) ip)[0]);
}
#ifdef IPV6
void ptrstring6(struct in6_addr *ip6, char *buf, size_t sz)
{
int i;
char *p, *q;
const char *hex = "0123456789abcdef";
p = buf;
q = buf + sz;
for (i = sizeof(struct in6_addr) - 1; i >= 0 && p < q; i--) {
*p++ = hex[ip6->s6_addr[i] & 0x0f];
*p++ = '.';
*p++ = hex[(ip6->s6_addr[i] >> 4) & 0x0f];
*p++ = '.';
*p = '\0';
}
strcpy(p, "ip6.arpa"); /* ip6.int is deprecated */
}
#endif
void ptrstring(struct sockaddr *addr, char *buf, size_t sz)
{
if (addr->sa_family == AF_INET)
ptrstring4((IP *) &((struct sockaddr_in *)addr)->sin_addr.s_addr,
buf, sz);
#ifdef IPV6
else if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)addr)->sin6_addr))
ptrstring4((IP *) &((struct sockaddr_in6 *)addr)->sin6_addr.s6_addr[12],
buf, sz);
else ptrstring6(&((struct sockaddr_in6 *)addr)->sin6_addr, buf, sz);
#endif
}
/*
* Network and resolver related functions
*/
/* Create packet for the request and send it to all available nameservers.
*/
static void dorequest(char *s, int type, uint16_t id)
{
packetheader *hp;
int r, i;
uint8_t *buf;
/* Use malloc here instead of a static buffer, as per res_mkquery()'s manual
* buf should be aligned on an eight byte boundary. malloc() should return a
* pointer to an address properly aligned for any data type. Failing to
* provide a aligned buffer will result in a SIGBUS crash at least on SPARC
* CPUs.
*/
buf = nmalloc(MAX_PACKETSIZE + 1);
r = RES_MKQUERY(QUERY, s, C_IN, type, NULL, 0, NULL, buf, MAX_PACKETSIZE);
if (r == -1) {
nfree(buf);
ddebug0(RES_ERR "Query too large.");
return;
}
hp = (packetheader *) buf;
hp->id = id; /* htons() deliberately left out (redundant) */
for (i = 0; i < myres.nscount; i++)
(void) sendto(resfd, buf, r, 0,
(struct sockaddr *) &myres.nsaddr_list[i],
sizeof(struct sockaddr));
nfree(buf);
}
/* (Re-)send request with existing id.
*/
static void resendrequest(struct resolve *rp, int type)
{
rp->sends++;
/* Update expire time */
rp->expiretime = now + (dns_retrydelay * rp->sends);
/* Add (back) to expire list */
linkresolve(rp);
if (type == T_A) {
#ifdef IPV6
dorequest(rp->hostn, T_AAAA, rp->id);
#endif
dorequest(rp->hostn, type, rp->id);
ddebug1(RES_MSG "Sent domain lookup request for \"%s\".", rp->hostn);
} else if (type == T_PTR) {
ptrstring(&rp->sockname.addr.sa, tempstring, sizeof tempstring);
dorequest(tempstring, type, rp->id);
ddebug1(RES_MSG "Sent domain lookup request for \"%s\".",
iptostr(&rp->sockname.addr.sa));
}
}
/* Send request for the first time.
*/
static void sendrequest(struct resolve *rp, int type)
{
/* Create unique id */
do {
idseed = (((idseed + idseed) | (long) time(NULL))
+ idseed - 0x54bad4a) ^ aseed;
aseed ^= idseed;
rp->id = (uint16_t) idseed;
} while (findid(rp->id));
linkresolveid(rp); /* Add id to id hash table */
resendrequest(rp, type); /* Send request */
}
/* Gets called as soon as the request turns out to have failed. Calls
* the eggdrop hook */
static void failrp(struct resolve *rp, int type)
{
if (rp->state == STATE_FINISHED)
return;
rp->expiretime = now + dns_negcache;
rp->state = STATE_FAILED;
/* Expire time was changed, reinsert entry to maintain order */
untieresolve(rp);
linkresolve(rp);
ddebug0(RES_MSG "Lookup failed.");
dns_event_failure(rp, type);
}
/* Gets called as soon as the request turns out to be successful. Calls
* the eggdrop hook.
*/
static void passrp(struct resolve *rp, long ttl, int type)
{
rp->state = STATE_FINISHED;
/* Do not cache entries for too long. */
if (ttl < dns_cache)
rp->expiretime = now + (time_t) ttl;
else
rp->expiretime = now + dns_cache;
/* Expire time was changed, reinsert entry to maintain order */
untieresolve(rp);
linkresolve(rp);
ddebug1(RES_MSG "Lookup successful: %s", rp->hostn);
dns_event_success(rp, type);
}
/* Parses the response packets received.
*/
void parserespacket(uint8_t *response, int len)
{
#ifdef IPV6
int ready = 0;
#endif
int r, rcount;
packetheader *hdr;
struct resolve *rp;
uint8_t rc, *c = response;
uint16_t qdatatype, qclass, datatype, class, datalength;
uint32_t ttl;
if (len < sizeof(packetheader)) {
debug1(RES_ERR "Packet smaller than standard header size: %d bytes.", len);
return;
}
hdr = ((packetheader *) response);
rp = findid(hdr->id);
if (!rp || (rp->state == STATE_FINISHED) || (rp->state == STATE_FAILED))
return;
if (getheader_tc(hdr)) {
ddebug0(RES_ERR "Nameserver packet truncated.");
return;
}
if (!getheader_qr(hdr)) {
ddebug0(RES_ERR "Query packet received on nameserver communication "
"socket.");
return;
}
if (getheader_opcode(hdr)) {
ddebug0(RES_ERR "Invalid opcode in response packet.");
return;
}
rc = getheader_rcode(hdr);
switch (rc) {
case NOERROR:
break;
case NXDOMAIN:
ddebug0(RES_MSG "Domain does not exist.");
failrp(rp, IS_A(rp) ? T_A : T_PTR);
return;
default:
ddebug2(RES_ERR "Received error response %u. (%s)",
rc, rc < RESPONSECODES_COUNT ?
responsecodes[rc] : responsecodes[RESPONSECODES_COUNT]);
return;
}
hdr->qdcount = ntohs(hdr->qdcount);
hdr->ancount = ntohs(hdr->ancount);
hdr->nscount = ntohs(hdr->nscount);
hdr->arcount = ntohs(hdr->arcount);
ddebug4(RES_MSG "Received nameserver reply. (qd:%u an:%u ns:%u ar:%u)",
hdr->qdcount, hdr->ancount, hdr->nscount, hdr->arcount);
if (hdr->qdcount != 1) {
ddebug0(RES_ERR "Reply does not contain one query.");
return;
}
switch (rp->state) {
case STATE_PTRREQ:
if (!hdr->ancount) {
ddebug0(RES_ERR "No error returned but no answers given.");
return;
}
ptrstring(&rp->sockname.addr.sa, stackstring, sizeof stackstring);
break;
case STATE_AREQ:
strncpy(stackstring, rp->hostn, 1024);
}
c = response + HFIXEDSZ;
r = dn_expand(response, response + len, c, namestring, MAXDNAME);
if (r == -1) {
ddebug0(RES_ERR "dn_expand() failed while expanding query domain.");
return;
}
if (strcasecmp(stackstring, namestring)) {
ddebug2(RES_MSG "Unknown query packet dropped. (\"%s\" does not "
"match \"%s\")", stackstring, namestring);
return;
}
c += r;
if (c + 4 > response + len) {
ddebug0(RES_ERR "Query resource record truncated.");
return;
}
NS_GET16(qdatatype, c);
NS_GET16(qclass, c);
if (qclass != C_IN) {
ddebug2(RES_ERR "Received unsupported query class: %u (%s)",
qclass, (qclass < CLASSTYPES_COUNT) ?
classtypes[qclass] : classtypes[CLASSTYPES_COUNT]);
return;
}
switch (qdatatype) {
case T_A:
if (!IS_A(rp)) {
ddebug0(RES_WRN "Ignoring response with unexpected query type \"A\".");
return;
}
#ifndef IPV6
rp->sockname.family = AF_INET;
break;
#else
if (rp->sockname.family == AF_INET6)
ready = 1;
rp->sockname.family = AF_INET;
break;
case T_AAAA:
if (!IS_A(rp)) {
ddebug0(RES_WRN "Ignoring response with unexpected query type "
"\"AAAA\".");
return;
}
if (rp->sockname.family == AF_INET)
ready = 1;
rp->sockname.family = AF_INET6;
break;
#endif
case T_PTR:
if (!IS_PTR(rp)) {
ddebug0(RES_WRN "Ignoring response with unexpected query type "
"\"PTR\".");
return;
}
break;
default:
ddebug2(RES_ERR "Received unimplemented query type: %u (%s)",
qdatatype, (qdatatype < RESOURCETYPES_COUNT) ?
resourcetypes[qdatatype] : resourcetypes[RESOURCETYPES_COUNT]);
return;
}
for (rcount = hdr->ancount + hdr->nscount + hdr->arcount; rcount; rcount--) {
r = dn_expand(response, response + len, c, namestring, MAXDNAME);
if (r == -1) {
ddebug0(RES_ERR "dn_expand() failed while expanding answer domain.");
return;
}
ddebug1(RES_MSG "answered domain query: \"%s\"", namestring);
c += r;
if (c + 10 > response + len) {
ddebug0(RES_ERR "Resource record truncated.");
return;
}
NS_GET16(datatype, c);
NS_GET16(class, c);
NS_GET32(ttl, c);
NS_GET16(datalength, c);
if (class != qclass) {
ddebug2(RES_ERR "Answered class (%s) does not match queried class (%s).",
(class < CLASSTYPES_COUNT) ?
classtypes[class] : classtypes[CLASSTYPES_COUNT],
(qclass < CLASSTYPES_COUNT) ?
classtypes[qclass] : classtypes[CLASSTYPES_COUNT]);
return;
}
if ((c + datalength) > (response + len)) {
ddebug0(RES_ERR "Specified rdata length exceeds packet size.");
return;
}
if (strcasecmp(stackstring, namestring))
continue;
if (datatype != qdatatype && datatype != T_CNAME) {
ddebug2(RES_MSG "Ignoring resource type %u. (%s)",
datatype, (datatype < RESOURCETYPES_COUNT) ?
resourcetypes[datatype] :
resourcetypes[RESOURCETYPES_COUNT]);
continue;
}
ddebug1(RES_MSG "TTL: %s", strtdiff(sendstring, ttl));
ddebug1(RES_MSG "TYPE: %s", (datatype < RESOURCETYPES_COUNT) ?
resourcetypes[datatype] : resourcetypes[RESOURCETYPES_COUNT]);
switch (datatype) {
case T_A:
if (datalength != 4) {
ddebug1(RES_ERR "Unsupported rdata format for \"A\" type. "