2 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
6 #pragma ident "%Z%%M% %I% %E% SMI"
10 * The contents of this file are subject to the Netscape Public
11 * License Version 1.1 (the "License"); you may not use this file
12 * except in compliance with the License. You may obtain a copy of
13 * the License at http://www.mozilla.org/NPL/
15 * Software distributed under the License is distributed on an "AS
16 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
17 * implied. See the License for the specific language governing
18 * rights and limitations under the License.
20 * The Original Code is Mozilla Communicator client code, released
23 * The Initial Developer of the Original Code is Netscape
24 * Communications Corporation. Portions created by Netscape are
25 * Copyright (C) 1998-1999 Netscape Communications Corporation. All
32 * Public interface for libprldap -- use NSPR (Netscape Portable Runtime)
33 * I/O, threads, etc. with libldap.
37 #include "ldappr-int.h"
41 * Function: prldap_init().
43 * Create a new LDAP session handle, but with NSPR I/O, threading, and DNS
44 * functions installed.
46 * Pass a non-zero value for the 'shared' parameter if you plan to use
47 * this LDAP * handle from more than one thread.
49 * prldap_init() returns an LDAP session handle (or NULL if an error occurs).
52 prldap_init( const char *defhost
, int defport
, int shared
)
56 if (( ld
= ldap_init( defhost
, defport
)) != NULL
) {
57 if ( prldap_install_routines( ld
, shared
) != LDAP_SUCCESS
) {
58 prldap_set_system_errno( EINVAL
); /* XXXmcs: just a guess! */
69 * Function: prldap_install_routines().
71 * Install NSPR I/O, threading, and DNS functions so they will be used by
74 * If 'ld' is NULL, the functions are installed as the default functions
75 * for all new LDAP * handles).
77 * Pass a non-zero value for the 'shared' parameter if you plan to use
78 * this LDAP * handle from more than one thread.
80 * prldap_install_routines() returns an LDAP API error code (LDAP_SUCCESS
84 prldap_install_routines( LDAP
*ld
, int shared
)
87 if ( prldap_install_io_functions( ld
, shared
) != 0
88 || prldap_install_thread_functions( ld
, shared
) != 0
89 || prldap_install_dns_functions( ld
) != 0 ) {
90 return( ldap_get_lderrno( ld
, NULL
, NULL
));
93 return( LDAP_SUCCESS
);
98 * Function: prldap_set_session_option().
100 * Given an LDAP session handle or a session argument such is passed to
101 * SOCKET, POLL, NEWHANDLE, or DISPOSEHANDLE extended I/O callbacks, set
102 * an option that affects the prldap layer.
104 * If 'ld' and 'session" are both NULL, the option is set as the default
105 * for all new prldap sessions.
107 * Returns an LDAP API error code (LDAP_SUCCESS if all goes well).
110 prldap_set_session_option( LDAP
*ld
, void *sessionarg
, int option
, ... )
112 int rc
= LDAP_SUCCESS
; /* optimistic */
113 PRLDAPIOSessionArg
*prsessp
= NULL
;
118 ( rc
= prldap_session_arg_from_ld( ld
, &prsessp
))) {
121 } else if ( NULL
!= sessionarg
) {
122 prsessp
= (PRLDAPIOSessionArg
*)sessionarg
;
125 va_start( ap
, option
);
127 case PRLDAP_OPT_IO_MAX_TIMEOUT
:
128 rc
= prldap_set_io_max_timeout( prsessp
, va_arg( ap
, int ));
131 rc
= LDAP_PARAM_ERROR
;
140 * Function: prldap_get_session_option().
142 * Given an LDAP session handle or a session argument such is passed to
143 * SOCKET, POLL, NEWHANDLE, or DISPOSEHANDLE extended I/O callbacks, retrieve
144 * the setting for an option that affects the prldap layer.
146 * If 'ld' and 'session" are both NULL, the default option value for all new
147 * new prldap sessions is retrieved.
149 * Returns an LDAP API error code (LDAP_SUCCESS if all goes well).
151 int LDAP_CALL
prldap_get_session_option( LDAP
*ld
, void *sessionarg
,
154 int rc
= LDAP_SUCCESS
; /* optimistic */
155 PRLDAPIOSessionArg
*prsessp
= NULL
;
160 ( rc
= prldap_session_arg_from_ld( ld
, &prsessp
))) {
163 } else if ( NULL
!= sessionarg
) {
164 prsessp
= (PRLDAPIOSessionArg
*)sessionarg
;
167 va_start( ap
, option
);
169 case PRLDAP_OPT_IO_MAX_TIMEOUT
:
170 rc
= prldap_get_io_max_timeout( prsessp
, va_arg( ap
, int * ));
173 rc
= LDAP_PARAM_ERROR
;
182 * Function: prldap_set_session_info().
184 * Given an LDAP session handle, set some application-specific data.
186 * Returns an LDAP API error code (LDAP_SUCCESS if all goes well).
189 prldap_set_session_info( LDAP
*ld
, void *sessionarg
, PRLDAPSessionInfo
*seip
)
192 PRLDAPIOSessionArg
*prsessp
;
194 if ( seip
== NULL
|| PRLDAP_SESSIONINFO_SIZE
!= seip
->seinfo_size
) {
195 ldap_set_lderrno( ld
, LDAP_PARAM_ERROR
, NULL
, NULL
);
196 return( LDAP_PARAM_ERROR
);
201 ( rc
= prldap_session_arg_from_ld( ld
, &prsessp
))) {
204 } else if ( NULL
!= sessionarg
) {
205 prsessp
= (PRLDAPIOSessionArg
*)sessionarg
;
207 ldap_set_lderrno( ld
, LDAP_PARAM_ERROR
, NULL
, NULL
);
208 return( LDAP_PARAM_ERROR
);
211 prsessp
->prsess_appdata
= seip
->seinfo_appdata
;
212 return( LDAP_SUCCESS
);
217 * Function: prldap_get_session_info().
219 * Given an LDAP session handle, retrieve some application-specific data.
221 * Returns an LDAP API error code (LDAP_SUCCESS if all goes well, in
222 * which case the fields in the structure that seip points to are filled in).
225 prldap_get_session_info( LDAP
*ld
, void *sessionarg
, PRLDAPSessionInfo
*seip
)
228 PRLDAPIOSessionArg
*prsessp
;
230 if ( seip
== NULL
|| PRLDAP_SESSIONINFO_SIZE
!= seip
->seinfo_size
) {
231 ldap_set_lderrno( ld
, LDAP_PARAM_ERROR
, NULL
, NULL
);
232 return( LDAP_PARAM_ERROR
);
237 ( rc
= prldap_session_arg_from_ld( ld
, &prsessp
))) {
240 } else if ( NULL
!= sessionarg
) {
241 prsessp
= (PRLDAPIOSessionArg
*)sessionarg
;
243 ldap_set_lderrno( ld
, LDAP_PARAM_ERROR
, NULL
, NULL
);
244 return( LDAP_PARAM_ERROR
);
247 seip
->seinfo_appdata
= prsessp
->prsess_appdata
;
248 return( LDAP_SUCCESS
);
253 * Function: prldap_set_socket_info().
255 * Given an integer fd and a void * argument such as those passed to the
256 * extended I/O callback functions, set socket specific information.
258 * Returns an LDAP API error code (LDAP_SUCCESS if all goes well).
260 * Note: it is only safe to change soinfo_prfd from within the SOCKET
261 * extended I/O callback function.
264 prldap_set_socket_info( int fd
, void *socketarg
, PRLDAPSocketInfo
*soip
)
266 PRLDAPIOSocketArg
*prsockp
;
268 if ( NULL
== socketarg
|| NULL
== soip
||
269 PRLDAP_SOCKETINFO_SIZE
!= soip
->soinfo_size
) {
270 return( LDAP_PARAM_ERROR
);
273 prsockp
= (PRLDAPIOSocketArg
*)socketarg
;
274 prsockp
->prsock_prfd
= soip
->soinfo_prfd
;
275 prsockp
->prsock_appdata
= soip
->soinfo_appdata
;
277 return( LDAP_SUCCESS
);
282 * Function: prldap_get_socket_info().
284 * Given an integer fd and a void * argument such as those passed to the
285 * extended I/O callback functions, retrieve socket specific information.
287 * Returns an LDAP API error code (LDAP_SUCCESS if all goes well, in
288 * which case the fields in the structure that soip points to are filled in).
291 prldap_get_socket_info( int fd
, void *socketarg
, PRLDAPSocketInfo
*soip
)
293 PRLDAPIOSocketArg
*prsockp
;
295 if ( NULL
== socketarg
|| NULL
== soip
||
296 PRLDAP_SOCKETINFO_SIZE
!= soip
->soinfo_size
) {
297 return( LDAP_PARAM_ERROR
);
300 prsockp
= (PRLDAPIOSocketArg
*)socketarg
;
301 soip
->soinfo_prfd
= prsockp
->prsock_prfd
;
302 soip
->soinfo_appdata
= prsockp
->prsock_appdata
;
304 return( LDAP_SUCCESS
);
308 * Function: prldap_get_default_socket_info().
310 * Given an LDAP session handle, retrieve socket specific information.
311 * If ld is NULL, LDAP_PARAM_ERROR is returned.
313 * Returns an LDAP API error code (LDAP_SUCCESS if all goes well, in
314 * which case the fields in the structure that soip points to are filled in).
317 prldap_get_default_socket_info( LDAP
*ld
, PRLDAPSocketInfo
*soip
)
320 PRLDAPIOSocketArg
*prsockp
;
323 if ( NULL
== soip
|| PRLDAP_SOCKETINFO_SIZE
!= soip
->soinfo_size
) {
324 ldap_set_lderrno( ld
, LDAP_PARAM_ERROR
, NULL
, NULL
);
325 return( LDAP_PARAM_ERROR
);
330 ( rc
= prldap_socket_arg_from_ld( ld
, &prsockp
))) {
334 ldap_set_lderrno( ld
, LDAP_PARAM_ERROR
, NULL
, NULL
);
335 return( LDAP_PARAM_ERROR
);
338 soip
->soinfo_prfd
= prsockp
->prsock_prfd
;
339 soip
->soinfo_appdata
= prsockp
->prsock_appdata
;
341 return( LDAP_SUCCESS
);
346 * Function: prldap_set_default_socket_info().
348 * Given an LDAP session handle, set socket specific information.
349 * If ld is NULL, LDAP_PARAM_ERROR is returned.
351 * Returns an LDAP API error code (LDAP_SUCCESS if all goes well, in
352 * which case the fields in the structure that soip points to are filled in).
355 prldap_set_default_socket_info( LDAP
*ld
, PRLDAPSocketInfo
*soip
)
358 PRLDAPIOSocketArg
*prsockp
;
361 if ( NULL
== soip
|| PRLDAP_SOCKETINFO_SIZE
!= soip
->soinfo_size
) {
362 ldap_set_lderrno( ld
, LDAP_PARAM_ERROR
, NULL
, NULL
);
363 return( LDAP_PARAM_ERROR
);
368 ( rc
= prldap_socket_arg_from_ld( ld
, &prsockp
))) {
372 ldap_set_lderrno( ld
, LDAP_PARAM_ERROR
, NULL
, NULL
);
373 return( LDAP_PARAM_ERROR
);
376 prsockp
->prsock_prfd
= soip
->soinfo_prfd
;
377 prsockp
->prsock_appdata
= soip
->soinfo_appdata
;
379 return( LDAP_SUCCESS
);