]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/result.c
NT ldap_pvt_thread_self fix from HEAD
[openldap] / libraries / libldap / result.c
index f6d62b64ef56ddafa071b24798d8ba516c446c04..0614f41be9b839e20db8db7b033c24ca1c16a851 100644 (file)
@@ -1,14 +1,22 @@
+/* result.c - wait for an ldap result */
 /* $OpenLDAP$ */
-/*
- * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 1998-2004 The OpenLDAP Foundation.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
  */
-/*  Portions
- *  Copyright (c) 1990 Regents of the University of Michigan.
- *  All rights reserved.
+/* Portions Copyright (c) 1990 Regents of the University of Michigan.
+ * All rights reserved.
  */
-/*---
- * This notice applies to changes, created by or for Novell, Inc.,
+/* This notice applies to changes, created by or for Novell, Inc.,
  * to preexisting works for which notices appear elsewhere in this file.
  *
  * Copyright (C) 1999, 2000 Novell, Inc. All Rights Reserved.
  *---
  * Modification to OpenLDAP source by Novell, Inc.
  * April 2000 sfs Add code to process V3 referrals and search results
- *
- *  result.c - wait for an ldap result
- */
-/* Note: A verbatim copy of version 2.0.1 of the OpenLDAP Public License 
+ *---
+ * Note: A verbatim copy of version 2.0.1 of the OpenLDAP Public License 
  * can be found in the file "build/LICENSE-2.0.1" in this distribution
  * of OpenLDAP Software.
  */
+/* Portions Copyright (C) The Internet Society (1997)
+ * ASN.1 fragments are from RFC 2251; see RFC for full legal notices.
+ */
 
 /*
  * LDAPv3 (RFC2251)
@@ -97,6 +106,7 @@ ldap_result(
        LDAPMessage **result )
 {
        LDAPMessage     *lm;
+       int     rc;
 
        assert( ld != NULL );
        assert( result != NULL );
@@ -107,15 +117,22 @@ ldap_result(
        Debug( LDAP_DEBUG_TRACE, "ldap_result msgid %d\n", msgid, 0, 0 );
 #endif
 
-    lm = chkResponseList(ld, msgid, all);
+#ifdef LDAP_R_COMPILE
+       ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
+#endif
+       lm = chkResponseList(ld, msgid, all);
 
        if ( lm == NULL ) {
-               return( wait4msg( ld, msgid, all, timeout, result ) );
+               rc = wait4msg( ld, msgid, all, timeout, result );
+       } else {
+               *result = lm;
+               ld->ld_errno = LDAP_SUCCESS;
+               rc = lm->lm_msgtype;
        }
-
-       *result = lm;
-       ld->ld_errno = LDAP_SUCCESS;
-       return( lm->lm_msgtype );
+#ifdef LDAP_R_COMPILE
+       ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
+#endif
+       return( rc );
 }
 
 static LDAPMessage *
@@ -177,7 +194,7 @@ chkResponseList(
                        for ( tmp = lm; tmp != NULL; tmp = tmp->lm_chain ) {
                                if ( tmp->lm_msgtype != LDAP_RES_SEARCH_ENTRY
                                    && tmp->lm_msgtype != LDAP_RES_SEARCH_REFERENCE
-                                       && tmp->lm_msgtype != LDAP_RES_EXTENDED_PARTIAL )
+                                       && tmp->lm_msgtype != LDAP_RES_INTERMEDIATE )
                                {
                                        break;
                                }
@@ -309,8 +326,6 @@ wait4msg(
 
                    if ( lc == NULL ) {
                            rc = ldap_int_select( ld, tvp );
-
-
 #ifdef LDAP_DEBUG
                            if ( rc == -1 ) {
 #ifdef NEW_LOGGING
@@ -338,12 +353,18 @@ wait4msg(
                                    rc = -2;    /* select interrupted: loop */
                            } else {
                                    rc = -2;
+#ifdef LDAP_R_COMPILE
+                                   ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
+#endif
                                    if ( ld->ld_requests &&
                                                ld->ld_requests->lr_status == LDAP_REQST_WRITING &&
                                                ldap_is_write_ready( ld,
                                                        ld->ld_requests->lr_conn->lconn_sb ) ) {
                                                ldap_int_flush_request( ld, ld->ld_requests );
                                        }
+#ifdef LDAP_R_COMPILE
+                                   ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
+#endif
                                    for ( lc = ld->ld_conns; rc == -2 && lc != NULL;
                                        lc = nextlc ) {
                                            nextlc = lc->lconn_next;
@@ -401,6 +422,9 @@ try_read1msg(
        BerElement      tmpber;
        int             rc, refer_cnt, hadref, simple_request;
        ber_int_t       lderr;
+#ifdef LDAP_CONNECTIONLESS
+       int             firstmsg = 1, moremsgs = 0, isv2 = 0;
+#endif
        /*
         * v3ref = flag for V3 referral / search reference
         * 0 = not a ref, 1 = sucessfully chased ref, -1 = pass ref to application
@@ -417,28 +441,36 @@ try_read1msg(
 #endif
 
 retry:
-    if ( lc->lconn_ber == NULL ) {
+       if ( lc->lconn_ber == NULL ) {
                lc->lconn_ber = ldap_alloc_ber_with_options(ld);
 
                if( lc->lconn_ber == NULL ) {
                        return -1;
                }
-    }
+       }
 
        ber = lc->lconn_ber;
        assert( LBER_VALID (ber) );
 
-retry2:
        /* get the next message */
        errno = 0;
 #ifdef LDAP_CONNECTIONLESS
        if ( LDAP_IS_UDP(ld) ) {
                struct sockaddr from;
                ber_int_sb_read(sb, &from, sizeof(struct sockaddr));
+               if (ld->ld_options.ldo_version == LDAP_VERSION2) isv2=1;
        }
+nextresp3:
 #endif
-       if ( (tag = ber_get_next( sb, &len, ber ))
-           != LDAP_TAG_MESSAGE ) {
+       tag = ber_get_next( sb, &len, ber );
+       if ( tag == LDAP_TAG_MESSAGE ) {
+               /*
+                * We read a complete message.
+                * The connection should no longer need this ber.
+                */
+               lc->lconn_ber = NULL;
+       }
+       if ( tag != LDAP_TAG_MESSAGE ) {
                if ( tag == LBER_DEFAULT) {
 #ifdef LDAP_DEBUG                 
 #ifdef NEW_LOGGING
@@ -462,12 +494,6 @@ retry2:
                return -1;
        }
 
-       /*
-     * We read a complete message.
-        * The connection should no longer need this ber.
-        */
-    lc->lconn_ber = NULL;
-
        /* message id */
        if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
                ber_free( ber, 1 );
@@ -483,12 +509,10 @@ retry2:
                Debug( LDAP_DEBUG_ANY, "abandoned\n", 0, 0, 0);
 #endif
 retry_ber:
+               ber_free( ber, 1 );
                if ( ber_sockbuf_ctrl( sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
-                       ber_free_buf( ber );
-                       ber_init2( ber, NULL, ld->ld_lberoptions );
-                       goto retry2;
+                       goto retry;
                }
-               ber_free( ber, 1 );
                return( -2 );   /* continue looking */
        }
 
@@ -505,10 +529,10 @@ retry_ber:
                goto retry_ber;
        }
 #ifdef LDAP_CONNECTIONLESS
-       if (LDAP_IS_UDP(ld) && ld->ld_options.ldo_version == LDAP_VERSION2) {
-               struct berval blank;
-               ber_scanf(ber, "m{", &blank);
+       if (LDAP_IS_UDP(ld) && isv2) {
+               ber_scanf(ber, "x{");
        }
+nextresp2:
 #endif
        /* the message type */
        if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
@@ -646,7 +670,8 @@ retry_ber:
         * and checks if all referrals have been chased.
         */
        if ( (tag != LDAP_RES_SEARCH_ENTRY) && (v3ref > -1) &&
-            (tag != LDAP_RES_INTERMEDIATE_RESP ) ) {
+               (tag != LDAP_RES_INTERMEDIATE ))
+       {
                /* For a v3 search referral/reference, only come here if already chased it */
                if ( ld->ld_version >= LDAP_VERSION2 &&
                        ( lr->lr_parent != NULL ||
@@ -808,11 +833,84 @@ lr->lr_res_matched ? lr->lr_res_matched : "" );
        new->lm_msgtype = tag;
        new->lm_ber = ber;
 
-#ifndef LDAP_NOCACHE
-               if ( ld->ld_cache != NULL ) {
-                       ldap_add_result_to_cache( ld, new );
+#ifdef LDAP_CONNECTIONLESS
+       /* CLDAP replies all fit in a single datagram. In LDAPv2 RFC1798
+        * the responses are all a sequence wrapped in one message. In
+        * LDAPv3 each response is in its own message. The datagram must
+        * end with a SearchResult. We can't just parse each response in
+        * separate calls to try_read1msg because the header info is only
+        * present at the beginning of the datagram, not at the beginning
+        * of each response. So parse all the responses at once and queue
+        * them up, then pull off the first response to return to the
+        * caller when all parsing is complete.
+        */
+       if ( LDAP_IS_UDP(ld) ) {
+               /* If not a result, look for more */
+               if ( tag != LDAP_RES_SEARCH_RESULT ) {
+                       int ok = 0;
+                       moremsgs = 1;
+                       if (isv2) {
+                               /* LDAPv2: dup the current ber, skip past the current
+                                * response, and see if there are any more after it.
+                                */
+                               ber = ber_dup( ber );
+                               ber_scanf( ber, "x" );
+                               if (ber_peek_tag(ber, &len) != LBER_DEFAULT) {
+                                       /* There's more - dup the ber buffer so they can all be
+                                        * individually freed by ldap_msgfree.
+                                        */
+                                       struct berval bv;
+                                       ber_get_option(ber, LBER_OPT_BER_REMAINING_BYTES, &len);
+                                       bv.bv_val = LDAP_MALLOC(len);
+                                       if (bv.bv_val) {
+                                               ok=1;
+                                               ber_read(ber, bv.bv_val, len);
+                                               bv.bv_len = len;
+                                               ber_init2(ber, &bv, ld->ld_lberoptions );
+                                       }
+                               }
+                       } else {
+                               /* LDAPv3: Just allocate a new ber. Since this is a buffered
+                                * datagram, if the sockbuf is readable we still have data
+                                * to parse.
+                                */
+                               ber = ldap_alloc_ber_with_options(ld);
+                               if (ber_sockbuf_ctrl(sb, LBER_SB_OPT_DATA_READY, NULL)) ok=1;
+                       }
+                       /* set up response chain */
+                       if ( firstmsg ) {
+                               firstmsg = 0;
+                               new->lm_next = ld->ld_responses;
+                               ld->ld_responses = new;
+                       } else {
+                               tmp->lm_chain = new;
+                       }
+                       tmp = new;
+                       /* "ok" means there's more to parse */
+                       if (ok) {
+                               if (isv2) goto nextresp2;
+                               else goto nextresp3;
+                       } else {
+                               /* got to end of datagram without a SearchResult. Free
+                                * our dup'd ber, but leave any buffer alone. For v2 case,
+                                * the previous response is still using this buffer. For v3,
+                                * the new ber has no buffer to free yet.
+                                */
+                               ber_free(ber, 0);
+                               return -1;
+                       }
+               } else if ( moremsgs ) {
+               /* got search result, and we had multiple responses in 1 datagram.
+                * stick the result onto the end of the chain, and then pull the
+                * first response off the head of the chain.
+                */
+                       tmp->lm_chain = new;
+                       *result = chkResponseList( ld, msgid, all );
+                       ld->ld_errno = LDAP_SUCCESS;
+                       return( (*result)->lm_msgtype );
                }
-#endif /* LDAP_NOCACHE */
+       }
+#endif
 
        /* is this the one we're looking for? */
        if ( msgid == LDAP_RES_ANY || id == msgid ) {
@@ -845,13 +943,12 @@ lr->lr_res_matched ? lr->lr_res_matched : "" );
        if ( l == NULL ) {
                if ( foundit ) {
                        *result = new;
-                       ld->ld_errno = LDAP_SUCCESS;
-                       return( tag );
+                       goto exit;
                }
 
                new->lm_next = ld->ld_responses;
                ld->ld_responses = new;
-               goto leave;
+               goto exit;
        }
 
 #ifdef NEW_LOGGING
@@ -867,7 +964,7 @@ lr->lr_res_matched ? lr->lr_res_matched : "" );
        for ( tmp = l; (tmp->lm_chain != NULL) &&
                ((tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY) ||
                 (tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_REFERENCE) ||
-                        (tmp->lm_chain->lm_msgtype == LDAP_RES_EXTENDED_PARTIAL ));
+                        (tmp->lm_chain->lm_msgtype == LDAP_RES_INTERMEDIATE ));
            tmp = tmp->lm_chain )
                ;       /* NULL */
        tmp->lm_chain = new;
@@ -879,22 +976,13 @@ lr->lr_res_matched ? lr->lr_res_matched : "" );
                else
                        prev->lm_next = l->lm_next;
                *result = l;
+       }
+
+exit:
+       if ( foundit ) {
                ld->ld_errno = LDAP_SUCCESS;
-#ifdef LDAP_WORLD_P16
-               /*
-                * XXX questionable fix; see text for [P16] on
-                * http://www.critical-angle.com/ldapworld/patch/
-                *
-                * inclusion of this patch causes searchs to hang on
-                * multiple platforms
-                */
-               return( l->lm_msgtype );
-#else  /* LDAP_WORLD_P16 */
                return( tag );
-#endif /* !LDAP_WORLD_P16 */
        }
-
-leave:
        if ( ber_sockbuf_ctrl( sb, LBER_SB_OPT_DATA_READY, NULL ) ) {
                goto retry;
        }
@@ -1028,7 +1116,7 @@ char * ldap_int_msgtype2str( ber_tag_t tag )
        case LDAP_RES_COMPARE: return "compare";
        case LDAP_RES_DELETE: return "delete";
        case LDAP_RES_EXTENDED: return "extended-result";
-       case LDAP_RES_EXTENDED_PARTIAL: return "extended-partial";
+       case LDAP_RES_INTERMEDIATE: return "intermediate";
        case LDAP_RES_MODIFY: return "modify";
        case LDAP_RES_RENAME: return "rename";
        case LDAP_RES_SEARCH_ENTRY: return "search-entry";
@@ -1069,6 +1157,7 @@ int
 ldap_msgdelete( LDAP *ld, int msgid )
 {
        LDAPMessage     *lm, *prev;
+       int rc = 0;
 
        assert( ld != NULL );
 
@@ -1079,24 +1168,30 @@ ldap_msgdelete( LDAP *ld, int msgid )
 #endif
 
        prev = NULL;
+#ifdef LDAP_R_COMPILE
+       ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
+#endif
        for ( lm = ld->ld_responses; lm != NULL; lm = lm->lm_next ) {
                if ( lm->lm_msgid == msgid )
                        break;
                prev = lm;
        }
 
-       if ( lm == NULL )
-               return( -1 );
-
-       if ( prev == NULL )
-               ld->ld_responses = lm->lm_next;
-       else
-               prev->lm_next = lm->lm_next;
-
-       if ( ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
-               return( -1 );
+       if ( lm == NULL ) {
+               rc = -1;
+       } else {
+               if ( prev == NULL )
+                       ld->ld_responses = lm->lm_next;
+               else
+                       prev->lm_next = lm->lm_next;
+       }
+#ifdef LDAP_R_COMPILE
+       ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
+#endif
+       if ( lm && ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
+               rc = -1;
 
-       return( 0 );
+       return( rc );
 }