]> git.sur5r.net Git - openldap/blob - libraries/libldap/os-ip.c
fc8d4b04c9bd457c80b1bc496ea840caa04217ed
[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-2004 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 = POLLOUT;
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 & POLLOUT ) {
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                 FD_ZERO(&wfds);
286                 FD_SET(s, &wfds );
287
288 #ifdef HAVE_WINSOCK
289                 FD_ZERO(&efds);
290                 FD_SET(s, &efds );
291 #endif
292
293                 do {
294                         rc = select(ldap_int_tblsize, z, &wfds,
295 #ifdef HAVE_WINSOCK
296                                 &efds,
297 #else
298                                 z,
299 #endif
300                                 opt_tv ? &tv : NULL);
301                 } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
302                         LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
303
304                 if( rc == AC_SOCKET_ERROR ) return rc;
305
306 #ifdef HAVE_WINSOCK
307                 /* This means the connection failed */
308                 if ( FD_ISSET(s, &efds) ) {
309                     int so_errno;
310                     int dummy = sizeof(so_errno);
311                     if ( getsockopt( s, SOL_SOCKET, SO_ERROR,
312                                 (char *) &so_errno, &dummy ) == AC_SOCKET_ERROR || !so_errno )
313                     {
314                         /* impossible */
315                         so_errno = WSAGetLastError();
316                     }
317                     ldap_pvt_set_errno(so_errno);
318                     osip_debug(ld, "ldap_pvt_connect: error on socket %d: "
319                                "errno: %d (%s)\n", s, errno, sock_errstr(errno));
320                     return -1;
321                 }
322 #endif
323                 if ( FD_ISSET(s, &wfds) ) {
324 #ifndef HAVE_WINSOCK
325                         if ( ldap_pvt_is_socket_ready(ld, s) == -1 ) return -1;
326 #endif
327                         if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
328                         return 0;
329                 }
330         }
331 #endif
332
333         osip_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
334         ldap_pvt_set_errno( ETIMEDOUT );
335         return -1;
336 }
337
338 #ifndef HAVE_INET_ATON
339 int
340 ldap_pvt_inet_aton( const char *host, struct in_addr *in)
341 {
342         unsigned long u = inet_addr( host );
343         if ( u != 0xffffffff || u != (unsigned long) -1 ) {
344                 in->s_addr = u;
345                 return 1;
346         }
347         return 0;
348 }
349 #endif
350
351
352 int
353 ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
354         int proto,
355         const char *host, int port,
356         int async )
357 {
358         int     rc;
359         int     socktype;
360         ber_socket_t            s = AC_SOCKET_INVALID;
361
362 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
363         char serv[7];
364         int err;
365         struct addrinfo hints, *res, *sai;
366 #else
367         int i;
368         int use_hp = 0;
369         struct hostent *hp = NULL;
370         struct hostent he_buf;
371         struct in_addr in;
372         char *ha_buf=NULL;
373 #endif
374
375         if( host == NULL ) host = "localhost";
376         
377         switch(proto) {
378         case LDAP_PROTO_TCP: socktype = SOCK_STREAM;
379                 osip_debug( ld,
380                         "ldap_connect_to_host: TCP %s:%d\n",
381                         host, port, 0);
382                 break;
383         case LDAP_PROTO_UDP: socktype = SOCK_DGRAM;
384                 osip_debug( ld,
385                         "ldap_connect_to_host: UDP %s:%d\n",
386                         host, port, 0);
387                 break;
388         default:
389                 osip_debug( ld, "ldap_connect_to_host: unknown proto: %d\n",
390                         proto, 0, 0 );
391                 return -1;
392         }
393
394 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
395         memset( &hints, '\0', sizeof(hints) );
396 #ifdef USE_AI_ATTRCONFIG /* FIXME: configure test needed */
397         /* Use AI_ATTRCONFIG only on systems where its known to be needed. */
398         hints.ai_flags = AI_ATTRCONFIG;
399 #endif
400         hints.ai_family = ldap_int_inet4or6;
401         hints.ai_socktype = socktype;
402         snprintf(serv, sizeof serv, "%d", port );
403
404 #ifdef LDAP_R_COMPILE
405         /* most getaddrinfo(3) use non-threadsafe resolver libraries */
406         ldap_pvt_thread_mutex_lock(&ldap_int_resolv_mutex);
407 #endif
408
409         err = getaddrinfo( host, serv, &hints, &res );
410
411 #ifdef LDAP_R_COMPILE
412         ldap_pvt_thread_mutex_unlock(&ldap_int_resolv_mutex);
413 #endif
414
415         if ( err != 0 ) {
416                 osip_debug(ld, "ldap_connect_to_host: getaddrinfo failed: %s\n",
417                         AC_GAI_STRERROR(err), 0, 0);
418                 return -1;
419         }
420         rc = -1;
421
422         for( sai=res; sai != NULL; sai=sai->ai_next) {
423                 if( sai->ai_addr == NULL ) {
424                         osip_debug(ld, "ldap_connect_to_host: getaddrinfo "
425                                 "ai_addr is NULL?\n", 0, 0, 0);
426                         continue;
427                 }
428
429                 /* we assume AF_x and PF_x are equal for all x */
430                 s = ldap_int_socket( ld, sai->ai_family, socktype );
431                 if ( s == AC_SOCKET_INVALID ) {
432                         continue;
433                 }
434
435                 if ( ldap_int_prepare_socket(ld, s, proto ) == -1 ) {
436                         ldap_pvt_close_socket(ld, s);
437                         break;
438                 }
439
440                 switch (sai->ai_family) {
441 #ifdef LDAP_PF_INET6
442                         case AF_INET6: {
443                                 char addr[INET6_ADDRSTRLEN];
444                                 inet_ntop( AF_INET6,
445                                         &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
446                                         addr, sizeof addr);
447                                 osip_debug(ld, "ldap_connect_to_host: Trying %s %s\n", 
448                                         addr, serv, 0);
449                         } break;
450 #endif
451                         case AF_INET: {
452                                 char addr[INET_ADDRSTRLEN];
453                                 inet_ntop( AF_INET,
454                                         &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
455                                         addr, sizeof addr);
456                                 osip_debug(ld, "ldap_connect_to_host: Trying %s:%s\n", 
457                                         addr, serv, 0);
458                         } break;
459                 }
460
461                 rc = ldap_pvt_connect( ld, s,
462                         sai->ai_addr, sai->ai_addrlen, async );
463                 if ( (rc == 0) || (rc == -2) ) {
464                         ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
465                         break;
466                 }
467                 ldap_pvt_close_socket(ld, s);
468         }
469         freeaddrinfo(res);
470
471 #else
472         if (! inet_aton( host, &in ) ) {
473                 int local_h_errno;
474                 rc = ldap_pvt_gethostbyname_a( host, &he_buf, &ha_buf,
475                         &hp, &local_h_errno );
476
477                 if ( (rc < 0) || (hp == NULL) ) {
478 #ifdef HAVE_WINSOCK
479                         ldap_pvt_set_errno( WSAGetLastError() );
480 #else
481                         /* not exactly right, but... */
482                         ldap_pvt_set_errno( EHOSTUNREACH );
483 #endif
484                         if (ha_buf) LDAP_FREE(ha_buf);
485                         return -1;
486                 }
487
488                 use_hp = 1;
489         }
490
491         rc = s = -1;
492         for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
493                 struct sockaddr_in      sin;
494
495                 s = ldap_int_socket( ld, PF_INET, socktype );
496                 if ( s == AC_SOCKET_INVALID ) {
497                         /* use_hp ? continue : break; */
498                         break;
499                 }
500            
501                 if ( ldap_int_prepare_socket( ld, s, proto ) == -1 ) {
502                         ldap_pvt_close_socket(ld, s);
503                         break;
504                 }
505
506                 (void)memset((char *)&sin, '\0', sizeof sin);
507                 sin.sin_family = AF_INET;
508                 sin.sin_port = htons((short) port);
509
510                 if( use_hp ) {
511                         AC_MEMCPY( &sin.sin_addr, hp->h_addr_list[i],
512                                 sizeof(sin.sin_addr) );
513                 } else {
514                         AC_MEMCPY( &sin.sin_addr, &in.s_addr,
515                                 sizeof(sin.sin_addr) );
516                 }
517
518                 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n", 
519                         inet_ntoa(sin.sin_addr), port, 0);
520
521                 rc = ldap_pvt_connect(ld, s,
522                         (struct sockaddr *)&sin, sizeof(sin),
523                         async);
524    
525                 if ( (rc == 0) || (rc == -2) ) {
526                         ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
527                         break;
528                 }
529
530                 ldap_pvt_close_socket(ld, s);
531
532                 if (!use_hp) break;
533         }
534         if (ha_buf) LDAP_FREE(ha_buf);
535 #endif
536
537         return rc;
538 }
539
540 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND ) || \
541         defined( HAVE_CYRUS_SASL )
542 char *
543 ldap_host_connected_to( Sockbuf *sb, const char *host )
544 {
545         socklen_t               len;
546 #ifdef LDAP_PF_INET6
547         struct sockaddr_storage sabuf;
548 #else
549         struct sockaddr sabuf;
550 #endif
551         struct sockaddr *sa = (struct sockaddr *) &sabuf;
552         ber_socket_t    sd;
553
554         (void)memset( (char *)sa, '\0', sizeof sabuf );
555         len = sizeof sabuf;
556
557         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
558         if ( getpeername( sd, sa, &len ) == -1 ) {
559                 return( NULL );
560         }
561
562         /*
563          * do a reverse lookup on the addr to get the official hostname.
564          * this is necessary for kerberos to work right, since the official
565          * hostname is used as the kerberos instance.
566          */
567
568         switch (sa->sa_family) {
569 #ifdef LDAP_PF_LOCAL
570         case AF_LOCAL:
571                 return LDAP_STRDUP( ldap_int_hostname );
572 #endif
573 #ifdef LDAP_PF_INET6
574         case AF_INET6:
575                 {
576                         struct in6_addr localhost = IN6ADDR_LOOPBACK_INIT;
577                         if( memcmp ( &((struct sockaddr_in6 *)sa)->sin6_addr,
578                                 &localhost, sizeof(localhost)) == 0 )
579                         {
580                                 return LDAP_STRDUP( ldap_int_hostname );
581                         }
582                 }
583                 break;
584 #endif
585         case AF_INET:
586                 {
587                         struct in_addr localhost;
588                         localhost.s_addr = htonl( INADDR_ANY );
589
590                         if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
591                                 &localhost, sizeof(localhost) ) == 0 )
592                         {
593                                 return LDAP_STRDUP( ldap_int_hostname );
594                         }
595
596 #ifdef INADDR_LOOPBACK
597                         localhost.s_addr = htonl( INADDR_LOOPBACK );
598
599                         if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
600                                 &localhost, sizeof(localhost) ) == 0 )
601                         {
602                                 return LDAP_STRDUP( ldap_int_hostname );
603                         }
604 #endif
605                 }
606                 break;
607
608         default:
609                 return( NULL );
610                 break;
611         }
612
613         {
614                 char *herr;
615 #ifdef NI_MAXHOST
616                 char hbuf[NI_MAXHOST];
617 #elif defined( MAXHOSTNAMELEN
618                 char hbuf[MAXHOSTNAMELEN];
619 #else
620                 char hbuf[256];
621 #endif
622                 hbuf[0] = 0;
623
624                 if (ldap_pvt_get_hname( sa, len, hbuf, sizeof(hbuf), &herr ) == 0
625                         && hbuf[0] ) 
626                 {
627                         return LDAP_STRDUP( hbuf );   
628                 }
629         }
630
631         return host ? LDAP_STRDUP( host ) : NULL;
632 }
633 #endif
634
635
636 #ifdef HAVE_POLL
637 /* for UNIX poll(2) */
638         /* ??? */
639 #else
640 /* for UNIX select(2) */
641 struct selectinfo {
642         fd_set  si_readfds;
643         fd_set  si_writefds;
644         fd_set  si_use_readfds;
645         fd_set  si_use_writefds;
646 };
647 #endif
648
649 void
650 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
651 {
652         struct selectinfo       *sip;
653         ber_socket_t            sd;
654
655         sip = (struct selectinfo *)ld->ld_selectinfo;
656         
657         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
658         if ( !FD_ISSET( sd, &sip->si_writefds )) {
659                 FD_SET( sd, &sip->si_writefds );
660         }
661 }
662
663
664 void
665 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
666 {
667         struct selectinfo       *sip;
668         ber_socket_t            sd;
669
670         sip = (struct selectinfo *)ld->ld_selectinfo;
671
672         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
673         if ( !FD_ISSET( sd, &sip->si_readfds )) {
674                 FD_SET( sd, &sip->si_readfds );
675         }
676 }
677
678
679 void
680 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
681 {
682         struct selectinfo       *sip;
683         ber_socket_t            sd;
684
685         sip = (struct selectinfo *)ld->ld_selectinfo;
686
687         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
688         FD_CLR( sd, &sip->si_writefds );
689         FD_CLR( sd, &sip->si_readfds );
690 }
691
692
693 int
694 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
695 {
696         struct selectinfo       *sip;
697         ber_socket_t            sd;
698
699         sip = (struct selectinfo *)ld->ld_selectinfo;
700
701         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
702         return( FD_ISSET( sd, &sip->si_use_writefds ));
703 }
704
705
706 int
707 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
708 {
709         struct selectinfo       *sip;
710         ber_socket_t            sd;
711
712         sip = (struct selectinfo *)ld->ld_selectinfo;
713
714         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
715         return( FD_ISSET( sd, &sip->si_use_readfds ));
716 }
717
718
719 void *
720 ldap_new_select_info( void )
721 {
722         struct selectinfo       *sip;
723
724         if (( sip = (struct selectinfo *)LDAP_CALLOC( 1,
725             sizeof( struct selectinfo ))) != NULL ) {
726                 FD_ZERO( &sip->si_readfds );
727                 FD_ZERO( &sip->si_writefds );
728         }
729
730         return( (void *)sip );
731 }
732
733
734 void
735 ldap_free_select_info( void *sip )
736 {
737         LDAP_FREE( sip );
738 }
739
740
741 #ifndef HAVE_POLL
742 int ldap_int_tblsize = 0;
743
744 void
745 ldap_int_ip_init( void )
746 {
747         int tblsize;
748
749 #if defined( HAVE_SYSCONF )
750         tblsize = sysconf( _SC_OPEN_MAX );
751 #elif defined( HAVE_GETDTABLESIZE )
752         tblsize = getdtablesize();
753 #else
754         tblsize = FD_SETSIZE;
755 #endif /* !USE_SYSCONF */
756
757 #ifdef FD_SETSIZE
758         if( tblsize > FD_SETSIZE ) tblsize = FD_SETSIZE;
759 #endif  /* FD_SETSIZE*/
760
761         ldap_int_tblsize = tblsize;
762 }
763 #endif
764
765
766 int
767 ldap_int_select( LDAP *ld, struct timeval *timeout )
768 {
769         struct selectinfo       *sip;
770
771         Debug( LDAP_DEBUG_TRACE, "ldap_int_select\n", 0, 0, 0 );
772
773 #ifndef HAVE_POLL
774         if ( ldap_int_tblsize == 0 ) ldap_int_ip_init();
775 #endif
776
777         sip = (struct selectinfo *)ld->ld_selectinfo;
778         sip->si_use_readfds = sip->si_readfds;
779         sip->si_use_writefds = sip->si_writefds;
780         
781 #ifdef HAVE_POLL
782         assert(0);
783         return -1;
784 #else
785         return( select( ldap_int_tblsize,
786                 &sip->si_use_readfds, &sip->si_use_writefds,
787                 NULL, timeout ));
788 #endif
789 }