xref: /illumos-gate/usr/src/lib/libresolv2/common/irs/gai_strerror.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 1999-2002 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate 
6*7c478bd9Sstevel@tonic-gate /*
7*7c478bd9Sstevel@tonic-gate  * Copyright (c) 2001 by Internet Software Consortium.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
10*7c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
11*7c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
12*7c478bd9Sstevel@tonic-gate  *
13*7c478bd9Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
14*7c478bd9Sstevel@tonic-gate  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
15*7c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
16*7c478bd9Sstevel@tonic-gate  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
17*7c478bd9Sstevel@tonic-gate  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
18*7c478bd9Sstevel@tonic-gate  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
19*7c478bd9Sstevel@tonic-gate  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
20*7c478bd9Sstevel@tonic-gate  * SOFTWARE.
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate 
23*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate #include <port_before.h>
26*7c478bd9Sstevel@tonic-gate #include <netdb.h>
27*7c478bd9Sstevel@tonic-gate #include <port_after.h>
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #ifdef DO_PTHREADS
30*7c478bd9Sstevel@tonic-gate #include <pthread.h>
31*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
32*7c478bd9Sstevel@tonic-gate #endif
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate static const char *gai_errlist[] = {
35*7c478bd9Sstevel@tonic-gate 	"no error",
36*7c478bd9Sstevel@tonic-gate 	"address family not supported for name",/* EAI_ADDRFAMILY */
37*7c478bd9Sstevel@tonic-gate 	"temporary failure",			/* EAI_AGAIN */
38*7c478bd9Sstevel@tonic-gate 	"invalid flags",			/* EAI_BADFLAGS */
39*7c478bd9Sstevel@tonic-gate 	"permanent failure",			/* EAI_FAIL */
40*7c478bd9Sstevel@tonic-gate 	"address family not supported",		/* EAI_FAMILY */
41*7c478bd9Sstevel@tonic-gate 	"memory failure",			/* EAI_MEMORY */
42*7c478bd9Sstevel@tonic-gate 	"no address",				/* EAI_NODATA */
43*7c478bd9Sstevel@tonic-gate 	"unknown name or service",		/* EAI_NONAME */
44*7c478bd9Sstevel@tonic-gate 	"service not supported for socktype",	/* EAI_SERVICE */
45*7c478bd9Sstevel@tonic-gate 	"socktype not supported",		/* EAI_SOCKTYPE */
46*7c478bd9Sstevel@tonic-gate 	"system failure",			/* EAI_SYSTEM */
47*7c478bd9Sstevel@tonic-gate 	"bad hints",				/* EAI_BADHINTS */
48*7c478bd9Sstevel@tonic-gate 	"bad protocol",				/* EAI_PROTOCOL */
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate 	"unknown error"				/* Must be last. */
51*7c478bd9Sstevel@tonic-gate };
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate static const int gai_nerr = (sizeof(gai_errlist)/sizeof(*gai_errlist));
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate #define EAI_BUFSIZE 128
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate const char *
58*7c478bd9Sstevel@tonic-gate gai_strerror(int ecode) {
59*7c478bd9Sstevel@tonic-gate #ifndef DO_PTHREADS
60*7c478bd9Sstevel@tonic-gate 	static char buf[EAI_BUFSIZE];
61*7c478bd9Sstevel@tonic-gate #else	/* DO_PTHREADS */
62*7c478bd9Sstevel@tonic-gate 	static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
63*7c478bd9Sstevel@tonic-gate 	static pthread_key_t key;
64*7c478bd9Sstevel@tonic-gate 	static int once = 0;
65*7c478bd9Sstevel@tonic-gate 	char *buf;
66*7c478bd9Sstevel@tonic-gate #endif
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate 	if (ecode >= 0 && ecode < (gai_nerr - 1))
69*7c478bd9Sstevel@tonic-gate 		return (gai_errlist[ecode]);
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate #ifdef DO_PTHREADS
72*7c478bd9Sstevel@tonic-gate         if (!once) {
73*7c478bd9Sstevel@tonic-gate                 pthread_mutex_lock(&lock);
74*7c478bd9Sstevel@tonic-gate                 if (!once++)
75*7c478bd9Sstevel@tonic-gate                         pthread_key_create(&key, free);
76*7c478bd9Sstevel@tonic-gate                 pthread_mutex_unlock(&lock);
77*7c478bd9Sstevel@tonic-gate         }
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 	buf = pthread_getspecific(key);
80*7c478bd9Sstevel@tonic-gate         if (buf == NULL) {
81*7c478bd9Sstevel@tonic-gate 		buf = malloc(EAI_BUFSIZE);
82*7c478bd9Sstevel@tonic-gate                 if (buf == NULL)
83*7c478bd9Sstevel@tonic-gate                         return ("unknown error");
84*7c478bd9Sstevel@tonic-gate                 pthread_setspecific(key, buf);
85*7c478bd9Sstevel@tonic-gate         }
86*7c478bd9Sstevel@tonic-gate #endif
87*7c478bd9Sstevel@tonic-gate 	/*
88*7c478bd9Sstevel@tonic-gate 	 * XXX This really should be snprintf(buf, EAI_BUFSIZE, ...).
89*7c478bd9Sstevel@tonic-gate 	 * It is safe until message catalogs are used.
90*7c478bd9Sstevel@tonic-gate 	 */
91*7c478bd9Sstevel@tonic-gate 	sprintf(buf, "%s: %d", gai_errlist[gai_nerr - 1], ecode);
92*7c478bd9Sstevel@tonic-gate 	return (buf);
93*7c478bd9Sstevel@tonic-gate }
94