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.
24 #include <ac/stdlib.h>
26 #include <ac/socket.h>
27 #include <ac/string.h>
33 * An abandon request looks like this:
34 * AbandonRequest ::= [APPLICATION 16] MessageID
35 * and has no response. (Source: RFC 4511)
47 * ldap_abandon_ext - perform an ldap extended abandon operation.
51 * msgid The message id of the operation to abandon
52 * scntrls Server Controls
53 * ccntrls Client Controls
55 * ldap_abandon_ext returns a LDAP error code.
56 * (LDAP_SUCCESS if everything went ok)
59 * ldap_abandon_ext( ld, msgid, scntrls, ccntrls );
66 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 );
77 rc = ldap_int_client_controls( ld, cctrls );
78 if ( rc == LDAP_SUCCESS ) {
79 rc = do_abandon( ld, msgid, msgid, sctrls, 1 );
83 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
91 * ldap_abandon - perform an ldap abandon operation. Parameters:
94 * msgid The message id of the operation to abandon
96 * ldap_abandon returns 0 if everything went ok, -1 otherwise.
99 * ldap_abandon( ld, msgid );
102 ldap_abandon( LDAP *ld, int msgid )
104 Debug( LDAP_DEBUG_TRACE, "ldap_abandon %d\n", msgid, 0, 0 );
105 return ldap_abandon_ext( ld, msgid, NULL, NULL ) == LDAP_SUCCESS
117 #ifdef LDAP_R_COMPILE
118 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
121 rc = do_abandon( ld, msgid, msgid, NULL, 0 );
123 #ifdef LDAP_R_COMPILE
124 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
135 LDAPControl **sctrls,
140 ber_int_t *old_abandon;
144 Debug( LDAP_DEBUG_TRACE, "do_abandon origid %d, msgid %d\n",
147 /* find the request that we are abandoning */
149 lr = ld->ld_requests;
150 while ( lr != NULL ) {
151 if ( lr->lr_msgid == msgid ) { /* this message */
155 if ( lr->lr_origid == msgid ) {/* child: abandon it */
156 (void)do_abandon( ld, lr->lr_origid, lr->lr_msgid,
157 sctrls, sendabandon );
159 /* restart, as lr may now be dangling... */
167 if ( origid == msgid && lr->lr_parent != NULL ) {
168 /* don't let caller abandon child requests! */
169 ld->ld_errno = LDAP_PARAM_ERROR;
170 return( LDAP_PARAM_ERROR );
172 if ( lr->lr_status != LDAP_REQST_INPROGRESS ) {
173 /* no need to send abandon message */
178 /* ldap_msgdelete locks the res_mutex. Give up the req_mutex
179 * while we're in there.
181 #ifdef LDAP_R_COMPILE
182 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
184 err = ldap_msgdelete( ld, msgid );
185 #ifdef LDAP_R_COMPILE
186 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
189 ld->ld_errno = LDAP_SUCCESS;
193 /* fetch again the request that we are abandoning */
195 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
197 if ( lr->lr_msgid == msgid ) {
205 if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
208 ld->ld_errno = LDAP_SERVER_DOWN;
210 } else if ( ( ber = ldap_alloc_ber_with_options( ld ) ) == NULL ) {
211 /* BER element allocation failed */
213 ld->ld_errno = LDAP_NO_MEMORY;
217 * We already have the mutex in LDAP_R_COMPILE, so
218 * don't try to get it again.
219 * LDAP_NEXT_MSGID(ld, i);
222 i = ++(ld)->ld_msgid;
223 #ifdef LDAP_CONNECTIONLESS
224 if ( LDAP_IS_UDP(ld) ) {
225 err = ber_write( ber, ld->ld_options.ldo_peer,
226 sizeof(struct sockaddr), 0);
228 if ( LDAP_IS_UDP(ld) && ld->ld_options.ldo_version ==
231 char *dn = ld->ld_options.ldo_cldapdn;
233 err = ber_printf( ber, "{isti", /* '}' */
235 LDAP_REQ_ABANDON, msgid );
239 /* create a message to send */
240 err = ber_printf( ber, "{iti", /* '}' */
242 LDAP_REQ_ABANDON, msgid );
247 ld->ld_errno = LDAP_ENCODING_ERROR;
250 /* Put Server Controls */
251 if ( ldap_int_put_controls( ld, sctrls, ber )
258 err = ber_printf( ber, /*{*/ "N}" );
262 ld->ld_errno = LDAP_ENCODING_ERROR;
271 /* send the message */
273 assert( lr->lr_conn != NULL );
274 sb = lr->lr_conn->lconn_sb;
279 if ( ber_flush2( sb, ber, LBER_FLUSH_FREE_ALWAYS ) != 0 ) {
280 ld->ld_errno = LDAP_SERVER_DOWN;
290 if ( sendabandon || lr->lr_status == LDAP_REQST_WRITING ) {
291 ldap_free_connection( ld, lr->lr_conn, 0, 1 );
294 if ( origid == msgid ) {
295 ldap_free_request( ld, lr );
299 #ifdef LDAP_R_COMPILE
300 /* ld_abandoned is actually protected by the ld_res_mutex;
301 * give up the ld_req_mutex and get the other */
302 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
303 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
308 if ( ld->ld_abandoned != NULL ) {
312 assert( ld->ld_nabandoned >= 0 );
315 end = ld->ld_nabandoned - 1;
317 if ( ld->ld_nabandoned == 0 || ld->ld_abandoned[ begin ] > msgid ) {
320 } else if ( ld->ld_abandoned[ end ] < msgid ) {
321 i = ld->ld_nabandoned;
326 while ( end >= begin ) {
327 pos = (begin + end)/2;
328 curid = ld->ld_abandoned[ pos ];
330 if ( msgid < curid ) {
333 } else if ( msgid > curid ) {
337 /* already abandoned? */
352 old_abandon = ld->ld_abandoned;
354 ld->ld_abandoned = (ber_int_t *) LDAP_REALLOC( (char *)ld->ld_abandoned,
355 ( ld->ld_nabandoned + 1 ) * sizeof( ber_int_t ) );
357 if ( ld->ld_abandoned == NULL ) {
358 ld->ld_abandoned = old_abandon;
359 ld->ld_errno = LDAP_NO_MEMORY;
363 for ( i = ld->ld_nabandoned; i > pos; i-- ) {
364 ld->ld_abandoned[ i ] = ld->ld_abandoned[ i - 1 ];
366 ld->ld_abandoned[ pos ] = msgid;
371 ld->ld_errno = LDAP_SUCCESS;
375 #ifdef LDAP_R_COMPILE
376 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
377 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
379 return( ld->ld_errno );