]> git.sur5r.net Git - openldap/blob - libraries/libldap/result.c
81c12f80cee89914d60fc7c0a5045732df0b6424
[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                                 || errno != EINTR )))
201                         {
202                                 ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
203                                     LDAP_TIMEOUT);
204                                 return( rc );
205                         }
206
207                         if ( rc == -1 ) {
208                                 rc = -2;        /* select interrupted: loop */
209                         } else {
210                                 rc = -2;
211                                 for ( lc = ld->ld_conns; rc == -2 && lc != NULL;
212                                     lc = nextlc ) {
213                                         nextlc = lc->lconn_next;
214                                         if ( lc->lconn_status ==
215                                             LDAP_CONNST_CONNECTED &&
216                                             ldap_is_read_ready( ld,
217                                             lc->lconn_sb )) {
218                                                 rc = try_read1msg( ld, msgid, all,
219                                                     lc->lconn_sb, lc, result );
220                                         }
221                                 }
222                         }
223                 }
224
225                 if ( rc == -2 && tvp != NULL ) {
226                         tmp_time = time( NULL );
227                         if (( tv.tv_sec -=  ( tmp_time - start_time )) <= 0 ) {
228                                 rc = 0; /* timed out */
229                                 ld->ld_errno = LDAP_TIMEOUT;
230                                 break;
231                         }
232
233                         Debug( LDAP_DEBUG_TRACE, "wait4msg:  %ld secs to go\n",
234                                (long) tv.tv_sec, 0, 0 );
235                         start_time = tmp_time;
236                 }
237         }
238
239         return( rc );
240 }
241
242
243 static int
244 try_read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb,
245     LDAPConn *lc, LDAPMessage **result )
246 {
247         BerElement      *ber;
248         LDAPMessage     *new, *l, *prev, *tmp;
249         long            id;
250         unsigned long   tag, len;
251         int             foundit = 0;
252         LDAPRequest     *lr;
253         BerElement      tmpber;
254         int             rc, refer_cnt, hadref, simple_request;
255         unsigned long   lderr;
256
257         assert( ld != NULL );
258         assert( lc != NULL );
259         
260         Debug( LDAP_DEBUG_TRACE, "read1msg\n", 0, 0, 0 );
261
262     if ( lc->lconn_ber == NULLBER ) {
263                 lc->lconn_ber = ldap_alloc_ber_with_options(ld);
264
265                 if( lc->lconn_ber == NULL ) {
266                         return -1;
267                 }
268     }
269
270         ber = lc->lconn_ber;
271         assert( BER_VALID (ber) );
272
273         /* get the next message */
274         errno = 0;
275         if ( (tag = ber_get_next( sb, &len, ber ))
276             != LDAP_TAG_MESSAGE ) {
277                 if ( tag == LBER_DEFAULT) {
278 #ifdef LDAP_DEBUG                  
279                         Debug( LDAP_DEBUG_CONNS,
280                               "ber_get_next failed.\n", 0, 0, 0 );
281 #endif             
282 #ifdef EWOULDBLOCK                      
283                         if (errno==EWOULDBLOCK) return -2;
284 #endif
285 #ifdef EAGAIN
286                         if (errno == EAGAIN) return -2;
287 #endif
288                         ld->ld_errno = LDAP_SERVER_DOWN;
289                         return -1;
290                 }
291                 ld->ld_errno = LDAP_LOCAL_ERROR;
292                 return -1;
293         }
294
295         /*
296      * We read a complete message.
297          * The connection should no longer need this ber.
298          */
299     lc->lconn_ber = NULLBER;
300
301         /* message id */
302         if ( ber_get_int( ber, &id ) == LBER_ERROR ) {
303                 ber_free( ber, 1 );
304                 ld->ld_errno = LDAP_DECODING_ERROR;
305                 return( -1 );
306         }
307
308         /* if it's been abandoned, toss it */
309         if ( ldap_abandoned( ld, (int)id ) ) {
310                 ber_free( ber, 1 );
311                 return( -2 );   /* continue looking */
312         }
313
314         if (( lr = ldap_find_request_by_msgid( ld, id )) == NULL ) {
315                 Debug( LDAP_DEBUG_ANY,
316                     "no request for response with msgid %ld (tossing)\n",
317                     id, 0, 0 );
318                 ber_free( ber, 1 );
319                 return( -2 );   /* continue looking */
320         }
321
322         /* the message type */
323         if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
324                 ld->ld_errno = LDAP_DECODING_ERROR;
325                 ber_free( ber, 1 );
326                 return( -1 );
327         }
328
329         Debug( LDAP_DEBUG_TRACE, "ldap_read: %s msgid %ld, original id %d\n",
330             ( tag == LDAP_RES_SEARCH_ENTRY ) ? "entry" : 
331                 ( tag == LDAP_RES_SEARCH_REFERENCE ) ? "reference" : "result",
332                 id, lr->lr_origid );
333
334         id = lr->lr_origid;
335         refer_cnt = 0;
336         hadref = simple_request = 0;
337         rc = -2;        /* default is to keep looking (no response found) */
338         lr->lr_res_msgtype = tag;
339
340         if ( tag != LDAP_RES_SEARCH_ENTRY ) {
341                 if ( ld->ld_version >= LDAP_VERSION2 &&
342                         ( lr->lr_parent != NULL ||
343                         LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ) )
344                 {
345                         tmpber = *ber;  /* struct copy */
346                         if ( ber_scanf( &tmpber, "{iaa}", &lderr,
347                             &lr->lr_res_matched, &lr->lr_res_error )
348                             != LBER_ERROR ) {
349                                 if ( lderr != LDAP_SUCCESS ) {
350                                         /* referrals are in error string */
351                                         refer_cnt = ldap_chase_referrals( ld, lr,
352                                             &lr->lr_res_error, &hadref );
353                                 }
354
355                                 /* save errno, message, and matched string */
356                                 if ( !hadref || lr->lr_res_error == NULL ) {
357                                         lr->lr_res_errno = ( lderr ==
358                                         LDAP_PARTIAL_RESULTS ) ? LDAP_SUCCESS
359                                         : lderr;
360                                 } else if ( ld->ld_errno != LDAP_SUCCESS ) {
361                                         lr->lr_res_errno = ld->ld_errno;
362                                 } else {
363                                         lr->lr_res_errno = LDAP_PARTIAL_RESULTS;
364                                 }
365 Debug( LDAP_DEBUG_TRACE,
366     "new result:  res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
367     lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
368     lr->lr_res_matched ? lr->lr_res_matched : "" );
369                         }
370                 }
371
372                 Debug( LDAP_DEBUG_TRACE,
373                     "read1msg:  %d new referrals\n", refer_cnt, 0, 0 );
374
375                 if ( refer_cnt != 0 ) { /* chasing referrals */
376                         ber_free( ber, 1 );
377                         ber = NULL;
378                         if ( refer_cnt < 0 ) {
379                                 return( -1 );   /* fatal error */
380                         }
381                         lr->lr_status = LDAP_REQST_CHASINGREFS;
382                 } else {
383                         if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
384                                 /* request without any referrals */
385                                 simple_request = ( hadref ? 0 : 1 );
386                         } else {
387                                 /* request with referrals or child request */
388                                 ber_free( ber, 1 );
389                                 ber = NULL;
390                         }
391
392                         while ( lr->lr_parent != NULL ) {
393                                 merge_error_info( ld, lr->lr_parent, lr );
394
395                                 lr = lr->lr_parent;
396                                 if ( --lr->lr_outrefcnt > 0 ) {
397                                         break;  /* not completely done yet */
398                                 }
399                         }
400
401                         if ( lr->lr_outrefcnt <= 0 && lr->lr_parent == NULL ) {
402                                 id = lr->lr_msgid;
403                                 tag = lr->lr_res_msgtype;
404                                 Debug( LDAP_DEBUG_ANY, "request %ld done\n",
405                                     id, 0, 0 );
406 Debug( LDAP_DEBUG_TRACE,
407 "res_errno: %d, res_error: <%s>, res_matched: <%s>\n",
408 lr->lr_res_errno, lr->lr_res_error ? lr->lr_res_error : "",
409 lr->lr_res_matched ? lr->lr_res_matched : "" );
410                                 if ( !simple_request ) {
411                                         ber_free( ber, 1 );
412                                         ber = NULL;
413                                         if ( build_result_ber( ld, ber, lr )
414                                             == LBER_ERROR ) {
415                                                 ld->ld_errno = LDAP_NO_MEMORY;
416                                                 rc = -1; /* fatal error */
417                                         }
418                                 }
419
420                                 ldap_free_request( ld, lr );
421                         }
422
423                         if ( lc != NULL ) {
424                                 ldap_free_connection( ld, lc, 0, 1 );
425                         }
426                 }
427         }
428
429         if ( ber == NULL ) {
430                 return( rc );
431         }
432
433         /* make a new ldap message */
434         if ( (new = (LDAPMessage *) LDAP_CALLOC( 1, sizeof(LDAPMessage) ))
435             == NULL ) {
436                 ld->ld_errno = LDAP_NO_MEMORY;
437                 return( -1 );
438         }
439         new->lm_msgid = (int)id;
440         new->lm_msgtype = tag;
441         new->lm_ber = ber;
442
443 #ifndef LDAP_NOCACHE
444                 if ( ld->ld_cache != NULL ) {
445                         ldap_add_result_to_cache( ld, new );
446                 }
447 #endif /* LDAP_NOCACHE */
448
449         /* is this the one we're looking for? */
450         if ( msgid == LDAP_RES_ANY || id == msgid ) {
451                 if ( all == 0
452                     || (new->lm_msgtype != LDAP_RES_SEARCH_RESULT
453                     && new->lm_msgtype != LDAP_RES_SEARCH_ENTRY) ) {
454                         *result = new;
455                         ld->ld_errno = LDAP_SUCCESS;
456                         return( tag );
457                 } else if ( new->lm_msgtype == LDAP_RES_SEARCH_RESULT) {
458                         foundit = 1;    /* return the chain later */
459                 }
460         }
461
462         /* 
463          * if not, we must add it to the list of responses.  if
464          * the msgid is already there, it must be part of an existing
465          * search response.
466          */
467
468         prev = NULLMSG;
469         for ( l = ld->ld_responses; l != NULLMSG; l = l->lm_next ) {
470                 if ( l->lm_msgid == new->lm_msgid )
471                         break;
472                 prev = l;
473         }
474
475         /* not part of an existing search response */
476         if ( l == NULLMSG ) {
477                 if ( foundit ) {
478                         *result = new;
479                         ld->ld_errno = LDAP_SUCCESS;
480                         return( tag );
481                 }
482
483                 new->lm_next = ld->ld_responses;
484                 ld->ld_responses = new;
485                 return( -2 );   /* continue looking */
486         }
487
488         Debug( LDAP_DEBUG_TRACE, "adding response id %d type %d:\n",
489             new->lm_msgid, new->lm_msgtype, 0 );
490
491         /* part of a search response - add to end of list of entries */
492         for ( tmp = l; tmp->lm_chain != NULLMSG &&
493             tmp->lm_chain->lm_msgtype == LDAP_RES_SEARCH_ENTRY;
494             tmp = tmp->lm_chain )
495                 ;       /* NULL */
496         tmp->lm_chain = new;
497
498         /* return the whole chain if that's what we were looking for */
499         if ( foundit ) {
500                 if ( prev == NULLMSG )
501                         ld->ld_responses = l->lm_next;
502                 else
503                         prev->lm_next = l->lm_next;
504                 *result = l;
505                 ld->ld_errno = LDAP_SUCCESS;
506 #ifdef LDAP_WORLD_P16
507                 /*
508                  * XXX questionable fix; see text for [P16] on
509                  * http://www.critical-angle.com/ldapworld/patch/
510                  *
511                  * inclusion of this patch causes searchs to hang on
512                  * multiple platforms
513                  */
514                 return( l->lm_msgtype );
515 #else   /* LDAP_WORLD_P16 */
516                 return( tag );
517 #endif  /* !LDAP_WORLD_P16 */
518         }
519
520         return( -2 );   /* continue looking */
521 }
522
523
524 static unsigned long
525 build_result_ber( LDAP *ld, BerElement *ber, LDAPRequest *lr )
526 {
527         unsigned long   len;
528         long            along;
529
530         ber_init_w_nullc( ber, 0 );
531         ldap_set_ber_options( ld, ber );
532         if ( ber_printf( ber, "{it{ess}}", lr->lr_msgid,
533             (unsigned long) lr->lr_res_msgtype, lr->lr_res_errno,
534             lr->lr_res_matched ? lr->lr_res_matched : "",
535             lr->lr_res_error ? lr->lr_res_error : "" ) == -1 ) {
536                 return( LBER_ERROR );
537         }
538
539         ber_reset( ber, 1 );
540         if ( ber_skip_tag( ber, &len ) == LBER_ERROR ) {
541                 return( LBER_ERROR );
542         }
543
544         if ( ber_get_int( ber, &along ) == LBER_ERROR ) {
545                 return( LBER_ERROR );
546         }
547
548         return( ber_peek_tag( ber, &len ));
549 }
550
551
552 static void
553 merge_error_info( LDAP *ld, LDAPRequest *parentr, LDAPRequest *lr )
554 {
555 /*
556  * Merge error information in "lr" with "parentr" error code and string.
557  */
558         if ( lr->lr_res_errno == LDAP_PARTIAL_RESULTS ) {
559                 parentr->lr_res_errno = lr->lr_res_errno;
560                 if ( lr->lr_res_error != NULL ) {
561                         (void)ldap_append_referral( ld, &parentr->lr_res_error,
562                             lr->lr_res_error );
563                 }
564         } else if ( lr->lr_res_errno != LDAP_SUCCESS &&
565             parentr->lr_res_errno == LDAP_SUCCESS ) {
566                 parentr->lr_res_errno = lr->lr_res_errno;
567                 if ( parentr->lr_res_error != NULL ) {
568                         LDAP_FREE( parentr->lr_res_error );
569                 }
570                 parentr->lr_res_error = lr->lr_res_error;
571                 lr->lr_res_error = NULL;
572                 if ( LDAP_NAME_ERROR( lr->lr_res_errno )) {
573                         if ( parentr->lr_res_matched != NULL ) {
574                                 LDAP_FREE( parentr->lr_res_matched );
575                         }
576                         parentr->lr_res_matched = lr->lr_res_matched;
577                         lr->lr_res_matched = NULL;
578                 }
579         }
580
581         Debug( LDAP_DEBUG_TRACE, "merged parent (id %d) error info:  ",
582             parentr->lr_msgid, 0, 0 );
583         Debug( LDAP_DEBUG_TRACE, "result errno %d, error <%s>, matched <%s>\n",
584             parentr->lr_res_errno, parentr->lr_res_error ?
585             parentr->lr_res_error : "", parentr->lr_res_matched ?
586             parentr->lr_res_matched : "" );
587 }
588
589
590
591 int
592 ldap_msgtype( LDAPMessage *lm )
593 {
594         assert( lm != NULL );
595         return ( lm == NULL ) ? lm->lm_msgtype : -1;
596 }
597
598
599 int
600 ldap_msgid( LDAPMessage *lm )
601 {
602         assert( lm != NULL );
603
604         return ( lm == NULL ) ? lm->lm_msgid : -1;
605 }
606
607
608 int
609 ldap_msgfree( LDAPMessage *lm )
610 {
611         LDAPMessage     *next;
612         int             type = 0;
613
614         assert( lm != NULL );
615
616         Debug( LDAP_DEBUG_TRACE, "ldap_msgfree\n", 0, 0, 0 );
617
618         for ( ; lm != NULLMSG; lm = next ) {
619                 next = lm->lm_chain;
620                 type = lm->lm_msgtype;
621                 ber_free( lm->lm_ber, 1 );
622                 LDAP_FREE( (char *) lm );
623         }
624
625         return( type );
626 }
627
628 /*
629  * ldap_msgdelete - delete a message.  It returns:
630  *      0       if the entire message was deleted
631  *      -1      if the message was not found, or only part of it was found
632  */
633 int
634 ldap_msgdelete( LDAP *ld, int msgid )
635 {
636         LDAPMessage     *lm, *prev;
637
638         assert( ld != NULL );
639
640         Debug( LDAP_DEBUG_TRACE, "ldap_msgdelete\n", 0, 0, 0 );
641
642         prev = NULLMSG;
643         for ( lm = ld->ld_responses; lm != NULLMSG; lm = lm->lm_next ) {
644                 if ( lm->lm_msgid == msgid )
645                         break;
646                 prev = lm;
647         }
648
649         if ( lm == NULLMSG )
650                 return( -1 );
651
652         if ( prev == NULLMSG )
653                 ld->ld_responses = lm->lm_next;
654         else
655                 prev->lm_next = lm->lm_next;
656
657         if ( ldap_msgfree( lm ) == LDAP_RES_SEARCH_ENTRY )
658                 return( -1 );
659
660         return( 0 );
661 }
662
663
664 /*
665  * return 1 if message msgid is waiting to be abandoned, 0 otherwise
666  */
667 static int
668 ldap_abandoned( LDAP *ld, int msgid )
669 {
670         int     i;
671
672         if ( ld->ld_abandoned == NULL )
673                 return( 0 );
674
675         for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
676                 if ( ld->ld_abandoned[i] == msgid )
677                         return( 1 );
678
679         return( 0 );
680 }
681
682
683 static int
684 ldap_mark_abandoned( LDAP *ld, int msgid )
685 {
686         int     i;
687
688         if ( ld->ld_abandoned == NULL )
689                 return( -1 );
690
691         for ( i = 0; ld->ld_abandoned[i] != -1; i++ )
692                 if ( ld->ld_abandoned[i] == msgid )
693                         break;
694
695         if ( ld->ld_abandoned[i] == -1 )
696                 return( -1 );
697
698         for ( ; ld->ld_abandoned[i] != -1; i++ ) {
699                 ld->ld_abandoned[i] = ld->ld_abandoned[i + 1];
700         }
701
702         return( 0 );
703 }
704
705
706 #ifdef LDAP_CONNECTIONLESS
707 int
708 cldap_getmsg( LDAP *ld, struct timeval *timeout, BerElement *ber )
709 {
710         int             rc;
711         unsigned long   tag, len;
712
713         if ( ! ber_pvt_sb_data_ready(&ld->ld_sb) ) {
714                 rc = ldap_select1( ld, timeout );
715                 if ( rc == -1 || rc == 0 ) {
716                         ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
717                             LDAP_TIMEOUT);
718                         return( rc );
719                 }
720         }
721
722         /* get the next message */
723         if ( (tag = ber_get_next( &ld->ld_sb, &len, ber ))
724             != LDAP_TAG_MESSAGE ) {
725                 ld->ld_errno = (tag == LBER_DEFAULT ? LDAP_SERVER_DOWN :
726                     LDAP_LOCAL_ERROR);
727                 return( -1 );
728         }
729
730         return( tag );
731 }
732 #endif /* LDAP_CONNECTIONLESS */