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