cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer/*
d63ae51ba337165172a9b50fd5d84805ffe50044Tinderbox User * Copyright (C) 2000-2017 Internet Systems Consortium, Inc. ("ISC")
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer */
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#include <config.h>
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#include <stdlib.h>
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt#include <unistd.h>
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
c9defbf4b968e8a61f391246431ac63d0d6a39abMichael Sawyer#include <isc/app.h>
c9defbf4b968e8a61f391246431ac63d0d6a39abMichael Sawyer#include <isc/buffer.h>
c9defbf4b968e8a61f391246431ac63d0d6a39abMichael Sawyer#include <isc/commandline.h>
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington#include <isc/event.h>
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson#include <isc/parseint.h>
0e77d336861c519c0c7da88620038ad95b2105efFrancis Dupont#include <isc/print.h>
c9defbf4b968e8a61f391246431ac63d0d6a39abMichael Sawyer#include <isc/string.h>
c9defbf4b968e8a61f391246431ac63d0d6a39abMichael Sawyer#include <isc/util.h>
1893b56ef9f5f2bc2a0fbe80d3c6b69df1bdc7c2Michael Sawyer#include <isc/task.h>
55bf97a1b4b7f78b669b1179df1422cb9789c484Michael Sawyer#include <isc/netaddr.h>
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer#include <dns/message.h>
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer#include <dns/name.h>
55bf97a1b4b7f78b669b1179df1422cb9789c484Michael Sawyer#include <dns/fixedname.h>
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer#include <dns/rdata.h>
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer#include <dns/rdataclass.h>
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer#include <dns/rdataset.h>
7ddb4c86a95ab721a70d406821352ce7b730a1bdAndreas Gustafsson#include <dns/rdatastruct.h>
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer#include <dns/rdatatype.h>
55bf97a1b4b7f78b669b1179df1422cb9789c484Michael Sawyer#include <dns/byaddr.h>
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer#include <dig/dig.h>
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt#if defined(HAVE_READLINE)
294ef74e5ad68d898207c4fb36d8b18d526a11f6Curtis Blackburn#if defined(HAVE_EDIT_READLINE_READLINE_H)
294ef74e5ad68d898207c4fb36d8b18d526a11f6Curtis Blackburn#include <edit/readline/readline.h>
294ef74e5ad68d898207c4fb36d8b18d526a11f6Curtis Blackburn#if defined(HAVE_EDIT_READLINE_HISTORY_H)
294ef74e5ad68d898207c4fb36d8b18d526a11f6Curtis Blackburn#include <edit/readline/history.h>
294ef74e5ad68d898207c4fb36d8b18d526a11f6Curtis Blackburn#endif
294ef74e5ad68d898207c4fb36d8b18d526a11f6Curtis Blackburn#elif defined(HAVE_EDITLINE_READLINE_H)
2a1860ad83294da4abe34a72bdb6f5a28b87f2efMark Andrews#include <editline/readline.h>
2a1860ad83294da4abe34a72bdb6f5a28b87f2efMark Andrews#elif defined(HAVE_READLINE_READLINE_H)
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt#include <readline/readline.h>
2a1860ad83294da4abe34a72bdb6f5a28b87f2efMark Andrews#if defined (HAVE_READLINE_HISTORY_H)
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt#include <readline/history.h>
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt#endif
2a1860ad83294da4abe34a72bdb6f5a28b87f2efMark Andrews#endif
2a1860ad83294da4abe34a72bdb6f5a28b87f2efMark Andrews#endif
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt
0e0e575ec135a983a53c501cf48734b823361ab4Brian Wellingtonstatic isc_boolean_t short_form = ISC_TRUE,
3001a1b4e6298466882a4147307b4c28a3b08f91Mark Andrews tcpmode = ISC_FALSE, tcpmode_set = ISC_FALSE,
0e0e575ec135a983a53c501cf48734b823361ab4Brian Wellington identify = ISC_FALSE, stats = ISC_TRUE,
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer comments = ISC_TRUE, section_question = ISC_TRUE,
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer section_answer = ISC_TRUE, section_authority = ISC_TRUE,
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer section_additional = ISC_TRUE, recurse = ISC_TRUE,
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews aaonly = ISC_FALSE, nofail = ISC_TRUE,
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews default_lookups = ISC_TRUE, a_noanswer = ISC_FALSE;
3d711f2f75cb9a9ddcbf1fca9b2de192e75340e6Mark Andrews
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Huntstatic isc_boolean_t interactive;
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt
0e0e575ec135a983a53c501cf48734b823361ab4Brian Wellingtonstatic isc_boolean_t in_use = ISC_FALSE;
0e0e575ec135a983a53c501cf48734b823361ab4Brian Wellingtonstatic char defclass[MXRD] = "IN";
0e0e575ec135a983a53c501cf48734b823361ab4Brian Wellingtonstatic char deftype[MXRD] = "A";
0e0e575ec135a983a53c501cf48734b823361ab4Brian Wellingtonstatic isc_event_t *global_event = NULL;
04e5f9812cb02d5d86b542dfa9a394f074ff5621Evan Huntstatic int query_error = 1, print_error = 0;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
0e0e575ec135a983a53c501cf48734b823361ab4Brian Wellingtonstatic char domainopt[DNS_NAME_MAXTEXT];
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson
a5ed46c9fd270775c39770bfd0250a52d374ebf2Michael Sawyerstatic const char *rcodetext[] = {
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer "NOERROR",
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer "FORMERR",
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer "SERVFAIL",
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer "NXDOMAIN",
da5d1cf1b1aa29ae53a0427be49291b04bd60549Mark Andrews "NOTIMP",
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer "REFUSED",
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer "YXDOMAIN",
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer "YXRRSET",
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer "NXRRSET",
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer "NOTAUTH",
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer "NOTZONE",
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer "RESERVED11",
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer "RESERVED12",
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer "RESERVED13",
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer "RESERVED14",
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer "RESERVED15",
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer "BADVERS"
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer};
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
a5ed46c9fd270775c39770bfd0250a52d374ebf2Michael Sawyerstatic const char *rtypetext[] = {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer "rtype_0 = ", /* 0 */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer "internet address = ", /* 1 */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer "nameserver = ", /* 2 */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer "md = ", /* 3 */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer "mf = ", /* 4 */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer "canonical name = ", /* 5 */
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington "soa = ", /* 6 */
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington "mb = ", /* 7 */
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington "mg = ", /* 8 */
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington "mr = ", /* 9 */
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington "rtype_10 = ", /* 10 */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer "protocol = ", /* 11 */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer "name = ", /* 12 */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer "hinfo = ", /* 13 */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer "minfo = ", /* 14 */
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington "mail exchanger = ", /* 15 */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer "text = ", /* 16 */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer "rp = ", /* 17 */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer "afsdb = ", /* 18 */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer "x25 address = ", /* 19 */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer "isdn address = ", /* 20 */
e32394a2ac3466a2235f79ee32c247a11be42a8dAndreas Gustafsson "rt = ", /* 21 */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer "nsap = ", /* 22 */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer "nsap_ptr = ", /* 23 */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer "signature = ", /* 24 */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer "key = ", /* 25 */
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington "px = ", /* 26 */
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington "gpos = ", /* 27 */
36c754f274dd439e5a7ebf2401427c1d51b21302Brian Wellington "has AAAA address ", /* 28 */
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington "loc = ", /* 29 */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer "next = ", /* 30 */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer "rtype_31 = ", /* 31 */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer "rtype_32 = ", /* 32 */
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington "service = ", /* 33 */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer "rtype_34 = ", /* 34 */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer "naptr = ", /* 35 */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer "kx = ", /* 36 */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer "cert = ", /* 37 */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer "v6 address = ", /* 38 */
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer "dname = ", /* 39 */
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington "rtype_40 = ", /* 40 */
9a762177e6a0aff3ea5c00f87568c8ae3cd0e6c7Andreas Gustafsson "optional = " /* 41 */
9a762177e6a0aff3ea5c00f87568c8ae3cd0e6c7Andreas Gustafsson};
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
066cb385cd8f801dd3757db2922c9b1f343e65edAndreas Gustafsson#define N_KNOWN_RRTYPES (sizeof(rtypetext) / sizeof(rtypetext[0]))
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellingtonstatic void flush_lookup_list(void);
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellingtonstatic void getinput(isc_task_t *task, isc_event_t *event);
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupontstatic char *
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupontrcode_totext(dns_rcode_t rcode)
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupont{
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupont static char buf[sizeof("?65535")];
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupont union {
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupont const char *consttext;
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupont char *deconsttext;
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupont } totext;
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupont
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupont if (rcode >= (sizeof(rcodetext)/sizeof(rcodetext[0]))) {
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupont snprintf(buf, sizeof(buf), "?%u", rcode);
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupont totext.deconsttext = buf;
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupont } else
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupont totext.consttext = rcodetext[rcode];
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupont return totext.deconsttext;
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupont}
d302a620e0d49811874b9555ac2e4c6e05861a6bFrancis Dupont
2b0060b8ff20eab8b7b1c42a9b9ccedb85047887Evan Huntstatic void
2b0060b8ff20eab8b7b1c42a9b9ccedb85047887Evan Huntquery_finished(void) {
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington isc_event_t *event = global_event;
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington flush_lookup_list();
cc48bb397fa6ba889f25157840492e68114dec8fBrian Wellington debug("dighost_shutdown()");
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington if (!in_use) {
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington isc_app_shutdown();
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington return;
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington }
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington isc_task_send(global_task, &event);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer}
cc48bb397fa6ba889f25157840492e68114dec8fBrian Wellington
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellingtonstatic void
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellingtonprintsoa(dns_rdata_t *rdata) {
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington dns_rdata_soa_t soa;
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington isc_result_t result;
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington char namebuf[DNS_NAME_FORMATSIZE];
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington result = dns_rdata_tostruct(rdata, &soa, NULL);
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington check_result(result, "dns_rdata_tostruct");
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington dns_name_format(&soa.origin, namebuf, sizeof(namebuf));
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington printf("\torigin = %s\n", namebuf);
c38b92000c0f1a95daaad5468777e165b8047de9Mark Andrews dns_name_format(&soa.contact, namebuf, sizeof(namebuf));
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington printf("\tmail addr = %s\n", namebuf);
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington printf("\tserial = %u\n", soa.serial);
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington printf("\trefresh = %u\n", soa.refresh);
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington printf("\tretry = %u\n", soa.retry);
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington printf("\texpire = %u\n", soa.expire);
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington printf("\tminimum = %u\n", soa.minimum);
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington dns_rdata_freestruct(&soa);
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington}
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellingtonstatic void
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrewsprintaddr(dns_rdata_t *rdata) {
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington isc_result_t result;
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews char text[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington isc_buffer_t b;
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington isc_buffer_init(&b, text, sizeof(text));
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington result = dns_rdata_totext(rdata, NULL, &b);
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington check_result(result, "dns_rdata_totext");
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington printf("Address: %.*s\n", (int)isc_buffer_usedlength(&b),
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington (char *)isc_buffer_base(&b));
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington}
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews
2a23a625246acfbf6ff92b86a6b8b9df59dbeaa4Mark Andrews#ifdef DIG_SIGCHASE
1ae75c1024eb0475c2be352b8707772e16332ad0Mark Andrews/* Just for compatibility : not use in host program */
2b0060b8ff20eab8b7b1c42a9b9ccedb85047887Evan Huntstatic isc_result_t
1ae75c1024eb0475c2be352b8707772e16332ad0Mark Andrewsprintrdataset(dns_name_t *owner_name, dns_rdataset_t *rdataset,
1ae75c1024eb0475c2be352b8707772e16332ad0Mark Andrews isc_buffer_t *target)
1ae75c1024eb0475c2be352b8707772e16332ad0Mark Andrews{
1ae75c1024eb0475c2be352b8707772e16332ad0Mark Andrews UNUSED(owner_name);
1ae75c1024eb0475c2be352b8707772e16332ad0Mark Andrews UNUSED(rdataset);
1ae75c1024eb0475c2be352b8707772e16332ad0Mark Andrews UNUSED(target);
1ae75c1024eb0475c2be352b8707772e16332ad0Mark Andrews return(ISC_FALSE);
1ae75c1024eb0475c2be352b8707772e16332ad0Mark Andrews}
1ae75c1024eb0475c2be352b8707772e16332ad0Mark Andrews#endif
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellingtonstatic void
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellingtonprintrdata(dns_rdata_t *rdata) {
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington isc_result_t result;
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington isc_buffer_t *b = NULL;
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington unsigned int size = 1024;
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington isc_boolean_t done = ISC_FALSE;
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington if (rdata->type < N_KNOWN_RRTYPES)
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington printf("%s", rtypetext[rdata->type]);
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington else
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington printf("rdata_%d = ", rdata->type);
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington while (!done) {
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington result = isc_buffer_allocate(mctx, &b, size);
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington if (result != ISC_R_SUCCESS)
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington check_result(result, "isc_buffer_allocate");
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington result = dns_rdata_totext(rdata, NULL, b);
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington if (result == ISC_R_SUCCESS) {
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington printf("%.*s\n", (int)isc_buffer_usedlength(b),
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington (char *)isc_buffer_base(b));
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington done = ISC_TRUE;
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington } else if (result != ISC_R_NOSPACE)
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington check_result(result, "dns_rdata_totext");
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington isc_buffer_free(&b);
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington size *= 2;
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington }
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington}
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyerstatic isc_result_t
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerprintsection(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers,
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer dns_section_t section) {
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer isc_result_t result, loopresult;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer dns_name_t *name;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer dns_rdataset_t *rdataset = NULL;
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews dns_rdata_t rdata = DNS_RDATA_INIT;
7ddb4c86a95ab721a70d406821352ce7b730a1bdAndreas Gustafsson char namebuf[DNS_NAME_FORMATSIZE];
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson UNUSED(query);
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson UNUSED(headers);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer debug("printsection()");
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer result = dns_message_firstname(msg, section);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer if (result == ISC_R_NOMORE)
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer return (ISC_R_SUCCESS);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer else if (result != ISC_R_SUCCESS)
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer return (result);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer for (;;) {
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer name = NULL;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence dns_message_currentname(msg, section,
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer &name);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer for (rdataset = ISC_LIST_HEAD(name->list);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer rdataset != NULL;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer rdataset = ISC_LIST_NEXT(rdataset, link)) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer loopresult = dns_rdataset_first(rdataset);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer while (loopresult == ISC_R_SUCCESS) {
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson dns_rdataset_current(rdataset, &rdata);
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson switch (rdata.type) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer case dns_rdatatype_a:
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews case dns_rdatatype_aaaa:
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (section != DNS_SECTION_ANSWER)
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer goto def_short_section;
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson dns_name_format(name, namebuf,
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson sizeof(namebuf));
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington printf("Name:\t%s\n", namebuf);
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews printaddr(&rdata);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer break;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer case dns_rdatatype_soa:
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson dns_name_format(name, namebuf,
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson sizeof(namebuf));
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson printf("%s\n", namebuf);
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington printsoa(&rdata);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer break;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer default:
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer def_short_section:
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson dns_name_format(name, namebuf,
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson sizeof(namebuf));
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington printf("%s\t", namebuf);
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington printrdata(&rdata);
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington break;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence }
368b37b616234fce3d23099eb180f1dd38e1fb62Mark Andrews dns_rdata_reset(&rdata);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer loopresult = dns_rdataset_next(rdataset);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer }
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer }
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer result = dns_message_nextname(msg, section);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer if (result == ISC_R_NOMORE)
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer break;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer else if (result != ISC_R_SUCCESS) {
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer return (result);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer }
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer }
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer return (ISC_R_SUCCESS);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer}
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyerstatic isc_result_t
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyerdetailsection(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers,
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer dns_section_t section) {
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer isc_result_t result, loopresult;
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer dns_name_t *name;
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer dns_rdataset_t *rdataset = NULL;
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews dns_rdata_t rdata = DNS_RDATA_INIT;
7ddb4c86a95ab721a70d406821352ce7b730a1bdAndreas Gustafsson char namebuf[DNS_NAME_FORMATSIZE];
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson UNUSED(query);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer
38cf6e52ce4b33795713388824b69d78e430b115Michael Sawyer debug("detailsection()");
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer if (headers) {
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer switch (section) {
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer case DNS_SECTION_QUESTION:
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson puts(" QUESTIONS:");
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer break;
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer case DNS_SECTION_ANSWER:
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson puts(" ANSWERS:");
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer break;
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer case DNS_SECTION_AUTHORITY:
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson puts(" AUTHORITY RECORDS:");
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer break;
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer case DNS_SECTION_ADDITIONAL:
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson puts(" ADDITIONAL RECORDS:");
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer break;
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer }
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer }
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer result = dns_message_firstname(msg, section);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer if (result == ISC_R_NOMORE)
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer return (ISC_R_SUCCESS);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer else if (result != ISC_R_SUCCESS)
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer return (result);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer for (;;) {
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer name = NULL;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence dns_message_currentname(msg, section,
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer &name);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer for (rdataset = ISC_LIST_HEAD(name->list);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer rdataset != NULL;
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer rdataset = ISC_LIST_NEXT(rdataset, link)) {
ee4429e13e08f30c366cdc5d10585388b8a9f212Michael Sawyer if (section == DNS_SECTION_QUESTION) {
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson dns_name_format(name, namebuf,
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson sizeof(namebuf));
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson printf("\t%s, ", namebuf);
ee4429e13e08f30c366cdc5d10585388b8a9f212Michael Sawyer dns_rdatatype_format(rdataset->type,
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson namebuf,
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson sizeof(namebuf));
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson printf("type = %s, ", namebuf);
ee4429e13e08f30c366cdc5d10585388b8a9f212Michael Sawyer dns_rdataclass_format(rdataset->rdclass,
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson namebuf,
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson sizeof(namebuf));
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson printf("class = %s\n", namebuf);
ee4429e13e08f30c366cdc5d10585388b8a9f212Michael Sawyer }
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer loopresult = dns_rdataset_first(rdataset);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer while (loopresult == ISC_R_SUCCESS) {
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer dns_rdataset_current(rdataset, &rdata);
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson dns_name_format(name, namebuf,
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson sizeof(namebuf));
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson printf(" -> %s\n", namebuf);
93988fb202645e5d06b337b8e3e20765a467149dAndreas Gustafsson
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer switch (rdata.type) {
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer case dns_rdatatype_soa:
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington printsoa(&rdata);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer break;
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer default:
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington printf("\t");
e56101fa6876c876d9957c23784b6493cdb05e09Brian Wellington printrdata(&rdata);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence }
368b37b616234fce3d23099eb180f1dd38e1fb62Mark Andrews dns_rdata_reset(&rdata);
4b30598fb908755c4fd04f51cf3ce1f550434bf3Mark Andrews printf("\tttl = %u\n", rdataset->ttl);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer loopresult = dns_rdataset_next(rdataset);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer }
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer }
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer result = dns_message_nextname(msg, section);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer if (result == ISC_R_NOMORE)
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer break;
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer else if (result != ISC_R_SUCCESS) {
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer return (result);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer }
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer }
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer return (ISC_R_SUCCESS);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer}
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer
2b0060b8ff20eab8b7b1c42a9b9ccedb85047887Evan Huntstatic void
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafssonreceived(int bytes, isc_sockaddr_t *from, dig_query_t *query)
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson{
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson UNUSED(bytes);
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson UNUSED(from);
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson UNUSED(query);
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson}
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson
2b0060b8ff20eab8b7b1c42a9b9ccedb85047887Evan Huntstatic void
f9521c63ea42fee93f7ae67512e4330ab13081c8Brian Wellingtontrying(char *frm, dig_lookup_t *lookup) {
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson UNUSED(frm);
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson UNUSED(lookup);
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson}
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrewsstatic void
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrewschase_cnamechain(dns_message_t *msg, dns_name_t *qname) {
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews isc_result_t result;
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews dns_rdataset_t *rdataset;
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews dns_rdata_cname_t cname;
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews dns_rdata_t rdata = DNS_RDATA_INIT;
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews unsigned int i = msg->counts[DNS_SECTION_ANSWER];
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews while (i-- > 0) {
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews rdataset = NULL;
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews result = dns_message_findname(msg, DNS_SECTION_ANSWER, qname,
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews dns_rdatatype_cname, 0, NULL, &rdataset);
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews if (result != ISC_R_SUCCESS)
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews return;
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews result = dns_rdataset_first(rdataset);
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews check_result(result, "dns_rdataset_first");
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews dns_rdata_reset(&rdata);
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews dns_rdataset_current(rdataset, &rdata);
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews result = dns_rdata_tostruct(&rdata, &cname, NULL);
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews check_result(result, "dns_rdata_tostruct");
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews dns_name_copy(&cname.cname, qname, NULL);
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews dns_rdata_freestruct(&cname);
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews }
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews}
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews
2b0060b8ff20eab8b7b1c42a9b9ccedb85047887Evan Huntstatic isc_result_t
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyerprintmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) {
96eeb9496c09114c116132d5a493ef5eb88e3192Automatic Updater char servtext[ISC_SOCKADDR_FORMATSIZE];
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
04e5f9812cb02d5d86b542dfa9a394f074ff5621Evan Hunt /* I've we've gotten this far, we've reached a server. */
04e5f9812cb02d5d86b542dfa9a394f074ff5621Evan Hunt query_error = 0;
04e5f9812cb02d5d86b542dfa9a394f074ff5621Evan Hunt
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson debug("printmessage()");
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews if(!default_lookups || query->lookup->rdtype == dns_rdatatype_a) {
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews isc_sockaddr_format(&query->sockaddr, servtext, sizeof(servtext));
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews printf("Server:\t\t%s\n", query->userarg);
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews printf("Address:\t%s\n", servtext);
96eeb9496c09114c116132d5a493ef5eb88e3192Automatic Updater
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews puts("");
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews }
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
20b6f1e657d049d9fdb251a0846465bff9b95948Andreas Gustafsson if (!short_form) {
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson puts("------------");
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer /* detailheader(query, msg);*/
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt detailsection(query, msg, ISC_TRUE, DNS_SECTION_QUESTION);
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt detailsection(query, msg, ISC_TRUE, DNS_SECTION_ANSWER);
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt detailsection(query, msg, ISC_TRUE, DNS_SECTION_AUTHORITY);
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt detailsection(query, msg, ISC_TRUE, DNS_SECTION_ADDITIONAL);
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson puts("------------");
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer }
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson if (msg->rcode != 0) {
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson char nametext[DNS_NAME_FORMATSIZE];
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson dns_name_format(query->lookup->name,
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson nametext, sizeof(nametext));
aaa42824d2d1f5cc4add01f0191e3784ee0f7396Mark Andrews printf("** server can't find %s: %s\n",
8afea636ab0c07399aa3e2410b2cfbd41099df98Mark Andrews nametext, rcode_totext(msg->rcode));
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson debug("returning with rcode == 0");
04e5f9812cb02d5d86b542dfa9a394f074ff5621Evan Hunt
04e5f9812cb02d5d86b542dfa9a394f074ff5621Evan Hunt /* the lookup failed */
04e5f9812cb02d5d86b542dfa9a394f074ff5621Evan Hunt print_error |= 1;
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson return (ISC_R_SUCCESS);
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson }
b4876b6ddf706977153954507b498baf79a23442Andreas Gustafsson
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews if ( default_lookups && query->lookup->rdtype == dns_rdatatype_a) {
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews char namestr[DNS_NAME_FORMATSIZE];
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews dig_lookup_t *lookup;
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews dns_fixedname_t fixed;
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews dns_name_t *name;
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews /* Add AAAA lookup. */
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews dns_fixedname_init(&fixed);
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews name = dns_fixedname_name(&fixed);
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews dns_name_copy(query->lookup->name, name, NULL);
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews chase_cnamechain(msg, name);
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews dns_name_format(name, namestr, sizeof(namestr));
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews lookup = clone_lookup(query->lookup, ISC_FALSE);
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews if (lookup != NULL) {
5a93d3be4e0c652f455066bb764416173a00c951Mark Andrews strlcpy(lookup->textname, namestr,
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews sizeof(lookup->textname));
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews lookup->rdtype = dns_rdatatype_aaaa;
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews lookup->rdtypeset = ISC_TRUE;
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews lookup->origin = NULL;
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews lookup->retries = tries;
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews ISC_LIST_APPEND(lookup_list, lookup, link);
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews }
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews }
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews if ((msg->flags & DNS_MESSAGEFLAG_AA) == 0 &&
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews ( !default_lookups || query->lookup->rdtype == dns_rdatatype_a) )
7804502532d6e2eee80083431a22eb5d957f52a0Michael Sawyer puts("Non-authoritative answer:");
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer if (!ISC_LIST_EMPTY(msg->sections[DNS_SECTION_ANSWER]))
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer printsection(query, msg, headers, DNS_SECTION_ANSWER);
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews else {
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews if (default_lookups && query->lookup->rdtype == dns_rdatatype_a)
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews a_noanswer = ISC_TRUE;
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews else if (!default_lookups ||
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews (query->lookup->rdtype == dns_rdatatype_aaaa &&
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews a_noanswer ) )
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews printf("*** Can't find %s: No answer\n",
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews query->lookup->textname);
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews }
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (((msg->flags & DNS_MESSAGEFLAG_AA) == 0) &&
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews (query->lookup->rdtype != dns_rdatatype_a) &&
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews (query->lookup->rdtype != dns_rdatatype_aaaa) ) {
7804502532d6e2eee80083431a22eb5d957f52a0Michael Sawyer puts("\nAuthoritative answers can be found from:");
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer printsection(query, msg, headers,
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer DNS_SECTION_AUTHORITY);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer printsection(query, msg, headers,
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer DNS_SECTION_ADDITIONAL);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer }
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer return (ISC_R_SUCCESS);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer}
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyerstatic void
f900be21902d02418c2c71ffed754fb3f9f54cffMichael Sawyershow_settings(isc_boolean_t full, isc_boolean_t serv_only) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer dig_server_t *srv;
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer isc_sockaddr_t sockaddr;
ee4429e13e08f30c366cdc5d10585388b8a9f212Michael Sawyer dig_searchlist_t *listent;
7f658603910358db7ee27ffb9783096250afab62Tatuya JINMEI 神明達哉 isc_result_t result;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer srv = ISC_LIST_HEAD(server_list);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer while (srv != NULL) {
46e349e515e0b992ecafdc9597f9d5ee0cf81b2aBrian Wellington char sockstr[ISC_SOCKADDR_FORMATSIZE];
46e349e515e0b992ecafdc9597f9d5ee0cf81b2aBrian Wellington
7f658603910358db7ee27ffb9783096250afab62Tatuya JINMEI 神明達哉 result = get_address(srv->servername, port, &sockaddr);
7f658603910358db7ee27ffb9783096250afab62Tatuya JINMEI 神明達哉 check_result(result, "get_address");
7f658603910358db7ee27ffb9783096250afab62Tatuya JINMEI 神明達哉
46e349e515e0b992ecafdc9597f9d5ee0cf81b2aBrian Wellington isc_sockaddr_format(&sockaddr, sockstr, sizeof(sockstr));
46e349e515e0b992ecafdc9597f9d5ee0cf81b2aBrian Wellington printf("Default server: %s\nAddress: %s\n",
3f79a8b6f1b50f8e1bc2f274bcfe91d6af74f161Mark Andrews srv->userarg, sockstr);
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer if (!full)
844eaa56d6d647b38b2a5cf08f7ea5ab7b752690Michael Sawyer return;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer srv = ISC_LIST_NEXT(srv, link);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer }
f900be21902d02418c2c71ffed754fb3f9f54cffMichael Sawyer if (serv_only)
f900be21902d02418c2c71ffed754fb3f9f54cffMichael Sawyer return;
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson printf("\nSet options:\n");
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson printf(" %s\t\t\t%s\t\t%s\n",
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson tcpmode ? "vc" : "novc",
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson short_form ? "nodebug" : "debug",
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson debugging ? "d2" : "nod2");
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson printf(" %s\t\t%s\n",
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson usesearch ? "search" : "nosearch",
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson recurse ? "recurse" : "norecurse");
8afea636ab0c07399aa3e2410b2cfbd41099df98Mark Andrews printf(" timeout = %d\t\tretry = %d\tport = %d\tndots = %d\n",
8afea636ab0c07399aa3e2410b2cfbd41099df98Mark Andrews timeout, tries, port, ndots);
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson printf(" querytype = %-8s\tclass = %s\n", deftype, defclass);
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson printf(" srchlist = ");
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson for (listent = ISC_LIST_HEAD(search_list);
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson listent != NULL;
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson listent = ISC_LIST_NEXT(listent, link)) {
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson printf("%s", listent->origin);
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson if (ISC_LIST_NEXT(listent, link) != NULL)
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson printf("/");
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson }
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson printf("\n");
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer}
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyerstatic isc_boolean_t
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyertesttype(char *typetext) {
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer isc_result_t result;
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer isc_textregion_t tr;
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer dns_rdatatype_t rdtype;
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer tr.base = typetext;
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer tr.length = strlen(typetext);
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer result = dns_rdatatype_fromtext(&rdtype, &tr);
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer if (result == ISC_R_SUCCESS)
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer return (ISC_TRUE);
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer else {
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer printf("unknown query type: %s\n", typetext);
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer return (ISC_FALSE);
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer }
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer}
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyerstatic isc_boolean_t
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyertestclass(char *typetext) {
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer isc_result_t result;
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer isc_textregion_t tr;
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer dns_rdataclass_t rdclass;
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer tr.base = typetext;
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer tr.length = strlen(typetext);
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer result = dns_rdataclass_fromtext(&rdclass, &tr);
96eeb9496c09114c116132d5a493ef5eb88e3192Automatic Updater if (result == ISC_R_SUCCESS)
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer return (ISC_TRUE);
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer else {
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer printf("unknown query class: %s\n", typetext);
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer return (ISC_FALSE);
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer }
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer}
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafssonstatic void
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafssonset_port(const char *value) {
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson isc_uint32_t n;
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson isc_result_t result = parse_uint(&n, value, 65535, "port");
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson if (result == ISC_R_SUCCESS)
8ef03ac2743f73f7ce3c92d739c64d31387abb45Danny Mayer port = (isc_uint16_t) n;
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson}
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafssonstatic void
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafssonset_timeout(const char *value) {
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson isc_uint32_t n;
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson isc_result_t result = parse_uint(&n, value, UINT_MAX, "timeout");
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson if (result == ISC_R_SUCCESS)
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson timeout = n;
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson}
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafssonstatic void
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafssonset_tries(const char *value) {
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson isc_uint32_t n;
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson isc_result_t result = parse_uint(&n, value, INT_MAX, "tries");
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson if (result == ISC_R_SUCCESS)
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson tries = n;
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson}
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer
8afea636ab0c07399aa3e2410b2cfbd41099df98Mark Andrewsstatic void
8afea636ab0c07399aa3e2410b2cfbd41099df98Mark Andrewsset_ndots(const char *value) {
8afea636ab0c07399aa3e2410b2cfbd41099df98Mark Andrews isc_uint32_t n;
8afea636ab0c07399aa3e2410b2cfbd41099df98Mark Andrews isc_result_t result = parse_uint(&n, value, 128, "ndots");
8afea636ab0c07399aa3e2410b2cfbd41099df98Mark Andrews if (result == ISC_R_SUCCESS)
8afea636ab0c07399aa3e2410b2cfbd41099df98Mark Andrews ndots = n;
8afea636ab0c07399aa3e2410b2cfbd41099df98Mark Andrews}
8afea636ab0c07399aa3e2410b2cfbd41099df98Mark Andrews
67d01dcacb2051a03377c8ec5c0e36604c17aea5Evan Huntstatic void
67d01dcacb2051a03377c8ec5c0e36604c17aea5Evan Huntversion(void) {
67d01dcacb2051a03377c8ec5c0e36604c17aea5Evan Hunt fputs("nslookup " VERSION "\n", stderr);
67d01dcacb2051a03377c8ec5c0e36604c17aea5Evan Hunt}
67d01dcacb2051a03377c8ec5c0e36604c17aea5Evan Hunt
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyerstatic void
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyersetoption(char *opt) {
c76e8412f4ff4f5945157410312df2a8950f942dMark Andrews size_t l = strlen(opt);
c76e8412f4ff4f5945157410312df2a8950f942dMark Andrews
c76e8412f4ff4f5945157410312df2a8950f942dMark Andrews#define CHECKOPT(A, N) \
c76e8412f4ff4f5945157410312df2a8950f942dMark Andrews ((l >= N) && (l < sizeof(A)) && (strncasecmp(opt, A, l) == 0))
c76e8412f4ff4f5945157410312df2a8950f942dMark Andrews
3f932812d78f7e17e2e11a56f36656f7e2679506Mark Andrews if (CHECKOPT("all", 3)) {
f900be21902d02418c2c71ffed754fb3f9f54cffMichael Sawyer show_settings(ISC_TRUE, ISC_FALSE);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer } else if (strncasecmp(opt, "class=", 6) == 0) {
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer if (testclass(&opt[6]))
0a92db42c6be6a158cd41ff863831a8d2d257935Mark Andrews strlcpy(defclass, &opt[6], sizeof(defclass));
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer } else if (strncasecmp(opt, "cl=", 3) == 0) {
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer if (testclass(&opt[3]))
0a92db42c6be6a158cd41ff863831a8d2d257935Mark Andrews strlcpy(defclass, &opt[3], sizeof(defclass));
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer } else if (strncasecmp(opt, "type=", 5) == 0) {
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews if (testtype(&opt[5])) {
0a92db42c6be6a158cd41ff863831a8d2d257935Mark Andrews strlcpy(deftype, &opt[5], sizeof(deftype));
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews default_lookups = ISC_FALSE;
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews }
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer } else if (strncasecmp(opt, "ty=", 3) == 0) {
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews if (testtype(&opt[3])) {
0a92db42c6be6a158cd41ff863831a8d2d257935Mark Andrews strlcpy(deftype, &opt[3], sizeof(deftype));
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews default_lookups = ISC_FALSE;
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews }
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer } else if (strncasecmp(opt, "querytype=", 10) == 0) {
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews if (testtype(&opt[10])) {
0a92db42c6be6a158cd41ff863831a8d2d257935Mark Andrews strlcpy(deftype, &opt[10], sizeof(deftype));
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews default_lookups = ISC_FALSE;
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews }
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer } else if (strncasecmp(opt, "query=", 6) == 0) {
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews if (testtype(&opt[6])) {
0a92db42c6be6a158cd41ff863831a8d2d257935Mark Andrews strlcpy(deftype, &opt[6], sizeof(deftype));
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews default_lookups = ISC_FALSE;
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews }
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer } else if (strncasecmp(opt, "qu=", 3) == 0) {
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews if (testtype(&opt[3])) {
0a92db42c6be6a158cd41ff863831a8d2d257935Mark Andrews strlcpy(deftype, &opt[3], sizeof(deftype));
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews default_lookups = ISC_FALSE;
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews }
7f20fd8ebb0cabc8f935381d958f8371990c9212Mark Andrews } else if (strncasecmp(opt, "q=", 2) == 0) {
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews if (testtype(&opt[2])) {
0a92db42c6be6a158cd41ff863831a8d2d257935Mark Andrews strlcpy(deftype, &opt[2], sizeof(deftype));
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews default_lookups = ISC_FALSE;
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews }
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer } else if (strncasecmp(opt, "domain=", 7) == 0) {
0a92db42c6be6a158cd41ff863831a8d2d257935Mark Andrews strlcpy(domainopt, &opt[7], sizeof(domainopt));
20b6f1e657d049d9fdb251a0846465bff9b95948Andreas Gustafsson set_search_domain(domainopt);
8e61de2efdc23d1248cff0abf4cadec3325a929cMichael Sawyer usesearch = ISC_TRUE;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer } else if (strncasecmp(opt, "do=", 3) == 0) {
0a92db42c6be6a158cd41ff863831a8d2d257935Mark Andrews strlcpy(domainopt, &opt[3], sizeof(domainopt));
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson set_search_domain(domainopt);
8e61de2efdc23d1248cff0abf4cadec3325a929cMichael Sawyer usesearch = ISC_TRUE;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer } else if (strncasecmp(opt, "port=", 5) == 0) {
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson set_port(&opt[5]);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer } else if (strncasecmp(opt, "po=", 3) == 0) {
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson set_port(&opt[3]);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer } else if (strncasecmp(opt, "timeout=", 8) == 0) {
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson set_timeout(&opt[8]);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer } else if (strncasecmp(opt, "t=", 2) == 0) {
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson set_timeout(&opt[2]);
c76e8412f4ff4f5945157410312df2a8950f942dMark Andrews } else if (CHECKOPT("recurse", 3)) {
ee4429e13e08f30c366cdc5d10585388b8a9f212Michael Sawyer recurse = ISC_TRUE;
c76e8412f4ff4f5945157410312df2a8950f942dMark Andrews } else if (CHECKOPT("norecurse", 5)) {
ee4429e13e08f30c366cdc5d10585388b8a9f212Michael Sawyer recurse = ISC_FALSE;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer } else if (strncasecmp(opt, "retry=", 6) == 0) {
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson set_tries(&opt[6]);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer } else if (strncasecmp(opt, "ret=", 4) == 0) {
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson set_tries(&opt[4]);
c76e8412f4ff4f5945157410312df2a8950f942dMark Andrews } else if (CHECKOPT("defname", 3)) {
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson usesearch = ISC_TRUE;
c76e8412f4ff4f5945157410312df2a8950f942dMark Andrews } else if (CHECKOPT("nodefname", 5)) {
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson usesearch = ISC_FALSE;
c76e8412f4ff4f5945157410312df2a8950f942dMark Andrews } else if (CHECKOPT("vc", 2) == 0) {
8e61de2efdc23d1248cff0abf4cadec3325a929cMichael Sawyer tcpmode = ISC_TRUE;
3001a1b4e6298466882a4147307b4c28a3b08f91Mark Andrews tcpmode_set = ISC_TRUE;
c76e8412f4ff4f5945157410312df2a8950f942dMark Andrews } else if (CHECKOPT("novc", 4) == 0) {
8e61de2efdc23d1248cff0abf4cadec3325a929cMichael Sawyer tcpmode = ISC_FALSE;
3001a1b4e6298466882a4147307b4c28a3b08f91Mark Andrews tcpmode_set = ISC_TRUE;
c76e8412f4ff4f5945157410312df2a8950f942dMark Andrews } else if (CHECKOPT("debug", 3) == 0) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer short_form = ISC_FALSE;
824cb6567555af556d0963d961798483d252eb5fMark Andrews showsearch = ISC_TRUE;
c76e8412f4ff4f5945157410312df2a8950f942dMark Andrews } else if (CHECKOPT("nodebug", 5) == 0) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer short_form = ISC_TRUE;
824cb6567555af556d0963d961798483d252eb5fMark Andrews showsearch = ISC_FALSE;
c76e8412f4ff4f5945157410312df2a8950f942dMark Andrews } else if (CHECKOPT("d2", 2) == 0) {
d821f1cd7e97552401296e880e7518c98c9ebea1Michael Sawyer debugging = ISC_TRUE;
c76e8412f4ff4f5945157410312df2a8950f942dMark Andrews } else if (CHECKOPT("nod2", 4) == 0) {
d821f1cd7e97552401296e880e7518c98c9ebea1Michael Sawyer debugging = ISC_FALSE;
c76e8412f4ff4f5945157410312df2a8950f942dMark Andrews } else if (CHECKOPT("search", 3) == 0) {
aa2c453d3c6e416b56b29247bedd9a0af2721e93Mark Andrews usesearch = ISC_TRUE;
c76e8412f4ff4f5945157410312df2a8950f942dMark Andrews } else if (CHECKOPT("nosearch", 5) == 0) {
aa2c453d3c6e416b56b29247bedd9a0af2721e93Mark Andrews usesearch = ISC_FALSE;
c76e8412f4ff4f5945157410312df2a8950f942dMark Andrews } else if (CHECKOPT("sil", 3) == 0) {
fb2c34419d268d36acb222e08e6f789b45a81306Mark Andrews /* deprecation_msg = ISC_FALSE; */
c76e8412f4ff4f5945157410312df2a8950f942dMark Andrews } else if (CHECKOPT("fail", 3) == 0) {
3d711f2f75cb9a9ddcbf1fca9b2de192e75340e6Mark Andrews nofail=ISC_FALSE;
c76e8412f4ff4f5945157410312df2a8950f942dMark Andrews } else if (CHECKOPT("nofail", 5) == 0) {
3d711f2f75cb9a9ddcbf1fca9b2de192e75340e6Mark Andrews nofail=ISC_TRUE;
8afea636ab0c07399aa3e2410b2cfbd41099df98Mark Andrews } else if (strncasecmp(opt, "ndots=", 6) == 0) {
8afea636ab0c07399aa3e2410b2cfbd41099df98Mark Andrews set_ndots(&opt[6]);
48674819ebf9176b5d5582ae851e485c324c1159Michael Sawyer } else {
96eeb9496c09114c116132d5a493ef5eb88e3192Automatic Updater printf("*** Invalid option: %s\n", opt);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer }
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer}
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
869f65bea49f8d65ab9bb14c4551215529bd7419David Lawrencestatic void
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyeraddlookup(char *opt) {
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer dig_lookup_t *lookup;
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer isc_result_t result;
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer isc_textregion_t tr;
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer dns_rdatatype_t rdtype;
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer dns_rdataclass_t rdclass;
5d20773abcdf9ff3afe14a349413174f94594188Michael Sawyer char store[MXNAME];
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson debug("addlookup()");
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews a_noanswer = ISC_FALSE;
529d8a7cf1960f96a78d1b2b004aa63aff80b16bMark Andrews
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer tr.base = deftype;
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer tr.length = strlen(deftype);
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer result = dns_rdatatype_fromtext(&rdtype, &tr);
3291587f23b940c986f41cf37b2e531f618ec2bdMichael Sawyer if (result != ISC_R_SUCCESS) {
32e783c745299f838dd7cf8ffe2032775a80f1d0Andreas Gustafsson printf("unknown query type: %s\n", deftype);
3291587f23b940c986f41cf37b2e531f618ec2bdMichael Sawyer rdclass = dns_rdatatype_a;
3291587f23b940c986f41cf37b2e531f618ec2bdMichael Sawyer }
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer tr.base = defclass;
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer tr.length = strlen(defclass);
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer result = dns_rdataclass_fromtext(&rdclass, &tr);
3291587f23b940c986f41cf37b2e531f618ec2bdMichael Sawyer if (result != ISC_R_SUCCESS) {
32e783c745299f838dd7cf8ffe2032775a80f1d0Andreas Gustafsson printf("unknown query class: %s\n", defclass);
3291587f23b940c986f41cf37b2e531f618ec2bdMichael Sawyer rdclass = dns_rdataclass_in;
3291587f23b940c986f41cf37b2e531f618ec2bdMichael Sawyer }
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer lookup = make_empty_lookup();
974f1d7b49442d65542ad3517dbd61146ee3b653Mark Andrews if (get_reverse(store, sizeof(store), opt, lookup->ip6_int, ISC_TRUE)
1ae75c1024eb0475c2be352b8707772e16332ad0Mark Andrews == ISC_R_SUCCESS) {
0a92db42c6be6a158cd41ff863831a8d2d257935Mark Andrews strlcpy(lookup->textname, store, sizeof(lookup->textname));
55bf97a1b4b7f78b669b1179df1422cb9789c484Michael Sawyer lookup->rdtype = dns_rdatatype_ptr;
cc7732e1299042bff430dfdfbc2624fb340047b2Michael Sawyer lookup->rdtypeset = ISC_TRUE;
55bf97a1b4b7f78b669b1179df1422cb9789c484Michael Sawyer } else {
0a92db42c6be6a158cd41ff863831a8d2d257935Mark Andrews strlcpy(lookup->textname, opt, sizeof(lookup->textname));
55bf97a1b4b7f78b669b1179df1422cb9789c484Michael Sawyer lookup->rdtype = rdtype;
cc7732e1299042bff430dfdfbc2624fb340047b2Michael Sawyer lookup->rdtypeset = ISC_TRUE;
55bf97a1b4b7f78b669b1179df1422cb9789c484Michael Sawyer }
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer lookup->rdclass = rdclass;
cc7732e1299042bff430dfdfbc2624fb340047b2Michael Sawyer lookup->rdclassset = ISC_TRUE;
1d78c8d7cd619df2255ec52d69b0dd0d8850293fBrian Wellington lookup->trace = ISC_FALSE;
1d78c8d7cd619df2255ec52d69b0dd0d8850293fBrian Wellington lookup->trace_root = lookup->trace;
1d78c8d7cd619df2255ec52d69b0dd0d8850293fBrian Wellington lookup->ns_search_only = ISC_FALSE;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer lookup->identify = identify;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer lookup->recurse = recurse;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer lookup->aaonly = aaonly;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer lookup->retries = tries;
0e0e575ec135a983a53c501cf48734b823361ab4Brian Wellington lookup->udpsize = 0;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer lookup->comments = comments;
3001a1b4e6298466882a4147307b4c28a3b08f91Mark Andrews if (lookup->rdtype == dns_rdatatype_any && !tcpmode_set)
3001a1b4e6298466882a4147307b4c28a3b08f91Mark Andrews lookup->tcp_mode = ISC_TRUE;
3001a1b4e6298466882a4147307b4c28a3b08f91Mark Andrews else
3001a1b4e6298466882a4147307b4c28a3b08f91Mark Andrews lookup->tcp_mode = tcpmode;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer lookup->stats = stats;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer lookup->section_question = section_question;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer lookup->section_answer = section_answer;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer lookup->section_authority = section_authority;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer lookup->section_additional = section_additional;
17747cd5ee98901b435cd2b3019c52937955a981Michael Sawyer lookup->new_search = ISC_TRUE;
3d711f2f75cb9a9ddcbf1fca9b2de192e75340e6Mark Andrews if (nofail)
3d711f2f75cb9a9ddcbf1fca9b2de192e75340e6Mark Andrews lookup->servfail_stops = ISC_FALSE;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer ISC_LIST_INIT(lookup->q);
8e61de2efdc23d1248cff0abf4cadec3325a929cMichael Sawyer ISC_LINK_INIT(lookup, link);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer ISC_LIST_APPEND(lookup_list, lookup, link);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer lookup->origin = NULL;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer ISC_LIST_INIT(lookup->my_server_list);
38cf6e52ce4b33795713388824b69d78e430b115Michael Sawyer debug("looking up %s", lookup->textname);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer}
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerstatic void
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Huntdo_next_command(char *input) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer char *ptr, *arg;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
e412ae947df6de858883564b8676a9650df70d9aMark Andrews ptr = next_token(&input, " \t\r\n");
5f01e77fc23fe9665fa2b8acd0a0c5bfbf61d61dBrian Wellington if (ptr == NULL)
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt return;
e412ae947df6de858883564b8676a9650df70d9aMark Andrews arg = next_token(&input, " \t\r\n");
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if ((strcasecmp(ptr, "set") == 0) &&
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer (arg != NULL))
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer setoption(arg);
f900be21902d02418c2c71ffed754fb3f9f54cffMichael Sawyer else if ((strcasecmp(ptr, "server") == 0) ||
f900be21902d02418c2c71ffed754fb3f9f54cffMichael Sawyer (strcasecmp(ptr, "lserver") == 0)) {
3f79a8b6f1b50f8e1bc2f274bcfe91d6af74f161Mark Andrews isc_app_block();
b31e7ab8857a62eb94d4a6a681243739f158374dDanny Mayer set_nameserver(arg);
715361d0c3800e5ad886e5df971936ce6cd1ca89Mark Andrews check_ra = ISC_FALSE;
3f79a8b6f1b50f8e1bc2f274bcfe91d6af74f161Mark Andrews isc_app_unblock();
f900be21902d02418c2c71ffed754fb3f9f54cffMichael Sawyer show_settings(ISC_TRUE, ISC_TRUE);
6e3a8e17fdf108f47bbba17474fd3d120d356b2fBrian Wellington } else if (strcasecmp(ptr, "exit") == 0) {
6e3a8e17fdf108f47bbba17474fd3d120d356b2fBrian Wellington in_use = ISC_FALSE;
5f01e77fc23fe9665fa2b8acd0a0c5bfbf61d61dBrian Wellington } else if (strcasecmp(ptr, "help") == 0 ||
1ae75c1024eb0475c2be352b8707772e16332ad0Mark Andrews strcasecmp(ptr, "?") == 0) {
5f01e77fc23fe9665fa2b8acd0a0c5bfbf61d61dBrian Wellington printf("The '%s' command is not yet implemented.\n", ptr);
5f01e77fc23fe9665fa2b8acd0a0c5bfbf61d61dBrian Wellington } else if (strcasecmp(ptr, "finger") == 0 ||
5f01e77fc23fe9665fa2b8acd0a0c5bfbf61d61dBrian Wellington strcasecmp(ptr, "root") == 0 ||
5f01e77fc23fe9665fa2b8acd0a0c5bfbf61d61dBrian Wellington strcasecmp(ptr, "ls") == 0 ||
1ae75c1024eb0475c2be352b8707772e16332ad0Mark Andrews strcasecmp(ptr, "view") == 0) {
5f01e77fc23fe9665fa2b8acd0a0c5bfbf61d61dBrian Wellington printf("The '%s' command is not implemented.\n", ptr);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence } else
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer addlookup(ptr);
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt}
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Huntstatic void
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Huntget_next_command(void) {
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt char *buf;
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt char *ptr;
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt fflush(stdout);
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt buf = isc_mem_allocate(mctx, COMMSIZE);
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt if (buf == NULL)
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt fatal("memory allocation failure");
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt isc_app_block();
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt if (interactive) {
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt#ifdef HAVE_READLINE
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt ptr = readline("> ");
1a4725bef25e60de161f77318f362c327b690d72Mark Andrews if (ptr != NULL)
1a4725bef25e60de161f77318f362c327b690d72Mark Andrews add_history(ptr);
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt#else
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt fputs("> ", stderr);
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt fflush(stderr);
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt ptr = fgets(buf, COMMSIZE, stdin);
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt#endif
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt } else
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt ptr = fgets(buf, COMMSIZE, stdin);
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt isc_app_unblock();
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt if (ptr == NULL) {
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt in_use = ISC_FALSE;
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt } else
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt do_next_command(ptr);
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt#ifdef HAVE_READLINE
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt if (interactive)
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt free(ptr);
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt#endif
07a926724c0a91d85b85a94441938d0094e88cffMark Andrews isc_mem_free(mctx, buf);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer}
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerstatic void
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerparse_args(int argc, char **argv) {
19c8df90f1f23c3df870c1771c89c1acdb15020eMichael Sawyer isc_boolean_t have_lookup = ISC_FALSE;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
66921d6dd259c4486a07aba19eee2b8349698575Brian Wellington usesearch = ISC_TRUE;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer for (argc--, argv++; argc > 0; argc--, argv++) {
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson debug("main parsing %s", argv[0]);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (argv[0][0] == '-') {
67d01dcacb2051a03377c8ec5c0e36604c17aea5Evan Hunt if (strncasecmp(argv[0], "-ver", 4) == 0) {
67d01dcacb2051a03377c8ec5c0e36604c17aea5Evan Hunt version();
67d01dcacb2051a03377c8ec5c0e36604c17aea5Evan Hunt exit(0);
67d01dcacb2051a03377c8ec5c0e36604c17aea5Evan Hunt } else if (argv[0][1] != 0) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer setoption(&argv[0][1]);
67d01dcacb2051a03377c8ec5c0e36604c17aea5Evan Hunt } else
19c8df90f1f23c3df870c1771c89c1acdb15020eMichael Sawyer have_lookup = ISC_TRUE;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer } else {
19c8df90f1f23c3df870c1771c89c1acdb15020eMichael Sawyer if (!have_lookup) {
19c8df90f1f23c3df870c1771c89c1acdb15020eMichael Sawyer have_lookup = ISC_TRUE;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer in_use = ISC_TRUE;
869f65bea49f8d65ab9bb14c4551215529bd7419David Lawrence addlookup(argv[0]);
715361d0c3800e5ad886e5df971936ce6cd1ca89Mark Andrews } else {
b31e7ab8857a62eb94d4a6a681243739f158374dDanny Mayer set_nameserver(argv[0]);
715361d0c3800e5ad886e5df971936ce6cd1ca89Mark Andrews check_ra = ISC_FALSE;
715361d0c3800e5ad886e5df971936ce6cd1ca89Mark Andrews }
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer }
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer }
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer}
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerstatic void
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyerflush_lookup_list(void) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer dig_lookup_t *l, *lp;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer dig_query_t *q, *qp;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer dig_server_t *s, *sp;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer lookup_counter = 0;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer l = ISC_LIST_HEAD(lookup_list);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer while (l != NULL) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer q = ISC_LIST_HEAD(l->q);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer while (q != NULL) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (q->sock != NULL) {
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_socket_cancel(q->sock, NULL,
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer ISC_SOCKCANCEL_ALL);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_socket_detach(&q->sock);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer }
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (ISC_LINK_LINKED(&q->recvbuf, link))
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer ISC_LIST_DEQUEUE(q->recvlist, &q->recvbuf,
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer link);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (ISC_LINK_LINKED(&q->lengthbuf, link))
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer ISC_LIST_DEQUEUE(q->lengthlist, &q->lengthbuf,
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer link);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_buffer_invalidate(&q->recvbuf);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_buffer_invalidate(&q->lengthbuf);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer qp = q;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer q = ISC_LIST_NEXT(q, link);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer ISC_LIST_DEQUEUE(l->q, qp, link);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_mem_free(mctx, qp);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer }
e715e011788a529446b8013239c33599542ece32Michael Sawyer s = ISC_LIST_HEAD(l->my_server_list);
e715e011788a529446b8013239c33599542ece32Michael Sawyer while (s != NULL) {
e715e011788a529446b8013239c33599542ece32Michael Sawyer sp = s;
e715e011788a529446b8013239c33599542ece32Michael Sawyer s = ISC_LIST_NEXT(s, link);
e715e011788a529446b8013239c33599542ece32Michael Sawyer ISC_LIST_DEQUEUE(l->my_server_list, sp, link);
e715e011788a529446b8013239c33599542ece32Michael Sawyer isc_mem_free(mctx, sp);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer }
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer if (l->sendmsg != NULL)
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer dns_message_destroy(&l->sendmsg);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer lp = l;
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer l = ISC_LIST_NEXT(l, link);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer ISC_LIST_DEQUEUE(lookup_list, lp, link);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer isc_mem_free(mctx, lp);
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer }
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence}
6fe03d6c83ec02d4494edc870f5e892d419b6885Michael Sawyer
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellingtonstatic void
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellingtongetinput(isc_task_t *task, isc_event_t *event) {
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington UNUSED(task);
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington if (global_event == NULL)
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington global_event = event;
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer while (in_use) {
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer get_next_command();
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer if (ISC_LIST_HEAD(lookup_list) != NULL) {
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer start_lookup();
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer return;
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer }
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer }
e42c402595802edceafbd3e5338dda011fbbcdb6Michael Sawyer isc_app_shutdown();
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington}
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyerint
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyermain(int argc, char **argv) {
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer isc_result_t result;
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt interactive = ISC_TF(isatty(0));
1d32b1df372d6be6bac6450739b9e5ea23819995Evan Hunt
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer ISC_LIST_INIT(lookup_list);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer ISC_LIST_INIT(server_list);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer ISC_LIST_INIT(search_list);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
715361d0c3800e5ad886e5df971936ce6cd1ca89Mark Andrews check_ra = ISC_TRUE;
715361d0c3800e5ad886e5df971936ce6cd1ca89Mark Andrews
2b0060b8ff20eab8b7b1c42a9b9ccedb85047887Evan Hunt /* setup dighost callbacks */
2b0060b8ff20eab8b7b1c42a9b9ccedb85047887Evan Hunt#ifdef DIG_SIGCHASE
2b0060b8ff20eab8b7b1c42a9b9ccedb85047887Evan Hunt dighost_printrdataset = printrdataset;
2b0060b8ff20eab8b7b1c42a9b9ccedb85047887Evan Hunt#endif
2b0060b8ff20eab8b7b1c42a9b9ccedb85047887Evan Hunt dighost_printmessage = printmessage;
2b0060b8ff20eab8b7b1c42a9b9ccedb85047887Evan Hunt dighost_received = received;
2b0060b8ff20eab8b7b1c42a9b9ccedb85047887Evan Hunt dighost_trying = trying;
2b0060b8ff20eab8b7b1c42a9b9ccedb85047887Evan Hunt dighost_shutdown = query_finished;
2b0060b8ff20eab8b7b1c42a9b9ccedb85047887Evan Hunt
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington result = isc_app_start();
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington check_result(result, "isc_app_start");
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer setup_libs();
38cf6e52ce4b33795713388824b69d78e430b115Michael Sawyer progname = argv[0];
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
b8a04d50a361eb5e6a6b098d530daa0c56c6fdd9Mark Andrews setup_system(ISC_FALSE, ISC_FALSE);
4e7973990c2aaec391cf307a7e60583331568e01Evan Hunt parse_args(argc, argv);
58e291cb8dd77656048d6d7727f2bc2388110785Evan Hunt if (keyfile[0] != 0)
58e291cb8dd77656048d6d7727f2bc2388110785Evan Hunt setup_file_key();
58e291cb8dd77656048d6d7727f2bc2388110785Evan Hunt else if (keysecret[0] != 0)
58e291cb8dd77656048d6d7727f2bc2388110785Evan Hunt setup_text_key();
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson if (domainopt[0] != '\0')
688a4c50c2025a683d8d2bf5bb53a591556df4fcAndreas Gustafsson set_search_domain(domainopt);
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington if (in_use)
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington result = isc_app_onrun(mctx, global_task, onrun_callback,
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington NULL);
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington else
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington result = isc_app_onrun(mctx, global_task, getinput, NULL);
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington check_result(result, "isc_app_onrun");
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington in_use = ISC_TF(!in_use);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington (void)isc_app_run();
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson puts("");
37e6e0ca1337351642798b1a6aa24ae40bf86399Andreas Gustafsson debug("done, and starting to shut down");
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington if (global_event != NULL)
b266f8fc42702debc6bd89365273223fa89cd8ddBrian Wellington isc_event_free(&global_event);
bc223aaf8e952f3c88ac7ff82d726b328b990880Michael Sawyer cancel_all();
aa6054ec74819f754bcf19442ca9b39d948171adMichael Sawyer destroy_libs();
db8b100cae62de849ecf4ba9ad3be811fb375b53Michael Sawyer isc_app_finish();
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
04e5f9812cb02d5d86b542dfa9a394f074ff5621Evan Hunt return (query_error | print_error);
cefd68008fbba3488a077052ae62aa12b6de502bMichael Sawyer}