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