1 /* os-ip.c -- platform-specific TCP & UDP related code */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2016 The OpenLDAP Foundation.
6 * Portions Copyright 1999 Lars Uffmann.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
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>.
17 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
18 * All rights reserved.
20 /* Significant additional contributors include:
28 #include <ac/stdlib.h>
31 #include <ac/socket.h>
32 #include <ac/string.h>
34 #include <ac/unistd.h>
38 #endif /* HAVE_IO_H */
45 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
47 int ldap_int_inet4or6 = AF_UNSPEC;
49 int ldap_int_inet4or6 = AF_INET;
55 #define osip_debug(ld,fmt,arg1,arg2,arg3) \
57 ldap_log_printf(NULL, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
62 #define osip_debug(ld,fmt,arg1,arg2,arg3) ((void)0)
64 #endif /* LDAP_DEBUG */
67 ldap_pvt_set_errno(int err)
73 ldap_int_timeval_dup( struct timeval **dest, const struct timeval *src )
77 assert( dest != NULL );
84 new = (struct timeval *) LDAP_MALLOC(sizeof(struct timeval));
91 AC_MEMCPY( (char *) new, (const char *) src, sizeof(struct timeval));
98 ldap_pvt_ndelay_on(LDAP *ld, int fd)
100 osip_debug(ld, "ldap_ndelay_on: %d\n",fd,0,0);
101 return ber_pvt_socket_set_nonblock( fd, 1 );
105 ldap_pvt_ndelay_off(LDAP *ld, int fd)
107 osip_debug(ld, "ldap_ndelay_off: %d\n",fd,0,0);
108 return ber_pvt_socket_set_nonblock( fd, 0 );
112 ldap_int_socket(LDAP *ld, int family, int type )
114 ber_socket_t s = socket(family, type, 0);
115 osip_debug(ld, "ldap_new_socket: %d\n",s,0,0);
117 fcntl(s, F_SETFD, FD_CLOEXEC);
123 ldap_pvt_close_socket(LDAP *ld, int s)
125 osip_debug(ld, "ldap_close_socket: %d\n",s,0,0);
130 ldap_int_prepare_socket(LDAP *ld, int s, int proto )
132 osip_debug( ld, "ldap_prepare_socket: %d\n", s, 0, 0 );
134 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
135 if ( proto == LDAP_PROTO_TCP ) {
138 if ( setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
139 (char*) &dummy, sizeof(dummy) ) == AC_SOCKET_ERROR )
141 osip_debug( ld, "ldap_prepare_socket: "
142 "setsockopt(%d, SO_KEEPALIVE) failed (ignored).\n",
145 if ( ld->ld_options.ldo_keepalive_idle > 0 )
148 if ( setsockopt( s, IPPROTO_TCP, TCP_KEEPIDLE,
149 (void*) &ld->ld_options.ldo_keepalive_idle,
150 sizeof(ld->ld_options.ldo_keepalive_idle) ) == AC_SOCKET_ERROR )
152 osip_debug( ld, "ldap_prepare_socket: "
153 "setsockopt(%d, TCP_KEEPIDLE) failed (ignored).\n",
157 osip_debug( ld, "ldap_prepare_socket: "
158 "sockopt TCP_KEEPIDLE not supported on this system.\n",
160 #endif /* TCP_KEEPIDLE */
162 if ( ld->ld_options.ldo_keepalive_probes > 0 )
165 if ( setsockopt( s, IPPROTO_TCP, TCP_KEEPCNT,
166 (void*) &ld->ld_options.ldo_keepalive_probes,
167 sizeof(ld->ld_options.ldo_keepalive_probes) ) == AC_SOCKET_ERROR )
169 osip_debug( ld, "ldap_prepare_socket: "
170 "setsockopt(%d, TCP_KEEPCNT) failed (ignored).\n",
174 osip_debug( ld, "ldap_prepare_socket: "
175 "sockopt TCP_KEEPCNT not supported on this system.\n",
177 #endif /* TCP_KEEPCNT */
179 if ( ld->ld_options.ldo_keepalive_interval > 0 )
182 if ( setsockopt( s, IPPROTO_TCP, TCP_KEEPINTVL,
183 (void*) &ld->ld_options.ldo_keepalive_interval,
184 sizeof(ld->ld_options.ldo_keepalive_interval) ) == AC_SOCKET_ERROR )
186 osip_debug( ld, "ldap_prepare_socket: "
187 "setsockopt(%d, TCP_KEEPINTVL) failed (ignored).\n",
191 osip_debug( ld, "ldap_prepare_socket: "
192 "sockopt TCP_KEEPINTVL not supported on this system.\n",
194 #endif /* TCP_KEEPINTVL */
196 #endif /* SO_KEEPALIVE */
198 if ( setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
199 (char*) &dummy, sizeof(dummy) ) == AC_SOCKET_ERROR )
201 osip_debug( ld, "ldap_prepare_socket: "
202 "setsockopt(%d, TCP_NODELAY) failed (ignored).\n",
205 #endif /* TCP_NODELAY */
207 #endif /* SO_KEEPALIVE || TCP_NODELAY */
217 "ldap_is_socket_ready: error on socket %d: errno: %d (%s)\n", \
220 sock_errstr(errno) ); \
224 * check the socket for errors after select returned.
227 ldap_pvt_is_socket_ready(LDAP *ld, int s)
229 osip_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
231 #if defined( notyet ) /* && defined( SO_ERROR ) */
234 ber_socklen_t dummy = sizeof(so_errno);
235 if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
241 ldap_pvt_set_errno(so_errno);
251 struct sockaddr_storage sin;
253 struct sockaddr_in sin;
256 ber_socklen_t dummy = sizeof(sin);
257 if ( getpeername( s, (struct sockaddr *) &sin, &dummy )
260 /* XXX: needs to be replace with ber_stream_read() */
261 (void)read(s, &ch, 1);
272 #endif /* HAVE_WINSOCK */
274 /* NOTE: this is identical to analogous code in os-local.c */
285 osip_debug(ld, "ldap_int_poll: fd: %d tm: %ld\n",
286 s, tvp ? tvp->tv_sec : -1L, 0);
291 int timeout = INFTIM;
292 short event = wr ? POLL_WRITE : POLL_READ;
298 timeout = TV2MILLISEC( tvp );
302 rc = poll( &fd, 1, timeout );
304 } while ( rc == AC_SOCKET_ERROR && errno == EINTR &&
305 LDAP_BOOL_GET( &ld->ld_options, LDAP_BOOL_RESTART ) );
307 if ( rc == AC_SOCKET_ERROR ) {
311 if ( timeout == 0 && rc == 0 ) {
315 if ( fd.revents & event ) {
316 if ( ldap_pvt_is_socket_ready( ld, s ) == -1 ) {
320 if ( ldap_pvt_ndelay_off( ld, s ) == -1 ) {
328 fd_set wfds, *z = NULL;
332 struct timeval tv = { 0 };
334 #if defined( FD_SETSIZE ) && !defined( HAVE_WINSOCK )
335 if ( s >= FD_SETSIZE ) {
336 rc = AC_SOCKET_ERROR;
338 ldap_pvt_set_errno( EMFILE );
356 rc = select( ldap_int_tblsize, z, &wfds,
363 } while ( rc == AC_SOCKET_ERROR && errno == EINTR &&
364 LDAP_BOOL_GET( &ld->ld_options, LDAP_BOOL_RESTART ) );
366 if ( rc == AC_SOCKET_ERROR ) {
370 if ( rc == 0 && tvp && tvp->tv_sec == 0 && tvp->tv_usec == 0 ) {
375 /* This means the connection failed */
376 if ( FD_ISSET(s, &efds) ) {
378 ber_socklen_t dummy = sizeof(so_errno);
379 if ( getsockopt( s, SOL_SOCKET, SO_ERROR,
380 (char *) &so_errno, &dummy ) == AC_SOCKET_ERROR || !so_errno )
383 so_errno = WSAGetLastError();
385 ldap_pvt_set_errno( so_errno );
386 osip_debug(ld, "ldap_int_poll: error on socket %d: "
387 "errno: %d (%s)\n", s, errno, sock_errstr( errno ));
391 if ( FD_ISSET(s, &wfds) ) {
393 if ( ldap_pvt_is_socket_ready( ld, s ) == -1 ) {
397 if ( ldap_pvt_ndelay_off(ld, s) == -1 ) {
405 osip_debug(ld, "ldap_int_poll: timed out\n",0,0,0);
406 ldap_pvt_set_errno( ETIMEDOUT );
411 ldap_pvt_connect(LDAP *ld, ber_socket_t s,
412 struct sockaddr *sin, ber_socklen_t addrlen,
416 struct timeval tv, *opt_tv = NULL;
418 #ifdef LDAP_CONNECTIONLESS
419 /* We could do a connect() but that would interfere with
420 * attempts to poll a broadcast address
422 if (LDAP_IS_UDP(ld)) {
423 if (ld->ld_options.ldo_peer)
424 ldap_memfree(ld->ld_options.ldo_peer);
425 ld->ld_options.ldo_peer=ldap_memcalloc(1, sizeof(struct sockaddr_storage));
426 AC_MEMCPY(ld->ld_options.ldo_peer,sin,addrlen);
430 if ( ld->ld_options.ldo_tm_net.tv_sec >= 0 ) {
431 tv = ld->ld_options.ldo_tm_net;
435 osip_debug(ld, "ldap_pvt_connect: fd: %d tm: %ld async: %d\n",
436 s, opt_tv ? tv.tv_sec : -1L, async);
438 if ( opt_tv && ldap_pvt_ndelay_on(ld, s) == -1 )
442 osip_debug(ld, "attempting to connect: \n", 0, 0, 0);
443 if ( connect(s, sin, addrlen) != AC_SOCKET_ERROR ) {
444 osip_debug(ld, "connect success\n", 0, 0, 0);
446 if ( opt_tv && ldap_pvt_ndelay_off(ld, s) == -1 )
451 osip_debug(ld, "connect errno: %d\n", err, 0, 0);
453 } while(err == EINTR &&
454 LDAP_BOOL_GET( &ld->ld_options, LDAP_BOOL_RESTART ));
456 if ( err != EINPROGRESS && err != EWOULDBLOCK ) {
461 /* caller will call ldap_int_poll() as appropriate? */
465 rc = ldap_int_poll( ld, s, opt_tv, 1 );
467 osip_debug(ld, "ldap_pvt_connect: %d\n", rc, 0, 0);
472 #ifndef HAVE_INET_ATON
474 ldap_pvt_inet_aton( const char *host, struct in_addr *in)
476 unsigned long u = inet_addr( host );
479 if ( u == INADDR_NONE ) return 0;
481 if ( u == 0xffffffffUL || u == (unsigned long) -1L ) return 0;
489 ldap_int_connect_cbs(LDAP *ld, Sockbuf *sb, ber_socket_t *s, LDAPURLDesc *srv, struct sockaddr *addr)
491 struct ldapoptions *lo;
496 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, s );
498 /* Invoke all handle-specific callbacks first */
499 lo = &ld->ld_options;
500 for (ll = lo->ldo_conn_cbs; ll; ll = ll->ll_next) {
502 rc = cb->lc_add( ld, sb, srv, addr, cb );
503 /* on any failure, call the teardown functions for anything
504 * that previously succeeded
508 for (l2 = lo->ldo_conn_cbs; l2 != ll; l2 = l2->ll_next) {
510 cb->lc_del( ld, sb, cb );
512 /* a failure might have implicitly closed the fd */
513 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, s );
517 lo = LDAP_INT_GLOBAL_OPT();
518 for (ll = lo->ldo_conn_cbs; ll; ll = ll->ll_next) {
520 rc = cb->lc_add( ld, sb, srv, addr, cb );
523 for (l2 = lo->ldo_conn_cbs; l2 != ll; l2 = l2->ll_next) {
525 cb->lc_del( ld, sb, cb );
527 lo = &ld->ld_options;
528 for (l2 = lo->ldo_conn_cbs; l2; l2 = l2->ll_next) {
530 cb->lc_del( ld, sb, cb );
532 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, s );
540 ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
541 int proto, LDAPURLDesc *srv,
546 ber_socket_t s = AC_SOCKET_INVALID;
549 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
552 struct addrinfo hints, *res, *sai;
556 struct hostent *hp = NULL;
557 struct hostent he_buf;
562 if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
565 host = srv->lud_host;
568 port = srv->lud_port;
571 if( strcmp(srv->lud_scheme, "ldaps") == 0 ) {
579 case LDAP_PROTO_TCP: socktype = SOCK_STREAM;
581 "ldap_connect_to_host: TCP %s:%d\n",
584 case LDAP_PROTO_UDP: socktype = SOCK_DGRAM;
586 "ldap_connect_to_host: UDP %s:%d\n",
590 osip_debug( ld, "ldap_connect_to_host: unknown proto: %d\n",
595 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
596 memset( &hints, '\0', sizeof(hints) );
597 #ifdef USE_AI_ADDRCONFIG /* FIXME: configure test needed */
598 /* Use AI_ADDRCONFIG only on systems where its known to be needed. */
599 hints.ai_flags = AI_ADDRCONFIG;
601 hints.ai_family = ldap_int_inet4or6;
602 hints.ai_socktype = socktype;
603 snprintf(serv, sizeof serv, "%d", port );
605 /* most getaddrinfo(3) use non-threadsafe resolver libraries */
606 LDAP_MUTEX_LOCK(&ldap_int_resolv_mutex);
608 err = getaddrinfo( host, serv, &hints, &res );
610 LDAP_MUTEX_UNLOCK(&ldap_int_resolv_mutex);
613 osip_debug(ld, "ldap_connect_to_host: getaddrinfo failed: %s\n",
614 AC_GAI_STRERROR(err), 0, 0);
619 for( sai=res; sai != NULL; sai=sai->ai_next) {
620 if( sai->ai_addr == NULL ) {
621 osip_debug(ld, "ldap_connect_to_host: getaddrinfo "
622 "ai_addr is NULL?\n", 0, 0, 0);
626 /* we assume AF_x and PF_x are equal for all x */
627 s = ldap_int_socket( ld, sai->ai_family, socktype );
628 if ( s == AC_SOCKET_INVALID ) {
632 if ( ldap_int_prepare_socket(ld, s, proto ) == -1 ) {
633 ldap_pvt_close_socket(ld, s);
637 switch (sai->ai_family) {
640 char addr[INET6_ADDRSTRLEN];
642 &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
644 osip_debug(ld, "ldap_connect_to_host: Trying %s %s\n",
649 char addr[INET_ADDRSTRLEN];
651 &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
653 osip_debug(ld, "ldap_connect_to_host: Trying %s:%s\n",
658 rc = ldap_pvt_connect( ld, s,
659 sai->ai_addr, sai->ai_addrlen, async );
660 if ( rc == 0 || rc == -2 ) {
661 err = ldap_int_connect_cbs( ld, sb, &s, srv, sai->ai_addr );
667 ldap_pvt_close_socket(ld, s);
672 if (! inet_aton( host, &in ) ) {
674 rc = ldap_pvt_gethostbyname_a( host, &he_buf, &ha_buf,
675 &hp, &local_h_errno );
677 if ( (rc < 0) || (hp == NULL) ) {
679 ldap_pvt_set_errno( WSAGetLastError() );
681 /* not exactly right, but... */
682 ldap_pvt_set_errno( EHOSTUNREACH );
684 if (ha_buf) LDAP_FREE(ha_buf);
692 for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
693 struct sockaddr_in sin;
695 s = ldap_int_socket( ld, PF_INET, socktype );
696 if ( s == AC_SOCKET_INVALID ) {
697 /* use_hp ? continue : break; */
701 if ( ldap_int_prepare_socket( ld, s, proto ) == -1 ) {
702 ldap_pvt_close_socket(ld, s);
706 (void)memset((char *)&sin, '\0', sizeof sin);
707 sin.sin_family = AF_INET;
708 sin.sin_port = htons((unsigned short) port);
711 AC_MEMCPY( &sin.sin_addr, hp->h_addr_list[i],
712 sizeof(sin.sin_addr) );
714 AC_MEMCPY( &sin.sin_addr, &in.s_addr,
715 sizeof(sin.sin_addr) );
718 #ifdef HAVE_INET_NTOA_B
721 char address[INET_ADDR_LEN];
722 inet_ntoa_b(sin.sin_address, address);
723 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n",
727 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n",
728 inet_ntoa(sin.sin_addr), port, 0);
731 rc = ldap_pvt_connect(ld, s,
732 (struct sockaddr *)&sin, sizeof(sin),
735 if ( (rc == 0) || (rc == -2) ) {
736 int err = ldap_int_connect_cbs( ld, sb, &s, srv, (struct sockaddr *)&sin );
743 ldap_pvt_close_socket(ld, s);
747 if (ha_buf) LDAP_FREE(ha_buf);
753 #if defined( HAVE_CYRUS_SASL )
755 ldap_host_connected_to( Sockbuf *sb, const char *host )
759 struct sockaddr_storage sabuf;
761 struct sockaddr sabuf;
763 struct sockaddr *sa = (struct sockaddr *) &sabuf;
766 (void)memset( (char *)sa, '\0', sizeof sabuf );
769 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
770 if ( getpeername( sd, sa, &len ) == -1 ) {
775 * do a reverse lookup on the addr to get the official hostname.
776 * this is necessary for kerberos to work right, since the official
777 * hostname is used as the kerberos instance.
780 switch (sa->sa_family) {
783 return LDAP_STRDUP( ldap_int_hostname );
788 struct in6_addr localhost = IN6ADDR_LOOPBACK_INIT;
789 if( memcmp ( &((struct sockaddr_in6 *)sa)->sin6_addr,
790 &localhost, sizeof(localhost)) == 0 )
792 return LDAP_STRDUP( ldap_int_hostname );
799 struct in_addr localhost;
800 localhost.s_addr = htonl( INADDR_ANY );
802 if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
803 &localhost, sizeof(localhost) ) == 0 )
805 return LDAP_STRDUP( ldap_int_hostname );
808 #ifdef INADDR_LOOPBACK
809 localhost.s_addr = htonl( INADDR_LOOPBACK );
811 if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
812 &localhost, sizeof(localhost) ) == 0 )
814 return LDAP_STRDUP( ldap_int_hostname );
828 char hbuf[NI_MAXHOST];
829 #elif defined( MAXHOSTNAMELEN )
830 char hbuf[MAXHOSTNAMELEN];
836 if (ldap_pvt_get_hname( sa, len, hbuf, sizeof(hbuf), &herr ) == 0
839 return LDAP_STRDUP( hbuf );
843 return host ? LDAP_STRDUP( host ) : NULL;
850 /* for UNIX poll(2) */
852 struct pollfd si_fds[FD_SETSIZE];
854 /* for UNIX select(2) */
857 fd_set si_use_readfds;
858 fd_set si_use_writefds;
863 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
865 struct selectinfo *sip;
868 sip = (struct selectinfo *)ld->ld_selectinfo;
870 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
873 /* for UNIX poll(2) */
877 for(i=0; i < sip->si_maxfd; i++) {
878 if( sip->si_fds[i].fd == sd ) {
879 sip->si_fds[i].events |= POLL_WRITE;
882 if( empty==-1 && sip->si_fds[i].fd == -1 ) {
888 if( sip->si_maxfd >= FD_SETSIZE ) {
892 empty = sip->si_maxfd++;
895 sip->si_fds[empty].fd = sd;
896 sip->si_fds[empty].events = POLL_WRITE;
899 /* for UNIX select(2) */
900 if ( !FD_ISSET( sd, &sip->si_writefds )) {
901 FD_SET( sd, &sip->si_writefds );
908 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
910 struct selectinfo *sip;
913 sip = (struct selectinfo *)ld->ld_selectinfo;
915 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
918 /* for UNIX poll(2) */
922 for(i=0; i < sip->si_maxfd; i++) {
923 if( sip->si_fds[i].fd == sd ) {
924 sip->si_fds[i].events |= POLL_READ;
927 if( empty==-1 && sip->si_fds[i].fd == -1 ) {
933 if( sip->si_maxfd >= FD_SETSIZE ) {
937 empty = sip->si_maxfd++;
940 sip->si_fds[empty].fd = sd;
941 sip->si_fds[empty].events = POLL_READ;
944 /* for UNIX select(2) */
945 if ( !FD_ISSET( sd, &sip->si_readfds )) {
946 FD_SET( sd, &sip->si_readfds );
953 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
955 struct selectinfo *sip;
958 sip = (struct selectinfo *)ld->ld_selectinfo;
960 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
963 /* for UNIX poll(2) */
966 for(i=0; i < sip->si_maxfd; i++) {
967 if( sip->si_fds[i].fd == sd ) {
968 sip->si_fds[i].fd = -1;
973 /* for UNIX select(2) */
974 FD_CLR( sd, &sip->si_writefds );
975 FD_CLR( sd, &sip->si_readfds );
980 ldap_clear_select_write( LDAP *ld, Sockbuf *sb )
982 struct selectinfo *sip;
985 sip = (struct selectinfo *)ld->ld_selectinfo;
987 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
990 /* for UNIX poll(2) */
993 for(i=0; i < sip->si_maxfd; i++) {
994 if( sip->si_fds[i].fd == sd ) {
995 sip->si_fds[i].events &= ~POLL_WRITE;
1000 /* for UNIX select(2) */
1001 FD_CLR( sd, &sip->si_writefds );
1007 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
1009 struct selectinfo *sip;
1012 sip = (struct selectinfo *)ld->ld_selectinfo;
1014 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
1017 /* for UNIX poll(2) */
1020 for(i=0; i < sip->si_maxfd; i++) {
1021 if( sip->si_fds[i].fd == sd ) {
1022 return sip->si_fds[i].revents & POLL_WRITE;
1029 /* for UNIX select(2) */
1030 return( FD_ISSET( sd, &sip->si_use_writefds ));
1036 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
1038 struct selectinfo *sip;
1041 sip = (struct selectinfo *)ld->ld_selectinfo;
1043 if (ber_sockbuf_ctrl( sb, LBER_SB_OPT_DATA_READY, NULL ))
1046 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
1049 /* for UNIX poll(2) */
1052 for(i=0; i < sip->si_maxfd; i++) {
1053 if( sip->si_fds[i].fd == sd ) {
1054 return sip->si_fds[i].revents & POLL_READ;
1061 /* for UNIX select(2) */
1062 return( FD_ISSET( sd, &sip->si_use_readfds ));
1068 ldap_new_select_info( void )
1070 struct selectinfo *sip;
1072 sip = (struct selectinfo *)LDAP_CALLOC( 1, sizeof( struct selectinfo ));
1074 if ( sip == NULL ) return NULL;
1077 /* for UNIX poll(2) */
1078 /* sip->si_maxfd=0 */
1080 /* for UNIX select(2) */
1081 FD_ZERO( &sip->si_readfds );
1082 FD_ZERO( &sip->si_writefds );
1085 return( (void *)sip );
1090 ldap_free_select_info( void *sip )
1097 int ldap_int_tblsize = 0;
1100 ldap_int_ip_init( void )
1102 #if defined( HAVE_SYSCONF )
1103 long tblsize = sysconf( _SC_OPEN_MAX );
1104 if( tblsize > INT_MAX ) tblsize = INT_MAX;
1106 #elif defined( HAVE_GETDTABLESIZE )
1107 int tblsize = getdtablesize();
1109 int tblsize = FD_SETSIZE;
1110 #endif /* !USE_SYSCONF */
1113 if( tblsize > FD_SETSIZE ) tblsize = FD_SETSIZE;
1114 #endif /* FD_SETSIZE */
1116 ldap_int_tblsize = tblsize;
1122 ldap_int_select( LDAP *ld, struct timeval *timeout )
1125 struct selectinfo *sip;
1127 Debug( LDAP_DEBUG_TRACE, "ldap_int_select\n", 0, 0, 0 );
1130 if ( ldap_int_tblsize == 0 ) ldap_int_ip_init();
1133 sip = (struct selectinfo *)ld->ld_selectinfo;
1134 assert( sip != NULL );
1138 int to = timeout ? TV2MILLISEC( timeout ) : INFTIM;
1139 rc = poll( sip->si_fds, sip->si_maxfd, to );
1142 sip->si_use_readfds = sip->si_readfds;
1143 sip->si_use_writefds = sip->si_writefds;
1145 rc = select( ldap_int_tblsize,
1146 &sip->si_use_readfds, &sip->si_use_writefds,