3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2007 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 struct sockaddr sa = {0};
195 /* dummy, filled with ldo_peer in request.c */
196 err = ber_write( ber, &sa, sizeof( sa ), 0 );
198 if ( LDAP_IS_UDP(ld) && ld->ld_options.ldo_version ==
200 char *dn = ld->ld_options.ldo_cldapdn;
202 err = ber_printf( ber, "{isti", /* '}' */
204 LDAP_REQ_ABANDON, msgid );
208 /* create a message to send */
209 err = ber_printf( ber, "{iti", /* '}' */
211 LDAP_REQ_ABANDON, msgid );
216 ld->ld_errno = LDAP_ENCODING_ERROR;
219 /* Put Server Controls */
220 if ( ldap_int_put_controls( ld, sctrls, ber )
227 err = ber_printf( ber, /*{*/ "N}" );
231 ld->ld_errno = LDAP_ENCODING_ERROR;
240 /* send the message */
242 assert( lr->lr_conn != NULL );
243 sb = lr->lr_conn->lconn_sb;
248 if ( ber_flush( sb, ber, 1 ) != 0 ) {
249 ld->ld_errno = LDAP_SERVER_DOWN;
259 if ( sendabandon || lr->lr_status == LDAP_REQST_WRITING ) {
260 ldap_free_connection( ld, lr->lr_conn, 0, 1 );
262 if ( origid == msgid ) {
263 ldap_free_request( ld, lr );
267 #ifdef LDAP_R_COMPILE
268 /* ld_abandoned is actually protected by the ld_res_mutex;
269 * give up the ld_req_mutex and get the other */
270 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
271 ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
274 if ( ld->ld_abandoned != NULL ) {
275 for ( ; ld->ld_abandoned[i] != -1; i++ )
279 old_abandon = ld->ld_abandoned;
281 ld->ld_abandoned = (ber_int_t *) LDAP_REALLOC( (char *)
282 ld->ld_abandoned, (i + 2) * sizeof(ber_int_t) );
284 if ( ld->ld_abandoned == NULL ) {
285 ld->ld_abandoned = old_abandon;
286 ld->ld_errno = LDAP_NO_MEMORY;
290 ld->ld_abandoned[i] = msgid;
291 ld->ld_abandoned[i + 1] = -1;
294 ld->ld_errno = LDAP_SUCCESS;
298 #ifdef LDAP_R_COMPILE
299 ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
300 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
302 return( ld->ld_errno );