dighost.c revision 099805b4c1185233d37fdbd63ff3af761eb0644c
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Copyright (C) 2000, 2001 Internet Software Consortium.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Permission to use, copy, modify, and distribute this software for any
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * purpose with or without fee is hereby granted, provided that the above
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * copyright notice and this permission notice appear in all copies.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
0c310d16b05ee94743d33f6920907edee6084fc8Michael Graff/* $Id: dighost.c,v 1.198 2001/03/28 03:09:47 bwelling Exp $ */
0c310d16b05ee94743d33f6920907edee6084fc8Michael Graff * Notice to programmers: Do not use this code as an example of how to
0c310d16b05ee94743d33f6920907edee6084fc8Michael Graff * use the ISC library to perform DNS lookups. Dig and Host both operate
3761c433912beabe43abeed2c3513b6201c59f64Mark Andrews * on the request level, since they allow fine-tuning of output and are
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * intended as debugging tools. As a result, they perform many of the
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * functions which could be better handled using the dns_resolver
0c310d16b05ee94743d33f6920907edee6084fc8Michael Graff * functions in most applications.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#endif /* DNS_OPT_NEWCODES */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsunsigned int timeout = 0;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Exit Codes:
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * 0 Everything went well, including things like NXDOMAIN
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * 1 Usage error
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * 7 Got too many RR's or Names
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * 8 Couldn't open batch file
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews * 9 No reply from server
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews * 10 Internal error
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * Apply and clear locks at the event level in global task.
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * Can I get rid of these using shutdown events? XXX
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews debug("lock_lookup %s:%d", __FILE__, __LINE__);\
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews check_result(isc_mutex_lock((&lookup_lock)), "isc_mutex_lock");\
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews debug("unlock_lookup %s:%d", __FILE__, __LINE__);\
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews check_result(isc_mutex_unlock((&lookup_lock)),\
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews "isc_mutex_unlock");\
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsrecv_done(isc_task_t *task, isc_event_t *event);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsconnect_timeout(isc_task_t *task, isc_event_t *event);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews while (*s != '\0') {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (*s == '.')
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews unsigned int len;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsget_reverse(char *reverse, char *value, isc_boolean_t nibble) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (strspn(value, "0123456789.") == strlen(value)) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (n == 0) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews for (i = n - 1; i >= 0; i--) {
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews } else if (strspn(value, "0123456789abcdefABCDEF:")
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews n = inet_pton(AF_INET6, value, &addr.type.in6);
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews result = dns_byaddr_createptrname(&addr, nibble,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewscheck_result(isc_result_t result, const char *msg) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews fatal("%s: %s", msg, isc_result_totext(result));
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews * Create a server structure, which is part of the lookup structure.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * This is little more than a linked list of servers to query in hopes
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * of finding the answer the user is looking for
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews srv = isc_mem_allocate(mctx, sizeof(struct dig_server));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Produce a cloned server list. The dest list must have already had
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * ISC_LIST_INIT applied.
a98551ef592e9be6008e0141ceeb32efd586c5efMark Andrews * Create an empty lookup structure, which holds all the information needed
a98551ef592e9be6008e0141ceeb32efd586c5efMark Andrews * to get an answer to a user's question. This structure contains two
a98551ef592e9be6008e0141ceeb32efd586c5efMark Andrews * linked lists: the server list (servers to query) and the query list
bed8e84810a80dad3d37870be927d1dfd015f480Mark Andrews * (outstanding queries which have been made to the listed servers).
a98551ef592e9be6008e0141ceeb32efd586c5efMark Andrews looknew = isc_mem_allocate(mctx, sizeof(struct dig_lookup));
bed8e84810a80dad3d37870be927d1dfd015f480Mark Andrews#endif /* DNS_OPT_NEWCODES_LIVE */
a98551ef592e9be6008e0141ceeb32efd586c5efMark Andrews * Clone a lookup, perhaps copying the server list. This does not clone
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * the query list, since it will be regenerated by the setup_lookup()
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * function, nor does it queue up the new lookup for processing.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Caution: If you don't clone the servers, you MUST clone the server
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * list seperately from somewhere else, or construct it by hand.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsclone_lookup(dig_lookup_t *lookold, isc_boolean_t servers) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews strncpy(looknew->textname, lookold->textname, MXNAME);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews strncpy(looknew->cmdline, lookold->cmdline, MXNAME);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews looknew->identify_previous_line = lookold->identify_previous_line;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews looknew->servfail_stops = lookold->servfail_stops;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews looknew->ns_search_only = lookold->ns_search_only;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews looknew->ns_search_only_leafnode = lookold->ns_search_only_leafnode;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews looknew->section_question = lookold->section_question;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews looknew->section_answer = lookold->section_answer;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews looknew->section_authority = lookold->section_authority;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews looknew->section_additional = lookold->section_additional;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews strncpy(looknew->viewname, lookold->viewname, MXNAME);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews strncpy(looknew->zonename, lookold->zonename, MXNAME);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#endif /* DNS_OPT_NEWCODES_LIVE */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Requeue a lookup for further processing, perhaps copying the server
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * list. The new lookup structure is returned to the caller, and is
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * queued for processing. If servers are not cloned in the requeue, they
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * must be added before allowing the current event to complete, since the
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews * completion of the event may result in the next entry on the lookup
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * queue getting run.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsrequeue_lookup(dig_lookup_t *lookold, isc_boolean_t servers) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews "-> %p, new@%p -> %p",
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews lookold, lookold->link.next, looknew, looknew->link.next);
bfb2a81b65579882a80855c279cedc45aebd62e8Mark Andrews "%p, new = %p, new -> %p",
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews unsigned char *secretstore;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews result = isc_buffer_allocate(mctx, &namebuf, MXNAME);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews secretstore = isc_mem_allocate(mctx, secretsize);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_buffer_init(&secretbuf, secretstore, secretsize);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews result = isc_base64_decodestring(keysecret, &secretbuf);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews secretsize = isc_buffer_usedlength(&secretbuf);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews result = dns_tsigkey_create(&keyname, dns_tsig_hmacmd5_name,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews result = dst_key_fromnamedfile(keyfile, DST_TYPE_PRIVATE,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews fprintf(stderr, "Couldn't read key from %s: %s\n",
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Get key size in bits, convert to bytes, rounding up (?)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews secretstore = isc_mem_allocate(mctx, secretlen);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_buffer_init(&secretbuf, secretstore, secretlen);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews fprintf(stderr, "Couldn't read key from %s: %s\n",
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_name_clone(dst_key_name(dstkey), &keyname);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews result = dns_tsigkey_create(&keyname, dns_tsig_hmacmd5_name,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews search = isc_mem_allocate(mctx, sizeof(*search));
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * Setup the system as a whole, reading key information and resolv.conf
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews get_servers = ISC_TF(server_list.head == NULL);
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews /* XXX Use lwres resolv.conf reader */
ndots);
!= NULL) {
!= NULL) {
if (keyfile[0] != 0)
else if (keysecret[0] != 0)
clear_searchlist(void) {
* Override the search list derived from resolv.conf by 'domain'.
setup_libs(void) {
#ifdef DNS_OPT_NEWCODES_LIVE
#ifdef DNS_OPT_NEWCODES_LIVE
unsigned int i, optsize = 0;
if (dnssec)
#ifdef DNS_OPT_NEWCODES_LIVE
* part of dig.c, host.c, or nslookup.c) to either shutdown the system as
check_if_done(void) {
sendcount == 0) {
link);
link);
sockcount--;
static isc_boolean_t
dig_server_t *s;
dig_query_t *q;
void *ptr;
if (debugging) {
while (q != NULL) {
q->servname);
return (ISC_FALSE);
while (s != NULL) {
s, lookup);
ptr = s;
return (ISC_TRUE);
start_lookup(void) {
if (cancel_now)
start_lookup();
isc_region_t r;
int len;
int numLookups = 0;
numLookups +=
return numLookups;
BUFSIZE);
NULL,
isc_buffer_usedregion(b, &r);
(char *)r.base);
numLookups++;
if (!success) {
if (section ==
query->
query->
query->
isc_buffer_free(&b);
numLookups +=
return numLookups;
static isc_boolean_t
if (!usesearch)
return (ISC_FALSE);
return (ISC_FALSE);
return (ISC_TRUE);
int len;
isc_region_t r;
isc_buffer_t b;
lookup_counter = 0;
* is TRUE or we got a domain line in the resolv.conf file.
isc_buffer_usedregion(&b, &r);
#ifndef DNS_OPT_NEWCODES_LIVE
#ifdef DNS_OPT_NEWCODES_LIVE
NULL);
MXNAME);
wirebuf);
optlist);
DNS_SECTION_QUESTION, 0);
ISC_TRUE);
sendcount--;
dig_lookup_t *l;
unsigned int local_timeout;
if (timeout == 0) {
NULL,
&l->interval,
l, &l->timer);
dig_lookup_t *l;
if (specified_source &&
sockcount++;
if (specified_source)
if (l->ns_search_only_leafnode) {
sockcount++;
if (specified_source) {
link);
query);
recvcount++;
link);
sendcount++;
if (l->ns_search_only_leafnode) {
if (!l->tcp_mode)
if (!l->tcp_mode) {
l->retries--;
l->retries);
cancel_lookup(l);
l->retries--;
cancel_lookup(l);
isc_region_t r;
dig_lookup_t *l;
recvcount--;
isc_buffer_usedregion(b, &r);
isc_buffer_free(&b);
sockcount--;
length);
recvcount++;
dig_lookup_t *l;
sockcount--;
if (include_question) {
link);
recvcount++;
sendcount++;
dig_lookup_t *l;
isc_region_t r;
sockcount--;
isc_buffer_usedregion(b, &r);
sockcount--;
isc_buffer_free(&b);
static isc_boolean_t
isc_buffer_t b;
return (ISC_TRUE);
&name);
return (ISC_TRUE);
goto next_rdata;
&soa,
mctx);
goto doexit;
goto next_rdata;
goto doexit;
goto doexit;
goto next_rdata;
goto doexit;
goto next_rdata;
goto doexit;
return (ISC_FALSE);
return (ISC_TRUE);
dig_lookup_t *n, *l;
unsigned int local_timeout;
recvcount--;
|| cancel_now) {
&msg);
l->sendmsg,
l->querysig);
if (l->msgcounter != 0)
l->msgcounter++;
if (l->besteffort)
hex_dump(b);
cancel_lookup(l);
cancel_lookup(l);
l->servfail_stops) {
if (l->tcp_mode)
l->querysig);
&l->querysig);
l->doing_xfr ) {
if (timeout == 0) {
if (l->tcp_mode)
local_timeout, 0);
NULL,
&l->interval,
if ((l->trace)||
(l->ns_search_only)) {
ISC_TRUE);
query);
if (l->ns_search_only)
DNS_SECTION_ANSWER) == 0)
l->trace_root)
ISC_TRUE);
query);
== ISC_R_SUCCESS) &&
!l->trace_root ) {
if (l->pending)
if (l->doing_xfr) {
if (! docancel)
if (docancel) {
cancel_lookup(l);
query);
cancel_lookup(l);
sockcount--;
#ifdef USE_GETADDRINFO
int result;
#ifdef USE_GETADDRINFO
if (!have_ipv6)
else if (!have_ipv4)
if (result != 0) {
port);
start_lookup();
cancel_all(void) {
dig_lookup_t *l, *n;
if (free_now) {
while (q != NULL) {
q, current_lookup);
clear_query (q);
q = nq;
while (l != NULL) {
try_clear_lookup(l);
destroy_libs(void) {
void *ptr;
dig_server_t *s;
while (s != NULL) {
ptr = s;
if (is_dst_up) {
if (memdebugging != 0)