1258945Sroberto/*
2258945Sroberto * Copyright (C) 2004, 2005, 2007, 2008  Internet Systems Consortium, Inc. ("ISC")
3258945Sroberto * Copyright (C) 2000-2002  Internet Software Consortium.
4258945Sroberto *
5258945Sroberto * Permission to use, copy, modify, and/or distribute this software for any
6258945Sroberto * purpose with or without fee is hereby granted, provided that the above
7258945Sroberto * copyright notice and this permission notice appear in all copies.
8258945Sroberto *
9258945Sroberto * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10258945Sroberto * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11258945Sroberto * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12258945Sroberto * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13258945Sroberto * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14258945Sroberto * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15258945Sroberto * PERFORMANCE OF THIS SOFTWARE.
16258945Sroberto */
17258945Sroberto
18258945Sroberto/* $Id: errno2result.c,v 1.17 2008/09/12 04:46:25 marka Exp $ */
19258945Sroberto
20258945Sroberto#include <config.h>
21258945Sroberto
22258945Sroberto#include <winsock2.h>
23258945Sroberto#include "errno2result.h"
24258945Sroberto#include <isc/result.h>
25258945Sroberto#include <isc/strerror.h>
26258945Sroberto#include <isc/util.h>
27258945Sroberto
28258945Sroberto/*
29258945Sroberto * Convert a POSIX errno value into an isc_result_t.  The
30258945Sroberto * list of supported errno values is not complete; new users
31258945Sroberto * of this function should add any expected errors that are
32258945Sroberto * not already there.
33258945Sroberto */
34258945Srobertoisc_result_t
35258945Srobertoisc__errno2resultx(int posixerrno, const char *file, int line) {
36258945Sroberto	char strbuf[ISC_STRERRORSIZE];
37258945Sroberto
38258945Sroberto	switch (posixerrno) {
39258945Sroberto	case ENOTDIR:
40258945Sroberto	case WSAELOOP:
41258945Sroberto	case WSAEINVAL:
42258945Sroberto	case EINVAL:		/* XXX sometimes this is not for files */
43258945Sroberto	case ENAMETOOLONG:
44258945Sroberto	case WSAENAMETOOLONG:
45258945Sroberto	case EBADF:
46258945Sroberto	case WSAEBADF:
47258945Sroberto		return (ISC_R_INVALIDFILE);
48258945Sroberto	case ENOENT:
49258945Sroberto		return (ISC_R_FILENOTFOUND);
50258945Sroberto	case EACCES:
51258945Sroberto	case WSAEACCES:
52258945Sroberto	case EPERM:
53258945Sroberto		return (ISC_R_NOPERM);
54258945Sroberto	case EEXIST:
55258945Sroberto		return (ISC_R_FILEEXISTS);
56258945Sroberto	case EIO:
57258945Sroberto		return (ISC_R_IOERROR);
58258945Sroberto	case ENOMEM:
59258945Sroberto		return (ISC_R_NOMEMORY);
60258945Sroberto	case ENFILE:
61258945Sroberto	case EMFILE:
62258945Sroberto	case WSAEMFILE:
63258945Sroberto		return (ISC_R_TOOMANYOPENFILES);
64258945Sroberto	case ERROR_CANCELLED:
65258945Sroberto		return (ISC_R_CANCELED);
66258945Sroberto	case ERROR_CONNECTION_REFUSED:
67258945Sroberto	case WSAECONNREFUSED:
68258945Sroberto		return (ISC_R_CONNREFUSED);
69258945Sroberto	case WSAENOTCONN:
70258945Sroberto	case ERROR_CONNECTION_INVALID:
71258945Sroberto		return (ISC_R_NOTCONNECTED);
72258945Sroberto	case ERROR_HOST_UNREACHABLE:
73258945Sroberto	case WSAEHOSTUNREACH:
74258945Sroberto		return (ISC_R_HOSTUNREACH);
75258945Sroberto	case ERROR_NETWORK_UNREACHABLE:
76258945Sroberto	case WSAENETUNREACH:
77258945Sroberto		return (ISC_R_NETUNREACH);
78258945Sroberto	case ERROR_NO_NETWORK:
79258945Sroberto		return (ISC_R_NETUNREACH);
80258945Sroberto	case ERROR_PORT_UNREACHABLE:
81258945Sroberto		return (ISC_R_HOSTUNREACH);
82258945Sroberto	case ERROR_SEM_TIMEOUT:
83258945Sroberto		return (ISC_R_TIMEDOUT);
84258945Sroberto	case WSAECONNRESET:
85258945Sroberto	case WSAENETRESET:
86258945Sroberto	case WSAECONNABORTED:
87258945Sroberto	case WSAEDISCON:
88258945Sroberto	case ERROR_OPERATION_ABORTED:
89258945Sroberto	case ERROR_CONNECTION_ABORTED:
90258945Sroberto	case ERROR_REQUEST_ABORTED:
91258945Sroberto		return (ISC_R_CONNECTIONRESET);
92258945Sroberto	case WSAEADDRNOTAVAIL:
93258945Sroberto		return (ISC_R_ADDRNOTAVAIL);
94258945Sroberto	case ERROR_NETNAME_DELETED:
95258945Sroberto	case WSAENETDOWN:
96258945Sroberto		return (ISC_R_NETUNREACH);
97258945Sroberto	case WSAEHOSTDOWN:
98258945Sroberto		return (ISC_R_HOSTUNREACH);
99258945Sroberto	case WSAENOBUFS:
100258945Sroberto		return (ISC_R_NORESOURCES);
101258945Sroberto	default:
102258945Sroberto		isc__strerror(posixerrno, strbuf, sizeof(strbuf));
103258945Sroberto		UNEXPECTED_ERROR(file, line, "unable to convert errno "
104258945Sroberto				 "to isc_result: %d: %s", posixerrno, strbuf);
105258945Sroberto		/*
106258945Sroberto		 * XXXDCL would be nice if perhaps this function could
107258945Sroberto		 * return the system's error string, so the caller
108258945Sroberto		 * might have something more descriptive than "unexpected
109258945Sroberto		 * error" to log with.
110258945Sroberto		 */
111258945Sroberto		return (ISC_R_UNEXPECTED);
112258945Sroberto	}
113258945Sroberto}
114