Commit 6c7a2db6 authored by Michael Sawyer's avatar Michael Sawyer
Browse files

Add +trace option, change some of the behavior of other options.

parent 76a33ffe
......@@ -15,8 +15,6 @@
* SOFTWARE.
*/
#include <config.h>
#include <errno.h>
......@@ -76,16 +74,13 @@ extern isc_buffer_t rootbuf;
extern int sendcount;
extern int ndots;
extern int tries;
extern int lookup_counter;
extern char fixeddomain[MXNAME];
#ifdef TWIDDLE
extern isc_boolean_t twiddle;
#endif
isc_boolean_t short_form = ISC_FALSE;
isc_boolean_t ns_search_only = ISC_FALSE;
isc_boolean_t comments = ISC_TRUE, section_question = ISC_TRUE,
section_answer = ISC_TRUE, section_authority = ISC_TRUE,
section_additional = ISC_TRUE, recurse = ISC_TRUE;
static char *opcodetext[] = {
......@@ -191,7 +186,7 @@ check_next_lookup(dig_lookup_t *lookup) {
debug ("Have %d retries left for %s\n",
lookup->retries, lookup->textname);
if ((next == NULL)&&((lookup->retries <= 1)
||tcp_mode)) {
||tcp_mode || !lookup->pending)) {
debug("Shutting Down.", stderr);
isc_app_shutdown();
return;
......@@ -201,10 +196,11 @@ check_next_lookup(dig_lookup_t *lookup) {
setup_lookup(next);
do_lookup_tcp(next);
} else {
if (lookup->retries > 1) {
if ((lookup->retries > 1) && (lookup->pending)) {
lookup->retries --;
send_udp(lookup);
} else {
ENSURE (next != NULL);
setup_lookup(next);
do_lookup_udp(next);
}
......@@ -220,37 +216,59 @@ received(int bytes, int frmsize, char *frm, dig_query_t *query) {
result = isc_time_now(&now);
check_result (result, "isc_time_now");
if (!short_form && query->lookup->comments) {
if (query->lookup->comments) {
diff = isc_time_microdiff(&now, &query->time_sent);
printf(";; Query time: %ld msec\n", (long int)diff/1000);
printf(";; Received %u bytes from %.*s\n",
bytes, frmsize, frm);
time (&tnow);
printf(";; When: %s\n", ctime(&tnow));
} else if (query->lookup->identify && !short_form) {
diff = isc_time_microdiff(&now, &query->time_sent);
printf(";; Received %u bytes from %.*s in %d ms\n",
bytes, frmsize, frm, (int)diff/1000);
}
}
void
trying(int frmsize, char *frm, dig_lookup_t *lookup) {
if (lookup->comments)
if (lookup->comments && !short_form)
printf ("; Trying %.*s\n", frmsize, frm);
}
static void
say_message(dns_rdata_t *rdata, dig_query_t *query) {
isc_buffer_t *b=NULL;
isc_region_t r;
isc_buffer_t *b=NULL, *b2=NULL;
isc_region_t r, r2;
isc_result_t result;
isc_uint64_t diff;
isc_time_t now;
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);
printf ( "%.*s", (int)r.length, (char *)r.base);
if (!query->lookup->trace && !query->lookup->ns_search_only)
printf ( "%.*s", (int)r.length, (char *)r.base);
else {
result = isc_buffer_allocate(mctx, &b2, BUFSIZE);
check_result (result, "isc_buffer_allocate");
result = dns_rdatatype_totext(rdata->type, b2);
check_result(result, "dns_rdatatype_totext");
isc_buffer_usedregion(b2, &r2);
printf ( "%.*s %.*s",(int)r2.length, (char *)r2.base,
(int)r.length, (char *)r.base);
isc_buffer_free (&b2);
}
if (query->lookup->identify) {
printf (" on server %s", query->servname);
result = isc_time_now(&now);
check_result (result, "isc_time_now");
diff = isc_time_microdiff(&now, &query->time_sent);
printf (" from server %s in %d ms", query->servname,
(int)diff/1000);
}
printf ("\n");
isc_buffer_free(&b);
......@@ -276,7 +294,7 @@ printsection(dns_message_t *msg, dns_section_t sectionid, char *section_name,
else
no_rdata = ISC_FALSE;
if (headers && query->lookup->comments)
if (headers && query->lookup->comments && !short_form)
printf(";; %s SECTION:\n", section_name);
dns_name_init(&empty_name, NULL);
......@@ -376,7 +394,7 @@ printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) {
result = ISC_R_SUCCESS;
if (headers) {
if (query->lookup->comments) {
if (query->lookup->comments && !short_form) {
printf(";; ->>HEADER<<- opcode: %s, status: %s, "
"id: %u\n",
opcodetext[msg->opcode], rcodetext[msg->rcode],
......@@ -439,16 +457,19 @@ printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) {
}
if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_ANSWER]) &&
query->lookup->section_answer ) {
if (headers && query->lookup->comments)
if (headers && query->lookup->comments && !short_form)
printf("\n");
result = printsection(msg, DNS_SECTION_ANSWER, "ANSWER",
headers, query);
if (result != ISC_R_SUCCESS)
return (result);
}
if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_AUTHORITY]) &&
headers && query->lookup->section_authority) {
if (headers && query->lookup->comments)
if ((! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_AUTHORITY]) &&
headers && query->lookup->section_authority) ||
( ISC_LIST_EMPTY(msg->sections[DNS_SECTION_ANSWER]) &&
headers && query->lookup->section_answer &&
query->lookup->trace )) {
if (headers && query->lookup->comments && !short_form)
printf("\n");
result = printsection(msg, DNS_SECTION_AUTHORITY, "AUTHORITY",
ISC_TRUE, query);
......@@ -457,7 +478,7 @@ printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) {
}
if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_ADDITIONAL]) &&
headers && query->lookup->section_additional) {
if (headers && query->lookup->comments)
if (headers && query->lookup->comments && !short_form)
printf("\n");
result = printsection(msg, DNS_SECTION_ADDITIONAL,
"ADDITIONAL", ISC_TRUE, query);
......@@ -465,14 +486,14 @@ printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) {
return (result);
}
if ((tsig != NULL) && headers && query->lookup->section_additional) {
if (headers && query->lookup->comments)
if (headers && query->lookup->comments && !short_form)
printf("\n");
result = printrdata(msg, tsig, tsigname,
"PSEUDOSECTION TSIG", ISC_TRUE);
if (result != ISC_R_SUCCESS)
return (result);
}
if (headers && query->lookup->comments)
if (headers && query->lookup->comments && !short_form)
printf("\n");
return (result);
......@@ -521,7 +542,12 @@ reorder_args(int argc, char *argv[]) {
*/
void
parse_args(isc_boolean_t is_batchfile, int argc, char **argv) {
isc_boolean_t have_host = ISC_FALSE, identify = ISC_FALSE;
isc_boolean_t have_host = ISC_FALSE, identify = ISC_FALSE,
trace = ISC_FALSE, ns_search_only = ISC_FALSE,
forcecomment = ISC_FALSE,
comments = ISC_TRUE, section_question = ISC_TRUE,
section_answer = ISC_TRUE, section_authority = ISC_TRUE,
section_additional = ISC_TRUE, recurse = ISC_TRUE;
dig_server_t *srv = NULL;
dig_lookup_t *lookup = NULL;
char *batchname = NULL;
......@@ -539,7 +565,7 @@ parse_args(isc_boolean_t is_batchfile, int argc, char **argv) {
if (srv == NULL)
fatal("Memory allocation failure.");
strncpy(srv->servername, &argv[0][1], MXNAME-1);
if ((is_batchfile) || (!have_host)) {
if (is_batchfile && have_host) {
if (!lookup->use_my_server_list) {
ISC_LIST_INIT (lookup->
my_server_list);
......@@ -589,23 +615,35 @@ parse_args(isc_boolean_t is_batchfile, int argc, char **argv) {
ns_search_only = ISC_TRUE;
recurse = ISC_FALSE;
identify = ISC_TRUE;
short_form = ISC_TRUE;
identify = ISC_TRUE;
if (!forcecomment)
comments = ISC_FALSE;
section_additional = ISC_FALSE;
section_authority = ISC_FALSE;
section_question = ISC_FALSE;
} else if (strncmp(argv[0], "+nons", 6) == 0) {
ns_search_only = ISC_FALSE;
} else if (strncmp(argv[0], "+tr", 3) == 0) {
trace = ISC_TRUE;
ns_search_only = ISC_TRUE;
recurse = ISC_FALSE;
identify = ISC_TRUE;
if (!forcecomment)
comments = ISC_FALSE;
section_additional = ISC_FALSE;
section_authority = ISC_FALSE;
section_question = ISC_FALSE;
show_details = ISC_TRUE;
} else if (strncmp(argv[0], "+notr", 6) == 0) {
trace = ISC_FALSE;
} else if (strncmp(argv[0], "+det", 4) == 0) {
show_details = ISC_TRUE;
short_form = ISC_FALSE;
} else if (strncmp(argv[0], "+nodet", 6) == 0) {
show_details = ISC_FALSE;
} else if (strncmp(argv[0], "+sho", 4) == 0) {
short_form = ISC_TRUE;
show_details = ISC_FALSE;
comments = ISC_FALSE;
if (!forcecomment)
comments = ISC_FALSE;
section_additional = ISC_FALSE;
section_authority = ISC_FALSE;
section_question = ISC_FALSE;
......@@ -617,8 +655,10 @@ parse_args(isc_boolean_t is_batchfile, int argc, char **argv) {
identify = ISC_FALSE;
} else if (strncmp(argv[0], "+com", 4) == 0) {
comments = ISC_TRUE;
forcecomment = ISC_TRUE;
} else if (strncmp(argv[0], "+nocom", 6) == 0) {
comments = ISC_FALSE;
forcecomment = ISC_FALSE;
} else if (strncmp(argv[0], "+que", 4) == 0) {
section_question = ISC_TRUE;
} else if (strncmp(argv[0], "+noque", 6) == 0) {
......@@ -635,6 +675,19 @@ parse_args(isc_boolean_t is_batchfile, int argc, char **argv) {
section_authority = ISC_TRUE;
} else if (strncmp(argv[0], "+noaut", 6) == 0) {
section_authority = ISC_FALSE;
} else if (strncmp(argv[0], "+all", 4) == 0) {
section_question = ISC_TRUE;
section_authority = ISC_TRUE;
section_answer = ISC_TRUE;
section_additional = ISC_TRUE;
comments = ISC_TRUE;
} else if (strncmp(argv[0], "+noall", 6) == 0) {
section_question = ISC_FALSE;
section_authority = ISC_FALSE;
section_answer = ISC_FALSE;
section_additional = ISC_FALSE;
comments = ISC_FALSE;
#ifdef TWIDDLE
} else if (strncmp(argv[0], "+twiddle", 6) == 0) {
twiddle = ISC_TRUE;
......@@ -687,6 +740,9 @@ parse_args(isc_boolean_t is_batchfile, int argc, char **argv) {
&adrs[2], &adrs[3]);
if (n == 0)
show_usage();
lookup_counter++;
if (lookup_counter > LOOKUP_LIMIT)
fatal ("Too many lookups.");
lookup = isc_mem_allocate(mctx,
sizeof(struct dig_lookup));
if (lookup == NULL)
......@@ -711,11 +767,14 @@ parse_args(isc_boolean_t is_batchfile, int argc, char **argv) {
lookup->xfr_q = NULL;
lookup->origin = NULL;
lookup->use_my_server_list = ISC_FALSE;
lookup->trace = (trace || ns_search_only);
lookup->trace_root = trace;
lookup->ns_search_only = ns_search_only;
lookup->doing_xfr = ISC_FALSE;
lookup->identify = identify;
lookup->recurse = recurse;
lookup->retries = tries;
lookup->nsfound = 0;
lookup->comments = comments;
lookup->section_question = section_question;
lookup->section_answer = section_answer;
......@@ -740,6 +799,9 @@ parse_args(isc_boolean_t is_batchfile, int argc, char **argv) {
continue;
}
}
lookup_counter++;
if (lookup_counter > LOOKUP_LIMIT)
fatal ("Too many lookups.");
lookup = isc_mem_allocate(mctx,
sizeof(struct dig_lookup));
if (lookup == NULL)
......@@ -758,10 +820,13 @@ parse_args(isc_boolean_t is_batchfile, int argc, char **argv) {
lookup->origin = NULL;
lookup->use_my_server_list = ISC_FALSE;
lookup->doing_xfr = ISC_FALSE;
lookup->trace = (trace || ns_search_only);
lookup->trace_root = trace;
lookup->ns_search_only = ns_search_only;
lookup->identify = identify;
lookup->recurse = recurse;
lookup->retries = tries;
lookup->nsfound = 0;
lookup->comments = comments;
lookup->section_question = section_question;
lookup->section_answer = section_answer;
......@@ -797,6 +862,9 @@ parse_args(isc_boolean_t is_batchfile, int argc, char **argv) {
}
}
if (lookup_list.head == NULL) {
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.");
......@@ -812,10 +880,13 @@ parse_args(isc_boolean_t is_batchfile, int argc, char **argv) {
lookup->origin = NULL;
lookup->use_my_server_list = ISC_FALSE;
lookup->doing_xfr = ISC_FALSE;
lookup->trace = (trace || ns_search_only);
lookup->trace_root = trace;
lookup->ns_search_only = ns_search_only;
lookup->identify = identify;
lookup->recurse = recurse;
lookup->retries = tries;
lookup->nsfound = 0;
lookup->comments = comments;
lookup->section_question = section_question;
lookup->section_answer = section_answer;
......
......@@ -60,8 +60,7 @@ ISC_LIST(dig_server_t) server_list;
ISC_LIST(dig_searchlist_t) search_list;
isc_boolean_t tcp_mode = ISC_FALSE, have_ipv6 = ISC_FALSE,
free_now = ISC_FALSE, show_details = ISC_FALSE, usesearch=ISC_TRUE,
trace = ISC_FALSE;
free_now = ISC_FALSE, show_details = ISC_FALSE, usesearch=ISC_TRUE;
#ifdef TWIDDLE
isc_boolean_t twiddle = ISC_FALSE;
#endif
......@@ -78,6 +77,7 @@ isc_buffer_t rootbuf;
int sendcount = 0;
int ndots = -1;
int tries = 3;
int lookup_counter = 0;
char fixeddomain[MXNAME]="";
static void
......@@ -167,8 +167,8 @@ isclass(char *text) {
* initialized. This list will have to be manually kept in
* sync with what the libs support.
*/
const char *classlist[] = {"in", "hs", "any"};
const int numclasses = 3;
const char *classlist[] = {"in", "hs"};
const int numclasses = 2;
int i;
for (i = 0; i < numclasses; i++)
......@@ -193,8 +193,8 @@ istype(char *text) {
"gpos", "aaaa", "loc", "nxt",
"srv", "naptr", "kx", "cert",
"a6", "dname", "opt", "unspec",
"tkey", "tsig", "axfr"};
const int numtypes = 41;
"tkey", "tsig", "axfr", "any"};
const int numtypes = 42;
int i;
for (i = 0; i < numtypes; i++) {
......@@ -245,7 +245,8 @@ setup_system(void) {
if (fixeddomain[0]!=0) {
search = isc_mem_allocate( mctx, sizeof(struct dig_server));
if (search == NULL)
fatal("Memory allocation failure.");
fatal("Memory allocation failure in %s:%d",
__FILE__, __LINE__);
strncpy(search->origin, fixeddomain, MXNAME - 1);
ISC_LIST_PREPEND(search_list, search, link);
}
......@@ -268,7 +269,10 @@ setup_system(void) {
if (srv == NULL)
fatal("Memory "
"allocation "
"failure.");
"failure in "
"%s:%d",
__FILE__,
__LINE__);
strncpy((char *)srv->
servername,
ptr,
......@@ -300,7 +304,9 @@ setup_system(void) {
if (search == NULL)
fatal("Memory "
"allocation "
"failure.");
"failure in %s:"
"%d", __FILE__,
__LINE__);
strncpy(search->
origin,
ptr,
......@@ -320,7 +326,9 @@ setup_system(void) {
if (search == NULL)
fatal("Memory "
"allocation "
"failure.");
"failure in %s:"
"%d", __FILE__,
__LINE__);
strncpy(search->
origin,
ptr,
......@@ -406,7 +414,8 @@ add_type(dns_message_t *message, dns_name_t *name, dns_rdataclass_t rdclass,
}
static void
followup_lookup(dns_message_t *msg, dig_query_t *query) {
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;
......@@ -415,13 +424,17 @@ followup_lookup(dns_message_t *msg, dig_query_t *query) {
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, DNS_SECTION_ANSWER);
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;
}
......@@ -429,15 +442,20 @@ followup_lookup(dns_message_t *msg, dig_query_t *query) {
for (;;) {
name = NULL;
dns_message_currentname(msg, DNS_SECTION_ANSWER,
&name);
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);
if (rdata.type == dns_rdatatype_ns) {
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,
......@@ -455,65 +473,114 @@ followup_lookup(dns_message_t *msg, dig_query_t *query) {
debug ("Found NS %d %.*s",
(int)r.length, (int)r.length,
(char *)r.base);
lookup = isc_mem_allocate
(mctx,
sizeof(struct
dig_lookup));
if (lookup == NULL)
fatal ("Memory "
"allocation "
"failure.");
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->identify = ISC_TRUE;
lookup->recurse = query->lookup->
recurse;
lookup->ns_search_only =
ISC_FALSE;
lookup->use_my_server_list =
ISC_TRUE;
lookup->retries = tries;
lookup->comments =
query->lookup->comments;
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 (!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->identify =
query->lookup->identify;
lookup->recurse =
query->lookup->
recurse;
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->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->