2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "namespace.h"
43 #include "reentrant.h"
45 #include "un-namespace.h"
47 /* Entries EAI_ADDRFAMILY (1) and EAI_NODATA (7) are obsoleted, but left */
48 /* for backwards compatibility with userland code prior to RFC2553bis-02 */
49 static const char *ai_errlist[] = {
51 "Address family for hostname not supported", /* 1: Obsolete */
52 "Name could not be resolved at this time", /* EAI_AGAIN */
53 "Flags parameter had an invalid value", /* EAI_BADFLAGS */
54 "Non-recoverable failure in name resolution", /* EAI_FAIL */
55 "Address family not recognized", /* EAI_FAMILY */
56 "Memory allocation failure", /* EAI_MEMORY */
57 "No address associated with hostname", /* 7: Obsolete*/
58 "Name does not resolve", /* EAI_NONAME */
59 "Service was not recognized for socket type", /* EAI_SERVICE */
60 "Intended socket type was not recognized", /* EAI_SOCKTYPE */
61 "System error returned in errno", /* EAI_SYSTEM */
62 "Invalid value for hints", /* EAI_BADHINTS */
63 "Resolved protocol is unknown", /* EAI_PROTOCOL */
64 "Argument buffer overflow" /* EAI_OVERFLOW */
68 static char gai_buf[NL_TEXTMAX];
69 static once_t gai_init_once = ONCE_INITIALIZER;
70 static thread_key_t gai_key;
71 static int gai_keycreated = 0;
76 gai_keycreated = (thr_keycreate(&gai_key, free) == 0);
81 gai_strerror(int ecode)
90 if (thr_once(&gai_init_once, gai_keycreate) != 0 ||
93 if ((buf = thr_getspecific(gai_key)) == NULL) {
94 if ((buf = malloc(sizeof(gai_buf))) == NULL)
96 if (thr_setspecific(gai_key, buf) != 0) {
103 catd = catopen("libc", NL_CAT_LOCALE);
104 if (ecode > 0 && ecode < EAI_MAX)
105 strlcpy(buf, catgets(catd, 3, ecode, ai_errlist[ecode]),
108 strlcpy(buf, catgets(catd, 3, NL_MSGMAX - 1, "Success"),
111 strlcpy(buf, catgets(catd, 3, NL_MSGMAX, "Unknown error"),
118 if (ecode >= 0 && ecode < EAI_MAX)
119 return ai_errlist[ecode];
120 return "Unknown error";