17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
37c478bd9Sstevel@tonic-gate * Copyright (c) 2001 by Internet Software Consortium.
47c478bd9Sstevel@tonic-gate *
57c478bd9Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any
67c478bd9Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate *
9*9525b14bSRao Shoaib * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*9525b14bSRao Shoaib * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*9525b14bSRao Shoaib * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12*9525b14bSRao Shoaib * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*9525b14bSRao Shoaib * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*9525b14bSRao Shoaib * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*9525b14bSRao Shoaib * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate */
177c478bd9Sstevel@tonic-gate
187c478bd9Sstevel@tonic-gate #include <port_before.h>
197c478bd9Sstevel@tonic-gate #include <netdb.h>
207c478bd9Sstevel@tonic-gate #include <port_after.h>
217c478bd9Sstevel@tonic-gate
227c478bd9Sstevel@tonic-gate #ifdef DO_PTHREADS
237c478bd9Sstevel@tonic-gate #include <pthread.h>
247c478bd9Sstevel@tonic-gate #include <stdlib.h>
257c478bd9Sstevel@tonic-gate #endif
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate static const char *gai_errlist[] = {
287c478bd9Sstevel@tonic-gate "no error",
29*9525b14bSRao Shoaib "address family not supported for name",/*%< EAI_ADDRFAMILY */
30*9525b14bSRao Shoaib "temporary failure", /*%< EAI_AGAIN */
31*9525b14bSRao Shoaib "invalid flags", /*%< EAI_BADFLAGS */
32*9525b14bSRao Shoaib "permanent failure", /*%< EAI_FAIL */
33*9525b14bSRao Shoaib "address family not supported", /*%< EAI_FAMILY */
34*9525b14bSRao Shoaib "memory failure", /*%< EAI_MEMORY */
35*9525b14bSRao Shoaib "no address", /*%< EAI_NODATA */
36*9525b14bSRao Shoaib "unknown name or service", /*%< EAI_NONAME */
37*9525b14bSRao Shoaib "service not supported for socktype", /*%< EAI_SERVICE */
38*9525b14bSRao Shoaib "socktype not supported", /*%< EAI_SOCKTYPE */
39*9525b14bSRao Shoaib "system failure", /*%< EAI_SYSTEM */
40*9525b14bSRao Shoaib "bad hints", /*%< EAI_BADHINTS */
41*9525b14bSRao Shoaib "bad protocol", /*%< EAI_PROTOCOL */
42*9525b14bSRao Shoaib "unknown error" /*%< Must be last. */
437c478bd9Sstevel@tonic-gate };
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate static const int gai_nerr = (sizeof(gai_errlist)/sizeof(*gai_errlist));
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate #define EAI_BUFSIZE 128
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate const char *
gai_strerror(int ecode)507c478bd9Sstevel@tonic-gate gai_strerror(int ecode) {
517c478bd9Sstevel@tonic-gate #ifndef DO_PTHREADS
527c478bd9Sstevel@tonic-gate static char buf[EAI_BUFSIZE];
537c478bd9Sstevel@tonic-gate #else /* DO_PTHREADS */
54*9525b14bSRao Shoaib #ifndef LIBBIND_MUTEX_INITIALIZER
55*9525b14bSRao Shoaib #define LIBBIND_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
56*9525b14bSRao Shoaib #endif
57*9525b14bSRao Shoaib static pthread_mutex_t lock = LIBBIND_MUTEX_INITIALIZER;
58*9525b14bSRao Shoaib static pthread_key_t key;
59*9525b14bSRao Shoaib static int once = 0;
607c478bd9Sstevel@tonic-gate char *buf;
617c478bd9Sstevel@tonic-gate #endif
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate if (ecode >= 0 && ecode < (gai_nerr - 1))
647c478bd9Sstevel@tonic-gate return (gai_errlist[ecode]);
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate #ifdef DO_PTHREADS
67*9525b14bSRao Shoaib if (!once) {
68*9525b14bSRao Shoaib if (pthread_mutex_lock(&lock) != 0)
69168c2130Sjs198686 goto unknown;
70*9525b14bSRao Shoaib if (!once) {
71*9525b14bSRao Shoaib if (pthread_key_create(&key, free) != 0) {
72*9525b14bSRao Shoaib (void)pthread_mutex_unlock(&lock);
73*9525b14bSRao Shoaib goto unknown;
74*9525b14bSRao Shoaib }
75*9525b14bSRao Shoaib once = 1;
76*9525b14bSRao Shoaib }
77*9525b14bSRao Shoaib if (pthread_mutex_unlock(&lock) != 0)
78*9525b14bSRao Shoaib goto unknown;
79*9525b14bSRao Shoaib }
80*9525b14bSRao Shoaib
817c478bd9Sstevel@tonic-gate buf = pthread_getspecific(key);
827c478bd9Sstevel@tonic-gate if (buf == NULL) {
837c478bd9Sstevel@tonic-gate buf = malloc(EAI_BUFSIZE);
847c478bd9Sstevel@tonic-gate if (buf == NULL)
85168c2130Sjs198686 goto unknown;
86168c2130Sjs198686 if (pthread_setspecific(key, buf) != 0) {
87168c2130Sjs198686 free(buf);
88168c2130Sjs198686 goto unknown;
89168c2130Sjs198686 }
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate #endif
927c478bd9Sstevel@tonic-gate /*
937c478bd9Sstevel@tonic-gate * XXX This really should be snprintf(buf, EAI_BUFSIZE, ...).
947c478bd9Sstevel@tonic-gate * It is safe until message catalogs are used.
957c478bd9Sstevel@tonic-gate */
967c478bd9Sstevel@tonic-gate sprintf(buf, "%s: %d", gai_errlist[gai_nerr - 1], ecode);
977c478bd9Sstevel@tonic-gate return (buf);
98168c2130Sjs198686
99168c2130Sjs198686 #ifdef DO_PTHREADS
100168c2130Sjs198686 unknown:
101168c2130Sjs198686 return ("unknown error");
102168c2130Sjs198686 #endif
1037c478bd9Sstevel@tonic-gate }
104*9525b14bSRao Shoaib
105*9525b14bSRao Shoaib /*! \file */
106