xref: /freebsd/lib/libc/net/gai_strerror.c (revision 4d846d26)
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 /*
48  * Entries EAI_ADDRFAMILY (1) and EAI_NODATA (7) were omitted from RFC 3493,
49  * but are or may be used as extensions or in old code.
50  */
51 static const char *ai_errlist[] = {
52 	"Success",					/* 0 */
53 	"Address family for hostname not supported",	/* EAI_ADDRFAMILY */
54 	"Name could not be resolved at this time",	/* EAI_AGAIN */
55 	"Flags parameter had an invalid value",		/* EAI_BADFLAGS */
56 	"Non-recoverable failure in name resolution",	/* EAI_FAIL */
57 	"Address family not recognized",		/* EAI_FAMILY */
58 	"Memory allocation failure", 			/* EAI_MEMORY */
59 	"No address associated with hostname",		/* EAI_NODATA*/
60 	"Name does not resolve",			/* EAI_NONAME */
61 	"Service was not recognized for socket type",	/* EAI_SERVICE */
62 	"Intended socket type was not recognized",	/* EAI_SOCKTYPE */
63 	"System error returned in errno", 		/* EAI_SYSTEM */
64 	"Invalid value for hints",			/* EAI_BADHINTS */
65 	"Resolved protocol is unknown",			/* EAI_PROTOCOL */
66 	"Argument buffer overflow"			/* EAI_OVERFLOW */
67 };
68 
69 #if defined(NLS)
70 static char		gai_buf[NL_TEXTMAX];
71 static once_t		gai_init_once = ONCE_INITIALIZER;
72 static thread_key_t	gai_key;
73 static int		gai_keycreated = 0;
74 
75 static void
76 gai_keycreate(void)
77 {
78 	gai_keycreated = (thr_keycreate(&gai_key, free) == 0);
79 }
80 #endif
81 
82 const char *
83 gai_strerror(int ecode)
84 {
85 #if defined(NLS)
86 	nl_catd catd;
87 	char *buf;
88 
89 	if (thr_main() != 0)
90 		buf = gai_buf;
91 	else {
92 		if (thr_once(&gai_init_once, gai_keycreate) != 0 ||
93 		    !gai_keycreated)
94 			goto thr_err;
95 		if ((buf = thr_getspecific(gai_key)) == NULL) {
96 			if ((buf = malloc(sizeof(gai_buf))) == NULL)
97 				goto thr_err;
98 			if (thr_setspecific(gai_key, buf) != 0) {
99 				free(buf);
100 				goto thr_err;
101 			}
102 		}
103 	}
104 
105 	catd = catopen("libc", NL_CAT_LOCALE);
106 	if (ecode > 0 && ecode < EAI_MAX)
107 		strlcpy(buf, catgets(catd, 3, ecode, ai_errlist[ecode]),
108 		    sizeof(gai_buf));
109 	else if (ecode == 0)
110 		strlcpy(buf, catgets(catd, 3, NL_MSGMAX - 1, "Success"),
111 		    sizeof(gai_buf));
112 	else
113 		strlcpy(buf, catgets(catd, 3, NL_MSGMAX, "Unknown error"),
114 		    sizeof(gai_buf));
115 	catclose(catd);
116 	return buf;
117 
118 thr_err:
119 #endif
120 	if (ecode >= 0 && ecode < EAI_MAX)
121 		return ai_errlist[ecode];
122 	return "Unknown error";
123 }
124