1/*	$NetBSD: gai_strerror.c,v 1.1 2024/02/18 20:57:47 christos Exp $	*/
2
3/*
4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 *
6 * SPDX-License-Identifier: MPL-2.0
7 *
8 * This Source Code Form is subject to the terms of the Mozilla Public
9 * License, v. 2.0.  If a copy of the MPL was not distributed with this
10 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11 *
12 * See the COPYRIGHT file distributed with this work for additional
13 * information regarding copyright ownership.
14 */
15
16/*! \file gai_strerror.c
17 * gai_strerror() returns an error message corresponding to an
18 * error code returned by getaddrinfo() and getnameinfo(). The following error
19 * codes and their meaning are defined in
20 * \link netdb.h include/irs/netdb.h.\endlink
21 * This implementation is almost an exact copy of lwres/gai_sterror.c except
22 * that it catches up the latest API standard, RFC3493.
23 *
24 * \li #EAI_ADDRFAMILY address family for hostname not supported
25 * \li #EAI_AGAIN temporary failure in name resolution
26 * \li #EAI_BADFLAGS invalid value for ai_flags
27 * \li #EAI_FAIL non-recoverable failure in name resolution
28 * \li #EAI_FAMILY ai_family not supported
29 * \li #EAI_MEMORY memory allocation failure
30 * \li #EAI_NODATA no address associated with hostname (obsoleted in RFC3493)
31 * \li #EAI_NONAME hostname nor servname provided, or not known
32 * \li #EAI_SERVICE servname not supported for ai_socktype
33 * \li #EAI_SOCKTYPE ai_socktype not supported
34 * \li #EAI_SYSTEM system error returned in errno
35 * \li #EAI_BADHINTS Invalid value for hints (non-standard)
36 * \li #EAI_PROTOCOL Resolved protocol is unknown (non-standard)
37 * \li #EAI_OVERFLOW Argument buffer overflow
38 * \li #EAI_INSECUREDATA Insecure Data (experimental)
39 *
40 * The message invalid error code is returned if ecode is out of range.
41 *
42 * ai_flags, ai_family and ai_socktype are elements of the struct
43 * addrinfo used by lwres_getaddrinfo().
44 *
45 * \section gai_strerror_see See Also
46 *
47 * strerror(), getaddrinfo(), getnameinfo(), RFC3493.
48 */
49
50#include <isc/net.h>
51
52#include <irs/netdb.h>
53
54/*% Text of error messages. */
55static const char *gai_messages[] = { "no error",
56				      "address family for hostname not "
57				      "supported",
58				      "temporary failure in name resolution",
59				      "invalid value for ai_flags",
60				      "non-recoverable failure in name "
61				      "resolution",
62				      "ai_family not supported",
63				      "memory allocation failure",
64				      "no address associated with hostname",
65				      "hostname nor servname provided, or not "
66				      "known",
67				      "servname not supported for ai_socktype",
68				      "ai_socktype not supported",
69				      "system error returned in errno",
70				      "bad hints",
71				      "bad protocol",
72				      "argument buffer overflow",
73				      "insecure data provided" };
74
75/*%
76 * Returns an error message corresponding to an error code returned by
77 * getaddrinfo() and getnameinfo()
78 */
79#if defined _WIN32
80char *
81#else  /* if defined _WIN32 */
82const char *
83#endif /* if defined _WIN32 */
84gai_strerror(int ecode) {
85	union {
86		const char *const_ptr;
87		char *deconst_ptr;
88	} ptr;
89
90	if ((ecode < 0) ||
91	    (ecode >= (int)(sizeof(gai_messages) / sizeof(*gai_messages))))
92	{
93		ptr.const_ptr = "invalid error code";
94	} else {
95		ptr.const_ptr = gai_messages[ecode];
96	}
97	return (ptr.deconst_ptr);
98}
99