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"
60 #include "ares_nowarn.h"
63 # include "ares_strdup.h"
64 # define strdup(ptr) ares_strdup(ptr)
67 #ifndef HAVE_STRCASECMP
68 # include "ares_strcasecmp.h"
69 # define strcasecmp(p1,p2) ares_strcasecmp(p1,p2)
72 #ifndef HAVE_STRNCASECMP
73 # include "ares_strcasecmp.h"
74 # define strncasecmp(p1,p2,n) ares_strncasecmp(p1,p2,n)
78 #undef WIN32 /* Redefined in MingW headers */
82 # define T_SRV 33 /* Server selection */
85 # define T_NAPTR 35 /* Naming authority pointer */
88 # define T_DS 43 /* Delegation Signer (RFC4034) */
91 # define T_SSHFP 44 /* SSH Key Fingerprint (RFC4255) */
94 # define T_RRSIG 46 /* Resource Record Signature (RFC4034) */
97 # define T_NSEC 47 /* Next Secure (RFC4034) */
100 # define T_DNSKEY 48 /* DNS Public Key (RFC4034) */
108 static const struct nv flags[] = {
109 { "usevc", ARES_FLAG_USEVC },
110 { "primary", ARES_FLAG_PRIMARY },
111 { "igntc", ARES_FLAG_IGNTC },
112 { "norecurse", ARES_FLAG_NORECURSE },
113 { "stayopen", ARES_FLAG_STAYOPEN },
114 { "noaliases", ARES_FLAG_NOALIASES }
116 static const int nflags = sizeof(flags) / sizeof(flags[0]);
118 static const struct nv classes[] = {
120 { "CHAOS", C_CHAOS },
124 static const int nclasses = sizeof(classes) / sizeof(classes[0]);
126 static const struct nv types[] = {
131 { "CNAME", T_CNAME },
139 { "HINFO", T_HINFO },
140 { "MINFO", T_MINFO },
144 { "AFSDB", T_AFSDB },
149 { "NSAP_PTR", T_NSAP_PTR },
158 { "MAILB", T_MAILB },
159 { "MAILA", T_MAILA },
160 { "NAPTR", T_NAPTR },
162 { "SSHFP", T_SSHFP },
163 { "RRSIG", T_RRSIG },
165 { "DNSKEY", T_DNSKEY },
168 static const int ntypes = sizeof(types) / sizeof(types[0]);
170 static const char *opcodes[] = {
171 "QUERY", "IQUERY", "STATUS", "(reserved)", "NOTIFY",
172 "(unknown)", "(unknown)", "(unknown)", "(unknown)",
173 "UPDATEA", "UPDATED", "UPDATEDA", "UPDATEM", "UPDATEMA",
174 "ZONEINIT", "ZONEREF"
177 static const char *rcodes[] = {
178 "NOERROR", "FORMERR", "SERVFAIL", "NXDOMAIN", "NOTIMP", "REFUSED",
179 "(unknown)", "(unknown)", "(unknown)", "(unknown)", "(unknown)",
180 "(unknown)", "(unknown)", "(unknown)", "(unknown)", "NOCHANGE"
183 static void callback(void *arg, int status, int timeouts,
184 unsigned char *abuf, int alen);
185 static const unsigned char *display_question(const unsigned char *aptr,
186 const unsigned char *abuf,
188 static const unsigned char *display_rr(const unsigned char *aptr,
189 const unsigned char *abuf, int alen);
190 static const char *type_name(int type);
191 static const char *class_name(int dnsclass);
192 static void usage(void);
193 static void destroy_addr_list(struct ares_addr_node *head);
194 static void append_addr_list(struct ares_addr_node **head,
195 struct ares_addr_node *node);
197 int main(int argc, char **argv)
199 ares_channel channel;
200 int c, i, optmask = ARES_OPT_FLAGS, dnsclass = C_IN, type = T_A;
201 int status, nfds, count;
202 struct ares_options options;
203 struct hostent *hostent;
204 fd_set read_fds, write_fds;
205 struct timeval *tvp, tv;
206 struct ares_addr_node *srvr, *servers = NULL;
209 WORD wVersionRequested = MAKEWORD(USE_WINSOCK,USE_WINSOCK);
211 WSAStartup(wVersionRequested, &wsaData);
214 status = ares_library_init(ARES_LIB_INIT_ALL);
215 if (status != ARES_SUCCESS)
217 fprintf(stderr, "ares_library_init: %s\n", ares_strerror(status));
221 options.flags = ARES_FLAG_NOCHECKRESP;
222 options.servers = NULL;
223 options.nservers = 0;
224 while ((c = ares_getopt(argc, argv, "df:s:c:t:T:U:")) != -1)
236 for (i = 0; i < nflags; i++)
238 if (strcmp(flags[i].name, optarg) == 0)
242 options.flags |= flags[i].value;
248 /* User-specified name servers override default ones. */
249 srvr = malloc(sizeof(struct ares_addr_node));
252 fprintf(stderr, "Out of memory!\n");
253 destroy_addr_list(servers);
256 append_addr_list(&servers, srvr);
257 if (ares_inet_pton(AF_INET, optarg, &srvr->addr.addr4) > 0)
258 srvr->family = AF_INET;
259 else if (ares_inet_pton(AF_INET6, optarg, &srvr->addr.addr6) > 0)
260 srvr->family = AF_INET6;
263 hostent = gethostbyname(optarg);
266 fprintf(stderr, "adig: server %s not found.\n", optarg);
267 destroy_addr_list(servers);
270 switch (hostent->h_addrtype)
273 srvr->family = AF_INET;
274 memcpy(&srvr->addr.addr4, hostent->h_addr,
275 sizeof(srvr->addr.addr4));
278 srvr->family = AF_INET6;
279 memcpy(&srvr->addr.addr6, hostent->h_addr,
280 sizeof(srvr->addr.addr6));
284 "adig: server %s unsupported address family.\n", optarg);
285 destroy_addr_list(servers);
289 /* Notice that calling ares_init_options() without servers in the
290 * options struct and with ARES_OPT_SERVERS set simultaneously in
291 * the options mask, results in an initialization with no servers.
292 * When alternative name servers have been specified these are set
293 * later calling ares_set_servers() overriding any existing server
294 * configuration. To prevent initial configuration with default
295 * servers that will be discarded later, ARES_OPT_SERVERS is set.
296 * If this flag is not set here the result shall be the same but
297 * ares_init_options() will do needless work. */
298 optmask |= ARES_OPT_SERVERS;
302 /* Set the query class. */
303 for (i = 0; i < nclasses; i++)
305 if (strcasecmp(classes[i].name, optarg) == 0)
309 dnsclass = classes[i].value;
315 /* Set the query type. */
316 for (i = 0; i < ntypes; i++)
318 if (strcasecmp(types[i].name, optarg) == 0)
322 type = types[i].value;
328 /* Set the TCP port number. */
329 if (!ISDIGIT(*optarg))
331 options.tcp_port = (unsigned short)strtol(optarg, NULL, 0);
332 optmask |= ARES_OPT_TCP_PORT;
336 /* Set the UDP port number. */
337 if (!ISDIGIT(*optarg))
339 options.udp_port = (unsigned short)strtol(optarg, NULL, 0);
340 optmask |= ARES_OPT_UDP_PORT;
349 status = ares_init_options(&channel, &options, optmask);
351 if (status != ARES_SUCCESS)
353 fprintf(stderr, "ares_init_options: %s\n",
354 ares_strerror(status));
360 status = ares_set_servers(channel, servers);
361 destroy_addr_list(servers);
362 if (status != ARES_SUCCESS)
364 fprintf(stderr, "ares_init_options: %s\n",
365 ares_strerror(status));
370 /* Initiate the queries, one per command-line argument. If there is
371 * only one query to do, supply NULL as the callback argument;
372 * otherwise, supply the query name as an argument so we can
373 * distinguish responses for the user when printing them out.
376 ares_query(channel, *argv, dnsclass, type, callback, (char *) NULL);
379 for (; *argv; argv++)
380 ares_query(channel, *argv, dnsclass, type, callback, *argv);
383 /* Wait for all queries to complete. */
388 nfds = ares_fds(channel, &read_fds, &write_fds);
391 tvp = ares_timeout(channel, NULL, &tv);
392 count = select(nfds, &read_fds, &write_fds, NULL, tvp);
393 if (count < 0 && SOCKERRNO != EINVAL)
398 ares_process(channel, &read_fds, &write_fds);
401 ares_destroy(channel);
403 ares_library_cleanup();
412 static void callback(void *arg, int status, int timeouts,
413 unsigned char *abuf, int alen)
415 char *name = (char *) arg;
416 int id, qr, opcode, aa, tc, rd, ra, rcode;
417 unsigned int qdcount, ancount, nscount, arcount, i;
418 const unsigned char *aptr;
422 /* Display the query name if given. */
424 printf("Answer for query %s:\n", name);
426 /* Display an error message if there was an error, but only stop if
427 * we actually didn't get an answer buffer.
429 if (status != ARES_SUCCESS)
431 printf("%s\n", ares_strerror(status));
436 /* Won't happen, but check anyway, for safety. */
440 /* Parse the answer header. */
441 id = DNS_HEADER_QID(abuf);
442 qr = DNS_HEADER_QR(abuf);
443 opcode = DNS_HEADER_OPCODE(abuf);
444 aa = DNS_HEADER_AA(abuf);
445 tc = DNS_HEADER_TC(abuf);
446 rd = DNS_HEADER_RD(abuf);
447 ra = DNS_HEADER_RA(abuf);
448 rcode = DNS_HEADER_RCODE(abuf);
449 qdcount = DNS_HEADER_QDCOUNT(abuf);
450 ancount = DNS_HEADER_ANCOUNT(abuf);
451 nscount = DNS_HEADER_NSCOUNT(abuf);
452 arcount = DNS_HEADER_ARCOUNT(abuf);
454 /* Display the answer header. */
455 printf("id: %d\n", id);
456 printf("flags: %s%s%s%s%s\n",
462 printf("opcode: %s\n", opcodes[opcode]);
463 printf("rcode: %s\n", rcodes[rcode]);
465 /* Display the questions. */
466 printf("Questions:\n");
467 aptr = abuf + HFIXEDSZ;
468 for (i = 0; i < qdcount; i++)
470 aptr = display_question(aptr, abuf, alen);
475 /* Display the answers. */
476 printf("Answers:\n");
477 for (i = 0; i < ancount; i++)
479 aptr = display_rr(aptr, abuf, alen);
484 /* Display the NS records. */
485 printf("NS records:\n");
486 for (i = 0; i < nscount; i++)
488 aptr = display_rr(aptr, abuf, alen);
493 /* Display the additional records. */
494 printf("Additional records:\n");
495 for (i = 0; i < arcount; i++)
497 aptr = display_rr(aptr, abuf, alen);
503 static const unsigned char *display_question(const unsigned char *aptr,
504 const unsigned char *abuf,
508 int type, dnsclass, status;
511 /* Parse the question name. */
512 status = ares_expand_name(aptr, abuf, alen, &name, &len);
513 if (status != ARES_SUCCESS)
517 /* Make sure there's enough data after the name for the fixed part
520 if (aptr + QFIXEDSZ > abuf + alen)
522 ares_free_string(name);
526 /* Parse the question type and class. */
527 type = DNS_QUESTION_TYPE(aptr);
528 dnsclass = DNS_QUESTION_CLASS(aptr);
531 /* Display the question, in a format sort of similar to how we will
534 printf("\t%-15s.\t", name);
535 if (dnsclass != C_IN)
536 printf("\t%s", class_name(dnsclass));
537 printf("\t%s\n", type_name(type));
538 ares_free_string(name);
542 static const unsigned char *display_rr(const unsigned char *aptr,
543 const unsigned char *abuf, int alen)
545 const unsigned char *p;
546 int type, dnsclass, ttl, dlen, status;
550 unsigned char * as_uchar;
554 /* Parse the RR name. */
555 status = ares_expand_name(aptr, abuf, alen, &name.as_char, &len);
556 if (status != ARES_SUCCESS)
560 /* Make sure there is enough data after the RR name for the fixed
563 if (aptr + RRFIXEDSZ > abuf + alen)
565 ares_free_string(name.as_char);
569 /* Parse the fixed part of the RR, and advance to the RR data
571 type = DNS_RR_TYPE(aptr);
572 dnsclass = DNS_RR_CLASS(aptr);
573 ttl = DNS_RR_TTL(aptr);
574 dlen = DNS_RR_LEN(aptr);
576 if (aptr + dlen > abuf + alen)
578 ares_free_string(name.as_char);
582 /* Display the RR name, class, and type. */
583 printf("\t%-15s.\t%d", name.as_char, ttl);
584 if (dnsclass != C_IN)
585 printf("\t%s", class_name(dnsclass));
586 printf("\t%s", type_name(type));
587 ares_free_string(name.as_char);
589 /* Display the RR data. Don't touch aptr. */
600 /* For these types, the RR data is just a domain name. */
601 status = ares_expand_name(aptr, abuf, alen, &name.as_char, &len);
602 if (status != ARES_SUCCESS)
604 printf("\t%s.", name.as_char);
605 ares_free_string(name.as_char);
609 /* The RR data is two length-counted character strings. */
612 if (p + len + 1 > aptr + dlen)
614 status = ares_expand_string(p, abuf, alen, &name.as_uchar, &len);
615 if (status != ARES_SUCCESS)
617 printf("\t%s", name.as_char);
618 ares_free_string(name.as_char);
621 if (p + len + 1 > aptr + dlen)
623 status = ares_expand_string(p, abuf, alen, &name.as_uchar, &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 domain names. */
633 status = ares_expand_name(p, abuf, alen, &name.as_char, &len);
634 if (status != ARES_SUCCESS)
636 printf("\t%s.", name.as_char);
637 ares_free_string(name.as_char);
639 status = ares_expand_name(p, abuf, alen, &name.as_char, &len);
640 if (status != ARES_SUCCESS)
642 printf("\t%s.", name.as_char);
643 ares_free_string(name.as_char);
647 /* The RR data is two bytes giving a preference ordering, and
648 * then a domain name.
652 printf("\t%d", DNS__16BIT(aptr));
653 status = ares_expand_name(aptr + 2, abuf, alen, &name.as_char, &len);
654 if (status != ARES_SUCCESS)
656 printf("\t%s.", name.as_char);
657 ares_free_string(name.as_char);
661 /* The RR data is two domain names and then five four-byte
662 * numbers giving the serial number and some timeouts.
665 status = ares_expand_name(p, abuf, alen, &name.as_char, &len);
666 if (status != ARES_SUCCESS)
668 printf("\t%s.\n", name.as_char);
669 ares_free_string(name.as_char);
671 status = ares_expand_name(p, abuf, alen, &name.as_char, &len);
672 if (status != ARES_SUCCESS)
674 printf("\t\t\t\t\t\t%s.\n", name.as_char);
675 ares_free_string(name.as_char);
677 if (p + 20 > aptr + dlen)
679 printf("\t\t\t\t\t\t( %lu %lu %lu %lu %lu )",
680 (unsigned long)DNS__32BIT(p), (unsigned long)DNS__32BIT(p+4),
681 (unsigned long)DNS__32BIT(p+8), (unsigned long)DNS__32BIT(p+12),
682 (unsigned long)DNS__32BIT(p+16));
686 /* The RR data is one or more length-counted character
689 while (p < aptr + dlen)
692 if (p + len + 1 > aptr + dlen)
694 status = ares_expand_string(p, abuf, alen, &name.as_uchar, &len);
695 if (status != ARES_SUCCESS)
697 printf("\t%s", name.as_char);
698 ares_free_string(name.as_char);
704 /* The RR data is a four-byte Internet address. */
707 printf("\t%s", ares_inet_ntop(AF_INET,aptr,addr,sizeof(addr)));
711 /* The RR data is a 16-byte IPv6 address. */
714 printf("\t%s", ares_inet_ntop(AF_INET6,aptr,addr,sizeof(addr)));
718 /* Not implemented yet */
722 /* The RR data is three two-byte numbers representing the
723 * priority, weight, and port, followed by a domain name.
726 printf("\t%d", DNS__16BIT(aptr));
727 printf(" %d", DNS__16BIT(aptr + 2));
728 printf(" %d", DNS__16BIT(aptr + 4));
730 status = ares_expand_name(aptr + 6, abuf, alen, &name.as_char, &len);
731 if (status != ARES_SUCCESS)
733 printf("\t%s.", name.as_char);
734 ares_free_string(name.as_char);
739 printf("\t%d", DNS__16BIT(aptr)); /* order */
740 printf(" %d\n", DNS__16BIT(aptr + 2)); /* preference */
743 status = ares_expand_string(p, abuf, alen, &name.as_uchar, &len);
744 if (status != ARES_SUCCESS)
746 printf("\t\t\t\t\t\t%s\n", name.as_char);
747 ares_free_string(name.as_char);
750 status = ares_expand_string(p, abuf, alen, &name.as_uchar, &len);
751 if (status != ARES_SUCCESS)
753 printf("\t\t\t\t\t\t%s\n", name.as_char);
754 ares_free_string(name.as_char);
757 status = ares_expand_string(p, abuf, alen, &name.as_uchar, &len);
758 if (status != ARES_SUCCESS)
760 printf("\t\t\t\t\t\t%s\n", name.as_char);
761 ares_free_string(name.as_char);
764 status = ares_expand_name(p, abuf, alen, &name.as_char, &len);
765 if (status != ARES_SUCCESS)
767 printf("\t\t\t\t\t\t%s", name.as_char);
768 ares_free_string(name.as_char);
776 printf("\t[RR type parsing unavailable]");
780 printf("\t[Unknown RR; cannot parse]");
788 static const char *type_name(int type)
792 for (i = 0; i < ntypes; i++)
794 if (types[i].value == type)
795 return types[i].name;
800 static const char *class_name(int dnsclass)
804 for (i = 0; i < nclasses; i++)
806 if (classes[i].value == dnsclass)
807 return classes[i].name;
812 static void usage(void)
814 fprintf(stderr, "usage: adig [-f flag] [-s server] [-c class] "
815 "[-t type] [-p port] name ...\n");
819 static void destroy_addr_list(struct ares_addr_node *head)
823 struct ares_addr_node *detached = head;
829 static void append_addr_list(struct ares_addr_node **head,
830 struct ares_addr_node *node)
832 struct ares_addr_node *last;