xref: /freebsd/lib/libc/net/gai_strerror.c (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
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 <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "namespace.h"
36 #include <netdb.h>
37 #if defined(NLS)
38 #include <nl_types.h>
39 #include <errno.h>
40 #include <limits.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include "reentrant.h"
44 #endif
45 #include "un-namespace.h"
46 
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[] = {
50 	"Success",					/* 0 */
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 */
65 };
66 
67 #if defined(NLS)
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;
72 
73 static void
74 gai_keycreate(void)
75 {
76 	gai_keycreated = (thr_keycreate(&gai_key, free) == 0);
77 }
78 #endif
79 
80 const char *
81 gai_strerror(int ecode)
82 {
83 #if defined(NLS)
84 	nl_catd catd;
85 	char *buf;
86 
87 	if (thr_main() != 0)
88 		buf = gai_buf;
89 	else {
90 		if (thr_once(&gai_init_once, gai_keycreate) != 0 ||
91 		    !gai_keycreated)
92 			goto thr_err;
93 		if ((buf = thr_getspecific(gai_key)) == NULL) {
94 			if ((buf = malloc(sizeof(gai_buf))) == NULL)
95 				goto thr_err;
96 			if (thr_setspecific(gai_key, buf) != 0) {
97 				free(buf);
98 				goto thr_err;
99 			}
100 		}
101 	}
102 
103 	catd = catopen("libc", NL_CAT_LOCALE);
104 	if (ecode > 0 && ecode < EAI_MAX)
105 		strlcpy(buf, catgets(catd, 3, ecode, ai_errlist[ecode]),
106 		    sizeof(gai_buf));
107 	else if (ecode == 0)
108 		strlcpy(buf, catgets(catd, 3, NL_MSGMAX - 1, "Success"),
109 		    sizeof(gai_buf));
110 	else
111 		strlcpy(buf, catgets(catd, 3, NL_MSGMAX, "Unknown error"),
112 		    sizeof(gai_buf));
113 	catclose(catd);
114 	return buf;
115 
116 thr_err:
117 #endif
118 	if (ecode >= 0 && ecode < EAI_MAX)
119 		return ai_errlist[ecode];
120 	return "Unknown error";
121 }
122