3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2006 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
17 * All rights reserved.
19 /* Portions Copyright (C) The Internet Society (1997).
20 * ASN.1 fragments are from RFC 2251; see RFC for full legal notices.
24 * An abandon request looks like this:
25 * AbandonRequest ::= MessageID
32 #include <ac/stdlib.h>
34 #include <ac/socket.h>
35 #include <ac/string.h>
40 static int do_abandon LDAP_P((
45 LDAPControl **cctrls));
48 * ldap_abandon_ext - perform an ldap extended abandon operation.
52 * msgid The message id of the operation to abandon
53 * scntrls Server Controls
54 * ccntrls Client Controls
56 * ldap_abandon_ext returns a LDAP error code.
57 * (LDAP_SUCCESS if everything went ok)
60 * ldap_abandon_ext( ld, msgid, scntrls, ccntrls );
67 LDAPControl **cctrls )
70 Debug( LDAP_DEBUG_TRACE, "ldap_abandon_ext %d\n", msgid, 0, 0 );
72 /* check client controls */
74 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
76 rc = ldap_int_client_controls( ld, cctrls );
77 if( rc == LDAP_SUCCESS )
78 rc = do_abandon( ld, msgid, msgid, sctrls, cctrls );
81 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
88 * ldap_abandon - perform an ldap abandon operation. Parameters:
91 * msgid The message id of the operation to abandon
93 * ldap_abandon returns 0 if everything went ok, -1 otherwise.
96 * ldap_abandon( ld, msgid );
99 ldap_abandon( LDAP *ld, int msgid )
101 Debug( LDAP_DEBUG_TRACE, "ldap_abandon %d\n", msgid, 0, 0 );
102 return ldap_abandon_ext( ld, msgid, NULL, NULL ) == LDAP_SUCCESS
112 LDAPControl **sctrls,
113 LDAPControl **cctrls )
116 int i, err, sendabandon;
117 ber_int_t *old_abandon;
121 Debug( LDAP_DEBUG_TRACE, "do_abandon origid %d, msgid %d\n",
126 /* find the request that we are abandoning */
128 lr = ld->ld_requests;
129 while ( lr != NULL ) {
130 if ( lr->lr_msgid == msgid ) { /* this message */
134 if ( lr->lr_origid == msgid ) {/* child: abandon it */
135 (void)do_abandon( ld,
136 lr->lr_origid, lr->lr_msgid, sctrls, cctrls );
138 /* restart, as lr may now be dangling... */
146 if ( origid == msgid && lr->lr_parent != NULL ) {
147 /* don't let caller abandon child requests! */
148 ld->ld_errno = LDAP_PARAM_ERROR;
149 return( LDAP_PARAM_ERROR );
151 if ( lr->lr_status != LDAP_REQST_INPROGRESS ) {
152 /* no need to send abandon message */
157 /* ldap_msgdelete locks the res_mutex. Give up the req_mutex
158 * while we're in there.
160 #ifdef LDAP_R_COMPILE
161 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
163 err = ldap_msgdelete( ld, msgid );
164 #ifdef LDAP_R_COMPILE
165 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
168 ld->ld_errno = LDAP_SUCCESS;
172 /* fetch again the request that we are abandoning */
174 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
175 if ( lr->lr_msgid == msgid ) { /* this message */
183 if( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
186 ld->ld_errno = LDAP_SERVER_DOWN;
188 } else if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
189 /* BER element alocation failed */
191 ld->ld_errno = LDAP_NO_MEMORY;
195 * We already have the mutex in LDAP_R_COMPILE, so
196 * don't try to get it again.
197 * LDAP_NEXT_MSGID(ld, i);
199 i = ++(ld)->ld_msgid;
200 #ifdef LDAP_CONNECTIONLESS
201 if ( LDAP_IS_UDP(ld) ) {
202 err = ber_write( ber, ld->ld_options.ldo_peer,
203 sizeof(struct sockaddr), 0);
205 if ( LDAP_IS_UDP(ld) && ld->ld_options.ldo_version ==
208 char *dn = ld->ld_options.ldo_cldapdn;
210 err = ber_printf( ber, "{isti", /* '}' */
212 LDAP_REQ_ABANDON, msgid );
216 /* create a message to send */
217 err = ber_printf( ber, "{iti", /* '}' */
219 LDAP_REQ_ABANDON, msgid );
224 ld->ld_errno = LDAP_ENCODING_ERROR;
227 /* Put Server Controls */
228 if ( ldap_int_put_controls( ld, sctrls, ber )
235 err = ber_printf( ber, /*{*/ "N}" );
239 ld->ld_errno = LDAP_ENCODING_ERROR;
248 /* send the message */
250 assert( lr->lr_conn != NULL );
251 sb = lr->lr_conn->lconn_sb;
256 if ( ber_flush2( sb, ber, LBER_FLUSH_FREE_ALWAYS ) != 0 ) {
257 ld->ld_errno = LDAP_SERVER_DOWN;
267 if ( sendabandon || lr->lr_status == LDAP_REQST_WRITING ) {
268 ldap_free_connection( ld, lr->lr_conn, 0, 1 );
270 if ( origid == msgid ) {
271 ldap_free_request( ld, lr );
275 #ifdef LDAP_R_COMPILE
276 /* ld_abandoned is actually protected by the ld_res_mutex;
277 * give up the ld_req_mutex and get the other */
278 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
279 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
282 if ( ld->ld_abandoned != NULL ) {
283 for ( ; ld->ld_abandoned[i] != -1; i++ )
287 old_abandon = ld->ld_abandoned;
289 ld->ld_abandoned = (ber_int_t *) LDAP_REALLOC( (char *)
290 ld->ld_abandoned, (i + 2) * sizeof(ber_int_t) );
292 if ( ld->ld_abandoned == NULL ) {
293 ld->ld_abandoned = old_abandon;
294 ld->ld_errno = LDAP_NO_MEMORY;
298 ld->ld_abandoned[i] = msgid;
299 ld->ld_abandoned[i + 1] = -1;
302 ld->ld_errno = LDAP_SUCCESS;
306 #ifdef LDAP_R_COMPILE
307 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
308 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
310 return( ld->ld_errno );