1 /* Copyright 1998 by the Massachusetts Institute of Technology.
5 * Permission to use, copy, modify, and distribute this
6 * software and its documentation for any purpose and without
7 * fee is hereby granted, provided that the above copyright
8 * notice appear in all copies and that both that copyright
9 * notice and this permission notice appear in supporting
10 * documentation, and that the name of M.I.T. not be used in
11 * advertising or publicity pertaining to distribution of the
12 * software without specific, written prior permission.
13 * M.I.T. makes no representations about the suitability of
14 * this software for any purpose. It is provided "as is"
15 * without express or implied warranty.
18 #include "ares_setup.h"
20 #ifdef HAVE_SYS_SOCKET_H
21 # include <sys/socket.h>
23 #ifdef HAVE_NETINET_IN_H
24 # include <netinet/in.h>
26 #ifdef HAVE_ARPA_INET_H
27 # include <arpa/inet.h>
32 #ifdef HAVE_ARPA_NAMESER_H
33 # include <arpa/nameser.h>
37 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
38 # include <arpa/nameser_compat.h>
41 #ifdef HAVE_SYS_TIME_H
42 # include <sys/time.h>
59 #include "inet_ntop.h"
60 #include "inet_net_pton.h"
61 #include "ares_getopt.h"
64 # include "ares_strdup.h"
65 # define strdup(ptr) ares_strdup(ptr)
68 #ifndef HAVE_STRCASECMP
69 # include "ares_strcasecmp.h"
70 # define strcasecmp(p1,p2) ares_strcasecmp(p1,p2)
73 #ifndef HAVE_STRNCASECMP
74 # include "ares_strcasecmp.h"
75 # define strncasecmp(p1,p2,n) ares_strncasecmp(p1,p2,n)
79 #undef WIN32 /* Redefined in MingW headers */
82 /* Mac OS X portability check */
84 #define T_SRV 33 /* server selection */
87 /* AIX portability check */
89 #define T_NAPTR 35 /* naming authority pointer */
97 static const struct nv flags[] = {
98 { "usevc", ARES_FLAG_USEVC },
99 { "primary", ARES_FLAG_PRIMARY },
100 { "igntc", ARES_FLAG_IGNTC },
101 { "norecurse", ARES_FLAG_NORECURSE },
102 { "stayopen", ARES_FLAG_STAYOPEN },
103 { "noaliases", ARES_FLAG_NOALIASES }
105 static const int nflags = sizeof(flags) / sizeof(flags[0]);
107 static const struct nv classes[] = {
109 { "CHAOS", C_CHAOS },
113 static const int nclasses = sizeof(classes) / sizeof(classes[0]);
115 static const struct nv types[] = {
120 { "CNAME", T_CNAME },
128 { "HINFO", T_HINFO },
129 { "MINFO", T_MINFO },
133 { "AFSDB", T_AFSDB },
138 { "NSAP_PTR", T_NSAP_PTR },
147 { "MAILB", T_MAILB },
148 { "MAILA", T_MAILA },
149 { "NAPTR", T_NAPTR },
152 static const int ntypes = sizeof(types) / sizeof(types[0]);
154 static const char *opcodes[] = {
155 "QUERY", "IQUERY", "STATUS", "(reserved)", "NOTIFY",
156 "(unknown)", "(unknown)", "(unknown)", "(unknown)",
157 "UPDATEA", "UPDATED", "UPDATEDA", "UPDATEM", "UPDATEMA",
158 "ZONEINIT", "ZONEREF"
161 static const char *rcodes[] = {
162 "NOERROR", "FORMERR", "SERVFAIL", "NXDOMAIN", "NOTIMP", "REFUSED",
163 "(unknown)", "(unknown)", "(unknown)", "(unknown)", "(unknown)",
164 "(unknown)", "(unknown)", "(unknown)", "(unknown)", "NOCHANGE"
167 static void callback(void *arg, int status, int timeouts,
168 unsigned char *abuf, int alen);
169 static const unsigned char *display_question(const unsigned char *aptr,
170 const unsigned char *abuf,
172 static const unsigned char *display_rr(const unsigned char *aptr,
173 const unsigned char *abuf, int alen);
174 static const char *type_name(int type);
175 static const char *class_name(int dnsclass);
176 static void usage(void);
177 static void destroy_addr_list(struct ares_addr_node *head);
178 static void append_addr_list(struct ares_addr_node **head,
179 struct ares_addr_node *node);
181 int main(int argc, char **argv)
183 ares_channel channel;
184 int c, i, optmask = ARES_OPT_FLAGS, dnsclass = C_IN, type = T_A;
185 int status, nfds, count;
186 struct ares_options options;
187 struct hostent *hostent;
188 fd_set read_fds, write_fds;
189 struct timeval *tvp, tv;
190 struct ares_addr_node *srvr, *servers = NULL;
193 WORD wVersionRequested = MAKEWORD(USE_WINSOCK,USE_WINSOCK);
195 WSAStartup(wVersionRequested, &wsaData);
198 status = ares_library_init(ARES_LIB_INIT_ALL);
199 if (status != ARES_SUCCESS)
201 fprintf(stderr, "ares_library_init: %s\n", ares_strerror(status));
205 options.flags = ARES_FLAG_NOCHECKRESP;
206 options.servers = NULL;
207 options.nservers = 0;
208 while ((c = ares_getopt(argc, argv, "df:s:c:t:T:U:")) != -1)
220 for (i = 0; i < nflags; i++)
222 if (strcmp(flags[i].name, optarg) == 0)
226 options.flags |= flags[i].value;
232 /* User specified name servers override default ones. */
233 srvr = malloc(sizeof(struct ares_addr_node));
236 fprintf(stderr, "Out of memory!\n");
237 destroy_addr_list(servers);
240 append_addr_list(&servers, srvr);
241 if (ares_inet_pton(AF_INET, optarg, &srvr->addr.addr4) > 0)
242 srvr->family = AF_INET;
243 else if (ares_inet_pton(AF_INET6, optarg, &srvr->addr.addr6) > 0)
244 srvr->family = AF_INET6;
247 hostent = gethostbyname(optarg);
250 fprintf(stderr, "adig: server %s not found.\n", optarg);
251 destroy_addr_list(servers);
254 switch (hostent->h_addrtype)
257 srvr->family = AF_INET;
258 memcpy(&srvr->addr.addr4, hostent->h_addr,
259 sizeof(srvr->addr.addr4));
262 srvr->family = AF_INET6;
263 memcpy(&srvr->addr.addr6, hostent->h_addr,
264 sizeof(srvr->addr.addr6));
268 "adig: server %s unsupported address family.\n", optarg);
269 destroy_addr_list(servers);
273 /* Notice that calling ares_init_options() without servers in the
274 * options struct and with ARES_OPT_SERVERS set simultaneously in
275 * the options mask, results in an initialization with no servers.
276 * When alternative name servers have been specified these are set
277 * later calling ares_set_servers() overriding any existing server
278 * configuration. To prevent initial configuration with default
279 * servers that will be discarded later ARES_OPT_SERVERS is set.
280 * If this flag is not set here the result shall be the same but
281 * ares_init_options() will do needless work. */
282 optmask |= ARES_OPT_SERVERS;
286 /* Set the query class. */
287 for (i = 0; i < nclasses; i++)
289 if (strcasecmp(classes[i].name, optarg) == 0)
293 dnsclass = classes[i].value;
299 /* Set the query type. */
300 for (i = 0; i < ntypes; i++)
302 if (strcasecmp(types[i].name, optarg) == 0)
306 type = types[i].value;
312 /* Set the TCP port number. */
313 if (!ISDIGIT(*optarg))
315 options.tcp_port = (unsigned short)strtol(optarg, NULL, 0);
316 optmask |= ARES_OPT_TCP_PORT;
320 /* Set the UDP port number. */
321 if (!ISDIGIT(*optarg))
323 options.udp_port = (unsigned short)strtol(optarg, NULL, 0);
324 optmask |= ARES_OPT_UDP_PORT;
333 status = ares_init_options(&channel, &options, optmask);
335 if (status != ARES_SUCCESS)
337 fprintf(stderr, "ares_init_options: %s\n",
338 ares_strerror(status));
344 status = ares_set_servers(channel, servers);
345 destroy_addr_list(servers);
346 if (status != ARES_SUCCESS)
348 fprintf(stderr, "ares_init_options: %s\n",
349 ares_strerror(status));
354 /* Initiate the queries, one per command-line argument. If there is
355 * only one query to do, supply NULL as the callback argument;
356 * otherwise, supply the query name as an argument so we can
357 * distinguish responses for the user when printing them out.
360 ares_query(channel, *argv, dnsclass, type, callback, (char *) NULL);
363 for (; *argv; argv++)
364 ares_query(channel, *argv, dnsclass, type, callback, *argv);
367 /* Wait for all queries to complete. */
372 nfds = ares_fds(channel, &read_fds, &write_fds);
375 tvp = ares_timeout(channel, NULL, &tv);
376 count = select(nfds, &read_fds, &write_fds, NULL, tvp);
377 if (count < 0 && SOCKERRNO != EINVAL)
382 ares_process(channel, &read_fds, &write_fds);
385 ares_destroy(channel);
387 ares_library_cleanup();
396 static void callback(void *arg, int status, int timeouts,
397 unsigned char *abuf, int alen)
399 char *name = (char *) arg;
400 int id, qr, opcode, aa, tc, rd, ra, rcode;
401 unsigned int qdcount, ancount, nscount, arcount, i;
402 const unsigned char *aptr;
406 /* Display the query name if given. */
408 printf("Answer for query %s:\n", name);
410 /* Display an error message if there was an error, but only stop if
411 * we actually didn't get an answer buffer.
413 if (status != ARES_SUCCESS)
415 printf("%s\n", ares_strerror(status));
420 /* Won't happen, but check anyway, for safety. */
424 /* Parse the answer header. */
425 id = DNS_HEADER_QID(abuf);
426 qr = DNS_HEADER_QR(abuf);
427 opcode = DNS_HEADER_OPCODE(abuf);
428 aa = DNS_HEADER_AA(abuf);
429 tc = DNS_HEADER_TC(abuf);
430 rd = DNS_HEADER_RD(abuf);
431 ra = DNS_HEADER_RA(abuf);
432 rcode = DNS_HEADER_RCODE(abuf);
433 qdcount = DNS_HEADER_QDCOUNT(abuf);
434 ancount = DNS_HEADER_ANCOUNT(abuf);
435 nscount = DNS_HEADER_NSCOUNT(abuf);
436 arcount = DNS_HEADER_ARCOUNT(abuf);
438 /* Display the answer header. */
439 printf("id: %d\n", id);
440 printf("flags: %s%s%s%s%s\n",
446 printf("opcode: %s\n", opcodes[opcode]);
447 printf("rcode: %s\n", rcodes[rcode]);
449 /* Display the questions. */
450 printf("Questions:\n");
451 aptr = abuf + HFIXEDSZ;
452 for (i = 0; i < qdcount; i++)
454 aptr = display_question(aptr, abuf, alen);
459 /* Display the answers. */
460 printf("Answers:\n");
461 for (i = 0; i < ancount; i++)
463 aptr = display_rr(aptr, abuf, alen);
468 /* Display the NS records. */
469 printf("NS records:\n");
470 for (i = 0; i < nscount; i++)
472 aptr = display_rr(aptr, abuf, alen);
477 /* Display the additional records. */
478 printf("Additional records:\n");
479 for (i = 0; i < arcount; i++)
481 aptr = display_rr(aptr, abuf, alen);
487 static const unsigned char *display_question(const unsigned char *aptr,
488 const unsigned char *abuf,
492 int type, dnsclass, status;
495 /* Parse the question name. */
496 status = ares_expand_name(aptr, abuf, alen, &name, &len);
497 if (status != ARES_SUCCESS)
501 /* Make sure there's enough data after the name for the fixed part
504 if (aptr + QFIXEDSZ > abuf + alen)
506 ares_free_string(name);
510 /* Parse the question type and class. */
511 type = DNS_QUESTION_TYPE(aptr);
512 dnsclass = DNS_QUESTION_CLASS(aptr);
515 /* Display the question, in a format sort of similar to how we will
518 printf("\t%-15s.\t", name);
519 if (dnsclass != C_IN)
520 printf("\t%s", class_name(dnsclass));
521 printf("\t%s\n", type_name(type));
522 ares_free_string(name);
526 static const unsigned char *display_rr(const unsigned char *aptr,
527 const unsigned char *abuf, int alen)
529 const unsigned char *p;
530 int type, dnsclass, ttl, dlen, status;
534 unsigned char * as_uchar;
538 /* Parse the RR name. */
539 status = ares_expand_name(aptr, abuf, alen, &name.as_char, &len);
540 if (status != ARES_SUCCESS)
544 /* Make sure there is enough data after the RR name for the fixed
547 if (aptr + RRFIXEDSZ > abuf + alen)
549 ares_free_string(name.as_char);
553 /* Parse the fixed part of the RR, and advance to the RR data
555 type = DNS_RR_TYPE(aptr);
556 dnsclass = DNS_RR_CLASS(aptr);
557 ttl = DNS_RR_TTL(aptr);
558 dlen = DNS_RR_LEN(aptr);
560 if (aptr + dlen > abuf + alen)
562 ares_free_string(name.as_char);
566 /* Display the RR name, class, and type. */
567 printf("\t%-15s.\t%d", name.as_char, ttl);
568 if (dnsclass != C_IN)
569 printf("\t%s", class_name(dnsclass));
570 printf("\t%s", type_name(type));
571 ares_free_string(name.as_char);
573 /* Display the RR data. Don't touch aptr. */
584 /* For these types, the RR data is just a domain name. */
585 status = ares_expand_name(aptr, abuf, alen, &name.as_char, &len);
586 if (status != ARES_SUCCESS)
588 printf("\t%s.", name.as_char);
589 ares_free_string(name.as_char);
593 /* The RR data is two length-counted character strings. */
596 if (p + len + 1 > aptr + dlen)
598 status = ares_expand_string(p, abuf, alen, &name.as_uchar, &len);
599 if (status != ARES_SUCCESS)
601 printf("\t%s", name.as_char);
602 ares_free_string(name.as_char);
605 if (p + len + 1 > aptr + dlen)
607 status = ares_expand_string(p, abuf, alen, &name.as_uchar, &len);
608 if (status != ARES_SUCCESS)
610 printf("\t%s", name.as_char);
611 ares_free_string(name.as_char);
615 /* The RR data is two domain names. */
617 status = ares_expand_name(p, abuf, alen, &name.as_char, &len);
618 if (status != ARES_SUCCESS)
620 printf("\t%s.", name.as_char);
621 ares_free_string(name.as_char);
623 status = ares_expand_name(p, abuf, alen, &name.as_char, &len);
624 if (status != ARES_SUCCESS)
626 printf("\t%s.", name.as_char);
627 ares_free_string(name.as_char);
631 /* The RR data is two bytes giving a preference ordering, and
632 * then a domain name.
636 printf("\t%d", DNS__16BIT(aptr));
637 status = ares_expand_name(aptr + 2, abuf, alen, &name.as_char, &len);
638 if (status != ARES_SUCCESS)
640 printf("\t%s.", name.as_char);
641 ares_free_string(name.as_char);
645 /* The RR data is two domain names and then five four-byte
646 * numbers giving the serial number and some timeouts.
649 status = ares_expand_name(p, abuf, alen, &name.as_char, &len);
650 if (status != ARES_SUCCESS)
652 printf("\t%s.\n", name.as_char);
653 ares_free_string(name.as_char);
655 status = ares_expand_name(p, abuf, alen, &name.as_char, &len);
656 if (status != ARES_SUCCESS)
658 printf("\t\t\t\t\t\t%s.\n", name.as_char);
659 ares_free_string(name.as_char);
661 if (p + 20 > aptr + dlen)
663 printf("\t\t\t\t\t\t( %lu %lu %lu %lu %lu )",
664 (unsigned long)DNS__32BIT(p), (unsigned long)DNS__32BIT(p+4),
665 (unsigned long)DNS__32BIT(p+8), (unsigned long)DNS__32BIT(p+12),
666 (unsigned long)DNS__32BIT(p+16));
670 /* The RR data is one or more length-counted character
673 while (p < aptr + dlen)
676 if (p + len + 1 > aptr + dlen)
678 status = ares_expand_string(p, abuf, alen, &name.as_uchar, &len);
679 if (status != ARES_SUCCESS)
681 printf("\t%s", name.as_char);
682 ares_free_string(name.as_char);
688 /* The RR data is a four-byte Internet address. */
691 printf("\t%s", ares_inet_ntop(AF_INET,aptr,addr,sizeof(addr)));
695 /* The RR data is a 16-byte IPv6 address. */
698 printf("\t%s", ares_inet_ntop(AF_INET6,aptr,addr,sizeof(addr)));
702 /* Not implemented yet */
706 /* The RR data is three two-byte numbers representing the
707 * priority, weight, and port, followed by a domain name.
710 printf("\t%d", DNS__16BIT(aptr));
711 printf(" %d", DNS__16BIT(aptr + 2));
712 printf(" %d", DNS__16BIT(aptr + 4));
714 status = ares_expand_name(aptr + 6, abuf, alen, &name.as_char, &len);
715 if (status != ARES_SUCCESS)
717 printf("\t%s.", name.as_char);
718 ares_free_string(name.as_char);
723 printf("\t%d", DNS__16BIT(aptr)); /* order */
724 printf(" %d\n", DNS__16BIT(aptr + 2)); /* preference */
727 status = ares_expand_string(p, abuf, alen, &name.as_uchar, &len);
728 if (status != ARES_SUCCESS)
730 printf("\t\t\t\t\t\t%s\n", name.as_char);
731 ares_free_string(name.as_char);
734 status = ares_expand_string(p, abuf, alen, &name.as_uchar, &len);
735 if (status != ARES_SUCCESS)
737 printf("\t\t\t\t\t\t%s\n", name.as_char);
738 ares_free_string(name.as_char);
741 status = ares_expand_string(p, abuf, alen, &name.as_uchar, &len);
742 if (status != ARES_SUCCESS)
744 printf("\t\t\t\t\t\t%s\n", name.as_char);
745 ares_free_string(name.as_char);
748 status = ares_expand_string(p, abuf, alen, &name.as_uchar, &len);
749 if (status != ARES_SUCCESS)
751 printf("\t\t\t\t\t\t%s", name.as_char);
752 ares_free_string(name.as_char);
757 printf("\t[Unknown RR; cannot parse]");
765 static const char *type_name(int type)
769 for (i = 0; i < ntypes; i++)
771 if (types[i].value == type)
772 return types[i].name;
777 static const char *class_name(int dnsclass)
781 for (i = 0; i < nclasses; i++)
783 if (classes[i].value == dnsclass)
784 return classes[i].name;
789 static void usage(void)
791 fprintf(stderr, "usage: adig [-f flag] [-s server] [-c class] "
792 "[-t type] [-p port] name ...\n");
796 static void destroy_addr_list(struct ares_addr_node *head)
800 struct ares_addr_node *detached = head;
806 static void append_addr_list(struct ares_addr_node **head,
807 struct ares_addr_node *node)
809 struct ares_addr_node *last;