1 /* os-ip.c -- platform-specific TCP & UDP related code */
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.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
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:
28 #include <ac/stdlib.h>
31 #include <ac/socket.h>
32 #include <ac/string.h>
34 #include <ac/unistd.h>
38 #endif /* HAVE_IO_H */
45 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
47 int ldap_int_inet4or6
= AF_UNSPEC
;
49 int ldap_int_inet4or6
= AF_INET
;
54 ldap_pvt_set_errno(int err
)
60 ldap_int_timeval_dup( struct timeval
**dest
, const struct timeval
*src
)
64 assert( dest
!= NULL
);
71 new = (struct timeval
*) LDAP_MALLOC(sizeof(struct timeval
));
78 AC_MEMCPY( (char *) new, (const char *) src
, sizeof(struct timeval
));
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 );
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 );
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
);
104 fcntl(s
, F_SETFD
, FD_CLOEXEC
);
110 ldap_pvt_close_socket(LDAP
*ld
, int s
)
112 Debug1(LDAP_DEBUG_TRACE
, "ldap_close_socket: %d\n",s
);
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
) {
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",
132 if ( ld
->ld_options
.ldo_keepalive_idle
> 0 )
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",
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 )
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",
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 )
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",
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 */
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",
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",
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 */
221 int saved_errno = errno; \
222 Debug3(LDAP_DEBUG_TRACE, "ldap_is_socket_ready: error on socket %d: errno: %d (%s)\n", \
225 sock_errstr(saved_errno, ebuf, sizeof(ebuf)) ); \
229 * check the socket for errors after select returned.
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 ) */
239 ber_socklen_t dummy
= sizeof(so_errno
);
240 if ( getsockopt( s
, SOL_SOCKET
, SO_ERROR
, &so_errno
, &dummy
)
246 ldap_pvt_set_errno(so_errno
);
256 struct sockaddr_storage sin
;
258 struct sockaddr_in sin
;
261 ber_socklen_t dummy
= sizeof(sin
);
262 if ( getpeername( s
, (struct sockaddr
*) &sin
, &dummy
)
265 /* XXX: needs to be replace with ber_stream_read() */
266 (void)!read(s
, &ch
, 1);
277 #endif /* HAVE_WINSOCK */
279 /* NOTE: this is identical to analogous code in os-local.c */
290 Debug2(LDAP_DEBUG_TRACE
, "ldap_int_poll: fd: %d tm: %ld\n",
291 s
, tvp
? tvp
->tv_sec
: -1L );
296 int timeout
= INFTIM
;
297 short event
= wr
? POLL_WRITE
: POLL_READ
;
303 timeout
= TV2MILLISEC( tvp
);
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
) {
316 if ( timeout
== 0 && rc
== 0 ) {
320 if ( fd
.revents
& event
) {
321 if ( ldap_pvt_is_socket_ready( ld
, s
) == -1 ) {
325 if ( ldap_pvt_ndelay_off( ld
, s
) == -1 ) {
333 fd_set wfds
, *z
= NULL
;
337 struct timeval tv
= { 0 };
339 #if defined( FD_SETSIZE ) && !defined( HAVE_WINSOCK )
340 if ( s
>= FD_SETSIZE
) {
341 rc
= AC_SOCKET_ERROR
;
343 ldap_pvt_set_errno( EMFILE
);
361 rc
= select( ldap_int_tblsize
, z
, &wfds
,
368 } while ( rc
== AC_SOCKET_ERROR
&& errno
== EINTR
&&
369 LDAP_BOOL_GET( &ld
->ld_options
, LDAP_BOOL_RESTART
) );
371 if ( rc
== AC_SOCKET_ERROR
) {
375 if ( rc
== 0 && tvp
&& tvp
->tv_sec
== 0 && tvp
->tv_usec
== 0 ) {
380 /* This means the connection failed */
381 if ( FD_ISSET(s
, &efds
) ) {
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
)
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
));
397 if ( FD_ISSET(s
, &wfds
) ) {
399 if ( ldap_pvt_is_socket_ready( ld
, s
) == -1 ) {
403 if ( ldap_pvt_ndelay_off(ld
, s
) == -1 ) {
411 Debug0(LDAP_DEBUG_TRACE
, "ldap_int_poll: timed out\n" );
412 ldap_pvt_set_errno( ETIMEDOUT
);
417 ldap_pvt_connect(LDAP
*ld
, ber_socket_t s
,
418 struct sockaddr
*sin
, ber_socklen_t addrlen
,
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
);
436 if ( ld
->ld_options
.ldo_tm_net
.tv_sec
>= 0 ) {
437 tv
= ld
->ld_options
.ldo_tm_net
;
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 )
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 )
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
) {
468 /* caller will call ldap_int_poll() as appropriate? */
472 rc
= ldap_int_poll( ld
, s
, opt_tv
, 1 );
474 Debug1(LDAP_DEBUG_TRACE
, "ldap_pvt_connect: %d\n", 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
);
486 if ( u
== INADDR_NONE
) return 0;
488 if ( u
== 0xffffffffUL
|| u
== (unsigned long) -1L ) return 0;
496 ldap_validate_and_fill_sourceip (char** source_ip_lst
, ldapsourceip
* temp_source_ip
)
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",
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
;
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
;
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",
533 ldap_int_connect_cbs(LDAP
*ld
, Sockbuf
*sb
, ber_socket_t
*s
, LDAPURLDesc
*srv
, struct sockaddr
*addr
)
535 struct ldapoptions
*lo
;
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
) {
546 rc
= cb
->lc_add( ld
, sb
, srv
, addr
, cb
);
547 /* on any failure, call the teardown functions for anything
548 * that previously succeeded
552 for (l2
= lo
->ldo_conn_cbs
; l2
!= ll
; l2
= l2
->ll_next
) {
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
);
561 lo
= LDAP_INT_GLOBAL_OPT();
562 for (ll
= lo
->ldo_conn_cbs
; ll
; ll
= ll
->ll_next
) {
564 rc
= cb
->lc_add( ld
, sb
, srv
, addr
, cb
);
567 for (l2
= lo
->ldo_conn_cbs
; l2
!= ll
; l2
= l2
->ll_next
) {
569 cb
->lc_del( ld
, sb
, cb
);
571 lo
= &ld
->ld_options
;
572 for (l2
= lo
->ldo_conn_cbs
; l2
; l2
= l2
->ll_next
) {
574 cb
->lc_del( ld
, sb
, cb
);
576 ber_sockbuf_ctrl( sb
, LBER_SB_OPT_GET_FD
, s
);
584 ldap_connect_to_host(LDAP
*ld
, Sockbuf
*sb
,
585 int proto
, LDAPURLDesc
*srv
,
590 ber_socket_t s
= AC_SOCKET_INVALID
;
593 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
596 struct addrinfo hints
, *res
, *sai
;
600 struct hostent
*hp
= NULL
;
601 struct hostent he_buf
;
606 if ( srv
->lud_host
== NULL
|| *srv
->lud_host
== 0 ) {
609 host
= srv
->lud_host
;
612 port
= srv
->lud_port
;
615 if( strcmp(srv
->lud_scheme
, "ldaps") == 0 ) {
623 case LDAP_PROTO_TCP
: socktype
= SOCK_STREAM
;
624 Debug2(LDAP_DEBUG_TRACE
, "ldap_connect_to_host: TCP %s:%d\n",
627 case LDAP_PROTO_UDP
: socktype
= SOCK_DGRAM
;
628 Debug2(LDAP_DEBUG_TRACE
, "ldap_connect_to_host: UDP %s:%d\n",
632 Debug1(LDAP_DEBUG_TRACE
,
633 "ldap_connect_to_host: unknown proto: %d\n",
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
;
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
);
656 Debug1(LDAP_DEBUG_TRACE
,
657 "ldap_connect_to_host: getaddrinfo failed: %s\n",
658 AC_GAI_STRERROR(err
) );
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" );
672 #ifndef LDAP_PF_INET6
673 if ( sai
->ai_family
== AF_INET6
) continue;
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
) {
681 if ( ldap_int_prepare_socket(ld
, s
, proto
) == -1 ) {
682 ldap_pvt_close_socket(ld
, s
);
686 switch (sai
->ai_family
) {
689 char addr
[INET6_ADDRSTRLEN
];
691 &((struct sockaddr_in6
*)sai
->ai_addr
)->sin6_addr
,
693 Debug2(LDAP_DEBUG_TRACE
,
694 "ldap_connect_to_host: Trying %s %s\n",
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
;
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",
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",
718 char addr
[INET_ADDRSTRLEN
];
720 &((struct sockaddr_in
*)sai
->ai_addr
)->sin_addr
,
722 Debug2(LDAP_DEBUG_TRACE
,
723 "ldap_connect_to_host: Trying %s:%s\n",
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
;
733 bind_addr
, sizeof bind_addr
);
734 Debug1( LDAP_DEBUG_TRACE
,
735 "ldap_connect_to_host: From source address %s\n",
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",
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
);
757 ldap_pvt_close_socket(ld
, s
);
762 if (! inet_aton( host
, &in
) ) {
764 rc
= ldap_pvt_gethostbyname_a( host
, &he_buf
, &ha_buf
,
765 &hp
, &local_h_errno
);
767 if ( (rc
< 0) || (hp
== NULL
) ) {
769 ldap_pvt_set_errno( WSAGetLastError() );
771 /* not exactly right, but... */
772 ldap_pvt_set_errno( EHOSTUNREACH
);
774 if (ha_buf
) LDAP_FREE(ha_buf
);
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
];
792 s
= ldap_int_socket( ld
, PF_INET
, socktype
);
793 if ( s
== AC_SOCKET_INVALID
) {
794 /* use_hp ? continue : break; */
798 if ( ldap_int_prepare_socket( ld
, s
, proto
) == -1 ) {
799 ldap_pvt_close_socket(ld
, s
);
803 (void)memset((char *)&sin
, '\0', sizeof sin
);
804 sin
.sin_family
= AF_INET
;
805 sin
.sin_port
= htons((unsigned short) port
);
808 AC_MEMCPY( &sin
.sin_addr
, hp
->h_addr_list
[i
],
809 sizeof(sin
.sin_addr
) );
811 AC_MEMCPY( &sin
.sin_addr
, &in
.s_addr
,
812 sizeof(sin
.sin_addr
) );
815 #ifdef HAVE_INET_NTOA_B
817 inet_ntoa_b( sin
.sin_address
, address
);
819 address
= inet_ntoa( sin
.sin_addr
);
821 Debug2( LDAP_DEBUG_TRACE
,
822 "ldap_connect_to_host: Trying %s:%d\n",
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
);
831 bind_addr
= inet_ntoa( ip4addr
.sin_addr
);
833 Debug1( LDAP_DEBUG_TRACE
,
834 "ldap_connect_to_host: From source address %s\n",
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",
843 if ( bind_success
) {
844 rc
= ldap_pvt_connect(ld
, s
,
845 (struct sockaddr
*)&sin
, sizeof(sin
),
848 if ( (rc
== 0) || (rc
== -2) ) {
849 int err
= ldap_int_connect_cbs( ld
, sb
, &s
, srv
, (struct sockaddr
*)&sin
);
857 ldap_pvt_close_socket(ld
, s
);
861 if (ha_buf
) LDAP_FREE(ha_buf
);
867 #if defined( HAVE_CYRUS_SASL )
869 ldap_host_connected_to( Sockbuf
*sb
, const char *host
)
873 struct sockaddr_storage sabuf
;
875 struct sockaddr sabuf
;
877 struct sockaddr
*sa
= (struct sockaddr
*) &sabuf
;
880 (void)memset( (char *)sa
, '\0', sizeof sabuf
);
883 ber_sockbuf_ctrl( sb
, LBER_SB_OPT_GET_FD
, &sd
);
884 if ( getpeername( sd
, sa
, &len
) == -1 ) {
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
) {
897 return LDAP_STRDUP( ldap_int_hostname
);
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
);
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
);
942 char hbuf
[NI_MAXHOST
];
943 #elif defined( MAXHOSTNAMELEN )
944 char hbuf
[MAXHOSTNAMELEN
];
950 if (ldap_pvt_get_hname( sa
, len
, hbuf
, sizeof(hbuf
), &herr
) == 0
953 return LDAP_STRDUP( hbuf
);
957 return host
? LDAP_STRDUP( host
) : NULL
;
964 /* for UNIX poll(2) */
966 struct pollfd si_fds
[FD_SETSIZE
];
968 /* for UNIX select(2) */
971 fd_set si_use_readfds
;
972 fd_set si_use_writefds
;
977 ldap_mark_select_write( LDAP
*ld
, Sockbuf
*sb
)
979 struct selectinfo
*sip
;
982 sip
= (struct selectinfo
*)ld
->ld_selectinfo
;
984 ber_sockbuf_ctrl( sb
, LBER_SB_OPT_GET_FD
, &sd
);
987 /* for UNIX poll(2) */
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
;
996 if( empty
==-1 && sip
->si_fds
[i
].fd
== -1 ) {
1002 if( sip
->si_maxfd
>= FD_SETSIZE
) {
1006 empty
= sip
->si_maxfd
++;
1009 sip
->si_fds
[empty
].fd
= sd
;
1010 sip
->si_fds
[empty
].events
= POLL_WRITE
;
1013 /* for UNIX select(2) */
1014 if ( !FD_ISSET( sd
, &sip
->si_writefds
)) {
1015 FD_SET( sd
, &sip
->si_writefds
);
1022 ldap_mark_select_read( LDAP
*ld
, Sockbuf
*sb
)
1024 struct selectinfo
*sip
;
1027 sip
= (struct selectinfo
*)ld
->ld_selectinfo
;
1029 ber_sockbuf_ctrl( sb
, LBER_SB_OPT_GET_FD
, &sd
);
1032 /* for UNIX poll(2) */
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
;
1041 if( empty
==-1 && sip
->si_fds
[i
].fd
== -1 ) {
1047 if( sip
->si_maxfd
>= FD_SETSIZE
) {
1051 empty
= sip
->si_maxfd
++;
1054 sip
->si_fds
[empty
].fd
= sd
;
1055 sip
->si_fds
[empty
].events
= POLL_READ
;
1058 /* for UNIX select(2) */
1059 if ( !FD_ISSET( sd
, &sip
->si_readfds
)) {
1060 FD_SET( sd
, &sip
->si_readfds
);
1067 ldap_mark_select_clear( LDAP
*ld
, Sockbuf
*sb
)
1069 struct selectinfo
*sip
;
1072 sip
= (struct selectinfo
*)ld
->ld_selectinfo
;
1074 ber_sockbuf_ctrl( sb
, LBER_SB_OPT_GET_FD
, &sd
);
1077 /* for UNIX poll(2) */
1080 for(i
=0; i
< sip
->si_maxfd
; i
++) {
1081 if( sip
->si_fds
[i
].fd
== sd
) {
1082 sip
->si_fds
[i
].fd
= -1;
1087 /* for UNIX select(2) */
1088 FD_CLR( sd
, &sip
->si_writefds
);
1089 FD_CLR( sd
, &sip
->si_readfds
);
1094 ldap_clear_select_write( LDAP
*ld
, Sockbuf
*sb
)
1096 struct selectinfo
*sip
;
1099 sip
= (struct selectinfo
*)ld
->ld_selectinfo
;
1101 ber_sockbuf_ctrl( sb
, LBER_SB_OPT_GET_FD
, &sd
);
1104 /* for UNIX poll(2) */
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
;
1114 /* for UNIX select(2) */
1115 FD_CLR( sd
, &sip
->si_writefds
);
1121 ldap_is_write_ready( LDAP
*ld
, Sockbuf
*sb
)
1123 struct selectinfo
*sip
;
1126 sip
= (struct selectinfo
*)ld
->ld_selectinfo
;
1128 ber_sockbuf_ctrl( sb
, LBER_SB_OPT_GET_FD
, &sd
);
1131 /* for UNIX poll(2) */
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
;
1143 /* for UNIX select(2) */
1144 return( FD_ISSET( sd
, &sip
->si_use_writefds
));
1150 ldap_is_read_ready( LDAP
*ld
, Sockbuf
*sb
)
1152 struct selectinfo
*sip
;
1155 sip
= (struct selectinfo
*)ld
->ld_selectinfo
;
1157 if (ber_sockbuf_ctrl( sb
, LBER_SB_OPT_DATA_READY
, NULL
))
1160 ber_sockbuf_ctrl( sb
, LBER_SB_OPT_GET_FD
, &sd
);
1163 /* for UNIX poll(2) */
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
;
1175 /* for UNIX select(2) */
1176 return( FD_ISSET( sd
, &sip
->si_use_readfds
));
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
;
1191 /* for UNIX poll(2) */
1192 /* sip->si_maxfd=0 */
1194 /* for UNIX select(2) */
1195 FD_ZERO( &sip
->si_readfds
);
1196 FD_ZERO( &sip
->si_writefds
);
1199 return( (void *)sip
);
1204 ldap_free_select_info( void *sip
)
1211 int ldap_int_tblsize
= 0;
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();
1223 int tblsize
= FD_SETSIZE
;
1224 #endif /* !USE_SYSCONF */
1227 if( tblsize
> FD_SETSIZE
) tblsize
= FD_SETSIZE
;
1228 #endif /* FD_SETSIZE */
1230 ldap_int_tblsize
= tblsize
;
1236 ldap_int_select( LDAP
*ld
, struct timeval
*timeout
)
1239 struct selectinfo
*sip
;
1241 Debug0( LDAP_DEBUG_TRACE
, "ldap_int_select\n" );
1244 if ( ldap_int_tblsize
== 0 ) ldap_int_ip_init();
1247 sip
= (struct selectinfo
*)ld
->ld_selectinfo
;
1248 assert( sip
!= NULL
);
1252 int to
= timeout
? TV2MILLISEC( timeout
) : INFTIM
;
1253 rc
= poll( sip
->si_fds
, sip
->si_maxfd
, to
);
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
,