printmsg.c revision ca41b452ede6feaa9d8739ec3cae19389a7b0d03
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews/*
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews * Copyright (C) 1998, 1999, 2000 Internet Software Consortium.
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews *
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews * Permission to use, copy, modify, and distribute this software for any
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews * purpose with or without fee is hereby granted, provided that the above
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews * copyright notice and this permission notice appear in all copies.
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews *
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews * SOFTWARE.
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews */
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#include <config.h>
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#include <ctype.h>
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#include <stdio.h>
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#include <stdlib.h>
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#include <string.h>
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#include <isc/assertions.h>
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#include <isc/error.h>
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#include <isc/boolean.h>
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#include <isc/region.h>
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#include <dns/types.h>
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#include <dns/result.h>
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#include <dns/name.h>
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#include <dns/rdata.h>
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#include <dns/rdataclass.h>
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#include <dns/rdatatype.h>
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#include <dns/rdatalist.h>
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#include <dns/rdataset.h>
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#include <dns/compress.h>
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#include <dns/message.h>
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#include "printmsg.h"
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrewsstatic char *opcodetext[] = {
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "QUERY",
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "IQUERY",
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "STATUS",
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "RESERVED3",
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "NOTIFY",
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "UPDATE",
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "RESERVED6",
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "RESERVED7",
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "RESERVED8",
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "RESERVED9",
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "RESERVED10",
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "RESERVED11",
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "RESERVED12",
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "RESERVED13",
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "RESERVED14",
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "RESERVED15"
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews};
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrewsstatic char *rcodetext[] = {
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "NOERROR",
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "FORMERR",
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "SERVFAIL",
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "NXDOMAIN",
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "NOTIMPL",
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "REFUSED",
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "YXDOMAIN",
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "YXRRSET",
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "NXRRSET",
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "NOTAUTH",
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "NOTZONE",
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "RESERVED11",
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "RESERVED12",
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "RESERVED13",
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "RESERVED14",
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "RESERVED15",
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews "BADVERS"
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews};
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrewsstatic isc_result_t
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrewsprintsection(dns_message_t *msg, dns_section_t sectionid, char *section_name)
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews{
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews dns_name_t *name, *print_name;
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews dns_rdataset_t *rdataset;
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews isc_buffer_t target;
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews isc_result_t result;
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews isc_region_t r;
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews dns_name_t empty_name;
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews char t[4096];
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews isc_boolean_t first;
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews isc_boolean_t no_rdata;
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews if (sectionid == DNS_SECTION_QUESTION)
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews no_rdata = ISC_TRUE;
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews else
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews no_rdata = ISC_FALSE;
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews printf(";; %s SECTION:\n", section_name);
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews dns_name_init(&empty_name, NULL);
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews result = dns_message_firstname(msg, sectionid);
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews if (result == DNS_R_NOMORE)
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews return (DNS_R_SUCCESS);
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews else if (result != DNS_R_SUCCESS)
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews return (result);
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews for (;;) {
name = NULL;
dns_message_currentname(msg, sectionid, &name);
isc_buffer_init(&target, t, sizeof t, ISC_BUFFERTYPE_TEXT);
first = ISC_TRUE;
print_name = name;
for (rdataset = ISC_LIST_HEAD(name->list);
rdataset != NULL;
rdataset = ISC_LIST_NEXT(rdataset, link)) {
result = dns_rdataset_totext(rdataset,
print_name,
ISC_FALSE,
no_rdata,
&target);
if (result != DNS_R_SUCCESS)
return (result);
#ifdef USEINITALWS
if (first) {
print_name = &empty_name;
first = ISC_FALSE;
}
#endif
}
isc_buffer_used(&target, &r);
printf("%.*s", (int)r.length, (char *)r.base);
result = dns_message_nextname(msg, sectionid);
if (result == DNS_R_NOMORE)
break;
else if (result != DNS_R_SUCCESS)
return (result);
}
return (DNS_R_SUCCESS);
}
isc_result_t
printmessage(dns_message_t *msg) {
isc_boolean_t did_flag = ISC_FALSE;
isc_result_t result;
dns_rdataset_t *opt;
result = DNS_R_SUCCESS;
printf(";; ->>HEADER<<- opcode: %s, status: %s, id: %u\n",
opcodetext[msg->opcode], rcodetext[msg->rcode], msg->id);
printf(";; flags: ");
if ((msg->flags & DNS_MESSAGEFLAG_QR) != 0) {
printf("qr");
did_flag = ISC_TRUE;
}
if ((msg->flags & DNS_MESSAGEFLAG_AA) != 0) {
printf("%saa", did_flag ? " " : "");
did_flag = ISC_TRUE;
}
if ((msg->flags & DNS_MESSAGEFLAG_TC) != 0) {
printf("%stc", did_flag ? " " : "");
did_flag = ISC_TRUE;
}
if ((msg->flags & DNS_MESSAGEFLAG_RD) != 0) {
printf("%srd", did_flag ? " " : "");
did_flag = ISC_TRUE;
}
if ((msg->flags & DNS_MESSAGEFLAG_RA) != 0) {
printf("%sra", did_flag ? " " : "");
did_flag = ISC_TRUE;
}
if ((msg->flags & DNS_MESSAGEFLAG_AD) != 0) {
printf("%sad", did_flag ? " " : "");
did_flag = ISC_TRUE;
}
if ((msg->flags & DNS_MESSAGEFLAG_CD) != 0) {
printf("%scd", did_flag ? " " : "");
did_flag = ISC_TRUE;
}
printf("; QUERY: %u, ANSWER: %u, AUTHORITY: %u, ADDITIONAL: %u\n",
msg->counts[DNS_SECTION_QUESTION],
msg->counts[DNS_SECTION_ANSWER],
msg->counts[DNS_SECTION_AUTHORITY],
msg->counts[DNS_SECTION_ADDITIONAL]);
opt = dns_message_getopt(msg);
if (opt != NULL)
printf(";; EDNS: version: %u, udp=%u\n",
(unsigned int)((opt->ttl & 0x00ff0000) >> 16),
(unsigned int)opt->rdclass);
if (msg->counts[DNS_SECTION_TSIG] > 0)
printf(";; PSEUDOSECTIONS: TSIG: %u\n",
msg->counts[DNS_SECTION_TSIG]);
if (msg->counts[DNS_SECTION_QUESTION] > 0) {
printf("\n");
result = printsection(msg, DNS_SECTION_QUESTION, "QUESTION");
if (result != DNS_R_SUCCESS)
return (result);
}
if (msg->counts[DNS_SECTION_ANSWER] > 0) {
printf("\n");
result = printsection(msg, DNS_SECTION_ANSWER, "ANSWER");
if (result != DNS_R_SUCCESS)
return (result);
}
if (msg->counts[DNS_SECTION_AUTHORITY] > 0) {
printf("\n");
result = printsection(msg, DNS_SECTION_AUTHORITY, "AUTHORITY");
if (result != DNS_R_SUCCESS)
return (result);
}
if (msg->counts[DNS_SECTION_ADDITIONAL] > 0) {
printf("\n");
result = printsection(msg, DNS_SECTION_ADDITIONAL,
"ADDITIONAL");
if (result != DNS_R_SUCCESS)
return (result);
}
if (msg->counts[DNS_SECTION_TSIG] > 0) {
printf("\n");
result = printsection(msg, DNS_SECTION_TSIG,
"PSEUDOSECTION TSIG");
if (result != DNS_R_SUCCESS)
return (result);
}
printf("\n");
return (result);
}