1 /* Copyright 1998 by the Massachusetts Institute of Technology.
3 * Permission to use, copy, modify, and distribute this
4 * software and its documentation for any purpose and without
5 * fee is hereby granted, provided that the above copyright
6 * notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting
8 * documentation, and that the name of M.I.T. not be used in
9 * advertising or publicity pertaining to distribution of the
10 * software without specific, written prior permission.
11 * M.I.T. makes no representations about the suitability of
12 * this software for any purpose. It is provided "as is"
13 * without express or implied warranty.
16 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <arpa/nameser.h>
39 #define INADDR_NONE 0xffffffff
42 /* Mac OS X portability check */
44 #define T_SRV 33 /* server selection */
55 static const struct nv flags[] = {
56 { "usevc", ARES_FLAG_USEVC },
57 { "primary", ARES_FLAG_PRIMARY },
58 { "igntc", ARES_FLAG_IGNTC },
59 { "norecurse", ARES_FLAG_NORECURSE },
60 { "stayopen", ARES_FLAG_STAYOPEN },
61 { "noaliases", ARES_FLAG_NOALIASES }
63 static const int nflags = sizeof(flags) / sizeof(flags[0]);
65 static const struct nv classes[] = {
71 static const int nclasses = sizeof(classes) / sizeof(classes[0]);
73 static const struct nv types[] = {
96 { "NSAP_PTR", T_NSAP_PTR },
105 { "MAILB", T_MAILB },
106 { "MAILA", T_MAILA },
109 static const int ntypes = sizeof(types) / sizeof(types[0]);
111 static const char *opcodes[] = {
112 "QUERY", "IQUERY", "STATUS", "(reserved)", "NOTIFY",
113 "(unknown)", "(unknown)", "(unknown)", "(unknown)",
114 "UPDATEA", "UPDATED", "UPDATEDA", "UPDATEM", "UPDATEMA",
115 "ZONEINIT", "ZONEREF"
118 static const char *rcodes[] = {
119 "NOERROR", "FORMERR", "SERVFAIL", "NXDOMAIN", "NOTIMP", "REFUSED",
120 "(unknown)", "(unknown)", "(unknown)", "(unknown)", "(unknown)",
121 "(unknown)", "(unknown)", "(unknown)", "(unknown)", "NOCHANGE"
124 static void callback(void *arg, int status, unsigned char *abuf, int alen);
125 static const unsigned char *display_question(const unsigned char *aptr,
126 const unsigned char *abuf,
128 static const unsigned char *display_rr(const unsigned char *aptr,
129 const unsigned char *abuf, int alen);
130 static const char *type_name(int type);
131 static const char *class_name(int dnsclass);
132 static void usage(void);
134 int main(int argc, char **argv)
136 ares_channel channel;
137 int c, i, optmask = ARES_OPT_FLAGS, dnsclass = C_IN, type = T_A;
138 int status, nfds, count;
139 struct ares_options options;
140 struct hostent *hostent;
141 fd_set read_fds, write_fds;
142 struct timeval *tvp, tv;
146 WORD wVersionRequested = MAKEWORD(1,1);
148 WSAStartup(wVersionRequested, &wsaData);
151 options.flags = ARES_FLAG_NOCHECKRESP;
152 options.servers = NULL;
153 options.nservers = 0;
154 while ((c = getopt(argc, argv, "f:s:c:t:T:U:")) != -1)
160 for (i = 0; i < nflags; i++)
162 if (strcmp(flags[i].name, optarg) == 0)
167 options.flags |= flags[i].value;
171 /* Add a server, and specify servers in the option mask. */
172 hostent = gethostbyname(optarg);
173 if (!hostent || hostent->h_addrtype != AF_INET)
175 fprintf(stderr, "adig: server %s not found.\n", optarg);
178 options.servers = realloc(options.servers, (options.nservers + 1)
179 * sizeof(struct in_addr));
180 if (!options.servers)
182 fprintf(stderr, "Out of memory!\n");
185 memcpy(&options.servers[options.nservers], hostent->h_addr,
186 sizeof(struct in_addr));
188 optmask |= ARES_OPT_SERVERS;
192 /* Set the query class. */
193 for (i = 0; i < nclasses; i++)
195 if (strcasecmp(classes[i].name, optarg) == 0)
200 dnsclass = classes[i].value;
204 /* Set the query type. */
205 for (i = 0; i < ntypes; i++)
207 if (strcasecmp(types[i].name, optarg) == 0)
212 type = types[i].value;
216 /* Set the TCP port number. */
217 if (!isdigit((unsigned char)*optarg))
219 options.tcp_port = strtol(optarg, NULL, 0);
220 optmask |= ARES_OPT_TCP_PORT;
224 /* Set the UDP port number. */
225 if (!isdigit((unsigned char)*optarg))
227 options.udp_port = strtol(optarg, NULL, 0);
228 optmask |= ARES_OPT_UDP_PORT;
237 status = ares_init_options(&channel, &options, optmask);
239 if (status != ARES_SUCCESS)
241 fprintf(stderr, "ares_init_options: %s\n",
242 ares_strerror(status));
243 ares_free_errmem(errmem);
247 /* Initiate the queries, one per command-line argument. If there is
248 * only one query to do, supply NULL as the callback argument;
249 * otherwise, supply the query name as an argument so we can
250 * distinguish responses for the user when printing them out.
253 ares_query(channel, *argv, dnsclass, type, callback, (char *) NULL);
256 for (; *argv; argv++)
257 ares_query(channel, *argv, dnsclass, type, callback, *argv);
260 /* Wait for all queries to complete. */
265 nfds = ares_fds(channel, &read_fds, &write_fds);
268 tvp = ares_timeout(channel, NULL, &tv);
269 count = select(nfds, &read_fds, &write_fds, NULL, tvp);
270 if (count < 0 && errno != EINVAL)
275 ares_process(channel, &read_fds, &write_fds);
278 ares_destroy(channel);
282 static void callback(void *arg, int status, unsigned char *abuf, int alen)
284 char *name = (char *) arg, *errmem;
285 int id, qr, opcode, aa, tc, rd, ra, rcode;
286 unsigned int qdcount, ancount, nscount, arcount, i;
287 const unsigned char *aptr;
289 /* Display the query name if given. */
291 printf("Answer for query %s:\n", name);
293 /* Display an error message if there was an error, but only stop if
294 * we actually didn't get an answer buffer.
296 if (status != ARES_SUCCESS)
298 printf("%s\n", ares_strerror(status));
299 ares_free_errmem(errmem);
304 /* Won't happen, but check anyway, for safety. */
308 /* Parse the answer header. */
309 id = DNS_HEADER_QID(abuf);
310 qr = DNS_HEADER_QR(abuf);
311 opcode = DNS_HEADER_OPCODE(abuf);
312 aa = DNS_HEADER_AA(abuf);
313 tc = DNS_HEADER_TC(abuf);
314 rd = DNS_HEADER_RD(abuf);
315 ra = DNS_HEADER_RA(abuf);
316 rcode = DNS_HEADER_RCODE(abuf);
317 qdcount = DNS_HEADER_QDCOUNT(abuf);
318 ancount = DNS_HEADER_ANCOUNT(abuf);
319 nscount = DNS_HEADER_NSCOUNT(abuf);
320 arcount = DNS_HEADER_ARCOUNT(abuf);
322 /* Display the answer header. */
323 printf("id: %d\n", id);
324 printf("flags: %s%s%s%s%s\n",
330 printf("opcode: %s\n", opcodes[opcode]);
331 printf("rcode: %s\n", rcodes[rcode]);
333 /* Display the questions. */
334 printf("Questions:\n");
335 aptr = abuf + HFIXEDSZ;
336 for (i = 0; i < qdcount; i++)
338 aptr = display_question(aptr, abuf, alen);
343 /* Display the answers. */
344 printf("Answers:\n");
345 for (i = 0; i < ancount; i++)
347 aptr = display_rr(aptr, abuf, alen);
352 /* Display the NS records. */
353 printf("NS records:\n");
354 for (i = 0; i < nscount; i++)
356 aptr = display_rr(aptr, abuf, alen);
361 /* Display the additional records. */
362 printf("Additional records:\n");
363 for (i = 0; i < arcount; i++)
365 aptr = display_rr(aptr, abuf, alen);
371 static const unsigned char *display_question(const unsigned char *aptr,
372 const unsigned char *abuf,
376 int type, dnsclass, status, len;
378 /* Parse the question name. */
379 status = ares_expand_name(aptr, abuf, alen, &name, &len);
380 if (status != ARES_SUCCESS)
384 /* Make sure there's enough data after the name for the fixed part
387 if (aptr + QFIXEDSZ > abuf + alen)
393 /* Parse the question type and class. */
394 type = DNS_QUESTION_TYPE(aptr);
395 dnsclass = DNS_QUESTION_CLASS(aptr);
398 /* Display the question, in a format sort of similar to how we will
401 printf("\t%-15s.\t", name);
402 if (dnsclass != C_IN)
403 printf("\t%s", class_name(dnsclass));
404 printf("\t%s\n", type_name(type));
409 static const unsigned char *display_rr(const unsigned char *aptr,
410 const unsigned char *abuf, int alen)
412 const unsigned char *p;
414 int type, dnsclass, ttl, dlen, status, len;
417 /* Parse the RR name. */
418 status = ares_expand_name(aptr, abuf, alen, &name, &len);
419 if (status != ARES_SUCCESS)
423 /* Make sure there is enough data after the RR name for the fixed
426 if (aptr + RRFIXEDSZ > abuf + alen)
432 /* Parse the fixed part of the RR, and advance to the RR data
434 type = DNS_RR_TYPE(aptr);
435 dnsclass = DNS_RR_CLASS(aptr);
436 ttl = DNS_RR_TTL(aptr);
437 dlen = DNS_RR_LEN(aptr);
439 if (aptr + dlen > abuf + alen)
445 /* Display the RR name, class, and type. */
446 printf("\t%-15s.\t%d", name, ttl);
447 if (dnsclass != C_IN)
448 printf("\t%s", class_name(dnsclass));
449 printf("\t%s", type_name(type));
452 /* Display the RR data. Don't touch aptr. */
463 /* For these types, the RR data is just a domain name. */
464 status = ares_expand_name(aptr, abuf, alen, &name, &len);
465 if (status != ARES_SUCCESS)
467 printf("\t%s.", name);
472 /* The RR data is two length-counted character strings. */
475 if (p + len + 1 > aptr + dlen)
477 printf("\t%.*s", len, p + 1);
480 if (p + len + 1 > aptr + dlen)
482 printf("\t%.*s", len, p + 1);
486 /* The RR data is two domain names. */
488 status = ares_expand_name(p, abuf, alen, &name, &len);
489 if (status != ARES_SUCCESS)
491 printf("\t%s.", name);
494 status = ares_expand_name(p, abuf, alen, &name, &len);
495 if (status != ARES_SUCCESS)
497 printf("\t%s.", name);
502 /* The RR data is two bytes giving a preference ordering, and
503 * then a domain name.
507 printf("\t%d", (aptr[0] << 8) | aptr[1]);
508 status = ares_expand_name(aptr + 2, abuf, alen, &name, &len);
509 if (status != ARES_SUCCESS)
511 printf("\t%s.", name);
516 /* The RR data is two domain names and then five four-byte
517 * numbers giving the serial number and some timeouts.
520 status = ares_expand_name(p, abuf, alen, &name, &len);
521 if (status != ARES_SUCCESS)
523 printf("\t%s.\n", name);
526 status = ares_expand_name(p, abuf, alen, &name, &len);
527 if (status != ARES_SUCCESS)
529 printf("\t\t\t\t\t\t%s.\n", name);
532 if (p + 20 > aptr + dlen)
534 printf("\t\t\t\t\t\t( %d %d %d %d %d )",
535 (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3],
536 (p[4] << 24) | (p[5] << 16) | (p[6] << 8) | p[7],
537 (p[8] << 24) | (p[9] << 16) | (p[10] << 8) | p[11],
538 (p[12] << 24) | (p[13] << 16) | (p[14] << 8) | p[15],
539 (p[16] << 24) | (p[17] << 16) | (p[18] << 8) | p[19]);
543 /* The RR data is one or more length-counted character
546 while (p < aptr + dlen)
549 if (p + len + 1 > aptr + dlen)
551 printf("\t%.*s", len, p + 1);
557 /* The RR data is a four-byte Internet address. */
560 memcpy(&addr, aptr, sizeof(struct in_addr));
561 printf("\t%s", inet_ntoa(addr));
565 /* Not implemented yet */
569 /* The RR data is three two-byte numbers representing the
570 * priority, weight, and port, followed by a domain name.
573 printf("\t%d", DNS__16BIT(aptr));
574 printf(" %d", DNS__16BIT(aptr + 2));
575 printf(" %d", DNS__16BIT(aptr + 4));
577 status = ares_expand_name(aptr + 6, abuf, alen, &name, &len);
578 if (status != ARES_SUCCESS)
580 printf("\t%s.", name);
585 printf("\t[Unknown RR; cannot parse]");
592 static const char *type_name(int type)
596 for (i = 0; i < ntypes; i++)
598 if (types[i].value == type)
599 return types[i].name;
604 static const char *class_name(int dnsclass)
608 for (i = 0; i < nclasses; i++)
610 if (classes[i].value == dnsclass)
611 return classes[i].name;
616 static void usage(void)
618 fprintf(stderr, "usage: adig [-f flag] [-s server] [-c class] "
619 "[-t type] [-p port] name ...\n");