1 /*
2  * Copyright (C) 2004, 2005, 2007, 2008  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2000-2002  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 /* $Id: errno2result.c,v 1.17 2008/09/12 04:46:25 marka Exp $ */
19 
20 #include <config.h>
21 
22 #include <winsock2.h>
23 #include "errno2result.h"
24 #include <isc/result.h>
25 #include <isc/strerror.h>
26 #include <isc/util.h>
27 
28 /*
29  * Convert a POSIX errno value into an isc_result_t.  The
30  * list of supported errno values is not complete; new users
31  * of this function should add any expected errors that are
32  * not already there.
33  */
34 isc_result_t
isc__errno2resultx(int posixerrno,const char * file,int line)35 isc__errno2resultx(int posixerrno, const char *file, int line) {
36 	char strbuf[ISC_STRERRORSIZE];
37 
38 	switch (posixerrno) {
39 	case ENOTDIR:
40 	case WSAELOOP:
41 	case WSAEINVAL:
42 	case EINVAL:		/* XXX sometimes this is not for files */
43 	case ENAMETOOLONG:
44 	case WSAENAMETOOLONG:
45 	case EBADF:
46 	case WSAEBADF:
47 		return (ISC_R_INVALIDFILE);
48 	case ENOENT:
49 		return (ISC_R_FILENOTFOUND);
50 	case EACCES:
51 	case WSAEACCES:
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 	case WSAEMFILE:
63 		return (ISC_R_TOOMANYOPENFILES);
64 	case ERROR_CANCELLED:
65 		return (ISC_R_CANCELED);
66 	case ERROR_CONNECTION_REFUSED:
67 	case WSAECONNREFUSED:
68 		return (ISC_R_CONNREFUSED);
69 	case WSAENOTCONN:
70 	case ERROR_CONNECTION_INVALID:
71 		return (ISC_R_NOTCONNECTED);
72 	case ERROR_HOST_UNREACHABLE:
73 	case WSAEHOSTUNREACH:
74 		return (ISC_R_HOSTUNREACH);
75 	case ERROR_NETWORK_UNREACHABLE:
76 	case WSAENETUNREACH:
77 		return (ISC_R_NETUNREACH);
78 	case ERROR_NO_NETWORK:
79 		return (ISC_R_NETUNREACH);
80 	case ERROR_PORT_UNREACHABLE:
81 		return (ISC_R_HOSTUNREACH);
82 	case ERROR_SEM_TIMEOUT:
83 		return (ISC_R_TIMEDOUT);
84 	case WSAECONNRESET:
85 	case WSAENETRESET:
86 	case WSAECONNABORTED:
87 	case WSAEDISCON:
88 	case ERROR_OPERATION_ABORTED:
89 	case ERROR_CONNECTION_ABORTED:
90 	case ERROR_REQUEST_ABORTED:
91 		return (ISC_R_CONNECTIONRESET);
92 	case WSAEADDRNOTAVAIL:
93 		return (ISC_R_ADDRNOTAVAIL);
94 	case ERROR_NETNAME_DELETED:
95 	case WSAENETDOWN:
96 		return (ISC_R_NETUNREACH);
97 	case WSAEHOSTDOWN:
98 		return (ISC_R_HOSTUNREACH);
99 	case WSAENOBUFS:
100 		return (ISC_R_NORESOURCES);
101 	default:
102 		isc__strerror(posixerrno, strbuf, sizeof(strbuf));
103 		UNEXPECTED_ERROR(file, line, "unable to convert errno "
104 				 "to isc_result: %d: %s", posixerrno, strbuf);
105 		/*
106 		 * XXXDCL would be nice if perhaps this function could
107 		 * return the system's error string, so the caller
108 		 * might have something more descriptive than "unexpected
109 		 * error" to log with.
110 		 */
111 		return (ISC_R_UNEXPECTED);
112 	}
113 }
114