2 * Copyright (C) 2004-2007, 2009-2011 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.124.40.3 2011/03/11 06:46:59 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;
144 ISC_PLATFORM_NORETURN_PRE static void
145 show_usage(void) ISC_PLATFORM_NORETURN_POST;
150 "Usage: host [-aCdlriTwv] [-c class] [-N ndots] [-t type] [-W time]\n"
151 " [-R number] [-m flag] hostname [server]\n"
152 " -a is equivalent to -v -t ANY\n"
153 " -c specifies query class for non-IN data\n"
154 " -C compares SOA records on authoritative nameservers\n"
155 " -d is equivalent to -v\n"
156 " -l lists all hosts in a domain, using AXFR\n"
157 " -i IP6.INT reverse lookups\n"
158 " -N changes the number of dots allowed before root lookup is done\n"
159 " -r disables recursive processing\n"
160 " -R specifies number of retries for UDP packets\n"
161 " -s a SERVFAIL response should stop query\n"
162 " -t specifies the query type\n"
163 " -T enables TCP/IP mode\n"
164 " -v enables verbose output\n"
165 " -w specifies to wait forever for a reply\n"
166 " -W specifies how long to wait for a reply\n"
167 " -4 use IPv4 query transport only\n"
168 " -6 use IPv6 query transport only\n"
169 " -m set memory debugging flag (trace|record|usage)\n", stderr);
174 dighost_shutdown(void) {
179 received(int bytes, isc_sockaddr_t *from, dig_query_t *query) {
184 char fromtext[ISC_SOCKADDR_FORMATSIZE];
185 isc_sockaddr_format(from, fromtext, sizeof(fromtext));
187 diff = (int) isc_time_microdiff(&now, &query->time_sent);
188 printf("Received %u bytes from %s in %d ms\n",
189 bytes, fromtext, diff/1000);
194 trying(char *frm, dig_lookup_t *lookup) {
198 printf("Trying \"%s\"\n", frm);
202 say_message(dns_name_t *name, const char *msg, dns_rdata_t *rdata,
205 isc_buffer_t *b = NULL;
206 char namestr[DNS_NAME_FORMATSIZE];
209 unsigned int bufsize = BUFSIZ;
211 dns_name_format(name, namestr, sizeof(namestr));
213 result = isc_buffer_allocate(mctx, &b, bufsize);
214 check_result(result, "isc_buffer_allocate");
215 result = dns_rdata_totext(rdata, NULL, b);
216 if (result == ISC_R_NOSPACE) {
221 check_result(result, "dns_rdata_totext");
222 isc_buffer_usedregion(b, &r);
223 if (query->lookup->identify_previous_line) {
224 printf("Nameserver %s:\n\t",
227 printf("%s %s %.*s", namestr,
228 msg, (int)r.length, (char *)r.base);
229 if (query->lookup->identify) {
230 printf(" on server %s", query->servname);
236 /* Just for compatibility : not use in host program */
238 printrdataset(dns_name_t *owner_name, dns_rdataset_t *rdataset,
239 isc_buffer_t *target)
248 printsection(dns_message_t *msg, dns_section_t sectionid,
249 const char *section_name, isc_boolean_t headers,
252 dns_name_t *name, *print_name;
253 dns_rdataset_t *rdataset;
254 dns_rdata_t rdata = DNS_RDATA_INIT;
256 isc_result_t result, loopresult;
258 dns_name_t empty_name;
261 isc_boolean_t no_rdata;
263 if (sectionid == DNS_SECTION_QUESTION)
266 no_rdata = ISC_FALSE;
269 printf(";; %s SECTION:\n", section_name);
271 dns_name_init(&empty_name, NULL);
273 result = dns_message_firstname(msg, sectionid);
274 if (result == ISC_R_NOMORE)
275 return (ISC_R_SUCCESS);
276 else if (result != ISC_R_SUCCESS)
281 dns_message_currentname(msg, sectionid, &name);
283 isc_buffer_init(&target, t, sizeof(t));
287 for (rdataset = ISC_LIST_HEAD(name->list);
289 rdataset = ISC_LIST_NEXT(rdataset, link)) {
290 if (query->lookup->rdtype == dns_rdatatype_axfr &&
291 !((!list_addresses &&
292 (list_type == dns_rdatatype_any ||
293 rdataset->type == list_type)) ||
295 (rdataset->type == dns_rdatatype_a ||
296 rdataset->type == dns_rdatatype_aaaa ||
297 rdataset->type == dns_rdatatype_ns ||
298 rdataset->type == dns_rdatatype_ptr))))
301 result = dns_rdataset_totext(rdataset,
306 if (result != ISC_R_SUCCESS)
310 print_name = &empty_name;
314 UNUSED(first); /* Shut up compiler. */
317 loopresult = dns_rdataset_first(rdataset);
318 while (loopresult == ISC_R_SUCCESS) {
321 char typebuf[DNS_RDATATYPE_FORMATSIZE];
322 char typebuf2[DNS_RDATATYPE_FORMATSIZE
324 dns_rdataset_current(rdataset, &rdata);
326 for (t = rtypes; t->text != NULL; t++) {
327 if (t->type == rdata.type) {
333 dns_rdatatype_format(rdata.type,
336 snprintf(typebuf2, sizeof(typebuf2),
337 "has %s record", typebuf);
340 say_message(print_name, rtt,
342 dns_rdata_reset(&rdata);
344 dns_rdataset_next(rdataset);
349 isc_buffer_usedregion(&target, &r);
351 printf(";%.*s", (int)r.length,
354 printf("%.*s", (int)r.length, (char *)r.base);
357 result = dns_message_nextname(msg, sectionid);
358 if (result == ISC_R_NOMORE)
360 else if (result != ISC_R_SUCCESS)
364 return (ISC_R_SUCCESS);
368 printrdata(dns_message_t *msg, dns_rdataset_t *rdataset, dns_name_t *owner,
369 const char *set_name, isc_boolean_t headers)
378 printf(";; %s SECTION:\n", set_name);
380 isc_buffer_init(&target, t, sizeof(t));
382 result = dns_rdataset_totext(rdataset, owner, ISC_FALSE, ISC_FALSE,
384 if (result != ISC_R_SUCCESS)
386 isc_buffer_usedregion(&target, &r);
387 printf("%.*s", (int)r.length, (char *)r.base);
389 return (ISC_R_SUCCESS);
393 chase_cnamechain(dns_message_t *msg, dns_name_t *qname) {
395 dns_rdataset_t *rdataset;
396 dns_rdata_cname_t cname;
397 dns_rdata_t rdata = DNS_RDATA_INIT;
398 unsigned int i = msg->counts[DNS_SECTION_ANSWER];
402 result = dns_message_findname(msg, DNS_SECTION_ANSWER, qname,
403 dns_rdatatype_cname, 0, NULL,
405 if (result != ISC_R_SUCCESS)
407 result = dns_rdataset_first(rdataset);
408 check_result(result, "dns_rdataset_first");
409 dns_rdata_reset(&rdata);
410 dns_rdataset_current(rdataset, &rdata);
411 result = dns_rdata_tostruct(&rdata, &cname, NULL);
412 check_result(result, "dns_rdata_tostruct");
413 dns_name_copy(&cname.cname, qname, NULL);
414 dns_rdata_freestruct(&cname);
419 printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) {
420 isc_boolean_t did_flag = ISC_FALSE;
421 dns_rdataset_t *opt, *tsig = NULL;
422 dns_name_t *tsigname;
423 isc_result_t result = ISC_R_SUCCESS;
429 * We get called multiple times.
430 * Preserve any existing error status.
432 force_error = (seen_error == 1) ? 1 : 0;
434 if (listed_server && !printed_server) {
435 char sockstr[ISC_SOCKADDR_FORMATSIZE];
437 printf("Using domain server:\n");
438 printf("Name: %s\n", query->userarg);
439 isc_sockaddr_format(&query->sockaddr, sockstr,
441 printf("Address: %s\n", sockstr);
442 printf("Aliases: \n\n");
443 printed_server = ISC_TRUE;
446 if (msg->rcode != 0) {
447 char namestr[DNS_NAME_FORMATSIZE];
448 dns_name_format(query->lookup->name, namestr, sizeof(namestr));
449 printf("Host %s not found: %d(%s)\n",
450 (msg->rcode != dns_rcode_nxdomain) ? namestr :
451 query->lookup->textname, msg->rcode,
452 rcode_totext(msg->rcode));
453 return (ISC_R_SUCCESS);
456 if (default_lookups && query->lookup->rdtype == dns_rdatatype_a) {
457 char namestr[DNS_NAME_FORMATSIZE];
458 dig_lookup_t *lookup;
459 dns_fixedname_t fixed;
462 /* Add AAAA and MX lookups. */
463 dns_fixedname_init(&fixed);
464 name = dns_fixedname_name(&fixed);
465 dns_name_copy(query->lookup->name, name, NULL);
466 chase_cnamechain(msg, name);
467 dns_name_format(name, namestr, sizeof(namestr));
468 lookup = clone_lookup(query->lookup, ISC_FALSE);
469 if (lookup != NULL) {
470 strncpy(lookup->textname, namestr,
471 sizeof(lookup->textname));
472 lookup->textname[sizeof(lookup->textname)-1] = 0;
473 lookup->rdtype = dns_rdatatype_aaaa;
474 lookup->rdtypeset = ISC_TRUE;
475 lookup->origin = NULL;
476 lookup->retries = tries;
477 ISC_LIST_APPEND(lookup_list, lookup, link);
479 lookup = clone_lookup(query->lookup, ISC_FALSE);
480 if (lookup != NULL) {
481 strncpy(lookup->textname, namestr,
482 sizeof(lookup->textname));
483 lookup->textname[sizeof(lookup->textname)-1] = 0;
484 lookup->rdtype = dns_rdatatype_mx;
485 lookup->rdtypeset = ISC_TRUE;
486 lookup->origin = NULL;
487 lookup->retries = tries;
488 ISC_LIST_APPEND(lookup_list, lookup, link);
493 printf(";; ->>HEADER<<- opcode: %s, status: %s, id: %u\n",
494 opcodetext[msg->opcode], rcode_totext(msg->rcode),
496 printf(";; flags: ");
497 if ((msg->flags & DNS_MESSAGEFLAG_QR) != 0) {
501 if ((msg->flags & DNS_MESSAGEFLAG_AA) != 0) {
502 printf("%saa", did_flag ? " " : "");
505 if ((msg->flags & DNS_MESSAGEFLAG_TC) != 0) {
506 printf("%stc", did_flag ? " " : "");
509 if ((msg->flags & DNS_MESSAGEFLAG_RD) != 0) {
510 printf("%srd", did_flag ? " " : "");
513 if ((msg->flags & DNS_MESSAGEFLAG_RA) != 0) {
514 printf("%sra", did_flag ? " " : "");
517 if ((msg->flags & DNS_MESSAGEFLAG_AD) != 0) {
518 printf("%sad", did_flag ? " " : "");
521 if ((msg->flags & DNS_MESSAGEFLAG_CD) != 0) {
522 printf("%scd", did_flag ? " " : "");
526 printf("; QUERY: %u, ANSWER: %u, "
527 "AUTHORITY: %u, ADDITIONAL: %u\n",
528 msg->counts[DNS_SECTION_QUESTION],
529 msg->counts[DNS_SECTION_ANSWER],
530 msg->counts[DNS_SECTION_AUTHORITY],
531 msg->counts[DNS_SECTION_ADDITIONAL]);
532 opt = dns_message_getopt(msg);
534 printf(";; EDNS: version: %u, udp=%u\n",
535 (unsigned int)((opt->ttl & 0x00ff0000) >> 16),
536 (unsigned int)opt->rdclass);
538 tsig = dns_message_gettsig(msg, &tsigname);
540 printf(";; PSEUDOSECTIONS: TSIG\n");
542 if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_QUESTION]) &&
545 result = printsection(msg, DNS_SECTION_QUESTION, "QUESTION",
547 if (result != ISC_R_SUCCESS)
550 if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_ANSWER])) {
553 result = printsection(msg, DNS_SECTION_ANSWER, "ANSWER",
554 ISC_TF(!short_form), query);
555 if (result != ISC_R_SUCCESS)
559 if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_AUTHORITY]) &&
562 result = printsection(msg, DNS_SECTION_AUTHORITY, "AUTHORITY",
564 if (result != ISC_R_SUCCESS)
567 if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_ADDITIONAL]) &&
570 result = printsection(msg, DNS_SECTION_ADDITIONAL,
571 "ADDITIONAL", ISC_TRUE, query);
572 if (result != ISC_R_SUCCESS)
575 if ((tsig != NULL) && !short_form) {
577 result = printrdata(msg, tsig, tsigname,
578 "PSEUDOSECTION TSIG", ISC_TRUE);
579 if (result != ISC_R_SUCCESS)
585 if (short_form && !default_lookups &&
586 ISC_LIST_EMPTY(msg->sections[DNS_SECTION_ANSWER])) {
587 char namestr[DNS_NAME_FORMATSIZE];
588 char typestr[DNS_RDATATYPE_FORMATSIZE];
589 dns_name_format(query->lookup->name, namestr, sizeof(namestr));
590 dns_rdatatype_format(query->lookup->rdtype, typestr,
592 printf("%s has no %s record\n", namestr, typestr);
594 seen_error = force_error;
598 static const char * optstring = "46ac:dilnm:rst:vwCDN:R:TW:";
601 pre_parse_args(int argc, char **argv) {
604 while ((c = isc_commandline_parse(argc, argv, optstring)) != -1) {
607 memdebugging = ISC_TRUE;
608 if (strcasecmp("trace", isc_commandline_argument) == 0)
609 isc_mem_debugging |= ISC_MEM_DEBUGTRACE;
610 else if (!strcasecmp("record",
611 isc_commandline_argument) == 0)
612 isc_mem_debugging |= ISC_MEM_DEBUGRECORD;
613 else if (strcasecmp("usage",
614 isc_commandline_argument) == 0)
615 isc_mem_debugging |= ISC_MEM_DEBUGUSAGE;
633 debugging = ISC_TRUE;
643 isc_commandline_reset = ISC_TRUE;
644 isc_commandline_index = 1;
648 parse_args(isc_boolean_t is_batchfile, int argc, char **argv) {
649 char hostname[MXNAME];
650 dig_lookup_t *lookup;
654 isc_result_t result = ISC_R_SUCCESS;
655 dns_rdatatype_t rdtype;
656 dns_rdataclass_t rdclass;
657 isc_uint32_t serial = 0;
659 UNUSED(is_batchfile);
661 lookup = make_empty_lookup();
663 lookup->servfail_stops = ISC_FALSE;
664 lookup->comments = ISC_FALSE;
666 while ((c = isc_commandline_parse(argc, argv, optstring)) != -1) {
669 lookup->tcp_mode = ISC_TRUE;
670 lookup->rdtype = dns_rdatatype_axfr;
671 lookup->rdtypeset = ISC_TRUE;
676 short_form = ISC_FALSE;
679 lookup->recurse = ISC_FALSE;
682 if (strncasecmp(isc_commandline_argument,
684 rdtype = dns_rdatatype_ixfr;
685 /* XXXMPA add error checking */
686 serial = strtoul(isc_commandline_argument + 5,
688 result = ISC_R_SUCCESS;
690 tr.base = isc_commandline_argument;
691 tr.length = strlen(isc_commandline_argument);
692 result = dns_rdatatype_fromtext(&rdtype,
693 (isc_textregion_t *)&tr);
696 if (result != ISC_R_SUCCESS) {
698 fatal("invalid type: %s\n",
699 isc_commandline_argument);
701 if (!lookup->rdtypeset ||
702 lookup->rdtype != dns_rdatatype_axfr)
703 lookup->rdtype = rdtype;
704 lookup->rdtypeset = ISC_TRUE;
708 if (rdtype == dns_rdatatype_axfr) {
710 list_type = dns_rdatatype_any;
711 short_form = ISC_FALSE;
712 lookup->tcp_mode = ISC_TRUE;
713 } else if (rdtype == dns_rdatatype_ixfr) {
714 lookup->ixfr_serial = serial;
715 lookup->tcp_mode = ISC_TRUE;
718 } else if (rdtype == dns_rdatatype_a ||
719 rdtype == dns_rdatatype_aaaa ||
720 rdtype == dns_rdatatype_mx) {
721 idnoptions = IDN_ASCCHECK;
726 list_addresses = ISC_FALSE;
727 default_lookups = ISC_FALSE;
730 tr.base = isc_commandline_argument;
731 tr.length = strlen(isc_commandline_argument);
732 result = dns_rdataclass_fromtext(&rdclass,
733 (isc_textregion_t *)&tr);
735 if (result != ISC_R_SUCCESS) {
737 fatal("invalid class: %s\n",
738 isc_commandline_argument);
740 lookup->rdclass = rdclass;
741 lookup->rdclassset = ISC_TRUE;
743 default_lookups = ISC_FALSE;
746 if (!lookup->rdtypeset ||
747 lookup->rdtype != dns_rdatatype_axfr)
748 lookup->rdtype = dns_rdatatype_any;
749 list_type = dns_rdatatype_any;
750 list_addresses = ISC_FALSE;
751 lookup->rdtypeset = ISC_TRUE;
752 short_form = ISC_FALSE;
753 default_lookups = ISC_FALSE;
756 lookup->ip6_int = ISC_TRUE;
762 /* Handled by pre_parse_args(). */
766 * The timer routines are coded such that
767 * timeout==MAXINT doesn't enable the timer
772 timeout = atoi(isc_commandline_argument);
777 tries = atoi(isc_commandline_argument) + 1;
782 lookup->tcp_mode = ISC_TRUE;
785 debug("showing all SOAs");
786 lookup->rdtype = dns_rdatatype_ns;
787 lookup->rdtypeset = ISC_TRUE;
788 lookup->rdclass = dns_rdataclass_in;
789 lookup->rdclassset = ISC_TRUE;
790 lookup->ns_search_only = ISC_TRUE;
791 lookup->trace_root = ISC_TRUE;
792 lookup->identify_previous_line = ISC_TRUE;
793 default_lookups = ISC_FALSE;
796 debug("setting NDOTS to %s",
797 isc_commandline_argument);
798 ndots = atoi(isc_commandline_argument);
801 /* Handled by pre_parse_args(). */
805 isc_net_disableipv6();
806 have_ipv6 = ISC_FALSE;
808 fatal("can't find IPv4 networking");
812 isc_net_disableipv4();
813 have_ipv4 = ISC_FALSE;
815 fatal("can't find IPv6 networking");
818 lookup->servfail_stops = ISC_TRUE;
823 lookup->retries = tries;
825 if (isc_commandline_index >= argc)
828 strlcpy(hostname, argv[isc_commandline_index], sizeof(hostname));
830 if (argc > isc_commandline_index + 1) {
831 set_nameserver(argv[isc_commandline_index+1]);
832 debug("server is %s", argv[isc_commandline_index+1]);
833 listed_server = ISC_TRUE;
837 lookup->pending = ISC_FALSE;
838 if (get_reverse(store, sizeof(store), hostname,
839 lookup->ip6_int, ISC_TRUE) == ISC_R_SUCCESS) {
840 strncpy(lookup->textname, store, sizeof(lookup->textname));
841 lookup->textname[sizeof(lookup->textname)-1] = 0;
842 lookup->rdtype = dns_rdatatype_ptr;
843 lookup->rdtypeset = ISC_TRUE;
844 default_lookups = ISC_FALSE;
846 strncpy(lookup->textname, hostname, sizeof(lookup->textname));
847 lookup->textname[sizeof(lookup->textname)-1]=0;
848 usesearch = ISC_TRUE;
850 lookup->new_search = ISC_TRUE;
851 ISC_LIST_APPEND(lookup_list, lookup, link);
855 main(int argc, char **argv) {
860 ISC_LIST_INIT(lookup_list);
861 ISC_LIST_INIT(server_list);
862 ISC_LIST_INIT(search_list);
866 idnoptions = IDN_ASCCHECK;
871 pre_parse_args(argc, argv);
872 result = isc_app_start();
873 check_result(result, "isc_app_start");
875 parse_args(ISC_FALSE, argc, argv);
877 result = isc_app_onrun(mctx, global_task, onrun_callback, NULL);
878 check_result(result, "isc_app_onrun");
883 return ((seen_error == 0) ? 0 : 1);