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.
19 #include <sys/types.h>
21 #if defined(WIN32) && !defined(WATT32)
24 #ifdef HAVE_SYS_TIME_H
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <arpa/nameser.h>
48 #include "inet_ntop.h"
51 #undef WIN32 /* Redefined in MingW headers */
54 /* Mac OS X portability check */
56 #define T_SRV 33 /* server selection */
69 static const struct nv flags[] = {
70 { "usevc", ARES_FLAG_USEVC },
71 { "primary", ARES_FLAG_PRIMARY },
72 { "igntc", ARES_FLAG_IGNTC },
73 { "norecurse", ARES_FLAG_NORECURSE },
74 { "stayopen", ARES_FLAG_STAYOPEN },
75 { "noaliases", ARES_FLAG_NOALIASES }
77 static const int nflags = sizeof(flags) / sizeof(flags[0]);
79 static const struct nv classes[] = {
85 static const int nclasses = sizeof(classes) / sizeof(classes[0]);
87 static const struct nv types[] = {
100 { "HINFO", T_HINFO },
101 { "MINFO", T_MINFO },
105 { "AFSDB", T_AFSDB },
110 { "NSAP_PTR", T_NSAP_PTR },
119 { "MAILB", T_MAILB },
120 { "MAILA", T_MAILA },
123 static const int ntypes = sizeof(types) / sizeof(types[0]);
125 static const char *opcodes[] = {
126 "QUERY", "IQUERY", "STATUS", "(reserved)", "NOTIFY",
127 "(unknown)", "(unknown)", "(unknown)", "(unknown)",
128 "UPDATEA", "UPDATED", "UPDATEDA", "UPDATEM", "UPDATEMA",
129 "ZONEINIT", "ZONEREF"
132 static const char *rcodes[] = {
133 "NOERROR", "FORMERR", "SERVFAIL", "NXDOMAIN", "NOTIMP", "REFUSED",
134 "(unknown)", "(unknown)", "(unknown)", "(unknown)", "(unknown)",
135 "(unknown)", "(unknown)", "(unknown)", "(unknown)", "NOCHANGE"
138 static void callback(void *arg, int status, unsigned char *abuf, int alen);
139 static const unsigned char *display_question(const unsigned char *aptr,
140 const unsigned char *abuf,
142 static const unsigned char *display_rr(const unsigned char *aptr,
143 const unsigned char *abuf, int alen);
144 static const char *type_name(int type);
145 static const char *class_name(int dnsclass);
146 static void usage(void);
148 int main(int argc, char **argv)
150 ares_channel channel;
151 int c, i, optmask = ARES_OPT_FLAGS, dnsclass = C_IN, type = T_A;
152 int status, nfds, count;
153 struct ares_options options;
154 struct hostent *hostent;
155 fd_set read_fds, write_fds;
156 struct timeval *tvp, tv;
159 WORD wVersionRequested = MAKEWORD(USE_WINSOCK,USE_WINSOCK);
161 WSAStartup(wVersionRequested, &wsaData);
164 options.flags = ARES_FLAG_NOCHECKRESP;
165 options.servers = NULL;
166 options.nservers = 0;
167 while ((c = getopt(argc, argv, "df:s:c:t:T:U:")) != -1)
179 for (i = 0; i < nflags; i++)
181 if (strcmp(flags[i].name, optarg) == 0)
186 options.flags |= flags[i].value;
190 /* Add a server, and specify servers in the option mask. */
191 hostent = gethostbyname(optarg);
192 if (!hostent || hostent->h_addrtype != AF_INET)
194 fprintf(stderr, "adig: server %s not found.\n", optarg);
197 options.servers = realloc(options.servers, (options.nservers + 1)
198 * sizeof(struct in_addr));
199 if (!options.servers)
201 fprintf(stderr, "Out of memory!\n");
204 memcpy(&options.servers[options.nservers], hostent->h_addr,
205 sizeof(struct in_addr));
207 optmask |= ARES_OPT_SERVERS;
211 /* Set the query class. */
212 for (i = 0; i < nclasses; i++)
214 if (strcasecmp(classes[i].name, optarg) == 0)
219 dnsclass = classes[i].value;
223 /* Set the query type. */
224 for (i = 0; i < ntypes; i++)
226 if (strcasecmp(types[i].name, optarg) == 0)
231 type = types[i].value;
235 /* Set the TCP port number. */
236 if (!ISDIGIT(*optarg))
238 options.tcp_port = (unsigned short)strtol(optarg, NULL, 0);
239 optmask |= ARES_OPT_TCP_PORT;
243 /* Set the UDP port number. */
244 if (!ISDIGIT(*optarg))
246 options.udp_port = (unsigned short)strtol(optarg, NULL, 0);
247 optmask |= ARES_OPT_UDP_PORT;
256 status = ares_init_options(&channel, &options, optmask);
258 if (status != ARES_SUCCESS)
260 fprintf(stderr, "ares_init_options: %s\n",
261 ares_strerror(status));
265 /* Initiate the queries, one per command-line argument. If there is
266 * only one query to do, supply NULL as the callback argument;
267 * otherwise, supply the query name as an argument so we can
268 * distinguish responses for the user when printing them out.
271 ares_query(channel, *argv, dnsclass, type, callback, (char *) NULL);
274 for (; *argv; argv++)
275 ares_query(channel, *argv, dnsclass, type, callback, *argv);
278 /* Wait for all queries to complete. */
283 nfds = ares_fds(channel, &read_fds, &write_fds);
286 tvp = ares_timeout(channel, NULL, &tv);
287 count = select(nfds, &read_fds, &write_fds, NULL, tvp);
288 if (count < 0 && SOCKERRNO != EINVAL)
293 ares_process(channel, &read_fds, &write_fds);
296 ares_destroy(channel);
300 static void callback(void *arg, int status, unsigned char *abuf, int alen)
302 char *name = (char *) arg;
303 int id, qr, opcode, aa, tc, rd, ra, rcode;
304 unsigned int qdcount, ancount, nscount, arcount, i;
305 const unsigned char *aptr;
307 /* Display the query name if given. */
309 printf("Answer for query %s:\n", name);
311 /* Display an error message if there was an error, but only stop if
312 * we actually didn't get an answer buffer.
314 if (status != ARES_SUCCESS)
316 printf("%s\n", ares_strerror(status));
321 /* Won't happen, but check anyway, for safety. */
325 /* Parse the answer header. */
326 id = DNS_HEADER_QID(abuf);
327 qr = DNS_HEADER_QR(abuf);
328 opcode = DNS_HEADER_OPCODE(abuf);
329 aa = DNS_HEADER_AA(abuf);
330 tc = DNS_HEADER_TC(abuf);
331 rd = DNS_HEADER_RD(abuf);
332 ra = DNS_HEADER_RA(abuf);
333 rcode = DNS_HEADER_RCODE(abuf);
334 qdcount = DNS_HEADER_QDCOUNT(abuf);
335 ancount = DNS_HEADER_ANCOUNT(abuf);
336 nscount = DNS_HEADER_NSCOUNT(abuf);
337 arcount = DNS_HEADER_ARCOUNT(abuf);
339 /* Display the answer header. */
340 printf("id: %d\n", id);
341 printf("flags: %s%s%s%s%s\n",
347 printf("opcode: %s\n", opcodes[opcode]);
348 printf("rcode: %s\n", rcodes[rcode]);
350 /* Display the questions. */
351 printf("Questions:\n");
352 aptr = abuf + HFIXEDSZ;
353 for (i = 0; i < qdcount; i++)
355 aptr = display_question(aptr, abuf, alen);
360 /* Display the answers. */
361 printf("Answers:\n");
362 for (i = 0; i < ancount; i++)
364 aptr = display_rr(aptr, abuf, alen);
369 /* Display the NS records. */
370 printf("NS records:\n");
371 for (i = 0; i < nscount; i++)
373 aptr = display_rr(aptr, abuf, alen);
378 /* Display the additional records. */
379 printf("Additional records:\n");
380 for (i = 0; i < arcount; i++)
382 aptr = display_rr(aptr, abuf, alen);
388 static const unsigned char *display_question(const unsigned char *aptr,
389 const unsigned char *abuf,
393 int type, dnsclass, status;
396 /* Parse the question name. */
397 status = ares_expand_name(aptr, abuf, alen, &name, &len);
398 if (status != ARES_SUCCESS)
402 /* Make sure there's enough data after the name for the fixed part
405 if (aptr + QFIXEDSZ > abuf + alen)
407 ares_free_string(name);
411 /* Parse the question type and class. */
412 type = DNS_QUESTION_TYPE(aptr);
413 dnsclass = DNS_QUESTION_CLASS(aptr);
416 /* Display the question, in a format sort of similar to how we will
419 printf("\t%-15s.\t", name);
420 if (dnsclass != C_IN)
421 printf("\t%s", class_name(dnsclass));
422 printf("\t%s\n", type_name(type));
423 ares_free_string(name);
427 static const unsigned char *display_rr(const unsigned char *aptr,
428 const unsigned char *abuf, int alen)
430 const unsigned char *p;
432 int type, dnsclass, ttl, dlen, status;
436 /* Parse the RR name. */
437 status = ares_expand_name(aptr, abuf, alen, &name, &len);
438 if (status != ARES_SUCCESS)
442 /* Make sure there is enough data after the RR name for the fixed
445 if (aptr + RRFIXEDSZ > abuf + alen)
447 ares_free_string(name);
451 /* Parse the fixed part of the RR, and advance to the RR data
453 type = DNS_RR_TYPE(aptr);
454 dnsclass = DNS_RR_CLASS(aptr);
455 ttl = DNS_RR_TTL(aptr);
456 dlen = DNS_RR_LEN(aptr);
458 if (aptr + dlen > abuf + alen)
460 ares_free_string(name);
464 /* Display the RR name, class, and type. */
465 printf("\t%-15s.\t%d", name, ttl);
466 if (dnsclass != C_IN)
467 printf("\t%s", class_name(dnsclass));
468 printf("\t%s", type_name(type));
469 ares_free_string(name);
471 /* Display the RR data. Don't touch aptr. */
482 /* For these types, the RR data is just a domain name. */
483 status = ares_expand_name(aptr, abuf, alen, &name, &len);
484 if (status != ARES_SUCCESS)
486 printf("\t%s.", name);
487 ares_free_string(name);
491 /* The RR data is two length-counted character strings. */
494 if (p + len + 1 > aptr + dlen)
496 printf("\t%.*s", (int)len, p + 1);
499 if (p + len + 1 > aptr + dlen)
501 printf("\t%.*s", (int)len, p + 1);
505 /* The RR data is two domain names. */
507 status = ares_expand_name(p, abuf, alen, &name, &len);
508 if (status != ARES_SUCCESS)
510 printf("\t%s.", name);
511 ares_free_string(name);
513 status = ares_expand_name(p, abuf, alen, &name, &len);
514 if (status != ARES_SUCCESS)
516 printf("\t%s.", name);
517 ares_free_string(name);
521 /* The RR data is two bytes giving a preference ordering, and
522 * then a domain name.
526 printf("\t%d", DNS__16BIT(aptr));
527 status = ares_expand_name(aptr + 2, abuf, alen, &name, &len);
528 if (status != ARES_SUCCESS)
530 printf("\t%s.", name);
531 ares_free_string(name);
535 /* The RR data is two domain names and then five four-byte
536 * numbers giving the serial number and some timeouts.
539 status = ares_expand_name(p, abuf, alen, &name, &len);
540 if (status != ARES_SUCCESS)
542 printf("\t%s.\n", name);
543 ares_free_string(name);
545 status = ares_expand_name(p, abuf, alen, &name, &len);
546 if (status != ARES_SUCCESS)
548 printf("\t\t\t\t\t\t%s.\n", name);
549 ares_free_string(name);
551 if (p + 20 > aptr + dlen)
553 printf("\t\t\t\t\t\t( %lu %lu %lu %lu %lu )",
554 (unsigned long)DNS__32BIT(p), (unsigned long)DNS__32BIT(p+4),
555 (unsigned long)DNS__32BIT(p+8), (unsigned long)DNS__32BIT(p+12),
556 (unsigned long)DNS__32BIT(p+16));
560 /* The RR data is one or more length-counted character
563 while (p < aptr + dlen)
566 if (p + len + 1 > aptr + dlen)
568 printf("\t%.*s", (int)len, p + 1);
574 /* The RR data is a four-byte Internet address. */
577 printf("\t%s", ares_inet_ntop(AF_INET,aptr,addr,sizeof(addr)));
581 /* The RR data is a 16-byte IPv6 address. */
584 printf("\t%s", ares_inet_ntop(AF_INET6,aptr,addr,sizeof(addr)));
588 /* Not implemented yet */
592 /* The RR data is three two-byte numbers representing the
593 * priority, weight, and port, followed by a domain name.
596 printf("\t%d", DNS__16BIT(aptr));
597 printf(" %d", DNS__16BIT(aptr + 2));
598 printf(" %d", DNS__16BIT(aptr + 4));
600 status = ares_expand_name(aptr + 6, abuf, alen, &name, &len);
601 if (status != ARES_SUCCESS)
603 printf("\t%s.", name);
604 ares_free_string(name);
608 printf("\t[Unknown RR; cannot parse]");
616 static const char *type_name(int type)
620 for (i = 0; i < ntypes; i++)
622 if (types[i].value == type)
623 return types[i].name;
628 static const char *class_name(int dnsclass)
632 for (i = 0; i < nclasses; i++)
634 if (classes[i].value == dnsclass)
635 return classes[i].name;
640 static void usage(void)
642 fprintf(stderr, "usage: adig [-f flag] [-s server] [-c class] "
643 "[-t type] [-p port] name ...\n");