2N/A/*
2N/A * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
2N/A * Copyright (c) 2001 by Internet Software Consortium.
2N/A *
2N/A * Permission to use, copy, modify, and distribute this software for any
2N/A * purpose with or without fee is hereby granted, provided that the above
2N/A * copyright notice and this permission notice appear in all copies.
2N/A *
2N/A * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
2N/A * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
2N/A * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
2N/A * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
2N/A * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
2N/A * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
2N/A * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2N/A */
2N/A
2N/A#include <port_before.h>
2N/A#include <netdb.h>
2N/A#include <port_after.h>
2N/A
2N/A#ifdef DO_PTHREADS
2N/A#include <pthread.h>
2N/A#include <stdlib.h>
2N/A#endif
2N/A
2N/Astatic const char *gai_errlist[] = {
2N/A "no error",
2N/A "address family not supported for name",/*%< EAI_ADDRFAMILY */
2N/A "temporary failure", /*%< EAI_AGAIN */
2N/A "invalid flags", /*%< EAI_BADFLAGS */
2N/A "permanent failure", /*%< EAI_FAIL */
2N/A "address family not supported", /*%< EAI_FAMILY */
2N/A "memory failure", /*%< EAI_MEMORY */
2N/A "no address", /*%< EAI_NODATA */
2N/A "unknown name or service", /*%< EAI_NONAME */
2N/A "service not supported for socktype", /*%< EAI_SERVICE */
2N/A "socktype not supported", /*%< EAI_SOCKTYPE */
2N/A "system failure", /*%< EAI_SYSTEM */
2N/A "bad hints", /*%< EAI_BADHINTS */
2N/A "bad protocol", /*%< EAI_PROTOCOL */
2N/A "unknown error" /*%< Must be last. */
2N/A};
2N/A
2N/Astatic const int gai_nerr = (sizeof(gai_errlist)/sizeof(*gai_errlist));
2N/A
2N/A#define EAI_BUFSIZE 128
2N/A
2N/Aconst char *
2N/Agai_strerror(int ecode) {
2N/A#ifndef DO_PTHREADS
2N/A static char buf[EAI_BUFSIZE];
2N/A#else /* DO_PTHREADS */
2N/A#ifndef LIBBIND_MUTEX_INITIALIZER
2N/A#define LIBBIND_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
2N/A#endif
2N/A static pthread_mutex_t lock = LIBBIND_MUTEX_INITIALIZER;
2N/A static pthread_key_t key;
2N/A static int once = 0;
2N/A char *buf;
2N/A#endif
2N/A
2N/A if (ecode >= 0 && ecode < (gai_nerr - 1))
2N/A return (gai_errlist[ecode]);
2N/A
2N/A#ifdef DO_PTHREADS
2N/A if (!once) {
2N/A if (pthread_mutex_lock(&lock) != 0)
2N/A goto unknown;
2N/A if (!once) {
2N/A if (pthread_key_create(&key, free) != 0) {
2N/A (void)pthread_mutex_unlock(&lock);
2N/A goto unknown;
2N/A }
2N/A once = 1;
2N/A }
2N/A if (pthread_mutex_unlock(&lock) != 0)
2N/A goto unknown;
2N/A }
2N/A
2N/A buf = pthread_getspecific(key);
2N/A if (buf == NULL) {
2N/A buf = malloc(EAI_BUFSIZE);
2N/A if (buf == NULL)
2N/A goto unknown;
2N/A if (pthread_setspecific(key, buf) != 0) {
2N/A free(buf);
2N/A goto unknown;
2N/A }
2N/A }
2N/A#endif
2N/A /*
2N/A * XXX This really should be snprintf(buf, EAI_BUFSIZE, ...).
2N/A * It is safe until message catalogs are used.
2N/A */
2N/A sprintf(buf, "%s: %d", gai_errlist[gai_nerr - 1], ecode);
2N/A return (buf);
2N/A
2N/A#ifdef DO_PTHREADS
2N/A unknown:
2N/A return ("unknown error");
2N/A#endif
2N/A}
2N/A
2N/A/*! \file */