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-2007 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 */
42 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
44 int ldap_int_inet4or6 = AF_UNSPEC;
46 int ldap_int_inet4or6 = AF_INET;
52 #define osip_debug(ld,fmt,arg1,arg2,arg3) \
54 ldap_log_printf(NULL, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
59 #define osip_debug(ld,fmt,arg1,arg2,arg3) ((void)0)
61 #endif /* LDAP_DEBUG */
64 ldap_pvt_set_errno(int err)
70 ldap_int_timeval_dup( struct timeval **dest, const struct timeval *src )
74 assert( dest != NULL );
81 new = (struct timeval *) LDAP_MALLOC(sizeof(struct timeval));
88 AC_MEMCPY( (char *) new, (const char *) src, sizeof(struct timeval));
95 ldap_pvt_ndelay_on(LDAP *ld, int fd)
97 osip_debug(ld, "ldap_ndelay_on: %d\n",fd,0,0);
98 return ber_pvt_socket_set_nonblock( fd, 1 );
102 ldap_pvt_ndelay_off(LDAP *ld, int fd)
104 osip_debug(ld, "ldap_ndelay_off: %d\n",fd,0,0);
105 return ber_pvt_socket_set_nonblock( fd, 0 );
109 ldap_int_socket(LDAP *ld, int family, int type )
111 ber_socket_t s = socket(family, type, 0);
112 osip_debug(ld, "ldap_new_socket: %d\n",s,0,0);
117 ldap_pvt_close_socket(LDAP *ld, int s)
119 osip_debug(ld, "ldap_close_socket: %d\n",s,0,0);
124 ldap_int_prepare_socket(LDAP *ld, int s, int proto )
126 osip_debug( ld, "ldap_prepare_socket: %d\n", s, 0, 0 );
128 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
129 if ( proto == LDAP_PROTO_TCP ) {
132 if ( setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
133 (char*) &dummy, sizeof(dummy) ) == AC_SOCKET_ERROR )
135 osip_debug( ld, "ldap_prepare_socket: "
136 "setsockopt(%d, SO_KEEPALIVE) failed (ignored).\n",
139 #endif /* SO_KEEPALIVE */
141 if ( setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
142 (char*) &dummy, sizeof(dummy) ) == AC_SOCKET_ERROR )
144 osip_debug( ld, "ldap_prepare_socket: "
145 "setsockopt(%d, TCP_NODELAY) failed (ignored).\n",
148 #endif /* TCP_NODELAY */
150 #endif /* SO_KEEPALIVE || TCP_NODELAY */
160 "ldap_is_socket_ready: error on socket %d: errno: %d (%s)\n", \
163 sock_errstr(errno) ); \
167 * check the socket for errors after select returned.
170 ldap_pvt_is_socket_ready(LDAP *ld, int s)
172 osip_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
174 #if defined( notyet ) /* && defined( SO_ERROR ) */
177 socklen_t dummy = sizeof(so_errno);
178 if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
184 ldap_pvt_set_errno(so_errno);
194 struct sockaddr_storage sin;
196 struct sockaddr_in sin;
199 socklen_t dummy = sizeof(sin);
200 if ( getpeername( s, (struct sockaddr *) &sin, &dummy )
203 /* XXX: needs to be replace with ber_stream_read() */
215 #endif /* HAVE_WINSOCK */
218 ldap_pvt_connect(LDAP *ld, ber_socket_t s,
219 struct sockaddr *sin, socklen_t addrlen,
223 struct timeval tv = { 0 },
226 #ifdef LDAP_CONNECTIONLESS
227 /* We could do a connect() but that would interfere with
228 * attempts to poll a broadcast address
230 if (LDAP_IS_UDP(ld)) {
231 if (ld->ld_options.ldo_peer)
232 ldap_memfree(ld->ld_options.ldo_peer);
233 ld->ld_options.ldo_peer=ldap_memalloc(sizeof(struct sockaddr));
234 AC_MEMCPY(ld->ld_options.ldo_peer,sin,sizeof(struct sockaddr));
238 opt_tv = ld->ld_options.ldo_tm_net;
239 if ( opt_tv != NULL ) {
243 osip_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
244 s, opt_tv ? tv.tv_sec : -1L, async);
246 if ( opt_tv && ldap_pvt_ndelay_on(ld, s) == -1 )
249 if ( connect(s, sin, addrlen) != AC_SOCKET_ERROR ) {
250 if ( opt_tv && ldap_pvt_ndelay_off(ld, s) == -1 )
256 if ( err != EINPROGRESS && err != EWOULDBLOCK ) {
261 if ( async ) return ( -2 );
267 int timeout = INFTIM;
269 if( opt_tv != NULL ) timeout = TV2MILLISEC( &tv );
272 fd.events = POLL_WRITE;
276 rc = poll( &fd, 1, timeout );
277 } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
278 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
280 if( rc == AC_SOCKET_ERROR ) return rc;
282 if( fd.revents & POLL_WRITE ) {
283 if ( ldap_pvt_is_socket_ready(ld, s) == -1 ) return -1;
284 if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
290 fd_set wfds, *z=NULL;
295 #if defined( FD_SETSIZE ) && !defined( HAVE_WINSOCK )
296 if ( s >= FD_SETSIZE ) {
297 rc = AC_SOCKET_ERROR;
299 ldap_pvt_set_errno( EMFILE );
313 rc = select(ldap_int_tblsize, z, &wfds,
319 opt_tv ? &tv : NULL);
320 } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
321 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
323 if( rc == AC_SOCKET_ERROR ) return rc;
326 /* This means the connection failed */
327 if ( FD_ISSET(s, &efds) ) {
329 int dummy = sizeof(so_errno);
330 if ( getsockopt( s, SOL_SOCKET, SO_ERROR,
331 (char *) &so_errno, &dummy ) == AC_SOCKET_ERROR || !so_errno )
334 so_errno = WSAGetLastError();
336 ldap_pvt_set_errno(so_errno);
337 osip_debug(ld, "ldap_pvt_connect: error on socket %d: "
338 "errno: %d (%s)\n", s, errno, sock_errstr(errno));
342 if ( FD_ISSET(s, &wfds) ) {
344 if ( ldap_pvt_is_socket_ready(ld, s) == -1 ) return -1;
346 if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
352 osip_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
353 ldap_pvt_set_errno( ETIMEDOUT );
357 #ifndef HAVE_INET_ATON
359 ldap_pvt_inet_aton( const char *host, struct in_addr *in)
361 unsigned long u = inet_addr( host );
364 if ( u == INADDR_NONE ) return 0;
366 if ( u == 0xffffffffUL || u == (unsigned long) -1L ) return 0;
375 ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
377 const char *host, int port,
382 ber_socket_t s = AC_SOCKET_INVALID;
384 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
387 struct addrinfo hints, *res, *sai;
391 struct hostent *hp = NULL;
392 struct hostent he_buf;
397 if( host == NULL ) host = "localhost";
400 case LDAP_PROTO_TCP: socktype = SOCK_STREAM;
402 "ldap_connect_to_host: TCP %s:%d\n",
405 case LDAP_PROTO_UDP: socktype = SOCK_DGRAM;
407 "ldap_connect_to_host: UDP %s:%d\n",
411 osip_debug( ld, "ldap_connect_to_host: unknown proto: %d\n",
416 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
417 memset( &hints, '\0', sizeof(hints) );
418 #ifdef USE_AI_ATTRCONFIG /* FIXME: configure test needed */
419 /* Use AI_ATTRCONFIG only on systems where its known to be needed. */
420 hints.ai_flags = AI_ATTRCONFIG;
422 hints.ai_family = ldap_int_inet4or6;
423 hints.ai_socktype = socktype;
424 snprintf(serv, sizeof serv, "%d", port );
426 #ifdef LDAP_R_COMPILE
427 /* most getaddrinfo(3) use non-threadsafe resolver libraries */
428 ldap_pvt_thread_mutex_lock(&ldap_int_resolv_mutex);
431 err = getaddrinfo( host, serv, &hints, &res );
433 #ifdef LDAP_R_COMPILE
434 ldap_pvt_thread_mutex_unlock(&ldap_int_resolv_mutex);
438 osip_debug(ld, "ldap_connect_to_host: getaddrinfo failed: %s\n",
439 AC_GAI_STRERROR(err), 0, 0);
444 for( sai=res; sai != NULL; sai=sai->ai_next) {
445 if( sai->ai_addr == NULL ) {
446 osip_debug(ld, "ldap_connect_to_host: getaddrinfo "
447 "ai_addr is NULL?\n", 0, 0, 0);
451 /* we assume AF_x and PF_x are equal for all x */
452 s = ldap_int_socket( ld, sai->ai_family, socktype );
453 if ( s == AC_SOCKET_INVALID ) {
457 if ( ldap_int_prepare_socket(ld, s, proto ) == -1 ) {
458 ldap_pvt_close_socket(ld, s);
462 switch (sai->ai_family) {
465 char addr[INET6_ADDRSTRLEN];
467 &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
469 osip_debug(ld, "ldap_connect_to_host: Trying %s %s\n",
474 char addr[INET_ADDRSTRLEN];
476 &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
478 osip_debug(ld, "ldap_connect_to_host: Trying %s:%s\n",
483 rc = ldap_pvt_connect( ld, s,
484 sai->ai_addr, sai->ai_addrlen, async );
485 if ( (rc == 0) || (rc == -2) ) {
486 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
489 ldap_pvt_close_socket(ld, s);
494 if (! inet_aton( host, &in ) ) {
496 rc = ldap_pvt_gethostbyname_a( host, &he_buf, &ha_buf,
497 &hp, &local_h_errno );
499 if ( (rc < 0) || (hp == NULL) ) {
501 ldap_pvt_set_errno( WSAGetLastError() );
503 /* not exactly right, but... */
504 ldap_pvt_set_errno( EHOSTUNREACH );
506 if (ha_buf) LDAP_FREE(ha_buf);
514 for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
515 struct sockaddr_in sin;
517 s = ldap_int_socket( ld, PF_INET, socktype );
518 if ( s == AC_SOCKET_INVALID ) {
519 /* use_hp ? continue : break; */
523 if ( ldap_int_prepare_socket( ld, s, proto ) == -1 ) {
524 ldap_pvt_close_socket(ld, s);
528 (void)memset((char *)&sin, '\0', sizeof sin);
529 sin.sin_family = AF_INET;
530 sin.sin_port = htons((short) port);
533 AC_MEMCPY( &sin.sin_addr, hp->h_addr_list[i],
534 sizeof(sin.sin_addr) );
536 AC_MEMCPY( &sin.sin_addr, &in.s_addr,
537 sizeof(sin.sin_addr) );
540 #ifdef HAVE_INET_NTOA_B
543 char address[INET_ADDR_LEN];
544 inet_ntoa_b(sin.sin_address, address);
545 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n",
549 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n",
550 inet_ntoa(sin.sin_addr), port, 0);
553 rc = ldap_pvt_connect(ld, s,
554 (struct sockaddr *)&sin, sizeof(sin),
557 if ( (rc == 0) || (rc == -2) ) {
558 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
562 ldap_pvt_close_socket(ld, s);
566 if (ha_buf) LDAP_FREE(ha_buf);
572 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND ) || \
573 defined( HAVE_CYRUS_SASL )
575 ldap_host_connected_to( Sockbuf *sb, const char *host )
579 struct sockaddr_storage sabuf;
581 struct sockaddr sabuf;
583 struct sockaddr *sa = (struct sockaddr *) &sabuf;
586 (void)memset( (char *)sa, '\0', sizeof sabuf );
589 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
590 if ( getpeername( sd, sa, &len ) == -1 ) {
595 * do a reverse lookup on the addr to get the official hostname.
596 * this is necessary for kerberos to work right, since the official
597 * hostname is used as the kerberos instance.
600 switch (sa->sa_family) {
603 return LDAP_STRDUP( ldap_int_hostname );
608 struct in6_addr localhost = IN6ADDR_LOOPBACK_INIT;
609 if( memcmp ( &((struct sockaddr_in6 *)sa)->sin6_addr,
610 &localhost, sizeof(localhost)) == 0 )
612 return LDAP_STRDUP( ldap_int_hostname );
619 struct in_addr localhost;
620 localhost.s_addr = htonl( INADDR_ANY );
622 if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
623 &localhost, sizeof(localhost) ) == 0 )
625 return LDAP_STRDUP( ldap_int_hostname );
628 #ifdef INADDR_LOOPBACK
629 localhost.s_addr = htonl( INADDR_LOOPBACK );
631 if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
632 &localhost, sizeof(localhost) ) == 0 )
634 return LDAP_STRDUP( ldap_int_hostname );
648 char hbuf[NI_MAXHOST];
649 #elif defined( MAXHOSTNAMELEN
650 char hbuf[MAXHOSTNAMELEN];
656 if (ldap_pvt_get_hname( sa, len, hbuf, sizeof(hbuf), &herr ) == 0
659 return LDAP_STRDUP( hbuf );
663 return host ? LDAP_STRDUP( host ) : NULL;
670 /* for UNIX poll(2) */
672 struct pollfd si_fds[FD_SETSIZE];
674 /* for UNIX select(2) */
677 fd_set si_use_readfds;
678 fd_set si_use_writefds;
683 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
685 struct selectinfo *sip;
688 sip = (struct selectinfo *)ld->ld_selectinfo;
690 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
693 /* for UNIX poll(2) */
697 for(i=0; i < sip->si_maxfd; i++) {
698 if( sip->si_fds[i].fd == sd ) {
699 sip->si_fds[i].events |= POLL_WRITE;
702 if( empty==-1 && sip->si_fds[i].fd == -1 ) {
708 if( sip->si_maxfd >= FD_SETSIZE ) {
712 empty = sip->si_maxfd++;
715 sip->si_fds[empty].fd = sd;
716 sip->si_fds[empty].events = POLL_WRITE;
719 /* for UNIX select(2) */
720 if ( !FD_ISSET( sd, &sip->si_writefds )) {
721 FD_SET( sd, &sip->si_writefds );
728 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
730 struct selectinfo *sip;
733 sip = (struct selectinfo *)ld->ld_selectinfo;
735 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
738 /* for UNIX poll(2) */
742 for(i=0; i < sip->si_maxfd; i++) {
743 if( sip->si_fds[i].fd == sd ) {
744 sip->si_fds[i].events |= POLL_READ;
747 if( empty==-1 && sip->si_fds[i].fd == -1 ) {
753 if( sip->si_maxfd >= FD_SETSIZE ) {
757 empty = sip->si_maxfd++;
760 sip->si_fds[empty].fd = sd;
761 sip->si_fds[empty].events = POLL_READ;
764 /* for UNIX select(2) */
765 if ( !FD_ISSET( sd, &sip->si_readfds )) {
766 FD_SET( sd, &sip->si_readfds );
773 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
775 struct selectinfo *sip;
778 sip = (struct selectinfo *)ld->ld_selectinfo;
780 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
783 /* for UNIX poll(2) */
786 for(i=0; i < sip->si_maxfd; i++) {
787 if( sip->si_fds[i].fd == sd ) {
788 sip->si_fds[i].fd = -1;
793 /* for UNIX select(2) */
794 FD_CLR( sd, &sip->si_writefds );
795 FD_CLR( sd, &sip->si_readfds );
801 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
803 struct selectinfo *sip;
806 sip = (struct selectinfo *)ld->ld_selectinfo;
808 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
811 /* for UNIX poll(2) */
814 for(i=0; i < sip->si_maxfd; i++) {
815 if( sip->si_fds[i].fd == sd ) {
816 return sip->si_fds[i].revents & POLL_WRITE;
823 /* for UNIX select(2) */
824 return( FD_ISSET( sd, &sip->si_use_writefds ));
830 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
832 struct selectinfo *sip;
835 sip = (struct selectinfo *)ld->ld_selectinfo;
837 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
840 /* for UNIX poll(2) */
843 for(i=0; i < sip->si_maxfd; i++) {
844 if( sip->si_fds[i].fd == sd ) {
845 return sip->si_fds[i].revents & POLL_READ;
852 /* for UNIX select(2) */
853 return( FD_ISSET( sd, &sip->si_use_readfds ));
859 ldap_new_select_info( void )
861 struct selectinfo *sip;
863 sip = (struct selectinfo *)LDAP_CALLOC( 1, sizeof( struct selectinfo ));
865 if ( sip == NULL ) return NULL;
868 /* for UNIX poll(2) */
869 /* sip->si_maxfd=0 */
871 /* for UNIX select(2) */
872 FD_ZERO( &sip->si_readfds );
873 FD_ZERO( &sip->si_writefds );
876 return( (void *)sip );
881 ldap_free_select_info( void *sip )
888 int ldap_int_tblsize = 0;
891 ldap_int_ip_init( void )
893 #if defined( HAVE_SYSCONF )
894 long tblsize = sysconf( _SC_OPEN_MAX );
895 if( tblsize > INT_MAX ) tblsize = INT_MAX;
897 #elif defined( HAVE_GETDTABLESIZE )
898 int tblsize = getdtablesize();
900 int tblsize = FD_SETSIZE;
901 #endif /* !USE_SYSCONF */
904 if( tblsize > FD_SETSIZE ) tblsize = FD_SETSIZE;
905 #endif /* FD_SETSIZE */
907 ldap_int_tblsize = tblsize;
913 ldap_int_select( LDAP *ld, struct timeval *timeout )
916 struct selectinfo *sip;
918 Debug( LDAP_DEBUG_TRACE, "ldap_int_select\n", 0, 0, 0 );
921 if ( ldap_int_tblsize == 0 ) ldap_int_ip_init();
924 sip = (struct selectinfo *)ld->ld_selectinfo;
925 assert( sip != NULL );
929 int to = timeout ? TV2MILLISEC( timeout ) : INFTIM;
930 rc = poll( sip->si_fds, sip->si_maxfd, to );
933 sip->si_use_readfds = sip->si_readfds;
934 sip->si_use_writefds = sip->si_writefds;
936 rc = select( ldap_int_tblsize,
937 &sip->si_use_readfds, &sip->si_use_writefds,