nslookup.c revision e32394a2ac3466a2235f79ee32c247a11be42a8d
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff/*
499b34cea04a46823d003d4c0520c8b03e8513cbBrian Wellington * Copyright (C) 2000 Internet Software Consortium.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff * Permission to use, copy, modify, and distribute this software for any
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff * purpose with or without fee is hereby granted, provided that the above
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff * copyright notice and this permission notice appear in all copies.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
15a44745412679c30a6d022733925af70a38b715David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
15a44745412679c30a6d022733925af70a38b715David Lawrence * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
15a44745412679c30a6d022733925af70a38b715David Lawrence * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
15a44745412679c30a6d022733925af70a38b715David Lawrence * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
15a44745412679c30a6d022733925af70a38b715David Lawrence * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
15a44745412679c30a6d022733925af70a38b715David Lawrence * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
15a44745412679c30a6d022733925af70a38b715David Lawrence * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
15a44745412679c30a6d022733925af70a38b715David Lawrence * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff */
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff
e61793f0865117ad87a19d6e245bea8f3b712d1bDanny Mayer/* $Id: nslookup.c,v 1.35 2000/08/15 18:44:05 gson Exp $ */
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff#include <config.h>
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff
b91bbaf50cf6d0c2cad7323720495165595e413bMichael Graff#include <stdlib.h>
74cf566d92d610c2dc090a0dc7f77651c0fb0635David Lawrence
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graffextern int h_errno;
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff
90c845947a6987a95d4facaed61406362fbb58dfBrian Wellington#include <isc/app.h>
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff#include <isc/buffer.h>
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff#include <isc/commandline.h>
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff#include <isc/condition.h>
3c0a81897768d0378813865278a49d285c95b8b8Brian Wellington#include <isc/mutex.h>
363cb30a83e8e57bc8874e621910f0e23dd84909Mark Andrews#include <isc/string.h>
363cb30a83e8e57bc8874e621910f0e23dd84909Mark Andrews#include <isc/timer.h>
363cb30a83e8e57bc8874e621910f0e23dd84909Mark Andrews#include <isc/util.h>
363cb30a83e8e57bc8874e621910f0e23dd84909Mark Andrews#include <isc/task.h>
363cb30a83e8e57bc8874e621910f0e23dd84909Mark Andrews
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff#include <dns/message.h>
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff#include <dns/name.h>
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff#include <dns/rdata.h>
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff#include <dns/rdataclass.h>
03a0fca86dd9db6a40743107ec9dea7c33e46a7aDavid Lawrence#include <dns/rdataset.h>
03a0fca86dd9db6a40743107ec9dea7c33e46a7aDavid Lawrence#include <dns/rdatatype.h>
03a0fca86dd9db6a40743107ec9dea7c33e46a7aDavid Lawrence
03a0fca86dd9db6a40743107ec9dea7c33e46a7aDavid Lawrence#include <dig/dig.h>
03a0fca86dd9db6a40743107ec9dea7c33e46a7aDavid Lawrence
03a0fca86dd9db6a40743107ec9dea7c33e46a7aDavid Lawrenceextern ISC_LIST(dig_lookup_t) lookup_list;
03a0fca86dd9db6a40743107ec9dea7c33e46a7aDavid Lawrenceextern ISC_LIST(dig_server_t) server_list;
03a0fca86dd9db6a40743107ec9dea7c33e46a7aDavid Lawrenceextern ISC_LIST(dig_searchlist_t) search_list;
03a0fca86dd9db6a40743107ec9dea7c33e46a7aDavid Lawrence
71811653336a000f6c61d26d4b2a0c0d2cec9c25Danny Mayerextern isc_boolean_t have_ipv6, show_details,
001d1ceec69e181f62367f4d344fb386135d91b1Andreas Gustafsson usesearch, trace, qr, debugging;
71811653336a000f6c61d26d4b2a0c0d2cec9c25Danny Mayerextern in_port_t port;
71811653336a000f6c61d26d4b2a0c0d2cec9c25Danny Mayerextern unsigned int timeout;
001d1ceec69e181f62367f4d344fb386135d91b1Andreas Gustafssonextern isc_mem_t *mctx;
001d1ceec69e181f62367f4d344fb386135d91b1Andreas Gustafssonextern dns_messageid_t id;
001d1ceec69e181f62367f4d344fb386135d91b1Andreas Gustafssonextern char *rootspace[BUFSIZE];
001d1ceec69e181f62367f4d344fb386135d91b1Andreas Gustafssonextern isc_buffer_t rootbuf;
3489d64bde03af5c7950661b083b530bd285ec27Mark Andrewsextern int sendcount;
001d1ceec69e181f62367f4d344fb386135d91b1Andreas Gustafssonextern int ndots;
001d1ceec69e181f62367f4d344fb386135d91b1Andreas Gustafssonextern int tries;
71811653336a000f6c61d26d4b2a0c0d2cec9c25Danny Mayerextern int lookup_counter;
71811653336a000f6c61d26d4b2a0c0d2cec9c25Danny Mayerextern char fixeddomain[MXNAME];
71811653336a000f6c61d26d4b2a0c0d2cec9c25Danny Mayerextern int exitcode;
e61793f0865117ad87a19d6e245bea8f3b712d1bDanny Mayerextern isc_taskmgr_t *taskmgr;
e61793f0865117ad87a19d6e245bea8f3b712d1bDanny Mayerextern char *progname;
412e5c62115e881cce79921e2467c6c0990a0a8aBrian Wellington
03a0fca86dd9db6a40743107ec9dea7c33e46a7aDavid Lawrenceisc_boolean_t short_form = ISC_TRUE, printcmd = ISC_TRUE,
03a0fca86dd9db6a40743107ec9dea7c33e46a7aDavid Lawrence filter = ISC_FALSE, showallsoa = ISC_FALSE,
03a0fca86dd9db6a40743107ec9dea7c33e46a7aDavid Lawrence tcpmode = ISC_FALSE, deprecation_msg = ISC_TRUE;
03a0fca86dd9db6a40743107ec9dea7c33e46a7aDavid Lawrence
03a0fca86dd9db6a40743107ec9dea7c33e46a7aDavid Lawrenceisc_uint16_t bufsize = 0;
03a0fca86dd9db6a40743107ec9dea7c33e46a7aDavid Lawrenceisc_boolean_t identify = ISC_FALSE,
03a0fca86dd9db6a40743107ec9dea7c33e46a7aDavid Lawrence trace = ISC_FALSE, ns_search_only = ISC_FALSE,
03a0fca86dd9db6a40743107ec9dea7c33e46a7aDavid Lawrence forcecomment = ISC_FALSE, stats = ISC_TRUE,
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff comments = ISC_TRUE, section_question = ISC_TRUE,
01933e930208da3291de3722cb0d0787636b1e4fMichael Graff section_answer = ISC_TRUE, section_authority = ISC_TRUE,
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff section_additional = ISC_TRUE, recurse = ISC_TRUE,
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff defname = ISC_TRUE, aaonly = ISC_FALSE;
efe2f579ba6fbec4229129015780f2d925085b04Michael Graffisc_mutex_t lock;
efe2f579ba6fbec4229129015780f2d925085b04Michael Graffisc_condition_t cond;
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graffisc_boolean_t busy = ISC_FALSE, in_use = ISC_FALSE;
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graffchar defclass[MXRD] = "IN";
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graffchar deftype[MXRD] = "A";
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellingtonstatic const char *rcodetext[] = {
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "NOERROR",
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "FORMERR",
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "SERVFAIL",
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "NXDOMAIN",
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "NOTIMPL",
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "REFUSED",
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "YXDOMAIN",
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "YXRRSET",
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "NXRRSET",
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "NOTAUTH",
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "NOTZONE",
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "RESERVED11",
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "RESERVED12",
8cd870e3f5e3db9808a4a0d6f98db3d1a5348e40Michael Graff "RESERVED13",
8cd870e3f5e3db9808a4a0d6f98db3d1a5348e40Michael Graff "RESERVED14",
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "RESERVED15",
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "BADVERS"
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff};
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graffstatic const char *rtypetext[] = {
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "rtype_0 = ", /* 0 */
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "internet address = ", /* 1 */
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff "nameserver = ", /* 2 */
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "md = ", /* 3 */
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "mf = ", /* 4 */
1d1d2fa57a954333c32ca3104b812b4e2389196fBrian Wellington "canonical name = ", /* 5 */
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "soa = ", /* 6 */
d736db6dc53e615e3f2d66d1ddbe28473694d107Michael Graff "mb = ", /* 7 */
d736db6dc53e615e3f2d66d1ddbe28473694d107Michael Graff "mg = ", /* 8 */
d736db6dc53e615e3f2d66d1ddbe28473694d107Michael Graff "mr = ", /* 9 */
d736db6dc53e615e3f2d66d1ddbe28473694d107Michael Graff "rtype_10 = ", /* 10 */
d736db6dc53e615e3f2d66d1ddbe28473694d107Michael Graff "protocol = ", /* 11 */
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "name = ", /* 12 */
8cd870e3f5e3db9808a4a0d6f98db3d1a5348e40Michael Graff "hinfo = ", /* 13 */
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "minfo = ", /* 14 */
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "mail exchanger = ", /* 15 */
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "text = ", /* 16 */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence "rp = ", /* 17 */
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "afsdb = ", /* 18 */
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "x25 address = ", /* 19 */
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "isdn address = ", /* 20 */
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "rt = ", /* 21 */
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "nsap = ", /* 22 */
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "nsap_ptr = ", /* 23 */
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "signature = ", /* 24 */
c1cfd8ef05f47f2ccb5db80639e9501c9f16864cMichael Graff "key = ", /* 25 */
c1cfd8ef05f47f2ccb5db80639e9501c9f16864cMichael Graff "px = ", /* 26 */
c1cfd8ef05f47f2ccb5db80639e9501c9f16864cMichael Graff "gpos = ", /* 27 */
c1cfd8ef05f47f2ccb5db80639e9501c9f16864cMichael Graff "has AAAA address", /* 28 */
c1cfd8ef05f47f2ccb5db80639e9501c9f16864cMichael Graff "loc = ", /* 29 */
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "next = ", /* 30 */
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "rtype_31 = ", /* 31 */
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "rtype_32 = ", /* 32 */
7a166c5c61a5aaa6eeb929bed152dc0a6b128e3dMichael Graff "service = ", /* 33 */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence "rtype_34 = ", /* 34 */
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "naptr = ", /* 35 */
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "kx = ", /* 36 */
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "cert = ", /* 37 */
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "v6 address = ", /* 38 */
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "dname = ", /* 39 */
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff "rtype_40 = ", /* 40 */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence "optional = "}; /* 41 */
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graffstatic void
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graffshow_usage(void) {
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff fputs("Usage:\n", stderr);
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff}
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graffvoid
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graffdighost_shutdown(void) {
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff debug("dighost_dhutdown()");
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff isc_mutex_lock(&lock);
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff busy = ISC_FALSE;
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff debug("signalling out");
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence isc_condition_signal(&cond);
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff isc_mutex_unlock(&lock);
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff}
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graffvoid
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graffreceived(int bytes, int frmsize, char *frm, dig_query_t *query) {
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff UNUSED(bytes);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence UNUSED(frmsize);
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff UNUSED(frm);
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff UNUSED(query);
68aa880dd538ab5643abdb07a800d0443e630d2eBrian Wellington}
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graffvoid
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Grafftrying(int frmsize, char *frm, dig_lookup_t *lookup) {
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff UNUSED(frmsize);
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff UNUSED(frm);
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff UNUSED(lookup);
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff}
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graffstatic isc_result_t
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrenceprintsection(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers,
68aa880dd538ab5643abdb07a800d0443e630d2eBrian Wellington dns_section_t section) {
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff isc_result_t result, loopresult;
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff isc_buffer_t *b = NULL;
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff dns_name_t *name;
64bed6c54393c2d213db83e9b171fb7c318cfc8eMichael Graff dns_rdataset_t *rdataset = NULL;
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff dns_rdata_t rdata;
01933e930208da3291de3722cb0d0787636b1e4fMichael Graff char *ptr;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff UNUSED(query);
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff UNUSED(headers);
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington debug("printsection()");
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington
3c0a81897768d0378813865278a49d285c95b8b8Brian Wellington result = dns_message_firstname(msg, section);
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington if (result == ISC_R_NOMORE)
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington return (ISC_R_SUCCESS);
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington else if (result != ISC_R_SUCCESS)
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington return (result);
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington result = isc_buffer_allocate(mctx, &b, MXNAME);
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington check_result(result, "isc_buffer_allocate");
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington for (;;) {
5b34d8267e8228b4615b9eae4e32d9ad1f308179Andreas Gustafsson name = NULL;
5b34d8267e8228b4615b9eae4e32d9ad1f308179Andreas Gustafsson dns_message_currentname(msg, section,
5b34d8267e8228b4615b9eae4e32d9ad1f308179Andreas Gustafsson &name);
5b34d8267e8228b4615b9eae4e32d9ad1f308179Andreas Gustafsson for (rdataset = ISC_LIST_HEAD(name->list);
5b34d8267e8228b4615b9eae4e32d9ad1f308179Andreas Gustafsson rdataset != NULL;
5b34d8267e8228b4615b9eae4e32d9ad1f308179Andreas Gustafsson rdataset = ISC_LIST_NEXT(rdataset, link)) {
5b34d8267e8228b4615b9eae4e32d9ad1f308179Andreas Gustafsson loopresult = dns_rdataset_first(rdataset);
5b34d8267e8228b4615b9eae4e32d9ad1f308179Andreas Gustafsson while (loopresult == ISC_R_SUCCESS) {
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington dns_rdataset_current(rdataset, &rdata);
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff switch (rdata.type) {
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington case dns_rdatatype_a:
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington if (section != DNS_SECTION_ANSWER)
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington goto def_short_section;
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington isc_buffer_clear(b);
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington result = dns_name_totext(name,
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington ISC_TRUE,
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington b);
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington check_result(result,
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington "dns_name_totext");
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington printf("Name:\t%.*s\n",
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington (int)isc_buffer_usedlength(b),
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington (char*)isc_buffer_base(b));
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington isc_buffer_clear(b);
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington result = dns_rdata_totext(&rdata,
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington NULL,
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington b);
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington check_result(result,
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington "dns_rdata_totext");
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff printf("Address: %.*s\n",
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington (int)isc_buffer_usedlength(b),
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff (char*)isc_buffer_base(b));
8cd870e3f5e3db9808a4a0d6f98db3d1a5348e40Michael Graff break;
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff case dns_rdatatype_soa:
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington isc_buffer_clear(b);
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff result = dns_name_totext(name,
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff ISC_TRUE,
8cd870e3f5e3db9808a4a0d6f98db3d1a5348e40Michael Graff b);
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff check_result(result,
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff "dns_name_totext");
001d1ceec69e181f62367f4d344fb386135d91b1Andreas Gustafsson printf("%.*s\n",
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson (int)isc_buffer_usedlength(b),
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson (char*)isc_buffer_base(b));
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson isc_buffer_clear(b);
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff result = dns_rdata_totext(&rdata,
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff NULL,
8cd870e3f5e3db9808a4a0d6f98db3d1a5348e40Michael Graff b);
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff check_result(result,
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff "dns_rdata_totext");
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson ((char *)isc_buffer_used(b))[0]=0;
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson ptr = strtok(isc_buffer_base(b),
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson " \t\r\n");
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson if (ptr == NULL)
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson break;
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson printf("\torigin = %s\n",
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson ptr);
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson ptr = strtok(NULL, " \t\r\n");
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson if (ptr == NULL)
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington break;
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington printf("\tmail addr = %s\n",
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington ptr);
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington ptr = strtok(NULL, " \t\r\n");
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington if (ptr == NULL)
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington break;
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington ptr = strtok(NULL, " \t\r\n");
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington if (ptr == NULL)
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson break;
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson printf("\tserial = %s\n",
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson ptr);
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson ptr = strtok(NULL, " \t\r\n");
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson if (ptr == NULL)
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson break;
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson printf("\trefresh = %s\n",
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson ptr);
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson ptr = strtok(NULL, " \t\r\n");
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson if (ptr == NULL)
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson break;
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson printf("\tretry = %s\n",
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson ptr);
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson ptr = strtok(NULL, " \t\r\n");
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson if (ptr == NULL)
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson break;
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington printf("\texpire = %s\n",
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington ptr);
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson ptr = strtok(NULL, " \t\r\n");
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson if (ptr == NULL)
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington break;
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington printf("\tminimum = %s\n",
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington ptr);
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington break;
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington default:
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington def_short_section:
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington isc_buffer_clear(b);
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington result = dns_name_totext(name,
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson ISC_TRUE,
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson b);
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson check_result(result,
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson "dns_name_totext");
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson if (rdata.type <= 41)
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson printf("%.*s\t%s",
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington (int)isc_buffer_usedlength(b),
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson (char*)isc_buffer_base(b),
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson rtypetext[rdata.type]);
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson else
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson printf("%.*s\trdata_%d = ",
5733d25b06b46067b3751d10436d82aef09cd705Brian Wellington (int)isc_buffer_usedlength(b),
5733d25b06b46067b3751d10436d82aef09cd705Brian Wellington (char*)isc_buffer_base(b),
5733d25b06b46067b3751d10436d82aef09cd705Brian Wellington rdata.type);
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson isc_buffer_clear(b);
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson result = dns_rdata_totext(&rdata,
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson NULL, b);
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson check_result(result,
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson "dns_rdata_totext");
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington printf("%.*s\n",
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington (int)isc_buffer_usedlength(b),
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington (char*)isc_buffer_base(b));
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington }
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington loopresult = dns_rdataset_next(rdataset);
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington }
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington }
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington result = dns_message_nextname(msg, section);
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington if (result == ISC_R_NOMORE)
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington break;
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington else if (result != ISC_R_SUCCESS) {
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington isc_buffer_free (&b);
8f80322fb58bac39c84bb76cb2bc82406bd6a9ecBrian Wellington return (result);
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson }
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson }
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson isc_buffer_free(&b);
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson return (ISC_R_SUCCESS);
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson}
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafssonstatic isc_result_t
01933e930208da3291de3722cb0d0787636b1e4fMichael Graffdetailsection(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers,
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff dns_section_t section) {
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff isc_result_t result, loopresult;
1a70537f01305cb84d402c84b1ba7cc89b97a5dcMichael Graff isc_buffer_t *b = NULL;
1a70537f01305cb84d402c84b1ba7cc89b97a5dcMichael Graff dns_name_t *name;
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff dns_rdataset_t *rdataset = NULL;
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson dns_rdata_t rdata;
b91bbaf50cf6d0c2cad7323720495165595e413bMichael Graff char *ptr;
b91bbaf50cf6d0c2cad7323720495165595e413bMichael Graff
b91bbaf50cf6d0c2cad7323720495165595e413bMichael Graff UNUSED(query);
b91bbaf50cf6d0c2cad7323720495165595e413bMichael Graff
74cf566d92d610c2dc090a0dc7f77651c0fb0635David Lawrence debug("detailsection()");
74cf566d92d610c2dc090a0dc7f77651c0fb0635David Lawrence
74cf566d92d610c2dc090a0dc7f77651c0fb0635David Lawrence if (headers) {
74cf566d92d610c2dc090a0dc7f77651c0fb0635David Lawrence switch (section) {
74cf566d92d610c2dc090a0dc7f77651c0fb0635David Lawrence case DNS_SECTION_QUESTION:
74cf566d92d610c2dc090a0dc7f77651c0fb0635David Lawrence puts(" QUESTIONS:");
74cf566d92d610c2dc090a0dc7f77651c0fb0635David Lawrence break;
74cf566d92d610c2dc090a0dc7f77651c0fb0635David Lawrence case DNS_SECTION_ANSWER:
74cf566d92d610c2dc090a0dc7f77651c0fb0635David Lawrence puts(" ANSWERS:");
b91bbaf50cf6d0c2cad7323720495165595e413bMichael Graff break;
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff case DNS_SECTION_AUTHORITY:
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson puts(" AUTHORITY RECORDS:");
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson break;
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson case DNS_SECTION_ADDITIONAL:
8cd870e3f5e3db9808a4a0d6f98db3d1a5348e40Michael Graff puts(" ADDITIONAL RECORDS:");
b91bbaf50cf6d0c2cad7323720495165595e413bMichael Graff break;
b91bbaf50cf6d0c2cad7323720495165595e413bMichael Graff }
d257033612f3052265085e0d7fdc116c871a28c0Michael Graff }
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson
b91bbaf50cf6d0c2cad7323720495165595e413bMichael Graff result = dns_message_firstname(msg, section);
b91bbaf50cf6d0c2cad7323720495165595e413bMichael Graff if (result == ISC_R_NOMORE)
b91bbaf50cf6d0c2cad7323720495165595e413bMichael Graff return (ISC_R_SUCCESS);
b91bbaf50cf6d0c2cad7323720495165595e413bMichael Graff else if (result != ISC_R_SUCCESS)
b91bbaf50cf6d0c2cad7323720495165595e413bMichael Graff return (result);
b91bbaf50cf6d0c2cad7323720495165595e413bMichael Graff result = isc_buffer_allocate(mctx, &b, MXNAME);
b91bbaf50cf6d0c2cad7323720495165595e413bMichael Graff check_result(result, "isc_buffer_allocate");
b91bbaf50cf6d0c2cad7323720495165595e413bMichael Graff for (;;) {
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson name = NULL;
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson dns_message_currentname(msg, section,
8cd870e3f5e3db9808a4a0d6f98db3d1a5348e40Michael Graff &name);
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson for (rdataset = ISC_LIST_HEAD(name->list);
7bee275ebe250b5e1705a997e2ee3e685d146fbdAndreas Gustafsson rdataset != NULL;
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff rdataset = ISC_LIST_NEXT(rdataset, link)) {
loopresult = dns_rdataset_first(rdataset);
while (loopresult == ISC_R_SUCCESS) {
dns_rdataset_current(rdataset, &rdata);
isc_buffer_clear(b);
result = dns_name_totext(name,
ISC_TRUE,
b);
check_result(result,
"dns_name_totext");
printf(" -> %.*s\n",
(int)isc_buffer_usedlength(b),
(char*)isc_buffer_base(b));
switch (rdata.type) {
case dns_rdatatype_soa:
isc_buffer_clear(b);
result = dns_rdata_totext(&rdata,
NULL,
b);
check_result(result,
"dns_rdata_totext");
((char *)isc_buffer_used(b))[0]=0;
ptr = strtok(isc_buffer_base(b),
" \t\r\n");
if (ptr == NULL)
break;
printf("\torigin = %s\n",
ptr);
ptr = strtok(NULL, " \t\r\n");
if (ptr == NULL)
break;
printf("\tmail addr = %s\n",
ptr);
ptr = strtok(NULL, " \t\r\n");
if (ptr == NULL)
break;
ptr = strtok(NULL, " \t\r\n");
if (ptr == NULL)
break;
printf("\tserial = %s\n",
ptr);
ptr = strtok(NULL, " \t\r\n");
if (ptr == NULL)
break;
printf("\trefresh = %s\n",
ptr);
ptr = strtok(NULL, " \t\r\n");
if (ptr == NULL)
break;
printf("\tretry = %s\n",
ptr);
ptr = strtok(NULL, " \t\r\n");
if (ptr == NULL)
break;
printf("\texpire = %s\n",
ptr);
ptr = strtok(NULL, " \t\r\n");
if (ptr == NULL)
break;
printf("\tminimum = %s\n",
ptr);
break;
default:
isc_buffer_clear(b);
if (rdata.type <= 41)
printf("\t%s",
rtypetext[rdata.type]);
else
printf("\trdata_%d = ",
rdata.type);
isc_buffer_clear(b);
result = dns_rdata_totext(&rdata,
NULL, b);
check_result(result,
"dns_rdata_totext");
printf("%.*s\n",
(int)isc_buffer_usedlength(b),
(char*)isc_buffer_base(b));
}
loopresult = dns_rdataset_next(rdataset);
}
}
result = dns_message_nextname(msg, section);
if (result == ISC_R_NOMORE)
break;
else if (result != ISC_R_SUCCESS) {
isc_buffer_free (&b);
return (result);
}
}
isc_buffer_free(&b);
return (ISC_R_SUCCESS);
}
isc_result_t
printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) {
isc_buffer_t *b = NULL;
isc_region_t r;
isc_result_t result;
debug("printmessage()");
if (msg->rcode != 0) {
result = isc_buffer_allocate(mctx, &b, MXNAME);
check_result(result, "isc_buffer_allocate");
result = dns_name_totext(query->lookup->name, ISC_FALSE,
b);
check_result(result, "dns_name_totext");
isc_buffer_usedregion(b, &r);
printf("** server can't find %.*s: %s\n",
(int)r.length, (char*)r.base,
rcodetext[msg->rcode]);
isc_buffer_free(&b);
debug("returning with rcode == 0");
return (ISC_R_SUCCESS);
}
debug("continuing on with rcode != 0");
result = isc_buffer_allocate(mctx, &b, MXNAME);
check_result(result, "isc_buffer_allocate");
printf("Server:\t\t%s\n", query->servname);
result = isc_sockaddr_totext(&query->sockaddr, b);
check_result(result, "isc_sockaddr_totext");
printf("Address:\t%.*s\n", (int)isc_buffer_usedlength(b),
(char*)isc_buffer_base(b));
isc_buffer_free(&b);
puts("");
if (!short_form){
puts("------------");
/* detailheader(query, msg);*/
detailsection(query, msg, headers, DNS_SECTION_QUESTION);
detailsection(query, msg, headers, DNS_SECTION_ANSWER);
detailsection(query, msg, headers, DNS_SECTION_AUTHORITY);
detailsection(query, msg, headers, DNS_SECTION_ADDITIONAL);
puts("------------");
}
if ((msg->flags & DNS_MESSAGEFLAG_AA) == 0)
puts("Non-authorative answer:");
printsection(query, msg, headers, DNS_SECTION_ANSWER);
if (((msg->flags & DNS_MESSAGEFLAG_AA) == 0) &&
(query->lookup->rdtype != dns_rdatatype_a)) {
puts("\nAuthorative answers can be found from:");
printsection(query, msg, headers,
DNS_SECTION_AUTHORITY);
printsection(query, msg, headers,
DNS_SECTION_ADDITIONAL);
}
return (ISC_R_SUCCESS);
}
static void
show_settings(isc_boolean_t full) {
dig_server_t *srv;
isc_sockaddr_t sockaddr;
isc_buffer_t *b = NULL;
isc_result_t result;
srv = ISC_LIST_HEAD(server_list);
while (srv != NULL) {
result = isc_buffer_allocate(mctx, &b, MXNAME);
check_result(result, "isc_buffer_allocate");
get_address(srv->servername, 53, &sockaddr);
result = isc_sockaddr_totext(&sockaddr, b);
check_result(result, "isc_sockaddr_totext");
printf("Default server: %s\nAddress: %.*s\n",
srv->servername, (int)isc_buffer_usedlength(b),
(char*)isc_buffer_base(b));
isc_buffer_free(&b);
if (!full)
return;
srv = ISC_LIST_NEXT(srv, link);
}
printf("\n\tSet options:\n");
printf("\t %s\t\t\t%s\t\t%s\n",
tcpmode?"vc":"novc", short_form?"nodebug":"debug",
debugging?"d2":"nod2");
printf("\t %s\t\t%s\t%s\n",
defname?"defname":"nodefname",
usesearch?"search ":"nosearch",
recurse?"recurse":"norecurse");
printf("\t timeout = %d\t\tretry = %d\tport = %d\n",
timeout, tries, port);
printf("\t querytype = %-8s\tclass = %s\n",deftype, defclass);
#if 0
printf("\t domain = %s\n", fixeddomain);
#endif
}
static isc_boolean_t
testtype(char *typetext) {
isc_result_t result;
isc_textregion_t tr;
dns_rdatatype_t rdtype;
tr.base = typetext;
tr.length = strlen(typetext);
result = dns_rdatatype_fromtext(&rdtype, &tr);
return (ISC_TF(result == ISC_R_SUCCESS));
}
static isc_boolean_t
testclass(char *typetext) {
isc_result_t result;
isc_textregion_t tr;
dns_rdataclass_t rdclass;
tr.base = typetext;
tr.length = strlen(typetext);
result = dns_rdataclass_fromtext(&rdclass, &tr);
return (ISC_TF(result == ISC_R_SUCCESS));
}
static void
setoption(char *opt) {
dig_server_t *srv;
if (strncasecmp(opt,"all",4) == 0) {
show_settings(ISC_TRUE);
} else if (strncasecmp(opt, "class=", 6) == 0) {
strncpy(defclass, &opt[6], MXRD);
} else if (strncasecmp(opt, "cl=", 3) == 0) {
strncpy(defclass, &opt[3], MXRD);
} else if (strncasecmp(opt, "type=", 5) == 0) {
if (testtype(&opt[5]))
strncpy(deftype, &opt[5], MXRD);
} else if (strncasecmp(opt, "ty=", 3) == 0) {
if (testclass(&opt[3]))
strncpy(defclass, &opt[3], MXRD);
} else if (strncasecmp(opt, "querytype=", 10) == 0) {
strncpy(deftype, &opt[10], MXRD);
} else if (strncasecmp(opt, "query=", 6) == 0) {
strncpy(deftype, &opt[6], MXRD);
} else if (strncasecmp(opt, "qu=", 3) == 0) {
strncpy(deftype, &opt[3], MXRD);
#if 0
/* XXXMWS domain= doesn't work now. */
} else if (strncasecmp(opt, "domain=", 7) == 0) {
strncpy(fixeddomain, &opt[7], MXNAME);
} else if (strncasecmp(opt, "do=", 3) == 0) {
strncpy(fixeddomain, &opt[3], MXNAME);
#endif
} else if (strncasecmp(opt, "port=", 5) == 0) {
port = atoi(&opt[5]);
} else if (strncasecmp(opt, "po=", 3) == 0) {
port = atoi(&opt[3]);
} else if (strncasecmp(opt, "timeout=", 8) == 0) {
timeout = atoi(&opt[8]);
} else if (strncasecmp(opt, "t=", 2) == 0) {
timeout = atoi(&opt[2]);
} else if (strncasecmp(opt, "retry=", 6) == 0) {
tries = atoi(&opt[6]);
} else if (strncasecmp(opt, "ret=", 4) == 0) {
tries = atoi(&opt[4]);
} else if (strncasecmp(opt, "def", 3) == 0) {
defname = ISC_TRUE;
} else if (strncasecmp(opt, "nodef", 5) == 0) {
defname = ISC_FALSE;
} else if (strncasecmp(opt, "deb", 3) == 0) {
short_form = ISC_FALSE;
} else if (strncasecmp(opt, "nodeb", 5) == 0) {
short_form = ISC_TRUE;
} else if (strncasecmp(opt, "d2", 2) == 0) {
debugging = ISC_TRUE;
} else if (strncasecmp(opt, "nod2", 4) == 0) {
debugging = ISC_FALSE;
} else if (strncasecmp(opt, "sil",3) == 0) {
deprecation_msg = ISC_FALSE;
} else {
srv = make_server(opt);
debug("server is %s", srv->servername);
ISC_LIST_APPEND(server_list, srv, link);
}
}
static dig_lookup_t*
addlookup(char *opt) {
dig_lookup_t *lookup;
isc_result_t result;
isc_textregion_t tr;
dns_rdatatype_t rdtype;
dns_rdataclass_t rdclass;
debug("addlookup()");
tr.base = deftype;
tr.length = strlen(deftype);
result = dns_rdatatype_fromtext(&rdtype, &tr);
INSIST(result == ISC_R_SUCCESS);
tr.base = defclass;
tr.length = strlen(defclass);
result = dns_rdataclass_fromtext(&rdclass, &tr);
INSIST(result == ISC_R_SUCCESS);
lookup = make_empty_lookup();
strncpy(lookup->textname, opt, MXNAME-1);
lookup->rdtype = rdtype;
lookup->rdclass = rdclass;
lookup->trace = ISC_TF(trace || ns_search_only);
lookup->trace_root = trace;
lookup->ns_search_only = ns_search_only;
lookup->identify = identify;
lookup->recurse = recurse;
lookup->aaonly = aaonly;
lookup->retries = tries;
lookup->udpsize = bufsize;
lookup->comments = comments;
lookup->tcp_mode = tcpmode;
lookup->stats = stats;
lookup->section_question = section_question;
lookup->section_answer = section_answer;
lookup->section_authority = section_authority;
lookup->section_additional = section_additional;
lookup->new_search = ISC_TRUE;
ISC_LIST_INIT(lookup->q);
ISC_LIST_APPEND(lookup_list, lookup, link);
lookup->origin = NULL;
ISC_LIST_INIT(lookup->my_server_list);
debug("looking up %s", lookup->textname);
return (lookup);
}
static void
flush_server_list(void) {
dig_server_t *s, *ps;
debug("flush_lookup_list()");
s = ISC_LIST_HEAD(server_list);
while (s != NULL) {
ps = s;
s = ISC_LIST_NEXT(s, link);
ISC_LIST_DEQUEUE(server_list, ps, link);
isc_mem_free(mctx, ps);
}
}
/*
* This works on the global server list, instead of on a per-lookup
* server list, since the change is persistent.
*/
static void
setsrv(char *opt) {
dig_server_t *srv;
flush_server_list();
srv=isc_mem_allocate(mctx, sizeof(struct dig_server));
if (srv == NULL)
fatal("Memory allocation failure.");
strncpy(srv->servername, opt, MXNAME-1);
ISC_LIST_APPEND(server_list, srv, link);
}
static void
get_next_command(void) {
char input[COMMSIZE];
char *ptr, *arg;
fputs("> ", stderr);
ptr = fgets(input, COMMSIZE, stdin);
if (ptr == NULL) {
in_use = ISC_FALSE;
return;
}
ptr = strtok(input, " \t\r\n");
if (ptr == NULL) {
in_use = ISC_FALSE;
return;
}
arg = strtok(NULL, " \t\r\n");
if ((strcasecmp(ptr, "set") == 0) &&
(arg != NULL))
setoption(arg);
else if ((strcasecmp(ptr, "server") == 0) ||
(strcasecmp(ptr, "lserver") == 0)) {
printf("Server:\t%s\n", arg);
setsrv(arg);
} else
addlookup(ptr);
}
static void
parse_args(int argc, char **argv) {
dig_lookup_t *lookup = NULL;
isc_boolean_t have_lookup = ISC_FALSE;
for (argc--, argv++; argc > 0; argc--, argv++) {
debug("main parsing %s", argv[0]);
if (argv[0][0] == '-') {
if ((argv[0][1] == 'h') &&
(argv[0][2] == 0)) {
show_usage();
exit (1);
}
if (argv[0][1] != 0)
setoption(&argv[0][1]);
else
have_lookup = ISC_TRUE;
} else {
if (!have_lookup) {
have_lookup = ISC_TRUE;
in_use = ISC_TRUE;
lookup = addlookup(argv[0]);
}
else
setsrv(argv[0]);
}
}
}
static void
flush_lookup_list(void) {
dig_lookup_t *l, *lp;
dig_query_t *q, *qp;
dig_server_t *s, *sp;
lookup_counter = 0;
l = ISC_LIST_HEAD(lookup_list);
while (l != NULL) {
q = ISC_LIST_HEAD(l->q);
while (q != NULL) {
if (q->sock != NULL) {
isc_socket_cancel(q->sock, NULL,
ISC_SOCKCANCEL_ALL);
isc_socket_detach(&q->sock);
}
if (ISC_LINK_LINKED(&q->recvbuf, link))
ISC_LIST_DEQUEUE(q->recvlist, &q->recvbuf,
link);
if (ISC_LINK_LINKED(&q->lengthbuf, link))
ISC_LIST_DEQUEUE(q->lengthlist, &q->lengthbuf,
link);
isc_buffer_invalidate(&q->recvbuf);
isc_buffer_invalidate(&q->lengthbuf);
qp = q;
q = ISC_LIST_NEXT(q, link);
ISC_LIST_DEQUEUE(l->q, qp, link);
isc_mem_free(mctx, qp);
}
s = ISC_LIST_HEAD(l->my_server_list);
while (s != NULL) {
sp = s;
s = ISC_LIST_NEXT(s, link);
ISC_LIST_DEQUEUE(l->my_server_list, sp, link);
isc_mem_free(mctx, sp);
}
if (l->sendmsg != NULL)
dns_message_destroy(&l->sendmsg);
if (l->timer != NULL)
isc_timer_detach(&l->timer);
lp = l;
l = ISC_LIST_NEXT(l, link);
ISC_LIST_DEQUEUE(lookup_list, lp, link);
isc_mem_free(mctx, lp);
}
}
int
main(int argc, char **argv) {
isc_result_t result;
ISC_LIST_INIT(lookup_list);
ISC_LIST_INIT(server_list);
ISC_LIST_INIT(search_list);
setup_libs();
progname = argv[0];
result = isc_mutex_init(&lock);
check_result(result, "isc_mutex_init");
result = isc_condition_init(&cond);
check_result(result, "isc_condition_init");
result = isc_mutex_trylock(&lock);
check_result(result, "isc_mutex_trylock");
parse_args(argc, argv);
if (deprecation_msg) {
fputs(
"Note: nslookup is deprecated and may be removed from future releases.\n"
"Consider using the `dig' or `host' programs instead. Run nslookup with\n"
"the `-sil[ent]' option to prevent this message from appearing.\n", stderr);
}
setup_system();
if (in_use) {
busy = ISC_TRUE;
start_lookup();
while (busy) {
result = isc_condition_wait(&cond, &lock);
check_result(result, "isc_condition_wait");
}
flush_lookup_list();
in_use = ISC_FALSE;
} else {
show_settings(ISC_FALSE);
in_use = ISC_TRUE;
}
while (in_use) {
get_next_command();
if (ISC_LIST_HEAD(lookup_list) != NULL) {
busy = ISC_TRUE;
start_lookup();
while (busy) {
result = isc_condition_wait(&cond, &lock);
check_result(result, "isc_condition_wait");
}
debug("out of the condition wait");
flush_lookup_list();
}
}
puts("");
debug("done, and starting to shut down");
destroy_libs();
isc_mutex_destroy(&lock);
isc_condition_destroy(&cond);
if (taskmgr != NULL) {
debug("freeing taskmgr");
isc_taskmgr_destroy(&taskmgr);
}
if (isc_mem_debugging != 0)
isc_mem_stats(mctx, stderr);
isc_app_finish();
if (mctx != NULL)
isc_mem_destroy(&mctx);
return (0);
}