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