]> git.sur5r.net Git - openldap/blob - libraries/libldap/result.c
ldap_result was losing messages when multiple msgids were queued,
[openldap] / libraries / libldap / result.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*  Portions
7  *  Copyright (c) 1990 Regents of the University of Michigan.
8  *  All rights reserved.
9  *
10  *  result.c - wait for an ldap result
11  */
12
13 /*
14  * LDAPv3 (RFC2251)
15  *      LDAPResult ::= SEQUENCE {
16  *              resultCode              ENUMERATED { ... },
17  *              matchedDN               LDAPDN,
18  *              errorMessage    LDAPString,
19  *              referral                Referral OPTIONAL
20  *      }
21  *      Referral ::= SEQUENCE OF LDAPURL        (one or more)
22  *      LDAPURL ::= LDAPString                          (limited to URL chars)
23  */
24
25 #include "portable.h"
26
27 #include <stdio.h>
28
29 #include <ac/stdlib.h>
30
31 #include <ac/errno.h>
32 #include <ac/socket.h>
33 #include <ac/string.h>
34 #include <ac/time.h>
35 #include <ac/unistd.h>
36
37 #include "ldap-int.h"
38
39
40 static int ldap_abandoned LDAP_P(( LDAP *ld, ber_int_t msgid ));
41 static int ldap_mark_abandoned LDAP_P(( LDAP *ld, ber_int_t msgid ));
42 static int wait4msg LDAP_P(( LDAP *ld, ber_int_t msgid, int all, struct timeval *timeout,
43         LDAPMessage **result ));
44 static ber_tag_t try_read1msg LDAP_P(( LDAP *ld, ber_int_t msgid,
45         int all, Sockbuf *sb, LDAPConn *lc, LDAPMessage **result ));
46 static ber_tag_t build_result_ber LDAP_P(( LDAP *ld, BerElement **bp, LDAPRequest *lr ));
47 static void merge_error_info LDAP_P(( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr ));
48
49
50 /*
51  * ldap_result - wait for an ldap result response to a message from the
52  * ldap server.  If msgid is -1, any message will be accepted, otherwise
53  * ldap_result will wait for a response with msgid.  If all is 0 the
54  * first message with id msgid will be accepted, otherwise, ldap_result
55  * will wait for all responses with id msgid and then return a pointer to
56  * the entire list of messages.  This is only useful for search responses,
57  * which can be of two message types (zero or more entries, followed by an
58  * ldap result).  The type of the first message received is returned.
59  * When waiting, any messages that have been abandoned are discarded.
60  *
61  * Example:
62  *      ldap_result( s, msgid, all, timeout, result )
63  */
64 int
65 ldap_result(
66         LDAP *ld,
67         int msgid,
68         int all,
69         struct timeval *timeout,
70         LDAPMessage **result )
71 {
72         LDAPMessage     *lm, *lastlm, *nextlm;
73
74         assert( ld != NULL );
75         assert( result != NULL );
76
77         Debug( LDAP_DEBUG_TRACE, "ldap_result\n", 0, 0, 0 );
78
79         if( ld == NULL ) {
80                 return -1;
81         }
82
83         if( result == NULL ) {
84                 ld->ld_errno = LDAP_PARAM_ERROR;
85                 return -1;
86         }
87
88         /*
89          * First, look through the list of responses we have received on
90          * this association and see if the response we're interested in
91          * is there.  If it is, return it.  If not, call wait4msg() to
92          * wait until it arrives or timeout occurs.
93          */
94
95         *result = NULL;
96         lastlm = NULL;
97         for ( lm = ld->ld_responses; lm != NULL; lm = nextlm ) {
98                 nextlm = lm->lm_next;
99
100                 if ( ldap_abandoned( ld, lm->lm_msgid ) ) {
101                         ldap_mark_abandoned( ld, lm->lm_msgid );
102
103                         if ( lastlm == NULL ) {
104                                 ld->ld_responses = lm->lm_next;
105                         } else {
106                                 lastlm->lm_next = nextlm;
107                         }
108
109                         ldap_msgfree( lm );
110
111                         continue;
112                 }
113
114                 if ( msgid == LDAP_RES_ANY || lm->lm_msgid == msgid ) {
115                         LDAPMessage     *tmp;
116
117                         if ( all == 0
118                             || (lm->lm_msgtype != LDAP_RES_SEARCH_RESULT
119                             && lm->lm_msgtype != LDAP_RES_SEARCH_REFERENCE      /* LDAPv3 */
120                             && lm->lm_msgtype != LDAP_RES_SEARCH_ENTRY) )
121                                 break;
122
123                         for ( tmp = lm; tmp != NULL; tmp = tmp->lm_chain ) {
124                                 if ( tmp->lm_msgtype == LDAP_RES_SEARCH_RESULT )
125                                         break;
126                         }
127
128                         if ( tmp == NULL ) {
129                                 lm = NULL;
130                         }
131
132                         break;
133                 }
134                 lastlm = lm;
135         }
136         if ( lm == NULL ) {
137                 return( wait4msg( ld, msgid, all, timeout, result ) );
138         }
139
140         if ( lastlm == NULL ) {
141                 ld->ld_responses = (all == 0 && lm->lm_chain != NULL
142                     ? lm->lm_chain : lm->lm_next);
143         } else {
144                 lastlm->lm_next = (all == 0 && lm->lm_chain != NULL
145                     ? lm->lm_chain : lm->lm_next);
146         }
147         if ( all == 0 && lm->lm_chain != NULL )
148         {
149                 lm->lm_chain->lm_next = lm->lm_next;
150                 lm->lm_chain = NULL;
151         }
152         lm->lm_next = NULL;
153
154         *result = lm;
155         ld->ld_errno = LDAP_SUCCESS;
156         return( lm->lm_msgtype );
157 }
158
159 static int
160 wait4msg(
161         LDAP *ld,
162         ber_int_t msgid,
163         int all,
164         struct timeval *timeout,
165         LDAPMessage **result )
166 {
167         int             rc;
168         struct timeval  tv, *tvp;
169         time_t          start_time = 0;
170         time_t          tmp_time;
171         LDAPConn        *lc, *nextlc;
172
173         assert( ld != NULL );
174         assert( result != NULL );
175
176 #ifdef LDAP_DEBUG
177         if ( timeout == NULL ) {
178                 Debug( LDAP_DEBUG_TRACE, "wait4msg (infinite timeout)\n",
179                     0, 0, 0 );
180         } else {
181                 Debug( LDAP_DEBUG_TRACE, "wait4msg (timeout %ld sec, %ld usec)\n",
182                        (long) timeout->tv_sec, (long) timeout->tv_usec, 0 );
183         }
184 #endif /* LDAP_DEBUG */
185
186         if ( timeout == NULL ) {
187                 tvp = NULL;
188         } else {
189                 tv = *timeout;
190                 tvp = &tv;
191                 start_time = time( NULL );
192         }
193                     
194         rc = -2;
195         while ( rc == -2 ) {
196 #ifdef LDAP_DEBUG
197                 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
198                         ldap_dump_connection( ld, ld->ld_conns, 1 );
199                         ldap_dump_requests_and_responses( ld );
200                 }
201 #endif /* LDAP_DEBUG */
202                 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
203                         if ( ber_pvt_sb_data_ready(lc->lconn_sb) ) {
204                                 rc = try_read1msg( ld, msgid, all, lc->lconn_sb,
205                                     lc, result );
206                                 break;
207                         }
208                 }
209
210                 if ( lc == NULL ) {
211                         rc = do_ldap_select( ld, tvp );
212
213
214 #ifdef LDAP_DEBUG
215                         if ( rc == -1 ) {
216                             Debug( LDAP_DEBUG_TRACE,
217                                     "do_ldap_select returned -1: errno %d\n",
218                                     errno, 0, 0 );
219                         }
220 #endif
221
222                         if ( rc == 0 || ( rc == -1 && (
223                                 !LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
224                                 || errno != EINTR )))
225                         {
226                                 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
227                                     LDAP_TIMEOUT);
228                                 return( rc );
229                         }
230
231                         if ( rc == -1 ) {
232                                 rc = -2;        /* select interrupted: loop */
233                         } else {
234                                 rc = -2;
235                                 for ( lc = ld->ld_conns; rc == -2 && lc != NULL;
236                                     lc = nextlc ) {
237                                         nextlc = lc->lconn_next;
238                                         if ( lc->lconn_status ==
239                                             LDAP_CONNST_CONNECTED &&
240                                             ldap_is_read_ready( ld,
241                                             lc->lconn_sb )) {
242                                                 rc = try_read1msg( ld, msgid, all,
243                                                     lc->lconn_sb, lc, result );
244                                         }
245                                 }
246                         }
247                 }
248
249                 if ( rc == -2 && tvp != NULL ) {
250                         tmp_time = time( NULL );
251                         if (( tv.tv_sec -=  ( tmp_time - start_time )) <= 0 ) {
252                                 rc = 0; /* timed out */
253                                 ld->ld_errno = LDAP_TIMEOUT;
254                                 break;
255                         }
256
257                         Debug( LDAP_DEBUG_TRACE, "wait4msg:  %ld secs to go\n",
258                                (long) tv.tv_sec, 0, 0 );
259                         start_time = tmp_time;
260                 }
261         }
262
263         return( rc );
264 }
265
266
267 static ber_tag_t
268 try_read1msg(
269         LDAP *ld,
270         ber_int_t msgid,
271         int all,
272         Sockbuf *sb,
273     LDAPConn *lc,
274         LDAPMessage **result )
275 {
276         BerElement      *ber;
277         LDAPMessage     *new, *l, *prev, *tmp;
278         ber_int_t       id;
279         ber_tag_t       tag;
280         ber_len_t       len;
281         int             foundit = 0;
282         LDAPRequest     *lr;
283         BerElement      tmpber;
284         int             rc, refer_cnt, hadref, simple_request;
285         ber_int_t       lderr;
286
287         assert( ld != NULL );
288         assert( lc != NULL );
289         
290         Debug( LDAP_DEBUG_TRACE, "read1msg\n", 0, 0, 0 );
291
292     if ( lc->lconn_ber == NULL ) {
293                 lc->lconn_ber = ldap_alloc_ber_with_options(ld);
294
295                 if( lc->lconn_ber == NULL ) {
296                         return -1;
297                 }
298     }
299
300         ber = lc->lconn_ber;
301         assert( BER_VALID (ber) );
302
303         /* get the next message */
304         errno = 0;
305         if ( (tag = ber_get_next( sb, &len, ber ))
306             != LDAP_TAG_MESSAGE ) {
307                 if ( tag == LBER_DEFAULT) {
308 #ifdef LDAP_DEBUG                  
309                         Debug( LDAP_DEBUG_CONNS,
310                               "ber_get_next failed.\n", 0, 0, 0 );
311 #endif             
312 #ifdef EWOULDBLOCK                      
313                         if (errno==EWOULDBLOCK) return -2;
314 #endif
315 #ifdef EAGAIN
316                         if (errno == EAGAIN) return -2;
317 #endif
318                         ld->ld_errno = LDAP_SERVER_DOWN;
319                         return -1;
320                 }
321                 ld->ld_errno = LDAP_LOCAL_ERROR;
322                 return -1;
323         }
324
325         /*
326      * We read a complete message.
327          * The connection should no longer need this ber.
328          */
329     lc->lconn_ber = NULL;
330
331         /* message id */
332         if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
333                 ber_free( ber, 1 );
334                 ld->ld_errno = LDAP_DECODING_ERROR;
335                 return( -1 );
336         }
337
338         /* if it's been abandoned, toss it */
339         if ( ldap_abandoned( ld, id ) ) {
340                 ber_free( ber, 1 );
341                 return( -2 );   /* continue looking */
342         }
343
344         if (( lr = ldap_find_request_by_msgid( ld, id )) == NULL ) {
345                 Debug( LDAP_DEBUG_ANY,
346                     "no request for response with msgid %ld (tossing)\n",
347                     (long) id, 0, 0 );
348                 ber_free( ber, 1 );
349                 return( -2 );   /* continue looking */
350         }
351
352         /* the message type */
353         if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
354                 ld->ld_errno = LDAP_DECODING_ERROR;
355                 ber_free( ber, 1 );
356                 return( -1 );
357         }
358
359         Debug( LDAP_DEBUG_TRACE, "ldap_read: %s msgid %ld, original id %ld\n",
360             ( tag == LDAP_RES_SEARCH_ENTRY ) ? "entry" : 
361                 ( tag == LDAP_RES_SEARCH_REFERENCE ) ? "reference" : "result",
362                 (long) id, (long) lr->lr_origid );
363
364         id = lr->lr_origid;
365         refer_cnt = 0;
366         hadref = simple_request = 0;
367         rc = -2;        /* default is to keep looking (no response found) */
368         lr->lr_res_msgtype = tag;
369
370         if ( tag != LDAP_RES_SEARCH_ENTRY ) {
371                 if ( ld->ld_version >= LDAP_VERSION2 &&
372                         ( lr->lr_parent != NULL ||
373                         LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ) )
374                 {
375                         tmpber = *ber;  /* struct copy */
376                         if ( ber_scanf( &tmpber, "{iaa}", &lderr,
377                             &lr->lr_res_matched, &lr->lr_res_error )
378                             != LBER_ERROR ) {
379                                 if ( lderr != LDAP_SUCCESS ) {
380                                         /* referrals are in error string */
381                                         refer_cnt = ldap_chase_referrals( ld, lr,
382                                             &lr->lr_res_error, &hadref );
383                                 }
384
385                                 /* save errno, message, and matched string */
386                                 if ( !hadref || lr->lr_res_error == NULL ) {
387                                         lr->lr_res_errno = ( lderr ==
388                                         LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
389                                         : lderr;
390                                 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
391                                         lr->lr_res_errno = ld->ld_errno;
392                                 } else {
393                                         lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
394                                 }
395 Debug( LDAP_DEBUG_TRACE,
396     "new result:  res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
397     lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
398     lr->lr_res_matched ? lr->lr_res_matched : "" );
399                         }
400                 }
401
402                 Debug( LDAP_DEBUG_TRACE,
403                     "read1msg:  %d new referrals\n", refer_cnt, 0, 0 );
404
405                 if ( refer_cnt != 0 ) { /* chasing referrals */
406                         ber_free( ber, 1 );
407                         ber = NULL;
408                         if ( refer_cnt < 0 ) {
409                                 return( -1 );   /* fatal error */
410                         }
411                         lr->lr_status = LDAP_REQST_CHASINGREFS;
412                 } else {
413                         if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
414                                 /* request without any referrals */
415                                 simple_request = ( hadref ? 0 : 1 );
416                         } else {
417                                 /* request with referrals or child request */
418                                 ber_free( ber, 1 );
419                                 ber = NULL;
420                         }
421
422                         while ( lr->lr_parent != NULL ) {
423                                 merge_error_info( ld, lr->lr_parent, lr );
424
425                                 lr = lr->lr_parent;
426                                 if ( --lr->lr_outrefcnt > 0 ) {
427                                         break;  /* not completely done yet */
428                                 }
429                         }
430
431                         if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
432                                 id = lr->lr_msgid;
433                                 tag = lr->lr_res_msgtype;
434                                 Debug( LDAP_DEBUG_ANY, "request %ld done\n",
435                                     (long) id, 0, 0 );
436 Debug( LDAP_DEBUG_TRACE,
437 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
438 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
439 lr->lr_res_matched ? lr->lr_res_matched : "" );
440                                 if ( !simple_request ) {
441                                         ber_free( ber, 1 );
442                                         ber = NULL;
443                                         if ( build_result_ber( ld, &ber, lr )
444                                             == LBER_ERROR ) {
445                                                 rc = -1; /* fatal error */
446                                         }
447                                 }
448
449                                 ldap_free_request( ld, lr );
450                         }
451
452                         if ( lc != NULL ) {
453                                 ldap_free_connection( ld, lc, 0, 1 );
454                         }
455                 }
456         }
457
458         if ( ber == NULL ) {
459                 return( rc );
460         }
461
462         /* make a new ldap message */
463         if ( (new = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) ))
464             == NULL ) {
465                 ld->ld_errno = LDAP_NO_MEMORY;
466                 return( -1 );
467         }
468         new->lm_msgid = (int)id;
469         new->lm_msgtype = tag;
470         new->lm_ber = ber;
471
472 #ifndef LDAP_NOCACHE
473                 if ( ld->ld_cache != NULL ) {
474                         ldap_add_result_to_cache( ld, new );
475                 }
476 #endif /* LDAP_NOCACHE */
477
478         /* is this the one we're looking for? */
479         if ( msgid == LDAP_RES_ANY || id == msgid ) {
480                 if ( all == 0
481                     || (new->lm_msgtype != LDAP_RES_SEARCH_RESULT
482                     && new->lm_msgtype != LDAP_RES_SEARCH_ENTRY) ) {
483                         *result = new;
484                         ld->ld_errno = LDAP_SUCCESS;
485                         return( tag );
486                 } else if ( new->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
487                         foundit = 1;    /* return the chain later */
488                 }
489         }
490
491         /* 
492          * if not, we must add it to the list of responses.  if
493          * the msgid is already there, it must be part of an existing
494          * search response.
495          */
496
497         prev = NULL;
498         for ( l = ld->ld_responses; l != NULL; l = l->lm_next ) {
499                 if ( l->lm_msgid == new->lm_msgid )
500                         break;
501                 prev = l;
502         }
503
504         /* not part of an existing search response */
505         if ( l == NULL ) {
506                 if ( foundit ) {
507                         *result = new;
508                         ld->ld_errno = LDAP_SUCCESS;
509                         return( tag );
510                 }
511
512                 new->lm_next = ld->ld_responses;
513                 ld->ld_responses = new;
514                 return( -2 );   /* continue looking */
515         }
516
517         Debug( LDAP_DEBUG_TRACE, "adding response id %ld type %ld:\n",
518             (long) new->lm_msgid, (long) new->lm_msgtype, 0 );
519
520         /* part of a search response - add to end of list of entries */
521         for ( tmp = l; tmp->lm_chain != NULL &&
522             tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY;
523             tmp = tmp->lm_chain )
524                 ;       /* NULL */
525         tmp->lm_chain = new;
526
527         /* return the whole chain if that's what we were looking for */
528         if ( foundit ) {
529                 if ( prev == NULL )
530                         ld->ld_responses = l->lm_next;
531                 else
532                         prev->lm_next = l->lm_next;
533                 *result = l;
534                 ld->ld_errno = LDAP_SUCCESS;
535 #ifdef LDAP_WORLD_P16
536                 /*
537                  * XXX questionable fix; see text for [P16] on
538                  * http://www.critical-angle.com/ldapworld/patch/
539                  *
540                  * inclusion of this patch causes searchs to hang on
541                  * multiple platforms
542                  */
543                 return( l->lm_msgtype );
544 #else   /* LDAP_WORLD_P16 */
545                 return( tag );
546 #endif  /* !LDAP_WORLD_P16 */
547         }
548
549         return( -2 );   /* continue looking */
550 }
551
552
553 static ber_tag_t
554 build_result_ber( LDAP *ld, BerElement **bp, LDAPRequest *lr )
555 {
556         ber_len_t       len;
557         ber_int_t       tag;
558         ber_int_t       along;
559         BerElement *ber;
560
561         *bp = NULL;
562         ber = ldap_alloc_ber_with_options( ld );
563
564         if( ber == NULL ) {
565                 ld->ld_errno = LDAP_NO_MEMORY;
566                 return LBER_ERROR;
567         }
568
569         if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
570             lr->lr_res_msgtype, lr->lr_res_errno,
571             lr->lr_res_matched ? lr->lr_res_matched : "",
572             lr->lr_res_error ? lr->lr_res_error : "" ) == -1 ) {
573
574                 ld->ld_errno = LDAP_ENCODING_ERROR;
575                 ber_free(ber, 1);
576                 return( LBER_ERROR );
577         }
578
579         ber_reset( ber, 1 );
580
581         if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
582                 ld->ld_errno = LDAP_DECODING_ERROR;
583                 ber_free(ber, 1);
584                 return( LBER_ERROR );
585         }
586
587         if ( ber_get_int( ber, &along ) == LBER_ERROR ) {
588                 ld->ld_errno = LDAP_DECODING_ERROR;
589                 ber_free(ber, 1);
590                 return( LBER_ERROR );
591         }
592
593         tag = ber_peek_tag( ber, &len );
594
595         if ( tag == LBER_ERROR ) {
596                 ld->ld_errno = LDAP_DECODING_ERROR;
597                 ber_free(ber, 1);
598                 return( LBER_ERROR );
599         }
600
601         *bp = ber;
602         return tag;
603 }
604
605
606 static void
607 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
608 {
609 /*
610  * Merge error information in "lr" with "parentr" error code and string.
611  */
612         if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
613                 parentr->lr_res_errno = lr->lr_res_errno;
614                 if ( lr->lr_res_error != NULL ) {
615                         (void)ldap_append_referral( ld, &parentr->lr_res_error,
616                             lr->lr_res_error );
617                 }
618         } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
619             parentr->lr_res_errno == LDAP_SUCCESS ) {
620                 parentr->lr_res_errno = lr->lr_res_errno;
621                 if ( parentr->lr_res_error != NULL ) {
622                         LDAP_FREE( parentr->lr_res_error );
623                 }
624                 parentr->lr_res_error = lr->lr_res_error;
625                 lr->lr_res_error = NULL;
626                 if ( LDAP_NAME_ERROR( lr->lr_res_errno )) {
627                         if ( parentr->lr_res_matched != NULL ) {
628                                 LDAP_FREE( parentr->lr_res_matched );
629                         }
630                         parentr->lr_res_matched = lr->lr_res_matched;
631                         lr->lr_res_matched = NULL;
632                 }
633         }
634
635         Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info:  ",
636             parentr->lr_msgid, 0, 0 );
637         Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
638             parentr->lr_res_errno, parentr->lr_res_error ?
639             parentr->lr_res_error : "", parentr->lr_res_matched ?
640             parentr->lr_res_matched : "" );
641 }
642
643
644
645 int
646 ldap_msgtype( LDAPMessage *lm )
647 {
648         assert( lm != NULL );
649         return ( lm != NULL ) ? lm->lm_msgtype : -1;
650 }
651
652
653 int
654 ldap_msgid( LDAPMessage *lm )
655 {
656         assert( lm != NULL );
657
658         return ( lm != NULL ) ? lm->lm_msgid : -1;
659 }
660
661
662 int
663 ldap_msgfree( LDAPMessage *lm )
664 {
665         LDAPMessage     *next;
666         int             type = 0;
667
668         assert( lm != NULL );
669
670         Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
671
672         for ( ; lm != NULL; lm = next ) {
673                 next = lm->lm_chain;
674                 type = lm->lm_msgtype;
675                 ber_free( lm->lm_ber, 1 );
676                 LDAP_FREE( (char *) lm );
677         }
678
679         return( type );
680 }
681
682 /*
683  * ldap_msgdelete - delete a message.  It returns:
684  *      0       if the entire message was deleted
685  *      -1      if the message was not found, or only part of it was found
686  */
687 int
688 ldap_msgdelete( LDAP *ld, int msgid )
689 {
690         LDAPMessage     *lm, *prev;
691
692         assert( ld != NULL );
693
694         Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
695
696         prev = NULL;
697         for ( lm = ld->ld_responses; lm != NULL; lm = lm->lm_next ) {
698                 if ( lm->lm_msgid == msgid )
699                         break;
700                 prev = lm;
701         }
702
703         if ( lm == NULL )
704                 return( -1 );
705
706         if ( prev == NULL )
707                 ld->ld_responses = lm->lm_next;
708         else
709                 prev->lm_next = lm->lm_next;
710
711         if ( ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
712                 return( -1 );
713
714         return( 0 );
715 }
716
717
718 /*
719  * return 1 if message msgid is waiting to be abandoned, 0 otherwise
720  */
721 static int
722 ldap_abandoned( LDAP *ld, ber_int_t msgid )
723 {
724         int     i;
725
726         if ( ld->ld_abandoned == NULL )
727                 return( 0 );
728
729         for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
730                 if ( ld->ld_abandoned[i] == msgid )
731                         return( 1 );
732
733         return( 0 );
734 }
735
736
737 static int
738 ldap_mark_abandoned( LDAP *ld, ber_int_t msgid )
739 {
740         int     i;
741
742         if ( ld->ld_abandoned == NULL )
743                 return( -1 );
744
745         for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
746                 if ( ld->ld_abandoned[i] == msgid )
747                         break;
748
749         if ( ld->ld_abandoned[i] == -1 )
750                 return( -1 );
751
752         for ( ; ld->ld_abandoned[i] != -1; i++ ) {
753                 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];
754         }
755
756         return( 0 );
757 }
758
759
760 #ifdef LDAP_CONNECTIONLESS
761 int
762 cldap_getmsg( LDAP *ld, struct timeval *timeout, BerElement *ber )
763 {
764         int     rc;
765         ber_tag_t       tag;
766         ber_len_t       len;
767
768         if ( ! ber_pvt_sb_data_ready(&ld->ld_sb) ) {
769                 /* restored from ldap_select1() in result.c version 1.24 */
770                 fd_set  readfds;
771                 if ( ldap_int_tblsize == 0 )
772                         ldap_int_ip_init();
773                 FD_ZERO( &readfds );
774                 FD_SET( ber_pvt_sb_get_desc(&ld->ld_sb), &readfds );
775                 rc = select( ldap_int_tblsize, &readfds, 0, 0, timeout );
776
777                 if ( rc == -1 || rc == 0 ) {
778                         ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
779                             LDAP_TIMEOUT);
780                         return( rc );
781                 }
782         }
783
784         /* get the next message */
785         if ( (tag = ber_get_next( &ld->ld_sb, &len, ber ))
786             != LDAP_TAG_MESSAGE ) {
787                 ld->ld_errno = (tag == LBER_DEFAULT ? LDAP_SERVER_DOWN :
788                     LDAP_LOCAL_ERROR);
789                 return( -1 );
790         }
791
792         return( 0 );
793 }
794 #endif /* LDAP_CONNECTIONLESS */