1 /*
2  * The contents of this file are subject to the Netscape Public
3  * License Version 1.1 (the "License"); you may not use this file
4  * except in compliance with the License. You may obtain a copy of
5  * the License at http://www.mozilla.org/NPL/
6  *
7  * Software distributed under the License is distributed on an "AS
8  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9  * implied. See the License for the specific language governing
10  * rights and limitations under the License.
11  *
12  * The Original Code is Mozilla Communicator client code, released
13  * March 31, 1998.
14  *
15  * The Initial Developer of the Original Code is Netscape
16  * Communications Corporation. Portions created by Netscape are
17  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
18  * Rights Reserved.
19  *
20  * Contributor(s):
21  */
22 
23 /*
24  * Utilities for manageing the relationship between NSPR errors and
25  * OS (errno-style) errors.
26  *
27  * The overall strategy used is to map NSPR errors into OS errors.
28  */
29 
30 #include "ldappr-int.h"
31 
32 
33 void
prldap_set_system_errno(int oserrno)34 prldap_set_system_errno( int oserrno )
35 {
36     PR_SetError( PR_UNKNOWN_ERROR, oserrno );
37 }
38 
39 
40 int
prldap_get_system_errno(void)41 prldap_get_system_errno( void )
42 {
43     return( PR_GetOSError());
44 }
45 
46 /*
47  * Retrieve the NSPR error number, convert to a system error code, and return
48  * the result.
49  */
50 struct prldap_errormap_entry {
51     PRInt32	erm_nspr;	/* NSPR error code */
52     int		erm_system;	/* corresponding system error code */
53 };
54 
55 /* XXX: not sure if this extra mapping for Windows is good or correct */
56 #ifdef _WINDOWS
57 #ifndef ENOTSUP
58 #define ENOTSUP		-1
59 #endif
60 #ifndef ETIMEDOUT
61 #define ETIMEDOUT	WSAETIMEDOUT
62 #endif
63 #ifndef EADDRNOTAVAIL
64 #define EADDRNOTAVAIL	WSAEADDRNOTAVAIL
65 #endif
66 #ifndef EAFNOSUPPORT
67 #define EAFNOSUPPORT	WSAEAFNOSUPPORT
68 #endif
69 #ifndef EISCONN
70 #define EISCONN		WSAEISCONN
71 #endif
72 #ifndef EADDRINUSE
73 #define EADDRINUSE	WSAEADDRINUSE
74 #endif
75 #ifndef ECONNREFUSED
76 #define ECONNREFUSED	WSAECONNREFUSED
77 #endif
78 #ifndef EHOSTUNREACH
79 #define EHOSTUNREACH	WSAEHOSTUNREACH
80 #endif
81 #ifndef ENOTCONN
82 #define ENOTCONN	WSAENOTCONN
83 #endif
84 #ifndef ENOTSOCK
85 #define ENOTSOCK	WSAENOTSOCK
86 #endif
87 #ifndef EPROTOTYPE
88 #define EPROTOTYPE	WSAEPROTOTYPE
89 #endif
90 #ifndef EOPNOTSUPP
91 #define EOPNOTSUPP	WSAEOPNOTSUPP
92 #endif
93 #ifndef EPROTONOSUPPORT
94 #define EPROTONOSUPPORT	WSAEPROTONOSUPPORT
95 #endif
96 #ifndef EOVERFLOW
97 #define EOVERFLOW	-1
98 #endif
99 #ifndef ECONNRESET
100 #define ECONNRESET	WSAECONNRESET
101 #endif
102 #ifndef ELOOP
103 #define ELOOP		WSAELOOP
104 #endif
105 #ifndef ENOTBLK
106 #define ENOTBLK		-1
107 #endif
108 #ifndef ETXTBSY
109 #define ETXTBSY		-1
110 #endif
111 #ifndef ENETDOWN
112 #define ENETDOWN	WSAENETDOWN
113 #endif
114 #ifndef ESHUTDOWN
115 #define ESHUTDOWN	WSAESHUTDOWN
116 #endif
117 #ifndef ECONNABORTED
118 #define ECONNABORTED	WSAECONNABORTED
119 #endif
120 #endif /* _WINDOWS */
121 
122 /* XXX: need to verify that the -1 entries are correct (no mapping) */
123 static struct prldap_errormap_entry prldap_errormap[] = {
124     {  PR_OUT_OF_MEMORY_ERROR, ENOMEM },
125     {  PR_BAD_DESCRIPTOR_ERROR, EBADF },
126     {  PR_WOULD_BLOCK_ERROR, EAGAIN },
127     {  PR_ACCESS_FAULT_ERROR, EFAULT },
128     {  PR_INVALID_METHOD_ERROR, EINVAL },	/* XXX: correct mapping ? */
129     {  PR_ILLEGAL_ACCESS_ERROR, EACCES },	/* XXX: correct mapping ? */
130     {  PR_UNKNOWN_ERROR, -1 },
131     {  PR_PENDING_INTERRUPT_ERROR, -1 },
132     {  PR_NOT_IMPLEMENTED_ERROR, ENOTSUP },
133     {  PR_IO_ERROR, EIO },
134     {  PR_IO_TIMEOUT_ERROR, ETIMEDOUT },	/* XXX: correct mapping ? */
135     {  PR_IO_PENDING_ERROR, -1 },
136     {  PR_DIRECTORY_OPEN_ERROR, ENOTDIR },
137     {  PR_INVALID_ARGUMENT_ERROR, EINVAL },
138     {  PR_ADDRESS_NOT_AVAILABLE_ERROR, EADDRNOTAVAIL },
139     {  PR_ADDRESS_NOT_SUPPORTED_ERROR, EAFNOSUPPORT },
140     {  PR_IS_CONNECTED_ERROR, EISCONN },
141     {  PR_BAD_ADDRESS_ERROR, EFAULT },		/* XXX: correct mapping ? */
142     {  PR_ADDRESS_IN_USE_ERROR, EADDRINUSE },
143     {  PR_CONNECT_REFUSED_ERROR, ECONNREFUSED },
144     {  PR_NETWORK_UNREACHABLE_ERROR, EHOSTUNREACH },
145     {  PR_CONNECT_TIMEOUT_ERROR, ETIMEDOUT },
146     {  PR_NOT_CONNECTED_ERROR, ENOTCONN },
147     {  PR_LOAD_LIBRARY_ERROR, -1 },
148     {  PR_UNLOAD_LIBRARY_ERROR, -1 },
149     {  PR_FIND_SYMBOL_ERROR, -1 },
150     {  PR_INSUFFICIENT_RESOURCES_ERROR, -1 },
151     {  PR_DIRECTORY_LOOKUP_ERROR, -1 },
152     {  PR_TPD_RANGE_ERROR, -1 },
153     {  PR_PROC_DESC_TABLE_FULL_ERROR, -1 },
154     {  PR_SYS_DESC_TABLE_FULL_ERROR, -1 },
155     {  PR_NOT_SOCKET_ERROR, ENOTSOCK },
156     {  PR_NOT_TCP_SOCKET_ERROR, EPROTOTYPE },
157     {  PR_SOCKET_ADDRESS_IS_BOUND_ERROR, -1 },
158     {  PR_NO_ACCESS_RIGHTS_ERROR, EACCES },	/* XXX: correct mapping ? */
159     {  PR_OPERATION_NOT_SUPPORTED_ERROR, EOPNOTSUPP },
160     {  PR_PROTOCOL_NOT_SUPPORTED_ERROR, EPROTONOSUPPORT },
161     {  PR_REMOTE_FILE_ERROR, -1 },
162 #if defined(OSF1)
163     {  PR_BUFFER_OVERFLOW_ERROR, -1 },
164 #else
165     {  PR_BUFFER_OVERFLOW_ERROR, EOVERFLOW },
166 #endif /* OSF1 */
167     {  PR_CONNECT_RESET_ERROR, ECONNRESET },
168     {  PR_RANGE_ERROR, ERANGE },
169     {  PR_DEADLOCK_ERROR, EDEADLK },
170 #if defined(HPUX11) || defined(AIX4_3) || defined(OSF1)
171     {  PR_FILE_IS_LOCKED_ERROR, -1 },	/* XXX: correct mapping ? */
172 #else
173     {  PR_FILE_IS_LOCKED_ERROR, EDEADLOCK },	/* XXX: correct mapping ? */
174 #endif /* HPUX11 */
175     {  PR_FILE_TOO_BIG_ERROR, EFBIG },
176     {  PR_NO_DEVICE_SPACE_ERROR, ENOSPC },
177     {  PR_PIPE_ERROR, EPIPE },
178     {  PR_NO_SEEK_DEVICE_ERROR, ESPIPE },
179     {  PR_IS_DIRECTORY_ERROR, EISDIR },
180     {  PR_LOOP_ERROR, ELOOP },
181     {  PR_NAME_TOO_LONG_ERROR, ENAMETOOLONG },
182     {  PR_FILE_NOT_FOUND_ERROR, ENOENT },
183     {  PR_NOT_DIRECTORY_ERROR, ENOTDIR },
184     {  PR_READ_ONLY_FILESYSTEM_ERROR, EROFS },
185     {  PR_DIRECTORY_NOT_EMPTY_ERROR, ENOTEMPTY },
186     {  PR_FILESYSTEM_MOUNTED_ERROR, EBUSY },
187     {  PR_NOT_SAME_DEVICE_ERROR, EXDEV },
188     {  PR_DIRECTORY_CORRUPTED_ERROR, -1 },
189     {  PR_FILE_EXISTS_ERROR, EEXIST },
190     {  PR_MAX_DIRECTORY_ENTRIES_ERROR, -1 },
191     {  PR_INVALID_DEVICE_STATE_ERROR, ENOTBLK }, /* XXX: correct mapping ? */
192     {  PR_DEVICE_IS_LOCKED_ERROR, -2 },
193     {  PR_NO_MORE_FILES_ERROR, ENFILE },
194     {  PR_END_OF_FILE_ERROR, -1 },
195     {  PR_FILE_SEEK_ERROR, ESPIPE },		/* XXX: correct mapping ? */
196     {  PR_FILE_IS_BUSY_ERROR, ETXTBSY },
197     {  PR_OPERATION_ABORTED_ERROR, -1 },
198     {  PR_IN_PROGRESS_ERROR, -1 },
199     {  PR_ALREADY_INITIATED_ERROR, -1 },
200     {  PR_GROUP_EMPTY_ERROR, -1 },
201     {  PR_INVALID_STATE_ERROR, -1 },
202     {  PR_NETWORK_DOWN_ERROR, ENETDOWN },
203     {  PR_SOCKET_SHUTDOWN_ERROR, ESHUTDOWN },
204     {  PR_CONNECT_ABORTED_ERROR, ECONNABORTED },
205     {  PR_HOST_UNREACHABLE_ERROR, EHOSTUNREACH },
206     {  PR_MAX_ERROR, -1 },
207 };
208 
209 
210 int
prldap_prerr2errno(void)211 prldap_prerr2errno( void )
212 {
213     int		oserr, i;
214     PRInt32	nsprerr;
215 
216     nsprerr = PR_GetError();
217 
218     oserr = -1;		/* unknown */
219     for ( i = 0; prldap_errormap[i].erm_nspr != PR_MAX_ERROR; ++i ) {
220 	if ( prldap_errormap[i].erm_nspr == nsprerr ) {
221 	    oserr = prldap_errormap[i].erm_system;
222 	    break;
223 	}
224     }
225 
226     return( oserr );
227 }
228