Back to index

lightning-sunbird  0.9+nobinonly
open.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Communicator client code, released
00015  * March 31, 1998.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 /*
00038  *  Copyright (c) 1995 Regents of the University of Michigan.
00039  *  All rights reserved.
00040  */
00041 /*
00042  *  open.c
00043  */
00044 
00045 #if 0
00046 #ifndef lint 
00047 static char copyright[] = "@(#) Copyright (c) 1995 Regents of the University of Michigan.\nAll rights reserved.\n";
00048 #endif
00049 #endif
00050 
00051 #include "ldap-int.h"
00052 
00053 #define VI_PRODUCTVERSION 3
00054 
00055 #ifndef INADDR_LOOPBACK
00056 #define INADDR_LOOPBACK     ((unsigned long) 0x7f000001)
00057 #endif
00058 
00059 #ifndef MAXHOSTNAMELEN
00060 #define MAXHOSTNAMELEN  64
00061 #endif
00062 
00063 #ifdef LDAP_DEBUG
00064 int    ldap_debug;
00065 #endif
00066 
00067 #ifdef _WINDOWS
00068 #define USE_WINDOWS_TLS /* thread local storage */
00069 #endif
00070 
00071 /*
00072  * global defaults for callbacks are stored here.  callers of the API set
00073  *    these by passing a NULL "ld" to ldap_set_option().  Everything in
00074  *    nsldapi_ld_defaults can be overridden on a per-ld basis as well (the
00075  *    memory allocation functions are global to all ld's).
00076  */
00077 struct ldap                     nsldapi_ld_defaults;
00078 struct ldap_memalloc_fns        nsldapi_memalloc_fns = { 0, 0, 0, 0 };
00079 int                         nsldapi_initialized = 0;
00080 
00081 #ifdef USE_PTHREADS
00082 #include <pthread.h>
00083 #ifdef VMS
00084 /*
00085 ** pthread_self() is not a routine on OpenVMS; it's inline assembler code. 
00086 ** Since we need a real address which we can stuff away into a table, we need 
00087 ** to make sure that pthread_self maps to the real pthread_self routine (yes,
00088 ** we do have one fortunately).
00089 */
00090 #undef pthread_self
00091 #define pthread_self PTHREAD_SELF
00092 extern pthread_t pthread_self (void);
00093 #endif
00094 static pthread_key_t        nsldapi_key;
00095 
00096 struct nsldapi_ldap_error {
00097         int     le_errno;
00098         char    *le_matched;
00099         char    *le_errmsg;
00100 };
00101 #elif defined (USE_WINDOWS_TLS)
00102 static DWORD dwTlsIndex;
00103 struct nsldapi_ldap_error {
00104        int    le_errno;
00105        char   *le_matched;
00106        char   *le_errmsg;
00107 };
00108 #elif defined (_WINDOWS) /* use static tls */
00109 __declspec ( thread ) int   nsldapi_gldaperrno;
00110 __declspec ( thread ) char  *nsldapi_gmatched = NULL;
00111 __declspec ( thread ) char  *nsldapi_gldaperror = NULL; 
00112 #endif /* USE_WINDOWS_TLS */
00113 
00114 
00115 #ifdef _WINDOWS
00116 #define       LDAP_MUTEX_T  HANDLE
00117 
00118 int
00119 pthread_mutex_init( LDAP_MUTEX_T *mp, void *attr)
00120 {
00121         if ( (*mp = CreateMutex(NULL, FALSE, NULL)) == NULL )
00122                 return( 1 );
00123         else
00124                 return( 0 );
00125 }
00126 
00127 static void *
00128 pthread_mutex_alloc( void )
00129 {
00130         LDAP_MUTEX_T *mutexp;
00131 
00132         if ( (mutexp = malloc( sizeof(LDAP_MUTEX_T) )) != NULL ) {
00133                 pthread_mutex_init( mutexp, NULL );
00134         }
00135         return( mutexp );
00136 }
00137 
00138 int
00139 pthread_mutex_destroy( LDAP_MUTEX_T *mp )
00140 {
00141         if ( !(CloseHandle(*mp)) )
00142                 return( 1 );
00143         else
00144                 return( 0 );
00145 }
00146 
00147 static void
00148 pthread_mutex_free( void *mutexp )
00149 {
00150         pthread_mutex_destroy( (LDAP_MUTEX_T *) mutexp );
00151         free( mutexp );
00152 }
00153 
00154 int
00155 pthread_mutex_lock( LDAP_MUTEX_T *mp )
00156 {
00157         if ( (WaitForSingleObject(*mp, INFINITE) != WAIT_OBJECT_0) )
00158                 return( 1 );
00159         else
00160                 return( 0 );
00161 }
00162 
00163 int
00164 pthread_mutex_unlock( LDAP_MUTEX_T *mp )
00165 {
00166         if ( !(ReleaseMutex(*mp)) )
00167                 return( 1 );
00168         else
00169                 return( 0 );
00170 }
00171 
00172 static int
00173 get_errno( void )
00174 {
00175        return errno;
00176 }
00177 
00178 static void
00179 set_errno( int Errno )
00180 {
00181        errno = Errno;
00182 }
00183 
00184 #ifdef USE_WINDOWS_TLS
00185 static void
00186 set_ld_error( int err, char *matched, char *errmsg, void *dummy )
00187 {
00188        struct nsldapi_ldap_error *le;
00189        void *tsd;
00190 
00191        le = TlsGetValue( dwTlsIndex );
00192 
00193        if (le == NULL) {
00194               tsd = (void *)calloc(1, sizeof(struct nsldapi_ldap_error));
00195               TlsSetValue( dwTlsIndex, tsd );
00196        }
00197 
00198        le = TlsGetValue ( dwTlsIndex );
00199 
00200        if (le == NULL)
00201               return;
00202 
00203        le->le_errno = err;
00204 
00205        if ( le->le_matched != NULL ) {
00206               ldap_memfree( le->le_matched );
00207        }
00208        le->le_matched = matched;
00209 
00210        if ( le->le_errmsg != NULL ) {
00211               ldap_memfree( le->le_errmsg );
00212        }
00213        le->le_errmsg = errmsg;
00214 }
00215 
00216 static int
00217 get_ld_error ( char **matched, char **errmsg, void *dummy )
00218 {
00219        struct nsldapi_ldap_error *le;
00220 
00221        le = TlsGetValue( dwTlsIndex );
00222        if ( matched != NULL ) {
00223               *matched = le->le_matched;
00224        }
00225 
00226        if ( errmsg != NULL ) {
00227               *errmsg = le->le_errmsg;
00228        }
00229 
00230        return( le->le_errno );
00231 }
00232 #else
00233 static int
00234 get_ld_error( char **LDMatched, char **LDError, void * Args )
00235 {
00236        if ( LDMatched != NULL )
00237        {
00238               *LDMatched = nsldapi_gmatched;
00239        }
00240        if ( LDError != NULL )
00241        {
00242               *LDError = nsldapi_gldaperror;
00243        }
00244        return nsldapi_gldaperrno;
00245 }
00246 
00247 static void
00248 set_ld_error( int LDErrno, char *  LDMatched, char *  LDError,
00249        void *  Args )
00250 {
00251        /* Clean up any previous string storage. */
00252        if ( nsldapi_gmatched != NULL )
00253        {
00254               ldap_memfree( nsldapi_gmatched );
00255        }
00256        if ( nsldapi_gldaperror != NULL )
00257        {
00258               ldap_memfree( nsldapi_gldaperror );
00259        }
00260 
00261        nsldapi_gldaperrno  = LDErrno;
00262        nsldapi_gmatched    = LDMatched;
00263        nsldapi_gldaperror  = LDError;
00264 }
00265 #endif /* USE_WINDOWS_TLS */
00266 #endif /* ! _WINDOWS */
00267 
00268 #ifdef USE_PTHREADS
00269 static void *
00270 pthread_mutex_alloc( void )
00271 {
00272        pthread_mutex_t *mutexp;
00273 
00274        if ( (mutexp = malloc( sizeof(pthread_mutex_t) )) != NULL ) {
00275               pthread_mutex_init( mutexp, NULL );
00276        }
00277        return( mutexp );
00278 }
00279 
00280 static void
00281 pthread_mutex_free( void *mutexp )
00282 {
00283        pthread_mutex_destroy( (pthread_mutex_t *) mutexp );
00284        free( mutexp );
00285 }
00286 
00287 static void
00288 set_ld_error( int err, char *matched, char *errmsg, void *dummy )
00289 {
00290         struct nsldapi_ldap_error *le;
00291         void *tsd;
00292 
00293         le = pthread_getspecific( nsldapi_key );
00294 
00295         if (le == NULL) {
00296                 tsd = (void *)calloc(1, sizeof(struct nsldapi_ldap_error));
00297                 pthread_setspecific( nsldapi_key, tsd );
00298         }
00299 
00300         le = pthread_getspecific( nsldapi_key );
00301 
00302         if (le == NULL)
00303                 return;
00304 
00305         le->le_errno = err;
00306 
00307         if ( le->le_matched != NULL ) {
00308                 ldap_memfree( le->le_matched );
00309         }
00310         le->le_matched = matched;
00311 
00312         if ( le->le_errmsg != NULL ) {
00313                 ldap_memfree( le->le_errmsg );
00314         }
00315         le->le_errmsg = errmsg;
00316 }
00317 
00318 static int
00319 get_ld_error( char **matched, char **errmsg, void *dummy )
00320 {
00321         struct nsldapi_ldap_error *le;
00322 
00323         le = pthread_getspecific( nsldapi_key );
00324         if ( matched != NULL ) {
00325                 *matched = le->le_matched;
00326         }
00327         if ( errmsg != NULL ) {
00328                 *errmsg = le->le_errmsg;
00329         }
00330         return( le->le_errno );
00331 }
00332 
00333 static void
00334 set_errno( int err )
00335 {
00336         errno = err;
00337 }
00338 
00339 static int
00340 get_errno( void )
00341 {
00342         return( errno );
00343 }
00344 #endif /* use_pthreads */
00345 
00346 #if defined(USE_PTHREADS) || defined(_WINDOWS)
00347 static struct ldap_thread_fns
00348        nsldapi_default_thread_fns = {
00349               (void *(*)(void))pthread_mutex_alloc,
00350               (void (*)(void *))pthread_mutex_free,
00351               (int (*)(void *))pthread_mutex_lock,
00352               (int (*)(void *))pthread_mutex_unlock,
00353                 (int (*)(void))get_errno,
00354                 (void (*)(int))set_errno,
00355                 (int (*)(char **, char **, void *))get_ld_error,
00356                 (void (*)(int, char *, char *, void *))set_ld_error,
00357               0 };
00358 
00359 static struct ldap_extra_thread_fns
00360        nsldapi_default_extra_thread_fns = {
00361               0, 0, 0, 0, 0,
00362 #ifdef _WINDOWS
00363               0
00364 #else
00365               (void *(*)(void))pthread_self
00366 #endif /* _WINDOWS */
00367               };
00368 #endif /* use_pthreads || _windows */
00369 
00370 void
00371 nsldapi_initialize_defaults( void )
00372 {
00373 
00374        if ( nsldapi_initialized ) {
00375               return;
00376        }
00377 
00378 #ifdef USE_PTHREADS
00379         if ( pthread_key_create(&nsldapi_key, free ) != 0) {
00380                 perror("pthread_key_create");
00381         }
00382 #elif defined(USE_WINDOWS_TLS)
00383        dwTlsIndex = TlsAlloc();
00384 #endif /* USE_WINDOWS_TLS */
00385 
00386        nsldapi_initialized = 1;
00387        memset( &nsldapi_memalloc_fns, 0, sizeof( nsldapi_memalloc_fns ));
00388        memset( &nsldapi_ld_defaults, 0, sizeof( nsldapi_ld_defaults ));
00389        nsldapi_ld_defaults.ld_options = LDAP_BITOPT_REFERRALS;
00390        nsldapi_ld_defaults.ld_version = LDAP_VERSION2;
00391        nsldapi_ld_defaults.ld_lberoptions = LBER_OPT_USE_DER;
00392        nsldapi_ld_defaults.ld_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
00393 
00394 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
00395        nsldapi_ld_defaults.ld_lberoptions |= LBER_OPT_TRANSLATE_STRINGS;
00396 #if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
00397        ldap_set_string_translators( &nsldapi_ld_defaults, ldap_8859_to_t61,
00398            ldap_t61_to_8859 );
00399 #endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
00400 #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
00401 
00402         /* set default connect timeout (in milliseconds) */
00403         /* this was picked as it is the standard tcp timeout as well */
00404         nsldapi_ld_defaults.ld_connect_timeout = LDAP_X_IO_TIMEOUT_NO_TIMEOUT;
00405 
00406 #if defined(USE_PTHREADS) || defined(_WINDOWS)
00407         /* load up default platform specific locking routines */
00408         if (ldap_set_option( NULL, LDAP_OPT_THREAD_FN_PTRS,
00409                 (void *)&nsldapi_default_thread_fns) != LDAP_SUCCESS) {
00410                 return;
00411         }
00412 
00413 #ifndef _WINDOWS
00414         /* load up default threadid function */
00415         if (ldap_set_option( NULL, LDAP_OPT_EXTRA_THREAD_FN_PTRS,
00416                 (void *)&nsldapi_default_extra_thread_fns) != LDAP_SUCCESS) {
00417                 return;
00418         }
00419 #endif /* _WINDOWS */
00420 #endif /* use_pthreads || _windows */
00421 }
00422 
00423 
00424 /*
00425  * ldap_version - report version levels for important properties
00426  * This function is deprecated.  Use ldap_get_option( ..., LDAP_OPT_API_INFO,
00427  *     ... ) instead.
00428  *
00429  * Example:
00430  *     LDAPVersion ver;
00431  *     ldap_version( &ver );
00432  *  if ( (ver.sdk_version < 100) || (ver.SSL_version < 300) )
00433  *      fprintf( stderr, "LDAP SDK level insufficient\n" );
00434  *
00435  * or:
00436  *  if ( ldap_version(NULL) < 100 )
00437  *      fprintf( stderr, "LDAP SDK level insufficient\n" );
00438  *
00439  */
00440 
00441 int
00442 LDAP_CALL
00443 ldap_version( LDAPVersion *ver )
00444 {
00445        if ( NULL != ver )
00446        {
00447               memset( ver, 0, sizeof(*ver) );
00448               ver->sdk_version = (int)(VI_PRODUCTVERSION * 100);
00449               ver->protocol_version = LDAP_VERSION_MAX * 100;
00450               ver->SSL_version = SSL_VERSION * 100;
00451               /* 
00452                * set security to none by default 
00453                */
00454 
00455               ver->security_level = LDAP_SECURITY_NONE;
00456 #if defined(LINK_SSL)
00457 #if defined(NS_DOMESTIC)
00458               ver->security_level = 128;
00459 #elif defined(NSS_EXPORT)
00460               ver->security_level = 40;
00461 #endif
00462 #endif
00463 
00464        }
00465        return (int)(VI_PRODUCTVERSION * 100);
00466 }
00467 
00468 /*
00469  * ldap_open - initialize and connect to an ldap server.  A magic cookie to
00470  * be used for future communication is returned on success, NULL on failure.
00471  * "host" may be a space-separated list of hosts or IP addresses
00472  *
00473  * Example:
00474  *     LDAP   *ld;
00475  *     ld = ldap_open( hostname, port );
00476  */
00477 
00478 LDAP *
00479 LDAP_CALL
00480 ldap_open( const char *host, int port )
00481 {
00482        LDAP   *ld;
00483 
00484        LDAPDebug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
00485 
00486        if (( ld = ldap_init( host, port )) == NULL ) {
00487               return( NULL );
00488        }
00489 
00490        LDAP_MUTEX_LOCK( ld, LDAP_CONN_LOCK );
00491        if ( nsldapi_open_ldap_defconn( ld ) < 0 ) {
00492               LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK );
00493               ldap_ld_free( ld, NULL, NULL, 0 );
00494               return( NULL );
00495        }
00496 
00497        LDAP_MUTEX_UNLOCK( ld, LDAP_CONN_LOCK );
00498        LDAPDebug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n",
00499               ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
00500 
00501        return( ld );
00502 }
00503 
00504 
00505 /*
00506  * ldap_init - initialize the LDAP library.  A magic cookie to be used for
00507  * future communication is returned on success, NULL on failure.
00508  * "defhost" may be a space-separated list of hosts or IP addresses
00509  *
00510  * NOTE: If you want to use IPv6, you must use prldap creating a LDAP handle
00511  * with prldap_init instead of ldap_init. Or install the NSPR functions
00512  * by calling prldap_install_routines. (See the nspr samples in examples)
00513  *
00514  * Example:
00515  *     LDAP   *ld;
00516  *     ld = ldap_init( default_hostname, default_port );
00517  */
00518 LDAP *
00519 LDAP_CALL
00520 ldap_init( const char *defhost, int defport )
00521 {
00522        LDAP   *ld;
00523 
00524        if ( !nsldapi_initialized ) {
00525               nsldapi_initialize_defaults();
00526        }
00527 
00528        if ( defport < 0 || defport > LDAP_PORT_MAX ) {
00529            LDAPDebug( LDAP_DEBUG_ANY,
00530                   "ldap_init: port %d is invalid (port numbers must range from 1 to %d)\n",
00531                   defport, LDAP_PORT_MAX, 0 );
00532 #if !defined( macintosh ) && !defined( DOS ) && !defined( BEOS )
00533            errno = EINVAL;
00534 #endif
00535            return( NULL );
00536        }
00537 
00538        LDAPDebug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 );
00539 
00540        if ( (ld = (LDAP*)NSLDAPI_MALLOC( sizeof(struct ldap) )) == NULL ) {
00541               return( NULL );
00542        }
00543 
00544        /* copy defaults */
00545        SAFEMEMCPY( ld, &nsldapi_ld_defaults, sizeof( struct ldap ));
00546        if ( nsldapi_ld_defaults.ld_io_fns_ptr != NULL ) {
00547               if (( ld->ld_io_fns_ptr = (struct ldap_io_fns *)NSLDAPI_MALLOC(
00548                   sizeof( struct ldap_io_fns ))) == NULL ) {
00549                      NSLDAPI_FREE( (char *)ld );
00550                      return( NULL );
00551               }
00552               /* struct copy */
00553               *(ld->ld_io_fns_ptr) = *(nsldapi_ld_defaults.ld_io_fns_ptr);
00554        }
00555 
00556        /* call the new handle I/O callback if one is defined */
00557        if ( ld->ld_extnewhandle_fn != NULL ) {
00558               /*
00559                * We always pass the session extended I/O argument to
00560                * the new handle callback.
00561                */
00562               if ( ld->ld_extnewhandle_fn( ld, ld->ld_ext_session_arg )
00563                   != LDAP_SUCCESS ) {
00564                      NSLDAPI_FREE( (char*)ld );
00565                      return( NULL );
00566               }
00567        }
00568 
00569        /* allocate session-specific resources */
00570        if (( ld->ld_sbp = ber_sockbuf_alloc()) == NULL ||
00571            ( defhost != NULL &&
00572            ( ld->ld_defhost = nsldapi_strdup( defhost )) == NULL ) ||
00573            ((ld->ld_mutex = (void **) NSLDAPI_CALLOC( LDAP_MAX_LOCK, sizeof(void *))) == NULL )) {
00574               if ( ld->ld_sbp != NULL ) {
00575                      ber_sockbuf_free( ld->ld_sbp );
00576               }
00577               if( ld->ld_mutex != NULL ) {
00578                      NSLDAPI_FREE( ld->ld_mutex );
00579               }
00580               NSLDAPI_FREE( (char*)ld );
00581               return( NULL );
00582        }
00583 
00584        /* install Sockbuf I/O functions if set in LDAP * */
00585        if ( ld->ld_extread_fn != NULL || ld->ld_extwrite_fn != NULL ) {
00586               struct lber_x_ext_io_fns lberiofns;
00587 
00588               memset( &lberiofns, 0, sizeof( lberiofns ));
00589 
00590               lberiofns.lbextiofn_size = LBER_X_EXTIO_FNS_SIZE;
00591               lberiofns.lbextiofn_read = ld->ld_extread_fn;
00592               lberiofns.lbextiofn_write = ld->ld_extwrite_fn;
00593               lberiofns.lbextiofn_writev = ld->ld_extwritev_fn;
00594               lberiofns.lbextiofn_socket_arg = NULL;
00595               ber_sockbuf_set_option( ld->ld_sbp, LBER_SOCKBUF_OPT_EXT_IO_FNS,
00596                      (void *)&lberiofns );
00597        }
00598 
00599        /* allocate mutexes */
00600        nsldapi_mutex_alloc_all( ld );
00601 
00602        /* set default port */
00603        ld->ld_defport = ( defport == 0 ) ? LDAP_PORT : defport;
00604 
00605        return( ld );
00606 }
00607 
00608 
00609 void
00610 nsldapi_mutex_alloc_all( LDAP *ld )
00611 {
00612        int    i;
00613 
00614        if ( ld != &nsldapi_ld_defaults && ld->ld_mutex != NULL ) {
00615               for ( i = 0; i<LDAP_MAX_LOCK; i++ ) {
00616                      ld->ld_mutex[i] = LDAP_MUTEX_ALLOC( ld );
00617                      ld->ld_mutex_threadid[i] = (void *) -1; 
00618                      ld->ld_mutex_refcnt[i] = 0; 
00619               }
00620        } 
00621 }
00622 
00623 
00624 void
00625 nsldapi_mutex_free_all( LDAP *ld )
00626 {
00627        int    i;
00628 
00629        if ( ld != &nsldapi_ld_defaults && ld->ld_mutex != NULL ) {
00630               for ( i = 0; i<LDAP_MAX_LOCK; i++ ) {
00631                      LDAP_MUTEX_FREE( ld, ld->ld_mutex[i] );
00632               }
00633        }
00634 }
00635 
00636 
00637 /* returns 0 if connection opened and -1 if an error occurs */
00638 int
00639 nsldapi_open_ldap_defconn( LDAP *ld )
00640 {
00641        LDAPServer    *srv;
00642 
00643        if (( srv = (LDAPServer *)NSLDAPI_CALLOC( 1, sizeof( LDAPServer ))) ==
00644            NULL || ( ld->ld_defhost != NULL && ( srv->lsrv_host =
00645            nsldapi_strdup( ld->ld_defhost )) == NULL )) {
00646               LDAP_SET_LDERRNO( ld, LDAP_NO_MEMORY, NULL, NULL );
00647               return( -1 );
00648        }
00649        srv->lsrv_port = ld->ld_defport;
00650 
00651        if (( ld->ld_options & LDAP_BITOPT_SSL ) != 0 ) {
00652               srv->lsrv_options |= LDAP_SRV_OPT_SECURE;
00653        }
00654 
00655        if (( ld->ld_defconn = nsldapi_new_connection( ld, &srv, 1, 1, 0 ))
00656            == NULL ) {
00657               if ( ld->ld_defhost != NULL ) {
00658                      NSLDAPI_FREE( srv->lsrv_host );
00659               }
00660               NSLDAPI_FREE( (char *)srv );
00661               return( -1 );
00662        }
00663        ++ld->ld_defconn->lconn_refcnt;    /* so it never gets closed/freed */
00664 
00665        return( 0 );
00666 }
00667 
00668 
00669 struct ldap_x_hostlist_status {
00670        char   *lhs_hostlist;
00671        char   *lhs_nexthost;
00672        int    lhs_defport;
00673 };
00674 
00675 /*
00676  * Return the first host and port in hostlist (setting *hostp and *portp).
00677  * Return value is an LDAP API error code (LDAP_SUCCESS if all goes well).
00678  * Note that a NULL or zero-length hostlist causes the host "127.0.0.1" to
00679  * be returned.
00680  */
00681 int LDAP_CALL
00682 ldap_x_hostlist_first( const char *hostlist, int defport, char **hostp,
00683     int *portp, struct ldap_x_hostlist_status **statusp )
00684 {
00685 
00686        if ( NULL == hostp || NULL == portp || NULL == statusp ) {
00687               return( LDAP_PARAM_ERROR );
00688        }
00689 
00690        if ( NULL == hostlist || *hostlist == '\0' ) {
00691               *hostp = nsldapi_strdup( "127.0.0.1" );
00692               if ( NULL == *hostp ) {
00693                      return( LDAP_NO_MEMORY );
00694               }
00695               *portp = defport;
00696               *statusp = NULL;
00697               return( LDAP_SUCCESS );
00698        }
00699 
00700        *statusp = NSLDAPI_CALLOC( 1, sizeof( struct ldap_x_hostlist_status ));
00701        if ( NULL == *statusp ) {
00702               return( LDAP_NO_MEMORY );
00703        }
00704        (*statusp)->lhs_hostlist = nsldapi_strdup( hostlist );
00705        if ( NULL == (*statusp)->lhs_hostlist ) {
00706               return( LDAP_NO_MEMORY );
00707        }
00708        (*statusp)->lhs_nexthost = (*statusp)->lhs_hostlist;
00709        (*statusp)->lhs_defport = defport;
00710        return( ldap_x_hostlist_next( hostp, portp, *statusp ));
00711 }
00712 
00713 /*
00714  * Return the next host and port in hostlist (setting *hostp and *portp).
00715  * Return value is an LDAP API error code (LDAP_SUCCESS if all goes well).
00716  * If no more hosts are available, LDAP_SUCCESS is returned but *hostp is set
00717  * to NULL.
00718  */
00719 int LDAP_CALL
00720 ldap_x_hostlist_next( char **hostp, int *portp,
00721        struct ldap_x_hostlist_status *status )
00722 {
00723        char   *q;
00724        int           squarebrackets = 0;
00725 
00726        if ( NULL == hostp || NULL == portp ) {
00727               return( LDAP_PARAM_ERROR );
00728        }
00729 
00730        if ( NULL == status || NULL == status->lhs_nexthost ) {
00731               *hostp = NULL;
00732               return( LDAP_SUCCESS );
00733        }
00734 
00735        /*
00736         * skip past leading '[' if present (IPv6 addresses may be surrounded
00737         * with square brackets, e.g., [fe80::a00:20ff:fee5:c0b4]:389
00738         */
00739        if ( status->lhs_nexthost[0] == '[' ) {
00740               ++status->lhs_nexthost;
00741               squarebrackets = 1;
00742        }
00743 
00744        /* copy host into *hostp */
00745        if ( NULL != ( q = strchr( status->lhs_nexthost, ' ' ))) {
00746               size_t len = q - status->lhs_nexthost;
00747               *hostp = NSLDAPI_MALLOC( len + 1 );
00748               if ( NULL == *hostp ) {
00749                      return( LDAP_NO_MEMORY );
00750               }
00751               strncpy( *hostp, status->lhs_nexthost, len );
00752               (*hostp)[len] = '\0';
00753               status->lhs_nexthost += ( len + 1 );
00754        } else {      /* last host */
00755               *hostp = nsldapi_strdup( status->lhs_nexthost );
00756               if ( NULL == *hostp ) {
00757                      return( LDAP_NO_MEMORY );
00758               }
00759               status->lhs_nexthost = NULL;
00760        }
00761 
00762        /* 
00763         * Look for closing ']' and skip past it before looking for port.
00764         */
00765        if ( squarebrackets && NULL != ( q = strchr( *hostp, ']' ))) {
00766               *q++ = '\0';
00767        } else {
00768               q = *hostp;
00769        }
00770 
00771        /* determine and set port */
00772        if ( NULL != ( q = strchr( q, ':' ))) {
00773               *q++ = '\0';
00774               *portp = atoi( q );
00775        } else {
00776               *portp = status->lhs_defport;
00777        }
00778 
00779        return( LDAP_SUCCESS );
00780 }
00781 
00782 
00783 void LDAP_CALL
00784 ldap_x_hostlist_statusfree( struct ldap_x_hostlist_status *status )
00785 {
00786        if ( NULL != status ) {
00787               if ( NULL != status->lhs_hostlist ) {
00788                      NSLDAPI_FREE( status->lhs_hostlist );
00789               }
00790               NSLDAPI_FREE( status );
00791        }
00792 }
00793 
00794 
00795 
00796 /*
00797  * memory allocation functions.  we include these in open.c since every
00798  *    LDAP application is likely to pull the rest of the code in this file
00799  *    in anyways.
00800  */
00801 void *
00802 ldap_x_malloc( size_t size )
00803 {
00804        return( nsldapi_memalloc_fns.ldapmem_malloc == NULL ?
00805            malloc( size ) :
00806            nsldapi_memalloc_fns.ldapmem_malloc( size ));
00807 }
00808 
00809 
00810 void *
00811 ldap_x_calloc( size_t nelem, size_t elsize )
00812 {
00813        return( nsldapi_memalloc_fns.ldapmem_calloc == NULL ?
00814            calloc(  nelem, elsize ) :
00815            nsldapi_memalloc_fns.ldapmem_calloc( nelem, elsize ));
00816 }
00817 
00818 
00819 void *
00820 ldap_x_realloc( void *ptr, size_t size )
00821 {
00822        return( nsldapi_memalloc_fns.ldapmem_realloc == NULL ?
00823            realloc( ptr, size ) :
00824            nsldapi_memalloc_fns.ldapmem_realloc( ptr, size ));
00825 }
00826 
00827 
00828 void
00829 ldap_x_free( void *ptr )
00830 {
00831        if ( nsldapi_memalloc_fns.ldapmem_free == NULL ) {
00832               free( ptr );
00833        } else {
00834               nsldapi_memalloc_fns.ldapmem_free( ptr );
00835        }
00836 }
00837 
00838 
00839 /* if s is NULL, returns NULL */
00840 char *
00841 nsldapi_strdup( const char *s )
00842 {
00843        char   *p;
00844 
00845        if ( s == NULL ||
00846            (p = (char *)NSLDAPI_MALLOC( strlen( s ) + 1 )) == NULL )
00847               return( NULL );
00848 
00849        strcpy( p, s );
00850 
00851        return( p );
00852 }