Chapter 29. Functions for Thread-Safe LDAP Connections
Table 29.1. Thread-Safe LDAP Connection Routines
| Function | Description |
|---|---|
| slapi_ldap_init() | Initializes an LDAP session with another LDAP server. |
| slapi_ldap_unbind() | Unbinds from another LDAP server and frees the resources contained in the LDAP structure. |
29.1. slapi_ldap_init()
This function initializes an LDAP session with another LDAP server. If you want to connect to another LDAP server over TLS 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 Red Hat Directory SDK.
ldap_unbind() or ldap_unbind_s() functions provided with the Directory Server SDK) when you are done with the session.
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() */
slapi_ldap_unbind( ld );
return 0;
}
#include "slapi-plugin.h" LDAP *slapi_ldap_init( char *ldaphost, int ldapport, int secure, int shared );
This function takes the following parameters:
|
ldaphost
| Space-delimited list of one or more host names (or IP address in dotted notation, such as 141.211.83.36) of the LDAP servers to which you want to connect. The names can be in hostname:portnumber format, in which case portnumber overrides the port number specified by the ldapport argument. |
|
ldapport
| Port number of the LDAP server. |
|
secure
| Determines whether to establish the connection over TLS. Set this to a non-zero value to establish the connection over TLS. |
|
shared
| Determines whether the LDAP session (the LDAP structure) can be shared by different threads. Set this to a non-zero value to allow multiple threads to share the session. |
This function returns one of the following values:
- If successful, returns a pointer to an LDAP structure, which should be passed to subsequent calls to other LDAP API functions.
- If unsuccessful, returns
NULL.

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.