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.6 1999/10/23 19:28:12 danw 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 },
97 { "UNSPEC", T_UNSPEC },
100 { "MAILA", T_MAILA },
103 static const int ntypes = sizeof(types) / sizeof(types[0]);
105 static const char *opcodes[] = {
106 "QUERY", "IQUERY", "STATUS", "(reserved)", "NOTIFY",
107 "(unknown)", "(unknown)", "(unknown)", "(unknown)",
108 "UPDATEA", "UPDATED", "UPDATEDA", "UPDATEM", "UPDATEMA",
109 "ZONEINIT", "ZONEREF"
112 static const char *rcodes[] = {
113 "NOERROR", "FORMERR", "SERVFAIL", "NXDOMAIN", "NOTIMP", "REFUSED",
114 "(unknown)", "(unknown)", "(unknown)", "(unknown)", "(unknown)",
115 "(unknown)", "(unknown)", "(unknown)", "(unknown)", "NOCHANGE"
118 static void callback(void *arg, int status, unsigned char *abuf, int alen);
119 static const unsigned char *display_question(const unsigned char *aptr,
120 const unsigned char *abuf,
122 static const unsigned char *display_rr(const unsigned char *aptr,
123 const unsigned char *abuf, int alen);
124 static const char *type_name(int type);
125 static const char *class_name(int class);
126 static void usage(void);
128 int main(int argc, char **argv)
130 ares_channel channel;
131 int c, i, optmask = ARES_OPT_FLAGS, class = C_IN, type = T_A;
132 int status, nfds, count;
133 struct ares_options options;
134 struct hostent *hostent;
135 fd_set read_fds, write_fds;
136 struct timeval *tvp, tv;
139 options.flags = ARES_FLAG_NOCHECKRESP;
140 options.servers = NULL;
141 options.nservers = 0;
142 while ((c = getopt(argc, argv, "f:s:c:t:T:U:")) != -1)
148 for (i = 0; i < nflags; i++)
150 if (strcmp(flags[i].name, optarg) == 0)
155 options.flags |= flags[i].value;
159 /* Add a server, and specify servers in the option mask. */
160 hostent = gethostbyname(optarg);
161 if (!hostent || hostent->h_addrtype != AF_INET)
163 fprintf(stderr, "adig: server %s not found.\n", optarg);
166 options.servers = realloc(options.servers, (options.nservers + 1)
167 * sizeof(struct in_addr));
168 if (!options.servers)
170 fprintf(stderr, "Out of memory!\n");
173 memcpy(&options.servers[options.nservers], hostent->h_addr,
174 sizeof(struct in_addr));
176 optmask |= ARES_OPT_SERVERS;
180 /* Set the query class. */
181 for (i = 0; i < nclasses; i++)
183 if (strcasecmp(classes[i].name, optarg) == 0)
188 class = classes[i].value;
192 /* Set the query type. */
193 for (i = 0; i < ntypes; i++)
195 if (strcasecmp(types[i].name, optarg) == 0)
200 type = types[i].value;
204 /* Set the TCP port number. */
205 if (!isdigit((unsigned char)*optarg))
207 options.tcp_port = strtol(optarg, NULL, 0);
208 optmask |= ARES_OPT_TCP_PORT;
212 /* Set the UDP port number. */
213 if (!isdigit((unsigned char)*optarg))
215 options.udp_port = strtol(optarg, NULL, 0);
216 optmask |= ARES_OPT_UDP_PORT;
225 status = ares_init_options(&channel, &options, optmask);
226 if (status != ARES_SUCCESS)
228 fprintf(stderr, "ares_init_options: %s\n",
229 ares_strerror(status, &errmem));
230 ares_free_errmem(errmem);
234 /* Initiate the queries, one per command-line argument. If there is
235 * only one query to do, supply NULL as the callback argument;
236 * otherwise, supply the query name as an argument so we can
237 * distinguish responses for the user when printing them out.
240 ares_query(channel, *argv, class, type, callback, (char *) NULL);
243 for (; *argv; argv++)
244 ares_query(channel, *argv, class, type, callback, *argv);
247 /* Wait for all queries to complete. */
252 nfds = ares_fds(channel, &read_fds, &write_fds);
255 tvp = ares_timeout(channel, NULL, &tv);
256 count = select(nfds, &read_fds, &write_fds, NULL, tvp);
257 if (count < 0 && errno != EINVAL)
262 ares_process(channel, &read_fds, &write_fds);
265 ares_destroy(channel);
269 static void callback(void *arg, int status, unsigned char *abuf, int alen)
271 char *name = (char *) arg, *errmem;
272 int id, qr, opcode, aa, tc, rd, ra, rcode, i;
273 unsigned int qdcount, ancount, nscount, arcount;
274 const unsigned char *aptr;
276 /* Display the query name if given. */
278 printf("Answer for query %s:\n", name);
280 /* Display an error message if there was an error, but only stop if
281 * we actually didn't get an answer buffer.
283 if (status != ARES_SUCCESS)
285 printf("%s\n", ares_strerror(status, &errmem));
286 ares_free_errmem(errmem);
291 /* Won't happen, but check anyway, for safety. */
295 /* Parse the answer header. */
296 id = DNS_HEADER_QID(abuf);
297 qr = DNS_HEADER_QR(abuf);
298 opcode = DNS_HEADER_OPCODE(abuf);
299 aa = DNS_HEADER_AA(abuf);
300 tc = DNS_HEADER_TC(abuf);
301 rd = DNS_HEADER_RD(abuf);
302 ra = DNS_HEADER_RA(abuf);
303 rcode = DNS_HEADER_RCODE(abuf);
304 qdcount = DNS_HEADER_QDCOUNT(abuf);
305 ancount = DNS_HEADER_ANCOUNT(abuf);
306 nscount = DNS_HEADER_NSCOUNT(abuf);
307 arcount = DNS_HEADER_ARCOUNT(abuf);
309 /* Display the answer header. */
310 printf("id: %d\n", id);
311 printf("flags: %s%s%s%s%s\n",
317 printf("opcode: %s\n", opcodes[opcode]);
318 printf("rcode: %s\n", rcodes[rcode]);
320 /* Display the questions. */
321 printf("Questions:\n");
322 aptr = abuf + HFIXEDSZ;
323 for (i = 0; i < qdcount; i++)
325 aptr = display_question(aptr, abuf, alen);
330 /* Display the answers. */
331 printf("Answers:\n");
332 for (i = 0; i < ancount; i++)
334 aptr = display_rr(aptr, abuf, alen);
339 /* Display the NS records. */
340 printf("NS records:\n");
341 for (i = 0; i < nscount; i++)
343 aptr = display_rr(aptr, abuf, alen);
348 /* Display the additional records. */
349 printf("Additional records:\n");
350 for (i = 0; i < arcount; i++)
352 aptr = display_rr(aptr, abuf, alen);
358 static const unsigned char *display_question(const unsigned char *aptr,
359 const unsigned char *abuf,
363 int type, class, status, len;
365 /* Parse the question name. */
366 status = ares_expand_name(aptr, abuf, alen, &name, &len);
367 if (status != ARES_SUCCESS)
371 /* Make sure there's enough data after the name for the fixed part
374 if (aptr + QFIXEDSZ > abuf + alen)
380 /* Parse the question type and class. */
381 type = DNS_QUESTION_TYPE(aptr);
382 class = DNS_QUESTION_CLASS(aptr);
385 /* Display the question, in a format sort of similar to how we will
388 printf("\t%-15s.\t", name);
390 printf("\t%s", class_name(class));
391 printf("\t%s\n", type_name(type));
396 static const unsigned char *display_rr(const unsigned char *aptr,
397 const unsigned char *abuf, int alen)
399 const unsigned char *p;
401 int type, class, ttl, dlen, status, len;
404 /* Parse the RR name. */
405 status = ares_expand_name(aptr, abuf, alen, &name, &len);
406 if (status != ARES_SUCCESS)
410 /* Make sure there is enough data after the RR name for the fixed
413 if (aptr + RRFIXEDSZ > abuf + alen)
419 /* Parse the fixed part of the RR, and advance to the RR data
421 type = DNS_RR_TYPE(aptr);
422 class = DNS_RR_CLASS(aptr);
423 ttl = DNS_RR_TTL(aptr);
424 dlen = DNS_RR_LEN(aptr);
426 if (aptr + dlen > abuf + alen)
432 /* Display the RR name, class, and type. */
433 printf("\t%-15s.\t%d", name, ttl);
435 printf("\t%s", class_name(class));
436 printf("\t%s", type_name(type));
439 /* Display the RR data. Don't touch aptr. */
450 /* For these types, the RR data is just a domain name. */
451 status = ares_expand_name(aptr, abuf, alen, &name, &len);
452 if (status != ARES_SUCCESS)
454 printf("\t%s.", name);
459 /* The RR data is two length-counted character strings. */
462 if (p + len + 1 > aptr + dlen)
464 printf("\t%.*s", len, p + 1);
467 if (p + len + 1 > aptr + dlen)
469 printf("\t%.*s", len, p + 1);
473 /* The RR data is two domain names. */
475 status = ares_expand_name(p, abuf, alen, &name, &len);
476 if (status != ARES_SUCCESS)
478 printf("\t%s.", name);
481 status = ares_expand_name(p, abuf, alen, &name, &len);
482 if (status != ARES_SUCCESS)
484 printf("\t%s.", name);
489 /* The RR data is two bytes giving a preference ordering, and
490 * then a domain name.
494 printf("\t%d", (aptr[0] << 8) | aptr[1]);
495 status = ares_expand_name(aptr + 2, abuf, alen, &name, &len);
496 if (status != ARES_SUCCESS)
498 printf("\t%s.", name);
503 /* The RR data is two domain names and then five four-byte
504 * numbers giving the serial number and some timeouts.
507 status = ares_expand_name(p, abuf, alen, &name, &len);
508 if (status != ARES_SUCCESS)
510 printf("\t%s.\n", name);
513 status = ares_expand_name(p, abuf, alen, &name, &len);
514 if (status != ARES_SUCCESS)
516 printf("\t\t\t\t\t\t%s.\n", name);
519 if (p + 20 > aptr + dlen)
521 printf("\t\t\t\t\t\t( %d %d %d %d %d )",
522 (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3],
523 (p[4] << 24) | (p[5] << 16) | (p[6] << 8) | p[7],
524 (p[8] << 24) | (p[9] << 16) | (p[10] << 8) | p[11],
525 (p[12] << 24) | (p[13] << 16) | (p[14] << 8) | p[15],
526 (p[16] << 24) | (p[17] << 16) | (p[18] << 8) | p[19]);
530 /* The RR data is one or more length-counted character
533 while (p < aptr + dlen)
536 if (p + len + 1 > aptr + dlen)
538 printf("\t%.*s", len, p + 1);
544 /* The RR data is a four-byte Internet address. */
547 memcpy(&addr, aptr, sizeof(struct in_addr));
548 printf("\t%s", inet_ntoa(addr));
552 /* Not implemented yet */
560 static const char *type_name(int type)
564 for (i = 0; i < ntypes; i++)
566 if (types[i].value == type)
567 return types[i].name;
572 static const char *class_name(int class)
576 for (i = 0; i < nclasses; i++)
578 if (classes[i].value == class)
579 return classes[i].name;
584 static void usage(void)
586 fprintf(stderr, "usage: adig [-f flag] [-s server] [-c class] "
587 "[-t type] [-p port] name ...\n");