1*a466cc55SCy Schubert /*
2*a466cc55SCy Schubert * Copyright (C) 2004, 2005, 2007, 2008, 2012 Internet Systems Consortium, Inc. ("ISC")
3*a466cc55SCy Schubert * Copyright (C) 1998-2001, 2003 Internet Software Consortium.
4*a466cc55SCy Schubert *
5*a466cc55SCy Schubert * Permission to use, copy, modify, and/or distribute this software for any
6*a466cc55SCy Schubert * purpose with or without fee is hereby granted, provided that the above
7*a466cc55SCy Schubert * copyright notice and this permission notice appear in all copies.
8*a466cc55SCy Schubert *
9*a466cc55SCy Schubert * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*a466cc55SCy Schubert * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*a466cc55SCy Schubert * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*a466cc55SCy Schubert * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*a466cc55SCy Schubert * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*a466cc55SCy Schubert * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*a466cc55SCy Schubert * PERFORMANCE OF THIS SOFTWARE.
16*a466cc55SCy Schubert */
17*a466cc55SCy Schubert
18*a466cc55SCy Schubert /* $Id$ */
19*a466cc55SCy Schubert
20*a466cc55SCy Schubert /*! \file */
21*a466cc55SCy Schubert
22*a466cc55SCy Schubert #include <config.h>
23*a466cc55SCy Schubert
24*a466cc55SCy Schubert #include <stddef.h>
25*a466cc55SCy Schubert #include <stdlib.h>
26*a466cc55SCy Schubert
27*a466cc55SCy Schubert #include <isc/lib.h>
28*a466cc55SCy Schubert #include <isc/msgs.h>
29*a466cc55SCy Schubert #include <isc/mutex.h>
30*a466cc55SCy Schubert #include <isc/once.h>
31*a466cc55SCy Schubert #include <isc/resultclass.h>
32*a466cc55SCy Schubert #include <isc/util.h>
33*a466cc55SCy Schubert
34*a466cc55SCy Schubert typedef struct resulttable {
35*a466cc55SCy Schubert unsigned int base;
36*a466cc55SCy Schubert unsigned int last;
37*a466cc55SCy Schubert const char ** text;
38*a466cc55SCy Schubert isc_msgcat_t * msgcat;
39*a466cc55SCy Schubert int set;
40*a466cc55SCy Schubert ISC_LINK(struct resulttable) link;
41*a466cc55SCy Schubert } resulttable;
42*a466cc55SCy Schubert
43*a466cc55SCy Schubert static const char *text[ISC_R_NRESULTS] = {
44*a466cc55SCy Schubert "success", /*%< 0 */
45*a466cc55SCy Schubert "out of memory", /*%< 1 */
46*a466cc55SCy Schubert "timed out", /*%< 2 */
47*a466cc55SCy Schubert "no available threads", /*%< 3 */
48*a466cc55SCy Schubert "address not available", /*%< 4 */
49*a466cc55SCy Schubert "address in use", /*%< 5 */
50*a466cc55SCy Schubert "permission denied", /*%< 6 */
51*a466cc55SCy Schubert "no pending connections", /*%< 7 */
52*a466cc55SCy Schubert "network unreachable", /*%< 8 */
53*a466cc55SCy Schubert "host unreachable", /*%< 9 */
54*a466cc55SCy Schubert "network down", /*%< 10 */
55*a466cc55SCy Schubert "host down", /*%< 11 */
56*a466cc55SCy Schubert "connection refused", /*%< 12 */
57*a466cc55SCy Schubert "not enough free resources", /*%< 13 */
58*a466cc55SCy Schubert "end of file", /*%< 14 */
59*a466cc55SCy Schubert "socket already bound", /*%< 15 */
60*a466cc55SCy Schubert "reload", /*%< 16 */
61*a466cc55SCy Schubert "lock busy", /*%< 17 */
62*a466cc55SCy Schubert "already exists", /*%< 18 */
63*a466cc55SCy Schubert "ran out of space", /*%< 19 */
64*a466cc55SCy Schubert "operation canceled", /*%< 20 */
65*a466cc55SCy Schubert "socket is not bound", /*%< 21 */
66*a466cc55SCy Schubert "shutting down", /*%< 22 */
67*a466cc55SCy Schubert "not found", /*%< 23 */
68*a466cc55SCy Schubert "unexpected end of input", /*%< 24 */
69*a466cc55SCy Schubert "failure", /*%< 25 */
70*a466cc55SCy Schubert "I/O error", /*%< 26 */
71*a466cc55SCy Schubert "not implemented", /*%< 27 */
72*a466cc55SCy Schubert "unbalanced parentheses", /*%< 28 */
73*a466cc55SCy Schubert "no more", /*%< 29 */
74*a466cc55SCy Schubert "invalid file", /*%< 30 */
75*a466cc55SCy Schubert "bad base64 encoding", /*%< 31 */
76*a466cc55SCy Schubert "unexpected token", /*%< 32 */
77*a466cc55SCy Schubert "quota reached", /*%< 33 */
78*a466cc55SCy Schubert "unexpected error", /*%< 34 */
79*a466cc55SCy Schubert "already running", /*%< 35 */
80*a466cc55SCy Schubert "ignore", /*%< 36 */
81*a466cc55SCy Schubert "address mask not contiguous", /*%< 37 */
82*a466cc55SCy Schubert "file not found", /*%< 38 */
83*a466cc55SCy Schubert "file already exists", /*%< 39 */
84*a466cc55SCy Schubert "socket is not connected", /*%< 40 */
85*a466cc55SCy Schubert "out of range", /*%< 41 */
86*a466cc55SCy Schubert "out of entropy", /*%< 42 */
87*a466cc55SCy Schubert "invalid use of multicast address", /*%< 43 */
88*a466cc55SCy Schubert "not a file", /*%< 44 */
89*a466cc55SCy Schubert "not a directory", /*%< 45 */
90*a466cc55SCy Schubert "queue is full", /*%< 46 */
91*a466cc55SCy Schubert "address family mismatch", /*%< 47 */
92*a466cc55SCy Schubert "address family not supported", /*%< 48 */
93*a466cc55SCy Schubert "bad hex encoding", /*%< 49 */
94*a466cc55SCy Schubert "too many open files", /*%< 50 */
95*a466cc55SCy Schubert "not blocking", /*%< 51 */
96*a466cc55SCy Schubert "unbalanced quotes", /*%< 52 */
97*a466cc55SCy Schubert "operation in progress", /*%< 53 */
98*a466cc55SCy Schubert "connection reset", /*%< 54 */
99*a466cc55SCy Schubert "soft quota reached", /*%< 55 */
100*a466cc55SCy Schubert "not a valid number", /*%< 56 */
101*a466cc55SCy Schubert "disabled", /*%< 57 */
102*a466cc55SCy Schubert "max size", /*%< 58 */
103*a466cc55SCy Schubert "invalid address format", /*%< 59 */
104*a466cc55SCy Schubert "bad base32 encoding", /*%< 60 */
105*a466cc55SCy Schubert "unset", /*%< 61 */
106*a466cc55SCy Schubert };
107*a466cc55SCy Schubert
108*a466cc55SCy Schubert #define ISC_RESULT_RESULTSET 2
109*a466cc55SCy Schubert #define ISC_RESULT_UNAVAILABLESET 3
110*a466cc55SCy Schubert
111*a466cc55SCy Schubert static isc_once_t once = ISC_ONCE_INIT;
112*a466cc55SCy Schubert static ISC_LIST(resulttable) tables;
113*a466cc55SCy Schubert static isc_mutex_t lock;
114*a466cc55SCy Schubert
115*a466cc55SCy Schubert static isc_result_t
register_table(unsigned int base,unsigned int nresults,const char ** txt,isc_msgcat_t * msgcat,int set)116*a466cc55SCy Schubert register_table(unsigned int base, unsigned int nresults, const char **txt,
117*a466cc55SCy Schubert isc_msgcat_t *msgcat, int set)
118*a466cc55SCy Schubert {
119*a466cc55SCy Schubert resulttable *table;
120*a466cc55SCy Schubert
121*a466cc55SCy Schubert REQUIRE(base % ISC_RESULTCLASS_SIZE == 0);
122*a466cc55SCy Schubert REQUIRE(nresults <= ISC_RESULTCLASS_SIZE);
123*a466cc55SCy Schubert REQUIRE(txt != NULL);
124*a466cc55SCy Schubert
125*a466cc55SCy Schubert /*
126*a466cc55SCy Schubert * We use malloc() here because we we want to be able to use
127*a466cc55SCy Schubert * isc_result_totext() even if there is no memory context.
128*a466cc55SCy Schubert */
129*a466cc55SCy Schubert table = malloc(sizeof(*table));
130*a466cc55SCy Schubert if (table == NULL)
131*a466cc55SCy Schubert return (ISC_R_NOMEMORY);
132*a466cc55SCy Schubert table->base = base;
133*a466cc55SCy Schubert table->last = base + nresults - 1;
134*a466cc55SCy Schubert table->text = txt;
135*a466cc55SCy Schubert table->msgcat = msgcat;
136*a466cc55SCy Schubert table->set = set;
137*a466cc55SCy Schubert ISC_LINK_INIT(table, link);
138*a466cc55SCy Schubert
139*a466cc55SCy Schubert LOCK(&lock);
140*a466cc55SCy Schubert
141*a466cc55SCy Schubert ISC_LIST_APPEND(tables, table, link);
142*a466cc55SCy Schubert
143*a466cc55SCy Schubert UNLOCK(&lock);
144*a466cc55SCy Schubert
145*a466cc55SCy Schubert return (ISC_R_SUCCESS);
146*a466cc55SCy Schubert }
147*a466cc55SCy Schubert
148*a466cc55SCy Schubert static void
initialize_action(void)149*a466cc55SCy Schubert initialize_action(void) {
150*a466cc55SCy Schubert isc_result_t result;
151*a466cc55SCy Schubert
152*a466cc55SCy Schubert RUNTIME_CHECK(isc_mutex_init(&lock) == ISC_R_SUCCESS);
153*a466cc55SCy Schubert ISC_LIST_INIT(tables);
154*a466cc55SCy Schubert
155*a466cc55SCy Schubert result = register_table(ISC_RESULTCLASS_ISC, ISC_R_NRESULTS, text,
156*a466cc55SCy Schubert isc_msgcat, ISC_RESULT_RESULTSET);
157*a466cc55SCy Schubert if (result != ISC_R_SUCCESS)
158*a466cc55SCy Schubert UNEXPECTED_ERROR(__FILE__, __LINE__,
159*a466cc55SCy Schubert "register_table() %s: %u",
160*a466cc55SCy Schubert isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
161*a466cc55SCy Schubert ISC_MSG_FAILED, "failed"),
162*a466cc55SCy Schubert result);
163*a466cc55SCy Schubert }
164*a466cc55SCy Schubert
165*a466cc55SCy Schubert static void
initialize(void)166*a466cc55SCy Schubert initialize(void) {
167*a466cc55SCy Schubert isc_lib_initmsgcat();
168*a466cc55SCy Schubert RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
169*a466cc55SCy Schubert }
170*a466cc55SCy Schubert
171*a466cc55SCy Schubert const char *
isc_result_totext(isc_result_t result)172*a466cc55SCy Schubert isc_result_totext(isc_result_t result) {
173*a466cc55SCy Schubert resulttable *table;
174*a466cc55SCy Schubert const char *txt, *default_text;
175*a466cc55SCy Schubert int idx;
176*a466cc55SCy Schubert
177*a466cc55SCy Schubert initialize();
178*a466cc55SCy Schubert
179*a466cc55SCy Schubert LOCK(&lock);
180*a466cc55SCy Schubert
181*a466cc55SCy Schubert txt = NULL;
182*a466cc55SCy Schubert for (table = ISC_LIST_HEAD(tables);
183*a466cc55SCy Schubert table != NULL;
184*a466cc55SCy Schubert table = ISC_LIST_NEXT(table, link)) {
185*a466cc55SCy Schubert if (result >= table->base && result <= table->last) {
186*a466cc55SCy Schubert idx = (int)(result - table->base);
187*a466cc55SCy Schubert default_text = table->text[idx];
188*a466cc55SCy Schubert /*
189*a466cc55SCy Schubert * Note: we use 'idx + 1' as the message number
190*a466cc55SCy Schubert * instead of idx because isc_msgcat_get() requires
191*a466cc55SCy Schubert * the message number to be > 0.
192*a466cc55SCy Schubert */
193*a466cc55SCy Schubert txt = isc_msgcat_get(table->msgcat, table->set,
194*a466cc55SCy Schubert idx + 1, default_text);
195*a466cc55SCy Schubert break;
196*a466cc55SCy Schubert }
197*a466cc55SCy Schubert }
198*a466cc55SCy Schubert if (txt == NULL)
199*a466cc55SCy Schubert txt = isc_msgcat_get(isc_msgcat, ISC_RESULT_UNAVAILABLESET,
200*a466cc55SCy Schubert 1, "(result code text not available)");
201*a466cc55SCy Schubert
202*a466cc55SCy Schubert UNLOCK(&lock);
203*a466cc55SCy Schubert
204*a466cc55SCy Schubert return (txt);
205*a466cc55SCy Schubert }
206*a466cc55SCy Schubert
207*a466cc55SCy Schubert isc_result_t
isc_result_register(unsigned int base,unsigned int nresults,const char ** txt,isc_msgcat_t * msgcat,int set)208*a466cc55SCy Schubert isc_result_register(unsigned int base, unsigned int nresults,
209*a466cc55SCy Schubert const char **txt, isc_msgcat_t *msgcat, int set)
210*a466cc55SCy Schubert {
211*a466cc55SCy Schubert initialize();
212*a466cc55SCy Schubert
213*a466cc55SCy Schubert return (register_table(base, nresults, txt, msgcat, set));
214*a466cc55SCy Schubert }
215