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