1 /*	$NetBSD: errno2result.c,v 1.5 2014/12/10 04:38:01 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2005, 2007, 2011-2013  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 2000-2002  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 /* Id */
21 
22 /*! \file */
23 
24 #include <config.h>
25 
26 #include <isc/result.h>
27 #include <isc/strerror.h>
28 #include <isc/util.h>
29 
30 #include "errno2result.h"
31 
32 /*%
33  * Convert a POSIX errno value into an isc_result_t.  The
34  * list of supported errno values is not complete; new users
35  * of this function should add any expected errors that are
36  * not already there.
37  */
38 isc_result_t
39 isc___errno2result(int posixerrno, const char *file, unsigned int line) {
40 	char strbuf[ISC_STRERRORSIZE];
41 
42 	switch (posixerrno) {
43 	case ENOTDIR:
44 	case ELOOP:
45 	case EINVAL:		/* XXX sometimes this is not for files */
46 	case ENAMETOOLONG:
47 	case EBADF:
48 		return (ISC_R_INVALIDFILE);
49 	case ENOENT:
50 		return (ISC_R_FILENOTFOUND);
51 	case EACCES:
52 	case EPERM:
53 		return (ISC_R_NOPERM);
54 	case EEXIST:
55 		return (ISC_R_FILEEXISTS);
56 	case EIO:
57 		return (ISC_R_IOERROR);
58 	case ENOMEM:
59 		return (ISC_R_NOMEMORY);
60 	case ENFILE:
61 	case EMFILE:
62 		return (ISC_R_TOOMANYOPENFILES);
63 #ifdef EOVERFLOW
64 	case EOVERFLOW:
65 		return (ISC_R_RANGE);
66 #endif
67 	case EPIPE:
68 #ifdef ECONNRESET
69 	case ECONNRESET:
70 #endif
71 #ifdef ECONNABORTED
72 	case ECONNABORTED:
73 #endif
74 		return (ISC_R_CONNECTIONRESET);
75 #ifdef ENOTCONN
76 	case ENOTCONN:
77 		return (ISC_R_NOTCONNECTED);
78 #endif
79 #ifdef ETIMEDOUT
80 	case ETIMEDOUT:
81 		return (ISC_R_TIMEDOUT);
82 #endif
83 #ifdef ENOBUFS
84 	case ENOBUFS:
85 		return (ISC_R_NORESOURCES);
86 #endif
87 #ifdef EAFNOSUPPORT
88 	case EAFNOSUPPORT:
89 		return (ISC_R_FAMILYNOSUPPORT);
90 #endif
91 #ifdef ENETDOWN
92 	case ENETDOWN:
93 		return (ISC_R_NETDOWN);
94 #endif
95 #ifdef EHOSTDOWN
96 	case EHOSTDOWN:
97 		return (ISC_R_HOSTDOWN);
98 #endif
99 #ifdef ENETUNREACH
100 	case ENETUNREACH:
101 		return (ISC_R_NETUNREACH);
102 #endif
103 #ifdef EHOSTUNREACH
104 	case EHOSTUNREACH:
105 		return (ISC_R_HOSTUNREACH);
106 #endif
107 #ifdef EADDRINUSE
108 	case EADDRINUSE:
109 		return (ISC_R_ADDRINUSE);
110 #endif
111 	case EADDRNOTAVAIL:
112 		return (ISC_R_ADDRNOTAVAIL);
113 	case ECONNREFUSED:
114 		return (ISC_R_CONNREFUSED);
115 	default:
116 		isc__strerror(posixerrno, strbuf, sizeof(strbuf));
117 		UNEXPECTED_ERROR(file, line, "unable to convert errno "
118 				 "to isc_result: %d: %s",
119 				 posixerrno, strbuf);
120 		/*
121 		 * XXXDCL would be nice if perhaps this function could
122 		 * return the system's error string, so the caller
123 		 * might have something more descriptive than "unexpected
124 		 * error" to log with.
125 		 */
126 		return (ISC_R_UNEXPECTED);
127 	}
128 }
129