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 static const char rcsid[] = "$Id: adig.c,v 1.9 2001/05/18 20:59:51 ghudson Exp $";
18 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23 #include <arpa/nameser.h>
35 #define INADDR_NONE 0xffffffff
46 static const struct nv flags[] = {
47 { "usevc", ARES_FLAG_USEVC },
48 { "primary", ARES_FLAG_PRIMARY },
49 { "igntc", ARES_FLAG_IGNTC },
50 { "norecurse", ARES_FLAG_NORECURSE },
51 { "stayopen", ARES_FLAG_STAYOPEN },
52 { "noaliases", ARES_FLAG_NOALIASES }
54 static const int nflags = sizeof(flags) / sizeof(flags[0]);
56 static const struct nv classes[] = {
62 static const int nclasses = sizeof(classes) / sizeof(classes[0]);
64 static const struct nv types[] = {
87 { "NSAP_PTR", T_NSAP_PTR },
100 static const int ntypes = sizeof(types) / sizeof(types[0]);
102 static const char *opcodes[] = {
103 "QUERY", "IQUERY", "STATUS", "(reserved)", "NOTIFY",
104 "(unknown)", "(unknown)", "(unknown)", "(unknown)",
105 "UPDATEA", "UPDATED", "UPDATEDA", "UPDATEM", "UPDATEMA",
106 "ZONEINIT", "ZONEREF"
109 static const char *rcodes[] = {
110 "NOERROR", "FORMERR", "SERVFAIL", "NXDOMAIN", "NOTIMP", "REFUSED",
111 "(unknown)", "(unknown)", "(unknown)", "(unknown)", "(unknown)",
112 "(unknown)", "(unknown)", "(unknown)", "(unknown)", "NOCHANGE"
115 static void callback(void *arg, int status, unsigned char *abuf, int alen);
116 static const unsigned char *display_question(const unsigned char *aptr,
117 const unsigned char *abuf,
119 static const unsigned char *display_rr(const unsigned char *aptr,
120 const unsigned char *abuf, int alen);
121 static const char *type_name(int type);
122 static const char *class_name(int dnsclass);
123 static void usage(void);
125 int main(int argc, char **argv)
127 ares_channel channel;
128 int c, i, optmask = ARES_OPT_FLAGS, dnsclass = C_IN, type = T_A;
129 int status, nfds, count;
130 struct ares_options options;
131 struct hostent *hostent;
132 fd_set read_fds, write_fds;
133 struct timeval *tvp, tv;
136 options.flags = ARES_FLAG_NOCHECKRESP;
137 options.servers = NULL;
138 options.nservers = 0;
139 while ((c = getopt(argc, argv, "f:s:c:t:T:U:")) != -1)
145 for (i = 0; i < nflags; i++)
147 if (strcmp(flags[i].name, optarg) == 0)
152 options.flags |= flags[i].value;
156 /* Add a server, and specify servers in the option mask. */
157 hostent = gethostbyname(optarg);
158 if (!hostent || hostent->h_addrtype != AF_INET)
160 fprintf(stderr, "adig: server %s not found.\n", optarg);
163 options.servers = realloc(options.servers, (options.nservers + 1)
164 * sizeof(struct in_addr));
165 if (!options.servers)
167 fprintf(stderr, "Out of memory!\n");
170 memcpy(&options.servers[options.nservers], hostent->h_addr,
171 sizeof(struct in_addr));
173 optmask |= ARES_OPT_SERVERS;
177 /* Set the query class. */
178 for (i = 0; i < nclasses; i++)
180 if (strcasecmp(classes[i].name, optarg) == 0)
185 dnsclass = classes[i].value;
189 /* Set the query type. */
190 for (i = 0; i < ntypes; i++)
192 if (strcasecmp(types[i].name, optarg) == 0)
197 type = types[i].value;
201 /* Set the TCP port number. */
202 if (!isdigit((unsigned char)*optarg))
204 options.tcp_port = strtol(optarg, NULL, 0);
205 optmask |= ARES_OPT_TCP_PORT;
209 /* Set the UDP port number. */
210 if (!isdigit((unsigned char)*optarg))
212 options.udp_port = strtol(optarg, NULL, 0);
213 optmask |= ARES_OPT_UDP_PORT;
222 status = ares_init_options(&channel, &options, optmask);
223 if (status != ARES_SUCCESS)
225 fprintf(stderr, "ares_init_options: %s\n",
226 ares_strerror(status, &errmem));
227 ares_free_errmem(errmem);
231 /* Initiate the queries, one per command-line argument. If there is
232 * only one query to do, supply NULL as the callback argument;
233 * otherwise, supply the query name as an argument so we can
234 * distinguish responses for the user when printing them out.
237 ares_query(channel, *argv, dnsclass, type, callback, (char *) NULL);
240 for (; *argv; argv++)
241 ares_query(channel, *argv, dnsclass, type, callback, *argv);
244 /* Wait for all queries to complete. */
249 nfds = ares_fds(channel, &read_fds, &write_fds);
252 tvp = ares_timeout(channel, NULL, &tv);
253 count = select(nfds, &read_fds, &write_fds, NULL, tvp);
254 if (count < 0 && errno != EINVAL)
259 ares_process(channel, &read_fds, &write_fds);
262 ares_destroy(channel);
266 static void callback(void *arg, int status, unsigned char *abuf, int alen)
268 char *name = (char *) arg, *errmem;
269 int id, qr, opcode, aa, tc, rd, ra, rcode, i;
270 unsigned int qdcount, ancount, nscount, arcount;
271 const unsigned char *aptr;
273 /* Display the query name if given. */
275 printf("Answer for query %s:\n", name);
277 /* Display an error message if there was an error, but only stop if
278 * we actually didn't get an answer buffer.
280 if (status != ARES_SUCCESS)
282 printf("%s\n", ares_strerror(status, &errmem));
283 ares_free_errmem(errmem);
288 /* Won't happen, but check anyway, for safety. */
292 /* Parse the answer header. */
293 id = DNS_HEADER_QID(abuf);
294 qr = DNS_HEADER_QR(abuf);
295 opcode = DNS_HEADER_OPCODE(abuf);
296 aa = DNS_HEADER_AA(abuf);
297 tc = DNS_HEADER_TC(abuf);
298 rd = DNS_HEADER_RD(abuf);
299 ra = DNS_HEADER_RA(abuf);
300 rcode = DNS_HEADER_RCODE(abuf);
301 qdcount = DNS_HEADER_QDCOUNT(abuf);
302 ancount = DNS_HEADER_ANCOUNT(abuf);
303 nscount = DNS_HEADER_NSCOUNT(abuf);
304 arcount = DNS_HEADER_ARCOUNT(abuf);
306 /* Display the answer header. */
307 printf("id: %d\n", id);
308 printf("flags: %s%s%s%s%s\n",
314 printf("opcode: %s\n", opcodes[opcode]);
315 printf("rcode: %s\n", rcodes[rcode]);
317 /* Display the questions. */
318 printf("Questions:\n");
319 aptr = abuf + HFIXEDSZ;
320 for (i = 0; i < qdcount; i++)
322 aptr = display_question(aptr, abuf, alen);
327 /* Display the answers. */
328 printf("Answers:\n");
329 for (i = 0; i < ancount; i++)
331 aptr = display_rr(aptr, abuf, alen);
336 /* Display the NS records. */
337 printf("NS records:\n");
338 for (i = 0; i < nscount; i++)
340 aptr = display_rr(aptr, abuf, alen);
345 /* Display the additional records. */
346 printf("Additional records:\n");
347 for (i = 0; i < arcount; i++)
349 aptr = display_rr(aptr, abuf, alen);
355 static const unsigned char *display_question(const unsigned char *aptr,
356 const unsigned char *abuf,
360 int type, dnsclass, status, len;
362 /* Parse the question name. */
363 status = ares_expand_name(aptr, abuf, alen, &name, &len);
364 if (status != ARES_SUCCESS)
368 /* Make sure there's enough data after the name for the fixed part
371 if (aptr + QFIXEDSZ > abuf + alen)
377 /* Parse the question type and class. */
378 type = DNS_QUESTION_TYPE(aptr);
379 dnsclass = DNS_QUESTION_CLASS(aptr);
382 /* Display the question, in a format sort of similar to how we will
385 printf("\t%-15s.\t", name);
386 if (dnsclass != C_IN)
387 printf("\t%s", class_name(dnsclass));
388 printf("\t%s\n", type_name(type));
393 static const unsigned char *display_rr(const unsigned char *aptr,
394 const unsigned char *abuf, int alen)
396 const unsigned char *p;
398 int type, dnsclass, ttl, dlen, status, len;
401 /* Parse the RR name. */
402 status = ares_expand_name(aptr, abuf, alen, &name, &len);
403 if (status != ARES_SUCCESS)
407 /* Make sure there is enough data after the RR name for the fixed
410 if (aptr + RRFIXEDSZ > abuf + alen)
416 /* Parse the fixed part of the RR, and advance to the RR data
418 type = DNS_RR_TYPE(aptr);
419 dnsclass = DNS_RR_CLASS(aptr);
420 ttl = DNS_RR_TTL(aptr);
421 dlen = DNS_RR_LEN(aptr);
423 if (aptr + dlen > abuf + alen)
429 /* Display the RR name, class, and type. */
430 printf("\t%-15s.\t%d", name, ttl);
431 if (dnsclass != C_IN)
432 printf("\t%s", class_name(dnsclass));
433 printf("\t%s", type_name(type));
436 /* Display the RR data. Don't touch aptr. */
447 /* For these types, the RR data is just a domain name. */
448 status = ares_expand_name(aptr, abuf, alen, &name, &len);
449 if (status != ARES_SUCCESS)
451 printf("\t%s.", name);
456 /* The RR data is two length-counted character strings. */
459 if (p + len + 1 > aptr + dlen)
461 printf("\t%.*s", len, p + 1);
464 if (p + len + 1 > aptr + dlen)
466 printf("\t%.*s", len, p + 1);
470 /* The RR data is two domain names. */
472 status = ares_expand_name(p, abuf, alen, &name, &len);
473 if (status != ARES_SUCCESS)
475 printf("\t%s.", name);
478 status = ares_expand_name(p, abuf, alen, &name, &len);
479 if (status != ARES_SUCCESS)
481 printf("\t%s.", name);
486 /* The RR data is two bytes giving a preference ordering, and
487 * then a domain name.
491 printf("\t%d", (aptr[0] << 8) | aptr[1]);
492 status = ares_expand_name(aptr + 2, abuf, alen, &name, &len);
493 if (status != ARES_SUCCESS)
495 printf("\t%s.", name);
500 /* The RR data is two domain names and then five four-byte
501 * numbers giving the serial number and some timeouts.
504 status = ares_expand_name(p, abuf, alen, &name, &len);
505 if (status != ARES_SUCCESS)
507 printf("\t%s.\n", name);
510 status = ares_expand_name(p, abuf, alen, &name, &len);
511 if (status != ARES_SUCCESS)
513 printf("\t\t\t\t\t\t%s.\n", name);
516 if (p + 20 > aptr + dlen)
518 printf("\t\t\t\t\t\t( %d %d %d %d %d )",
519 (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3],
520 (p[4] << 24) | (p[5] << 16) | (p[6] << 8) | p[7],
521 (p[8] << 24) | (p[9] << 16) | (p[10] << 8) | p[11],
522 (p[12] << 24) | (p[13] << 16) | (p[14] << 8) | p[15],
523 (p[16] << 24) | (p[17] << 16) | (p[18] << 8) | p[19]);
527 /* The RR data is one or more length-counted character
530 while (p < aptr + dlen)
533 if (p + len + 1 > aptr + dlen)
535 printf("\t%.*s", len, p + 1);
541 /* The RR data is a four-byte Internet address. */
544 memcpy(&addr, aptr, sizeof(struct in_addr));
545 printf("\t%s", inet_ntoa(addr));
549 /* Not implemented yet */
553 /* The RR data is three two-byte numbers representing the
554 * priority, weight, and port, followed by a domain name.
557 printf("\t%d", DNS__16BIT(aptr));
558 printf(" %d", DNS__16BIT(aptr + 2));
559 printf(" %d", DNS__16BIT(aptr + 4));
561 status = ares_expand_name(aptr + 6, abuf, alen, &name, &len);
562 if (status != ARES_SUCCESS)
564 printf("\t%s.", name);
569 printf("\t[Unknown RR; cannot parse]");
576 static const char *type_name(int type)
580 for (i = 0; i < ntypes; i++)
582 if (types[i].value == type)
583 return types[i].name;
588 static const char *class_name(int dnsclass)
592 for (i = 0; i < nclasses; i++)
594 if (classes[i].value == dnsclass)
595 return classes[i].name;
600 static void usage(void)
602 fprintf(stderr, "usage: adig [-f flag] [-s server] [-c class] "
603 "[-t type] [-p port] name ...\n");