dighost.c revision 6bc106bb7ed216ca3a86245181ea8e2b0b658a64
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/*
b7b6b01a0d0622181a4c28dd60401f0ab2480d00Mark Andrews * Copyright (C) 2000 Internet Software Consortium.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Permission to use, copy, modify, and distribute this software for any
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * purpose with or without fee is hereby granted, provided that the above
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * copyright notice and this permission notice appear in all copies.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * SOFTWARE.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews/* $Id: dighost.c,v 1.57 2000/06/23 20:57:19 mws Exp $ */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Notice to programmers: Do not use this code as an example of how to
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * use the ISC library to perform DNS lookups. Dig and Host both operate
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley * on the request level, since they allow fine-tuning of output and are
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * intended as debugging tools. As a result, they perform many of the
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * functions which could be better handled using the dns_resolver
fc8a6561cbb0a1fea849950a3217aacd3b8f1eacDavid Lawrence * functions in most applications.
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence */
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <config.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <stdlib.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <unistd.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#if defined(HAVE_ADDRINFO) && defined(HAVE_GETADDRINFO)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <netdb.h>
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews#include <string.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#else
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellingtonextern int h_errno;
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews#endif
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews#include <dns/message.h>
09f22ac5b09e70bc526015f37168ba33e21ea91fDavid Lawrence#include <dns/name.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/rdata.h>
92ef1a9b9dbd48ecb507b42ac62c15afefdaf838David Lawrence#include <dns/rdataclass.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <dns/rdatalist.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/rdataset.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dns/rdatastruct.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dns/rdatatype.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dns/result.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dns/tsig.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dst/dst.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/app.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/base64.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/entropy.h>
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence#include <isc/lang.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/lex.h>
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence#include <isc/netdb.h>
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews#include <isc/result.h>
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence#include <isc/string.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <isc/task.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <isc/timer.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <isc/types.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <isc/util.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dig/dig.h>
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas GustafssonISC_LIST(dig_lookup_t) lookup_list;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas GustafssonISC_LIST(dig_server_t) server_list;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas GustafssonISC_LIST(dig_searchlist_t) search_list;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc_boolean_t have_ipv6 = ISC_FALSE, specified_source = ISC_FALSE,
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein free_now = ISC_FALSE, show_details = ISC_FALSE, usesearch=ISC_TRUE,
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein qr = ISC_FALSE, is_dst_up = ISC_FALSE;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#ifdef TWIDDLE
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc_boolean_t twiddle = ISC_FALSE;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson#endif
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrencein_port_t port = 53;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrenceunsigned int timeout = 5;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrenceisc_mem_t *mctx = NULL;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafssonisc_taskmgr_t *taskmgr = NULL;
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellingtonisc_task_t *global_task = NULL;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafssonisc_timermgr_t *timermgr = NULL;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafssonisc_socketmgr_t *socketmgr = NULL;
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellingtonisc_sockaddr_t bind_address;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafssonisc_sockaddr_t bind_any;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafssonchar *rootspace[BUFSIZE];
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinisc_buffer_t rootbuf;
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrenceint sendcount = 0;
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrenceint sockcount = 0;
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrenceint ndots = -1;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonint tries = 3;
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafssonint lookup_counter = 0;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinchar fixeddomain[MXNAME]="";
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafssonint exitcode = 9;
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafssonchar keynametext[MXNAME];
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafssonchar keysecret[MXNAME]="";
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafssondns_name_t keyname;
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafssondns_tsig_keyring_t *keyring = NULL;
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafssonisc_buffer_t *namebuf = NULL;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssondns_tsigkey_t *key = NULL;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc_boolean_t validated = ISC_TRUE;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinisc_entropy_t *entp = NULL;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonextern isc_boolean_t isc_mem_debugging;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinisc_boolean_t debugging = ISC_FALSE;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrewsstatic void
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssoncancel_lookup(dig_lookup_t *lookup);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrewsstatic int
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssoncount_dots(char *string) {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein char *s;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein int i=0;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson s = string;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein while (*s != 0) {
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (*s == '.')
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson i++;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson s++;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence return (i);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic void
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonhex_dump(isc_buffer_t *b) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson unsigned int len;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_region_t r;
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence isc_buffer_remainingregion(b, &r);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson printf("Printing a buffer with length %d\n", r.length);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson for (len = 0 ; len < r.length ; len++) {
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington printf("%02x ", r.base[len]);
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington if (len != 0 && len % 16 == 0)
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington printf("\n");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (len % 16 != 0)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson printf("\n");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonvoid
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonfatal(const char *format, ...) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson va_list args;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson va_start(args, format);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence vfprintf(stderr, format, args);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence va_end(args);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson fprintf(stderr, "\n");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (exitcode == 0)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson exitcode = 8;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#ifdef NEVER
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dighost_shutdown();
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson free_lists(exitcode);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (mctx != NULL) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (isc_mem_debugging)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_mem_stats(mctx, stderr);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_mem_destroy(&mctx);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews }
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews#endif
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews exit(exitcode);
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews}
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrewsvoid
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrewsdebug(const char *format, ...) {
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews va_list args;
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews if (debugging) {
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews va_start(args, format);
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews vfprintf(stderr, format, args);
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews va_end(args);
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews fprintf(stderr, "\n");
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews }
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews}
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrewsvoid
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrewscheck_result(isc_result_t result, const char *msg) {
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews if (result != ISC_R_SUCCESS) {
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews exitcode = 1;
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews fatal("%s: %s", msg, isc_result_totext(result));
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews }
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews}
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrewsisc_boolean_t
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrewsisclass(char *text) {
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews /* Tests if a field is a class, without needing isc libs
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews * initialized. This list will have to be manually kept in
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews * sync with what the libs support.
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews */
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews const char *classlist[] = {"in", "hs", "chaos"};
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews const int numclasses = 3;
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews int i;
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews for (i = 0; i < numclasses; i++)
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews if (strcasecmp(text, classlist[i]) == 0)
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews return ISC_TRUE;
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews return ISC_FALSE;
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews}
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrewsisc_boolean_t
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrewsistype(char *text) {
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews /* Tests if a field is a type, without needing isc libs
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews * initialized. This list will have to be manually kept in
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews * sync with what the libs support.
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews */
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews const char *typelist[] = {"a", "ns", "md", "mf", "cname",
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews "soa", "mb", "mg", "mr", "null",
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews "wks", "ptr", "hinfo", "minfo",
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews "mx", "txt", "rp", "afsdb",
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews "x25", "isdn", "rt", "nsap",
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews "nsap_ptr", "sig", "key", "px",
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews "gpos", "aaaa", "loc", "nxt",
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews "srv", "naptr", "kx", "cert",
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews "a6", "dname", "opt", "unspec",
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews "tkey", "tsig", "axfr", "any"};
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews const int numtypes = 42;
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews int i;
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington for (i = 0; i < numtypes; i++) {
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington if (strcasecmp(text, typelist[i]) == 0)
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington return ISC_TRUE;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington }
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington return ISC_FALSE;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington}
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#ifdef TWIDDLE
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonvoid
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencetwiddlebuf(isc_buffer_t buf) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_region_t r;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson int len, pos, bit;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson unsigned char bitfield;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson int i, tw;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson hex_dump(&buf);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson tw=TWIDDLE;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson printf ("Twiddling %d bits: ", tw);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson for (i=0;i<tw;i++) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_buffer_usedregion (&buf, &r);
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson len = r.length;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson pos=(int)random();
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson pos = pos%len;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson bit = (int)random()%8;
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley bitfield = 1 << bit;
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley printf ("%d@%03x ", bit, pos);
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley r.base[pos] ^= bitfield;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson }
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews puts ("");
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson hex_dump(&buf);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington#endif
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellingtondig_lookup_t
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington*requeue_lookup(dig_lookup_t *lookold, isc_boolean_t servers) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dig_lookup_t *looknew;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dig_server_t *s, *srv;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson debug("requeue_lookup()");
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington if (free_now)
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington return(ISC_R_SUCCESS);
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington lookup_counter++;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington if (lookup_counter > LOOKUP_LIMIT)
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington fatal ("Too many lookups.");
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington looknew = isc_mem_allocate
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington (mctx, sizeof(struct dig_lookup));
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington if (looknew == NULL)
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington fatal ("Memory allocation failure in %s:%d",
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington __FILE__, __LINE__);
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington looknew->pending = ISC_FALSE;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington strncpy (looknew->textname, lookold-> textname, MXNAME);
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington strncpy (looknew->rttext, lookold-> rttext, 32);
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington strncpy (looknew->rctext, lookold-> rctext, 32);
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington looknew->namespace[0]=0;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington looknew->sendspace[0]=0;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington looknew->sendmsg=NULL;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington looknew->name=NULL;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington looknew->oname=NULL;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington looknew->timer = NULL;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington looknew->xfr_q = NULL;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington looknew->doing_xfr = lookold->doing_xfr;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington looknew->ixfr_serial = lookold->ixfr_serial;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington looknew->defname = lookold->defname;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington looknew->trace = lookold->trace;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew->trace_root = lookold->trace_root;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington looknew->identify = lookold->identify;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson looknew->udpsize = lookold->udpsize;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington looknew->recurse = lookold->recurse;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew->aaonly = lookold->aaonly;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence looknew->adflag = lookold->adflag;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence looknew->cdflag = lookold->cdflag;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew->ns_search_only = lookold->ns_search_only;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew->origin = NULL;
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence looknew->querysig = NULL;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff looknew->retries = tries;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson looknew->nsfound = 0;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew->tcp_mode = lookold->tcp_mode;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew->comments = lookold->comments;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew->stats = lookold->stats;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson looknew->section_question = lookold->section_question;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson looknew->section_answer = lookold->section_answer;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson looknew->section_authority = lookold->section_authority;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington looknew->section_additional = lookold->section_additional;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington looknew->new_search = ISC_FALSE;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington ISC_LIST_INIT(looknew->my_server_list);
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington ISC_LIST_INIT(looknew->q);
e724512f2be8d3de5f2982ebba0b2cdaf8325702Mark Andrews
e724512f2be8d3de5f2982ebba0b2cdaf8325702Mark Andrews looknew->use_my_server_list = ISC_FALSE;
e724512f2be8d3de5f2982ebba0b2cdaf8325702Mark Andrews if (servers) {
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington looknew->use_my_server_list = lookold->use_my_server_list;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington if (looknew->use_my_server_list) {
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington s = ISC_LIST_HEAD(lookold->my_server_list);
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington while (s != NULL) {
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington srv = isc_mem_allocate (mctx, sizeof(struct
5e387b9ce6bafdfadedb5b34e4c33a4404e5d589Brian Wellington dig_server));
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson if (srv == NULL)
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson fatal("Memory allocation failure "
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley "in %s:%d", __FILE__, __LINE__);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson strncpy(srv->servername, s->servername,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson MXNAME);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ISC_LIST_ENQUEUE(looknew->my_server_list, srv,
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence link);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson s = ISC_LIST_NEXT(s, link);
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley }
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington }
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson debug ("Before insertion, init@%lx "
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson "-> %lx, new@%lx "
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson "-> %lx", (long int)lookold,
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews (long int)lookold->link.next,
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews (long int)looknew, (long int)looknew->
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson link.next);
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson ISC_LIST_INSERTAFTER(lookup_list, lookold, looknew, link);
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson debug ("After insertion, init -> "
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews "%lx, new = %lx, "
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews "new -> %lx", (long int)lookold,
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews (long int)looknew, (long int)looknew->
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson link.next);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson return (looknew);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafssonvoid
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonsetup_system(void) {
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson char rcinput[MXNAME];
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson FILE *fp;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson char *ptr;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dig_server_t *srv;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dig_searchlist_t *search;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dig_lookup_t *l;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_boolean_t get_servers;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington isc_result_t result;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_buffer_t secretsrc;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_buffer_t secretbuf;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington int secretsize;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington unsigned char *secretstore;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington isc_lex_t *lex = NULL;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington isc_stdtime_t now;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington debug ("setup_system()");
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington if (fixeddomain[0]!=0) {
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington debug("Using fixed domain %s", fixeddomain);
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington search = isc_mem_allocate( mctx, sizeof(struct dig_server));
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington if (search == NULL)
5e387b9ce6bafdfadedb5b34e4c33a4404e5d589Brian Wellington fatal("Memory allocation failure in %s:%d",
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington __FILE__, __LINE__);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence strncpy(search->origin, fixeddomain, MXNAME - 1);
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley ISC_LIST_PREPEND(search_list, search, link);
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson }
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence free_now = ISC_FALSE;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson get_servers = ISC_TF(server_list.head == NULL);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson fp = fopen (RESOLVCONF, "r");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (fp != NULL) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson while (fgets(rcinput, MXNAME, fp) != 0) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ptr = strtok (rcinput, " \t\r\n");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (ptr != NULL) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (get_servers &&
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson strcasecmp(ptr, "nameserver") == 0) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson debug ("Got a nameserver line");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ptr = strtok (NULL, " \t\r\n");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (ptr != NULL) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson srv = isc_mem_allocate(mctx,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson sizeof(struct dig_server));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (srv == NULL)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson fatal("Memory "
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson "allocation "
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson "failure in "
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson "%s:%d",
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson __FILE__,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson __LINE__);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson strncpy((char *)srv->
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson servername,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ptr,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson MXNAME - 1);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ISC_LIST_APPEND
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson (server_list,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson srv, link);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews }
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews } else if (strcasecmp(ptr, "options") == 0) {
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson ptr = strtok(NULL, " \t\r\n");
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews if (ptr != NULL) {
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence if ((strncasecmp(ptr, "ndots:",
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson 6) == 0) &&
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence (ndots == -1)) {
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson ndots = atoi(
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington &ptr[6]);
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington debug ("ndots is "
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington "%d.",
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington ndots);
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson }
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson }
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson } else if ((strcasecmp(ptr, "search") == 0)
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson && usesearch){
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson while ((ptr = strtok(NULL, " \t\r\n"))
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson != NULL) {
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington search = isc_mem_allocate(
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence mctx, sizeof(struct
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence dig_server));
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence if (search == NULL)
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence fatal("Memory "
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson "allocation "
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence "failure in %s:"
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence "%d", __FILE__,
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson __LINE__);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence strncpy(search->
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence origin,
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence ptr,
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence MXNAME - 1);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence ISC_LIST_APPEND
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson (search_list,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson search,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson link);
b120e39deac3e9c323cdc7801ed2313f8ebbb744Andreas Gustafsson }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson } else if ((strcasecmp(ptr, "domain") == 0) &&
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson (fixeddomain[0] == 0 )){
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson while ((ptr = strtok(NULL, " \t\r\n"))
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence != NULL) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson search = isc_mem_allocate(
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson mctx, sizeof(struct
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dig_server));
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence if (search == NULL)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson fatal("Memory "
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson "allocation "
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson "failure in %s:"
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington "%d", __FILE__,
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington __LINE__);
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington strncpy(search->
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington origin,
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington ptr,
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington MXNAME - 1);
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington ISC_LIST_PREPEND
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson (search_list,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson search,
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence link);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence }
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence fclose (fp);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (ndots == -1)
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson ndots = 1;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson if (server_list.head == NULL) {
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington srv = isc_mem_allocate(mctx, sizeof(dig_server_t));
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington if (srv == NULL)
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington fatal("Memory allocation failure");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson strcpy(srv->servername, "127.0.0.1");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ISC_LIST_APPEND(server_list, srv, link);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson for (l = ISC_LIST_HEAD(lookup_list) ;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence l != NULL;
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence l = ISC_LIST_NEXT(l, link) ) {
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson l -> origin = ISC_LIST_HEAD(search_list);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson if (keysecret[0] != 0) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson debug("keyring");
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington result = dns_tsigkeyring_create(mctx, &keyring);
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington check_result(result, "dns_tsigkeyring_create");
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington debug("buffer");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson result = isc_buffer_allocate(mctx, &namebuf, MXNAME);
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington check_result(result, "isc_buffer_allocate");
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington debug("name");
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence dns_name_init(&keyname, NULL);
bd703253b745aaec6e45a60108bec513bf79bd2cBrian Wellington check_result(result, "dns_name_init");
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington isc_buffer_putstr(namebuf, keynametext);
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington secretsize = strlen(keysecret) * 3 / 4;
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington debug("secretstore");
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence secretstore = isc_mem_get(mctx, secretsize);
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington ENSURE (secretstore != NULL);
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington isc_buffer_init(&secretsrc, keysecret, strlen(keysecret));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_buffer_add(&secretsrc, strlen(keysecret));
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson isc_buffer_init(&secretbuf, secretstore, secretsize);
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson debug("lex");
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence result = isc_lex_create(mctx, strlen(keysecret), &lex);
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson check_result(result, "isc_lex_create");
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence result = isc_lex_openbuffer(lex, &secretsrc);
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson check_result(result, "isc_lex_openbuffer");
618a5e4923c3d064c1cc0f1866e27e8ddca90700Andreas Gustafsson result = isc_base64_tobuffer(lex, &secretbuf, -1);
618a5e4923c3d064c1cc0f1866e27e8ddca90700Andreas Gustafsson if (result != ISC_R_SUCCESS) {
618a5e4923c3d064c1cc0f1866e27e8ddca90700Andreas Gustafsson printf (";; Couldn't create key %s: %s\n",
618a5e4923c3d064c1cc0f1866e27e8ddca90700Andreas Gustafsson keynametext, isc_result_totext(result));
618a5e4923c3d064c1cc0f1866e27e8ddca90700Andreas Gustafsson isc_lex_close(lex);
7acfb3bcd29db4f19aa42690826e57dafdd28cdaAndreas Gustafsson isc_lex_destroy(&lex);
618a5e4923c3d064c1cc0f1866e27e8ddca90700Andreas Gustafsson goto SYSSETUP_FAIL;
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence }
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson secretsize = isc_buffer_usedlength(&secretbuf);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence debug("close");
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson isc_lex_close(lex);
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson isc_lex_destroy(&lex);
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson isc_stdtime_get(&now);
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson debug("namefromtext");
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence result = dns_name_fromtext(&keyname, namebuf,
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence dns_rootname, ISC_FALSE,
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson namebuf);
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson if (result != ISC_R_SUCCESS) {
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson printf (";; Couldn't create key %s: %s\n",
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson keynametext, dns_result_totext(result));
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson goto SYSSETUP_FAIL;
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson }
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson debug("tsigkey");
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson result = dns_tsigkey_create(&keyname, dns_tsig_hmacmd5_name,
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson secretstore, secretsize,
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson ISC_TRUE, NULL, now, now, mctx,
618a5e4923c3d064c1cc0f1866e27e8ddca90700Andreas Gustafsson keyring, &key);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (result != ISC_R_SUCCESS) {
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson printf (";; Couldn't create key %s: %s\n",
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson keynametext, dns_result_totext(result));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence isc_mem_put(mctx, secretstore, secretsize);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dns_name_invalidate(&keyname);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_buffer_free(&namebuf);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson return;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson SYSSETUP_FAIL:
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence isc_mem_put(mctx, secretstore, secretsize);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dns_name_invalidate(&keyname);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_buffer_free(&namebuf);
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington dns_tsigkeyring_destroy(&keyring);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson return;
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson }
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews}
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafssonvoid
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrencesetup_libs(void) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_result_t result;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson debug ("setup_libs()");
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence /*
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Warning: This is not particularly good randomness. We'll
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * just use random() now for getting id values, but doing so
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * does NOT insure that id's cann't be guessed.
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews */
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence srandom (getpid() + (int)&setup_libs);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley result = isc_app_start();
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson check_result(result, "isc_app_start");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson result = isc_net_probeipv4();
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson check_result(result, "isc_net_probeipv4");
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson result = isc_net_probeipv6();
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (result == ISC_R_SUCCESS)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson have_ipv6=ISC_TRUE;
e485d16462b0a66b835107fae04fe9d562e77af0Bob Halley
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson result = isc_mem_create(0, 0, &mctx);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson check_result(result, "isc_mem_create");
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson result = isc_taskmgr_create (mctx, 1, 0, &taskmgr);
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson check_result(result, "isc_taskmgr_create");
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson result = isc_task_create (taskmgr, 0, &global_task);
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson check_result(result, "isc_task_create");
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson result = isc_timermgr_create (mctx, &timermgr);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence check_result(result, "isc_timermgr_create");
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson result = isc_socketmgr_create (mctx, &socketmgr);
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson check_result(result, "isc_socketmgr_create");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson result = isc_entropy_create (mctx, &entp);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson check_result(result, "isc_entropy_create");
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson result = dst_lib_init (mctx, entp, 0);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson check_result(result, "dst_lib_init");
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson is_dst_up = ISC_TRUE;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson}
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic void
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrenceadd_opt (dns_message_t *msg, isc_uint16_t udpsize) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dns_rdataset_t *rdataset = NULL;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dns_rdatalist_t *rdatalist = NULL;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dns_rdata_t *rdata = NULL;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_result_t result;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson debug ("add_opt()");
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson result = dns_message_gettemprdataset(msg, &rdataset);
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson check_result (result, "dns_message_gettemprdataset");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dns_rdataset_init (rdataset);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews result = dns_message_gettemprdatalist(msg, &rdatalist);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews check_result (result, "dns_message_gettemprdatalist");
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews result = dns_message_gettemprdata(msg, &rdata);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews check_result (result, "dns_message_gettemprdata");
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews debug ("Setting udp size of %d", udpsize);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews rdatalist->type = dns_rdatatype_opt;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews rdatalist->covers = 0;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews rdatalist->rdclass = udpsize;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews rdatalist->ttl = 0;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson rdata->data = NULL;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence rdata->length = 0;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ISC_LIST_INIT(rdatalist->rdata);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson dns_rdatalist_tordataset(rdatalist, rdataset);
e724512f2be8d3de5f2982ebba0b2cdaf8325702Mark Andrews result = dns_message_setopt(msg, rdataset);
e724512f2be8d3de5f2982ebba0b2cdaf8325702Mark Andrews check_result (result, "dns_message_setopt");
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews}
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafssonstatic void
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafssonadd_type(dns_message_t *message, dns_name_t *name, dns_rdataclass_t rdclass,
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson dns_rdatatype_t rdtype)
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson{
3476dfe9d7e0a13338a16ee096f68d73c5a60bf2Mark Andrews dns_rdataset_t *rdataset;
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson isc_result_t result;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson debug ("add_type()");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson rdataset = NULL;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson result = dns_message_gettemprdataset(message, &rdataset);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson check_result(result, "dns_message_gettemprdataset()");
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson dns_rdataset_init(rdataset);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dns_rdataset_makequestion(rdataset, rdclass, rdtype);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson ISC_LIST_APPEND(name->list, rdataset, link);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafssonstatic void
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssoncheck_next_lookup(dig_lookup_t *lookup) {
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson dig_lookup_t *next;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson dig_query_t *query;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence isc_boolean_t still_working=ISC_FALSE;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson if (free_now)
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson return;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence debug("check_next_lookup(%lx)", (long int)lookup);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence for (query = ISC_LIST_HEAD(lookup->q);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence query != NULL;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson query = ISC_LIST_NEXT(query, link)) {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence if (query->working) {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence debug("Still have a worker.", stderr);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson still_working=ISC_TRUE;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence }
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence }
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson if (still_working)
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence return;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence debug ("Have %d retries left for %s",
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence lookup->retries-1, lookup->textname);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence debug ("Lookup %s pending", lookup->pending?"is":"is not");
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence next = ISC_LIST_NEXT(lookup, link);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence if (lookup->tcp_mode) {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence if (next == NULL) {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence debug("Shutting Down.", stderr);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence dighost_shutdown();
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence return;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence }
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence if (next->sendmsg == NULL) {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence debug ("Setting up for TCP");
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence setup_lookup(next);
acc8b7ac3d16538bf223545bbf71899b9accaebbMark Andrews do_lookup(next);
acc8b7ac3d16538bf223545bbf71899b9accaebbMark Andrews }
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence } else {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence if (!lookup->pending) {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence if (next == NULL) {
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson debug("Shutting Down.", stderr);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson dighost_shutdown();
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson return;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson }
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson if (next->sendmsg == NULL) {
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson debug ("Setting up for UDP");
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson setup_lookup(next);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence do_lookup(next);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence }
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson } else {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence if (lookup->retries > 1) {
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence debug ("Retrying");
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence lookup->retries --;
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence if (lookup->timer != NULL)
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence isc_timer_detach(&lookup->timer);
acc8b7ac3d16538bf223545bbf71899b9accaebbMark Andrews send_udp(lookup);
acc8b7ac3d16538bf223545bbf71899b9accaebbMark Andrews } else {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence debug ("Cancelling");
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson cancel_lookup(lookup);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson }
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson }
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson }
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson}
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafssonstatic void
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafssonfollowup_lookup(dns_message_t *msg, dig_query_t *query,
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff dns_section_t section) {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence dig_lookup_t *lookup = NULL;
82ca33427bdd4f3bc4ed3431e86bd810fe751674Andreas Gustafsson dig_server_t *srv = NULL;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence dns_rdataset_t *rdataset = NULL;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson dns_rdata_t rdata;
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff dns_name_t *name = NULL;
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff isc_result_t result, loopresult;
86a292d45977f1d8400df04b04bf659d9a5d68a1Andreas Gustafsson isc_buffer_t *b = NULL;
86a292d45977f1d8400df04b04bf659d9a5d68a1Andreas Gustafsson isc_region_t r;
86a292d45977f1d8400df04b04bf659d9a5d68a1Andreas Gustafsson isc_boolean_t success = ISC_FALSE;
86a292d45977f1d8400df04b04bf659d9a5d68a1Andreas Gustafsson int len;
937ccb270ef87dc24e30fcca15dd23ebd20873abAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson debug ("followup_lookup()");
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence if (free_now)
937ccb270ef87dc24e30fcca15dd23ebd20873abAndreas Gustafsson return;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson result = dns_message_firstname (msg,section);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson if (result != ISC_R_SUCCESS) {
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence debug ("Firstname returned %s",
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence isc_result_totext(result));
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence if ((section == DNS_SECTION_ANSWER) &&
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence (query->lookup->trace || query->lookup->ns_search_only))
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews followup_lookup (msg, query, DNS_SECTION_AUTHORITY);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews return;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews }
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews debug ("Following up %s", query->lookup->textname);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews for (;;) {
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews name = NULL;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews dns_message_currentname(msg, section, &name);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews for (rdataset = ISC_LIST_HEAD(name->list);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews rdataset != NULL;
86a292d45977f1d8400df04b04bf659d9a5d68a1Andreas Gustafsson rdataset = ISC_LIST_NEXT(rdataset, link)) {
86a292d45977f1d8400df04b04bf659d9a5d68a1Andreas Gustafsson loopresult = dns_rdataset_first(rdataset);
86a292d45977f1d8400df04b04bf659d9a5d68a1Andreas Gustafsson while (loopresult == ISC_R_SUCCESS) {
86a292d45977f1d8400df04b04bf659d9a5d68a1Andreas Gustafsson dns_rdataset_current(rdataset, &rdata);
86a292d45977f1d8400df04b04bf659d9a5d68a1Andreas Gustafsson debug ("Got rdata with type %d",
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson rdata.type);
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson if ((rdata.type == dns_rdatatype_ns) &&
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence (!query->lookup->trace_root ||
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence (query->lookup->nsfound < ROOTNS)))
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence query->lookup->nsfound++;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence result = isc_buffer_allocate(mctx, &b,
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence BUFSIZE);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence check_result (result,
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence "isc_buffer_allocate");
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence result = dns_rdata_totext (&rdata,
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence NULL,
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence b);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence check_result (result,
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence "dns_rdata_totext");
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence isc_buffer_usedregion(b, &r);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence len = r.length-1;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence if (len >= MXNAME)
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews len = MXNAME-1;
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson /* Initialize lookup if we've not yet */
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson debug ("Found NS %d %.*s",
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson (int)r.length, (int)r.length,
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson (char *)r.base);
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson if (!success) {
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews success = ISC_TRUE;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews lookup_counter++;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson lookup = requeue_lookup
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson (query->lookup,
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson ISC_FALSE);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson lookup->doing_xfr = ISC_FALSE;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson lookup->defname = ISC_FALSE;
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff lookup->use_my_server_list =
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence ISC_TRUE;
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews if (section ==
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews DNS_SECTION_ANSWER) {
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff lookup->trace =
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence ISC_FALSE;
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence lookup->ns_search_only =
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence ISC_FALSE;
937ccb270ef87dc24e30fcca15dd23ebd20873abAndreas Gustafsson }
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence else {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence lookup->trace =
ba94ff2e56ff8cafeb5a701adfbe71bf9a5932e7Mark Andrews query->
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence lookup->trace;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence lookup->ns_search_only =
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence query->
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence lookup->ns_search_only;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson }
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff lookup->trace_root = ISC_FALSE;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson ISC_LIST_INIT(lookup->
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson my_server_list);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson }
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson srv = isc_mem_allocate (mctx,
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews sizeof(
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson struct
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dig_server));
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence if (srv == NULL)
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson fatal("Memory allocation "
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence "failure in %s:%d",
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson __FILE__, __LINE__);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson strncpy(srv->servername,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson (char *)r.base, len);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson srv->servername[len]=0;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson debug ("Adding server %s",
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews srv->servername);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence ISC_LIST_APPEND
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence (lookup->my_server_list,
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson srv, link);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_buffer_free (&b);
f3083bc781333f6314a44cb19fe31fe17e59f26fDavid Lawrence }
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence loopresult = dns_rdataset_next(rdataset);
f3083bc781333f6314a44cb19fe31fe17e59f26fDavid Lawrence }
f3083bc781333f6314a44cb19fe31fe17e59f26fDavid Lawrence }
f3083bc781333f6314a44cb19fe31fe17e59f26fDavid Lawrence result = dns_message_nextname (msg, section);
f3083bc781333f6314a44cb19fe31fe17e59f26fDavid Lawrence if (result != ISC_R_SUCCESS)
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence break;
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence }
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence if ((lookup == NULL) && (section == DNS_SECTION_ANSWER) &&
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson (query->lookup->trace || query->lookup->ns_search_only))
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence followup_lookup(msg, query, DNS_SECTION_AUTHORITY);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence}
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrencestatic void
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrencenext_origin(dns_message_t *msg, dig_query_t *query) {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence dig_lookup_t *lookup;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence UNUSED (msg);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence debug ("next_origin()");
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence if (free_now)
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence return;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence debug ("Following up %s", query->lookup->textname);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence if (query->lookup->origin == NULL) { /*Then we just did rootorg;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence there's nothing left. */
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence debug ("Made it to the root whith nowhere to go.");
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence return;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence }
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence lookup = requeue_lookup(query->lookup, ISC_TRUE);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence lookup->defname = ISC_FALSE;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence lookup->origin = ISC_LIST_NEXT(query->lookup->origin, link);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence}
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrencestatic void
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrenceinsert_soa(dig_lookup_t *lookup) {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence isc_result_t result;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence dns_rdata_soa_t soa;
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews dns_rdata_t *rdata = NULL;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence dns_rdatalist_t *rdatalist = NULL;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence dns_rdataset_t *rdataset = NULL;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dns_name_t *soaname = NULL;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson debug ("insert_soa()");
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews soa.mctx = mctx;
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews soa.serial = lookup->ixfr_serial;
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence soa.refresh = 1;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence soa.retry = 1;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence soa.expire = 1;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence soa.minimum = 1;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence soa.common.rdclass = dns_rdataclass_in;
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence soa.common.rdtype = dns_rdatatype_soa;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence dns_name_init(&soa.origin, NULL);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence dns_name_init(&soa.mname, NULL);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews dns_name_clone(lookup->name, &soa.origin);
acc8b7ac3d16538bf223545bbf71899b9accaebbMark Andrews dns_name_clone(lookup->name, &soa.mname);
acc8b7ac3d16538bf223545bbf71899b9accaebbMark Andrews
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence isc_buffer_init(&lookup->rdatabuf, lookup->rdatastore,
dee489d6c1487c5fb450e352c48b1a37848e9b6cMark Andrews MXNAME);
dee489d6c1487c5fb450e352c48b1a37848e9b6cMark Andrews
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson result = dns_message_gettemprdata(lookup->sendmsg, &rdata);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson check_result(result, "dns_message_gettemprdata");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson result = dns_rdata_fromstruct(rdata, dns_rdataclass_in,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dns_rdatatype_soa, &soa,
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence &lookup->rdatabuf);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson check_result(result, "isc_rdata_fromstruct");
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson result = dns_message_gettemprdatalist(lookup->sendmsg, &rdatalist);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson check_result(result, "dns_message_gettemprdatalist");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson result = dns_message_gettemprdataset(lookup->sendmsg, &rdataset);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews check_result(result, "dns_message_gettemprdataset");
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson dns_rdatalist_init(rdatalist);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson rdatalist->type = dns_rdatatype_soa;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson rdatalist->rdclass = dns_rdataclass_in;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence rdatalist->covers = dns_rdatatype_soa;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson rdatalist->ttl = 1;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ISC_LIST_INIT(rdatalist->rdata);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson dns_rdataset_init(rdataset);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence dns_rdatalist_tordataset(rdatalist, rdataset);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson result = dns_message_gettempname(lookup->sendmsg, &soaname);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson check_result(result, "dns_message_gettempname");
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence dns_name_init(soaname, NULL);
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence dns_name_clone(lookup->name, soaname);
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence ISC_LIST_INIT(soaname->list);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson ISC_LIST_APPEND(soaname->list, rdataset, link);
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson dns_message_addname(lookup->sendmsg, soaname, DNS_SECTION_AUTHORITY);
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson}
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafssonvoid
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafssonsetup_lookup(dig_lookup_t *lookup) {
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence isc_result_t result, res2;
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence int len;
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence dns_rdatatype_t rdtype;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson dns_rdataclass_t rdclass;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dig_server_t *serv;
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence dig_query_t *query;
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence isc_region_t r;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence isc_constregion_t tr;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_buffer_t b;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson char store[MXNAME];
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE (lookup != NULL);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson debug("setup_lookup(%lx)",(long int)lookup);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson if (free_now)
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson return;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence debug("Setting up for looking up %s @%lx->%lx",
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson lookup->textname, (long int)lookup,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson (long int)lookup->link.next);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews result = dns_message_create(mctx, DNS_MESSAGE_INTENTRENDER,
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews &lookup->sendmsg);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews check_result(result, "dns_message_create");
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews if (lookup->new_search) {
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence debug ("Resetting lookup counter.");
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson lookup_counter = 0;
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson }
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews result = dns_message_gettempname(lookup->sendmsg, &lookup->name);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews check_result(result, "dns_message_gettempname");
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence dns_name_init(lookup->name, NULL);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews isc_buffer_init(&lookup->namebuf, lookup->namespace, BUFSIZE);
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson isc_buffer_init(&lookup->onamebuf, lookup->onamespace, BUFSIZE);
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews if ((count_dots(lookup->textname) >= ndots) || lookup->defname)
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews lookup->origin = NULL; /* Force root lookup */
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews debug ("lookup->origin = %lx", (long int)lookup->origin);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews if (lookup->origin != NULL) {
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews debug ("Trying origin %s", lookup->origin->origin);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews result = dns_message_gettempname(lookup->sendmsg,
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews &lookup->oname);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews check_result(result, "dns_message_gettempname");
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews dns_name_init(lookup->oname, NULL);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence len=strlen(lookup->origin->origin);
ed8daea26475309e1ac8ed8556b7d4ec8ae4432cDavid Lawrence isc_buffer_init(&b, lookup->origin->origin, len);
ed8daea26475309e1ac8ed8556b7d4ec8ae4432cDavid Lawrence isc_buffer_add(&b, len);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence result = dns_name_fromtext(lookup->oname, &b, dns_rootname,
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence ISC_FALSE, &lookup->onamebuf);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence if (result != ISC_R_SUCCESS) {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence dns_message_puttempname(lookup->sendmsg,
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence &lookup->name);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence dns_message_puttempname(lookup->sendmsg,
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews &lookup->oname);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence fatal("Aborting: %s is not a legal name syntax. (%s)",
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence lookup->origin->origin,
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence dns_result_totext(result));
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence }
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence if (!lookup->trace_root) {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence len=strlen(lookup->textname);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence isc_buffer_init(&b, lookup->textname, len);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence isc_buffer_add(&b, len);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence result = dns_name_fromtext(lookup->name, &b,
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence lookup->oname, ISC_FALSE,
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence &lookup->namebuf);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence } else {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence isc_buffer_init(&b, ". ", 1);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence isc_buffer_add(&b, 1);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence result = dns_name_fromtext(lookup->name, &b,
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence lookup->oname, ISC_FALSE,
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence &lookup->namebuf);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews }
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews if (result != ISC_R_SUCCESS) {
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews dns_message_puttempname(lookup->sendmsg,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson &lookup->name);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dns_message_puttempname(lookup->sendmsg,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson &lookup->oname);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson fatal("Aborting: %s is not a legal name syntax. (%s)",
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson lookup->textname, dns_result_totext(result));
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff }
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence dns_message_puttempname(lookup->sendmsg, &lookup->oname);
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff } else {
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson debug ("Using root origin.");
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff if (!lookup->trace_root) {
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence len = strlen (lookup->textname);
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff isc_buffer_init(&b, lookup->textname, len);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence isc_buffer_add(&b, len);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence result = dns_name_fromtext(lookup->name, &b,
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence dns_rootname,
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence ISC_FALSE,
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence &lookup->namebuf);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence } else {
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence isc_buffer_init(&b, ". ", 1);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence isc_buffer_add(&b, 1);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson result = dns_name_fromtext(lookup->name, &b,
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson dns_rootname,
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson ISC_FALSE,
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson &lookup->namebuf);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence }
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson if (result != ISC_R_SUCCESS) {
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson dns_message_puttempname(lookup->sendmsg,
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson &lookup->name);
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson isc_buffer_init(&b, store, MXNAME);
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson res2 = dns_name_totext(dns_rootname, ISC_FALSE, &b);
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson check_result (res2, "dns_name_totext");
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson isc_buffer_usedregion (&b, &r);
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson fatal("Aborting: %s/%.*s is not a legal name syntax. "
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson "(%s)", lookup->textname, (int)r.length,
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson (char *)r.base, dns_result_totext(result));
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson }
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_buffer_init (&b, store, MXNAME);
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson dns_name_totext(lookup->name, ISC_FALSE, &b);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_buffer_usedregion (&b, &r);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson trying((int)r.length, (char *)r.base, lookup);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (dns_name_isabsolute(lookup->name))
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington debug ("This is an absolute name.");
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington else
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington debug ("This is a relative name (which is wrong).");
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington if (lookup->rctext[0] == 0)
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington strcpy(lookup->rctext, "IN");
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington if (lookup->rttext[0] == 0)
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington strcpy(lookup->rttext, "A");
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington lookup->sendmsg->id = (unsigned short)(random() & 0xFFFF);
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington lookup->sendmsg->opcode = dns_opcode_query;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington lookup->msgcounter = 0;
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington /*
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington * If this is a trace request, completely disallow recursion, since
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews * it's meaningless for traces.
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews */
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews if (lookup->recurse && !lookup->trace && !lookup->ns_search_only) {
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews debug ("Recursive query");
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews lookup->sendmsg->flags |= DNS_MESSAGEFLAG_RD;
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews }
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews if (lookup->aaonly) {
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews debug ("AA query");
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews lookup->sendmsg->flags |= DNS_MESSAGEFLAG_AA;
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews }
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews if (lookup->adflag) {
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews debug ("AD query");
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews lookup->sendmsg->flags |= DNS_MESSAGEFLAG_AD;
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews }
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews if (lookup->cdflag) {
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews debug ("CD query");
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews lookup->sendmsg->flags |= DNS_MESSAGEFLAG_CD;
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews }
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews dns_message_addname(lookup->sendmsg, lookup->name,
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews DNS_SECTION_QUESTION);
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews if (lookup->trace_root) {
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews debug("Doing trace_root");
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews tr.base="SOA";
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews tr.length=3;
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews } else {
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews tr.base=lookup->rttext;
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews tr.length=strlen(lookup->rttext);
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews }
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews debug ("Data type is %s", lookup->rttext);
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews result = dns_rdatatype_fromtext(&rdtype, (isc_textregion_t *)&tr);
e90994e3f358bdc6fe03005da08567c807a14c1dMark Andrews check_result(result, "dns_rdatatype_fromtext");
e90994e3f358bdc6fe03005da08567c807a14c1dMark Andrews if ((rdtype == dns_rdatatype_axfr) ||
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews (rdtype == dns_rdatatype_ixfr)) {
e90994e3f358bdc6fe03005da08567c807a14c1dMark Andrews lookup->doing_xfr = ISC_TRUE;
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews /*
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews * Force TCP mode if we're doing an xfr.
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews */
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews lookup->tcp_mode = ISC_TRUE;
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews }
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews if (lookup->trace_root) {
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews tr.base="IN";
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews tr.length=2;
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews } else {
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews tr.base=lookup->rctext;
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews tr.length=strlen(lookup->rctext);
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews }
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews result = dns_rdataclass_fromtext(&rdclass, (isc_textregion_t *)&tr);
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews check_result(result, "dns_rdataclass_fromtext");
add_type(lookup->sendmsg, lookup->name, rdclass, rdtype);
if (rdtype == dns_rdatatype_ixfr)
insert_soa(lookup);
if (key != NULL) {
debug ("Initializing keys");
result = dns_message_settsigkey(lookup->sendmsg, key);
check_result(result, "dns_message_settsigkey");
lookup->tsigctx = NULL;
lookup->querysig = NULL;
}
debug ("Starting to render the message");
isc_buffer_init(&lookup->sendbuf, lookup->sendspace, COMMSIZE);
result = dns_message_renderbegin(lookup->sendmsg, &lookup->sendbuf);
check_result(result, "dns_message_renderbegin");
if (lookup->udpsize > 0) {
add_opt(lookup->sendmsg, lookup->udpsize);
}
result = dns_message_rendersection(lookup->sendmsg,
DNS_SECTION_QUESTION, 0);
check_result(result, "dns_message_rendersection");
result = dns_message_rendersection(lookup->sendmsg,
DNS_SECTION_AUTHORITY, 0);
check_result(result, "dns_message_rendersection");
result = dns_message_renderend(lookup->sendmsg);
check_result(result, "dns_message_renderend");
debug ("Done rendering.");
lookup->pending = ISC_FALSE;
if (lookup->use_my_server_list)
serv = ISC_LIST_HEAD(lookup->my_server_list);
else
serv = ISC_LIST_HEAD(server_list);
for (; serv != NULL;
serv = ISC_LIST_NEXT(serv, link)) {
query = isc_mem_allocate(mctx, sizeof(dig_query_t));
if (query == NULL)
fatal("Memory allocation failure in %s:%d",
__FILE__, __LINE__);
debug ("Create query %lx linked to lookup %lx",
(long int)query, (long int)lookup);
query->lookup = lookup;
query->working = ISC_FALSE;
query->waiting_connect = ISC_FALSE;
query->first_pass = ISC_TRUE;
query->first_soa_rcvd = ISC_FALSE;
query->second_rr_rcvd = ISC_FALSE;
query->second_rr_serial = 0;
query->servname = serv->servername;
ISC_LIST_INIT(query->sendlist);
ISC_LIST_INIT(query->recvlist);
ISC_LIST_INIT(query->lengthlist);
query->sock = NULL;
isc_buffer_init(&query->recvbuf, query->recvspace, COMMSIZE);
isc_buffer_init(&query->lengthbuf, query->lengthspace, 2);
isc_buffer_init(&query->slbuf, query->slspace, 2);
ISC_LIST_ENQUEUE(lookup->q, query, link);
}
if (!ISC_LIST_EMPTY(lookup->q) && qr) {
printmessage (ISC_LIST_HEAD(lookup->q), lookup->sendmsg,
ISC_TRUE);
}
}
static void
send_done(isc_task_t *task, isc_event_t *event) {
UNUSED(task);
isc_event_free(&event);
debug("send_done()");
}
static void
cancel_lookup(dig_lookup_t *lookup) {
dig_query_t *query=NULL;
debug("cancel_lookup()");
for (query = ISC_LIST_HEAD(lookup->q);
query != NULL;
query = ISC_LIST_NEXT(query, link)) {
if (query->working) {
debug ("Cancelling a worker.");
}
if (query->sock != NULL) {
isc_socket_cancel(query->sock, global_task,
ISC_SOCKCANCEL_ALL);
isc_socket_detach(&query->sock);
sockcount--;
debug ("Socket = %d",sockcount);
}
}
lookup->pending = ISC_FALSE;
lookup->retries = 0;
check_next_lookup(lookup);
}
static void
recv_done(isc_task_t *task, isc_event_t *event);
static void
connect_timeout(isc_task_t *task, isc_event_t *event);
void
send_udp(dig_lookup_t *lookup) {
dig_query_t *query;
isc_result_t result;
debug ("send_udp()");
isc_interval_set(&lookup->interval, timeout, 0);
result = isc_timer_create(timermgr, isc_timertype_once, NULL,
&lookup->interval, global_task,
connect_timeout, lookup, &lookup->timer);
check_result(result, "isc_timer_create");
for (query = ISC_LIST_HEAD(lookup->q);
query != NULL;
query = ISC_LIST_NEXT(query, link)) {
debug ("Working on lookup %lx, query %lx",
(long int)query->lookup, (long int)query);
ISC_LIST_ENQUEUE(query->recvlist, &query->recvbuf, link);
query->working = ISC_TRUE;
debug ("recving with lookup=%lx, query=%lx, sock=%lx",
(long int)query->lookup, (long int)query,
(long int)query->sock);
result = isc_socket_recvv(query->sock, &query->recvlist, 1,
global_task, recv_done, query);
check_result(result, "isc_socket_recvv");
sendcount++;
debug("Sent count number %d", sendcount);
#ifdef TWIDDLE
if (twiddle) {
twiddlebuf(lookup->sendbuf);
}
#endif
ISC_LIST_ENQUEUE(query->sendlist, &lookup->sendbuf, link);
debug("Sending a request.");
result = isc_time_now(&query->time_sent);
check_result(result, "isc_time_now");
ENSURE (query->sock != NULL);
result = isc_socket_sendtov(query->sock, &query->sendlist,
global_task, send_done, query,
&query->sockaddr, NULL);
check_result(result, "isc_socket_sendtov");
}
}
/* connect_timeout is used for both UDP recieves and TCP connects. */
static void
connect_timeout(isc_task_t *task, isc_event_t *event) {
dig_lookup_t *lookup=NULL, *next=NULL;
dig_query_t *q=NULL;
isc_result_t result;
isc_buffer_t *b=NULL;
isc_region_t r;
REQUIRE(event->ev_type == ISC_TIMEREVENT_IDLE);
debug("connect_timeout()");
lookup = event->ev_arg;
isc_event_free(&event);
debug ("Buffer Allocate connect_timeout");
result = isc_buffer_allocate(mctx, &b, 256);
check_result(result, "isc_buffer_allocate");
for (q = ISC_LIST_HEAD(lookup->q);
q != NULL;
q = ISC_LIST_NEXT(q, link)) {
if (q->working) {
if (!free_now) {
isc_buffer_clear(b);
result = isc_sockaddr_totext(&q->sockaddr, b);
check_result(result, "isc_sockaddr_totext");
isc_buffer_usedregion(b, &r);
if (q->lookup->retries > 1)
printf(";; Connection to server %.*s "
"for %s timed out. "
"Retrying %d.\n",
(int)r.length, r.base,
q->lookup->textname,
q->lookup->retries-1);
else {
if (lookup->tcp_mode) {
printf(";; Connection to "
"server %.*s "
"for %s timed out. "
"Giving up.\n",
(int)r.length, r.base,
q->lookup->textname);
} else {
printf(";; Connection to "
"server %.*s "
"for %s timed out. "
"Trying TCP.\n",
(int)r.length, r.base,
q->lookup->textname);
next = requeue_lookup
(lookup,ISC_TRUE);
next->tcp_mode = ISC_TRUE;
}
}
}
isc_socket_cancel(q->sock, task,
ISC_SOCKCANCEL_ALL);
}
}
ENSURE(lookup->timer != NULL);
isc_timer_detach(&lookup->timer);
isc_buffer_free(&b);
debug ("Done with connect_timeout()");
}
static void
tcp_length_done(isc_task_t *task, isc_event_t *event) {
isc_socketevent_t *sevent;
isc_buffer_t *b=NULL;
isc_region_t r;
isc_result_t result;
dig_query_t *query=NULL;
isc_uint16_t length;
REQUIRE(event->ev_type == ISC_SOCKEVENT_RECVDONE);
UNUSED(task);
debug("tcp_length_done()");
if (free_now) {
isc_event_free(&event);
return;
}
sevent = (isc_socketevent_t *)event;
query = event->ev_arg;
if (sevent->result == ISC_R_CANCELED) {
query->working = ISC_FALSE;
check_next_lookup(query->lookup);
isc_event_free(&event);
return;
}
if (sevent->result != ISC_R_SUCCESS) {
debug ("Buffer Allocate connect_timeout");
result = isc_buffer_allocate(mctx, &b, 256);
check_result(result, "isc_buffer_allocate");
result = isc_sockaddr_totext(&query->sockaddr, b);
check_result(result, "isc_sockaddr_totext");
isc_buffer_usedregion(b, &r);
printf("%.*s: %s\n", (int)r.length, r.base,
isc_result_totext(sevent->result));
isc_buffer_free(&b);
query->working = ISC_FALSE;
sockcount--;
debug ("Socket = %d",sockcount);
isc_socket_detach(&query->sock);
check_next_lookup(query->lookup);
isc_event_free(&event);
return;
}
b = ISC_LIST_HEAD(sevent->bufferlist);
ISC_LIST_DEQUEUE(sevent->bufferlist, &query->lengthbuf, link);
length = isc_buffer_getuint16(b);
if (length > COMMSIZE) {
isc_event_free (&event);
fatal ("Length of %X was longer than I can handle!",
length);
}
/*
* Even though the buffer was already init'ed, we need
* to redo it now, to force the length we want.
*/
isc_buffer_invalidate(&query->recvbuf);
isc_buffer_init(&query->recvbuf, query->recvspace, length);
ENSURE(ISC_LIST_EMPTY(query->recvlist));
ISC_LIST_ENQUEUE(query->recvlist, &query->recvbuf, link);
debug ("recving with lookup=%lx, query=%lx",
(long int)query->lookup, (long int)query);
result = isc_socket_recvv(query->sock, &query->recvlist, length, task,
recv_done, query);
check_result(result, "isc_socket_recvv");
debug("Resubmitted recv request with length %d", length);
isc_event_free(&event);
}
static void
launch_next_query(dig_query_t *query, isc_boolean_t include_question) {
isc_result_t result;
debug("launch_next_query()");
if (free_now)
return;
if (!query->lookup->pending) {
debug("Ignoring launch_next_query because !pending.");
sockcount--;
debug ("Socket = %d",sockcount);
isc_socket_detach(&query->sock);
query->working = ISC_FALSE;
query->waiting_connect = ISC_FALSE;
check_next_lookup(query->lookup);
return;
}
isc_buffer_clear(&query->slbuf);
isc_buffer_clear(&query->lengthbuf);
isc_buffer_putuint16(&query->slbuf, query->lookup->sendbuf.used);
ISC_LIST_ENQUEUE(query->sendlist, &query->slbuf, link);
if (include_question) {
#ifdef TWIDDLE
if (twiddle) {
twiddlebuf(query->lookup->sendbuf);
}
#endif
ISC_LIST_ENQUEUE(query->sendlist, &query->lookup->sendbuf,
link);
}
ISC_LIST_ENQUEUE(query->lengthlist, &query->lengthbuf, link);
result = isc_socket_recvv(query->sock, &query->lengthlist, 0,
global_task, tcp_length_done, query);
check_result(result, "isc_socket_recvv");
sendcount++;
if (!query->first_soa_rcvd) {
debug("Sending a request.");
result = isc_time_now(&query->time_sent);
check_result(result, "isc_time_now");
result = isc_socket_sendv(query->sock, &query->sendlist,
global_task, send_done, query);
check_result(result, "isc_socket_recvv");
}
query->waiting_connect = ISC_FALSE;
check_next_lookup(query->lookup);
return;
}
static void
connect_done(isc_task_t *task, isc_event_t *event) {
isc_result_t result;
isc_socketevent_t *sevent=NULL;
dig_query_t *query=NULL;
isc_buffer_t *b=NULL;
isc_region_t r;
UNUSED(task);
REQUIRE(event->ev_type == ISC_SOCKEVENT_CONNECT);
debug ("connect_done()");
if (free_now) {
isc_event_free(&event);
return;
}
sevent = (isc_socketevent_t *)event;
query = sevent->ev_arg;
REQUIRE(query->waiting_connect);
query->waiting_connect = ISC_FALSE;
if (sevent->result != ISC_R_SUCCESS) {
debug ("Buffer Allocate connect_timeout");
result = isc_buffer_allocate(mctx, &b, 256);
check_result(result, "isc_buffer_allocate");
result = isc_sockaddr_totext(&query->sockaddr, b);
check_result(result, "isc_sockaddr_totext");
isc_buffer_usedregion(b, &r);
printf(";; Connection to server %.*s for %s failed: %s.\n",
(int)r.length, r.base, query->lookup->textname,
isc_result_totext(sevent->result));
if (exitcode < 9)
exitcode = 9;
isc_buffer_free(&b);
query->working = ISC_FALSE;
query->waiting_connect = ISC_FALSE;
check_next_lookup(query->lookup);
isc_event_free(&event);
return;
}
launch_next_query(query, ISC_TRUE);
isc_event_free(&event);
}
#if 0
static isc_boolean_t
msg_contains_soa(dns_message_t *msg, dig_query_t *query) {
isc_result_t result;
dns_name_t *name=NULL;
debug("msg_contains_soa()");
result = dns_message_findname(msg, DNS_SECTION_ANSWER,
query->lookup->name, dns_rdatatype_soa,
0, &name, NULL);
if (result == ISC_R_SUCCESS) {
debug("Found SOA", stderr);
return (ISC_TRUE);
} else {
debug("Didn't find SOA, result=%d:%s",
result, dns_result_totext(result));
return (ISC_FALSE);
}
}
#endif
static void
check_for_more_data(dig_query_t *query, dns_message_t *msg,
isc_socketevent_t *sevent)
{
dns_name_t *name = NULL;
dns_rdataset_t *rdataset = NULL;
dns_rdata_t rdata;
dns_rdata_soa_t soa;
isc_result_t result;
isc_buffer_t b;
isc_region_t r;
char *abspace[MXNAME];
debug ("check_for_more_data()");
/*
* By the time we're in this routine, we know we're doing
* either an AXFR or IXFR. If there's no second_rr_type,
* then we don't yet know which kind of answer we got back
* from the server. Here, we're going to walk through the
* rr's in the message, acting as necessary whenever we hit
* an SOA rr.
*/
result = dns_message_firstname(msg, DNS_SECTION_ANSWER);
if (result != ISC_R_SUCCESS) {
puts("; Transfer failed.");
query->working = ISC_FALSE;
cancel_lookup(query->lookup);
return;
}
#ifdef NEVER
check_result(result, "dns_message_firstname");
#endif
do {
dns_message_currentname(msg, DNS_SECTION_ANSWER,
&name);
for (rdataset = ISC_LIST_HEAD(name->list);
rdataset != NULL;
rdataset = ISC_LIST_NEXT(rdataset, link)) {
result = dns_rdataset_first(rdataset);
if (result != ISC_R_SUCCESS)
continue;
do {
dns_rdataset_current(rdataset, &rdata);
/*
* If this is the first rr, make sure
* it's an SOA
*/
if ((!query->first_soa_rcvd) &&
(rdata.type != dns_rdatatype_soa)) {
puts("; Transfer failed. "
"Didn't start with "
"SOA answer.");
query->working = ISC_FALSE;
cancel_lookup(query->lookup);
return;
}
if ((!query->second_rr_rcvd) &&
(rdata.type != dns_rdatatype_soa)) {
query->second_rr_rcvd = ISC_TRUE;
query->second_rr_serial = 0;
debug ("Got the second rr as nonsoa");
continue;
}
/*
* If the record is anything except an SOA
* now, just continue on...
*/
if (rdata.type != dns_rdatatype_soa)
goto NEXT_RDATA;
/* Now we have an SOA. Work with it. */
debug ("Got a SOA");
result = dns_rdata_tostruct(&rdata,
&soa,
mctx);
check_result(result,
"dns_rdata_tostruct");
if (!query->first_soa_rcvd) {
query->first_soa_rcvd =
ISC_TRUE;
query->first_rr_serial =
soa.serial;
debug ("This is the first. %d",
query->lookup->ixfr_serial);
if (query->lookup->ixfr_serial >=
soa.serial) {
dns_rdata_freestruct(&soa);
goto XFR_DONE;
}
dns_rdata_freestruct(&soa);
goto NEXT_RDATA;
}
if (!query->second_rr_rcvd) {
debug ("This is the second. %d",
query->lookup->ixfr_serial);
query->second_rr_rcvd = ISC_TRUE;
query->second_rr_serial =
soa.serial;
goto NEXT_RDATA;
}
if (query->second_rr_serial == 0) {
/*
* If the second RR was a non-SOA
* record, and we're getting any
* other SOA, then this is an
* AXFR, and we're done.
*/
debug ("Done, since axfr.");
XFR_DONE:
isc_buffer_init(&b, abspace, MXNAME);
result = isc_sockaddr_totext(&sevent->
address,
&b);
check_result(result,
"isc_sockaddr_totext");
isc_buffer_usedregion(&b, &r);
received(b.used, r.length,
(char *)r.base, query);
query->working = ISC_FALSE;
cancel_lookup(query->lookup);
dns_rdata_freestruct(&soa);
return;
}
/*
* If we get to this point, we're doing an
* IXFR and have to start really looking
* at serial numbers.
*/
if (query->first_rr_serial == soa.serial) {
debug ("Got a match for ixfr");
if (!query->first_repeat_rcvd) {
query->first_repeat_rcvd =
ISC_TRUE;
dns_rdata_freestruct(&soa);
goto NEXT_RDATA;
}
debug ("Done with ixfr");
dns_rdata_freestruct(&soa);
goto XFR_DONE;
}
debug ("Meaningless soa, %d",
soa.serial);
dns_rdata_freestruct(&soa);
NEXT_RDATA:
result = dns_rdataset_next(rdataset);
} while (result == ISC_R_SUCCESS);
}
result = dns_message_nextname(msg, DNS_SECTION_ANSWER);
} while (result == ISC_R_SUCCESS);
launch_next_query(query, ISC_FALSE);
return;
}
static void
recv_done(isc_task_t *task, isc_event_t *event) {
isc_socketevent_t *sevent = NULL;
dig_query_t *query = NULL;
isc_buffer_t *b = NULL;
dns_message_t *msg = NULL;
isc_result_t result;
isc_buffer_t ab;
char abspace[MXNAME];
isc_region_t r;
dig_lookup_t *n;
UNUSED (task);
debug ("recv_done()");
if (free_now) {
isc_event_free(&event);
return;
}
query = event->ev_arg;
debug("(lookup=%lx, query=%lx)",
(long int)query->lookup, (long int)query);
if (free_now) {
debug("Bailing out, since freeing now.");
isc_event_free(&event);
return;
}
sendcount--;
debug("In recv_done, counter down to %d", sendcount);
REQUIRE(event->ev_type == ISC_SOCKEVENT_RECVDONE);
sevent = (isc_socketevent_t *)event;
if (!query->lookup->pending && !query->lookup->ns_search_only) {
debug("No longer pending. Got %s",
isc_result_totext(sevent->result));
query->working = ISC_FALSE;
query->waiting_connect = ISC_FALSE;
cancel_lookup(query->lookup);
isc_event_free(&event);
return;
}
if (sevent->result == ISC_R_SUCCESS) {
b = ISC_LIST_HEAD(sevent->bufferlist);
ISC_LIST_DEQUEUE(sevent->bufferlist, &query->recvbuf, link);
result = dns_message_create(mctx, DNS_MESSAGE_INTENTPARSE,
&msg);
check_result(result, "dns_message_create");
if (key != NULL) {
debug ("querysig 1 is %lx", query->lookup->querysig);
if (query->lookup->querysig == NULL) {
debug ("Getting initial querysig");
result = dns_message_getquerytsig(
query->lookup->sendmsg,
mctx, &query->lookup->querysig);
check_result(result,
"dns_message_getquerytsig");
}
result = dns_message_setquerytsig(msg,
query->lookup->querysig);
check_result(result, "dns_message_setquerytsig");
result = dns_message_settsigkey(msg, key);
check_result(result, "dns_message_settsigkey");
msg->tsigctx = query->lookup->tsigctx;
if (query->lookup->msgcounter != 0)
msg->tcp_continuation = 1;
query->lookup->msgcounter++;
}
debug ("Before parse starts");
result = dns_message_parse(msg, b, ISC_TRUE);
if (result != ISC_R_SUCCESS) {
printf (";; Got bad UDP packet:\n");
hex_dump(b);
query->working = ISC_FALSE;
query->waiting_connect = ISC_FALSE;
if (!query->lookup->tcp_mode) {
printf (";; Retrying in TCP mode.\n");
n = requeue_lookup(query->lookup, ISC_TRUE);
n->tcp_mode = ISC_TRUE;
}
cancel_lookup(query->lookup);
dns_message_destroy(&msg);
isc_event_free(&event);
return;
}
if (key != NULL) {
debug ("querysig 2 is %lx", query->lookup->querysig);
debug ("Before verify");
result = dns_tsig_verify(&query->recvbuf, msg,
NULL, keyring);
debug ("After verify");
if (result != ISC_R_SUCCESS) {
printf (";; Couldn't verify signature: %s\n",
dns_result_totext(result));
validated = ISC_FALSE;
}
query->lookup->tsigctx = msg->tsigctx;
if (query->lookup->querysig != NULL) {
debug ("Freeing querysig buffer %lx",
query->lookup->querysig);
isc_buffer_free(&query->lookup->querysig);
}
result = dns_message_getquerytsig(msg, mctx,
&query->lookup->querysig);
check_result(result,"dns_message_getquerytsig");
debug ("querysig 3 is %lx", query->lookup->querysig);
}
debug ("After parse");
if (query->lookup->xfr_q == NULL)
query->lookup->xfr_q = query;
if (query->lookup->xfr_q == query) {
if ((query->lookup->trace)||
(query->lookup->ns_search_only)) {
debug ("In TRACE code");
if ((show_details ||
((dns_message_firstname(msg,
DNS_SECTION_ANSWER)
== ISC_R_SUCCESS))) &&
!query->lookup->trace_root ) {
printmessage(query, msg, ISC_TRUE);
}
if ((msg->rcode != 0) &&
(query->lookup->origin != NULL)) {
next_origin(msg, query);
} else {
result = dns_message_firstname
(msg,DNS_SECTION_ANSWER);
if ((result != ISC_R_SUCCESS) ||
query->lookup->trace_root)
followup_lookup(msg, query,
DNS_SECTION_AUTHORITY);
}
} else if ((msg->rcode != 0) &&
(query->lookup->origin != NULL)) {
next_origin(msg, query);
if (show_details) {
printmessage(query, msg, ISC_TRUE);
}
} else {
if (query->first_soa_rcvd &&
query->lookup->doing_xfr)
printmessage(query, msg, ISC_FALSE);
else
printmessage(query, msg, ISC_TRUE);
}
} else if (( dns_message_firstname(msg, DNS_SECTION_ANSWER)
== ISC_R_SUCCESS) &&
query->lookup->ns_search_only &&
!query->lookup->trace_root ) {
printmessage (query, msg, ISC_TRUE);
}
if (query->lookup->pending)
debug("Still pending.");
if (query->lookup->doing_xfr) {
if (query != query->lookup->xfr_q) {
dns_message_destroy (&msg);
isc_event_free (&event);
query->working = ISC_FALSE;
query->waiting_connect = ISC_FALSE;
return;
}
check_for_more_data(query, msg, sevent);
}
else {
if ((msg->rcode == 0) ||
(query->lookup->origin == NULL)) {
isc_buffer_init(&ab, abspace, MXNAME);
result = isc_sockaddr_totext(&sevent->address,
&ab);
check_result(result, "isc_sockaddr_totext");
isc_buffer_usedregion(&ab, &r);
if ((( dns_message_firstname(msg,
DNS_SECTION_ANSWER)
== ISC_R_SUCCESS) &&
query->lookup->ns_search_only &&
!query->lookup->trace_root) ||
query->lookup->trace) {
received(b->used, r.length,
(char *)r.base,
query);
}
}
query->working = ISC_FALSE;
query->lookup->pending = ISC_FALSE;
if (!query->lookup->ns_search_only ||
query->lookup->trace_root ) {
cancel_lookup(query->lookup);
}
check_next_lookup(query->lookup);
}
dns_message_destroy(&msg);
isc_event_free(&event);
return;
}
/* In truth, we should never get into the CANCELED routine, since
the cancel_lookup() routine clears the pending flag. */
if (sevent->result == ISC_R_CANCELED) {
debug ("In cancel handler");
query->working = ISC_FALSE;
query->waiting_connect = ISC_FALSE;
check_next_lookup(query->lookup);
isc_event_free(&event);
return;
}
fatal("recv_done got result %s",
isc_result_totext(sevent->result));
}
void
get_address(char *host, in_port_t port, isc_sockaddr_t *sockaddr) {
struct in_addr in4;
struct in6_addr in6;
#if defined(HAVE_ADDRINFO) && defined(HAVE_GETADDRINFO)
struct addrinfo *res = NULL;
int result;
#else
struct hostent *he;
#endif
debug("get_address()");
if (have_ipv6 && inet_pton(AF_INET6, host, &in6) == 1)
isc_sockaddr_fromin6(sockaddr, &in6, port);
else if (inet_pton(AF_INET, host, &in4) == 1)
isc_sockaddr_fromin(sockaddr, &in4, port);
else {
#if defined(HAVE_ADDRINFO) && defined(HAVE_GETADDRINFO)
result = getaddrinfo(host, NULL, NULL, &res);
if (result != 0) {
fatal ("Couldn't find server %s. %s",
host, gai_strerror(result));
}
memcpy(&sockaddr->type.sa,res->ai_addr, res->ai_addrlen);
sockaddr->length = res->ai_addrlen;
isc_sockaddr_setport(sockaddr, port);
freeaddrinfo(res);
#else
he = gethostbyname(host);
if (he == NULL)
fatal("Couldn't find server %s. errno=%d",
host, h_errno);
INSIST(he->h_addrtype == AF_INET);
isc_sockaddr_fromin(sockaddr,
(struct in_addr *)(he->h_addr_list[0]),
port);
#endif
}
}
static void
do_lookup_tcp(dig_lookup_t *lookup) {
dig_query_t *query;
isc_result_t result;
debug("do_lookup_tcp()");
lookup->pending = ISC_TRUE;
isc_interval_set(&lookup->interval, timeout, 0);
result = isc_timer_create(timermgr, isc_timertype_once, NULL,
&lookup->interval, global_task,
connect_timeout, lookup, &lookup->timer);
check_result(result, "isc_timer_create");
for (query = ISC_LIST_HEAD(lookup->q);
query != NULL;
query = ISC_LIST_NEXT(query, link)) {
query->working = ISC_TRUE;
query->waiting_connect = ISC_TRUE;
get_address(query->servname, port, &query->sockaddr);
sockcount++;
debug ("Socket = %d",sockcount);
ENSURE (query->sock == NULL);
result = isc_socket_create(socketmgr,
isc_sockaddr_pf(&query->sockaddr),
isc_sockettype_tcp, &query->sock) ;
check_result(result, "isc_socket_create");
if (specified_source)
result = isc_socket_bind(query->sock, &bind_address);
else {
if (isc_sockaddr_pf(&query->sockaddr) == AF_INET)
isc_sockaddr_any(&bind_any);
else
isc_sockaddr_any6(&bind_any);
result = isc_socket_bind(query->sock, &bind_any);
}
check_result(result, "isc_socket_bind");
result = isc_socket_connect(query->sock, &query->sockaddr,
global_task, connect_done, query);
check_result (result, "isc_socket_connect");
}
}
static void
do_lookup_udp(dig_lookup_t *lookup) {
dig_query_t *query;
isc_result_t result;
debug("do_lookup_udp()");
if (lookup->tcp_mode)
debug("I'm starting UDP with tcp_mode set!!!");
lookup->pending = ISC_TRUE;
for (query = ISC_LIST_HEAD(lookup->q);
query != NULL;
query = ISC_LIST_NEXT(query, link)) {
query->working = ISC_TRUE;
query->waiting_connect = ISC_FALSE;
get_address(query->servname, port, &query->sockaddr);
sockcount++;
debug ("Socket = %d",sockcount);
result = isc_socket_create(socketmgr,
isc_sockaddr_pf(&query->sockaddr),
isc_sockettype_udp, &query->sock) ;
check_result(result, "isc_socket_create");
if (specified_source)
result = isc_socket_bind(query->sock, &bind_address);
else {
if (isc_sockaddr_pf(&query->sockaddr) == AF_INET)
isc_sockaddr_any(&bind_any);
else
isc_sockaddr_any6(&bind_any);
result = isc_socket_bind(query->sock, &bind_any);
}
check_result(result, "isc_socket_bind");
}
send_udp(lookup);
}
void
do_lookup(dig_lookup_t *lookup) {
REQUIRE (lookup != NULL);
debug ("do_lookup()");
if (lookup->tcp_mode)
do_lookup_tcp(lookup);
else
do_lookup_udp(lookup);
}
void
start_lookup(void) {
dig_lookup_t *lookup;
debug ("start_lookup()");
if (free_now)
return;
lookup = ISC_LIST_HEAD(lookup_list);
if (lookup != NULL) {
setup_lookup(lookup);
do_lookup(lookup);
}
}
void
free_lists(int _exitcode) {
void *ptr;
dig_lookup_t *l;
dig_query_t *q;
dig_server_t *s;
dig_searchlist_t *o;
debug("free_lists()");
if (free_now)
return;
free_now = ISC_TRUE;
l = ISC_LIST_HEAD(lookup_list);
while (l != NULL) {
q = ISC_LIST_HEAD(l->q);
while (q != NULL) {
debug ("Freeing query %lx, belonging to %lx",
q, l);
if (q->sock != NULL) {
isc_socket_cancel(q->sock, NULL,
ISC_SOCKCANCEL_ALL);
isc_socket_detach(&q->sock);
sockcount--;
debug ("Socket = %d",sockcount);
}
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);
ptr = q;
q = ISC_LIST_NEXT(q, link);
isc_mem_free(mctx, ptr);
}
if (l->use_my_server_list) {
s = ISC_LIST_HEAD(l->my_server_list);
while (s != NULL) {
debug ("Freeing server %lx belonging to %lx",
s, l);
ptr = s;
s = ISC_LIST_NEXT(s, link);
isc_mem_free(mctx, ptr);
}
}
if (l->sendmsg != NULL)
dns_message_destroy (&l->sendmsg);
if (l->timer != NULL)
isc_timer_detach (&l->timer);
if (l->querysig != NULL) {
debug ("Freeing buffer %lx", l->querysig);
isc_buffer_free(&l->querysig);
}
ptr = l;
l = ISC_LIST_NEXT(l, link);
isc_mem_free(mctx, ptr);
}
s = ISC_LIST_HEAD(server_list);
while (s != NULL) {
debug ("Freeing global server %lx", s);
ptr = s;
s = ISC_LIST_NEXT(s, link);
isc_mem_free(mctx, ptr);
}
o = ISC_LIST_HEAD(search_list);
while (o != NULL) {
debug ("Freeing search %lx", o);
ptr = o;
o = ISC_LIST_NEXT(o, link);
isc_mem_free(mctx, ptr);
}
if (socketmgr != NULL) {
debug ("Freeing socketmgr");
isc_socketmgr_destroy(&socketmgr);
}
if (timermgr != NULL) {
debug ("Freeing timermgr");
isc_timermgr_destroy(&timermgr);
}
if (global_task != NULL) {
debug ("Freeing task");
isc_task_detach(&global_task);
}
if (taskmgr != NULL) {
debug ("Freeing taskmgr");
isc_taskmgr_destroy(&taskmgr);
}
if (key != NULL) {
debug ("Freeing key %lx", key);
dns_tsigkey_setdeleted(key);
dns_tsigkey_detach(&key);
}
if (namebuf != NULL)
isc_buffer_free(&namebuf);
if (keyring != NULL) {
debug ("Freeing keyring %lx", keyring);
dns_tsigkeyring_destroy(&keyring);
}
if (is_dst_up) {
debug ("Destroy DST lib");
dst_lib_destroy();
is_dst_up = ISC_FALSE;
}
if (entp != NULL) {
debug ("Detach from entropy");
isc_entropy_detach(&entp);
}
if (isc_mem_debugging)
isc_mem_stats(mctx, stderr);
isc_app_finish();
if (mctx != NULL)
isc_mem_destroy(&mctx);
debug("Getting ready to exit, code=%d",_exitcode);
if (_exitcode != 0)
exit(_exitcode);
}