Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Functions | Variables
open.c File Reference
#include "ldap-int.h"

Go to the source code of this file.

Classes

struct  ldap_x_hostlist_status

Defines

#define VI_PRODUCTVERSION   3
#define INADDR_LOOPBACK   ((unsigned long) 0x7f000001)
#define MAXHOSTNAMELEN   64

Functions

void nsldapi_initialize_defaults (void)
int LDAP_CALL ldap_version (LDAPVersion *ver)
LDAP *LDAP_CALL ldap_open (const char *host, int port)
LDAP *LDAP_CALL ldap_init (const char *defhost, int defport)
void nsldapi_mutex_alloc_all (LDAP *ld)
void nsldapi_mutex_free_all (LDAP *ld)
int nsldapi_open_ldap_defconn (LDAP *ld)
int LDAP_CALL ldap_x_hostlist_first (const char *hostlist, int defport, char **hostp, int *portp, struct ldap_x_hostlist_status **statusp)
int LDAP_CALL ldap_x_hostlist_next (char **hostp, int *portp, struct ldap_x_hostlist_status *status)
void LDAP_CALL ldap_x_hostlist_statusfree (struct ldap_x_hostlist_status *status)
voidldap_x_malloc (size_t size)
voidldap_x_calloc (size_t nelem, size_t elsize)
voidldap_x_realloc (void *ptr, size_t size)
void ldap_x_free (void *ptr)
char * nsldapi_strdup (const char *s)

Variables

int nsldapi_initialized = 0

Class Documentation

struct ldap_x_hostlist_status

Definition at line 669 of file open.c.

Collaboration diagram for ldap_x_hostlist_status:
Class Members
int lhs_defport
char * lhs_hostlist
char * lhs_nexthost

Define Documentation

#define INADDR_LOOPBACK   ((unsigned long) 0x7f000001)

Definition at line 56 of file open.c.

Definition at line 60 of file open.c.

Definition at line 53 of file open.c.


Function Documentation

LDAP* LDAP_CALL ldap_init ( const char *  defhost,
int  defport 
)

Definition at line 520 of file open.c.

{
       LDAP   *ld;

       if ( !nsldapi_initialized ) {
              nsldapi_initialize_defaults();
       }

       if ( defport < 0 || defport > LDAP_PORT_MAX ) {
           LDAPDebug( LDAP_DEBUG_ANY,
                  "ldap_init: port %d is invalid (port numbers must range from 1 to %d)\n",
                  defport, LDAP_PORT_MAX, 0 );
#if !defined( macintosh ) && !defined( DOS ) && !defined( BEOS )
           errno = EINVAL;
#endif
           return( NULL );
       }

       LDAPDebug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 );

       if ( (ld = (LDAP*)NSLDAPI_MALLOC( sizeof(struct ldap) )) == NULL ) {
              return( NULL );
       }

       /* copy defaults */
       SAFEMEMCPY( ld, &nsldapi_ld_defaults, sizeof( struct ldap ));
       if ( nsldapi_ld_defaults.ld_io_fns_ptr != NULL ) {
              if (( ld->ld_io_fns_ptr = (struct ldap_io_fns *)NSLDAPI_MALLOC(
                  sizeof( struct ldap_io_fns ))) == NULL ) {
                     NSLDAPI_FREE( (char *)ld );
                     return( NULL );
              }
              /* struct copy */
              *(ld->ld_io_fns_ptr) = *(nsldapi_ld_defaults.ld_io_fns_ptr);
       }

       /* call the new handle I/O callback if one is defined */
       if ( ld->ld_extnewhandle_fn != NULL ) {
              /*
               * We always pass the session extended I/O argument to
               * the new handle callback.
               */
              if ( ld->ld_extnewhandle_fn( ld, ld->ld_ext_session_arg )
                  != LDAP_SUCCESS ) {
                     NSLDAPI_FREE( (char*)ld );
                     return( NULL );
              }
       }

       /* allocate session-specific resources */
       if (( ld->ld_sbp = ber_sockbuf_alloc()) == NULL ||
           ( defhost != NULL &&
           ( ld->ld_defhost = nsldapi_strdup( defhost )) == NULL ) ||
           ((ld->ld_mutex = (void **) NSLDAPI_CALLOC( LDAP_MAX_LOCK, sizeof(void *))) == NULL )) {
              if ( ld->ld_sbp != NULL ) {
                     ber_sockbuf_free( ld->ld_sbp );
              }
              if( ld->ld_mutex != NULL ) {
                     NSLDAPI_FREE( ld->ld_mutex );
              }
              NSLDAPI_FREE( (char*)ld );
              return( NULL );
       }

       /* install Sockbuf I/O functions if set in LDAP * */
       if ( ld->ld_extread_fn != NULL || ld->ld_extwrite_fn != NULL ) {
              struct lber_x_ext_io_fns lberiofns;

              memset( &lberiofns, 0, sizeof( lberiofns ));

              lberiofns.lbextiofn_size = LBER_X_EXTIO_FNS_SIZE;
              lberiofns.lbextiofn_read = ld->ld_extread_fn;
              lberiofns.lbextiofn_write = ld->ld_extwrite_fn;
              lberiofns.lbextiofn_writev = ld->ld_extwritev_fn;
              lberiofns.lbextiofn_socket_arg = NULL;
              ber_sockbuf_set_option( ld->ld_sbp, LBER_SOCKBUF_OPT_EXT_IO_FNS,
                     (void *)&lberiofns );
       }

       /* allocate mutexes */
       nsldapi_mutex_alloc_all( ld );

       /* set default port */
       ld->ld_defport = ( defport == 0 ) ? LDAP_PORT : defport;

       return( ld );
}

Here is the call graph for this function:

Here is the caller graph for this function:

LDAP* LDAP_CALL ldap_open ( const char *  host,
int  port 
)

Definition at line 480 of file open.c.

{
       LDAP   *ld;

       LDAPDebug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );

       if (( ld = ldap_init( host, port )) == NULL ) {
              return( NULL );
       }

       LDAP_MUTEX_LOCK( ld, LDAP_CONN_LOCK );
       if ( nsldapi_open_ldap_defconn( ld ) < 0 ) {
              LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK );
              ldap_ld_free( ld, NULL, NULL, 0 );
              return( NULL );
       }

       LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK );
       LDAPDebug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n",
              ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );

       return( ld );
}

Here is the call graph for this function:

Definition at line 443 of file open.c.

{
       if ( NULL != ver )
       {
              memset( ver, 0, sizeof(*ver) );
              ver->sdk_version = (int)(VI_PRODUCTVERSION * 100);
              ver->protocol_version = LDAP_VERSION_MAX * 100;
              ver->SSL_version = SSL_VERSION * 100;
              /* 
               * set security to none by default 
               */

              ver->security_level = LDAP_SECURITY_NONE;
#if defined(LINK_SSL)
#if defined(NS_DOMESTIC)
              ver->security_level = 128;
#elif defined(NSS_EXPORT)
              ver->security_level = 40;
#endif
#endif

       }
       return (int)(VI_PRODUCTVERSION * 100);
}

Here is the call graph for this function:

void* ldap_x_calloc ( size_t  nelem,
size_t  elsize 
)

Definition at line 811 of file open.c.

{
       return( nsldapi_memalloc_fns.ldapmem_calloc == NULL ?
           calloc(  nelem, elsize ) :
           nsldapi_memalloc_fns.ldapmem_calloc( nelem, elsize ));
}

Here is the call graph for this function:

void ldap_x_free ( void ptr)

Definition at line 829 of file open.c.

{
       if ( nsldapi_memalloc_fns.ldapmem_free == NULL ) {
              free( ptr );
       } else {
              nsldapi_memalloc_fns.ldapmem_free( ptr );
       }
}
int LDAP_CALL ldap_x_hostlist_first ( const char *  hostlist,
int  defport,
char **  hostp,
int portp,
struct ldap_x_hostlist_status **  statusp 
)

Definition at line 682 of file open.c.

{

       if ( NULL == hostp || NULL == portp || NULL == statusp ) {
              return( LDAP_PARAM_ERROR );
       }

       if ( NULL == hostlist || *hostlist == '\0' ) {
              *hostp = nsldapi_strdup( "127.0.0.1" );
              if ( NULL == *hostp ) {
                     return( LDAP_NO_MEMORY );
              }
              *portp = defport;
              *statusp = NULL;
              return( LDAP_SUCCESS );
       }

       *statusp = NSLDAPI_CALLOC( 1, sizeof( struct ldap_x_hostlist_status ));
       if ( NULL == *statusp ) {
              return( LDAP_NO_MEMORY );
       }
       (*statusp)->lhs_hostlist = nsldapi_strdup( hostlist );
       if ( NULL == (*statusp)->lhs_hostlist ) {
              return( LDAP_NO_MEMORY );
       }
       (*statusp)->lhs_nexthost = (*statusp)->lhs_hostlist;
       (*statusp)->lhs_defport = defport;
       return( ldap_x_hostlist_next( hostp, portp, *statusp ));
}

Here is the call graph for this function:

Here is the caller graph for this function:

int LDAP_CALL ldap_x_hostlist_next ( char **  hostp,
int portp,
struct ldap_x_hostlist_status status 
)

Definition at line 720 of file open.c.

{
       char   *q;
       int           squarebrackets = 0;

       if ( NULL == hostp || NULL == portp ) {
              return( LDAP_PARAM_ERROR );
       }

       if ( NULL == status || NULL == status->lhs_nexthost ) {
              *hostp = NULL;
              return( LDAP_SUCCESS );
       }

       /*
        * skip past leading '[' if present (IPv6 addresses may be surrounded
        * with square brackets, e.g., [fe80::a00:20ff:fee5:c0b4]:389
        */
       if ( status->lhs_nexthost[0] == '[' ) {
              ++status->lhs_nexthost;
              squarebrackets = 1;
       }

       /* copy host into *hostp */
       if ( NULL != ( q = strchr( status->lhs_nexthost, ' ' ))) {
              size_t len = q - status->lhs_nexthost;
              *hostp = NSLDAPI_MALLOC( len + 1 );
              if ( NULL == *hostp ) {
                     return( LDAP_NO_MEMORY );
              }
              strncpy( *hostp, status->lhs_nexthost, len );
              (*hostp)[len] = '\0';
              status->lhs_nexthost += ( len + 1 );
       } else {      /* last host */
              *hostp = nsldapi_strdup( status->lhs_nexthost );
              if ( NULL == *hostp ) {
                     return( LDAP_NO_MEMORY );
              }
              status->lhs_nexthost = NULL;
       }

       /* 
        * Look for closing ']' and skip past it before looking for port.
        */
       if ( squarebrackets && NULL != ( q = strchr( *hostp, ']' ))) {
              *q++ = '\0';
       } else {
              q = *hostp;
       }

       /* determine and set port */
       if ( NULL != ( q = strchr( q, ':' ))) {
              *q++ = '\0';
              *portp = atoi( q );
       } else {
              *portp = status->lhs_defport;
       }

       return( LDAP_SUCCESS );
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 784 of file open.c.

{
       if ( NULL != status ) {
              if ( NULL != status->lhs_hostlist ) {
                     NSLDAPI_FREE( status->lhs_hostlist );
              }
              NSLDAPI_FREE( status );
       }
}

Here is the caller graph for this function:

void* ldap_x_malloc ( size_t  size)

Definition at line 802 of file open.c.

{
       return( nsldapi_memalloc_fns.ldapmem_malloc == NULL ?
           malloc( size ) :
           nsldapi_memalloc_fns.ldapmem_malloc( size ));
}
void* ldap_x_realloc ( void ptr,
size_t  size 
)

Definition at line 820 of file open.c.

{
       return( nsldapi_memalloc_fns.ldapmem_realloc == NULL ?
           realloc( ptr, size ) :
           nsldapi_memalloc_fns.ldapmem_realloc( ptr, size ));
}

Here is the call graph for this function:

Definition at line 371 of file open.c.

{

       if ( nsldapi_initialized ) {
              return;
       }

#ifdef USE_PTHREADS
        if ( pthread_key_create(&nsldapi_key, free ) != 0) {
                perror("pthread_key_create");
        }
#elif defined(USE_WINDOWS_TLS)
       dwTlsIndex = TlsAlloc();
#endif /* USE_WINDOWS_TLS */

       nsldapi_initialized = 1;
       memset( &nsldapi_memalloc_fns, 0, sizeof( nsldapi_memalloc_fns ));
       memset( &nsldapi_ld_defaults, 0, sizeof( nsldapi_ld_defaults ));
       nsldapi_ld_defaults.ld_options = LDAP_BITOPT_REFERRALS;
       nsldapi_ld_defaults.ld_version = LDAP_VERSION2;
       nsldapi_ld_defaults.ld_lberoptions = LBER_OPT_USE_DER;
       nsldapi_ld_defaults.ld_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;

#if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
       nsldapi_ld_defaults.ld_lberoptions |= LBER_OPT_TRANSLATE_STRINGS;
#if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
       ldap_set_string_translators( &nsldapi_ld_defaults, ldap_8859_to_t61,
           ldap_t61_to_8859 );
#endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
#endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */

        /* set default connect timeout (in milliseconds) */
        /* this was picked as it is the standard tcp timeout as well */
        nsldapi_ld_defaults.ld_connect_timeout = LDAP_X_IO_TIMEOUT_NO_TIMEOUT;

#if defined(USE_PTHREADS) || defined(_WINDOWS)
        /* load up default platform specific locking routines */
        if (ldap_set_option( NULL, LDAP_OPT_THREAD_FN_PTRS,
                (void *)&nsldapi_default_thread_fns) != LDAP_SUCCESS) {
                return;
        }

#ifndef _WINDOWS
        /* load up default threadid function */
        if (ldap_set_option( NULL, LDAP_OPT_EXTRA_THREAD_FN_PTRS,
                (void *)&nsldapi_default_extra_thread_fns) != LDAP_SUCCESS) {
                return;
        }
#endif /* _WINDOWS */
#endif /* use_pthreads || _windows */
}

Here is the call graph for this function:

Here is the caller graph for this function:

void nsldapi_mutex_alloc_all ( LDAP *  ld)

Definition at line 610 of file open.c.

{
       int    i;

       if ( ld != &nsldapi_ld_defaults && ld->ld_mutex != NULL ) {
              for ( i = 0; i<LDAP_MAX_LOCK; i++ ) {
                     ld->ld_mutex[i] = LDAP_MUTEX_ALLOC( ld );
                     ld->ld_mutex_threadid[i] = (void *) -1; 
                     ld->ld_mutex_refcnt[i] = 0; 
              }
       } 
}

Here is the caller graph for this function:

void nsldapi_mutex_free_all ( LDAP *  ld)

Definition at line 625 of file open.c.

{
       int    i;

       if ( ld != &nsldapi_ld_defaults && ld->ld_mutex != NULL ) {
              for ( i = 0; i<LDAP_MAX_LOCK; i++ ) {
                     LDAP_MUTEX_FREE( ld, ld->ld_mutex[i] );
              }
       }
}

Here is the caller graph for this function:

Definition at line 639 of file open.c.

{
       LDAPServer    *srv;

       if (( srv = (LDAPServer *)NSLDAPI_CALLOC( 1, sizeof( LDAPServer ))) ==
           NULL || ( ld->ld_defhost != NULL && ( srv->lsrv_host =
           nsldapi_strdup( ld->ld_defhost )) == NULL )) {
              LDAP_SET_LDERRNO( ld, LDAP_NO_MEMORY, NULL, NULL );
              return( -1 );
       }
       srv->lsrv_port = ld->ld_defport;

       if (( ld->ld_options & LDAP_BITOPT_SSL ) != 0 ) {
              srv->lsrv_options |= LDAP_SRV_OPT_SECURE;
       }

       if (( ld->ld_defconn = nsldapi_new_connection( ld, &srv, 1, 1, 0 ))
           == NULL ) {
              if ( ld->ld_defhost != NULL ) {
                     NSLDAPI_FREE( srv->lsrv_host );
              }
              NSLDAPI_FREE( (char *)srv );
              return( -1 );
       }
       ++ld->ld_defconn->lconn_refcnt;    /* so it never gets closed/freed */

       return( 0 );
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* nsldapi_strdup ( const char *  s)

Definition at line 841 of file open.c.

{
       char   *p;

       if ( s == NULL ||
           (p = (char *)NSLDAPI_MALLOC( strlen( s ) + 1 )) == NULL )
              return( NULL );

       strcpy( p, s );

       return( p );
}

Here is the caller graph for this function:


Variable Documentation

Definition at line 79 of file open.c.