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