result.c revision 40f53fa8d9c6a4fc38c0014495e7a42b08f52481
1633838b8255282d10af15c5c84cee5a51466712Bob Halley/*
49dbdb0186eb23d87d685b96eaefa9ec3c71d9b8David Lawrence * Copyright (C) 1998-2000 Internet Software Consortium.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * Permission to use, copy, modify, and distribute this software for any
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * purpose with or without fee is hereby granted, provided that the above
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * copyright notice and this permission notice appear in all copies.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
15a44745412679c30a6d022733925af70a38b715David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
15a44745412679c30a6d022733925af70a38b715David Lawrence * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
15a44745412679c30a6d022733925af70a38b715David Lawrence * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
15a44745412679c30a6d022733925af70a38b715David Lawrence * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
15a44745412679c30a6d022733925af70a38b715David Lawrence * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
15a44745412679c30a6d022733925af70a38b715David Lawrence * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
15a44745412679c30a6d022733925af70a38b715David Lawrence * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
15a44745412679c30a6d022733925af70a38b715David Lawrence * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1633838b8255282d10af15c5c84cee5a51466712Bob Halley */
fda0ab6a96a7edb1acbde0c77ead0542979b0f24Bob Halley
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence/* $Id: result.c,v 1.46 2000/08/01 01:29:43 tale Exp $ */
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley#include <config.h>
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley#include <stdlib.h>
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/lib.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/msgcat.h>
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley#include <isc/mutex.h>
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley#include <isc/once.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/resultclass.h>
440be4c866f6935ac069db79a414304507a664c2Michael Graff#include <isc/util.h>
fda0ab6a96a7edb1acbde0c77ead0542979b0f24Bob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halleytypedef struct resulttable {
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley unsigned int base;
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley unsigned int last;
87cafc5e70f79f2586d067fbdd64f61bbab069d2David Lawrence const char ** text;
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley isc_msgcat_t * msgcat;
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley int set;
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley ISC_LINK(struct resulttable) link;
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley} resulttable;
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
87cafc5e70f79f2586d067fbdd64f61bbab069d2David Lawrencestatic const char *text[ISC_R_NRESULTS] = {
f02c1d9431a7bad59ef2d40a341fa2f68a8a7550Michael Graff "success", /* 0 */
f02c1d9431a7bad59ef2d40a341fa2f68a8a7550Michael Graff "out of memory", /* 1 */
f02c1d9431a7bad59ef2d40a341fa2f68a8a7550Michael Graff "timed out", /* 2 */
f02c1d9431a7bad59ef2d40a341fa2f68a8a7550Michael Graff "no available threads", /* 3 */
f02c1d9431a7bad59ef2d40a341fa2f68a8a7550Michael Graff "address not available", /* 4 */
f02c1d9431a7bad59ef2d40a341fa2f68a8a7550Michael Graff "address in use", /* 5 */
f02c1d9431a7bad59ef2d40a341fa2f68a8a7550Michael Graff "permission denied", /* 6 */
f02c1d9431a7bad59ef2d40a341fa2f68a8a7550Michael Graff "no pending connections", /* 7 */
f02c1d9431a7bad59ef2d40a341fa2f68a8a7550Michael Graff "network unreachable", /* 8 */
f02c1d9431a7bad59ef2d40a341fa2f68a8a7550Michael Graff "host unreachable", /* 9 */
f02c1d9431a7bad59ef2d40a341fa2f68a8a7550Michael Graff "network down", /* 10 */
f02c1d9431a7bad59ef2d40a341fa2f68a8a7550Michael Graff "host down", /* 11 */
f02c1d9431a7bad59ef2d40a341fa2f68a8a7550Michael Graff "connection refused", /* 12 */
f02c1d9431a7bad59ef2d40a341fa2f68a8a7550Michael Graff "not enough free resources", /* 13 */
f02c1d9431a7bad59ef2d40a341fa2f68a8a7550Michael Graff "end of file", /* 14 */
f02c1d9431a7bad59ef2d40a341fa2f68a8a7550Michael Graff "socket already bound", /* 15 */
010170f711705f72d488d097daea31b7be4af9d4Bob Halley "reload", /* 16 */
903c1c35fcec1aa32b6cea9cbdf0a2f055abed2dBob Halley "lock busy", /* 17 */
6a632f69681d2af06026345f92f8ee879ce18e08Bob Halley "already exists", /* 18 */
af0fce400bfa79289aae239c10ae7f4fb2be8fa6Bob Halley "ran out of space", /* 19 */
a0084eaa9f134e32d456537c67d2db7516aba867Michael Graff "operation canceled", /* 20 */
8321dd2150bea461fbe0333965ef072d576b4f6cMichael Graff "socket is not bound", /* 21 */
0d3119d4d1394adf61f5ab69ef3573993cde6fe3Michael Graff "shutting down", /* 22 */
af0fce400bfa79289aae239c10ae7f4fb2be8fa6Bob Halley "not found", /* 23 */
af0fce400bfa79289aae239c10ae7f4fb2be8fa6Bob Halley "unexpected end of input", /* 24 */
af0fce400bfa79289aae239c10ae7f4fb2be8fa6Bob Halley "failure", /* 25 */
af0fce400bfa79289aae239c10ae7f4fb2be8fa6Bob Halley "I/O error", /* 26 */
aa3a328647720d733397b67c62f15331295fac09Bob Halley "not implemented", /* 27 */
516d43e889d4fba3711671b6329f4145748c78ffBob Halley "unbalanced parentheses", /* 28 */
f3d38475531f218d0a2c2917b506b7ce60664b7cBob Halley "no more", /* 29 */
822f6cdabb1edd44472c7a758b5cae71376fa9beBrian Wellington "invalid file", /* 30 */
822f6cdabb1edd44472c7a758b5cae71376fa9beBrian Wellington "bad base64 encoding", /* 31 */
822f6cdabb1edd44472c7a758b5cae71376fa9beBrian Wellington "unexpected token", /* 32 */
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley "quota reached", /* 33 */
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley "unexpected error", /* 34 */
55396fb3fba8ba140c12623acac27e007d4119a6Bob Halley "already running", /* 35 */
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson "ignore", /* 36 */
6a13d6f3c687d463a2a88f696a5193a5651612baAndreas Gustafsson "address mask not contiguous", /* 37 */
6a13d6f3c687d463a2a88f696a5193a5651612baAndreas Gustafsson "file not found", /* 38 */
8321dd2150bea461fbe0333965ef072d576b4f6cMichael Graff "file already exists", /* 39 */
c5c3b17a0a5668fadc604f16c1b9961bb6a0f249David Lawrence "socket is not connected", /* 40 */
d10733a5a3bf2c9ec3b5106e5f268cfd2ffebbc2Michael Graff "out of range", /* 41 */
d2e9c94edbc8d9a32216e69ef812c00955a916e0Michael Graff "out of entropy", /* 42 */
1f5eafd61b4c4271dbcf9d9106a722148e51d5ebDavid Lawrence "invalid use of multicast address", /* 43 */
1f5eafd61b4c4271dbcf9d9106a722148e51d5ebDavid Lawrence "not a file", /* 44 */
96e79f7ede9fd09c79ac6452ab09e4e48b288e4dMichael Graff "not a directory", /* 45 */
96e79f7ede9fd09c79ac6452ab09e4e48b288e4dMichael Graff "queue is full" /* 46 */
fda0ab6a96a7edb1acbde0c77ead0542979b0f24Bob Halley};
fda0ab6a96a7edb1acbde0c77ead0542979b0f24Bob Halley
5804af97ab0875fafd233f486aa1c1899ed5c00aBob Halley#define ISC_RESULT_RESULTSET 2
5804af97ab0875fafd233f486aa1c1899ed5c00aBob Halley#define ISC_RESULT_UNAVAILABLESET 3
5804af97ab0875fafd233f486aa1c1899ed5c00aBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halleystatic isc_once_t once = ISC_ONCE_INIT;
e4e071ae12aee942fefc2c0a3280e402938669deBob Halleystatic ISC_LIST(resulttable) tables;
e4e071ae12aee942fefc2c0a3280e402938669deBob Halleystatic isc_mutex_t lock;
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halleystatic isc_result_t
87cafc5e70f79f2586d067fbdd64f61bbab069d2David Lawrenceregister_table(unsigned int base, unsigned int nresults, const char **text,
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley isc_msgcat_t *msgcat, int set)
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley{
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley resulttable *table;
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley REQUIRE(base % ISC_RESULTCLASS_SIZE == 0);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley REQUIRE(nresults <= ISC_RESULTCLASS_SIZE);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley REQUIRE(text != NULL);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley /*
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley * We use malloc() here because we we want to be able to use
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley * isc_result_totext() even if there is no memory context.
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley */
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley table = malloc(sizeof *table);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley if (table == NULL)
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley return (ISC_R_NOMEMORY);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley table->base = base;
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley table->last = base + nresults;
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley table->text = text;
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley table->msgcat = msgcat;
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley table->set = set;
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley ISC_LINK_INIT(table, link);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley LOCK(&lock);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley ISC_LIST_APPEND(tables, table, link);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley UNLOCK(&lock);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley return (ISC_R_SUCCESS);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley}
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halleystatic void
e4e071ae12aee942fefc2c0a3280e402938669deBob Halleyinitialize_action(void) {
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley isc_result_t result;
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley RUNTIME_CHECK(isc_mutex_init(&lock) == ISC_R_SUCCESS);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley ISC_LIST_INIT(tables);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley result = register_table(ISC_RESULTCLASS_ISC, ISC_R_NRESULTS, text,
5804af97ab0875fafd233f486aa1c1899ed5c00aBob Halley isc_msgcat, ISC_RESULT_RESULTSET);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley if (result != ISC_R_SUCCESS)
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley UNEXPECTED_ERROR(__FILE__, __LINE__,
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley "register_table() failed: %u", result);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley}
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halleystatic void
e4e071ae12aee942fefc2c0a3280e402938669deBob Halleyinitialize(void) {
96754ed7b400ce080279de2f92111ad868105290Bob Halley isc_lib_initmsgcat();
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley}
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
87cafc5e70f79f2586d067fbdd64f61bbab069d2David Lawrenceconst char *
3cddfdd29969046e598e9465c72936ccd30df233Bob Halleyisc_result_totext(isc_result_t result) {
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley resulttable *table;
87cafc5e70f79f2586d067fbdd64f61bbab069d2David Lawrence const char *text, *default_text;
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley int index;
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley initialize();
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley LOCK(&lock);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
5804af97ab0875fafd233f486aa1c1899ed5c00aBob Halley text = NULL;
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley for (table = ISC_LIST_HEAD(tables);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley table != NULL;
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley table = ISC_LIST_NEXT(table, link)) {
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley if (result >= table->base && result <= table->last) {
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley index = (int)(result - table->base);
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley default_text = table->text[index];
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley /*
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley * Note: we use 'index + 1' as the message number
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley * instead of index because isc_msgcat_get() requires
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley * the message number to be > 0.
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley */
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley text = isc_msgcat_get(table->msgcat, table->set,
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley index + 1, default_text);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley break;
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley }
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley }
5804af97ab0875fafd233f486aa1c1899ed5c00aBob Halley if (text == NULL)
5804af97ab0875fafd233f486aa1c1899ed5c00aBob Halley text = isc_msgcat_get(isc_msgcat, ISC_RESULT_UNAVAILABLESET,
5804af97ab0875fafd233f486aa1c1899ed5c00aBob Halley 1, "(result code text not available)");
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley UNLOCK(&lock);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley return (text);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley}
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halleyisc_result_t
87cafc5e70f79f2586d067fbdd64f61bbab069d2David Lawrenceisc_result_register(unsigned int base, unsigned int nresults,
87cafc5e70f79f2586d067fbdd64f61bbab069d2David Lawrence const char **text, isc_msgcat_t *msgcat, int set)
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley{
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley initialize();
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley return (register_table(base, nresults, text, msgcat, set));
fda0ab6a96a7edb1acbde0c77ead0542979b0f24Bob Halley}