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 */
217 /* NOTE: this is identical to analogous code in os-local.c */
222 struct timeval *tvp )
224 int timeout = INFTIM;
225 struct timeval tv = { 0 };
230 timeout = TV2MILLISEC( tvp );
233 osip_debug(ld, "ldap_int_poll: fd: %d tm: %ld\n",
234 s, tvp ? tvp->tv_sec : -1L, 0);
241 fd.events = POLL_WRITE;
245 rc = poll( &fd, 1, timeout );
247 } while ( rc == AC_SOCKET_ERROR && errno == EINTR &&
248 LDAP_BOOL_GET( &ld->ld_options, LDAP_BOOL_RESTART ) );
250 if ( rc == AC_SOCKET_ERROR ) {
254 if ( timeout == 0 && rc == 0 ) {
258 if ( fd.revents & POLL_WRITE ) {
259 if ( ldap_pvt_is_socket_ready( ld, s ) == -1 ) {
263 if ( ldap_pvt_ndelay_off( ld, s ) == -1 ) {
271 fd_set wfds, *z = NULL;
276 #if defined( FD_SETSIZE ) && !defined( HAVE_WINSOCK )
277 if ( s >= FD_SETSIZE ) {
278 rc = AC_SOCKET_ERROR;
280 ldap_pvt_set_errno( EMFILE );
294 rc = select( ldap_int_tblsize, z, &wfds,
301 } while ( rc == AC_SOCKET_ERROR && errno == EINTR &&
302 LDAP_BOOL_GET( &ld->ld_options, LDAP_BOOL_RESTART ) );
304 if ( rc == AC_SOCKET_ERROR ) {
308 if ( timeout == 0 && rc == 0 ) {
313 /* This means the connection failed */
314 if ( FD_ISSET(s, &efds) ) {
316 int dummy = sizeof(so_errno);
317 if ( getsockopt( s, SOL_SOCKET, SO_ERROR,
318 (char *) &so_errno, &dummy ) == AC_SOCKET_ERROR || !so_errno )
321 so_errno = WSAGetLastError();
323 ldap_pvt_set_errno( so_errno );
324 osip_debug(ld, "ldap_int_poll: error on socket %d: "
325 "errno: %d (%s)\n", s, errno, sock_errstr( errno ));
329 if ( FD_ISSET(s, &wfds) ) {
331 if ( ldap_pvt_is_socket_ready( ld, s ) == -1 ) {
335 if ( ldap_pvt_ndelay_off(ld, s) == -1 ) {
343 osip_debug(ld, "ldap_int_poll: timed out\n",0,0,0);
344 ldap_pvt_set_errno( ETIMEDOUT );
349 ldap_pvt_connect(LDAP *ld, ber_socket_t s,
350 struct sockaddr *sin, socklen_t addrlen,
354 struct timeval tv = { 0 },
357 #ifdef LDAP_CONNECTIONLESS
358 /* We could do a connect() but that would interfere with
359 * attempts to poll a broadcast address
361 if (LDAP_IS_UDP(ld)) {
362 if (ld->ld_options.ldo_peer)
363 ldap_memfree(ld->ld_options.ldo_peer);
364 ld->ld_options.ldo_peer=ldap_memalloc(sizeof(struct sockaddr));
365 AC_MEMCPY(ld->ld_options.ldo_peer,sin,sizeof(struct sockaddr));
369 opt_tv = ld->ld_options.ldo_tm_net;
370 if ( opt_tv != NULL ) {
374 osip_debug(ld, "ldap_pvt_connect: fd: %d tm: %ld async: %d\n",
375 s, opt_tv ? tv.tv_sec : -1L, async);
377 if ( opt_tv && ldap_pvt_ndelay_on(ld, s) == -1 )
380 if ( connect(s, sin, addrlen) != AC_SOCKET_ERROR ) {
381 if ( opt_tv && ldap_pvt_ndelay_off(ld, s) == -1 )
387 if ( err != EINPROGRESS && err != EWOULDBLOCK ) {
392 /* caller will call ldap_int_poll() as appropriate? */
396 rc = ldap_int_poll( ld, s, opt_tv );
402 int timeout = INFTIM;
404 if( opt_tv != NULL ) timeout = TV2MILLISEC( &tv );
407 fd.events = POLL_WRITE;
411 rc = poll( &fd, 1, timeout );
412 } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
413 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
415 if( rc == AC_SOCKET_ERROR ) return rc;
417 if( fd.revents & POLL_WRITE ) {
418 if ( ldap_pvt_is_socket_ready(ld, s) == -1 ) return -1;
419 if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
425 fd_set wfds, *z=NULL;
430 #if defined( FD_SETSIZE ) && !defined( HAVE_WINSOCK )
431 if ( s >= FD_SETSIZE ) {
432 rc = AC_SOCKET_ERROR;
434 ldap_pvt_set_errno( EMFILE );
448 rc = select(ldap_int_tblsize, z, &wfds,
454 opt_tv ? &tv : NULL);
455 } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
456 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
458 if( rc == AC_SOCKET_ERROR ) return rc;
461 /* This means the connection failed */
462 if ( FD_ISSET(s, &efds) ) {
464 int dummy = sizeof(so_errno);
465 if ( getsockopt( s, SOL_SOCKET, SO_ERROR,
466 (char *) &so_errno, &dummy ) == AC_SOCKET_ERROR || !so_errno )
469 so_errno = WSAGetLastError();
471 ldap_pvt_set_errno(so_errno);
472 osip_debug(ld, "ldap_pvt_connect: error on socket %d: "
473 "errno: %d (%s)\n", s, errno, sock_errstr(errno));
477 if ( FD_ISSET(s, &wfds) ) {
479 if ( ldap_pvt_is_socket_ready(ld, s) == -1 ) return -1;
481 if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
489 osip_debug(ld, "ldap_pvt_connect: %d\n", rc, 0, 0);
493 #ifndef HAVE_INET_ATON
495 ldap_pvt_inet_aton( const char *host, struct in_addr *in)
497 unsigned long u = inet_addr( host );
500 if ( u == INADDR_NONE ) return 0;
502 if ( u == 0xffffffffUL || u == (unsigned long) -1L ) return 0;
511 ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
513 const char *host, int port,
518 ber_socket_t s = AC_SOCKET_INVALID;
520 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
523 struct addrinfo hints, *res, *sai;
527 struct hostent *hp = NULL;
528 struct hostent he_buf;
533 if( host == NULL ) host = "localhost";
536 case LDAP_PROTO_TCP: socktype = SOCK_STREAM;
538 "ldap_connect_to_host: TCP %s:%d\n",
541 case LDAP_PROTO_UDP: socktype = SOCK_DGRAM;
543 "ldap_connect_to_host: UDP %s:%d\n",
547 osip_debug( ld, "ldap_connect_to_host: unknown proto: %d\n",
552 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
553 memset( &hints, '\0', sizeof(hints) );
554 #ifdef USE_AI_ATTRCONFIG /* FIXME: configure test needed */
555 /* Use AI_ATTRCONFIG only on systems where its known to be needed. */
556 hints.ai_flags = AI_ATTRCONFIG;
558 hints.ai_family = ldap_int_inet4or6;
559 hints.ai_socktype = socktype;
560 snprintf(serv, sizeof serv, "%d", port );
562 #ifdef LDAP_R_COMPILE
563 /* most getaddrinfo(3) use non-threadsafe resolver libraries */
564 ldap_pvt_thread_mutex_lock(&ldap_int_resolv_mutex);
567 err = getaddrinfo( host, serv, &hints, &res );
569 #ifdef LDAP_R_COMPILE
570 ldap_pvt_thread_mutex_unlock(&ldap_int_resolv_mutex);
574 osip_debug(ld, "ldap_connect_to_host: getaddrinfo failed: %s\n",
575 AC_GAI_STRERROR(err), 0, 0);
580 for( sai=res; sai != NULL; sai=sai->ai_next) {
581 if( sai->ai_addr == NULL ) {
582 osip_debug(ld, "ldap_connect_to_host: getaddrinfo "
583 "ai_addr is NULL?\n", 0, 0, 0);
587 /* we assume AF_x and PF_x are equal for all x */
588 s = ldap_int_socket( ld, sai->ai_family, socktype );
589 if ( s == AC_SOCKET_INVALID ) {
593 if ( ldap_int_prepare_socket(ld, s, proto ) == -1 ) {
594 ldap_pvt_close_socket(ld, s);
598 switch (sai->ai_family) {
601 char addr[INET6_ADDRSTRLEN];
603 &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
605 osip_debug(ld, "ldap_connect_to_host: Trying %s %s\n",
610 char addr[INET_ADDRSTRLEN];
612 &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
614 osip_debug(ld, "ldap_connect_to_host: Trying %s:%s\n",
619 rc = ldap_pvt_connect( ld, s,
620 sai->ai_addr, sai->ai_addrlen, async );
621 if ( rc == 0 || rc == -2 ) {
622 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
625 ldap_pvt_close_socket(ld, s);
630 if (! inet_aton( host, &in ) ) {
632 rc = ldap_pvt_gethostbyname_a( host, &he_buf, &ha_buf,
633 &hp, &local_h_errno );
635 if ( (rc < 0) || (hp == NULL) ) {
637 ldap_pvt_set_errno( WSAGetLastError() );
639 /* not exactly right, but... */
640 ldap_pvt_set_errno( EHOSTUNREACH );
642 if (ha_buf) LDAP_FREE(ha_buf);
650 for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
651 struct sockaddr_in sin;
653 s = ldap_int_socket( ld, PF_INET, socktype );
654 if ( s == AC_SOCKET_INVALID ) {
655 /* use_hp ? continue : break; */
659 if ( ldap_int_prepare_socket( ld, s, proto ) == -1 ) {
660 ldap_pvt_close_socket(ld, s);
664 (void)memset((char *)&sin, '\0', sizeof sin);
665 sin.sin_family = AF_INET;
666 sin.sin_port = htons((short) port);
669 AC_MEMCPY( &sin.sin_addr, hp->h_addr_list[i],
670 sizeof(sin.sin_addr) );
672 AC_MEMCPY( &sin.sin_addr, &in.s_addr,
673 sizeof(sin.sin_addr) );
676 #ifdef HAVE_INET_NTOA_B
679 char address[INET_ADDR_LEN];
680 inet_ntoa_b(sin.sin_address, address);
681 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n",
685 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n",
686 inet_ntoa(sin.sin_addr), port, 0);
689 rc = ldap_pvt_connect(ld, s,
690 (struct sockaddr *)&sin, sizeof(sin),
693 if ( (rc == 0) || (rc == -2) ) {
694 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
698 ldap_pvt_close_socket(ld, s);
702 if (ha_buf) LDAP_FREE(ha_buf);
708 #if defined( HAVE_CYRUS_SASL )
710 ldap_host_connected_to( Sockbuf *sb, const char *host )
714 struct sockaddr_storage sabuf;
716 struct sockaddr sabuf;
718 struct sockaddr *sa = (struct sockaddr *) &sabuf;
721 (void)memset( (char *)sa, '\0', sizeof sabuf );
724 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
725 if ( getpeername( sd, sa, &len ) == -1 ) {
730 * do a reverse lookup on the addr to get the official hostname.
731 * this is necessary for kerberos to work right, since the official
732 * hostname is used as the kerberos instance.
735 switch (sa->sa_family) {
738 return LDAP_STRDUP( ldap_int_hostname );
743 struct in6_addr localhost = IN6ADDR_LOOPBACK_INIT;
744 if( memcmp ( &((struct sockaddr_in6 *)sa)->sin6_addr,
745 &localhost, sizeof(localhost)) == 0 )
747 return LDAP_STRDUP( ldap_int_hostname );
754 struct in_addr localhost;
755 localhost.s_addr = htonl( INADDR_ANY );
757 if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
758 &localhost, sizeof(localhost) ) == 0 )
760 return LDAP_STRDUP( ldap_int_hostname );
763 #ifdef INADDR_LOOPBACK
764 localhost.s_addr = htonl( INADDR_LOOPBACK );
766 if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
767 &localhost, sizeof(localhost) ) == 0 )
769 return LDAP_STRDUP( ldap_int_hostname );
783 char hbuf[NI_MAXHOST];
784 #elif defined( MAXHOSTNAMELEN
785 char hbuf[MAXHOSTNAMELEN];
791 if (ldap_pvt_get_hname( sa, len, hbuf, sizeof(hbuf), &herr ) == 0
794 return LDAP_STRDUP( hbuf );
798 return host ? LDAP_STRDUP( host ) : NULL;
805 /* for UNIX poll(2) */
807 struct pollfd si_fds[FD_SETSIZE];
809 /* for UNIX select(2) */
812 fd_set si_use_readfds;
813 fd_set si_use_writefds;
818 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
820 struct selectinfo *sip;
823 sip = (struct selectinfo *)ld->ld_selectinfo;
825 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
828 /* for UNIX poll(2) */
832 for(i=0; i < sip->si_maxfd; i++) {
833 if( sip->si_fds[i].fd == sd ) {
834 sip->si_fds[i].events |= POLL_WRITE;
837 if( empty==-1 && sip->si_fds[i].fd == -1 ) {
843 if( sip->si_maxfd >= FD_SETSIZE ) {
847 empty = sip->si_maxfd++;
850 sip->si_fds[empty].fd = sd;
851 sip->si_fds[empty].events = POLL_WRITE;
854 /* for UNIX select(2) */
855 if ( !FD_ISSET( sd, &sip->si_writefds )) {
856 FD_SET( sd, &sip->si_writefds );
863 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
865 struct selectinfo *sip;
868 sip = (struct selectinfo *)ld->ld_selectinfo;
870 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
873 /* for UNIX poll(2) */
877 for(i=0; i < sip->si_maxfd; i++) {
878 if( sip->si_fds[i].fd == sd ) {
879 sip->si_fds[i].events |= POLL_READ;
882 if( empty==-1 && sip->si_fds[i].fd == -1 ) {
888 if( sip->si_maxfd >= FD_SETSIZE ) {
892 empty = sip->si_maxfd++;
895 sip->si_fds[empty].fd = sd;
896 sip->si_fds[empty].events = POLL_READ;
899 /* for UNIX select(2) */
900 if ( !FD_ISSET( sd, &sip->si_readfds )) {
901 FD_SET( sd, &sip->si_readfds );
908 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
910 struct selectinfo *sip;
913 sip = (struct selectinfo *)ld->ld_selectinfo;
915 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
918 /* for UNIX poll(2) */
921 for(i=0; i < sip->si_maxfd; i++) {
922 if( sip->si_fds[i].fd == sd ) {
923 sip->si_fds[i].fd = -1;
928 /* for UNIX select(2) */
929 FD_CLR( sd, &sip->si_writefds );
930 FD_CLR( sd, &sip->si_readfds );
936 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
938 struct selectinfo *sip;
941 sip = (struct selectinfo *)ld->ld_selectinfo;
943 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
946 /* for UNIX poll(2) */
949 for(i=0; i < sip->si_maxfd; i++) {
950 if( sip->si_fds[i].fd == sd ) {
951 return sip->si_fds[i].revents & POLL_WRITE;
958 /* for UNIX select(2) */
959 return( FD_ISSET( sd, &sip->si_use_writefds ));
965 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
967 struct selectinfo *sip;
970 sip = (struct selectinfo *)ld->ld_selectinfo;
972 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
975 /* for UNIX poll(2) */
978 for(i=0; i < sip->si_maxfd; i++) {
979 if( sip->si_fds[i].fd == sd ) {
980 return sip->si_fds[i].revents & POLL_READ;
987 /* for UNIX select(2) */
988 return( FD_ISSET( sd, &sip->si_use_readfds ));
994 ldap_new_select_info( void )
996 struct selectinfo *sip;
998 sip = (struct selectinfo *)LDAP_CALLOC( 1, sizeof( struct selectinfo ));
1000 if ( sip == NULL ) return NULL;
1003 /* for UNIX poll(2) */
1004 /* sip->si_maxfd=0 */
1006 /* for UNIX select(2) */
1007 FD_ZERO( &sip->si_readfds );
1008 FD_ZERO( &sip->si_writefds );
1011 return( (void *)sip );
1016 ldap_free_select_info( void *sip )
1023 int ldap_int_tblsize = 0;
1026 ldap_int_ip_init( void )
1028 #if defined( HAVE_SYSCONF )
1029 long tblsize = sysconf( _SC_OPEN_MAX );
1030 if( tblsize > INT_MAX ) tblsize = INT_MAX;
1032 #elif defined( HAVE_GETDTABLESIZE )
1033 int tblsize = getdtablesize();
1035 int tblsize = FD_SETSIZE;
1036 #endif /* !USE_SYSCONF */
1039 if( tblsize > FD_SETSIZE ) tblsize = FD_SETSIZE;
1040 #endif /* FD_SETSIZE */
1042 ldap_int_tblsize = tblsize;
1048 ldap_int_select( LDAP *ld, struct timeval *timeout )
1051 struct selectinfo *sip;
1053 Debug( LDAP_DEBUG_TRACE, "ldap_int_select\n", 0, 0, 0 );
1056 if ( ldap_int_tblsize == 0 ) ldap_int_ip_init();
1059 sip = (struct selectinfo *)ld->ld_selectinfo;
1060 assert( sip != NULL );
1064 int to = timeout ? TV2MILLISEC( timeout ) : INFTIM;
1065 rc = poll( sip->si_fds, sip->si_maxfd, to );
1068 sip->si_use_readfds = sip->si_readfds;
1069 sip->si_use_writefds = sip->si_writefds;
1071 rc = select( ldap_int_tblsize,
1072 &sip->si_use_readfds, &sip->si_use_writefds,