Blame lib/isc/result.c

Packit 5ce601
/*
Packit 5ce601
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
Packit 5ce601
 *
Packit 5ce601
 * This Source Code Form is subject to the terms of the Mozilla Public
Packit 5ce601
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit Service 704ed8
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
Packit 5ce601
 *
Packit 5ce601
 * See the COPYRIGHT file distributed with this work for additional
Packit 5ce601
 * information regarding copyright ownership.
Packit 5ce601
 */
Packit 5ce601
Packit 5ce601
/*! \file */
Packit 5ce601
Packit 5ce601
#include <config.h>
Packit 5ce601
Packit 5ce601
#include <stddef.h>
Packit 5ce601
#include <stdlib.h>
Packit 5ce601
Packit 5ce601
#include <isc/lib.h>
Packit 5ce601
#include <isc/msgs.h>
Packit 5ce601
#include <isc/mutex.h>
Packit 5ce601
#include <isc/once.h>
Packit 5ce601
#include <isc/resultclass.h>
Packit 5ce601
#include <isc/util.h>
Packit 5ce601
Packit 5ce601
typedef struct resulttable {
Packit 5ce601
	unsigned int				base;
Packit 5ce601
	unsigned int				last;
Packit 5ce601
	const char **				text;
Packit 5ce601
	isc_msgcat_t *				msgcat;
Packit 5ce601
	int					set;
Packit 5ce601
	ISC_LINK(struct resulttable)		link;
Packit 5ce601
} resulttable;
Packit 5ce601
Packit 5ce601
typedef ISC_LIST(resulttable) resulttable_list_t;
Packit 5ce601
Packit 5ce601
static const char *description[ISC_R_NRESULTS] = {
Packit 5ce601
	"success",				/*%< 0 */
Packit 5ce601
	"out of memory",			/*%< 1 */
Packit 5ce601
	"timed out",				/*%< 2 */
Packit 5ce601
	"no available threads",			/*%< 3 */
Packit 5ce601
	"address not available",		/*%< 4 */
Packit 5ce601
	"address in use",			/*%< 5 */
Packit 5ce601
	"permission denied",			/*%< 6 */
Packit 5ce601
	"no pending connections",		/*%< 7 */
Packit 5ce601
	"network unreachable",			/*%< 8 */
Packit 5ce601
	"host unreachable",			/*%< 9 */
Packit 5ce601
	"network down",				/*%< 10 */
Packit 5ce601
	"host down",				/*%< 11 */
Packit 5ce601
	"connection refused",			/*%< 12 */
Packit 5ce601
	"not enough free resources",		/*%< 13 */
Packit 5ce601
	"end of file",				/*%< 14 */
Packit 5ce601
	"socket already bound",			/*%< 15 */
Packit 5ce601
	"reload",				/*%< 16 */
Packit 5ce601
	"lock busy",				/*%< 17 */
Packit 5ce601
	"already exists",			/*%< 18 */
Packit 5ce601
	"ran out of space",			/*%< 19 */
Packit 5ce601
	"operation canceled",			/*%< 20 */
Packit 5ce601
	"socket is not bound",			/*%< 21 */
Packit 5ce601
	"shutting down",			/*%< 22 */
Packit 5ce601
	"not found",				/*%< 23 */
Packit 5ce601
	"unexpected end of input",		/*%< 24 */
Packit 5ce601
	"failure",				/*%< 25 */
Packit 5ce601
	"I/O error",				/*%< 26 */
Packit 5ce601
	"not implemented",			/*%< 27 */
Packit 5ce601
	"unbalanced parentheses",		/*%< 28 */
Packit 5ce601
	"no more",				/*%< 29 */
Packit 5ce601
	"invalid file",				/*%< 30 */
Packit 5ce601
	"bad base64 encoding",			/*%< 31 */
Packit 5ce601
	"unexpected token",			/*%< 32 */
Packit 5ce601
	"quota reached",			/*%< 33 */
Packit 5ce601
	"unexpected error",			/*%< 34 */
Packit 5ce601
	"already running",			/*%< 35 */
Packit 5ce601
	"ignore",				/*%< 36 */
Packit 5ce601
	"address mask not contiguous",		/*%< 37 */
Packit 5ce601
	"file not found",			/*%< 38 */
Packit 5ce601
	"file already exists",			/*%< 39 */
Packit 5ce601
	"socket is not connected",		/*%< 40 */
Packit 5ce601
	"out of range",				/*%< 41 */
Packit 5ce601
	"out of entropy",			/*%< 42 */
Packit 5ce601
	"invalid use of multicast address",	/*%< 43 */
Packit 5ce601
	"not a file",				/*%< 44 */
Packit 5ce601
	"not a directory",			/*%< 45 */
Packit 5ce601
	"queue is full",			/*%< 46 */
Packit 5ce601
	"address family mismatch",		/*%< 47 */
Packit 5ce601
	"address family not supported",		/*%< 48 */
Packit 5ce601
	"bad hex encoding",			/*%< 49 */
Packit 5ce601
	"too many open files",			/*%< 50 */
Packit 5ce601
	"not blocking",				/*%< 51 */
Packit 5ce601
	"unbalanced quotes",			/*%< 52 */
Packit 5ce601
	"operation in progress",		/*%< 53 */
Packit 5ce601
	"connection reset",			/*%< 54 */
Packit 5ce601
	"soft quota reached",			/*%< 55 */
Packit 5ce601
	"not a valid number",			/*%< 56 */
Packit 5ce601
	"disabled",				/*%< 57 */
Packit 5ce601
	"max size",				/*%< 58 */
Packit 5ce601
	"invalid address format",		/*%< 59 */
Packit 5ce601
	"bad base32 encoding",			/*%< 60 */
Packit 5ce601
	"unset",				/*%< 61 */
Packit 5ce601
	"multiple",				/*%< 62 */
Packit 5ce601
	"would block",				/*%< 63 */
Packit 5ce601
	"complete",				/*%< 64 */
Packit 5ce601
	"crypto failure",			/*%< 65 */
Packit 5ce601
	"disc quota",				/*%< 66 */
Packit 5ce601
	"disc full",				/*%< 67 */
Packit 5ce601
	"default",				/*%< 68 */
Packit 5ce601
	"IPv4 prefix",				/*%< 69 */
Packit 5ce601
};
Packit 5ce601
Packit 5ce601
static const char *identifier[ISC_R_NRESULTS] = {
Packit 5ce601
	"ISC_R_SUCCESS",
Packit 5ce601
	"ISC_R_NOMEMORY",
Packit 5ce601
	"ISC_R_TIMEDOUT",
Packit 5ce601
	"ISC_R_NOTHREADS",
Packit 5ce601
	"ISC_R_ADDRNOTAVAIL",
Packit 5ce601
	"ISC_R_ADDRINUSE",
Packit 5ce601
	"ISC_R_NOPERM",
Packit 5ce601
	"ISC_R_NOCONN",
Packit 5ce601
	"ISC_R_NETUNREACH",
Packit 5ce601
	"ISC_R_HOSTUNREACH",
Packit 5ce601
	"ISC_R_NETDOWN",
Packit 5ce601
	"ISC_R_HOSTDOWN",
Packit 5ce601
	"ISC_R_CONNREFUSED",
Packit 5ce601
	"ISC_R_NORESOURCES",
Packit 5ce601
	"ISC_R_EOF",
Packit 5ce601
	"ISC_R_BOUND",
Packit 5ce601
	"ISC_R_RELOAD",
Packit 5ce601
	"ISC_R_LOCKBUSY",
Packit 5ce601
	"ISC_R_EXISTS",
Packit 5ce601
	"ISC_R_NOSPACE",
Packit 5ce601
	"ISC_R_CANCELED",
Packit 5ce601
	"ISC_R_NOTBOUND",
Packit 5ce601
	"ISC_R_SHUTTINGDOWN",
Packit 5ce601
	"ISC_R_NOTFOUND",
Packit 5ce601
	"ISC_R_UNEXPECTEDEND",
Packit 5ce601
	"ISC_R_FAILURE",
Packit 5ce601
	"ISC_R_IOERROR",
Packit 5ce601
	"ISC_R_NOTIMPLEMENTED",
Packit 5ce601
	"ISC_R_UNBALANCED",
Packit 5ce601
	"ISC_R_NOMORE",
Packit 5ce601
	"ISC_R_INVALIDFILE",
Packit 5ce601
	"ISC_R_BADBASE64",
Packit 5ce601
	"ISC_R_UNEXPECTEDTOKEN",
Packit 5ce601
	"ISC_R_QUOTA",
Packit 5ce601
	"ISC_R_UNEXPECTED",
Packit 5ce601
	"ISC_R_ALREADYRUNNING",
Packit 5ce601
	"ISC_R_IGNORE",
Packit 5ce601
	"ISC_R_MASKNONCONTIG",
Packit 5ce601
	"ISC_R_FILENOTFOUND",
Packit 5ce601
	"ISC_R_FILEEXISTS",
Packit 5ce601
	"ISC_R_NOTCONNECTED",
Packit 5ce601
	"ISC_R_RANGE",
Packit 5ce601
	"ISC_R_NOENTROPY",
Packit 5ce601
	"ISC_R_MULTICAST",
Packit 5ce601
	"ISC_R_NOTFILE",
Packit 5ce601
	"ISC_R_NOTDIRECTORY",
Packit 5ce601
	"ISC_R_QUEUEFULL",
Packit 5ce601
	"ISC_R_FAMILYMISMATCH",
Packit 5ce601
	"ISC_R_FAMILYNOSUPPORT",
Packit 5ce601
	"ISC_R_BADHEX",
Packit 5ce601
	"ISC_R_TOOMANYOPENFILES",
Packit 5ce601
	"ISC_R_NOTBLOCKING",
Packit 5ce601
	"ISC_R_UNBALANCEDQUOTES",
Packit 5ce601
	"ISC_R_INPROGRESS",
Packit 5ce601
	"ISC_R_CONNECTIONRESET",
Packit 5ce601
	"ISC_R_SOFTQUOTA",
Packit 5ce601
	"ISC_R_BADNUMBER",
Packit 5ce601
	"ISC_R_DISABLED",
Packit 5ce601
	"ISC_R_MAXSIZE",
Packit 5ce601
	"ISC_R_BADADDRESSFORM",
Packit 5ce601
	"ISC_R_BADBASE32",
Packit 5ce601
	"ISC_R_UNSET",
Packit 5ce601
	"ISC_R_MULTIPLE",
Packit 5ce601
	"ISC_R_WOULDBLOCK",
Packit 5ce601
	"ISC_R_COMPLETE",
Packit 5ce601
	"ISC_R_CRYPTOFAILURE",
Packit 5ce601
	"ISC_R_DISCQUOTA",
Packit 5ce601
	"ISC_R_DISCFULL",
Packit 5ce601
	"ISC_R_DEFAULT",
Packit 5ce601
	"ISC_R_IPV4PREFIX",
Packit 5ce601
};
Packit 5ce601
Packit 5ce601
#define ISC_RESULT_RESULTSET			2
Packit 5ce601
#define ISC_RESULT_UNAVAILABLESET		3
Packit 5ce601
Packit 5ce601
static isc_once_t		once = ISC_ONCE_INIT;
Packit 5ce601
static resulttable_list_t	description_tables;
Packit 5ce601
static resulttable_list_t	identifier_tables;
Packit 5ce601
static isc_mutex_t		lock;
Packit 5ce601
Packit 5ce601
static isc_result_t
Packit 5ce601
register_table(resulttable_list_t *tables, unsigned int base,
Packit 5ce601
	       unsigned int nresults, const char **text,
Packit 5ce601
	       isc_msgcat_t *msgcat, int set)
Packit 5ce601
{
Packit 5ce601
	resulttable *table;
Packit 5ce601
Packit 5ce601
	REQUIRE(base % ISC_RESULTCLASS_SIZE == 0);
Packit 5ce601
	REQUIRE(nresults <= ISC_RESULTCLASS_SIZE);
Packit 5ce601
	REQUIRE(text != NULL);
Packit 5ce601
Packit 5ce601
	/*
Packit 5ce601
	 * We use malloc() here because we we want to be able to use
Packit 5ce601
	 * isc_result_totext() even if there is no memory context.
Packit 5ce601
	 */
Packit 5ce601
	table = malloc(sizeof(*table));
Packit 5ce601
	if (table == NULL)
Packit 5ce601
		return (ISC_R_NOMEMORY);
Packit 5ce601
	table->base = base;
Packit 5ce601
	table->last = base + nresults - 1;
Packit 5ce601
	table->text = text;
Packit 5ce601
	table->msgcat = msgcat;
Packit 5ce601
	table->set = set;
Packit 5ce601
	ISC_LINK_INIT(table, link);
Packit 5ce601
Packit 5ce601
	LOCK(&lock);
Packit 5ce601
Packit 5ce601
	ISC_LIST_APPEND(*tables, table, link);
Packit 5ce601
Packit 5ce601
	UNLOCK(&lock);
Packit 5ce601
Packit 5ce601
	return (ISC_R_SUCCESS);
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
static void
Packit 5ce601
initialize_action(void) {
Packit 5ce601
	isc_result_t result;
Packit 5ce601
Packit 5ce601
	RUNTIME_CHECK(isc_mutex_init(&lock) == ISC_R_SUCCESS);
Packit 5ce601
	ISC_LIST_INIT(description_tables);
Packit 5ce601
	ISC_LIST_INIT(identifier_tables);
Packit 5ce601
Packit 5ce601
	result = register_table(&description_tables,
Packit 5ce601
				ISC_RESULTCLASS_ISC, ISC_R_NRESULTS,
Packit 5ce601
				description, isc_msgcat, ISC_RESULT_RESULTSET);
Packit 5ce601
	if (result != ISC_R_SUCCESS)
Packit 5ce601
		UNEXPECTED_ERROR(__FILE__, __LINE__,
Packit 5ce601
				 "register_table() %s: %u",
Packit 5ce601
				 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
Packit 5ce601
						ISC_MSG_FAILED, "failed"),
Packit 5ce601
				 result);
Packit 5ce601
Packit 5ce601
	result = register_table(&identifier_tables,
Packit 5ce601
				ISC_RESULTCLASS_ISC, ISC_R_NRESULTS,
Packit 5ce601
				identifier, isc_msgcat, ISC_RESULT_RESULTSET);
Packit 5ce601
	if (result != ISC_R_SUCCESS)
Packit 5ce601
		UNEXPECTED_ERROR(__FILE__, __LINE__,
Packit 5ce601
				 "register_table() %s: %u",
Packit 5ce601
				 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
Packit 5ce601
						ISC_MSG_FAILED, "failed"),
Packit 5ce601
				 result);
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
static void
Packit 5ce601
initialize(void) {
Packit 5ce601
	isc_lib_initmsgcat();
Packit 5ce601
	RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
static const char *
Packit 5ce601
isc_result_tomany_helper(resulttable_list_t *tables, isc_result_t result) {
Packit 5ce601
	resulttable *table;
Packit 5ce601
	const char *text, *default_text;
Packit 5ce601
	int index;
Packit 5ce601
Packit 5ce601
	initialize();
Packit 5ce601
Packit 5ce601
	LOCK(&lock);
Packit 5ce601
Packit 5ce601
	text = NULL;
Packit 5ce601
	for (table = ISC_LIST_HEAD(*tables);
Packit 5ce601
	     table != NULL;
Packit 5ce601
	     table = ISC_LIST_NEXT(table, link)) {
Packit 5ce601
		if (result >= table->base && result <= table->last) {
Packit 5ce601
			index = (int)(result - table->base);
Packit 5ce601
			default_text = table->text[index];
Packit 5ce601
			/*
Packit 5ce601
			 * Note: we use 'index + 1' as the message number
Packit 5ce601
			 * instead of index because isc_msgcat_get() requires
Packit 5ce601
			 * the message number to be > 0.
Packit 5ce601
			 */
Packit 5ce601
			text = isc_msgcat_get(table->msgcat, table->set,
Packit 5ce601
					      index + 1, default_text);
Packit 5ce601
			break;
Packit 5ce601
		}
Packit 5ce601
	}
Packit 5ce601
	if (text == NULL)
Packit 5ce601
		text = isc_msgcat_get(isc_msgcat, ISC_RESULT_UNAVAILABLESET,
Packit 5ce601
				      1, "(result code text not available)");
Packit 5ce601
Packit 5ce601
	UNLOCK(&lock);
Packit 5ce601
Packit 5ce601
	return (text);
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
const char *
Packit 5ce601
isc_result_totext(isc_result_t result) {
Packit 5ce601
	return (isc_result_tomany_helper(&description_tables, result));
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
const char *
Packit 5ce601
isc_result_toid(isc_result_t result) {
Packit 5ce601
	return (isc_result_tomany_helper(&identifier_tables, result));
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
isc_result_t
Packit 5ce601
isc_result_register(unsigned int base, unsigned int nresults,
Packit 5ce601
		    const char **text, isc_msgcat_t *msgcat, int set)
Packit 5ce601
{
Packit 5ce601
	initialize();
Packit 5ce601
Packit 5ce601
	return (register_table(&description_tables, base, nresults, text,
Packit 5ce601
			       msgcat, set));
Packit 5ce601
}
Packit 5ce601
Packit 5ce601
isc_result_t
Packit 5ce601
isc_result_registerids(unsigned int base, unsigned int nresults,
Packit 5ce601
		       const char **ids, isc_msgcat_t *msgcat, int set)
Packit 5ce601
{
Packit 5ce601
	initialize();
Packit 5ce601
Packit 5ce601
	return (register_table(&identifier_tables, base, nresults, ids,
Packit 5ce601
			       msgcat, set));
Packit 5ce601
}