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.
20 #if defined(WIN32) && !defined(WATT32)
23 #ifdef HAVE_SYS_TIME_H
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
29 #include <arpa/nameser.h>
47 #include "inet_ntop.h"
50 #undef WIN32 /* Redefined in MingW headers */
53 /* Mac OS X portability check */
55 #define T_SRV 33 /* server selection */
68 static const struct nv flags[] = {
69 { "usevc", ARES_FLAG_USEVC },
70 { "primary", ARES_FLAG_PRIMARY },
71 { "igntc", ARES_FLAG_IGNTC },
72 { "norecurse", ARES_FLAG_NORECURSE },
73 { "stayopen", ARES_FLAG_STAYOPEN },
74 { "noaliases", ARES_FLAG_NOALIASES }
76 static const int nflags = sizeof(flags) / sizeof(flags[0]);
78 static const struct nv classes[] = {
84 static const int nclasses = sizeof(classes) / sizeof(classes[0]);
86 static const struct nv types[] = {
100 { "MINFO", T_MINFO },
104 { "AFSDB", T_AFSDB },
109 { "NSAP_PTR", T_NSAP_PTR },
118 { "MAILB", T_MAILB },
119 { "MAILA", T_MAILA },
122 static const int ntypes = sizeof(types) / sizeof(types[0]);
124 static const char *opcodes[] = {
125 "QUERY", "IQUERY", "STATUS", "(reserved)", "NOTIFY",
126 "(unknown)", "(unknown)", "(unknown)", "(unknown)",
127 "UPDATEA", "UPDATED", "UPDATEDA", "UPDATEM", "UPDATEMA",
128 "ZONEINIT", "ZONEREF"
131 static const char *rcodes[] = {
132 "NOERROR", "FORMERR", "SERVFAIL", "NXDOMAIN", "NOTIMP", "REFUSED",
133 "(unknown)", "(unknown)", "(unknown)", "(unknown)", "(unknown)",
134 "(unknown)", "(unknown)", "(unknown)", "(unknown)", "NOCHANGE"
137 static void callback(void *arg, int status, unsigned char *abuf, int alen);
138 static const unsigned char *display_question(const unsigned char *aptr,
139 const unsigned char *abuf,
141 static const unsigned char *display_rr(const unsigned char *aptr,
142 const unsigned char *abuf, int alen);
143 static const char *type_name(int type);
144 static const char *class_name(int dnsclass);
145 static void usage(void);
147 int main(int argc, char **argv)
149 ares_channel channel;
150 int c, i, optmask = ARES_OPT_FLAGS, dnsclass = C_IN, type = T_A;
151 int status, nfds, count;
152 struct ares_options options;
153 struct hostent *hostent;
154 fd_set read_fds, write_fds;
155 struct timeval *tvp, tv;
158 WORD wVersionRequested = MAKEWORD(USE_WINSOCK,USE_WINSOCK);
160 WSAStartup(wVersionRequested, &wsaData);
163 options.flags = ARES_FLAG_NOCHECKRESP;
164 options.servers = NULL;
165 options.nservers = 0;
166 while ((c = getopt(argc, argv, "df:s:c:t:T:U:")) != -1)
178 for (i = 0; i < nflags; i++)
180 if (strcmp(flags[i].name, optarg) == 0)
185 options.flags |= flags[i].value;
189 /* Add a server, and specify servers in the option mask. */
190 hostent = gethostbyname(optarg);
191 if (!hostent || hostent->h_addrtype != AF_INET)
193 fprintf(stderr, "adig: server %s not found.\n", optarg);
196 options.servers = realloc(options.servers, (options.nservers + 1)
197 * sizeof(struct in_addr));
198 if (!options.servers)
200 fprintf(stderr, "Out of memory!\n");
203 memcpy(&options.servers[options.nservers], hostent->h_addr,
204 sizeof(struct in_addr));
206 optmask |= ARES_OPT_SERVERS;
210 /* Set the query class. */
211 for (i = 0; i < nclasses; i++)
213 if (strcasecmp(classes[i].name, optarg) == 0)
218 dnsclass = classes[i].value;
222 /* Set the query type. */
223 for (i = 0; i < ntypes; i++)
225 if (strcasecmp(types[i].name, optarg) == 0)
230 type = types[i].value;
234 /* Set the TCP port number. */
235 if (!ISDIGIT(*optarg))
237 options.tcp_port = (unsigned short)strtol(optarg, NULL, 0);
238 optmask |= ARES_OPT_TCP_PORT;
242 /* Set the UDP port number. */
243 if (!ISDIGIT(*optarg))
245 options.udp_port = (unsigned short)strtol(optarg, NULL, 0);
246 optmask |= ARES_OPT_UDP_PORT;
255 status = ares_init_options(&channel, &options, optmask);
257 if (status != ARES_SUCCESS)
259 fprintf(stderr, "ares_init_options: %s\n",
260 ares_strerror(status));
264 /* Initiate the queries, one per command-line argument. If there is
265 * only one query to do, supply NULL as the callback argument;
266 * otherwise, supply the query name as an argument so we can
267 * distinguish responses for the user when printing them out.
270 ares_query(channel, *argv, dnsclass, type, callback, (char *) NULL);
273 for (; *argv; argv++)
274 ares_query(channel, *argv, dnsclass, type, callback, *argv);
277 /* Wait for all queries to complete. */
282 nfds = ares_fds(channel, &read_fds, &write_fds);
285 tvp = ares_timeout(channel, NULL, &tv);
286 count = select(nfds, &read_fds, &write_fds, NULL, tvp);
287 if (count < 0 && SOCKERRNO != EINVAL)
292 ares_process(channel, &read_fds, &write_fds);
295 ares_destroy(channel);
304 static void callback(void *arg, int status, unsigned char *abuf, int alen)
306 char *name = (char *) arg;
307 int id, qr, opcode, aa, tc, rd, ra, rcode;
308 unsigned int qdcount, ancount, nscount, arcount, i;
309 const unsigned char *aptr;
311 /* Display the query name if given. */
313 printf("Answer for query %s:\n", name);
315 /* Display an error message if there was an error, but only stop if
316 * we actually didn't get an answer buffer.
318 if (status != ARES_SUCCESS)
320 printf("%s\n", ares_strerror(status));
325 /* Won't happen, but check anyway, for safety. */
329 /* Parse the answer header. */
330 id = DNS_HEADER_QID(abuf);
331 qr = DNS_HEADER_QR(abuf);
332 opcode = DNS_HEADER_OPCODE(abuf);
333 aa = DNS_HEADER_AA(abuf);
334 tc = DNS_HEADER_TC(abuf);
335 rd = DNS_HEADER_RD(abuf);
336 ra = DNS_HEADER_RA(abuf);
337 rcode = DNS_HEADER_RCODE(abuf);
338 qdcount = DNS_HEADER_QDCOUNT(abuf);
339 ancount = DNS_HEADER_ANCOUNT(abuf);
340 nscount = DNS_HEADER_NSCOUNT(abuf);
341 arcount = DNS_HEADER_ARCOUNT(abuf);
343 /* Display the answer header. */
344 printf("id: %d\n", id);
345 printf("flags: %s%s%s%s%s\n",
351 printf("opcode: %s\n", opcodes[opcode]);
352 printf("rcode: %s\n", rcodes[rcode]);
354 /* Display the questions. */
355 printf("Questions:\n");
356 aptr = abuf + HFIXEDSZ;
357 for (i = 0; i < qdcount; i++)
359 aptr = display_question(aptr, abuf, alen);
364 /* Display the answers. */
365 printf("Answers:\n");
366 for (i = 0; i < ancount; i++)
368 aptr = display_rr(aptr, abuf, alen);
373 /* Display the NS records. */
374 printf("NS records:\n");
375 for (i = 0; i < nscount; i++)
377 aptr = display_rr(aptr, abuf, alen);
382 /* Display the additional records. */
383 printf("Additional records:\n");
384 for (i = 0; i < arcount; i++)
386 aptr = display_rr(aptr, abuf, alen);
392 static const unsigned char *display_question(const unsigned char *aptr,
393 const unsigned char *abuf,
397 int type, dnsclass, status;
400 /* Parse the question name. */
401 status = ares_expand_name(aptr, abuf, alen, &name, &len);
402 if (status != ARES_SUCCESS)
406 /* Make sure there's enough data after the name for the fixed part
409 if (aptr + QFIXEDSZ > abuf + alen)
411 ares_free_string(name);
415 /* Parse the question type and class. */
416 type = DNS_QUESTION_TYPE(aptr);
417 dnsclass = DNS_QUESTION_CLASS(aptr);
420 /* Display the question, in a format sort of similar to how we will
423 printf("\t%-15s.\t", name);
424 if (dnsclass != C_IN)
425 printf("\t%s", class_name(dnsclass));
426 printf("\t%s\n", type_name(type));
427 ares_free_string(name);
431 static const unsigned char *display_rr(const unsigned char *aptr,
432 const unsigned char *abuf, int alen)
434 const unsigned char *p;
436 int type, dnsclass, ttl, dlen, status;
440 /* Parse the RR name. */
441 status = ares_expand_name(aptr, abuf, alen, &name, &len);
442 if (status != ARES_SUCCESS)
446 /* Make sure there is enough data after the RR name for the fixed
449 if (aptr + RRFIXEDSZ > abuf + alen)
451 ares_free_string(name);
455 /* Parse the fixed part of the RR, and advance to the RR data
457 type = DNS_RR_TYPE(aptr);
458 dnsclass = DNS_RR_CLASS(aptr);
459 ttl = DNS_RR_TTL(aptr);
460 dlen = DNS_RR_LEN(aptr);
462 if (aptr + dlen > abuf + alen)
464 ares_free_string(name);
468 /* Display the RR name, class, and type. */
469 printf("\t%-15s.\t%d", name, ttl);
470 if (dnsclass != C_IN)
471 printf("\t%s", class_name(dnsclass));
472 printf("\t%s", type_name(type));
473 ares_free_string(name);
475 /* Display the RR data. Don't touch aptr. */
486 /* For these types, the RR data is just a domain name. */
487 status = ares_expand_name(aptr, abuf, alen, &name, &len);
488 if (status != ARES_SUCCESS)
490 printf("\t%s.", name);
491 ares_free_string(name);
495 /* The RR data is two length-counted character strings. */
498 if (p + len + 1 > aptr + dlen)
500 printf("\t%.*s", (int)len, p + 1);
503 if (p + len + 1 > aptr + dlen)
505 printf("\t%.*s", (int)len, p + 1);
509 /* The RR data is two domain names. */
511 status = ares_expand_name(p, abuf, alen, &name, &len);
512 if (status != ARES_SUCCESS)
514 printf("\t%s.", name);
515 ares_free_string(name);
517 status = ares_expand_name(p, abuf, alen, &name, &len);
518 if (status != ARES_SUCCESS)
520 printf("\t%s.", name);
521 ares_free_string(name);
525 /* The RR data is two bytes giving a preference ordering, and
526 * then a domain name.
530 printf("\t%d", DNS__16BIT(aptr));
531 status = ares_expand_name(aptr + 2, abuf, alen, &name, &len);
532 if (status != ARES_SUCCESS)
534 printf("\t%s.", name);
535 ares_free_string(name);
539 /* The RR data is two domain names and then five four-byte
540 * numbers giving the serial number and some timeouts.
543 status = ares_expand_name(p, abuf, alen, &name, &len);
544 if (status != ARES_SUCCESS)
546 printf("\t%s.\n", name);
547 ares_free_string(name);
549 status = ares_expand_name(p, abuf, alen, &name, &len);
550 if (status != ARES_SUCCESS)
552 printf("\t\t\t\t\t\t%s.\n", name);
553 ares_free_string(name);
555 if (p + 20 > aptr + dlen)
557 printf("\t\t\t\t\t\t( %lu %lu %lu %lu %lu )",
558 (unsigned long)DNS__32BIT(p), (unsigned long)DNS__32BIT(p+4),
559 (unsigned long)DNS__32BIT(p+8), (unsigned long)DNS__32BIT(p+12),
560 (unsigned long)DNS__32BIT(p+16));
564 /* The RR data is one or more length-counted character
567 while (p < aptr + dlen)
570 if (p + len + 1 > aptr + dlen)
572 printf("\t%.*s", (int)len, p + 1);
578 /* The RR data is a four-byte Internet address. */
581 printf("\t%s", ares_inet_ntop(AF_INET,aptr,addr,sizeof(addr)));
585 /* The RR data is a 16-byte IPv6 address. */
588 printf("\t%s", ares_inet_ntop(AF_INET6,aptr,addr,sizeof(addr)));
592 /* Not implemented yet */
596 /* The RR data is three two-byte numbers representing the
597 * priority, weight, and port, followed by a domain name.
600 printf("\t%d", DNS__16BIT(aptr));
601 printf(" %d", DNS__16BIT(aptr + 2));
602 printf(" %d", DNS__16BIT(aptr + 4));
604 status = ares_expand_name(aptr + 6, abuf, alen, &name, &len);
605 if (status != ARES_SUCCESS)
607 printf("\t%s.", name);
608 ares_free_string(name);
612 printf("\t[Unknown RR; cannot parse]");
620 static const char *type_name(int type)
624 for (i = 0; i < ntypes; i++)
626 if (types[i].value == type)
627 return types[i].name;
632 static const char *class_name(int dnsclass)
636 for (i = 0; i < nclasses; i++)
638 if (classes[i].value == dnsclass)
639 return classes[i].name;
644 static void usage(void)
646 fprintf(stderr, "usage: adig [-f flag] [-s server] [-c class] "
647 "[-t type] [-p port] name ...\n");