2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-09-01 15:05:23 +00:00

Modify dig to use message_*totext() routines.

This commit is contained in:
Michael Sawyer
2000-05-24 19:27:01 +00:00
parent 6b554f7f0d
commit 932b781b81
2 changed files with 119 additions and 161 deletions

View File

@@ -29,6 +29,7 @@ extern int h_errno;
#include <dns/rdata.h> #include <dns/rdata.h>
#include <dns/rdataset.h> #include <dns/rdataset.h>
#include <dns/rdatatype.h> #include <dns/rdatatype.h>
#include <dns/message.h>
#include <dig/dig.h> #include <dig/dig.h>
@@ -36,6 +37,12 @@ extern ISC_LIST(dig_lookup_t) lookup_list;
extern ISC_LIST(dig_server_t) server_list; extern ISC_LIST(dig_server_t) server_list;
extern ISC_LIST(dig_searchlist_t) search_list; extern ISC_LIST(dig_searchlist_t) search_list;
#define ADD_STRING(b, s) {if (strlen(s) >= \
isc_buffer_availablelength(b)) \
return(ISC_R_NOSPACE); else \
isc_buffer_putstr(b, s);}
extern isc_boolean_t have_ipv6, show_details, specified_source, extern isc_boolean_t have_ipv6, show_details, specified_source,
usesearch, qr; usesearch, qr;
extern in_port_t port; extern in_port_t port;
@@ -196,45 +203,39 @@ trying(int frmsize, char *frm, dig_lookup_t *lookup) {
} }
static void isc_result_t
say_message(dns_rdata_t *rdata, dig_query_t *query) { say_message(dns_rdata_t *rdata, dig_query_t *query, isc_buffer_t *buf) {
isc_buffer_t *b = NULL, *b2 = NULL;
isc_region_t r, r2; isc_region_t r, r2;
isc_result_t result; isc_result_t result;
isc_uint64_t diff; isc_uint64_t diff;
isc_time_t now; isc_time_t now;
char store[sizeof("12345678901234567890")];
result = isc_buffer_allocate(mctx, &b, BUFSIZE); if (query->lookup->trace || query->lookup->ns_search_only) {
check_result (result, "isc_buffer_allocate"); result = dns_rdatatype_totext(rdata->type, buf);
result = dns_rdata_totext(rdata, NULL, b); if (result != ISC_R_SUCCESS)
check_result(result, "dns_rdata_totext"); return (result);
isc_buffer_usedregion(b, &r); ADD_STRING(buf, " ");
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);
} }
result = dns_rdata_totext(rdata, NULL, buf);
check_result(result, "dns_rdata_totext");
if (query->lookup->identify) { if (query->lookup->identify) {
result = isc_time_now(&now); result = isc_time_now(&now);
check_result (result, "isc_time_now"); if (result != ISC_R_SUCCESS)
return (result);
diff = isc_time_microdiff(&now, &query->time_sent); diff = isc_time_microdiff(&now, &query->time_sent);
printf (" from server %s in %d ms", query->servname, ADD_STRING(buf, " from server ");
(int)diff/1000); ADD_STRING(buf, query->servname);
snprintf (store, 19, " in %d ms.", (int)diff/1000);
ADD_STRING(buf, store);
} }
printf ("\n"); ADD_STRING(buf,"\n");
isc_buffer_free(&b); return (ISC_R_SUCCESS);
} }
static isc_result_t isc_result_t
printsection(dns_message_t *msg, dns_section_t sectionid, char *section_name, short_answer(dns_message_t *msg, dns_messagetextflag_t flags,
isc_boolean_t headers, dig_query_t *query) isc_buffer_t *buf, dig_query_t *query)
{ {
dns_name_t *name, *print_name; dns_name_t *name, *print_name;
dns_rdataset_t *rdataset; dns_rdataset_t *rdataset;
@@ -247,17 +248,8 @@ printsection(dns_message_t *msg, dns_section_t sectionid, char *section_name,
isc_boolean_t no_rdata; isc_boolean_t no_rdata;
dns_rdata_t rdata; dns_rdata_t rdata;
if (sectionid == DNS_SECTION_QUESTION)
no_rdata = ISC_TRUE;
else
no_rdata = ISC_FALSE;
if (headers && query->lookup->comments && !short_form)
printf(";; %s SECTION:\n", section_name);
dns_name_init(&empty_name, NULL); dns_name_init(&empty_name, NULL);
result = dns_message_firstname(msg, DNS_SECTION_ANSWER);
result = dns_message_firstname(msg, sectionid);
if (result == ISC_R_NOMORE) if (result == ISC_R_NOMORE)
return (ISC_R_SUCCESS); return (ISC_R_SUCCESS);
else if (result != ISC_R_SUCCESS) else if (result != ISC_R_SUCCESS)
@@ -265,7 +257,7 @@ printsection(dns_message_t *msg, dns_section_t sectionid, char *section_name,
for (;;) { for (;;) {
name = NULL; name = NULL;
dns_message_currentname(msg, sectionid, &name); dns_message_currentname(msg, DNS_SECTION_ANSWER, &name);
isc_buffer_init(&target, t, sizeof(t)); isc_buffer_init(&target, t, sizeof(t));
first = ISC_TRUE; first = ISC_TRUE;
@@ -274,40 +266,18 @@ printsection(dns_message_t *msg, dns_section_t sectionid, char *section_name,
for (rdataset = ISC_LIST_HEAD(name->list); for (rdataset = ISC_LIST_HEAD(name->list);
rdataset != NULL; rdataset != NULL;
rdataset = ISC_LIST_NEXT(rdataset, link)) { rdataset = ISC_LIST_NEXT(rdataset, link)) {
if (!short_form) { loopresult = dns_rdataset_first(rdataset);
result = dns_rdataset_totext(rdataset, while (loopresult == ISC_R_SUCCESS) {
print_name, dns_rdataset_current(rdataset, &rdata);
ISC_FALSE, result = say_message(&rdata, query,
no_rdata, buf);
&target); check_result (result, "say_message");
if (result != ISC_R_SUCCESS) loopresult = dns_rdataset_next(
return (result); rdataset);
#ifdef USEINITALWS
if (first) {
print_name = &empty_name;
first = ISC_FALSE;
}
#else
UNUSED(first); /* Shut up compiler. */
#endif
} else {
loopresult = dns_rdataset_first(rdataset);
while (loopresult == ISC_R_SUCCESS) {
dns_rdataset_current(rdataset, &rdata);
say_message(&rdata, query);
loopresult = dns_rdataset_next(
rdataset);
}
} }
} }
isc_buffer_usedregion(&target, &r); result = dns_message_nextname(msg, DNS_SECTION_ANSWER);
if (no_rdata)
printf(";%.*s", (int)r.length, (char *)r.base);
else
printf("%.*s", (int)r.length, (char *)r.base);
result = dns_message_nextname(msg, sectionid);
if (result == ISC_R_NOMORE) if (result == ISC_R_NOMORE)
break; break;
else if (result != ISC_R_SUCCESS) else if (result != ISC_R_SUCCESS)
@@ -317,40 +287,18 @@ printsection(dns_message_t *msg, dns_section_t sectionid, char *section_name,
return (ISC_R_SUCCESS); return (ISC_R_SUCCESS);
} }
static isc_result_t
printrdata(dns_message_t *msg, dns_rdataset_t *rdataset, dns_name_t *owner,
char *set_name, isc_boolean_t headers)
{
isc_buffer_t target;
isc_result_t result;
isc_region_t r;
char t[4096];
UNUSED(msg);
if (headers)
printf(";; %s SECTION:\n", set_name);
isc_buffer_init(&target, t, sizeof(t));
result = dns_rdataset_totext(rdataset, owner, ISC_FALSE, ISC_FALSE,
&target);
if (result != ISC_R_SUCCESS)
return (result);
isc_buffer_usedregion(&target, &r);
printf("%.*s", (int)r.length, (char *)r.base);
return (ISC_R_SUCCESS);
}
isc_result_t isc_result_t
printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) { printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) {
isc_boolean_t did_flag = ISC_FALSE; isc_boolean_t did_flag = ISC_FALSE;
isc_result_t result; isc_result_t result;
dns_rdataset_t *opt, *tsig = NULL; dns_messagetextflag_t flags;
dns_name_t *tsigname; isc_buffer_t *buf;
UNUSED (query); UNUSED (query);
debug ("printmessage(%s)",headers?"headers":"noheaders");
/* /*
* Exitcode 9 means we timed out, but if we're printing a message, * Exitcode 9 means we timed out, but if we're printing a message,
* we much have recovered. Go ahead and reset it to code 0, and * we much have recovered. Go ahead and reset it to code 0, and
@@ -359,18 +307,28 @@ printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) {
if (exitcode == 9) if (exitcode == 9)
exitcode = 0; exitcode = 0;
flags = 0;
if (!headers) {
flags |= DNS_MESSAGETEXTFLAG_NOHEADERS;
flags |= DNS_MESSAGETEXTFLAG_NOCOMMENTS;
}
if (!query->lookup->comments)
flags |= DNS_MESSAGETEXTFLAG_NOCOMMENTS;
result = ISC_R_SUCCESS; result = ISC_R_SUCCESS;
if (query->lookup->comments && !short_form && result = isc_buffer_allocate(mctx, &buf, OUTPUTBUF);
!query->lookup->doing_xfr) { check_result (result, "isc_buffer_allocate");
if (msg == query->lookup->sendmsg)
printf (";; Sending:\n");
else
printf (";; Got answer:\n");
}
if (headers) { if (query->lookup->comments && !short_form) {
if (query->lookup->comments && !short_form) { if (!query->lookup->doing_xfr) {
if (msg == query->lookup->sendmsg)
printf (";; Sending:\n");
else
printf (";; Got answer:\n");
}
if (headers) {
printf(";; ->>HEADER<<- opcode: %s, status: %s, " printf(";; ->>HEADER<<- opcode: %s, status: %s, "
"id: %u\n", "id: %u\n",
opcodetext[msg->opcode], rcodetext[msg->rcode], opcodetext[msg->opcode], rcodetext[msg->rcode],
@@ -411,67 +369,66 @@ printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) {
msg->counts[DNS_SECTION_AUTHORITY], msg->counts[DNS_SECTION_AUTHORITY],
msg->counts[DNS_SECTION_ADDITIONAL]); msg->counts[DNS_SECTION_ADDITIONAL]);
opt = dns_message_getopt(msg); result = dns_message_pseudosectiontotext(msg,
if (opt != NULL) DNS_PSEUDOSECTION_OPT,
printf(";; EDNS: version: %u, udp=%u\n", flags, buf);
(unsigned int)((opt->ttl & check_result(result,
0x00ff0000) >> 16), "dns_message_pseudosectiontotext");
(unsigned int)opt->rdclass);
tsigname = NULL;
tsig = dns_message_gettsig(msg, &tsigname);
if (tsig != NULL)
printf(";; PSEUDOSECTIONS: TSIG\n");
} }
} }
if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_QUESTION]) &&
headers && query->lookup->section_question) { if (query->lookup->section_question && headers) {
printf("\n"); if (!short_form) {
result = printsection(msg, DNS_SECTION_QUESTION, "QUESTION", result = dns_message_sectiontotext(msg,
ISC_TRUE, query); DNS_SECTION_QUESTION,
if (result != ISC_R_SUCCESS) flags, buf);
return (result); check_result(result, "dns_message_sectiontotext");
} }
if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_ANSWER]) && }
query->lookup->section_answer ) { if (query->lookup->section_answer) {
if (headers && query->lookup->comments && !short_form) if (!short_form) {
printf("\n"); result = dns_message_sectiontotext(msg,
result = printsection(msg, DNS_SECTION_ANSWER, "ANSWER", DNS_SECTION_ANSWER,
headers, query); flags, buf);
if (result != ISC_R_SUCCESS) check_result(result, "dns_message_sectiontotext");
return (result); }
} else {
if ((! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_AUTHORITY]) && result = short_answer(msg, flags, buf, query);
headers && query->lookup->section_authority) || check_result (result, "short_answer");
( ISC_LIST_EMPTY(msg->sections[DNS_SECTION_ANSWER]) && }
headers && query->lookup->section_answer && }
query->lookup->trace )) { if (query->lookup->section_authority) {
if (headers && query->lookup->comments && !short_form) if (!short_form) {
printf("\n"); result = dns_message_sectiontotext(msg,
result = printsection(msg, DNS_SECTION_AUTHORITY, "AUTHORITY", DNS_SECTION_AUTHORITY,
ISC_TRUE, query); flags, buf);
if (result != ISC_R_SUCCESS) check_result(result, "dns_message_sectiontotext");
return (result); }
} }
if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_ADDITIONAL]) && if (query->lookup->section_additional) {
headers && query->lookup->section_additional) { if (!short_form) {
if (headers && query->lookup->comments && !short_form) result = dns_message_sectiontotext(msg,
printf("\n"); DNS_SECTION_ADDITIONAL,
result = printsection(msg, DNS_SECTION_ADDITIONAL, flags, buf);
"ADDITIONAL", ISC_TRUE, query); check_result(result, "dns_message_sectiontotext");
if (result != ISC_R_SUCCESS) result = dns_message_pseudosectiontotext(msg,
return (result); DNS_PSEUDOSECTION_TSIG,
} flags, buf);
if ((tsig != NULL) && headers && query->lookup->section_additional) { check_result(result, "dns_message_pseudosectiontotext");
if (headers && query->lookup->comments && !short_form) result = dns_message_pseudosectiontotext(msg,
printf("\n"); DNS_PSEUDOSECTION_SIG0,
result = printrdata(msg, tsig, tsigname, flags, buf);
"PSEUDOSECTION TSIG", ISC_TRUE);
if (result != ISC_R_SUCCESS) check_result(result,
return (result); "dns_message_pseudosectiontotext");
} }
}
if (headers && query->lookup->comments && !short_form) if (headers && query->lookup->comments && !short_form)
printf("\n"); printf("\n");
printf ("%.*s",(int)isc_buffer_usedlength(buf),
(char *)isc_buffer_base(buf));
isc_buffer_free(&buf);
return (result); return (result);
} }

View File

@@ -35,6 +35,7 @@
#define BUFSIZE 512 #define BUFSIZE 512
#define COMMSIZE 32767 #define COMMSIZE 32767
#define RESOLVCONF "/etc/resolv.conf" #define RESOLVCONF "/etc/resolv.conf"
#define OUTPUTBUF 32767
#define LOOKUP_LIMIT 64 #define LOOKUP_LIMIT 64
/* Lookup_limit is just a limiter, keeping too many lookups from being /* Lookup_limit is just a limiter, keeping too many lookups from being
* created. It's job is mainly to prevent the program from running away * created. It's job is mainly to prevent the program from running away