dighost.c revision b8dbd3c5dce84fd6e4d1d4be23b087d46f183928
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/*
499b34cea04a46823d003d4c0520c8b03e8513cbBrian Wellington * Copyright (C) 2000 Internet Software Consortium.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * 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.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
15a44745412679c30a6d022733925af70a38b715David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
15a44745412679c30a6d022733925af70a38b715David Lawrence * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
15a44745412679c30a6d022733925af70a38b715David Lawrence * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
15a44745412679c30a6d022733925af70a38b715David Lawrence * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
15a44745412679c30a6d022733925af70a38b715David Lawrence * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15a44745412679c30a6d022733925af70a38b715David Lawrence * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
15a44745412679c30a6d022733925af70a38b715David Lawrence * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15a44745412679c30a6d022733925af70a38b715David Lawrence * SOFTWARE.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence/* $Id: dighost.c,v 1.66 2000/06/30 22:57:47 bwelling Exp $ */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/*
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley * Notice to programmers: Do not use this code as an example of how to
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * use the ISC library to perform DNS lookups. Dig and Host both operate
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * on the request level, since they allow fine-tuning of output and are
fc8a6561cbb0a1fea849950a3217aacd3b8f1eacDavid 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
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson * functions in most applications.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas 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)
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington#include <netdb.h>
09f22ac5b09e70bc526015f37168ba33e21ea91fDavid Lawrence#include <string.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#else
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrenceextern int h_errno;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#endif
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/message.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/name.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/rdata.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/rdataclass.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/rdatalist.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/rdataset.h>
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence#include <dns/rdatastruct.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/rdatatype.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/result.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/tsig.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dst/dst.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <isc/app.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <isc/base64.h>
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence#include <isc/entropy.h>
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence#include <isc/lang.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <isc/lex.h>
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson#include <isc/netdb.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/result.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/string.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/task.h>
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson#include <isc/timer.h>
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington#include <isc/types.h>
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson#include <isc/util.h>
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington#include <dig/dig.h>
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas 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
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafssonisc_boolean_t have_ipv6 = ISC_FALSE, specified_source = ISC_FALSE,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson free_now = ISC_FALSE, show_details = ISC_FALSE, usesearch=ISC_TRUE,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson qr = ISC_FALSE, is_dst_up = ISC_FALSE;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrencein_port_t port = 53;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonunsigned int timeout = 5;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrewsisc_mem_t *mctx = NULL;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc_taskmgr_t *taskmgr = NULL;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc_task_t *global_task = NULL;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrenceisc_timermgr_t *timermgr = NULL;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc_socketmgr_t *socketmgr = NULL;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc_sockaddr_t bind_address;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrenceisc_sockaddr_t bind_any;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonchar *rootspace[BUFSIZE];
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc_buffer_t rootbuf;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonint sendcount = 0;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrenceint sockcount = 0;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonint ndots = -1;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonint tries = 3;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonint lookup_counter = 0;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonchar fixeddomain[MXNAME]="";
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonint exitcode = 9;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonchar keynametext[MXNAME];
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonchar keysecret[MXNAME]="";
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssondns_name_t keyname;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssondns_tsig_keyring_t *keyring = NULL;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc_buffer_t *namebuf = NULL;
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafssondns_tsigkey_t *key = NULL;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrenceisc_boolean_t validated = ISC_TRUE;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc_entropy_t *entp = NULL;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonextern isc_boolean_t isc_mem_debugging;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc_boolean_t debugging = ISC_FALSE;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic void
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssoncancel_lookup(dig_lookup_t *lookup);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic int
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssoncount_dots(char *string) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson char *s;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson int i=0;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson s = string;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson while (*s != 0) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (*s == '.')
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson i++;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson s++;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson 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;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews isc_buffer_remainingregion(b, &r);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews printf("Printing a buffer with length %d\n", r.length);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence for (len = 0 ; len < r.length ; len++) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson printf("%02x ", r.base[len]);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (len != 0 && len % 16 == 0)
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence printf("\n");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (len % 16 != 0)
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson printf("\n");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonvoid
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonfatal(const char *format, ...) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson va_list args;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson va_start(args, format);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson vfprintf(stderr, format, args);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson va_end(args);
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley fprintf(stderr, "\n");
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley if (exitcode == 0)
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley exitcode = 8;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson#ifdef NEVER
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson dighost_shutdown();
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson free_lists(exitcode);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (mctx != NULL) {
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson if (isc_mem_debugging)
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson isc_mem_stats(mctx, stderr);
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson isc_mem_destroy(&mctx);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#endif
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington exit(exitcode);
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington}
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellingtonvoid
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellingtondebug(const char *format, ...) {
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington va_list args;
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington if (debugging) {
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington va_start(args, format);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson vfprintf(stderr, format, args);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson va_end(args);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson fprintf(stderr, "\n");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
a6733246eafeb43755ce6d7ec3627ac4209cbccbMark Andrews
a6733246eafeb43755ce6d7ec3627ac4209cbccbMark Andrewsvoid
a6733246eafeb43755ce6d7ec3627ac4209cbccbMark Andrewscheck_result(isc_result_t result, const char *msg) {
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson if (result != ISC_R_SUCCESS) {
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington exitcode = 1;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson fatal("%s: %s", msg, isc_result_totext(result));
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence }
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc_boolean_t
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafssonisclass(char *text) {
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson /* Tests if a field is a class, without needing isc libs
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff * initialized. This list will have to be manually kept in
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson * sync with what the libs support.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson const char *classlist[] = {"in", "hs", "chaos"};
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson const int numclasses = 3;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson int i;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson for (i = 0; i < numclasses; i++)
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington if (strcasecmp(text, classlist[i]) == 0)
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington return ISC_TRUE;
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington
5e387b9ce6bafdfadedb5b34e4c33a4404e5d589Brian Wellington return ISC_FALSE;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson}
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halleyisc_boolean_t
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafssonistype(char *text) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson /* Tests if a field is a type, without needing isc libs
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * initialized. This list will have to be manually kept in
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * sync with what the libs support.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley const char *typelist[] = {"a", "ns", "md", "mf", "cname",
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley "soa", "mb", "mg", "mr", "null",
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson "wks", "ptr", "hinfo", "minfo",
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson "mx", "txt", "rp", "afsdb",
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson "x25", "isdn", "rt", "nsap",
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews "nsap_ptr", "sig", "key", "px",
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews "gpos", "aaaa", "loc", "nxt",
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson "srv", "naptr", "kx", "cert",
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson "a6", "dname", "opt", "unspec",
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson "tkey", "tsig", "axfr", "any"};
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews const int numtypes = 42;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews int i;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson for (i = 0; i < numtypes; i++) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (strcasecmp(text, typelist[i]) == 0)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson return ISC_TRUE;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson }
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson return ISC_FALSE;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssondig_lookup_t
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson*requeue_lookup(dig_lookup_t *lookold, isc_boolean_t servers) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dig_lookup_t *looknew;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dig_server_t *s, *srv;
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson debug("requeue_lookup()");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
5e387b9ce6bafdfadedb5b34e4c33a4404e5d589Brian Wellington if (free_now)
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington return(ISC_R_SUCCESS);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley lookup_counter++;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (lookup_counter > LOOKUP_LIMIT)
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley fatal ("Too many lookups.");
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence looknew = isc_mem_allocate
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson (mctx, sizeof(struct dig_lookup));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (looknew == NULL)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson fatal ("Memory allocation failure in %s:%d",
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson __FILE__, __LINE__);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew->pending = ISC_FALSE;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson strncpy (looknew->textname, lookold-> textname, MXNAME);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson strncpy (looknew->rttext, lookold-> rttext, 32);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson strncpy (looknew->rctext, lookold-> rctext, 32);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew->namespace[0]=0;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew->sendspace[0]=0;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew->sendmsg=NULL;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew->name=NULL;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew->oname=NULL;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew->timer = NULL;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew->xfr_q = NULL;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew->doing_xfr = lookold->doing_xfr;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew->ixfr_serial = lookold->ixfr_serial;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew->defname = lookold->defname;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew->trace = lookold->trace;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew->trace_root = lookold->trace_root;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew->identify = lookold->identify;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew->udpsize = lookold->udpsize;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew->recurse = lookold->recurse;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew->aaonly = lookold->aaonly;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew->adflag = lookold->adflag;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew->cdflag = lookold->cdflag;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew->ns_search_only = lookold->ns_search_only;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews looknew->origin = NULL;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews looknew->querysig = NULL;
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson looknew->retries = tries;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews looknew->nsfound = 0;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew->tcp_mode = lookold->tcp_mode;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew->comments = lookold->comments;
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson looknew->stats = lookold->stats;
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington looknew->section_question = lookold->section_question;
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington looknew->section_answer = lookold->section_answer;
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington looknew->section_authority = lookold->section_authority;
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington looknew->section_additional = lookold->section_additional;
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington looknew->new_search = ISC_FALSE;
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington ISC_LIST_INIT(looknew->my_server_list);
5dbf258e2c780189fa30d51a872d8edb372e5589Bob Halley ISC_LIST_INIT(looknew->q);
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson looknew->use_my_server_list = ISC_FALSE;
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson if (servers) {
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson looknew->use_my_server_list = lookold->use_my_server_list;
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson if (looknew->use_my_server_list) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson s = ISC_LIST_HEAD(lookold->my_server_list);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson while (s != NULL) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson srv = isc_mem_allocate (mctx, sizeof(struct
b120e39deac3e9c323cdc7801ed2313f8ebbb744Andreas Gustafsson dig_server));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (srv == NULL)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson fatal("Memory allocation failure "
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson "in %s:%d", __FILE__, __LINE__);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson strncpy(srv->servername, s->servername,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson MXNAME);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ISC_LIST_ENQUEUE(looknew->my_server_list, srv,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson link);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson s = ISC_LIST_NEXT(s, link);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson }
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington }
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington }
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington debug ("Before insertion, init@%p "
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington "-> %p, new@%p "
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington "-> %p", lookold,
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington lookold->link.next,
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington looknew, looknew->
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson link.next);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ISC_LIST_INSERTAFTER(lookup_list, lookold, looknew, link);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence debug ("After insertion, init -> "
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson "%p, new = %p, "
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson "new -> %p", lookold,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson looknew, looknew->
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence link.next);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson return (looknew);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafssonvoid
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonsetup_system(void) {
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson char rcinput[MXNAME];
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington FILE *fp;
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington char *ptr;
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington dig_server_t *srv;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dig_searchlist_t *search;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dig_lookup_t *l;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_boolean_t get_servers;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson isc_result_t result;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence isc_buffer_t secretsrc;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson isc_buffer_t secretbuf;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson int secretsize;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson unsigned char *secretstore;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson isc_lex_t *lex = NULL;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_stdtime_t now;
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington debug ("setup_system()");
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (fixeddomain[0]!=0) {
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington debug("Using fixed domain %s", fixeddomain);
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington search = isc_mem_allocate( mctx, sizeof(struct dig_server));
bd703253b745aaec6e45a60108bec513bf79bd2cBrian Wellington if (search == NULL)
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington fatal("Memory allocation failure in %s:%d",
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington __FILE__, __LINE__);
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington strncpy(search->origin, fixeddomain, MXNAME - 1);
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington ISC_LIST_PREPEND(search_list, search, link);
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson free_now = ISC_FALSE;
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson get_servers = ISC_TF(server_list.head == NULL);
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson fp = fopen (RESOLVCONF, "r");
618a5e4923c3d064c1cc0f1866e27e8ddca90700Andreas Gustafsson if (fp != NULL) {
618a5e4923c3d064c1cc0f1866e27e8ddca90700Andreas Gustafsson while (fgets(rcinput, MXNAME, fp) != 0) {
618a5e4923c3d064c1cc0f1866e27e8ddca90700Andreas Gustafsson ptr = strtok (rcinput, " \t\r\n");
618a5e4923c3d064c1cc0f1866e27e8ddca90700Andreas Gustafsson if (ptr != NULL) {
618a5e4923c3d064c1cc0f1866e27e8ddca90700Andreas Gustafsson if (get_servers &&
7acfb3bcd29db4f19aa42690826e57dafdd28cdaAndreas Gustafsson strcasecmp(ptr, "nameserver") == 0) {
618a5e4923c3d064c1cc0f1866e27e8ddca90700Andreas Gustafsson debug ("Got a nameserver line");
618a5e4923c3d064c1cc0f1866e27e8ddca90700Andreas Gustafsson ptr = strtok (NULL, " \t\r\n");
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson if (ptr != NULL) {
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson srv = isc_mem_allocate(mctx,
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence sizeof(struct dig_server));
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence if (srv == NULL)
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson fatal("Memory "
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson "allocation "
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence "failure in "
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence "%s:%d",
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence __FILE__,
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence __LINE__);
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson strncpy((char *)srv->
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson servername,
618a5e4923c3d064c1cc0f1866e27e8ddca90700Andreas Gustafsson ptr,
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence MXNAME - 1);
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson ISC_LIST_APPEND
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson (server_list,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson srv, link);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson } else if (strcasecmp(ptr, "options") == 0) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ptr = strtok(NULL, " \t\r\n");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (ptr != NULL) {
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson if ((strncasecmp(ptr, "ndots:",
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence 6) == 0) &&
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson (ndots == -1)) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ndots = atoi(
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington &ptr[6]);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson debug ("ndots is "
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson "%d.",
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ndots);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews } else if ((strcasecmp(ptr, "search") == 0)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence && usesearch){
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson while ((ptr = strtok(NULL, " \t\r\n"))
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson != NULL) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson search = isc_mem_allocate(
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews mctx, sizeof(struct
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence dig_server));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (search == NULL)
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley fatal("Memory "
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson "allocation "
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson "failure in %s:"
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson "%d", __FILE__,
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson __LINE__);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson strncpy(search->
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson origin,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ptr,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson MXNAME - 1);
e485d16462b0a66b835107fae04fe9d562e77af0Bob Halley ISC_LIST_APPEND
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson (search_list,
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson search,
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson link);
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson }
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson } else if ((strcasecmp(ptr, "domain") == 0) &&
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson (fixeddomain[0] == 0 )){
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson while ((ptr = strtok(NULL, " \t\r\n"))
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson != NULL) {
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson search = isc_mem_allocate(
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson mctx, sizeof(struct
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence dig_server));
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson if (search == NULL)
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson fatal("Memory "
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson "allocation "
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson "failure in %s:"
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson "%d", __FILE__,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson __LINE__);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson strncpy(search->
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson origin,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ptr,
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson MXNAME - 1);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson ISC_LIST_PREPEND
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson (search_list,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson search,
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence link);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson fclose (fp);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson }
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (ndots == -1)
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews ndots = 1;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews if (server_list.head == NULL) {
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews srv = isc_mem_allocate(mctx, sizeof(dig_server_t));
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews if (srv == NULL)
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews fatal("Memory allocation failure");
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews strcpy(srv->servername, "127.0.0.1");
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews ISC_LIST_APPEND(server_list, srv, link);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews }
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson for (l = ISC_LIST_HEAD(lookup_list) ;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence l != NULL;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson l = ISC_LIST_NEXT(l, link) ) {
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence l -> origin = ISC_LIST_HEAD(search_list);
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson }
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews if (keysecret[0] != 0) {
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson debug("keyring");
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson result = dns_tsigkeyring_create(mctx, &keyring);
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson check_result(result, "dns_tsigkeyring_create");
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson debug("buffer");
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson result = isc_buffer_allocate(mctx, &namebuf, MXNAME);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson check_result(result, "isc_buffer_allocate");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson debug("name");
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson dns_name_init(&keyname, NULL);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson check_result(result, "dns_name_init");
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson isc_buffer_putstr(namebuf, keynametext);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson secretsize = strlen(keysecret) * 3 / 4;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson debug("secretstore");
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence secretstore = isc_mem_get(mctx, secretsize);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ENSURE (secretstore != NULL);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson isc_buffer_init(&secretsrc, keysecret, strlen(keysecret));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_buffer_add(&secretsrc, strlen(keysecret));
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson isc_buffer_init(&secretbuf, secretstore, secretsize);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson debug("lex");
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence result = isc_lex_create(mctx, strlen(keysecret), &lex);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence check_result(result, "isc_lex_create");
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson result = isc_lex_openbuffer(lex, &secretsrc);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson check_result(result, "isc_lex_openbuffer");
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson result = isc_base64_tobuffer(lex, &secretbuf, -1);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence if (result != ISC_R_SUCCESS) {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence printf (";; Couldn't create key %s: %s\n",
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence keynametext, isc_result_totext(result));
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson isc_lex_close(lex);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence isc_lex_destroy(&lex);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence goto SYSSETUP_FAIL;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson }
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence secretsize = isc_buffer_usedlength(&secretbuf);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence debug("close");
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson isc_lex_close(lex);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence isc_lex_destroy(&lex);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence isc_stdtime_get(&now);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence debug("namefromtext");
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence result = dns_name_fromtext(&keyname, namebuf,
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson dns_rootname, ISC_FALSE,
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence namebuf);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence if (result != ISC_R_SUCCESS) {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence printf (";; Couldn't create key %s: %s\n",
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence keynametext, dns_result_totext(result));
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence goto SYSSETUP_FAIL;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence }
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence debug("tsigkey");
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence result = dns_tsigkey_create(&keyname, dns_tsig_hmacmd5_name,
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence secretstore, secretsize,
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence ISC_TRUE, NULL, now, now, mctx,
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence keyring, &key);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence if (result != ISC_R_SUCCESS) {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence printf (";; Couldn't create key %s: %s\n",
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence keynametext, dns_result_totext(result));
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence }
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence isc_mem_put(mctx, secretstore, secretsize);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson dns_name_invalidate(&keyname);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson isc_buffer_free(&namebuf);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson return;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson SYSSETUP_FAIL:
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson isc_mem_put(mctx, secretstore, secretsize);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson dns_name_invalidate(&keyname);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson isc_buffer_free(&namebuf);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence dns_tsigkeyring_destroy(&keyring);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence return;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson }
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence}
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrencevoid
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrencesetup_libs(void) {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence isc_result_t result;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson debug ("setup_libs()");
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson /*
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson * Warning: This is not particularly good randomness. We'll
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson * just use random() now for getting id values, but doing so
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson * does NOT insure that id's cann't be guessed.
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson */
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson srandom (getpid() + (int)&setup_libs);
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence result = isc_app_start();
82ca33427bdd4f3bc4ed3431e86bd810fe751674Andreas Gustafsson check_result(result, "isc_app_start");
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson result = isc_net_probeipv4();
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff check_result(result, "isc_net_probeipv4");
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence result = isc_net_probeipv6();
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson if (result == ISC_R_SUCCESS)
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence have_ipv6=ISC_TRUE;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson result = isc_mem_create(0, 0, &mctx);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson check_result(result, "isc_mem_create");
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson result = isc_taskmgr_create (mctx, 1, 0, &taskmgr);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson check_result(result, "isc_taskmgr_create");
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews result = isc_task_create (taskmgr, 0, &global_task);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews check_result(result, "isc_task_create");
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews result = isc_timermgr_create (mctx, &timermgr);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews check_result(result, "isc_timermgr_create");
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews result = isc_socketmgr_create (mctx, &socketmgr);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews check_result(result, "isc_socketmgr_create");
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews result = isc_entropy_create (mctx, &entp);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence check_result(result, "isc_entropy_create");
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence result = dst_lib_init (mctx, entp, 0);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence check_result(result, "dst_lib_init");
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence is_dst_up = ISC_TRUE;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence}
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrencestatic void
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrewsadd_opt (dns_message_t *msg, isc_uint16_t udpsize) {
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews dns_rdataset_t *rdataset = NULL;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews dns_rdatalist_t *rdatalist = NULL;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson dns_rdata_t *rdata = NULL;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson isc_result_t result;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson debug ("add_opt()");
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson result = dns_message_gettemprdataset(msg, &rdataset);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson check_result (result, "dns_message_gettemprdataset");
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff dns_rdataset_init (rdataset);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson result = dns_message_gettemprdatalist(msg, &rdatalist);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson check_result (result, "dns_message_gettemprdatalist");
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff result = dns_message_gettemprdata(msg, &rdata);
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff check_result (result, "dns_message_gettemprdata");
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence debug ("Setting udp size of %d", udpsize);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence rdatalist->type = dns_rdatatype_opt;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence rdatalist->covers = 0;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence rdatalist->rdclass = udpsize;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence rdatalist->ttl = 0;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson rdata->data = NULL;
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff rdata->length = 0;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson ISC_LIST_INIT(rdatalist->rdata);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson dns_rdatalist_tordataset(rdatalist, rdataset);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson result = dns_message_setopt(msg, rdataset);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson check_result (result, "dns_message_setopt");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrencestatic void
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafssonadd_type(dns_message_t *message, dns_name_t *name, dns_rdataclass_t rdclass,
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence dns_rdatatype_t rdtype)
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson{
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dns_rdataset_t *rdataset;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_result_t result;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson debug ("add_type()");
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence rdataset = NULL;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence result = dns_message_gettemprdataset(message, &rdataset);
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson check_result(result, "dns_message_gettemprdataset()");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dns_rdataset_init(rdataset);
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence dns_rdataset_makequestion(rdataset, rdclass, rdtype);
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence ISC_LIST_APPEND(name->list, rdataset, link);
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence}
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrencestatic void
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrencecheck_next_lookup(dig_lookup_t *lookup) {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence dig_lookup_t *next;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence dig_query_t *query;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence isc_boolean_t still_working=ISC_FALSE;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence if (free_now)
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence return;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence debug("check_next_lookup(%p)", lookup);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence for (query = ISC_LIST_HEAD(lookup->q);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence query != NULL;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence query = ISC_LIST_NEXT(query, link)) {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence if (query->working) {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence debug("Still have a worker.", stderr);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence still_working=ISC_TRUE;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence }
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence }
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence 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");
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence
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);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dighost_shutdown();
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson return;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence if (next->sendmsg == NULL) {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence debug ("Setting up for TCP");
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence setup_lookup(next);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence do_lookup(next);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence }
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence } else {
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson if (!lookup->pending) {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence if (next == NULL) {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence debug("Shutting Down.", stderr);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson dighost_shutdown();
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson return;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson if (next->sendmsg == NULL) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson debug ("Setting up for UDP");
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson setup_lookup(next);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson do_lookup(next);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson } else {
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson if (lookup->retries > 1) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson debug ("Retrying");
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson lookup->retries --;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (lookup->timer != NULL)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_timer_detach(&lookup->timer);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews send_udp(lookup);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews } else {
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson debug ("Cancelling");
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson cancel_lookup(lookup);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson }
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence }
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafssonstatic void
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrencefollowup_lookup(dns_message_t *msg, dig_query_t *query,
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson dns_section_t section) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dig_lookup_t *lookup = NULL;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dig_server_t *srv = NULL;
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence dns_rdataset_t *rdataset = NULL;
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence dns_rdata_t rdata;
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence dns_name_t *name = NULL;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson isc_result_t result, loopresult;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson isc_buffer_t *b = NULL;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson isc_region_t r;
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence isc_boolean_t success = ISC_FALSE;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson int len;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence debug ("followup_lookup()");
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence if (free_now)
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence return;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson result = dns_message_firstname (msg,section);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (result != ISC_R_SUCCESS) {
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence debug ("Firstname returned %s",
951c247f2923f667c5c97355b31dc564bd540bfeDavid Lawrence isc_result_totext(result));
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if ((section == DNS_SECTION_ANSWER) &&
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson (query->lookup->trace || query->lookup->ns_search_only))
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson followup_lookup (msg, query, DNS_SECTION_AUTHORITY);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson return;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson debug ("Following up %s", query->lookup->textname);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson for (;;) {
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson name = NULL;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence dns_message_currentname(msg, section, &name);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson for (rdataset = ISC_LIST_HEAD(name->list);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson rdataset != NULL;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson rdataset = ISC_LIST_NEXT(rdataset, link)) {
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews loopresult = dns_rdataset_first(rdataset);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews while (loopresult == ISC_R_SUCCESS) {
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews dns_rdataset_current(rdataset, &rdata);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews debug ("Got rdata with type %d",
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews rdata.type);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews if ((rdata.type == dns_rdatatype_ns) &&
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews (!query->lookup->trace_root ||
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews (query->lookup->nsfound < ROOTNS)))
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews {
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews query->lookup->nsfound++;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews result = isc_buffer_allocate(mctx, &b,
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews BUFSIZE);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews check_result (result,
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews "isc_buffer_allocate");
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews result = dns_rdata_totext (&rdata,
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews NULL,
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews b);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews check_result (result,
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews "dns_rdata_totext");
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews isc_buffer_usedregion(b, &r);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews len = r.length-1;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews if (len >= MXNAME)
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence len = MXNAME-1;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence /* Initialize lookup if we've not yet */
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence debug ("Found NS %d %.*s",
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence (int)r.length, (int)r.length,
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence (char *)r.base);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence if (!success) {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence success = ISC_TRUE;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence lookup_counter++;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews lookup = requeue_lookup
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews (query->lookup,
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews ISC_FALSE);
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence lookup->doing_xfr = ISC_FALSE;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence lookup->defname = ISC_FALSE;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence lookup->use_my_server_list =
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence ISC_TRUE;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence if (section ==
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence DNS_SECTION_ANSWER) {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence lookup->trace =
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews ISC_FALSE;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence lookup->ns_search_only =
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence ISC_FALSE;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence }
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence else {
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence lookup->trace =
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence query->
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence lookup->trace;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence lookup->ns_search_only =
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence query->
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence lookup->ns_search_only;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence }
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence lookup->trace_root = ISC_FALSE;
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews ISC_LIST_INIT(lookup->
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews my_server_list);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson srv = isc_mem_allocate (mctx,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson sizeof(
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson struct
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dig_server));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (srv == NULL)
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff fatal("Memory allocation "
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence "failure in %s:%d",
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff __FILE__, __LINE__);
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson strncpy(srv->servername,
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff (char *)r.base, len);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson srv->servername[len]=0;
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson debug ("Adding server %s",
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff srv->servername);
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson ISC_LIST_APPEND
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence (lookup->my_server_list,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson srv, link);
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson isc_buffer_free (&b);
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson }
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson loopresult = dns_rdataset_next(rdataset);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence }
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson }
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson result = dns_message_nextname (msg, section);
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson if (result != ISC_R_SUCCESS)
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson break;
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson }
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson if ((lookup == NULL) && (section == DNS_SECTION_ANSWER) &&
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson (query->lookup->trace || query->lookup->ns_search_only))
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson followup_lookup(msg, query, DNS_SECTION_AUTHORITY);
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson}
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafssonstatic void
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrencenext_origin(dns_message_t *msg, dig_query_t *query) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dig_lookup_t *lookup;
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson UNUSED (msg);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson debug ("next_origin()");
if (free_now)
return;
debug ("Following up %s", query->lookup->textname);
if (query->lookup->origin == NULL) { /*Then we just did rootorg;
there's nothing left. */
debug ("Made it to the root whith nowhere to go.");
return;
}
lookup = requeue_lookup(query->lookup, ISC_TRUE);
lookup->defname = ISC_FALSE;
lookup->origin = ISC_LIST_NEXT(query->lookup->origin, link);
}
static void
insert_soa(dig_lookup_t *lookup) {
isc_result_t result;
dns_rdata_soa_t soa;
dns_rdata_t *rdata = NULL;
dns_rdatalist_t *rdatalist = NULL;
dns_rdataset_t *rdataset = NULL;
dns_name_t *soaname = NULL;
debug ("insert_soa()");
soa.mctx = mctx;
soa.serial = lookup->ixfr_serial;
soa.refresh = 1;
soa.retry = 1;
soa.expire = 1;
soa.minimum = 1;
soa.common.rdclass = dns_rdataclass_in;
soa.common.rdtype = dns_rdatatype_soa;
dns_name_init(&soa.origin, NULL);
dns_name_init(&soa.mname, NULL);
dns_name_clone(lookup->name, &soa.origin);
dns_name_clone(lookup->name, &soa.mname);
isc_buffer_init(&lookup->rdatabuf, lookup->rdatastore,
MXNAME);
result = dns_message_gettemprdata(lookup->sendmsg, &rdata);
check_result(result, "dns_message_gettemprdata");
result = dns_rdata_fromstruct(rdata, dns_rdataclass_in,
dns_rdatatype_soa, &soa,
&lookup->rdatabuf);
check_result(result, "isc_rdata_fromstruct");
result = dns_message_gettemprdatalist(lookup->sendmsg, &rdatalist);
check_result(result, "dns_message_gettemprdatalist");
result = dns_message_gettemprdataset(lookup->sendmsg, &rdataset);
check_result(result, "dns_message_gettemprdataset");
dns_rdatalist_init(rdatalist);
rdatalist->type = dns_rdatatype_soa;
rdatalist->rdclass = dns_rdataclass_in;
rdatalist->covers = dns_rdatatype_soa;
rdatalist->ttl = 1;
ISC_LIST_INIT(rdatalist->rdata);
ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
dns_rdataset_init(rdataset);
dns_rdatalist_tordataset(rdatalist, rdataset);
result = dns_message_gettempname(lookup->sendmsg, &soaname);
check_result(result, "dns_message_gettempname");
dns_name_init(soaname, NULL);
dns_name_clone(lookup->name, soaname);
ISC_LIST_INIT(soaname->list);
ISC_LIST_APPEND(soaname->list, rdataset, link);
dns_message_addname(lookup->sendmsg, soaname, DNS_SECTION_AUTHORITY);
}
void
setup_lookup(dig_lookup_t *lookup) {
isc_result_t result, res2;
int len;
dns_rdatatype_t rdtype;
dns_rdataclass_t rdclass;
dig_server_t *serv;
dig_query_t *query;
isc_region_t r;
isc_constregion_t tr;
isc_buffer_t b;
char store[MXNAME];
REQUIRE (lookup != NULL);
debug("setup_lookup(%p)",lookup);
if (free_now)
return;
debug("Setting up for looking up %s @%p->%p",
lookup->textname, lookup,
lookup->link.next);
result = dns_message_create(mctx, DNS_MESSAGE_INTENTRENDER,
&lookup->sendmsg);
check_result(result, "dns_message_create");
if (lookup->new_search) {
debug ("Resetting lookup counter.");
lookup_counter = 0;
}
result = dns_message_gettempname(lookup->sendmsg, &lookup->name);
check_result(result, "dns_message_gettempname");
dns_name_init(lookup->name, NULL);
isc_buffer_init(&lookup->namebuf, lookup->namespace, BUFSIZE);
isc_buffer_init(&lookup->onamebuf, lookup->onamespace, BUFSIZE);
if ((count_dots(lookup->textname) >= ndots) || lookup->defname)
lookup->origin = NULL; /* Force root lookup */
debug ("lookup->origin = %p", lookup->origin);
if (lookup->origin != NULL) {
debug ("Trying origin %s", lookup->origin->origin);
result = dns_message_gettempname(lookup->sendmsg,
&lookup->oname);
check_result(result, "dns_message_gettempname");
dns_name_init(lookup->oname, NULL);
len=strlen(lookup->origin->origin);
isc_buffer_init(&b, lookup->origin->origin, len);
isc_buffer_add(&b, len);
result = dns_name_fromtext(lookup->oname, &b, dns_rootname,
ISC_FALSE, &lookup->onamebuf);
if (result != ISC_R_SUCCESS) {
dns_message_puttempname(lookup->sendmsg,
&lookup->name);
dns_message_puttempname(lookup->sendmsg,
&lookup->oname);
fatal("Aborting: %s is not a legal name syntax. (%s)",
lookup->origin->origin,
dns_result_totext(result));
}
if (!lookup->trace_root) {
len=strlen(lookup->textname);
isc_buffer_init(&b, lookup->textname, len);
isc_buffer_add(&b, len);
result = dns_name_fromtext(lookup->name, &b,
lookup->oname, ISC_FALSE,
&lookup->namebuf);
} else {
isc_buffer_init(&b, ". ", 1);
isc_buffer_add(&b, 1);
result = dns_name_fromtext(lookup->name, &b,
lookup->oname, ISC_FALSE,
&lookup->namebuf);
}
if (result != ISC_R_SUCCESS) {
dns_message_puttempname(lookup->sendmsg,
&lookup->name);
dns_message_puttempname(lookup->sendmsg,
&lookup->oname);
fatal("Aborting: %s is not a legal name syntax. (%s)",
lookup->textname, dns_result_totext(result));
}
dns_message_puttempname(lookup->sendmsg, &lookup->oname);
} else {
debug ("Using root origin.");
if (!lookup->trace_root) {
len = strlen (lookup->textname);
isc_buffer_init(&b, lookup->textname, len);
isc_buffer_add(&b, len);
result = dns_name_fromtext(lookup->name, &b,
dns_rootname,
ISC_FALSE,
&lookup->namebuf);
} else {
isc_buffer_init(&b, ". ", 1);
isc_buffer_add(&b, 1);
result = dns_name_fromtext(lookup->name, &b,
dns_rootname,
ISC_FALSE,
&lookup->namebuf);
}
if (result != ISC_R_SUCCESS) {
dns_message_puttempname(lookup->sendmsg,
&lookup->name);
isc_buffer_init(&b, store, MXNAME);
res2 = dns_name_totext(dns_rootname, ISC_FALSE, &b);
check_result (res2, "dns_name_totext");
isc_buffer_usedregion (&b, &r);
fatal("Aborting: %s/%.*s is not a legal name syntax. "
"(%s)", lookup->textname, (int)r.length,
(char *)r.base, dns_result_totext(result));
}
}
isc_buffer_init (&b, store, MXNAME);
dns_name_totext(lookup->name, ISC_FALSE, &b);
isc_buffer_usedregion (&b, &r);
trying((int)r.length, (char *)r.base, lookup);
if (dns_name_isabsolute(lookup->name))
debug ("This is an absolute name.");
else
debug ("This is a relative name (which is wrong).");
if (lookup->rctext[0] == 0)
strcpy(lookup->rctext, "IN");
if (lookup->rttext[0] == 0)
strcpy(lookup->rttext, "A");
lookup->sendmsg->id = (unsigned short)(random() & 0xFFFF);
lookup->sendmsg->opcode = dns_opcode_query;
lookup->msgcounter = 0;
/*
* If this is a trace request, completely disallow recursion, since
* it's meaningless for traces.
*/
if (lookup->recurse && !lookup->trace && !lookup->ns_search_only) {
debug ("Recursive query");
lookup->sendmsg->flags |= DNS_MESSAGEFLAG_RD;
}
if (lookup->aaonly) {
debug ("AA query");
lookup->sendmsg->flags |= DNS_MESSAGEFLAG_AA;
}
if (lookup->adflag) {
debug ("AD query");
lookup->sendmsg->flags |= DNS_MESSAGEFLAG_AD;
}
if (lookup->cdflag) {
debug ("CD query");
lookup->sendmsg->flags |= DNS_MESSAGEFLAG_CD;
}
dns_message_addname(lookup->sendmsg, lookup->name,
DNS_SECTION_QUESTION);
if (lookup->trace_root) {
debug("Doing trace_root");
tr.base="SOA";
tr.length=3;
} else {
tr.base=lookup->rttext;
tr.length=strlen(lookup->rttext);
}
debug ("Data type is %s", lookup->rttext);
result = dns_rdatatype_fromtext(&rdtype, (isc_textregion_t *)&tr);
check_result(result, "dns_rdatatype_fromtext");
if ((rdtype == dns_rdatatype_axfr) ||
(rdtype == dns_rdatatype_ixfr)) {
lookup->doing_xfr = ISC_TRUE;
/*
* Force TCP mode if we're doing an xfr.
*/
lookup->tcp_mode = ISC_TRUE;
}
if (lookup->trace_root) {
tr.base="IN";
tr.length=2;
} else {
tr.base=lookup->rctext;
tr.length=strlen(lookup->rctext);
}
result = dns_rdataclass_fromtext(&rdclass, (isc_textregion_t *)&tr);
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 %p linked to lookup %p",
query, 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 %p, query %p",
query->lookup, query);
ISC_LIST_ENQUEUE(query->recvlist, &query->recvbuf, link);
query->working = ISC_TRUE;
debug ("recving with lookup=%p, query=%p, sock=%p",
query->lookup, query,
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);
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;
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 {
printf(";; Connection to "
"server %.*s "
"for %s timed out. "
"Giving up.\n",
(int)r.length, r.base,
q->lookup->textname);
}
}
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=%p, query=%p",
query->lookup, 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) {
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
/*
* Returns true if we should call cancel_lookup(). This is a hack.
*/
static isc_boolean_t
check_for_more_data(dig_query_t *query, dns_message_t *msg,
isc_socketevent_t *sevent)
{
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;
return (ISC_TRUE);
}
#ifdef NEVER
check_result(result, "dns_message_firstname");
#endif
do {
dns_name_t *name = NULL;
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;
return (ISC_TRUE);
}
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 an 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;
dns_rdata_freestruct(&soa);
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;
dns_rdata_freestruct(&soa);
return (ISC_TRUE);
}
/*
* 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 (ISC_FALSE);
}
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;
isc_boolean_t docancel = ISC_FALSE;
UNUSED (task);
debug ("recv_done()");
if (free_now) {
isc_event_free(&event);
return;
}
query = event->ev_arg;
debug("(lookup=%p, query=%p)",
query->lookup, 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 %p", 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;
}
dns_message_destroy(&msg);
cancel_lookup(query->lookup);
isc_event_free(&event);
return;
}
if (key != NULL) {
debug ("querysig 2 is %p", 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 %p",
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 %p", 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;
}
docancel = check_for_more_data(query, msg, sevent);
if (docancel) {
dns_message_destroy(&msg);
cancel_lookup(query->lookup);
}
}
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 ) {
dns_message_destroy(&msg);
cancel_lookup(query->lookup);
}
check_next_lookup(query->lookup);
}
if (msg != NULL)
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) {
if (l->timer != NULL)
isc_timer_detach (&l->timer);
q = ISC_LIST_HEAD(l->q);
while (q != NULL) {
debug ("Cancelling query %p, belonging to %p",
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);
}
q = ISC_LIST_NEXT(q, link);
}
l = ISC_LIST_NEXT(l, link);
}
s = ISC_LIST_HEAD(server_list);
while (s != NULL) {
debug ("Freeing global server %p", s);
ptr = s;
s = ISC_LIST_NEXT(s, link);
debug ("ptr is now %p", ptr);
isc_mem_free(mctx, ptr);
}
o = ISC_LIST_HEAD(search_list);
while (o != NULL) {
debug ("Freeing search %p", 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 (key != NULL) {
debug ("Freeing key %p", key);
dns_tsigkey_setdeleted(key);
dns_tsigkey_detach(&key);
}
if (namebuf != NULL)
isc_buffer_free(&namebuf);
l = ISC_LIST_HEAD(lookup_list);
while (l != NULL) {
q = ISC_LIST_HEAD(l->q);
while (q != NULL) {
debug ("Freeing query %p, belonging to %p",
q, l);
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 %p belonging to %p",
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->querysig != NULL) {
debug ("Freeing buffer %p", l->querysig);
isc_buffer_free(&l->querysig);
}
ptr = l;
l = ISC_LIST_NEXT(l, link);
isc_mem_free(mctx, ptr);
}
if (keyring != NULL) {
debug ("Freeing keyring %p", 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);
}
debug("Getting ready to exit, code=%d",_exitcode);
if (_exitcode != 0)
exit(_exitcode);
}