1331722Seadler/*
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 * 4. Neither the name of the University nor the names of its contributors
14156952Sume *    may be used to endorse or promote products derived from this software
15156952Sume *    without specific prior written permission.
16156952Sume *
17156952Sume * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18156952Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19156952Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20156952Sume * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21156952Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22156952Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23156952Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24156952Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25156952Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26156952Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27156952Sume * SUCH DAMAGE.
28156952Sume */
29156952Sume
30156952Sume/*
31156952Sume * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
32156952Sume * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
33156952Sume *
34156952Sume * Permission to use, copy, modify, and distribute this software for any
35156952Sume * purpose with or without fee is hereby granted, provided that the above
36156952Sume * copyright notice and this permission notice appear in all copies.
37156952Sume *
38156952Sume * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
39156952Sume * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
40156952Sume * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
41156952Sume * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
42156952Sume * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
43156952Sume * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
44156952Sume * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
45156952Sume */
46156952Sume
47156952Sume#if defined(LIBC_SCCS) && !defined(lint)
48156952Sumestatic const char sccsid[] = "@(#)herror.c	8.1 (Berkeley) 6/4/93";
49269867Sumestatic const char rcsid[] = "$Id: herror.c,v 1.4 2005/04/27 04:56:41 sra Exp $";
50156952Sume#endif /* LIBC_SCCS and not lint */
51156956Sume#include <sys/cdefs.h>
52156956Sume__FBSDID("$FreeBSD$");
53156952Sume
54156952Sume#include "port_before.h"
55156952Sume
56156956Sume#include "namespace.h"
57156952Sume#include <sys/param.h>
58156952Sume#include <sys/uio.h>
59156952Sume
60156952Sume#include <netinet/in.h>
61156952Sume#include <arpa/nameser.h>
62156952Sume
63156952Sume#include <netdb.h>
64156952Sume#include <resolv.h>
65156952Sume#include <string.h>
66156952Sume#include <unistd.h>
67156956Sume#include "un-namespace.h"
68156952Sume
69156952Sume#include "port_after.h"
70156952Sume
71156952Sumeconst char *h_errlist[] = {
72156952Sume	"Resolver Error 0 (no error)",
73170244Sume	"Unknown host",				/*%< 1 HOST_NOT_FOUND */
74170244Sume	"Host name lookup failure",		/*%< 2 TRY_AGAIN */
75170244Sume	"Unknown server error",			/*%< 3 NO_RECOVERY */
76170244Sume	"No address associated with name",	/*%< 4 NO_ADDRESS */
77156952Sume};
78298120Spfgconst int h_nerr = { nitems(h_errlist) };
79156952Sume
80156952Sume#undef	h_errno
81156952Sumeint	h_errno;
82156952Sume
83170244Sume/*%
84156952Sume * herror --
85156952Sume *	print the error indicated by the h_errno value.
86156952Sume */
87156952Sumevoid
88156952Sumeherror(const char *s) {
89156952Sume	struct iovec iov[4], *v = iov;
90156952Sume	char *t;
91156952Sume
92156952Sume	if (s != NULL && *s != '\0') {
93156952Sume		DE_CONST(s, t);
94156952Sume		v->iov_base = t;
95156952Sume		v->iov_len = strlen(t);
96156952Sume		v++;
97156952Sume		DE_CONST(": ", t);
98156952Sume		v->iov_base = t;
99156952Sume		v->iov_len = 2;
100156952Sume		v++;
101156952Sume	}
102156952Sume	DE_CONST(hstrerror(*__h_errno()), t);
103156952Sume	v->iov_base = t;
104156952Sume	v->iov_len = strlen(v->iov_base);
105156952Sume	v++;
106156952Sume	DE_CONST("\n", t);
107156952Sume	v->iov_base = t;
108156952Sume	v->iov_len = 1;
109156956Sume	_writev(STDERR_FILENO, iov, (v - iov) + 1);
110156952Sume}
111156952Sume
112170244Sume/*%
113156952Sume * hstrerror --
114156952Sume *	return the string associated with a given "host" errno value.
115156952Sume */
116156952Sumeconst char *
117156952Sumehstrerror(int err) {
118156952Sume	if (err < 0)
119156952Sume		return ("Resolver internal error");
120156952Sume	else if (err < h_nerr)
121156952Sume		return (h_errlist[err]);
122156952Sume	return ("Unknown resolver error");
123156952Sume}
124170244Sume
125170244Sume/*! \file */
126