1*a466cc55SCy Schubert /*
2*a466cc55SCy Schubert * Copyright (C) 2004, 2005, 2007, 2011, 2012 Internet Systems Consortium, Inc. ("ISC")
3*a466cc55SCy Schubert * Copyright (C) 2000-2002 Internet Software Consortium.
4*a466cc55SCy Schubert *
5*a466cc55SCy Schubert * Permission to use, copy, modify, and/or distribute this software for any
6*a466cc55SCy Schubert * purpose with or without fee is hereby granted, provided that the above
7*a466cc55SCy Schubert * copyright notice and this permission notice appear in all copies.
8*a466cc55SCy Schubert *
9*a466cc55SCy Schubert * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*a466cc55SCy Schubert * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*a466cc55SCy Schubert * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*a466cc55SCy Schubert * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*a466cc55SCy Schubert * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*a466cc55SCy Schubert * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*a466cc55SCy Schubert * PERFORMANCE OF THIS SOFTWARE.
16*a466cc55SCy Schubert */
17*a466cc55SCy Schubert
18*a466cc55SCy Schubert /* $Id$ */
19*a466cc55SCy Schubert
20*a466cc55SCy Schubert /*! \file */
21*a466cc55SCy Schubert
22*a466cc55SCy Schubert #include <config.h>
23*a466cc55SCy Schubert
24*a466cc55SCy Schubert #include <isc/result.h>
25*a466cc55SCy Schubert #include <isc/strerror.h>
26*a466cc55SCy Schubert #include <isc/util.h>
27*a466cc55SCy Schubert
28*a466cc55SCy Schubert #include "errno2result.h"
29*a466cc55SCy Schubert
30*a466cc55SCy Schubert /*%
31*a466cc55SCy Schubert * Convert a POSIX errno value into an isc_result_t. The
32*a466cc55SCy Schubert * list of supported errno values is not complete; new users
33*a466cc55SCy Schubert * of this function should add any expected errors that are
34*a466cc55SCy Schubert * not already there.
35*a466cc55SCy Schubert */
36*a466cc55SCy Schubert isc_result_t
isc___errno2result(int posixerrno,const char * file,unsigned int line)37*a466cc55SCy Schubert isc___errno2result(int posixerrno, const char *file, unsigned int line) {
38*a466cc55SCy Schubert char strbuf[ISC_STRERRORSIZE];
39*a466cc55SCy Schubert
40*a466cc55SCy Schubert switch (posixerrno) {
41*a466cc55SCy Schubert case ENOTDIR:
42*a466cc55SCy Schubert case ELOOP:
43*a466cc55SCy Schubert case EINVAL: /* XXX sometimes this is not for files */
44*a466cc55SCy Schubert case ENAMETOOLONG:
45*a466cc55SCy Schubert case EBADF:
46*a466cc55SCy Schubert return (ISC_R_INVALIDFILE);
47*a466cc55SCy Schubert case ENOENT:
48*a466cc55SCy Schubert return (ISC_R_FILENOTFOUND);
49*a466cc55SCy Schubert case EACCES:
50*a466cc55SCy Schubert case EPERM:
51*a466cc55SCy Schubert return (ISC_R_NOPERM);
52*a466cc55SCy Schubert case EEXIST:
53*a466cc55SCy Schubert return (ISC_R_FILEEXISTS);
54*a466cc55SCy Schubert case EIO:
55*a466cc55SCy Schubert return (ISC_R_IOERROR);
56*a466cc55SCy Schubert case ENOMEM:
57*a466cc55SCy Schubert return (ISC_R_NOMEMORY);
58*a466cc55SCy Schubert case ENFILE:
59*a466cc55SCy Schubert case EMFILE:
60*a466cc55SCy Schubert return (ISC_R_TOOMANYOPENFILES);
61*a466cc55SCy Schubert case EPIPE:
62*a466cc55SCy Schubert #ifdef ECONNRESET
63*a466cc55SCy Schubert case ECONNRESET:
64*a466cc55SCy Schubert #endif
65*a466cc55SCy Schubert #ifdef ECONNABORTED
66*a466cc55SCy Schubert case ECONNABORTED:
67*a466cc55SCy Schubert #endif
68*a466cc55SCy Schubert return (ISC_R_CONNECTIONRESET);
69*a466cc55SCy Schubert #ifdef ENOTCONN
70*a466cc55SCy Schubert case ENOTCONN:
71*a466cc55SCy Schubert return (ISC_R_NOTCONNECTED);
72*a466cc55SCy Schubert #endif
73*a466cc55SCy Schubert #ifdef ETIMEDOUT
74*a466cc55SCy Schubert case ETIMEDOUT:
75*a466cc55SCy Schubert return (ISC_R_TIMEDOUT);
76*a466cc55SCy Schubert #endif
77*a466cc55SCy Schubert #ifdef ENOBUFS
78*a466cc55SCy Schubert case ENOBUFS:
79*a466cc55SCy Schubert return (ISC_R_NORESOURCES);
80*a466cc55SCy Schubert #endif
81*a466cc55SCy Schubert #ifdef EAFNOSUPPORT
82*a466cc55SCy Schubert case EAFNOSUPPORT:
83*a466cc55SCy Schubert return (ISC_R_FAMILYNOSUPPORT);
84*a466cc55SCy Schubert #endif
85*a466cc55SCy Schubert #ifdef ENETDOWN
86*a466cc55SCy Schubert case ENETDOWN:
87*a466cc55SCy Schubert return (ISC_R_NETDOWN);
88*a466cc55SCy Schubert #endif
89*a466cc55SCy Schubert #ifdef EHOSTDOWN
90*a466cc55SCy Schubert case EHOSTDOWN:
91*a466cc55SCy Schubert return (ISC_R_HOSTDOWN);
92*a466cc55SCy Schubert #endif
93*a466cc55SCy Schubert #ifdef ENETUNREACH
94*a466cc55SCy Schubert case ENETUNREACH:
95*a466cc55SCy Schubert return (ISC_R_NETUNREACH);
96*a466cc55SCy Schubert #endif
97*a466cc55SCy Schubert #ifdef EHOSTUNREACH
98*a466cc55SCy Schubert case EHOSTUNREACH:
99*a466cc55SCy Schubert return (ISC_R_HOSTUNREACH);
100*a466cc55SCy Schubert #endif
101*a466cc55SCy Schubert #ifdef EADDRINUSE
102*a466cc55SCy Schubert case EADDRINUSE:
103*a466cc55SCy Schubert return (ISC_R_ADDRINUSE);
104*a466cc55SCy Schubert #endif
105*a466cc55SCy Schubert case EADDRNOTAVAIL:
106*a466cc55SCy Schubert return (ISC_R_ADDRNOTAVAIL);
107*a466cc55SCy Schubert case ECONNREFUSED:
108*a466cc55SCy Schubert return (ISC_R_CONNREFUSED);
109*a466cc55SCy Schubert default:
110*a466cc55SCy Schubert isc__strerror(posixerrno, strbuf, sizeof(strbuf));
111*a466cc55SCy Schubert UNEXPECTED_ERROR(file, line, "unable to convert errno "
112*a466cc55SCy Schubert "to isc_result: %d: %s",
113*a466cc55SCy Schubert posixerrno, strbuf);
114*a466cc55SCy Schubert /*
115*a466cc55SCy Schubert * XXXDCL would be nice if perhaps this function could
116*a466cc55SCy Schubert * return the system's error string, so the caller
117*a466cc55SCy Schubert * might have something more descriptive than "unexpected
118*a466cc55SCy Schubert * error" to log with.
119*a466cc55SCy Schubert */
120*a466cc55SCy Schubert return (ISC_R_UNEXPECTED);
121*a466cc55SCy Schubert }
122*a466cc55SCy Schubert }
123