No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / bind / dist / bin / dig / host.c
blobf5d69eb3df4451c9fa6860959d2272eb33705328
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004-2007, 2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2000-2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: host.c,v 1.120 2009/09/29 15:06:05 fdupont Exp */
22 /*! \file */
24 #include <config.h>
25 #include <stdlib.h>
26 #include <limits.h>
28 #ifdef HAVE_LOCALE_H
29 #include <locale.h>
30 #endif
32 #ifdef WITH_IDN
33 #include <idn/result.h>
34 #include <idn/log.h>
35 #include <idn/resconf.h>
36 #include <idn/api.h>
37 #endif
39 #include <isc/app.h>
40 #include <isc/commandline.h>
41 #include <isc/netaddr.h>
42 #include <isc/print.h>
43 #include <isc/string.h>
44 #include <isc/util.h>
45 #include <isc/task.h>
46 #include <isc/stdlib.h>
48 #include <dns/byaddr.h>
49 #include <dns/fixedname.h>
50 #include <dns/message.h>
51 #include <dns/name.h>
52 #include <dns/rdata.h>
53 #include <dns/rdataclass.h>
54 #include <dns/rdataset.h>
55 #include <dns/rdatatype.h>
56 #include <dns/rdatastruct.h>
58 #include <dig/dig.h>
60 static isc_boolean_t short_form = ISC_TRUE, listed_server = ISC_FALSE;
61 static isc_boolean_t default_lookups = ISC_TRUE;
62 static int seen_error = -1;
63 static isc_boolean_t list_addresses = ISC_TRUE;
64 static dns_rdatatype_t list_type = dns_rdatatype_a;
65 static isc_boolean_t printed_server = ISC_FALSE;
67 static const char *opcodetext[] = {
68 "QUERY",
69 "IQUERY",
70 "STATUS",
71 "RESERVED3",
72 "NOTIFY",
73 "UPDATE",
74 "RESERVED6",
75 "RESERVED7",
76 "RESERVED8",
77 "RESERVED9",
78 "RESERVED10",
79 "RESERVED11",
80 "RESERVED12",
81 "RESERVED13",
82 "RESERVED14",
83 "RESERVED15"
86 static const char *rcodetext[] = {
87 "NOERROR",
88 "FORMERR",
89 "SERVFAIL",
90 "NXDOMAIN",
91 "NOTIMP",
92 "REFUSED",
93 "YXDOMAIN",
94 "YXRRSET",
95 "NXRRSET",
96 "NOTAUTH",
97 "NOTZONE",
98 "RESERVED11",
99 "RESERVED12",
100 "RESERVED13",
101 "RESERVED14",
102 "RESERVED15",
103 "BADVERS"
106 struct rtype {
107 unsigned int type;
108 const char *text;
111 struct rtype rtypes[] = {
112 { 1, "has address" },
113 { 2, "name server" },
114 { 5, "is an alias for" },
115 { 11, "has well known services" },
116 { 12, "domain name pointer" },
117 { 13, "host information" },
118 { 15, "mail is handled by" },
119 { 16, "descriptive text" },
120 { 19, "x25 address" },
121 { 20, "ISDN address" },
122 { 24, "has signature" },
123 { 25, "has key" },
124 { 28, "has IPv6 address" },
125 { 29, "location" },
126 { 0, NULL }
129 static char *
130 rcode_totext(dns_rcode_t rcode)
132 static char buf[sizeof("?65535")];
133 union {
134 const char *consttext;
135 char *deconsttext;
136 } totext;
138 if (rcode >= (sizeof(rcodetext)/sizeof(rcodetext[0]))) {
139 snprintf(buf, sizeof(buf), "?%u", rcode);
140 totext.deconsttext = buf;
141 } else
142 totext.consttext = rcodetext[rcode];
143 return totext.deconsttext;
146 ISC_PLATFORM_NORETURN_PRE static void
147 show_usage(void) ISC_PLATFORM_NORETURN_POST;
149 static void
150 show_usage(void) {
151 fputs(
152 "Usage: host [-aCdlriTwv] [-c class] [-N ndots] [-t type] [-W time]\n"
153 " [-R number] [-m flag] hostname [server]\n"
154 " -a is equivalent to -v -t ANY\n"
155 " -c specifies query class for non-IN data\n"
156 " -C compares SOA records on authoritative nameservers\n"
157 " -d is equivalent to -v\n"
158 " -l lists all hosts in a domain, using AXFR\n"
159 " -i IP6.INT reverse lookups\n"
160 " -N changes the number of dots allowed before root lookup is done\n"
161 " -r disables recursive processing\n"
162 " -R specifies number of retries for UDP packets\n"
163 " -s a SERVFAIL response should stop query\n"
164 " -t specifies the query type\n"
165 " -T enables TCP/IP mode\n"
166 " -v enables verbose output\n"
167 " -w specifies to wait forever for a reply\n"
168 " -W specifies how long to wait for a reply\n"
169 " -4 use IPv4 query transport only\n"
170 " -6 use IPv6 query transport only\n"
171 " -m set memory debugging flag (trace|record|usage)\n", stderr);
172 exit(1);
175 void
176 dighost_shutdown(void) {
177 isc_app_shutdown();
180 void
181 received(int bytes, isc_sockaddr_t *from, dig_query_t *query) {
182 isc_time_t now;
183 int diff;
185 if (!short_form) {
186 char fromtext[ISC_SOCKADDR_FORMATSIZE];
187 isc_sockaddr_format(from, fromtext, sizeof(fromtext));
188 TIME_NOW(&now);
189 diff = (int) isc_time_microdiff(&now, &query->time_sent);
190 printf("Received %u bytes from %s in %d ms\n",
191 bytes, fromtext, diff/1000);
195 void
196 trying(char *frm, dig_lookup_t *lookup) {
197 UNUSED(lookup);
199 if (!short_form)
200 printf("Trying \"%s\"\n", frm);
203 static void
204 say_message(dns_name_t *name, const char *msg, dns_rdata_t *rdata,
205 dig_query_t *query)
207 isc_buffer_t *b = NULL;
208 char namestr[DNS_NAME_FORMATSIZE];
209 isc_region_t r;
210 isc_result_t result;
211 unsigned int bufsize = BUFSIZ;
213 dns_name_format(name, namestr, sizeof(namestr));
214 retry:
215 result = isc_buffer_allocate(mctx, &b, bufsize);
216 check_result(result, "isc_buffer_allocate");
217 result = dns_rdata_totext(rdata, NULL, b);
218 if (result == ISC_R_NOSPACE) {
219 isc_buffer_free(&b);
220 bufsize *= 2;
221 goto retry;
223 check_result(result, "dns_rdata_totext");
224 isc_buffer_usedregion(b, &r);
225 if (query->lookup->identify_previous_line) {
226 printf("Nameserver %s:\n\t",
227 query->servname);
229 printf("%s %s %.*s", namestr,
230 msg, (int)r.length, (char *)r.base);
231 if (query->lookup->identify) {
232 printf(" on server %s", query->servname);
234 printf("\n");
235 isc_buffer_free(&b);
237 #ifdef DIG_SIGCHASE
238 /* Just for compatibility : not use in host program */
239 isc_result_t
240 printrdataset(dns_name_t *owner_name, dns_rdataset_t *rdataset,
241 isc_buffer_t *target)
243 UNUSED(owner_name);
244 UNUSED(rdataset);
245 UNUSED(target);
246 return(ISC_FALSE);
248 #endif
249 static isc_result_t
250 printsection(dns_message_t *msg, dns_section_t sectionid,
251 const char *section_name, isc_boolean_t headers,
252 dig_query_t *query)
254 dns_name_t *name, *print_name;
255 dns_rdataset_t *rdataset;
256 dns_rdata_t rdata = DNS_RDATA_INIT;
257 isc_buffer_t target;
258 isc_result_t result, loopresult;
259 isc_region_t r;
260 dns_name_t empty_name;
261 char t[4096];
262 isc_boolean_t first;
263 isc_boolean_t no_rdata;
265 if (sectionid == DNS_SECTION_QUESTION)
266 no_rdata = ISC_TRUE;
267 else
268 no_rdata = ISC_FALSE;
270 if (headers)
271 printf(";; %s SECTION:\n", section_name);
273 dns_name_init(&empty_name, NULL);
275 result = dns_message_firstname(msg, sectionid);
276 if (result == ISC_R_NOMORE)
277 return (ISC_R_SUCCESS);
278 else if (result != ISC_R_SUCCESS)
279 return (result);
281 for (;;) {
282 name = NULL;
283 dns_message_currentname(msg, sectionid, &name);
285 isc_buffer_init(&target, t, sizeof(t));
286 first = ISC_TRUE;
287 print_name = name;
289 for (rdataset = ISC_LIST_HEAD(name->list);
290 rdataset != NULL;
291 rdataset = ISC_LIST_NEXT(rdataset, link)) {
292 if (query->lookup->rdtype == dns_rdatatype_axfr &&
293 !((!list_addresses &&
294 (list_type == dns_rdatatype_any ||
295 rdataset->type == list_type)) ||
296 (list_addresses &&
297 (rdataset->type == dns_rdatatype_a ||
298 rdataset->type == dns_rdatatype_aaaa ||
299 rdataset->type == dns_rdatatype_ns ||
300 rdataset->type == dns_rdatatype_ptr))))
301 continue;
302 if (!short_form) {
303 result = dns_rdataset_totext(rdataset,
304 print_name,
305 ISC_FALSE,
306 no_rdata,
307 &target);
308 if (result != ISC_R_SUCCESS)
309 return (result);
310 #ifdef USEINITALWS
311 if (first) {
312 print_name = &empty_name;
313 first = ISC_FALSE;
315 #else
316 UNUSED(first); /* Shut up compiler. */
317 #endif
318 } else {
319 loopresult = dns_rdataset_first(rdataset);
320 while (loopresult == ISC_R_SUCCESS) {
321 struct rtype *t;
322 const char *rtt;
323 char typebuf[DNS_RDATATYPE_FORMATSIZE];
324 char typebuf2[DNS_RDATATYPE_FORMATSIZE
325 + 20];
326 dns_rdataset_current(rdataset, &rdata);
328 for (t = rtypes; t->text != NULL; t++) {
329 if (t->type == rdata.type) {
330 rtt = t->text;
331 goto found;
335 dns_rdatatype_format(rdata.type,
336 typebuf,
337 sizeof(typebuf));
338 snprintf(typebuf2, sizeof(typebuf2),
339 "has %s record", typebuf);
340 rtt = typebuf2;
341 found:
342 say_message(print_name, rtt,
343 &rdata, query);
344 dns_rdata_reset(&rdata);
345 loopresult =
346 dns_rdataset_next(rdataset);
350 if (!short_form) {
351 isc_buffer_usedregion(&target, &r);
352 if (no_rdata)
353 printf(";%.*s", (int)r.length,
354 (char *)r.base);
355 else
356 printf("%.*s", (int)r.length, (char *)r.base);
359 result = dns_message_nextname(msg, sectionid);
360 if (result == ISC_R_NOMORE)
361 break;
362 else if (result != ISC_R_SUCCESS)
363 return (result);
366 return (ISC_R_SUCCESS);
369 static isc_result_t
370 printrdata(dns_message_t *msg, dns_rdataset_t *rdataset, dns_name_t *owner,
371 const char *set_name, isc_boolean_t headers)
373 isc_buffer_t target;
374 isc_result_t result;
375 isc_region_t r;
376 char t[4096];
378 UNUSED(msg);
379 if (headers)
380 printf(";; %s SECTION:\n", set_name);
382 isc_buffer_init(&target, t, sizeof(t));
384 result = dns_rdataset_totext(rdataset, owner, ISC_FALSE, ISC_FALSE,
385 &target);
386 if (result != ISC_R_SUCCESS)
387 return (result);
388 isc_buffer_usedregion(&target, &r);
389 printf("%.*s", (int)r.length, (char *)r.base);
391 return (ISC_R_SUCCESS);
394 static void
395 chase_cnamechain(dns_message_t *msg, dns_name_t *qname) {
396 isc_result_t result;
397 dns_rdataset_t *rdataset;
398 dns_rdata_cname_t cname;
399 dns_rdata_t rdata = DNS_RDATA_INIT;
400 unsigned int i = msg->counts[DNS_SECTION_ANSWER];
402 while (i-- > 0) {
403 rdataset = NULL;
404 result = dns_message_findname(msg, DNS_SECTION_ANSWER, qname,
405 dns_rdatatype_cname, 0, NULL,
406 &rdataset);
407 if (result != ISC_R_SUCCESS)
408 return;
409 result = dns_rdataset_first(rdataset);
410 check_result(result, "dns_rdataset_first");
411 dns_rdata_reset(&rdata);
412 dns_rdataset_current(rdataset, &rdata);
413 result = dns_rdata_tostruct(&rdata, &cname, NULL);
414 check_result(result, "dns_rdata_tostruct");
415 dns_name_copy(&cname.cname, qname, NULL);
416 dns_rdata_freestruct(&cname);
420 isc_result_t
421 printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) {
422 isc_boolean_t did_flag = ISC_FALSE;
423 dns_rdataset_t *opt, *tsig = NULL;
424 dns_name_t *tsigname;
425 isc_result_t result = ISC_R_SUCCESS;
426 int force_error;
428 UNUSED(headers);
431 * We get called multiple times.
432 * Preserve any existing error status.
434 force_error = (seen_error == 1) ? 1 : 0;
435 seen_error = 1;
436 if (listed_server && !printed_server) {
437 char sockstr[ISC_SOCKADDR_FORMATSIZE];
439 printf("Using domain server:\n");
440 printf("Name: %s\n", query->userarg);
441 isc_sockaddr_format(&query->sockaddr, sockstr,
442 sizeof(sockstr));
443 printf("Address: %s\n", sockstr);
444 printf("Aliases: \n\n");
445 printed_server = ISC_TRUE;
448 if (msg->rcode != 0) {
449 char namestr[DNS_NAME_FORMATSIZE];
450 dns_name_format(query->lookup->name, namestr, sizeof(namestr));
451 printf("Host %s not found: %d(%s)\n",
452 (msg->rcode != dns_rcode_nxdomain) ? namestr :
453 query->lookup->textname, msg->rcode,
454 rcode_totext(msg->rcode));
455 return (ISC_R_SUCCESS);
458 if (default_lookups && query->lookup->rdtype == dns_rdatatype_a) {
459 char namestr[DNS_NAME_FORMATSIZE];
460 dig_lookup_t *lookup;
461 dns_fixedname_t fixed;
462 dns_name_t *name;
464 /* Add AAAA and MX lookups. */
465 dns_fixedname_init(&fixed);
466 name = dns_fixedname_name(&fixed);
467 dns_name_copy(query->lookup->name, name, NULL);
468 chase_cnamechain(msg, name);
469 dns_name_format(name, namestr, sizeof(namestr));
470 lookup = clone_lookup(query->lookup, ISC_FALSE);
471 if (lookup != NULL) {
472 strncpy(lookup->textname, namestr,
473 sizeof(lookup->textname));
474 lookup->textname[sizeof(lookup->textname)-1] = 0;
475 lookup->rdtype = dns_rdatatype_aaaa;
476 lookup->rdtypeset = ISC_TRUE;
477 lookup->origin = NULL;
478 lookup->retries = tries;
479 ISC_LIST_APPEND(lookup_list, lookup, link);
481 lookup = clone_lookup(query->lookup, ISC_FALSE);
482 if (lookup != NULL) {
483 strncpy(lookup->textname, namestr,
484 sizeof(lookup->textname));
485 lookup->textname[sizeof(lookup->textname)-1] = 0;
486 lookup->rdtype = dns_rdatatype_mx;
487 lookup->rdtypeset = ISC_TRUE;
488 lookup->origin = NULL;
489 lookup->retries = tries;
490 ISC_LIST_APPEND(lookup_list, lookup, link);
494 if (!short_form) {
495 printf(";; ->>HEADER<<- opcode: %s, status: %s, id: %u\n",
496 opcodetext[msg->opcode], rcode_totext(msg->rcode),
497 msg->id);
498 printf(";; flags: ");
499 if ((msg->flags & DNS_MESSAGEFLAG_QR) != 0) {
500 printf("qr");
501 did_flag = ISC_TRUE;
503 if ((msg->flags & DNS_MESSAGEFLAG_AA) != 0) {
504 printf("%saa", did_flag ? " " : "");
505 did_flag = ISC_TRUE;
507 if ((msg->flags & DNS_MESSAGEFLAG_TC) != 0) {
508 printf("%stc", did_flag ? " " : "");
509 did_flag = ISC_TRUE;
511 if ((msg->flags & DNS_MESSAGEFLAG_RD) != 0) {
512 printf("%srd", did_flag ? " " : "");
513 did_flag = ISC_TRUE;
515 if ((msg->flags & DNS_MESSAGEFLAG_RA) != 0) {
516 printf("%sra", did_flag ? " " : "");
517 did_flag = ISC_TRUE;
519 if ((msg->flags & DNS_MESSAGEFLAG_AD) != 0) {
520 printf("%sad", did_flag ? " " : "");
521 did_flag = ISC_TRUE;
523 if ((msg->flags & DNS_MESSAGEFLAG_CD) != 0) {
524 printf("%scd", did_flag ? " " : "");
525 did_flag = ISC_TRUE;
527 printf("; QUERY: %u, ANSWER: %u, "
528 "AUTHORITY: %u, ADDITIONAL: %u\n",
529 msg->counts[DNS_SECTION_QUESTION],
530 msg->counts[DNS_SECTION_ANSWER],
531 msg->counts[DNS_SECTION_AUTHORITY],
532 msg->counts[DNS_SECTION_ADDITIONAL]);
533 opt = dns_message_getopt(msg);
534 if (opt != NULL)
535 printf(";; EDNS: version: %u, udp=%u\n",
536 (unsigned int)((opt->ttl & 0x00ff0000) >> 16),
537 (unsigned int)opt->rdclass);
538 tsigname = NULL;
539 tsig = dns_message_gettsig(msg, &tsigname);
540 if (tsig != NULL)
541 printf(";; PSEUDOSECTIONS: TSIG\n");
543 if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_QUESTION]) &&
544 !short_form) {
545 printf("\n");
546 result = printsection(msg, DNS_SECTION_QUESTION, "QUESTION",
547 ISC_TRUE, query);
548 if (result != ISC_R_SUCCESS)
549 return (result);
551 if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_ANSWER])) {
552 if (!short_form)
553 printf("\n");
554 result = printsection(msg, DNS_SECTION_ANSWER, "ANSWER",
555 ISC_TF(!short_form), query);
556 if (result != ISC_R_SUCCESS)
557 return (result);
560 if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_AUTHORITY]) &&
561 !short_form) {
562 printf("\n");
563 result = printsection(msg, DNS_SECTION_AUTHORITY, "AUTHORITY",
564 ISC_TRUE, query);
565 if (result != ISC_R_SUCCESS)
566 return (result);
568 if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_ADDITIONAL]) &&
569 !short_form) {
570 printf("\n");
571 result = printsection(msg, DNS_SECTION_ADDITIONAL,
572 "ADDITIONAL", ISC_TRUE, query);
573 if (result != ISC_R_SUCCESS)
574 return (result);
576 if ((tsig != NULL) && !short_form) {
577 printf("\n");
578 result = printrdata(msg, tsig, tsigname,
579 "PSEUDOSECTION TSIG", ISC_TRUE);
580 if (result != ISC_R_SUCCESS)
581 return (result);
583 if (!short_form)
584 printf("\n");
586 if (short_form && !default_lookups &&
587 ISC_LIST_EMPTY(msg->sections[DNS_SECTION_ANSWER])) {
588 char namestr[DNS_NAME_FORMATSIZE];
589 char typestr[DNS_RDATATYPE_FORMATSIZE];
590 dns_name_format(query->lookup->name, namestr, sizeof(namestr));
591 dns_rdatatype_format(query->lookup->rdtype, typestr,
592 sizeof(typestr));
593 printf("%s has no %s record\n", namestr, typestr);
595 seen_error = force_error;
596 return (result);
599 static const char * optstring = "46ac:dilnm:rst:vwCDN:R:TW:";
601 static void
602 pre_parse_args(int argc, char **argv) {
603 int c;
605 while ((c = isc_commandline_parse(argc, argv, optstring)) != -1) {
606 switch (c) {
607 case 'm':
608 memdebugging = ISC_TRUE;
609 if (strcasecmp("trace", isc_commandline_argument) == 0)
610 isc_mem_debugging |= ISC_MEM_DEBUGTRACE;
611 else if (!strcasecmp("record",
612 isc_commandline_argument) == 0)
613 isc_mem_debugging |= ISC_MEM_DEBUGRECORD;
614 else if (strcasecmp("usage",
615 isc_commandline_argument) == 0)
616 isc_mem_debugging |= ISC_MEM_DEBUGUSAGE;
617 break;
619 case '4': break;
620 case '6': break;
621 case 'a': break;
622 case 'c': break;
623 case 'd': break;
624 case 'i': break;
625 case 'l': break;
626 case 'n': break;
627 case 'r': break;
628 case 's': break;
629 case 't': break;
630 case 'v': break;
631 case 'w': break;
632 case 'C': break;
633 case 'D': break;
634 case 'N': break;
635 case 'R': break;
636 case 'T': break;
637 case 'W': break;
638 default:
639 show_usage();
642 isc_commandline_reset = ISC_TRUE;
643 isc_commandline_index = 1;
646 static void
647 parse_args(isc_boolean_t is_batchfile, int argc, char **argv) {
648 char hostname[MXNAME];
649 dig_lookup_t *lookup;
650 int c;
651 char store[MXNAME];
652 isc_textregion_t tr;
653 isc_result_t result = ISC_R_SUCCESS;
654 dns_rdatatype_t rdtype;
655 dns_rdataclass_t rdclass;
656 isc_uint32_t serial = 0;
658 UNUSED(is_batchfile);
660 lookup = make_empty_lookup();
662 lookup->servfail_stops = ISC_FALSE;
663 lookup->comments = ISC_FALSE;
665 while ((c = isc_commandline_parse(argc, argv, optstring)) != -1) {
666 switch (c) {
667 case 'l':
668 lookup->tcp_mode = ISC_TRUE;
669 lookup->rdtype = dns_rdatatype_axfr;
670 lookup->rdtypeset = ISC_TRUE;
671 fatalexit = 3;
672 break;
673 case 'v':
674 case 'd':
675 short_form = ISC_FALSE;
676 break;
677 case 'r':
678 lookup->recurse = ISC_FALSE;
679 break;
680 case 't':
681 if (strncasecmp(isc_commandline_argument,
682 "ixfr=", 5) == 0) {
683 rdtype = dns_rdatatype_ixfr;
684 /* XXXMPA add error checking */
685 serial = strtoul(isc_commandline_argument + 5,
686 NULL, 10);
687 result = ISC_R_SUCCESS;
688 } else {
689 tr.base = isc_commandline_argument;
690 tr.length = strlen(isc_commandline_argument);
691 result = dns_rdatatype_fromtext(&rdtype,
692 (isc_textregion_t *)&tr);
695 if (result != ISC_R_SUCCESS) {
696 fatalexit = 2;
697 fatal("invalid type: %s\n",
698 isc_commandline_argument);
700 if (!lookup->rdtypeset ||
701 lookup->rdtype != dns_rdatatype_axfr)
702 lookup->rdtype = rdtype;
703 lookup->rdtypeset = ISC_TRUE;
704 #ifdef WITH_IDN
705 idnoptions = 0;
706 #endif
707 if (rdtype == dns_rdatatype_axfr) {
708 /* -l -t any -v */
709 list_type = dns_rdatatype_any;
710 short_form = ISC_FALSE;
711 lookup->tcp_mode = ISC_TRUE;
712 } else if (rdtype == dns_rdatatype_ixfr) {
713 lookup->ixfr_serial = serial;
714 lookup->tcp_mode = ISC_TRUE;
715 list_type = rdtype;
716 #ifdef WITH_IDN
717 } else if (rdtype == dns_rdatatype_a ||
718 rdtype == dns_rdatatype_aaaa ||
719 rdtype == dns_rdatatype_mx) {
720 idnoptions = IDN_ASCCHECK;
721 list_type = rdtype;
722 #endif
723 } else
724 list_type = rdtype;
725 list_addresses = ISC_FALSE;
726 default_lookups = ISC_FALSE;
727 break;
728 case 'c':
729 tr.base = isc_commandline_argument;
730 tr.length = strlen(isc_commandline_argument);
731 result = dns_rdataclass_fromtext(&rdclass,
732 (isc_textregion_t *)&tr);
734 if (result != ISC_R_SUCCESS) {
735 fatalexit = 2;
736 fatal("invalid class: %s\n",
737 isc_commandline_argument);
738 } else {
739 lookup->rdclass = rdclass;
740 lookup->rdclassset = ISC_TRUE;
742 default_lookups = ISC_FALSE;
743 break;
744 case 'a':
745 if (!lookup->rdtypeset ||
746 lookup->rdtype != dns_rdatatype_axfr)
747 lookup->rdtype = dns_rdatatype_any;
748 list_type = dns_rdatatype_any;
749 list_addresses = ISC_FALSE;
750 lookup->rdtypeset = ISC_TRUE;
751 short_form = ISC_FALSE;
752 default_lookups = ISC_FALSE;
753 break;
754 case 'i':
755 lookup->ip6_int = ISC_TRUE;
756 break;
757 case 'n':
758 /* deprecated */
759 break;
760 case 'm':
761 /* Handled by pre_parse_args(). */
762 break;
763 case 'w':
765 * The timer routines are coded such that
766 * timeout==MAXINT doesn't enable the timer
768 timeout = INT_MAX;
769 break;
770 case 'W':
771 timeout = atoi(isc_commandline_argument);
772 if (timeout < 1)
773 timeout = 1;
774 break;
775 case 'R':
776 tries = atoi(isc_commandline_argument) + 1;
777 if (tries < 2)
778 tries = 2;
779 break;
780 case 'T':
781 lookup->tcp_mode = ISC_TRUE;
782 break;
783 case 'C':
784 debug("showing all SOAs");
785 lookup->rdtype = dns_rdatatype_ns;
786 lookup->rdtypeset = ISC_TRUE;
787 lookup->rdclass = dns_rdataclass_in;
788 lookup->rdclassset = ISC_TRUE;
789 lookup->ns_search_only = ISC_TRUE;
790 lookup->trace_root = ISC_TRUE;
791 lookup->identify_previous_line = ISC_TRUE;
792 default_lookups = ISC_FALSE;
793 break;
794 case 'N':
795 debug("setting NDOTS to %s",
796 isc_commandline_argument);
797 ndots = atoi(isc_commandline_argument);
798 break;
799 case 'D':
800 debugging = ISC_TRUE;
801 break;
802 case '4':
803 if (have_ipv4) {
804 isc_net_disableipv6();
805 have_ipv6 = ISC_FALSE;
806 } else
807 fatal("can't find IPv4 networking");
808 break;
809 case '6':
810 if (have_ipv6) {
811 isc_net_disableipv4();
812 have_ipv4 = ISC_FALSE;
813 } else
814 fatal("can't find IPv6 networking");
815 break;
816 case 's':
817 lookup->servfail_stops = ISC_TRUE;
818 break;
822 lookup->retries = tries;
824 if (isc_commandline_index >= argc)
825 show_usage();
827 strncpy(hostname, argv[isc_commandline_index], sizeof(hostname));
828 hostname[sizeof(hostname)-1]=0;
829 if (argc > isc_commandline_index + 1) {
830 set_nameserver(argv[isc_commandline_index+1]);
831 debug("server is %s", argv[isc_commandline_index+1]);
832 listed_server = ISC_TRUE;
833 } else
834 check_ra = ISC_TRUE;
836 lookup->pending = ISC_FALSE;
837 if (get_reverse(store, sizeof(store), hostname,
838 lookup->ip6_int, ISC_TRUE) == ISC_R_SUCCESS) {
839 strncpy(lookup->textname, store, sizeof(lookup->textname));
840 lookup->textname[sizeof(lookup->textname)-1] = 0;
841 lookup->rdtype = dns_rdatatype_ptr;
842 lookup->rdtypeset = ISC_TRUE;
843 default_lookups = ISC_FALSE;
844 } else {
845 strncpy(lookup->textname, hostname, sizeof(lookup->textname));
846 lookup->textname[sizeof(lookup->textname)-1]=0;
847 usesearch = ISC_TRUE;
849 lookup->new_search = ISC_TRUE;
850 ISC_LIST_APPEND(lookup_list, lookup, link);
854 main(int argc, char **argv) {
855 isc_result_t result;
857 tries = 2;
859 ISC_LIST_INIT(lookup_list);
860 ISC_LIST_INIT(server_list);
861 ISC_LIST_INIT(search_list);
863 fatalexit = 1;
864 #ifdef WITH_IDN
865 idnoptions = IDN_ASCCHECK;
866 #endif
868 debug("main()");
869 progname = argv[0];
870 pre_parse_args(argc, argv);
871 result = isc_app_start();
872 check_result(result, "isc_app_start");
873 setup_libs();
874 parse_args(ISC_FALSE, argc, argv);
875 setup_system();
876 result = isc_app_onrun(mctx, global_task, onrun_callback, NULL);
877 check_result(result, "isc_app_onrun");
878 isc_app_run();
879 cancel_all();
880 destroy_libs();
881 isc_app_finish();
882 return ((seen_error == 0) ? 0 : 1);