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