]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/os-ip.c
Removed numerous memory leaks detected by Mark Meredith.
[openldap] / libraries / libldap / os-ip.c
index 6f1ef1603db125c249c72fc46397d94441e5df2c..b17e71a58fd78657f56154c5ff8152dd75ce43c2 100644 (file)
@@ -12,7 +12,8 @@
 #include "portable.h"
 
 #include <stdio.h>
-#include <stdlib.h>
+
+#include <ac/stdlib.h>
 
 #include <ac/errno.h>
 #include <ac/socket.h>
@@ -33,7 +34,7 @@
 #include "ldap-int.h"
 
 int
-ldap_connect_to_host( Sockbuf *sb, char *host, unsigned long address,
+ldap_connect_to_host( Sockbuf *sb, const char *host, unsigned long address,
        int port, int async )
 /*
  * if host == NULL, connect using address
@@ -43,21 +44,20 @@ ldap_connect_to_host( Sockbuf *sb, char *host, unsigned long address,
  * XXX async is not used yet!
  */
 {
-       int                     rc, i, s = 0;
+       int                     rc, i;
+       ber_socket_t s = AC_SOCKET_INVALID;
        int                     connected, use_hp;
        struct sockaddr_in      sin;
        struct hostent          *hp = NULL;
 #ifdef notyet
-#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
-       int                     status; /* for ioctl call */
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
+       ioctl_t                 status; /* for ioctl call */
 #endif /* notyet */
    
-       /* buffers for ldap_int_gethostbyname_a */
+       /* buffers for ldap_pvt_gethostbyname_a */
        struct hostent          he_buf;
        int                     local_h_errno;
        char                    *ha_buf=NULL;
-#define DO_RETURN(x) if (ha_buf) free(ha_buf); return (x);
+#define DO_RETURN(x) if (ha_buf) LDAP_FREE(ha_buf); return (x);
    
        Debug( LDAP_DEBUG_TRACE, "ldap_connect_to_host: %s:%d\n",
            ( host == NULL ) ? "(by address)" : host, (int) ntohs( (short) port ), 0 );
@@ -69,7 +69,7 @@ ldap_connect_to_host( Sockbuf *sb, char *host, unsigned long address,
            /* This was just a test for -1 until OSF1 let inet_addr return
               unsigned int, which is narrower than 'unsigned long address' */
            if ( address == 0xffffffff || address == (unsigned long) -1 ) {
-               if ( ( ldap_int_gethostbyname_a( host, &he_buf, &ha_buf,
+               if ( ( ldap_pvt_gethostbyname_a( host, &he_buf, &ha_buf,
                        &hp, &local_h_errno) < 0) || (hp==NULL))
                {
 #ifdef HAVE_WINSOCK
@@ -89,13 +89,11 @@ ldap_connect_to_host( Sockbuf *sb, char *host, unsigned long address,
                        DO_RETURN( -1 );
                }
 #ifdef notyet
-#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
                status = 1;
                if ( async && ioctl( s, FIONBIO, (caddr_t)&status ) == -1 ) {
                        Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
                            s, 0, 0 );
                }
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 #endif /* notyet */
                (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
                sin.sin_family = AF_INET;
@@ -114,7 +112,6 @@ ldap_connect_to_host( Sockbuf *sb, char *host, unsigned long address,
                        errno = WSAGetLastError();
 #endif
 #ifdef notyet
-#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 #ifdef EAGAIN
                        if ( errno == EINPROGRESS || errno == EAGAIN ) {
 #else /* EAGAIN */
@@ -125,7 +122,6 @@ ldap_connect_to_host( Sockbuf *sb, char *host, unsigned long address,
                                rc = -2;
                                break;
                        }
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 #endif /* notyet */
 
 #ifdef LDAP_DEBUG              
@@ -140,17 +136,16 @@ ldap_connect_to_host( Sockbuf *sb, char *host, unsigned long address,
                }
        }
 
-       sb->sb_sd = s;
+       ber_pvt_sb_set_desc( sb, s );           
 
        if ( connected ) {
+          
 #ifdef notyet
-#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
                status = 0;
                if ( !async && ioctl( s, FIONBIO, (caddr_t)&on ) == -1 ) {
                        Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
                            s, 0, 0 );
                }
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 #endif /* notyet */
 
                Debug( LDAP_DEBUG_TRACE, "sd %d connected to: %s\n",
@@ -158,8 +153,6 @@ ldap_connect_to_host( Sockbuf *sb, char *host, unsigned long address,
        }
 
        DO_RETURN( rc );
-       
-       
 }
    
 #undef DO_RETURN
@@ -168,11 +161,11 @@ ldap_connect_to_host( Sockbuf *sb, char *host, unsigned long address,
 void
 ldap_close_connection( Sockbuf *sb )
 {
-    tcp_close( sb->sb_sd );
+       ber_pvt_sb_close( sb );
 }
 
 
-#ifdef HAVE_KERBEROS
+#if defined( HAVE_KERBEROS ) || defined( HAVE_TLS )
 char *
 ldap_host_connected_to( Sockbuf *sb )
 {
@@ -185,11 +178,12 @@ ldap_host_connected_to( Sockbuf *sb )
        struct hostent          he_buf;
         int                    local_h_errno;
        char                    *ha_buf=NULL;
-#define DO_RETURN(x) if (ha_buf) free(ha_buf); return (x);
+#define DO_RETURN(x) if (ha_buf) LDAP_FREE(ha_buf); return (x);
    
        (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
        len = sizeof( sin );
-       if ( getpeername( sb->sb_sd, (struct sockaddr *)&sin, &len ) == -1 ) {
+
+       if ( getpeername( ber_pvt_sb_get_desc(sb), (struct sockaddr *)&sin, &len ) == -1 ) {
                return( NULL );
        }
 
@@ -198,13 +192,14 @@ ldap_host_connected_to( Sockbuf *sb )
         * this is necessary for kerberos to work right, since the official
         * hostname is used as the kerberos instance.
         */
-       if ((ldap_int_gethostbyaddr_a( (char *) &sin.sin_addr,
+       if ((ldap_pvt_gethostbyaddr_a( (char *) &sin.sin_addr,
                sizeof( sin.sin_addr ), 
                AF_INET, &he_buf, &ha_buf,
                &hp,&local_h_errno ) ==0 ) && (hp != NULL) )
        {
                if ( hp->h_name != NULL ) {
-                       DO_RETURN( ldap_strdup( hp->h_name ));
+                       char *host = LDAP_STRDUP( hp->h_name );   
+                       DO_RETURN( host );
                }
        }
 
@@ -212,10 +207,9 @@ ldap_host_connected_to( Sockbuf *sb )
 }
 #undef DO_RETURN   
    
-#endif /* HAVE_KERBEROS */
+#endif /* HAVE_KERBEROS || HAVE_TLS */
 
 
-#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
 /* for UNIX */
 struct selectinfo {
        fd_set  si_readfds;
@@ -231,8 +225,8 @@ ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
        struct selectinfo       *sip;
 
        sip = (struct selectinfo *)ld->ld_selectinfo;
-
-       if ( !FD_ISSET( sb->sb_sd, &sip->si_writefds )) {
+       
+       if ( !FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_writefds )) {
                FD_SET( (u_int) sb->sb_sd, &sip->si_writefds );
        }
 }
@@ -245,7 +239,7 @@ ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
 
        sip = (struct selectinfo *)ld->ld_selectinfo;
 
-       if ( !FD_ISSET( sb->sb_sd, &sip->si_readfds )) {
+       if ( !FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_readfds )) {
                FD_SET( (u_int) sb->sb_sd, &sip->si_readfds );
        }
 }
@@ -258,8 +252,8 @@ ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
 
        sip = (struct selectinfo *)ld->ld_selectinfo;
 
-       FD_CLR( (u_int) sb->sb_sd, &sip->si_writefds );
-       FD_CLR( (u_int) sb->sb_sd, &sip->si_readfds );
+       FD_CLR( (u_int) ber_pvt_sb_get_desc(sb), &sip->si_writefds );
+       FD_CLR( (u_int) ber_pvt_sb_get_desc(sb), &sip->si_readfds );
 }
 
 
@@ -270,7 +264,7 @@ ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
 
        sip = (struct selectinfo *)ld->ld_selectinfo;
 
-       return( FD_ISSET( sb->sb_sd, &sip->si_use_writefds ));
+       return( FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_use_writefds ));
 }
 
 
@@ -281,7 +275,7 @@ ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
 
        sip = (struct selectinfo *)ld->ld_selectinfo;
 
-       return( FD_ISSET( sb->sb_sd, &sip->si_use_readfds ));
+       return( FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_use_readfds ));
 }
 
 
@@ -290,7 +284,7 @@ ldap_new_select_info( void )
 {
        struct selectinfo       *sip;
 
-       if (( sip = (struct selectinfo *)calloc( 1,
+       if (( sip = (struct selectinfo *)LDAP_CALLOC( 1,
            sizeof( struct selectinfo ))) != NULL ) {
                FD_ZERO( &sip->si_readfds );
                FD_ZERO( &sip->si_writefds );
@@ -303,7 +297,7 @@ ldap_new_select_info( void )
 void
 ldap_free_select_info( void *sip )
 {
-       free( sip );
+       LDAP_FREE( sip );
 }
 
 
@@ -338,4 +332,3 @@ do_ldap_select( LDAP *ld, struct timeval *timeout )
        return( select( tblsize, &sip->si_use_readfds, &sip->si_use_writefds,
            NULL, timeout ));
 }
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */