]> git.sur5r.net Git - openldap/blob - libraries/libldap/os-ip.c
407f5ebad2c1030371bc870cfb6f91fa7b8cade6
[openldap] / libraries / libldap / os-ip.c
1 /* os-ip.c -- platform-specific TCP & UDP related code */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2005 The OpenLDAP Foundation.
6  * Portions Copyright 1999 Lars Uffmann.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
18  * All rights reserved.
19  */
20 /* Significant additional contributors include:
21  *    Lars Uffman
22  */
23
24 #include "portable.h"
25
26 #include <stdio.h>
27
28 #include <ac/stdlib.h>
29
30 #include <ac/errno.h>
31 #include <ac/socket.h>
32 #include <ac/string.h>
33 #include <ac/time.h>
34 #include <ac/unistd.h>
35
36 #ifdef HAVE_IO_H
37 #include <io.h>
38 #endif /* HAVE_IO_H */
39
40 #include "ldap-int.h"
41
42 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
43 #  ifdef LDAP_PF_INET6
44 int ldap_int_inet4or6 = AF_UNSPEC;
45 #  else
46 int ldap_int_inet4or6 = AF_INET;
47 #  endif
48 #endif
49
50 #ifdef LDAP_DEBUG
51
52 #define osip_debug(ld,fmt,arg1,arg2,arg3) \
53 do { \
54         ldap_log_printf(NULL, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
55 } while(0)
56
57 #else
58
59 #define osip_debug(ld,fmt,arg1,arg2,arg3) ((void)0)
60
61 #endif /* LDAP_DEBUG */
62
63 static void
64 ldap_pvt_set_errno(int err)
65 {
66         errno = err;
67 }
68
69 int
70 ldap_int_timeval_dup( struct timeval **dest, const struct timeval *src )
71 {
72         struct timeval *new;
73
74         assert( dest != NULL );
75
76         if (src == NULL) {
77                 *dest = NULL;
78                 return 0;
79         }
80
81         new = (struct timeval *) LDAP_MALLOC(sizeof(struct timeval));
82
83         if( new == NULL ) {
84                 *dest = NULL;
85                 return 1;
86         }
87
88         AC_MEMCPY( (char *) new, (const char *) src, sizeof(struct timeval));
89
90         *dest = new;
91         return 0;
92 }
93
94 static int
95 ldap_pvt_ndelay_on(LDAP *ld, int fd)
96 {
97         osip_debug(ld, "ldap_ndelay_on: %d\n",fd,0,0);
98         return ber_pvt_socket_set_nonblock( fd, 1 );
99 }
100    
101 static int
102 ldap_pvt_ndelay_off(LDAP *ld, int fd)
103 {
104         osip_debug(ld, "ldap_ndelay_off: %d\n",fd,0,0);
105         return ber_pvt_socket_set_nonblock( fd, 0 );
106 }
107
108 static ber_socket_t
109 ldap_int_socket(LDAP *ld, int family, int type )
110 {
111         ber_socket_t s = socket(family, type, 0);
112         osip_debug(ld, "ldap_new_socket: %d\n",s,0,0);
113         return ( s );
114 }
115
116 static int
117 ldap_pvt_close_socket(LDAP *ld, int s)
118 {
119         osip_debug(ld, "ldap_close_socket: %d\n",s,0,0);
120         return tcp_close(s);
121 }
122
123 static int
124 ldap_int_prepare_socket(LDAP *ld, int s, int proto )
125 {
126         osip_debug(ld, "ldap_prepare_socket: %d\n", s,0,0);
127
128 #ifdef TCP_NODELAY
129         if( proto == LDAP_PROTO_TCP ) {
130                 int dummy = 1;
131                 if ( setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
132                         (char*) &dummy, sizeof(dummy) ) == AC_SOCKET_ERROR )
133                 {
134                         osip_debug(ld, "ldap_prepare_socket: "
135                                 "setsockopt(%d, TCP_NODELAY) failed (ignored).\n",
136                                 s, 0, 0);
137                 }
138         }
139 #endif
140
141         return 0;
142 }
143
144 #ifndef HAVE_WINSOCK
145
146 #undef TRACE
147 #define TRACE do { \
148         osip_debug(ld, \
149                 "ldap_is_socket_ready: error on socket %d: errno: %d (%s)\n", \
150                 s, \
151                 errno, \
152                 sock_errstr(errno) ); \
153 } while( 0 )
154
155 /*
156  * check the socket for errors after select returned.
157  */
158 static int
159 ldap_pvt_is_socket_ready(LDAP *ld, int s)
160 {
161         osip_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
162
163 #if defined( notyet ) /* && defined( SO_ERROR ) */
164 {
165         int so_errno;
166         socklen_t dummy = sizeof(so_errno);
167         if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
168                 == AC_SOCKET_ERROR )
169         {
170                 return -1;
171         }
172         if ( so_errno ) {
173                 ldap_pvt_set_errno(so_errno);
174                 TRACE;
175                 return -1;
176         }
177         return 0;
178 }
179 #else
180 {
181         /* error slippery */
182 #ifdef LDAP_PF_INET6
183         struct sockaddr_storage sin;
184 #else
185         struct sockaddr_in sin;
186 #endif
187         char ch;
188         socklen_t dummy = sizeof(sin);
189         if ( getpeername( s, (struct sockaddr *) &sin, &dummy )
190                 == AC_SOCKET_ERROR )
191         {
192                 /* XXX: needs to be replace with ber_stream_read() */
193                 read(s, &ch, 1);
194                 TRACE;
195                 return -1;
196         }
197         return 0;
198 }
199 #endif
200         return -1;
201 }
202 #undef TRACE
203
204 #endif /* HAVE_WINSOCK */
205
206 static int
207 ldap_pvt_connect(LDAP *ld, ber_socket_t s,
208         struct sockaddr *sin, socklen_t addrlen,
209         int async)
210 {
211         int rc;
212         struct timeval  tv, *opt_tv=NULL;
213
214 #ifdef LDAP_CONNECTIONLESS
215         /* We could do a connect() but that would interfere with
216          * attempts to poll a broadcast address
217          */
218         if (LDAP_IS_UDP(ld)) {
219                 if (ld->ld_options.ldo_peer)
220                         ldap_memfree(ld->ld_options.ldo_peer);
221                 ld->ld_options.ldo_peer=ldap_memalloc(sizeof(struct sockaddr));
222                 AC_MEMCPY(ld->ld_options.ldo_peer,sin,sizeof(struct sockaddr));
223                 return ( 0 );
224         }
225 #endif
226         if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
227                 tv.tv_usec = opt_tv->tv_usec;
228                 tv.tv_sec = opt_tv->tv_sec;
229         }
230
231         osip_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
232                         s, opt_tv ? tv.tv_sec : -1L, async);
233
234         if ( ldap_pvt_ndelay_on(ld, s) == -1 )
235                 return ( -1 );
236
237         if ( connect(s, sin, addrlen) != AC_SOCKET_ERROR ) {
238                 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
239                         return ( -1 );
240                 return ( 0 );
241         }
242
243 #ifdef HAVE_WINSOCK
244         ldap_pvt_set_errno( WSAGetLastError() );
245 #endif
246
247         if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
248                 return ( -1 );
249         }
250         
251 #ifdef notyet
252         if ( async ) return ( -2 );
253 #endif
254
255 #ifdef HAVE_POLL
256         {
257                 struct pollfd fd;
258                 int timeout = INFTIM;
259
260                 if( opt_tv != NULL ) timeout = TV2MILLISEC( &tv );
261
262                 fd.fd = s;
263                 fd.events = POLL_WRITE;
264
265                 do {
266                         fd.revents = 0;
267                         rc = poll( &fd, 1, timeout );
268                 } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
269                         LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
270
271                 if( rc == AC_SOCKET_ERROR ) return rc;
272
273                 if( fd.revents & POLL_WRITE ) {
274                         if ( ldap_pvt_is_socket_ready(ld, s) == -1 ) return -1;
275                         if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
276                         return ( 0 );
277                 }
278         }
279 #else
280         {
281                 fd_set          wfds, *z=NULL;
282 #ifdef HAVE_WINSOCK
283                 fd_set          efds;
284 #endif
285
286 #if defined( FD_SETSIZE ) && !defined( HAVE_WINSOCK )
287                 if ( s >= FD_SETSIZE ) {
288                         rc = AC_SOCKET_ERROR;
289                         tcp_close( s );
290                         ldap_pvt_set_errno( EMFILE );
291                         return rc;
292                 }
293 #endif
294
295                 do {
296                         FD_ZERO(&wfds);
297                         FD_SET(s, &wfds );
298
299 #ifdef HAVE_WINSOCK
300                         FD_ZERO(&efds);
301                         FD_SET(s, &efds );
302 #endif
303
304                         rc = select(ldap_int_tblsize, z, &wfds,
305 #ifdef HAVE_WINSOCK
306                                 &efds,
307 #else
308                                 z,
309 #endif
310                                 opt_tv ? &tv : NULL);
311                 } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
312                         LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
313
314                 if( rc == AC_SOCKET_ERROR ) return rc;
315
316 #ifdef HAVE_WINSOCK
317                 /* This means the connection failed */
318                 if ( FD_ISSET(s, &efds) ) {
319                     int so_errno;
320                     int dummy = sizeof(so_errno);
321                     if ( getsockopt( s, SOL_SOCKET, SO_ERROR,
322                                 (char *) &so_errno, &dummy ) == AC_SOCKET_ERROR || !so_errno )
323                     {
324                         /* impossible */
325                         so_errno = WSAGetLastError();
326                     }
327                     ldap_pvt_set_errno(so_errno);
328                     osip_debug(ld, "ldap_pvt_connect: error on socket %d: "
329                                "errno: %d (%s)\n", s, errno, sock_errstr(errno));
330                     return -1;
331                 }
332 #endif
333                 if ( FD_ISSET(s, &wfds) ) {
334 #ifndef HAVE_WINSOCK
335                         if ( ldap_pvt_is_socket_ready(ld, s) == -1 ) return -1;
336 #endif
337                         if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
338                         return 0;
339                 }
340         }
341 #endif
342
343         osip_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
344         ldap_pvt_set_errno( ETIMEDOUT );
345         return -1;
346 }
347
348 #ifndef HAVE_INET_ATON
349 int
350 ldap_pvt_inet_aton( const char *host, struct in_addr *in)
351 {
352         unsigned long u = inet_addr( host );
353
354 #ifdef INADDR_NONE
355         if ( u == INADDR_NONE ) return 0;
356 #endif
357         if ( u == 0xffffffffUL || u == (unsigned long) -1L ) return 0;
358
359         in->s_addr = u;
360         return 1;
361 }
362 #endif
363
364
365 int
366 ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
367         int proto,
368         const char *host, int port,
369         int async )
370 {
371         int     rc;
372         int     socktype;
373         ber_socket_t            s = AC_SOCKET_INVALID;
374
375 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
376         char serv[7];
377         int err;
378         struct addrinfo hints, *res, *sai;
379 #else
380         int i;
381         int use_hp = 0;
382         struct hostent *hp = NULL;
383         struct hostent he_buf;
384         struct in_addr in;
385         char *ha_buf=NULL;
386 #endif
387
388         if( host == NULL ) host = "localhost";
389         
390         switch(proto) {
391         case LDAP_PROTO_TCP: socktype = SOCK_STREAM;
392                 osip_debug( ld,
393                         "ldap_connect_to_host: TCP %s:%d\n",
394                         host, port, 0);
395                 break;
396         case LDAP_PROTO_UDP: socktype = SOCK_DGRAM;
397                 osip_debug( ld,
398                         "ldap_connect_to_host: UDP %s:%d\n",
399                         host, port, 0);
400                 break;
401         default:
402                 osip_debug( ld, "ldap_connect_to_host: unknown proto: %d\n",
403                         proto, 0, 0 );
404                 return -1;
405         }
406
407 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
408         memset( &hints, '\0', sizeof(hints) );
409 #ifdef USE_AI_ATTRCONFIG /* FIXME: configure test needed */
410         /* Use AI_ATTRCONFIG only on systems where its known to be needed. */
411         hints.ai_flags = AI_ATTRCONFIG;
412 #endif
413         hints.ai_family = ldap_int_inet4or6;
414         hints.ai_socktype = socktype;
415         snprintf(serv, sizeof serv, "%d", port );
416
417 #ifdef LDAP_R_COMPILE
418         /* most getaddrinfo(3) use non-threadsafe resolver libraries */
419         ldap_pvt_thread_mutex_lock(&ldap_int_resolv_mutex);
420 #endif
421
422         err = getaddrinfo( host, serv, &hints, &res );
423
424 #ifdef LDAP_R_COMPILE
425         ldap_pvt_thread_mutex_unlock(&ldap_int_resolv_mutex);
426 #endif
427
428         if ( err != 0 ) {
429                 osip_debug(ld, "ldap_connect_to_host: getaddrinfo failed: %s\n",
430                         AC_GAI_STRERROR(err), 0, 0);
431                 return -1;
432         }
433         rc = -1;
434
435         for( sai=res; sai != NULL; sai=sai->ai_next) {
436                 if( sai->ai_addr == NULL ) {
437                         osip_debug(ld, "ldap_connect_to_host: getaddrinfo "
438                                 "ai_addr is NULL?\n", 0, 0, 0);
439                         continue;
440                 }
441
442                 /* we assume AF_x and PF_x are equal for all x */
443                 s = ldap_int_socket( ld, sai->ai_family, socktype );
444                 if ( s == AC_SOCKET_INVALID ) {
445                         continue;
446                 }
447
448                 if ( ldap_int_prepare_socket(ld, s, proto ) == -1 ) {
449                         ldap_pvt_close_socket(ld, s);
450                         break;
451                 }
452
453                 switch (sai->ai_family) {
454 #ifdef LDAP_PF_INET6
455                         case AF_INET6: {
456                                 char addr[INET6_ADDRSTRLEN];
457                                 inet_ntop( AF_INET6,
458                                         &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
459                                         addr, sizeof addr);
460                                 osip_debug(ld, "ldap_connect_to_host: Trying %s %s\n", 
461                                         addr, serv, 0);
462                         } break;
463 #endif
464                         case AF_INET: {
465                                 char addr[INET_ADDRSTRLEN];
466                                 inet_ntop( AF_INET,
467                                         &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
468                                         addr, sizeof addr);
469                                 osip_debug(ld, "ldap_connect_to_host: Trying %s:%s\n", 
470                                         addr, serv, 0);
471                         } break;
472                 }
473
474                 rc = ldap_pvt_connect( ld, s,
475                         sai->ai_addr, sai->ai_addrlen, async );
476                 if ( (rc == 0) || (rc == -2) ) {
477                         ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
478                         break;
479                 }
480                 ldap_pvt_close_socket(ld, s);
481         }
482         freeaddrinfo(res);
483
484 #else
485         if (! inet_aton( host, &in ) ) {
486                 int local_h_errno;
487                 rc = ldap_pvt_gethostbyname_a( host, &he_buf, &ha_buf,
488                         &hp, &local_h_errno );
489
490                 if ( (rc < 0) || (hp == NULL) ) {
491 #ifdef HAVE_WINSOCK
492                         ldap_pvt_set_errno( WSAGetLastError() );
493 #else
494                         /* not exactly right, but... */
495                         ldap_pvt_set_errno( EHOSTUNREACH );
496 #endif
497                         if (ha_buf) LDAP_FREE(ha_buf);
498                         return -1;
499                 }
500
501                 use_hp = 1;
502         }
503
504         rc = s = -1;
505         for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
506                 struct sockaddr_in      sin;
507
508                 s = ldap_int_socket( ld, PF_INET, socktype );
509                 if ( s == AC_SOCKET_INVALID ) {
510                         /* use_hp ? continue : break; */
511                         break;
512                 }
513            
514                 if ( ldap_int_prepare_socket( ld, s, proto ) == -1 ) {
515                         ldap_pvt_close_socket(ld, s);
516                         break;
517                 }
518
519                 (void)memset((char *)&sin, '\0', sizeof sin);
520                 sin.sin_family = AF_INET;
521                 sin.sin_port = htons((short) port);
522
523                 if( use_hp ) {
524                         AC_MEMCPY( &sin.sin_addr, hp->h_addr_list[i],
525                                 sizeof(sin.sin_addr) );
526                 } else {
527                         AC_MEMCPY( &sin.sin_addr, &in.s_addr,
528                                 sizeof(sin.sin_addr) );
529                 }
530
531                 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n", 
532                         inet_ntoa(sin.sin_addr), port, 0);
533
534                 rc = ldap_pvt_connect(ld, s,
535                         (struct sockaddr *)&sin, sizeof(sin),
536                         async);
537    
538                 if ( (rc == 0) || (rc == -2) ) {
539                         ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
540                         break;
541                 }
542
543                 ldap_pvt_close_socket(ld, s);
544
545                 if (!use_hp) break;
546         }
547         if (ha_buf) LDAP_FREE(ha_buf);
548 #endif
549
550         return rc;
551 }
552
553 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND ) || \
554         defined( HAVE_CYRUS_SASL )
555 char *
556 ldap_host_connected_to( Sockbuf *sb, const char *host )
557 {
558         socklen_t               len;
559 #ifdef LDAP_PF_INET6
560         struct sockaddr_storage sabuf;
561 #else
562         struct sockaddr sabuf;
563 #endif
564         struct sockaddr *sa = (struct sockaddr *) &sabuf;
565         ber_socket_t    sd;
566
567         (void)memset( (char *)sa, '\0', sizeof sabuf );
568         len = sizeof sabuf;
569
570         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
571         if ( getpeername( sd, sa, &len ) == -1 ) {
572                 return( NULL );
573         }
574
575         /*
576          * do a reverse lookup on the addr to get the official hostname.
577          * this is necessary for kerberos to work right, since the official
578          * hostname is used as the kerberos instance.
579          */
580
581         switch (sa->sa_family) {
582 #ifdef LDAP_PF_LOCAL
583         case AF_LOCAL:
584                 return LDAP_STRDUP( ldap_int_hostname );
585 #endif
586 #ifdef LDAP_PF_INET6
587         case AF_INET6:
588                 {
589                         struct in6_addr localhost = IN6ADDR_LOOPBACK_INIT;
590                         if( memcmp ( &((struct sockaddr_in6 *)sa)->sin6_addr,
591                                 &localhost, sizeof(localhost)) == 0 )
592                         {
593                                 return LDAP_STRDUP( ldap_int_hostname );
594                         }
595                 }
596                 break;
597 #endif
598         case AF_INET:
599                 {
600                         struct in_addr localhost;
601                         localhost.s_addr = htonl( INADDR_ANY );
602
603                         if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
604                                 &localhost, sizeof(localhost) ) == 0 )
605                         {
606                                 return LDAP_STRDUP( ldap_int_hostname );
607                         }
608
609 #ifdef INADDR_LOOPBACK
610                         localhost.s_addr = htonl( INADDR_LOOPBACK );
611
612                         if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
613                                 &localhost, sizeof(localhost) ) == 0 )
614                         {
615                                 return LDAP_STRDUP( ldap_int_hostname );
616                         }
617 #endif
618                 }
619                 break;
620
621         default:
622                 return( NULL );
623                 break;
624         }
625
626         {
627                 char *herr;
628 #ifdef NI_MAXHOST
629                 char hbuf[NI_MAXHOST];
630 #elif defined( MAXHOSTNAMELEN
631                 char hbuf[MAXHOSTNAMELEN];
632 #else
633                 char hbuf[256];
634 #endif
635                 hbuf[0] = 0;
636
637                 if (ldap_pvt_get_hname( sa, len, hbuf, sizeof(hbuf), &herr ) == 0
638                         && hbuf[0] ) 
639                 {
640                         return LDAP_STRDUP( hbuf );   
641                 }
642         }
643
644         return host ? LDAP_STRDUP( host ) : NULL;
645 }
646 #endif
647
648
649 struct selectinfo {
650 #ifdef HAVE_POLL
651         /* for UNIX poll(2) */
652         int si_maxfd;
653         struct pollfd si_fds[FD_SETSIZE];
654 #else
655         /* for UNIX select(2) */
656         fd_set  si_readfds;
657         fd_set  si_writefds;
658         fd_set  si_use_readfds;
659         fd_set  si_use_writefds;
660 #endif
661 };
662
663 void
664 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
665 {
666         struct selectinfo       *sip;
667         ber_socket_t            sd;
668
669         sip = (struct selectinfo *)ld->ld_selectinfo;
670         
671         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
672
673 #ifdef HAVE_POLL
674         /* for UNIX poll(2) */
675         {
676                 int empty=-1;
677                 int i;
678                 for(i=0; i < sip->si_maxfd; i++) {
679                         if( sip->si_fds[i].fd == sd ) {
680                                 sip->si_fds[i].events |= POLL_WRITE;
681                                 return;
682                         }
683                         if( empty==-1 && sip->si_fds[i].fd == -1 ) {
684                                 empty=i;
685                         }
686                 }
687
688                 if( empty == -1 ) {
689                         if( sip->si_maxfd >= FD_SETSIZE ) {
690                                 /* FIXME */
691                                 return;
692                         }
693                         empty = sip->si_maxfd++;
694                 }
695
696                 sip->si_fds[empty].fd = sd;
697                 sip->si_fds[empty].events = POLL_WRITE;
698         }
699 #else
700         /* for UNIX select(2) */
701         if ( !FD_ISSET( sd, &sip->si_writefds )) {
702                 FD_SET( sd, &sip->si_writefds );
703         }
704 #endif
705 }
706
707
708 void
709 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
710 {
711         struct selectinfo       *sip;
712         ber_socket_t            sd;
713
714         sip = (struct selectinfo *)ld->ld_selectinfo;
715
716         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
717
718 #ifdef HAVE_POLL
719         /* for UNIX poll(2) */
720         {
721                 int empty=-1;
722                 int i;
723                 for(i=0; i < sip->si_maxfd; i++) {
724                         if( sip->si_fds[i].fd == sd ) {
725                                 sip->si_fds[i].events |= POLL_READ;
726                                 return;
727                         }
728                         if( empty==-1 && sip->si_fds[i].fd == -1 ) {
729                                 empty=i;
730                         }
731                 }
732
733                 if( empty == -1 ) {
734                         if( sip->si_maxfd >= FD_SETSIZE ) {
735                                 /* FIXME */
736                                 return;
737                         }
738                         empty = sip->si_maxfd++;
739                 }
740
741                 sip->si_fds[empty].fd = sd;
742                 sip->si_fds[empty].events = POLL_READ;
743         }
744 #else
745         /* for UNIX select(2) */
746         if ( !FD_ISSET( sd, &sip->si_readfds )) {
747                 FD_SET( sd, &sip->si_readfds );
748         }
749 #endif
750 }
751
752
753 void
754 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
755 {
756         struct selectinfo       *sip;
757         ber_socket_t            sd;
758
759         sip = (struct selectinfo *)ld->ld_selectinfo;
760
761         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
762
763 #ifdef HAVE_POLL
764         /* for UNIX poll(2) */
765         {
766                 int i;
767                 for(i=0; i < sip->si_maxfd; i++) {
768                         if( sip->si_fds[i].fd == sd ) {
769                                 sip->si_fds[i].fd = -1;
770                         }
771                 }
772         }
773 #else
774         /* for UNIX select(2) */
775         FD_CLR( sd, &sip->si_writefds );
776         FD_CLR( sd, &sip->si_readfds );
777 #endif
778 }
779
780
781 int
782 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
783 {
784         struct selectinfo       *sip;
785         ber_socket_t            sd;
786
787         sip = (struct selectinfo *)ld->ld_selectinfo;
788
789         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
790
791 #ifdef HAVE_POLL
792         /* for UNIX poll(2) */
793         {
794                 int i;
795                 for(i=0; i < sip->si_maxfd; i++) {
796                         if( sip->si_fds[i].fd == sd ) {
797                                 return sip->si_fds[i].revents & POLL_WRITE;
798                         }
799                 }
800
801                 return 0;
802         }
803 #else
804         /* for UNIX select(2) */
805         return( FD_ISSET( sd, &sip->si_use_writefds ));
806 #endif
807 }
808
809
810 int
811 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
812 {
813         struct selectinfo       *sip;
814         ber_socket_t            sd;
815
816         sip = (struct selectinfo *)ld->ld_selectinfo;
817
818         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
819
820 #ifdef HAVE_POLL
821         /* for UNIX poll(2) */
822         {
823                 int i;
824                 for(i=0; i < sip->si_maxfd; i++) {
825                         if( sip->si_fds[i].fd == sd ) {
826                                 return sip->si_fds[i].revents & POLL_READ;
827                         }
828                 }
829
830                 return 0;
831         }
832 #else
833         /* for UNIX select(2) */
834         return( FD_ISSET( sd, &sip->si_use_readfds ));
835 #endif
836 }
837
838
839 void *
840 ldap_new_select_info( void )
841 {
842         struct selectinfo       *sip;
843
844         sip = (struct selectinfo *)LDAP_CALLOC( 1, sizeof( struct selectinfo ));
845
846         if ( sip == NULL ) return NULL;
847
848 #ifdef HAVE_POLL
849         /* for UNIX poll(2) */
850         /* sip->si_maxfd=0 */
851 #else
852         /* for UNIX select(2) */
853         FD_ZERO( &sip->si_readfds );
854         FD_ZERO( &sip->si_writefds );
855 #endif
856
857         return( (void *)sip );
858 }
859
860
861 void
862 ldap_free_select_info( void *sip )
863 {
864         LDAP_FREE( sip );
865 }
866
867
868 #ifndef HAVE_POLL
869 int ldap_int_tblsize = 0;
870
871 void
872 ldap_int_ip_init( void )
873 {
874 #if defined( HAVE_SYSCONF )
875         long tblsize = sysconf( _SC_OPEN_MAX );
876         if( tblsize > INT_MAX ) tblsize = INT_MAX;
877
878 #elif defined( HAVE_GETDTABLESIZE )
879         int tblsize = getdtablesize();
880 #else
881         int tblsize = FD_SETSIZE;
882 #endif /* !USE_SYSCONF */
883
884 #ifdef FD_SETSIZE
885         if( tblsize > FD_SETSIZE ) tblsize = FD_SETSIZE;
886 #endif  /* FD_SETSIZE */
887
888         ldap_int_tblsize = tblsize;
889 }
890 #endif
891
892
893 int
894 ldap_int_select( LDAP *ld, struct timeval *timeout )
895 {
896         int rc;
897         struct selectinfo       *sip;
898
899         Debug( LDAP_DEBUG_TRACE, "ldap_int_select\n", 0, 0, 0 );
900
901 #ifndef HAVE_POLL
902         if ( ldap_int_tblsize == 0 ) ldap_int_ip_init();
903 #endif
904
905         sip = (struct selectinfo *)ld->ld_selectinfo;
906
907 #ifdef HAVE_POLL
908         {
909                 int to = timeout ? TV2MILLISEC( timeout ) : INFTIM;
910                 rc = poll( sip->si_fds, sip->si_maxfd, to );
911         }
912 #else
913         sip->si_use_readfds = sip->si_readfds;
914         sip->si_use_writefds = sip->si_writefds;
915         
916         rc = select( ldap_int_tblsize,
917                 &sip->si_use_readfds, &sip->si_use_writefds,
918                 NULL, timeout );
919 #endif
920
921         return rc;
922 }