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