xref: /minix/external/bsd/bind/dist/lib/lwres/herror.c (revision 00b67f09)
1 /*	$NetBSD: herror.c,v 1.6 2014/12/10 04:38:02 christos Exp $	*/
2 
3 /*
4  * Portions Copyright (C) 2004, 2005, 2007, 2011, 2012, 2014  Internet Systems Consortium, Inc. ("ISC")
5  * Portions Copyright (C) 2000, 2001, 2003  Internet Software Consortium.
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /*
21  * Copyright (c) 1987, 1993
22  *    The Regents of the University of California.  All rights reserved.
23  *
24  * Redistribution and use in source and binary forms, with or without
25  * modification, are permitted provided that the following conditions
26  * are met:
27  * 1. Redistributions of source code must retain the above copyright
28  *    notice, this list of conditions and the following disclaimer.
29  * 2. Redistributions in binary form must reproduce the above copyright
30  *    notice, this list of conditions and the following disclaimer in the
31  *    documentation and/or other materials provided with the distribution.
32  * 3. Neither the name of the University nor the names of its contributors
33  *    may be used to endorse or promote products derived from this software
34  *    without specific prior written permission.
35  *
36  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
37  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
40  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  */
48 
49 /*! \file herror.c
50    lwres_herror() prints the string s on stderr followed by the string
51    generated by lwres_hstrerror() for the error code stored in the global
52    variable lwres_h_errno.
53 
54    lwres_hstrerror() returns an appropriate string for the error code
55    gievn by err. The values of the error codes and messages are as
56    follows:
57 
58 \li   #NETDB_SUCCESS: Resolver Error 0 (no error)
59 
60 \li   #HOST_NOT_FOUND: Unknown host
61 
62 \li #TRY_AGAIN: Host name lookup failure
63 
64 \li   #NO_RECOVERY: Unknown server error
65 
66 \li   #NO_DATA: No address associated with name
67 
68  */
69 
70 #if defined(LIBC_SCCS) && !defined(lint)
71 static const char sccsid[] = "@(#)herror.c	8.1 (Berkeley) 6/4/93";
72 static const char rcsid[] =
73 	"Id";
74 #endif /* LIBC_SCCS and not lint */
75 
76 #include <config.h>
77 
78 #include <stdio.h>
79 
80 #include <lwres/netdb.h>
81 #include <lwres/platform.h>
82 
83 LIBLWRES_EXTERNAL_DATA int	lwres_h_errno;
84 
85 /*!
86  * these have never been declared in any header file so make them static
87  */
88 
89 static const char *h_errlist[] = {
90 	"Resolver Error 0 (no error)",		/*%< 0 no error */
91 	"Unknown host",				/*%< 1 HOST_NOT_FOUND */
92 	"Host name lookup failure",		/*%< 2 TRY_AGAIN */
93 	"Unknown server error",			/*%< 3 NO_RECOVERY */
94 	"No address associated with name",	/*%< 4 NO_ADDRESS */
95 };
96 
97 static int	h_nerr = sizeof(h_errlist) / sizeof(h_errlist[0]);
98 
99 
100 /*!
101  * herror --
102  *	print the error indicated by the h_errno value.
103  */
104 void
lwres_herror(const char * s)105 lwres_herror(const char *s) {
106 	fprintf(stderr, "%s: %s\n", s, lwres_hstrerror(lwres_h_errno));
107 }
108 
109 /*!
110  * hstrerror --
111  *	return the string associated with a given "host" errno value.
112  */
113 const char *
lwres_hstrerror(int err)114 lwres_hstrerror(int err) {
115 	if (err < 0)
116 		return ("Resolver internal error");
117 	else if (err < h_nerr)
118 		return (h_errlist[err]);
119 	return ("Unknown resolver error");
120 }
121