From: Kurt Zeilenga Date: Sun, 13 Jan 2002 04:53:46 +0000 (+0000) Subject: Updated Winsock codes X-Git-Tag: OPENLDAP_REL_ENG_2_0_20~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=79ee9bda407e9793f29b9f75ef11f0a5056553b7;p=thirdparty%2Fopenldap.git Updated Winsock codes --- diff --git a/CHANGES b/CHANGES index 6b1509631e..e77bd1edd1 100644 --- a/CHANGES +++ b/CHANGES @@ -12,6 +12,7 @@ OpenLDAP 2.0.20 Engineering Fixed repl_user build error (ITS#1503) Updated BerkeleyDB 4 support Updated schema files + Updated Windows NT/2000 MSVC5 port OpenLDAP 2.0.19 Release Fixed back-ldbm not filter indexing (ITS#1405) diff --git a/libraries/libldap/init.c b/libraries/libldap/init.c index c553d678d0..4809fdd00c 100644 --- a/libraries/libldap/init.c +++ b/libraries/libldap/init.c @@ -263,11 +263,11 @@ static void openldap_ldap_init_w_userconf(const char *file) /* we assume UNIX path syntax is used... */ /* try ~/file */ - sprintf(path, "%s/%s", home, file); + sprintf(path, "%s%s%s", home, LDAP_DIRSEP, file); openldap_ldap_init_w_conf(path, 1); /* try ~/.file */ - sprintf(path, "%s/.%s", home, file); + sprintf(path, "%s%s.%s", home, LDAP_DIRSEP, file); openldap_ldap_init_w_conf(path, 1); } @@ -364,6 +364,20 @@ static void openldap_ldap_init_w_env( } } +static void +ldap_int_destroy_global_options(void) +{ + struct ldapoptions *gopts = LDAP_INT_GLOBAL_OPT(); + + if ( gopts->ldo_defludp ) { + ldap_free_urllist( gopts->ldo_defludp ); + gopts->ldo_defludp = NULL; + } +#if defined(HAVE_WINSOCK) || defined(HAVE_WINSOCK2) + WSACleanup( ); +#endif +} + /* * Initialize the global options structure with default values. */ @@ -382,11 +396,11 @@ void ldap_int_initialize_global_options( struct ldapoptions *gopts, int *dbglvl gopts->ldo_tm_api = (struct timeval *)NULL; gopts->ldo_tm_net = (struct timeval *)NULL; - /* ldo_defludp is leaked, we should have an at_exit() handler - * to free this and whatever else needs to cleaned up. + /* ldo_defludp wll be freed by the atexit() handler */ ldap_url_parselist(&gopts->ldo_defludp, "ldap://localhost/"); gopts->ldo_defport = LDAP_PORT; + atexit(ldap_int_destroy_global_options); gopts->ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT; gopts->ldo_rebindproc = NULL; @@ -425,11 +439,44 @@ void ldap_int_initialize( struct ldapoptions *gopts, int *dbglvl ) return; } +#ifdef HAVE_WINSOCK2 +{ WORD wVersionRequested; + WSADATA wsaData; + + wVersionRequested = MAKEWORD( 2, 0 ); + if ( WSAStartup( wVersionRequested, &wsaData ) != 0 ) { + /* Tell the user that we couldn't find a usable */ + /* WinSock DLL. */ + return; + } + + /* Confirm that the WinSock DLL supports 2.0.*/ + /* Note that if the DLL supports versions greater */ + /* than 2.0 in addition to 2.0, it will still return */ + /* 2.0 in wVersion since that is the version we */ + /* requested. */ + + if ( LOBYTE( wsaData.wVersion ) != 2 || + HIBYTE( wsaData.wVersion ) != 0 ) + { + /* Tell the user that we couldn't find a usable */ + /* WinSock DLL. */ + WSACleanup( ); + return; + } +} /* The WinSock DLL is acceptable. Proceed. */ +#elif HAVE_WINSOCK +{ WSADATA wsaData; + if ( WSAStartup( 0x0101, &wsaData ) != 0 ) { + return; + } +} +#endif + #if defined(LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND) \ || defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL) ldap_int_hostname = ldap_pvt_get_fqdn( ldap_int_hostname ); #endif - ldap_int_utils_init(); if ( ldap_int_tblsize == 0 ) @@ -450,8 +497,7 @@ void ldap_int_initialize( struct ldapoptions *gopts, int *dbglvl ) if( user == NULL ) user = getenv("LOGNAME"); if( user != NULL ) { - /* this value is leaked, need at_exit() handler */ - gopts->ldo_def_sasl_authcid = LDAP_STRDUP( user ); + gopts->ldo_def_sasl_authcid = user; } } #endif diff --git a/libraries/libldap/open.c b/libraries/libldap/open.c index 93b4ad3f62..ce648ffa6c 100644 --- a/libraries/libldap/open.c +++ b/libraries/libldap/open.c @@ -92,47 +92,13 @@ ldap_create( LDAP **ldp ) /* Initialize the global options, if not already done. */ if( gopts->ldo_valid != LDAP_INITIALIZED ) { ldap_int_initialize(gopts, NULL); + if ( gopts->ldo_valid != LDAP_INITIALIZED ) + return LDAP_LOCAL_ERROR; } Debug( LDAP_DEBUG_TRACE, "ldap_create\n", 0, 0, 0 ); -#ifdef HAVE_WINSOCK2 -{ WORD wVersionRequested; - WSADATA wsaData; - - wVersionRequested = MAKEWORD( 2, 0 ); - if ( WSAStartup( wVersionRequested, &wsaData ) != 0 ) { - /* Tell the user that we couldn't find a usable */ - /* WinSock DLL. */ - return LDAP_LOCAL_ERROR; - } - - /* Confirm that the WinSock DLL supports 2.0.*/ - /* Note that if the DLL supports versions greater */ - /* than 2.0 in addition to 2.0, it will still return */ - /* 2.0 in wVersion since that is the version we */ - /* requested. */ - - if ( LOBYTE( wsaData.wVersion ) != 2 || - HIBYTE( wsaData.wVersion ) != 0 ) - { - /* Tell the user that we couldn't find a usable */ - /* WinSock DLL. */ - WSACleanup( ); - return LDAP_LOCAL_ERROR; - } -} /* The WinSock DLL is acceptable. Proceed. */ - -#elif HAVE_WINSOCK -{ WSADATA wsaData; - if ( WSAStartup( 0x0101, &wsaData ) != 0 ) { - return LDAP_LOCAL_ERROR; - } -} -#endif - if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) { - WSACleanup( ); return( LDAP_NO_MEMORY ); } @@ -160,14 +126,12 @@ ldap_create( LDAP **ldp ) if ( ld->ld_options.ldo_defludp == NULL ) { LDAP_FREE( (char*)ld ); - WSACleanup( ); return LDAP_NO_MEMORY; } if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) { ldap_free_urllist( ld->ld_options.ldo_defludp ); LDAP_FREE( (char*) ld ); - WSACleanup( ); return LDAP_NO_MEMORY; } @@ -177,7 +141,6 @@ ldap_create( LDAP **ldp ) if ( ld->ld_sb == NULL ) { ldap_free_urllist( ld->ld_options.ldo_defludp ); LDAP_FREE( (char*) ld ); - WSACleanup( ); return LDAP_NO_MEMORY; } diff --git a/libraries/libldap/os-ip.c b/libraries/libldap/os-ip.c index 5bf1064f33..7e13ccd75c 100644 --- a/libraries/libldap/os-ip.c +++ b/libraries/libldap/os-ip.c @@ -130,6 +130,8 @@ ldap_int_prepare_socket(LDAP *ld, int s, int proto ) return 0; } +#ifndef HAVE_WINSOCK + #undef TRACE #define TRACE do { \ osip_debug(ld, \ @@ -174,9 +176,6 @@ ldap_pvt_is_socket_ready(LDAP *ld, int s) { /* XXX: needs to be replace with ber_stream_read() */ read(s, &ch, 1); -#ifdef HAVE_WINSOCK - ldap_pvt_set_errno( WSAGetLastError() ); -#endif TRACE; return -1; } @@ -187,6 +186,8 @@ ldap_pvt_is_socket_ready(LDAP *ld, int s) } #undef TRACE +#endif /* HAVE_WINSOCK */ + static int ldap_pvt_connect(LDAP *ld, ber_socket_t s, struct sockaddr *sin, socklen_t addrlen, @@ -250,15 +251,25 @@ ldap_pvt_connect(LDAP *ld, ber_socket_t s, #ifdef HAVE_WINSOCK /* This means the connection failed */ if ( FD_ISSET(s, &efds) ) { - ldap_pvt_set_errno(WSAECONNREFUSED); + int so_errno; + int dummy = sizeof(so_errno); + if ( getsockopt( s, SOL_SOCKET, SO_ERROR, + (char *) &so_errno, &dummy ) == AC_SOCKET_ERROR || !so_errno ) + { + /* impossible */ + so_errno = WSAGetLastError(); + } + ldap_pvt_set_errno(so_errno); osip_debug(ld, "ldap_pvt_connect: error on socket %d: " "errno: %d (%s)\n", s, errno, sock_errstr(errno)); return -1; } #endif if ( FD_ISSET(s, &wfds) ) { +#ifndef HAVE_WINSOCK if ( ldap_pvt_is_socket_ready(ld, s) == -1 ) return ( -1 ); +#endif if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return ( -1 ); return ( 0 );