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