xref: /openbsd-src/lib/libc/net/herror.c (revision 06201dd4d8b19e0383322797ffab28e23bd09e0e)
1*06201dd4Sguenther /*	$OpenBSD: herror.c,v 1.10 2015/09/14 07:38:38 guenther Exp $	*/
27949d54bSdownsj 
37949d54bSdownsj /*
47949d54bSdownsj  * ++Copyright++ 1987, 1993
57949d54bSdownsj  * -
6df930be7Sderaadt  * Copyright (c) 1987, 1993
7df930be7Sderaadt  *    The Regents of the University of California.  All rights reserved.
8df930be7Sderaadt  *
9df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
10df930be7Sderaadt  * modification, are permitted provided that the following conditions
11df930be7Sderaadt  * are met:
12df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
13df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
14df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
15df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
16df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
176580fee3Smillert  * 3. Neither the name of the University nor the names of its contributors
18df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
19df930be7Sderaadt  *    without specific prior written permission.
20df930be7Sderaadt  *
21df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df930be7Sderaadt  * SUCH DAMAGE.
32df930be7Sderaadt  * -
33df930be7Sderaadt  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
34df930be7Sderaadt  *
35df930be7Sderaadt  * Permission to use, copy, modify, and distribute this software for any
36df930be7Sderaadt  * purpose with or without fee is hereby granted, provided that the above
37df930be7Sderaadt  * copyright notice and this permission notice appear in all copies, and that
38df930be7Sderaadt  * the name of Digital Equipment Corporation not be used in advertising or
39df930be7Sderaadt  * publicity pertaining to distribution of the document or software without
40df930be7Sderaadt  * specific, written prior permission.
41df930be7Sderaadt  *
42df930be7Sderaadt  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
43df930be7Sderaadt  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
44df930be7Sderaadt  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
45df930be7Sderaadt  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
46df930be7Sderaadt  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
47df930be7Sderaadt  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
48df930be7Sderaadt  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
49df930be7Sderaadt  * SOFTWARE.
50df930be7Sderaadt  * -
51df930be7Sderaadt  * --Copyright--
52df930be7Sderaadt  */
53df930be7Sderaadt 
54df930be7Sderaadt #include <sys/types.h>
55df930be7Sderaadt #include <sys/uio.h>
56df930be7Sderaadt #include <netdb.h>
57df930be7Sderaadt #include <unistd.h>
58df930be7Sderaadt #include <string.h>
59df930be7Sderaadt 
60c58df661Smillert const char * const h_errlist[] = {
611ddd5cc1Sdm 	"Resolver Error 0 (no error)",
62df930be7Sderaadt 	"Unknown host",				/* 1 HOST_NOT_FOUND */
63df930be7Sderaadt 	"Host name lookup failure",		/* 2 TRY_AGAIN */
64df930be7Sderaadt 	"Unknown server error",			/* 3 NO_RECOVERY */
65df930be7Sderaadt 	"No address associated with name",	/* 4 NO_ADDRESS */
66df930be7Sderaadt };
67c58df661Smillert const int	h_nerr = { sizeof h_errlist / sizeof h_errlist[0] };
68df930be7Sderaadt 
69df930be7Sderaadt extern int	h_errno;
70df930be7Sderaadt 
71df930be7Sderaadt /*
72df930be7Sderaadt  * herror --
73df930be7Sderaadt  *	print the error indicated by the h_errno value.
74df930be7Sderaadt  */
75df930be7Sderaadt void
herror(const char * s)76db5b349cSotto herror(const char *s)
77df930be7Sderaadt {
78df930be7Sderaadt 	struct iovec iov[4];
79db5b349cSotto 	struct iovec *v = iov;
80df930be7Sderaadt 
81df930be7Sderaadt 	if (s && *s) {
82df930be7Sderaadt 		v->iov_base = (char *)s;
83df930be7Sderaadt 		v->iov_len = strlen(s);
84df930be7Sderaadt 		v++;
85df930be7Sderaadt 		v->iov_base = ": ";
86df930be7Sderaadt 		v->iov_len = 2;
87df930be7Sderaadt 		v++;
88df930be7Sderaadt 	}
891ddd5cc1Sdm 	v->iov_base = (char *)hstrerror(h_errno);
90df930be7Sderaadt 	v->iov_len = strlen(v->iov_base);
91df930be7Sderaadt 	v++;
92df930be7Sderaadt 	v->iov_base = "\n";
93df930be7Sderaadt 	v->iov_len = 1;
94df930be7Sderaadt 	writev(STDERR_FILENO, iov, (v - iov) + 1);
95df930be7Sderaadt }
96df930be7Sderaadt 
971ddd5cc1Sdm const char *
hstrerror(int err)98db5b349cSotto hstrerror(int err)
99df930be7Sderaadt {
1001ddd5cc1Sdm 	if (err < 0)
1011ddd5cc1Sdm 		return ("Resolver internal error");
1021ddd5cc1Sdm 	else if (err < h_nerr)
1031ddd5cc1Sdm 		return (h_errlist[err]);
1041ddd5cc1Sdm 	return ("Unknown resolver error");
105df930be7Sderaadt }
106*06201dd4Sguenther DEF_WEAK(hstrerror);
107