3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1995 Regents of the University of Michigan.
10 * os-ip.c -- platform-specific TCP & UDP related code
17 #include <ac/stdlib.h>
20 #include <ac/socket.h>
21 #include <ac/string.h>
23 #include <ac/unistd.h>
27 #endif /* HAVE_IO_H */
31 int ldap_int_tblsize = 0;
33 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
35 int ldap_int_inet4or6 = AF_UNSPEC;
37 int ldap_int_inet4or6 = AF_INET;
42 * nonblock connect code
43 * written by Lars Uffmann, <lars.uffmann@mediaway.net>.
45 * Copyright 1999, Lars Uffmann, All rights reserved.
46 * This software is not subject to any license of my employer
49 * OpenLDAP COPYING RESTRICTIONS APPLY, see COPYRIGHT file
51 * Read about the rationale in ldap_connect_timeout:
52 * ftp://koobera.math.uic.edu/www/docs/connect.html.
57 #define osip_debug(ld,fmt,arg1,arg2,arg3) \
59 ldap_log_printf(NULL, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
64 #define osip_debug(ld,fmt,arg1,arg2,arg3) ((void)0)
66 #endif /* LDAP_DEBUG */
69 ldap_pvt_set_errno(int err)
75 ldap_int_timeval_dup( struct timeval **dest, const struct timeval *src )
79 assert( dest != NULL );
86 new = (struct timeval *) LDAP_MALLOC(sizeof(struct timeval));
93 AC_MEMCPY( (char *) new, (const char *) src, sizeof(struct timeval));
100 ldap_pvt_ndelay_on(LDAP *ld, int fd)
102 osip_debug(ld, "ldap_ndelay_on: %d\n",fd,0,0);
103 return ber_pvt_socket_set_nonblock( fd, 1 );
107 ldap_pvt_ndelay_off(LDAP *ld, int fd)
109 osip_debug(ld, "ldap_ndelay_off: %d\n",fd,0,0);
110 return ber_pvt_socket_set_nonblock( fd, 0 );
114 ldap_int_socket(LDAP *ld, int family, int type )
116 ber_socket_t s = socket(family, type, 0);
117 osip_debug(ld, "ldap_new_socket: %d\n",s,0,0);
122 ldap_pvt_close_socket(LDAP *ld, int s)
124 osip_debug(ld, "ldap_close_socket: %d\n",s,0,0);
129 ldap_int_prepare_socket(LDAP *ld, int s, int proto )
131 osip_debug(ld, "ldap_prepare_socket: %d\n", s,0,0);
134 if( proto == LDAP_PROTO_TCP ) {
136 if ( setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
137 (char*) &dummy, sizeof(dummy) ) == AC_SOCKET_ERROR )
139 osip_debug(ld, "ldap_prepare_socket: "
140 "setsockopt(%d, TCP_NODELAY) failed (ignored).\n",
154 "ldap_is_socket_ready: error on socket %d: errno: %d (%s)\n", \
157 sock_errstr(errno) ); \
161 * check the socket for errors after select returned.
164 ldap_pvt_is_socket_ready(LDAP *ld, int s)
166 osip_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
168 #if defined( notyet ) /* && defined( SO_ERROR ) */
171 socklen_t dummy = sizeof(so_errno);
172 if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
178 ldap_pvt_set_errno(so_errno);
188 struct sockaddr_storage sin;
190 struct sockaddr_in sin;
193 socklen_t dummy = sizeof(sin);
194 if ( getpeername( s, (struct sockaddr *) &sin, &dummy )
197 /* XXX: needs to be replace with ber_stream_read() */
209 #endif /* HAVE_WINSOCK */
212 ldap_pvt_connect(LDAP *ld, ber_socket_t s,
213 struct sockaddr *sin, socklen_t addrlen,
217 struct timeval tv, *opt_tv=NULL;
218 fd_set wfds, *z=NULL;
223 #ifdef LDAP_CONNECTIONLESS
224 /* We could do a connect() but that would interfere with
225 * attempts to poll a broadcast address
227 if (LDAP_IS_UDP(ld)) {
228 if (ld->ld_options.ldo_peer)
229 ldap_memfree(ld->ld_options.ldo_peer);
230 ld->ld_options.ldo_peer=ldap_memalloc(sizeof(struct sockaddr));
231 AC_MEMCPY(ld->ld_options.ldo_peer,sin,sizeof(struct sockaddr));
235 if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
236 tv.tv_usec = opt_tv->tv_usec;
237 tv.tv_sec = opt_tv->tv_sec;
240 osip_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
241 s, opt_tv ? tv.tv_sec : -1L, async);
243 if ( ldap_pvt_ndelay_on(ld, s) == -1 )
246 if ( connect(s, sin, addrlen) != AC_SOCKET_ERROR ) {
247 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
253 ldap_pvt_set_errno( WSAGetLastError() );
256 if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
261 if ( async ) return ( -2 );
273 rc = select(ldap_int_tblsize, z, &wfds,
279 opt_tv ? &tv : NULL);
280 } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
281 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
283 if( rc == AC_SOCKET_ERROR ) return rc;
286 /* This means the connection failed */
287 if ( FD_ISSET(s, &efds) ) {
289 int dummy = sizeof(so_errno);
290 if ( getsockopt( s, SOL_SOCKET, SO_ERROR,
291 (char *) &so_errno, &dummy ) == AC_SOCKET_ERROR || !so_errno )
294 so_errno = WSAGetLastError();
296 ldap_pvt_set_errno(so_errno);
297 osip_debug(ld, "ldap_pvt_connect: error on socket %d: "
298 "errno: %d (%s)\n", s, errno, sock_errstr(errno));
302 if ( FD_ISSET(s, &wfds) ) {
304 if ( ldap_pvt_is_socket_ready(ld, s) == -1 )
307 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
311 osip_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
312 ldap_pvt_set_errno( ETIMEDOUT );
316 #ifndef HAVE_INET_ATON
318 ldap_pvt_inet_aton( const char *host, struct in_addr *in)
320 unsigned long u = inet_addr( host );
321 if ( u != 0xffffffff || u != (unsigned long) -1 ) {
331 ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
333 const char *host, int port,
338 ber_socket_t s = AC_SOCKET_INVALID;
340 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
343 struct addrinfo hints, *res, *sai;
347 struct hostent *hp = NULL;
348 struct hostent he_buf;
353 if( host == NULL ) host = "localhost";
356 case LDAP_PROTO_TCP: socktype = SOCK_STREAM;
358 "ldap_connect_to_host: TCP %s:%d\n",
361 case LDAP_PROTO_UDP: socktype = SOCK_DGRAM;
363 "ldap_connect_to_host: UDP %s:%d\n",
367 osip_debug( ld, "ldap_connect_to_host: unknown proto: %d\n",
372 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
373 memset( &hints, '\0', sizeof(hints) );
374 hints.ai_family = ldap_int_inet4or6;
375 hints.ai_socktype = socktype;
376 snprintf(serv, sizeof serv, "%d", port );
378 #ifdef LDAP_R_COMPILE
379 /* most getaddrinfo(3) use non-threadsafe resolver libraries */
380 ldap_pvt_thread_mutex_lock(&ldap_int_resolv_mutex);
383 err = getaddrinfo( host, serv, &hints, &res );
385 #ifdef LDAP_R_COMPILE
386 ldap_pvt_thread_mutex_unlock(&ldap_int_resolv_mutex);
390 osip_debug(ld, "ldap_connect_to_host: getaddrinfo failed: %s\n",
391 AC_GAI_STRERROR(err), 0, 0);
396 for( sai=res; sai != NULL; sai=sai->ai_next) {
397 if( sai->ai_addr == NULL ) {
398 osip_debug(ld, "ldap_connect_to_host: getaddrinfo "
399 "ai_addr is NULL?\n", 0, 0, 0);
403 /* we assume AF_x and PF_x are equal for all x */
404 s = ldap_int_socket( ld, sai->ai_family, socktype );
405 if ( s == AC_SOCKET_INVALID ) {
409 if ( ldap_int_prepare_socket(ld, s, proto ) == -1 ) {
410 ldap_pvt_close_socket(ld, s);
414 switch (sai->ai_family) {
417 char addr[INET6_ADDRSTRLEN];
419 &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
421 osip_debug(ld, "ldap_connect_to_host: Trying %s %s\n",
426 char addr[INET_ADDRSTRLEN];
428 &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
430 osip_debug(ld, "ldap_connect_to_host: Trying %s:%s\n",
435 rc = ldap_pvt_connect( ld, s,
436 sai->ai_addr, sai->ai_addrlen, async );
437 if ( (rc == 0) || (rc == -2) ) {
438 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
441 ldap_pvt_close_socket(ld, s);
446 if (! inet_aton( host, &in ) ) {
448 rc = ldap_pvt_gethostbyname_a( host, &he_buf, &ha_buf,
449 &hp, &local_h_errno );
451 if ( (rc < 0) || (hp == NULL) ) {
453 ldap_pvt_set_errno( WSAGetLastError() );
455 /* not exactly right, but... */
456 ldap_pvt_set_errno( EHOSTUNREACH );
458 if (ha_buf) LDAP_FREE(ha_buf);
466 for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
467 struct sockaddr_in sin;
469 s = ldap_int_socket( ld, PF_INET, socktype );
470 if ( s == AC_SOCKET_INVALID ) {
471 /* use_hp ? continue : break; */
475 if ( ldap_int_prepare_socket( ld, s, proto ) == -1 ) {
476 ldap_pvt_close_socket(ld, s);
480 (void)memset((char *)&sin, '\0', sizeof sin);
481 sin.sin_family = AF_INET;
482 sin.sin_port = htons((short) port);
485 AC_MEMCPY( &sin.sin_addr, hp->h_addr_list[i],
486 sizeof(sin.sin_addr) );
488 AC_MEMCPY( &sin.sin_addr, &in.s_addr,
489 sizeof(sin.sin_addr) );
492 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n",
493 inet_ntoa(sin.sin_addr), port, 0);
495 rc = ldap_pvt_connect(ld, s,
496 (struct sockaddr *)&sin, sizeof(sin),
499 if ( (rc == 0) || (rc == -2) ) {
500 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
504 ldap_pvt_close_socket(ld, s);
508 if (ha_buf) LDAP_FREE(ha_buf);
514 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND ) || \
515 defined( HAVE_CYRUS_SASL )
517 ldap_host_connected_to( Sockbuf *sb, const char *host )
521 struct sockaddr_storage sabuf;
523 struct sockaddr sabuf;
525 struct sockaddr *sa = (struct sockaddr *) &sabuf;
529 (void)memset( (char *)sa, '\0', sizeof sabuf );
532 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
533 if ( getpeername( sd, sa, &len ) == -1 ) {
538 * do a reverse lookup on the addr to get the official hostname.
539 * this is necessary for kerberos to work right, since the official
540 * hostname is used as the kerberos instance.
543 switch (sa->sa_family) {
546 return LDAP_STRDUP( ldap_int_hostname );
551 struct in6_addr localhost = IN6ADDR_LOOPBACK_INIT;
552 if( memcmp ( &((struct sockaddr_in6 *)sa)->sin6_addr,
553 &localhost, sizeof(localhost)) == 0 )
555 return LDAP_STRDUP( ldap_int_hostname );
562 struct in_addr localhost;
563 localhost.s_addr = htonl( INADDR_ANY );
565 if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
566 &localhost, sizeof(localhost) ) == 0 )
568 return LDAP_STRDUP( ldap_int_hostname );
571 #ifdef INADDR_LOOPBACK
572 localhost.s_addr = htonl( INADDR_LOOPBACK );
574 if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
575 &localhost, sizeof(localhost) ) == 0 )
577 return LDAP_STRDUP( ldap_int_hostname );
591 char hbuf[NI_MAXHOST];
594 if (ldap_pvt_get_hname( sa, len, hbuf, sizeof(hbuf), &herr ) == 0
597 return LDAP_STRDUP( hbuf );
602 return host ? LDAP_STRDUP( host ) : NULL;
611 fd_set si_use_readfds;
612 fd_set si_use_writefds;
617 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
619 struct selectinfo *sip;
622 sip = (struct selectinfo *)ld->ld_selectinfo;
624 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
625 if ( !FD_ISSET( sd, &sip->si_writefds )) {
626 FD_SET( sd, &sip->si_writefds );
632 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
634 struct selectinfo *sip;
637 sip = (struct selectinfo *)ld->ld_selectinfo;
639 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
640 if ( !FD_ISSET( sd, &sip->si_readfds )) {
641 FD_SET( sd, &sip->si_readfds );
647 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
649 struct selectinfo *sip;
652 sip = (struct selectinfo *)ld->ld_selectinfo;
654 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
655 FD_CLR( sd, &sip->si_writefds );
656 FD_CLR( sd, &sip->si_readfds );
661 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
663 struct selectinfo *sip;
666 sip = (struct selectinfo *)ld->ld_selectinfo;
668 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
669 return( FD_ISSET( sd, &sip->si_use_writefds ));
674 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
676 struct selectinfo *sip;
679 sip = (struct selectinfo *)ld->ld_selectinfo;
681 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
682 return( FD_ISSET( sd, &sip->si_use_readfds ));
687 ldap_new_select_info( void )
689 struct selectinfo *sip;
691 if (( sip = (struct selectinfo *)LDAP_CALLOC( 1,
692 sizeof( struct selectinfo ))) != NULL ) {
693 FD_ZERO( &sip->si_readfds );
694 FD_ZERO( &sip->si_writefds );
697 return( (void *)sip );
702 ldap_free_select_info( void *sip )
709 ldap_int_ip_init( void )
712 #if defined( HAVE_SYSCONF )
713 tblsize = sysconf( _SC_OPEN_MAX );
714 #elif defined( HAVE_GETDTABLESIZE )
715 tblsize = getdtablesize();
717 tblsize = FD_SETSIZE;
718 #endif /* !USE_SYSCONF */
721 if( tblsize > FD_SETSIZE )
722 tblsize = FD_SETSIZE;
723 #endif /* FD_SETSIZE*/
724 ldap_int_tblsize = tblsize;
729 ldap_int_select( LDAP *ld, struct timeval *timeout )
731 struct selectinfo *sip;
734 LDAP_LOG ( CONNECTION, ENTRY, "ldap_int_select\n", 0, 0, 0 );
736 Debug( LDAP_DEBUG_TRACE, "ldap_int_select\n", 0, 0, 0 );
739 if ( ldap_int_tblsize == 0 )
742 sip = (struct selectinfo *)ld->ld_selectinfo;
743 sip->si_use_readfds = sip->si_readfds;
744 sip->si_use_writefds = sip->si_writefds;
746 return( select( ldap_int_tblsize,
747 &sip->si_use_readfds, &sip->si_use_writefds,