dighost.c revision fa6f2ebcbb822add6aa1f1b27f13ead4a55fd909
/*
* Copyright (C) 2000 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
/* $Id: dighost.c,v 1.46 2000/06/08 18:36:53 mws Exp $ */
/*
* Notice to programmers: Do not use this code as an example of how to
* use the ISC library to perform DNS lookups. Dig and Host both operate
* on the request level, since they allow fine-tuning of output and are
* intended as debugging tools. As a result, they perform many of the
* functions which could be better handled using the dns_resolver
* functions in most applications.
*/
#include <config.h>
#include <stdlib.h>
#include <unistd.h>
extern int h_errno;
#include <dns/rdataclass.h>
#include <dns/rdataset.h>
#include <dns/rdatatype.h>
#include <dns/rdatalist.h>
#include <dns/rdatastruct.h>
#ifdef TWIDDLE
#endif
unsigned int timeout = 5;
int sendcount = 0;
int sockcount = 0;
int ndots = -1;
int tries = 3;
int lookup_counter = 0;
int exitcode = 9;
char keynametext[MXNAME];
extern isc_boolean_t isc_mem_debugging;
static void
static int
count_dots(char *string) {
char *s;
int i=0;
s = string;
while (*s != 0) {
if (*s == '.')
i++;
s++;
}
return (i);
}
static void
hex_dump(isc_buffer_t *b) {
unsigned int len;
isc_region_t r;
isc_buffer_remainingregion(b, &r);
printf("\n");
}
if (len % 16 != 0)
printf("\n");
}
void
if (exitcode == 0)
exitcode = 8;
#ifdef NEVER
if (isc_mem_debugging)
}
#endif
}
void
if (debugging) {
}
}
void
if (result != ISC_R_SUCCESS) {
exitcode = 1;
}
}
/* Tests if a field is a class, without needing isc libs
* initialized. This list will have to be manually kept in
* sync with what the libs support.
*/
const int numclasses = 3;
int i;
for (i = 0; i < numclasses; i++)
return ISC_TRUE;
return ISC_FALSE;
}
/* Tests if a field is a type, without needing isc libs
* initialized. This list will have to be manually kept in
* sync with what the libs support.
*/
"soa", "mb", "mg", "mr", "null",
"wks", "ptr", "hinfo", "minfo",
"mx", "txt", "rp", "afsdb",
"x25", "isdn", "rt", "nsap",
"nsap_ptr", "sig", "key", "px",
"gpos", "aaaa", "loc", "nxt",
"srv", "naptr", "kx", "cert",
"a6", "dname", "opt", "unspec",
"tkey", "tsig", "axfr", "any"};
const int numtypes = 42;
int i;
for (i = 0; i < numtypes; i++) {
return ISC_TRUE;
}
return ISC_FALSE;
}
#ifdef TWIDDLE
void
isc_region_t r;
unsigned char bitfield;
int i, tw;
for (i=0;i<tw;i++) {
isc_buffer_usedregion (&buf, &r);
}
puts ("");
}
#endif
dig_server_t *s, *srv;
debug("requeue_lookup()");
if (free_now)
return(ISC_R_SUCCESS);
if (lookup_counter > LOOKUP_LIMIT)
fatal ("Too many lookups.");
(mctx, sizeof(struct dig_lookup));
fatal ("Memory allocation failure in %s:%d",
ISC_LIST_INIT(looknew->q);
if (servers) {
if (looknew->use_my_server_list) {
while (s != NULL) {
dig_server));
fatal("Memory allocation failure "
MXNAME);
link);
s = ISC_LIST_NEXT(s, link);
}
}
}
debug ("Before insertion, init@%lx "
"-> %lx, new@%lx "
"-> %lx", (long int)lookold,
debug ("After insertion, init -> "
"%lx, new = %lx, "
"new -> %lx", (long int)lookold,
return (looknew);
}
void
setup_system(void) {
char *ptr;
dig_lookup_t *l;
int secretsize;
unsigned char *secretstore;
if (fixeddomain[0]!=0) {
fatal("Memory allocation failure in %s:%d",
}
debug ("setup_system()");
if (get_servers &&
debug ("Got a nameserver line");
sizeof(struct dig_server));
fatal("Memory "
"allocation "
"failure in "
"%s:%d",
__LINE__);
ptr,
MXNAME - 1);
}
6) == 0) &&
(ndots == -1)) {
&ptr[6]);
debug ("ndots is "
"%d.",
ndots);
}
}
&& usesearch){
!= NULL) {
mctx, sizeof(struct
dig_server));
fatal("Memory "
"allocation "
"failure in %s:"
"%d", __FILE__,
__LINE__);
ptr,
MXNAME - 1);
link);
}
(fixeddomain[0] == 0 )){
!= NULL) {
mctx, sizeof(struct
dig_server));
fatal("Memory "
"allocation "
"failure in %s:"
"%d", __FILE__,
__LINE__);
ptr,
MXNAME - 1);
link);
}
}
}
}
}
if (ndots == -1)
ndots = 1;
fatal("Memory allocation failure");
}
for (l = ISC_LIST_HEAD(lookup_list) ;
l != NULL;
l = ISC_LIST_NEXT(l, link) ) {
}
if (keysecret[0] != 0) {
debug("keyring");
debug("buffer");
debug("name");
debug("secretstore");
debug("lex");
if (result != ISC_R_SUCCESS) {
printf (";; Couldn't create key %s: %s\n",
goto SYSSETUP_FAIL;
}
debug("close");
debug("namefromtext");
namebuf);
if (result != ISC_R_SUCCESS) {
printf (";; Couldn't create key %s: %s\n",
goto SYSSETUP_FAIL;
}
debug("tsigkey");
if (result != ISC_R_SUCCESS) {
printf (";; Couldn't create key %s: %s\n",
}
return;
return;
}
}
void
setup_libs(void) {
debug ("setup_libs()");
/*
* Warning: This is not particularly good randomness. We'll
* just use random() now for getting id values, but doing so
* does NOT insure that id's cann't be guessed.
*/
result = isc_app_start();
result = isc_net_probeipv4();
result = isc_net_probeipv6();
if (result == ISC_R_SUCCESS)
}
static void
debug ("add_opt()");
}
static void
{
debug ("add_type()");
}
static void
if (free_now)
return;
}
}
if (still_working)
return;
debug ("Have %d retries left for %s",
return;
}
debug ("Setting up for TCP");
}
} else {
return;
}
debug ("Setting up for UDP");
}
} else {
debug ("Retrying");
} else {
debug ("Cancelling");
}
}
}
}
static void
isc_buffer_t *b = NULL;
isc_region_t r;
int len;
debug ("followup_lookup()");
if (free_now)
return;
if (result != ISC_R_SUCCESS) {
debug ("Firstname returned %s",
if ((section == DNS_SECTION_ANSWER) &&
return;
}
for (;;) {
while (loopresult == ISC_R_SUCCESS) {
debug ("Got rdata with type %d",
{
BUFSIZE);
"isc_buffer_allocate");
NULL,
b);
"dns_rdata_totext");
isc_buffer_usedregion(b, &r);
/* Initialize lookup if we've not yet */
debug ("Found NS %d %.*s",
(char *)r.base);
if (!success) {
if (section ==
else
query->
}
sizeof(
struct
dig_server));
fatal("Memory allocation "
"failure in %s:%d",
debug ("Adding server %s",
srv->servername);
isc_buffer_free (&b);
}
}
}
if (result != ISC_R_SUCCESS)
break;
}
}
static void
debug ("next_origin()");
if (free_now)
return;
there's nothing left. */
debug ("Made it to the root whith nowhere to go.");
return;
}
}
static void
debug ("insert_soa()");
MXNAME);
}
void
int len;
isc_region_t r;
isc_buffer_t b;
if (free_now)
return;
debug("Setting up for looking up %s @%lx->%lx",
if (lookup->new_search) {
debug ("Resetting lookup counter.");
lookup_counter = 0;
}
isc_buffer_add(&b, len);
if (result != ISC_R_SUCCESS) {
fatal("Aborting: %s is not a legal name syntax. (%s)",
}
if (!lookup->trace_root) {
isc_buffer_add(&b, len);
} else {
isc_buffer_add(&b, 1);
}
if (result != ISC_R_SUCCESS) {
fatal("Aborting: %s is not a legal name syntax. (%s)",
}
} else {
debug ("Using root origin.");
if (!lookup->trace_root) {
isc_buffer_add(&b, len);
} else {
isc_buffer_add(&b, 1);
}
if (result != ISC_R_SUCCESS) {
isc_buffer_usedregion (&b, &r);
fatal("Aborting: %s/%.*s is not a legal name syntax. "
}
}
isc_buffer_usedregion (&b, &r);
debug ("This is an absolute name.");
else
debug ("This is a relative name (which is wrong).");
lookup->msgcounter = 0;
/*
* If this is a trace request, completely disallow recursion, since
* it's meaningless for traces.
*/
debug ("Recursive query");
}
debug ("AA query");
}
if (lookup->trace_root) {
} else {
}
if ((rdtype == dns_rdatatype_axfr) ||
(rdtype == dns_rdatatype_ixfr)) {
/*
* Force TCP mode if we're doing an xfr.
*/
}
if (lookup->trace_root) {
} else {
}
if (rdtype == dns_rdatatype_ixfr)
debug ("Initializing keys");
}
debug ("Starting to render the message");
}
DNS_SECTION_QUESTION, 0);
debug ("Done rendering.");
if (lookup->use_my_server_list)
else
fatal("Memory allocation failure in %s:%d",
debug ("Create query %lx linked to lookup %lx",
query->second_rr_serial = 0;
}
ISC_TRUE);
}
}
static void
debug("send_done()");
}
static void
debug("cancel_lookup()");
debug ("Cancelling a worker.");
}
sockcount--;
}
}
}
static void
static void
void
debug ("send_udp()");
debug ("Working on lookup %lx, query %lx",
debug ("recving with lookup=%lx, query=%lx, sock=%lx",
sendcount++;
#ifdef TWIDDLE
if (twiddle) {
}
#endif
debug("Sending a request.");
}
}
/* connect_timeout is used for both UDP recieves and TCP connects. */
static void
dig_query_t *q=NULL;
isc_buffer_t *b=NULL;
isc_region_t r;
debug("connect_timeout()");
debug ("Buffer Allocate connect_timeout");
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);
isc_buffer_usedregion(b, &r);
printf(";; Connection to server %.*s "
"for %s timed out. "
"Retrying %d.\n",
else {
printf(";; Connection to "
"server %.*s "
"for %s timed out. "
"Giving up.\n",
} else {
printf(";; Connection to "
"server %.*s "
"for %s timed out. "
"Trying TCP.\n",
}
}
}
}
}
isc_buffer_free(&b);
debug ("Done with connect_timeout()");
}
static void
isc_buffer_t *b=NULL;
isc_region_t r;
debug("tcp_length_done()");
if (free_now) {
return;
}
return;
}
debug ("Buffer Allocate connect_timeout");
isc_buffer_usedregion(b, &r);
isc_buffer_free(&b);
sockcount--;
return;
}
length = isc_buffer_getuint16(b);
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.
*/
debug ("recving with lookup=%lx, query=%lx",
}
static void
debug("launch_next_query()");
if (free_now)
return;
debug("Ignoring launch_next_query because !pending.");
sockcount--;
return;
}
if (include_question) {
#ifdef TWIDDLE
if (twiddle) {
}
#endif
link);
}
sendcount++;
if (!query->first_soa_rcvd) {
debug("Sending a request.");
}
return;
}
static void
isc_buffer_t *b=NULL;
isc_region_t r;
debug ("connect_done()");
if (free_now) {
return;
}
debug ("Buffer Allocate connect_timeout");
isc_buffer_usedregion(b, &r);
printf(";; Connection to server %.*s for %s failed: %s.\n",
if (exitcode < 9)
exitcode = 9;
isc_buffer_free(&b);
return;
}
}
#if 0
static isc_boolean_t
debug("msg_contains_soa()");
if (result == ISC_R_SUCCESS) {
return (ISC_TRUE);
} else {
debug("Didn't find SOA, result=%d:%s",
return (ISC_FALSE);
}
}
#endif
static void
{
isc_buffer_t b;
isc_region_t r;
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.
*/
if (result != ISC_R_SUCCESS) {
puts("; Transfer failed.");
return;
}
#ifdef NEVER
#endif
do {
&name);
if (result != ISC_R_SUCCESS)
continue;
do {
/*
* If this is the first rr, make sure
* it's an SOA
*/
if ((!query->first_soa_rcvd) &&
puts("; Transfer failed. "
"Didn't start with "
"SOA answer.");
return;
}
if ((!query->second_rr_rcvd) &&
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...
*/
goto NEXT_RDATA;
/* Now we have an SOA. Work with it. */
debug ("Got a SOA");
&soa,
mctx);
"dns_rdata_tostruct");
if (!query->first_soa_rcvd) {
debug ("This is the first. %d",
goto XFR_DONE;
}
goto NEXT_RDATA;
}
if (!query->second_rr_rcvd) {
debug ("This is the second. %d",
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.");
&b);
"isc_sockaddr_totext");
isc_buffer_usedregion(&b, &r);
return;
}
/*
* If we get to this point, we're doing an
* IXFR and have to start really looking
* at serial numbers.
*/
debug ("Got a match for ixfr");
if (!query->first_repeat_rcvd) {
goto NEXT_RDATA;
}
debug ("Done with ixfr");
goto XFR_DONE;
}
debug ("Meaningless soa, %d",
} while (result == ISC_R_SUCCESS);
}
} while (result == ISC_R_SUCCESS);
return;
}
static void
isc_buffer_t *b = NULL;
isc_region_t r;
dig_lookup_t *n;
debug ("recv_done()");
if (free_now) {
return;
}
debug("(lookup=%lx, query=%lx)",
if (free_now) {
debug("Bailing out, since freeing now.");
return;
}
sendcount--;
debug("No longer pending. Got %s",
return;
}
&msg);
debug ("Getting initial querysig");
"dns_message_getquerytsig");
}
}
debug ("Before parse starts");
if (result != ISC_R_SUCCESS) {
printf (";; Got bad UDP packet:\n");
hex_dump(b);
printf (";; Retrying in TCP mode.\n");
}
return;
}
debug ("Before verify");
debug ("After verify");
if (result != ISC_R_SUCCESS) {
printf (";; Couldn't verify signature: %s\n",
}
debug ("Freeing querysig buffer %lx",
}
}
debug ("After parse");
if (show_details ||
== ISC_R_SUCCESS) &&
}
} else {
if ((result != ISC_R_SUCCESS) ||
}
if (show_details) {
}
} else {
if (query->first_soa_rcvd &&
else
}
== ISC_R_SUCCESS) &&
}
debug("Still pending.");
isc_event_free (&event);
return;
}
}
else {
&ab);
isc_buffer_usedregion(&ab, &r);
query);
}
}
}
return;
}
/* In truth, we should never get into the CANCELED routine, since
the cancel_lookup() routine clears the pending flag. */
debug ("In cancel handler");
return;
}
fatal("recv_done got result %s",
}
void
debug("get_address()");
else {
fatal("Couldn't look up your server host %s. errno=%d",
port);
}
}
static void
debug("do_lookup_tcp()");
sockcount++;
if (specified_source) {
}
}
}
static void
debug("do_lookup_udp()");
debug("I'm starting UDP with tcp_mode set!!!");
sockcount++;
if (specified_source) {
}
}
}
void
debug ("do_lookup()");
else
}
void
start_lookup(void) {
debug ("start_lookup()");
if (free_now)
return;
}
}
void
free_lists(int _exitcode) {
void *ptr;
dig_lookup_t *l;
dig_query_t *q;
dig_server_t *s;
dig_searchlist_t *o;
debug("free_lists()");
if (free_now)
return;
l = ISC_LIST_HEAD(lookup_list);
while (l != NULL) {
q = ISC_LIST_HEAD(l->q);
while (q != NULL) {
debug ("Freeing query %lx, belonging to %lx",
q, l);
isc_socket_detach(&q->sock);
sockcount--;
}
link);
link);
isc_buffer_invalidate(&q->recvbuf);
ptr = q;
q = ISC_LIST_NEXT(q, link);
}
if (l->use_my_server_list) {
s = ISC_LIST_HEAD(l->my_server_list);
while (s != NULL) {
debug ("Freeing server %lx belonging to %lx",
s, l);
ptr = s;
s = ISC_LIST_NEXT(s, link);
}
}
dns_message_destroy (&l->sendmsg);
isc_timer_detach (&l->timer);
isc_buffer_free(&l->querysig);
}
ptr = l;
l = ISC_LIST_NEXT(l, link);
}
s = ISC_LIST_HEAD(server_list);
while (s != NULL) {
debug ("Freeing global server %lx", s);
ptr = s;
s = ISC_LIST_NEXT(s, link);
}
o = ISC_LIST_HEAD(search_list);
while (o != NULL) {
debug ("Freeing search %lx", o);
ptr = o;
o = ISC_LIST_NEXT(o, link);
}
debug ("Freeing socketmgr");
}
debug ("Freeing timermgr");
}
if (global_task != NULL) {
debug ("Freeing task");
}
debug ("Freeing taskmgr");
}
}
}
if (isc_mem_debugging)
if (_exitcode != 0)
}