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