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