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-2009 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 */
45 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
47 int ldap_int_inet4or6 = AF_UNSPEC;
49 int ldap_int_inet4or6 = AF_INET;
55 #define osip_debug(ld,fmt,arg1,arg2,arg3) \
57 ldap_log_printf(NULL, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
62 #define osip_debug(ld,fmt,arg1,arg2,arg3) ((void)0)
64 #endif /* LDAP_DEBUG */
67 ldap_pvt_set_errno(int err)
73 ldap_int_timeval_dup( struct timeval **dest, const struct timeval *src )
77 assert( dest != NULL );
84 new = (struct timeval *) LDAP_MALLOC(sizeof(struct timeval));
91 AC_MEMCPY( (char *) new, (const char *) src, sizeof(struct timeval));
98 ldap_pvt_ndelay_on(LDAP *ld, int fd)
100 osip_debug(ld, "ldap_ndelay_on: %d\n",fd,0,0);
101 return ber_pvt_socket_set_nonblock( fd, 1 );
105 ldap_pvt_ndelay_off(LDAP *ld, int fd)
107 osip_debug(ld, "ldap_ndelay_off: %d\n",fd,0,0);
108 return ber_pvt_socket_set_nonblock( fd, 0 );
112 ldap_int_socket(LDAP *ld, int family, int type )
114 ber_socket_t s = socket(family, type, 0);
115 osip_debug(ld, "ldap_new_socket: %d\n",s,0,0);
117 fcntl(s, F_SETFD, FD_CLOEXEC);
123 ldap_pvt_close_socket(LDAP *ld, int s)
125 osip_debug(ld, "ldap_close_socket: %d\n",s,0,0);
130 ldap_int_prepare_socket(LDAP *ld, int s, int proto )
132 osip_debug( ld, "ldap_prepare_socket: %d\n", s, 0, 0 );
134 #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY )
135 if ( proto == LDAP_PROTO_TCP ) {
138 if ( setsockopt( s, SOL_SOCKET, SO_KEEPALIVE,
139 (char*) &dummy, sizeof(dummy) ) == AC_SOCKET_ERROR )
141 osip_debug( ld, "ldap_prepare_socket: "
142 "setsockopt(%d, SO_KEEPALIVE) failed (ignored).\n",
145 #endif /* SO_KEEPALIVE */
147 if ( setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
148 (char*) &dummy, sizeof(dummy) ) == AC_SOCKET_ERROR )
150 osip_debug( ld, "ldap_prepare_socket: "
151 "setsockopt(%d, TCP_NODELAY) failed (ignored).\n",
154 #endif /* TCP_NODELAY */
156 #endif /* SO_KEEPALIVE || TCP_NODELAY */
166 "ldap_is_socket_ready: error on socket %d: errno: %d (%s)\n", \
169 sock_errstr(errno) ); \
173 * check the socket for errors after select returned.
176 ldap_pvt_is_socket_ready(LDAP *ld, int s)
178 osip_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
180 #if defined( notyet ) /* && defined( SO_ERROR ) */
183 ber_socklen_t dummy = sizeof(so_errno);
184 if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
190 ldap_pvt_set_errno(so_errno);
200 struct sockaddr_storage sin;
202 struct sockaddr_in sin;
205 ber_socklen_t dummy = sizeof(sin);
206 if ( getpeername( s, (struct sockaddr *) &sin, &dummy )
209 /* XXX: needs to be replace with ber_stream_read() */
210 int rc = read(s, &ch, 1);
221 #endif /* HAVE_WINSOCK */
223 /* NOTE: this is identical to analogous code in os-local.c */
228 struct timeval *tvp )
233 osip_debug(ld, "ldap_int_poll: fd: %d tm: %ld\n",
234 s, tvp ? tvp->tv_sec : -1L, 0);
239 int timeout = INFTIM;
242 fd.events = POLL_WRITE;
245 timeout = TV2MILLISEC( tvp );
249 rc = poll( &fd, 1, timeout );
251 } while ( rc == AC_SOCKET_ERROR && errno == EINTR &&
252 LDAP_BOOL_GET( &ld->ld_options, LDAP_BOOL_RESTART ) );
254 if ( rc == AC_SOCKET_ERROR ) {
258 if ( timeout == 0 && rc == 0 ) {
262 if ( fd.revents & POLL_WRITE ) {
263 if ( ldap_pvt_is_socket_ready( ld, s ) == -1 ) {
267 if ( ldap_pvt_ndelay_off( ld, s ) == -1 ) {
275 fd_set wfds, *z = NULL;
279 struct timeval tv = { 0 };
281 #if defined( FD_SETSIZE ) && !defined( HAVE_WINSOCK )
282 if ( s >= FD_SETSIZE ) {
283 rc = AC_SOCKET_ERROR;
285 ldap_pvt_set_errno( EMFILE );
303 rc = select( ldap_int_tblsize, z, &wfds,
310 } while ( rc == AC_SOCKET_ERROR && errno == EINTR &&
311 LDAP_BOOL_GET( &ld->ld_options, LDAP_BOOL_RESTART ) );
313 if ( rc == AC_SOCKET_ERROR ) {
317 if ( rc == 0 && tvp && tvp->tv_sec == 0 && tvp->tv_usec == 0 ) {
322 /* This means the connection failed */
323 if ( FD_ISSET(s, &efds) ) {
325 ber_socklen_t dummy = sizeof(so_errno);
326 if ( getsockopt( s, SOL_SOCKET, SO_ERROR,
327 (char *) &so_errno, &dummy ) == AC_SOCKET_ERROR || !so_errno )
330 so_errno = WSAGetLastError();
332 ldap_pvt_set_errno( so_errno );
333 osip_debug(ld, "ldap_int_poll: error on socket %d: "
334 "errno: %d (%s)\n", s, errno, sock_errstr( errno ));
338 if ( FD_ISSET(s, &wfds) ) {
340 if ( ldap_pvt_is_socket_ready( ld, s ) == -1 ) {
344 if ( ldap_pvt_ndelay_off(ld, s) == -1 ) {
352 osip_debug(ld, "ldap_int_poll: timed out\n",0,0,0);
353 ldap_pvt_set_errno( ETIMEDOUT );
358 ldap_pvt_connect(LDAP *ld, ber_socket_t s,
359 struct sockaddr *sin, ber_socklen_t addrlen,
363 struct timeval tv, *opt_tv = NULL;
365 #ifdef LDAP_CONNECTIONLESS
366 /* We could do a connect() but that would interfere with
367 * attempts to poll a broadcast address
369 if (LDAP_IS_UDP(ld)) {
370 if (ld->ld_options.ldo_peer)
371 ldap_memfree(ld->ld_options.ldo_peer);
372 ld->ld_options.ldo_peer=ldap_memalloc(sizeof(struct sockaddr));
373 AC_MEMCPY(ld->ld_options.ldo_peer,sin,sizeof(struct sockaddr));
377 if ( ld->ld_options.ldo_tm_net.tv_sec >= 0 ) {
378 tv = ld->ld_options.ldo_tm_net;
382 osip_debug(ld, "ldap_pvt_connect: fd: %d tm: %ld async: %d\n",
383 s, opt_tv ? tv.tv_sec : -1L, async);
385 if ( opt_tv && ldap_pvt_ndelay_on(ld, s) == -1 )
388 if ( connect(s, sin, addrlen) != AC_SOCKET_ERROR ) {
389 if ( opt_tv && ldap_pvt_ndelay_off(ld, s) == -1 )
395 if ( err != EINPROGRESS && err != EWOULDBLOCK ) {
400 /* caller will call ldap_int_poll() as appropriate? */
404 rc = ldap_int_poll( ld, s, opt_tv );
406 osip_debug(ld, "ldap_pvt_connect: %d\n", rc, 0, 0);
411 #ifndef HAVE_INET_ATON
413 ldap_pvt_inet_aton( const char *host, struct in_addr *in)
415 unsigned long u = inet_addr( host );
418 if ( u == INADDR_NONE ) return 0;
420 if ( u == 0xffffffffUL || u == (unsigned long) -1L ) return 0;
428 ldap_int_connect_cbs(LDAP *ld, Sockbuf *sb, ber_socket_t *s, LDAPURLDesc *srv, struct sockaddr *addr)
430 struct ldapoptions *lo;
435 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, s );
437 /* Invoke all handle-specific callbacks first */
438 lo = &ld->ld_options;
439 for (ll = lo->ldo_conn_cbs; ll; ll = ll->ll_next) {
441 rc = cb->lc_add( ld, sb, srv, addr, cb );
442 /* on any failure, call the teardown functions for anything
443 * that previously succeeded
447 for (l2 = lo->ldo_conn_cbs; l2 != ll; l2 = l2->ll_next) {
449 cb->lc_del( ld, sb, cb );
451 /* a failure might have implicitly closed the fd */
452 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, s );
456 lo = LDAP_INT_GLOBAL_OPT();
457 for (ll = lo->ldo_conn_cbs; ll; ll = ll->ll_next) {
459 rc = cb->lc_add( ld, sb, srv, addr, cb );
462 for (l2 = lo->ldo_conn_cbs; l2 != ll; l2 = l2->ll_next) {
464 cb->lc_del( ld, sb, cb );
466 lo = &ld->ld_options;
467 for (l2 = lo->ldo_conn_cbs; l2; l2 = l2->ll_next) {
469 cb->lc_del( ld, sb, cb );
471 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, s );
479 ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
480 int proto, LDAPURLDesc *srv,
485 ber_socket_t s = AC_SOCKET_INVALID;
488 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
491 struct addrinfo hints, *res, *sai;
495 struct hostent *hp = NULL;
496 struct hostent he_buf;
501 if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
504 host = srv->lud_host;
507 port = srv->lud_port;
510 if( strcmp(srv->lud_scheme, "ldaps") == 0 ) {
518 case LDAP_PROTO_TCP: socktype = SOCK_STREAM;
520 "ldap_connect_to_host: TCP %s:%d\n",
523 case LDAP_PROTO_UDP: socktype = SOCK_DGRAM;
525 "ldap_connect_to_host: UDP %s:%d\n",
529 osip_debug( ld, "ldap_connect_to_host: unknown proto: %d\n",
534 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
535 memset( &hints, '\0', sizeof(hints) );
536 #ifdef USE_AI_ADDRCONFIG /* FIXME: configure test needed */
537 /* Use AI_ADDRCONFIG only on systems where its known to be needed. */
538 hints.ai_flags = AI_ADDRCONFIG;
540 hints.ai_family = ldap_int_inet4or6;
541 hints.ai_socktype = socktype;
542 snprintf(serv, sizeof serv, "%d", port );
544 #ifdef LDAP_R_COMPILE
545 /* most getaddrinfo(3) use non-threadsafe resolver libraries */
546 ldap_pvt_thread_mutex_lock(&ldap_int_resolv_mutex);
549 err = getaddrinfo( host, serv, &hints, &res );
551 #ifdef LDAP_R_COMPILE
552 ldap_pvt_thread_mutex_unlock(&ldap_int_resolv_mutex);
556 osip_debug(ld, "ldap_connect_to_host: getaddrinfo failed: %s\n",
557 AC_GAI_STRERROR(err), 0, 0);
562 for( sai=res; sai != NULL; sai=sai->ai_next) {
563 if( sai->ai_addr == NULL ) {
564 osip_debug(ld, "ldap_connect_to_host: getaddrinfo "
565 "ai_addr is NULL?\n", 0, 0, 0);
569 /* we assume AF_x and PF_x are equal for all x */
570 s = ldap_int_socket( ld, sai->ai_family, socktype );
571 if ( s == AC_SOCKET_INVALID ) {
575 if ( ldap_int_prepare_socket(ld, s, proto ) == -1 ) {
576 ldap_pvt_close_socket(ld, s);
580 switch (sai->ai_family) {
583 char addr[INET6_ADDRSTRLEN];
585 &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
587 osip_debug(ld, "ldap_connect_to_host: Trying %s %s\n",
592 char addr[INET_ADDRSTRLEN];
594 &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
596 osip_debug(ld, "ldap_connect_to_host: Trying %s:%s\n",
601 rc = ldap_pvt_connect( ld, s,
602 sai->ai_addr, sai->ai_addrlen, async );
603 if ( rc == 0 || rc == -2 ) {
604 err = ldap_int_connect_cbs( ld, sb, &s, srv, sai->ai_addr );
610 ldap_pvt_close_socket(ld, s);
615 if (! inet_aton( host, &in ) ) {
617 rc = ldap_pvt_gethostbyname_a( host, &he_buf, &ha_buf,
618 &hp, &local_h_errno );
620 if ( (rc < 0) || (hp == NULL) ) {
622 ldap_pvt_set_errno( WSAGetLastError() );
624 /* not exactly right, but... */
625 ldap_pvt_set_errno( EHOSTUNREACH );
627 if (ha_buf) LDAP_FREE(ha_buf);
635 for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
636 struct sockaddr_in sin;
638 s = ldap_int_socket( ld, PF_INET, socktype );
639 if ( s == AC_SOCKET_INVALID ) {
640 /* use_hp ? continue : break; */
644 if ( ldap_int_prepare_socket( ld, s, proto ) == -1 ) {
645 ldap_pvt_close_socket(ld, s);
649 (void)memset((char *)&sin, '\0', sizeof sin);
650 sin.sin_family = AF_INET;
651 sin.sin_port = htons((unsigned short) port);
654 AC_MEMCPY( &sin.sin_addr, hp->h_addr_list[i],
655 sizeof(sin.sin_addr) );
657 AC_MEMCPY( &sin.sin_addr, &in.s_addr,
658 sizeof(sin.sin_addr) );
661 #ifdef HAVE_INET_NTOA_B
664 char address[INET_ADDR_LEN];
665 inet_ntoa_b(sin.sin_address, address);
666 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n",
670 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n",
671 inet_ntoa(sin.sin_addr), port, 0);
674 rc = ldap_pvt_connect(ld, s,
675 (struct sockaddr *)&sin, sizeof(sin),
678 if ( (rc == 0) || (rc == -2) ) {
679 i = ldap_int_connect_cbs( ld, sb, &s, srv, (struct sockaddr *)&sin );
686 ldap_pvt_close_socket(ld, s);
690 if (ha_buf) LDAP_FREE(ha_buf);
696 #if defined( HAVE_CYRUS_SASL )
698 ldap_host_connected_to( Sockbuf *sb, const char *host )
702 struct sockaddr_storage sabuf;
704 struct sockaddr sabuf;
706 struct sockaddr *sa = (struct sockaddr *) &sabuf;
709 (void)memset( (char *)sa, '\0', sizeof sabuf );
712 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
713 if ( getpeername( sd, sa, &len ) == -1 ) {
718 * do a reverse lookup on the addr to get the official hostname.
719 * this is necessary for kerberos to work right, since the official
720 * hostname is used as the kerberos instance.
723 switch (sa->sa_family) {
726 return LDAP_STRDUP( ldap_int_hostname );
731 struct in6_addr localhost = IN6ADDR_LOOPBACK_INIT;
732 if( memcmp ( &((struct sockaddr_in6 *)sa)->sin6_addr,
733 &localhost, sizeof(localhost)) == 0 )
735 return LDAP_STRDUP( ldap_int_hostname );
742 struct in_addr localhost;
743 localhost.s_addr = htonl( INADDR_ANY );
745 if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
746 &localhost, sizeof(localhost) ) == 0 )
748 return LDAP_STRDUP( ldap_int_hostname );
751 #ifdef INADDR_LOOPBACK
752 localhost.s_addr = htonl( INADDR_LOOPBACK );
754 if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
755 &localhost, sizeof(localhost) ) == 0 )
757 return LDAP_STRDUP( ldap_int_hostname );
771 char hbuf[NI_MAXHOST];
772 #elif defined( MAXHOSTNAMELEN )
773 char hbuf[MAXHOSTNAMELEN];
779 if (ldap_pvt_get_hname( sa, len, hbuf, sizeof(hbuf), &herr ) == 0
782 return LDAP_STRDUP( hbuf );
786 return host ? LDAP_STRDUP( host ) : NULL;
793 /* for UNIX poll(2) */
795 struct pollfd si_fds[FD_SETSIZE];
797 /* for UNIX select(2) */
800 fd_set si_use_readfds;
801 fd_set si_use_writefds;
806 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
808 struct selectinfo *sip;
811 sip = (struct selectinfo *)ld->ld_selectinfo;
813 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
816 /* for UNIX poll(2) */
820 for(i=0; i < sip->si_maxfd; i++) {
821 if( sip->si_fds[i].fd == sd ) {
822 sip->si_fds[i].events |= POLL_WRITE;
825 if( empty==-1 && sip->si_fds[i].fd == -1 ) {
831 if( sip->si_maxfd >= FD_SETSIZE ) {
835 empty = sip->si_maxfd++;
838 sip->si_fds[empty].fd = sd;
839 sip->si_fds[empty].events = POLL_WRITE;
842 /* for UNIX select(2) */
843 if ( !FD_ISSET( sd, &sip->si_writefds )) {
844 FD_SET( sd, &sip->si_writefds );
851 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
853 struct selectinfo *sip;
856 sip = (struct selectinfo *)ld->ld_selectinfo;
858 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
861 /* for UNIX poll(2) */
865 for(i=0; i < sip->si_maxfd; i++) {
866 if( sip->si_fds[i].fd == sd ) {
867 sip->si_fds[i].events |= POLL_READ;
870 if( empty==-1 && sip->si_fds[i].fd == -1 ) {
876 if( sip->si_maxfd >= FD_SETSIZE ) {
880 empty = sip->si_maxfd++;
883 sip->si_fds[empty].fd = sd;
884 sip->si_fds[empty].events = POLL_READ;
887 /* for UNIX select(2) */
888 if ( !FD_ISSET( sd, &sip->si_readfds )) {
889 FD_SET( sd, &sip->si_readfds );
896 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
898 struct selectinfo *sip;
901 sip = (struct selectinfo *)ld->ld_selectinfo;
903 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
906 /* for UNIX poll(2) */
909 for(i=0; i < sip->si_maxfd; i++) {
910 if( sip->si_fds[i].fd == sd ) {
911 sip->si_fds[i].fd = -1;
916 /* for UNIX select(2) */
917 FD_CLR( sd, &sip->si_writefds );
918 FD_CLR( sd, &sip->si_readfds );
924 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
926 struct selectinfo *sip;
929 sip = (struct selectinfo *)ld->ld_selectinfo;
931 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
934 /* for UNIX poll(2) */
937 for(i=0; i < sip->si_maxfd; i++) {
938 if( sip->si_fds[i].fd == sd ) {
939 return sip->si_fds[i].revents & POLL_WRITE;
946 /* for UNIX select(2) */
947 return( FD_ISSET( sd, &sip->si_use_writefds ));
953 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
955 struct selectinfo *sip;
958 sip = (struct selectinfo *)ld->ld_selectinfo;
960 if (ber_sockbuf_ctrl( sb, LBER_SB_OPT_DATA_READY, NULL ))
963 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
966 /* for UNIX poll(2) */
969 for(i=0; i < sip->si_maxfd; i++) {
970 if( sip->si_fds[i].fd == sd ) {
971 return sip->si_fds[i].revents & POLL_READ;
978 /* for UNIX select(2) */
979 return( FD_ISSET( sd, &sip->si_use_readfds ));
985 ldap_new_select_info( void )
987 struct selectinfo *sip;
989 sip = (struct selectinfo *)LDAP_CALLOC( 1, sizeof( struct selectinfo ));
991 if ( sip == NULL ) return NULL;
994 /* for UNIX poll(2) */
995 /* sip->si_maxfd=0 */
997 /* for UNIX select(2) */
998 FD_ZERO( &sip->si_readfds );
999 FD_ZERO( &sip->si_writefds );
1002 return( (void *)sip );
1007 ldap_free_select_info( void *sip )
1014 int ldap_int_tblsize = 0;
1017 ldap_int_ip_init( void )
1019 #if defined( HAVE_SYSCONF )
1020 long tblsize = sysconf( _SC_OPEN_MAX );
1021 if( tblsize > INT_MAX ) tblsize = INT_MAX;
1023 #elif defined( HAVE_GETDTABLESIZE )
1024 int tblsize = getdtablesize();
1026 int tblsize = FD_SETSIZE;
1027 #endif /* !USE_SYSCONF */
1030 if( tblsize > FD_SETSIZE ) tblsize = FD_SETSIZE;
1031 #endif /* FD_SETSIZE */
1033 ldap_int_tblsize = tblsize;
1039 ldap_int_select( LDAP *ld, struct timeval *timeout )
1042 struct selectinfo *sip;
1044 Debug( LDAP_DEBUG_TRACE, "ldap_int_select\n", 0, 0, 0 );
1047 if ( ldap_int_tblsize == 0 ) ldap_int_ip_init();
1050 sip = (struct selectinfo *)ld->ld_selectinfo;
1051 assert( sip != NULL );
1055 int to = timeout ? TV2MILLISEC( timeout ) : INFTIM;
1056 rc = poll( sip->si_fds, sip->si_maxfd, to );
1059 sip->si_use_readfds = sip->si_readfds;
1060 sip->si_use_writefds = sip->si_writefds;
1062 rc = select( ldap_int_tblsize,
1063 &sip->si_use_readfds, &sip->si_use_writefds,