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 */
81 # define T_SRV 33 /* Server selection */
84 # define T_NAPTR 35 /* Naming authority pointer */
87 # define T_DS 43 /* Delegation Signer (RFC4034) */
90 # define T_SSHFP 44 /* SSH Key Fingerprint (RFC4255) */
93 # define T_RRSIG 46 /* Resource Record Signature (RFC4034) */
96 # define T_NSEC 47 /* Next Secure (RFC4034) */
99 # define T_DNSKEY 48 /* DNS Public Key (RFC4034) */
107 static const struct nv flags[] = {
108 { "usevc", ARES_FLAG_USEVC },
109 { "primary", ARES_FLAG_PRIMARY },
110 { "igntc", ARES_FLAG_IGNTC },
111 { "norecurse", ARES_FLAG_NORECURSE },
112 { "stayopen", ARES_FLAG_STAYOPEN },
113 { "noaliases", ARES_FLAG_NOALIASES }
115 static const int nflags = sizeof(flags) / sizeof(flags[0]);
117 static const struct nv classes[] = {
119 { "CHAOS", C_CHAOS },
123 static const int nclasses = sizeof(classes) / sizeof(classes[0]);
125 static const struct nv types[] = {
130 { "CNAME", T_CNAME },
138 { "HINFO", T_HINFO },
139 { "MINFO", T_MINFO },
143 { "AFSDB", T_AFSDB },
148 { "NSAP_PTR", T_NSAP_PTR },
157 { "MAILB", T_MAILB },
158 { "MAILA", T_MAILA },
159 { "NAPTR", T_NAPTR },
161 { "SSHFP", T_SSHFP },
162 { "RRSIG", T_RRSIG },
164 { "DNSKEY", T_DNSKEY },
167 static const int ntypes = sizeof(types) / sizeof(types[0]);
169 static const char *opcodes[] = {
170 "QUERY", "IQUERY", "STATUS", "(reserved)", "NOTIFY",
171 "(unknown)", "(unknown)", "(unknown)", "(unknown)",
172 "UPDATEA", "UPDATED", "UPDATEDA", "UPDATEM", "UPDATEMA",
173 "ZONEINIT", "ZONEREF"
176 static const char *rcodes[] = {
177 "NOERROR", "FORMERR", "SERVFAIL", "NXDOMAIN", "NOTIMP", "REFUSED",
178 "(unknown)", "(unknown)", "(unknown)", "(unknown)", "(unknown)",
179 "(unknown)", "(unknown)", "(unknown)", "(unknown)", "NOCHANGE"
182 static void callback(void *arg, int status, int timeouts,
183 unsigned char *abuf, int alen);
184 static const unsigned char *display_question(const unsigned char *aptr,
185 const unsigned char *abuf,
187 static const unsigned char *display_rr(const unsigned char *aptr,
188 const unsigned char *abuf, int alen);
189 static const char *type_name(int type);
190 static const char *class_name(int dnsclass);
191 static void usage(void);
192 static void destroy_addr_list(struct ares_addr_node *head);
193 static void append_addr_list(struct ares_addr_node **head,
194 struct ares_addr_node *node);
196 int main(int argc, char **argv)
198 ares_channel channel;
199 int c, i, optmask = ARES_OPT_FLAGS, dnsclass = C_IN, type = T_A;
200 int status, nfds, count;
201 struct ares_options options;
202 struct hostent *hostent;
203 fd_set read_fds, write_fds;
204 struct timeval *tvp, tv;
205 struct ares_addr_node *srvr, *servers = NULL;
208 WORD wVersionRequested = MAKEWORD(USE_WINSOCK,USE_WINSOCK);
210 WSAStartup(wVersionRequested, &wsaData);
213 status = ares_library_init(ARES_LIB_INIT_ALL);
214 if (status != ARES_SUCCESS)
216 fprintf(stderr, "ares_library_init: %s\n", ares_strerror(status));
220 options.flags = ARES_FLAG_NOCHECKRESP;
221 options.servers = NULL;
222 options.nservers = 0;
223 while ((c = ares_getopt(argc, argv, "df:s:c:t:T:U:")) != -1)
235 for (i = 0; i < nflags; i++)
237 if (strcmp(flags[i].name, optarg) == 0)
241 options.flags |= flags[i].value;
247 /* User-specified name servers override default ones. */
248 srvr = malloc(sizeof(struct ares_addr_node));
251 fprintf(stderr, "Out of memory!\n");
252 destroy_addr_list(servers);
255 append_addr_list(&servers, srvr);
256 if (ares_inet_pton(AF_INET, optarg, &srvr->addr.addr4) > 0)
257 srvr->family = AF_INET;
258 else if (ares_inet_pton(AF_INET6, optarg, &srvr->addr.addr6) > 0)
259 srvr->family = AF_INET6;
262 hostent = gethostbyname(optarg);
265 fprintf(stderr, "adig: server %s not found.\n", optarg);
266 destroy_addr_list(servers);
269 switch (hostent->h_addrtype)
272 srvr->family = AF_INET;
273 memcpy(&srvr->addr.addr4, hostent->h_addr,
274 sizeof(srvr->addr.addr4));
277 srvr->family = AF_INET6;
278 memcpy(&srvr->addr.addr6, hostent->h_addr,
279 sizeof(srvr->addr.addr6));
283 "adig: server %s unsupported address family.\n", optarg);
284 destroy_addr_list(servers);
288 /* Notice that calling ares_init_options() without servers in the
289 * options struct and with ARES_OPT_SERVERS set simultaneously in
290 * the options mask, results in an initialization with no servers.
291 * When alternative name servers have been specified these are set
292 * later calling ares_set_servers() overriding any existing server
293 * configuration. To prevent initial configuration with default
294 * servers that will be discarded later, ARES_OPT_SERVERS is set.
295 * If this flag is not set here the result shall be the same but
296 * ares_init_options() will do needless work. */
297 optmask |= ARES_OPT_SERVERS;
301 /* Set the query class. */
302 for (i = 0; i < nclasses; i++)
304 if (strcasecmp(classes[i].name, optarg) == 0)
308 dnsclass = classes[i].value;
314 /* Set the query type. */
315 for (i = 0; i < ntypes; i++)
317 if (strcasecmp(types[i].name, optarg) == 0)
321 type = types[i].value;
327 /* Set the TCP port number. */
328 if (!ISDIGIT(*optarg))
330 options.tcp_port = (unsigned short)strtol(optarg, NULL, 0);
331 optmask |= ARES_OPT_TCP_PORT;
335 /* Set the UDP port number. */
336 if (!ISDIGIT(*optarg))
338 options.udp_port = (unsigned short)strtol(optarg, NULL, 0);
339 optmask |= ARES_OPT_UDP_PORT;
348 status = ares_init_options(&channel, &options, optmask);
350 if (status != ARES_SUCCESS)
352 fprintf(stderr, "ares_init_options: %s\n",
353 ares_strerror(status));
359 status = ares_set_servers(channel, servers);
360 destroy_addr_list(servers);
361 if (status != ARES_SUCCESS)
363 fprintf(stderr, "ares_init_options: %s\n",
364 ares_strerror(status));
369 /* Initiate the queries, one per command-line argument. If there is
370 * only one query to do, supply NULL as the callback argument;
371 * otherwise, supply the query name as an argument so we can
372 * distinguish responses for the user when printing them out.
375 ares_query(channel, *argv, dnsclass, type, callback, (char *) NULL);
378 for (; *argv; argv++)
379 ares_query(channel, *argv, dnsclass, type, callback, *argv);
382 /* Wait for all queries to complete. */
387 nfds = ares_fds(channel, &read_fds, &write_fds);
390 tvp = ares_timeout(channel, NULL, &tv);
391 count = select(nfds, &read_fds, &write_fds, NULL, tvp);
392 if (count < 0 && SOCKERRNO != EINVAL)
397 ares_process(channel, &read_fds, &write_fds);
400 ares_destroy(channel);
402 ares_library_cleanup();
411 static void callback(void *arg, int status, int timeouts,
412 unsigned char *abuf, int alen)
414 char *name = (char *) arg;
415 int id, qr, opcode, aa, tc, rd, ra, rcode;
416 unsigned int qdcount, ancount, nscount, arcount, i;
417 const unsigned char *aptr;
421 /* Display the query name if given. */
423 printf("Answer for query %s:\n", name);
425 /* Display an error message if there was an error, but only stop if
426 * we actually didn't get an answer buffer.
428 if (status != ARES_SUCCESS)
430 printf("%s\n", ares_strerror(status));
435 /* Won't happen, but check anyway, for safety. */
439 /* Parse the answer header. */
440 id = DNS_HEADER_QID(abuf);
441 qr = DNS_HEADER_QR(abuf);
442 opcode = DNS_HEADER_OPCODE(abuf);
443 aa = DNS_HEADER_AA(abuf);
444 tc = DNS_HEADER_TC(abuf);
445 rd = DNS_HEADER_RD(abuf);
446 ra = DNS_HEADER_RA(abuf);
447 rcode = DNS_HEADER_RCODE(abuf);
448 qdcount = DNS_HEADER_QDCOUNT(abuf);
449 ancount = DNS_HEADER_ANCOUNT(abuf);
450 nscount = DNS_HEADER_NSCOUNT(abuf);
451 arcount = DNS_HEADER_ARCOUNT(abuf);
453 /* Display the answer header. */
454 printf("id: %d\n", id);
455 printf("flags: %s%s%s%s%s\n",
461 printf("opcode: %s\n", opcodes[opcode]);
462 printf("rcode: %s\n", rcodes[rcode]);
464 /* Display the questions. */
465 printf("Questions:\n");
466 aptr = abuf + HFIXEDSZ;
467 for (i = 0; i < qdcount; i++)
469 aptr = display_question(aptr, abuf, alen);
474 /* Display the answers. */
475 printf("Answers:\n");
476 for (i = 0; i < ancount; i++)
478 aptr = display_rr(aptr, abuf, alen);
483 /* Display the NS records. */
484 printf("NS records:\n");
485 for (i = 0; i < nscount; i++)
487 aptr = display_rr(aptr, abuf, alen);
492 /* Display the additional records. */
493 printf("Additional records:\n");
494 for (i = 0; i < arcount; i++)
496 aptr = display_rr(aptr, abuf, alen);
502 static const unsigned char *display_question(const unsigned char *aptr,
503 const unsigned char *abuf,
507 int type, dnsclass, status;
510 /* Parse the question name. */
511 status = ares_expand_name(aptr, abuf, alen, &name, &len);
512 if (status != ARES_SUCCESS)
516 /* Make sure there's enough data after the name for the fixed part
519 if (aptr + QFIXEDSZ > abuf + alen)
521 ares_free_string(name);
525 /* Parse the question type and class. */
526 type = DNS_QUESTION_TYPE(aptr);
527 dnsclass = DNS_QUESTION_CLASS(aptr);
530 /* Display the question, in a format sort of similar to how we will
533 printf("\t%-15s.\t", name);
534 if (dnsclass != C_IN)
535 printf("\t%s", class_name(dnsclass));
536 printf("\t%s\n", type_name(type));
537 ares_free_string(name);
541 static const unsigned char *display_rr(const unsigned char *aptr,
542 const unsigned char *abuf, int alen)
544 const unsigned char *p;
545 int type, dnsclass, ttl, dlen, status;
549 unsigned char * as_uchar;
553 /* Parse the RR name. */
554 status = ares_expand_name(aptr, abuf, alen, &name.as_char, &len);
555 if (status != ARES_SUCCESS)
559 /* Make sure there is enough data after the RR name for the fixed
562 if (aptr + RRFIXEDSZ > abuf + alen)
564 ares_free_string(name.as_char);
568 /* Parse the fixed part of the RR, and advance to the RR data
570 type = DNS_RR_TYPE(aptr);
571 dnsclass = DNS_RR_CLASS(aptr);
572 ttl = DNS_RR_TTL(aptr);
573 dlen = DNS_RR_LEN(aptr);
575 if (aptr + dlen > abuf + alen)
577 ares_free_string(name.as_char);
581 /* Display the RR name, class, and type. */
582 printf("\t%-15s.\t%d", name.as_char, ttl);
583 if (dnsclass != C_IN)
584 printf("\t%s", class_name(dnsclass));
585 printf("\t%s", type_name(type));
586 ares_free_string(name.as_char);
588 /* Display the RR data. Don't touch aptr. */
599 /* For these types, the RR data is just a domain name. */
600 status = ares_expand_name(aptr, abuf, alen, &name.as_char, &len);
601 if (status != ARES_SUCCESS)
603 printf("\t%s.", name.as_char);
604 ares_free_string(name.as_char);
608 /* The RR data is two length-counted character strings. */
611 if (p + len + 1 > aptr + dlen)
613 status = ares_expand_string(p, abuf, alen, &name.as_uchar, &len);
614 if (status != ARES_SUCCESS)
616 printf("\t%s", name.as_char);
617 ares_free_string(name.as_char);
620 if (p + len + 1 > aptr + dlen)
622 status = ares_expand_string(p, abuf, alen, &name.as_uchar, &len);
623 if (status != ARES_SUCCESS)
625 printf("\t%s", name.as_char);
626 ares_free_string(name.as_char);
630 /* The RR data is two domain names. */
632 status = ares_expand_name(p, abuf, alen, &name.as_char, &len);
633 if (status != ARES_SUCCESS)
635 printf("\t%s.", name.as_char);
636 ares_free_string(name.as_char);
638 status = ares_expand_name(p, abuf, alen, &name.as_char, &len);
639 if (status != ARES_SUCCESS)
641 printf("\t%s.", name.as_char);
642 ares_free_string(name.as_char);
646 /* The RR data is two bytes giving a preference ordering, and
647 * then a domain name.
651 printf("\t%d", DNS__16BIT(aptr));
652 status = ares_expand_name(aptr + 2, abuf, alen, &name.as_char, &len);
653 if (status != ARES_SUCCESS)
655 printf("\t%s.", name.as_char);
656 ares_free_string(name.as_char);
660 /* The RR data is two domain names and then five four-byte
661 * numbers giving the serial number and some timeouts.
664 status = ares_expand_name(p, abuf, alen, &name.as_char, &len);
665 if (status != ARES_SUCCESS)
667 printf("\t%s.\n", name.as_char);
668 ares_free_string(name.as_char);
670 status = ares_expand_name(p, abuf, alen, &name.as_char, &len);
671 if (status != ARES_SUCCESS)
673 printf("\t\t\t\t\t\t%s.\n", name.as_char);
674 ares_free_string(name.as_char);
676 if (p + 20 > aptr + dlen)
678 printf("\t\t\t\t\t\t( %lu %lu %lu %lu %lu )",
679 (unsigned long)DNS__32BIT(p), (unsigned long)DNS__32BIT(p+4),
680 (unsigned long)DNS__32BIT(p+8), (unsigned long)DNS__32BIT(p+12),
681 (unsigned long)DNS__32BIT(p+16));
685 /* The RR data is one or more length-counted character
688 while (p < aptr + dlen)
691 if (p + len + 1 > aptr + dlen)
693 status = ares_expand_string(p, abuf, alen, &name.as_uchar, &len);
694 if (status != ARES_SUCCESS)
696 printf("\t%s", name.as_char);
697 ares_free_string(name.as_char);
703 /* The RR data is a four-byte Internet address. */
706 printf("\t%s", ares_inet_ntop(AF_INET,aptr,addr,sizeof(addr)));
710 /* The RR data is a 16-byte IPv6 address. */
713 printf("\t%s", ares_inet_ntop(AF_INET6,aptr,addr,sizeof(addr)));
717 /* Not implemented yet */
721 /* The RR data is three two-byte numbers representing the
722 * priority, weight, and port, followed by a domain name.
725 printf("\t%d", DNS__16BIT(aptr));
726 printf(" %d", DNS__16BIT(aptr + 2));
727 printf(" %d", DNS__16BIT(aptr + 4));
729 status = ares_expand_name(aptr + 6, abuf, alen, &name.as_char, &len);
730 if (status != ARES_SUCCESS)
732 printf("\t%s.", name.as_char);
733 ares_free_string(name.as_char);
738 printf("\t%d", DNS__16BIT(aptr)); /* order */
739 printf(" %d\n", DNS__16BIT(aptr + 2)); /* preference */
742 status = ares_expand_string(p, abuf, alen, &name.as_uchar, &len);
743 if (status != ARES_SUCCESS)
745 printf("\t\t\t\t\t\t%s\n", name.as_char);
746 ares_free_string(name.as_char);
749 status = ares_expand_string(p, abuf, alen, &name.as_uchar, &len);
750 if (status != ARES_SUCCESS)
752 printf("\t\t\t\t\t\t%s\n", name.as_char);
753 ares_free_string(name.as_char);
756 status = ares_expand_string(p, abuf, alen, &name.as_uchar, &len);
757 if (status != ARES_SUCCESS)
759 printf("\t\t\t\t\t\t%s\n", name.as_char);
760 ares_free_string(name.as_char);
763 status = ares_expand_name(p, abuf, alen, &name.as_char, &len);
764 if (status != ARES_SUCCESS)
766 printf("\t\t\t\t\t\t%s", name.as_char);
767 ares_free_string(name.as_char);
775 printf("\t[RR type parsing unavailable]");
779 printf("\t[Unknown RR; cannot parse]");
787 static const char *type_name(int type)
791 for (i = 0; i < ntypes; i++)
793 if (types[i].value == type)
794 return types[i].name;
799 static const char *class_name(int dnsclass)
803 for (i = 0; i < nclasses; i++)
805 if (classes[i].value == dnsclass)
806 return classes[i].name;
811 static void usage(void)
813 fprintf(stderr, "usage: adig [-f flag] [-s server] [-c class] "
814 "[-t type] [-p port] name ...\n");
818 static void destroy_addr_list(struct ares_addr_node *head)
822 struct ares_addr_node *detached = head;
828 static void append_addr_list(struct ares_addr_node **head,
829 struct ares_addr_node *node)
831 struct ares_addr_node *last;