xref: /haiku/src/system/libnetwork/netresolv/resolv/herror.c (revision aa4f1de9ad92a025ba6bdc36703a6b97f558f7a0)
1aa6411e2SAugustin Cavalier /*	$NetBSD: herror.c,v 1.10 2015/02/24 17:56:20 christos Exp $	*/
2aa6411e2SAugustin Cavalier 
3aa6411e2SAugustin Cavalier /*
4aa6411e2SAugustin Cavalier  * Copyright (c) 1987, 1993
5aa6411e2SAugustin Cavalier  *    The Regents of the University of California.  All rights reserved.
6aa6411e2SAugustin Cavalier  *
7aa6411e2SAugustin Cavalier  * Redistribution and use in source and binary forms, with or without
8aa6411e2SAugustin Cavalier  * modification, are permitted provided that the following conditions
9aa6411e2SAugustin Cavalier  * are met:
10aa6411e2SAugustin Cavalier  * 1. Redistributions of source code must retain the above copyright
11aa6411e2SAugustin Cavalier  *    notice, this list of conditions and the following disclaimer.
12aa6411e2SAugustin Cavalier  * 2. Redistributions in binary form must reproduce the above copyright
13aa6411e2SAugustin Cavalier  *    notice, this list of conditions and the following disclaimer in the
14aa6411e2SAugustin Cavalier  *    documentation and/or other materials provided with the distribution.
15aa6411e2SAugustin Cavalier  * 3. Neither the name of the University nor the names of its contributors
16aa6411e2SAugustin Cavalier  *    may be used to endorse or promote products derived from this software
17aa6411e2SAugustin Cavalier  *    without specific prior written permission.
18aa6411e2SAugustin Cavalier  *
19aa6411e2SAugustin Cavalier  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20aa6411e2SAugustin Cavalier  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21aa6411e2SAugustin Cavalier  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22aa6411e2SAugustin Cavalier  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23aa6411e2SAugustin Cavalier  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24aa6411e2SAugustin Cavalier  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25aa6411e2SAugustin Cavalier  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26aa6411e2SAugustin Cavalier  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27aa6411e2SAugustin Cavalier  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28aa6411e2SAugustin Cavalier  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29aa6411e2SAugustin Cavalier  * SUCH DAMAGE.
30aa6411e2SAugustin Cavalier  */
31aa6411e2SAugustin Cavalier 
32aa6411e2SAugustin Cavalier /*
33aa6411e2SAugustin Cavalier  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
34aa6411e2SAugustin Cavalier  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
35aa6411e2SAugustin Cavalier  *
36aa6411e2SAugustin Cavalier  * Permission to use, copy, modify, and distribute this software for any
37aa6411e2SAugustin Cavalier  * purpose with or without fee is hereby granted, provided that the above
38aa6411e2SAugustin Cavalier  * copyright notice and this permission notice appear in all copies.
39aa6411e2SAugustin Cavalier  *
40aa6411e2SAugustin Cavalier  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
41aa6411e2SAugustin Cavalier  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
42aa6411e2SAugustin Cavalier  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
43aa6411e2SAugustin Cavalier  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
44aa6411e2SAugustin Cavalier  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
45aa6411e2SAugustin Cavalier  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
46aa6411e2SAugustin Cavalier  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
47aa6411e2SAugustin Cavalier  */
48aa6411e2SAugustin Cavalier 
49aa6411e2SAugustin Cavalier #if defined(LIBC_SCCS) && !defined(lint)
50aa6411e2SAugustin Cavalier static const char sccsid[] = "@(#)herror.c	8.1 (Berkeley) 6/4/93";
51aa6411e2SAugustin Cavalier static const char rcsid[] = "$Id: herror.c,v 1.4 2005/04/27 04:56:41 sra Exp $";
52aa6411e2SAugustin Cavalier #endif /* LIBC_SCCS and not lint */
53aa6411e2SAugustin Cavalier 
54aa6411e2SAugustin Cavalier #include "port_before.h"
55aa6411e2SAugustin Cavalier 
56aa6411e2SAugustin Cavalier #include <sys/types.h>
57aa6411e2SAugustin Cavalier #include <sys/param.h>
58aa6411e2SAugustin Cavalier #include <sys/uio.h>
59aa6411e2SAugustin Cavalier 
60aa6411e2SAugustin Cavalier #include <netinet/in.h>
61aa6411e2SAugustin Cavalier #include <arpa/nameser.h>
62aa6411e2SAugustin Cavalier 
63aa6411e2SAugustin Cavalier #include <netdb.h>
64aa6411e2SAugustin Cavalier #include <resolv.h>
65aa6411e2SAugustin Cavalier #include <string.h>
66aa6411e2SAugustin Cavalier #include <unistd.h>
67aa6411e2SAugustin Cavalier 
68aa6411e2SAugustin Cavalier #include "port_after.h"
69aa6411e2SAugustin Cavalier 
70aa6411e2SAugustin Cavalier const char *h_errlist[] = {
71aa6411e2SAugustin Cavalier 	"Resolver Error 0 (no error)",
72aa6411e2SAugustin Cavalier 	"Unknown host",				/*%< 1 HOST_NOT_FOUND */
73aa6411e2SAugustin Cavalier 	"Host name lookup failure",		/*%< 2 TRY_AGAIN */
74aa6411e2SAugustin Cavalier 	"Unknown server error",			/*%< 3 NO_RECOVERY */
75aa6411e2SAugustin Cavalier 	"No address associated with name",	/*%< 4 NO_ADDRESS */
76aa6411e2SAugustin Cavalier };
77aa6411e2SAugustin Cavalier int	h_nerr = { sizeof h_errlist / sizeof h_errlist[0] };
78aa6411e2SAugustin Cavalier 
79aa6411e2SAugustin Cavalier #if !(__GLIBC__ > 2 || __GLIBC__ == 2 &&  __GLIBC_MINOR__ >= 3)
80aa6411e2SAugustin Cavalier #undef	h_errno
81aa6411e2SAugustin Cavalier int	h_errno;
82aa6411e2SAugustin Cavalier #endif
83aa6411e2SAugustin Cavalier 
84aa6411e2SAugustin Cavalier #ifdef __weak_alias
__weak_alias(herror,_herror)85aa6411e2SAugustin Cavalier __weak_alias(herror,_herror)
86aa6411e2SAugustin Cavalier #endif
87aa6411e2SAugustin Cavalier 
88aa6411e2SAugustin Cavalier /*%
89aa6411e2SAugustin Cavalier  * herror --
90aa6411e2SAugustin Cavalier  *	print the error indicated by the h_errno value.
91aa6411e2SAugustin Cavalier  */
92*aa4f1de9SOwen Anderson __attribute__((weak))
93aa6411e2SAugustin Cavalier void
94aa6411e2SAugustin Cavalier herror(const char *s) {
95aa6411e2SAugustin Cavalier 	struct iovec iov[4], *v = iov;
96aa6411e2SAugustin Cavalier 	char *t;
97aa6411e2SAugustin Cavalier 
98aa6411e2SAugustin Cavalier 	if (s != NULL && *s != '\0') {
99aa6411e2SAugustin Cavalier 		DE_CONST(s, t);
100aa6411e2SAugustin Cavalier 		v->iov_base = t;
101aa6411e2SAugustin Cavalier 		v->iov_len = strlen(t);
102aa6411e2SAugustin Cavalier 		v++;
103aa6411e2SAugustin Cavalier 		DE_CONST(": ", t);
104aa6411e2SAugustin Cavalier 		v->iov_base = t;
105aa6411e2SAugustin Cavalier 		v->iov_len = 2;
106aa6411e2SAugustin Cavalier 		v++;
107aa6411e2SAugustin Cavalier 	}
108aa6411e2SAugustin Cavalier 	DE_CONST(hstrerror(*__h_errno()), t);
109aa6411e2SAugustin Cavalier 	v->iov_base = t;
110aa6411e2SAugustin Cavalier 	v->iov_len = strlen(v->iov_base);
111aa6411e2SAugustin Cavalier 	v++;
112aa6411e2SAugustin Cavalier 	DE_CONST("\n", t);
113aa6411e2SAugustin Cavalier 	v->iov_base = t;
114aa6411e2SAugustin Cavalier 	v->iov_len = 1;
115aa6411e2SAugustin Cavalier 	(void)writev(STDERR_FILENO, iov, (int)((v - iov) + 1));
116aa6411e2SAugustin Cavalier }
117aa6411e2SAugustin Cavalier 
118aa6411e2SAugustin Cavalier /*%
119aa6411e2SAugustin Cavalier  * hstrerror --
120aa6411e2SAugustin Cavalier  *	return the string associated with a given "host" errno value.
121aa6411e2SAugustin Cavalier  */
122aa6411e2SAugustin Cavalier const char *
hstrerror(int err)123aa6411e2SAugustin Cavalier hstrerror(int err) {
124aa6411e2SAugustin Cavalier 	if (err < 0)
125aa6411e2SAugustin Cavalier 		return ("Resolver internal error");
126aa6411e2SAugustin Cavalier 	else if (err < h_nerr)
127aa6411e2SAugustin Cavalier 		return (h_errlist[err]);
128aa6411e2SAugustin Cavalier 	return ("Unknown resolver error");
129aa6411e2SAugustin Cavalier }
130aa6411e2SAugustin Cavalier 
131aa6411e2SAugustin Cavalier /*! \file */
132