Table of Contents | Previous | Next | Index

Netscape Directory SDK 3.0 for C Programmer’s Guide
     Chapter 18 Functions

ldap_init()

Initializes a session with an LDAP server and returns an LDAP structure that represents the context of the connection to that server.

Syntax

#include <ldap.h>
LDAP * ldap_init( const char *defhost, int defport );

Parameters

This function has the following parameters:

defhost

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 that you want the LDAP client to connect to.

The names can be in hostname:portnumber format (in which case, portnumber overrides the port number specified by the defport argument.

defport

Default port number of the LDAP server. To specify the standard LDAP port (port 389), use LDAP_PORT as the value for this parameter.

Returns

One of the following values:

Description

The ldap_init() function initializes a session with an LDAP server. ldap_init() 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.

You can specify the list of LDAP servers by passing a space-delimited list of the host names as the host argument. For example:

...
LDAP *ld
...
ld = ldap_init( "ld1.netscape.com ld2.netscape.com ld3.netscape.com", 
      LDAP_PORT );
In the example above, the LDAP client will attempt to connect to the LDAP server on ld1.netscape.com, port 389. If that server does not respond, the client will attempt to connect to the LDAP server on ld2.netscape.com, port 389. If that server does not respond, the client will use the server on ld3.netscape.com, port 389.

If any of the servers do not use the default port specified by the defport argument, use the host:port format to specify the server name. For example:

...
LDAP *ld
...
ld = ldap_init( "ld1.netscape.com ld2.netscape.com:38900", LDAP_PORT );
In the example above, the LDAP client will attempt to connect to the LDAP server on ld1.netscape.com, port 389. If that server does not respond, the client will attempt to connect to the LDAP server on ld2.netscape.com, port 38900.

Once you initialize a session, you need to call the ldap_simple_bind() or ldap_simple_bind_s() function to connect and authenticate to the LDAP server.

For more information, see "Initializing an LDAP Session" on page 51.

(Note that if you are connecting to a secure LDAP server over SSL, you should be calling the ldapssl_init() function instead. For details, see Chapter 12, "Connecting Over SSL".)

Example

The following section of code initializes a session with the LDAP server at ldap.netscape.com:389.

#include <ldap.h>
...
LDAP *ld;
/* Specify the host name of the LDAP server. */
char *ldap_host = "ldap.netscape.com";
/* Because the LDAP server is running on the standard LDAP port (port 389), you can use LDAP_PORT to identify the port number. */
int ldap_port = LDAP_PORT;
...
/* Initialize the session with ldap.netscape.com:389 */
if ( ( ld = ldap_init( ldap_host, ldap_port ) ) == NULL ) { 
   perror( "ldap_init" );
   return( 1 );
}
... 
/* Subsequent calls that authenticate to the LDAP server. */
...

See Also

ldap_unbind(), ldap_unbind_s().


Table of Contents | Previous | Next | Index

Last Updated: 10/01/98 17:06:23