dighost.c revision f17b62a64bec4690f7ef0263efee1fa727b66fd5
1633838b8255282d10af15c5c84cee5a51466712Bob Halley/*
803d8426030e1f40c45d6861e95587189173f368Tinderbox User * Copyright (C) 2000 Internet Software Consortium.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Permission to use, copy, modify, and distribute this software for any
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * purpose with or without fee is hereby granted, provided that the above
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * copyright notice and this permission notice appear in all copies.
1633838b8255282d10af15c5c84cee5a51466712Bob Halley *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * SOFTWARE.
1633838b8255282d10af15c5c84cee5a51466712Bob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence * Notice to programmers: Do not use this code as an example of how to
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley * use the ISC library to perform DNS lookups. Dig and Host both operate
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley * on the request level, since they allow fine-tuning of output and are
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley * intended as debugging tools. As a result, they perform many of the
364a82f7c25b62967678027043425201a5e5171aBob Halley * functions which could be better handled using the dns_resolver
96754ed7b400ce080279de2f92111ad868105290Bob Halley * functions in most applications.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
96754ed7b400ce080279de2f92111ad868105290Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#include <config.h>
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <stdlib.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <unistd.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrewsextern int h_errno;
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews#include <isc/app.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/netdb.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/string.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/task.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/timer.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/util.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dns/message.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dns/rdata.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dns/rdataclass.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dns/rdataset.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dns/rdatatype.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dns/rdatalist.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dns/result.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dig/dig.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob AusteinISC_LIST(dig_lookup_t) lookup_list;
ab023a65562e62b85a824509d829b6fad87e00b1Rob AusteinISC_LIST(dig_server_t) server_list;
ab023a65562e62b85a824509d829b6fad87e00b1Rob AusteinISC_LIST(dig_searchlist_t) search_list;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinisc_boolean_t tcp_mode = ISC_FALSE, have_ipv6 = ISC_FALSE,
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein free_now = ISC_FALSE, show_details = ISC_FALSE, usesearch=ISC_TRUE,
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein qr = ISC_FALSE;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#ifdef TWIDDLE
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinisc_boolean_t twiddle = ISC_FALSE;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#endif
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinin_port_t port = 53;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinunsigned int timeout = 5;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinisc_mem_t *mctx = NULL;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinisc_taskmgr_t *taskmgr = NULL;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinisc_task_t *task = NULL;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinisc_timermgr_t *timermgr = NULL;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinisc_socketmgr_t *socketmgr = NULL;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinchar *rootspace[BUFSIZE];
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinisc_buffer_t rootbuf;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinint sendcount = 0;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinint ndots = -1;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinint tries = 3;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinint lookup_counter = 0;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinchar fixeddomain[MXNAME]="";
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinint exitcode = 9;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinstatic void
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinfree_lists(void);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinstatic int
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeincount_dots(char *string) {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein char *s;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein int i=0;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein s = string;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein while (*s != 0) {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein if (*s == '.')
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein i++;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein s++;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein }
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein return (i);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein}
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinstatic void
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinhex_dump(isc_buffer_t *b) {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein unsigned int len;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_region_t r;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_buffer_remainingregion(b, &r);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein printf("Printing a buffer with length %d\n", r.length);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein for (len = 0 ; len < r.length ; len++) {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein printf("%02x ", r.base[len]);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein if (len != 0 && len % 16 == 0)
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein printf("\n");
bfe0517fdcbe1dc62fee18e460ecf467dd491d9bEvan Hunt }
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein if (len % 16 != 0)
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein printf("\n");
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein}
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinvoid
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinfatal(char *format, ...) {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein va_list args;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein va_start(args, format);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein vfprintf(stderr, format, args);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein va_end(args);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein fprintf(stderr, "\n");
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#ifdef NEVER
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_app_shutdown();
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein free_lists();
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein if (mctx != NULL) {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#ifdef MEMDEBUG
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_mem_stats(mctx, stderr);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#endif
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_mem_destroy(&mctx);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein }
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#endif
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein exit(exitcode);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein}
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#ifdef DEBUG
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinvoid
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeindebug(char *format, ...) {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein va_list args;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein va_start(args, format);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein vfprintf(stderr, format, args);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein va_end(args);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein fprintf(stderr, "\n");
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein}
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#else
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinvoid
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeindebug(char *format, ...) {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein va_list args;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein UNUSED(args);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein UNUSED(format);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein}
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#endif
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinvoid
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeincheck_result(isc_result_t result, char *msg) {
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews if (result != ISC_R_SUCCESS) {
9b80f3a7c739a99b498a37a711a51b6a88df3a78Mark Andrews exitcode = 1;
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews fatal("%s: %s", msg, isc_result_totext(result));
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews }
0cae66577c69c89086cd065bb297690072b471b4Mark Andrews}
77b8f88f144928eddcca144c348d6ef53e7d5c43Evan Hunt
0cae66577c69c89086cd065bb297690072b471b4Mark Andrewsisc_boolean_t
3ad3e9c948fd8781d0343b19f0412f5bf3ea6415Evan Huntisclass(char *text) {
3a6d62c59f73d024d1b2ba2ac94d7600530166ffMark Andrews /* Tests if a field is a class, without needing isc libs
bad93fb90cc5f14e200f23ff67cce3127f45b705Mark Andrews * initialized. This list will have to be manually kept in
bad93fb90cc5f14e200f23ff67cce3127f45b705Mark Andrews * sync with what the libs support.
0cfb24736841b3e98bb25853229a0efabab88bddEvan Hunt */
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley const char *classlist[] = {"in", "hs"};
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley const int numclasses = 2;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt int i;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt for (i = 0; i < numclasses; i++)
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt if (strcasecmp(text, classlist[i]) == 0)
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt return ISC_TRUE;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt return ISC_FALSE;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt}
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt
9e804040a29b9c3066c8471b43835f30707039b7Evan Huntisc_boolean_t
9e804040a29b9c3066c8471b43835f30707039b7Evan Huntistype(char *text) {
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt /* Tests if a field is a type, without needing isc libs
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt * initialized. This list will have to be manually kept in
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt * sync with what the libs support.
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt */
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt const char *typelist[] = {"a", "ns", "md", "mf", "cname",
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt "soa", "mb", "mg", "mr", "null",
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt "wks", "ptr", "hinfo", "minfo",
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt "mx", "txt", "rp", "afsdb",
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt "x25", "isdn", "rt", "nsap",
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt "nsap_ptr", "sig", "key", "px",
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt "gpos", "aaaa", "loc", "nxt",
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt "srv", "naptr", "kx", "cert",
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt "a6", "dname", "opt", "unspec",
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt "tkey", "tsig", "axfr", "any"};
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt const int numtypes = 42;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt int i;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt for (i = 0; i < numtypes; i++) {
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt if (strcasecmp(text, typelist[i]) == 0)
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt return ISC_TRUE;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt }
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt return ISC_FALSE;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt}
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt#ifdef TWIDDLE
9e804040a29b9c3066c8471b43835f30707039b7Evan Huntvoid
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunttwiddlebuf(isc_buffer_t buf) {
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt isc_region_t r;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt int len, pos, bit;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt unsigned char bitfield;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt int i, tw;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt hex_dump(&buf);
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt tw=TWIDDLE;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt printf ("Twiddling %d bits: ", tw);
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt for (i=0;i<tw;i++) {
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt isc_buffer_usedregion (&buf, &r);
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt len = r.length;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt pos=(int)random();
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt pos = pos%len;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt bit = (int)random()%8;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt bitfield = 1 << bit;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt printf ("%d@%03x ", bit, pos);
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt r.base[pos] ^= bitfield;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt }
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt puts ("");
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt hex_dump(&buf);
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt}
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt#endif
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt
9e804040a29b9c3066c8471b43835f30707039b7Evan Huntstatic void
9e804040a29b9c3066c8471b43835f30707039b7Evan Huntsetup_system(void) {
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt char rcinput[MXNAME];
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt FILE *fp;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt char *ptr;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt dig_server_t *srv;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt dig_searchlist_t *search;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt dig_lookup_t *l;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt isc_boolean_t get_servers;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt if (fixeddomain[0]!=0) {
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt search = isc_mem_allocate( mctx, sizeof(struct dig_server));
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt if (search == NULL)
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt fatal("Memory allocation failure in %s:%d",
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt __FILE__, __LINE__);
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt strncpy(search->origin, fixeddomain, MXNAME - 1);
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt ISC_LIST_PREPEND(search_list, search, link);
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt }
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt debug ("setup_system()");
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt /*
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt * Warning: This is not particularly good randomness. We'll
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt * just use random() now for getting id values, but doing so
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt * does NOT insure that id's cann't be guessed.
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt */
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt srandom (getpid() + (int)&setup_system);
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt get_servers = (server_list.head == NULL);
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt fp = fopen (RESOLVCONF, "r");
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt if (fp != NULL) {
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt while (fgets(rcinput, MXNAME, fp) != 0) {
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt ptr = strtok (rcinput, " \t\r\n");
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt if (ptr != NULL) {
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt if (get_servers &&
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt strcasecmp(ptr, "nameserver") == 0) {
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt debug ("Got a nameserver line");
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt ptr = strtok (NULL, " \t\r\n");
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt if (ptr != NULL) {
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt srv = isc_mem_allocate(mctx,
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt sizeof(struct dig_server));
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt if (srv == NULL)
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt fatal("Memory "
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt "allocation "
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt "failure in "
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt "%s:%d",
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt __FILE__,
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt __LINE__);
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt strncpy((char *)srv->
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt servername,
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt ptr,
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt MXNAME - 1);
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt ISC_LIST_APPEND
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt (server_list,
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt srv, link);
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt }
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt } else if (strcasecmp(ptr, "options") == 0) {
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence ptr = strtok(NULL, " \t\r\n");
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt if (ptr != NULL) {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein if ((strncasecmp(ptr, "ndots:",
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein 6) == 0) &&
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein (ndots == -1)) {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein ndots = atoi(
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein &ptr[6]);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein debug ("ndots is "
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein "%d.",
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein ndots);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein }
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein }
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein } else if ((strcasecmp(ptr, "search") == 0)
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein && usesearch){
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt while ((ptr = strtok(NULL, " \t\r\n"))
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt != NULL) {
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt search = isc_mem_allocate(
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt mctx, sizeof(struct
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein dig_server));
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt if (search == NULL)
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein fatal("Memory "
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "allocation "
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "failure in %s:"
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt "%d", __FILE__,
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt __LINE__);
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt strncpy(search->
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt origin,
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt ptr,
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt MXNAME - 1);
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt ISC_LIST_APPEND
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt (search_list,
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt search,
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt link);
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt }
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt } else if ((strcasecmp(ptr, "domain") == 0) &&
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt (fixeddomain[0] == 0 )){
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt while ((ptr = strtok(NULL, " \t\r\n"))
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt != NULL) {
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt search = isc_mem_allocate(
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt mctx, sizeof(struct
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt dig_server));
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt if (search == NULL)
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt fatal("Memory "
138a6660fb74d458270511ca0ba24aa19bf375d5Bob Halley "allocation "
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley "failure in %s:"
138a6660fb74d458270511ca0ba24aa19bf375d5Bob Halley "%d", __FILE__,
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley __LINE__);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley strncpy(search->
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley origin,
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley ptr,
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley MXNAME - 1);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley ISC_LIST_PREPEND
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley (search_list,
138a6660fb74d458270511ca0ba24aa19bf375d5Bob Halley search,
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley link);
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley }
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley }
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley }
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley }
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley fclose (fp);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley }
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt if (ndots == -1)
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt ndots = 1;
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt if (server_list.head == NULL) {
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt srv = isc_mem_allocate(mctx, sizeof(dig_server_t));
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt if (srv == NULL)
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt fatal("Memory allocation failure");
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt strcpy(srv->servername, "127.0.0.1");
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt ISC_LIST_APPEND(server_list, srv, link);
9e804040a29b9c3066c8471b43835f30707039b7Evan Hunt }
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley for (l = ISC_LIST_HEAD(lookup_list) ;
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley l != NULL;
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley l = ISC_LIST_NEXT(l, link) ) {
96754ed7b400ce080279de2f92111ad868105290Bob Halley l -> origin = ISC_LIST_HEAD(search_list);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley }
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley}
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencestatic void
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halleysetup_libs(void) {
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley isc_result_t result;
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley isc_buffer_t b;
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley debug ("setup_libs()");
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley result = isc_app_start();
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley check_result(result, "isc_app_start");
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley result = isc_net_probeipv4();
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley check_result(result, "isc_net_probeipv4");
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley result = isc_net_probeipv6();
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley if (result == ISC_R_SUCCESS)
711b0bed7b7f9e505c81e5810224f01df80a2ee4Bob Halley have_ipv6=ISC_TRUE;
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley result = isc_mem_create(0, 0, &mctx);
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley check_result(result, "isc_mem_create");
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley result = isc_taskmgr_create (mctx, 1, 0, &taskmgr);
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley check_result(result, "isc_taskmgr_create");
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley result = isc_task_create (taskmgr, 0, &task);
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley check_result(result, "isc_task_create");
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley result = isc_timermgr_create (mctx, &timermgr);
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley check_result(result, "isc_timermgr_create");
542189f21b3ea9b27b0fbc047d832a34dcaf75bcAndreas Gustafsson
fa756a197b059b28c44f24e332bd072178861dc6Mark Andrews result = isc_socketmgr_create (mctx, &socketmgr);
542189f21b3ea9b27b0fbc047d832a34dcaf75bcAndreas Gustafsson check_result(result, "isc_socketmgr_create");
34b394b43e2207e8f8f3703f0402422121455638David Lawrence isc_buffer_init(&b, ".", 1);
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley isc_buffer_add(&b, 1);
34b394b43e2207e8f8f3703f0402422121455638David Lawrence}
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley
34b394b43e2207e8f8f3703f0402422121455638David Lawrencestatic void
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrewsadd_opt (dns_message_t *msg, isc_uint16_t udpsize) {
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley dns_rdataset_t *rdataset = NULL;
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley dns_rdatalist_t *rdatalist = NULL;
34b394b43e2207e8f8f3703f0402422121455638David Lawrence dns_rdata_t *rdata = NULL;
34b394b43e2207e8f8f3703f0402422121455638David Lawrence isc_result_t result;
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley result = dns_message_gettemprdataset(msg, &rdataset);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews check_result (result, "dns_message_gettemprdataset");
34b394b43e2207e8f8f3703f0402422121455638David Lawrence dns_rdataset_init (rdataset);
34b394b43e2207e8f8f3703f0402422121455638David Lawrence result = dns_message_gettemprdatalist(msg, &rdatalist);
34b394b43e2207e8f8f3703f0402422121455638David Lawrence check_result (result, "dns_message_gettemprdatalist");
34b394b43e2207e8f8f3703f0402422121455638David Lawrence result = dns_message_gettemprdata(msg, &rdata);
34b394b43e2207e8f8f3703f0402422121455638David Lawrence check_result (result, "dns_message_gettemprdata");
34b394b43e2207e8f8f3703f0402422121455638David Lawrence
04b8111f2137a9cf9b0b71228f76b3e40ffa1173Brian Wellington rdatalist->type = dns_rdatatype_opt;
34b394b43e2207e8f8f3703f0402422121455638David Lawrence rdatalist->covers = 0;
c07c2a862e0f9d671e2961cd179c7b28d7b04f10Mark Andrews rdatalist->rdclass = udpsize;
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley rdatalist->ttl = 0;
147fd0bdf83272952521a7f343d88a421e2ff997Brian Wellington rdata->data = NULL;
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley rdata->length = 0;
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley ISC_LIST_INIT(rdatalist->rdata);
147fd0bdf83272952521a7f343d88a421e2ff997Brian Wellington ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
04b8111f2137a9cf9b0b71228f76b3e40ffa1173Brian Wellington dns_rdatalist_tordataset(rdatalist, rdataset);
95b604c5e972a5e9eb713bf45cf0b2d9b98da27eMark Andrews result = dns_message_setopt(msg, rdataset);
04b8111f2137a9cf9b0b71228f76b3e40ffa1173Brian Wellington check_result (result, "dns_message_setopt");
04b8111f2137a9cf9b0b71228f76b3e40ffa1173Brian Wellington}
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halleystatic void
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halleyadd_type(dns_message_t *message, dns_name_t *name, dns_rdataclass_t rdclass,
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley dns_rdatatype_t rdtype)
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley{
25e43e68b7431d5e4ff8b5427108cd7f5f9bcf3eBob Halley dns_rdataset_t *rdataset;
isc_result_t result;
debug ("add_type()");
rdataset = NULL;
result = dns_message_gettemprdataset(message, &rdataset);
check_result(result, "dns_message_gettemprdataset()");
dns_rdataset_init(rdataset);
dns_rdataset_makequestion(rdataset, rdclass, rdtype);
ISC_LIST_APPEND(name->list, rdataset, link);
}
static void
followup_lookup(dns_message_t *msg, dig_query_t *query,
dns_section_t section) {
dig_lookup_t *lookup = NULL;
dig_server_t *srv = NULL;
dns_rdataset_t *rdataset = NULL;
dns_rdata_t rdata;
dns_name_t *name = NULL;
isc_result_t result, loopresult;
isc_buffer_t *b = NULL;
isc_region_t r;
isc_boolean_t success = ISC_FALSE;
int len;
debug ("followup_lookup()");
result = dns_message_firstname (msg,section);
if (result != ISC_R_SUCCESS) {
debug ("Firstname returned %s",
isc_result_totext(result));
if ((section == DNS_SECTION_ANSWER) &&
query->lookup->trace)
followup_lookup (msg, query, DNS_SECTION_AUTHORITY);
return;
}
debug ("Following up %s", query->lookup->textname);
for (;;) {
name = NULL;
dns_message_currentname(msg, section, &name);
for (rdataset = ISC_LIST_HEAD(name->list);
rdataset != NULL;
rdataset = ISC_LIST_NEXT(rdataset, link)) {
loopresult = dns_rdataset_first(rdataset);
while (loopresult == ISC_R_SUCCESS) {
dns_rdataset_current(rdataset, &rdata);
debug ("Got rdata with type %d",
rdata.type);
if ((rdata.type == dns_rdatatype_ns) &&
(!query->lookup->trace_root ||
(query->lookup->nsfound < ROOTNS)))
{
query->lookup->nsfound++;
result = isc_buffer_allocate(mctx, &b,
BUFSIZE);
check_result (result,
"isc_buffer_allocate");
result = dns_rdata_totext (&rdata,
NULL,
b);
check_result (result,
"dns_rdata_totext");
isc_buffer_usedregion(b, &r);
len = r.length-1;
if (len >= MXNAME)
len = MXNAME-1;
/* Initialize lookup if we've not yet */
debug ("Found NS %d %.*s",
(int)r.length, (int)r.length,
(char *)r.base);
if (!success) {
success = ISC_TRUE;
lookup_counter++;
if (lookup_counter >
LOOKUP_LIMIT)
fatal ("Too many "
"lookups.");
lookup = isc_mem_allocate
(mctx,
sizeof(struct
dig_lookup));
if (lookup == NULL)
fatal ("Memory "
"allocation "
"failure in %s:"
"%d", __FILE__,
__LINE__);
lookup->pending = ISC_FALSE;
strncpy (lookup->textname,
query->lookup->
textname, MXNAME);
strncpy (lookup->rttext,
query->lookup->
rttext, 32);
strncpy (lookup->rctext,
query->lookup->
rctext, 32);
lookup->namespace[0]=0;
lookup->sendspace[0]=0;
lookup->sendmsg=NULL;
lookup->name=NULL;
lookup->oname=NULL;
lookup->timer = NULL;
lookup->xfr_q = NULL;
lookup->origin = NULL;
lookup->doing_xfr = ISC_FALSE;
lookup->defname = ISC_FALSE;
lookup->identify =
query->lookup->identify;
lookup->udpsize =
query->lookup->udpsize;
lookup->recurse =
query->lookup->
recurse;
lookup->aaonly =
query->lookup->
aaonly;
lookup->ns_search_only =
query->lookup->
ns_search_only;
lookup->use_my_server_list =
ISC_TRUE;
if (section ==
DNS_SECTION_ANSWER)
lookup->trace =
ISC_FALSE;
else
lookup->trace =
query->
lookup->trace;
lookup->trace_root = ISC_FALSE;
lookup->retries = tries;
lookup->nsfound = 0;
lookup->comments =
query->lookup->
comments;
lookup->stats =
query->lookup->
stats;
lookup->section_question =
query->lookup->
section_question;
lookup->section_answer =
query->lookup->
section_answer;
lookup->section_authority =
query->lookup->
section_authority;
lookup->section_additional =
query->lookup->
section_additional;
ISC_LIST_INIT(lookup->
my_server_list);
ISC_LIST_INIT(lookup->q);
debug ("Before insertion, "
"init@%lx "
"-> %lx, new@%lx "
"-> %lx",
(long int)query->lookup,
(long int)query->
lookup->link.next,
(long int)lookup,
(long int)lookup->
link.next);
ISC_LIST_INSERTAFTER(
lookup_list,
query->lookup,
lookup,
link);
debug ("After insertion, "
"init -> "
"%lx, new = %lx, "
"new -> %lx",
(long int)query->
lookup->link.next,
(long int)lookup,
(long int)lookup->
link.next);
}
srv = isc_mem_allocate (mctx,
sizeof(
struct
dig_server));
if (srv == NULL)
fatal("Memory allocation "
"failure in %s:%d",
__FILE__, __LINE__);
strncpy(srv->servername,
(char *)r.base, len);
srv->servername[len]=0;
ISC_LIST_APPEND
(lookup->my_server_list,
srv, link);
isc_buffer_free (&b);
}
loopresult = dns_rdataset_next(rdataset);
}
}
result = dns_message_nextname (msg, section);
if (result != ISC_R_SUCCESS)
break;
}
if ((lookup == NULL) && (section == DNS_SECTION_ANSWER) &&
query->lookup->trace)
followup_lookup(msg, query, DNS_SECTION_AUTHORITY);
}
static void
next_origin(dns_message_t *msg, dig_query_t *query) {
dig_lookup_t *lookup;
dig_server_t *srv;
dig_server_t *s;
UNUSED (msg);
debug ("next_origin()");
debug ("Following up %s", query->lookup->textname);
if (query->lookup->origin == NULL) { /*Then we just did rootorg;
there's nothing left. */
debug ("Made it to the root whith nowhere to go.");
return;
}
lookup_counter++;
if (lookup_counter > LOOKUP_LIMIT)
fatal ("Too many lookups.");
lookup = isc_mem_allocate
(mctx, sizeof(struct dig_lookup));
if (lookup == NULL)
fatal ("Memory allocation failure in %s:%d",
__FILE__, __LINE__);
lookup->pending = ISC_FALSE;
strncpy (lookup->textname, query->lookup-> textname, MXNAME);
strncpy (lookup->rttext, query->lookup-> rttext, 32);
strncpy (lookup->rctext, query->lookup-> rctext, 32);
lookup->namespace[0]=0;
lookup->sendspace[0]=0;
lookup->sendmsg=NULL;
lookup->name=NULL;
lookup->oname=NULL;
lookup->timer = NULL;
lookup->xfr_q = NULL;
lookup->doing_xfr = ISC_FALSE;
lookup->defname = ISC_FALSE;
lookup->trace = query->lookup->trace;
lookup->trace_root = query->lookup->trace_root;
lookup->identify = query->lookup->identify;
lookup->udpsize = query->lookup->udpsize;
lookup->recurse = query->lookup->recurse;
lookup->aaonly = query->lookup->aaonly;
lookup->ns_search_only = query->lookup->ns_search_only;
lookup->use_my_server_list = query->lookup->use_my_server_list;
lookup->origin = ISC_LIST_NEXT(query->lookup->origin, link);
lookup->retries = tries;
lookup->nsfound = 0;
lookup->comments = query->lookup->comments;
lookup->stats = query->lookup->stats;
lookup->section_question = query->lookup->section_question;
lookup->section_answer = query->lookup->section_answer;
lookup->section_authority = query->lookup->section_authority;
lookup->section_additional = query->lookup->section_additional;
ISC_LIST_INIT(lookup->my_server_list);
ISC_LIST_INIT(lookup->q);
if (lookup->use_my_server_list) {
s = ISC_LIST_HEAD(query->lookup->my_server_list);
while (s != NULL) {
srv = isc_mem_allocate (mctx, sizeof(struct
dig_server));
if (srv == NULL)
fatal("Memory allocation failure in %s:%d", __FILE__, __LINE__);
strncpy(srv->servername, s->servername, MXNAME);
ISC_LIST_ENQUEUE(lookup->my_server_list, srv,
link);
s = ISC_LIST_NEXT(s, link);
}
}
debug ("Before insertion, init@%ld "
"-> %ld, new@%ld "
"-> %ld", (long int)query->lookup,
(long int)query->lookup->link.next,
(long int)lookup, (long int)lookup->
link.next);
ISC_LIST_INSERTAFTER(lookup_list, query->
lookup, lookup,
link);
debug ("After insertion, init -> "
"%ld, new = %ld, "
"new -> %ld", (long int)query->
lookup->link.next,
(long int)lookup, (long int)lookup->
link.next);
}
void
setup_lookup(dig_lookup_t *lookup) {
isc_result_t result, res2;
int len;
dns_rdatatype_t rdtype;
dns_rdataclass_t rdclass;
dig_server_t *serv;
dig_query_t *query;
isc_region_t r;
isc_textregion_t tr;
isc_buffer_t b;
char store[MXNAME];
debug("setup_lookup()");
debug("Setting up for looking up %s @%lx->%lx",
lookup->textname, (long int)lookup,
(long int)lookup->link.next);
result = dns_message_create(mctx, DNS_MESSAGE_INTENTRENDER,
&lookup->sendmsg);
check_result(result, "dns_message_create");
result = dns_message_gettempname(lookup->sendmsg, &lookup->name);
check_result(result, "dns_message_gettempname");
dns_name_init(lookup->name, NULL);
isc_buffer_init(&lookup->namebuf, lookup->namespace, BUFSIZE);
isc_buffer_init(&lookup->onamebuf, lookup->onamespace, BUFSIZE);
if ((count_dots(lookup->textname) >= ndots) || lookup->defname)
lookup->origin = NULL; /* Force root lookup */
debug ("lookup->origin = %lx", (long int)lookup->origin);
if (lookup->origin != NULL) {
debug ("Trying origin %s", lookup->origin->origin);
result = dns_message_gettempname(lookup->sendmsg,
&lookup->oname);
check_result(result, "dns_message_gettempname");
dns_name_init(lookup->oname, NULL);
len=strlen(lookup->origin->origin);
isc_buffer_init(&b, lookup->origin->origin, len);
isc_buffer_add(&b, len);
result = dns_name_fromtext(lookup->oname, &b, dns_rootname,
ISC_FALSE, &lookup->onamebuf);
if (result != ISC_R_SUCCESS) {
dns_message_puttempname(lookup->sendmsg,
&lookup->name);
dns_message_puttempname(lookup->sendmsg,
&lookup->oname);
fatal("Aborting: %s is not a legal name syntax. (%s)",
lookup->origin->origin,
dns_result_totext(result));
}
if (!lookup->trace_root) {
len=strlen(lookup->textname);
isc_buffer_init(&b, lookup->textname, len);
isc_buffer_add(&b, len);
result = dns_name_fromtext(lookup->name, &b,
lookup->oname, ISC_FALSE,
&lookup->namebuf);
} else {
isc_buffer_init(&b, ". ", 1);
isc_buffer_add(&b, 1);
result = dns_name_fromtext(lookup->name, &b,
lookup->oname, ISC_FALSE,
&lookup->namebuf);
}
if (result != ISC_R_SUCCESS) {
dns_message_puttempname(lookup->sendmsg,
&lookup->name);
dns_message_puttempname(lookup->sendmsg,
&lookup->oname);
fatal("Aborting: %s is not a legal name syntax. (%s)",
lookup->textname, dns_result_totext(result));
}
dns_message_puttempname(lookup->sendmsg, &lookup->oname);
} else {
debug ("Using root origin.");
if (!lookup->trace_root) {
len = strlen (lookup->textname);
isc_buffer_init(&b, lookup->textname, len);
isc_buffer_add(&b, len);
result = dns_name_fromtext(lookup->name, &b,
dns_rootname,
ISC_FALSE,
&lookup->namebuf);
} else {
isc_buffer_init(&b, ". ", 1);
isc_buffer_add(&b, 1);
result = dns_name_fromtext(lookup->name, &b,
dns_rootname,
ISC_FALSE,
&lookup->namebuf);
}
if (result != ISC_R_SUCCESS) {
dns_message_puttempname(lookup->sendmsg,
&lookup->name);
isc_buffer_init(&b, store, MXNAME);
res2 = dns_name_totext(dns_rootname, ISC_FALSE, &b);
check_result (res2, "dns_name_totext");
isc_buffer_usedregion (&b, &r);
fatal("Aborting: %s/%.*s is not a legal name syntax. "
"(%s)", lookup->textname, (int)r.length,
(char *)r.base, dns_result_totext(result));
}
}
isc_buffer_init (&b, store, MXNAME);
dns_name_totext(lookup->name, ISC_FALSE, &b);
isc_buffer_usedregion (&b, &r);
trying((int)r.length, (char *)r.base, lookup);
#ifdef DEBUG
if (dns_name_isabsolute(lookup->name))
debug ("This is an absolute name.");
else
debug ("This is a relative name (which is wrong).");
#endif
if (lookup->rctext[0] == 0)
strcpy(lookup->rctext, "IN");
if (lookup->rttext[0] == 0)
strcpy(lookup->rttext, "A");
lookup->sendmsg->id = random();
lookup->sendmsg->opcode = dns_opcode_query;
/* If this is a trace request, completely disallow recursion, since
* it's meaningless for traces */
if (lookup->recurse && !lookup->trace) {
debug ("Recursive query");
lookup->sendmsg->flags |= DNS_MESSAGEFLAG_RD;
}
if (lookup->aaonly) {
debug ("AA query");
lookup->sendmsg->flags |= DNS_MESSAGEFLAG_AA;
}
dns_message_addname(lookup->sendmsg, lookup->name,
DNS_SECTION_QUESTION);
if (lookup->trace_root) {
tr.base="SOA";
tr.length=3;
} else {
tr.base=lookup->rttext;
tr.length=strlen(lookup->rttext);
}
result = dns_rdatatype_fromtext(&rdtype, &tr);
check_result(result, "dns_rdatatype_fromtext");
if (rdtype == dns_rdatatype_axfr) {
lookup->doing_xfr = ISC_TRUE;
/*
* Force TCP mode if we're doing an xfr.
*/
tcp_mode = ISC_TRUE;
}
if (lookup->trace_root) {
tr.base="IN";
tr.length=2;
} else {
tr.base=lookup->rctext;
tr.length=strlen(lookup->rctext);
}
result = dns_rdataclass_fromtext(&rdclass, &tr);
check_result(result, "dns_rdataclass_fromtext");
add_type(lookup->sendmsg, lookup->name, rdclass, rdtype);
isc_buffer_init(&lookup->sendbuf, lookup->sendspace, COMMSIZE);
debug ("Starting to render the message");
result = dns_message_renderbegin(lookup->sendmsg, &lookup->sendbuf);
check_result(result, "dns_message_renderbegin");
if (lookup->udpsize > 0) {
add_opt(lookup->sendmsg, lookup->udpsize);
}
result = dns_message_rendersection(lookup->sendmsg,
DNS_SECTION_QUESTION, 0);
check_result(result, "dns_message_rendersection");
result = dns_message_renderend(lookup->sendmsg);
check_result(result, "dns_message_renderend");
debug ("Done rendering.");
lookup->pending = ISC_FALSE;
if (lookup->use_my_server_list)
serv = ISC_LIST_HEAD(lookup->my_server_list);
else
serv = ISC_LIST_HEAD(server_list);
for (; serv != NULL;
serv = ISC_LIST_NEXT(serv, link)) {
query = isc_mem_allocate(mctx, sizeof(dig_query_t));
if (query == NULL)
fatal("Memory allocation failure in %s:%d", __FILE__, __LINE__);
query->lookup = lookup;
query->working = ISC_FALSE;
query->waiting_connect = ISC_FALSE;
query->first_pass = ISC_TRUE;
query->first_soa_rcvd = ISC_FALSE;
query->servname = serv->servername;
ISC_LIST_INIT(query->sendlist);
ISC_LIST_INIT(query->recvlist);
ISC_LIST_INIT(query->lengthlist);
query->sock = NULL;
isc_buffer_init(&query->recvbuf, query->recvspace, COMMSIZE);
isc_buffer_init(&query->lengthbuf, query->lengthspace, 2);
isc_buffer_init(&query->slbuf, query->slspace, 2);
ISC_LIST_ENQUEUE(lookup->q, query, link);
}
if (!ISC_LIST_EMPTY(lookup->q) && qr) {
printmessage (ISC_LIST_HEAD(lookup->q), lookup->sendmsg,
ISC_TRUE);
}
}
static void
send_done(isc_task_t *task, isc_event_t *event) {
UNUSED(task);
isc_event_free(&event);
debug("send_done()");
}
static void
cancel_lookup(dig_lookup_t *lookup) {
dig_query_t *query=NULL;
debug("cancel_lookup()");
if (!lookup->pending)
return;
lookup->pending = ISC_FALSE;
lookup->retries = 0;
for (query = ISC_LIST_HEAD(lookup->q);
query != NULL;
query = ISC_LIST_NEXT(query, link)) {
if (query->working) {
isc_socket_cancel(query->sock, task,
ISC_SOCKCANCEL_ALL);
}
}
}
static void
recv_done(isc_task_t *task, isc_event_t *event);
static void
connect_timeout(isc_task_t *task, isc_event_t *event);
void
send_udp(dig_lookup_t *lookup) {
dig_query_t *query;
isc_result_t result;
debug ("send_udp()");
isc_interval_set(&lookup->interval, timeout, 0);
result = isc_timer_create(timermgr, isc_timertype_once, NULL,
&lookup->interval, 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)) {
ISC_LIST_ENQUEUE(query->recvlist, &query->recvbuf, link);
query->working = ISC_TRUE;
result = isc_socket_recvv(query->sock, &query->recvlist, 1,
task, recv_done, query);
check_result(result, "isc_socket_recvv");
sendcount++;
debug("Sent count number %d", sendcount);
#ifdef TWIDDLE
if (twiddle) {
twiddlebuf(lookup->sendbuf);
}
#endif
ISC_LIST_ENQUEUE(query->sendlist, &lookup->sendbuf, link);
debug("Sending a request.");
result = isc_time_now(&query->time_sent);
check_result(result, "isc_time_now");
result = isc_socket_sendtov(query->sock, &query->sendlist,
task, send_done, query,
&query->sockaddr, NULL);
check_result(result, "isc_socket_sendtov");
}
}
/* connect_timeout is used for both UDP recieves and TCP connects. */
static void
connect_timeout(isc_task_t *task, isc_event_t *event) {
dig_lookup_t *lookup=NULL;
dig_query_t *q=NULL;
isc_result_t result;
isc_buffer_t *b=NULL;
isc_region_t r;
REQUIRE(event->ev_type == ISC_TIMEREVENT_IDLE);
debug("connect_timeout()");
lookup = event->ev_arg;
debug ("Buffer Allocate connect_timeout");
result = isc_buffer_allocate(mctx, &b, 256);
check_result(result, "isc_buffer_allocate");
for (q = ISC_LIST_HEAD(lookup->q);
q != NULL;
q = ISC_LIST_NEXT(q, link)) {
if (q->working) {
if (!free_now) {
isc_buffer_clear(b);
result = isc_sockaddr_totext(&q->sockaddr, b);
check_result(result, "isc_sockaddr_totext");
isc_buffer_usedregion(b, &r);
if (q->lookup->retries > 1)
printf(";; Connection to server %.*s "
"for %s timed out. "
"Retrying %d.\n",
(int)r.length, r.base,
q->lookup->textname,
q->lookup->retries-1);
else {
printf(";; Connection to server %.*s "
"for %s timed out. "
"Giving up.\n",
(int)r.length, r.base,
q->lookup->textname);
}
}
isc_socket_cancel(q->sock, task,
ISC_SOCKCANCEL_ALL);
}
}
ENSURE(lookup->timer != NULL);
isc_timer_detach(&lookup->timer);
isc_buffer_free(&b);
isc_event_free(&event);
debug ("Done with connect_timeout()");
}
static void
tcp_length_done(isc_task_t *task, isc_event_t *event) {
isc_socketevent_t *sevent;
isc_buffer_t *b=NULL;
isc_region_t r;
isc_result_t result;
dig_query_t *query=NULL;
isc_uint16_t length;
REQUIRE(event->ev_type == ISC_SOCKEVENT_RECVDONE);
UNUSED(task);
debug("tcp_length_done()");
sevent = (isc_socketevent_t *)event;
query = event->ev_arg;
if (sevent->result == ISC_R_CANCELED) {
query->working = ISC_FALSE;
check_next_lookup(query->lookup);
isc_event_free(&event);
return;
}
if (sevent->result != ISC_R_SUCCESS) {
debug ("Buffer Allocate connect_timeout");
result = isc_buffer_allocate(mctx, &b, 256);
check_result(result, "isc_buffer_allocate");
result = isc_sockaddr_totext(&query->sockaddr, b);
check_result(result, "isc_sockaddr_totext");
isc_buffer_usedregion(b, &r);
printf("%.*s: %s\n", (int)r.length, r.base,
isc_result_totext(sevent->result));
isc_buffer_free(&b);
query->working = ISC_FALSE;
isc_socket_detach(&query->sock);
check_next_lookup(query->lookup);
isc_event_free(&event);
return;
}
b = ISC_LIST_HEAD(sevent->bufferlist);
ISC_LIST_DEQUEUE(sevent->bufferlist, &query->lengthbuf, link);
length = isc_buffer_getuint16(b);
if (length > COMMSIZE) {
isc_event_free (&event);
fatal ("Length of %X was longer than I can handle!",
length);
}
/*
* Even though the buffer was already init'ed, we need
* to redo it now, to force the length we want.
*/
isc_buffer_invalidate(&query->recvbuf);
isc_buffer_init(&query->recvbuf, query->recvspace, length);
ENSURE(ISC_LIST_EMPTY(query->recvlist));
ISC_LIST_ENQUEUE(query->recvlist, &query->recvbuf, link);
result = isc_socket_recvv(query->sock, &query->recvlist, length, task,
recv_done, query);
check_result(result, "isc_socket_recvv");
debug("Resubmitted recv request with length %d", length);
isc_event_free(&event);
}
static void
launch_next_query(dig_query_t *query, isc_boolean_t include_question) {
isc_result_t result;
debug("launch_next_query()");
if (!query->lookup->pending) {
debug("Ignoring launch_next_query because !pending.");
isc_socket_detach(&query->sock);
query->working = ISC_FALSE;
query->waiting_connect = ISC_FALSE;
check_next_lookup(query->lookup);
return;
}
isc_buffer_clear(&query->slbuf);
isc_buffer_clear(&query->lengthbuf);
isc_buffer_putuint16(&query->slbuf, query->lookup->sendbuf.used);
ISC_LIST_ENQUEUE(query->sendlist, &query->slbuf, link);
if (include_question) {
#ifdef TWIDDLE
if (twiddle) {
twiddlebuf(query->lookup->sendbuf);
}
#endif
ISC_LIST_ENQUEUE(query->sendlist, &query->lookup->sendbuf,
link);
}
ISC_LIST_ENQUEUE(query->lengthlist, &query->lengthbuf, link);
result = isc_socket_recvv(query->sock, &query->lengthlist, 0, 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, 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);
sevent = (isc_socketevent_t *)event;
query = sevent->ev_arg;
REQUIRE(query->waiting_connect);
query->waiting_connect = ISC_FALSE;
debug("connect_done()");
if (sevent->result != ISC_R_SUCCESS) {
debug ("Buffer Allocate connect_timeout");
result = isc_buffer_allocate(mctx, &b, 256);
check_result(result, "isc_buffer_allocate");
result = isc_sockaddr_totext(&query->sockaddr, b);
check_result(result, "isc_sockaddr_totext");
isc_buffer_usedregion(b, &r);
printf(";; Connection to server %.*s for %s failed: %s.\n",
(int)r.length, r.base, query->lookup->textname,
isc_result_totext(sevent->result));
if (exitcode < 9)
exitcode = 9;
isc_buffer_free(&b);
query->working = ISC_FALSE;
query->waiting_connect = ISC_FALSE;
check_next_lookup(query->lookup);
isc_event_free(&event);
return;
}
isc_event_free(&event);
launch_next_query(query, ISC_TRUE);
}
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);
}
}
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;
UNUSED (task);
debug("recv_done()");
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;
query = event->ev_arg;
if (!query->lookup->pending && !query->lookup->ns_search_only) {
debug("No longer pending. Got %s",
isc_result_totext(sevent->result));
query->working = ISC_FALSE;
query->waiting_connect = ISC_FALSE;
cancel_lookup(query->lookup);
check_next_lookup(query->lookup);
isc_event_free(&event);
return;
}
if (sevent->result == ISC_R_SUCCESS) {
b = ISC_LIST_HEAD(sevent->bufferlist);
ISC_LIST_DEQUEUE(sevent->bufferlist, &query->recvbuf, link);
result = dns_message_create(mctx, DNS_MESSAGE_INTENTPARSE,
&msg);
check_result(result, "dns_message_create");
result = dns_message_parse(msg, b, ISC_TRUE);
if (result != ISC_R_SUCCESS)
hex_dump(b);
check_result(result, "dns_message_parse");
if (query->lookup->xfr_q == NULL)
query->lookup->xfr_q = query;
if (query->lookup->xfr_q == query) {
if (query->lookup->trace) {
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);
}
#ifdef DEBUG
if (query->lookup->pending)
debug("Still pending.");
#endif
if (query->lookup->doing_xfr) {
if (!query->first_soa_rcvd) {
debug("Not yet got first SOA");
if (!msg_contains_soa(msg, query)) {
puts("; Transfer failed. "
"Didn't start with SOA answer.");
query->working = ISC_FALSE;
cancel_lookup(query->lookup);
check_next_lookup (query->lookup);
isc_event_free (&event);
dns_message_destroy (&msg);
return;
}
else {
query->first_soa_rcvd = ISC_TRUE;
launch_next_query(query, ISC_FALSE);
}
}
else {
if (msg_contains_soa(msg, query)) {
isc_buffer_init(&ab, abspace, MXNAME);
result = isc_sockaddr_totext(&sevent->
address,
&ab);
check_result(result,
"isc_sockaddr_totext");
isc_buffer_usedregion(&ab, &r);
received(b->used, r.length,
(char *)r.base, query);
cancel_lookup(query->lookup);
query->working = ISC_FALSE;
check_next_lookup(query->lookup);
isc_event_free(&event);
dns_message_destroy (&msg);
return;
}
else {
launch_next_query(query, ISC_FALSE);
}
}
}
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);
received(b->used, r.length, (char *)r.base,
query);
}
query->working = ISC_FALSE;
query->lookup->pending = ISC_FALSE;
if (!query->lookup->ns_search_only ||
query->lookup->trace_root ) {
cancel_lookup(query->lookup);
}
check_next_lookup(query->lookup);
}
dns_message_destroy(&msg);
isc_event_free(&event);
return;
}
/* In truth, we should never get into the CANCELED routine, since
the cancel_lookup() routine clears the pending flag. */
if (sevent->result == ISC_R_CANCELED) {
debug ("In cancel handler");
query->working = ISC_FALSE;
query->waiting_connect = ISC_FALSE;
check_next_lookup(query->lookup);
isc_event_free(&event);
return;
}
isc_event_free(&event);
fatal("recv_done got result %s",
isc_result_totext(sevent->result));
}
static void
get_address(char *host, in_port_t port, isc_sockaddr_t *sockaddr) {
struct in_addr in4;
struct in6_addr in6;
struct hostent *he;
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 {
he = gethostbyname(host);
if (he == NULL)
fatal("Couldn't look up your server host %s. errno=%d",
host, h_errno);
INSIST(he->h_addrtype == AF_INET);
isc_sockaddr_fromin(sockaddr,
(struct in_addr *)(he->h_addr_list[0]),
port);
}
}
void
do_lookup_tcp(dig_lookup_t *lookup) {
dig_query_t *query;
isc_result_t result;
debug("do_lookup_tcp()");
lookup->pending = ISC_TRUE;
isc_interval_set(&lookup->interval, timeout, 0);
result = isc_timer_create(timermgr, isc_timertype_once, NULL,
&lookup->interval, 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);
result = isc_socket_create(socketmgr,
isc_sockaddr_pf(&query->sockaddr),
isc_sockettype_tcp, &query->sock) ;
check_result(result, "isc_socket_create");
result = isc_socket_connect(query->sock, &query->sockaddr,
task, connect_done, query);
check_result (result, "isc_socket_connect");
}
}
void
do_lookup_udp(dig_lookup_t *lookup) {
dig_query_t *query;
isc_result_t result;
#ifdef DEBUG
debug("do_lookup_udp()");
if (tcp_mode)
debug("I'm starting UDP with tcp_mode set!!!");
#endif
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);
result = isc_socket_create(socketmgr,
isc_sockaddr_pf(&query->sockaddr),
isc_sockettype_udp, &query->sock) ;
check_result(result, "isc_socket_create");
}
send_udp(lookup);
}
static 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) {
q = ISC_LIST_HEAD(l->q);
while (q != NULL) {
if (q->sock != NULL) {
isc_socket_cancel(q->sock, NULL,
ISC_SOCKCANCEL_ALL);
isc_socket_detach(&q->sock);
}
if (ISC_LINK_LINKED(&q->recvbuf, link))
ISC_LIST_DEQUEUE(q->recvlist, &q->recvbuf,
link);
if (ISC_LINK_LINKED(&q->lengthbuf, link))
ISC_LIST_DEQUEUE(q->lengthlist, &q->lengthbuf,
link);
isc_buffer_invalidate(&q->recvbuf);
isc_buffer_invalidate(&q->lengthbuf);
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) {
ptr = s;
s = ISC_LIST_NEXT(s, link);
isc_mem_free(mctx, ptr);
}
}
if (l->sendmsg != NULL)
dns_message_destroy (&l->sendmsg);
if (l->timer != NULL)
isc_timer_detach (&l->timer);
ptr = l;
l = ISC_LIST_NEXT(l, link);
isc_mem_free(mctx, ptr);
}
s = ISC_LIST_HEAD(server_list);
while (s != NULL) {
ptr = s;
s = ISC_LIST_NEXT(s, link);
isc_mem_free(mctx, ptr);
}
o = ISC_LIST_HEAD(search_list);
while (o != NULL) {
ptr = o;
o = ISC_LIST_NEXT(o, link);
isc_mem_free(mctx, ptr);
}
if (socketmgr != NULL)
isc_socketmgr_destroy(&socketmgr);
if (timermgr != NULL)
isc_timermgr_destroy(&timermgr);
if (task != NULL)
isc_task_detach(&task);
if (taskmgr != NULL)
isc_taskmgr_destroy(&taskmgr);
#ifdef MEMDEBUG
isc_mem_stats(mctx, stderr);
#endif
isc_app_finish();
if (mctx != NULL)
isc_mem_destroy(&mctx);
exit(exitcode);
}
int
main(int argc, char **argv) {
dig_lookup_t *lookup = NULL;
#ifdef TWIDDLE
FILE *fp;
int i, p;
#endif
ISC_LIST_INIT(lookup_list);
ISC_LIST_INIT(server_list);
ISC_LIST_INIT(search_list);
#ifdef TWIDDLE
fp = fopen("/dev/urandom", "r");
if (fp!=NULL) {
fread (&i, sizeof(int), 1, fp);
srandom(i);
}
else {
srandom ((int)&main);
}
p = getpid()%16+8;
for (i=0 ; i<p; i++);
#endif
setup_libs();
parse_args(ISC_FALSE, argc, argv);
setup_system();
lookup = ISC_LIST_HEAD(lookup_list);
setup_lookup(lookup);
if (tcp_mode)
do_lookup_tcp(lookup);
else
do_lookup_udp(lookup);
isc_app_run();
free_lists();
/*
* Should never get here.
*/
return (2);
}