1135446Strhodes/*
2234010Sdougb * Copyright (C) 2004, 2005, 2007, 2011, 2012  Internet Systems Consortium, Inc. ("ISC")
3135446Strhodes * Copyright (C) 2000-2002  Internet Software Consortium.
4135446Strhodes *
5193149Sdougb * Permission to use, copy, modify, and/or distribute this software for any
6135446Strhodes * purpose with or without fee is hereby granted, provided that the above
7135446Strhodes * copyright notice and this permission notice appear in all copies.
8135446Strhodes *
9135446Strhodes * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10135446Strhodes * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11135446Strhodes * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12135446Strhodes * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13135446Strhodes * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14135446Strhodes * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15135446Strhodes * PERFORMANCE OF THIS SOFTWARE.
16135446Strhodes */
17135446Strhodes
18234010Sdougb/* $Id$ */
19135446Strhodes
20170222Sdougb/*! \file */
21170222Sdougb
22135446Strhodes#include <config.h>
23135446Strhodes
24135446Strhodes#include <isc/result.h>
25135446Strhodes#include <isc/strerror.h>
26135446Strhodes#include <isc/util.h>
27135446Strhodes
28135446Strhodes#include "errno2result.h"
29135446Strhodes
30170222Sdougb/*%
31135446Strhodes * Convert a POSIX errno value into an isc_result_t.  The
32135446Strhodes * list of supported errno values is not complete; new users
33135446Strhodes * of this function should add any expected errors that are
34135446Strhodes * not already there.
35135446Strhodes */
36135446Strhodesisc_result_t
37234010Sdougbisc___errno2result(int posixerrno, const char *file, unsigned int line) {
38135446Strhodes	char strbuf[ISC_STRERRORSIZE];
39135446Strhodes
40135446Strhodes	switch (posixerrno) {
41135446Strhodes	case ENOTDIR:
42135446Strhodes	case ELOOP:
43135446Strhodes	case EINVAL:		/* XXX sometimes this is not for files */
44135446Strhodes	case ENAMETOOLONG:
45135446Strhodes	case EBADF:
46135446Strhodes		return (ISC_R_INVALIDFILE);
47135446Strhodes	case ENOENT:
48135446Strhodes		return (ISC_R_FILENOTFOUND);
49135446Strhodes	case EACCES:
50135446Strhodes	case EPERM:
51135446Strhodes		return (ISC_R_NOPERM);
52135446Strhodes	case EEXIST:
53135446Strhodes		return (ISC_R_FILEEXISTS);
54135446Strhodes	case EIO:
55135446Strhodes		return (ISC_R_IOERROR);
56135446Strhodes	case ENOMEM:
57135446Strhodes		return (ISC_R_NOMEMORY);
58234010Sdougb	case ENFILE:
59135446Strhodes	case EMFILE:
60135446Strhodes		return (ISC_R_TOOMANYOPENFILES);
61135446Strhodes	case EPIPE:
62135446Strhodes#ifdef ECONNRESET
63135446Strhodes	case ECONNRESET:
64135446Strhodes#endif
65135446Strhodes#ifdef ECONNABORTED
66135446Strhodes	case ECONNABORTED:
67135446Strhodes#endif
68135446Strhodes		return (ISC_R_CONNECTIONRESET);
69135446Strhodes#ifdef ENOTCONN
70135446Strhodes	case ENOTCONN:
71135446Strhodes		return (ISC_R_NOTCONNECTED);
72135446Strhodes#endif
73135446Strhodes#ifdef ETIMEDOUT
74135446Strhodes	case ETIMEDOUT:
75135446Strhodes		return (ISC_R_TIMEDOUT);
76135446Strhodes#endif
77135446Strhodes#ifdef ENOBUFS
78135446Strhodes	case ENOBUFS:
79135446Strhodes		return (ISC_R_NORESOURCES);
80135446Strhodes#endif
81135446Strhodes#ifdef EAFNOSUPPORT
82135446Strhodes	case EAFNOSUPPORT:
83135446Strhodes		return (ISC_R_FAMILYNOSUPPORT);
84135446Strhodes#endif
85135446Strhodes#ifdef ENETDOWN
86135446Strhodes	case ENETDOWN:
87135446Strhodes		return (ISC_R_NETDOWN);
88135446Strhodes#endif
89135446Strhodes#ifdef EHOSTDOWN
90135446Strhodes	case EHOSTDOWN:
91135446Strhodes		return (ISC_R_HOSTDOWN);
92135446Strhodes#endif
93135446Strhodes#ifdef ENETUNREACH
94135446Strhodes	case ENETUNREACH:
95135446Strhodes		return (ISC_R_NETUNREACH);
96135446Strhodes#endif
97135446Strhodes#ifdef EHOSTUNREACH
98135446Strhodes	case EHOSTUNREACH:
99135446Strhodes		return (ISC_R_HOSTUNREACH);
100135446Strhodes#endif
101135446Strhodes#ifdef EADDRINUSE
102135446Strhodes	case EADDRINUSE:
103135446Strhodes		return (ISC_R_ADDRINUSE);
104135446Strhodes#endif
105135446Strhodes	case EADDRNOTAVAIL:
106135446Strhodes		return (ISC_R_ADDRNOTAVAIL);
107135446Strhodes	case ECONNREFUSED:
108135446Strhodes		return (ISC_R_CONNREFUSED);
109135446Strhodes	default:
110135446Strhodes		isc__strerror(posixerrno, strbuf, sizeof(strbuf));
111234010Sdougb		UNEXPECTED_ERROR(file, line, "unable to convert errno "
112135446Strhodes				 "to isc_result: %d: %s",
113135446Strhodes				 posixerrno, strbuf);
114135446Strhodes		/*
115135446Strhodes		 * XXXDCL would be nice if perhaps this function could
116135446Strhodes		 * return the system's error string, so the caller
117135446Strhodes		 * might have something more descriptive than "unexpected
118135446Strhodes		 * error" to log with.
119135446Strhodes		 */
120135446Strhodes		return (ISC_R_UNEXPECTED);
121135446Strhodes	}
122135446Strhodes}
123