No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / bind / dist / bin / tests / printmsg.c
blob6811c532d75f4fa0e5c6767644e1a4407f266721
1 /* $NetBSD$ */
3 /*
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 */
22 #include <config.h>
24 #include <isc/buffer.h>
25 #include <isc/util.h>
27 #include <dns/name.h>
28 #include <dns/rdataset.h>
30 #include "printmsg.h"
32 static const char *opcodetext[] = {
33 "QUERY",
34 "IQUERY",
35 "STATUS",
36 "RESERVED3",
37 "NOTIFY",
38 "UPDATE",
39 "RESERVED6",
40 "RESERVED7",
41 "RESERVED8",
42 "RESERVED9",
43 "RESERVED10",
44 "RESERVED11",
45 "RESERVED12",
46 "RESERVED13",
47 "RESERVED14",
48 "RESERVED15"
51 static const char *rcodetext[] = {
52 "NOERROR",
53 "FORMERR",
54 "SERVFAIL",
55 "NXDOMAIN",
56 "NOTIMP",
57 "REFUSED",
58 "YXDOMAIN",
59 "YXRRSET",
60 "NXRRSET",
61 "NOTAUTH",
62 "NOTZONE",
63 "RESERVED11",
64 "RESERVED12",
65 "RESERVED13",
66 "RESERVED14",
67 "RESERVED15",
68 "BADVERS"
71 static isc_result_t
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;
77 isc_buffer_t target;
78 isc_result_t result;
79 isc_region_t r;
80 dns_name_t empty_name;
81 char t[65536];
82 isc_boolean_t first;
83 isc_boolean_t no_rdata;
85 if (sectionid == DNS_SECTION_QUESTION)
86 no_rdata = ISC_TRUE;
87 else
88 no_rdata = ISC_FALSE;
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)
98 return (result);
100 for (;;) {
101 name = NULL;
102 dns_message_currentname(msg, sectionid, &name);
104 isc_buffer_init(&target, t, sizeof(t));
105 first = ISC_TRUE;
106 print_name = name;
108 for (rdataset = ISC_LIST_HEAD(name->list);
109 rdataset != NULL;
110 rdataset = ISC_LIST_NEXT(rdataset, link)) {
111 result = dns_rdataset_totext(rdataset,
112 print_name,
113 ISC_FALSE,
114 no_rdata,
115 &target);
116 if (result != ISC_R_SUCCESS)
117 return (result);
118 #ifdef USEINITALWS
119 if (first) {
120 print_name = &empty_name;
121 first = ISC_FALSE;
123 #endif
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)
130 break;
131 else if (result != ISC_R_SUCCESS)
132 return (result);
135 return (ISC_R_SUCCESS);
138 static isc_result_t
139 printrdata(dns_message_t *msg, dns_rdataset_t *rdataset, dns_name_t *owner,
140 const char *set_name)
142 isc_buffer_t target;
143 isc_result_t result;
144 isc_region_t r;
145 char t[65536];
147 UNUSED(msg);
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,
153 &target);
154 if (result != ISC_R_SUCCESS)
155 return (result);
156 isc_buffer_usedregion(&target, &r);
157 printf("%.*s", (int)r.length, (char *)r.base);
159 return (ISC_R_SUCCESS);
162 isc_result_t
163 printmessage(dns_message_t *msg) {
164 isc_boolean_t did_flag = ISC_FALSE;
165 isc_result_t result;
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) {
176 printf("qr");
177 did_flag = ISC_TRUE;
179 if ((msg->flags & DNS_MESSAGEFLAG_AA) != 0) {
180 printf("%saa", did_flag ? " " : "");
181 did_flag = ISC_TRUE;
183 if ((msg->flags & DNS_MESSAGEFLAG_TC) != 0) {
184 printf("%stc", did_flag ? " " : "");
185 did_flag = ISC_TRUE;
187 if ((msg->flags & DNS_MESSAGEFLAG_RD) != 0) {
188 printf("%srd", did_flag ? " " : "");
189 did_flag = ISC_TRUE;
191 if ((msg->flags & DNS_MESSAGEFLAG_RA) != 0) {
192 printf("%sra", did_flag ? " " : "");
193 did_flag = ISC_TRUE;
195 if ((msg->flags & DNS_MESSAGEFLAG_AD) != 0) {
196 printf("%sad", did_flag ? " " : "");
197 did_flag = ISC_TRUE;
199 if ((msg->flags & DNS_MESSAGEFLAG_CD) != 0) {
200 printf("%scd", did_flag ? " " : "");
201 did_flag = ISC_TRUE;
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);
209 if (opt != NULL)
210 printf(";; EDNS: version: %u, udp=%u\n",
211 (unsigned int)((opt->ttl & 0x00ff0000) >> 16),
212 (unsigned int)opt->rdclass);
214 tsigname = NULL;
215 tsig = dns_message_gettsig(msg, &tsigname);
216 if (tsig != NULL)
217 printf(";; PSEUDOSECTIONS: TSIG\n");
218 if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_QUESTION])) {
219 printf("\n");
220 result = printsection(msg, DNS_SECTION_QUESTION, "QUESTION");
221 if (result != ISC_R_SUCCESS)
222 return (result);
224 if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_ANSWER])) {
225 printf("\n");
226 result = printsection(msg, DNS_SECTION_ANSWER, "ANSWER");
227 if (result != ISC_R_SUCCESS)
228 return (result);
230 if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_AUTHORITY])) {
231 printf("\n");
232 result = printsection(msg, DNS_SECTION_AUTHORITY, "AUTHORITY");
233 if (result != ISC_R_SUCCESS)
234 return (result);
236 if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_ADDITIONAL])) {
237 printf("\n");
238 result = printsection(msg, DNS_SECTION_ADDITIONAL,
239 "ADDITIONAL");
240 if (result != ISC_R_SUCCESS)
241 return (result);
243 if (tsig != NULL) {
244 printf("\n");
245 result = printrdata(msg, tsig, tsigname,
246 "PSEUDOSECTION TSIG");
247 if (result != ISC_R_SUCCESS)
248 return (result);
250 printf("\n");
252 return (result);