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