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