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>
44 #include "inet_ntop.h"
45 #include "ares_getopt.h"
48 #undef WIN32 /* Redefined in MingW headers */
51 /* Mac OS X portability check */
53 #define T_SRV 33 /* server selection */
61 static const struct nv flags[] = {
62 { "usevc", ARES_FLAG_USEVC },
63 { "primary", ARES_FLAG_PRIMARY },
64 { "igntc", ARES_FLAG_IGNTC },
65 { "norecurse", ARES_FLAG_NORECURSE },
66 { "stayopen", ARES_FLAG_STAYOPEN },
67 { "noaliases", ARES_FLAG_NOALIASES }
69 static const int nflags = sizeof(flags) / sizeof(flags[0]);
71 static const struct nv classes[] = {
77 static const int nclasses = sizeof(classes) / sizeof(classes[0]);
79 static const struct nv types[] = {
102 { "NSAP_PTR", T_NSAP_PTR },
111 { "MAILB", T_MAILB },
112 { "MAILA", T_MAILA },
115 static const int ntypes = sizeof(types) / sizeof(types[0]);
117 static const char *opcodes[] = {
118 "QUERY", "IQUERY", "STATUS", "(reserved)", "NOTIFY",
119 "(unknown)", "(unknown)", "(unknown)", "(unknown)",
120 "UPDATEA", "UPDATED", "UPDATEDA", "UPDATEM", "UPDATEMA",
121 "ZONEINIT", "ZONEREF"
124 static const char *rcodes[] = {
125 "NOERROR", "FORMERR", "SERVFAIL", "NXDOMAIN", "NOTIMP", "REFUSED",
126 "(unknown)", "(unknown)", "(unknown)", "(unknown)", "(unknown)",
127 "(unknown)", "(unknown)", "(unknown)", "(unknown)", "NOCHANGE"
130 static void callback(void *arg, int status, int timeouts,
131 unsigned char *abuf, int alen);
132 static const unsigned char *display_question(const unsigned char *aptr,
133 const unsigned char *abuf,
135 static const unsigned char *display_rr(const unsigned char *aptr,
136 const unsigned char *abuf, int alen);
137 static const char *type_name(int type);
138 static const char *class_name(int dnsclass);
139 static void usage(void);
141 int main(int argc, char **argv)
143 ares_channel channel;
144 int c, i, optmask = ARES_OPT_FLAGS, dnsclass = C_IN, type = T_A;
145 int status, nfds, count;
146 struct ares_options options;
147 struct hostent *hostent;
148 fd_set read_fds, write_fds;
149 struct timeval *tvp, tv;
152 WORD wVersionRequested = MAKEWORD(USE_WINSOCK,USE_WINSOCK);
154 WSAStartup(wVersionRequested, &wsaData);
157 options.flags = ARES_FLAG_NOCHECKRESP;
158 options.servers = NULL;
159 options.nservers = 0;
160 while ((c = ares_getopt(argc, argv, "df:s:c:t:T:U:")) != -1)
172 for (i = 0; i < nflags; i++)
174 if (strcmp(flags[i].name, optarg) == 0)
179 options.flags |= flags[i].value;
183 /* Add a server, and specify servers in the option mask. */
184 hostent = gethostbyname(optarg);
185 if (!hostent || hostent->h_addrtype != AF_INET)
187 fprintf(stderr, "adig: server %s not found.\n", optarg);
190 options.servers = realloc(options.servers, (options.nservers + 1)
191 * sizeof(struct in_addr));
192 if (!options.servers)
194 fprintf(stderr, "Out of memory!\n");
197 memcpy(&options.servers[options.nservers], hostent->h_addr,
198 sizeof(struct in_addr));
200 optmask |= ARES_OPT_SERVERS;
204 /* Set the query class. */
205 for (i = 0; i < nclasses; i++)
207 if (strcasecmp(classes[i].name, optarg) == 0)
212 dnsclass = classes[i].value;
216 /* Set the query type. */
217 for (i = 0; i < ntypes; i++)
219 if (strcasecmp(types[i].name, optarg) == 0)
224 type = types[i].value;
228 /* Set the TCP port number. */
229 if (!ISDIGIT(*optarg))
231 options.tcp_port = (unsigned short)strtol(optarg, NULL, 0);
232 optmask |= ARES_OPT_TCP_PORT;
236 /* Set the UDP port number. */
237 if (!ISDIGIT(*optarg))
239 options.udp_port = (unsigned short)strtol(optarg, NULL, 0);
240 optmask |= ARES_OPT_UDP_PORT;
249 status = ares_init_options(&channel, &options, optmask);
251 if (status != ARES_SUCCESS)
253 fprintf(stderr, "ares_init_options: %s\n",
254 ares_strerror(status));
258 /* Initiate the queries, one per command-line argument. If there is
259 * only one query to do, supply NULL as the callback argument;
260 * otherwise, supply the query name as an argument so we can
261 * distinguish responses for the user when printing them out.
264 ares_query(channel, *argv, dnsclass, type, callback, (char *) NULL);
267 for (; *argv; argv++)
268 ares_query(channel, *argv, dnsclass, type, callback, *argv);
271 /* Wait for all queries to complete. */
276 nfds = ares_fds(channel, &read_fds, &write_fds);
279 tvp = ares_timeout(channel, NULL, &tv);
280 count = select(nfds, &read_fds, &write_fds, NULL, tvp);
281 if (count < 0 && SOCKERRNO != EINVAL)
286 ares_process(channel, &read_fds, &write_fds);
289 ares_destroy(channel);
298 static void callback(void *arg, int status, int timeouts,
299 unsigned char *abuf, int alen)
301 char *name = (char *) arg;
302 int id, qr, opcode, aa, tc, rd, ra, rcode;
303 unsigned int qdcount, ancount, nscount, arcount, i;
304 const unsigned char *aptr;
306 /* Display the query name if given. */
308 printf("Answer for query %s:\n", name);
310 /* Display an error message if there was an error, but only stop if
311 * we actually didn't get an answer buffer.
313 if (status != ARES_SUCCESS)
315 printf("%s\n", ares_strerror(status));
320 /* Won't happen, but check anyway, for safety. */
324 /* Parse the answer header. */
325 id = DNS_HEADER_QID(abuf);
326 qr = DNS_HEADER_QR(abuf);
327 opcode = DNS_HEADER_OPCODE(abuf);
328 aa = DNS_HEADER_AA(abuf);
329 tc = DNS_HEADER_TC(abuf);
330 rd = DNS_HEADER_RD(abuf);
331 ra = DNS_HEADER_RA(abuf);
332 rcode = DNS_HEADER_RCODE(abuf);
333 qdcount = DNS_HEADER_QDCOUNT(abuf);
334 ancount = DNS_HEADER_ANCOUNT(abuf);
335 nscount = DNS_HEADER_NSCOUNT(abuf);
336 arcount = DNS_HEADER_ARCOUNT(abuf);
338 /* Display the answer header. */
339 printf("id: %d\n", id);
340 printf("flags: %s%s%s%s%s\n",
346 printf("opcode: %s\n", opcodes[opcode]);
347 printf("rcode: %s\n", rcodes[rcode]);
349 /* Display the questions. */
350 printf("Questions:\n");
351 aptr = abuf + HFIXEDSZ;
352 for (i = 0; i < qdcount; i++)
354 aptr = display_question(aptr, abuf, alen);
359 /* Display the answers. */
360 printf("Answers:\n");
361 for (i = 0; i < ancount; i++)
363 aptr = display_rr(aptr, abuf, alen);
368 /* Display the NS records. */
369 printf("NS records:\n");
370 for (i = 0; i < nscount; i++)
372 aptr = display_rr(aptr, abuf, alen);
377 /* Display the additional records. */
378 printf("Additional records:\n");
379 for (i = 0; i < arcount; i++)
381 aptr = display_rr(aptr, abuf, alen);
387 static const unsigned char *display_question(const unsigned char *aptr,
388 const unsigned char *abuf,
392 int type, dnsclass, status;
395 /* Parse the question name. */
396 status = ares_expand_name(aptr, abuf, alen, &name, &len);
397 if (status != ARES_SUCCESS)
401 /* Make sure there's enough data after the name for the fixed part
404 if (aptr + QFIXEDSZ > abuf + alen)
406 ares_free_string(name);
410 /* Parse the question type and class. */
411 type = DNS_QUESTION_TYPE(aptr);
412 dnsclass = DNS_QUESTION_CLASS(aptr);
415 /* Display the question, in a format sort of similar to how we will
418 printf("\t%-15s.\t", name);
419 if (dnsclass != C_IN)
420 printf("\t%s", class_name(dnsclass));
421 printf("\t%s\n", type_name(type));
422 ares_free_string(name);
426 static const unsigned char *display_rr(const unsigned char *aptr,
427 const unsigned char *abuf, int alen)
429 const unsigned char *p;
431 int type, dnsclass, ttl, dlen, status;
435 /* Parse the RR name. */
436 status = ares_expand_name(aptr, abuf, alen, &name, &len);
437 if (status != ARES_SUCCESS)
441 /* Make sure there is enough data after the RR name for the fixed
444 if (aptr + RRFIXEDSZ > abuf + alen)
446 ares_free_string(name);
450 /* Parse the fixed part of the RR, and advance to the RR data
452 type = DNS_RR_TYPE(aptr);
453 dnsclass = DNS_RR_CLASS(aptr);
454 ttl = DNS_RR_TTL(aptr);
455 dlen = DNS_RR_LEN(aptr);
457 if (aptr + dlen > abuf + alen)
459 ares_free_string(name);
463 /* Display the RR name, class, and type. */
464 printf("\t%-15s.\t%d", name, ttl);
465 if (dnsclass != C_IN)
466 printf("\t%s", class_name(dnsclass));
467 printf("\t%s", type_name(type));
468 ares_free_string(name);
470 /* Display the RR data. Don't touch aptr. */
481 /* For these types, the RR data is just a domain name. */
482 status = ares_expand_name(aptr, abuf, alen, &name, &len);
483 if (status != ARES_SUCCESS)
485 printf("\t%s.", name);
486 ares_free_string(name);
490 /* The RR data is two length-counted character strings. */
493 if (p + len + 1 > aptr + dlen)
495 printf("\t%.*s", (int)len, p + 1);
498 if (p + len + 1 > aptr + dlen)
500 printf("\t%.*s", (int)len, p + 1);
504 /* The RR data is two domain names. */
506 status = ares_expand_name(p, abuf, alen, &name, &len);
507 if (status != ARES_SUCCESS)
509 printf("\t%s.", name);
510 ares_free_string(name);
512 status = ares_expand_name(p, abuf, alen, &name, &len);
513 if (status != ARES_SUCCESS)
515 printf("\t%s.", name);
516 ares_free_string(name);
520 /* The RR data is two bytes giving a preference ordering, and
521 * then a domain name.
525 printf("\t%d", DNS__16BIT(aptr));
526 status = ares_expand_name(aptr + 2, abuf, alen, &name, &len);
527 if (status != ARES_SUCCESS)
529 printf("\t%s.", name);
530 ares_free_string(name);
534 /* The RR data is two domain names and then five four-byte
535 * numbers giving the serial number and some timeouts.
538 status = ares_expand_name(p, abuf, alen, &name, &len);
539 if (status != ARES_SUCCESS)
541 printf("\t%s.\n", name);
542 ares_free_string(name);
544 status = ares_expand_name(p, abuf, alen, &name, &len);
545 if (status != ARES_SUCCESS)
547 printf("\t\t\t\t\t\t%s.\n", name);
548 ares_free_string(name);
550 if (p + 20 > aptr + dlen)
552 printf("\t\t\t\t\t\t( %lu %lu %lu %lu %lu )",
553 (unsigned long)DNS__32BIT(p), (unsigned long)DNS__32BIT(p+4),
554 (unsigned long)DNS__32BIT(p+8), (unsigned long)DNS__32BIT(p+12),
555 (unsigned long)DNS__32BIT(p+16));
559 /* The RR data is one or more length-counted character
562 while (p < aptr + dlen)
565 if (p + len + 1 > aptr + dlen)
567 printf("\t%.*s", (int)len, p + 1);
573 /* The RR data is a four-byte Internet address. */
576 printf("\t%s", ares_inet_ntop(AF_INET,aptr,addr,sizeof(addr)));
580 /* The RR data is a 16-byte IPv6 address. */
583 printf("\t%s", ares_inet_ntop(AF_INET6,aptr,addr,sizeof(addr)));
587 /* Not implemented yet */
591 /* The RR data is three two-byte numbers representing the
592 * priority, weight, and port, followed by a domain name.
595 printf("\t%d", DNS__16BIT(aptr));
596 printf(" %d", DNS__16BIT(aptr + 2));
597 printf(" %d", DNS__16BIT(aptr + 4));
599 status = ares_expand_name(aptr + 6, abuf, alen, &name, &len);
600 if (status != ARES_SUCCESS)
602 printf("\t%s.", name);
603 ares_free_string(name);
607 printf("\t[Unknown RR; cannot parse]");
615 static const char *type_name(int type)
619 for (i = 0; i < ntypes; i++)
621 if (types[i].value == type)
622 return types[i].name;
627 static const char *class_name(int dnsclass)
631 for (i = 0; i < nclasses; i++)
633 if (classes[i].value == dnsclass)
634 return classes[i].name;
639 static void usage(void)
641 fprintf(stderr, "usage: adig [-f flag] [-s server] [-c class] "
642 "[-t type] [-p port] name ...\n");