]> git.sur5r.net Git - openldap/blob - libraries/libldap/abandon.c
s/ldap_int_discard/ldap_pvt_discard/
[openldap] / libraries / libldap / abandon.c
1 /* abandon.c */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2006 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16 /* Portions  Copyright (c) 1990 Regents of the University of Michigan.
17  * All rights reserved.
18  */
19
20 #include "portable.h"
21
22 #include <stdio.h>
23
24 #include <ac/stdlib.h>
25
26 #include <ac/socket.h>
27 #include <ac/string.h>
28 #include <ac/time.h>
29
30 #include "ldap-int.h"
31
32 /*
33  * An abandon request looks like this:
34  *              AbandonRequest ::= [APPLICATION 16] MessageID
35  * and has no response.  (Source: RFC 4511)
36  */
37
38 static int
39 do_abandon(
40         LDAP *ld,
41         ber_int_t origid,
42         ber_int_t msgid,
43         LDAPControl **sctrls,
44         int sendabandon );
45
46 /*
47  * ldap_abandon_ext - perform an ldap extended abandon operation.
48  *
49  * Parameters:
50  *      ld                      LDAP descriptor
51  *      msgid           The message id of the operation to abandon
52  *      scntrls         Server Controls
53  *      ccntrls         Client Controls
54  *
55  * ldap_abandon_ext returns a LDAP error code.
56  *              (LDAP_SUCCESS if everything went ok)
57  *
58  * Example:
59  *      ldap_abandon_ext( ld, msgid, scntrls, ccntrls );
60  */
61 int
62 ldap_abandon_ext(
63         LDAP *ld,
64         int msgid,
65         LDAPControl **sctrls,
66         LDAPControl **cctrls )
67 {
68         int     rc;
69
70         Debug( LDAP_DEBUG_TRACE, "ldap_abandon_ext %d\n", msgid, 0, 0 );
71
72         /* check client controls */
73 #ifdef LDAP_R_COMPILE
74         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
75 #endif
76
77         rc = ldap_int_client_controls( ld, cctrls );
78         if ( rc == LDAP_SUCCESS ) {
79                 rc = do_abandon( ld, msgid, msgid, sctrls, 1 );
80         }
81
82 #ifdef LDAP_R_COMPILE
83         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
84 #endif
85
86         return rc;
87 }
88
89
90 /*
91  * ldap_abandon - perform an ldap abandon operation. Parameters:
92  *
93  *      ld              LDAP descriptor
94  *      msgid           The message id of the operation to abandon
95  *
96  * ldap_abandon returns 0 if everything went ok, -1 otherwise.
97  *
98  * Example:
99  *      ldap_abandon( ld, msgid );
100  */
101 int
102 ldap_abandon( LDAP *ld, int msgid )
103 {
104         Debug( LDAP_DEBUG_TRACE, "ldap_abandon %d\n", msgid, 0, 0 );
105         return ldap_abandon_ext( ld, msgid, NULL, NULL ) == LDAP_SUCCESS
106                 ? 0 : -1;
107 }
108
109
110 int
111 ldap_pvt_discard(
112         LDAP *ld,
113         ber_int_t msgid )
114 {
115         int     rc;
116
117 #ifdef LDAP_R_COMPILE
118         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
119 #endif
120
121         rc = do_abandon( ld, msgid, msgid, NULL, 0 );
122
123 #ifdef LDAP_R_COMPILE
124         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
125 #endif
126
127         return rc;
128 }
129
130 static int
131 do_abandon(
132         LDAP *ld,
133         ber_int_t origid,
134         ber_int_t msgid,
135         LDAPControl **sctrls,
136         int sendabandon )
137 {
138         BerElement      *ber;
139         int             i, err;
140         ber_int_t       *old_abandon;
141         Sockbuf         *sb;
142         LDAPRequest     *lr;
143
144         Debug( LDAP_DEBUG_TRACE, "ldap_pvt_discard origid %d, msgid %d\n",
145                 origid, msgid, 0 );
146
147         /* find the request that we are abandoning */
148 start_again:;
149         lr = ld->ld_requests;
150         while ( lr != NULL ) {
151                 if ( lr->lr_msgid == msgid ) {  /* this message */
152                         break;
153                 }
154
155                 if ( lr->lr_origid == msgid ) {/* child:  abandon it */
156                         (void)do_abandon( ld, lr->lr_origid, lr->lr_msgid,
157                                 sctrls, sendabandon );
158
159                         /* restart, as lr may now be dangling... */
160                         goto start_again;
161                 }
162
163                 lr = lr->lr_next;
164         }
165
166         if ( lr != NULL ) {
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 );
171                 }
172                 if ( lr->lr_status != LDAP_REQST_INPROGRESS ) {
173                         /* no need to send abandon message */
174                         sendabandon = 0;
175                 }
176         }
177
178         /* ldap_msgdelete locks the res_mutex. Give up the req_mutex
179          * while we're in there.
180          */
181 #ifdef LDAP_R_COMPILE
182         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
183 #endif
184         err = ldap_msgdelete( ld, msgid );
185 #ifdef LDAP_R_COMPILE
186         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
187 #endif
188         if ( err == 0 ) {
189                 ld->ld_errno = LDAP_SUCCESS;
190                 return LDAP_SUCCESS;
191         }
192
193         /* fetch again the request that we are abandoning */
194         if ( lr != NULL ) {
195                 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
196                         /* this message */
197                         if ( lr->lr_msgid == msgid ) {
198                                 break;
199                         }
200                 }
201         }
202
203         err = 0;
204         if ( sendabandon ) {
205                 if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
206                         /* not connected */
207                         err = -1;
208                         ld->ld_errno = LDAP_SERVER_DOWN;
209
210                 } else if ( ( ber = ldap_alloc_ber_with_options( ld ) ) == NULL ) {
211                         /* BER element allocation failed */
212                         err = -1;
213                         ld->ld_errno = LDAP_NO_MEMORY;
214
215                 } else {
216                         /*
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);
220                          */
221
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);
227                         }
228                         if ( LDAP_IS_UDP(ld) && ld->ld_options.ldo_version ==
229                                 LDAP_VERSION2 )
230                         {
231                                 char *dn = ld->ld_options.ldo_cldapdn;
232                                 if (!dn) dn = "";
233                                 err = ber_printf( ber, "{isti",  /* '}' */
234                                         i, dn,
235                                         LDAP_REQ_ABANDON, msgid );
236                         } else
237 #endif
238                         {
239                                 /* create a message to send */
240                                 err = ber_printf( ber, "{iti",  /* '}' */
241                                         i,
242                                         LDAP_REQ_ABANDON, msgid );
243                         }
244
245                         if ( err == -1 ) {
246                                 /* encoding error */
247                                 ld->ld_errno = LDAP_ENCODING_ERROR;
248
249                         } else {
250                                 /* Put Server Controls */
251                                 if ( ldap_int_put_controls( ld, sctrls, ber )
252                                         != LDAP_SUCCESS )
253                                 {
254                                         err = -1;
255
256                                 } else {
257                                         /* close '{' */
258                                         err = ber_printf( ber, /*{*/ "N}" );
259
260                                         if ( err == -1 ) {
261                                                 /* encoding error */
262                                                 ld->ld_errno = LDAP_ENCODING_ERROR;
263                                         }
264                                 }
265                         }
266
267                         if ( err == -1 ) {
268                                 ber_free( ber, 1 );
269
270                         } else {
271                                 /* send the message */
272                                 if ( lr != NULL ) {
273                                         assert( lr->lr_conn != NULL );
274                                         sb = lr->lr_conn->lconn_sb;
275                                 } else {
276                                         sb = ld->ld_sb;
277                                 }
278
279                                 if ( ber_flush2( sb, ber, LBER_FLUSH_FREE_ALWAYS ) != 0 ) {
280                                         ld->ld_errno = LDAP_SERVER_DOWN;
281                                         err = -1;
282                                 } else {
283                                         err = 0;
284                                 }
285                         }
286                 }
287         }
288
289         if ( lr != NULL ) {
290                 if ( sendabandon || lr->lr_status == LDAP_REQST_WRITING ) {
291                         ldap_free_connection( ld, lr->lr_conn, 0, 1 );
292                 }
293
294                 if ( origid == msgid ) {
295                         ldap_free_request( ld, lr );
296                 }
297         }
298
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 );
304 #endif
305
306         /* use bisection */
307         i = 0;
308         if ( ld->ld_abandoned != NULL ) {
309                 int             begin,
310                                 end;
311
312                 assert( ld->ld_nabandoned >= 0 );
313
314                 begin = 0;
315                 end = ld->ld_nabandoned - 1;
316
317                 if ( ld->ld_nabandoned == 0 || ld->ld_abandoned[ begin ] > msgid ) {
318                         i = 0;
319
320                 } else if ( ld->ld_abandoned[ end ] < msgid ) {
321                         i = ld->ld_nabandoned;
322
323                 } else {
324                         int     pos, curid;
325
326                         while ( end >= begin ) {
327                                 pos = (begin + end)/2;
328                                 curid = ld->ld_abandoned[ pos ];
329
330                                 if ( msgid < curid ) {
331                                         end = pos - 1;
332
333                                 } else if ( msgid > curid ) {
334                                         begin = pos + 1;
335
336                                 } else {
337                                         /* already abandoned? */
338                                         i = -1;
339                                         break;
340                                 }
341                         }
342
343                         if ( i == 0 ) {
344                                 i = pos;
345                         }
346                 }
347         }
348
349         if ( i != -1 ) {
350                 int     pos = i;
351
352                 old_abandon = ld->ld_abandoned;
353
354                 ld->ld_abandoned = (ber_int_t *) LDAP_REALLOC( (char *)ld->ld_abandoned,
355                         ( ld->ld_nabandoned + 1 ) * sizeof( ber_int_t ) );
356
357                 if ( ld->ld_abandoned == NULL ) {
358                         ld->ld_abandoned = old_abandon;
359                         ld->ld_errno = LDAP_NO_MEMORY;
360                         goto done;
361                 }
362
363                 for ( i = ld->ld_nabandoned; i > pos; i-- ) {
364                         ld->ld_abandoned[ i ] = ld->ld_abandoned[ i - 1 ];
365                 }
366                 ld->ld_abandoned[ pos ] = msgid;
367                 ++ld->ld_nabandoned;
368         }
369
370         if ( err != -1 ) {
371                 ld->ld_errno = LDAP_SUCCESS;
372         }
373
374 done:;
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 );
378 #endif
379         return( ld->ld_errno );
380 }