]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/cldap.c
LDAP C-API changes
[openldap] / libraries / libldap / cldap.c
index 19782f77d898a18ac23f97c874b055fcf393ea1e..1ed8693a15b31a192e48c43dde9774a9318660ee 100644 (file)
@@ -5,42 +5,28 @@
  *  cldap.c - synchronous, retrying interface to the cldap protocol
  */
 
+#include "portable.h"
 
-#ifdef CLDAP
+#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 <string.h>
-#include <errno.h>
-#ifdef MACOS
 #include <stdlib.h>
-#include "macos.h"
-#else /* MACOS */
-#ifdef DOS
-#include "msdos.h"
-#else /* DOS */
-#include <sys/time.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <netdb.h>
-#endif /* DOS */
-#endif /* MACOS */
-
-#include "lber.h"
-#include "ldap.h"
+
+#include <ac/errno.h>
+#include <ac/socket.h>
+#include <ac/string.h>
+#include <ac/time.h>
+#include <ac/unistd.h>
+
 #include "ldap-int.h"
 
 #define DEF_CLDAP_TIMEOUT      3
 #define DEF_CLDAP_TRIES                4
 
-#ifndef INADDR_LOOPBACK
-#define INADDR_LOOPBACK        ((unsigned long) 0x7f000001)
-#endif
-
 
 struct cldap_retinfo {
        int             cri_maxtries;
@@ -49,17 +35,14 @@ struct cldap_retinfo {
        long            cri_timeout;
 };
 
-#ifdef NEEDPROTOS
-static int add_addr( LDAP *ld, struct sockaddr *sap );
-static int cldap_result( LDAP *ld, int msgid, LDAPMessage **res,
-       struct cldap_retinfo *crip, char *base );
-static int cldap_parsemsg( LDAP *ld, int msgid, BerElement *ber,
-       LDAPMessage **res, char *base );
-#else /* NEEDPROTOS */
-static int add_addr();
-static int cldap_result();
-static int cldap_parsemsg();
-#endif /* NEEDPROTOS */
+static int add_addr LDAP_P((
+       LDAP *ld, struct sockaddr *sap ));
+static int cldap_result LDAP_P((
+       LDAP *ld, int msgid, LDAPMessage **res,
+       struct cldap_retinfo *crip, char *base ));
+static int cldap_parsemsg LDAP_P((
+       LDAP *ld, int msgid, BerElement *ber,
+       LDAPMessage **res, char *base ));
 
 /*
  * cldap_open - initialize and connect to an ldap server.  A magic cookie to
@@ -81,11 +64,14 @@ cldap_open( char *host, int port )
     char               *p;
     int                        i;
 
-    Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
+    /* buffers for ldap_int_gethostbyname_a ... */
+    struct hostent      he_buf;
+    int                 local_h_errno;
+    char               *ha_buf=NULL;
 
-    if ( port == 0 ) {
-           port = LDAP_PORT;
-    }
+#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 ) {
        return( NULL );
@@ -105,8 +91,8 @@ cldap_open( char *host, int port )
     }
     if ( (ld->ld_sb.sb_fromaddr = (void *) calloc( 1,
            sizeof( struct sockaddr ))) == NULL ) {
-       free( ld );
-       close( s );
+
+       ldap_ld_free(ld, 1);
        return( NULL );
     }  
     ld->ld_sb.sb_sd = s;
@@ -127,10 +113,15 @@ cldap_open( char *host, int port )
                }
            }
 
-           if ( (address = inet_addr( host )) == -1 ) {
-               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_int_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 ) {
@@ -138,43 +129,39 @@ cldap_open( char *host, int port )
                            (char *)hp->h_addr_list[ i ],
                            sizeof(sock.sin_addr.s_addr));
                    if ( add_addr( ld, (struct sockaddr *)&sock ) < 0 ) {
-                       close( s );
-                       free( ld );
-                       return( NULL );
+                       ldap_ld_free( ld, 1 );
+                       DO_RETURN( NULL );
                    }
                }
 
            } else {
                sock.sin_addr.s_addr = address;
                if ( add_addr( ld, (struct sockaddr *)&sock ) < 0 ) {
-                   close( s );
-                   free( ld );
-                   return( NULL );
+                   ldap_ld_free( ld, 1 );
+                   DO_RETURN( NULL );
                }
            }
 
            if ( ld->ld_host == NULL ) {
-                   ld->ld_host = strdup( host );
+                   ld->ld_host = ldap_strdup( host );
            }
        }
-
     } else {
        address = INADDR_LOOPBACK;
        sock.sin_addr.s_addr = htonl( address );
        if ( add_addr( ld, (struct sockaddr *)&sock ) < 0 ) {
-           close( s );
-           free( ld );
-           return( NULL );
+           ldap_ld_free( ld, 1 );
+           DO_RETURN( NULL );
        }
     }
 
     if ( ld->ld_sb.sb_addrs == NULL
-#ifdef LDAP_REFERRALS
-           || ( ld->ld_defconn = new_connection( ld, NULL, 1,0,0 )) == NULL
-#endif /* LDAP_REFERRALS */
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
+           || ( ld->ld_defconn = ldap_new_connection( ld, NULL, 1,0,0 )) == NULL
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
            ) {
-       free( ld );
-       return( NULL );
+       ldap_ld_free( ld, 0 );
+       DO_RETURN( NULL );
     }
 
     ld->ld_sb.sb_useaddr = ld->ld_sb.sb_addrs[ 0 ];
@@ -189,10 +176,10 @@ cldap_open( char *host, int port )
     }
 #endif
 
-    return( ld );
+    DO_RETURN( ld );
 }
 
-
+#undef DO_RETURN
 
 void
 cldap_close( LDAP *ld )
@@ -240,7 +227,7 @@ cldap_search_s( LDAP *ld, char *base, int scope, char *filter, char **attrs,
                attrsonly )) == -1 ) {
                    return( ld->ld_errno );
            }
-#ifndef NO_CACHE
+#ifndef LDAP_NOCACHE
            if ( ld->ld_cache != NULL && ld->ld_responses != NULL ) {
                Debug( LDAP_DEBUG_TRACE, "cldap_search_s res from cache\n",
                        0, 0, 0 );
@@ -248,7 +235,7 @@ cldap_search_s( LDAP *ld, char *base, int scope, char *filter, char **attrs,
                ld->ld_responses = ld->ld_responses->lm_next;
                return( ldap_result2error( ld, *res, 0 ));
            }
-#endif /* NO_CACHE */
+#endif /* LDAP_NOCACHE */
            ret = cldap_result( ld, msgid, res, &cri, base );
        } while (ret == -1);
 
@@ -313,10 +300,10 @@ 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 );
-    ber_init( &ber, 0 );
-    set_ber_options( ld, &ber );
+           "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 );
 
     if ( cldap_getmsg( ld, &tv, &ber ) == -1 ) {
        ret = ld->ld_errno;
@@ -432,7 +419,7 @@ cldap_parsemsg( LDAP *ld, int msgid, BerElement *ber,
            tag != LBER_DEFAULT && rc != LDAP_SUCCESS;
            tag = ber_next_element( ber, &len, cookie )) {
        if (( ldm = (LDAPMessage *)calloc( 1, sizeof(LDAPMessage)))
-               == NULL || ( ldm->lm_ber = alloc_ber_with_options( ld ))
+               == NULL || ( ldm->lm_ber = ldap_alloc_ber_with_options( ld ))
                == NULLBER ) {
            rc = LDAP_NO_MEMORY;
            break;      /* return w/error*/
@@ -486,7 +473,7 @@ cldap_parsemsg( LDAP *ld, int msgid, BerElement *ber,
            bv = NULL;
                
        } 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 */
@@ -504,11 +491,11 @@ cldap_parsemsg( LDAP *ld, int msgid, BerElement *ber,
        }
 #endif /* LDAP_DEBUG */
 
-#ifndef NO_CACHE
+#ifndef LDAP_NOCACHE
            if ( ld->ld_cache != NULL ) {
-               add_result_to_cache( ld, ldm );
+               ldap_add_result_to_cache( ld, ldm );
            }
-#endif /* NO_CACHE */
+#endif /* LDAP_NOCACHE */
 
        if ( chain == NULL ) {
            chain = ldm;
@@ -534,4 +521,4 @@ cldap_parsemsg( LDAP *ld, int msgid, BerElement *ber,
     *res = chain;
     return(( *res == NULLMSG ) ? rc : ldap_result2error( ld, *res, 0 ));
 }
-#endif /* CLDAP */
+#endif /* LDAP_CONNECTIONLESS */