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