unified result type support

This commit is contained in:
Bob Halley 1999-06-18 22:35:18 +00:00
parent c84bdaa684
commit e4e071ae12
5 changed files with 247 additions and 69 deletions

View file

@ -19,66 +19,68 @@
#define DNS_RESULT_H 1
#include <isc/lang.h>
#include <isc/result.h>
#include <isc/resultclass.h>
ISC_LANG_BEGINDECLS
typedef unsigned int dns_result_t;
typedef isc_result_t dns_result_t; /* XXXRTH for legacy use only */
#define DNS_R_SUCCESS 0
#define DNS_R_NOMEMORY 1
#define DNS_R_NOSPACE 2
#define DNS_R_LABELTOOLONG 3
#define DNS_R_BADESCAPE 4
#define DNS_R_BADBITSTRING 5
#define DNS_R_BITSTRINGTOOLONG 6
#define DNS_R_EMPTYLABEL 7
#define DNS_R_BADDOTTEDQUAD 8
#define DNS_R_UNEXPECTEDEND 9
#define DNS_R_NOTIMPLEMENTED 10
#define DNS_R_UNKNOWN 11
#define DNS_R_BADLABELTYPE 12
#define DNS_R_BADPOINTER 13
#define DNS_R_TOOMANYHOPS 14
#define DNS_R_DISALLOWED 15
#define DNS_R_NOMORE 16
#define DNS_R_EXTRATOKEN 17
#define DNS_R_EXTRADATA 18
#define DNS_R_TEXTTOLONG 19
#define DNS_R_RANGE 20
#define DNS_R_EXISTS 21
#define DNS_R_NOTFOUND 22
#define DNS_R_SYNTAX 23
#define DNS_R_BADCKSUM 24
#define DNS_R_BADAAAA 25
#define DNS_R_NOOWNER 26
#define DNS_R_NOTTL 27
#define DNS_R_BADCLASS 28
#define DNS_R_UNEXPECTEDTOKEN 29
#define DNS_R_BADBASE64 30
#define DNS_R_PARTIALMATCH 31
#define DNS_R_NEWORIGIN 32
#define DNS_R_UNCHANGED 33
#define DNS_R_BADTTL 34
#define DNS_R_NOREDATA 35
#define DNS_R_CONTINUE 36
#define DNS_R_DELEGATION 37
#define DNS_R_GLUE 38
#define DNS_R_DNAME 39
#define DNS_R_CNAME 40
#define DNS_R_NXDOMAIN 41
#define DNS_R_NXRDATASET 42
#define DNS_R_BADDB 43
#define DNS_R_ZONECUT 44
#define DNS_R_FORMERR 45
#define DNS_R_BADZONE 46 /* XXX MPA*/
#define DNS_R_TIMEDOUT 47
#define DNS_R_CANCELED 48
#define DNS_R_SUCCESS (ISC_RESULTCLASS_DNS + 0)
#define DNS_R_NOMEMORY (ISC_RESULTCLASS_DNS + 1)
#define DNS_R_NOSPACE (ISC_RESULTCLASS_DNS + 2)
#define DNS_R_LABELTOOLONG (ISC_RESULTCLASS_DNS + 3)
#define DNS_R_BADESCAPE (ISC_RESULTCLASS_DNS + 4)
#define DNS_R_BADBITSTRING (ISC_RESULTCLASS_DNS + 5)
#define DNS_R_BITSTRINGTOOLONG (ISC_RESULTCLASS_DNS + 6)
#define DNS_R_EMPTYLABEL (ISC_RESULTCLASS_DNS + 7)
#define DNS_R_BADDOTTEDQUAD (ISC_RESULTCLASS_DNS + 8)
#define DNS_R_UNEXPECTEDEND (ISC_RESULTCLASS_DNS + 9)
#define DNS_R_NOTIMPLEMENTED (ISC_RESULTCLASS_DNS + 10)
#define DNS_R_UNKNOWN (ISC_RESULTCLASS_DNS + 11)
#define DNS_R_BADLABELTYPE (ISC_RESULTCLASS_DNS + 12)
#define DNS_R_BADPOINTER (ISC_RESULTCLASS_DNS + 13)
#define DNS_R_TOOMANYHOPS (ISC_RESULTCLASS_DNS + 14)
#define DNS_R_DISALLOWED (ISC_RESULTCLASS_DNS + 15)
#define DNS_R_NOMORE (ISC_RESULTCLASS_DNS + 16)
#define DNS_R_EXTRATOKEN (ISC_RESULTCLASS_DNS + 17)
#define DNS_R_EXTRADATA (ISC_RESULTCLASS_DNS + 18)
#define DNS_R_TEXTTOLONG (ISC_RESULTCLASS_DNS + 19)
#define DNS_R_RANGE (ISC_RESULTCLASS_DNS + 20)
#define DNS_R_EXISTS (ISC_RESULTCLASS_DNS + 21)
#define DNS_R_NOTFOUND (ISC_RESULTCLASS_DNS + 22)
#define DNS_R_SYNTAX (ISC_RESULTCLASS_DNS + 23)
#define DNS_R_BADCKSUM (ISC_RESULTCLASS_DNS + 24)
#define DNS_R_BADAAAA (ISC_RESULTCLASS_DNS + 25)
#define DNS_R_NOOWNER (ISC_RESULTCLASS_DNS + 26)
#define DNS_R_NOTTL (ISC_RESULTCLASS_DNS + 27)
#define DNS_R_BADCLASS (ISC_RESULTCLASS_DNS + 28)
#define DNS_R_UNEXPECTEDTOKEN (ISC_RESULTCLASS_DNS + 29)
#define DNS_R_BADBASE64 (ISC_RESULTCLASS_DNS + 30)
#define DNS_R_PARTIALMATCH (ISC_RESULTCLASS_DNS + 31)
#define DNS_R_NEWORIGIN (ISC_RESULTCLASS_DNS + 32)
#define DNS_R_UNCHANGED (ISC_RESULTCLASS_DNS + 33)
#define DNS_R_BADTTL (ISC_RESULTCLASS_DNS + 34)
#define DNS_R_NOREDATA (ISC_RESULTCLASS_DNS + 35)
#define DNS_R_CONTINUE (ISC_RESULTCLASS_DNS + 36)
#define DNS_R_DELEGATION (ISC_RESULTCLASS_DNS + 37)
#define DNS_R_GLUE (ISC_RESULTCLASS_DNS + 38)
#define DNS_R_DNAME (ISC_RESULTCLASS_DNS + 39)
#define DNS_R_CNAME (ISC_RESULTCLASS_DNS + 40)
#define DNS_R_NXDOMAIN (ISC_RESULTCLASS_DNS + 41)
#define DNS_R_NXRDATASET (ISC_RESULTCLASS_DNS + 42)
#define DNS_R_BADDB (ISC_RESULTCLASS_DNS + 43)
#define DNS_R_ZONECUT (ISC_RESULTCLASS_DNS + 44)
#define DNS_R_FORMERR (ISC_RESULTCLASS_DNS + 45)
#define DNS_R_BADZONE (ISC_RESULTCLASS_DNS + 46) /* XXX MPA*/
#define DNS_R_TIMEDOUT (ISC_RESULTCLASS_DNS + 47)
#define DNS_R_CANCELED (ISC_RESULTCLASS_DNS + 48)
#define DNS_R_UNEXPECTED (ISC_RESULTCLASS_DNS + 49)
#define DNS_R_LASTENTRY 48 /* Last entry on list. */
#define DNS_R_UNEXPECTED 0xFFFFFFFFL
#define DNS_R_NRESULTS 50 /* Number of results */
char * dns_result_totext(dns_result_t);
void dns_result_register(void);
ISC_LANG_ENDDECLS

View file

@ -17,9 +17,14 @@
#include <config.h>
#include <stddef.h>
#include <isc/resultclass.h>
#include <isc/once.h>
#include <isc/error.h>
#include <dns/result.h>
static char *text_table[DNS_R_LASTENTRY + 1] = {
static char *text[DNS_R_NRESULTS] = {
"success", /* 0 */
"out of memory", /* 1 */
"ran out of space", /* 2 */
@ -69,13 +74,35 @@ static char *text_table[DNS_R_LASTENTRY + 1] = {
"bad zone", /* 46 */
"timed out", /* 47 */
"canceled", /* 48 */
"unexpected error", /* 49 */
};
static isc_once_t once = ISC_ONCE_INIT;
static void
initialize_action(void) {
isc_result_t result;
result = isc_result_register(ISC_RESULTCLASS_DNS, DNS_R_NRESULTS,
text);
if (result != ISC_R_SUCCESS)
UNEXPECTED_ERROR(__FILE__, __LINE__,
"isc_result_register() failed: %u", result);
}
static void
initialize(void) {
RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
}
char *
dns_result_totext(dns_result_t result) {
if (result == DNS_R_UNEXPECTED)
return ("unexpected error");
if (result > DNS_R_LASTENTRY)
return ("unknown result code");
return (text_table[result]);
initialize();
return (isc_result_totext(result));
}
void
dns_result_register(void) {
initialize();
}

View file

@ -18,7 +18,9 @@
#ifndef ISC_RESULT_H
#define ISC_RESULT_H 1
#include <isc/boolean.h>
#include <isc/lang.h>
#include <isc/list.h>
ISC_LANG_BEGINDECLS
@ -57,11 +59,15 @@ typedef unsigned int isc_result_t;
#define ISC_R_INVALIDFILE 30
#define ISC_R_BADBASE64 31
#define ISC_R_UNEXPECTEDTOKEN 32
#define ISC_R_LASTENTRY 32 /* last entry in the list */
#define ISC_R_QUOTA 33
#define ISC_R_UNEXPECTED 34
#define ISC_R_UNEXPECTED 0xFFFFFFFFL
#define ISC_R_NRESULTS 35 /* Number of results */
char * isc_result_totext(isc_result_t);
char * isc_result_totext(isc_result_t);
isc_result_t isc_result_register(unsigned int base,
unsigned int nresults,
char **text);
ISC_LANG_ENDDECLS

View file

@ -0,0 +1,51 @@
/*
* Copyright (C) 1999 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
#ifndef ISC_RESULTCLASS_H
#define ISC_RESULTCLASS_H 1
#include <isc/lang.h>
ISC_LANG_BEGINDECLS
/*****
***** Registry of Predefined Result Type Classes
*****/
/*
* A result class is an unsigned 16 bit number. Each class may contain up
* to 65536 results. A result is formed by adding the result number
* within the class to the class number.
*/
#define ISC_RESULTCLASS(rclass) ((rclass) << 16)
#define ISC_RESULTCLASS_SIZE 65536
/*
* Classes < 1024 are reserved for ISC use.
*/
#define ISC_RESULTCLASS_ISC ISC_RESULTCLASS(0)
#define ISC_RESULTCLASS_DNS ISC_RESULTCLASS(1)
/*
* Result classes >= 1024 and <= 65535 are reserved for application use.
*/
ISC_LANG_ENDDECLS
#endif /* ISC_RESULTCLASS_H */

View file

@ -17,9 +17,26 @@
#include <config.h>
#include <isc/result.h>
#include <stddef.h>
#include <stdlib.h>
static char *text_table[ISC_R_LASTENTRY + 1] = {
#include <isc/result.h>
#include <isc/resultclass.h>
#include <isc/assertions.h>
#include <isc/error.h>
#include <isc/mutex.h>
#include <isc/once.h>
#include "util.h"
typedef struct resulttable {
unsigned int base;
unsigned int last;
char ** text;
ISC_LINK(struct resulttable) link;
} resulttable;
static char *text[ISC_R_NRESULTS] = {
"success", /* 0 */
"out of memory", /* 1 */
"timed out", /* 2 */
@ -53,13 +70,88 @@ static char *text_table[ISC_R_LASTENTRY + 1] = {
"invalid file", /* 30 */
"bad base64 encoding", /* 31 */
"unexpected token", /* 32 */
"quota reached", /* 33 */
"unexpected error", /* 34 */
};
static isc_once_t once = ISC_ONCE_INIT;
static ISC_LIST(resulttable) tables;
static isc_mutex_t lock;
static isc_result_t
register_table(unsigned int base, unsigned int nresults, char **text) {
resulttable *table;
REQUIRE(base % ISC_RESULTCLASS_SIZE == 0);
REQUIRE(nresults <= ISC_RESULTCLASS_SIZE);
REQUIRE(text != NULL);
/*
* We use malloc() here because we we want to be able to use
* isc_result_totext() even if there is no memory context.
*/
table = malloc(sizeof *table);
if (table == NULL)
return (ISC_R_NOMEMORY);
table->base = base;
table->last = base + nresults;
table->text = text;
ISC_LINK_INIT(table, link);
LOCK(&lock);
ISC_LIST_APPEND(tables, table, link);
UNLOCK(&lock);
return (ISC_R_SUCCESS);
}
static void
initialize_action(void) {
isc_result_t result;
RUNTIME_CHECK(isc_mutex_init(&lock) == ISC_R_SUCCESS);
ISC_LIST_INIT(tables);
result = register_table(ISC_RESULTCLASS_ISC, ISC_R_NRESULTS, text);
if (result != ISC_R_SUCCESS)
UNEXPECTED_ERROR(__FILE__, __LINE__,
"register_table() failed: %u", result);
}
static void
initialize(void) {
RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
}
char *
isc_result_totext(isc_result_t result) {
if (result == ISC_R_UNEXPECTED)
return ("unexpected error");
if (result > ISC_R_LASTENTRY)
return ("unknown result code");
return (text_table[result]);
resulttable *table;
char *text;
initialize();
LOCK(&lock);
text = "(result code text not available)";
for (table = ISC_LIST_HEAD(tables);
table != NULL;
table = ISC_LIST_NEXT(table, link)) {
if (result >= table->base && result <= table->last) {
text = table->text[result - table->base];
break;
}
}
UNLOCK(&lock);
return (text);
}
isc_result_t
isc_result_register(unsigned int base, unsigned int nresults, char **text) {
initialize();
return (register_table(base, nresults, text));
}