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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
|
/* $OpenBSD: dig.c,v 1.8 2003/01/03 17:59:45 jason Exp $ */
#ifndef lint
#if 0
static char rcsid[] = "$From: dig.c,v 8.8 1996/05/21 07:32:40 vixie Exp $";
#else
static char rcsid[] = "$OpenBSD: dig.c,v 1.8 2003/01/03 17:59:45 jason Exp $";
#endif
#endif
/*
* ++Copyright++ 1989
* -
* Copyright (c) 1989
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* -
* Portions Copyright (c) 1993 by Digital Equipment Corporation.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies, and that
* the name of Digital Equipment Corporation not be used in advertising or
* publicity pertaining to distribution of the document or software without
* specific, written prior permission.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
* CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
* -
* --Copyright--
*/
/*********************** Notes for the BIND 4.9 release (Paul Vixie, DEC)
* dig 2.0 was written by copying sections of libresolv.a and nslookup
* and modifying them to be more useful for a general lookup utility.
* as of BIND 4.9, the changes needed to support dig have mostly been
* incorporated into libresolv.a and nslookup; dig now links against
* some of nslookup's .o files rather than #including them or maintaining
* local copies of them. in some sense, dig belongs in the nslookup
* subdirectory rather than up here in "tools", but that's for arc@sgi.com
* (owner of nslookup) to decide.
*
* while merging dig back into the BIND release, i made a number of
* structural changes. for one thing, i put all of dig's private
* library routines into this file rather than maintaining them in
* separate, #included, files. i don't like to #include ".c" files.
* i removed all calls to "bcopy", replacing them with structure
* assignments. i removed all "extern"'s of standard functions,
* replacing them with #include's of standard header files. this
* version of dig is probably as portable as the rest of BIND.
*
* i had to remove the query-time and packet-count statistics since
* the current libresolv.a is a lot harder to modify to maintain these
* than the 4.8 one (used in the original dig) was. for consolation,
* i added a "usage" message with extensive help text.
*
* to save my (limited, albeit) sanity, i ran "indent" over the source.
* i also added the standard berkeley/DEC copyrights, since this file now
* contains a fair amount of non-USC code. note that the berkeley and
* DEC copyrights do not prohibit redistribution, with or without fee;
* we add them only to protect ourselves (you have to claim copyright
* in order to disclaim liability and warranty).
*
* Paul Vixie, Palo Alto, CA, April 1993
****************************************************************************
/*******************************************************************
** DiG -- Domain Information Groper **
** **
** dig.c - Version 2.1 (7/12/94) ("BIND takeover") **
** **
** Developed by: Steve Hotz & Paul Mockapetris **
** USC Information Sciences Institute (USC-ISI) **
** Marina del Rey, California **
** 1989 **
** **
** dig.c - **
** Version 2.0 (9/1/90) **
** o renamed difftime() difftv() to avoid **
** clash with ANSI C **
** o fixed incorrect # args to strcmp,gettimeofday **
** o incorrect length specified to strncmp **
** o fixed broken -sticky -envsa -envset functions **
** o print options/flags redefined & modified **
** **
** Version 2.0.beta (5/9/90) **
** o output format - helpful to `doc` **
** o minor cleanup **
** o release to beta testers **
** **
** Version 1.1.beta (10/26/89) **
** o hanging zone transer (when REFUSED) fixed **
** o trailing dot added to domain names in RDATA **
** o ISI internal **
** **
** Version 1.0.tmp (8/27/89) **
** o Error in prnttime() fixed **
** o no longer dumps core on large pkts **
** o zone transfer (axfr) added **
** o -x added for inverse queries **
** (i.e. "dig -x 128.9.0.32") **
** o give address of default server **
** o accept broadcast to server @255.255.255.255 **
** **
** Version 1.0 (3/27/89) **
** o original release **
** **
** DiG is Public Domain, and may be used for any purpose as **
** long as this notice is not removed. **
**** ****
**** NOTE: Version 2.0.beta is not for public distribution ****
**** ****
*******************************************************************/
#define VERSION 22
#define VSTRING "2.2"
#include <sys/types.h>
#include <sys/param.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>
#include <netdb.h>
#include <stdio.h>
#include <resolv.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <setjmp.h>
#include <fcntl.h>
#include "nslookup/res.h"
#include "../conf/portability.h"
#define PRF_DEF 0x2ff9
#define PRF_MIN 0xA930
#define PRF_ZONE 0x24f9
#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 256
#endif
int eecode = 0;
FILE *qfp;
int sockFD;
#define SAVEENV "DiG.env"
#define DIG_MAXARGS 30
char *defsrv, *srvmsg;
char defbuf[40] = "default -- ";
char srvbuf[60];
static void Usage();
static int SetOption(), printZone(), printRR();
static struct timeval difftv();
static void prnttime();
static int xstrtonum();
static void res_re_init();
/* stuff for nslookup modules */
FILE *filePtr;
jmp_buf env;
HostInfo *defaultPtr = NULL;
HostInfo curHostInfo, defaultRec;
int curHostValid = FALSE;
int queryType, queryClass;
extern int StringToClass(), StringToType(); /* subr.c */
#if defined(BSD) && BSD >= 199006 && !defined(RISCOS_BSD)
FILE *yyin = NULL;
void yyrestart(f) { }
#endif
char *pager = NULL;
/* end of nslookup stuff */
/*
** Take arguments appearing in simple string (from file or command line)
** place in char**.
*/
void
stackarg(y, l)
char *l;
char **y;
{
int done=0;
while (!done) {
switch (*l) {
case '\t':
case ' ':
l++; break;
case '\0':
case '\n':
done++;
*y = NULL;
break;
default:
*y++=l;
while (!isspace(*l))
l++;
if (*l == '\n')
done++;
*l++ = '\0';
*y = NULL;
}
}
}
char myhostname[MAXHOSTNAMELEN];
int
main(argc, argv)
int argc;
char **argv;
{
struct hostent *hp;
short port = htons(NAMESERVER_PORT);
/* Wierd stuff for SPARC alignment, hurts nothing else. */
union {
HEADER header_;
u_char packet_[PACKETSZ];
} packet_;
#define packet (packet_.packet_)
u_char answer[8*1024];
int n;
char doping[90];
char pingstr[50];
char *afile;
char *addrc, *addrend, *addrbegin;
struct timeval exectime, tv1, tv2, start_time, end_time, query_time;
time_t tt;
char *srv;
int anyflag = 0;
int sticky = 0;
int tmp;
int qtypeSet;
int addrflag = 0;
int zone = 0;
int bytes_out, bytes_in;
char cmd[256];
char domain[MAXDNAME];
char msg[120], *msgptr;
char **vtmp;
char *args[DIG_MAXARGS];
char **ax;
char **ay;
int once = 1, dofile = 0; /* batch -vs- interactive control */
char fileq[100];
char *qptr;
int fp;
int wait=0, delay;
int envset=0, envsave=0;
struct __res_state res_x, res_t;
char *pp;
res_init();
_res.pfcode = PRF_DEF;
qtypeSet = 0;
bzero(domain, (sizeof domain));
gethostname(myhostname, (sizeof myhostname));
defsrv = strcat(defbuf, inet_ntoa(_res.nsaddr.sin_addr));
res_x = _res;
/*
** If LOCALDEF in environment, should point to file
** containing local favourite defaults. Also look for file
** DiG.env (i.e. SAVEENV) in local directory.
*/
if ((((afile = (char *) getenv("LOCALDEF")) != (char *) NULL) &&
((fp = open(afile, O_RDONLY)) > 0)) ||
((fp = open(SAVEENV, O_RDONLY)) > 0)) {
read(fp, (char *)&res_x, (sizeof res_x));
close(fp);
_res = res_x;
}
/*
** check for batch-mode DiG; also pre-scan for 'help'
*/
vtmp = argv;
ax = args;
while (*vtmp != NULL) {
if (strcmp(*vtmp, "-h") == 0 ||
strcmp(*vtmp, "-help") == 0 ||
strcmp(*vtmp, "-usage") == 0 ||
strcmp(*vtmp, "help") == 0) {
Usage();
exit(0);
}
if (strcmp(*vtmp, "-f") == 0) {
dofile++; once=0;
if ((qfp = fopen(*++vtmp, "r")) == NULL) {
fflush(stdout);
perror("file open");
fflush(stderr);
exit(10);
}
} else {
if (ax - args == DIG_MAXARGS) {
fprintf(stderr, "dig: too many arguments\n");
exit(10);
}
*ax++ = strdup(*vtmp);
}
vtmp++;
}
_res.id = 1;
gettimeofday(&tv1, NULL);
/*
** Main section: once if cmd-line query
** while !EOF if batch mode
*/
*fileq = '\0';
while ((dofile && (fgets(fileq,100,qfp) != NULL)) ||
((!dofile) && (once--)))
{
if ((*fileq=='\n') || (*fileq=='#') || (*fileq==';')) {
continue; /* ignore blank lines & comments */
}
/*
* "sticky" requests that before current parsing args
* return to current "working" environment (X******)
*/
if (sticky) {
printf(";; (using sticky settings)\n");
_res = res_x;
}
/* concat cmd-line and file args */
ay = ax;
qptr = fileq;
stackarg(ay, qptr);
/* defaults */
queryType = T_NS;
queryClass = C_IN;
zone = 0;
*pingstr = 0;
srv = NULL;
sprintf(cmd,"\n; <<>> DiG %s <<>> ",VSTRING);
argv = args;
argc = ax - args;
/*
* More cmd-line options than anyone should ever have to
* deal with ....
*/
while (*(++argv) != NULL && **argv != '\0') {
if ((strlen(*argv) + 2) >
(sizeof(cmd)/sizeof(char) - strlen(cmd))) {
fprintf(stderr, "Command line too long.\n");
exit(1);
}
strcat(cmd,*argv); strcat(cmd," ");
if (**argv == '@') {
srv = (*argv+1);
continue;
}
if (**argv == '%')
continue;
if (**argv == '+') {
SetOption(*argv+1);
continue;
}
if (strncmp(*argv,"-nost",5) == 0) {
sticky = 0;
continue;
} else if (strncmp(*argv,"-st",3) == 0) {
sticky++;
continue;
} else if (strncmp(*argv,"-envsa",6) == 0) {
envsave++;
continue;
} else if (strncmp(*argv,"-envse",6) == 0) {
envset++;
continue;
}
if (**argv == '-') {
switch (argv[0][1]) {
case 'T': wait = atoi(*++argv);
break;
case 'c':
if ((tmp = atoi(*++argv))
|| *argv[0]=='0') {
queryClass = tmp;
} else if (tmp = StringToClass(*argv,
0, NULL)
) {
queryClass = tmp;
} else {
printf(
"; invalid class specified\n"
);
}
break;
case 't':
if ((tmp = atoi(*++argv))
|| *argv[0]=='0') {
queryType = tmp;
qtypeSet++;
} else if (tmp = StringToType(*argv,
0, NULL)
) {
queryType = tmp;
qtypeSet++;
} else {
printf(
"; invalid type specified\n"
);
}
break;
case 'x':
if (!qtypeSet) {
queryType = T_ANY;
qtypeSet++;
}
if (!(addrc = *++argv)) {
printf(
"; no arg for -x?\n"
);
break;
}
addrend = addrc + strlen(addrc);
if (*addrend == '.')
*addrend = '\0';
*domain = '\0';
while (addrbegin = strrchr(addrc,'.')) {
strcat(domain, addrbegin+1);
strcat(domain, ".");
*addrbegin = '\0';
}
strcat(domain, addrc);
strcat(domain, ".in-addr.arpa.");
break;
case 'p': port = htons(atoi(*++argv)); break;
case 'P':
if (argv[0][2] != '\0')
strcpy(pingstr,&argv[0][2]);
else
strcpy(pingstr,"ping -s");
break;
#if defined(__RES) && (__RES >= 19931104)
case 'n':
_res.ndots = atoi(&argv[0][2]);
break;
#endif /*__RES*/
} /* switch - */
continue;
} /* if '-' */
if ((tmp = StringToType(*argv, -1, NULL)) != -1) {
if ((T_ANY == tmp) && anyflag++) {
queryClass = C_ANY;
continue;
}
if (T_AXFR == tmp) {
_res.pfcode = PRF_ZONE;
zone++;
} else {
queryType = tmp;
qtypeSet++;
}
} else if ((tmp = StringToClass(*argv, -1, NULL))
!= -1) {
queryClass = tmp;
} else {
bzero(domain, (sizeof domain));
sprintf(domain,"%s",*argv);
}
} /* while argv remains */
if (_res.pfcode & 0x80000)
printf("; pfcode: %08lx, options: %08lx\n",
_res.pfcode, _res.options);
/*
* Current env. (after this parse) is to become the
* new "working environmnet. Used in conj. with sticky.
*/
if (envset) {
res_x = _res;
envset = 0;
}
/*
* Current env. (after this parse) is to become the
* new default saved environmnet. Save in user specified
* file if exists else is SAVEENV (== "DiG.env").
*/
if (envsave) {
afile = (char *) getenv("LOCALDEF");
if ((afile &&
((fp = open(afile,
O_WRONLY|O_CREAT|O_TRUNC,
S_IREAD|S_IWRITE)) > 0))
||
((fp = open(SAVEENV,
O_WRONLY|O_CREAT|O_TRUNC,
S_IREAD|S_IWRITE)) > 0)) {
write(fp, (char *)&_res, (sizeof _res));
close(fp);
}
envsave = 0;
}
if (_res.pfcode & RES_PRF_CMD)
printf("%s\n", cmd);
addrflag = anyflag = 0;
/*
* Find address of server to query. If not dot-notation, then
* try to resolve domain-name (if so, save and turn off print
* options, this domain-query is not the one we want. Restore
* user options when done.
* Things get a bit weird since we need to use resolver to be
* able to "put the resolver to work".
*/
srvbuf[0] = 0;
srvmsg = defsrv;
if (srv != NULL) {
struct in_addr addr;
if (inet_aton(srv, &addr)) {
_res.nscount = 1;
_res.nsaddr.sin_addr = addr;
srvmsg = strcat(srvbuf, srv);
} else {
res_t = _res;
_res.pfcode = 0;
_res.options = RES_DEFAULT;
res_init();
hp = gethostbyname(srv);
_res = res_t;
if (hp == NULL
|| hp->h_addr_list == NULL
|| *hp->h_addr_list == NULL) {
fflush(stdout);
fprintf(stderr,
"; Bad server: %s -- using default server and timer opts\n",
srv);
fflush(stderr);
srvmsg = defsrv;
srv = NULL;
} else {
u_int32_t **addr;
_res.nscount = 0;
for (addr = (u_int32_t**)hp->h_addr_list;
*addr && (_res.nscount < MAXNS);
addr++) {
_res.nsaddr_list[
_res.nscount++
].sin_addr.s_addr = **addr;
}
srvmsg = strcat(srvbuf,srv);
strcat(srvbuf, " ");
strcat(srvmsg,
inet_ntoa(_res.nsaddr.sin_addr)
);
}
}
printf("; (%d server%s found)\n",
_res.nscount, (_res.nscount==1)?"":"s");
_res.id += _res.retry;
}
{
int i;
for (i = 0; i < _res.nscount; i++) {
_res.nsaddr_list[i].sin_family = AF_INET;
_res.nsaddr_list[i].sin_port = port;
}
_res.id += _res.retry;
}
if (zone) {
int i;
for (i = 0; i < _res.nscount; i++) {
int x = printZone(domain,
&_res.nsaddr_list[i]);
if (_res.pfcode & RES_PRF_STATS) {
struct timeval exectime;
gettimeofday(&exectime,NULL);
printf(";; FROM: %s to SERVER: %s\n",
myhostname,
inet_ntoa(_res.nsaddr_list[i]
.sin_addr));
tt = (time_t)exectime.tv_sec;
printf(";; WHEN: %s", ctime(&tt));
}
if (!x)
break; /* success */
}
fflush(stdout);
continue;
}
if (*domain && !qtypeSet) {
queryType = T_A;
qtypeSet++;
}
bytes_out = n = res_mkquery(QUERY, domain,
queryClass, queryType,
NULL, 0, NULL,
packet, sizeof(packet));
if (n < 0) {
fflush(stderr);
printf(";; res_mkquery: buffer too small\n\n");
continue;
}
eecode = 0;
if (_res.pfcode & RES_PRF_HEAD1)
__fp_resstat(NULL, stdout);
(void) gettimeofday(&start_time, NULL);
if ((bytes_in = n = res_send(packet, n,
answer, sizeof(answer))) < 0) {
fflush(stdout);
n = 0 - n;
msg[0]=0;
strcat(msg,";; res_send to server ");
strcat(msg,srvmsg);
perror(msg);
fflush(stderr);
if (!dofile) {
if (eecode)
exit(eecode);
else
exit(9);
}
}
(void) gettimeofday(&end_time, NULL);
if (_res.pfcode & RES_PRF_STATS) {
query_time = difftv(start_time, end_time);
printf(";; Total query time: ");
prnttime(query_time);
putchar('\n');
gettimeofday(&exectime,NULL);
printf(";; FROM: %s to SERVER: %s\n",
myhostname, srvmsg);
tt = (time_t)exectime.tv_sec;
printf(";; WHEN: %s", ctime(&tt));
printf(";; MSG SIZE sent: %d rcvd: %d\n",
bytes_out, bytes_in);
}
fflush(stdout);
/*
* Argh ... not particularly elegant. Should put in *real* ping code.
* Would necessitate root priviledges for icmp port though!
*/
if (*pingstr) {
sprintf(doping,"%s %s 56 3 | tail -3",pingstr,
(srv==NULL)?(defsrv+10):srv);
system(doping);
}
putchar('\n');
/*
* Fairly crude method and low overhead method of keeping two
* batches started at different sites somewhat synchronized.
*/
gettimeofday(&tv2, NULL);
delay = (int)(tv2.tv_sec - tv1.tv_sec);
if (delay < wait) {
sleep(wait - delay);
}
}
return(eecode);
}
static void
Usage()
{
fputs("\
usage: dig [@server] [domain] [q-type] [q-class] {q-opt} {d-opt} [%comment]\n\
where: server,\n\
domain are names in the Domain Name System\n\
q-class is one of (in,any,...) [default: in]\n\
q-type is one of (a,any,mx,ns,soa,hinfo,axfr,txt,...) [default: a]\n\
", stderr);
fputs("\
q-opt is one of:\n\
-x dot-notation-address (shortcut to in-addr.arpa lookups)\n\
-f file (batch mode input file name)\n\
-T time (batch mode time delay, per query)\n\
-p port (nameserver is on this port) [53]\n\
-Pping-string (see man page)\n\
-t query-type (synonym for q-type)\n\
-c query-class (synonym for q-class)\n\
-envsav,-envset (see man page)\n\
-[no]stick (see man page)\n\
", stderr);
fputs("\
d-opt is of the form ``+keyword=value'' where keyword is one of:\n\
[no]debug [no]d2 [no]recurse retry=# time=# [no]ko [no]vc\n\
[no]defname [no]search domain=NAME [no]ignore [no]primary\n\
[no]aaonly [no]sort [no]cmd [no]stats [no]Header [no]header\n\
[no]ttlid [no]cl [no]qr [no]reply [no]ques [no]answer\n\
[no]author [no]addit pfdef pfmin pfset=# pfand=# pfor=#\n\
", stderr);
fputs("\
notes: defname and search don't work; use fully-qualified names.\n\
", stderr);
}
static int
SetOption(string)
char *string;
{
char option[NAME_LEN];
char type[NAME_LEN];
char *ptr;
char get[80];
int i;
snprintf(get, sizeof get, " %%%ds", sizeof option-1);
i = sscanf(string, get, option);
if (i != 1) {
fprintf(stderr, ";*** Invalid option: %s\n", option);
return(ERROR);
}
if (strncmp(option, "aa", 2) == 0) { /* aaonly */
_res.options |= RES_AAONLY;
} else if (strncmp(option, "noaa", 4) == 0) {
_res.options &= ~RES_AAONLY;
} else if (strncmp(option, "deb", 3) == 0) { /* debug */
_res.options |= RES_DEBUG;
} else if (strncmp(option, "nodeb", 5) == 0) {
_res.options &= ~(RES_DEBUG | RES_DEBUG2);
} else if (strncmp(option, "ko", 2) == 0) { /* keepopen */
_res.options |= (RES_STAYOPEN | RES_USEVC);
} else if (strncmp(option, "noko", 4) == 0) {
_res.options &= ~RES_STAYOPEN;
} else if (strncmp(option, "d2", 2) == 0) { /* d2 (more debug) */
_res.options |= (RES_DEBUG | RES_DEBUG2);
} else if (strncmp(option, "nod2", 4) == 0) {
_res.options &= ~RES_DEBUG2;
} else if (strncmp(option, "def", 3) == 0) { /* defname */
_res.options |= RES_DEFNAMES;
} else if (strncmp(option, "nodef", 5) == 0) {
_res.options &= ~RES_DEFNAMES;
} else if (strncmp(option, "sea", 3) == 0) { /* search list */
_res.options |= RES_DNSRCH;
} else if (strncmp(option, "nosea", 5) == 0) {
_res.options &= ~RES_DNSRCH;
} else if (strncmp(option, "do", 2) == 0) { /* domain */
ptr = strchr(option, '=');
if (ptr != NULL) {
snprintf(get, sizeof get, "%%%ds", sizeof _res.defdname-1);
sscanf(++ptr, get, _res.defdname);
}
} else if (strncmp(option, "ti", 2) == 0) { /* timeout */
ptr = strchr(option, '=');
if (ptr != NULL) {
sscanf(++ptr, "%d", &_res.retrans);
}
} else if (strncmp(option, "ret", 3) == 0) { /* retry */
ptr = strchr(option, '=');
if (ptr != NULL) {
sscanf(++ptr, "%d", &_res.retry);
}
} else if (strncmp(option, "i", 1) == 0) { /* ignore */
_res.options |= RES_IGNTC;
} else if (strncmp(option, "noi", 3) == 0) {
_res.options &= ~RES_IGNTC;
} else if (strncmp(option, "pr", 2) == 0) { /* primary */
_res.options |= RES_PRIMARY;
} else if (strncmp(option, "nop", 3) == 0) {
_res.options &= ~RES_PRIMARY;
} else if (strncmp(option, "rec", 3) == 0) { /* recurse */
_res.options |= RES_RECURSE;
} else if (strncmp(option, "norec", 5) == 0) {
_res.options &= ~RES_RECURSE;
} else if (strncmp(option, "v", 1) == 0) { /* vc */
_res.options |= RES_USEVC;
} else if (strncmp(option, "nov", 3) == 0) {
_res.options &= ~RES_USEVC;
} else if (strncmp(option, "pfset", 5) == 0) {
ptr = strchr(option, '=');
if (ptr != NULL) {
_res.pfcode = xstrtonum(++ptr);
}
} else if (strncmp(option, "pfand", 5) == 0) {
ptr = strchr(option, '=');
if (ptr != NULL) {
_res.pfcode = _res.pfcode & xstrtonum(++ptr);
}
} else if (strncmp(option, "pfor", 4) == 0) {
ptr = strchr(option, '=');
if (ptr != NULL) {
_res.pfcode |= xstrtonum(++ptr);
}
} else if (strncmp(option, "pfmin", 5) == 0) {
_res.pfcode = PRF_MIN;
} else if (strncmp(option, "pfdef", 5) == 0) {
_res.pfcode = PRF_DEF;
} else if (strncmp(option, "an", 2) == 0) { /* answer section */
_res.pfcode |= RES_PRF_ANS;
} else if (strncmp(option, "noan", 4) == 0) {
_res.pfcode &= ~RES_PRF_ANS;
} else if (strncmp(option, "qu", 2) == 0) { /* question section */
_res.pfcode |= RES_PRF_QUES;
} else if (strncmp(option, "noqu", 4) == 0) {
_res.pfcode &= ~RES_PRF_QUES;
} else if (strncmp(option, "au", 2) == 0) { /* authority section */
_res.pfcode |= RES_PRF_AUTH;
} else if (strncmp(option, "noau", 4) == 0) {
_res.pfcode &= ~RES_PRF_AUTH;
} else if (strncmp(option, "ad", 2) == 0) { /* addition section */
_res.pfcode |= RES_PRF_ADD;
} else if (strncmp(option, "noad", 4) == 0) {
_res.pfcode &= ~RES_PRF_ADD;
} else if (strncmp(option, "tt", 2) == 0) { /* TTL & ID */
_res.pfcode |= RES_PRF_TTLID;
} else if (strncmp(option, "nott", 4) == 0) {
_res.pfcode &= ~RES_PRF_TTLID;
} else if (strncmp(option, "he", 2) == 0) { /* head flags stats */
_res.pfcode |= RES_PRF_HEAD2;
} else if (strncmp(option, "nohe", 4) == 0) {
_res.pfcode &= ~RES_PRF_HEAD2;
} else if (strncmp(option, "H", 1) == 0) { /* header all */
_res.pfcode |= RES_PRF_HEADX;
} else if (strncmp(option, "noH", 3) == 0) {
_res.pfcode &= ~(RES_PRF_HEADX);
} else if (strncmp(option, "qr", 2) == 0) { /* query */
_res.pfcode |= RES_PRF_QUERY;
} else if (strncmp(option, "noqr", 4) == 0) {
_res.pfcode &= ~RES_PRF_QUERY;
} else if (strncmp(option, "rep", 3) == 0) { /* reply */
_res.pfcode |= RES_PRF_REPLY;
} else if (strncmp(option, "norep", 5) == 0) {
_res.pfcode &= ~RES_PRF_REPLY;
} else if (strncmp(option, "cm", 2) == 0) { /* command line */
_res.pfcode |= RES_PRF_CMD;
} else if (strncmp(option, "nocm", 4) == 0) {
_res.pfcode &= ~RES_PRF_CMD;
} else if (strncmp(option, "cl", 2) == 0) { /* class mnemonic */
_res.pfcode |= RES_PRF_CLASS;
} else if (strncmp(option, "nocl", 4) == 0) {
_res.pfcode &= ~RES_PRF_CLASS;
} else if (strncmp(option, "st", 2) == 0) { /* stats*/
_res.pfcode |= RES_PRF_STATS;
} else if (strncmp(option, "nost", 4) == 0) {
_res.pfcode &= ~RES_PRF_STATS;
} else {
fprintf(stderr, "; *** Invalid option: %s\n", option);
return(ERROR);
}
res_re_init();
return(SUCCESS);
}
/*
* Force a reinitialization when the domain is changed.
*/
static void
res_re_init()
{
static char localdomain[] = "LOCALDOMAIN";
char *buf;
long pfcode = _res.pfcode;
#if defined(__RES) && (__RES >= 19931104)
long ndots = _res.ndots;
#endif
/* this is ugly but putenv() is more portable than setenv() */
buf = malloc((sizeof localdomain) +strlen(_res.defdname) +10/*fuzz*/);
sprintf(buf, "%s=%s", localdomain, _res.defdname);
putenv(buf); /* keeps the argument, so we won't free it */
res_init();
_res.pfcode = pfcode;
#if defined(__RES) && (__RES >= 19931104)
_res.ndots = ndots;
#endif
}
/*
* convert char string (decimal, octal, or hex) to integer
*/
static int
xstrtonum(p)
char *p;
{
int v = 0;
int i;
int b = 10;
int flag = 0;
while (*p != 0) {
if (!flag++)
if (*p == '0') {
b = 8; p++;
continue;
}
if (isupper(*p))
*p=tolower(*p);
if (*p == 'x') {
b = 16; p++;
continue;
}
if (isdigit(*p)) {
i = *p - '0';
} else if (isxdigit(*p)) {
i = *p - 'a' + 10;
} else {
fprintf(stderr,
"; *** Bad char in numeric string..ignored\n");
i = -1;
}
if (i >= b) {
fprintf(stderr,
"; *** Bad char in numeric string..ignored\n");
i = -1;
}
if (i >= 0)
v = v * b + i;
p++;
}
return(v);
}
/* this code was cloned from nslookup/list.c */
extern char *_res_resultcodes[]; /* res_debug.c */
typedef union {
HEADER qb1;
u_char qb2[PACKETSZ];
} querybuf;
static int
printZone(zone, sin)
char *zone;
struct sockaddr_in *sin;
{
querybuf buf;
HEADER *headerPtr;
int msglen;
int amtToRead;
int numRead;
int numAnswers = 0;
int numRecords = 0;
int result;
int soacnt = 0;
int sockFD;
int count, type, class, rlen, done, n;
u_short len;
u_char *cp;
char dname[2][NAME_LEN];
char file[NAME_LEN];
static u_char *answer = NULL;
static int answerLen = 0;
enum {
NO_ERRORS,
ERR_READING_LEN,
ERR_READING_MSG,
ERR_PRINTING
} error = NO_ERRORS;
/*
* Create a query packet for the requested zone name.
*/
msglen = res_mkquery(QUERY, zone, queryClass, T_AXFR, NULL,
0, 0, buf.qb2, sizeof(buf));
if (msglen < 0) {
if (_res.options & RES_DEBUG) {
fprintf(stderr, ";; res_mkquery failed\n");
}
return (ERROR);
}
/*
* Set up a virtual circuit to the server.
*/
if ((sockFD = socket(sin->sin_family, SOCK_STREAM, 0)) < 0) {
int e = errno;
perror(";; socket");
return(e);
}
if (connect(sockFD, (struct sockaddr *)sin, sizeof(*sin)) < 0) {
int e = errno;
perror(";; connect");
(void) close(sockFD);
sockFD = -1;
return e;
}
/*
* Send length & message for zone transfer
*/
__putshort(msglen, (u_char *)&len);
if (write(sockFD, (char *)&len, INT16SZ) != INT16SZ ||
write(sockFD, (char *)&buf, msglen) != msglen) {
int e = errno;
perror(";; write");
(void) close(sockFD);
sockFD = -1;
return(e);
}
dname[0][0] = '\0';
for (done = 0; !done; NULL) {
u_int16_t tmp;
/*
* Read the length of the response.
*/
cp = (u_char *) &tmp;
amtToRead = INT16SZ;
while (amtToRead > 0 && (numRead=read(sockFD, cp, amtToRead)) > 0){
cp += numRead;
amtToRead -= numRead;
}
if (numRead <= 0) {
error = ERR_READING_LEN;
break;
}
if ((len = _getshort((u_char*)&tmp)) == 0) {
break; /* nothing left to read */
}
/*
* The server sent too much data to fit the existing buffer --
* allocate a new one.
*/
if (len > (u_int)answerLen) {
if (answerLen != 0) {
free(answer);
}
answerLen = len;
answer = (u_char *)Malloc(answerLen);
}
/*
* Read the response.
*/
amtToRead = len;
cp = answer;
while (amtToRead > 0 && (numRead=read(sockFD, cp, amtToRead)) > 0) {
cp += numRead;
amtToRead -= numRead;
}
if (numRead <= 0) {
error = ERR_READING_MSG;
break;
}
result = printRR(stdout, answer, cp);
if (result != 0) {
error = ERR_PRINTING;
break;
}
numRecords += htons(((HEADER *)answer)->ancount);
numAnswers++;
/* Header. */
cp = answer + HFIXEDSZ;
/* Question. */
for (count = ntohs(((HEADER *)answer)->qdcount);
count > 0;
count--)
cp += dn_skipname(cp, answer + len) + QFIXEDSZ;
/* Answer. */
for (count = ntohs(((HEADER *)answer)->ancount);
count > 0;
count--) {
n = dn_expand(answer, answer + len, cp,
dname[soacnt], sizeof dname[0]);
if (n < 0) {
error = ERR_PRINTING;
done++;
break;
}
cp += n;
GETSHORT(type, cp);
GETSHORT(class, cp);
cp += INT32SZ; /* ttl */
GETSHORT(rlen, cp);
cp += rlen;
if (type == T_SOA && soacnt++ &&
!strcasecmp(dname[0], dname[1])) {
done++;
break;
}
}
}
printf(";; Received %d answer%s (%d record%s).\n",
numAnswers, (numAnswers != 1) ? "s" : "",
numRecords, (numRecords != 1) ? "s" : "");
(void) close(sockFD);
sockFD = -1;
switch (error) {
case NO_ERRORS:
return (0);
case ERR_READING_LEN:
return(EMSGSIZE);
case ERR_PRINTING:
return(result);
case ERR_READING_MSG:
return(EMSGSIZE);
default:
return(EFAULT);
}
}
static int
printRR(file, msg, eom)
FILE *file;
u_char *msg, *eom;
{
register u_char *cp;
HEADER *headerPtr;
int type, class, dlen, nameLen;
u_int32_t ttl;
int n, pref;
struct in_addr inaddr;
char name[NAME_LEN];
char name2[NAME_LEN];
Boolean stripped;
/*
* Read the header fields.
*/
headerPtr = (HEADER *)msg;
cp = msg + HFIXEDSZ;
if (headerPtr->rcode != NOERROR) {
return(headerPtr->rcode);
}
/*
* We are looking for info from answer resource records.
* If there aren't any, return with an error. We assume
* there aren't any question records.
*/
if (ntohs(headerPtr->ancount) == 0) {
return(NO_INFO);
}
for (n = ntohs(headerPtr->qdcount); n > 0; n--) {
nameLen = dn_skipname(cp, eom);
if (nameLen < 0)
return (ERROR);
cp += nameLen + QFIXEDSZ;
}
#ifdef PROTOCOLDEBUG
printf(";;; (message of %d octets has %d answers)\n",
eom - msg, ntohs(headerPtr->ancount));
#endif
for (n = ntohs(headerPtr->ancount); n > 0; n--)
cp = (u_char*) p_rr(cp, msg, stdout);
return(SUCCESS);
}
static struct timeval
difftv(a, b)
struct timeval a, b;
{
static struct timeval diff;
diff.tv_sec = b.tv_sec - a.tv_sec;
if ((diff.tv_usec = b.tv_usec - a.tv_usec) < 0) {
diff.tv_sec--;
diff.tv_usec += 1000000;
}
return(diff);
}
static void
prnttime(t)
struct timeval t;
{
printf("%lu msec", (u_long)(t.tv_sec * 1000 + (t.tv_usec / 1000)));
}
|