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-2005 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 ( ldap_pvt_ndelay_on(ld, s) == -1 )
238 if ( connect(s, sin, addrlen) != AC_SOCKET_ERROR ) {
239 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
245 ldap_pvt_set_errno( WSAGetLastError() );
248 if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
253 if ( async ) return ( -2 );
259 int timeout = INFTIM;
261 if( opt_tv != NULL ) timeout = TV2MILLISEC( &tv );
264 fd.events = POLL_WRITE;
268 rc = poll( &fd, 1, timeout );
269 } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
270 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
272 if( rc == AC_SOCKET_ERROR ) return rc;
274 if( fd.revents & POLL_WRITE ) {
275 if ( ldap_pvt_is_socket_ready(ld, s) == -1 ) return -1;
276 if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
282 fd_set wfds, *z=NULL;
287 #if defined( FD_SETSIZE ) && !defined( HAVE_WINSOCK )
288 if ( s >= FD_SETSIZE ) {
289 rc = AC_SOCKET_ERROR;
291 ldap_pvt_set_errno( EMFILE );
305 rc = select(ldap_int_tblsize, z, &wfds,
311 opt_tv ? &tv : NULL);
312 } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
313 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
315 if( rc == AC_SOCKET_ERROR ) return rc;
318 /* This means the connection failed */
319 if ( FD_ISSET(s, &efds) ) {
321 int dummy = sizeof(so_errno);
322 if ( getsockopt( s, SOL_SOCKET, SO_ERROR,
323 (char *) &so_errno, &dummy ) == AC_SOCKET_ERROR || !so_errno )
326 so_errno = WSAGetLastError();
328 ldap_pvt_set_errno(so_errno);
329 osip_debug(ld, "ldap_pvt_connect: error on socket %d: "
330 "errno: %d (%s)\n", s, errno, sock_errstr(errno));
334 if ( FD_ISSET(s, &wfds) ) {
336 if ( ldap_pvt_is_socket_ready(ld, s) == -1 ) return -1;
338 if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
344 osip_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
345 ldap_pvt_set_errno( ETIMEDOUT );
349 #ifndef HAVE_INET_ATON
351 ldap_pvt_inet_aton( const char *host, struct in_addr *in)
353 unsigned long u = inet_addr( host );
356 if ( u == INADDR_NONE ) return 0;
358 if ( u == 0xffffffffUL || u == (unsigned long) -1L ) return 0;
367 ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
369 const char *host, int port,
374 ber_socket_t s = AC_SOCKET_INVALID;
376 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
379 struct addrinfo hints, *res, *sai;
383 struct hostent *hp = NULL;
384 struct hostent he_buf;
389 if( host == NULL ) host = "localhost";
392 case LDAP_PROTO_TCP: socktype = SOCK_STREAM;
394 "ldap_connect_to_host: TCP %s:%d\n",
397 case LDAP_PROTO_UDP: socktype = SOCK_DGRAM;
399 "ldap_connect_to_host: UDP %s:%d\n",
403 osip_debug( ld, "ldap_connect_to_host: unknown proto: %d\n",
408 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
409 memset( &hints, '\0', sizeof(hints) );
410 #ifdef USE_AI_ATTRCONFIG /* FIXME: configure test needed */
411 /* Use AI_ATTRCONFIG only on systems where its known to be needed. */
412 hints.ai_flags = AI_ATTRCONFIG;
414 hints.ai_family = ldap_int_inet4or6;
415 hints.ai_socktype = socktype;
416 snprintf(serv, sizeof serv, "%d", port );
418 #ifdef LDAP_R_COMPILE
419 /* most getaddrinfo(3) use non-threadsafe resolver libraries */
420 ldap_pvt_thread_mutex_lock(&ldap_int_resolv_mutex);
423 err = getaddrinfo( host, serv, &hints, &res );
425 #ifdef LDAP_R_COMPILE
426 ldap_pvt_thread_mutex_unlock(&ldap_int_resolv_mutex);
430 osip_debug(ld, "ldap_connect_to_host: getaddrinfo failed: %s\n",
431 AC_GAI_STRERROR(err), 0, 0);
436 for( sai=res; sai != NULL; sai=sai->ai_next) {
437 if( sai->ai_addr == NULL ) {
438 osip_debug(ld, "ldap_connect_to_host: getaddrinfo "
439 "ai_addr is NULL?\n", 0, 0, 0);
443 /* we assume AF_x and PF_x are equal for all x */
444 s = ldap_int_socket( ld, sai->ai_family, socktype );
445 if ( s == AC_SOCKET_INVALID ) {
449 if ( ldap_int_prepare_socket(ld, s, proto ) == -1 ) {
450 ldap_pvt_close_socket(ld, s);
454 switch (sai->ai_family) {
457 char addr[INET6_ADDRSTRLEN];
459 &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
461 osip_debug(ld, "ldap_connect_to_host: Trying %s %s\n",
466 char addr[INET_ADDRSTRLEN];
468 &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
470 osip_debug(ld, "ldap_connect_to_host: Trying %s:%s\n",
475 rc = ldap_pvt_connect( ld, s,
476 sai->ai_addr, sai->ai_addrlen, async );
477 if ( (rc == 0) || (rc == -2) ) {
478 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
481 ldap_pvt_close_socket(ld, s);
486 if (! inet_aton( host, &in ) ) {
488 rc = ldap_pvt_gethostbyname_a( host, &he_buf, &ha_buf,
489 &hp, &local_h_errno );
491 if ( (rc < 0) || (hp == NULL) ) {
493 ldap_pvt_set_errno( WSAGetLastError() );
495 /* not exactly right, but... */
496 ldap_pvt_set_errno( EHOSTUNREACH );
498 if (ha_buf) LDAP_FREE(ha_buf);
506 for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
507 struct sockaddr_in sin;
509 s = ldap_int_socket( ld, PF_INET, socktype );
510 if ( s == AC_SOCKET_INVALID ) {
511 /* use_hp ? continue : break; */
515 if ( ldap_int_prepare_socket( ld, s, proto ) == -1 ) {
516 ldap_pvt_close_socket(ld, s);
520 (void)memset((char *)&sin, '\0', sizeof sin);
521 sin.sin_family = AF_INET;
522 sin.sin_port = htons((short) port);
525 AC_MEMCPY( &sin.sin_addr, hp->h_addr_list[i],
526 sizeof(sin.sin_addr) );
528 AC_MEMCPY( &sin.sin_addr, &in.s_addr,
529 sizeof(sin.sin_addr) );
532 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n",
533 inet_ntoa(sin.sin_addr), port, 0);
535 rc = ldap_pvt_connect(ld, s,
536 (struct sockaddr *)&sin, sizeof(sin),
539 if ( (rc == 0) || (rc == -2) ) {
540 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
544 ldap_pvt_close_socket(ld, s);
548 if (ha_buf) LDAP_FREE(ha_buf);
554 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND ) || \
555 defined( HAVE_CYRUS_SASL )
557 ldap_host_connected_to( Sockbuf *sb, const char *host )
561 struct sockaddr_storage sabuf;
563 struct sockaddr sabuf;
565 struct sockaddr *sa = (struct sockaddr *) &sabuf;
568 (void)memset( (char *)sa, '\0', sizeof sabuf );
571 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
572 if ( getpeername( sd, sa, &len ) == -1 ) {
577 * do a reverse lookup on the addr to get the official hostname.
578 * this is necessary for kerberos to work right, since the official
579 * hostname is used as the kerberos instance.
582 switch (sa->sa_family) {
585 return LDAP_STRDUP( ldap_int_hostname );
590 struct in6_addr localhost = IN6ADDR_LOOPBACK_INIT;
591 if( memcmp ( &((struct sockaddr_in6 *)sa)->sin6_addr,
592 &localhost, sizeof(localhost)) == 0 )
594 return LDAP_STRDUP( ldap_int_hostname );
601 struct in_addr localhost;
602 localhost.s_addr = htonl( INADDR_ANY );
604 if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
605 &localhost, sizeof(localhost) ) == 0 )
607 return LDAP_STRDUP( ldap_int_hostname );
610 #ifdef INADDR_LOOPBACK
611 localhost.s_addr = htonl( INADDR_LOOPBACK );
613 if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
614 &localhost, sizeof(localhost) ) == 0 )
616 return LDAP_STRDUP( ldap_int_hostname );
630 char hbuf[NI_MAXHOST];
631 #elif defined( MAXHOSTNAMELEN
632 char hbuf[MAXHOSTNAMELEN];
638 if (ldap_pvt_get_hname( sa, len, hbuf, sizeof(hbuf), &herr ) == 0
641 return LDAP_STRDUP( hbuf );
645 return host ? LDAP_STRDUP( host ) : NULL;
652 /* for UNIX poll(2) */
654 struct pollfd si_fds[FD_SETSIZE];
656 /* for UNIX select(2) */
659 fd_set si_use_readfds;
660 fd_set si_use_writefds;
665 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
667 struct selectinfo *sip;
670 sip = (struct selectinfo *)ld->ld_selectinfo;
672 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
675 /* for UNIX poll(2) */
679 for(i=0; i < sip->si_maxfd; i++) {
680 if( sip->si_fds[i].fd == sd ) {
681 sip->si_fds[i].events |= POLL_WRITE;
684 if( empty==-1 && sip->si_fds[i].fd == -1 ) {
690 if( sip->si_maxfd >= FD_SETSIZE ) {
694 empty = sip->si_maxfd++;
697 sip->si_fds[empty].fd = sd;
698 sip->si_fds[empty].events = POLL_WRITE;
701 /* for UNIX select(2) */
702 if ( !FD_ISSET( sd, &sip->si_writefds )) {
703 FD_SET( sd, &sip->si_writefds );
710 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
712 struct selectinfo *sip;
715 sip = (struct selectinfo *)ld->ld_selectinfo;
717 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
720 /* for UNIX poll(2) */
724 for(i=0; i < sip->si_maxfd; i++) {
725 if( sip->si_fds[i].fd == sd ) {
726 sip->si_fds[i].events |= POLL_READ;
729 if( empty==-1 && sip->si_fds[i].fd == -1 ) {
735 if( sip->si_maxfd >= FD_SETSIZE ) {
739 empty = sip->si_maxfd++;
742 sip->si_fds[empty].fd = sd;
743 sip->si_fds[empty].events = POLL_READ;
746 /* for UNIX select(2) */
747 if ( !FD_ISSET( sd, &sip->si_readfds )) {
748 FD_SET( sd, &sip->si_readfds );
755 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
757 struct selectinfo *sip;
760 sip = (struct selectinfo *)ld->ld_selectinfo;
762 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
765 /* for UNIX poll(2) */
768 for(i=0; i < sip->si_maxfd; i++) {
769 if( sip->si_fds[i].fd == sd ) {
770 sip->si_fds[i].fd = -1;
775 /* for UNIX select(2) */
776 FD_CLR( sd, &sip->si_writefds );
777 FD_CLR( sd, &sip->si_readfds );
783 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
785 struct selectinfo *sip;
788 sip = (struct selectinfo *)ld->ld_selectinfo;
790 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
793 /* for UNIX poll(2) */
796 for(i=0; i < sip->si_maxfd; i++) {
797 if( sip->si_fds[i].fd == sd ) {
798 return sip->si_fds[i].revents & POLL_WRITE;
805 /* for UNIX select(2) */
806 return( FD_ISSET( sd, &sip->si_use_writefds ));
812 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
814 struct selectinfo *sip;
817 sip = (struct selectinfo *)ld->ld_selectinfo;
819 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
822 /* for UNIX poll(2) */
825 for(i=0; i < sip->si_maxfd; i++) {
826 if( sip->si_fds[i].fd == sd ) {
827 return sip->si_fds[i].revents & POLL_READ;
834 /* for UNIX select(2) */
835 return( FD_ISSET( sd, &sip->si_use_readfds ));
841 ldap_new_select_info( void )
843 struct selectinfo *sip;
845 sip = (struct selectinfo *)LDAP_CALLOC( 1, sizeof( struct selectinfo ));
847 if ( sip == NULL ) return NULL;
850 /* for UNIX poll(2) */
851 /* sip->si_maxfd=0 */
853 /* for UNIX select(2) */
854 FD_ZERO( &sip->si_readfds );
855 FD_ZERO( &sip->si_writefds );
858 return( (void *)sip );
863 ldap_free_select_info( void *sip )
870 int ldap_int_tblsize = 0;
873 ldap_int_ip_init( void )
875 #if defined( HAVE_SYSCONF )
876 long tblsize = sysconf( _SC_OPEN_MAX );
877 if( tblsize > INT_MAX ) tblsize = INT_MAX;
879 #elif defined( HAVE_GETDTABLESIZE )
880 int tblsize = getdtablesize();
882 int tblsize = FD_SETSIZE;
883 #endif /* !USE_SYSCONF */
886 if( tblsize > FD_SETSIZE ) tblsize = FD_SETSIZE;
887 #endif /* FD_SETSIZE */
889 ldap_int_tblsize = tblsize;
895 ldap_int_select( LDAP *ld, struct timeval *timeout )
898 struct selectinfo *sip;
900 Debug( LDAP_DEBUG_TRACE, "ldap_int_select\n", 0, 0, 0 );
903 if ( ldap_int_tblsize == 0 ) ldap_int_ip_init();
906 sip = (struct selectinfo *)ld->ld_selectinfo;
907 assert( sip != NULL );
911 int to = timeout ? TV2MILLISEC( timeout ) : INFTIM;
912 rc = poll( sip->si_fds, sip->si_maxfd, to );
915 sip->si_use_readfds = sip->si_readfds;
916 sip->si_use_writefds = sip->si_writefds;
918 rc = select( ldap_int_tblsize,
919 &sip->si_use_readfds, &sip->si_use_writefds,