1999-01-06 05:42:58 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1999 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <isc/assertions.h>
|
1999-04-30 00:17:15 +00:00
|
|
|
#include <isc/error.h>
|
1999-01-06 05:42:58 +00:00
|
|
|
#include <isc/boolean.h>
|
|
|
|
#include <isc/region.h>
|
|
|
|
|
|
|
|
#include <dns/types.h>
|
|
|
|
#include <dns/result.h>
|
|
|
|
#include <dns/name.h>
|
1999-01-13 19:11:26 +00:00
|
|
|
#include <dns/rdata.h>
|
1999-01-20 07:49:50 +00:00
|
|
|
#include <dns/rdataclass.h>
|
|
|
|
#include <dns/rdatatype.h>
|
1999-01-15 03:30:36 +00:00
|
|
|
#include <dns/rdatalist.h>
|
|
|
|
#include <dns/rdataset.h>
|
1999-01-06 20:05:09 +00:00
|
|
|
#include <dns/compress.h>
|
1999-04-30 00:17:15 +00:00
|
|
|
#include <dns/message.h>
|
1999-01-13 19:11:26 +00:00
|
|
|
|
1999-01-06 20:05:09 +00:00
|
|
|
dns_decompress_t dctx;
|
1999-01-06 05:42:58 +00:00
|
|
|
|
1999-01-19 19:50:10 +00:00
|
|
|
dns_result_t printmessage(dns_message_t *message);
|
|
|
|
|
1999-04-30 00:17:15 +00:00
|
|
|
static inline void
|
|
|
|
CHECKRESULT(dns_result_t result, char *msg)
|
|
|
|
{
|
|
|
|
if (result != DNS_R_SUCCESS) {
|
|
|
|
printf("%s: %s\n", msg, dns_result_totext(result));
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-01-13 19:50:05 +00:00
|
|
|
#ifdef NOISY
|
1999-01-06 05:42:58 +00:00
|
|
|
static void
|
|
|
|
print_wirename(isc_region_t *name) {
|
|
|
|
unsigned char *ccurr, *cend;
|
|
|
|
|
|
|
|
ccurr = name->base;
|
|
|
|
cend = ccurr + name->length;
|
|
|
|
while (ccurr != cend)
|
|
|
|
printf("%02x ", *ccurr++);
|
|
|
|
printf("\n");
|
|
|
|
}
|
1999-01-13 19:50:05 +00:00
|
|
|
#endif
|
1999-01-06 05:42:58 +00:00
|
|
|
|
1999-04-30 05:42:06 +00:00
|
|
|
#ifndef NOMAIN
|
1999-01-06 05:42:58 +00:00
|
|
|
static int
|
|
|
|
fromhex(char c) {
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
return (c - '0');
|
|
|
|
else if (c >= 'a' && c <= 'f')
|
|
|
|
return (c - 'a' + 10);
|
|
|
|
else if (c >= 'A' && c <= 'F')
|
|
|
|
return (c - 'A' + 10);
|
1999-02-11 06:38:12 +00:00
|
|
|
|
1999-01-06 05:42:58 +00:00
|
|
|
printf("bad input format: %02x\n", c);
|
|
|
|
exit(3);
|
1999-02-11 06:38:12 +00:00
|
|
|
/* NOTREACHED */
|
1999-01-06 05:42:58 +00:00
|
|
|
}
|
1999-04-30 05:42:06 +00:00
|
|
|
#endif
|
1999-01-06 05:42:58 +00:00
|
|
|
|
1999-01-13 19:11:26 +00:00
|
|
|
static char *opcodetext[] = {
|
|
|
|
"QUERY",
|
|
|
|
"IQUERY",
|
|
|
|
"STATUS",
|
|
|
|
"RESERVED3",
|
|
|
|
"NOTIFY",
|
|
|
|
"UPDATE",
|
|
|
|
"RESERVED6",
|
|
|
|
"RESERVED7",
|
|
|
|
"RESERVED8",
|
|
|
|
"RESERVED9",
|
|
|
|
"RESERVED10",
|
|
|
|
"RESERVED11",
|
|
|
|
"RESERVED12",
|
|
|
|
"RESERVED13",
|
|
|
|
"RESERVED14",
|
|
|
|
"RESERVED15"
|
|
|
|
};
|
|
|
|
|
|
|
|
static char *rcodetext[] = {
|
|
|
|
"NOERROR",
|
|
|
|
"FORMERR",
|
|
|
|
"SERVFAIL",
|
|
|
|
"NXDOMAIN",
|
|
|
|
"NOTIMPL",
|
|
|
|
"REFUSED",
|
|
|
|
"YXDOMAIN",
|
|
|
|
"YXRRSET",
|
|
|
|
"NXRRSET",
|
|
|
|
"NOTAUTH",
|
|
|
|
"NOTZONE",
|
|
|
|
"RESERVED11",
|
|
|
|
"RESERVED12",
|
|
|
|
"RESERVED13",
|
|
|
|
"RESERVED14",
|
|
|
|
"RESERVED15"
|
|
|
|
};
|
|
|
|
|
1999-01-15 19:36:33 +00:00
|
|
|
static dns_result_t
|
1999-04-30 00:17:15 +00:00
|
|
|
printsection(dns_message_t *msg, dns_section_t sectionid, char *section_name)
|
|
|
|
{
|
1999-01-15 20:02:22 +00:00
|
|
|
dns_name_t *name, *print_name;
|
1999-04-30 00:17:15 +00:00
|
|
|
dns_rdataset_t *rdataset;
|
1999-01-13 19:11:26 +00:00
|
|
|
isc_buffer_t target;
|
|
|
|
dns_result_t result;
|
1999-01-15 19:36:33 +00:00
|
|
|
isc_region_t r;
|
1999-01-15 20:02:22 +00:00
|
|
|
dns_name_t empty_name;
|
1999-01-15 19:36:33 +00:00
|
|
|
char t[1000];
|
1999-01-15 20:02:22 +00:00
|
|
|
isc_boolean_t first;
|
1999-04-30 00:17:15 +00:00
|
|
|
isc_boolean_t no_rdata;
|
|
|
|
|
|
|
|
if (sectionid == DNS_SECTION_QUESTION)
|
|
|
|
no_rdata = ISC_TRUE;
|
|
|
|
else
|
|
|
|
no_rdata = ISC_FALSE;
|
1999-01-13 19:11:26 +00:00
|
|
|
|
|
|
|
printf("\n;; %s SECTION:\n", section_name);
|
1999-04-30 00:17:15 +00:00
|
|
|
|
|
|
|
dns_name_init(&empty_name, NULL);
|
|
|
|
|
|
|
|
result = dns_message_firstname(msg, sectionid);
|
|
|
|
if (result == DNS_R_NOMORE)
|
|
|
|
return (DNS_R_SUCCESS);
|
|
|
|
else if (result != DNS_R_SUCCESS)
|
|
|
|
return (result);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
name = NULL;
|
|
|
|
dns_message_currentname(msg, sectionid, &name);
|
|
|
|
|
1999-01-13 19:11:26 +00:00
|
|
|
isc_buffer_init(&target, t, sizeof t, ISC_BUFFERTYPE_TEXT);
|
1999-01-15 20:02:22 +00:00
|
|
|
first = ISC_TRUE;
|
|
|
|
print_name = name;
|
1999-04-30 00:17:15 +00:00
|
|
|
|
|
|
|
for (rdataset = ISC_LIST_HEAD(name->list);
|
|
|
|
rdataset != NULL;
|
|
|
|
rdataset = ISC_LIST_NEXT(rdataset, link)) {
|
|
|
|
result = dns_rdataset_totext(rdataset, print_name,
|
1999-04-30 21:15:02 +00:00
|
|
|
ISC_FALSE, no_rdata,
|
|
|
|
&target);
|
1999-01-15 19:36:33 +00:00
|
|
|
if (result != DNS_R_SUCCESS)
|
|
|
|
return (result);
|
1999-01-15 20:02:22 +00:00
|
|
|
#ifdef USEINITALWS
|
|
|
|
if (first) {
|
|
|
|
print_name = &empty_name;
|
|
|
|
first = ISC_FALSE;
|
|
|
|
}
|
|
|
|
#endif
|
1999-01-13 19:11:26 +00:00
|
|
|
}
|
1999-01-15 19:36:33 +00:00
|
|
|
isc_buffer_used(&target, &r);
|
|
|
|
printf("%.*s", (int)r.length, (char *)r.base);
|
1999-04-30 00:17:15 +00:00
|
|
|
|
|
|
|
result = dns_message_nextname(msg, sectionid);
|
|
|
|
if (result == DNS_R_NOMORE)
|
|
|
|
break;
|
|
|
|
else if (result != DNS_R_SUCCESS)
|
|
|
|
return (result);
|
1999-01-13 19:11:26 +00:00
|
|
|
}
|
1999-01-15 19:36:33 +00:00
|
|
|
|
|
|
|
return (DNS_R_SUCCESS);
|
1999-01-13 19:11:26 +00:00
|
|
|
}
|
|
|
|
|
1999-01-19 06:35:54 +00:00
|
|
|
dns_result_t
|
1999-04-30 00:17:15 +00:00
|
|
|
printmessage(dns_message_t *msg) {
|
1999-01-13 19:11:26 +00:00
|
|
|
isc_boolean_t did_flag = ISC_FALSE;
|
1999-01-15 19:36:33 +00:00
|
|
|
dns_result_t result;
|
1999-01-13 19:11:26 +00:00
|
|
|
|
1999-04-30 00:17:15 +00:00
|
|
|
result = DNS_R_UNEXPECTED;
|
|
|
|
|
1999-01-13 19:11:26 +00:00
|
|
|
printf(";; ->>HEADER<<- opcode: %s, status: %s, id: %u\n",
|
1999-04-30 00:17:15 +00:00
|
|
|
opcodetext[msg->opcode], rcodetext[msg->rcode], msg->id);
|
|
|
|
|
1999-01-13 19:11:26 +00:00
|
|
|
printf(";; flags: ");
|
1999-04-30 00:17:15 +00:00
|
|
|
if ((msg->flags & DNS_MESSAGEFLAG_QR) != 0) {
|
1999-01-13 19:11:26 +00:00
|
|
|
printf("qr");
|
|
|
|
did_flag = ISC_TRUE;
|
|
|
|
}
|
1999-04-30 00:17:15 +00:00
|
|
|
if ((msg->flags & DNS_MESSAGEFLAG_AA) != 0) {
|
1999-01-13 19:11:26 +00:00
|
|
|
printf("%saa", did_flag ? " " : "");
|
|
|
|
did_flag = ISC_TRUE;
|
|
|
|
}
|
1999-04-30 00:17:15 +00:00
|
|
|
if ((msg->flags & DNS_MESSAGEFLAG_TC) != 0) {
|
1999-01-13 19:11:26 +00:00
|
|
|
printf("%stc", did_flag ? " " : "");
|
|
|
|
did_flag = ISC_TRUE;
|
|
|
|
}
|
1999-04-30 00:17:15 +00:00
|
|
|
if ((msg->flags & DNS_MESSAGEFLAG_RD) != 0) {
|
1999-01-13 19:11:26 +00:00
|
|
|
printf("%srd", did_flag ? " " : "");
|
|
|
|
did_flag = ISC_TRUE;
|
|
|
|
}
|
1999-04-30 00:17:15 +00:00
|
|
|
if ((msg->flags & DNS_MESSAGEFLAG_RA) != 0) {
|
1999-01-13 19:11:26 +00:00
|
|
|
printf("%sra", did_flag ? " " : "");
|
|
|
|
did_flag = ISC_TRUE;
|
|
|
|
}
|
1999-01-13 19:58:40 +00:00
|
|
|
printf("; QUERY: %u, ANSWER: %u, AUTHORITY: %u, ADDITIONAL: %u\n",
|
1999-04-30 00:17:15 +00:00
|
|
|
msg->counts[DNS_SECTION_QUESTION],
|
|
|
|
msg->counts[DNS_SECTION_ANSWER],
|
|
|
|
msg->counts[DNS_SECTION_AUTHORITY],
|
|
|
|
msg->counts[DNS_SECTION_ADDITIONAL]);
|
|
|
|
printf("; PSEUDOSECTIONS: OPT: %u, TSIG: %u\n",
|
|
|
|
msg->counts[DNS_SECTION_OPT],
|
|
|
|
msg->counts[DNS_SECTION_TSIG]);
|
|
|
|
|
|
|
|
result = printsection(msg, DNS_SECTION_QUESTION, "QUESTION");
|
|
|
|
if (result != DNS_R_SUCCESS)
|
|
|
|
return (result);
|
|
|
|
result = printsection(msg, DNS_SECTION_ANSWER, "ANSWER");
|
1999-01-15 19:36:33 +00:00
|
|
|
if (result != DNS_R_SUCCESS)
|
|
|
|
return (result);
|
1999-04-30 00:17:15 +00:00
|
|
|
result = printsection(msg, DNS_SECTION_AUTHORITY, "AUTHORITY");
|
|
|
|
if (result != DNS_R_SUCCESS)
|
|
|
|
return (result);
|
|
|
|
result = printsection(msg, DNS_SECTION_ADDITIONAL, "ADDITIONAL");
|
|
|
|
if (result != DNS_R_SUCCESS)
|
|
|
|
return (result);
|
|
|
|
result = printsection(msg, DNS_SECTION_OPT, "PSEUDOSECTION OPT");
|
|
|
|
if (result != DNS_R_SUCCESS)
|
|
|
|
return (result);
|
|
|
|
result = printsection(msg, DNS_SECTION_TSIG, "PSEUDOSECTION TSIG");
|
1999-01-15 19:36:33 +00:00
|
|
|
if (result != DNS_R_SUCCESS)
|
|
|
|
return (result);
|
|
|
|
|
|
|
|
return (result);
|
1999-01-13 19:11:26 +00:00
|
|
|
}
|
|
|
|
|
1999-01-19 06:35:54 +00:00
|
|
|
#ifndef NOMAIN
|
1999-01-06 05:42:58 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[]) {
|
1999-01-13 19:57:13 +00:00
|
|
|
char *rp, *wp;
|
1999-01-06 05:42:58 +00:00
|
|
|
unsigned char *bp;
|
1999-04-30 00:17:15 +00:00
|
|
|
isc_buffer_t source;
|
1999-01-06 05:42:58 +00:00
|
|
|
size_t len, i;
|
|
|
|
int n;
|
|
|
|
FILE *f;
|
|
|
|
isc_boolean_t need_close = ISC_FALSE;
|
1999-01-13 19:11:26 +00:00
|
|
|
unsigned char b[1000];
|
|
|
|
char s[1000];
|
1999-04-30 00:17:15 +00:00
|
|
|
dns_message_t *message;
|
1999-01-15 19:36:33 +00:00
|
|
|
dns_result_t result;
|
1999-04-30 00:17:15 +00:00
|
|
|
isc_mem_t *mctx;
|
|
|
|
|
|
|
|
RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
|
1999-01-06 05:42:58 +00:00
|
|
|
|
|
|
|
if (argc > 1) {
|
|
|
|
f = fopen(argv[1], "r");
|
|
|
|
if (f == NULL) {
|
|
|
|
printf("fopen failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
need_close = ISC_TRUE;
|
|
|
|
} else
|
|
|
|
f = stdin;
|
|
|
|
|
|
|
|
bp = b;
|
|
|
|
while (fgets(s, sizeof s, f) != NULL) {
|
1999-01-13 19:57:13 +00:00
|
|
|
rp = s;
|
|
|
|
wp = s;
|
|
|
|
len = 0;
|
|
|
|
while (*rp != '\0') {
|
|
|
|
if (*rp != ' ' && *rp != '\t' &&
|
|
|
|
*rp != '\r' && *rp != '\n') {
|
|
|
|
*wp++ = *rp;
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
rp++;
|
1999-01-06 05:42:58 +00:00
|
|
|
}
|
|
|
|
if (len == 0)
|
|
|
|
break;
|
|
|
|
if (len % 2 != 0) {
|
|
|
|
printf("bad input format: %d\n", len);
|
|
|
|
exit(1);
|
|
|
|
}
|
1999-01-13 19:57:13 +00:00
|
|
|
if (len > (sizeof b) * 2) {
|
1999-01-06 05:42:58 +00:00
|
|
|
printf("input too long\n");
|
|
|
|
exit(2);
|
|
|
|
}
|
1999-01-13 19:57:13 +00:00
|
|
|
rp = s;
|
1999-01-06 05:42:58 +00:00
|
|
|
for (i = 0; i < len; i += 2) {
|
1999-01-13 19:57:13 +00:00
|
|
|
n = fromhex(*rp++);
|
1999-01-06 05:42:58 +00:00
|
|
|
n *= 16;
|
1999-01-13 19:57:13 +00:00
|
|
|
n += fromhex(*rp++);
|
1999-01-06 05:42:58 +00:00
|
|
|
*bp++ = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (need_close)
|
|
|
|
fclose(f);
|
1999-01-06 20:05:09 +00:00
|
|
|
|
1999-01-13 19:11:26 +00:00
|
|
|
isc_buffer_init(&source, b, sizeof b, ISC_BUFFERTYPE_BINARY);
|
1999-01-06 05:42:58 +00:00
|
|
|
isc_buffer_add(&source, bp - b);
|
1999-04-30 00:17:15 +00:00
|
|
|
|
|
|
|
result = dns_message_create(mctx, &message, DNS_MESSAGE_INTENT_PARSE);
|
|
|
|
CHECKRESULT(result, "dns_message_create failed");
|
|
|
|
|
|
|
|
result = dns_message_parse(message, &source);
|
|
|
|
CHECKRESULT(result, "dns_message_parse failed");
|
|
|
|
|
|
|
|
result = printmessage(message);
|
|
|
|
CHECKRESULT(result, "printmessage() failed");
|
|
|
|
|
1999-04-30 21:52:40 +00:00
|
|
|
isc_mem_stats(mctx, stdout);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXXMLG
|
|
|
|
* Changing this here is a hack, and should not be done in reasonable
|
|
|
|
* application code, ever.
|
|
|
|
*/
|
|
|
|
message->from_to_wire = DNS_MESSAGE_INTENT_RENDER;
|
|
|
|
memset(&b, 0, sizeof(b));
|
|
|
|
isc_buffer_clear(&source);
|
|
|
|
|
1999-04-30 22:35:49 +00:00
|
|
|
for (i = 0 ; i < DNS_SECTION_MAX ; i++)
|
|
|
|
message->counts[i] = 0; /* Another hack XXX */
|
|
|
|
|
1999-04-30 21:52:40 +00:00
|
|
|
result = dns_message_renderbegin(message, &source);
|
|
|
|
CHECKRESULT(result, "dns_message_renderbegin() failed");
|
|
|
|
|
|
|
|
result = dns_message_rendersection(message, DNS_SECTION_QUESTION,
|
|
|
|
0, 0);
|
|
|
|
CHECKRESULT(result, "dns_message_rendersection(QUESTION) failed");
|
|
|
|
|
|
|
|
result = dns_message_rendersection(message, DNS_SECTION_ANSWER,
|
|
|
|
0, 0);
|
|
|
|
CHECKRESULT(result, "dns_message_rendersection(ANSWER) failed");
|
|
|
|
|
|
|
|
result = dns_message_rendersection(message, DNS_SECTION_AUTHORITY,
|
|
|
|
0, 0);
|
|
|
|
CHECKRESULT(result, "dns_message_rendersection(AUTHORITY) failed");
|
|
|
|
|
|
|
|
result = dns_message_rendersection(message, DNS_SECTION_ADDITIONAL,
|
|
|
|
0, 0);
|
|
|
|
CHECKRESULT(result, "dns_message_rendersection(ADDITIONAL) failed");
|
|
|
|
|
|
|
|
result = dns_message_rendersection(message, DNS_SECTION_OPT,
|
|
|
|
0, 0);
|
|
|
|
CHECKRESULT(result, "dns_message_rendersection(OPT) failed");
|
|
|
|
|
|
|
|
result = dns_message_rendersection(message, DNS_SECTION_TSIG,
|
|
|
|
0, 0);
|
|
|
|
CHECKRESULT(result, "dns_message_rendersection(TSIG) failed");
|
|
|
|
|
|
|
|
dns_message_renderend(message);
|
|
|
|
|
|
|
|
message->from_to_wire = DNS_MESSAGE_INTENT_PARSE;
|
|
|
|
dns_message_destroy(&message);
|
|
|
|
|
|
|
|
isc_mem_stats(mctx, stdout);
|
|
|
|
|
|
|
|
result = dns_message_create(mctx, &message, DNS_MESSAGE_INTENT_PARSE);
|
|
|
|
CHECKRESULT(result, "dns_message_create failed");
|
|
|
|
|
|
|
|
result = dns_message_parse(message, &source);
|
|
|
|
CHECKRESULT(result, "dns_message_parse failed");
|
|
|
|
|
|
|
|
result = printmessage(message);
|
|
|
|
CHECKRESULT(result, "printmessage() failed");
|
|
|
|
|
1999-04-30 00:17:15 +00:00
|
|
|
dns_message_destroy(&message);
|
|
|
|
|
|
|
|
isc_mem_stats(mctx, stdout);
|
|
|
|
isc_mem_destroy(&mctx);
|
1999-01-13 19:11:26 +00:00
|
|
|
|
1999-01-06 05:42:58 +00:00
|
|
|
return (0);
|
|
|
|
}
|
1999-01-19 06:35:54 +00:00
|
|
|
#endif /* !NOMAIN */
|