Home | History | Annotate | Download | only in ssl
      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  * ***** BEGIN LICENSE BLOCK *****
     11  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
     12  *
     13  * The contents of this file are subject to the Mozilla Public License Version
     14  * 1.1 (the "License"); you may not use this file except in compliance with
     15  * the License. You may obtain a copy of the License at
     16  * http://www.mozilla.org/MPL/
     17  *
     18  * Software distributed under the License is distributed on an "AS IS" basis,
     19  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
     20  * for the specific language governing rights and limitations under the
     21  * License.
     22  *
     23  * The Original Code is the Netscape security libraries.
     24  *
     25  * The Initial Developer of the Original Code is
     26  * Netscape Communications Corporation.
     27  * Portions created by the Initial Developer are Copyright (C) 1994-2000
     28  * the Initial Developer. All Rights Reserved.
     29  *
     30  * Contributor(s):
     31  *
     32  * Alternatively, the contents of this file may be used under the terms of
     33  * either the GNU General Public License Version 2 or later (the "GPL"), or
     34  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
     35  * in which case the provisions of the GPL or the LGPL are applicable instead
     36  * of those above. If you wish to allow use of your version of this file only
     37  * under the terms of either the GPL or the LGPL, and not to allow others to
     38  * use your version of this file under the terms of the MPL, indicate your
     39  * decision by deleting the provisions above and replace them with the notice
     40  * and other provisions required by the GPL or the LGPL. If you do not delete
     41  * the provisions above, a recipient may use your version of this file under
     42  * the terms of any one of the MPL, the GPL or the LGPL.
     43  *
     44  * ***** END LICENSE BLOCK ***** */
     45 /* $Id: win32err.c,v 1.5 2008/11/20 04:39:59 nelson%bolyard.com Exp $ */
     46 
     47 #include "prerror.h"
     48 #include "prlog.h"
     49 #include <errno.h>
     50 #include <windows.h>
     51 
     52 /*
     53  * On Win32, we map three kinds of error codes:
     54  * - GetLastError(): for Win32 functions
     55  * - WSAGetLastError(): for Winsock functions
     56  * - errno: for standard C library functions
     57  *
     58  * We do not check for WSAEINPROGRESS and WSAEINTR because we do not
     59  * use blocking Winsock 1.1 calls.
     60  *
     61  * Except for the 'socket' call, we do not check for WSAEINITIALISED.
     62  * It is assumed that if Winsock is not initialized, that fact will
     63  * be detected at the time we create new sockets.
     64  */
     65 
     66 /* forward declaration. */
     67 void nss_MD_win32_map_default_error(PRInt32 err);
     68 
     69 void nss_MD_win32_map_opendir_error(PRInt32 err)
     70 {
     71     nss_MD_win32_map_default_error(err);
     72 }
     73 
     74 void nss_MD_win32_map_closedir_error(PRInt32 err)
     75 {
     76     nss_MD_win32_map_default_error(err);
     77 }
     78 
     79 void nss_MD_win32_map_readdir_error(PRInt32 err)
     80 {
     81     nss_MD_win32_map_default_error(err);
     82 }
     83 
     84 void nss_MD_win32_map_delete_error(PRInt32 err)
     85 {
     86     nss_MD_win32_map_default_error(err);
     87 }
     88 
     89 /* The error code for stat() is in errno. */
     90 void nss_MD_win32_map_stat_error(PRInt32 err)
     91 {
     92     nss_MD_win32_map_default_error(err);
     93 }
     94 
     95 void nss_MD_win32_map_fstat_error(PRInt32 err)
     96 {
     97     nss_MD_win32_map_default_error(err);
     98 }
     99 
    100 void nss_MD_win32_map_rename_error(PRInt32 err)
    101 {
    102     nss_MD_win32_map_default_error(err);
    103 }
    104 
    105 /* The error code for access() is in errno. */
    106 void nss_MD_win32_map_access_error(PRInt32 err)
    107 {
    108     nss_MD_win32_map_default_error(err);
    109 }
    110 
    111 void nss_MD_win32_map_mkdir_error(PRInt32 err)
    112 {
    113     nss_MD_win32_map_default_error(err);
    114 }
    115 
    116 void nss_MD_win32_map_rmdir_error(PRInt32 err)
    117 {
    118     nss_MD_win32_map_default_error(err);
    119 }
    120 
    121 void nss_MD_win32_map_read_error(PRInt32 err)
    122 {
    123     nss_MD_win32_map_default_error(err);
    124 }
    125 
    126 void nss_MD_win32_map_transmitfile_error(PRInt32 err)
    127 {
    128     nss_MD_win32_map_default_error(err);
    129 }
    130 
    131 void nss_MD_win32_map_write_error(PRInt32 err)
    132 {
    133     nss_MD_win32_map_default_error(err);
    134 }
    135 
    136 void nss_MD_win32_map_lseek_error(PRInt32 err)
    137 {
    138     nss_MD_win32_map_default_error(err);
    139 }
    140 
    141 void nss_MD_win32_map_fsync_error(PRInt32 err)
    142 {
    143     nss_MD_win32_map_default_error(err);
    144 }
    145 
    146 /*
    147  * For both CloseHandle() and closesocket().
    148  */
    149 void nss_MD_win32_map_close_error(PRInt32 err)
    150 {
    151     nss_MD_win32_map_default_error(err);
    152 }
    153 
    154 void nss_MD_win32_map_socket_error(PRInt32 err)
    155 {
    156     PR_ASSERT(err != WSANOTINITIALISED);
    157     nss_MD_win32_map_default_error(err);
    158 }
    159 
    160 void nss_MD_win32_map_recv_error(PRInt32 err)
    161 {
    162     nss_MD_win32_map_default_error(err);
    163 }
    164 
    165 void nss_MD_win32_map_recvfrom_error(PRInt32 err)
    166 {
    167     nss_MD_win32_map_default_error(err);
    168 }
    169 
    170 void nss_MD_win32_map_send_error(PRInt32 err)
    171 {
    172     PRErrorCode prError;
    173     switch (err) {
    174     case WSAEMSGSIZE: 	prError = PR_INVALID_ARGUMENT_ERROR; break;
    175     default:		nss_MD_win32_map_default_error(err); return;
    176     }
    177     PR_SetError(prError, err);
    178 }
    179 
    180 void nss_MD_win32_map_sendto_error(PRInt32 err)
    181 {
    182     PRErrorCode prError;
    183     switch (err) {
    184     case WSAEMSGSIZE: 	prError = PR_INVALID_ARGUMENT_ERROR; break;
    185     default:		nss_MD_win32_map_default_error(err); return;
    186     }
    187     PR_SetError(prError, err);
    188 }
    189 
    190 void nss_MD_win32_map_accept_error(PRInt32 err)
    191 {
    192     PRErrorCode prError;
    193     switch (err) {
    194     case WSAEOPNOTSUPP: prError = PR_NOT_TCP_SOCKET_ERROR; break;
    195     case WSAEINVAL: 	prError = PR_INVALID_STATE_ERROR; break;
    196     default:		nss_MD_win32_map_default_error(err); return;
    197     }
    198     PR_SetError(prError, err);
    199 }
    200 
    201 void nss_MD_win32_map_acceptex_error(PRInt32 err)
    202 {
    203     nss_MD_win32_map_default_error(err);
    204 }
    205 
    206 void nss_MD_win32_map_connect_error(PRInt32 err)
    207 {
    208     PRErrorCode prError;
    209     switch (err) {
    210     case WSAEWOULDBLOCK: prError = PR_IN_PROGRESS_ERROR; break;
    211     case WSAEINVAL: 	prError = PR_ALREADY_INITIATED_ERROR; break;
    212     case WSAETIMEDOUT: 	prError = PR_IO_TIMEOUT_ERROR; break;
    213     default:		nss_MD_win32_map_default_error(err); return;
    214     }
    215     PR_SetError(prError, err);
    216 }
    217 
    218 void nss_MD_win32_map_bind_error(PRInt32 err)
    219 {
    220     PRErrorCode prError;
    221     switch (err) {
    222     case WSAEINVAL: 	prError = PR_SOCKET_ADDRESS_IS_BOUND_ERROR; break;
    223     default:		nss_MD_win32_map_default_error(err); return;
    224     }
    225     PR_SetError(prError, err);
    226 }
    227 
    228 void nss_MD_win32_map_listen_error(PRInt32 err)
    229 {
    230     PRErrorCode prError;
    231     switch (err) {
    232     case WSAEOPNOTSUPP: prError = PR_NOT_TCP_SOCKET_ERROR; break;
    233     case WSAEINVAL: 	prError = PR_INVALID_STATE_ERROR; break;
    234     default:		nss_MD_win32_map_default_error(err); return;
    235     }
    236     PR_SetError(prError, err);
    237 }
    238 
    239 void nss_MD_win32_map_shutdown_error(PRInt32 err)
    240 {
    241     nss_MD_win32_map_default_error(err);
    242 }
    243 
    244 void nss_MD_win32_map_getsockname_error(PRInt32 err)
    245 {
    246     PRErrorCode prError;
    247     switch (err) {
    248     case WSAEINVAL: 	prError = PR_INVALID_STATE_ERROR; break;
    249     default:		nss_MD_win32_map_default_error(err); return;
    250     }
    251     PR_SetError(prError, err);
    252 }
    253 
    254 void nss_MD_win32_map_getpeername_error(PRInt32 err)
    255 {
    256     nss_MD_win32_map_default_error(err);
    257 }
    258 
    259 void nss_MD_win32_map_getsockopt_error(PRInt32 err)
    260 {
    261     nss_MD_win32_map_default_error(err);
    262 }
    263 
    264 void nss_MD_win32_map_setsockopt_error(PRInt32 err)
    265 {
    266     nss_MD_win32_map_default_error(err);
    267 }
    268 
    269 void nss_MD_win32_map_open_error(PRInt32 err)
    270 {
    271     nss_MD_win32_map_default_error(err);
    272 }
    273 
    274 void nss_MD_win32_map_gethostname_error(PRInt32 err)
    275 {
    276     nss_MD_win32_map_default_error(err);
    277 }
    278 
    279 /* Win32 select() only works on sockets.  So in this
    280 ** context, WSAENOTSOCK is equivalent to EBADF on Unix.
    281 */
    282 void nss_MD_win32_map_select_error(PRInt32 err)
    283 {
    284     PRErrorCode prError;
    285     switch (err) {
    286     case WSAENOTSOCK:	prError = PR_BAD_DESCRIPTOR_ERROR; break;
    287     default:		nss_MD_win32_map_default_error(err); return;
    288     }
    289     PR_SetError(prError, err);
    290 }
    291 
    292 void nss_MD_win32_map_lockf_error(PRInt32 err)
    293 {
    294     nss_MD_win32_map_default_error(err);
    295 }
    296 
    297 
    298 
    299 void nss_MD_win32_map_default_error(PRInt32 err)
    300 {
    301     PRErrorCode prError;
    302 
    303     switch (err) {
    304     case EACCES: 		prError = PR_NO_ACCESS_RIGHTS_ERROR; break;
    305     case ENOENT: 		prError = PR_FILE_NOT_FOUND_ERROR; break;
    306     case ERROR_ACCESS_DENIED: 	prError = PR_NO_ACCESS_RIGHTS_ERROR; break;
    307     case ERROR_ALREADY_EXISTS: 	prError = PR_FILE_EXISTS_ERROR; break;
    308     case ERROR_DISK_CORRUPT: 	prError = PR_IO_ERROR; break;
    309     case ERROR_DISK_FULL: 	prError = PR_NO_DEVICE_SPACE_ERROR; break;
    310     case ERROR_DISK_OPERATION_FAILED: prError = PR_IO_ERROR; break;
    311     case ERROR_DRIVE_LOCKED: 	prError = PR_FILE_IS_LOCKED_ERROR; break;
    312     case ERROR_FILENAME_EXCED_RANGE: prError = PR_NAME_TOO_LONG_ERROR; break;
    313     case ERROR_FILE_CORRUPT: 	prError = PR_IO_ERROR; break;
    314     case ERROR_FILE_EXISTS: 	prError = PR_FILE_EXISTS_ERROR; break;
    315     case ERROR_FILE_INVALID: 	prError = PR_BAD_DESCRIPTOR_ERROR; break;
    316 #if ERROR_FILE_NOT_FOUND != ENOENT
    317     case ERROR_FILE_NOT_FOUND: 	prError = PR_FILE_NOT_FOUND_ERROR; break;
    318 #endif
    319     case ERROR_HANDLE_DISK_FULL: prError = PR_NO_DEVICE_SPACE_ERROR; break;
    320     case ERROR_INVALID_ADDRESS: prError = PR_ACCESS_FAULT_ERROR; break;
    321     case ERROR_INVALID_HANDLE: 	prError = PR_BAD_DESCRIPTOR_ERROR; break;
    322     case ERROR_INVALID_NAME: 	prError = PR_INVALID_ARGUMENT_ERROR; break;
    323     case ERROR_INVALID_PARAMETER: prError = PR_INVALID_ARGUMENT_ERROR; break;
    324     case ERROR_INVALID_USER_BUFFER: prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
    325     case ERROR_LOCKED:	 	prError = PR_FILE_IS_LOCKED_ERROR; break;
    326     case ERROR_NETNAME_DELETED: prError = PR_CONNECT_RESET_ERROR; break;
    327     case ERROR_NOACCESS: 	prError = PR_ACCESS_FAULT_ERROR; break;
    328     case ERROR_NOT_ENOUGH_MEMORY: prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
    329     case ERROR_NOT_ENOUGH_QUOTA: prError = PR_OUT_OF_MEMORY_ERROR; break;
    330     case ERROR_NOT_READY: 	prError = PR_IO_ERROR; break;
    331     case ERROR_NO_MORE_FILES: 	prError = PR_NO_MORE_FILES_ERROR; break;
    332     case ERROR_OPEN_FAILED: 	prError = PR_IO_ERROR; break;
    333     case ERROR_OPEN_FILES: 	prError = PR_IO_ERROR; break;
    334     case ERROR_OUTOFMEMORY: 	prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
    335     case ERROR_PATH_BUSY: 	prError = PR_IO_ERROR; break;
    336     case ERROR_PATH_NOT_FOUND: 	prError = PR_FILE_NOT_FOUND_ERROR; break;
    337     case ERROR_SEEK_ON_DEVICE: 	prError = PR_IO_ERROR; break;
    338     case ERROR_SHARING_VIOLATION: prError = PR_FILE_IS_BUSY_ERROR; break;
    339     case ERROR_STACK_OVERFLOW: 	prError = PR_ACCESS_FAULT_ERROR; break;
    340     case ERROR_TOO_MANY_OPEN_FILES: prError = PR_SYS_DESC_TABLE_FULL_ERROR; break;
    341     case ERROR_WRITE_PROTECT: 	prError = PR_NO_ACCESS_RIGHTS_ERROR; break;
    342     case WSAEACCES: 		prError = PR_NO_ACCESS_RIGHTS_ERROR; break;
    343     case WSAEADDRINUSE: 	prError = PR_ADDRESS_IN_USE_ERROR; break;
    344     case WSAEADDRNOTAVAIL: 	prError = PR_ADDRESS_NOT_AVAILABLE_ERROR; break;
    345     case WSAEAFNOSUPPORT: 	prError = PR_ADDRESS_NOT_SUPPORTED_ERROR; break;
    346     case WSAEALREADY: 		prError = PR_ALREADY_INITIATED_ERROR; break;
    347     case WSAEBADF: 		prError = PR_BAD_DESCRIPTOR_ERROR; break;
    348     case WSAECONNABORTED: 	prError = PR_CONNECT_ABORTED_ERROR; break;
    349     case WSAECONNREFUSED: 	prError = PR_CONNECT_REFUSED_ERROR; break;
    350     case WSAECONNRESET: 	prError = PR_CONNECT_RESET_ERROR; break;
    351     case WSAEDESTADDRREQ: 	prError = PR_INVALID_ARGUMENT_ERROR; break;
    352     case WSAEFAULT: 		prError = PR_ACCESS_FAULT_ERROR; break;
    353     case WSAEHOSTUNREACH: 	prError = PR_HOST_UNREACHABLE_ERROR; break;
    354     case WSAEINVAL: 		prError = PR_INVALID_ARGUMENT_ERROR; break;
    355     case WSAEISCONN: 		prError = PR_IS_CONNECTED_ERROR; break;
    356     case WSAEMFILE: 		prError = PR_PROC_DESC_TABLE_FULL_ERROR; break;
    357     case WSAEMSGSIZE: 		prError = PR_BUFFER_OVERFLOW_ERROR; break;
    358     case WSAENETDOWN: 		prError = PR_NETWORK_DOWN_ERROR; break;
    359     case WSAENETRESET: 		prError = PR_CONNECT_ABORTED_ERROR; break;
    360     case WSAENETUNREACH: 	prError = PR_NETWORK_UNREACHABLE_ERROR; break;
    361     case WSAENOBUFS: 		prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
    362     case WSAENOPROTOOPT: 	prError = PR_INVALID_ARGUMENT_ERROR; break;
    363     case WSAENOTCONN: 		prError = PR_NOT_CONNECTED_ERROR; break;
    364     case WSAENOTSOCK: 		prError = PR_NOT_SOCKET_ERROR; break;
    365     case WSAEOPNOTSUPP: 	prError = PR_OPERATION_NOT_SUPPORTED_ERROR; break;
    366     case WSAEPROTONOSUPPORT: 	prError = PR_PROTOCOL_NOT_SUPPORTED_ERROR; break;
    367     case WSAEPROTOTYPE: 	prError = PR_INVALID_ARGUMENT_ERROR; break;
    368     case WSAESHUTDOWN: 		prError = PR_SOCKET_SHUTDOWN_ERROR; break;
    369     case WSAESOCKTNOSUPPORT: 	prError = PR_INVALID_ARGUMENT_ERROR; break;
    370     case WSAETIMEDOUT: 		prError = PR_CONNECT_ABORTED_ERROR; break;
    371     case WSAEWOULDBLOCK: 	prError = PR_WOULD_BLOCK_ERROR; break;
    372     default: 			prError = PR_UNKNOWN_ERROR; break;
    373     }
    374     PR_SetError(prError, err);
    375 }
    376 
    377