2 * Copyright (C) 2004-2007 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.94.18.19 2007/08/28 07:19:55 tbox 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" },
130 "Usage: host [-aCdlriTwv] [-c class] [-N ndots] [-t type] [-W time]\n"
131 " [-R number] [-m flag] hostname [server]\n"
132 " -a is equivalent to -v -t ANY\n"
133 " -c specifies query class for non-IN data\n"
134 " -C compares SOA records on authoritative nameservers\n"
135 " -d is equivalent to -v\n"
136 " -l lists all hosts in a domain, using AXFR\n"
137 " -i IP6.INT reverse lookups\n"
138 " -N changes the number of dots allowed before root lookup is done\n"
139 " -r disables recursive processing\n"
140 " -R specifies number of retries for UDP packets\n"
141 " -s a SERVFAIL response should stop query\n"
142 " -t specifies the query type\n"
143 " -T enables TCP/IP mode\n"
144 " -v enables verbose output\n"
145 " -w specifies to wait forever for a reply\n"
146 " -W specifies how long to wait for a reply\n"
147 " -4 use IPv4 query transport only\n"
148 " -6 use IPv6 query transport only\n"
149 " -m set memory debugging flag (trace|record|usage)\n", stderr);
154 dighost_shutdown(void) {
159 received(int bytes, isc_sockaddr_t *from, dig_query_t *query) {
164 char fromtext[ISC_SOCKADDR_FORMATSIZE];
165 isc_sockaddr_format(from, fromtext, sizeof(fromtext));
167 diff = (int) isc_time_microdiff(&now, &query->time_sent);
168 printf("Received %u bytes from %s in %d ms\n",
169 bytes, fromtext, diff/1000);
174 trying(char *frm, dig_lookup_t *lookup) {
178 printf("Trying \"%s\"\n", frm);
182 say_message(dns_name_t *name, const char *msg, dns_rdata_t *rdata,
185 isc_buffer_t *b = NULL;
186 char namestr[DNS_NAME_FORMATSIZE];
189 unsigned int bufsize = BUFSIZ;
191 dns_name_format(name, namestr, sizeof(namestr));
193 result = isc_buffer_allocate(mctx, &b, bufsize);
194 check_result(result, "isc_buffer_allocate");
195 result = dns_rdata_totext(rdata, NULL, b);
196 if (result == ISC_R_NOSPACE) {
201 check_result(result, "dns_rdata_totext");
202 isc_buffer_usedregion(b, &r);
203 if (query->lookup->identify_previous_line) {
204 printf("Nameserver %s:\n\t",
207 printf("%s %s %.*s", namestr,
208 msg, (int)r.length, (char *)r.base);
209 if (query->lookup->identify) {
210 printf(" on server %s", query->servname);
216 /* Just for compatibility : not use in host program */
218 printrdataset(dns_name_t *owner_name, dns_rdataset_t *rdataset,
219 isc_buffer_t *target)
228 printsection(dns_message_t *msg, dns_section_t sectionid,
229 const char *section_name, isc_boolean_t headers,
232 dns_name_t *name, *print_name;
233 dns_rdataset_t *rdataset;
234 dns_rdata_t rdata = DNS_RDATA_INIT;
236 isc_result_t result, loopresult;
238 dns_name_t empty_name;
241 isc_boolean_t no_rdata;
243 if (sectionid == DNS_SECTION_QUESTION)
246 no_rdata = ISC_FALSE;
249 printf(";; %s SECTION:\n", section_name);
251 dns_name_init(&empty_name, NULL);
253 result = dns_message_firstname(msg, sectionid);
254 if (result == ISC_R_NOMORE)
255 return (ISC_R_SUCCESS);
256 else if (result != ISC_R_SUCCESS)
261 dns_message_currentname(msg, sectionid, &name);
263 isc_buffer_init(&target, t, sizeof(t));
267 for (rdataset = ISC_LIST_HEAD(name->list);
269 rdataset = ISC_LIST_NEXT(rdataset, link)) {
270 if (query->lookup->rdtype == dns_rdatatype_axfr &&
271 !((!list_addresses &&
272 (list_type == dns_rdatatype_any ||
273 rdataset->type == list_type)) ||
275 (rdataset->type == dns_rdatatype_a ||
276 rdataset->type == dns_rdatatype_aaaa ||
277 rdataset->type == dns_rdatatype_ns ||
278 rdataset->type == dns_rdatatype_ptr))))
281 result = dns_rdataset_totext(rdataset,
286 if (result != ISC_R_SUCCESS)
290 print_name = &empty_name;
294 UNUSED(first); /* Shut up compiler. */
297 loopresult = dns_rdataset_first(rdataset);
298 while (loopresult == ISC_R_SUCCESS) {
301 char typebuf[DNS_RDATATYPE_FORMATSIZE];
302 char typebuf2[DNS_RDATATYPE_FORMATSIZE
304 dns_rdataset_current(rdataset, &rdata);
306 for (t = rtypes; t->text != NULL; t++) {
307 if (t->type == rdata.type) {
313 dns_rdatatype_format(rdata.type,
316 snprintf(typebuf2, sizeof(typebuf2),
317 "has %s record", typebuf);
320 say_message(print_name, rtt,
322 dns_rdata_reset(&rdata);
324 dns_rdataset_next(rdataset);
329 isc_buffer_usedregion(&target, &r);
331 printf(";%.*s", (int)r.length,
334 printf("%.*s", (int)r.length, (char *)r.base);
337 result = dns_message_nextname(msg, sectionid);
338 if (result == ISC_R_NOMORE)
340 else if (result != ISC_R_SUCCESS)
344 return (ISC_R_SUCCESS);
348 printrdata(dns_message_t *msg, dns_rdataset_t *rdataset, dns_name_t *owner,
349 const char *set_name, isc_boolean_t headers)
358 printf(";; %s SECTION:\n", set_name);
360 isc_buffer_init(&target, t, sizeof(t));
362 result = dns_rdataset_totext(rdataset, owner, ISC_FALSE, ISC_FALSE,
364 if (result != ISC_R_SUCCESS)
366 isc_buffer_usedregion(&target, &r);
367 printf("%.*s", (int)r.length, (char *)r.base);
369 return (ISC_R_SUCCESS);
373 chase_cnamechain(dns_message_t *msg, dns_name_t *qname) {
375 dns_rdataset_t *rdataset;
376 dns_rdata_cname_t cname;
377 dns_rdata_t rdata = DNS_RDATA_INIT;
378 unsigned int i = msg->counts[DNS_SECTION_ANSWER];
382 result = dns_message_findname(msg, DNS_SECTION_ANSWER, qname,
383 dns_rdatatype_cname, 0, NULL,
385 if (result != ISC_R_SUCCESS)
387 result = dns_rdataset_first(rdataset);
388 check_result(result, "dns_rdataset_first");
389 dns_rdata_reset(&rdata);
390 dns_rdataset_current(rdataset, &rdata);
391 result = dns_rdata_tostruct(&rdata, &cname, NULL);
392 check_result(result, "dns_rdata_tostruct");
393 dns_name_copy(&cname.cname, qname, NULL);
394 dns_rdata_freestruct(&cname);
399 printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) {
400 isc_boolean_t did_flag = ISC_FALSE;
401 dns_rdataset_t *opt, *tsig = NULL;
402 dns_name_t *tsigname;
403 isc_result_t result = ISC_R_SUCCESS;
409 * We get called multiple times.
410 * Preserve any existing error status.
412 force_error = (seen_error == 1) ? 1 : 0;
414 if (listed_server && !printed_server) {
415 char sockstr[ISC_SOCKADDR_FORMATSIZE];
417 printf("Using domain server:\n");
418 printf("Name: %s\n", query->userarg);
419 isc_sockaddr_format(&query->sockaddr, sockstr,
421 printf("Address: %s\n", sockstr);
422 printf("Aliases: \n\n");
423 printed_server = ISC_TRUE;
426 if (msg->rcode != 0) {
427 char namestr[DNS_NAME_FORMATSIZE];
428 dns_name_format(query->lookup->name, namestr, sizeof(namestr));
429 printf("Host %s not found: %d(%s)\n",
430 (msg->rcode != dns_rcode_nxdomain) ? namestr :
431 query->lookup->textname, msg->rcode,
432 rcodetext[msg->rcode]);
433 return (ISC_R_SUCCESS);
436 if (default_lookups && query->lookup->rdtype == dns_rdatatype_a) {
437 char namestr[DNS_NAME_FORMATSIZE];
438 dig_lookup_t *lookup;
439 dns_fixedname_t fixed;
442 /* Add AAAA and MX lookups. */
443 dns_fixedname_init(&fixed);
444 name = dns_fixedname_name(&fixed);
445 dns_name_copy(query->lookup->name, name, NULL);
446 chase_cnamechain(msg, name);
447 dns_name_format(name, namestr, sizeof(namestr));
448 lookup = clone_lookup(query->lookup, ISC_FALSE);
449 if (lookup != NULL) {
450 strncpy(lookup->textname, namestr,
451 sizeof(lookup->textname));
452 lookup->textname[sizeof(lookup->textname)-1] = 0;
453 lookup->rdtype = dns_rdatatype_aaaa;
454 lookup->rdtypeset = ISC_TRUE;
455 lookup->origin = NULL;
456 lookup->retries = tries;
457 ISC_LIST_APPEND(lookup_list, lookup, link);
459 lookup = clone_lookup(query->lookup, ISC_FALSE);
460 if (lookup != NULL) {
461 strncpy(lookup->textname, namestr,
462 sizeof(lookup->textname));
463 lookup->textname[sizeof(lookup->textname)-1] = 0;
464 lookup->rdtype = dns_rdatatype_mx;
465 lookup->rdtypeset = ISC_TRUE;
466 lookup->origin = NULL;
467 lookup->retries = tries;
468 ISC_LIST_APPEND(lookup_list, lookup, link);
473 printf(";; ->>HEADER<<- opcode: %s, status: %s, id: %u\n",
474 opcodetext[msg->opcode], rcodetext[msg->rcode],
476 printf(";; flags: ");
477 if ((msg->flags & DNS_MESSAGEFLAG_QR) != 0) {
481 if ((msg->flags & DNS_MESSAGEFLAG_AA) != 0) {
482 printf("%saa", did_flag ? " " : "");
485 if ((msg->flags & DNS_MESSAGEFLAG_TC) != 0) {
486 printf("%stc", did_flag ? " " : "");
489 if ((msg->flags & DNS_MESSAGEFLAG_RD) != 0) {
490 printf("%srd", did_flag ? " " : "");
493 if ((msg->flags & DNS_MESSAGEFLAG_RA) != 0) {
494 printf("%sra", did_flag ? " " : "");
497 if ((msg->flags & DNS_MESSAGEFLAG_AD) != 0) {
498 printf("%sad", did_flag ? " " : "");
501 if ((msg->flags & DNS_MESSAGEFLAG_CD) != 0) {
502 printf("%scd", did_flag ? " " : "");
505 printf("; QUERY: %u, ANSWER: %u, "
506 "AUTHORITY: %u, ADDITIONAL: %u\n",
507 msg->counts[DNS_SECTION_QUESTION],
508 msg->counts[DNS_SECTION_ANSWER],
509 msg->counts[DNS_SECTION_AUTHORITY],
510 msg->counts[DNS_SECTION_ADDITIONAL]);
511 opt = dns_message_getopt(msg);
513 printf(";; EDNS: version: %u, udp=%u\n",
514 (unsigned int)((opt->ttl & 0x00ff0000) >> 16),
515 (unsigned int)opt->rdclass);
517 tsig = dns_message_gettsig(msg, &tsigname);
519 printf(";; PSEUDOSECTIONS: TSIG\n");
521 if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_QUESTION]) &&
524 result = printsection(msg, DNS_SECTION_QUESTION, "QUESTION",
526 if (result != ISC_R_SUCCESS)
529 if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_ANSWER])) {
532 result = printsection(msg, DNS_SECTION_ANSWER, "ANSWER",
533 ISC_TF(!short_form), query);
534 if (result != ISC_R_SUCCESS)
538 if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_AUTHORITY]) &&
541 result = printsection(msg, DNS_SECTION_AUTHORITY, "AUTHORITY",
543 if (result != ISC_R_SUCCESS)
546 if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_ADDITIONAL]) &&
549 result = printsection(msg, DNS_SECTION_ADDITIONAL,
550 "ADDITIONAL", ISC_TRUE, query);
551 if (result != ISC_R_SUCCESS)
554 if ((tsig != NULL) && !short_form) {
556 result = printrdata(msg, tsig, tsigname,
557 "PSEUDOSECTION TSIG", ISC_TRUE);
558 if (result != ISC_R_SUCCESS)
564 if (short_form && !default_lookups &&
565 ISC_LIST_EMPTY(msg->sections[DNS_SECTION_ANSWER])) {
566 char namestr[DNS_NAME_FORMATSIZE];
567 char typestr[DNS_RDATATYPE_FORMATSIZE];
568 dns_name_format(query->lookup->name, namestr, sizeof(namestr));
569 dns_rdatatype_format(query->lookup->rdtype, typestr,
571 printf("%s has no %s record\n", namestr, typestr);
573 seen_error = force_error;
577 static const char * optstring = "46ac:dilnm:rst:vwCDN:R:TW:";
580 pre_parse_args(int argc, char **argv) {
583 while ((c = isc_commandline_parse(argc, argv, optstring)) != -1) {
586 memdebugging = ISC_TRUE;
587 if (strcasecmp("trace", isc_commandline_argument) == 0)
588 isc_mem_debugging |= ISC_MEM_DEBUGTRACE;
589 else if (!strcasecmp("record",
590 isc_commandline_argument) == 0)
591 isc_mem_debugging |= ISC_MEM_DEBUGRECORD;
592 else if (strcasecmp("usage",
593 isc_commandline_argument) == 0)
594 isc_mem_debugging |= ISC_MEM_DEBUGUSAGE;
620 isc_commandline_reset = ISC_TRUE;
621 isc_commandline_index = 1;
625 parse_args(isc_boolean_t is_batchfile, int argc, char **argv) {
626 char hostname[MXNAME];
627 dig_lookup_t *lookup;
631 isc_result_t result = ISC_R_SUCCESS;
632 dns_rdatatype_t rdtype;
633 dns_rdataclass_t rdclass;
634 isc_uint32_t serial = 0;
636 UNUSED(is_batchfile);
638 lookup = make_empty_lookup();
640 lookup->servfail_stops = ISC_FALSE;
641 lookup->comments = ISC_FALSE;
643 while ((c = isc_commandline_parse(argc, argv, optstring)) != -1) {
646 lookup->tcp_mode = ISC_TRUE;
647 lookup->rdtype = dns_rdatatype_axfr;
648 lookup->rdtypeset = ISC_TRUE;
653 short_form = ISC_FALSE;
656 lookup->recurse = ISC_FALSE;
659 if (strncasecmp(isc_commandline_argument,
661 rdtype = dns_rdatatype_ixfr;
662 /* XXXMPA add error checking */
663 serial = strtoul(isc_commandline_argument + 5,
665 result = ISC_R_SUCCESS;
667 tr.base = isc_commandline_argument;
668 tr.length = strlen(isc_commandline_argument);
669 result = dns_rdatatype_fromtext(&rdtype,
670 (isc_textregion_t *)&tr);
673 if (result != ISC_R_SUCCESS) {
675 fatal("invalid type: %s\n",
676 isc_commandline_argument);
678 if (!lookup->rdtypeset ||
679 lookup->rdtype != dns_rdatatype_axfr)
680 lookup->rdtype = rdtype;
681 lookup->rdtypeset = ISC_TRUE;
685 if (rdtype == dns_rdatatype_axfr) {
687 list_type = dns_rdatatype_any;
688 short_form = ISC_FALSE;
689 lookup->tcp_mode = ISC_TRUE;
690 } else if (rdtype == dns_rdatatype_ixfr) {
691 lookup->ixfr_serial = serial;
694 } else if (rdtype == dns_rdatatype_a ||
695 rdtype == dns_rdatatype_aaaa ||
696 rdtype == dns_rdatatype_mx) {
697 idnoptions = IDN_ASCCHECK;
702 list_addresses = ISC_FALSE;
703 default_lookups = ISC_FALSE;
706 tr.base = isc_commandline_argument;
707 tr.length = strlen(isc_commandline_argument);
708 result = dns_rdataclass_fromtext(&rdclass,
709 (isc_textregion_t *)&tr);
711 if (result != ISC_R_SUCCESS) {
713 fatal("invalid class: %s\n",
714 isc_commandline_argument);
716 lookup->rdclass = rdclass;
717 lookup->rdclassset = ISC_TRUE;
719 default_lookups = ISC_FALSE;
722 if (!lookup->rdtypeset ||
723 lookup->rdtype != dns_rdatatype_axfr)
724 lookup->rdtype = dns_rdatatype_any;
725 list_type = dns_rdatatype_any;
726 list_addresses = ISC_FALSE;
727 lookup->rdtypeset = ISC_TRUE;
728 short_form = ISC_FALSE;
729 default_lookups = ISC_FALSE;
732 lookup->ip6_int = ISC_TRUE;
738 /* Handled by pre_parse_args(). */
742 * The timer routines are coded such that
743 * timeout==MAXINT doesn't enable the timer
748 timeout = atoi(isc_commandline_argument);
753 tries = atoi(isc_commandline_argument) + 1;
758 lookup->tcp_mode = ISC_TRUE;
761 debug("showing all SOAs");
762 lookup->rdtype = dns_rdatatype_ns;
763 lookup->rdtypeset = ISC_TRUE;
764 lookup->rdclass = dns_rdataclass_in;
765 lookup->rdclassset = ISC_TRUE;
766 lookup->ns_search_only = ISC_TRUE;
767 lookup->trace_root = ISC_TRUE;
768 lookup->identify_previous_line = ISC_TRUE;
769 default_lookups = ISC_FALSE;
772 debug("setting NDOTS to %s",
773 isc_commandline_argument);
774 ndots = atoi(isc_commandline_argument);
777 debugging = ISC_TRUE;
781 isc_net_disableipv6();
782 have_ipv6 = ISC_FALSE;
784 fatal("can't find IPv4 networking");
788 isc_net_disableipv4();
789 have_ipv4 = ISC_FALSE;
791 fatal("can't find IPv6 networking");
794 lookup->servfail_stops = ISC_TRUE;
799 lookup->retries = tries;
801 if (isc_commandline_index >= argc)
804 strncpy(hostname, argv[isc_commandline_index], sizeof(hostname));
805 hostname[sizeof(hostname)-1]=0;
806 if (argc > isc_commandline_index + 1) {
807 set_nameserver(argv[isc_commandline_index+1]);
808 debug("server is %s", argv[isc_commandline_index+1]);
809 listed_server = ISC_TRUE;
813 lookup->pending = ISC_FALSE;
814 if (get_reverse(store, sizeof(store), hostname,
815 lookup->ip6_int, ISC_TRUE) == ISC_R_SUCCESS) {
816 strncpy(lookup->textname, store, sizeof(lookup->textname));
817 lookup->textname[sizeof(lookup->textname)-1] = 0;
818 lookup->rdtype = dns_rdatatype_ptr;
819 lookup->rdtypeset = ISC_TRUE;
820 default_lookups = ISC_FALSE;
822 strncpy(lookup->textname, hostname, sizeof(lookup->textname));
823 lookup->textname[sizeof(lookup->textname)-1]=0;
825 lookup->new_search = ISC_TRUE;
826 ISC_LIST_APPEND(lookup_list, lookup, link);
828 usesearch = ISC_TRUE;
832 main(int argc, char **argv) {
837 ISC_LIST_INIT(lookup_list);
838 ISC_LIST_INIT(server_list);
839 ISC_LIST_INIT(search_list);
843 idnoptions = IDN_ASCCHECK;
848 pre_parse_args(argc, argv);
849 result = isc_app_start();
850 check_result(result, "isc_app_start");
852 parse_args(ISC_FALSE, argc, argv);
854 result = isc_app_onrun(mctx, global_task, onrun_callback, NULL);
855 check_result(result, "isc_app_onrun");
860 return ((seen_error == 0) ? 0 : 1);