1 /* 2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the project nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include "namespace.h" 34 #include <netdb.h> 35 #if defined(NLS) 36 #include <nl_types.h> 37 #include <errno.h> 38 #include <limits.h> 39 #include <stdlib.h> 40 #include <string.h> 41 #include "reentrant.h" 42 #endif 43 #include "un-namespace.h" 44 45 /* Entries EAI_ADDRFAMILY (1) and EAI_NODATA (7) are obsoleted, but left */ 46 /* for backward compatibility with userland code prior to 2553bis-02 */ 47 static const char *ai_errlist[] = { 48 "Success", /* 0 */ 49 "Address family for hostname not supported", /* 1 */ 50 "Temporary failure in name resolution", /* EAI_AGAIN */ 51 "Invalid value for ai_flags", /* EAI_BADFLAGS */ 52 "Non-recoverable failure in name resolution", /* EAI_FAIL */ 53 "ai_family not supported", /* EAI_FAMILY */ 54 "Memory allocation failure", /* EAI_MEMORY */ 55 "No address associated with hostname", /* 7 */ 56 "hostname nor servname provided, or not known", /* EAI_NONAME */ 57 "servname not supported for ai_socktype", /* EAI_SERVICE */ 58 "ai_socktype not supported", /* EAI_SOCKTYPE */ 59 "System error returned in errno", /* EAI_SYSTEM */ 60 "Invalid value for hints", /* EAI_BADHINTS */ 61 "Resolved protocol is unknown", /* EAI_PROTOCOL */ 62 "Argument buffer overflow" /* EAI_OVERFLOW */ 63 }; 64 65 #if defined(NLS) 66 static char gai_buf[NL_TEXTMAX]; 67 static once_t gai_init_once = ONCE_INITIALIZER; 68 static thread_key_t gai_key; 69 static int gai_keycreated = 0; 70 71 static void 72 gai_keycreate(void) 73 { 74 gai_keycreated = (thr_keycreate(&gai_key, free) == 0); 75 } 76 #endif 77 78 const char * 79 gai_strerror(int ecode) 80 { 81 #if defined(NLS) 82 nl_catd catd; 83 char *buf; 84 85 if (thr_main() != 0) 86 buf = gai_buf; 87 else { 88 if (thr_once(&gai_init_once, gai_keycreate) != 0 || 89 !gai_keycreated) 90 goto thr_err; 91 if ((buf = thr_getspecific(gai_key)) == NULL) { 92 if ((buf = malloc(sizeof(gai_buf))) == NULL) 93 goto thr_err; 94 if (thr_setspecific(gai_key, buf) != 0) { 95 free(buf); 96 goto thr_err; 97 } 98 } 99 } 100 101 catd = catopen("libc", NL_CAT_LOCALE); 102 if (ecode > 0 && ecode < EAI_MAX) 103 strlcpy(buf, catgets(catd, 3, ecode, ai_errlist[ecode]), 104 sizeof(gai_buf)); 105 else if (ecode == 0) 106 strlcpy(buf, catgets(catd, 3, NL_MSGMAX - 1, "Success"), 107 sizeof(gai_buf)); 108 else 109 strlcpy(buf, catgets(catd, 3, NL_MSGMAX, "Unknown error"), 110 sizeof(gai_buf)); 111 catclose(catd); 112 return buf; 113 114 thr_err: 115 #endif 116 if (ecode >= 0 && ecode < EAI_MAX) 117 return ai_errlist[ecode]; 118 return "Unknown error"; 119 } 120