herror.c revision 156956
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 */
55156956Sume#include <sys/cdefs.h>
56156956Sume__FBSDID("$FreeBSD: head/lib/libc/resolv/herror.c 156956 2006-03-21 15:37:16Z ume $");
57156952Sume
58156952Sume#include "port_before.h"
59156952Sume
60156956Sume#include "namespace.h"
61156952Sume#include <sys/types.h>
62156952Sume#include <sys/param.h>
63156952Sume#include <sys/uio.h>
64156952Sume
65156952Sume#include <netinet/in.h>
66156952Sume#include <arpa/nameser.h>
67156952Sume
68156952Sume#include <netdb.h>
69156952Sume#include <resolv.h>
70156952Sume#include <string.h>
71156952Sume#include <unistd.h>
72156956Sume#include "un-namespace.h"
73156952Sume
74156952Sume#include "port_after.h"
75156952Sume
76156952Sumeconst char *h_errlist[] = {
77156952Sume	"Resolver Error 0 (no error)",
78156952Sume	"Unknown host",				/* 1 HOST_NOT_FOUND */
79156952Sume	"Host name lookup failure",		/* 2 TRY_AGAIN */
80156952Sume	"Unknown server error",			/* 3 NO_RECOVERY */
81156952Sume	"No address associated with name",	/* 4 NO_ADDRESS */
82156952Sume};
83156956Sumeconst int h_nerr = { sizeof h_errlist / sizeof h_errlist[0] };
84156952Sume
85156952Sume#undef	h_errno
86156952Sumeint	h_errno;
87156952Sume
88156952Sume/*
89156952Sume * herror --
90156952Sume *	print the error indicated by the h_errno value.
91156952Sume */
92156952Sumevoid
93156952Sumeherror(const char *s) {
94156952Sume	struct iovec iov[4], *v = iov;
95156952Sume	char *t;
96156952Sume
97156952Sume	if (s != NULL && *s != '\0') {
98156952Sume		DE_CONST(s, t);
99156952Sume		v->iov_base = t;
100156952Sume		v->iov_len = strlen(t);
101156952Sume		v++;
102156952Sume		DE_CONST(": ", t);
103156952Sume		v->iov_base = t;
104156952Sume		v->iov_len = 2;
105156952Sume		v++;
106156952Sume	}
107156952Sume	DE_CONST(hstrerror(*__h_errno()), t);
108156952Sume	v->iov_base = t;
109156952Sume	v->iov_len = strlen(v->iov_base);
110156952Sume	v++;
111156952Sume	DE_CONST("\n", t);
112156952Sume	v->iov_base = t;
113156952Sume	v->iov_len = 1;
114156956Sume	_writev(STDERR_FILENO, iov, (v - iov) + 1);
115156952Sume}
116156952Sume
117156952Sume/*
118156952Sume * hstrerror --
119156952Sume *	return the string associated with a given "host" errno value.
120156952Sume */
121156952Sumeconst char *
122156952Sumehstrerror(int err) {
123156952Sume	if (err < 0)
124156952Sume		return ("Resolver internal error");
125156952Sume	else if (err < h_nerr)
126156952Sume		return (h_errlist[err]);
127156952Sume	return ("Unknown resolver error");
128156952Sume}
129