herror.c revision 156952
1133359Sobrien/*
2133359Sobrien * Copyright (c) 1987, 1993
368349Sobrien *    The Regents of the University of California.  All rights reserved.
4133359Sobrien *
5133359Sobrien * Redistribution and use in source and binary forms, with or without
668349Sobrien * modification, are permitted provided that the following conditions
768349Sobrien * are met:
868349Sobrien * 1. Redistributions of source code must retain the above copyright
968349Sobrien *    notice, this list of conditions and the following disclaimer.
1068349Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1168349Sobrien *    notice, this list of conditions and the following disclaimer in the
1268349Sobrien *    documentation and/or other materials provided with the distribution.
1368349Sobrien * 3. All advertising materials mentioning features or use of this software
1468349Sobrien *    must display the following acknowledgement:
15133359Sobrien * 	This product includes software developed by the University of
1668349Sobrien * 	California, Berkeley and its contributors.
1768349Sobrien * 4. Neither the name of the University nor the names of its contributors
1868349Sobrien *    may be used to endorse or promote products derived from this software
1968349Sobrien *    without specific prior written permission.
2068349Sobrien *
2168349Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2268349Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23133359Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2468349Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25133359Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2668349Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27133359Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28133359Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29133359Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30133359Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31133359Sobrien * SUCH DAMAGE.
3268349Sobrien */
3368349Sobrien
3468349Sobrien/*
3568349Sobrien * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3668349Sobrien * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
3768349Sobrien *
38133359Sobrien * Permission to use, copy, modify, and distribute this software for any
39133359Sobrien * purpose with or without fee is hereby granted, provided that the above
40133359Sobrien * copyright notice and this permission notice appear in all copies.
41133359Sobrien *
42133359Sobrien * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
43133359Sobrien * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
44133359Sobrien * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
45133359Sobrien * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
46133359Sobrien * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
4768349Sobrien * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
48133359Sobrien * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
49133359Sobrien */
50133359Sobrien
51133359Sobrien#if defined(LIBC_SCCS) && !defined(lint)
52133359Sobrienstatic const char sccsid[] = "@(#)herror.c	8.1 (Berkeley) 6/4/93";
53133359Sobrienstatic const char rcsid[] = "$Id: herror.c,v 1.2.206.1 2004/03/09 08:33:54 marka Exp $";
54133359Sobrien#endif /* LIBC_SCCS and not lint */
55133359Sobrien
56133359Sobrien#include "port_before.h"
57133359Sobrien
58133359Sobrien#include <sys/types.h>
59133359Sobrien#include <sys/param.h>
60133359Sobrien#include <sys/uio.h>
61133359Sobrien
62133359Sobrien#include <netinet/in.h>
63133359Sobrien#include <arpa/nameser.h>
64133359Sobrien
65133359Sobrien#include <netdb.h>
66133359Sobrien#include <resolv.h>
67133359Sobrien#include <string.h>
68133359Sobrien#include <unistd.h>
69133359Sobrien#include <irs.h>
7068349Sobrien
71133359Sobrien#include "port_after.h"
72133359Sobrien
73133359Sobrienconst char *h_errlist[] = {
74133359Sobrien	"Resolver Error 0 (no error)",
7568349Sobrien	"Unknown host",				/* 1 HOST_NOT_FOUND */
76133359Sobrien	"Host name lookup failure",		/* 2 TRY_AGAIN */
7768349Sobrien	"Unknown server error",			/* 3 NO_RECOVERY */
78133359Sobrien	"No address associated with name",	/* 4 NO_ADDRESS */
79133359Sobrien};
80133359Sobrienint	h_nerr = { sizeof h_errlist / sizeof h_errlist[0] };
81133359Sobrien
82133359Sobrien#if !(__GLIBC__ > 2 || __GLIBC__ == 2 &&  __GLIBC_MINOR__ >= 3)
83133359Sobrien#undef	h_errno
84133359Sobrienint	h_errno;
85133359Sobrien#endif
86133359Sobrien
87133359Sobrien/*
8868349Sobrien * herror --
89133359Sobrien *	print the error indicated by the h_errno value.
90133359Sobrien */
91133359Sobrienvoid
92133359Sobrienherror(const char *s) {
93133359Sobrien	struct iovec iov[4], *v = iov;
94133359Sobrien	char *t;
95133359Sobrien
96133359Sobrien	if (s != NULL && *s != '\0') {
97133359Sobrien		DE_CONST(s, t);
98133359Sobrien		v->iov_base = t;
99133359Sobrien		v->iov_len = strlen(t);
100133359Sobrien		v++;
101133359Sobrien		DE_CONST(": ", t);
102133359Sobrien		v->iov_base = t;
103133359Sobrien		v->iov_len = 2;
104133359Sobrien		v++;
10580588Sobrien	}
106133359Sobrien	DE_CONST(hstrerror(*__h_errno()), t);
107133359Sobrien	v->iov_base = t;
108133359Sobrien	v->iov_len = strlen(v->iov_base);
109133359Sobrien	v++;
110133359Sobrien	DE_CONST("\n", t);
111133359Sobrien	v->iov_base = t;
112133359Sobrien	v->iov_len = 1;
113133359Sobrien	writev(STDERR_FILENO, iov, (v - iov) + 1);
114133359Sobrien}
115133359Sobrien
116133359Sobrien/*
117133359Sobrien * hstrerror --
118133359Sobrien *	return the string associated with a given "host" errno value.
119133359Sobrien */
120133359Sobrienconst char *
121133359Sobrienhstrerror(int err) {
122133359Sobrien	if (err < 0)
123133359Sobrien		return ("Resolver internal error");
124133359Sobrien	else if (err < h_nerr)
125133359Sobrien		return (h_errlist[err]);
12668349Sobrien	return ("Unknown resolver error");
127133359Sobrien}
12868349Sobrien