]> git.sur5r.net Git - openldap/commitdiff
ITS#2459 (partial) fix. Eliminate address parameter all together.
authorKurt Zeilenga <kurt@openldap.org>
Mon, 21 Apr 2003 17:06:37 +0000 (17:06 +0000)
committerKurt Zeilenga <kurt@openldap.org>
Mon, 21 Apr 2003 17:06:37 +0000 (17:06 +0000)
Need to look closely at the memcpy.  (It likely can be eliminated
as well.)

libraries/libldap/ldap-int.h
libraries/libldap/open.c
libraries/libldap/os-ip.c

index 9bb6a9b2ca9cba95e05136976635e05bbcdce1a5..2d63291ae78187001c1921e5be7499c60fbedf5b 100644 (file)
@@ -433,10 +433,10 @@ LDAP_F (int) ldap_int_open_connection( LDAP *ld,
  * in os-ip.c
  */
 LDAP_V (int) ldap_int_tblsize;
-LDAP_F (int) ldap_int_timeval_dup( struct timeval **dest, const struct timeval *tm );
+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 944e1f49700ea1b2dbb0ce5c9d7d7d4bb1676c43..ea51f91c0a55b49fff47e1c140c0a85b0a9749b5 100644 (file)
@@ -242,7 +242,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 );
@@ -254,10 +253,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;
                        }
@@ -271,7 +268,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;
 
@@ -291,10 +288,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;
                        }
@@ -303,7 +298,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..b5601ba25874c0f250cf6f2e1b123b3d04886518 100644 (file)
@@ -322,82 +322,85 @@ 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 )
 {
        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
        struct hostent he_buf;
+       struct in_addr in;
 #endif
-       char                    *ha_buf=NULL, *p, *q;
-       int                     socktype;
+       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 +419,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;
@@ -470,7 +470,7 @@ ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
                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;
+               q = use_hp ? (char *)hp->h_addr_list[i] : (char *)&in.s_addr;
                AC_MEMCPY(p, q, sizeof(sin.sin_addr) );
 
                osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n", 
@@ -491,6 +491,8 @@ ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
                        break;
        }
        if (ha_buf) LDAP_FREE(ha_buf);
+#endif
+
        return rc;
 }