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