1 /* Copyright 1998 by the Massachusetts Institute of Technology.
4 * Permission to use, copy, modify, and distribute this
5 * software and its documentation for any purpose and without
6 * fee is hereby granted, provided that the above copyright
7 * notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting
9 * documentation, and that the name of M.I.T. not be used in
10 * advertising or publicity pertaining to distribution of the
11 * software without specific, written prior permission.
12 * M.I.T. makes no representations about the suitability of
13 * this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
17 #include "ares_setup.h"
19 #ifdef HAVE_SYS_SOCKET_H
20 # include <sys/socket.h>
22 #ifdef HAVE_NETINET_IN_H
23 # include <netinet/in.h>
25 #ifdef HAVE_ARPA_INET_H
26 # include <arpa/inet.h>
31 #ifdef HAVE_ARPA_NAMESER_H
32 # include <arpa/nameser.h>
36 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
37 # include <arpa/nameser_compat.h>
40 #ifdef HAVE_SYS_TIME_H
41 # include <sys/time.h>
57 #include "inet_ntop.h"
58 #include "inet_net_pton.h"
59 #include "ares_getopt.h"
62 # include "ares_strdup.h"
63 # define strdup(ptr) ares_strdup(ptr)
66 #ifndef HAVE_STRCASECMP
67 # include "ares_strcasecmp.h"
68 # define strcasecmp(p1,p2) ares_strcasecmp(p1,p2)
71 #ifndef HAVE_STRNCASECMP
72 # include "ares_strcasecmp.h"
73 # define strncasecmp(p1,p2,n) ares_strncasecmp(p1,p2,n)
77 #undef WIN32 /* Redefined in MingW headers */
80 /* Mac OS X portability check */
82 #define T_SRV 33 /* server selection */
85 /* AIX portability check */
87 #define T_NAPTR 35 /* naming authority pointer */
95 static const struct nv flags[] = {
96 { "usevc", ARES_FLAG_USEVC },
97 { "primary", ARES_FLAG_PRIMARY },
98 { "igntc", ARES_FLAG_IGNTC },
99 { "norecurse", ARES_FLAG_NORECURSE },
100 { "stayopen", ARES_FLAG_STAYOPEN },
101 { "noaliases", ARES_FLAG_NOALIASES }
103 static const int nflags = sizeof(flags) / sizeof(flags[0]);
105 static const struct nv classes[] = {
107 { "CHAOS", C_CHAOS },
111 static const int nclasses = sizeof(classes) / sizeof(classes[0]);
113 static const struct nv types[] = {
118 { "CNAME", T_CNAME },
126 { "HINFO", T_HINFO },
127 { "MINFO", T_MINFO },
131 { "AFSDB", T_AFSDB },
136 { "NSAP_PTR", T_NSAP_PTR },
145 { "MAILB", T_MAILB },
146 { "MAILA", T_MAILA },
147 { "NAPTR", T_NAPTR },
150 static const int ntypes = sizeof(types) / sizeof(types[0]);
152 static const char *opcodes[] = {
153 "QUERY", "IQUERY", "STATUS", "(reserved)", "NOTIFY",
154 "(unknown)", "(unknown)", "(unknown)", "(unknown)",
155 "UPDATEA", "UPDATED", "UPDATEDA", "UPDATEM", "UPDATEMA",
156 "ZONEINIT", "ZONEREF"
159 static const char *rcodes[] = {
160 "NOERROR", "FORMERR", "SERVFAIL", "NXDOMAIN", "NOTIMP", "REFUSED",
161 "(unknown)", "(unknown)", "(unknown)", "(unknown)", "(unknown)",
162 "(unknown)", "(unknown)", "(unknown)", "(unknown)", "NOCHANGE"
165 static void callback(void *arg, int status, int timeouts,
166 unsigned char *abuf, int alen);
167 static const unsigned char *display_question(const unsigned char *aptr,
168 const unsigned char *abuf,
170 static const unsigned char *display_rr(const unsigned char *aptr,
171 const unsigned char *abuf, int alen);
172 static const char *type_name(int type);
173 static const char *class_name(int dnsclass);
174 static void usage(void);
175 static void destroy_addr_list(struct ares_addr_node *head);
176 static void append_addr_list(struct ares_addr_node **head,
177 struct ares_addr_node *node);
179 int main(int argc, char **argv)
181 ares_channel channel;
182 int c, i, optmask = ARES_OPT_FLAGS, dnsclass = C_IN, type = T_A;
183 int status, nfds, count;
184 struct ares_options options;
185 struct hostent *hostent;
186 fd_set read_fds, write_fds;
187 struct timeval *tvp, tv;
188 struct ares_addr_node *srvr, *servers = NULL;
191 WORD wVersionRequested = MAKEWORD(USE_WINSOCK,USE_WINSOCK);
193 WSAStartup(wVersionRequested, &wsaData);
196 status = ares_library_init(ARES_LIB_INIT_ALL);
197 if (status != ARES_SUCCESS)
199 fprintf(stderr, "ares_library_init: %s\n", ares_strerror(status));
203 options.flags = ARES_FLAG_NOCHECKRESP;
204 options.servers = NULL;
205 options.nservers = 0;
206 while ((c = ares_getopt(argc, argv, "df:s:c:t:T:U:")) != -1)
218 for (i = 0; i < nflags; i++)
220 if (strcmp(flags[i].name, optarg) == 0)
224 options.flags |= flags[i].value;
230 /* User-specified name servers override default ones. */
231 srvr = malloc(sizeof(struct ares_addr_node));
234 fprintf(stderr, "Out of memory!\n");
235 destroy_addr_list(servers);
238 append_addr_list(&servers, srvr);
239 if (ares_inet_pton(AF_INET, optarg, &srvr->addr.addr4) > 0)
240 srvr->family = AF_INET;
241 else if (ares_inet_pton(AF_INET6, optarg, &srvr->addr.addr6) > 0)
242 srvr->family = AF_INET6;
245 hostent = gethostbyname(optarg);
248 fprintf(stderr, "adig: server %s not found.\n", optarg);
249 destroy_addr_list(servers);
252 switch (hostent->h_addrtype)
255 srvr->family = AF_INET;
256 memcpy(&srvr->addr.addr4, hostent->h_addr,
257 sizeof(srvr->addr.addr4));
260 srvr->family = AF_INET6;
261 memcpy(&srvr->addr.addr6, hostent->h_addr,
262 sizeof(srvr->addr.addr6));
266 "adig: server %s unsupported address family.\n", optarg);
267 destroy_addr_list(servers);
271 /* Notice that calling ares_init_options() without servers in the
272 * options struct and with ARES_OPT_SERVERS set simultaneously in
273 * the options mask, results in an initialization with no servers.
274 * When alternative name servers have been specified these are set
275 * later calling ares_set_servers() overriding any existing server
276 * configuration. To prevent initial configuration with default
277 * servers that will be discarded later, ARES_OPT_SERVERS is set.
278 * If this flag is not set here the result shall be the same but
279 * ares_init_options() will do needless work. */
280 optmask |= ARES_OPT_SERVERS;
284 /* Set the query class. */
285 for (i = 0; i < nclasses; i++)
287 if (strcasecmp(classes[i].name, optarg) == 0)
291 dnsclass = classes[i].value;
297 /* Set the query type. */
298 for (i = 0; i < ntypes; i++)
300 if (strcasecmp(types[i].name, optarg) == 0)
304 type = types[i].value;
310 /* Set the TCP port number. */
311 if (!ISDIGIT(*optarg))
313 options.tcp_port = (unsigned short)strtol(optarg, NULL, 0);
314 optmask |= ARES_OPT_TCP_PORT;
318 /* Set the UDP port number. */
319 if (!ISDIGIT(*optarg))
321 options.udp_port = (unsigned short)strtol(optarg, NULL, 0);
322 optmask |= ARES_OPT_UDP_PORT;
331 status = ares_init_options(&channel, &options, optmask);
333 if (status != ARES_SUCCESS)
335 fprintf(stderr, "ares_init_options: %s\n",
336 ares_strerror(status));
342 status = ares_set_servers(channel, servers);
343 destroy_addr_list(servers);
344 if (status != ARES_SUCCESS)
346 fprintf(stderr, "ares_init_options: %s\n",
347 ares_strerror(status));
352 /* Initiate the queries, one per command-line argument. If there is
353 * only one query to do, supply NULL as the callback argument;
354 * otherwise, supply the query name as an argument so we can
355 * distinguish responses for the user when printing them out.
358 ares_query(channel, *argv, dnsclass, type, callback, (char *) NULL);
361 for (; *argv; argv++)
362 ares_query(channel, *argv, dnsclass, type, callback, *argv);
365 /* Wait for all queries to complete. */
370 nfds = ares_fds(channel, &read_fds, &write_fds);
373 tvp = ares_timeout(channel, NULL, &tv);
374 count = select(nfds, &read_fds, &write_fds, NULL, tvp);
375 if (count < 0 && SOCKERRNO != EINVAL)
380 ares_process(channel, &read_fds, &write_fds);
383 ares_destroy(channel);
385 ares_library_cleanup();
394 static void callback(void *arg, int status, int timeouts,
395 unsigned char *abuf, int alen)
397 char *name = (char *) arg;
398 int id, qr, opcode, aa, tc, rd, ra, rcode;
399 unsigned int qdcount, ancount, nscount, arcount, i;
400 const unsigned char *aptr;
404 /* Display the query name if given. */
406 printf("Answer for query %s:\n", name);
408 /* Display an error message if there was an error, but only stop if
409 * we actually didn't get an answer buffer.
411 if (status != ARES_SUCCESS)
413 printf("%s\n", ares_strerror(status));
418 /* Won't happen, but check anyway, for safety. */
422 /* Parse the answer header. */
423 id = DNS_HEADER_QID(abuf);
424 qr = DNS_HEADER_QR(abuf);
425 opcode = DNS_HEADER_OPCODE(abuf);
426 aa = DNS_HEADER_AA(abuf);
427 tc = DNS_HEADER_TC(abuf);
428 rd = DNS_HEADER_RD(abuf);
429 ra = DNS_HEADER_RA(abuf);
430 rcode = DNS_HEADER_RCODE(abuf);
431 qdcount = DNS_HEADER_QDCOUNT(abuf);
432 ancount = DNS_HEADER_ANCOUNT(abuf);
433 nscount = DNS_HEADER_NSCOUNT(abuf);
434 arcount = DNS_HEADER_ARCOUNT(abuf);
436 /* Display the answer header. */
437 printf("id: %d\n", id);
438 printf("flags: %s%s%s%s%s\n",
444 printf("opcode: %s\n", opcodes[opcode]);
445 printf("rcode: %s\n", rcodes[rcode]);
447 /* Display the questions. */
448 printf("Questions:\n");
449 aptr = abuf + HFIXEDSZ;
450 for (i = 0; i < qdcount; i++)
452 aptr = display_question(aptr, abuf, alen);
457 /* Display the answers. */
458 printf("Answers:\n");
459 for (i = 0; i < ancount; i++)
461 aptr = display_rr(aptr, abuf, alen);
466 /* Display the NS records. */
467 printf("NS records:\n");
468 for (i = 0; i < nscount; i++)
470 aptr = display_rr(aptr, abuf, alen);
475 /* Display the additional records. */
476 printf("Additional records:\n");
477 for (i = 0; i < arcount; i++)
479 aptr = display_rr(aptr, abuf, alen);
485 static const unsigned char *display_question(const unsigned char *aptr,
486 const unsigned char *abuf,
490 int type, dnsclass, status;
493 /* Parse the question name. */
494 status = ares_expand_name(aptr, abuf, alen, &name, &len);
495 if (status != ARES_SUCCESS)
499 /* Make sure there's enough data after the name for the fixed part
502 if (aptr + QFIXEDSZ > abuf + alen)
504 ares_free_string(name);
508 /* Parse the question type and class. */
509 type = DNS_QUESTION_TYPE(aptr);
510 dnsclass = DNS_QUESTION_CLASS(aptr);
513 /* Display the question, in a format sort of similar to how we will
516 printf("\t%-15s.\t", name);
517 if (dnsclass != C_IN)
518 printf("\t%s", class_name(dnsclass));
519 printf("\t%s\n", type_name(type));
520 ares_free_string(name);
524 static const unsigned char *display_rr(const unsigned char *aptr,
525 const unsigned char *abuf, int alen)
527 const unsigned char *p;
528 int type, dnsclass, ttl, dlen, status;
532 unsigned char * as_uchar;
536 /* Parse the RR name. */
537 status = ares_expand_name(aptr, abuf, alen, &name.as_char, &len);
538 if (status != ARES_SUCCESS)
542 /* Make sure there is enough data after the RR name for the fixed
545 if (aptr + RRFIXEDSZ > abuf + alen)
547 ares_free_string(name.as_char);
551 /* Parse the fixed part of the RR, and advance to the RR data
553 type = DNS_RR_TYPE(aptr);
554 dnsclass = DNS_RR_CLASS(aptr);
555 ttl = DNS_RR_TTL(aptr);
556 dlen = DNS_RR_LEN(aptr);
558 if (aptr + dlen > abuf + alen)
560 ares_free_string(name.as_char);
564 /* Display the RR name, class, and type. */
565 printf("\t%-15s.\t%d", name.as_char, ttl);
566 if (dnsclass != C_IN)
567 printf("\t%s", class_name(dnsclass));
568 printf("\t%s", type_name(type));
569 ares_free_string(name.as_char);
571 /* Display the RR data. Don't touch aptr. */
582 /* For these types, the RR data is just a domain name. */
583 status = ares_expand_name(aptr, abuf, alen, &name.as_char, &len);
584 if (status != ARES_SUCCESS)
586 printf("\t%s.", name.as_char);
587 ares_free_string(name.as_char);
591 /* The RR data is two length-counted character strings. */
594 if (p + len + 1 > aptr + dlen)
596 status = ares_expand_string(p, abuf, alen, &name.as_uchar, &len);
597 if (status != ARES_SUCCESS)
599 printf("\t%s", name.as_char);
600 ares_free_string(name.as_char);
603 if (p + len + 1 > aptr + dlen)
605 status = ares_expand_string(p, abuf, alen, &name.as_uchar, &len);
606 if (status != ARES_SUCCESS)
608 printf("\t%s", name.as_char);
609 ares_free_string(name.as_char);
613 /* The RR data is two domain names. */
615 status = ares_expand_name(p, abuf, alen, &name.as_char, &len);
616 if (status != ARES_SUCCESS)
618 printf("\t%s.", name.as_char);
619 ares_free_string(name.as_char);
621 status = ares_expand_name(p, abuf, alen, &name.as_char, &len);
622 if (status != ARES_SUCCESS)
624 printf("\t%s.", name.as_char);
625 ares_free_string(name.as_char);
629 /* The RR data is two bytes giving a preference ordering, and
630 * then a domain name.
634 printf("\t%d", DNS__16BIT(aptr));
635 status = ares_expand_name(aptr + 2, abuf, alen, &name.as_char, &len);
636 if (status != ARES_SUCCESS)
638 printf("\t%s.", name.as_char);
639 ares_free_string(name.as_char);
643 /* The RR data is two domain names and then five four-byte
644 * numbers giving the serial number and some timeouts.
647 status = ares_expand_name(p, abuf, alen, &name.as_char, &len);
648 if (status != ARES_SUCCESS)
650 printf("\t%s.\n", name.as_char);
651 ares_free_string(name.as_char);
653 status = ares_expand_name(p, abuf, alen, &name.as_char, &len);
654 if (status != ARES_SUCCESS)
656 printf("\t\t\t\t\t\t%s.\n", name.as_char);
657 ares_free_string(name.as_char);
659 if (p + 20 > aptr + dlen)
661 printf("\t\t\t\t\t\t( %lu %lu %lu %lu %lu )",
662 (unsigned long)DNS__32BIT(p), (unsigned long)DNS__32BIT(p+4),
663 (unsigned long)DNS__32BIT(p+8), (unsigned long)DNS__32BIT(p+12),
664 (unsigned long)DNS__32BIT(p+16));
668 /* The RR data is one or more length-counted character
671 while (p < aptr + dlen)
674 if (p + len + 1 > aptr + dlen)
676 status = ares_expand_string(p, abuf, alen, &name.as_uchar, &len);
677 if (status != ARES_SUCCESS)
679 printf("\t%s", name.as_char);
680 ares_free_string(name.as_char);
686 /* The RR data is a four-byte Internet address. */
689 printf("\t%s", ares_inet_ntop(AF_INET,aptr,addr,sizeof(addr)));
693 /* The RR data is a 16-byte IPv6 address. */
696 printf("\t%s", ares_inet_ntop(AF_INET6,aptr,addr,sizeof(addr)));
700 /* Not implemented yet */
704 /* The RR data is three two-byte numbers representing the
705 * priority, weight, and port, followed by a domain name.
708 printf("\t%d", DNS__16BIT(aptr));
709 printf(" %d", DNS__16BIT(aptr + 2));
710 printf(" %d", DNS__16BIT(aptr + 4));
712 status = ares_expand_name(aptr + 6, abuf, alen, &name.as_char, &len);
713 if (status != ARES_SUCCESS)
715 printf("\t%s.", name.as_char);
716 ares_free_string(name.as_char);
721 printf("\t%d", DNS__16BIT(aptr)); /* order */
722 printf(" %d\n", DNS__16BIT(aptr + 2)); /* preference */
725 status = ares_expand_string(p, abuf, alen, &name.as_uchar, &len);
726 if (status != ARES_SUCCESS)
728 printf("\t\t\t\t\t\t%s\n", name.as_char);
729 ares_free_string(name.as_char);
732 status = ares_expand_string(p, abuf, alen, &name.as_uchar, &len);
733 if (status != ARES_SUCCESS)
735 printf("\t\t\t\t\t\t%s\n", name.as_char);
736 ares_free_string(name.as_char);
739 status = ares_expand_string(p, abuf, alen, &name.as_uchar, &len);
740 if (status != ARES_SUCCESS)
742 printf("\t\t\t\t\t\t%s\n", name.as_char);
743 ares_free_string(name.as_char);
746 status = ares_expand_name(p, abuf, alen, &name.as_char, &len);
747 if (status != ARES_SUCCESS)
749 printf("\t\t\t\t\t\t%s", name.as_char);
750 ares_free_string(name.as_char);
755 printf("\t[Unknown RR; cannot parse]");
763 static const char *type_name(int type)
767 for (i = 0; i < ntypes; i++)
769 if (types[i].value == type)
770 return types[i].name;
775 static const char *class_name(int dnsclass)
779 for (i = 0; i < nclasses; i++)
781 if (classes[i].value == dnsclass)
782 return classes[i].name;
787 static void usage(void)
789 fprintf(stderr, "usage: adig [-f flag] [-s server] [-c class] "
790 "[-t type] [-p port] name ...\n");
794 static void destroy_addr_list(struct ares_addr_node *head)
798 struct ares_addr_node *detached = head;
804 static void append_addr_list(struct ares_addr_node **head,
805 struct ares_addr_node *node)
807 struct ares_addr_node *last;