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