From b878aa88eb8bb7ab74e24314e87c88586f15598d Mon Sep 17 00:00:00 2001 From: Chris Hall Date: Sun, 11 Apr 2010 11:17:37 +0100 Subject: Support gai_strerror() and tidy bgp_listener code. Added support for EAI_XXX error names and gai_strerror() error messages. Tidied up bgp_listener set up to remove #if skips around the "old" way -- so that the older code doesn't simply rot away. --- lib/errno_names.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) (limited to 'lib/errno_names.c') diff --git a/lib/errno_names.c b/lib/errno_names.c index 51f430a9..791f8001 100644 --- a/lib/errno_names.c +++ b/lib/errno_names.c @@ -21,6 +21,8 @@ #include #include +#include + #include "errno_names.h" /*============================================================================== @@ -291,7 +293,7 @@ static const char* errno_name_table[] = enum { errno_last = (sizeof(errno_name_table) / sizeof(char*)) - 1 } ; -/*============================================================================== +/*------------------------------------------------------------------------------ * Lookup the name for given error number. * * Returns: address of string, or NULL if not known @@ -308,4 +310,60 @@ errno_name_lookup(int err) return errno_name_table[err] ; } ; +/*============================================================================== + * Table to map EAI error number to its name -- the errors generated by + * getaddrinfo() and getnameinfo(). + * + * At least one system uses -ve numbers for these... so the following will + * support either +ve or -ve values, provided that all have the same sign. + */ +#if EAI_AGAIN < 0 +enum { eai_sgn = -1 } ; +#else +enum { eai_sgn = +1 } ; +#endif + +#define EAINO(eai) [eai * eai_sgn] = #eai + +static const char* eaino_name_table[] = +{ + /* Error number for no error + * + * (123456789012345), /-- no name is more than 15 characters + */ + EAINO(EAI_OK), /* No error */ + /* POSIX Error Numbers -- taken Open Group Base Specifications Issue 7 + * IEEE Std 1003.1-2008 + */ + EAINO(EAI_AGAIN), /* Temporary failure in name resolution. */ + EAINO(EAI_BADFLAGS), /* Invalid value for 'ai_flags' field. */ + EAINO(EAI_FAIL), /* Non-recoverable failure in name res. */ + EAINO(EAI_FAMILY), /* 'ai_family' not supported. */ + EAINO(EAI_MEMORY), /* Memory allocation failure. */ + EAINO(EAI_NONAME), /* NAME or SERVICE is unknown. */ + EAINO(EAI_OVERFLOW), /* Argument buffer overflow. */ + EAINO(EAI_SERVICE), /* SERVICE not supported for 'ai_socktype'. */ + EAINO(EAI_SOCKTYPE), /* 'ai_socktype' not supported. */ + EAINO(EAI_SYSTEM), /* System error returned in 'errno'. */ +} ; + +enum { eaino_last = (sizeof(eaino_name_table) / sizeof(char*)) - 1 } ; + +/*------------------------------------------------------------------------------ + * Lookup the name for given error number. + * + * Returns: address of string, or NULL if not known + * + * NB: for 0 returns "EOK". + * + * NB: async-signal-safe and thread-safe ! + */ +extern const char* +eaino_name_lookup(int eai) +{ + eai *= eai_sgn ; + if ((eai < 0) || (eai > eaino_last)) + return NULL ; + return eaino_name_table[eai] ; +} ; -- cgit v1.2.3