]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/abandon.c
Renamed ppcontrol.c to ppolicy.c
[openldap] / libraries / libldap / abandon.c
index b2b2b83009c7133e2642bf83760c1efc8de74426..24880ab4f9c566e6067a84db1f5a836f944636da 100644 (file)
@@ -1,18 +1,35 @@
-/*
- * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
- */
-/*  Portions
- *  Copyright (c) 1990 Regents of the University of Michigan.
- *  All rights reserved.
+/* abandon.c */
+/* $OpenLDAP$ */
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 1998-2004 The OpenLDAP Foundation.
+ * All rights reserved.
  *
- *  abandon.c
+ * 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) The Internet Society (1997).
+ * ASN.1 fragments are from RFC 2251; see RFC for full legal notices.
+ */
+
+/*
+ * An abandon request looks like this:
+ *     AbandonRequest ::= MessageID
  */
 
 #include "portable.h"
 
 #include <stdio.h>
-#include <stdlib.h>
+
+#include <ac/stdlib.h>
 
 #include <ac/socket.h>
 #include <ac/string.h>
@@ -22,8 +39,8 @@
 
 static int do_abandon LDAP_P((
        LDAP *ld,
-       int origid,
-       int msgid,
+       ber_int_t origid,
+       ber_int_t msgid,
        LDAPControl **sctrls,
        LDAPControl **cctrls));
 
@@ -49,9 +66,25 @@ ldap_abandon_ext(
        LDAPControl **sctrls,
        LDAPControl **cctrls )
 {
+       int rc;
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, ARGS, "ldap_abandon_ext %d\n", msgid, 0, 0 );
+#else
        Debug( LDAP_DEBUG_TRACE, "ldap_abandon_ext %d\n", msgid, 0, 0 );
+#endif
 
-       return do_abandon( ld, msgid, msgid, sctrls, cctrls );
+       /* check client controls */
+#ifdef LDAP_R_COMPILE
+       ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
+#endif
+       rc = ldap_int_client_controls( ld, cctrls );
+       if( rc == LDAP_SUCCESS )
+               rc = do_abandon( ld, msgid, msgid, sctrls, cctrls );
+
+#ifdef LDAP_R_COMPILE
+       ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
+#endif
+       return rc;
 }
 
 
@@ -69,8 +102,12 @@ ldap_abandon_ext(
 int
 ldap_abandon( LDAP *ld, int msgid )
 {
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, ARGS, "ldap_abandon %d\n", msgid, 0, 0 );
+#else
        Debug( LDAP_DEBUG_TRACE, "ldap_abandon %d\n", msgid, 0, 0 );
-       return do_abandon( ld, msgid, msgid, NULL, NULL ) == LDAP_SUCCESS
+#endif
+       return ldap_abandon_ext( ld, msgid, NULL, NULL ) == LDAP_SUCCESS
                ? 0 : -1;
 }
 
@@ -78,29 +115,26 @@ ldap_abandon( LDAP *ld, int msgid )
 static int
 do_abandon(
        LDAP *ld,
-       int origid,
-       int msgid,
+       ber_int_t origid,
+       ber_int_t msgid,
        LDAPControl **sctrls,
        LDAPControl **cctrls)
 {
        BerElement      *ber;
        int             i, err, sendabandon;
+       ber_int_t *old_abandon;
        Sockbuf         *sb;
-#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
        LDAPRequest     *lr;
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
-
-       /*
-        * An abandon request looks like this:
-        *      AbandonRequest ::= MessageID
-        */
 
+#ifdef NEW_LOGGING
+       LDAP_LOG ( OPERATION, ARGS, "do_abandon %d, msgid %d\n", origid, msgid, 0 );
+#else
        Debug( LDAP_DEBUG_TRACE, "do_abandon origid %d, msgid %d\n",
                origid, msgid, 0 );
+#endif
 
        sendabandon = 1;
 
-#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
        /* find the request that we are abandoning */
        for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
                if ( lr->lr_msgid == msgid ) {  /* this message */
@@ -123,32 +157,60 @@ do_abandon(
                        sendabandon = 0;
                }
        }
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
-       if ( ldap_msgdelete( ld, msgid ) == 0 ) {
+/* ldap_msgdelete locks the res_mutex. Give up the req_mutex
+ * while we're in there.
+ */
+#ifdef LDAP_R_COMPILE
+       ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
+#endif
+       err = ldap_msgdelete( ld, msgid );
+#ifdef LDAP_R_COMPILE
+       ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
+#endif
+       if ( err == 0 ) {
                ld->ld_errno = LDAP_SUCCESS;
                return LDAP_SUCCESS;
        }
 
        err = 0;
        if ( sendabandon ) {
-               /* create a message to send */
-               if ( (ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
+               if( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
+                       /* not connected */
+                       err = -1;
+                       ld->ld_errno = LDAP_SERVER_DOWN;
+
+               } else if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
+                       /* BER element alocation failed */
                        err = -1;
                        ld->ld_errno = LDAP_NO_MEMORY;
 
                } else {
+       /*
+        * We already have the mutex in LDAP_R_COMPILE, so
+        * don't try to get it again.
+        *              LDAP_NEXT_MSGID(ld, i);
+        */
+                       i = ++(ld)->ld_msgid;
 #ifdef LDAP_CONNECTIONLESS
-                       if ( ld->ld_cldapnaddr > 0 ) {
-                               err = ber_printf( ber, "{isti", /* leave open '}' */
-                                   ++ld->ld_msgid, ld->ld_cldapdn,
-                                   LDAP_REQ_ABANDON, msgid );
+                       if ( LDAP_IS_UDP(ld) ) {
+                           err = ber_write( ber, ld->ld_options.ldo_peer,
+                               sizeof(struct sockaddr), 0);
+                       }
+                       if ( LDAP_IS_UDP(ld) && ld->ld_options.ldo_version ==
+                               LDAP_VERSION2) {
+                           char *dn = ld->ld_options.ldo_cldapdn;
+                           if (!dn) dn = "";
+                           err = ber_printf( ber, "{isti",  /* '}' */
+                               i, dn,
+                               LDAP_REQ_ABANDON, msgid );
                        } else
-#endif /* LDAP_CONNECTIONLESS */
+#endif
                        {
-                               err = ber_printf( ber, "{iti",  /* leave open '}' */
-                                       ++ld->ld_msgid,
-                                   LDAP_REQ_ABANDON, msgid );
+                           /* create a message to send */
+                           err = ber_printf( ber, "{iti",  /* '}' */
+                               i,
+                               LDAP_REQ_ABANDON, msgid );
                        }
 
                        if( err == -1 ) {
@@ -164,7 +226,7 @@ do_abandon(
 
                                } else {
                                        /* close '{' */
-                                       err = ber_printf( ber, "}" );
+                                       err = ber_printf( ber, /*{*/ "N}" );
 
                                        if( err == -1 ) {
                                                /* encoding error */
@@ -178,13 +240,10 @@ do_abandon(
 
                        } else {
                                /* send the message */
-#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
                                if ( lr != NULL ) {
                                        sb = lr->lr_conn->lconn_sb;
-                               } else
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
-                               {
-                                       sb = &ld->ld_sb;
+                               } else {
+                                       sb = ld->ld_sb;
                                }
 
                                if ( ber_flush( sb, ber, 1 ) != 0 ) {
@@ -197,34 +256,32 @@ do_abandon(
                }
        }
 
-#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
        if ( lr != NULL ) {
-               if ( sendabandon ) {
+               if ( sendabandon || lr->lr_status == LDAP_REQST_WRITING ) {
                        ldap_free_connection( ld, lr->lr_conn, 0, 1 );
                }
                if ( origid == msgid ) {
                        ldap_free_request( ld, lr );
                }
        }
-#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
 
+       i = 0;
+       if ( ld->ld_abandoned != NULL ) {
+               for ( ; ld->ld_abandoned[i] != -1; i++ )
+                       ;       /* NULL */
+       }
+
+       old_abandon = ld->ld_abandoned;
 
+       ld->ld_abandoned = (ber_int_t *) LDAP_REALLOC( (char *)
+               ld->ld_abandoned, (i + 2) * sizeof(ber_int_t) );
+               
        if ( ld->ld_abandoned == NULL ) {
-               if ( (ld->ld_abandoned = (int *) malloc( 2 * sizeof(int) ))
-                   == NULL ) {
-                       ld->ld_errno = LDAP_NO_MEMORY;
-                       return( ld->ld_errno );
-               }
-               i = 0;
-       } else {
-               for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
-                       ;       /* NULL */
-               if ( (ld->ld_abandoned = (int *) realloc( (char *)
-                   ld->ld_abandoned, (i + 2) * sizeof(int) )) == NULL ) {
-                       ld->ld_errno = LDAP_NO_MEMORY;
-                       return( ld->ld_errno );
-               }
+               ld->ld_abandoned = old_abandon;
+               ld->ld_errno = LDAP_NO_MEMORY;
+               return( ld->ld_errno );
        }
+
        ld->ld_abandoned[i] = msgid;
        ld->ld_abandoned[i + 1] = -1;