1258945Sroberto/* 2280849Scy * Copyright (C) 2004, 2005, 2007, 2008, 2012 Internet Systems Consortium, Inc. ("ISC") 3258945Sroberto * Copyright (C) 1998-2001, 2003 Internet Software Consortium. 4258945Sroberto * 5258945Sroberto * Permission to use, copy, modify, and/or distribute this software for any 6258945Sroberto * purpose with or without fee is hereby granted, provided that the above 7258945Sroberto * copyright notice and this permission notice appear in all copies. 8258945Sroberto * 9258945Sroberto * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 10258945Sroberto * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 11258945Sroberto * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 12258945Sroberto * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 13258945Sroberto * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 14258945Sroberto * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 15258945Sroberto * PERFORMANCE OF THIS SOFTWARE. 16258945Sroberto */ 17258945Sroberto 18280849Scy/* $Id$ */ 19258945Sroberto 20258945Sroberto/*! \file */ 21258945Sroberto 22258945Sroberto#include <config.h> 23258945Sroberto 24258945Sroberto#include <stddef.h> 25258945Sroberto#include <stdlib.h> 26258945Sroberto 27258945Sroberto#include <isc/lib.h> 28258945Sroberto#include <isc/msgs.h> 29258945Sroberto#include <isc/mutex.h> 30258945Sroberto#include <isc/once.h> 31258945Sroberto#include <isc/resultclass.h> 32258945Sroberto#include <isc/util.h> 33258945Sroberto 34258945Srobertotypedef struct resulttable { 35258945Sroberto unsigned int base; 36258945Sroberto unsigned int last; 37258945Sroberto const char ** text; 38258945Sroberto isc_msgcat_t * msgcat; 39258945Sroberto int set; 40258945Sroberto ISC_LINK(struct resulttable) link; 41258945Sroberto} resulttable; 42258945Sroberto 43258945Srobertostatic const char *text[ISC_R_NRESULTS] = { 44258945Sroberto "success", /*%< 0 */ 45258945Sroberto "out of memory", /*%< 1 */ 46258945Sroberto "timed out", /*%< 2 */ 47258945Sroberto "no available threads", /*%< 3 */ 48258945Sroberto "address not available", /*%< 4 */ 49258945Sroberto "address in use", /*%< 5 */ 50258945Sroberto "permission denied", /*%< 6 */ 51258945Sroberto "no pending connections", /*%< 7 */ 52258945Sroberto "network unreachable", /*%< 8 */ 53258945Sroberto "host unreachable", /*%< 9 */ 54258945Sroberto "network down", /*%< 10 */ 55258945Sroberto "host down", /*%< 11 */ 56258945Sroberto "connection refused", /*%< 12 */ 57258945Sroberto "not enough free resources", /*%< 13 */ 58258945Sroberto "end of file", /*%< 14 */ 59258945Sroberto "socket already bound", /*%< 15 */ 60258945Sroberto "reload", /*%< 16 */ 61258945Sroberto "lock busy", /*%< 17 */ 62258945Sroberto "already exists", /*%< 18 */ 63258945Sroberto "ran out of space", /*%< 19 */ 64258945Sroberto "operation canceled", /*%< 20 */ 65258945Sroberto "socket is not bound", /*%< 21 */ 66258945Sroberto "shutting down", /*%< 22 */ 67258945Sroberto "not found", /*%< 23 */ 68258945Sroberto "unexpected end of input", /*%< 24 */ 69258945Sroberto "failure", /*%< 25 */ 70258945Sroberto "I/O error", /*%< 26 */ 71258945Sroberto "not implemented", /*%< 27 */ 72258945Sroberto "unbalanced parentheses", /*%< 28 */ 73258945Sroberto "no more", /*%< 29 */ 74258945Sroberto "invalid file", /*%< 30 */ 75258945Sroberto "bad base64 encoding", /*%< 31 */ 76258945Sroberto "unexpected token", /*%< 32 */ 77258945Sroberto "quota reached", /*%< 33 */ 78258945Sroberto "unexpected error", /*%< 34 */ 79258945Sroberto "already running", /*%< 35 */ 80258945Sroberto "ignore", /*%< 36 */ 81258945Sroberto "address mask not contiguous", /*%< 37 */ 82258945Sroberto "file not found", /*%< 38 */ 83258945Sroberto "file already exists", /*%< 39 */ 84258945Sroberto "socket is not connected", /*%< 40 */ 85258945Sroberto "out of range", /*%< 41 */ 86258945Sroberto "out of entropy", /*%< 42 */ 87258945Sroberto "invalid use of multicast address", /*%< 43 */ 88258945Sroberto "not a file", /*%< 44 */ 89258945Sroberto "not a directory", /*%< 45 */ 90258945Sroberto "queue is full", /*%< 46 */ 91258945Sroberto "address family mismatch", /*%< 47 */ 92258945Sroberto "address family not supported", /*%< 48 */ 93258945Sroberto "bad hex encoding", /*%< 49 */ 94258945Sroberto "too many open files", /*%< 50 */ 95258945Sroberto "not blocking", /*%< 51 */ 96258945Sroberto "unbalanced quotes", /*%< 52 */ 97258945Sroberto "operation in progress", /*%< 53 */ 98258945Sroberto "connection reset", /*%< 54 */ 99258945Sroberto "soft quota reached", /*%< 55 */ 100258945Sroberto "not a valid number", /*%< 56 */ 101258945Sroberto "disabled", /*%< 57 */ 102258945Sroberto "max size", /*%< 58 */ 103258945Sroberto "invalid address format", /*%< 59 */ 104258945Sroberto "bad base32 encoding", /*%< 60 */ 105280849Scy "unset", /*%< 61 */ 106258945Sroberto}; 107258945Sroberto 108258945Sroberto#define ISC_RESULT_RESULTSET 2 109258945Sroberto#define ISC_RESULT_UNAVAILABLESET 3 110258945Sroberto 111258945Srobertostatic isc_once_t once = ISC_ONCE_INIT; 112258945Srobertostatic ISC_LIST(resulttable) tables; 113258945Srobertostatic isc_mutex_t lock; 114258945Sroberto 115258945Srobertostatic isc_result_t 116280849Scyregister_table(unsigned int base, unsigned int nresults, const char **txt, 117258945Sroberto isc_msgcat_t *msgcat, int set) 118258945Sroberto{ 119258945Sroberto resulttable *table; 120258945Sroberto 121258945Sroberto REQUIRE(base % ISC_RESULTCLASS_SIZE == 0); 122258945Sroberto REQUIRE(nresults <= ISC_RESULTCLASS_SIZE); 123280849Scy REQUIRE(txt != NULL); 124258945Sroberto 125258945Sroberto /* 126258945Sroberto * We use malloc() here because we we want to be able to use 127258945Sroberto * isc_result_totext() even if there is no memory context. 128258945Sroberto */ 129258945Sroberto table = malloc(sizeof(*table)); 130258945Sroberto if (table == NULL) 131258945Sroberto return (ISC_R_NOMEMORY); 132258945Sroberto table->base = base; 133258945Sroberto table->last = base + nresults - 1; 134280849Scy table->text = txt; 135258945Sroberto table->msgcat = msgcat; 136258945Sroberto table->set = set; 137258945Sroberto ISC_LINK_INIT(table, link); 138258945Sroberto 139258945Sroberto LOCK(&lock); 140258945Sroberto 141258945Sroberto ISC_LIST_APPEND(tables, table, link); 142258945Sroberto 143258945Sroberto UNLOCK(&lock); 144258945Sroberto 145258945Sroberto return (ISC_R_SUCCESS); 146258945Sroberto} 147258945Sroberto 148258945Srobertostatic void 149258945Srobertoinitialize_action(void) { 150258945Sroberto isc_result_t result; 151258945Sroberto 152258945Sroberto RUNTIME_CHECK(isc_mutex_init(&lock) == ISC_R_SUCCESS); 153258945Sroberto ISC_LIST_INIT(tables); 154258945Sroberto 155258945Sroberto result = register_table(ISC_RESULTCLASS_ISC, ISC_R_NRESULTS, text, 156258945Sroberto isc_msgcat, ISC_RESULT_RESULTSET); 157258945Sroberto if (result != ISC_R_SUCCESS) 158258945Sroberto UNEXPECTED_ERROR(__FILE__, __LINE__, 159258945Sroberto "register_table() %s: %u", 160258945Sroberto isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL, 161258945Sroberto ISC_MSG_FAILED, "failed"), 162258945Sroberto result); 163258945Sroberto} 164258945Sroberto 165258945Srobertostatic void 166258945Srobertoinitialize(void) { 167258945Sroberto isc_lib_initmsgcat(); 168258945Sroberto RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS); 169258945Sroberto} 170258945Sroberto 171258945Srobertoconst char * 172258945Srobertoisc_result_totext(isc_result_t result) { 173258945Sroberto resulttable *table; 174280849Scy const char *txt, *default_text; 175282408Scy int idx; 176258945Sroberto 177258945Sroberto initialize(); 178258945Sroberto 179258945Sroberto LOCK(&lock); 180258945Sroberto 181280849Scy txt = NULL; 182258945Sroberto for (table = ISC_LIST_HEAD(tables); 183258945Sroberto table != NULL; 184258945Sroberto table = ISC_LIST_NEXT(table, link)) { 185258945Sroberto if (result >= table->base && result <= table->last) { 186282408Scy idx = (int)(result - table->base); 187282408Scy default_text = table->text[idx]; 188258945Sroberto /* 189282408Scy * Note: we use 'idx + 1' as the message number 190282408Scy * instead of idx because isc_msgcat_get() requires 191258945Sroberto * the message number to be > 0. 192258945Sroberto */ 193280849Scy txt = isc_msgcat_get(table->msgcat, table->set, 194282408Scy idx + 1, default_text); 195258945Sroberto break; 196258945Sroberto } 197258945Sroberto } 198280849Scy if (txt == NULL) 199280849Scy txt = isc_msgcat_get(isc_msgcat, ISC_RESULT_UNAVAILABLESET, 200280849Scy 1, "(result code text not available)"); 201258945Sroberto 202258945Sroberto UNLOCK(&lock); 203258945Sroberto 204280849Scy return (txt); 205258945Sroberto} 206258945Sroberto 207258945Srobertoisc_result_t 208258945Srobertoisc_result_register(unsigned int base, unsigned int nresults, 209280849Scy const char **txt, isc_msgcat_t *msgcat, int set) 210258945Sroberto{ 211258945Sroberto initialize(); 212258945Sroberto 213280849Scy return (register_table(base, nresults, txt, msgcat, set)); 214258945Sroberto} 215