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