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