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