2 * Copyright (C) 2004-2007, 2009 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: host.c,v 1.116.216.3 2009/09/08 23:28:20 marka Exp $ */
31 #include <idn/result.h>
33 #include <idn/resconf.h>
38 #include <isc/commandline.h>
39 #include <isc/netaddr.h>
40 #include <isc/print.h>
41 #include <isc/string.h>
44 #include <isc/stdlib.h>
46 #include <dns/byaddr.h>
47 #include <dns/fixedname.h>
48 #include <dns/message.h>
50 #include <dns/rdata.h>
51 #include <dns/rdataclass.h>
52 #include <dns/rdataset.h>
53 #include <dns/rdatatype.h>
54 #include <dns/rdatastruct.h>
58 static isc_boolean_t short_form = ISC_TRUE, listed_server = ISC_FALSE;
59 static isc_boolean_t default_lookups = ISC_TRUE;
60 static int seen_error = -1;
61 static isc_boolean_t list_addresses = ISC_TRUE;
62 static dns_rdatatype_t list_type = dns_rdatatype_a;
63 static isc_boolean_t printed_server = ISC_FALSE;
65 static const char *opcodetext[] = {
84 static const char *rcodetext[] = {
109 struct rtype rtypes[] = {
110 { 1, "has address" },
111 { 2, "name server" },
112 { 5, "is an alias for" },
113 { 11, "has well known services" },
114 { 12, "domain name pointer" },
115 { 13, "host information" },
116 { 15, "mail is handled by" },
117 { 16, "descriptive text" },
118 { 19, "x25 address" },
119 { 20, "ISDN address" },
120 { 24, "has signature" },
122 { 28, "has IPv6 address" },
128 rcode_totext(dns_rcode_t rcode)
130 static char buf[sizeof("?65535")];
132 const char *consttext;
136 if (rcode >= (sizeof(rcodetext)/sizeof(rcodetext[0]))) {
137 snprintf(buf, sizeof(buf), "?%u", rcode);
138 totext.deconsttext = buf;
140 totext.consttext = rcodetext[rcode];
141 return totext.deconsttext;
147 "Usage: host [-aCdlriTwv] [-c class] [-N ndots] [-t type] [-W time]\n"
148 " [-R number] [-m flag] hostname [server]\n"
149 " -a is equivalent to -v -t ANY\n"
150 " -c specifies query class for non-IN data\n"
151 " -C compares SOA records on authoritative nameservers\n"
152 " -d is equivalent to -v\n"
153 " -l lists all hosts in a domain, using AXFR\n"
154 " -i IP6.INT reverse lookups\n"
155 " -N changes the number of dots allowed before root lookup is done\n"
156 " -r disables recursive processing\n"
157 " -R specifies number of retries for UDP packets\n"
158 " -s a SERVFAIL response should stop query\n"
159 " -t specifies the query type\n"
160 " -T enables TCP/IP mode\n"
161 " -v enables verbose output\n"
162 " -w specifies to wait forever for a reply\n"
163 " -W specifies how long to wait for a reply\n"
164 " -4 use IPv4 query transport only\n"
165 " -6 use IPv6 query transport only\n"
166 " -m set memory debugging flag (trace|record|usage)\n", stderr);
171 dighost_shutdown(void) {
176 received(int bytes, isc_sockaddr_t *from, dig_query_t *query) {
181 char fromtext[ISC_SOCKADDR_FORMATSIZE];
182 isc_sockaddr_format(from, fromtext, sizeof(fromtext));
184 diff = (int) isc_time_microdiff(&now, &query->time_sent);
185 printf("Received %u bytes from %s in %d ms\n",
186 bytes, fromtext, diff/1000);
191 trying(char *frm, dig_lookup_t *lookup) {
195 printf("Trying \"%s\"\n", frm);
199 say_message(dns_name_t *name, const char *msg, dns_rdata_t *rdata,
202 isc_buffer_t *b = NULL;
203 char namestr[DNS_NAME_FORMATSIZE];
206 unsigned int bufsize = BUFSIZ;
208 dns_name_format(name, namestr, sizeof(namestr));
210 result = isc_buffer_allocate(mctx, &b, bufsize);
211 check_result(result, "isc_buffer_allocate");
212 result = dns_rdata_totext(rdata, NULL, b);
213 if (result == ISC_R_NOSPACE) {
218 check_result(result, "dns_rdata_totext");
219 isc_buffer_usedregion(b, &r);
220 if (query->lookup->identify_previous_line) {
221 printf("Nameserver %s:\n\t",
224 printf("%s %s %.*s", namestr,
225 msg, (int)r.length, (char *)r.base);
226 if (query->lookup->identify) {
227 printf(" on server %s", query->servname);
233 /* Just for compatibility : not use in host program */
235 printrdataset(dns_name_t *owner_name, dns_rdataset_t *rdataset,
236 isc_buffer_t *target)
245 printsection(dns_message_t *msg, dns_section_t sectionid,
246 const char *section_name, isc_boolean_t headers,
249 dns_name_t *name, *print_name;
250 dns_rdataset_t *rdataset;
251 dns_rdata_t rdata = DNS_RDATA_INIT;
253 isc_result_t result, loopresult;
255 dns_name_t empty_name;
258 isc_boolean_t no_rdata;
260 if (sectionid == DNS_SECTION_QUESTION)
263 no_rdata = ISC_FALSE;
266 printf(";; %s SECTION:\n", section_name);
268 dns_name_init(&empty_name, NULL);
270 result = dns_message_firstname(msg, sectionid);
271 if (result == ISC_R_NOMORE)
272 return (ISC_R_SUCCESS);
273 else if (result != ISC_R_SUCCESS)
278 dns_message_currentname(msg, sectionid, &name);
280 isc_buffer_init(&target, t, sizeof(t));
284 for (rdataset = ISC_LIST_HEAD(name->list);
286 rdataset = ISC_LIST_NEXT(rdataset, link)) {
287 if (query->lookup->rdtype == dns_rdatatype_axfr &&
288 !((!list_addresses &&
289 (list_type == dns_rdatatype_any ||
290 rdataset->type == list_type)) ||
292 (rdataset->type == dns_rdatatype_a ||
293 rdataset->type == dns_rdatatype_aaaa ||
294 rdataset->type == dns_rdatatype_ns ||
295 rdataset->type == dns_rdatatype_ptr))))
298 result = dns_rdataset_totext(rdataset,
303 if (result != ISC_R_SUCCESS)
307 print_name = &empty_name;
311 UNUSED(first); /* Shut up compiler. */
314 loopresult = dns_rdataset_first(rdataset);
315 while (loopresult == ISC_R_SUCCESS) {
318 char typebuf[DNS_RDATATYPE_FORMATSIZE];
319 char typebuf2[DNS_RDATATYPE_FORMATSIZE
321 dns_rdataset_current(rdataset, &rdata);
323 for (t = rtypes; t->text != NULL; t++) {
324 if (t->type == rdata.type) {
330 dns_rdatatype_format(rdata.type,
333 snprintf(typebuf2, sizeof(typebuf2),
334 "has %s record", typebuf);
337 say_message(print_name, rtt,
339 dns_rdata_reset(&rdata);
341 dns_rdataset_next(rdataset);
346 isc_buffer_usedregion(&target, &r);
348 printf(";%.*s", (int)r.length,
351 printf("%.*s", (int)r.length, (char *)r.base);
354 result = dns_message_nextname(msg, sectionid);
355 if (result == ISC_R_NOMORE)
357 else if (result != ISC_R_SUCCESS)
361 return (ISC_R_SUCCESS);
365 printrdata(dns_message_t *msg, dns_rdataset_t *rdataset, dns_name_t *owner,
366 const char *set_name, isc_boolean_t headers)
375 printf(";; %s SECTION:\n", set_name);
377 isc_buffer_init(&target, t, sizeof(t));
379 result = dns_rdataset_totext(rdataset, owner, ISC_FALSE, ISC_FALSE,
381 if (result != ISC_R_SUCCESS)
383 isc_buffer_usedregion(&target, &r);
384 printf("%.*s", (int)r.length, (char *)r.base);
386 return (ISC_R_SUCCESS);
390 chase_cnamechain(dns_message_t *msg, dns_name_t *qname) {
392 dns_rdataset_t *rdataset;
393 dns_rdata_cname_t cname;
394 dns_rdata_t rdata = DNS_RDATA_INIT;
395 unsigned int i = msg->counts[DNS_SECTION_ANSWER];
399 result = dns_message_findname(msg, DNS_SECTION_ANSWER, qname,
400 dns_rdatatype_cname, 0, NULL,
402 if (result != ISC_R_SUCCESS)
404 result = dns_rdataset_first(rdataset);
405 check_result(result, "dns_rdataset_first");
406 dns_rdata_reset(&rdata);
407 dns_rdataset_current(rdataset, &rdata);
408 result = dns_rdata_tostruct(&rdata, &cname, NULL);
409 check_result(result, "dns_rdata_tostruct");
410 dns_name_copy(&cname.cname, qname, NULL);
411 dns_rdata_freestruct(&cname);
416 printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) {
417 isc_boolean_t did_flag = ISC_FALSE;
418 dns_rdataset_t *opt, *tsig = NULL;
419 dns_name_t *tsigname;
420 isc_result_t result = ISC_R_SUCCESS;
426 * We get called multiple times.
427 * Preserve any existing error status.
429 force_error = (seen_error == 1) ? 1 : 0;
431 if (listed_server && !printed_server) {
432 char sockstr[ISC_SOCKADDR_FORMATSIZE];
434 printf("Using domain server:\n");
435 printf("Name: %s\n", query->userarg);
436 isc_sockaddr_format(&query->sockaddr, sockstr,
438 printf("Address: %s\n", sockstr);
439 printf("Aliases: \n\n");
440 printed_server = ISC_TRUE;
443 if (msg->rcode != 0) {
444 char namestr[DNS_NAME_FORMATSIZE];
445 dns_name_format(query->lookup->name, namestr, sizeof(namestr));
446 printf("Host %s not found: %d(%s)\n",
447 (msg->rcode != dns_rcode_nxdomain) ? namestr :
448 query->lookup->textname, msg->rcode,
449 rcode_totext(msg->rcode));
450 return (ISC_R_SUCCESS);
453 if (default_lookups && query->lookup->rdtype == dns_rdatatype_a) {
454 char namestr[DNS_NAME_FORMATSIZE];
455 dig_lookup_t *lookup;
456 dns_fixedname_t fixed;
459 /* Add AAAA and MX lookups. */
460 dns_fixedname_init(&fixed);
461 name = dns_fixedname_name(&fixed);
462 dns_name_copy(query->lookup->name, name, NULL);
463 chase_cnamechain(msg, name);
464 dns_name_format(name, namestr, sizeof(namestr));
465 lookup = clone_lookup(query->lookup, ISC_FALSE);
466 if (lookup != NULL) {
467 strncpy(lookup->textname, namestr,
468 sizeof(lookup->textname));
469 lookup->textname[sizeof(lookup->textname)-1] = 0;
470 lookup->rdtype = dns_rdatatype_aaaa;
471 lookup->rdtypeset = ISC_TRUE;
472 lookup->origin = NULL;
473 lookup->retries = tries;
474 ISC_LIST_APPEND(lookup_list, lookup, link);
476 lookup = clone_lookup(query->lookup, ISC_FALSE);
477 if (lookup != NULL) {
478 strncpy(lookup->textname, namestr,
479 sizeof(lookup->textname));
480 lookup->textname[sizeof(lookup->textname)-1] = 0;
481 lookup->rdtype = dns_rdatatype_mx;
482 lookup->rdtypeset = ISC_TRUE;
483 lookup->origin = NULL;
484 lookup->retries = tries;
485 ISC_LIST_APPEND(lookup_list, lookup, link);
490 printf(";; ->>HEADER<<- opcode: %s, status: %s, id: %u\n",
491 opcodetext[msg->opcode], rcode_totext(msg->rcode),
493 printf(";; flags: ");
494 if ((msg->flags & DNS_MESSAGEFLAG_QR) != 0) {
498 if ((msg->flags & DNS_MESSAGEFLAG_AA) != 0) {
499 printf("%saa", did_flag ? " " : "");
502 if ((msg->flags & DNS_MESSAGEFLAG_TC) != 0) {
503 printf("%stc", did_flag ? " " : "");
506 if ((msg->flags & DNS_MESSAGEFLAG_RD) != 0) {
507 printf("%srd", did_flag ? " " : "");
510 if ((msg->flags & DNS_MESSAGEFLAG_RA) != 0) {
511 printf("%sra", did_flag ? " " : "");
514 if ((msg->flags & DNS_MESSAGEFLAG_AD) != 0) {
515 printf("%sad", did_flag ? " " : "");
518 if ((msg->flags & DNS_MESSAGEFLAG_CD) != 0) {
519 printf("%scd", did_flag ? " " : "");
522 printf("; QUERY: %u, ANSWER: %u, "
523 "AUTHORITY: %u, ADDITIONAL: %u\n",
524 msg->counts[DNS_SECTION_QUESTION],
525 msg->counts[DNS_SECTION_ANSWER],
526 msg->counts[DNS_SECTION_AUTHORITY],
527 msg->counts[DNS_SECTION_ADDITIONAL]);
528 opt = dns_message_getopt(msg);
530 printf(";; EDNS: version: %u, udp=%u\n",
531 (unsigned int)((opt->ttl & 0x00ff0000) >> 16),
532 (unsigned int)opt->rdclass);
534 tsig = dns_message_gettsig(msg, &tsigname);
536 printf(";; PSEUDOSECTIONS: TSIG\n");
538 if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_QUESTION]) &&
541 result = printsection(msg, DNS_SECTION_QUESTION, "QUESTION",
543 if (result != ISC_R_SUCCESS)
546 if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_ANSWER])) {
549 result = printsection(msg, DNS_SECTION_ANSWER, "ANSWER",
550 ISC_TF(!short_form), query);
551 if (result != ISC_R_SUCCESS)
555 if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_AUTHORITY]) &&
558 result = printsection(msg, DNS_SECTION_AUTHORITY, "AUTHORITY",
560 if (result != ISC_R_SUCCESS)
563 if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_ADDITIONAL]) &&
566 result = printsection(msg, DNS_SECTION_ADDITIONAL,
567 "ADDITIONAL", ISC_TRUE, query);
568 if (result != ISC_R_SUCCESS)
571 if ((tsig != NULL) && !short_form) {
573 result = printrdata(msg, tsig, tsigname,
574 "PSEUDOSECTION TSIG", ISC_TRUE);
575 if (result != ISC_R_SUCCESS)
581 if (short_form && !default_lookups &&
582 ISC_LIST_EMPTY(msg->sections[DNS_SECTION_ANSWER])) {
583 char namestr[DNS_NAME_FORMATSIZE];
584 char typestr[DNS_RDATATYPE_FORMATSIZE];
585 dns_name_format(query->lookup->name, namestr, sizeof(namestr));
586 dns_rdatatype_format(query->lookup->rdtype, typestr,
588 printf("%s has no %s record\n", namestr, typestr);
590 seen_error = force_error;
594 static const char * optstring = "46ac:dilnm:rst:vwCDN:R:TW:";
597 pre_parse_args(int argc, char **argv) {
600 while ((c = isc_commandline_parse(argc, argv, optstring)) != -1) {
603 memdebugging = ISC_TRUE;
604 if (strcasecmp("trace", isc_commandline_argument) == 0)
605 isc_mem_debugging |= ISC_MEM_DEBUGTRACE;
606 else if (!strcasecmp("record",
607 isc_commandline_argument) == 0)
608 isc_mem_debugging |= ISC_MEM_DEBUGRECORD;
609 else if (strcasecmp("usage",
610 isc_commandline_argument) == 0)
611 isc_mem_debugging |= ISC_MEM_DEBUGUSAGE;
637 isc_commandline_reset = ISC_TRUE;
638 isc_commandline_index = 1;
642 parse_args(isc_boolean_t is_batchfile, int argc, char **argv) {
643 char hostname[MXNAME];
644 dig_lookup_t *lookup;
648 isc_result_t result = ISC_R_SUCCESS;
649 dns_rdatatype_t rdtype;
650 dns_rdataclass_t rdclass;
651 isc_uint32_t serial = 0;
653 UNUSED(is_batchfile);
655 lookup = make_empty_lookup();
657 lookup->servfail_stops = ISC_FALSE;
658 lookup->comments = ISC_FALSE;
660 while ((c = isc_commandline_parse(argc, argv, optstring)) != -1) {
663 lookup->tcp_mode = ISC_TRUE;
664 lookup->rdtype = dns_rdatatype_axfr;
665 lookup->rdtypeset = ISC_TRUE;
670 short_form = ISC_FALSE;
673 lookup->recurse = ISC_FALSE;
676 if (strncasecmp(isc_commandline_argument,
678 rdtype = dns_rdatatype_ixfr;
679 /* XXXMPA add error checking */
680 serial = strtoul(isc_commandline_argument + 5,
682 result = ISC_R_SUCCESS;
684 tr.base = isc_commandline_argument;
685 tr.length = strlen(isc_commandline_argument);
686 result = dns_rdatatype_fromtext(&rdtype,
687 (isc_textregion_t *)&tr);
690 if (result != ISC_R_SUCCESS) {
692 fatal("invalid type: %s\n",
693 isc_commandline_argument);
695 if (!lookup->rdtypeset ||
696 lookup->rdtype != dns_rdatatype_axfr)
697 lookup->rdtype = rdtype;
698 lookup->rdtypeset = ISC_TRUE;
702 if (rdtype == dns_rdatatype_axfr) {
704 list_type = dns_rdatatype_any;
705 short_form = ISC_FALSE;
706 lookup->tcp_mode = ISC_TRUE;
707 } else if (rdtype == dns_rdatatype_ixfr) {
708 lookup->ixfr_serial = serial;
709 lookup->tcp_mode = ISC_TRUE;
712 } else if (rdtype == dns_rdatatype_a ||
713 rdtype == dns_rdatatype_aaaa ||
714 rdtype == dns_rdatatype_mx) {
715 idnoptions = IDN_ASCCHECK;
720 list_addresses = ISC_FALSE;
721 default_lookups = ISC_FALSE;
724 tr.base = isc_commandline_argument;
725 tr.length = strlen(isc_commandline_argument);
726 result = dns_rdataclass_fromtext(&rdclass,
727 (isc_textregion_t *)&tr);
729 if (result != ISC_R_SUCCESS) {
731 fatal("invalid class: %s\n",
732 isc_commandline_argument);
734 lookup->rdclass = rdclass;
735 lookup->rdclassset = ISC_TRUE;
737 default_lookups = ISC_FALSE;
740 if (!lookup->rdtypeset ||
741 lookup->rdtype != dns_rdatatype_axfr)
742 lookup->rdtype = dns_rdatatype_any;
743 list_type = dns_rdatatype_any;
744 list_addresses = ISC_FALSE;
745 lookup->rdtypeset = ISC_TRUE;
746 short_form = ISC_FALSE;
747 default_lookups = ISC_FALSE;
750 lookup->ip6_int = ISC_TRUE;
756 /* Handled by pre_parse_args(). */
760 * The timer routines are coded such that
761 * timeout==MAXINT doesn't enable the timer
766 timeout = atoi(isc_commandline_argument);
771 tries = atoi(isc_commandline_argument) + 1;
776 lookup->tcp_mode = ISC_TRUE;
779 debug("showing all SOAs");
780 lookup->rdtype = dns_rdatatype_ns;
781 lookup->rdtypeset = ISC_TRUE;
782 lookup->rdclass = dns_rdataclass_in;
783 lookup->rdclassset = ISC_TRUE;
784 lookup->ns_search_only = ISC_TRUE;
785 lookup->trace_root = ISC_TRUE;
786 lookup->identify_previous_line = ISC_TRUE;
787 default_lookups = ISC_FALSE;
790 debug("setting NDOTS to %s",
791 isc_commandline_argument);
792 ndots = atoi(isc_commandline_argument);
795 debugging = ISC_TRUE;
799 isc_net_disableipv6();
800 have_ipv6 = ISC_FALSE;
802 fatal("can't find IPv4 networking");
806 isc_net_disableipv4();
807 have_ipv4 = ISC_FALSE;
809 fatal("can't find IPv6 networking");
812 lookup->servfail_stops = ISC_TRUE;
817 lookup->retries = tries;
819 if (isc_commandline_index >= argc)
822 strncpy(hostname, argv[isc_commandline_index], sizeof(hostname));
823 hostname[sizeof(hostname)-1]=0;
824 if (argc > isc_commandline_index + 1) {
825 set_nameserver(argv[isc_commandline_index+1]);
826 debug("server is %s", argv[isc_commandline_index+1]);
827 listed_server = ISC_TRUE;
831 lookup->pending = ISC_FALSE;
832 if (get_reverse(store, sizeof(store), hostname,
833 lookup->ip6_int, ISC_TRUE) == ISC_R_SUCCESS) {
834 strncpy(lookup->textname, store, sizeof(lookup->textname));
835 lookup->textname[sizeof(lookup->textname)-1] = 0;
836 lookup->rdtype = dns_rdatatype_ptr;
837 lookup->rdtypeset = ISC_TRUE;
838 default_lookups = ISC_FALSE;
840 strncpy(lookup->textname, hostname, sizeof(lookup->textname));
841 lookup->textname[sizeof(lookup->textname)-1]=0;
842 usesearch = ISC_TRUE;
844 lookup->new_search = ISC_TRUE;
845 ISC_LIST_APPEND(lookup_list, lookup, link);
849 main(int argc, char **argv) {
854 ISC_LIST_INIT(lookup_list);
855 ISC_LIST_INIT(server_list);
856 ISC_LIST_INIT(search_list);
860 idnoptions = IDN_ASCCHECK;
865 pre_parse_args(argc, argv);
866 result = isc_app_start();
867 check_result(result, "isc_app_start");
869 parse_args(ISC_FALSE, argc, argv);
871 result = isc_app_onrun(mctx, global_task, onrun_callback, NULL);
872 check_result(result, "isc_app_onrun");
877 return ((seen_error == 0) ? 0 : 1);