1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
18 *
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
29 * SUCH DAMAGE.
30 */
31
32 #include "namespace.h"
33 #include <netdb.h>
34 #if defined(NLS)
35 #include <nl_types.h>
36 #include <errno.h>
37 #include <limits.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include "reentrant.h"
41 #endif
42 #include "un-namespace.h"
43
44 /*
45 * Entries EAI_ADDRFAMILY (1) and EAI_NODATA (7) were omitted from RFC 3493,
46 * but are or may be used as extensions or in old code.
47 */
48 static const char *const ai_errlist[] = {
49 [0] = "Success",
50 [EAI_ADDRFAMILY] = "Address family for hostname not supported",
51 [EAI_AGAIN] = "Name could not be resolved at this time",
52 [EAI_BADFLAGS] = "Flags parameter had an invalid value",
53 [EAI_FAIL] = "Non-recoverable failure in name resolution",
54 [EAI_FAMILY] = "Address family not recognized",
55 [EAI_MEMORY] = "Memory allocation failure",
56 [EAI_NODATA] = "No address associated with hostname",
57 [EAI_NONAME] = "Name does not resolve",
58 [EAI_SERVICE] = "Service was not recognized for socket type",
59 [EAI_SOCKTYPE] = "Intended socket type was not recognized",
60 [EAI_SYSTEM] = "System error returned in errno",
61 [EAI_BADHINTS] = "Invalid value for hints",
62 [EAI_PROTOCOL] = "Resolved protocol is unknown",
63 [EAI_OVERFLOW] = "Argument buffer overflow",
64 };
65
66 #if defined(NLS)
67 static char gai_buf[NL_TEXTMAX];
68 static once_t gai_init_once = ONCE_INITIALIZER;
69 static thread_key_t gai_key;
70 static int gai_keycreated = 0;
71
72 static void
gai_keycreate(void)73 gai_keycreate(void)
74 {
75 gai_keycreated = thr_keycreate(&gai_key, free) == 0;
76 }
77 #endif
78
79 const char *
gai_strerror(int ecode)80 gai_strerror(int ecode)
81 {
82 #if defined(NLS)
83 nl_catd catd;
84 char *buf;
85 int saved_errno;
86
87 saved_errno = errno;
88 if (thr_main() != 0)
89 buf = gai_buf;
90 else {
91 if (thr_once(&gai_init_once, gai_keycreate) != 0 ||
92 !gai_keycreated)
93 goto thr_err;
94 if ((buf = thr_getspecific(gai_key)) == NULL) {
95 if ((buf = malloc(sizeof(gai_buf))) == NULL)
96 goto thr_err;
97 if (thr_setspecific(gai_key, buf) != 0) {
98 free(buf);
99 goto thr_err;
100 }
101 }
102 }
103
104 catd = catopen("libc", NL_CAT_LOCALE);
105 if (ecode > 0 && ecode < EAI_MAX)
106 strlcpy(buf, catgets(catd, 3, ecode, ai_errlist[ecode]),
107 sizeof(gai_buf));
108 else if (ecode == 0)
109 strlcpy(buf, catgets(catd, 3, NL_MSGMAX - 1, "Success"),
110 sizeof(gai_buf));
111 else
112 strlcpy(buf, catgets(catd, 3, NL_MSGMAX, "Unknown error"),
113 sizeof(gai_buf));
114 catclose(catd);
115 errno = saved_errno;
116 return (buf);
117
118 thr_err:
119 errno = saved_errno;
120 #endif
121 if (ecode >= 0 && ecode < EAI_MAX)
122 return (ai_errlist[ecode]);
123 return ("Unknown error");
124 }
125