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-2004 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, *opt_tv=NULL;
214 #ifdef LDAP_CONNECTIONLESS
215 /* We could do a connect() but that would interfere with
216 * attempts to poll a broadcast address
218 if (LDAP_IS_UDP(ld)) {
219 if (ld->ld_options.ldo_peer)
220 ldap_memfree(ld->ld_options.ldo_peer);
221 ld->ld_options.ldo_peer=ldap_memalloc(sizeof(struct sockaddr));
222 AC_MEMCPY(ld->ld_options.ldo_peer,sin,sizeof(struct sockaddr));
226 if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
227 tv.tv_usec = opt_tv->tv_usec;
228 tv.tv_sec = opt_tv->tv_sec;
231 osip_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
232 s, opt_tv ? tv.tv_sec : -1L, async);
234 if ( ldap_pvt_ndelay_on(ld, s) == -1 )
237 if ( connect(s, sin, addrlen) != AC_SOCKET_ERROR ) {
238 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
244 ldap_pvt_set_errno( WSAGetLastError() );
247 if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
252 if ( async ) return ( -2 );
258 int timeout = INFTIM;
260 if( opt_tv != NULL ) timeout = TV2MILLISEC( &tv );
263 fd.events = POLL_WRITE;
267 rc = poll( &fd, 1, timeout );
268 } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
269 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
271 if( rc == AC_SOCKET_ERROR ) return rc;
273 if( fd.revents & POLL_WRITE ) {
274 if ( ldap_pvt_is_socket_ready(ld, s) == -1 ) return -1;
275 if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
281 fd_set wfds, *z=NULL;
294 rc = select(ldap_int_tblsize, z, &wfds,
300 opt_tv ? &tv : NULL);
301 } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
302 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
304 if( rc == AC_SOCKET_ERROR ) return rc;
307 /* This means the connection failed */
308 if ( FD_ISSET(s, &efds) ) {
310 int dummy = sizeof(so_errno);
311 if ( getsockopt( s, SOL_SOCKET, SO_ERROR,
312 (char *) &so_errno, &dummy ) == AC_SOCKET_ERROR || !so_errno )
315 so_errno = WSAGetLastError();
317 ldap_pvt_set_errno(so_errno);
318 osip_debug(ld, "ldap_pvt_connect: error on socket %d: "
319 "errno: %d (%s)\n", s, errno, sock_errstr(errno));
323 if ( FD_ISSET(s, &wfds) ) {
325 if ( ldap_pvt_is_socket_ready(ld, s) == -1 ) return -1;
327 if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
333 osip_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
334 ldap_pvt_set_errno( ETIMEDOUT );
338 #ifndef HAVE_INET_ATON
340 ldap_pvt_inet_aton( const char *host, struct in_addr *in)
342 unsigned long u = inet_addr( host );
343 if ( u != 0xffffffff || u != (unsigned long) -1 ) {
353 ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
355 const char *host, int port,
360 ber_socket_t s = AC_SOCKET_INVALID;
362 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
365 struct addrinfo hints, *res, *sai;
369 struct hostent *hp = NULL;
370 struct hostent he_buf;
375 if( host == NULL ) host = "localhost";
378 case LDAP_PROTO_TCP: socktype = SOCK_STREAM;
380 "ldap_connect_to_host: TCP %s:%d\n",
383 case LDAP_PROTO_UDP: socktype = SOCK_DGRAM;
385 "ldap_connect_to_host: UDP %s:%d\n",
389 osip_debug( ld, "ldap_connect_to_host: unknown proto: %d\n",
394 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
395 memset( &hints, '\0', sizeof(hints) );
396 #ifdef USE_AI_ATTRCONFIG /* FIXME: configure test needed */
397 /* Use AI_ATTRCONFIG only on systems where its known to be needed. */
398 hints.ai_flags = AI_ATTRCONFIG;
400 hints.ai_family = ldap_int_inet4or6;
401 hints.ai_socktype = socktype;
402 snprintf(serv, sizeof serv, "%d", port );
404 #ifdef LDAP_R_COMPILE
405 /* most getaddrinfo(3) use non-threadsafe resolver libraries */
406 ldap_pvt_thread_mutex_lock(&ldap_int_resolv_mutex);
409 err = getaddrinfo( host, serv, &hints, &res );
411 #ifdef LDAP_R_COMPILE
412 ldap_pvt_thread_mutex_unlock(&ldap_int_resolv_mutex);
416 osip_debug(ld, "ldap_connect_to_host: getaddrinfo failed: %s\n",
417 AC_GAI_STRERROR(err), 0, 0);
422 for( sai=res; sai != NULL; sai=sai->ai_next) {
423 if( sai->ai_addr == NULL ) {
424 osip_debug(ld, "ldap_connect_to_host: getaddrinfo "
425 "ai_addr is NULL?\n", 0, 0, 0);
429 /* we assume AF_x and PF_x are equal for all x */
430 s = ldap_int_socket( ld, sai->ai_family, socktype );
431 if ( s == AC_SOCKET_INVALID ) {
435 if ( ldap_int_prepare_socket(ld, s, proto ) == -1 ) {
436 ldap_pvt_close_socket(ld, s);
440 switch (sai->ai_family) {
443 char addr[INET6_ADDRSTRLEN];
445 &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
447 osip_debug(ld, "ldap_connect_to_host: Trying %s %s\n",
452 char addr[INET_ADDRSTRLEN];
454 &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
456 osip_debug(ld, "ldap_connect_to_host: Trying %s:%s\n",
461 rc = ldap_pvt_connect( ld, s,
462 sai->ai_addr, sai->ai_addrlen, async );
463 if ( (rc == 0) || (rc == -2) ) {
464 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
467 ldap_pvt_close_socket(ld, s);
472 if (! inet_aton( host, &in ) ) {
474 rc = ldap_pvt_gethostbyname_a( host, &he_buf, &ha_buf,
475 &hp, &local_h_errno );
477 if ( (rc < 0) || (hp == NULL) ) {
479 ldap_pvt_set_errno( WSAGetLastError() );
481 /* not exactly right, but... */
482 ldap_pvt_set_errno( EHOSTUNREACH );
484 if (ha_buf) LDAP_FREE(ha_buf);
492 for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
493 struct sockaddr_in sin;
495 s = ldap_int_socket( ld, PF_INET, socktype );
496 if ( s == AC_SOCKET_INVALID ) {
497 /* use_hp ? continue : break; */
501 if ( ldap_int_prepare_socket( ld, s, proto ) == -1 ) {
502 ldap_pvt_close_socket(ld, s);
506 (void)memset((char *)&sin, '\0', sizeof sin);
507 sin.sin_family = AF_INET;
508 sin.sin_port = htons((short) port);
511 AC_MEMCPY( &sin.sin_addr, hp->h_addr_list[i],
512 sizeof(sin.sin_addr) );
514 AC_MEMCPY( &sin.sin_addr, &in.s_addr,
515 sizeof(sin.sin_addr) );
518 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n",
519 inet_ntoa(sin.sin_addr), port, 0);
521 rc = ldap_pvt_connect(ld, s,
522 (struct sockaddr *)&sin, sizeof(sin),
525 if ( (rc == 0) || (rc == -2) ) {
526 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
530 ldap_pvt_close_socket(ld, s);
534 if (ha_buf) LDAP_FREE(ha_buf);
540 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND ) || \
541 defined( HAVE_CYRUS_SASL )
543 ldap_host_connected_to( Sockbuf *sb, const char *host )
547 struct sockaddr_storage sabuf;
549 struct sockaddr sabuf;
551 struct sockaddr *sa = (struct sockaddr *) &sabuf;
554 (void)memset( (char *)sa, '\0', sizeof sabuf );
557 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
558 if ( getpeername( sd, sa, &len ) == -1 ) {
563 * do a reverse lookup on the addr to get the official hostname.
564 * this is necessary for kerberos to work right, since the official
565 * hostname is used as the kerberos instance.
568 switch (sa->sa_family) {
571 return LDAP_STRDUP( ldap_int_hostname );
576 struct in6_addr localhost = IN6ADDR_LOOPBACK_INIT;
577 if( memcmp ( &((struct sockaddr_in6 *)sa)->sin6_addr,
578 &localhost, sizeof(localhost)) == 0 )
580 return LDAP_STRDUP( ldap_int_hostname );
587 struct in_addr localhost;
588 localhost.s_addr = htonl( INADDR_ANY );
590 if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
591 &localhost, sizeof(localhost) ) == 0 )
593 return LDAP_STRDUP( ldap_int_hostname );
596 #ifdef INADDR_LOOPBACK
597 localhost.s_addr = htonl( INADDR_LOOPBACK );
599 if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
600 &localhost, sizeof(localhost) ) == 0 )
602 return LDAP_STRDUP( ldap_int_hostname );
616 char hbuf[NI_MAXHOST];
617 #elif defined( MAXHOSTNAMELEN
618 char hbuf[MAXHOSTNAMELEN];
624 if (ldap_pvt_get_hname( sa, len, hbuf, sizeof(hbuf), &herr ) == 0
627 return LDAP_STRDUP( hbuf );
631 return host ? LDAP_STRDUP( host ) : NULL;
638 /* for UNIX poll(2) */
640 struct pollfd si_fds[FD_SETSIZE];
642 /* for UNIX select(2) */
645 fd_set si_use_readfds;
646 fd_set si_use_writefds;
651 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
653 struct selectinfo *sip;
656 sip = (struct selectinfo *)ld->ld_selectinfo;
658 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
661 /* for UNIX poll(2) */
665 for(i=0; i < sip->si_maxfd; i++) {
666 if( sip->si_fds[i].fd == sd ) {
667 sip->si_fds[i].events |= POLL_WRITE;
670 if( empty==-1 && sip->si_fds[i].fd == -1 ) {
676 if( sip->si_maxfd >= FD_SETSIZE ) {
680 empty = sip->si_maxfd++;
683 sip->si_fds[empty].fd = sd;
684 sip->si_fds[empty].events = POLL_WRITE;
687 /* for UNIX select(2) */
688 if ( !FD_ISSET( sd, &sip->si_writefds )) {
689 FD_SET( sd, &sip->si_writefds );
696 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
698 struct selectinfo *sip;
701 sip = (struct selectinfo *)ld->ld_selectinfo;
703 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
706 /* for UNIX poll(2) */
710 for(i=0; i < sip->si_maxfd; i++) {
711 if( sip->si_fds[i].fd == sd ) {
712 sip->si_fds[i].events |= POLL_READ;
715 if( empty==-1 && sip->si_fds[i].fd == -1 ) {
721 if( sip->si_maxfd >= FD_SETSIZE ) {
725 empty = sip->si_maxfd++;
728 sip->si_fds[empty].fd = sd;
729 sip->si_fds[empty].events = POLL_READ;
732 /* for UNIX select(2) */
733 if ( !FD_ISSET( sd, &sip->si_readfds )) {
734 FD_SET( sd, &sip->si_readfds );
741 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
743 struct selectinfo *sip;
746 sip = (struct selectinfo *)ld->ld_selectinfo;
748 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
751 /* for UNIX poll(2) */
754 for(i=0; i < sip->si_maxfd; i++) {
755 if( sip->si_fds[i].fd == sd ) {
756 sip->si_fds[i].fd = -1;
761 /* for UNIX select(2) */
762 FD_CLR( sd, &sip->si_writefds );
763 FD_CLR( sd, &sip->si_readfds );
769 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
771 struct selectinfo *sip;
774 sip = (struct selectinfo *)ld->ld_selectinfo;
776 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
779 /* for UNIX poll(2) */
782 for(i=0; i < sip->si_maxfd; i++) {
783 if( sip->si_fds[i].fd == sd ) {
784 return sip->si_fds[i].revents & POLL_WRITE;
791 /* for UNIX select(2) */
792 return( FD_ISSET( sd, &sip->si_use_writefds ));
798 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
800 struct selectinfo *sip;
803 sip = (struct selectinfo *)ld->ld_selectinfo;
805 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
808 /* for UNIX poll(2) */
811 for(i=0; i < sip->si_maxfd; i++) {
812 if( sip->si_fds[i].fd == sd ) {
813 return sip->si_fds[i].revents & POLL_READ;
820 /* for UNIX select(2) */
821 return( FD_ISSET( sd, &sip->si_use_readfds ));
827 ldap_new_select_info( void )
829 struct selectinfo *sip;
831 sip = (struct selectinfo *)LDAP_CALLOC( 1, sizeof( struct selectinfo ));
833 if ( sip == NULL ) return NULL;
836 /* for UNIX poll(2) */
837 /* sip->si_maxfd=0 */
839 /* for UNIX select(2) */
840 FD_ZERO( &sip->si_readfds );
841 FD_ZERO( &sip->si_writefds );
844 return( (void *)sip );
849 ldap_free_select_info( void *sip )
856 int ldap_int_tblsize = 0;
859 ldap_int_ip_init( void )
863 #if defined( HAVE_SYSCONF )
864 tblsize = sysconf( _SC_OPEN_MAX );
865 #elif defined( HAVE_GETDTABLESIZE )
866 tblsize = getdtablesize();
868 tblsize = FD_SETSIZE;
869 #endif /* !USE_SYSCONF */
872 if( tblsize > FD_SETSIZE ) tblsize = FD_SETSIZE;
873 #endif /* FD_SETSIZE */
875 ldap_int_tblsize = tblsize;
881 ldap_int_select( LDAP *ld, struct timeval *timeout )
884 struct selectinfo *sip;
886 Debug( LDAP_DEBUG_TRACE, "ldap_int_select\n", 0, 0, 0 );
889 if ( ldap_int_tblsize == 0 ) ldap_int_ip_init();
892 sip = (struct selectinfo *)ld->ld_selectinfo;
896 int to = timeout ? TV2MILLISEC( timeout ) : INFTIM;
897 rc = poll( sip->si_fds, sip->si_maxfd, to );
900 sip->si_use_readfds = sip->si_readfds;
901 sip->si_use_writefds = sip->si_writefds;
903 rc = select( ldap_int_tblsize,
904 &sip->si_use_readfds, &sip->si_use_writefds,