]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/cldap.c
Fix ldap_send_initial_request() to open connection if not already
[openldap] / libraries / libldap / cldap.c
index c12859b858ddf9dffe5f906441df2b2ba1eb20dc..d5ca787bd3eee1750ed26bed7a507060d4ff1b4d 100644 (file)
@@ -1,4 +1,8 @@
 /*
+ * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
+ * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+ */
+/*  Portions
  *  Copyright (c) 1990, 1994 Regents of the University of Michigan.
  *  All rights reserved.
  *
@@ -9,10 +13,6 @@
 
 #ifdef LDAP_CONNECTIONLESS
 
-#ifndef lint 
-static char copyright[] = "@(#) Copyright (c) 1990, 1994 Regents of the University of Michigan.\nAll rights reserved.\n";
-#endif
-
 #include <stdio.h>
 #include <stdlib.h>
 
@@ -20,6 +20,7 @@ static char copyright[] = "@(#) Copyright (c) 1990, 1994 Regents of the Universi
 #include <ac/socket.h>
 #include <ac/string.h>
 #include <ac/time.h>
+#include <ac/unistd.h>
 
 #include "ldap-int.h"
 
@@ -63,6 +64,13 @@ cldap_open( char *host, int port )
     char               *p;
     int                        i;
 
+    /* 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);
+   
     Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
 
     if ( (s = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 ) {
@@ -105,10 +113,15 @@ cldap_open( char *host, int port )
                }
            }
 
-           if ( (address = inet_addr( host )) == (unsigned long) -1L ) {
-               if ( (hp = gethostbyname( host )) == NULL ) {
-                   errno = EHOSTUNREACH;
-                   continue;
+           address = inet_addr( host );
+           /* 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_pvt_gethostbyname_a( host, &he_buf, &ha_buf,
+                                             &hp,&local_h_errno)<0) || 
+                   (hp==NULL)) {
+                  errno = EHOSTUNREACH;
+                  continue;
                }
 
                for ( i = 0; hp->h_addr_list[ i ] != 0; ++i ) {
@@ -117,7 +130,7 @@ cldap_open( char *host, int port )
                            sizeof(sock.sin_addr.s_addr));
                    if ( add_addr( ld, (struct sockaddr *)&sock ) < 0 ) {
                        ldap_ld_free( ld, 1 );
-                       return( NULL );
+                       DO_RETURN( NULL );
                    }
                }
 
@@ -125,7 +138,7 @@ cldap_open( char *host, int port )
                sock.sin_addr.s_addr = address;
                if ( add_addr( ld, (struct sockaddr *)&sock ) < 0 ) {
                    ldap_ld_free( ld, 1 );
-                   return( NULL );
+                   DO_RETURN( NULL );
                }
            }
 
@@ -133,13 +146,12 @@ cldap_open( char *host, int port )
                    ld->ld_host = strdup( host );
            }
        }
-
     } else {
        address = INADDR_LOOPBACK;
        sock.sin_addr.s_addr = htonl( address );
        if ( add_addr( ld, (struct sockaddr *)&sock ) < 0 ) {
            ldap_ld_free( ld, 1 );
-           return( NULL );
+           DO_RETURN( NULL );
        }
     }
 
@@ -149,7 +161,7 @@ cldap_open( char *host, int port )
 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
            ) {
        ldap_ld_free( ld, 0 );
-       return( NULL );
+       DO_RETURN( NULL );
     }
 
     ld->ld_sb.sb_useaddr = ld->ld_sb.sb_addrs[ 0 ];
@@ -164,10 +176,10 @@ cldap_open( char *host, int port )
     }
 #endif
 
-    return( ld );
+    DO_RETURN( ld );
 }
 
-
+#undef DO_RETURN
 
 void
 cldap_close( LDAP *ld )
@@ -288,8 +300,8 @@ cldap_result( LDAP *ld, int msgid, LDAPMessage **res,
     tv.tv_usec = 0;
 
     Debug( LDAP_DEBUG_TRACE,
-           "cldap_result waiting up to %d seconds for a response\n",
-           tv.tv_sec, 0, 0 );
+           "cldap_result waiting up to %ld seconds for a response\n",
+           (long) tv.tv_sec, 0, 0 );
     ber_init_w_nullc( &ber, 0 );
     ldap_set_ber_options( ld, &ber );
 
@@ -460,8 +472,11 @@ cldap_parsemsg( LDAP *ld, int msgid, BerElement *ber,
            ber_bvfree( bv );
            bv = NULL;
                
+#ifdef notyet
+       } else if ( tag == LDAP_RES_SEARCH_REFERENCE ) {
+#endif
        } else {
-           Debug( LDAP_DEBUG_TRACE, "cldap_parsemsg got unknown tag %d\n",
+           Debug( LDAP_DEBUG_TRACE, "cldap_parsemsg got unknown tag %lu\n",
                    tag, 0, 0 );
            rc = LDAP_PROTOCOL_ERROR;
            break;      /* return w/error */
@@ -475,7 +490,7 @@ cldap_parsemsg( LDAP *ld, int msgid, BerElement *ber,
        if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
            fprintf( stderr, "cldap_parsemsg add message id %d type %d:\n",
                    ldm->lm_msgid, ldm->lm_msgtype  );
-           ber_dump( ldm->lm_ber, 1 );
+           lber_log_dump( LDAP_DEBUG_BER, ldap_debug, ldm->lm_ber, 1 );
        }
 #endif /* LDAP_DEBUG */