dighost.c revision 6331b9a45efba4e4e71e66ce4e2613a3433d18b2
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Copyright (C) 2000 Internet Software Consortium.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington *
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Permission to use, copy, modify, and distribute this software for any
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * purpose with or without fee is hereby granted, provided that the above
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * copyright notice and this permission notice appear in all copies.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington *
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * SOFTWARE.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
0a45f098e446a7f41c2a71d9dd0fd8f459b49c34Brian Wellington/* $Id: dighost.c,v 1.74 2000/07/11 17:35:49 mws Exp $ */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Notice to programmers: Do not use this code as an example of how to
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * use the ISC library to perform DNS lookups. Dig and Host both operate
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * on the request level, since they allow fine-tuning of output and are
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * intended as debugging tools. As a result, they perform many of the
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * functions which could be better handled using the dns_resolver
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * functions in most applications.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <config.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <stdlib.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <unistd.h>
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington#include <netdb.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <string.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <limits.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#if (!(defined(HAVE_ADDRINFO) && defined(HAVE_GETADDRINFO)))
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonextern int h_errno;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#endif
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <dns/message.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <dns/name.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <dns/rdata.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <dns/rdataclass.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <dns/rdatalist.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <dns/rdataset.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <dns/rdatastruct.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <dns/rdatatype.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <dns/result.h>
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington#include <dns/tsig.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <dst/dst.h>
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <isc/app.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <isc/base64.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <isc/entropy.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <isc/lang.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <isc/lex.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <isc/netdb.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <isc/result.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <isc/string.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <isc/task.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <isc/timer.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <isc/types.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <isc/util.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <dig/dig.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian WellingtonISC_LIST(dig_lookup_t) lookup_list;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian WellingtonISC_LIST(dig_server_t) server_list;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian WellingtonISC_LIST(dig_searchlist_t) search_list;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc_boolean_t
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington have_ipv6 = ISC_FALSE,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington specified_source = ISC_FALSE,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington free_now = ISC_FALSE,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington show_details = ISC_FALSE,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington usesearch = ISC_FALSE,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington qr = ISC_FALSE,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington is_dst_up = ISC_FALSE;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonin_port_t port = 53;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonunsigned int timeout = 0;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonisc_mem_t *mctx = NULL;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonisc_taskmgr_t *taskmgr = NULL;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonisc_task_t *global_task = NULL;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonisc_timermgr_t *timermgr = NULL;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonisc_socketmgr_t *socketmgr = NULL;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonisc_sockaddr_t bind_address;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonisc_sockaddr_t bind_any;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonchar *rootspace[BUFSIZE];
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc_buffer_t rootbuf;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonint sendcount = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonint sockcount = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonint ndots = -1;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonint tries = 3;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonint lookup_counter = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonchar fixeddomain[MXNAME] = "";
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonint exitcode = 9;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonchar keynametext[MXNAME];
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonchar keysecret[MXNAME] = "";
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtondns_name_t keyname;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtondns_tsig_keyring_t *keyring = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc_buffer_t *namebuf = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtondns_tsigkey_t *key = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc_boolean_t validated = ISC_TRUE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc_entropy_t *entp = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonextern isc_boolean_t isc_mem_debugging;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc_boolean_t debugging = ISC_FALSE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonchar *progname = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic isc_boolean_t
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtoncancel_lookup(dig_lookup_t *lookup);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic int
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtoncount_dots(char *string) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington char *s;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington int i = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington s = string;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington while (*s != '\0') {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (*s == '.')
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington i++;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington s++;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (i);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonstatic void
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonhex_dump(isc_buffer_t *b) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington unsigned int len;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_region_t r;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_buffer_remainingregion(b, &r);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington printf("Printing a buffer with length %d\n", r.length);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington for (len = 0; len < r.length; len++) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington printf("%02x ", r.base[len]);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (len != 0 && len % 16 == 0)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington printf("\n");
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington }
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (len % 16 != 0)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington printf("\n");
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington}
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonvoid
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonfatal(const char *format, ...) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington va_list args;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington fprintf (stderr, "%s: ", progname);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington va_start(args, format);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington vfprintf(stderr, format, args);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington va_end(args);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington fprintf(stderr, "\n");
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (exitcode == 0)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington exitcode = 8;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington#ifdef NEVER
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dighost_shutdown();
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington free_lists(exitcode);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (mctx != NULL) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (isc_mem_debugging)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_mem_stats(mctx, stderr);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mem_destroy(&mctx);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington }
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington#endif
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington exit(exitcode);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington}
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonvoid
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtondebug(const char *format, ...) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington va_list args;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (debugging) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington va_start(args, format);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington vfprintf(stderr, format, args);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington va_end(args);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington fprintf(stderr, "\n");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonvoid
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtoncheck_result(isc_result_t result, const char *msg) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (result != ISC_R_SUCCESS) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington exitcode = 1;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington fatal("%s: %s", msg, isc_result_totext(result));
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington }
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
8a63ecd93331cab4fd006f0556335d77abddd924Andreas Gustafssonisc_boolean_t
8a63ecd93331cab4fd006f0556335d77abddd924Andreas Gustafssonisclass(char *text) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Tests if a field is a class, without needing isc libs
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * initialized. This list will have to be manually kept in
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington * sync with what the libs support.
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington const char *classlist[] = { "in", "hs", "chaos" };
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington const int numclasses = 3;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington int i;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington for (i = 0; i < numclasses; i++)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (strcasecmp(text, classlist[i]) == 0)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (ISC_TRUE);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (ISC_FALSE);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc_boolean_t
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonistype(char *text) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Tests if a field is a type, without needing isc libs
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * initialized. This list will have to be manually kept in
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * sync with what the libs support.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
8a63ecd93331cab4fd006f0556335d77abddd924Andreas Gustafsson const char *typelist[] = {"a", "ns", "md", "mf", "cname",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "soa", "mb", "mg", "mr", "null",
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington "wks", "ptr", "hinfo", "minfo",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "mx", "txt", "rp", "afsdb",
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington "x25", "isdn", "rt", "nsap",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "nsap_ptr", "sig", "key", "px",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "gpos", "aaaa", "loc", "nxt",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "srv", "naptr", "kx", "cert",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "a6", "dname", "opt", "unspec",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "tkey", "tsig", "axfr", "any"};
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington const int numtypes = 42;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington int i;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington for (i = 0; i < numtypes; i++) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (strcasecmp(text, typelist[i]) == 0)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (ISC_TRUE);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (ISC_FALSE);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtondig_lookup_t *
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonrequeue_lookup(dig_lookup_t *lookold, isc_boolean_t servers) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dig_lookup_t *looknew;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dig_server_t *s, *srv;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("requeue_lookup()");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (free_now)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return(ISC_R_SUCCESS);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup_counter++;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (lookup_counter > LOOKUP_LIMIT)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington fatal("Too many lookups");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew = isc_mem_allocate(mctx, sizeof(struct dig_lookup));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (looknew == NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington fatal("Memory allocation failure in %s:%d",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington __FILE__, __LINE__);
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington looknew->pending = ISC_FALSE;
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington strncpy(looknew->textname, lookold-> textname, MXNAME);
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington strncpy(looknew->rttext, lookold-> rttext, 32);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington strncpy(looknew->rctext, lookold-> rctext, 32);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->namespace[0] = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->sendspace[0] = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->sendmsg = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->name = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->oname = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->timer = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->xfr_q = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->doing_xfr = lookold->doing_xfr;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->ixfr_serial = lookold->ixfr_serial;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->defname = lookold->defname;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->trace = lookold->trace;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->trace_root = lookold->trace_root;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->identify = lookold->identify;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->udpsize = lookold->udpsize;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->recurse = lookold->recurse;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->aaonly = lookold->aaonly;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->adflag = lookold->adflag;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->cdflag = lookold->cdflag;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->ns_search_only = lookold->ns_search_only;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->origin = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->querysig = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->retries = tries;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->nsfound = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->tcp_mode = lookold->tcp_mode;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->comments = lookold->comments;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->stats = lookold->stats;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->section_question = lookold->section_question;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->section_answer = lookold->section_answer;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->section_authority = lookold->section_authority;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->section_additional = lookold->section_additional;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->new_search = ISC_FALSE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_INIT(looknew->my_server_list);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_INIT(looknew->q);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->use_my_server_list = ISC_FALSE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (servers) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington looknew->use_my_server_list = lookold->use_my_server_list;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (looknew->use_my_server_list) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington s = ISC_LIST_HEAD(lookold->my_server_list);
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington while (s != NULL) {
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington srv = isc_mem_allocate(mctx,
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington sizeof(struct dig_server));
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington if (srv == NULL)
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington fatal("Memory allocation failure "
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington "in %s:%d", __FILE__, __LINE__);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington strncpy(srv->servername, s->servername,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington MXNAME);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_ENQUEUE(looknew->my_server_list, srv,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington link);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington s = ISC_LIST_NEXT(s, link);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("before insertion, init@%p "
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "-> %p, new@%p -> %p",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookold, lookold->link.next, looknew, looknew->link.next);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_INSERTAFTER(lookup_list, lookold, looknew, link);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("after insertion, init -> "
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "%p, new = %p, new -> %p",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookold, looknew, looknew->link.next);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (looknew);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellingtonvoid
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonsetup_system(void) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington char rcinput[MXNAME];
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington FILE *fp;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington char *ptr;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dig_server_t *srv;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dig_searchlist_t *search;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dig_lookup_t *l;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_boolean_t get_servers;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_result_t result;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_buffer_t secretsrc;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_buffer_t secretbuf;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington int secretsize;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington unsigned char *secretstore;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_lex_t *lex = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_stdtime_t now;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("setup_system()");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (fixeddomain[0] != 0) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("using fixed domain %s", fixeddomain);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington search = isc_mem_allocate(mctx, sizeof(struct dig_server));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (search == NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington fatal("Memory allocation failure in %s:%d",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington __FILE__, __LINE__);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington strncpy(search->origin, fixeddomain, MXNAME - 1);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_PREPEND(search_list, search, link);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington free_now = ISC_FALSE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington get_servers = ISC_TF(server_list.head == NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington fp = fopen(RESOLVCONF, "r");
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (fp != NULL) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington while (fgets(rcinput, MXNAME, fp) != 0) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ptr = strtok(rcinput, " \t\r\n");
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (ptr != NULL) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (get_servers &&
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington strcasecmp(ptr, "nameserver") == 0) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington debug("got a nameserver line");
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ptr = strtok(NULL, " \t\r\n");
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (ptr != NULL) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington srv = isc_mem_allocate(mctx,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington sizeof(struct dig_server));
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (srv == NULL)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington fatal("Memory "
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington "allocation "
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington "failure in "
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington "%s:%d",
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington __FILE__,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington __LINE__);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington strncpy((char *)srv->
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington servername,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ptr,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington MXNAME - 1);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ISC_LIST_APPEND
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington (server_list,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington srv, link);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington }
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington } else if (strcasecmp(ptr, "options") == 0) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ptr = strtok(NULL, " \t\r\n");
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (ptr != NULL) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if((strncasecmp(ptr, "ndots:",
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington 6) == 0) &&
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington (ndots == -1)) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ndots = atoi(
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington &ptr[6]);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington debug("ndots is "
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington "%d.",
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ndots);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington }
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington }
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington } else if ((strcasecmp(ptr, "search") == 0)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington && usesearch){
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington while ((ptr = strtok(NULL, " \t\r\n"))
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington != NULL) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington search = isc_mem_allocate(
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington mctx, sizeof(struct
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dig_server));
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (search == NULL)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington fatal("Memory "
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington "allocation "
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington "failure in %s:"
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington "%d", __FILE__,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington __LINE__);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington strncpy(search->
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington origin,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ptr,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington MXNAME - 1);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ISC_LIST_APPEND
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington (search_list,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington search,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington link);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington } else if ((strcasecmp(ptr, "domain") == 0) &&
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington (fixeddomain[0] == 0 )){
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington while ((ptr = strtok(NULL, " \t\r\n"))
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington != NULL) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington search = isc_mem_allocate(
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington mctx, sizeof(struct
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dig_server));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (search == NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington fatal("Memory "
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "allocation "
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "failure in %s:"
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "%d", __FILE__,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington __LINE__);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington strncpy(search->
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington origin,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ptr,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington MXNAME - 1);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_PREPEND
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington (search_list,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington search,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington link);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington fclose(fp);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (ndots == -1)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ndots = 1;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (server_list.head == NULL) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington srv = isc_mem_allocate(mctx, sizeof(dig_server_t));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (srv == NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington fatal("Memory allocation failure");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington strcpy(srv->servername, "127.0.0.1");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_APPEND(server_list, srv, link);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington for (l = ISC_LIST_HEAD(lookup_list) ;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington l != NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington l = ISC_LIST_NEXT(l, link) ) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington l -> origin = ISC_LIST_HEAD(search_list);
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington }
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington if (keysecret[0] != 0) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("keyring");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_tsigkeyring_create(mctx, &keyring);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "dns_tsigkeyring_create");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("buffer");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = isc_buffer_allocate(mctx, &namebuf, MXNAME);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "isc_buffer_allocate");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("name");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_name_init(&keyname, NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "dns_name_init");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_buffer_putstr(namebuf, keynametext);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington secretsize = strlen(keysecret) * 3 / 4;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("secretstore");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington secretstore = isc_mem_get(mctx, secretsize);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (secretstore == NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington fatal("Memory allocation failure in %s:%d",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington __FILE__, __LINE__);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_buffer_init(&secretsrc, keysecret, strlen(keysecret));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_buffer_add(&secretsrc, strlen(keysecret));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_buffer_init(&secretbuf, secretstore, secretsize);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("lex");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = isc_lex_create(mctx, strlen(keysecret), &lex);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "isc_lex_create");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = isc_lex_openbuffer(lex, &secretsrc);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "isc_lex_openbuffer");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = isc_base64_tobuffer(lex, &secretbuf, -1);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (result != ISC_R_SUCCESS) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington printf(";; Couldn't create key %s: %s\n",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington keynametext, isc_result_totext(result));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_lex_close(lex);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_lex_destroy(&lex);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington goto SYSSETUP_FAIL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington secretsize = isc_buffer_usedlength(&secretbuf);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("close");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_lex_close(lex);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_lex_destroy(&lex);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_stdtime_get(&now);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("namefromtext");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_name_fromtext(&keyname, namebuf,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_rootname, ISC_FALSE,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington namebuf);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (result != ISC_R_SUCCESS) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington printf (";; Couldn't create key %s: %s\n",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington keynametext, dns_result_totext(result));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington goto SYSSETUP_FAIL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("tsigkey");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_tsigkey_create(&keyname, dns_tsig_hmacmd5_name,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington secretstore, secretsize,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_TRUE, NULL, now, now, mctx,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington keyring, &key);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (result != ISC_R_SUCCESS) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington printf(";; Couldn't create key %s: %s\n",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington keynametext, dns_result_totext(result));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mem_put(mctx, secretstore, secretsize);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_name_invalidate(&keyname);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_buffer_free(&namebuf);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington SYSSETUP_FAIL:
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mem_put(mctx, secretstore, secretsize);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_name_invalidate(&keyname);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_buffer_free(&namebuf);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_tsigkeyring_destroy(&keyring);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonvoid
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonsetup_libs(void) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_result_t result;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("setup_libs()");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Warning: This is not particularly good randomness. We'll
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * just use random() now for getting id values, but doing so
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * does NOT insure that id's cann't be guessed.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington srandom(getpid() + (int)&setup_libs);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = isc_app_start();
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "isc_app_start");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = isc_net_probeipv4();
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "isc_net_probeipv4");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = isc_net_probeipv6();
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (result == ISC_R_SUCCESS)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington have_ipv6 = ISC_TRUE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = isc_mem_create(0, 0, &mctx);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "isc_mem_create");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = isc_taskmgr_create(mctx, 1, 0, &taskmgr);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "isc_taskmgr_create");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = isc_task_create(taskmgr, 0, &global_task);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "isc_task_create");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = isc_timermgr_create(mctx, &timermgr);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "isc_timermgr_create");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = isc_socketmgr_create(mctx, &socketmgr);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "isc_socketmgr_create");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = isc_entropy_create(mctx, &entp);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "isc_entropy_create");
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington result = dst_lib_init(mctx, entp, 0);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "dst_lib_init");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington is_dst_up = ISC_TRUE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonadd_opt(dns_message_t *msg, isc_uint16_t udpsize) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_rdataset_t *rdataset = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_rdatalist_t *rdatalist = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_rdata_t *rdata = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_result_t result;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("add_opt()");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_message_gettemprdataset(msg, &rdataset);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "dns_message_gettemprdataset");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_rdataset_init(rdataset);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_message_gettemprdatalist(msg, &rdatalist);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "dns_message_gettemprdatalist");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_message_gettemprdata(msg, &rdata);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "dns_message_gettemprdata");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("setting udp size of %d", udpsize);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington rdatalist->type = dns_rdatatype_opt;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington rdatalist->covers = 0;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington rdatalist->rdclass = udpsize;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington rdatalist->ttl = 0;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington rdata->data = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington rdata->length = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_INIT(rdatalist->rdata);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_rdatalist_tordataset(rdatalist, rdataset);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_message_setopt(msg, rdataset);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "dns_message_setopt");
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic void
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonadd_question(dns_message_t *message, dns_name_t *name,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_rdataclass_t rdclass, dns_rdatatype_t rdtype)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington{
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_rdataset_t *rdataset;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_result_t result;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("add_question()");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington rdataset = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_message_gettemprdataset(message, &rdataset);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington check_result(result, "dns_message_gettemprdataset()");
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_rdataset_init(rdataset);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_rdataset_makequestion(rdataset, rdclass, rdtype);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ISC_LIST_APPEND(name->list, rdataset, link);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Return ISC_TRUE if we're in the process of shutting down on the
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * return.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic isc_boolean_t
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtoncheck_next_lookup(dig_lookup_t *lookup) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dig_lookup_t *next;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dig_query_t *query;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_boolean_t still_working=ISC_FALSE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (free_now)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (ISC_TRUE);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("check_next_lookup(%p)", lookup);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington for (query = ISC_LIST_HEAD(lookup->q);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington query != NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington query = ISC_LIST_NEXT(query, link)) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (query->working) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("still have a worker", stderr);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington still_working=ISC_TRUE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington if (still_working)
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington return (ISC_FALSE);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington debug("have %d retries left for %s",
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington lookup->retries-1, lookup->textname);
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington debug("lookup %s pending", lookup->pending ? "is" : "is not");
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington next = ISC_LIST_NEXT(lookup, link);
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington if (lookup->tcp_mode) {
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington if (next == NULL) {
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington debug("shutting down", stderr);
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington dighost_shutdown();
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington return (ISC_TRUE);
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington }
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington if (next->sendmsg == NULL) {
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington debug("setting up for TCP");
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington setup_lookup(next);
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington do_lookup(next);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington } else {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (!lookup->pending) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (next == NULL) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("shutting down", stderr);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dighost_shutdown();
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (ISC_TRUE);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington if (next->sendmsg == NULL) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("setting up for UDP");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington setup_lookup(next);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington do_lookup(next);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington } else {
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington if (lookup->retries > 1) {
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington debug("retrying");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup->retries --;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (lookup->timer != NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_timer_detach(&lookup->timer);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington send_udp(lookup);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington } else {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("cancelling");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return(cancel_lookup(lookup));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (ISC_FALSE);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonfollowup_lookup(dns_message_t *msg, dig_query_t *query,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_section_t section) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dig_lookup_t *lookup = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dig_server_t *srv = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_rdataset_t *rdataset = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_rdata_t rdata;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_name_t *name = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_result_t result, loopresult;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_buffer_t *b = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_region_t r;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_boolean_t success = ISC_FALSE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington int len;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("followup_lookup()");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (free_now)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_message_firstname(msg,section);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (result != ISC_R_SUCCESS) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("firstname returned %s",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_result_totext(result));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if ((section == DNS_SECTION_ANSWER) &&
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington (query->lookup->trace || query->lookup->ns_search_only))
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington followup_lookup (msg, query, DNS_SECTION_AUTHORITY);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("following up %s", query->lookup->textname);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington for (;;) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington name = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_message_currentname(msg, section, &name);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington for (rdataset = ISC_LIST_HEAD(name->list);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington rdataset != NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington rdataset = ISC_LIST_NEXT(rdataset, link)) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington loopresult = dns_rdataset_first(rdataset);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington while (loopresult == ISC_R_SUCCESS) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_rdataset_current(rdataset, &rdata);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("got rdata with type %d",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington rdata.type);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if ((rdata.type == dns_rdatatype_ns) &&
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington (!query->lookup->trace_root ||
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington (query->lookup->nsfound < ROOTNS)))
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington query->lookup->nsfound++;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = isc_buffer_allocate(mctx, &b,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington BUFSIZE);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "isc_buffer_allocate");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_rdata_totext(&rdata,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington NULL,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington b);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "dns_rdata_totext");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_buffer_usedregion(b, &r);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington len = r.length-1;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (len >= MXNAME)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington len = MXNAME-1;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* Initialize lookup if we've not yet */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("found NS %d %.*s",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington (int)r.length, (int)r.length,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington (char *)r.base);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (!success) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington success = ISC_TRUE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup_counter++;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup = requeue_lookup
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington (query->lookup,
0a45f098e446a7f41c2a71d9dd0fd8f459b49c34Brian Wellington ISC_FALSE);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup->doing_xfr = ISC_FALSE;
0a45f098e446a7f41c2a71d9dd0fd8f459b49c34Brian Wellington lookup->defname = ISC_FALSE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup->use_my_server_list =
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_TRUE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (section ==
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington DNS_SECTION_ANSWER) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup->trace =
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_FALSE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup->ns_search_only =
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_FALSE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington else {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup->trace =
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington query->
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup->trace;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup->ns_search_only =
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington query->
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup->ns_search_only;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup->trace_root = ISC_FALSE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_INIT(lookup->
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington my_server_list);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington srv = isc_mem_allocate(mctx,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington sizeof(struct dig_server));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (srv == NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington fatal("Memory allocation "
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "failure in %s:%d",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington __FILE__, __LINE__);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington strncpy(srv->servername,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington (char *)r.base, len);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington srv->servername[len] = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("adding server %s",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington srv->servername);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_APPEND
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington (lookup->my_server_list,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington srv, link);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_buffer_free(&b);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington loopresult = dns_rdataset_next(rdataset);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_message_nextname (msg, section);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (result != ISC_R_SUCCESS)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington break;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if ((lookup == NULL) && (section == DNS_SECTION_ANSWER) &&
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington (query->lookup->trace || query->lookup->ns_search_only))
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington followup_lookup(msg, query, DNS_SECTION_AUTHORITY);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonnext_origin(dns_message_t *msg, dig_query_t *query) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dig_lookup_t *lookup;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNUSED(msg);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("next_origin()");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (free_now)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("following up %s", query->lookup->textname);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (query->lookup->origin == NULL) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Then we just did rootorg; there's nothing left.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("made it to the root with nowhere to go");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup = requeue_lookup(query->lookup, ISC_TRUE);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup->defname = ISC_FALSE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup->origin = ISC_LIST_NEXT(query->lookup->origin, link);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtoninsert_soa(dig_lookup_t *lookup) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_result_t result;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_rdata_soa_t soa;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_rdata_t *rdata = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_rdatalist_t *rdatalist = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_rdataset_t *rdataset = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_name_t *soaname = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("insert_soa()");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington soa.mctx = mctx;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington soa.serial = lookup->ixfr_serial;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington soa.refresh = 1;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington soa.retry = 1;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington soa.expire = 1;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington soa.minimum = 1;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington soa.common.rdclass = dns_rdataclass_in;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington soa.common.rdtype = dns_rdatatype_soa;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_name_init(&soa.origin, NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_name_init(&soa.mname, NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_name_clone(lookup->name, &soa.origin);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_name_clone(lookup->name, &soa.mname);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_buffer_init(&lookup->rdatabuf, lookup->rdatastore,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington MXNAME);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_message_gettemprdata(lookup->sendmsg, &rdata);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "dns_message_gettemprdata");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_rdata_fromstruct(rdata, dns_rdataclass_in,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_rdatatype_soa, &soa,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington &lookup->rdatabuf);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "isc_rdata_fromstruct");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_message_gettemprdatalist(lookup->sendmsg, &rdatalist);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "dns_message_gettemprdatalist");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_message_gettemprdataset(lookup->sendmsg, &rdataset);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "dns_message_gettemprdataset");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_rdatalist_init(rdatalist);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington rdatalist->type = dns_rdatatype_soa;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington rdatalist->rdclass = dns_rdataclass_in;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington rdatalist->covers = dns_rdatatype_soa;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington rdatalist->ttl = 1;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_INIT(rdatalist->rdata);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_rdataset_init(rdataset);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_rdatalist_tordataset(rdatalist, rdataset);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_message_gettempname(lookup->sendmsg, &soaname);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "dns_message_gettempname");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_name_init(soaname, NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_name_clone(lookup->name, soaname);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_INIT(soaname->list);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_APPEND(soaname->list, rdataset, link);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_message_addname(lookup->sendmsg, soaname, DNS_SECTION_AUTHORITY);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonvoid
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonsetup_lookup(dig_lookup_t *lookup) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_result_t result, res2;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington int len;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_rdatatype_t rdtype;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_rdataclass_t rdclass;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dig_server_t *serv;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dig_query_t *query;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_region_t r;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_constregion_t tr;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_buffer_t b;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington char store[MXNAME];
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(lookup != NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("setup_lookup(%p)",lookup);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (free_now)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington debug("setting up for looking up %s @%p->%p",
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington lookup->textname, lookup,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington lookup->link.next);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington result = dns_message_create(mctx, DNS_MESSAGE_INTENTRENDER,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington &lookup->sendmsg);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington check_result(result, "dns_message_create");
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (lookup->new_search) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington debug("resetting lookup counter.");
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington lookup_counter = 0;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington }
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington result = dns_message_gettempname(lookup->sendmsg, &lookup->name);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington check_result(result, "dns_message_gettempname");
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_name_init(lookup->name, NULL);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_buffer_init(&lookup->namebuf, lookup->namespace,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington sizeof(lookup->namespace));
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_buffer_init(&lookup->onamebuf, lookup->onamespace,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington sizeof(lookup->onamespace));
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if ((count_dots(lookup->textname) >= ndots) || lookup->defname)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington lookup->origin = NULL; /* Force root lookup */
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington debug("lookup->origin = %p", lookup->origin);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (lookup->origin != NULL) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington debug("trying origin %s", lookup->origin->origin);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington result = dns_message_gettempname(lookup->sendmsg,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington &lookup->oname);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington check_result(result, "dns_message_gettempname");
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_name_init(lookup->oname, NULL);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington len = strlen(lookup->origin->origin);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_buffer_init(&b, lookup->origin->origin, len);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_buffer_add(&b, len);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington result = dns_name_fromtext(lookup->oname, &b, dns_rootname,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ISC_FALSE, &lookup->onamebuf);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (result != ISC_R_SUCCESS) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_message_puttempname(lookup->sendmsg,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington &lookup->name);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_message_puttempname(lookup->sendmsg,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington &lookup->oname);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington fatal("%s is not a legal name syntax (%s)",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup->origin->origin,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_result_totext(result));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (!lookup->trace_root) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington len = strlen(lookup->textname);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_buffer_init(&b, lookup->textname, len);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_buffer_add(&b, len);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_name_fromtext(lookup->name, &b,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup->oname, ISC_FALSE,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington &lookup->namebuf);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington } else {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_name_clone(dns_rootname, lookup->name);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (result != ISC_R_SUCCESS) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_message_puttempname(lookup->sendmsg,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington &lookup->name);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_message_puttempname(lookup->sendmsg,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington &lookup->oname);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington fatal("%s is not a legal name syntax (%s)",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup->textname, dns_result_totext(result));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_message_puttempname(lookup->sendmsg, &lookup->oname);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington } else {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("using root origin");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (!lookup->trace_root) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington len = strlen(lookup->textname);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_buffer_init(&b, lookup->textname, len);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_buffer_add(&b, len);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_name_fromtext(lookup->name, &b,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_rootname,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_FALSE,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington &lookup->namebuf);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington } else {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_name_clone(dns_rootname, lookup->name);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (result != ISC_R_SUCCESS) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_message_puttempname(lookup->sendmsg,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington &lookup->name);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_buffer_init(&b, store, MXNAME);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington res2 = dns_name_totext(dns_rootname, ISC_FALSE, &b);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(res2, "dns_name_totext");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_buffer_usedregion(&b, &r);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington fatal("%s/%.*s is not a legal name syntax "
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "(%s)", lookup->textname, (int)r.length,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington (char *)r.base, dns_result_totext(result));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_buffer_init(&b, store, MXNAME);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_name_totext(lookup->name, ISC_FALSE, &b);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_buffer_usedregion(&b, &r);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington trying((int)r.length, (char *)r.base, lookup);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ENSURE(dns_name_isabsolute(lookup->name));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (lookup->rctext[0] == 0)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington strcpy(lookup->rctext, "IN");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (lookup->rttext[0] == 0)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington strcpy(lookup->rttext, "A");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup->sendmsg->id = (unsigned short)(random() & 0xFFFF);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup->sendmsg->opcode = dns_opcode_query;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup->msgcounter = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * If this is a trace request, completely disallow recursion, since
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * it's meaningless for traces.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (lookup->recurse && !lookup->trace && !lookup->ns_search_only) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("recursive query");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup->sendmsg->flags |= DNS_MESSAGEFLAG_RD;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (lookup->aaonly) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("AA query");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup->sendmsg->flags |= DNS_MESSAGEFLAG_AA;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (lookup->adflag) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("AD query");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup->sendmsg->flags |= DNS_MESSAGEFLAG_AD;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (lookup->cdflag) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("CD query");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup->sendmsg->flags |= DNS_MESSAGEFLAG_CD;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_message_addname(lookup->sendmsg, lookup->name,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington DNS_SECTION_QUESTION);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (lookup->trace_root) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("doing trace_root");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington tr.base = "SOA";
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington tr.length = 3;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington } else {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington tr.base = lookup->rttext;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington tr.length = strlen(lookup->rttext);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("data type is %s", lookup->rttext);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_rdatatype_fromtext(&rdtype, (isc_textregion_t *)&tr);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "dns_rdatatype_fromtext");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if ((rdtype == dns_rdatatype_axfr) ||
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington (rdtype == dns_rdatatype_ixfr)) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup->doing_xfr = ISC_TRUE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Force TCP mode if we're doing an xfr.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup->tcp_mode = ISC_TRUE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (lookup->trace_root) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington tr.base = "IN";
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington tr.length = 2;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington } else {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington tr.base = lookup->rctext;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington tr.length = strlen(lookup->rctext);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_rdataclass_fromtext(&rdclass, (isc_textregion_t *)&tr);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "dns_rdataclass_fromtext");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington add_question(lookup->sendmsg, lookup->name, rdclass, rdtype);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (rdtype == dns_rdatatype_ixfr)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington insert_soa(lookup);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (key != NULL) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("initializing keys");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_message_settsigkey(lookup->sendmsg, key);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "dns_message_settsigkey");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup->tsigctx = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup->querysig = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("starting to render the message");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_buffer_init(&lookup->sendbuf, lookup->sendspace, COMMSIZE);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_message_renderbegin(lookup->sendmsg, &lookup->sendbuf);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "dns_message_renderbegin");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (lookup->udpsize > 0) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington add_opt(lookup->sendmsg, lookup->udpsize);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_message_rendersection(lookup->sendmsg,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington DNS_SECTION_QUESTION, 0);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "dns_message_rendersection");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_message_rendersection(lookup->sendmsg,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington DNS_SECTION_AUTHORITY, 0);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "dns_message_rendersection");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_message_renderend(lookup->sendmsg);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "dns_message_renderend");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("done rendering");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup->pending = ISC_FALSE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (lookup->use_my_server_list)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington serv = ISC_LIST_HEAD(lookup->my_server_list);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington else
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington serv = ISC_LIST_HEAD(server_list);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington for (; serv != NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington serv = ISC_LIST_NEXT(serv, link)) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington query = isc_mem_allocate(mctx, sizeof(dig_query_t));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (query == NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington fatal("Memory allocation failure in %s:%d",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington __FILE__, __LINE__);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("create query %p linked to lookup %p",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington query, lookup);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington query->lookup = lookup;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington query->working = ISC_FALSE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington query->waiting_connect = ISC_FALSE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington query->first_pass = ISC_TRUE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington query->first_soa_rcvd = ISC_FALSE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington query->second_rr_rcvd = ISC_FALSE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington query->second_rr_serial = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington query->servname = serv->servername;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_INIT(query->sendlist);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_INIT(query->recvlist);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_INIT(query->lengthlist);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington query->sock = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_buffer_init(&query->recvbuf, query->recvspace, COMMSIZE);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_buffer_init(&query->lengthbuf, query->lengthspace, 2);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_buffer_init(&query->slbuf, query->slspace, 2);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_ENQUEUE(lookup->q, query, link);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (!ISC_LIST_EMPTY(lookup->q) && qr) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington printmessage(ISC_LIST_HEAD(lookup->q), lookup->sendmsg,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_TRUE);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonsend_done(isc_task_t *task, isc_event_t *event) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNUSED(task);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_event_free(&event);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("send_done()");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Return ISC_TRUE if we're in the process of shutting down
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic isc_boolean_t
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtoncancel_lookup(dig_lookup_t *lookup) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dig_query_t *query = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("cancel_lookup()");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington for (query = ISC_LIST_HEAD(lookup->q);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington query != NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington query = ISC_LIST_NEXT(query, link)) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (query->working) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("cancelling a worker");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (query->sock != NULL) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_socket_cancel(query->sock, global_task,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_SOCKCANCEL_ALL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_socket_detach(&query->sock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington sockcount--;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("socket = %d", sockcount);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup->pending = ISC_FALSE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington lookup->retries = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return(check_next_lookup(lookup));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonrecv_done(isc_task_t *task, isc_event_t *event);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellingtonstatic void
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellingtonconnect_timeout(isc_task_t *task, isc_event_t *event);
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonvoid
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonsend_udp(dig_lookup_t *lookup) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dig_query_t *query;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_result_t result;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington unsigned int local_timeout;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("send_udp()");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (timeout != INT_MAX) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (timeout == 0) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (lookup->tcp_mode)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington local_timeout = TCP_TIMEOUT;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington else
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington local_timeout = UDP_TIMEOUT;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington } else
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington local_timeout = timeout;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug ("have local timeout of %d", local_timeout);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_interval_set(&lookup->interval, local_timeout, 0);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = isc_timer_create(timermgr, isc_timertype_once, NULL,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington &lookup->interval, global_task,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington connect_timeout, lookup,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington &lookup->timer);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "isc_timer_create");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonfor (query = ISC_LIST_HEAD(lookup->q);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington query != NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington query = ISC_LIST_NEXT(query, link)) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("working on lookup %p, query %p",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington query->lookup, query);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_ENQUEUE(query->recvlist, &query->recvbuf, link);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington query->working = ISC_TRUE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("recving with lookup=%p, query=%p, sock=%p",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington query->lookup, query,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington query->sock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = isc_socket_recvv(query->sock, &query->recvlist, 1,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington global_task, recv_done, query);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "isc_socket_recvv");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington sendcount++;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("sent count number %d", sendcount);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_ENQUEUE(query->sendlist, &lookup->sendbuf, link);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("sending a request");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = isc_time_now(&query->time_sent);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "isc_time_now");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ENSURE(query->sock != NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = isc_socket_sendtov(query->sock, &query->sendlist,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington global_task, send_done, query,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington &query->sockaddr, NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "isc_socket_sendtov");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * connect_timeout is used for both UDP recieves and TCP connects.
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington */
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonstatic void
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonconnect_timeout(isc_task_t *task, isc_event_t *event) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dig_lookup_t *lookup=NULL;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dig_query_t *q=NULL;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_result_t result;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_buffer_t *b=NULL;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_region_t r;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington REQUIRE(event->ev_type == ISC_TIMEREVENT_IDLE);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington debug("connect_timeout()");
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington lookup = event->ev_arg;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_event_free(&event);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington debug("buffer allocate connect_timeout");
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington result = isc_buffer_allocate(mctx, &b, 256);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington check_result(result, "isc_buffer_allocate");
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington for (q = ISC_LIST_HEAD(lookup->q);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington q != NULL;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington q = ISC_LIST_NEXT(q, link)) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (q->working) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (!free_now) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_buffer_clear(b);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington result = isc_sockaddr_totext(&q->sockaddr, b);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington check_result(result, "isc_sockaddr_totext");
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_buffer_usedregion(b, &r);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if ((q->lookup->retries > 1) &&
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington (!q->lookup->tcp_mode))
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington printf(";; Connection to server %.*s "
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington "for %s timed out. "
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington "Retrying %d.\n",
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington (int)r.length, r.base,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington q->lookup->textname,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington q->lookup->retries-1);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington else {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington printf(";; Connection to "
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington "server %.*s "
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington "for %s timed out. "
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington "Giving up.\n",
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington (int)r.length, r.base,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington q->lookup->textname);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington }
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington }
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_socket_cancel(q->sock, task,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ISC_SOCKCANCEL_ALL);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington }
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington }
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ENSURE(lookup->timer != NULL);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_timer_detach(&lookup->timer);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_buffer_free(&b);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington debug("done with connect_timeout()");
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington}
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonstatic void
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtontcp_length_done(isc_task_t *task, isc_event_t *event) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_socketevent_t *sevent;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_buffer_t *b=NULL;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_region_t r;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_result_t result;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dig_query_t *query=NULL;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_uint16_t length;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington REQUIRE(event->ev_type == ISC_SOCKEVENT_RECVDONE);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington UNUSED(task);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington debug("tcp_length_done()");
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (free_now) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_event_free(&event);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington return;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington }
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington sevent = (isc_socketevent_t *)event;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington query = event->ev_arg;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (sevent->result == ISC_R_CANCELED) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington query->working = ISC_FALSE;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_event_free(&event);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington check_next_lookup(query->lookup);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington return;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington }
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (sevent->result != ISC_R_SUCCESS) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington debug("buffer allocate connect_timeout");
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington result = isc_buffer_allocate(mctx, &b, 256);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington check_result(result, "isc_buffer_allocate");
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington result = isc_sockaddr_totext(&query->sockaddr, b);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington check_result(result, "isc_sockaddr_totext");
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_buffer_usedregion(b, &r);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington printf("%.*s: %s\n", (int)r.length, r.base,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_result_totext(sevent->result));
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_buffer_free(&b);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington query->working = ISC_FALSE;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington sockcount--;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington debug("socket = %d",sockcount);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_socket_detach(&query->sock);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_event_free(&event);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington check_next_lookup(query->lookup);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington b = ISC_LIST_HEAD(sevent->bufferlist);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_DEQUEUE(sevent->bufferlist, &query->lengthbuf, link);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington length = isc_buffer_getuint16(b);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (length > COMMSIZE) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_event_free(&event);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington fatal("Length of %X was longer than I can handle!",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington length);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Even though the buffer was already init'ed, we need
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * to redo it now, to force the length we want.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_buffer_invalidate(&query->recvbuf);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_buffer_init(&query->recvbuf, query->recvspace, length);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ENSURE(ISC_LIST_EMPTY(query->recvlist));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_ENQUEUE(query->recvlist, &query->recvbuf, link);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("recving with lookup=%p, query=%p",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington query->lookup, query);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = isc_socket_recvv(query->sock, &query->recvlist, length, task,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington recv_done, query);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_result(result, "isc_socket_recvv");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("resubmitted recv request with length %d", length);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_event_free(&event);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonlaunch_next_query(dig_query_t *query, isc_boolean_t include_question) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_result_t result;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("launch_next_query()");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (free_now)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (!query->lookup->pending) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("ignoring launch_next_query because !pending");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington sockcount--;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington debug("socket = %d", sockcount);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_socket_detach(&query->sock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington 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;
isc_event_free(&event);
check_next_lookup(query->lookup);
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;
isc_boolean_t result_bool;
unsigned int local_timeout;
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->tcp_mode) &&
(query->lookup->timer != NULL))
isc_timer_touch(query->lookup->timer);
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;
isc_event_free(&event);
/*
* In this case, we don't actually use result_bool
*/
result_bool = cancel_lookup(query->lookup);
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);
isc_event_free(&event);
result_bool = cancel_lookup(query->lookup);
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;
/*
* Once we are in the XFR message, increase
* the timeout to much longer, so brief network
* outages won't cause the XFR to abort
*/
if ((timeout != INT_MAX) &&
(query->lookup->timer != NULL)) {
if (timeout == 0) {
if (query->lookup->tcp_mode)
local_timeout = TCP_TIMEOUT;
else
local_timeout = UDP_TIMEOUT;
} else {
if (timeout < (INT_MAX / 4))
local_timeout = timeout * 4;
else
local_timeout = INT_MAX;
}
debug ("have local timeout of %d",
local_timeout);
isc_interval_set(&query->lookup->interval,
local_timeout, 0);
result = isc_timer_reset(query->lookup->timer,
isc_timertype_once,
NULL,
&query->lookup->interval,
ISC_FALSE);
check_result(result, "isc_timer_create");
}
}
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);
result_bool = cancel_lookup(query->lookup);
}
if (msg != NULL)
dns_message_destroy(&msg);
isc_event_free(&event);
}
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->trace ) {
received(b->used, r.length,
(char *)r.base,
query);
}
}
query->working = ISC_FALSE;
query->lookup->pending = ISC_FALSE;
result_bool = ISC_FALSE;
if (!query->lookup->ns_search_only ||
query->lookup->trace_root) {
dns_message_destroy(&msg);
result_bool = cancel_lookup(query->lookup);
}
if (msg != NULL)
dns_message_destroy(&msg);
isc_event_free(&event);
if ((!free_now) && (!result_bool))
check_next_lookup(query->lookup);
}
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;
isc_event_free(&event);
check_next_lookup(query->lookup);
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' (h_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;
unsigned int local_timeout;
debug("do_lookup_tcp()");
lookup->pending = ISC_TRUE;
if (timeout != INT_MAX) {
if (timeout == 0) {
if (lookup->tcp_mode)
local_timeout = TCP_TIMEOUT;
else
local_timeout = UDP_TIMEOUT;
} else
local_timeout = timeout;
debug ("have local timeout of %d", local_timeout);
isc_interval_set(&lookup->interval, local_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()");
ENSURE(!lookup->tcp_mode);
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
onrun_callback(isc_task_t *task, isc_event_t *event) {
UNUSED(task);
isc_event_free(&event);
start_lookup();
}
void
free_lists(void) {
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);
}
}