iostat - add read/write details to output
[dragonfly.git] / contrib / bind / bin / dig / nslookup.c
1 /*
2  * Copyright (C) 2004-2009  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2000-2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /* $Id: nslookup.c,v 1.117.130.5 2009/05/06 23:46:49 tbox Exp $ */
19
20 #include <config.h>
21
22 #include <stdlib.h>
23
24 #include <isc/app.h>
25 #include <isc/buffer.h>
26 #include <isc/commandline.h>
27 #include <isc/event.h>
28 #include <isc/parseint.h>
29 #include <isc/print.h>
30 #include <isc/string.h>
31 #include <isc/timer.h>
32 #include <isc/util.h>
33 #include <isc/task.h>
34 #include <isc/netaddr.h>
35
36 #include <dns/message.h>
37 #include <dns/name.h>
38 #include <dns/fixedname.h>
39 #include <dns/rdata.h>
40 #include <dns/rdataclass.h>
41 #include <dns/rdataset.h>
42 #include <dns/rdatastruct.h>
43 #include <dns/rdatatype.h>
44 #include <dns/byaddr.h>
45
46 #include <dig/dig.h>
47
48 static isc_boolean_t short_form = ISC_TRUE,
49         tcpmode = ISC_FALSE,
50         identify = ISC_FALSE, stats = ISC_TRUE,
51         comments = ISC_TRUE, section_question = ISC_TRUE,
52         section_answer = ISC_TRUE, section_authority = ISC_TRUE,
53         section_additional = ISC_TRUE, recurse = ISC_TRUE,
54         aaonly = ISC_FALSE, nofail = ISC_TRUE;
55
56 static isc_boolean_t in_use = ISC_FALSE;
57 static char defclass[MXRD] = "IN";
58 static char deftype[MXRD] = "A";
59 static isc_event_t *global_event = NULL;
60
61 static char domainopt[DNS_NAME_MAXTEXT];
62
63 static const char *rcodetext[] = {
64         "NOERROR",
65         "FORMERR",
66         "SERVFAIL",
67         "NXDOMAIN",
68         "NOTIMP",
69         "REFUSED",
70         "YXDOMAIN",
71         "YXRRSET",
72         "NXRRSET",
73         "NOTAUTH",
74         "NOTZONE",
75         "RESERVED11",
76         "RESERVED12",
77         "RESERVED13",
78         "RESERVED14",
79         "RESERVED15",
80         "BADVERS"
81 };
82
83 static const char *rtypetext[] = {
84         "rtype_0 = ",                   /* 0 */
85         "internet address = ",          /* 1 */
86         "nameserver = ",                /* 2 */
87         "md = ",                        /* 3 */
88         "mf = ",                        /* 4 */
89         "canonical name = ",            /* 5 */
90         "soa = ",                       /* 6 */
91         "mb = ",                        /* 7 */
92         "mg = ",                        /* 8 */
93         "mr = ",                        /* 9 */
94         "rtype_10 = ",                  /* 10 */
95         "protocol = ",                  /* 11 */
96         "name = ",                      /* 12 */
97         "hinfo = ",                     /* 13 */
98         "minfo = ",                     /* 14 */
99         "mail exchanger = ",            /* 15 */
100         "text = ",                      /* 16 */
101         "rp = ",                        /* 17 */
102         "afsdb = ",                     /* 18 */
103         "x25 address = ",               /* 19 */
104         "isdn address = ",              /* 20 */
105         "rt = ",                        /* 21 */
106         "nsap = ",                      /* 22 */
107         "nsap_ptr = ",                  /* 23 */
108         "signature = ",                 /* 24 */
109         "key = ",                       /* 25 */
110         "px = ",                        /* 26 */
111         "gpos = ",                      /* 27 */
112         "has AAAA address ",            /* 28 */
113         "loc = ",                       /* 29 */
114         "next = ",                      /* 30 */
115         "rtype_31 = ",                  /* 31 */
116         "rtype_32 = ",                  /* 32 */
117         "service = ",                   /* 33 */
118         "rtype_34 = ",                  /* 34 */
119         "naptr = ",                     /* 35 */
120         "kx = ",                        /* 36 */
121         "cert = ",                      /* 37 */
122         "v6 address = ",                /* 38 */
123         "dname = ",                     /* 39 */
124         "rtype_40 = ",                  /* 40 */
125         "optional = "                   /* 41 */
126 };
127
128 #define N_KNOWN_RRTYPES (sizeof(rtypetext) / sizeof(rtypetext[0]))
129
130 static void flush_lookup_list(void);
131 static void getinput(isc_task_t *task, isc_event_t *event);
132
133 static char *
134 rcode_totext(dns_rcode_t rcode)
135 {
136         static char buf[sizeof("?65535")];
137         union {
138                 const char *consttext;
139                 char *deconsttext;
140         } totext;
141
142         if (rcode >= (sizeof(rcodetext)/sizeof(rcodetext[0]))) {
143                 snprintf(buf, sizeof(buf), "?%u", rcode);
144                 totext.deconsttext = buf;
145         } else
146                 totext.consttext = rcodetext[rcode];
147         return totext.deconsttext;
148 }
149
150 void
151 dighost_shutdown(void) {
152         isc_event_t *event = global_event;
153
154         flush_lookup_list();
155         debug("dighost_shutdown()");
156
157         if (!in_use) {
158                 isc_app_shutdown();
159                 return;
160         }
161
162         isc_task_send(global_task, &event);
163 }
164
165 static void
166 printsoa(dns_rdata_t *rdata) {
167         dns_rdata_soa_t soa;
168         isc_result_t result;
169         char namebuf[DNS_NAME_FORMATSIZE];
170
171         result = dns_rdata_tostruct(rdata, &soa, NULL);
172         check_result(result, "dns_rdata_tostruct");
173
174         dns_name_format(&soa.origin, namebuf, sizeof(namebuf));
175         printf("\torigin = %s\n", namebuf);
176         dns_name_format(&soa.contact, namebuf, sizeof(namebuf));
177         printf("\tmail addr = %s\n", namebuf);
178         printf("\tserial = %u\n", soa.serial);
179         printf("\trefresh = %u\n", soa.refresh);
180         printf("\tretry = %u\n", soa.retry);
181         printf("\texpire = %u\n", soa.expire);
182         printf("\tminimum = %u\n", soa.minimum);
183         dns_rdata_freestruct(&soa);
184 }
185
186 static void
187 printa(dns_rdata_t *rdata) {
188         isc_result_t result;
189         char text[sizeof("255.255.255.255")];
190         isc_buffer_t b;
191
192         isc_buffer_init(&b, text, sizeof(text));
193         result = dns_rdata_totext(rdata, NULL, &b);
194         check_result(result, "dns_rdata_totext");
195         printf("Address: %.*s\n", (int)isc_buffer_usedlength(&b),
196                (char *)isc_buffer_base(&b));
197 }
198 #ifdef DIG_SIGCHASE
199 /* Just for compatibility : not use in host program */
200 isc_result_t
201 printrdataset(dns_name_t *owner_name, dns_rdataset_t *rdataset,
202               isc_buffer_t *target)
203 {
204         UNUSED(owner_name);
205         UNUSED(rdataset);
206         UNUSED(target);
207         return(ISC_FALSE);
208 }
209 #endif
210 static void
211 printrdata(dns_rdata_t *rdata) {
212         isc_result_t result;
213         isc_buffer_t *b = NULL;
214         unsigned int size = 1024;
215         isc_boolean_t done = ISC_FALSE;
216
217         if (rdata->type < N_KNOWN_RRTYPES)
218                 printf("%s", rtypetext[rdata->type]);
219         else
220                 printf("rdata_%d = ", rdata->type);
221
222         while (!done) {
223                 result = isc_buffer_allocate(mctx, &b, size);
224                 if (result != ISC_R_SUCCESS)
225                         check_result(result, "isc_buffer_allocate");
226                 result = dns_rdata_totext(rdata, NULL, b);
227                 if (result == ISC_R_SUCCESS) {
228                         printf("%.*s\n", (int)isc_buffer_usedlength(b),
229                                (char *)isc_buffer_base(b));
230                         done = ISC_TRUE;
231                 } else if (result != ISC_R_NOSPACE)
232                         check_result(result, "dns_rdata_totext");
233                 isc_buffer_free(&b);
234                 size *= 2;
235         }
236 }
237
238 static isc_result_t
239 printsection(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers,
240              dns_section_t section) {
241         isc_result_t result, loopresult;
242         dns_name_t *name;
243         dns_rdataset_t *rdataset = NULL;
244         dns_rdata_t rdata = DNS_RDATA_INIT;
245         char namebuf[DNS_NAME_FORMATSIZE];
246
247         UNUSED(query);
248         UNUSED(headers);
249
250         debug("printsection()");
251
252         result = dns_message_firstname(msg, section);
253         if (result == ISC_R_NOMORE)
254                 return (ISC_R_SUCCESS);
255         else if (result != ISC_R_SUCCESS)
256                 return (result);
257         for (;;) {
258                 name = NULL;
259                 dns_message_currentname(msg, section,
260                                         &name);
261                 for (rdataset = ISC_LIST_HEAD(name->list);
262                      rdataset != NULL;
263                      rdataset = ISC_LIST_NEXT(rdataset, link)) {
264                         loopresult = dns_rdataset_first(rdataset);
265                         while (loopresult == ISC_R_SUCCESS) {
266                                 dns_rdataset_current(rdataset, &rdata);
267                                 switch (rdata.type) {
268                                 case dns_rdatatype_a:
269                                         if (section != DNS_SECTION_ANSWER)
270                                                 goto def_short_section;
271                                         dns_name_format(name, namebuf,
272                                                         sizeof(namebuf));
273                                         printf("Name:\t%s\n", namebuf);
274                                         printa(&rdata);
275                                         break;
276                                 case dns_rdatatype_soa:
277                                         dns_name_format(name, namebuf,
278                                                         sizeof(namebuf));
279                                         printf("%s\n", namebuf);
280                                         printsoa(&rdata);
281                                         break;
282                                 default:
283                                 def_short_section:
284                                         dns_name_format(name, namebuf,
285                                                         sizeof(namebuf));
286                                         printf("%s\t", namebuf);
287                                         printrdata(&rdata);
288                                         break;
289                                 }
290                                 dns_rdata_reset(&rdata);
291                                 loopresult = dns_rdataset_next(rdataset);
292                         }
293                 }
294                 result = dns_message_nextname(msg, section);
295                 if (result == ISC_R_NOMORE)
296                         break;
297                 else if (result != ISC_R_SUCCESS) {
298                         return (result);
299                 }
300         }
301         return (ISC_R_SUCCESS);
302 }
303
304 static isc_result_t
305 detailsection(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers,
306              dns_section_t section) {
307         isc_result_t result, loopresult;
308         dns_name_t *name;
309         dns_rdataset_t *rdataset = NULL;
310         dns_rdata_t rdata = DNS_RDATA_INIT;
311         char namebuf[DNS_NAME_FORMATSIZE];
312
313         UNUSED(query);
314
315         debug("detailsection()");
316
317         if (headers) {
318                 switch (section) {
319                 case DNS_SECTION_QUESTION:
320                         puts("    QUESTIONS:");
321                         break;
322                 case DNS_SECTION_ANSWER:
323                         puts("    ANSWERS:");
324                         break;
325                 case DNS_SECTION_AUTHORITY:
326                         puts("    AUTHORITY RECORDS:");
327                         break;
328                 case DNS_SECTION_ADDITIONAL:
329                         puts("    ADDITIONAL RECORDS:");
330                         break;
331                 }
332         }
333
334         result = dns_message_firstname(msg, section);
335         if (result == ISC_R_NOMORE)
336                 return (ISC_R_SUCCESS);
337         else if (result != ISC_R_SUCCESS)
338                 return (result);
339         for (;;) {
340                 name = NULL;
341                 dns_message_currentname(msg, section,
342                                         &name);
343                 for (rdataset = ISC_LIST_HEAD(name->list);
344                      rdataset != NULL;
345                      rdataset = ISC_LIST_NEXT(rdataset, link)) {
346                         if (section == DNS_SECTION_QUESTION) {
347                                 dns_name_format(name, namebuf,
348                                                 sizeof(namebuf));
349                                 printf("\t%s, ", namebuf);
350                                 dns_rdatatype_format(rdataset->type,
351                                                      namebuf,
352                                                      sizeof(namebuf));
353                                 printf("type = %s, ", namebuf);
354                                 dns_rdataclass_format(rdataset->rdclass,
355                                                       namebuf,
356                                                       sizeof(namebuf));
357                                 printf("class = %s\n", namebuf);
358                         }
359                         loopresult = dns_rdataset_first(rdataset);
360                         while (loopresult == ISC_R_SUCCESS) {
361                                 dns_rdataset_current(rdataset, &rdata);
362
363                                 dns_name_format(name, namebuf,
364                                                 sizeof(namebuf));
365                                 printf("    ->  %s\n", namebuf);
366
367                                 switch (rdata.type) {
368                                 case dns_rdatatype_soa:
369                                         printsoa(&rdata);
370                                         break;
371                                 default:
372                                         printf("\t");
373                                         printrdata(&rdata);
374                                 }
375                                 dns_rdata_reset(&rdata);
376                                 loopresult = dns_rdataset_next(rdataset);
377                         }
378                 }
379                 result = dns_message_nextname(msg, section);
380                 if (result == ISC_R_NOMORE)
381                         break;
382                 else if (result != ISC_R_SUCCESS) {
383                         return (result);
384                 }
385         }
386         return (ISC_R_SUCCESS);
387 }
388
389 void
390 received(int bytes, isc_sockaddr_t *from, dig_query_t *query)
391 {
392         UNUSED(bytes);
393         UNUSED(from);
394         UNUSED(query);
395 }
396
397 void
398 trying(char *frm, dig_lookup_t *lookup) {
399         UNUSED(frm);
400         UNUSED(lookup);
401
402 }
403
404 isc_result_t
405 printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) {
406         char servtext[ISC_SOCKADDR_FORMATSIZE];
407
408         debug("printmessage()");
409
410         isc_sockaddr_format(&query->sockaddr, servtext, sizeof(servtext));
411         printf("Server:\t\t%s\n", query->userarg);
412         printf("Address:\t%s\n", servtext);
413
414         puts("");
415
416         if (!short_form) {
417                 isc_boolean_t headers = ISC_TRUE;
418                 puts("------------");
419                 /*              detailheader(query, msg);*/
420                 detailsection(query, msg, headers, DNS_SECTION_QUESTION);
421                 detailsection(query, msg, headers, DNS_SECTION_ANSWER);
422                 detailsection(query, msg, headers, DNS_SECTION_AUTHORITY);
423                 detailsection(query, msg, headers, DNS_SECTION_ADDITIONAL);
424                 puts("------------");
425         }
426
427         if (msg->rcode != 0) {
428                 char nametext[DNS_NAME_FORMATSIZE];
429                 dns_name_format(query->lookup->name,
430                                 nametext, sizeof(nametext));
431                 printf("** server can't find %s: %s\n",
432                        (msg->rcode != dns_rcode_nxdomain) ? nametext :
433                        query->lookup->textname, rcode_totext(msg->rcode));
434                 debug("returning with rcode == 0");
435                 return (ISC_R_SUCCESS);
436         }
437
438         if ((msg->flags & DNS_MESSAGEFLAG_AA) == 0)
439                 puts("Non-authoritative answer:");
440         if (!ISC_LIST_EMPTY(msg->sections[DNS_SECTION_ANSWER]))
441                 printsection(query, msg, headers, DNS_SECTION_ANSWER);
442         else
443                 printf("*** Can't find %s: No answer\n",
444                        query->lookup->textname);
445
446         if (((msg->flags & DNS_MESSAGEFLAG_AA) == 0) &&
447             (query->lookup->rdtype != dns_rdatatype_a)) {
448                 puts("\nAuthoritative answers can be found from:");
449                 printsection(query, msg, headers,
450                              DNS_SECTION_AUTHORITY);
451                 printsection(query, msg, headers,
452                              DNS_SECTION_ADDITIONAL);
453         }
454         return (ISC_R_SUCCESS);
455 }
456
457 static void
458 show_settings(isc_boolean_t full, isc_boolean_t serv_only) {
459         dig_server_t *srv;
460         isc_sockaddr_t sockaddr;
461         dig_searchlist_t *listent;
462         isc_result_t result;
463
464         srv = ISC_LIST_HEAD(server_list);
465
466         while (srv != NULL) {
467                 char sockstr[ISC_SOCKADDR_FORMATSIZE];
468
469                 result = get_address(srv->servername, port, &sockaddr);
470                 check_result(result, "get_address");
471
472                 isc_sockaddr_format(&sockaddr, sockstr, sizeof(sockstr));
473                 printf("Default server: %s\nAddress: %s\n",
474                         srv->userarg, sockstr);
475                 if (!full)
476                         return;
477                 srv = ISC_LIST_NEXT(srv, link);
478         }
479         if (serv_only)
480                 return;
481         printf("\nSet options:\n");
482         printf("  %s\t\t\t%s\t\t%s\n",
483                tcpmode ? "vc" : "novc",
484                short_form ? "nodebug" : "debug",
485                debugging ? "d2" : "nod2");
486         printf("  %s\t\t%s\n",
487                usesearch ? "search" : "nosearch",
488                recurse ? "recurse" : "norecurse");
489         printf("  timeout = %d\t\tretry = %d\tport = %d\n",
490                timeout, tries, port);
491         printf("  querytype = %-8s\tclass = %s\n", deftype, defclass);
492         printf("  srchlist = ");
493         for (listent = ISC_LIST_HEAD(search_list);
494              listent != NULL;
495              listent = ISC_LIST_NEXT(listent, link)) {
496                      printf("%s", listent->origin);
497                      if (ISC_LIST_NEXT(listent, link) != NULL)
498                              printf("/");
499         }
500         printf("\n");
501 }
502
503 static isc_boolean_t
504 testtype(char *typetext) {
505         isc_result_t result;
506         isc_textregion_t tr;
507         dns_rdatatype_t rdtype;
508
509         tr.base = typetext;
510         tr.length = strlen(typetext);
511         result = dns_rdatatype_fromtext(&rdtype, &tr);
512         if (result == ISC_R_SUCCESS)
513                 return (ISC_TRUE);
514         else {
515                 printf("unknown query type: %s\n", typetext);
516                 return (ISC_FALSE);
517         }
518 }
519
520 static isc_boolean_t
521 testclass(char *typetext) {
522         isc_result_t result;
523         isc_textregion_t tr;
524         dns_rdataclass_t rdclass;
525
526         tr.base = typetext;
527         tr.length = strlen(typetext);
528         result = dns_rdataclass_fromtext(&rdclass, &tr);
529         if (result == ISC_R_SUCCESS)
530                 return (ISC_TRUE);
531         else {
532                 printf("unknown query class: %s\n", typetext);
533                 return (ISC_FALSE);
534         }
535 }
536
537 static void
538 safecpy(char *dest, char *src, int size) {
539         strncpy(dest, src, size);
540         dest[size-1] = 0;
541 }
542
543 static isc_result_t
544 parse_uint(isc_uint32_t *uip, const char *value, isc_uint32_t max,
545            const char *desc) {
546         isc_uint32_t n;
547         isc_result_t result = isc_parse_uint32(&n, value, 10);
548         if (result == ISC_R_SUCCESS && n > max)
549                 result = ISC_R_RANGE;
550         if (result != ISC_R_SUCCESS) {
551                 printf("invalid %s '%s': %s\n", desc,
552                        value, isc_result_totext(result));
553                 return result;
554         }
555         *uip = n;
556         return (ISC_R_SUCCESS);
557 }
558
559 static void
560 set_port(const char *value) {
561         isc_uint32_t n;
562         isc_result_t result = parse_uint(&n, value, 65535, "port");
563         if (result == ISC_R_SUCCESS)
564                 port = (isc_uint16_t) n;
565 }
566
567 static void
568 set_timeout(const char *value) {
569         isc_uint32_t n;
570         isc_result_t result = parse_uint(&n, value, UINT_MAX, "timeout");
571         if (result == ISC_R_SUCCESS)
572                 timeout = n;
573 }
574
575 static void
576 set_tries(const char *value) {
577         isc_uint32_t n;
578         isc_result_t result = parse_uint(&n, value, INT_MAX, "tries");
579         if (result == ISC_R_SUCCESS)
580                 tries = n;
581 }
582
583 static void
584 setoption(char *opt) {
585         if (strncasecmp(opt, "all", 4) == 0) {
586                 show_settings(ISC_TRUE, ISC_FALSE);
587         } else if (strncasecmp(opt, "class=", 6) == 0) {
588                 if (testclass(&opt[6]))
589                         safecpy(defclass, &opt[6], sizeof(defclass));
590         } else if (strncasecmp(opt, "cl=", 3) == 0) {
591                 if (testclass(&opt[3]))
592                         safecpy(defclass, &opt[3], sizeof(defclass));
593         } else if (strncasecmp(opt, "type=", 5) == 0) {
594                 if (testtype(&opt[5]))
595                         safecpy(deftype, &opt[5], sizeof(deftype));
596         } else if (strncasecmp(opt, "ty=", 3) == 0) {
597                 if (testtype(&opt[3]))
598                         safecpy(deftype, &opt[3], sizeof(deftype));
599         } else if (strncasecmp(opt, "querytype=", 10) == 0) {
600                 if (testtype(&opt[10]))
601                         safecpy(deftype, &opt[10], sizeof(deftype));
602         } else if (strncasecmp(opt, "query=", 6) == 0) {
603                 if (testtype(&opt[6]))
604                         safecpy(deftype, &opt[6], sizeof(deftype));
605         } else if (strncasecmp(opt, "qu=", 3) == 0) {
606                 if (testtype(&opt[3]))
607                         safecpy(deftype, &opt[3], sizeof(deftype));
608         } else if (strncasecmp(opt, "q=", 2) == 0) {
609                 if (testtype(&opt[2]))
610                         safecpy(deftype, &opt[2], sizeof(deftype));
611         } else if (strncasecmp(opt, "domain=", 7) == 0) {
612                 safecpy(domainopt, &opt[7], sizeof(domainopt));
613                 set_search_domain(domainopt);
614                 usesearch = ISC_TRUE;
615         } else if (strncasecmp(opt, "do=", 3) == 0) {
616                 safecpy(domainopt, &opt[3], sizeof(domainopt));
617                 set_search_domain(domainopt);
618                 usesearch = ISC_TRUE;
619         } else if (strncasecmp(opt, "port=", 5) == 0) {
620                 set_port(&opt[5]);
621         } else if (strncasecmp(opt, "po=", 3) == 0) {
622                 set_port(&opt[3]);
623         } else if (strncasecmp(opt, "timeout=", 8) == 0) {
624                 set_timeout(&opt[8]);
625         } else if (strncasecmp(opt, "t=", 2) == 0) {
626                 set_timeout(&opt[2]);
627         } else if (strncasecmp(opt, "rec", 3) == 0) {
628                 recurse = ISC_TRUE;
629         } else if (strncasecmp(opt, "norec", 5) == 0) {
630                 recurse = ISC_FALSE;
631         } else if (strncasecmp(opt, "retry=", 6) == 0) {
632                 set_tries(&opt[6]);
633         } else if (strncasecmp(opt, "ret=", 4) == 0) {
634                 set_tries(&opt[4]);
635         } else if (strncasecmp(opt, "def", 3) == 0) {
636                 usesearch = ISC_TRUE;
637         } else if (strncasecmp(opt, "nodef", 5) == 0) {
638                 usesearch = ISC_FALSE;
639         } else if (strncasecmp(opt, "vc", 3) == 0) {
640                 tcpmode = ISC_TRUE;
641         } else if (strncasecmp(opt, "novc", 5) == 0) {
642                 tcpmode = ISC_FALSE;
643         } else if (strncasecmp(opt, "deb", 3) == 0) {
644                 short_form = ISC_FALSE;
645                 showsearch = ISC_TRUE;
646         } else if (strncasecmp(opt, "nodeb", 5) == 0) {
647                 short_form = ISC_TRUE;
648                 showsearch = ISC_FALSE;
649         } else if (strncasecmp(opt, "d2", 2) == 0) {
650                 debugging = ISC_TRUE;
651         } else if (strncasecmp(opt, "nod2", 4) == 0) {
652                 debugging = ISC_FALSE;
653         } else if (strncasecmp(opt, "search", 3) == 0) {
654                 usesearch = ISC_TRUE;
655         } else if (strncasecmp(opt, "nosearch", 5) == 0) {
656                 usesearch = ISC_FALSE;
657         } else if (strncasecmp(opt, "sil", 3) == 0) {
658                 /* deprecation_msg = ISC_FALSE; */
659         } else if (strncasecmp(opt, "fail", 3) == 0) {
660                 nofail=ISC_FALSE;
661         } else if (strncasecmp(opt, "nofail", 3) == 0) {
662                 nofail=ISC_TRUE;
663         } else {
664                 printf("*** Invalid option: %s\n", opt);
665         }
666 }
667
668 static void
669 addlookup(char *opt) {
670         dig_lookup_t *lookup;
671         isc_result_t result;
672         isc_textregion_t tr;
673         dns_rdatatype_t rdtype;
674         dns_rdataclass_t rdclass;
675         char store[MXNAME];
676
677         debug("addlookup()");
678         tr.base = deftype;
679         tr.length = strlen(deftype);
680         result = dns_rdatatype_fromtext(&rdtype, &tr);
681         if (result != ISC_R_SUCCESS) {
682                 printf("unknown query type: %s\n", deftype);
683                 rdclass = dns_rdatatype_a;
684         }
685         tr.base = defclass;
686         tr.length = strlen(defclass);
687         result = dns_rdataclass_fromtext(&rdclass, &tr);
688         if (result != ISC_R_SUCCESS) {
689                 printf("unknown query class: %s\n", defclass);
690                 rdclass = dns_rdataclass_in;
691         }
692         lookup = make_empty_lookup();
693         if (get_reverse(store, sizeof(store), opt, lookup->ip6_int, ISC_TRUE)
694             == ISC_R_SUCCESS) {
695                 safecpy(lookup->textname, store, sizeof(lookup->textname));
696                 lookup->rdtype = dns_rdatatype_ptr;
697                 lookup->rdtypeset = ISC_TRUE;
698         } else {
699                 safecpy(lookup->textname, opt, sizeof(lookup->textname));
700                 lookup->rdtype = rdtype;
701                 lookup->rdtypeset = ISC_TRUE;
702         }
703         lookup->rdclass = rdclass;
704         lookup->rdclassset = ISC_TRUE;
705         lookup->trace = ISC_FALSE;
706         lookup->trace_root = lookup->trace;
707         lookup->ns_search_only = ISC_FALSE;
708         lookup->identify = identify;
709         lookup->recurse = recurse;
710         lookup->aaonly = aaonly;
711         lookup->retries = tries;
712         lookup->udpsize = 0;
713         lookup->comments = comments;
714         lookup->tcp_mode = tcpmode;
715         lookup->stats = stats;
716         lookup->section_question = section_question;
717         lookup->section_answer = section_answer;
718         lookup->section_authority = section_authority;
719         lookup->section_additional = section_additional;
720         lookup->new_search = ISC_TRUE;
721         if (nofail)
722                 lookup->servfail_stops = ISC_FALSE;
723         ISC_LIST_INIT(lookup->q);
724         ISC_LINK_INIT(lookup, link);
725         ISC_LIST_APPEND(lookup_list, lookup, link);
726         lookup->origin = NULL;
727         ISC_LIST_INIT(lookup->my_server_list);
728         debug("looking up %s", lookup->textname);
729 }
730
731 static void
732 get_next_command(void) {
733         char *buf;
734         char *ptr, *arg;
735         char *input;
736
737         fflush(stdout);
738         buf = isc_mem_allocate(mctx, COMMSIZE);
739         if (buf == NULL)
740                 fatal("memory allocation failure");
741         fputs("> ", stderr);
742         fflush(stderr);
743         isc_app_block();
744         ptr = fgets(buf, COMMSIZE, stdin);
745         isc_app_unblock();
746         if (ptr == NULL) {
747                 in_use = ISC_FALSE;
748                 goto cleanup;
749         }
750         input = buf;
751         ptr = next_token(&input, " \t\r\n");
752         if (ptr == NULL)
753                 goto cleanup;
754         arg = next_token(&input, " \t\r\n");
755         if ((strcasecmp(ptr, "set") == 0) &&
756             (arg != NULL))
757                 setoption(arg);
758         else if ((strcasecmp(ptr, "server") == 0) ||
759                  (strcasecmp(ptr, "lserver") == 0)) {
760                 isc_app_block();
761                 set_nameserver(arg);
762                 check_ra = ISC_FALSE;
763                 isc_app_unblock();
764                 show_settings(ISC_TRUE, ISC_TRUE);
765         } else if (strcasecmp(ptr, "exit") == 0) {
766                 in_use = ISC_FALSE;
767                 goto cleanup;
768         } else if (strcasecmp(ptr, "help") == 0 ||
769                    strcasecmp(ptr, "?") == 0) {
770                 printf("The '%s' command is not yet implemented.\n", ptr);
771                 goto cleanup;
772         } else if (strcasecmp(ptr, "finger") == 0 ||
773                    strcasecmp(ptr, "root") == 0 ||
774                    strcasecmp(ptr, "ls") == 0 ||
775                    strcasecmp(ptr, "view") == 0) {
776                 printf("The '%s' command is not implemented.\n", ptr);
777                 goto cleanup;
778         } else
779                 addlookup(ptr);
780  cleanup:
781         isc_mem_free(mctx, buf);
782 }
783
784 static void
785 parse_args(int argc, char **argv) {
786         isc_boolean_t have_lookup = ISC_FALSE;
787
788         usesearch = ISC_TRUE;
789         for (argc--, argv++; argc > 0; argc--, argv++) {
790                 debug("main parsing %s", argv[0]);
791                 if (argv[0][0] == '-') {
792                         if (argv[0][1] != 0)
793                                 setoption(&argv[0][1]);
794                         else
795                                 have_lookup = ISC_TRUE;
796                 } else {
797                         if (!have_lookup) {
798                                 have_lookup = ISC_TRUE;
799                                 in_use = ISC_TRUE;
800                                 addlookup(argv[0]);
801                         } else {
802                                 set_nameserver(argv[0]);
803                                 check_ra = ISC_FALSE;
804                         }
805                 }
806         }
807 }
808
809 static void
810 flush_lookup_list(void) {
811         dig_lookup_t *l, *lp;
812         dig_query_t *q, *qp;
813         dig_server_t *s, *sp;
814
815         lookup_counter = 0;
816         l = ISC_LIST_HEAD(lookup_list);
817         while (l != NULL) {
818                 q = ISC_LIST_HEAD(l->q);
819                 while (q != NULL) {
820                         if (q->sock != NULL) {
821                                 isc_socket_cancel(q->sock, NULL,
822                                                   ISC_SOCKCANCEL_ALL);
823                                 isc_socket_detach(&q->sock);
824                         }
825                         if (ISC_LINK_LINKED(&q->recvbuf, link))
826                                 ISC_LIST_DEQUEUE(q->recvlist, &q->recvbuf,
827                                                  link);
828                         if (ISC_LINK_LINKED(&q->lengthbuf, link))
829                                 ISC_LIST_DEQUEUE(q->lengthlist, &q->lengthbuf,
830                                                  link);
831                         isc_buffer_invalidate(&q->recvbuf);
832                         isc_buffer_invalidate(&q->lengthbuf);
833                         qp = q;
834                         q = ISC_LIST_NEXT(q, link);
835                         ISC_LIST_DEQUEUE(l->q, qp, link);
836                         isc_mem_free(mctx, qp);
837                 }
838                 s = ISC_LIST_HEAD(l->my_server_list);
839                 while (s != NULL) {
840                         sp = s;
841                         s = ISC_LIST_NEXT(s, link);
842                         ISC_LIST_DEQUEUE(l->my_server_list, sp, link);
843                         isc_mem_free(mctx, sp);
844
845                 }
846                 if (l->sendmsg != NULL)
847                         dns_message_destroy(&l->sendmsg);
848                 if (l->timer != NULL)
849                         isc_timer_detach(&l->timer);
850                 lp = l;
851                 l = ISC_LIST_NEXT(l, link);
852                 ISC_LIST_DEQUEUE(lookup_list, lp, link);
853                 isc_mem_free(mctx, lp);
854         }
855 }
856
857 static void
858 getinput(isc_task_t *task, isc_event_t *event) {
859         UNUSED(task);
860         if (global_event == NULL)
861                 global_event = event;
862         while (in_use) {
863                 get_next_command();
864                 if (ISC_LIST_HEAD(lookup_list) != NULL) {
865                         start_lookup();
866                         return;
867                 }
868         }
869         isc_app_shutdown();
870 }
871
872 int
873 main(int argc, char **argv) {
874         isc_result_t result;
875
876         ISC_LIST_INIT(lookup_list);
877         ISC_LIST_INIT(server_list);
878         ISC_LIST_INIT(search_list);
879
880         check_ra = ISC_TRUE;
881
882         result = isc_app_start();
883         check_result(result, "isc_app_start");
884
885         setup_libs();
886         progname = argv[0];
887
888         parse_args(argc, argv);
889
890         setup_system();
891         if (domainopt[0] != '\0')
892                 set_search_domain(domainopt);
893         if (in_use)
894                 result = isc_app_onrun(mctx, global_task, onrun_callback,
895                                        NULL);
896         else
897                 result = isc_app_onrun(mctx, global_task, getinput, NULL);
898         check_result(result, "isc_app_onrun");
899         in_use = ISC_TF(!in_use);
900
901         (void)isc_app_run();
902
903         puts("");
904         debug("done, and starting to shut down");
905         if (global_event != NULL)
906                 isc_event_free(&global_event);
907         cancel_all();
908         destroy_libs();
909         isc_app_finish();
910
911         return (0);
912 }