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