+/* $OpenLDAP$ */
/*
* Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
#include <ac/stdlib.h>
+#include <ac/param.h>
#include <ac/socket.h>
#include <ac/string.h>
#include <ac/time.h>
-#ifdef HAVE_SYS_PARAM_H
-#include <sys/param.h>
-#endif
-
#include "ldap-int.h"
+int ldap_open_defconn( LDAP *ld )
+{
+ LDAPServer *srv;
+
+ if (( srv = (LDAPServer *)LDAP_CALLOC( 1, sizeof( LDAPServer ))) ==
+ NULL || ( ld->ld_defhost != NULL && ( srv->lsrv_host =
+ LDAP_STRDUP( ld->ld_defhost )) == NULL ))
+ {
+ if( srv != NULL ) LDAP_FREE( (char*) srv );
+ ld->ld_errno = LDAP_NO_MEMORY;
+ return -1;
+ }
+
+ srv->lsrv_port = ld->ld_defport;
+
+ if (( ld->ld_defconn = ldap_new_connection( ld, &srv, 1,1,0 )) == NULL )
+ {
+ if ( ld->ld_defhost != NULL ) LDAP_FREE( srv->lsrv_host );
+ LDAP_FREE( (char *)srv );
+ ld->ld_errno = LDAP_SERVER_DOWN;
+ return -1;
+ }
+
+ ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
+
+ return 0;
+}
/*
* ldap_open - initialize and connect to an ldap server. A magic cookie to
LDAP *
ldap_open( LDAP_CONST char *host, int port )
{
+ int rc;
LDAP *ld;
- LDAPServer *srv;
Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
return( NULL );
}
- if (( srv = (LDAPServer *)LDAP_CALLOC( 1, sizeof( LDAPServer ))) ==
- NULL || ( ld->ld_defhost != NULL && ( srv->lsrv_host =
- LDAP_STRDUP( ld->ld_defhost )) == NULL )) {
- if(srv != NULL) LDAP_FREE( (char*) srv );
- ldap_ld_free( ld, 0, NULL, NULL );
- return( NULL );
- }
- srv->lsrv_port = ld->ld_defport;
+ rc = ldap_open_defconn( ld );
- if (( ld->ld_defconn = ldap_new_connection( ld, &srv, 1,1,0 )) == NULL ) {
- if ( ld->ld_defhost != NULL ) LDAP_FREE( srv->lsrv_host );
- LDAP_FREE( (char *)srv );
+ if( rc < 0 ) {
ldap_ld_free( ld, 0, NULL, NULL );
return( NULL );
}
- ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
Debug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n",
( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
port = defport;
}
- if (( rc = ldap_connect_to_host( sb, curhost, 0L,
+ if (( rc = ldap_connect_to_host( ld, sb, curhost, 0L,
port, async )) != -1 ) {
break;
}
}
} else {
- rc = ldap_connect_to_host( sb, NULL, htonl( INADDR_LOOPBACK ),
+ rc = ldap_connect_to_host( ld, sb, 0, htonl( INADDR_LOOPBACK ),
defport, async );
}