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 */
127 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
128 if ( lr->lr_msgid == msgid ) { /* this message */
131 if ( lr->lr_origid == msgid ) {/* child: abandon it */
132 (void) do_abandon( ld,
133 lr->lr_origid, lr->lr_msgid, sctrls, cctrls );
138 if ( origid == msgid && lr->lr_parent != NULL ) {
139 /* don't let caller abandon child requests! */
140 ld->ld_errno = LDAP_PARAM_ERROR;
141 return( LDAP_PARAM_ERROR );
143 if ( lr->lr_status != LDAP_REQST_INPROGRESS ) {
144 /* no need to send abandon message */
149 /* ldap_msgdelete locks the res_mutex. Give up the req_mutex
150 * while we're in there.
152 #ifdef LDAP_R_COMPILE
153 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
155 err = ldap_msgdelete( ld, msgid );
156 #ifdef LDAP_R_COMPILE
157 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
160 ld->ld_errno = LDAP_SUCCESS;
164 /* fetch again the request that we are abandoning */
166 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
167 if ( lr->lr_msgid == msgid ) { /* this message */
175 if( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
178 ld->ld_errno = LDAP_SERVER_DOWN;
180 } else if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
181 /* BER element alocation failed */
183 ld->ld_errno = LDAP_NO_MEMORY;
187 * We already have the mutex in LDAP_R_COMPILE, so
188 * don't try to get it again.
189 * LDAP_NEXT_MSGID(ld, i);
191 i = ++(ld)->ld_msgid;
192 #ifdef LDAP_CONNECTIONLESS
193 if ( LDAP_IS_UDP(ld) ) {
194 err = ber_write( ber, ld->ld_options.ldo_peer,
195 sizeof(struct sockaddr), 0);
197 if ( LDAP_IS_UDP(ld) && ld->ld_options.ldo_version ==
199 char *dn = ld->ld_options.ldo_cldapdn;
201 err = ber_printf( ber, "{isti", /* '}' */
203 LDAP_REQ_ABANDON, msgid );
207 /* create a message to send */
208 err = ber_printf( ber, "{iti", /* '}' */
210 LDAP_REQ_ABANDON, msgid );
215 ld->ld_errno = LDAP_ENCODING_ERROR;
218 /* Put Server Controls */
219 if ( ldap_int_put_controls( ld, sctrls, ber )
226 err = ber_printf( ber, /*{*/ "N}" );
230 ld->ld_errno = LDAP_ENCODING_ERROR;
239 /* send the message */
241 assert( lr->lr_conn != NULL );
242 sb = lr->lr_conn->lconn_sb;
247 if ( ber_flush( sb, ber, 1 ) != 0 ) {
248 ld->ld_errno = LDAP_SERVER_DOWN;
258 if ( sendabandon || lr->lr_status == LDAP_REQST_WRITING ) {
259 ldap_free_connection( ld, lr->lr_conn, 0, 1 );
261 if ( origid == msgid ) {
262 ldap_free_request( ld, lr );
266 #ifdef LDAP_R_COMPILE
267 /* ld_abandoned is actually protected by the ld_res_mutex;
268 * give up the ld_req_mutex and get the other */
269 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
270 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
273 if ( ld->ld_abandoned != NULL ) {
274 for ( ; ld->ld_abandoned[i] != -1; i++ )
278 old_abandon = ld->ld_abandoned;
280 ld->ld_abandoned = (ber_int_t *) LDAP_REALLOC( (char *)
281 ld->ld_abandoned, (i + 2) * sizeof(ber_int_t) );
283 if ( ld->ld_abandoned == NULL ) {
284 ld->ld_abandoned = old_abandon;
285 ld->ld_errno = LDAP_NO_MEMORY;
289 ld->ld_abandoned[i] = msgid;
290 ld->ld_abandoned[i + 1] = -1;
293 ld->ld_errno = LDAP_SUCCESS;
297 #ifdef LDAP_R_COMPILE
298 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
299 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
301 return( ld->ld_errno );