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