herror.c revision 1.3 1 1.3 christos /* $NetBSD: herror.c,v 1.3 2004/05/20 19:43:39 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.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.1 christos static const char sccsid[] = "@(#)herror.c 8.1 (Berkeley) 6/4/93";
57 1.1 christos static const char rcsid[] = "Id: herror.c,v 1.2.206.1 2004/03/09 08:33:54 marka Exp";
58 1.3 christos #else
59 1.3 christos __RCSID("$NetBSD: herror.c,v 1.3 2004/05/20 19:43:39 christos 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.1 christos #include <sys/types.h>
66 1.1 christos #include <sys/param.h>
67 1.1 christos #include <sys/uio.h>
68 1.1 christos
69 1.1 christos #include <netinet/in.h>
70 1.1 christos #include <arpa/nameser.h>
71 1.1 christos
72 1.1 christos #include <netdb.h>
73 1.1 christos #include <resolv.h>
74 1.1 christos #include <string.h>
75 1.1 christos #include <unistd.h>
76 1.1 christos
77 1.1 christos #include "port_after.h"
78 1.1 christos
79 1.1 christos const char *h_errlist[] = {
80 1.1 christos "Resolver Error 0 (no error)",
81 1.1 christos "Unknown host", /* 1 HOST_NOT_FOUND */
82 1.1 christos "Host name lookup failure", /* 2 TRY_AGAIN */
83 1.1 christos "Unknown server error", /* 3 NO_RECOVERY */
84 1.1 christos "No address associated with name", /* 4 NO_ADDRESS */
85 1.1 christos };
86 1.1 christos int h_nerr = { sizeof h_errlist / sizeof h_errlist[0] };
87 1.1 christos
88 1.1 christos #if !(__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)
89 1.1 christos #undef h_errno
90 1.1 christos int h_errno;
91 1.1 christos #endif
92 1.1 christos
93 1.1 christos /*
94 1.1 christos * herror --
95 1.1 christos * print the error indicated by the h_errno value.
96 1.1 christos */
97 1.1 christos void
98 1.1 christos herror(const char *s) {
99 1.1 christos struct iovec iov[4], *v = iov;
100 1.1 christos char *t;
101 1.1 christos
102 1.1 christos if (s != NULL && *s != '\0') {
103 1.1 christos DE_CONST(s, t);
104 1.1 christos v->iov_base = t;
105 1.1 christos v->iov_len = strlen(t);
106 1.1 christos v++;
107 1.1 christos DE_CONST(": ", t);
108 1.1 christos v->iov_base = t;
109 1.1 christos v->iov_len = 2;
110 1.1 christos v++;
111 1.1 christos }
112 1.2 christos DE_CONST(hstrerror(h_errno), t);
113 1.1 christos v->iov_base = t;
114 1.1 christos v->iov_len = strlen(v->iov_base);
115 1.1 christos v++;
116 1.1 christos DE_CONST("\n", t);
117 1.1 christos v->iov_base = t;
118 1.1 christos v->iov_len = 1;
119 1.1 christos writev(STDERR_FILENO, iov, (v - iov) + 1);
120 1.1 christos }
121 1.1 christos
122 1.1 christos /*
123 1.1 christos * hstrerror --
124 1.1 christos * return the string associated with a given "host" errno value.
125 1.1 christos */
126 1.1 christos const char *
127 1.1 christos hstrerror(int err) {
128 1.1 christos if (err < 0)
129 1.1 christos return ("Resolver internal error");
130 1.1 christos else if (err < h_nerr)
131 1.1 christos return (h_errlist[err]);
132 1.1 christos return ("Unknown resolver error");
133 1.1 christos }
134