xref: /minix3/lib/libc/resolv/herror.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: herror.c,v 1.10 2015/02/24 17:56:20 christos Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras  * Copyright (c) 1987, 1993
52fe8fb19SBen Gras  *    The Regents of the University of California.  All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras  * are met:
102fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
15*0a6a1f1dSLionel Sambuc  * 3. Neither the name of the University nor the names of its contributors
162fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
172fe8fb19SBen Gras  *    without specific prior written permission.
182fe8fb19SBen Gras  *
192fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
202fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
212fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
222fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
232fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
242fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
252fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
272fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
282fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
292fe8fb19SBen Gras  * SUCH DAMAGE.
302fe8fb19SBen Gras  */
312fe8fb19SBen Gras 
322fe8fb19SBen Gras /*
332fe8fb19SBen Gras  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
342fe8fb19SBen Gras  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
352fe8fb19SBen Gras  *
362fe8fb19SBen Gras  * Permission to use, copy, modify, and distribute this software for any
372fe8fb19SBen Gras  * purpose with or without fee is hereby granted, provided that the above
382fe8fb19SBen Gras  * copyright notice and this permission notice appear in all copies.
392fe8fb19SBen Gras  *
402fe8fb19SBen Gras  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
412fe8fb19SBen Gras  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
422fe8fb19SBen Gras  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
432fe8fb19SBen Gras  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
442fe8fb19SBen Gras  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
452fe8fb19SBen Gras  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
462fe8fb19SBen Gras  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
472fe8fb19SBen Gras  */
482fe8fb19SBen Gras 
492fe8fb19SBen Gras #include <sys/cdefs.h>
502fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
512fe8fb19SBen Gras #ifdef notdef
522fe8fb19SBen Gras static const char sccsid[] = "@(#)herror.c	8.1 (Berkeley) 6/4/93";
532fe8fb19SBen Gras static const char rcsid[] = "Id: herror.c,v 1.4 2005/04/27 04:56:41 sra Exp";
542fe8fb19SBen Gras #else
55*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: herror.c,v 1.10 2015/02/24 17:56:20 christos Exp $");
562fe8fb19SBen Gras #endif
572fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
582fe8fb19SBen Gras 
592fe8fb19SBen Gras #include "port_before.h"
602fe8fb19SBen Gras 
612fe8fb19SBen Gras #include "namespace.h"
622fe8fb19SBen Gras #include <sys/types.h>
632fe8fb19SBen Gras #include <sys/param.h>
642fe8fb19SBen Gras #include <sys/uio.h>
652fe8fb19SBen Gras 
662fe8fb19SBen Gras #include <netinet/in.h>
672fe8fb19SBen Gras #include <arpa/nameser.h>
682fe8fb19SBen Gras 
692fe8fb19SBen Gras #include <netdb.h>
702fe8fb19SBen Gras #include <resolv.h>
712fe8fb19SBen Gras #include <string.h>
722fe8fb19SBen Gras #include <unistd.h>
732fe8fb19SBen Gras 
742fe8fb19SBen Gras #include "port_after.h"
752fe8fb19SBen Gras 
762fe8fb19SBen Gras const char *h_errlist[] = {
772fe8fb19SBen Gras 	"Resolver Error 0 (no error)",
782fe8fb19SBen Gras 	"Unknown host",				/*%< 1 HOST_NOT_FOUND */
792fe8fb19SBen Gras 	"Host name lookup failure",		/*%< 2 TRY_AGAIN */
802fe8fb19SBen Gras 	"Unknown server error",			/*%< 3 NO_RECOVERY */
812fe8fb19SBen Gras 	"No address associated with name",	/*%< 4 NO_ADDRESS */
822fe8fb19SBen Gras };
832fe8fb19SBen Gras int	h_nerr = { sizeof h_errlist / sizeof h_errlist[0] };
842fe8fb19SBen Gras 
852fe8fb19SBen Gras #if !(__GLIBC__ > 2 || __GLIBC__ == 2 &&  __GLIBC_MINOR__ >= 3)
862fe8fb19SBen Gras #undef	h_errno
872fe8fb19SBen Gras int	h_errno;
882fe8fb19SBen Gras #endif
892fe8fb19SBen Gras 
902fe8fb19SBen Gras #ifdef __weak_alias
__weak_alias(herror,_herror)912fe8fb19SBen Gras __weak_alias(herror,_herror)
922fe8fb19SBen Gras #endif
932fe8fb19SBen Gras 
942fe8fb19SBen Gras /*%
952fe8fb19SBen Gras  * herror --
962fe8fb19SBen Gras  *	print the error indicated by the h_errno value.
972fe8fb19SBen Gras  */
982fe8fb19SBen Gras void
992fe8fb19SBen Gras herror(const char *s) {
1002fe8fb19SBen Gras 	struct iovec iov[4], *v = iov;
1012fe8fb19SBen Gras 	char *t;
1022fe8fb19SBen Gras 
1032fe8fb19SBen Gras 	if (s != NULL && *s != '\0') {
1042fe8fb19SBen Gras 		DE_CONST(s, t);
1052fe8fb19SBen Gras 		v->iov_base = t;
1062fe8fb19SBen Gras 		v->iov_len = strlen(t);
1072fe8fb19SBen Gras 		v++;
1082fe8fb19SBen Gras 		DE_CONST(": ", t);
1092fe8fb19SBen Gras 		v->iov_base = t;
1102fe8fb19SBen Gras 		v->iov_len = 2;
1112fe8fb19SBen Gras 		v++;
1122fe8fb19SBen Gras 	}
1132fe8fb19SBen Gras 	DE_CONST(hstrerror(*__h_errno()), t);
1142fe8fb19SBen Gras 	v->iov_base = t;
1152fe8fb19SBen Gras 	v->iov_len = strlen(v->iov_base);
1162fe8fb19SBen Gras 	v++;
1172fe8fb19SBen Gras 	DE_CONST("\n", t);
1182fe8fb19SBen Gras 	v->iov_base = t;
1192fe8fb19SBen Gras 	v->iov_len = 1;
120f14fb602SLionel Sambuc 	(void)writev(STDERR_FILENO, iov, (int)((v - iov) + 1));
1212fe8fb19SBen Gras }
1222fe8fb19SBen Gras 
1232fe8fb19SBen Gras /*%
1242fe8fb19SBen Gras  * hstrerror --
1252fe8fb19SBen Gras  *	return the string associated with a given "host" errno value.
1262fe8fb19SBen Gras  */
1272fe8fb19SBen Gras const char *
hstrerror(int err)1282fe8fb19SBen Gras hstrerror(int err) {
1292fe8fb19SBen Gras 	if (err < 0)
1302fe8fb19SBen Gras 		return ("Resolver internal error");
1312fe8fb19SBen Gras 	else if (err < h_nerr)
1322fe8fb19SBen Gras 		return (h_errlist[err]);
1332fe8fb19SBen Gras 	return ("Unknown resolver error");
1342fe8fb19SBen Gras }
1352fe8fb19SBen Gras 
1362fe8fb19SBen Gras /*! \file */
137