herror.c revision 156952
1156952Sume/*
2156952Sume * Copyright (c) 1987, 1993
3156952Sume *    The Regents of the University of California.  All rights reserved.
4156952Sume *
5156952Sume * Redistribution and use in source and binary forms, with or without
6156952Sume * modification, are permitted provided that the following conditions
7156952Sume * are met:
8156952Sume * 1. Redistributions of source code must retain the above copyright
9156952Sume *    notice, this list of conditions and the following disclaimer.
10156952Sume * 2. Redistributions in binary form must reproduce the above copyright
11156952Sume *    notice, this list of conditions and the following disclaimer in the
12156952Sume *    documentation and/or other materials provided with the distribution.
13156952Sume * 3. All advertising materials mentioning features or use of this software
14156952Sume *    must display the following acknowledgement:
15156952Sume * 	This product includes software developed by the University of
16156952Sume * 	California, Berkeley and its contributors.
17156952Sume * 4. Neither the name of the University nor the names of its contributors
18156952Sume *    may be used to endorse or promote products derived from this software
19156952Sume *    without specific prior written permission.
20156952Sume *
21156952Sume * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22156952Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23156952Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24156952Sume * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25156952Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26156952Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27156952Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28156952Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29156952Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30156952Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31156952Sume * SUCH DAMAGE.
32156952Sume */
33156952Sume
34156952Sume/*
35156952Sume * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
36156952Sume * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
37156952Sume *
38156952Sume * Permission to use, copy, modify, and distribute this software for any
39156952Sume * purpose with or without fee is hereby granted, provided that the above
40156952Sume * copyright notice and this permission notice appear in all copies.
41156952Sume *
42156952Sume * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
43156952Sume * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
44156952Sume * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
45156952Sume * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
46156952Sume * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
47156952Sume * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
48156952Sume * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
49156952Sume */
50156952Sume
51156952Sume#if defined(LIBC_SCCS) && !defined(lint)
52156952Sumestatic const char sccsid[] = "@(#)herror.c	8.1 (Berkeley) 6/4/93";
53156952Sumestatic const char rcsid[] = "$Id: herror.c,v 1.2.206.1 2004/03/09 08:33:54 marka Exp $";
54156952Sume#endif /* LIBC_SCCS and not lint */
55156952Sume
56156952Sume#include "port_before.h"
57156952Sume
58156952Sume#include <sys/types.h>
59156952Sume#include <sys/param.h>
60156952Sume#include <sys/uio.h>
61156952Sume
62156952Sume#include <netinet/in.h>
63156952Sume#include <arpa/nameser.h>
64156952Sume
65156952Sume#include <netdb.h>
66156952Sume#include <resolv.h>
67156952Sume#include <string.h>
68156952Sume#include <unistd.h>
69156952Sume#include <irs.h>
70156952Sume
71156952Sume#include "port_after.h"
72156952Sume
73156952Sumeconst char *h_errlist[] = {
74156952Sume	"Resolver Error 0 (no error)",
75156952Sume	"Unknown host",				/* 1 HOST_NOT_FOUND */
76156952Sume	"Host name lookup failure",		/* 2 TRY_AGAIN */
77156952Sume	"Unknown server error",			/* 3 NO_RECOVERY */
78156952Sume	"No address associated with name",	/* 4 NO_ADDRESS */
79156952Sume};
80156952Sumeint	h_nerr = { sizeof h_errlist / sizeof h_errlist[0] };
81156952Sume
82156952Sume#if !(__GLIBC__ > 2 || __GLIBC__ == 2 &&  __GLIBC_MINOR__ >= 3)
83156952Sume#undef	h_errno
84156952Sumeint	h_errno;
85156952Sume#endif
86156952Sume
87156952Sume/*
88156952Sume * herror --
89156952Sume *	print the error indicated by the h_errno value.
90156952Sume */
91156952Sumevoid
92156952Sumeherror(const char *s) {
93156952Sume	struct iovec iov[4], *v = iov;
94156952Sume	char *t;
95156952Sume
96156952Sume	if (s != NULL && *s != '\0') {
97156952Sume		DE_CONST(s, t);
98156952Sume		v->iov_base = t;
99156952Sume		v->iov_len = strlen(t);
100156952Sume		v++;
101156952Sume		DE_CONST(": ", t);
102156952Sume		v->iov_base = t;
103156952Sume		v->iov_len = 2;
104156952Sume		v++;
105156952Sume	}
106156952Sume	DE_CONST(hstrerror(*__h_errno()), t);
107156952Sume	v->iov_base = t;
108156952Sume	v->iov_len = strlen(v->iov_base);
109156952Sume	v++;
110156952Sume	DE_CONST("\n", t);
111156952Sume	v->iov_base = t;
112156952Sume	v->iov_len = 1;
113156952Sume	writev(STDERR_FILENO, iov, (v - iov) + 1);
114156952Sume}
115156952Sume
116156952Sume/*
117156952Sume * hstrerror --
118156952Sume *	return the string associated with a given "host" errno value.
119156952Sume */
120156952Sumeconst char *
121156952Sumehstrerror(int err) {
122156952Sume	if (err < 0)
123156952Sume		return ("Resolver internal error");
124156952Sume	else if (err < h_nerr)
125156952Sume		return (h_errlist[err]);
126156952Sume	return ("Unknown resolver error");
127156952Sume}
128