1 /* $NetBSD: printmsg.c,v 1.4 2014/12/10 04:37:53 christos Exp $ */
4 * Copyright (C) 2004, 2007, 2011 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.31 2011/08/25 23:46:42 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
;
85 isc_boolean_t no_rdata
;
87 if (sectionid
== DNS_SECTION_QUESTION
)
92 printf(";; %s SECTION:\n", section_name
);
94 dns_name_init(&empty_name
, NULL
);
96 result
= dns_message_firstname(msg
, sectionid
);
97 if (result
== ISC_R_NOMORE
)
98 return (ISC_R_SUCCESS
);
99 else if (result
!= ISC_R_SUCCESS
)
104 dns_message_currentname(msg
, sectionid
, &name
);
106 isc_buffer_init(&target
, t
, sizeof(t
));
112 for (rdataset
= ISC_LIST_HEAD(name
->list
);
114 rdataset
= ISC_LIST_NEXT(rdataset
, link
)) {
115 result
= dns_rdataset_totext(rdataset
,
120 if (result
!= ISC_R_SUCCESS
)
124 print_name
= &empty_name
;
129 isc_buffer_usedregion(&target
, &r
);
130 printf("%.*s", (int)r
.length
, (char *)r
.base
);
132 result
= dns_message_nextname(msg
, sectionid
);
133 if (result
== ISC_R_NOMORE
)
135 else if (result
!= ISC_R_SUCCESS
)
139 return (ISC_R_SUCCESS
);
143 printrdata(dns_message_t
*msg
, dns_rdataset_t
*rdataset
, dns_name_t
*owner
,
144 const char *set_name
)
152 printf(";; %s SECTION:\n", set_name
);
154 isc_buffer_init(&target
, t
, sizeof(t
));
156 result
= dns_rdataset_totext(rdataset
, owner
, ISC_FALSE
, ISC_FALSE
,
158 if (result
!= ISC_R_SUCCESS
)
160 isc_buffer_usedregion(&target
, &r
);
161 printf("%.*s", (int)r
.length
, (char *)r
.base
);
163 return (ISC_R_SUCCESS
);
167 printmessage(dns_message_t
*msg
) {
169 dns_rdataset_t
*opt
, *tsig
;
170 dns_name_t
*tsigname
;
172 result
= ISC_R_SUCCESS
;
174 printf(";; ->>HEADER<<- opcode: %s, status: %s, id: %u\n",
175 opcodetext
[msg
->opcode
], rcodetext
[msg
->rcode
], msg
->id
);
178 if ((msg
->flags
& DNS_MESSAGEFLAG_QR
) != 0)
180 if ((msg
->flags
& DNS_MESSAGEFLAG_AA
) != 0)
182 if ((msg
->flags
& DNS_MESSAGEFLAG_TC
) != 0)
184 if ((msg
->flags
& DNS_MESSAGEFLAG_RD
) != 0)
186 if ((msg
->flags
& DNS_MESSAGEFLAG_RA
) != 0)
188 if ((msg
->flags
& DNS_MESSAGEFLAG_AD
) != 0)
190 if ((msg
->flags
& DNS_MESSAGEFLAG_CD
) != 0)
192 printf("; QUERY: %u, ANSWER: %u, AUTHORITY: %u, ADDITIONAL: %u\n",
193 msg
->counts
[DNS_SECTION_QUESTION
],
194 msg
->counts
[DNS_SECTION_ANSWER
],
195 msg
->counts
[DNS_SECTION_AUTHORITY
],
196 msg
->counts
[DNS_SECTION_ADDITIONAL
]);
197 opt
= dns_message_getopt(msg
);
199 printf(";; EDNS: version: %u, udp=%u\n",
200 (unsigned int)((opt
->ttl
& 0x00ff0000) >> 16),
201 (unsigned int)opt
->rdclass
);
204 tsig
= dns_message_gettsig(msg
, &tsigname
);
206 printf(";; PSEUDOSECTIONS: TSIG\n");
207 if (! ISC_LIST_EMPTY(msg
->sections
[DNS_SECTION_QUESTION
])) {
209 result
= printsection(msg
, DNS_SECTION_QUESTION
, "QUESTION");
210 if (result
!= ISC_R_SUCCESS
)
213 if (! ISC_LIST_EMPTY(msg
->sections
[DNS_SECTION_ANSWER
])) {
215 result
= printsection(msg
, DNS_SECTION_ANSWER
, "ANSWER");
216 if (result
!= ISC_R_SUCCESS
)
219 if (! ISC_LIST_EMPTY(msg
->sections
[DNS_SECTION_AUTHORITY
])) {
221 result
= printsection(msg
, DNS_SECTION_AUTHORITY
, "AUTHORITY");
222 if (result
!= ISC_R_SUCCESS
)
225 if (! ISC_LIST_EMPTY(msg
->sections
[DNS_SECTION_ADDITIONAL
])) {
227 result
= printsection(msg
, DNS_SECTION_ADDITIONAL
,
229 if (result
!= ISC_R_SUCCESS
)
234 result
= printrdata(msg
, tsig
, tsigname
,
235 "PSEUDOSECTION TSIG");
236 if (result
!= ISC_R_SUCCESS
)