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>
49 * ldap_abandon_ext - perform an ldap extended abandon operation.
53 * msgid The message id of the operation to abandon
54 * scntrls Server Controls
55 * ccntrls Client Controls
57 * ldap_abandon_ext returns a LDAP error code.
58 * (LDAP_SUCCESS if everything went ok)
61 * ldap_abandon_ext( ld, msgid, scntrls, ccntrls );
68 LDAPControl **cctrls )
72 Debug( LDAP_DEBUG_TRACE, "ldap_abandon_ext %d\n", msgid, 0, 0 );
74 /* check client controls */
76 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
79 rc = ldap_int_client_controls( ld, cctrls );
80 if ( rc == LDAP_SUCCESS ) {
81 rc = do_abandon( ld, msgid, msgid, sctrls, 1 );
85 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
93 * ldap_abandon - perform an ldap abandon operation. Parameters:
96 * msgid The message id of the operation to abandon
98 * ldap_abandon returns 0 if everything went ok, -1 otherwise.
101 * ldap_abandon( ld, msgid );
104 ldap_abandon( LDAP *ld, int msgid )
106 Debug( LDAP_DEBUG_TRACE, "ldap_abandon %d\n", msgid, 0, 0 );
107 return ldap_abandon_ext( ld, msgid, NULL, NULL ) == LDAP_SUCCESS
119 #ifdef LDAP_R_COMPILE
120 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
123 rc = do_abandon( ld, msgid, msgid, NULL, 0 );
125 #ifdef LDAP_R_COMPILE
126 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
137 LDAPControl **sctrls,
142 ber_int_t *old_abandon;
146 Debug( LDAP_DEBUG_TRACE, "ldap_int_discard origid %d, msgid %d\n",
149 /* find the request that we are abandoning */
151 lr = ld->ld_requests;
152 while ( lr != NULL ) {
153 if ( lr->lr_msgid == msgid ) { /* this message */
157 if ( lr->lr_origid == msgid ) {/* child: abandon it */
158 (void)do_abandon( ld, lr->lr_origid, lr->lr_msgid,
159 sctrls, sendabandon );
161 /* restart, as lr may now be dangling... */
169 if ( origid == msgid && lr->lr_parent != NULL ) {
170 /* don't let caller abandon child requests! */
171 ld->ld_errno = LDAP_PARAM_ERROR;
172 return( LDAP_PARAM_ERROR );
174 if ( lr->lr_status != LDAP_REQST_INPROGRESS ) {
175 /* no need to send abandon message */
180 /* ldap_msgdelete locks the res_mutex. Give up the req_mutex
181 * while we're in there.
183 #ifdef LDAP_R_COMPILE
184 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
186 err = ldap_msgdelete( ld, msgid );
187 #ifdef LDAP_R_COMPILE
188 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
191 ld->ld_errno = LDAP_SUCCESS;
195 /* fetch again the request that we are abandoning */
197 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
199 if ( lr->lr_msgid == msgid ) {
207 if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
210 ld->ld_errno = LDAP_SERVER_DOWN;
212 } else if ( ( ber = ldap_alloc_ber_with_options( ld ) ) == NULL ) {
213 /* BER element allocation failed */
215 ld->ld_errno = LDAP_NO_MEMORY;
219 * We already have the mutex in LDAP_R_COMPILE, so
220 * don't try to get it again.
221 * LDAP_NEXT_MSGID(ld, i);
224 i = ++(ld)->ld_msgid;
225 #ifdef LDAP_CONNECTIONLESS
226 if ( LDAP_IS_UDP(ld) ) {
227 err = ber_write( ber, ld->ld_options.ldo_peer,
228 sizeof(struct sockaddr), 0);
230 if ( LDAP_IS_UDP(ld) && ld->ld_options.ldo_version ==
233 char *dn = ld->ld_options.ldo_cldapdn;
235 err = ber_printf( ber, "{isti", /* '}' */
237 LDAP_REQ_ABANDON, msgid );
241 /* create a message to send */
242 err = ber_printf( ber, "{iti", /* '}' */
244 LDAP_REQ_ABANDON, msgid );
249 ld->ld_errno = LDAP_ENCODING_ERROR;
252 /* Put Server Controls */
253 if ( ldap_int_put_controls( ld, sctrls, ber )
260 err = ber_printf( ber, /*{*/ "N}" );
264 ld->ld_errno = LDAP_ENCODING_ERROR;
273 /* send the message */
275 assert( lr->lr_conn != NULL );
276 sb = lr->lr_conn->lconn_sb;
281 if ( ber_flush2( sb, ber, LBER_FLUSH_FREE_ALWAYS ) != 0 ) {
282 ld->ld_errno = LDAP_SERVER_DOWN;
292 if ( sendabandon || lr->lr_status == LDAP_REQST_WRITING ) {
293 ldap_free_connection( ld, lr->lr_conn, 0, 1 );
296 if ( origid == msgid ) {
297 ldap_free_request( ld, lr );
301 #ifdef LDAP_R_COMPILE
302 /* ld_abandoned is actually protected by the ld_res_mutex;
303 * give up the ld_req_mutex and get the other */
304 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
305 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
310 if ( ld->ld_abandoned != NULL ) {
314 assert( ld->ld_nabandoned >= 0 );
317 end = ld->ld_nabandoned - 1;
319 if ( ld->ld_nabandoned == 0 || ld->ld_abandoned[ begin ] > msgid ) {
322 } else if ( ld->ld_abandoned[ end ] < msgid ) {
323 i = ld->ld_nabandoned;
328 while ( end >= begin ) {
329 pos = (begin + end)/2;
330 curid = ld->ld_abandoned[ pos ];
332 if ( msgid < curid ) {
335 } else if ( msgid > curid ) {
339 /* already abandoned? */
354 old_abandon = ld->ld_abandoned;
356 ld->ld_abandoned = (ber_int_t *) LDAP_REALLOC( (char *)ld->ld_abandoned,
357 ( ld->ld_nabandoned + 1 ) * sizeof( ber_int_t ) );
359 if ( ld->ld_abandoned == NULL ) {
360 ld->ld_abandoned = old_abandon;
361 ld->ld_errno = LDAP_NO_MEMORY;
365 for ( i = ld->ld_nabandoned; i > pos; i-- ) {
366 ld->ld_abandoned[ i ] = ld->ld_abandoned[ i - 1 ];
368 ld->ld_abandoned[ pos ] = msgid;
373 ld->ld_errno = LDAP_SUCCESS;
377 #ifdef LDAP_R_COMPILE
378 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
379 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
381 return( ld->ld_errno );