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