Home | History | Annotate | Line # | Download | only in resolv
herror.c revision 1.8.6.1
      1  1.8.6.1      yamt /*	$NetBSD: herror.c,v 1.8.6.1 2012/04/17 00:05:22 yamt Exp $	*/
      2      1.1  christos 
      3      1.1  christos /*
      4      1.1  christos  * Copyright (c) 1987, 1993
      5      1.1  christos  *    The Regents of the University of California.  All rights reserved.
      6      1.1  christos  *
      7      1.1  christos  * Redistribution and use in source and binary forms, with or without
      8      1.1  christos  * modification, are permitted provided that the following conditions
      9      1.1  christos  * are met:
     10      1.1  christos  * 1. Redistributions of source code must retain the above copyright
     11      1.1  christos  *    notice, this list of conditions and the following disclaimer.
     12      1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     14      1.1  christos  *    documentation and/or other materials provided with the distribution.
     15      1.1  christos  * 3. All advertising materials mentioning features or use of this software
     16      1.1  christos  *    must display the following acknowledgement:
     17      1.1  christos  * 	This product includes software developed by the University of
     18      1.1  christos  * 	California, Berkeley and its contributors.
     19      1.1  christos  * 4. Neither the name of the University nor the names of its contributors
     20      1.1  christos  *    may be used to endorse or promote products derived from this software
     21      1.1  christos  *    without specific prior written permission.
     22      1.1  christos  *
     23      1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24      1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25      1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26      1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27      1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28      1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29      1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30      1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31      1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32      1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33      1.1  christos  * SUCH DAMAGE.
     34      1.1  christos  */
     35      1.1  christos 
     36      1.1  christos /*
     37      1.1  christos  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
     38      1.1  christos  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
     39      1.1  christos  *
     40      1.1  christos  * Permission to use, copy, modify, and distribute this software for any
     41      1.1  christos  * purpose with or without fee is hereby granted, provided that the above
     42      1.1  christos  * copyright notice and this permission notice appear in all copies.
     43      1.1  christos  *
     44      1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
     45      1.1  christos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     46      1.1  christos  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
     47      1.1  christos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     48      1.1  christos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     49      1.1  christos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     50      1.1  christos  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     51      1.1  christos  */
     52      1.1  christos 
     53      1.3  christos #include <sys/cdefs.h>
     54      1.1  christos #if defined(LIBC_SCCS) && !defined(lint)
     55      1.3  christos #ifdef notdef
     56      1.7  christos static const char sccsid[] = "@(#)herror.c	8.1 (Berkeley) 6/4/93";
     57      1.7  christos static const char rcsid[] = "Id: herror.c,v 1.4 2005/04/27 04:56:41 sra Exp";
     58      1.3  christos #else
     59  1.8.6.1      yamt __RCSID("$NetBSD: herror.c,v 1.8.6.1 2012/04/17 00:05:22 yamt Exp $");
     60      1.3  christos #endif
     61      1.1  christos #endif /* LIBC_SCCS and not lint */
     62      1.1  christos 
     63      1.1  christos #include "port_before.h"
     64      1.1  christos 
     65      1.4  christos #include "namespace.h"
     66      1.1  christos #include <sys/types.h>
     67      1.1  christos #include <sys/param.h>
     68      1.1  christos #include <sys/uio.h>
     69      1.1  christos 
     70      1.1  christos #include <netinet/in.h>
     71      1.1  christos #include <arpa/nameser.h>
     72      1.1  christos 
     73      1.1  christos #include <netdb.h>
     74      1.1  christos #include <resolv.h>
     75      1.1  christos #include <string.h>
     76      1.1  christos #include <unistd.h>
     77      1.1  christos 
     78      1.1  christos #include "port_after.h"
     79      1.1  christos 
     80      1.1  christos const char *h_errlist[] = {
     81      1.1  christos 	"Resolver Error 0 (no error)",
     82      1.5  christos 	"Unknown host",				/*%< 1 HOST_NOT_FOUND */
     83      1.5  christos 	"Host name lookup failure",		/*%< 2 TRY_AGAIN */
     84      1.5  christos 	"Unknown server error",			/*%< 3 NO_RECOVERY */
     85      1.5  christos 	"No address associated with name",	/*%< 4 NO_ADDRESS */
     86      1.1  christos };
     87      1.1  christos int	h_nerr = { sizeof h_errlist / sizeof h_errlist[0] };
     88      1.1  christos 
     89      1.1  christos #if !(__GLIBC__ > 2 || __GLIBC__ == 2 &&  __GLIBC_MINOR__ >= 3)
     90      1.1  christos #undef	h_errno
     91      1.1  christos int	h_errno;
     92      1.1  christos #endif
     93      1.1  christos 
     94      1.4  christos #ifdef __weak_alias
     95      1.4  christos __weak_alias(herror,_herror)
     96      1.4  christos #endif
     97      1.4  christos 
     98      1.5  christos /*%
     99      1.1  christos  * herror --
    100      1.1  christos  *	print the error indicated by the h_errno value.
    101      1.1  christos  */
    102      1.1  christos void
    103      1.1  christos herror(const char *s) {
    104      1.1  christos 	struct iovec iov[4], *v = iov;
    105      1.1  christos 	char *t;
    106      1.1  christos 
    107      1.1  christos 	if (s != NULL && *s != '\0') {
    108      1.1  christos 		DE_CONST(s, t);
    109      1.1  christos 		v->iov_base = t;
    110      1.1  christos 		v->iov_len = strlen(t);
    111      1.1  christos 		v++;
    112      1.1  christos 		DE_CONST(": ", t);
    113      1.1  christos 		v->iov_base = t;
    114      1.1  christos 		v->iov_len = 2;
    115      1.1  christos 		v++;
    116      1.1  christos 	}
    117      1.7  christos 	DE_CONST(hstrerror(*__h_errno()), t);
    118      1.1  christos 	v->iov_base = t;
    119      1.1  christos 	v->iov_len = strlen(v->iov_base);
    120      1.1  christos 	v++;
    121      1.1  christos 	DE_CONST("\n", t);
    122      1.1  christos 	v->iov_base = t;
    123      1.1  christos 	v->iov_len = 1;
    124  1.8.6.1      yamt 	(void)writev(STDERR_FILENO, iov, (int)((v - iov) + 1));
    125      1.1  christos }
    126      1.1  christos 
    127      1.5  christos /*%
    128      1.1  christos  * hstrerror --
    129      1.1  christos  *	return the string associated with a given "host" errno value.
    130      1.1  christos  */
    131      1.1  christos const char *
    132      1.1  christos hstrerror(int err) {
    133      1.1  christos 	if (err < 0)
    134      1.1  christos 		return ("Resolver internal error");
    135      1.1  christos 	else if (err < h_nerr)
    136      1.1  christos 		return (h_errlist[err]);
    137      1.1  christos 	return ("Unknown resolver error");
    138      1.1  christos }
    139      1.5  christos 
    140      1.5  christos /*! \file */
    141