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