]> git.sur5r.net Git - openldap/blob - libraries/libldap/abandon.c
ITS#4840 fix typo
[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-2007 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 #include "lutil.h"
38
39 static int
40 do_abandon(
41         LDAP *ld,
42         ber_int_t origid,
43         ber_int_t msgid,
44         LDAPControl **sctrls,
45         int sendabandon );
46
47 /*
48  * ldap_abandon_ext - perform an ldap extended abandon operation.
49  *
50  * Parameters:
51  *      ld                      LDAP descriptor
52  *      msgid           The message id of the operation to abandon
53  *      scntrls         Server Controls
54  *      ccntrls         Client Controls
55  *
56  * ldap_abandon_ext returns a LDAP error code.
57  *              (LDAP_SUCCESS if everything went ok)
58  *
59  * Example:
60  *      ldap_abandon_ext( ld, msgid, scntrls, ccntrls );
61  */
62 int
63 ldap_abandon_ext(
64         LDAP *ld,
65         int msgid,
66         LDAPControl **sctrls,
67         LDAPControl **cctrls )
68 {
69         int     rc;
70
71         Debug( LDAP_DEBUG_TRACE, "ldap_abandon_ext %d\n", msgid, 0, 0 );
72
73         /* check client controls */
74 #ifdef LDAP_R_COMPILE
75         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
76 #endif
77
78         rc = ldap_int_client_controls( ld, cctrls );
79         if ( rc == LDAP_SUCCESS ) {
80                 rc = do_abandon( ld, msgid, msgid, sctrls, 1 );
81         }
82
83 #ifdef LDAP_R_COMPILE
84         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
85 #endif
86
87         return rc;
88 }
89
90
91 /*
92  * ldap_abandon - perform an ldap abandon operation. Parameters:
93  *
94  *      ld              LDAP descriptor
95  *      msgid           The message id of the operation to abandon
96  *
97  * ldap_abandon returns 0 if everything went ok, -1 otherwise.
98  *
99  * Example:
100  *      ldap_abandon( ld, msgid );
101  */
102 int
103 ldap_abandon( LDAP *ld, int msgid )
104 {
105         Debug( LDAP_DEBUG_TRACE, "ldap_abandon %d\n", msgid, 0, 0 );
106         return ldap_abandon_ext( ld, msgid, NULL, NULL ) == LDAP_SUCCESS
107                 ? 0 : -1;
108 }
109
110
111 int
112 ldap_pvt_discard(
113         LDAP *ld,
114         ber_int_t msgid )
115 {
116         int     rc;
117
118 #ifdef LDAP_R_COMPILE
119         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
120 #endif
121
122         rc = do_abandon( ld, msgid, msgid, NULL, 0 );
123
124 #ifdef LDAP_R_COMPILE
125         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
126 #endif
127
128         return rc;
129 }
130
131 static int
132 do_abandon(
133         LDAP *ld,
134         ber_int_t origid,
135         ber_int_t msgid,
136         LDAPControl **sctrls,
137         int sendabandon )
138 {
139         BerElement      *ber;
140         int             i, err;
141         Sockbuf         *sb;
142         LDAPRequest     *lr;
143
144         Debug( LDAP_DEBUG_TRACE, "do_abandon 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                 /* this message */
152                 if ( lr->lr_msgid == msgid ) {
153                         break;
154                 }
155
156                 /* child: abandon it */
157                 if ( lr->lr_origid == msgid && !lr->lr_abandoned ) {
158                         (void)do_abandon( ld, lr->lr_origid, lr->lr_msgid,
159                                 sctrls, sendabandon );
160
161                         /* restart, as lr may now be dangling... */
162                         goto start_again;
163                 }
164
165                 lr = lr->lr_next;
166         }
167
168         if ( lr != NULL ) {
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 );
173                 }
174                 if ( lr->lr_status != LDAP_REQST_INPROGRESS ) {
175                         /* no need to send abandon message */
176                         sendabandon = 0;
177                 }
178         }
179
180         /* ldap_msgdelete locks the res_mutex. Give up the req_mutex
181          * while we're in there.
182          */
183 #ifdef LDAP_R_COMPILE
184         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
185 #endif
186         err = ldap_msgdelete( ld, msgid );
187 #ifdef LDAP_R_COMPILE
188         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
189 #endif
190         if ( err == 0 ) {
191                 ld->ld_errno = LDAP_SUCCESS;
192                 return LDAP_SUCCESS;
193         }
194
195         /* fetch again the request that we are abandoning */
196         if ( lr != NULL ) {
197                 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
198                         /* this message */
199                         if ( lr->lr_msgid == msgid ) {
200                                 break;
201                         }
202                 }
203         }
204
205         err = 0;
206         if ( sendabandon ) {
207                 if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, NULL ) == -1 ) {
208                         /* not connected */
209                         err = -1;
210                         ld->ld_errno = LDAP_SERVER_DOWN;
211
212                 } else if ( ( ber = ldap_alloc_ber_with_options( ld ) ) == NULL ) {
213                         /* BER element allocation failed */
214                         err = -1;
215                         ld->ld_errno = LDAP_NO_MEMORY;
216
217                 } else {
218                         /*
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);
222                          */
223
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);
229                         }
230                         if ( LDAP_IS_UDP(ld) && ld->ld_options.ldo_version ==
231                                 LDAP_VERSION2 )
232                         {
233                                 char *dn = ld->ld_options.ldo_cldapdn;
234                                 if (!dn) dn = "";
235                                 err = ber_printf( ber, "{isti",  /* '}' */
236                                         i, dn,
237                                         LDAP_REQ_ABANDON, msgid );
238                         } else
239 #endif
240                         {
241                                 /* create a message to send */
242                                 err = ber_printf( ber, "{iti",  /* '}' */
243                                         i,
244                                         LDAP_REQ_ABANDON, msgid );
245                         }
246
247                         if ( err == -1 ) {
248                                 /* encoding error */
249                                 ld->ld_errno = LDAP_ENCODING_ERROR;
250
251                         } else {
252                                 /* Put Server Controls */
253                                 if ( ldap_int_put_controls( ld, sctrls, ber )
254                                         != LDAP_SUCCESS )
255                                 {
256                                         err = -1;
257
258                                 } else {
259                                         /* close '{' */
260                                         err = ber_printf( ber, /*{*/ "N}" );
261
262                                         if ( err == -1 ) {
263                                                 /* encoding error */
264                                                 ld->ld_errno = LDAP_ENCODING_ERROR;
265                                         }
266                                 }
267                         }
268
269                         if ( err == -1 ) {
270                                 ber_free( ber, 1 );
271
272                         } else {
273                                 /* send the message */
274                                 if ( lr != NULL ) {
275                                         assert( lr->lr_conn != NULL );
276                                         sb = lr->lr_conn->lconn_sb;
277                                 } else {
278                                         sb = ld->ld_sb;
279                                 }
280
281                                 if ( ber_flush2( sb, ber, LBER_FLUSH_FREE_ALWAYS ) != 0 ) {
282                                         ld->ld_errno = LDAP_SERVER_DOWN;
283                                         err = -1;
284                                 } else {
285                                         err = 0;
286                                 }
287                         }
288                 }
289         }
290
291         if ( lr != NULL ) {
292                 if ( sendabandon || lr->lr_status == LDAP_REQST_WRITING ) {
293                         ldap_free_connection( ld, lr->lr_conn, 0, 1 );
294                 }
295
296                 if ( origid == msgid ) {
297                         ldap_free_request( ld, lr );
298
299                 } else {
300                         lr->lr_abandoned = 1;
301                 }
302         }
303
304 #ifdef LDAP_R_COMPILE
305         /* ld_abandoned is actually protected by the ld_res_mutex;
306          * give up the ld_req_mutex and get the other */
307         ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
308         ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
309 #endif
310
311         /* use bisection */
312         i = 0;
313         if ( ld->ld_nabandoned == 0 ||
314                 ldap_int_bisect_find( ld->ld_abandoned, ld->ld_nabandoned, msgid, &i ) == 0 )
315         {
316                 ldap_int_bisect_insert( &ld->ld_abandoned, &ld->ld_nabandoned, msgid, i );
317         }
318
319         if ( err != -1 ) {
320                 ld->ld_errno = LDAP_SUCCESS;
321         }
322
323 #ifdef LDAP_R_COMPILE
324         ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
325         ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
326 #endif
327         return( ld->ld_errno );
328 }
329
330 /*
331  * ldap_int_bisect_find
332  *
333  * args:
334  *      v:      array of length n (in)
335  *      n:      length of array v (in)
336  *      id:     value to look for (in)
337  *      idxp:   pointer to location of value/insert point
338  *
339  * return:
340  *      0:      not found
341  *      1:      found
342  *      -1:     error
343  */
344 int
345 ldap_int_bisect_find( ber_int_t *v, ber_len_t n, ber_int_t id, int *idxp )
346 {
347         int             begin,
348                         end,
349                         rc = 0;
350
351         assert( n >= 0 );
352         assert( id >= 0 );
353
354         begin = 0;
355         end = n - 1;
356
357         if ( n > 0 ) {
358                 if ( id < v[ begin ] ) {
359                         *idxp = 0;
360
361                 } else if ( id > v[ end ] ) {
362                         *idxp = n;
363
364                 } else {
365                         int             pos;
366                         ber_int_t       curid;
367         
368                         while ( end >= begin ) {
369                                 pos = (begin + end)/2;
370                                 curid = v[ pos ];
371         
372                                 if ( id < curid ) {
373                                         end = pos - 1;
374         
375                                 } else if ( id > curid ) {
376                                         begin = pos + 1;
377         
378                                 } else {
379                                         /* already abandoned? */
380                                         *idxp = pos;
381                                         rc = 1;
382                                         break;
383                                 }
384                         }
385         
386                         if ( rc == 0 ) {
387                                 *idxp = pos + ( id > curid ? 1 : 0 );
388                         }
389                 }
390
391         } else {
392                 *idxp = 0;
393         }
394
395         return rc;
396 }
397
398 /*
399  * ldap_int_bisect_insert
400  *
401  * args:
402  *      vp:     pointer to array of length *np (in/out)
403  *      np:     pointer to length of array *vp (in/out)
404  *      id:     value to insert (in)
405  *      idx:    location of insert point (as computed by ldap_int_bisect_find())
406  *
407  * return:
408  *      0:      inserted
409  *      -1:     error
410  */
411 int
412 ldap_int_bisect_insert( ber_int_t **vp, ber_len_t *np, int id, int idx )
413 {
414         ber_int_t       *v;
415         ber_len_t       n;
416         int             i;
417
418         assert( vp != NULL );
419         assert( np != NULL );
420         assert( *np >= 0 );
421         assert( idx >= 0 );
422         assert( idx <= *np );
423
424         n = *np;
425
426         v = ber_memrealloc( *vp, sizeof( ber_int_t ) * ( n + 1 ) );
427         if ( v == NULL ) {
428                 return -1;
429         }
430         *vp = v;
431
432         for ( i = n; i > idx; i-- ) {
433                 v[ i ] = v[ i - 1 ];
434         }
435         v[ idx ] = id;
436         ++(*np);
437
438         return 0;
439 }
440
441 /*
442  * ldap_int_bisect_delete
443  *
444  * args:
445  *      vp:     pointer to array of length *np (in/out)
446  *      np:     pointer to length of array *vp (in/out)
447  *      id:     value to delete (in)
448  *      idx:    location of value to delete (as computed by ldap_int_bisect_find())
449  *
450  * return:
451  *      0:      deleted
452  */
453 int
454 ldap_int_bisect_delete( ber_int_t **vp, ber_len_t *np, int id, int idx )
455 {
456         ber_int_t       *v;
457         ber_len_t       n;
458         int             i;
459
460         assert( vp != NULL );
461         assert( np != NULL );
462         assert( *np >= 0 );
463         assert( idx >= 0 );
464         assert( idx < *np );
465
466         v = *vp;
467
468         assert( v[ idx ] == id );
469
470         --(*np);
471         n = *np;
472
473         for ( i = idx; i < n; i++ ) {
474                 v[ i ] = v[ i + 1 ];
475         }
476
477         return 0;
478 }
479