1*eabc0478Schristos /* $NetBSD: result.c,v 1.2 2024/08/18 20:47:14 christos Exp $ */ 2897be3a4Schristos 3897be3a4Schristos /* 4897be3a4Schristos * Copyright (C) 2004, 2005, 2007, 2008, 2012 Internet Systems Consortium, Inc. ("ISC") 5897be3a4Schristos * Copyright (C) 1998-2001, 2003 Internet Software Consortium. 6897be3a4Schristos * 7897be3a4Schristos * Permission to use, copy, modify, and/or distribute this software for any 8897be3a4Schristos * purpose with or without fee is hereby granted, provided that the above 9897be3a4Schristos * copyright notice and this permission notice appear in all copies. 10897be3a4Schristos * 11897be3a4Schristos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 12897be3a4Schristos * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 13897be3a4Schristos * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 14897be3a4Schristos * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 15897be3a4Schristos * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 16897be3a4Schristos * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17897be3a4Schristos * PERFORMANCE OF THIS SOFTWARE. 18897be3a4Schristos */ 19897be3a4Schristos 20897be3a4Schristos /* Id */ 21897be3a4Schristos 22897be3a4Schristos /*! \file */ 23897be3a4Schristos 24897be3a4Schristos #include <config.h> 25897be3a4Schristos 26897be3a4Schristos #include <stddef.h> 27897be3a4Schristos #include <stdlib.h> 28897be3a4Schristos 29897be3a4Schristos #include <isc/lib.h> 30897be3a4Schristos #include <isc/msgs.h> 31897be3a4Schristos #include <isc/mutex.h> 32897be3a4Schristos #include <isc/once.h> 33897be3a4Schristos #include <isc/resultclass.h> 34897be3a4Schristos #include <isc/util.h> 35897be3a4Schristos 36897be3a4Schristos typedef struct resulttable { 37897be3a4Schristos unsigned int base; 38897be3a4Schristos unsigned int last; 39897be3a4Schristos const char ** text; 40897be3a4Schristos isc_msgcat_t * msgcat; 41897be3a4Schristos int set; 42897be3a4Schristos ISC_LINK(struct resulttable) link; 43897be3a4Schristos } resulttable; 44897be3a4Schristos 45897be3a4Schristos static const char *text[ISC_R_NRESULTS] = { 46897be3a4Schristos "success", /*%< 0 */ 47897be3a4Schristos "out of memory", /*%< 1 */ 48897be3a4Schristos "timed out", /*%< 2 */ 49897be3a4Schristos "no available threads", /*%< 3 */ 50897be3a4Schristos "address not available", /*%< 4 */ 51897be3a4Schristos "address in use", /*%< 5 */ 52897be3a4Schristos "permission denied", /*%< 6 */ 53897be3a4Schristos "no pending connections", /*%< 7 */ 54897be3a4Schristos "network unreachable", /*%< 8 */ 55897be3a4Schristos "host unreachable", /*%< 9 */ 56897be3a4Schristos "network down", /*%< 10 */ 57897be3a4Schristos "host down", /*%< 11 */ 58897be3a4Schristos "connection refused", /*%< 12 */ 59897be3a4Schristos "not enough free resources", /*%< 13 */ 60897be3a4Schristos "end of file", /*%< 14 */ 61897be3a4Schristos "socket already bound", /*%< 15 */ 62897be3a4Schristos "reload", /*%< 16 */ 63897be3a4Schristos "lock busy", /*%< 17 */ 64897be3a4Schristos "already exists", /*%< 18 */ 65897be3a4Schristos "ran out of space", /*%< 19 */ 66897be3a4Schristos "operation canceled", /*%< 20 */ 67897be3a4Schristos "socket is not bound", /*%< 21 */ 68897be3a4Schristos "shutting down", /*%< 22 */ 69897be3a4Schristos "not found", /*%< 23 */ 70897be3a4Schristos "unexpected end of input", /*%< 24 */ 71897be3a4Schristos "failure", /*%< 25 */ 72897be3a4Schristos "I/O error", /*%< 26 */ 73897be3a4Schristos "not implemented", /*%< 27 */ 74897be3a4Schristos "unbalanced parentheses", /*%< 28 */ 75897be3a4Schristos "no more", /*%< 29 */ 76897be3a4Schristos "invalid file", /*%< 30 */ 77897be3a4Schristos "bad base64 encoding", /*%< 31 */ 78897be3a4Schristos "unexpected token", /*%< 32 */ 79897be3a4Schristos "quota reached", /*%< 33 */ 80897be3a4Schristos "unexpected error", /*%< 34 */ 81897be3a4Schristos "already running", /*%< 35 */ 82897be3a4Schristos "ignore", /*%< 36 */ 83897be3a4Schristos "address mask not contiguous", /*%< 37 */ 84897be3a4Schristos "file not found", /*%< 38 */ 85897be3a4Schristos "file already exists", /*%< 39 */ 86897be3a4Schristos "socket is not connected", /*%< 40 */ 87897be3a4Schristos "out of range", /*%< 41 */ 88897be3a4Schristos "out of entropy", /*%< 42 */ 89897be3a4Schristos "invalid use of multicast address", /*%< 43 */ 90897be3a4Schristos "not a file", /*%< 44 */ 91897be3a4Schristos "not a directory", /*%< 45 */ 92897be3a4Schristos "queue is full", /*%< 46 */ 93897be3a4Schristos "address family mismatch", /*%< 47 */ 94897be3a4Schristos "address family not supported", /*%< 48 */ 95897be3a4Schristos "bad hex encoding", /*%< 49 */ 96897be3a4Schristos "too many open files", /*%< 50 */ 97897be3a4Schristos "not blocking", /*%< 51 */ 98897be3a4Schristos "unbalanced quotes", /*%< 52 */ 99897be3a4Schristos "operation in progress", /*%< 53 */ 100897be3a4Schristos "connection reset", /*%< 54 */ 101897be3a4Schristos "soft quota reached", /*%< 55 */ 102897be3a4Schristos "not a valid number", /*%< 56 */ 103897be3a4Schristos "disabled", /*%< 57 */ 104897be3a4Schristos "max size", /*%< 58 */ 105897be3a4Schristos "invalid address format", /*%< 59 */ 106897be3a4Schristos "bad base32 encoding", /*%< 60 */ 107897be3a4Schristos "unset", /*%< 61 */ 108897be3a4Schristos }; 109897be3a4Schristos 110897be3a4Schristos #define ISC_RESULT_RESULTSET 2 111897be3a4Schristos #define ISC_RESULT_UNAVAILABLESET 3 112897be3a4Schristos 113897be3a4Schristos static isc_once_t once = ISC_ONCE_INIT; 114897be3a4Schristos static ISC_LIST(resulttable) tables; 115897be3a4Schristos static isc_mutex_t lock; 116897be3a4Schristos 117897be3a4Schristos static isc_result_t 118897be3a4Schristos register_table(unsigned int base, unsigned int nresults, const char **txt, 119897be3a4Schristos isc_msgcat_t *msgcat, int set) 120897be3a4Schristos { 121897be3a4Schristos resulttable *table; 122897be3a4Schristos 123897be3a4Schristos REQUIRE(base % ISC_RESULTCLASS_SIZE == 0); 124897be3a4Schristos REQUIRE(nresults <= ISC_RESULTCLASS_SIZE); 125897be3a4Schristos REQUIRE(txt != NULL); 126897be3a4Schristos 127897be3a4Schristos /* 128897be3a4Schristos * We use malloc() here because we we want to be able to use 129897be3a4Schristos * isc_result_totext() even if there is no memory context. 130897be3a4Schristos */ 131897be3a4Schristos table = malloc(sizeof(*table)); 132897be3a4Schristos if (table == NULL) 133897be3a4Schristos return (ISC_R_NOMEMORY); 134897be3a4Schristos table->base = base; 135897be3a4Schristos table->last = base + nresults - 1; 136897be3a4Schristos table->text = txt; 137897be3a4Schristos table->msgcat = msgcat; 138897be3a4Schristos table->set = set; 139897be3a4Schristos ISC_LINK_INIT(table, link); 140897be3a4Schristos 141897be3a4Schristos LOCK(&lock); 142897be3a4Schristos 143897be3a4Schristos ISC_LIST_APPEND(tables, table, link); 144897be3a4Schristos 145897be3a4Schristos UNLOCK(&lock); 146897be3a4Schristos 147897be3a4Schristos return (ISC_R_SUCCESS); 148897be3a4Schristos } 149897be3a4Schristos 150897be3a4Schristos static void 151897be3a4Schristos initialize_action(void) { 152897be3a4Schristos isc_result_t result; 153897be3a4Schristos 154897be3a4Schristos RUNTIME_CHECK(isc_mutex_init(&lock) == ISC_R_SUCCESS); 155897be3a4Schristos ISC_LIST_INIT(tables); 156897be3a4Schristos 157897be3a4Schristos result = register_table(ISC_RESULTCLASS_ISC, ISC_R_NRESULTS, text, 158897be3a4Schristos isc_msgcat, ISC_RESULT_RESULTSET); 159897be3a4Schristos if (result != ISC_R_SUCCESS) 160897be3a4Schristos UNEXPECTED_ERROR(__FILE__, __LINE__, 161897be3a4Schristos "register_table() %s: %u", 162897be3a4Schristos isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL, 163897be3a4Schristos ISC_MSG_FAILED, "failed"), 164897be3a4Schristos result); 165897be3a4Schristos } 166897be3a4Schristos 167897be3a4Schristos static void 168897be3a4Schristos initialize(void) { 169897be3a4Schristos isc_lib_initmsgcat(); 170897be3a4Schristos RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS); 171897be3a4Schristos } 172897be3a4Schristos 173897be3a4Schristos const char * 174897be3a4Schristos isc_result_totext(isc_result_t result) { 175897be3a4Schristos resulttable *table; 176897be3a4Schristos const char *txt, *default_text; 177897be3a4Schristos int idx; 178897be3a4Schristos 179897be3a4Schristos initialize(); 180897be3a4Schristos 181897be3a4Schristos LOCK(&lock); 182897be3a4Schristos 183897be3a4Schristos txt = NULL; 184897be3a4Schristos for (table = ISC_LIST_HEAD(tables); 185897be3a4Schristos table != NULL; 186897be3a4Schristos table = ISC_LIST_NEXT(table, link)) { 187897be3a4Schristos if (result >= table->base && result <= table->last) { 188897be3a4Schristos idx = (int)(result - table->base); 189897be3a4Schristos default_text = table->text[idx]; 190897be3a4Schristos /* 191897be3a4Schristos * Note: we use 'idx + 1' as the message number 192897be3a4Schristos * instead of idx because isc_msgcat_get() requires 193897be3a4Schristos * the message number to be > 0. 194897be3a4Schristos */ 195897be3a4Schristos txt = isc_msgcat_get(table->msgcat, table->set, 196897be3a4Schristos idx + 1, default_text); 197897be3a4Schristos break; 198897be3a4Schristos } 199897be3a4Schristos } 200897be3a4Schristos if (txt == NULL) 201897be3a4Schristos txt = isc_msgcat_get(isc_msgcat, ISC_RESULT_UNAVAILABLESET, 202897be3a4Schristos 1, "(result code text not available)"); 203897be3a4Schristos 204897be3a4Schristos UNLOCK(&lock); 205897be3a4Schristos 206897be3a4Schristos return (txt); 207897be3a4Schristos } 208897be3a4Schristos 209897be3a4Schristos isc_result_t 210897be3a4Schristos isc_result_register(unsigned int base, unsigned int nresults, 211897be3a4Schristos const char **txt, isc_msgcat_t *msgcat, int set) 212897be3a4Schristos { 213897be3a4Schristos initialize(); 214897be3a4Schristos 215897be3a4Schristos return (register_table(base, nresults, txt, msgcat, set)); 216897be3a4Schristos } 217