]> git.sur5r.net Git - openldap/blob - libraries/libldap/os-ip.c
e6a140a9d0acd91163915aab9677da64b263b5a2
[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( HAVE_CYRUS_SASL )
709 char *
710 ldap_host_connected_to( Sockbuf *sb, const char *host )
711 {
712         socklen_t               len;
713 #ifdef LDAP_PF_INET6
714         struct sockaddr_storage sabuf;
715 #else
716         struct sockaddr sabuf;
717 #endif
718         struct sockaddr *sa = (struct sockaddr *) &sabuf;
719         ber_socket_t    sd;
720
721         (void)memset( (char *)sa, '\0', sizeof sabuf );
722         len = sizeof sabuf;
723
724         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
725         if ( getpeername( sd, sa, &len ) == -1 ) {
726                 return( NULL );
727         }
728
729         /*
730          * do a reverse lookup on the addr to get the official hostname.
731          * this is necessary for kerberos to work right, since the official
732          * hostname is used as the kerberos instance.
733          */
734
735         switch (sa->sa_family) {
736 #ifdef LDAP_PF_LOCAL
737         case AF_LOCAL:
738                 return LDAP_STRDUP( ldap_int_hostname );
739 #endif
740 #ifdef LDAP_PF_INET6
741         case AF_INET6:
742                 {
743                         struct in6_addr localhost = IN6ADDR_LOOPBACK_INIT;
744                         if( memcmp ( &((struct sockaddr_in6 *)sa)->sin6_addr,
745                                 &localhost, sizeof(localhost)) == 0 )
746                         {
747                                 return LDAP_STRDUP( ldap_int_hostname );
748                         }
749                 }
750                 break;
751 #endif
752         case AF_INET:
753                 {
754                         struct in_addr localhost;
755                         localhost.s_addr = htonl( INADDR_ANY );
756
757                         if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
758                                 &localhost, sizeof(localhost) ) == 0 )
759                         {
760                                 return LDAP_STRDUP( ldap_int_hostname );
761                         }
762
763 #ifdef INADDR_LOOPBACK
764                         localhost.s_addr = htonl( INADDR_LOOPBACK );
765
766                         if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
767                                 &localhost, sizeof(localhost) ) == 0 )
768                         {
769                                 return LDAP_STRDUP( ldap_int_hostname );
770                         }
771 #endif
772                 }
773                 break;
774
775         default:
776                 return( NULL );
777                 break;
778         }
779
780         {
781                 char *herr;
782 #ifdef NI_MAXHOST
783                 char hbuf[NI_MAXHOST];
784 #elif defined( MAXHOSTNAMELEN
785                 char hbuf[MAXHOSTNAMELEN];
786 #else
787                 char hbuf[256];
788 #endif
789                 hbuf[0] = 0;
790
791                 if (ldap_pvt_get_hname( sa, len, hbuf, sizeof(hbuf), &herr ) == 0
792                         && hbuf[0] ) 
793                 {
794                         return LDAP_STRDUP( hbuf );   
795                 }
796         }
797
798         return host ? LDAP_STRDUP( host ) : NULL;
799 }
800 #endif
801
802
803 struct selectinfo {
804 #ifdef HAVE_POLL
805         /* for UNIX poll(2) */
806         int si_maxfd;
807         struct pollfd si_fds[FD_SETSIZE];
808 #else
809         /* for UNIX select(2) */
810         fd_set  si_readfds;
811         fd_set  si_writefds;
812         fd_set  si_use_readfds;
813         fd_set  si_use_writefds;
814 #endif
815 };
816
817 void
818 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
819 {
820         struct selectinfo       *sip;
821         ber_socket_t            sd;
822
823         sip = (struct selectinfo *)ld->ld_selectinfo;
824         
825         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
826
827 #ifdef HAVE_POLL
828         /* for UNIX poll(2) */
829         {
830                 int empty=-1;
831                 int i;
832                 for(i=0; i < sip->si_maxfd; i++) {
833                         if( sip->si_fds[i].fd == sd ) {
834                                 sip->si_fds[i].events |= POLL_WRITE;
835                                 return;
836                         }
837                         if( empty==-1 && sip->si_fds[i].fd == -1 ) {
838                                 empty=i;
839                         }
840                 }
841
842                 if( empty == -1 ) {
843                         if( sip->si_maxfd >= FD_SETSIZE ) {
844                                 /* FIXME */
845                                 return;
846                         }
847                         empty = sip->si_maxfd++;
848                 }
849
850                 sip->si_fds[empty].fd = sd;
851                 sip->si_fds[empty].events = POLL_WRITE;
852         }
853 #else
854         /* for UNIX select(2) */
855         if ( !FD_ISSET( sd, &sip->si_writefds )) {
856                 FD_SET( sd, &sip->si_writefds );
857         }
858 #endif
859 }
860
861
862 void
863 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
864 {
865         struct selectinfo       *sip;
866         ber_socket_t            sd;
867
868         sip = (struct selectinfo *)ld->ld_selectinfo;
869
870         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
871
872 #ifdef HAVE_POLL
873         /* for UNIX poll(2) */
874         {
875                 int empty=-1;
876                 int i;
877                 for(i=0; i < sip->si_maxfd; i++) {
878                         if( sip->si_fds[i].fd == sd ) {
879                                 sip->si_fds[i].events |= POLL_READ;
880                                 return;
881                         }
882                         if( empty==-1 && sip->si_fds[i].fd == -1 ) {
883                                 empty=i;
884                         }
885                 }
886
887                 if( empty == -1 ) {
888                         if( sip->si_maxfd >= FD_SETSIZE ) {
889                                 /* FIXME */
890                                 return;
891                         }
892                         empty = sip->si_maxfd++;
893                 }
894
895                 sip->si_fds[empty].fd = sd;
896                 sip->si_fds[empty].events = POLL_READ;
897         }
898 #else
899         /* for UNIX select(2) */
900         if ( !FD_ISSET( sd, &sip->si_readfds )) {
901                 FD_SET( sd, &sip->si_readfds );
902         }
903 #endif
904 }
905
906
907 void
908 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
909 {
910         struct selectinfo       *sip;
911         ber_socket_t            sd;
912
913         sip = (struct selectinfo *)ld->ld_selectinfo;
914
915         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
916
917 #ifdef HAVE_POLL
918         /* for UNIX poll(2) */
919         {
920                 int i;
921                 for(i=0; i < sip->si_maxfd; i++) {
922                         if( sip->si_fds[i].fd == sd ) {
923                                 sip->si_fds[i].fd = -1;
924                         }
925                 }
926         }
927 #else
928         /* for UNIX select(2) */
929         FD_CLR( sd, &sip->si_writefds );
930         FD_CLR( sd, &sip->si_readfds );
931 #endif
932 }
933
934
935 int
936 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
937 {
938         struct selectinfo       *sip;
939         ber_socket_t            sd;
940
941         sip = (struct selectinfo *)ld->ld_selectinfo;
942
943         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
944
945 #ifdef HAVE_POLL
946         /* for UNIX poll(2) */
947         {
948                 int i;
949                 for(i=0; i < sip->si_maxfd; i++) {
950                         if( sip->si_fds[i].fd == sd ) {
951                                 return sip->si_fds[i].revents & POLL_WRITE;
952                         }
953                 }
954
955                 return 0;
956         }
957 #else
958         /* for UNIX select(2) */
959         return( FD_ISSET( sd, &sip->si_use_writefds ));
960 #endif
961 }
962
963
964 int
965 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
966 {
967         struct selectinfo       *sip;
968         ber_socket_t            sd;
969
970         sip = (struct selectinfo *)ld->ld_selectinfo;
971
972         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
973
974 #ifdef HAVE_POLL
975         /* for UNIX poll(2) */
976         {
977                 int i;
978                 for(i=0; i < sip->si_maxfd; i++) {
979                         if( sip->si_fds[i].fd == sd ) {
980                                 return sip->si_fds[i].revents & POLL_READ;
981                         }
982                 }
983
984                 return 0;
985         }
986 #else
987         /* for UNIX select(2) */
988         return( FD_ISSET( sd, &sip->si_use_readfds ));
989 #endif
990 }
991
992
993 void *
994 ldap_new_select_info( void )
995 {
996         struct selectinfo       *sip;
997
998         sip = (struct selectinfo *)LDAP_CALLOC( 1, sizeof( struct selectinfo ));
999
1000         if ( sip == NULL ) return NULL;
1001
1002 #ifdef HAVE_POLL
1003         /* for UNIX poll(2) */
1004         /* sip->si_maxfd=0 */
1005 #else
1006         /* for UNIX select(2) */
1007         FD_ZERO( &sip->si_readfds );
1008         FD_ZERO( &sip->si_writefds );
1009 #endif
1010
1011         return( (void *)sip );
1012 }
1013
1014
1015 void
1016 ldap_free_select_info( void *sip )
1017 {
1018         LDAP_FREE( sip );
1019 }
1020
1021
1022 #ifndef HAVE_POLL
1023 int ldap_int_tblsize = 0;
1024
1025 void
1026 ldap_int_ip_init( void )
1027 {
1028 #if defined( HAVE_SYSCONF )
1029         long tblsize = sysconf( _SC_OPEN_MAX );
1030         if( tblsize > INT_MAX ) tblsize = INT_MAX;
1031
1032 #elif defined( HAVE_GETDTABLESIZE )
1033         int tblsize = getdtablesize();
1034 #else
1035         int tblsize = FD_SETSIZE;
1036 #endif /* !USE_SYSCONF */
1037
1038 #ifdef FD_SETSIZE
1039         if( tblsize > FD_SETSIZE ) tblsize = FD_SETSIZE;
1040 #endif  /* FD_SETSIZE */
1041
1042         ldap_int_tblsize = tblsize;
1043 }
1044 #endif
1045
1046
1047 int
1048 ldap_int_select( LDAP *ld, struct timeval *timeout )
1049 {
1050         int rc;
1051         struct selectinfo       *sip;
1052
1053         Debug( LDAP_DEBUG_TRACE, "ldap_int_select\n", 0, 0, 0 );
1054
1055 #ifndef HAVE_POLL
1056         if ( ldap_int_tblsize == 0 ) ldap_int_ip_init();
1057 #endif
1058
1059         sip = (struct selectinfo *)ld->ld_selectinfo;
1060         assert( sip != NULL );
1061
1062 #ifdef HAVE_POLL
1063         {
1064                 int to = timeout ? TV2MILLISEC( timeout ) : INFTIM;
1065                 rc = poll( sip->si_fds, sip->si_maxfd, to );
1066         }
1067 #else
1068         sip->si_use_readfds = sip->si_readfds;
1069         sip->si_use_writefds = sip->si_writefds;
1070         
1071         rc = select( ldap_int_tblsize,
1072                 &sip->si_use_readfds, &sip->si_use_writefds,
1073                 NULL, timeout );
1074 #endif
1075
1076         return rc;
1077 }