]> git.sur5r.net Git - openldap/blob - libraries/libldap/os-ip.c
55636aaece8abfb388279e724bc952ba66382bae
[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-2008 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 #ifdef HAVE_FCNTL_H
40 #include <fcntl.h>
41 #endif
42
43 #include "ldap-int.h"
44
45 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
46 #  ifdef LDAP_PF_INET6
47 int ldap_int_inet4or6 = AF_UNSPEC;
48 #  else
49 int ldap_int_inet4or6 = AF_INET;
50 #  endif
51 #endif
52
53 #ifdef LDAP_DEBUG
54
55 #define osip_debug(ld,fmt,arg1,arg2,arg3) \
56 do { \
57         ldap_log_printf(NULL, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
58 } while(0)
59
60 #else
61
62 #define osip_debug(ld,fmt,arg1,arg2,arg3) ((void)0)
63
64 #endif /* LDAP_DEBUG */
65
66 static void
67 ldap_pvt_set_errno(int err)
68 {
69         sock_errset(err);
70 }
71
72 int
73 ldap_int_timeval_dup( struct timeval **dest, const struct timeval *src )
74 {
75         struct timeval *new;
76
77         assert( dest != NULL );
78
79         if (src == NULL) {
80                 *dest = NULL;
81                 return 0;
82         }
83
84         new = (struct timeval *) LDAP_MALLOC(sizeof(struct timeval));
85
86         if( new == NULL ) {
87                 *dest = NULL;
88                 return 1;
89         }
90
91         AC_MEMCPY( (char *) new, (const char *) src, sizeof(struct timeval));
92
93         *dest = new;
94         return 0;
95 }
96
97 static int
98 ldap_pvt_ndelay_on(LDAP *ld, int fd)
99 {
100         osip_debug(ld, "ldap_ndelay_on: %d\n",fd,0,0);
101         return ber_pvt_socket_set_nonblock( fd, 1 );
102 }
103    
104 static int
105 ldap_pvt_ndelay_off(LDAP *ld, int fd)
106 {
107         osip_debug(ld, "ldap_ndelay_off: %d\n",fd,0,0);
108         return ber_pvt_socket_set_nonblock( fd, 0 );
109 }
110
111 static ber_socket_t
112 ldap_int_socket(LDAP *ld, int family, int type )
113 {
114         ber_socket_t s = socket(family, type, 0);
115         osip_debug(ld, "ldap_new_socket: %d\n",s,0,0);
116 #ifdef FD_CLOEXEC
117         fcntl(s, F_SETFD, FD_CLOEXEC);
118 #endif
119         return ( s );
120 }
121
122 static int
123 ldap_pvt_close_socket(LDAP *ld, int s)
124 {
125         osip_debug(ld, "ldap_close_socket: %d\n",s,0,0);
126         return tcp_close(s);
127 }
128
129 static int
130 ldap_int_prepare_socket(LDAP *ld, int s, int proto )
131 {
132         osip_debug( ld, "ldap_prepare_socket: %d\n", s, 0, 0 );
133
134 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
135         if ( proto == LDAP_PROTO_TCP ) {
136                 int dummy = 1;
137 #ifdef SO_KEEPALIVE
138                 if ( setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
139                         (char*) &dummy, sizeof(dummy) ) == AC_SOCKET_ERROR )
140                 {
141                         osip_debug( ld, "ldap_prepare_socket: "
142                                 "setsockopt(%d, SO_KEEPALIVE) failed (ignored).\n",
143                                 s, 0, 0 );
144                 }
145 #endif /* SO_KEEPALIVE */
146 #ifdef TCP_NODELAY
147                 if ( setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
148                         (char*) &dummy, sizeof(dummy) ) == AC_SOCKET_ERROR )
149                 {
150                         osip_debug( ld, "ldap_prepare_socket: "
151                                 "setsockopt(%d, TCP_NODELAY) failed (ignored).\n",
152                                 s, 0, 0 );
153                 }
154 #endif /* TCP_NODELAY */
155         }
156 #endif /* SO_KEEPALIVE || TCP_NODELAY */
157
158         return 0;
159 }
160
161 #ifndef HAVE_WINSOCK
162
163 #undef TRACE
164 #define TRACE do { \
165         osip_debug(ld, \
166                 "ldap_is_socket_ready: error on socket %d: errno: %d (%s)\n", \
167                 s, \
168                 errno, \
169                 sock_errstr(errno) ); \
170 } while( 0 )
171
172 /*
173  * check the socket for errors after select returned.
174  */
175 static int
176 ldap_pvt_is_socket_ready(LDAP *ld, int s)
177 {
178         osip_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
179
180 #if defined( notyet ) /* && defined( SO_ERROR ) */
181 {
182         int so_errno;
183         ber_socklen_t dummy = sizeof(so_errno);
184         if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
185                 == AC_SOCKET_ERROR )
186         {
187                 return -1;
188         }
189         if ( so_errno ) {
190                 ldap_pvt_set_errno(so_errno);
191                 TRACE;
192                 return -1;
193         }
194         return 0;
195 }
196 #else
197 {
198         /* error slippery */
199 #ifdef LDAP_PF_INET6
200         struct sockaddr_storage sin;
201 #else
202         struct sockaddr_in sin;
203 #endif
204         char ch;
205         ber_socklen_t dummy = sizeof(sin);
206         if ( getpeername( s, (struct sockaddr *) &sin, &dummy )
207                 == AC_SOCKET_ERROR )
208         {
209                 /* XXX: needs to be replace with ber_stream_read() */
210                 int rc = read(s, &ch, 1);
211                 TRACE;
212                 return -1;
213         }
214         return 0;
215 }
216 #endif
217         return -1;
218 }
219 #undef TRACE
220
221 #endif /* HAVE_WINSOCK */
222
223 /* NOTE: this is identical to analogous code in os-local.c */
224 int
225 ldap_int_poll(
226         LDAP *ld,
227         ber_socket_t s,
228         struct timeval *tvp )
229 {
230         int             rc;
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                 int timeout = INFTIM;
240
241                 fd.fd = s;
242                 fd.events = POLL_WRITE;
243
244                 if ( tvp != NULL ) {
245                         timeout = TV2MILLISEC( tvp );
246                 }
247                 do {
248                         fd.revents = 0;
249                         rc = poll( &fd, 1, timeout );
250                 
251                 } while ( rc == AC_SOCKET_ERROR && errno == EINTR &&
252                         LDAP_BOOL_GET( &ld->ld_options, LDAP_BOOL_RESTART ) );
253
254                 if ( rc == AC_SOCKET_ERROR ) {
255                         return rc;
256                 }
257
258                 if ( timeout == 0 && rc == 0 ) {
259                         return -2;
260                 }
261
262                 if ( fd.revents & POLL_WRITE ) {
263                         if ( ldap_pvt_is_socket_ready( ld, s ) == -1 ) {
264                                 return -1;
265                         }
266
267                         if ( ldap_pvt_ndelay_off( ld, s ) == -1 ) {
268                                 return -1;
269                         }
270                         return 0;
271                 }
272         }
273 #else
274         {
275                 fd_set          wfds, *z = NULL;
276 #ifdef HAVE_WINSOCK
277                 fd_set          efds;
278 #endif
279                 struct timeval  tv = { 0 };
280
281 #if defined( FD_SETSIZE ) && !defined( HAVE_WINSOCK )
282                 if ( s >= FD_SETSIZE ) {
283                         rc = AC_SOCKET_ERROR;
284                         tcp_close( s );
285                         ldap_pvt_set_errno( EMFILE );
286                         return rc;
287                 }
288 #endif
289
290                 if ( tvp != NULL ) {
291                         tv = *tvp;
292                 }
293
294                 do {
295                         FD_ZERO(&wfds);
296                         FD_SET(s, &wfds );
297
298 #ifdef HAVE_WINSOCK
299                         FD_ZERO(&efds);
300                         FD_SET(s, &efds );
301 #endif
302
303                         rc = select( ldap_int_tblsize, z, &wfds,
304 #ifdef HAVE_WINSOCK
305                                 &efds,
306 #else
307                                 z,
308 #endif
309                                 tvp ? &tv : NULL );
310                 } while ( rc == AC_SOCKET_ERROR && errno == EINTR &&
311                         LDAP_BOOL_GET( &ld->ld_options, LDAP_BOOL_RESTART ) );
312
313                 if ( rc == AC_SOCKET_ERROR ) {
314                         return rc;
315                 }
316
317                 if ( rc == 0 && tvp && tvp->tv_sec == 0 && tvp->tv_usec == 0 ) {
318                         return -2;
319                 }
320
321 #ifdef HAVE_WINSOCK
322                 /* This means the connection failed */
323                 if ( FD_ISSET(s, &efds) ) {
324                         int so_errno;
325                         ber_socklen_t dummy = sizeof(so_errno);
326                         if ( getsockopt( s, SOL_SOCKET, SO_ERROR,
327                                 (char *) &so_errno, &dummy ) == AC_SOCKET_ERROR || !so_errno )
328                         {
329                                 /* impossible */
330                                 so_errno = WSAGetLastError();
331                         }
332                         ldap_pvt_set_errno( so_errno );
333                         osip_debug(ld, "ldap_int_poll: error on socket %d: "
334                                "errno: %d (%s)\n", s, errno, sock_errstr( errno ));
335                         return -1;
336                 }
337 #endif
338                 if ( FD_ISSET(s, &wfds) ) {
339 #ifndef HAVE_WINSOCK
340                         if ( ldap_pvt_is_socket_ready( ld, s ) == -1 ) {
341                                 return -1;
342                         }
343 #endif
344                         if ( ldap_pvt_ndelay_off(ld, s) == -1 ) {
345                                 return -1;
346                         }
347                         return 0;
348                 }
349         }
350 #endif
351
352         osip_debug(ld, "ldap_int_poll: timed out\n",0,0,0);
353         ldap_pvt_set_errno( ETIMEDOUT );
354         return -1;
355 }
356
357 static int
358 ldap_pvt_connect(LDAP *ld, ber_socket_t s,
359         struct sockaddr *sin, ber_socklen_t addrlen,
360         int async)
361 {
362         int rc, err;
363         struct timeval  tv, *opt_tv = NULL;
364
365 #ifdef LDAP_CONNECTIONLESS
366         /* We could do a connect() but that would interfere with
367          * attempts to poll a broadcast address
368          */
369         if (LDAP_IS_UDP(ld)) {
370                 if (ld->ld_options.ldo_peer)
371                         ldap_memfree(ld->ld_options.ldo_peer);
372                 ld->ld_options.ldo_peer=ldap_memalloc(sizeof(struct sockaddr));
373                 AC_MEMCPY(ld->ld_options.ldo_peer,sin,sizeof(struct sockaddr));
374                 return ( 0 );
375         }
376 #endif
377         if ( ld->ld_options.ldo_tm_net.tv_sec >= 0 ) {
378                 tv = ld->ld_options.ldo_tm_net;
379                 opt_tv = &tv;
380         }
381
382         osip_debug(ld, "ldap_pvt_connect: fd: %d tm: %ld async: %d\n",
383                         s, opt_tv ? tv.tv_sec : -1L, async);
384
385         if ( opt_tv && ldap_pvt_ndelay_on(ld, s) == -1 )
386                 return ( -1 );
387
388         if ( connect(s, sin, addrlen) != AC_SOCKET_ERROR ) {
389                 if ( opt_tv && ldap_pvt_ndelay_off(ld, s) == -1 )
390                         return ( -1 );
391                 return ( 0 );
392         }
393
394         err = sock_errno();
395         if ( err != EINPROGRESS && err != EWOULDBLOCK ) {
396                 return ( -1 );
397         }
398         
399         if ( async ) {
400                 /* caller will call ldap_int_poll() as appropriate? */
401                 return ( -2 );
402         }
403
404         rc = ldap_int_poll( ld, s, opt_tv );
405
406         osip_debug(ld, "ldap_pvt_connect: %d\n", rc, 0, 0);
407
408         return rc;
409 }
410
411 #ifndef HAVE_INET_ATON
412 int
413 ldap_pvt_inet_aton( const char *host, struct in_addr *in)
414 {
415         unsigned long u = inet_addr( host );
416
417 #ifdef INADDR_NONE
418         if ( u == INADDR_NONE ) return 0;
419 #endif
420         if ( u == 0xffffffffUL || u == (unsigned long) -1L ) return 0;
421
422         in->s_addr = u;
423         return 1;
424 }
425 #endif
426
427 int
428 ldap_int_connect_cbs(LDAP *ld, Sockbuf *sb, ber_socket_t *s, LDAPURLDesc *srv, struct sockaddr *addr)
429 {
430         struct ldapoptions *lo;
431         ldaplist *ll;
432         ldap_conncb *cb;
433         int rc;
434
435         ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, s );
436
437         /* Invoke all handle-specific callbacks first */
438         lo = &ld->ld_options;
439         for (ll = lo->ldo_conn_cbs; ll; ll = ll->ll_next) {
440                 cb = ll->ll_data;
441                 rc = cb->lc_add( ld, sb, srv, addr, cb );
442                 /* on any failure, call the teardown functions for anything
443                  * that previously succeeded
444                  */
445                 if ( rc ) {
446                         ldaplist *l2;
447                         for (l2 = lo->ldo_conn_cbs; l2 != ll; l2 = l2->ll_next) {
448                                 cb = l2->ll_data;
449                                 cb->lc_del( ld, sb, cb );
450                         }
451                         /* a failure might have implicitly closed the fd */
452                         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, s );
453                         return rc;
454                 }
455         }
456         lo = LDAP_INT_GLOBAL_OPT();
457         for (ll = lo->ldo_conn_cbs; ll; ll = ll->ll_next) {
458                 cb = ll->ll_data;
459                 rc = cb->lc_add( ld, sb, srv, addr, cb );
460                 if ( rc ) {
461                         ldaplist *l2;
462                         for (l2 = lo->ldo_conn_cbs; l2 != ll; l2 = l2->ll_next) {
463                                 cb = l2->ll_data;
464                                 cb->lc_del( ld, sb, cb );
465                         }
466                         lo = &ld->ld_options;
467                         for (l2 = lo->ldo_conn_cbs; l2; l2 = l2->ll_next) {
468                                 cb = l2->ll_data;
469                                 cb->lc_del( ld, sb, cb );
470                         }
471                         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, s );
472                         return rc;
473                 }
474         }
475         return 0;
476 }
477
478 int
479 ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
480         int proto, LDAPURLDesc *srv,
481         int async )
482 {
483         int     rc;
484         int     socktype, port;
485         ber_socket_t            s = AC_SOCKET_INVALID;
486         char *host;
487
488 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
489         char serv[7];
490         int err;
491         struct addrinfo hints, *res, *sai;
492 #else
493         int i;
494         int use_hp = 0;
495         struct hostent *hp = NULL;
496         struct hostent he_buf;
497         struct in_addr in;
498         char *ha_buf=NULL;
499 #endif
500
501         if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
502                 host = "localhost";
503         } else {
504                 host = srv->lud_host;
505         }
506
507         port = srv->lud_port;
508
509         if( !port ) {
510                 if( strcmp(srv->lud_scheme, "ldaps") == 0 ) {
511                         port = LDAPS_PORT;
512                 } else {
513                         port = LDAP_PORT;
514                 }
515         }
516
517         switch(proto) {
518         case LDAP_PROTO_TCP: socktype = SOCK_STREAM;
519                 osip_debug( ld,
520                         "ldap_connect_to_host: TCP %s:%d\n",
521                         host, port, 0);
522                 break;
523         case LDAP_PROTO_UDP: socktype = SOCK_DGRAM;
524                 osip_debug( ld,
525                         "ldap_connect_to_host: UDP %s:%d\n",
526                         host, port, 0);
527                 break;
528         default:
529                 osip_debug( ld, "ldap_connect_to_host: unknown proto: %d\n",
530                         proto, 0, 0 );
531                 return -1;
532         }
533
534 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
535         memset( &hints, '\0', sizeof(hints) );
536 #ifdef USE_AI_ATTRCONFIG /* FIXME: configure test needed */
537         /* Use AI_ATTRCONFIG only on systems where its known to be needed. */
538         hints.ai_flags = AI_ATTRCONFIG;
539 #endif
540         hints.ai_family = ldap_int_inet4or6;
541         hints.ai_socktype = socktype;
542         snprintf(serv, sizeof serv, "%d", port );
543
544 #ifdef LDAP_R_COMPILE
545         /* most getaddrinfo(3) use non-threadsafe resolver libraries */
546         ldap_pvt_thread_mutex_lock(&ldap_int_resolv_mutex);
547 #endif
548
549         err = getaddrinfo( host, serv, &hints, &res );
550
551 #ifdef LDAP_R_COMPILE
552         ldap_pvt_thread_mutex_unlock(&ldap_int_resolv_mutex);
553 #endif
554
555         if ( err != 0 ) {
556                 osip_debug(ld, "ldap_connect_to_host: getaddrinfo failed: %s\n",
557                         AC_GAI_STRERROR(err), 0, 0);
558                 return -1;
559         }
560         rc = -1;
561
562         for( sai=res; sai != NULL; sai=sai->ai_next) {
563                 if( sai->ai_addr == NULL ) {
564                         osip_debug(ld, "ldap_connect_to_host: getaddrinfo "
565                                 "ai_addr is NULL?\n", 0, 0, 0);
566                         continue;
567                 }
568
569                 /* we assume AF_x and PF_x are equal for all x */
570                 s = ldap_int_socket( ld, sai->ai_family, socktype );
571                 if ( s == AC_SOCKET_INVALID ) {
572                         continue;
573                 }
574
575                 if ( ldap_int_prepare_socket(ld, s, proto ) == -1 ) {
576                         ldap_pvt_close_socket(ld, s);
577                         break;
578                 }
579
580                 switch (sai->ai_family) {
581 #ifdef LDAP_PF_INET6
582                         case AF_INET6: {
583                                 char addr[INET6_ADDRSTRLEN];
584                                 inet_ntop( AF_INET6,
585                                         &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
586                                         addr, sizeof addr);
587                                 osip_debug(ld, "ldap_connect_to_host: Trying %s %s\n", 
588                                         addr, serv, 0);
589                         } break;
590 #endif
591                         case AF_INET: {
592                                 char addr[INET_ADDRSTRLEN];
593                                 inet_ntop( AF_INET,
594                                         &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
595                                         addr, sizeof addr);
596                                 osip_debug(ld, "ldap_connect_to_host: Trying %s:%s\n", 
597                                         addr, serv, 0);
598                         } break;
599                 }
600
601                 rc = ldap_pvt_connect( ld, s,
602                         sai->ai_addr, sai->ai_addrlen, async );
603                 if ( rc == 0 || rc == -2 ) {
604                         err = ldap_int_connect_cbs( ld, sb, &s, srv, sai->ai_addr );
605                         if ( err )
606                                 rc = err;
607                         else
608                                 break;
609                 }
610                 ldap_pvt_close_socket(ld, s);
611         }
612         freeaddrinfo(res);
613
614 #else
615         if (! inet_aton( host, &in ) ) {
616                 int local_h_errno;
617                 rc = ldap_pvt_gethostbyname_a( host, &he_buf, &ha_buf,
618                         &hp, &local_h_errno );
619
620                 if ( (rc < 0) || (hp == NULL) ) {
621 #ifdef HAVE_WINSOCK
622                         ldap_pvt_set_errno( WSAGetLastError() );
623 #else
624                         /* not exactly right, but... */
625                         ldap_pvt_set_errno( EHOSTUNREACH );
626 #endif
627                         if (ha_buf) LDAP_FREE(ha_buf);
628                         return -1;
629                 }
630
631                 use_hp = 1;
632         }
633
634         rc = s = -1;
635         for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
636                 struct sockaddr_in      sin;
637
638                 s = ldap_int_socket( ld, PF_INET, socktype );
639                 if ( s == AC_SOCKET_INVALID ) {
640                         /* use_hp ? continue : break; */
641                         break;
642                 }
643            
644                 if ( ldap_int_prepare_socket( ld, s, proto ) == -1 ) {
645                         ldap_pvt_close_socket(ld, s);
646                         break;
647                 }
648
649                 (void)memset((char *)&sin, '\0', sizeof sin);
650                 sin.sin_family = AF_INET;
651                 sin.sin_port = htons((unsigned short) port);
652
653                 if( use_hp ) {
654                         AC_MEMCPY( &sin.sin_addr, hp->h_addr_list[i],
655                                 sizeof(sin.sin_addr) );
656                 } else {
657                         AC_MEMCPY( &sin.sin_addr, &in.s_addr,
658                                 sizeof(sin.sin_addr) );
659                 }
660
661 #ifdef HAVE_INET_NTOA_B
662                 {
663                         /* for VxWorks */
664                         char address[INET_ADDR_LEN];
665                         inet_ntoa_b(sin.sin_address, address);
666                         osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n", 
667                                 address, port, 0);
668                 }
669 #else
670                 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n", 
671                         inet_ntoa(sin.sin_addr), port, 0);
672 #endif
673
674                 rc = ldap_pvt_connect(ld, s,
675                         (struct sockaddr *)&sin, sizeof(sin),
676                         async);
677    
678                 if ( (rc == 0) || (rc == -2) ) {
679                         i = ldap_int_connect_cbs( ld, sb, &s, srv, (struct sockaddr *)&sin );
680                         if ( i )
681                                 rc = i;
682                         else
683                                 break;
684                 }
685
686                 ldap_pvt_close_socket(ld, s);
687
688                 if (!use_hp) break;
689         }
690         if (ha_buf) LDAP_FREE(ha_buf);
691 #endif
692
693         return rc;
694 }
695
696 #if defined( HAVE_CYRUS_SASL )
697 char *
698 ldap_host_connected_to( Sockbuf *sb, const char *host )
699 {
700         ber_socklen_t   len;
701 #ifdef LDAP_PF_INET6
702         struct sockaddr_storage sabuf;
703 #else
704         struct sockaddr sabuf;
705 #endif
706         struct sockaddr *sa = (struct sockaddr *) &sabuf;
707         ber_socket_t    sd;
708
709         (void)memset( (char *)sa, '\0', sizeof sabuf );
710         len = sizeof sabuf;
711
712         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
713         if ( getpeername( sd, sa, &len ) == -1 ) {
714                 return( NULL );
715         }
716
717         /*
718          * do a reverse lookup on the addr to get the official hostname.
719          * this is necessary for kerberos to work right, since the official
720          * hostname is used as the kerberos instance.
721          */
722
723         switch (sa->sa_family) {
724 #ifdef LDAP_PF_LOCAL
725         case AF_LOCAL:
726                 return LDAP_STRDUP( ldap_int_hostname );
727 #endif
728 #ifdef LDAP_PF_INET6
729         case AF_INET6:
730                 {
731                         struct in6_addr localhost = IN6ADDR_LOOPBACK_INIT;
732                         if( memcmp ( &((struct sockaddr_in6 *)sa)->sin6_addr,
733                                 &localhost, sizeof(localhost)) == 0 )
734                         {
735                                 return LDAP_STRDUP( ldap_int_hostname );
736                         }
737                 }
738                 break;
739 #endif
740         case AF_INET:
741                 {
742                         struct in_addr localhost;
743                         localhost.s_addr = htonl( INADDR_ANY );
744
745                         if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
746                                 &localhost, sizeof(localhost) ) == 0 )
747                         {
748                                 return LDAP_STRDUP( ldap_int_hostname );
749                         }
750
751 #ifdef INADDR_LOOPBACK
752                         localhost.s_addr = htonl( INADDR_LOOPBACK );
753
754                         if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
755                                 &localhost, sizeof(localhost) ) == 0 )
756                         {
757                                 return LDAP_STRDUP( ldap_int_hostname );
758                         }
759 #endif
760                 }
761                 break;
762
763         default:
764                 return( NULL );
765                 break;
766         }
767
768         {
769                 char *herr;
770 #ifdef NI_MAXHOST
771                 char hbuf[NI_MAXHOST];
772 #elif defined( MAXHOSTNAMELEN )
773                 char hbuf[MAXHOSTNAMELEN];
774 #else
775                 char hbuf[256];
776 #endif
777                 hbuf[0] = 0;
778
779                 if (ldap_pvt_get_hname( sa, len, hbuf, sizeof(hbuf), &herr ) == 0
780                         && hbuf[0] ) 
781                 {
782                         return LDAP_STRDUP( hbuf );   
783                 }
784         }
785
786         return host ? LDAP_STRDUP( host ) : NULL;
787 }
788 #endif
789
790
791 struct selectinfo {
792 #ifdef HAVE_POLL
793         /* for UNIX poll(2) */
794         int si_maxfd;
795         struct pollfd si_fds[FD_SETSIZE];
796 #else
797         /* for UNIX select(2) */
798         fd_set  si_readfds;
799         fd_set  si_writefds;
800         fd_set  si_use_readfds;
801         fd_set  si_use_writefds;
802 #endif
803 };
804
805 void
806 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
807 {
808         struct selectinfo       *sip;
809         ber_socket_t            sd;
810
811         sip = (struct selectinfo *)ld->ld_selectinfo;
812         
813         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
814
815 #ifdef HAVE_POLL
816         /* for UNIX poll(2) */
817         {
818                 int empty=-1;
819                 int i;
820                 for(i=0; i < sip->si_maxfd; i++) {
821                         if( sip->si_fds[i].fd == sd ) {
822                                 sip->si_fds[i].events |= POLL_WRITE;
823                                 return;
824                         }
825                         if( empty==-1 && sip->si_fds[i].fd == -1 ) {
826                                 empty=i;
827                         }
828                 }
829
830                 if( empty == -1 ) {
831                         if( sip->si_maxfd >= FD_SETSIZE ) {
832                                 /* FIXME */
833                                 return;
834                         }
835                         empty = sip->si_maxfd++;
836                 }
837
838                 sip->si_fds[empty].fd = sd;
839                 sip->si_fds[empty].events = POLL_WRITE;
840         }
841 #else
842         /* for UNIX select(2) */
843         if ( !FD_ISSET( sd, &sip->si_writefds )) {
844                 FD_SET( sd, &sip->si_writefds );
845         }
846 #endif
847 }
848
849
850 void
851 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
852 {
853         struct selectinfo       *sip;
854         ber_socket_t            sd;
855
856         sip = (struct selectinfo *)ld->ld_selectinfo;
857
858         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
859
860 #ifdef HAVE_POLL
861         /* for UNIX poll(2) */
862         {
863                 int empty=-1;
864                 int i;
865                 for(i=0; i < sip->si_maxfd; i++) {
866                         if( sip->si_fds[i].fd == sd ) {
867                                 sip->si_fds[i].events |= POLL_READ;
868                                 return;
869                         }
870                         if( empty==-1 && sip->si_fds[i].fd == -1 ) {
871                                 empty=i;
872                         }
873                 }
874
875                 if( empty == -1 ) {
876                         if( sip->si_maxfd >= FD_SETSIZE ) {
877                                 /* FIXME */
878                                 return;
879                         }
880                         empty = sip->si_maxfd++;
881                 }
882
883                 sip->si_fds[empty].fd = sd;
884                 sip->si_fds[empty].events = POLL_READ;
885         }
886 #else
887         /* for UNIX select(2) */
888         if ( !FD_ISSET( sd, &sip->si_readfds )) {
889                 FD_SET( sd, &sip->si_readfds );
890         }
891 #endif
892 }
893
894
895 void
896 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
897 {
898         struct selectinfo       *sip;
899         ber_socket_t            sd;
900
901         sip = (struct selectinfo *)ld->ld_selectinfo;
902
903         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
904
905 #ifdef HAVE_POLL
906         /* for UNIX poll(2) */
907         {
908                 int i;
909                 for(i=0; i < sip->si_maxfd; i++) {
910                         if( sip->si_fds[i].fd == sd ) {
911                                 sip->si_fds[i].fd = -1;
912                         }
913                 }
914         }
915 #else
916         /* for UNIX select(2) */
917         FD_CLR( sd, &sip->si_writefds );
918         FD_CLR( sd, &sip->si_readfds );
919 #endif
920 }
921
922
923 int
924 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
925 {
926         struct selectinfo       *sip;
927         ber_socket_t            sd;
928
929         sip = (struct selectinfo *)ld->ld_selectinfo;
930
931         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
932
933 #ifdef HAVE_POLL
934         /* for UNIX poll(2) */
935         {
936                 int i;
937                 for(i=0; i < sip->si_maxfd; i++) {
938                         if( sip->si_fds[i].fd == sd ) {
939                                 return sip->si_fds[i].revents & POLL_WRITE;
940                         }
941                 }
942
943                 return 0;
944         }
945 #else
946         /* for UNIX select(2) */
947         return( FD_ISSET( sd, &sip->si_use_writefds ));
948 #endif
949 }
950
951
952 int
953 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
954 {
955         struct selectinfo       *sip;
956         ber_socket_t            sd;
957
958         sip = (struct selectinfo *)ld->ld_selectinfo;
959
960         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
961
962 #ifdef HAVE_POLL
963         /* for UNIX poll(2) */
964         {
965                 int i;
966                 for(i=0; i < sip->si_maxfd; i++) {
967                         if( sip->si_fds[i].fd == sd ) {
968                                 return sip->si_fds[i].revents & POLL_READ;
969                         }
970                 }
971
972                 return 0;
973         }
974 #else
975         /* for UNIX select(2) */
976         return( FD_ISSET( sd, &sip->si_use_readfds ));
977 #endif
978 }
979
980
981 void *
982 ldap_new_select_info( void )
983 {
984         struct selectinfo       *sip;
985
986         sip = (struct selectinfo *)LDAP_CALLOC( 1, sizeof( struct selectinfo ));
987
988         if ( sip == NULL ) return NULL;
989
990 #ifdef HAVE_POLL
991         /* for UNIX poll(2) */
992         /* sip->si_maxfd=0 */
993 #else
994         /* for UNIX select(2) */
995         FD_ZERO( &sip->si_readfds );
996         FD_ZERO( &sip->si_writefds );
997 #endif
998
999         return( (void *)sip );
1000 }
1001
1002
1003 void
1004 ldap_free_select_info( void *sip )
1005 {
1006         LDAP_FREE( sip );
1007 }
1008
1009
1010 #ifndef HAVE_POLL
1011 int ldap_int_tblsize = 0;
1012
1013 void
1014 ldap_int_ip_init( void )
1015 {
1016 #if defined( HAVE_SYSCONF )
1017         long tblsize = sysconf( _SC_OPEN_MAX );
1018         if( tblsize > INT_MAX ) tblsize = INT_MAX;
1019
1020 #elif defined( HAVE_GETDTABLESIZE )
1021         int tblsize = getdtablesize();
1022 #else
1023         int tblsize = FD_SETSIZE;
1024 #endif /* !USE_SYSCONF */
1025
1026 #ifdef FD_SETSIZE
1027         if( tblsize > FD_SETSIZE ) tblsize = FD_SETSIZE;
1028 #endif  /* FD_SETSIZE */
1029
1030         ldap_int_tblsize = tblsize;
1031 }
1032 #endif
1033
1034
1035 int
1036 ldap_int_select( LDAP *ld, struct timeval *timeout )
1037 {
1038         int rc;
1039         struct selectinfo       *sip;
1040
1041         Debug( LDAP_DEBUG_TRACE, "ldap_int_select\n", 0, 0, 0 );
1042
1043 #ifndef HAVE_POLL
1044         if ( ldap_int_tblsize == 0 ) ldap_int_ip_init();
1045 #endif
1046
1047         sip = (struct selectinfo *)ld->ld_selectinfo;
1048         assert( sip != NULL );
1049
1050 #ifdef HAVE_POLL
1051         {
1052                 int to = timeout ? TV2MILLISEC( timeout ) : INFTIM;
1053                 rc = poll( sip->si_fds, sip->si_maxfd, to );
1054         }
1055 #else
1056         sip->si_use_readfds = sip->si_readfds;
1057         sip->si_use_writefds = sip->si_writefds;
1058         
1059         rc = select( ldap_int_tblsize,
1060                 &sip->si_use_readfds, &sip->si_use_writefds,
1061                 NULL, timeout );
1062 #endif
1063
1064         return rc;
1065 }