1290001Sglebius/*
2290001Sglebius * Copyright (C) 2004, 2005, 2007, 2008  Internet Systems Consortium, Inc. ("ISC")
3290001Sglebius * Copyright (C) 2000-2002  Internet Software Consortium.
4290001Sglebius *
5290001Sglebius * Permission to use, copy, modify, and/or distribute this software for any
6290001Sglebius * purpose with or without fee is hereby granted, provided that the above
7290001Sglebius * copyright notice and this permission notice appear in all copies.
8290001Sglebius *
9290001Sglebius * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10290001Sglebius * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11290001Sglebius * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12290001Sglebius * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13290001Sglebius * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14290001Sglebius * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15290001Sglebius * PERFORMANCE OF THIS SOFTWARE.
16290001Sglebius */
17290001Sglebius
18290001Sglebius/* $Id: errno2result.c,v 1.17 2008/09/12 04:46:25 marka Exp $ */
19290001Sglebius
20290001Sglebius#include <config.h>
21290001Sglebius
22290001Sglebius#include <winsock2.h>
23290001Sglebius#include "errno2result.h"
24290001Sglebius#include <isc/result.h>
25290001Sglebius#include <isc/strerror.h>
26290001Sglebius#include <isc/util.h>
27290001Sglebius
28290001Sglebius/*
29290001Sglebius * Convert a POSIX errno value into an isc_result_t.  The
30290001Sglebius * list of supported errno values is not complete; new users
31290001Sglebius * of this function should add any expected errors that are
32290001Sglebius * not already there.
33290001Sglebius */
34290001Sglebiusisc_result_t
35290001Sglebiusisc__errno2resultx(int posixerrno, const char *file, int line) {
36290001Sglebius	char strbuf[ISC_STRERRORSIZE];
37290001Sglebius
38290001Sglebius	switch (posixerrno) {
39290001Sglebius	case ENOTDIR:
40290001Sglebius	case WSAELOOP:
41290001Sglebius	case WSAEINVAL:
42290001Sglebius	case EINVAL:		/* XXX sometimes this is not for files */
43290001Sglebius	case ENAMETOOLONG:
44290001Sglebius	case WSAENAMETOOLONG:
45290001Sglebius	case EBADF:
46290001Sglebius	case WSAEBADF:
47290001Sglebius		return (ISC_R_INVALIDFILE);
48290001Sglebius	case ENOENT:
49290001Sglebius		return (ISC_R_FILENOTFOUND);
50290001Sglebius	case EACCES:
51290001Sglebius	case WSAEACCES:
52290001Sglebius	case EPERM:
53290001Sglebius		return (ISC_R_NOPERM);
54290001Sglebius	case EEXIST:
55290001Sglebius		return (ISC_R_FILEEXISTS);
56290001Sglebius	case EIO:
57290001Sglebius		return (ISC_R_IOERROR);
58290001Sglebius	case ENOMEM:
59290001Sglebius		return (ISC_R_NOMEMORY);
60290001Sglebius	case ENFILE:
61290001Sglebius	case EMFILE:
62290001Sglebius	case WSAEMFILE:
63290001Sglebius		return (ISC_R_TOOMANYOPENFILES);
64290001Sglebius	case ERROR_CANCELLED:
65290001Sglebius		return (ISC_R_CANCELED);
66290001Sglebius	case ERROR_CONNECTION_REFUSED:
67290001Sglebius	case WSAECONNREFUSED:
68290001Sglebius		return (ISC_R_CONNREFUSED);
69290001Sglebius	case WSAENOTCONN:
70290001Sglebius	case ERROR_CONNECTION_INVALID:
71290001Sglebius		return (ISC_R_NOTCONNECTED);
72290001Sglebius	case ERROR_HOST_UNREACHABLE:
73290001Sglebius	case WSAEHOSTUNREACH:
74290001Sglebius		return (ISC_R_HOSTUNREACH);
75290001Sglebius	case ERROR_NETWORK_UNREACHABLE:
76290001Sglebius	case WSAENETUNREACH:
77290001Sglebius		return (ISC_R_NETUNREACH);
78290001Sglebius	case ERROR_NO_NETWORK:
79290001Sglebius		return (ISC_R_NETUNREACH);
80290001Sglebius	case ERROR_PORT_UNREACHABLE:
81290001Sglebius		return (ISC_R_HOSTUNREACH);
82290001Sglebius	case ERROR_SEM_TIMEOUT:
83290001Sglebius		return (ISC_R_TIMEDOUT);
84290001Sglebius	case WSAECONNRESET:
85290001Sglebius	case WSAENETRESET:
86290001Sglebius	case WSAECONNABORTED:
87290001Sglebius	case WSAEDISCON:
88290001Sglebius	case ERROR_OPERATION_ABORTED:
89290001Sglebius	case ERROR_CONNECTION_ABORTED:
90290001Sglebius	case ERROR_REQUEST_ABORTED:
91290001Sglebius		return (ISC_R_CONNECTIONRESET);
92290001Sglebius	case WSAEADDRNOTAVAIL:
93290001Sglebius		return (ISC_R_ADDRNOTAVAIL);
94290001Sglebius	case ERROR_NETNAME_DELETED:
95290001Sglebius	case WSAENETDOWN:
96290001Sglebius		return (ISC_R_NETUNREACH);
97290001Sglebius	case WSAEHOSTDOWN:
98290001Sglebius		return (ISC_R_HOSTUNREACH);
99290001Sglebius	case WSAENOBUFS:
100290001Sglebius		return (ISC_R_NORESOURCES);
101290001Sglebius	default:
102290001Sglebius		isc__strerror(posixerrno, strbuf, sizeof(strbuf));
103290001Sglebius		UNEXPECTED_ERROR(file, line, "unable to convert errno "
104290001Sglebius				 "to isc_result: %d: %s", posixerrno, strbuf);
105290001Sglebius		/*
106290001Sglebius		 * XXXDCL would be nice if perhaps this function could
107290001Sglebius		 * return the system's error string, so the caller
108290001Sglebius		 * might have something more descriptive than "unexpected
109290001Sglebius		 * error" to log with.
110290001Sglebius		 */
111290001Sglebius		return (ISC_R_UNEXPECTED);
112290001Sglebius	}
113290001Sglebius}
114