]> git.ipfire.org Git - thirdparty/openldap.git/commitdiff
ldap_connect_to_host portability fix...
authorKurt Zeilenga <kurt@openldap.org>
Tue, 29 Apr 2003 01:41:54 +0000 (01:41 +0000)
committerKurt Zeilenga <kurt@openldap.org>
Tue, 29 Apr 2003 01:41:54 +0000 (01:41 +0000)
CHANGES
libraries/libldap/ldap-int.h
libraries/libldap/open.c
libraries/libldap/os-ip.c

diff --git a/CHANGES b/CHANGES
index c258a4e7fa1229ce694b00287668adc4c4a60a18..58606be69b262acc0bbf66afa4d6eba51072e67a 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -8,7 +8,9 @@ OpenLDAP 2.1.18 Engineering
        Fixed slapd ACL group DN crash (ITS#2467)
        Fixed slapd substring normalization bug (ITS#2468)
        Fixed back-bdb search filter empty value bug (ITS#2453)
+       Fixed back-bdb filter index computed/undefined bugs (ITS#2449)
        Fixed SASL interactive free bug (ITS#2423)
+       Fixed libldap ldap_host_connected_to portability bug (ITS#2459)
        Fixed liblber multi-value decode bug (ITS#2458)
        Fixed liblber over read bug (ITS#2465)
        Fixed ldappasswd -y support (ITS#2441)
index a769f287af49bc586b475b2ef6f57db525b1b842..57ee4fcf6acf8d44fa6a287d72062a126f9cc898 100644 (file)
@@ -413,8 +413,7 @@ LDAP_F (int) ldap_int_open_connection( LDAP *ld,
 LDAP_V (int) ldap_int_tblsize;
 LDAP_F (int) ldap_int_timeval_dup( struct timeval **dest, const struct timeval *tm );
 LDAP_F (int) ldap_connect_to_host( LDAP *ld, Sockbuf *sb,
-       int proto, const char *host, unsigned long address, int port,
-       int async );
+       int proto, const char *host, int port, int async );
 
 #if defined(LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND) || \
        defined(HAVE_TLS) || defined(HAVE_CYRUS_SASL)
index fba85908fd5e7875d2507ee8b458b12def520b07..e4839d91427fa89d4a4ddf2ff26a6a774b533705 100644 (file)
@@ -238,7 +238,6 @@ ldap_int_open_connection(
 #endif
        char *host;
        int port, proto;
-       long addr;
 
 #ifdef NEW_LOGGING
        LDAP_LOG ( CONNECTION, ENTRY, "ldap_int_open_connection\n", 0, 0, 0 );
@@ -250,10 +249,8 @@ ldap_int_open_connection(
                case LDAP_PROTO_TCP:
                        port = srv->lud_port;
 
-                       addr = 0;
                        if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
                                host = NULL;
-                               addr = htonl( INADDR_LOOPBACK );
                        } else {
                                host = srv->lud_host;
                        }
@@ -267,7 +264,7 @@ ldap_int_open_connection(
                        }
 
                        rc = ldap_connect_to_host( ld, conn->lconn_sb,
-                               proto, host, addr, port, async );
+                               proto, host, port, async );
 
                        if ( rc == -1 ) return rc;
 
@@ -287,10 +284,8 @@ ldap_int_open_connection(
                case LDAP_PROTO_UDP:
                        port = srv->lud_port;
 
-                       addr = 0;
                        if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
                                host = NULL;
-                               addr = htonl( INADDR_LOOPBACK );
                        } else {
                                host = srv->lud_host;
                        }
@@ -299,7 +294,7 @@ ldap_int_open_connection(
 
                        LDAP_IS_UDP(ld) = 1;
                        rc = ldap_connect_to_host( ld, conn->lconn_sb,
-                               proto, host, addr, port, async );
+                               proto, host, port, async );
 
                        if ( rc == -1 ) return rc;
 #ifdef LDAP_DEBUG
index 738e97f930ed903ef16a9dce65a04476d669ce7e..ccd24b6db7348f03b764d9b045c8cfce3cb3d3bf 100644 (file)
@@ -322,82 +322,88 @@ ldap_pvt_inet_aton( const char *host, struct in_addr *in)
 int
 ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
        int proto,
-       const char *host,
-       unsigned long address, int port, int async )
+       const char *host, int port,
+       int async )
 {
+       int     rc;
+       int     socktype;
        ber_socket_t            s = AC_SOCKET_INVALID;
-       int                     rc, i, use_hp = 0;
-       struct hostent          *hp = NULL;
-#if !defined( HAVE_GETADDRINFO ) || !defined( HAVE_INET_NTOP )
+
+#if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
+       char serv[7];
+       int err;
+       struct addrinfo hints, *res, *sai;
+#else
+       int i;
+       int use_hp = 0;
+       struct hostent *hp = NULL;
        struct hostent he_buf;
+       struct in_addr in;
+       char *ha_buf=NULL;
 #endif
-       char                    *ha_buf=NULL, *p, *q;
-       int                     socktype;
 
+       if( host == NULL ) host = "localhost";
        
        switch(proto) {
        case LDAP_PROTO_TCP: socktype = SOCK_STREAM;
-               osip_debug(ld, "ldap_connect_to_host: TCP %s:%d\n",host,port,0);
+               osip_debug( ld,
+                       "ldap_connect_to_host: TCP %s:%d\n",
+                       host, port, 0);
                break;
        case LDAP_PROTO_UDP: socktype = SOCK_DGRAM;
-               osip_debug(ld, "ldap_connect_to_host: UDP %s:%d\n",host,port,0);
+               osip_debug( ld,
+                       "ldap_connect_to_host: UDP %s:%d\n",
+                       host, port, 0);
                break;
-
        default:
-               osip_debug(ld, "ldap_connect_to_host: unknown proto: %d\n",
-                       proto, 0, 0);
+               osip_debug( ld, "ldap_connect_to_host: unknown proto: %d\n",
+                       proto, 0, 0 );
                return -1;
        }
 
-       if (host != NULL) {
 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
-               char serv[7];
-               int err;
-               struct addrinfo hints, *res, *sai;
-
-               memset( &hints, '\0', sizeof(hints) );
-               hints.ai_family = ldap_int_inet4or6;
-               hints.ai_socktype = socktype;
-
-               snprintf(serv, sizeof serv, "%d", port );
+       memset( &hints, '\0', sizeof(hints) );
+       hints.ai_family = ldap_int_inet4or6;
+       hints.ai_socktype = socktype;
+       snprintf(serv, sizeof serv, "%d", port );
 
 #ifdef LDAP_R_COMPILE
-               /* most getaddrinfo(3) use non-threadsafe resolver libraries */
-               ldap_pvt_thread_mutex_lock(&ldap_int_resolv_mutex);
+       /* most getaddrinfo(3) use non-threadsafe resolver libraries */
+       ldap_pvt_thread_mutex_lock(&ldap_int_resolv_mutex);
 #endif
 
-               err = getaddrinfo( host, serv, &hints, &res );
+       err = getaddrinfo( host, serv, &hints, &res );
 
 #ifdef LDAP_R_COMPILE
-               ldap_pvt_thread_mutex_unlock(&ldap_int_resolv_mutex);
+       ldap_pvt_thread_mutex_unlock(&ldap_int_resolv_mutex);
 #endif
 
-               if ( err != 0 ) {
-                       osip_debug(ld, "ldap_connect_to_host: getaddrinfo failed: %s\n",
-                               AC_GAI_STRERROR(err), 0, 0);
-                       return -1;
-               }
-               rc = -1;
+       if ( err != 0 ) {
+               osip_debug(ld, "ldap_connect_to_host: getaddrinfo failed: %s\n",
+                       AC_GAI_STRERROR(err), 0, 0);
+               return -1;
+       }
+       rc = -1;
 
-               for( sai=res; sai != NULL; sai=sai->ai_next) {
-                       if( sai->ai_addr == NULL ) {
-                               osip_debug(ld, "ldap_connect_to_host: getaddrinfo "
-                                       "ai_addr is NULL?\n", 0, 0, 0);
-                               continue;
-                       }
+       for( sai=res; sai != NULL; sai=sai->ai_next) {
+               if( sai->ai_addr == NULL ) {
+                       osip_debug(ld, "ldap_connect_to_host: getaddrinfo "
+                               "ai_addr is NULL?\n", 0, 0, 0);
+                       continue;
+               }
 
-                       /* we assume AF_x and PF_x are equal for all x */
-                       s = ldap_int_socket( ld, sai->ai_family, socktype );
-                       if ( s == AC_SOCKET_INVALID ) {
-                               continue;
-                       }
+               /* we assume AF_x and PF_x are equal for all x */
+               s = ldap_int_socket( ld, sai->ai_family, socktype );
+               if ( s == AC_SOCKET_INVALID ) {
+                       continue;
+               }
 
-                       if ( ldap_int_prepare_socket(ld, s, proto ) == -1 ) {
-                               ldap_pvt_close_socket(ld, s);
-                               break;
-                       }
+               if ( ldap_int_prepare_socket(ld, s, proto ) == -1 ) {
+                       ldap_pvt_close_socket(ld, s);
+                       break;
+               }
 
-                       switch (sai->ai_family) {
+               switch (sai->ai_family) {
 #ifdef LDAP_PF_INET6
                        case AF_INET6: {
                                char addr[INET6_ADDRSTRLEN];
@@ -416,39 +422,36 @@ ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
                                osip_debug(ld, "ldap_connect_to_host: Trying %s:%s\n", 
                                        addr, serv, 0);
                        } break;
-                       }
+               }
 
-                       rc = ldap_pvt_connect(ld, s, sai->ai_addr, sai->ai_addrlen, async);
-                       if ( (rc == 0) || (rc == -2) ) {
-                               ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
-                               break;
-                       }
-                       ldap_pvt_close_socket(ld, s);
+               rc = ldap_pvt_connect( ld, s,
+                       sai->ai_addr, sai->ai_addrlen, async );
+               if ( (rc == 0) || (rc == -2) ) {
+                       ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
+                       break;
                }
-               freeaddrinfo(res);
-               return rc;
+               ldap_pvt_close_socket(ld, s);
+       }
+       freeaddrinfo(res);
 
 #else
-               struct in_addr in;
-               if (! inet_aton( host, &in) ) {
-                       int local_h_errno;
-                       rc = ldap_pvt_gethostbyname_a(host, &he_buf, &ha_buf,
-                                       &hp, &local_h_errno);
+       if (! inet_aton( host, &in ) ) {
+               int local_h_errno;
+               rc = ldap_pvt_gethostbyname_a( host, &he_buf, &ha_buf,
+                       &hp, &local_h_errno );
 
-                       if ( (rc < 0) || (hp == NULL) ) {
+               if ( (rc < 0) || (hp == NULL) ) {
 #ifdef HAVE_WINSOCK
-                               ldap_pvt_set_errno( WSAGetLastError() );
+                       ldap_pvt_set_errno( WSAGetLastError() );
 #else
-                               /* not exactly right, but... */
-                               ldap_pvt_set_errno( EHOSTUNREACH );
+                       /* not exactly right, but... */
+                       ldap_pvt_set_errno( EHOSTUNREACH );
 #endif
-                               if (ha_buf) LDAP_FREE(ha_buf);
-                               return -1;
-                       }
-                       use_hp = 1;
+                       if (ha_buf) LDAP_FREE(ha_buf);
+                       return -1;
                }
-               address = in.s_addr;
-#endif
+
+               use_hp = 1;
        }
 
        rc = s = -1;
@@ -469,15 +472,20 @@ ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
                (void)memset((char *)&sin, '\0', sizeof sin);
                sin.sin_family = AF_INET;
                sin.sin_port = htons((short) port);
-               p = (char *)&sin.sin_addr;
-               q = use_hp ? (char *)hp->h_addr_list[i] : (char *)&address;
-               AC_MEMCPY(p, q, sizeof(sin.sin_addr) );
+
+               if( use_hp ) {
+                       AC_MEMCPY( &sin.sin_addr, hp->h_addr_list[i],
+                               sizeof(sin.sin_addr) );
+               } else {
+                       AC_MEMCPY( &sin.sin_addr, &in.s_addr,
+                               sizeof(sin.sin_addr) );
+               }
 
                osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n", 
-                       inet_ntoa(sin.sin_addr),port,0);
+                       inet_ntoa(sin.sin_addr), port, 0);
 
                rc = ldap_pvt_connect(ld, s,
-                       (struct sockaddr *)&sin, sizeof(struct sockaddr_in),
+                       (struct sockaddr *)&sin, sizeof(sin),
                        async);
    
                if ( (rc == 0) || (rc == -2) ) {
@@ -487,10 +495,11 @@ ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
 
                ldap_pvt_close_socket(ld, s);
 
-               if (!use_hp)
-                       break;
+               if (!use_hp) break;
        }
        if (ha_buf) LDAP_FREE(ha_buf);
+#endif
+
        return rc;
 }