• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3  * This file essentially replicates NSPR's source for the functions that
4  * map system-specific error codes to NSPR error codes.  We would use
5  * NSPR's functions, instead of duplicating them, but they're private.
6  * As long as SSL's server session cache code must do platform native I/O
7  * to accomplish its job, and NSPR's error mapping functions remain private,
8  * this code will continue to need to be replicated.
9  *
10  * This Source Code Form is subject to the terms of the Mozilla Public
11  * License, v. 2.0. If a copy of the MPL was not distributed with this
12  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
13 
14 #include "prerror.h"
15 #include "prlog.h"
16 #include <errno.h>
17 #include <windows.h>
18 
19 /*
20  * On Win32, we map three kinds of error codes:
21  * - GetLastError(): for Win32 functions
22  * - WSAGetLastError(): for Winsock functions
23  * - errno: for standard C library functions
24  *
25  * We do not check for WSAEINPROGRESS and WSAEINTR because we do not
26  * use blocking Winsock 1.1 calls.
27  *
28  * Except for the 'socket' call, we do not check for WSAEINITIALISED.
29  * It is assumed that if Winsock is not initialized, that fact will
30  * be detected at the time we create new sockets.
31  */
32 
33 /* forward declaration. */
34 void nss_MD_win32_map_default_error(PRInt32 err);
35 
nss_MD_win32_map_opendir_error(PRInt32 err)36 void nss_MD_win32_map_opendir_error(PRInt32 err)
37 {
38     nss_MD_win32_map_default_error(err);
39 }
40 
nss_MD_win32_map_closedir_error(PRInt32 err)41 void nss_MD_win32_map_closedir_error(PRInt32 err)
42 {
43     nss_MD_win32_map_default_error(err);
44 }
45 
nss_MD_win32_map_readdir_error(PRInt32 err)46 void nss_MD_win32_map_readdir_error(PRInt32 err)
47 {
48     nss_MD_win32_map_default_error(err);
49 }
50 
nss_MD_win32_map_delete_error(PRInt32 err)51 void nss_MD_win32_map_delete_error(PRInt32 err)
52 {
53     nss_MD_win32_map_default_error(err);
54 }
55 
56 /* The error code for stat() is in errno. */
nss_MD_win32_map_stat_error(PRInt32 err)57 void nss_MD_win32_map_stat_error(PRInt32 err)
58 {
59     nss_MD_win32_map_default_error(err);
60 }
61 
nss_MD_win32_map_fstat_error(PRInt32 err)62 void nss_MD_win32_map_fstat_error(PRInt32 err)
63 {
64     nss_MD_win32_map_default_error(err);
65 }
66 
nss_MD_win32_map_rename_error(PRInt32 err)67 void nss_MD_win32_map_rename_error(PRInt32 err)
68 {
69     nss_MD_win32_map_default_error(err);
70 }
71 
72 /* The error code for access() is in errno. */
nss_MD_win32_map_access_error(PRInt32 err)73 void nss_MD_win32_map_access_error(PRInt32 err)
74 {
75     nss_MD_win32_map_default_error(err);
76 }
77 
nss_MD_win32_map_mkdir_error(PRInt32 err)78 void nss_MD_win32_map_mkdir_error(PRInt32 err)
79 {
80     nss_MD_win32_map_default_error(err);
81 }
82 
nss_MD_win32_map_rmdir_error(PRInt32 err)83 void nss_MD_win32_map_rmdir_error(PRInt32 err)
84 {
85     nss_MD_win32_map_default_error(err);
86 }
87 
nss_MD_win32_map_read_error(PRInt32 err)88 void nss_MD_win32_map_read_error(PRInt32 err)
89 {
90     nss_MD_win32_map_default_error(err);
91 }
92 
nss_MD_win32_map_transmitfile_error(PRInt32 err)93 void nss_MD_win32_map_transmitfile_error(PRInt32 err)
94 {
95     nss_MD_win32_map_default_error(err);
96 }
97 
nss_MD_win32_map_write_error(PRInt32 err)98 void nss_MD_win32_map_write_error(PRInt32 err)
99 {
100     nss_MD_win32_map_default_error(err);
101 }
102 
nss_MD_win32_map_lseek_error(PRInt32 err)103 void nss_MD_win32_map_lseek_error(PRInt32 err)
104 {
105     nss_MD_win32_map_default_error(err);
106 }
107 
nss_MD_win32_map_fsync_error(PRInt32 err)108 void nss_MD_win32_map_fsync_error(PRInt32 err)
109 {
110     nss_MD_win32_map_default_error(err);
111 }
112 
113 /*
114  * For both CloseHandle() and closesocket().
115  */
nss_MD_win32_map_close_error(PRInt32 err)116 void nss_MD_win32_map_close_error(PRInt32 err)
117 {
118     nss_MD_win32_map_default_error(err);
119 }
120 
nss_MD_win32_map_socket_error(PRInt32 err)121 void nss_MD_win32_map_socket_error(PRInt32 err)
122 {
123     PR_ASSERT(err != WSANOTINITIALISED);
124     nss_MD_win32_map_default_error(err);
125 }
126 
nss_MD_win32_map_recv_error(PRInt32 err)127 void nss_MD_win32_map_recv_error(PRInt32 err)
128 {
129     nss_MD_win32_map_default_error(err);
130 }
131 
nss_MD_win32_map_recvfrom_error(PRInt32 err)132 void nss_MD_win32_map_recvfrom_error(PRInt32 err)
133 {
134     nss_MD_win32_map_default_error(err);
135 }
136 
nss_MD_win32_map_send_error(PRInt32 err)137 void nss_MD_win32_map_send_error(PRInt32 err)
138 {
139     PRErrorCode prError;
140     switch (err) {
141     case WSAEMSGSIZE: 	prError = PR_INVALID_ARGUMENT_ERROR; break;
142     default:		nss_MD_win32_map_default_error(err); return;
143     }
144     PR_SetError(prError, err);
145 }
146 
nss_MD_win32_map_sendto_error(PRInt32 err)147 void nss_MD_win32_map_sendto_error(PRInt32 err)
148 {
149     PRErrorCode prError;
150     switch (err) {
151     case WSAEMSGSIZE: 	prError = PR_INVALID_ARGUMENT_ERROR; break;
152     default:		nss_MD_win32_map_default_error(err); return;
153     }
154     PR_SetError(prError, err);
155 }
156 
nss_MD_win32_map_accept_error(PRInt32 err)157 void nss_MD_win32_map_accept_error(PRInt32 err)
158 {
159     PRErrorCode prError;
160     switch (err) {
161     case WSAEOPNOTSUPP: prError = PR_NOT_TCP_SOCKET_ERROR; break;
162     case WSAEINVAL: 	prError = PR_INVALID_STATE_ERROR; break;
163     default:		nss_MD_win32_map_default_error(err); return;
164     }
165     PR_SetError(prError, err);
166 }
167 
nss_MD_win32_map_acceptex_error(PRInt32 err)168 void nss_MD_win32_map_acceptex_error(PRInt32 err)
169 {
170     nss_MD_win32_map_default_error(err);
171 }
172 
nss_MD_win32_map_connect_error(PRInt32 err)173 void nss_MD_win32_map_connect_error(PRInt32 err)
174 {
175     PRErrorCode prError;
176     switch (err) {
177     case WSAEWOULDBLOCK: prError = PR_IN_PROGRESS_ERROR; break;
178     case WSAEINVAL: 	prError = PR_ALREADY_INITIATED_ERROR; break;
179     case WSAETIMEDOUT: 	prError = PR_IO_TIMEOUT_ERROR; break;
180     default:		nss_MD_win32_map_default_error(err); return;
181     }
182     PR_SetError(prError, err);
183 }
184 
nss_MD_win32_map_bind_error(PRInt32 err)185 void nss_MD_win32_map_bind_error(PRInt32 err)
186 {
187     PRErrorCode prError;
188     switch (err) {
189     case WSAEINVAL: 	prError = PR_SOCKET_ADDRESS_IS_BOUND_ERROR; break;
190     default:		nss_MD_win32_map_default_error(err); return;
191     }
192     PR_SetError(prError, err);
193 }
194 
nss_MD_win32_map_listen_error(PRInt32 err)195 void nss_MD_win32_map_listen_error(PRInt32 err)
196 {
197     PRErrorCode prError;
198     switch (err) {
199     case WSAEOPNOTSUPP: prError = PR_NOT_TCP_SOCKET_ERROR; break;
200     case WSAEINVAL: 	prError = PR_INVALID_STATE_ERROR; break;
201     default:		nss_MD_win32_map_default_error(err); return;
202     }
203     PR_SetError(prError, err);
204 }
205 
nss_MD_win32_map_shutdown_error(PRInt32 err)206 void nss_MD_win32_map_shutdown_error(PRInt32 err)
207 {
208     nss_MD_win32_map_default_error(err);
209 }
210 
nss_MD_win32_map_getsockname_error(PRInt32 err)211 void nss_MD_win32_map_getsockname_error(PRInt32 err)
212 {
213     PRErrorCode prError;
214     switch (err) {
215     case WSAEINVAL: 	prError = PR_INVALID_STATE_ERROR; break;
216     default:		nss_MD_win32_map_default_error(err); return;
217     }
218     PR_SetError(prError, err);
219 }
220 
nss_MD_win32_map_getpeername_error(PRInt32 err)221 void nss_MD_win32_map_getpeername_error(PRInt32 err)
222 {
223     nss_MD_win32_map_default_error(err);
224 }
225 
nss_MD_win32_map_getsockopt_error(PRInt32 err)226 void nss_MD_win32_map_getsockopt_error(PRInt32 err)
227 {
228     nss_MD_win32_map_default_error(err);
229 }
230 
nss_MD_win32_map_setsockopt_error(PRInt32 err)231 void nss_MD_win32_map_setsockopt_error(PRInt32 err)
232 {
233     nss_MD_win32_map_default_error(err);
234 }
235 
nss_MD_win32_map_open_error(PRInt32 err)236 void nss_MD_win32_map_open_error(PRInt32 err)
237 {
238     nss_MD_win32_map_default_error(err);
239 }
240 
nss_MD_win32_map_gethostname_error(PRInt32 err)241 void nss_MD_win32_map_gethostname_error(PRInt32 err)
242 {
243     nss_MD_win32_map_default_error(err);
244 }
245 
246 /* Win32 select() only works on sockets.  So in this
247 ** context, WSAENOTSOCK is equivalent to EBADF on Unix.
248 */
nss_MD_win32_map_select_error(PRInt32 err)249 void nss_MD_win32_map_select_error(PRInt32 err)
250 {
251     PRErrorCode prError;
252     switch (err) {
253     case WSAENOTSOCK:	prError = PR_BAD_DESCRIPTOR_ERROR; break;
254     default:		nss_MD_win32_map_default_error(err); return;
255     }
256     PR_SetError(prError, err);
257 }
258 
nss_MD_win32_map_lockf_error(PRInt32 err)259 void nss_MD_win32_map_lockf_error(PRInt32 err)
260 {
261     nss_MD_win32_map_default_error(err);
262 }
263 
264 
265 
nss_MD_win32_map_default_error(PRInt32 err)266 void nss_MD_win32_map_default_error(PRInt32 err)
267 {
268     PRErrorCode prError;
269 
270     switch (err) {
271     case EACCES: 		prError = PR_NO_ACCESS_RIGHTS_ERROR; break;
272     case ENOENT: 		prError = PR_FILE_NOT_FOUND_ERROR; break;
273     case ERROR_ACCESS_DENIED: 	prError = PR_NO_ACCESS_RIGHTS_ERROR; break;
274     case ERROR_ALREADY_EXISTS: 	prError = PR_FILE_EXISTS_ERROR; break;
275     case ERROR_DISK_CORRUPT: 	prError = PR_IO_ERROR; break;
276     case ERROR_DISK_FULL: 	prError = PR_NO_DEVICE_SPACE_ERROR; break;
277     case ERROR_DISK_OPERATION_FAILED: prError = PR_IO_ERROR; break;
278     case ERROR_DRIVE_LOCKED: 	prError = PR_FILE_IS_LOCKED_ERROR; break;
279     case ERROR_FILENAME_EXCED_RANGE: prError = PR_NAME_TOO_LONG_ERROR; break;
280     case ERROR_FILE_CORRUPT: 	prError = PR_IO_ERROR; break;
281     case ERROR_FILE_EXISTS: 	prError = PR_FILE_EXISTS_ERROR; break;
282     case ERROR_FILE_INVALID: 	prError = PR_BAD_DESCRIPTOR_ERROR; break;
283 #if ERROR_FILE_NOT_FOUND != ENOENT
284     case ERROR_FILE_NOT_FOUND: 	prError = PR_FILE_NOT_FOUND_ERROR; break;
285 #endif
286     case ERROR_HANDLE_DISK_FULL: prError = PR_NO_DEVICE_SPACE_ERROR; break;
287     case ERROR_INVALID_ADDRESS: prError = PR_ACCESS_FAULT_ERROR; break;
288     case ERROR_INVALID_HANDLE: 	prError = PR_BAD_DESCRIPTOR_ERROR; break;
289     case ERROR_INVALID_NAME: 	prError = PR_INVALID_ARGUMENT_ERROR; break;
290     case ERROR_INVALID_PARAMETER: prError = PR_INVALID_ARGUMENT_ERROR; break;
291     case ERROR_INVALID_USER_BUFFER: prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
292     case ERROR_LOCKED:	 	prError = PR_FILE_IS_LOCKED_ERROR; break;
293     case ERROR_NETNAME_DELETED: prError = PR_CONNECT_RESET_ERROR; break;
294     case ERROR_NOACCESS: 	prError = PR_ACCESS_FAULT_ERROR; break;
295     case ERROR_NOT_ENOUGH_MEMORY: prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
296     case ERROR_NOT_ENOUGH_QUOTA: prError = PR_OUT_OF_MEMORY_ERROR; break;
297     case ERROR_NOT_READY: 	prError = PR_IO_ERROR; break;
298     case ERROR_NO_MORE_FILES: 	prError = PR_NO_MORE_FILES_ERROR; break;
299     case ERROR_OPEN_FAILED: 	prError = PR_IO_ERROR; break;
300     case ERROR_OPEN_FILES: 	prError = PR_IO_ERROR; break;
301     case ERROR_OUTOFMEMORY: 	prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
302     case ERROR_PATH_BUSY: 	prError = PR_IO_ERROR; break;
303     case ERROR_PATH_NOT_FOUND: 	prError = PR_FILE_NOT_FOUND_ERROR; break;
304     case ERROR_SEEK_ON_DEVICE: 	prError = PR_IO_ERROR; break;
305     case ERROR_SHARING_VIOLATION: prError = PR_FILE_IS_BUSY_ERROR; break;
306     case ERROR_STACK_OVERFLOW: 	prError = PR_ACCESS_FAULT_ERROR; break;
307     case ERROR_TOO_MANY_OPEN_FILES: prError = PR_SYS_DESC_TABLE_FULL_ERROR; break;
308     case ERROR_WRITE_PROTECT: 	prError = PR_NO_ACCESS_RIGHTS_ERROR; break;
309     case WSAEACCES: 		prError = PR_NO_ACCESS_RIGHTS_ERROR; break;
310     case WSAEADDRINUSE: 	prError = PR_ADDRESS_IN_USE_ERROR; break;
311     case WSAEADDRNOTAVAIL: 	prError = PR_ADDRESS_NOT_AVAILABLE_ERROR; break;
312     case WSAEAFNOSUPPORT: 	prError = PR_ADDRESS_NOT_SUPPORTED_ERROR; break;
313     case WSAEALREADY: 		prError = PR_ALREADY_INITIATED_ERROR; break;
314     case WSAEBADF: 		prError = PR_BAD_DESCRIPTOR_ERROR; break;
315     case WSAECONNABORTED: 	prError = PR_CONNECT_ABORTED_ERROR; break;
316     case WSAECONNREFUSED: 	prError = PR_CONNECT_REFUSED_ERROR; break;
317     case WSAECONNRESET: 	prError = PR_CONNECT_RESET_ERROR; break;
318     case WSAEDESTADDRREQ: 	prError = PR_INVALID_ARGUMENT_ERROR; break;
319     case WSAEFAULT: 		prError = PR_ACCESS_FAULT_ERROR; break;
320     case WSAEHOSTUNREACH: 	prError = PR_HOST_UNREACHABLE_ERROR; break;
321     case WSAEINVAL: 		prError = PR_INVALID_ARGUMENT_ERROR; break;
322     case WSAEISCONN: 		prError = PR_IS_CONNECTED_ERROR; break;
323     case WSAEMFILE: 		prError = PR_PROC_DESC_TABLE_FULL_ERROR; break;
324     case WSAEMSGSIZE: 		prError = PR_BUFFER_OVERFLOW_ERROR; break;
325     case WSAENETDOWN: 		prError = PR_NETWORK_DOWN_ERROR; break;
326     case WSAENETRESET: 		prError = PR_CONNECT_ABORTED_ERROR; break;
327     case WSAENETUNREACH: 	prError = PR_NETWORK_UNREACHABLE_ERROR; break;
328     case WSAENOBUFS: 		prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
329     case WSAENOPROTOOPT: 	prError = PR_INVALID_ARGUMENT_ERROR; break;
330     case WSAENOTCONN: 		prError = PR_NOT_CONNECTED_ERROR; break;
331     case WSAENOTSOCK: 		prError = PR_NOT_SOCKET_ERROR; break;
332     case WSAEOPNOTSUPP: 	prError = PR_OPERATION_NOT_SUPPORTED_ERROR; break;
333     case WSAEPROTONOSUPPORT: 	prError = PR_PROTOCOL_NOT_SUPPORTED_ERROR; break;
334     case WSAEPROTOTYPE: 	prError = PR_INVALID_ARGUMENT_ERROR; break;
335     case WSAESHUTDOWN: 		prError = PR_SOCKET_SHUTDOWN_ERROR; break;
336     case WSAESOCKTNOSUPPORT: 	prError = PR_INVALID_ARGUMENT_ERROR; break;
337     case WSAETIMEDOUT: 		prError = PR_CONNECT_ABORTED_ERROR; break;
338     case WSAEWOULDBLOCK: 	prError = PR_WOULD_BLOCK_ERROR; break;
339     default: 			prError = PR_UNKNOWN_ERROR; break;
340     }
341     PR_SetError(prError, err);
342 }
343 
344