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