xref: /onnv-gate/usr/src/lib/libresolv2/common/irs/gai_strerror.c (revision 11038:74b12212b8a2)
10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
30Sstevel@tonic-gate  * Copyright (c) 2001 by Internet Software Consortium.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
60Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
70Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
80Sstevel@tonic-gate  *
9*11038SRao.Shoaib@Sun.COM  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*11038SRao.Shoaib@Sun.COM  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*11038SRao.Shoaib@Sun.COM  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12*11038SRao.Shoaib@Sun.COM  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*11038SRao.Shoaib@Sun.COM  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*11038SRao.Shoaib@Sun.COM  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*11038SRao.Shoaib@Sun.COM  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
160Sstevel@tonic-gate  */
170Sstevel@tonic-gate 
180Sstevel@tonic-gate #include <port_before.h>
190Sstevel@tonic-gate #include <netdb.h>
200Sstevel@tonic-gate #include <port_after.h>
210Sstevel@tonic-gate 
220Sstevel@tonic-gate #ifdef DO_PTHREADS
230Sstevel@tonic-gate #include <pthread.h>
240Sstevel@tonic-gate #include <stdlib.h>
250Sstevel@tonic-gate #endif
260Sstevel@tonic-gate 
270Sstevel@tonic-gate static const char *gai_errlist[] = {
280Sstevel@tonic-gate 	"no error",
29*11038SRao.Shoaib@Sun.COM 	"address family not supported for name",/*%< EAI_ADDRFAMILY */
30*11038SRao.Shoaib@Sun.COM 	"temporary failure",			/*%< EAI_AGAIN */
31*11038SRao.Shoaib@Sun.COM 	"invalid flags",			/*%< EAI_BADFLAGS */
32*11038SRao.Shoaib@Sun.COM 	"permanent failure",			/*%< EAI_FAIL */
33*11038SRao.Shoaib@Sun.COM 	"address family not supported",		/*%< EAI_FAMILY */
34*11038SRao.Shoaib@Sun.COM 	"memory failure",			/*%< EAI_MEMORY */
35*11038SRao.Shoaib@Sun.COM 	"no address",				/*%< EAI_NODATA */
36*11038SRao.Shoaib@Sun.COM 	"unknown name or service",		/*%< EAI_NONAME */
37*11038SRao.Shoaib@Sun.COM 	"service not supported for socktype",	/*%< EAI_SERVICE */
38*11038SRao.Shoaib@Sun.COM 	"socktype not supported",		/*%< EAI_SOCKTYPE */
39*11038SRao.Shoaib@Sun.COM 	"system failure",			/*%< EAI_SYSTEM */
40*11038SRao.Shoaib@Sun.COM 	"bad hints",				/*%< EAI_BADHINTS */
41*11038SRao.Shoaib@Sun.COM 	"bad protocol",				/*%< EAI_PROTOCOL */
42*11038SRao.Shoaib@Sun.COM 	"unknown error"				/*%< Must be last. */
430Sstevel@tonic-gate };
440Sstevel@tonic-gate 
450Sstevel@tonic-gate static const int gai_nerr = (sizeof(gai_errlist)/sizeof(*gai_errlist));
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #define EAI_BUFSIZE 128
480Sstevel@tonic-gate 
490Sstevel@tonic-gate const char *
gai_strerror(int ecode)500Sstevel@tonic-gate gai_strerror(int ecode) {
510Sstevel@tonic-gate #ifndef DO_PTHREADS
520Sstevel@tonic-gate 	static char buf[EAI_BUFSIZE];
530Sstevel@tonic-gate #else	/* DO_PTHREADS */
54*11038SRao.Shoaib@Sun.COM #ifndef LIBBIND_MUTEX_INITIALIZER
55*11038SRao.Shoaib@Sun.COM #define LIBBIND_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
56*11038SRao.Shoaib@Sun.COM #endif
57*11038SRao.Shoaib@Sun.COM 	static pthread_mutex_t lock = LIBBIND_MUTEX_INITIALIZER;
58*11038SRao.Shoaib@Sun.COM 	static pthread_key_t key;
59*11038SRao.Shoaib@Sun.COM 	static int once = 0;
600Sstevel@tonic-gate 	char *buf;
610Sstevel@tonic-gate #endif
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	if (ecode >= 0 && ecode < (gai_nerr - 1))
640Sstevel@tonic-gate 		return (gai_errlist[ecode]);
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #ifdef DO_PTHREADS
67*11038SRao.Shoaib@Sun.COM         if (!once) {
68*11038SRao.Shoaib@Sun.COM                 if (pthread_mutex_lock(&lock) != 0)
69*11038SRao.Shoaib@Sun.COM 			goto unknown;
70*11038SRao.Shoaib@Sun.COM                 if (!once) {
71*11038SRao.Shoaib@Sun.COM                         if (pthread_key_create(&key, free) != 0) {
72*11038SRao.Shoaib@Sun.COM 				(void)pthread_mutex_unlock(&lock);
73*11038SRao.Shoaib@Sun.COM 				goto unknown;
74*11038SRao.Shoaib@Sun.COM 			}
75*11038SRao.Shoaib@Sun.COM 			once = 1;
76*11038SRao.Shoaib@Sun.COM 		}
77*11038SRao.Shoaib@Sun.COM                 if (pthread_mutex_unlock(&lock) != 0)
78*11038SRao.Shoaib@Sun.COM 			goto unknown;
79*11038SRao.Shoaib@Sun.COM         }
80*11038SRao.Shoaib@Sun.COM 
810Sstevel@tonic-gate 	buf = pthread_getspecific(key);
82*11038SRao.Shoaib@Sun.COM         if (buf == NULL) {
830Sstevel@tonic-gate 		buf = malloc(EAI_BUFSIZE);
84*11038SRao.Shoaib@Sun.COM                 if (buf == NULL)
85*11038SRao.Shoaib@Sun.COM                         goto unknown;
86*11038SRao.Shoaib@Sun.COM                 if (pthread_setspecific(key, buf) != 0) {
873822Sjs198686 			free(buf);
883822Sjs198686 			goto unknown;
893822Sjs198686 		}
90*11038SRao.Shoaib@Sun.COM         }
910Sstevel@tonic-gate #endif
920Sstevel@tonic-gate 	/*
930Sstevel@tonic-gate 	 * XXX This really should be snprintf(buf, EAI_BUFSIZE, ...).
940Sstevel@tonic-gate 	 * It is safe until message catalogs are used.
950Sstevel@tonic-gate 	 */
960Sstevel@tonic-gate 	sprintf(buf, "%s: %d", gai_errlist[gai_nerr - 1], ecode);
970Sstevel@tonic-gate 	return (buf);
983822Sjs198686 
99*11038SRao.Shoaib@Sun.COM #ifdef DO_PTHREADS
100*11038SRao.Shoaib@Sun.COM  unknown:
1013822Sjs198686 	return ("unknown error");
1023822Sjs198686 #endif
1030Sstevel@tonic-gate }
104*11038SRao.Shoaib@Sun.COM 
105*11038SRao.Shoaib@Sun.COM /*! \file */
106