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