bind - Upgraded vendor branch to 9.5.2-P1
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / dns / result.c
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1998-2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /* $Id: result.c,v 1.90.2.12 2004/04/06 01:38:47 marka Exp $ */
19
20 #include <config.h>
21
22 #include <isc/once.h>
23 #include <isc/util.h>
24
25 #include <dns/result.h>
26 #include <dns/lib.h>
27
28 static const char *text[DNS_R_NRESULTS] = {
29         "label too long",                      /*  0 DNS_R_LABELTOOLONG      */
30         "bad escape",                          /*  1 DNS_R_BADESCAPE         */
31         "bad bitstring",                       /*  2 DNS_R_BADBITSTRING      */
32         "bitstring too long",                  /*  3 DNS_R_BITSTRINGTOOLONG  */
33         "empty label",                         /*  4 DNS_R_EMPTYLABEL        */
34
35         "bad dotted quad",                     /*  5 DNS_R_BADDOTTEDQUAD     */
36         "invalid NS owner name (wildcard)",    /*  6 DNS_R_INVALIDNS         */
37         "unknown class/type",                  /*  7 DNS_R_UNKNOWN           */
38         "bad label type",                      /*  8 DNS_R_BADLABELTYPE      */
39         "bad compression pointer",             /*  9 DNS_R_BADPOINTER        */
40
41         "too many hops",                       /* 10 DNS_R_TOOMANYHOPS       */
42         "disallowed (by application policy)",  /* 11 DNS_R_DISALLOWED        */
43         "extra input text",                    /* 12 DNS_R_EXTRATOKEN        */
44         "extra input data",                    /* 13 DNS_R_EXTRADATA         */
45         "text too long",                       /* 14 DNS_R_TEXTTOOLONG       */
46
47         "not at top of zone",                  /* 15 DNS_R_NOTZONETOP        */
48         "syntax error",                        /* 16 DNS_R_SYNTAX            */
49         "bad checksum",                        /* 17 DNS_R_BADCKSUM          */
50         "bad IPv6 address",                    /* 18 DNS_R_BADAAAA           */
51         "no owner",                            /* 19 DNS_R_NOOWNER           */
52
53         "no ttl",                              /* 20 DNS_R_NOTTL             */
54         "bad class",                           /* 21 DNS_R_BADCLASS          */
55         "name too long",                       /* 22 DNS_R_NAMETOOLONG       */
56         "partial match",                       /* 23 DNS_R_PARTIALMATCH      */
57         "new origin",                          /* 24 DNS_R_NEWORIGIN         */
58
59         "unchanged",                           /* 25 DNS_R_UNCHANGED         */
60         "bad ttl",                             /* 26 DNS_R_BADTTL            */
61         "more data needed/to be rendered",     /* 27 DNS_R_NOREDATA          */
62         "continue",                            /* 28 DNS_R_CONTINUE          */
63         "delegation",                          /* 29 DNS_R_DELEGATION        */
64
65         "glue",                                /* 30 DNS_R_GLUE              */
66         "dname",                               /* 31 DNS_R_DNAME             */
67         "cname",                               /* 32 DNS_R_CNAME             */
68         "bad database",                        /* 33 DNS_R_BADDB             */
69         "zonecut",                             /* 34 DNS_R_ZONECUT           */
70
71         "bad zone",                            /* 35 DNS_R_BADZONE           */
72         "more data",                           /* 36 DNS_R_MOREDATA          */
73         "up to date",                          /* 37 DNS_R_UPTODATE          */
74         "tsig verify failure",                 /* 38 DNS_R_TSIGVERIFYFAILURE */
75         "tsig indicates error",                /* 39 DNS_R_TSIGERRORSET      */
76
77         "SIG failed to verify",                /* 40 DNS_R_SIGINVALID        */
78         "SIG has expired",                     /* 41 DNS_R_SIGEXPIRED        */
79         "SIG validity period has not begun",   /* 42 DNS_R_SIGFUTURE         */
80         "key is unauthorized to sign data",    /* 43 DNS_R_KEYUNAUTHORIZED   */
81         "invalid time",                        /* 44 DNS_R_INVALIDTIME       */
82
83         "expected a TSIG or SIG(0)",           /* 45 DNS_R_EXPECTEDTSIG      */
84         "did not expect a TSIG or SIG(0)",     /* 46 DNS_R_UNEXPECTEDTSIG    */
85         "TKEY is unacceptable",                /* 47 DNS_R_INVALIDTKEY       */
86         "hint",                                /* 48 DNS_R_HINT              */
87         "drop",                                /* 49 DNS_R_DROP              */
88
89         "zone not loaded",                     /* 50 DNS_R_NOTLOADED         */
90         "ncache nxdomain",                     /* 51 DNS_R_NCACHENXDOMAIN    */
91         "ncache nxrrset",                      /* 52 DNS_R_NCACHENXRRSET     */
92         "wait",                                /* 53 DNS_R_WAIT              */
93         "not verified yet",                    /* 54 DNS_R_NOTVERIFIEDYET    */
94
95         "no identity",                         /* 55 DNS_R_NOIDENTITY        */
96         "no journal",                          /* 56 DNS_R_NOJOURNAL         */
97         "alias",                               /* 57 DNS_R_ALIAS             */
98         "use TCP",                             /* 58 DNS_R_USETCP            */
99         "no valid SIG",                        /* 59 DNS_R_NOVALIDSIG        */
100
101         "no valid NXT",                        /* 60 DNS_R_NOVALIDNXT        */
102         "not insecure",                        /* 61 DNS_R_NOTINSECURE       */
103         "unknown service",                     /* 62 DNS_R_UNKNOWNSERVICE    */
104         "recoverable error occurred",          /* 63 DNS_R_RECOVERABLE       */
105         "unknown opt attribute record",        /* 64 DNS_R_UNKNOWNOPT        */
106
107         "unexpected message id",               /* 65 DNS_R_UNEXPECTEDID      */
108         "seen include file",                   /* 66 DNS_R_SEENINCLUDE       */
109         "not exact",                           /* 67 DNS_R_NOTEXACT          */
110         "address blackholed",                  /* 68 DNS_R_BLACKHOLED        */
111         "bad algorithm",                       /* 69 DNS_R_BADALG            */
112
113         "invalid use of a meta type",          /* 70 DNS_R_METATYPE          */
114         "CNAME and other data",                /* 71 DNS_R_CNAMEANDOTHER     */
115         "multiple RRs of singleton type",      /* 72 DNS_R_SINGLETON         */
116         "hint nxrrset",                        /* 73 DNS_R_HINTNXRRSET       */
117         "no master file configured",           /* 74 DNS_R_NOMASTERFILE      */
118
119         "unknown protocol",                    /* 75 DNS_R_UNKNOWNPROTO      */
120         "clocks are unsynchronized",           /* 76 DNS_R_CLOCKSKEW         */
121         "IXFR failed",                         /* 77 DNS_R_BADIXFR           */
122         "<unused 78>",                         /* 78 unused                  */
123         "no valid KEY",                        /* 79 DNS_R_NOVALIDKEY        */
124
125         "obsolete",                            /* 80 DNS_R_OBSOLETE          */
126         "already frozen",                      /* 81 DNS_R_FROZEN            */
127         "unknown flag",                        /* 82 DNS_R_UNKNOWNFLAG       */
128         "expected a response",                 /* 83 DNS_R_EXPECTEDRESPONSE  */
129         "<unused 84>",
130
131         "<unused 85>",
132         "<unused 86>",
133         "<unused 87>",
134         "<unused 88>",
135         "<unused 89>",
136
137         "<unused 90>",
138         "<unused 91>",
139         "empty name",                           /* 92 DNS_R_EMPTYNAME        */
140         "empty wild",                           /* 93 DNS_R_EMPTYWILD        */
141         "bad bitmap",                           /* 94 DNS_R_BADBITMAP        */
142
143         "from wildcard",                        /* 95 DNS_R_FROMWILDCARD     */
144         "bad owner name (check-names)",         /* 96 DNS_R_BADOWNERNAME     */
145         "bad name (check-names)",               /* 97 DNS_R_BADNAME          */
146         "dynamic zone",                         /* 98 DNS_R_DYNAMIC          */
147         "unknown command"                       /* 99 DNS_R_UNKNOWNCOMMAND   */
148 };
149
150 static const char *rcode_text[DNS_R_NRCODERESULTS] = {
151         "NOERROR",                              /*  0 DNS_R_NOEROR           */
152         "FORMERR",                              /*  1 DNS_R_FORMERR          */
153         "SERVFAIL",                             /*  2 DNS_R_SERVFAIL         */
154         "NXDOMAIN",                             /*  3 DNS_R_NXDOMAIN         */
155         "NOTIMP",                               /*  4 DNS_R_NOTIMP           */
156
157         "REFUSED",                              /*  5 DNS_R_REFUSED          */
158         "YXDOMAIN",                             /*  6 DNS_R_YXDOMAIN         */
159         "YXRRSET",                              /*  7 DNS_R_YXRRSET          */
160         "NXRRSET",                              /*  8 DNS_R_NXRRSET          */
161         "NOTAUTH",                              /*  9 DNS_R_NOTAUTH          */
162
163         "NOTZONE",                              /* 10 DNS_R_NOTZONE          */
164         "<rcode 11>",                           /* 11 has no macro           */
165         "<rcode 12>",                           /* 12 has no macro           */
166         "<rcode 13>",                           /* 13 has no macro           */
167         "<rcode 14>",                           /* 14 has no macro           */
168
169         "<rcode 15>",                           /* 15 has no macro           */
170         "BADVERS",                              /* 16 DNS_R_BADVERS          */
171 };
172
173 #define DNS_RESULT_RESULTSET                    2
174 #define DNS_RESULT_RCODERESULTSET               3
175
176 static isc_once_t               once = ISC_ONCE_INIT;
177
178 static void
179 initialize_action(void) {
180         isc_result_t result;
181
182         result = isc_result_register(ISC_RESULTCLASS_DNS, DNS_R_NRESULTS,
183                                      text, dns_msgcat, DNS_RESULT_RESULTSET);
184         if (result == ISC_R_SUCCESS)
185                 result = isc_result_register(ISC_RESULTCLASS_DNSRCODE,
186                                              DNS_R_NRCODERESULTS,
187                                              rcode_text, dns_msgcat,
188                                              DNS_RESULT_RCODERESULTSET);
189         if (result != ISC_R_SUCCESS)
190                 UNEXPECTED_ERROR(__FILE__, __LINE__,
191                                  "isc_result_register() failed: %u", result);
192 }
193
194 static void
195 initialize(void) {
196         dns_lib_initmsgcat();
197         RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
198 }
199
200 const char *
201 dns_result_totext(isc_result_t result) {
202         initialize();
203
204         return (isc_result_totext(result));
205 }
206
207 void
208 dns_result_register(void) {
209         initialize();
210 }
211
212 dns_rcode_t
213 dns_result_torcode(isc_result_t result) {
214         dns_rcode_t rcode = dns_rcode_servfail;
215
216         if (DNS_RESULT_ISRCODE(result)) {
217                 /*
218                  * Rcodes can't be bigger than 12 bits, which is why we
219                  * AND with 0xFFF instead of 0xFFFF.
220                  */
221                 return ((dns_rcode_t)((result) & 0xFFF));
222         }
223         /*
224          * Try to supply an appropriate rcode.
225          */
226         switch (result) {
227         case ISC_R_SUCCESS:
228                 rcode = dns_rcode_noerror;
229                 break;
230         case ISC_R_BADBASE64:
231         case ISC_R_NOSPACE:
232         case ISC_R_RANGE:
233         case ISC_R_UNEXPECTEDEND:
234         case DNS_R_BADAAAA:
235         case DNS_R_BADBITSTRING:
236         case DNS_R_BADCKSUM:
237         case DNS_R_BADCLASS:
238         case DNS_R_BADLABELTYPE:
239         case DNS_R_BADPOINTER:
240         case DNS_R_BADTTL:
241         case DNS_R_BADZONE:
242         case DNS_R_BITSTRINGTOOLONG:
243         case DNS_R_EXTRADATA:
244         case DNS_R_LABELTOOLONG:
245         case DNS_R_NOREDATA:
246         case DNS_R_SYNTAX:
247         case DNS_R_TEXTTOOLONG:
248         case DNS_R_TOOMANYHOPS:
249         case DNS_R_TSIGERRORSET:
250         case DNS_R_UNKNOWN:
251                 rcode = dns_rcode_formerr;
252                 break;
253         case DNS_R_DISALLOWED:
254                 rcode = dns_rcode_refused;
255                 break;
256         case DNS_R_TSIGVERIFYFAILURE:
257         case DNS_R_CLOCKSKEW:
258                 rcode = dns_rcode_notauth;
259                 break;
260         default:
261                 rcode = dns_rcode_servfail;
262         }
263
264         return (rcode);
265 }