4 * Copyright (C) 2004, 2007 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1998-2001 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: printmsg.c,v 1.29 2007/06/19 23:46:59 tbox Exp */
24 #include <isc/buffer.h>
28 #include <dns/rdataset.h>
32 static const char *opcodetext
[] = {
51 static const char *rcodetext
[] = {
72 printsection(dns_message_t
*msg
, dns_section_t sectionid
,
73 const char *section_name
)
75 dns_name_t
*name
, *print_name
;
76 dns_rdataset_t
*rdataset
;
80 dns_name_t empty_name
;
83 isc_boolean_t no_rdata
;
85 if (sectionid
== DNS_SECTION_QUESTION
)
90 printf(";; %s SECTION:\n", section_name
);
92 dns_name_init(&empty_name
, NULL
);
94 result
= dns_message_firstname(msg
, sectionid
);
95 if (result
== ISC_R_NOMORE
)
96 return (ISC_R_SUCCESS
);
97 else if (result
!= ISC_R_SUCCESS
)
102 dns_message_currentname(msg
, sectionid
, &name
);
104 isc_buffer_init(&target
, t
, sizeof(t
));
108 for (rdataset
= ISC_LIST_HEAD(name
->list
);
110 rdataset
= ISC_LIST_NEXT(rdataset
, link
)) {
111 result
= dns_rdataset_totext(rdataset
,
116 if (result
!= ISC_R_SUCCESS
)
120 print_name
= &empty_name
;
125 isc_buffer_usedregion(&target
, &r
);
126 printf("%.*s", (int)r
.length
, (char *)r
.base
);
128 result
= dns_message_nextname(msg
, sectionid
);
129 if (result
== ISC_R_NOMORE
)
131 else if (result
!= ISC_R_SUCCESS
)
135 return (ISC_R_SUCCESS
);
139 printrdata(dns_message_t
*msg
, dns_rdataset_t
*rdataset
, dns_name_t
*owner
,
140 const char *set_name
)
148 printf(";; %s SECTION:\n", set_name
);
150 isc_buffer_init(&target
, t
, sizeof(t
));
152 result
= dns_rdataset_totext(rdataset
, owner
, ISC_FALSE
, ISC_FALSE
,
154 if (result
!= ISC_R_SUCCESS
)
156 isc_buffer_usedregion(&target
, &r
);
157 printf("%.*s", (int)r
.length
, (char *)r
.base
);
159 return (ISC_R_SUCCESS
);
163 printmessage(dns_message_t
*msg
) {
164 isc_boolean_t did_flag
= ISC_FALSE
;
166 dns_rdataset_t
*opt
, *tsig
;
167 dns_name_t
*tsigname
;
169 result
= ISC_R_SUCCESS
;
171 printf(";; ->>HEADER<<- opcode: %s, status: %s, id: %u\n",
172 opcodetext
[msg
->opcode
], rcodetext
[msg
->rcode
], msg
->id
);
174 printf(";; flags: ");
175 if ((msg
->flags
& DNS_MESSAGEFLAG_QR
) != 0) {
179 if ((msg
->flags
& DNS_MESSAGEFLAG_AA
) != 0) {
180 printf("%saa", did_flag
? " " : "");
183 if ((msg
->flags
& DNS_MESSAGEFLAG_TC
) != 0) {
184 printf("%stc", did_flag
? " " : "");
187 if ((msg
->flags
& DNS_MESSAGEFLAG_RD
) != 0) {
188 printf("%srd", did_flag
? " " : "");
191 if ((msg
->flags
& DNS_MESSAGEFLAG_RA
) != 0) {
192 printf("%sra", did_flag
? " " : "");
195 if ((msg
->flags
& DNS_MESSAGEFLAG_AD
) != 0) {
196 printf("%sad", did_flag
? " " : "");
199 if ((msg
->flags
& DNS_MESSAGEFLAG_CD
) != 0) {
200 printf("%scd", did_flag
? " " : "");
203 printf("; QUERY: %u, ANSWER: %u, AUTHORITY: %u, ADDITIONAL: %u\n",
204 msg
->counts
[DNS_SECTION_QUESTION
],
205 msg
->counts
[DNS_SECTION_ANSWER
],
206 msg
->counts
[DNS_SECTION_AUTHORITY
],
207 msg
->counts
[DNS_SECTION_ADDITIONAL
]);
208 opt
= dns_message_getopt(msg
);
210 printf(";; EDNS: version: %u, udp=%u\n",
211 (unsigned int)((opt
->ttl
& 0x00ff0000) >> 16),
212 (unsigned int)opt
->rdclass
);
215 tsig
= dns_message_gettsig(msg
, &tsigname
);
217 printf(";; PSEUDOSECTIONS: TSIG\n");
218 if (! ISC_LIST_EMPTY(msg
->sections
[DNS_SECTION_QUESTION
])) {
220 result
= printsection(msg
, DNS_SECTION_QUESTION
, "QUESTION");
221 if (result
!= ISC_R_SUCCESS
)
224 if (! ISC_LIST_EMPTY(msg
->sections
[DNS_SECTION_ANSWER
])) {
226 result
= printsection(msg
, DNS_SECTION_ANSWER
, "ANSWER");
227 if (result
!= ISC_R_SUCCESS
)
230 if (! ISC_LIST_EMPTY(msg
->sections
[DNS_SECTION_AUTHORITY
])) {
232 result
= printsection(msg
, DNS_SECTION_AUTHORITY
, "AUTHORITY");
233 if (result
!= ISC_R_SUCCESS
)
236 if (! ISC_LIST_EMPTY(msg
->sections
[DNS_SECTION_ADDITIONAL
])) {
238 result
= printsection(msg
, DNS_SECTION_ADDITIONAL
,
240 if (result
!= ISC_R_SUCCESS
)
245 result
= printrdata(msg
, tsig
, tsigname
,
246 "PSEUDOSECTION TSIG");
247 if (result
!= ISC_R_SUCCESS
)