Sun Directory Server Enterprise Edition 7.0 Developer's Guide

slapi_ldap_init()

Get a thread-safe handle to an LDAP connection.

Syntax

#include "slapi-plugin.h"
LDAP *slapi_ldap_init(char *ldaphost, int ldapport, int secure,
    int shared);

Parameters

This function takes the following parameters:

ldaphost

Host on which the LDAP server is running

ldapport

Port on which the LDAP server is listening

secure

1 for a secure connection over SSL, NULL otherwise

shared

If not NULL, then the connection may be shared between threads

Description

This function allows a plug-in to retrieve a thread-safe handle to an LDAP connection. When done with the handle, call slapi_ldap_unbind().

A timeout may be set for the connection using the Directory SDK for C provided as part of Directory Server Resource Kit. Example 15–1 demonstrates how to set a timeout.


Example 15–1 Setting a Timeout

#include "slapi-plugin.h"
#include "ldap.h"

void
my_ldap_function(void)
{
    LDAP * ld;
    int    to = 5000;                  /* 5000 ms == 5 s 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 for a search for example.                  */

    slapi_ldap_unbind(ld);
    return 0;
}

Returns

This function returns an LDAP connection handle if successful. Otherwise, it returns NULL.

See Also

slapi_ldap_unbind()