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