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;
145 WORD wVersionRequested = MAKEWORD(1,1);
147 WSAStartup(wVersionRequested, &wsaData);
150 options.flags = ARES_FLAG_NOCHECKRESP;
151 options.servers = NULL;
152 options.nservers = 0;
153 while ((c = getopt(argc, argv, "f:s:c:t:T:U:")) != -1)
159 for (i = 0; i < nflags; i++)
161 if (strcmp(flags[i].name, optarg) == 0)
166 options.flags |= flags[i].value;
170 /* Add a server, and specify servers in the option mask. */
171 hostent = gethostbyname(optarg);
172 if (!hostent || hostent->h_addrtype != AF_INET)
174 fprintf(stderr, "adig: server %s not found.\n", optarg);
177 options.servers = realloc(options.servers, (options.nservers + 1)
178 * sizeof(struct in_addr));
179 if (!options.servers)
181 fprintf(stderr, "Out of memory!\n");
184 memcpy(&options.servers[options.nservers], hostent->h_addr,
185 sizeof(struct in_addr));
187 optmask |= ARES_OPT_SERVERS;
191 /* Set the query class. */
192 for (i = 0; i < nclasses; i++)
194 if (strcasecmp(classes[i].name, optarg) == 0)
199 dnsclass = classes[i].value;
203 /* Set the query type. */
204 for (i = 0; i < ntypes; i++)
206 if (strcasecmp(types[i].name, optarg) == 0)
211 type = types[i].value;
215 /* Set the TCP port number. */
216 if (!isdigit((unsigned char)*optarg))
218 options.tcp_port = strtol(optarg, NULL, 0);
219 optmask |= ARES_OPT_TCP_PORT;
223 /* Set the UDP port number. */
224 if (!isdigit((unsigned char)*optarg))
226 options.udp_port = strtol(optarg, NULL, 0);
227 optmask |= ARES_OPT_UDP_PORT;
236 status = ares_init_options(&channel, &options, optmask);
238 if (status != ARES_SUCCESS)
240 fprintf(stderr, "ares_init_options: %s\n",
241 ares_strerror(status));
245 /* Initiate the queries, one per command-line argument. If there is
246 * only one query to do, supply NULL as the callback argument;
247 * otherwise, supply the query name as an argument so we can
248 * distinguish responses for the user when printing them out.
251 ares_query(channel, *argv, dnsclass, type, callback, (char *) NULL);
254 for (; *argv; argv++)
255 ares_query(channel, *argv, dnsclass, type, callback, *argv);
258 /* Wait for all queries to complete. */
263 nfds = ares_fds(channel, &read_fds, &write_fds);
266 tvp = ares_timeout(channel, NULL, &tv);
267 count = select(nfds, &read_fds, &write_fds, NULL, tvp);
268 if (count < 0 && errno != EINVAL)
273 ares_process(channel, &read_fds, &write_fds);
276 ares_destroy(channel);
280 static void callback(void *arg, int status, unsigned char *abuf, int alen)
282 char *name = (char *) arg;
283 int id, qr, opcode, aa, tc, rd, ra, rcode;
284 unsigned int qdcount, ancount, nscount, arcount, i;
285 const unsigned char *aptr;
287 /* Display the query name if given. */
289 printf("Answer for query %s:\n", name);
291 /* Display an error message if there was an error, but only stop if
292 * we actually didn't get an answer buffer.
294 if (status != ARES_SUCCESS)
296 printf("%s\n", ares_strerror(status));
301 /* Won't happen, but check anyway, for safety. */
305 /* Parse the answer header. */
306 id = DNS_HEADER_QID(abuf);
307 qr = DNS_HEADER_QR(abuf);
308 opcode = DNS_HEADER_OPCODE(abuf);
309 aa = DNS_HEADER_AA(abuf);
310 tc = DNS_HEADER_TC(abuf);
311 rd = DNS_HEADER_RD(abuf);
312 ra = DNS_HEADER_RA(abuf);
313 rcode = DNS_HEADER_RCODE(abuf);
314 qdcount = DNS_HEADER_QDCOUNT(abuf);
315 ancount = DNS_HEADER_ANCOUNT(abuf);
316 nscount = DNS_HEADER_NSCOUNT(abuf);
317 arcount = DNS_HEADER_ARCOUNT(abuf);
319 /* Display the answer header. */
320 printf("id: %d\n", id);
321 printf("flags: %s%s%s%s%s\n",
327 printf("opcode: %s\n", opcodes[opcode]);
328 printf("rcode: %s\n", rcodes[rcode]);
330 /* Display the questions. */
331 printf("Questions:\n");
332 aptr = abuf + HFIXEDSZ;
333 for (i = 0; i < qdcount; i++)
335 aptr = display_question(aptr, abuf, alen);
340 /* Display the answers. */
341 printf("Answers:\n");
342 for (i = 0; i < ancount; i++)
344 aptr = display_rr(aptr, abuf, alen);
349 /* Display the NS records. */
350 printf("NS records:\n");
351 for (i = 0; i < nscount; i++)
353 aptr = display_rr(aptr, abuf, alen);
358 /* Display the additional records. */
359 printf("Additional records:\n");
360 for (i = 0; i < arcount; i++)
362 aptr = display_rr(aptr, abuf, alen);
368 static const unsigned char *display_question(const unsigned char *aptr,
369 const unsigned char *abuf,
373 int type, dnsclass, status, len;
375 /* Parse the question name. */
376 status = ares_expand_name(aptr, abuf, alen, &name, &len);
377 if (status != ARES_SUCCESS)
381 /* Make sure there's enough data after the name for the fixed part
384 if (aptr + QFIXEDSZ > abuf + alen)
390 /* Parse the question type and class. */
391 type = DNS_QUESTION_TYPE(aptr);
392 dnsclass = DNS_QUESTION_CLASS(aptr);
395 /* Display the question, in a format sort of similar to how we will
398 printf("\t%-15s.\t", name);
399 if (dnsclass != C_IN)
400 printf("\t%s", class_name(dnsclass));
401 printf("\t%s\n", type_name(type));
406 static const unsigned char *display_rr(const unsigned char *aptr,
407 const unsigned char *abuf, int alen)
409 const unsigned char *p;
411 int type, dnsclass, ttl, dlen, status, len;
414 /* Parse the RR name. */
415 status = ares_expand_name(aptr, abuf, alen, &name, &len);
416 if (status != ARES_SUCCESS)
420 /* Make sure there is enough data after the RR name for the fixed
423 if (aptr + RRFIXEDSZ > abuf + alen)
429 /* Parse the fixed part of the RR, and advance to the RR data
431 type = DNS_RR_TYPE(aptr);
432 dnsclass = DNS_RR_CLASS(aptr);
433 ttl = DNS_RR_TTL(aptr);
434 dlen = DNS_RR_LEN(aptr);
436 if (aptr + dlen > abuf + alen)
442 /* Display the RR name, class, and type. */
443 printf("\t%-15s.\t%d", name, ttl);
444 if (dnsclass != C_IN)
445 printf("\t%s", class_name(dnsclass));
446 printf("\t%s", type_name(type));
449 /* Display the RR data. Don't touch aptr. */
460 /* For these types, the RR data is just a domain name. */
461 status = ares_expand_name(aptr, abuf, alen, &name, &len);
462 if (status != ARES_SUCCESS)
464 printf("\t%s.", name);
469 /* The RR data is two length-counted character strings. */
472 if (p + len + 1 > aptr + dlen)
474 printf("\t%.*s", len, p + 1);
477 if (p + len + 1 > aptr + dlen)
479 printf("\t%.*s", len, p + 1);
483 /* The RR data is two domain names. */
485 status = ares_expand_name(p, abuf, alen, &name, &len);
486 if (status != ARES_SUCCESS)
488 printf("\t%s.", name);
491 status = ares_expand_name(p, abuf, alen, &name, &len);
492 if (status != ARES_SUCCESS)
494 printf("\t%s.", name);
499 /* The RR data is two bytes giving a preference ordering, and
500 * then a domain name.
504 printf("\t%d", (aptr[0] << 8) | aptr[1]);
505 status = ares_expand_name(aptr + 2, abuf, alen, &name, &len);
506 if (status != ARES_SUCCESS)
508 printf("\t%s.", name);
513 /* The RR data is two domain names and then five four-byte
514 * numbers giving the serial number and some timeouts.
517 status = ares_expand_name(p, abuf, alen, &name, &len);
518 if (status != ARES_SUCCESS)
520 printf("\t%s.\n", name);
523 status = ares_expand_name(p, abuf, alen, &name, &len);
524 if (status != ARES_SUCCESS)
526 printf("\t\t\t\t\t\t%s.\n", name);
529 if (p + 20 > aptr + dlen)
531 printf("\t\t\t\t\t\t( %d %d %d %d %d )",
532 (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3],
533 (p[4] << 24) | (p[5] << 16) | (p[6] << 8) | p[7],
534 (p[8] << 24) | (p[9] << 16) | (p[10] << 8) | p[11],
535 (p[12] << 24) | (p[13] << 16) | (p[14] << 8) | p[15],
536 (p[16] << 24) | (p[17] << 16) | (p[18] << 8) | p[19]);
540 /* The RR data is one or more length-counted character
543 while (p < aptr + dlen)
546 if (p + len + 1 > aptr + dlen)
548 printf("\t%.*s", len, p + 1);
554 /* The RR data is a four-byte Internet address. */
557 memcpy(&addr, aptr, sizeof(struct in_addr));
558 printf("\t%s", inet_ntoa(addr));
562 /* Not implemented yet */
566 /* The RR data is three two-byte numbers representing the
567 * priority, weight, and port, followed by a domain name.
570 printf("\t%d", DNS__16BIT(aptr));
571 printf(" %d", DNS__16BIT(aptr + 2));
572 printf(" %d", DNS__16BIT(aptr + 4));
574 status = ares_expand_name(aptr + 6, abuf, alen, &name, &len);
575 if (status != ARES_SUCCESS)
577 printf("\t%s.", name);
582 printf("\t[Unknown RR; cannot parse]");
589 static const char *type_name(int type)
593 for (i = 0; i < ntypes; i++)
595 if (types[i].value == type)
596 return types[i].name;
601 static const char *class_name(int dnsclass)
605 for (i = 0; i < nclasses; i++)
607 if (classes[i].value == dnsclass)
608 return classes[i].name;
613 static void usage(void)
615 fprintf(stderr, "usage: adig [-f flag] [-s server] [-c class] "
616 "[-t type] [-p port] name ...\n");