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-2006 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);
129 if( proto == LDAP_PROTO_TCP ) {
131 if ( setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
132 (char*) &dummy, sizeof(dummy) ) == AC_SOCKET_ERROR )
134 osip_debug(ld, "ldap_prepare_socket: "
135 "setsockopt(%d, TCP_NODELAY) failed (ignored).\n",
149 "ldap_is_socket_ready: error on socket %d: errno: %d (%s)\n", \
152 sock_errstr(errno) ); \
156 * check the socket for errors after select returned.
159 ldap_pvt_is_socket_ready(LDAP *ld, int s)
161 osip_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
163 #if defined( notyet ) /* && defined( SO_ERROR ) */
166 socklen_t dummy = sizeof(so_errno);
167 if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
173 ldap_pvt_set_errno(so_errno);
183 struct sockaddr_storage sin;
185 struct sockaddr_in sin;
188 socklen_t dummy = sizeof(sin);
189 if ( getpeername( s, (struct sockaddr *) &sin, &dummy )
192 /* XXX: needs to be replace with ber_stream_read() */
204 #endif /* HAVE_WINSOCK */
207 ldap_pvt_connect(LDAP *ld, ber_socket_t s,
208 struct sockaddr *sin, socklen_t addrlen,
212 struct timeval tv = { 0 },
215 #ifdef LDAP_CONNECTIONLESS
216 /* We could do a connect() but that would interfere with
217 * attempts to poll a broadcast address
219 if (LDAP_IS_UDP(ld)) {
220 if (ld->ld_options.ldo_peer)
221 ldap_memfree(ld->ld_options.ldo_peer);
222 ld->ld_options.ldo_peer=ldap_memalloc(sizeof(struct sockaddr));
223 AC_MEMCPY(ld->ld_options.ldo_peer,sin,sizeof(struct sockaddr));
227 opt_tv = ld->ld_options.ldo_tm_net;
228 if ( opt_tv != NULL ) {
232 osip_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
233 s, opt_tv ? tv.tv_sec : -1L, async);
235 if ( opt_tv && ldap_pvt_ndelay_on(ld, s) == -1 )
238 if ( connect(s, sin, addrlen) != AC_SOCKET_ERROR ) {
239 if ( opt_tv && ldap_pvt_ndelay_off(ld, s) == -1 )
245 if ( err != EINPROGRESS && err != EWOULDBLOCK ) {
250 if ( async ) return ( -2 );
256 int timeout = INFTIM;
258 if( opt_tv != NULL ) timeout = TV2MILLISEC( &tv );
261 fd.events = POLL_WRITE;
265 rc = poll( &fd, 1, timeout );
266 } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
267 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
269 if( rc == AC_SOCKET_ERROR ) return rc;
271 if( fd.revents & POLL_WRITE ) {
272 if ( ldap_pvt_is_socket_ready(ld, s) == -1 ) return -1;
273 if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
279 fd_set wfds, *z=NULL;
284 #if defined( FD_SETSIZE ) && !defined( HAVE_WINSOCK )
285 if ( s >= FD_SETSIZE ) {
286 rc = AC_SOCKET_ERROR;
288 ldap_pvt_set_errno( EMFILE );
302 rc = select(ldap_int_tblsize, z, &wfds,
308 opt_tv ? &tv : NULL);
309 } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
310 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
312 if( rc == AC_SOCKET_ERROR ) return rc;
315 /* This means the connection failed */
316 if ( FD_ISSET(s, &efds) ) {
318 int dummy = sizeof(so_errno);
319 if ( getsockopt( s, SOL_SOCKET, SO_ERROR,
320 (char *) &so_errno, &dummy ) == AC_SOCKET_ERROR || !so_errno )
323 so_errno = WSAGetLastError();
325 ldap_pvt_set_errno(so_errno);
326 osip_debug(ld, "ldap_pvt_connect: error on socket %d: "
327 "errno: %d (%s)\n", s, errno, sock_errstr(errno));
331 if ( FD_ISSET(s, &wfds) ) {
333 if ( ldap_pvt_is_socket_ready(ld, s) == -1 ) return -1;
335 if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
341 osip_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
342 ldap_pvt_set_errno( ETIMEDOUT );
346 #ifndef HAVE_INET_ATON
348 ldap_pvt_inet_aton( const char *host, struct in_addr *in)
350 unsigned long u = inet_addr( host );
353 if ( u == INADDR_NONE ) return 0;
355 if ( u == 0xffffffffUL || u == (unsigned long) -1L ) return 0;
364 ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
366 const char *host, int port,
371 ber_socket_t s = AC_SOCKET_INVALID;
373 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
376 struct addrinfo hints, *res, *sai;
380 struct hostent *hp = NULL;
381 struct hostent he_buf;
386 if( host == NULL ) host = "localhost";
389 case LDAP_PROTO_TCP: socktype = SOCK_STREAM;
391 "ldap_connect_to_host: TCP %s:%d\n",
394 case LDAP_PROTO_UDP: socktype = SOCK_DGRAM;
396 "ldap_connect_to_host: UDP %s:%d\n",
400 osip_debug( ld, "ldap_connect_to_host: unknown proto: %d\n",
405 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
406 memset( &hints, '\0', sizeof(hints) );
407 #ifdef USE_AI_ATTRCONFIG /* FIXME: configure test needed */
408 /* Use AI_ATTRCONFIG only on systems where its known to be needed. */
409 hints.ai_flags = AI_ATTRCONFIG;
411 hints.ai_family = ldap_int_inet4or6;
412 hints.ai_socktype = socktype;
413 snprintf(serv, sizeof serv, "%d", port );
415 #ifdef LDAP_R_COMPILE
416 /* most getaddrinfo(3) use non-threadsafe resolver libraries */
417 ldap_pvt_thread_mutex_lock(&ldap_int_resolv_mutex);
420 err = getaddrinfo( host, serv, &hints, &res );
422 #ifdef LDAP_R_COMPILE
423 ldap_pvt_thread_mutex_unlock(&ldap_int_resolv_mutex);
427 osip_debug(ld, "ldap_connect_to_host: getaddrinfo failed: %s\n",
428 AC_GAI_STRERROR(err), 0, 0);
433 for( sai=res; sai != NULL; sai=sai->ai_next) {
434 if( sai->ai_addr == NULL ) {
435 osip_debug(ld, "ldap_connect_to_host: getaddrinfo "
436 "ai_addr is NULL?\n", 0, 0, 0);
440 /* we assume AF_x and PF_x are equal for all x */
441 s = ldap_int_socket( ld, sai->ai_family, socktype );
442 if ( s == AC_SOCKET_INVALID ) {
446 if ( ldap_int_prepare_socket(ld, s, proto ) == -1 ) {
447 ldap_pvt_close_socket(ld, s);
451 switch (sai->ai_family) {
454 char addr[INET6_ADDRSTRLEN];
456 &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
458 osip_debug(ld, "ldap_connect_to_host: Trying %s %s\n",
463 char addr[INET_ADDRSTRLEN];
465 &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
467 osip_debug(ld, "ldap_connect_to_host: Trying %s:%s\n",
472 rc = ldap_pvt_connect( ld, s,
473 sai->ai_addr, sai->ai_addrlen, async );
474 if ( (rc == 0) || (rc == -2) ) {
475 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
478 ldap_pvt_close_socket(ld, s);
483 if (! inet_aton( host, &in ) ) {
485 rc = ldap_pvt_gethostbyname_a( host, &he_buf, &ha_buf,
486 &hp, &local_h_errno );
488 if ( (rc < 0) || (hp == NULL) ) {
490 ldap_pvt_set_errno( WSAGetLastError() );
492 /* not exactly right, but... */
493 ldap_pvt_set_errno( EHOSTUNREACH );
495 if (ha_buf) LDAP_FREE(ha_buf);
503 for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
504 struct sockaddr_in sin;
506 s = ldap_int_socket( ld, PF_INET, socktype );
507 if ( s == AC_SOCKET_INVALID ) {
508 /* use_hp ? continue : break; */
512 if ( ldap_int_prepare_socket( ld, s, proto ) == -1 ) {
513 ldap_pvt_close_socket(ld, s);
517 (void)memset((char *)&sin, '\0', sizeof sin);
518 sin.sin_family = AF_INET;
519 sin.sin_port = htons((short) port);
522 AC_MEMCPY( &sin.sin_addr, hp->h_addr_list[i],
523 sizeof(sin.sin_addr) );
525 AC_MEMCPY( &sin.sin_addr, &in.s_addr,
526 sizeof(sin.sin_addr) );
529 #ifdef HAVE_INET_NTOA_B
532 char address[INET_ADDR_LEN];
533 inet_ntoa_b(sin.sin_address, address);
534 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n",
538 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n",
539 inet_ntoa(sin.sin_addr), port, 0);
542 rc = ldap_pvt_connect(ld, s,
543 (struct sockaddr *)&sin, sizeof(sin),
546 if ( (rc == 0) || (rc == -2) ) {
547 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
551 ldap_pvt_close_socket(ld, s);
555 if (ha_buf) LDAP_FREE(ha_buf);
561 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND ) || \
562 defined( HAVE_CYRUS_SASL )
564 ldap_host_connected_to( Sockbuf *sb, const char *host )
568 struct sockaddr_storage sabuf;
570 struct sockaddr sabuf;
572 struct sockaddr *sa = (struct sockaddr *) &sabuf;
575 (void)memset( (char *)sa, '\0', sizeof sabuf );
578 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
579 if ( getpeername( sd, sa, &len ) == -1 ) {
584 * do a reverse lookup on the addr to get the official hostname.
585 * this is necessary for kerberos to work right, since the official
586 * hostname is used as the kerberos instance.
589 switch (sa->sa_family) {
592 return LDAP_STRDUP( ldap_int_hostname );
597 struct in6_addr localhost = IN6ADDR_LOOPBACK_INIT;
598 if( memcmp ( &((struct sockaddr_in6 *)sa)->sin6_addr,
599 &localhost, sizeof(localhost)) == 0 )
601 return LDAP_STRDUP( ldap_int_hostname );
608 struct in_addr localhost;
609 localhost.s_addr = htonl( INADDR_ANY );
611 if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
612 &localhost, sizeof(localhost) ) == 0 )
614 return LDAP_STRDUP( ldap_int_hostname );
617 #ifdef INADDR_LOOPBACK
618 localhost.s_addr = htonl( INADDR_LOOPBACK );
620 if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
621 &localhost, sizeof(localhost) ) == 0 )
623 return LDAP_STRDUP( ldap_int_hostname );
637 char hbuf[NI_MAXHOST];
638 #elif defined( MAXHOSTNAMELEN
639 char hbuf[MAXHOSTNAMELEN];
645 if (ldap_pvt_get_hname( sa, len, hbuf, sizeof(hbuf), &herr ) == 0
648 return LDAP_STRDUP( hbuf );
652 return host ? LDAP_STRDUP( host ) : NULL;
659 /* for UNIX poll(2) */
661 struct pollfd si_fds[FD_SETSIZE];
663 /* for UNIX select(2) */
666 fd_set si_use_readfds;
667 fd_set si_use_writefds;
672 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
674 struct selectinfo *sip;
677 sip = (struct selectinfo *)ld->ld_selectinfo;
679 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
682 /* for UNIX poll(2) */
686 for(i=0; i < sip->si_maxfd; i++) {
687 if( sip->si_fds[i].fd == sd ) {
688 sip->si_fds[i].events |= POLL_WRITE;
691 if( empty==-1 && sip->si_fds[i].fd == -1 ) {
697 if( sip->si_maxfd >= FD_SETSIZE ) {
701 empty = sip->si_maxfd++;
704 sip->si_fds[empty].fd = sd;
705 sip->si_fds[empty].events = POLL_WRITE;
708 /* for UNIX select(2) */
709 if ( !FD_ISSET( sd, &sip->si_writefds )) {
710 FD_SET( sd, &sip->si_writefds );
717 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
719 struct selectinfo *sip;
722 sip = (struct selectinfo *)ld->ld_selectinfo;
724 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
727 /* for UNIX poll(2) */
731 for(i=0; i < sip->si_maxfd; i++) {
732 if( sip->si_fds[i].fd == sd ) {
733 sip->si_fds[i].events |= POLL_READ;
736 if( empty==-1 && sip->si_fds[i].fd == -1 ) {
742 if( sip->si_maxfd >= FD_SETSIZE ) {
746 empty = sip->si_maxfd++;
749 sip->si_fds[empty].fd = sd;
750 sip->si_fds[empty].events = POLL_READ;
753 /* for UNIX select(2) */
754 if ( !FD_ISSET( sd, &sip->si_readfds )) {
755 FD_SET( sd, &sip->si_readfds );
762 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
764 struct selectinfo *sip;
767 sip = (struct selectinfo *)ld->ld_selectinfo;
769 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
772 /* for UNIX poll(2) */
775 for(i=0; i < sip->si_maxfd; i++) {
776 if( sip->si_fds[i].fd == sd ) {
777 sip->si_fds[i].fd = -1;
782 /* for UNIX select(2) */
783 FD_CLR( sd, &sip->si_writefds );
784 FD_CLR( sd, &sip->si_readfds );
790 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
792 struct selectinfo *sip;
795 sip = (struct selectinfo *)ld->ld_selectinfo;
797 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
800 /* for UNIX poll(2) */
803 for(i=0; i < sip->si_maxfd; i++) {
804 if( sip->si_fds[i].fd == sd ) {
805 return sip->si_fds[i].revents & POLL_WRITE;
812 /* for UNIX select(2) */
813 return( FD_ISSET( sd, &sip->si_use_writefds ));
819 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
821 struct selectinfo *sip;
824 sip = (struct selectinfo *)ld->ld_selectinfo;
826 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
829 /* for UNIX poll(2) */
832 for(i=0; i < sip->si_maxfd; i++) {
833 if( sip->si_fds[i].fd == sd ) {
834 return sip->si_fds[i].revents & POLL_READ;
841 /* for UNIX select(2) */
842 return( FD_ISSET( sd, &sip->si_use_readfds ));
848 ldap_new_select_info( void )
850 struct selectinfo *sip;
852 sip = (struct selectinfo *)LDAP_CALLOC( 1, sizeof( struct selectinfo ));
854 if ( sip == NULL ) return NULL;
857 /* for UNIX poll(2) */
858 /* sip->si_maxfd=0 */
860 /* for UNIX select(2) */
861 FD_ZERO( &sip->si_readfds );
862 FD_ZERO( &sip->si_writefds );
865 return( (void *)sip );
870 ldap_free_select_info( void *sip )
877 int ldap_int_tblsize = 0;
880 ldap_int_ip_init( void )
882 #if defined( HAVE_SYSCONF )
883 long tblsize = sysconf( _SC_OPEN_MAX );
884 if( tblsize > INT_MAX ) tblsize = INT_MAX;
886 #elif defined( HAVE_GETDTABLESIZE )
887 int tblsize = getdtablesize();
889 int tblsize = FD_SETSIZE;
890 #endif /* !USE_SYSCONF */
893 if( tblsize > FD_SETSIZE ) tblsize = FD_SETSIZE;
894 #endif /* FD_SETSIZE */
896 ldap_int_tblsize = tblsize;
902 ldap_int_select( LDAP *ld, struct timeval *timeout )
905 struct selectinfo *sip;
907 Debug( LDAP_DEBUG_TRACE, "ldap_int_select\n", 0, 0, 0 );
910 if ( ldap_int_tblsize == 0 ) ldap_int_ip_init();
913 sip = (struct selectinfo *)ld->ld_selectinfo;
914 assert( sip != NULL );
918 int to = timeout ? TV2MILLISEC( timeout ) : INFTIM;
919 rc = poll( sip->si_fds, sip->si_maxfd, to );
922 sip->si_use_readfds = sip->si_readfds;
923 sip->si_use_writefds = sip->si_writefds;
925 rc = select( ldap_int_tblsize,
926 &sip->si_use_readfds, &sip->si_use_writefds,