HP-UX Directory Server 8.1 plug-in reference

Table Of Contents
29 Functions for thread-safe LDAP connections
This chapter contains reference information on functions for thread-safe LDAP connections.
Table 29-1 Thread-safe LDAP connection routines
DescriptionFunction
Initializes an LDAP session with another LDAP server.“slapi_ldap_init()”
Unbinds from another LDAP server and frees the resources contained in the
LDAP structure.
“slapi_ldap_unbind()”
29.1 slapi_ldap_init()
Initializes an LDAP session with another LDAP server.
Description This function initializes an LDAP session with another LDAP server. If you want
to connect to another LDAP server over SSL or if you want to allow multiple threads to use the
same connection, call this function instead of the ldap_init() function provided with the HP
Directory SDK.
This function allocates an LDAP structure containing information about the session, including
the host name and port of the LDAP server, preferences for the session (such as the maximum
number of entries to return in a search), and the error code of the last LDAP operation performed.
You can specify a list of LDAP servers that you want to attempt to connect to. Your client will
attempt to connect to the first LDAP server in the list. If the attempt fails, your client will attempt
to connect to the next LDAP server in the list.
If you specify a nonzero value for the secure argument, this function initializes the plug-in for
SSL and installs the I/O routines for SSL.
If you specify a nonzero value for the shared argument, this function installs the server's
threading functions and allows multiple threads to share this session (the returned LDAP structure).
The Directory Server processes each request in a separate thread. When handling multiple
requests, it is possible for the server to call your plug-in function concurrently for different
threads.
If you initialize a session by calling this function, make sure to call the “slapi_ldap_unbind()”
function (not the ldap_unbind() or ldap_unbind_s() functions provided with the Directory
Server SDK) when you are done with the session.
As the slapi_ldap_init() function returns a regular LDAP *, you can use the LDAP C SDK
connect timeout feature for plug-ins. That is, when connecting to an external LDAP server from
a plug-in, you can specify a time limit for establishing the connection. To specify the timeout,
call ldap_set_option() with the LDAP_X_OPT_CONNECT_TIMEOUT option after calling
slapi_ldap_init(), as illustrated in the sample code below:
void my_ldap_function( void ) {
LDAP *ld;
int to = 5000; /* 5000 milliseconds == 5 second timeout */
if (( ld = slapi_ldap_init( host, port, 0, 1 )) == NULL ) {
/* error trying to create an LDAP session */
return -1;
}
if ( ldap_set_option( ld, LDAP_X_OPT_CONNECT_TIMEOUT, &to ) != 0 ) {
/* error setting timeout */
slapi_ldap_unbind( ld );
return -1;
}
/* use the handle, e.g., call ldap_search_ext() */
29.1 slapi_ldap_init() 251