libs: Import code from upstream openldap 2.5.13.
[wine.git] / libs / ldap / libldap / os-ip.c
blobf31d95c2ec8cc232f1992e8b67eed52cfae32f08
1 /* os-ip.c -- platform-specific TCP & UDP related code */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2022 The OpenLDAP Foundation.
6 * Portions Copyright 1999 Lars Uffmann.
7 * All rights reserved.
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.
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>.
17 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
18 * All rights reserved.
20 /* Significant additional contributors include:
21 * Lars Uffman
24 #include "portable.h"
26 #include <stdio.h>
28 #include <ac/stdlib.h>
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>
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
43 #include "ldap-int.h"
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
53 static void
54 ldap_pvt_set_errno(int err)
56 sock_errset(err);
59 int
60 ldap_int_timeval_dup( struct timeval **dest, const struct timeval *src )
62 struct timeval *new;
64 assert( dest != NULL );
66 if (src == NULL) {
67 *dest = NULL;
68 return 0;
71 new = (struct timeval *) LDAP_MALLOC(sizeof(struct timeval));
73 if( new == NULL ) {
74 *dest = NULL;
75 return 1;
78 AC_MEMCPY( (char *) new, (const char *) src, sizeof(struct timeval));
80 *dest = new;
81 return 0;
84 static int
85 ldap_pvt_ndelay_on(LDAP *ld, int fd)
87 Debug1(LDAP_DEBUG_TRACE, "ldap_ndelay_on: %d\n",fd );
88 return ber_pvt_socket_set_nonblock( fd, 1 );
91 static int
92 ldap_pvt_ndelay_off(LDAP *ld, int fd)
94 Debug1(LDAP_DEBUG_TRACE, "ldap_ndelay_off: %d\n",fd );
95 return ber_pvt_socket_set_nonblock( fd, 0 );
98 static ber_socket_t
99 ldap_int_socket(LDAP *ld, int family, int type )
101 ber_socket_t s = socket(family, type, 0);
102 Debug1(LDAP_DEBUG_TRACE, "ldap_new_socket: %d\n",s );
103 #ifdef FD_CLOEXEC
104 fcntl(s, F_SETFD, FD_CLOEXEC);
105 #endif
106 return ( s );
109 static int
110 ldap_pvt_close_socket(LDAP *ld, int s)
112 Debug1(LDAP_DEBUG_TRACE, "ldap_close_socket: %d\n",s );
113 return tcp_close(s);
116 static int
117 ldap_int_prepare_socket(LDAP *ld, int s, int proto )
119 Debug1(LDAP_DEBUG_TRACE, "ldap_prepare_socket: %d\n", s );
121 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY ) || defined( TCP_USER_TIMEOUT )
122 if ( proto == LDAP_PROTO_TCP ) {
123 int dummy = 1;
124 #ifdef SO_KEEPALIVE
125 if ( setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
126 (char*) &dummy, sizeof(dummy) ) == AC_SOCKET_ERROR )
128 Debug1(LDAP_DEBUG_TRACE, "ldap_prepare_socket: "
129 "setsockopt(%d, SO_KEEPALIVE) failed (ignored).\n",
130 s );
132 if ( ld->ld_options.ldo_keepalive_idle > 0 )
134 #ifdef TCP_KEEPIDLE
135 if ( setsockopt( s, IPPROTO_TCP, TCP_KEEPIDLE,
136 (void*) &ld->ld_options.ldo_keepalive_idle,
137 sizeof(ld->ld_options.ldo_keepalive_idle) ) == AC_SOCKET_ERROR )
139 Debug1(LDAP_DEBUG_TRACE,
140 "ldap_prepare_socket: "
141 "setsockopt(%d, TCP_KEEPIDLE) failed (ignored).\n",
142 s );
144 #else
145 Debug0(LDAP_DEBUG_TRACE, "ldap_prepare_socket: "
146 "sockopt TCP_KEEPIDLE not supported on this system.\n" );
147 #endif /* TCP_KEEPIDLE */
149 if ( ld->ld_options.ldo_keepalive_probes > 0 )
151 #ifdef TCP_KEEPCNT
152 if ( setsockopt( s, IPPROTO_TCP, TCP_KEEPCNT,
153 (void*) &ld->ld_options.ldo_keepalive_probes,
154 sizeof(ld->ld_options.ldo_keepalive_probes) ) == AC_SOCKET_ERROR )
156 Debug1(LDAP_DEBUG_TRACE,
157 "ldap_prepare_socket: "
158 "setsockopt(%d, TCP_KEEPCNT) failed (ignored).\n",
159 s );
161 #else
162 Debug0(LDAP_DEBUG_TRACE, "ldap_prepare_socket: "
163 "sockopt TCP_KEEPCNT not supported on this system.\n" );
164 #endif /* TCP_KEEPCNT */
166 if ( ld->ld_options.ldo_keepalive_interval > 0 )
168 #ifdef TCP_KEEPINTVL
169 if ( setsockopt( s, IPPROTO_TCP, TCP_KEEPINTVL,
170 (void*) &ld->ld_options.ldo_keepalive_interval,
171 sizeof(ld->ld_options.ldo_keepalive_interval) ) == AC_SOCKET_ERROR )
173 Debug1(LDAP_DEBUG_TRACE,
174 "ldap_prepare_socket: "
175 "setsockopt(%d, TCP_KEEPINTVL) failed (ignored).\n",
176 s );
178 #else
179 Debug0(LDAP_DEBUG_TRACE, "ldap_prepare_socket: "
180 "sockopt TCP_KEEPINTVL not supported on this system.\n" );
181 #endif /* TCP_KEEPINTVL */
183 #endif /* SO_KEEPALIVE */
184 #ifdef TCP_NODELAY
185 if ( setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
186 (char*) &dummy, sizeof(dummy) ) == AC_SOCKET_ERROR )
188 Debug1(LDAP_DEBUG_TRACE, "ldap_prepare_socket: "
189 "setsockopt(%d, TCP_NODELAY) failed (ignored).\n",
190 s );
192 #endif /* TCP_NODELAY */
193 if ( ld->ld_options.ldo_tcp_user_timeout > 0 )
195 #ifdef TCP_USER_TIMEOUT
196 if ( setsockopt( s, IPPROTO_TCP, TCP_USER_TIMEOUT,
197 (void*) &ld->ld_options.ldo_tcp_user_timeout,
198 sizeof(ld->ld_options.ldo_tcp_user_timeout) ) == AC_SOCKET_ERROR )
200 Debug1(LDAP_DEBUG_TRACE,
201 "ldap_prepare_socket: "
202 "setsockopt(%d, TCP_USER_TIMEOUT) failed (ignored).\n",
203 s );
205 #else
206 Debug0(LDAP_DEBUG_TRACE, "ldap_prepare_socket: "
207 "sockopt TCP_USER_TIMEOUT not supported on this system.\n" );
208 #endif /* TCP_USER_TIMEOUT */
211 #endif /* SO_KEEPALIVE || TCP_NODELAY || TCP_USER_TIMEOUT */
213 return 0;
216 #ifndef HAVE_WINSOCK
218 #undef TRACE
219 #define TRACE do { \
220 char ebuf[128]; \
221 int saved_errno = errno; \
222 Debug3(LDAP_DEBUG_TRACE, "ldap_is_socket_ready: error on socket %d: errno: %d (%s)\n", \
223 s, \
224 saved_errno, \
225 sock_errstr(saved_errno, ebuf, sizeof(ebuf)) ); \
226 } while( 0 )
229 * check the socket for errors after select returned.
231 static int
232 ldap_pvt_is_socket_ready(LDAP *ld, int s)
234 Debug1(LDAP_DEBUG_TRACE, "ldap_is_sock_ready: %d\n",s );
236 #if defined( notyet ) /* && defined( SO_ERROR ) */
238 int so_errno;
239 ber_socklen_t dummy = sizeof(so_errno);
240 if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
241 == AC_SOCKET_ERROR )
243 return -1;
245 if ( so_errno ) {
246 ldap_pvt_set_errno(so_errno);
247 TRACE;
248 return -1;
250 return 0;
252 #else
254 /* error slippery */
255 #ifdef LDAP_PF_INET6
256 struct sockaddr_storage sin;
257 #else
258 struct sockaddr_in sin;
259 #endif
260 char ch;
261 ber_socklen_t dummy = sizeof(sin);
262 if ( getpeername( s, (struct sockaddr *) &sin, &dummy )
263 == AC_SOCKET_ERROR )
265 /* XXX: needs to be replace with ber_stream_read() */
266 (void)!read(s, &ch, 1);
267 TRACE;
268 return -1;
270 return 0;
272 #endif
273 return -1;
275 #undef TRACE
277 #endif /* HAVE_WINSOCK */
279 /* NOTE: this is identical to analogous code in os-local.c */
281 ldap_int_poll(
282 LDAP *ld,
283 ber_socket_t s,
284 struct timeval *tvp,
285 int wr )
287 int rc;
290 Debug2(LDAP_DEBUG_TRACE, "ldap_int_poll: fd: %d tm: %ld\n",
291 s, tvp ? tvp->tv_sec : -1L );
293 #ifdef HAVE_POLL
295 struct pollfd fd;
296 int timeout = INFTIM;
297 short event = wr ? POLL_WRITE : POLL_READ;
299 fd.fd = s;
300 fd.events = event;
302 if ( tvp != NULL ) {
303 timeout = TV2MILLISEC( tvp );
305 do {
306 fd.revents = 0;
307 rc = poll( &fd, 1, timeout );
309 } while ( rc == AC_SOCKET_ERROR && errno == EINTR &&
310 LDAP_BOOL_GET( &ld->ld_options, LDAP_BOOL_RESTART ) );
312 if ( rc == AC_SOCKET_ERROR ) {
313 return rc;
316 if ( timeout == 0 && rc == 0 ) {
317 return -2;
320 if ( fd.revents & event ) {
321 if ( ldap_pvt_is_socket_ready( ld, s ) == -1 ) {
322 return -1;
325 if ( ldap_pvt_ndelay_off( ld, s ) == -1 ) {
326 return -1;
328 return 0;
331 #else
333 fd_set wfds, *z = NULL;
334 #ifdef HAVE_WINSOCK
335 fd_set efds;
336 #endif
337 struct timeval tv = { 0 };
339 #if defined( FD_SETSIZE ) && !defined( HAVE_WINSOCK )
340 if ( s >= FD_SETSIZE ) {
341 rc = AC_SOCKET_ERROR;
342 tcp_close( s );
343 ldap_pvt_set_errno( EMFILE );
344 return rc;
346 #endif
348 if ( tvp != NULL ) {
349 tv = *tvp;
352 do {
353 FD_ZERO(&wfds);
354 FD_SET(s, &wfds );
356 #ifdef HAVE_WINSOCK
357 FD_ZERO(&efds);
358 FD_SET(s, &efds );
359 #endif
361 rc = select( ldap_int_tblsize, z, &wfds,
362 #ifdef HAVE_WINSOCK
363 &efds,
364 #else
366 #endif
367 tvp ? &tv : NULL );
368 } while ( rc == AC_SOCKET_ERROR && errno == EINTR &&
369 LDAP_BOOL_GET( &ld->ld_options, LDAP_BOOL_RESTART ) );
371 if ( rc == AC_SOCKET_ERROR ) {
372 return rc;
375 if ( rc == 0 && tvp && tvp->tv_sec == 0 && tvp->tv_usec == 0 ) {
376 return -2;
379 #ifdef HAVE_WINSOCK
380 /* This means the connection failed */
381 if ( FD_ISSET(s, &efds) ) {
382 int so_errno;
383 ber_socklen_t dummy = sizeof(so_errno);
384 if ( getsockopt( s, SOL_SOCKET, SO_ERROR,
385 (char *) &so_errno, &dummy ) == AC_SOCKET_ERROR || !so_errno )
387 /* impossible */
388 so_errno = WSAGetLastError();
390 ldap_pvt_set_errno( so_errno );
391 Debug3(LDAP_DEBUG_TRACE,
392 "ldap_int_poll: error on socket %d: "
393 "errno: %d (%s)\n", s, so_errno, sock_errstr( so_errno, dummy, dummy ));
394 return -1;
396 #endif
397 if ( FD_ISSET(s, &wfds) ) {
398 #ifndef HAVE_WINSOCK
399 if ( ldap_pvt_is_socket_ready( ld, s ) == -1 ) {
400 return -1;
402 #endif
403 if ( ldap_pvt_ndelay_off(ld, s) == -1 ) {
404 return -1;
406 return 0;
409 #endif
411 Debug0(LDAP_DEBUG_TRACE, "ldap_int_poll: timed out\n" );
412 ldap_pvt_set_errno( ETIMEDOUT );
413 return -1;
416 static int
417 ldap_pvt_connect(LDAP *ld, ber_socket_t s,
418 struct sockaddr *sin, ber_socklen_t addrlen,
419 int async)
421 int rc, err;
422 struct timeval tv, *opt_tv = NULL;
424 #ifdef LDAP_CONNECTIONLESS
425 /* We could do a connect() but that would interfere with
426 * attempts to poll a broadcast address
428 if (LDAP_IS_UDP(ld)) {
429 if (ld->ld_options.ldo_peer)
430 ldap_memfree(ld->ld_options.ldo_peer);
431 ld->ld_options.ldo_peer=ldap_memcalloc(1, sizeof(struct sockaddr_storage));
432 AC_MEMCPY(ld->ld_options.ldo_peer,sin,addrlen);
433 return ( 0 );
435 #endif
436 if ( ld->ld_options.ldo_tm_net.tv_sec >= 0 ) {
437 tv = ld->ld_options.ldo_tm_net;
438 opt_tv = &tv;
441 Debug3(LDAP_DEBUG_TRACE,
442 "ldap_pvt_connect: fd: %d tm: %ld async: %d\n",
443 s, opt_tv ? tv.tv_sec : -1L, async);
445 if ( opt_tv && ldap_pvt_ndelay_on(ld, s) == -1 )
446 return ( -1 );
449 Debug0(LDAP_DEBUG_TRACE, "attempting to connect: \n" );
450 if ( connect(s, sin, addrlen) != AC_SOCKET_ERROR ) {
451 Debug0(LDAP_DEBUG_TRACE, "connect success\n" );
453 if ( !async && opt_tv && ldap_pvt_ndelay_off(ld, s) == -1 )
454 return ( -1 );
455 return ( 0 );
457 err = sock_errno();
458 Debug1(LDAP_DEBUG_TRACE, "connect errno: %d\n", err );
460 } while(err == EINTR &&
461 LDAP_BOOL_GET( &ld->ld_options, LDAP_BOOL_RESTART ));
463 if ( err != EINPROGRESS && err != EWOULDBLOCK ) {
464 return ( -1 );
467 if ( async ) {
468 /* caller will call ldap_int_poll() as appropriate? */
469 return ( -2 );
472 rc = ldap_int_poll( ld, s, opt_tv, 1 );
474 Debug1(LDAP_DEBUG_TRACE, "ldap_pvt_connect: %d\n", rc );
476 return rc;
479 #ifndef HAVE_INET_ATON
481 ldap_pvt_inet_aton( const char *host, struct in_addr *in)
483 unsigned long u = inet_addr( host );
485 #ifdef INADDR_NONE
486 if ( u == INADDR_NONE ) return 0;
487 #endif
488 if ( u == 0xffffffffUL || u == (unsigned long) -1L ) return 0;
490 in->s_addr = u;
491 return 1;
493 #endif
496 ldap_validate_and_fill_sourceip (char** source_ip_lst, ldapsourceip* temp_source_ip )
498 int i = 0;
499 int rc = LDAP_PARAM_ERROR;
501 for ( i = 0; source_ip_lst[i] != NULL; i++ ) {
502 Debug1( LDAP_DEBUG_TRACE,
503 "ldap_validate_and_fill_sourceip(%s)\n",
504 source_ip_lst[i] );
506 if ( !temp_source_ip->has_ipv4 ) {
507 if ( inet_aton( source_ip_lst[i], &temp_source_ip->ip4_addr ) ) {
508 temp_source_ip->has_ipv4 = 1;
509 rc = LDAP_OPT_SUCCESS;
510 continue;
513 #ifdef LDAP_PF_INET6
514 if ( !temp_source_ip->has_ipv6 ) {
515 if ( inet_pton( AF_INET6, source_ip_lst[i],
516 & temp_source_ip->ip6_addr ) ) {
517 temp_source_ip->has_ipv6 = 1;
518 rc = LDAP_OPT_SUCCESS;
519 continue;
522 #endif
523 memset( temp_source_ip, 0, sizeof( * (temp_source_ip ) ) );
524 Debug1( LDAP_DEBUG_TRACE,
525 "ldap_validate_and_fill_sourceip: validation failed for (%s)\n",
526 source_ip_lst[i] );
527 break;
529 return rc;
533 ldap_int_connect_cbs(LDAP *ld, Sockbuf *sb, ber_socket_t *s, LDAPURLDesc *srv, struct sockaddr *addr)
535 struct ldapoptions *lo;
536 ldaplist *ll;
537 ldap_conncb *cb;
538 int rc;
540 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, s );
542 /* Invoke all handle-specific callbacks first */
543 lo = &ld->ld_options;
544 for (ll = lo->ldo_conn_cbs; ll; ll = ll->ll_next) {
545 cb = ll->ll_data;
546 rc = cb->lc_add( ld, sb, srv, addr, cb );
547 /* on any failure, call the teardown functions for anything
548 * that previously succeeded
550 if ( rc ) {
551 ldaplist *l2;
552 for (l2 = lo->ldo_conn_cbs; l2 != ll; l2 = l2->ll_next) {
553 cb = l2->ll_data;
554 cb->lc_del( ld, sb, cb );
556 /* a failure might have implicitly closed the fd */
557 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, s );
558 return rc;
561 lo = LDAP_INT_GLOBAL_OPT();
562 for (ll = lo->ldo_conn_cbs; ll; ll = ll->ll_next) {
563 cb = ll->ll_data;
564 rc = cb->lc_add( ld, sb, srv, addr, cb );
565 if ( rc ) {
566 ldaplist *l2;
567 for (l2 = lo->ldo_conn_cbs; l2 != ll; l2 = l2->ll_next) {
568 cb = l2->ll_data;
569 cb->lc_del( ld, sb, cb );
571 lo = &ld->ld_options;
572 for (l2 = lo->ldo_conn_cbs; l2; l2 = l2->ll_next) {
573 cb = l2->ll_data;
574 cb->lc_del( ld, sb, cb );
576 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, s );
577 return rc;
580 return 0;
584 ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
585 int proto, LDAPURLDesc *srv,
586 int async )
588 int rc;
589 int socktype, port;
590 ber_socket_t s = AC_SOCKET_INVALID;
591 char *host;
593 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
594 char serv[7];
595 int err;
596 struct addrinfo hints, *res, *sai;
597 #else
598 int i;
599 int use_hp = 0;
600 struct hostent *hp = NULL;
601 struct hostent he_buf;
602 struct in_addr in;
603 char *ha_buf=NULL;
604 #endif
606 if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
607 host = "localhost";
608 } else {
609 host = srv->lud_host;
612 port = srv->lud_port;
614 if( !port ) {
615 if( strcmp(srv->lud_scheme, "ldaps") == 0 ) {
616 port = LDAPS_PORT;
617 } else {
618 port = LDAP_PORT;
622 switch(proto) {
623 case LDAP_PROTO_TCP: socktype = SOCK_STREAM;
624 Debug2(LDAP_DEBUG_TRACE, "ldap_connect_to_host: TCP %s:%d\n",
625 host, port );
626 break;
627 case LDAP_PROTO_UDP: socktype = SOCK_DGRAM;
628 Debug2(LDAP_DEBUG_TRACE, "ldap_connect_to_host: UDP %s:%d\n",
629 host, port );
630 break;
631 default:
632 Debug1(LDAP_DEBUG_TRACE,
633 "ldap_connect_to_host: unknown proto: %d\n",
634 proto );
635 return -1;
638 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
639 memset( &hints, '\0', sizeof(hints) );
640 #ifdef USE_AI_ADDRCONFIG /* FIXME: configure test needed */
641 /* Use AI_ADDRCONFIG only on systems where its known to be needed. */
642 hints.ai_flags = AI_ADDRCONFIG;
643 #endif
644 hints.ai_family = ldap_int_inet4or6;
645 hints.ai_socktype = socktype;
646 snprintf(serv, sizeof serv, "%d", port );
648 /* most getaddrinfo(3) use non-threadsafe resolver libraries */
649 LDAP_MUTEX_LOCK(&ldap_int_resolv_mutex);
651 err = getaddrinfo( host, serv, &hints, &res );
653 LDAP_MUTEX_UNLOCK(&ldap_int_resolv_mutex);
655 if ( err != 0 ) {
656 Debug1(LDAP_DEBUG_TRACE,
657 "ldap_connect_to_host: getaddrinfo failed: %s\n",
658 AC_GAI_STRERROR(err) );
659 return -1;
661 rc = -1;
663 for( sai=res; sai != NULL; sai=sai->ai_next) {
664 unsigned short bind_success = 1;
665 if( sai->ai_addr == NULL ) {
666 Debug0(LDAP_DEBUG_TRACE,
667 "ldap_connect_to_host: getaddrinfo "
668 "ai_addr is NULL?\n" );
669 continue;
672 #ifndef LDAP_PF_INET6
673 if ( sai->ai_family == AF_INET6 ) continue;
674 #endif
675 /* we assume AF_x and PF_x are equal for all x */
676 s = ldap_int_socket( ld, sai->ai_family, socktype );
677 if ( s == AC_SOCKET_INVALID ) {
678 continue;
681 if ( ldap_int_prepare_socket(ld, s, proto ) == -1 ) {
682 ldap_pvt_close_socket(ld, s);
683 break;
686 switch (sai->ai_family) {
687 #ifdef LDAP_PF_INET6
688 case AF_INET6: {
689 char addr[INET6_ADDRSTRLEN];
690 inet_ntop( AF_INET6,
691 &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
692 addr, sizeof addr);
693 Debug2(LDAP_DEBUG_TRACE,
694 "ldap_connect_to_host: Trying %s %s\n",
695 addr, serv );
696 if( ld->ld_options.ldo_local_ip_addrs.has_ipv6 ) {
697 struct sockaddr_in6 ip6addr;
698 char bind_addr[INET6_ADDRSTRLEN];
699 ip6addr.sin6_family = AF_INET6;
700 ip6addr.sin6_port = 0;
701 ip6addr.sin6_addr = ld->ld_options.ldo_local_ip_addrs.ip6_addr;
702 inet_ntop( AF_INET6,
703 &(ip6addr.sin6_addr),
704 bind_addr, sizeof bind_addr );
705 Debug1( LDAP_DEBUG_TRACE,
706 "ldap_connect_to_host: From source address %s\n",
707 bind_addr );
708 if ( bind( s, ( struct sockaddr* ) &ip6addr, sizeof ip6addr ) != 0 ) {
709 Debug1( LDAP_DEBUG_TRACE,
710 "ldap_connect_to_host: Failed to bind source address %s\n",
711 bind_addr );
712 bind_success = 0;
715 } break;
716 #endif
717 case AF_INET: {
718 char addr[INET_ADDRSTRLEN];
719 inet_ntop( AF_INET,
720 &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
721 addr, sizeof addr);
722 Debug2(LDAP_DEBUG_TRACE,
723 "ldap_connect_to_host: Trying %s:%s\n",
724 addr, serv );
725 if( ld->ld_options.ldo_local_ip_addrs.has_ipv4 ) {
726 struct sockaddr_in ip4addr;
727 char bind_addr[INET_ADDRSTRLEN];
728 ip4addr.sin_family = AF_INET;
729 ip4addr.sin_port = 0;
730 ip4addr.sin_addr = ld->ld_options.ldo_local_ip_addrs.ip4_addr;
731 inet_ntop( AF_INET,
732 &(ip4addr.sin_addr),
733 bind_addr, sizeof bind_addr );
734 Debug1( LDAP_DEBUG_TRACE,
735 "ldap_connect_to_host: From source address %s\n",
736 bind_addr );
737 if ( bind(s, ( struct sockaddr* )&ip4addr, sizeof ip4addr ) != 0 ) {
738 Debug1( LDAP_DEBUG_TRACE,
739 "ldap_connect_to_host: Failed to bind source address %s\n",
740 bind_addr );
741 bind_success = 0;
744 } break;
746 if ( bind_success ) {
747 rc = ldap_pvt_connect( ld, s,
748 sai->ai_addr, sai->ai_addrlen, async );
749 if ( rc == 0 || rc == -2 ) {
750 err = ldap_int_connect_cbs( ld, sb, &s, srv, sai->ai_addr );
751 if ( err )
752 rc = err;
753 else
754 break;
757 ldap_pvt_close_socket(ld, s);
759 freeaddrinfo(res);
761 #else
762 if (! inet_aton( host, &in ) ) {
763 int local_h_errno;
764 rc = ldap_pvt_gethostbyname_a( host, &he_buf, &ha_buf,
765 &hp, &local_h_errno );
767 if ( (rc < 0) || (hp == NULL) ) {
768 #ifdef HAVE_WINSOCK
769 ldap_pvt_set_errno( WSAGetLastError() );
770 #else
771 /* not exactly right, but... */
772 ldap_pvt_set_errno( EHOSTUNREACH );
773 #endif
774 if (ha_buf) LDAP_FREE(ha_buf);
775 return -1;
778 use_hp = 1;
781 rc = s = -1;
782 for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
783 struct sockaddr_in sin;
784 unsigned short bind_success = 1;
785 #ifdef HAVE_INET_NTOA_B
786 char address[INET_ADDR_LEN];
787 char bind_addr[INET_ADDR_LEN];
788 #else
789 char *address;
790 char *bind_addr;
791 #endif
792 s = ldap_int_socket( ld, PF_INET, socktype );
793 if ( s == AC_SOCKET_INVALID ) {
794 /* use_hp ? continue : break; */
795 break;
798 if ( ldap_int_prepare_socket( ld, s, proto ) == -1 ) {
799 ldap_pvt_close_socket(ld, s);
800 break;
803 (void)memset((char *)&sin, '\0', sizeof sin);
804 sin.sin_family = AF_INET;
805 sin.sin_port = htons((unsigned short) port);
807 if( use_hp ) {
808 AC_MEMCPY( &sin.sin_addr, hp->h_addr_list[i],
809 sizeof(sin.sin_addr) );
810 } else {
811 AC_MEMCPY( &sin.sin_addr, &in.s_addr,
812 sizeof(sin.sin_addr) );
815 #ifdef HAVE_INET_NTOA_B
816 /* for VxWorks */
817 inet_ntoa_b( sin.sin_address, address );
818 #else
819 address = inet_ntoa( sin.sin_addr );
820 #endif
821 Debug2( LDAP_DEBUG_TRACE,
822 "ldap_connect_to_host: Trying %s:%d\n",
823 address, port );
824 if( ld->ld_options.ldo_local_ip_addrs.has_ipv4 ) {
825 struct sockaddr_in ip4addr;
826 ip4addr.sin_family = AF_INET;
827 ip4addr.sin_addr = ld->ld_options.ldo_local_ip_addrs.ip4_addr;
828 #ifdef HAVE_INET_NTOA_B
829 inet_ntoa_b( ip4addr.sin_address, bind_addr );
830 #else
831 bind_addr = inet_ntoa( ip4addr.sin_addr );
832 #endif
833 Debug1( LDAP_DEBUG_TRACE,
834 "ldap_connect_to_host: From source address %s\n",
835 bind_addr );
836 if ( bind( s, (struct sockaddr*)&ip4addr, sizeof ip4addr ) != 0 ) {
837 Debug1( LDAP_DEBUG_TRACE,
838 "ldap_connect_to_host: Failed to bind source address %s\n",
839 bind_addr );
840 bind_success = 0;
843 if ( bind_success ) {
844 rc = ldap_pvt_connect(ld, s,
845 (struct sockaddr *)&sin, sizeof(sin),
846 async);
848 if ( (rc == 0) || (rc == -2) ) {
849 int err = ldap_int_connect_cbs( ld, sb, &s, srv, (struct sockaddr *)&sin );
850 if ( err )
851 rc = err;
852 else
853 break;
857 ldap_pvt_close_socket(ld, s);
859 if (!use_hp) break;
861 if (ha_buf) LDAP_FREE(ha_buf);
862 #endif
864 return rc;
867 #if defined( HAVE_CYRUS_SASL )
868 char *
869 ldap_host_connected_to( Sockbuf *sb, const char *host )
871 ber_socklen_t len;
872 #ifdef LDAP_PF_INET6
873 struct sockaddr_storage sabuf;
874 #else
875 struct sockaddr sabuf;
876 #endif
877 struct sockaddr *sa = (struct sockaddr *) &sabuf;
878 ber_socket_t sd;
880 (void)memset( (char *)sa, '\0', sizeof sabuf );
881 len = sizeof sabuf;
883 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
884 if ( getpeername( sd, sa, &len ) == -1 ) {
885 return( NULL );
889 * do a reverse lookup on the addr to get the official hostname.
890 * this is necessary for kerberos to work right, since the official
891 * hostname is used as the kerberos instance.
894 switch (sa->sa_family) {
895 #ifdef LDAP_PF_LOCAL
896 case AF_LOCAL:
897 return LDAP_STRDUP( ldap_int_hostname );
898 #endif
899 #ifdef LDAP_PF_INET6
900 case AF_INET6:
902 struct in6_addr localhost = IN6ADDR_LOOPBACK_INIT;
903 if( memcmp ( &((struct sockaddr_in6 *)sa)->sin6_addr,
904 &localhost, sizeof(localhost)) == 0 )
906 return LDAP_STRDUP( ldap_int_hostname );
909 break;
910 #endif
911 case AF_INET:
913 struct in_addr localhost;
914 localhost.s_addr = htonl( INADDR_ANY );
916 if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
917 &localhost, sizeof(localhost) ) == 0 )
919 return LDAP_STRDUP( ldap_int_hostname );
922 #ifdef INADDR_LOOPBACK
923 localhost.s_addr = htonl( INADDR_LOOPBACK );
925 if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
926 &localhost, sizeof(localhost) ) == 0 )
928 return LDAP_STRDUP( ldap_int_hostname );
930 #endif
932 break;
934 default:
935 return( NULL );
936 break;
940 char *herr;
941 #ifdef NI_MAXHOST
942 char hbuf[NI_MAXHOST];
943 #elif defined( MAXHOSTNAMELEN )
944 char hbuf[MAXHOSTNAMELEN];
945 #else
946 char hbuf[256];
947 #endif
948 hbuf[0] = 0;
950 if (ldap_pvt_get_hname( sa, len, hbuf, sizeof(hbuf), &herr ) == 0
951 && hbuf[0] )
953 return LDAP_STRDUP( hbuf );
957 return host ? LDAP_STRDUP( host ) : NULL;
959 #endif
962 struct selectinfo {
963 #ifdef HAVE_POLL
964 /* for UNIX poll(2) */
965 int si_maxfd;
966 struct pollfd si_fds[FD_SETSIZE];
967 #else
968 /* for UNIX select(2) */
969 fd_set si_readfds;
970 fd_set si_writefds;
971 fd_set si_use_readfds;
972 fd_set si_use_writefds;
973 #endif
976 void
977 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
979 struct selectinfo *sip;
980 ber_socket_t sd;
982 sip = (struct selectinfo *)ld->ld_selectinfo;
984 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
986 #ifdef HAVE_POLL
987 /* for UNIX poll(2) */
989 int empty=-1;
990 int i;
991 for(i=0; i < sip->si_maxfd; i++) {
992 if( sip->si_fds[i].fd == sd ) {
993 sip->si_fds[i].events |= POLL_WRITE;
994 return;
996 if( empty==-1 && sip->si_fds[i].fd == -1 ) {
997 empty=i;
1001 if( empty == -1 ) {
1002 if( sip->si_maxfd >= FD_SETSIZE ) {
1003 /* FIXME */
1004 return;
1006 empty = sip->si_maxfd++;
1009 sip->si_fds[empty].fd = sd;
1010 sip->si_fds[empty].events = POLL_WRITE;
1012 #else
1013 /* for UNIX select(2) */
1014 if ( !FD_ISSET( sd, &sip->si_writefds )) {
1015 FD_SET( sd, &sip->si_writefds );
1017 #endif
1021 void
1022 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
1024 struct selectinfo *sip;
1025 ber_socket_t sd;
1027 sip = (struct selectinfo *)ld->ld_selectinfo;
1029 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
1031 #ifdef HAVE_POLL
1032 /* for UNIX poll(2) */
1034 int empty=-1;
1035 int i;
1036 for(i=0; i < sip->si_maxfd; i++) {
1037 if( sip->si_fds[i].fd == sd ) {
1038 sip->si_fds[i].events |= POLL_READ;
1039 return;
1041 if( empty==-1 && sip->si_fds[i].fd == -1 ) {
1042 empty=i;
1046 if( empty == -1 ) {
1047 if( sip->si_maxfd >= FD_SETSIZE ) {
1048 /* FIXME */
1049 return;
1051 empty = sip->si_maxfd++;
1054 sip->si_fds[empty].fd = sd;
1055 sip->si_fds[empty].events = POLL_READ;
1057 #else
1058 /* for UNIX select(2) */
1059 if ( !FD_ISSET( sd, &sip->si_readfds )) {
1060 FD_SET( sd, &sip->si_readfds );
1062 #endif
1066 void
1067 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
1069 struct selectinfo *sip;
1070 ber_socket_t sd;
1072 sip = (struct selectinfo *)ld->ld_selectinfo;
1074 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
1076 #ifdef HAVE_POLL
1077 /* for UNIX poll(2) */
1079 int i;
1080 for(i=0; i < sip->si_maxfd; i++) {
1081 if( sip->si_fds[i].fd == sd ) {
1082 sip->si_fds[i].fd = -1;
1086 #else
1087 /* for UNIX select(2) */
1088 FD_CLR( sd, &sip->si_writefds );
1089 FD_CLR( sd, &sip->si_readfds );
1090 #endif
1093 void
1094 ldap_clear_select_write( LDAP *ld, Sockbuf *sb )
1096 struct selectinfo *sip;
1097 ber_socket_t sd;
1099 sip = (struct selectinfo *)ld->ld_selectinfo;
1101 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
1103 #ifdef HAVE_POLL
1104 /* for UNIX poll(2) */
1106 int i;
1107 for(i=0; i < sip->si_maxfd; i++) {
1108 if( sip->si_fds[i].fd == sd ) {
1109 sip->si_fds[i].events &= ~POLL_WRITE;
1113 #else
1114 /* for UNIX select(2) */
1115 FD_CLR( sd, &sip->si_writefds );
1116 #endif
1121 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
1123 struct selectinfo *sip;
1124 ber_socket_t sd;
1126 sip = (struct selectinfo *)ld->ld_selectinfo;
1128 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
1130 #ifdef HAVE_POLL
1131 /* for UNIX poll(2) */
1133 int i;
1134 for(i=0; i < sip->si_maxfd; i++) {
1135 if( sip->si_fds[i].fd == sd ) {
1136 return sip->si_fds[i].revents & POLL_WRITE;
1140 return 0;
1142 #else
1143 /* for UNIX select(2) */
1144 return( FD_ISSET( sd, &sip->si_use_writefds ));
1145 #endif
1150 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
1152 struct selectinfo *sip;
1153 ber_socket_t sd;
1155 sip = (struct selectinfo *)ld->ld_selectinfo;
1157 if (ber_sockbuf_ctrl( sb, LBER_SB_OPT_DATA_READY, NULL ))
1158 return 1;
1160 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
1162 #ifdef HAVE_POLL
1163 /* for UNIX poll(2) */
1165 int i;
1166 for(i=0; i < sip->si_maxfd; i++) {
1167 if( sip->si_fds[i].fd == sd ) {
1168 return sip->si_fds[i].revents & POLL_READ;
1172 return 0;
1174 #else
1175 /* for UNIX select(2) */
1176 return( FD_ISSET( sd, &sip->si_use_readfds ));
1177 #endif
1181 void *
1182 ldap_new_select_info( void )
1184 struct selectinfo *sip;
1186 sip = (struct selectinfo *)LDAP_CALLOC( 1, sizeof( struct selectinfo ));
1188 if ( sip == NULL ) return NULL;
1190 #ifdef HAVE_POLL
1191 /* for UNIX poll(2) */
1192 /* sip->si_maxfd=0 */
1193 #else
1194 /* for UNIX select(2) */
1195 FD_ZERO( &sip->si_readfds );
1196 FD_ZERO( &sip->si_writefds );
1197 #endif
1199 return( (void *)sip );
1203 void
1204 ldap_free_select_info( void *sip )
1206 LDAP_FREE( sip );
1210 #ifndef HAVE_POLL
1211 int ldap_int_tblsize = 0;
1213 void
1214 ldap_int_ip_init( void )
1216 #if defined( HAVE_SYSCONF )
1217 long tblsize = sysconf( _SC_OPEN_MAX );
1218 if( tblsize > INT_MAX ) tblsize = INT_MAX;
1220 #elif defined( HAVE_GETDTABLESIZE )
1221 int tblsize = getdtablesize();
1222 #else
1223 int tblsize = FD_SETSIZE;
1224 #endif /* !USE_SYSCONF */
1226 #ifdef FD_SETSIZE
1227 if( tblsize > FD_SETSIZE ) tblsize = FD_SETSIZE;
1228 #endif /* FD_SETSIZE */
1230 ldap_int_tblsize = tblsize;
1232 #endif
1236 ldap_int_select( LDAP *ld, struct timeval *timeout )
1238 int rc;
1239 struct selectinfo *sip;
1241 Debug0( LDAP_DEBUG_TRACE, "ldap_int_select\n" );
1243 #ifndef HAVE_POLL
1244 if ( ldap_int_tblsize == 0 ) ldap_int_ip_init();
1245 #endif
1247 sip = (struct selectinfo *)ld->ld_selectinfo;
1248 assert( sip != NULL );
1250 #ifdef HAVE_POLL
1252 int to = timeout ? TV2MILLISEC( timeout ) : INFTIM;
1253 rc = poll( sip->si_fds, sip->si_maxfd, to );
1255 #else
1256 sip->si_use_readfds = sip->si_readfds;
1257 sip->si_use_writefds = sip->si_writefds;
1259 rc = select( ldap_int_tblsize,
1260 &sip->si_use_readfds, &sip->si_use_writefds,
1261 NULL, timeout );
1262 #endif
1264 return rc;