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 fd_set wfds, *z=NULL;
220 #ifdef LDAP_CONNECTIONLESS
221 /* We could do a connect() but that would interfere with
222 * attempts to poll a broadcast address
224 if (LDAP_IS_UDP(ld)) {
225 if (ld->ld_options.ldo_peer)
226 ldap_memfree(ld->ld_options.ldo_peer);
227 ld->ld_options.ldo_peer=ldap_memalloc(sizeof(struct sockaddr));
228 AC_MEMCPY(ld->ld_options.ldo_peer,sin,sizeof(struct sockaddr));
232 if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
233 tv.tv_usec = opt_tv->tv_usec;
234 tv.tv_sec = opt_tv->tv_sec;
237 osip_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
238 s, opt_tv ? tv.tv_sec : -1L, async);
240 if ( ldap_pvt_ndelay_on(ld, s) == -1 )
243 if ( connect(s, sin, addrlen) != AC_SOCKET_ERROR ) {
244 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
250 ldap_pvt_set_errno( WSAGetLastError() );
253 if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
258 if ( async ) return ( -2 );
273 rc = select(ldap_int_tblsize, z, &wfds,
279 opt_tv ? &tv : NULL);
280 } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
281 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
283 if( rc == AC_SOCKET_ERROR ) return rc;
286 /* This means the connection failed */
287 if ( FD_ISSET(s, &efds) ) {
289 int dummy = sizeof(so_errno);
290 if ( getsockopt( s, SOL_SOCKET, SO_ERROR,
291 (char *) &so_errno, &dummy ) == AC_SOCKET_ERROR || !so_errno )
294 so_errno = WSAGetLastError();
296 ldap_pvt_set_errno(so_errno);
297 osip_debug(ld, "ldap_pvt_connect: error on socket %d: "
298 "errno: %d (%s)\n", s, errno, sock_errstr(errno));
302 if ( FD_ISSET(s, &wfds) ) {
304 if ( ldap_pvt_is_socket_ready(ld, s) == -1 )
307 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
313 osip_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
314 ldap_pvt_set_errno( ETIMEDOUT );
318 #ifndef HAVE_INET_ATON
320 ldap_pvt_inet_aton( const char *host, struct in_addr *in)
322 unsigned long u = inet_addr( host );
323 if ( u != 0xffffffff || u != (unsigned long) -1 ) {
333 ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
335 const char *host, int port,
340 ber_socket_t s = AC_SOCKET_INVALID;
342 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
345 struct addrinfo hints, *res, *sai;
349 struct hostent *hp = NULL;
350 struct hostent he_buf;
355 if( host == NULL ) host = "localhost";
358 case LDAP_PROTO_TCP: socktype = SOCK_STREAM;
360 "ldap_connect_to_host: TCP %s:%d\n",
363 case LDAP_PROTO_UDP: socktype = SOCK_DGRAM;
365 "ldap_connect_to_host: UDP %s:%d\n",
369 osip_debug( ld, "ldap_connect_to_host: unknown proto: %d\n",
374 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
375 memset( &hints, '\0', sizeof(hints) );
377 hints.ai_flags = AI_ADDRCONFIG;
379 hints.ai_family = ldap_int_inet4or6;
380 hints.ai_socktype = socktype;
381 snprintf(serv, sizeof serv, "%d", port );
383 #ifdef LDAP_R_COMPILE
384 /* most getaddrinfo(3) use non-threadsafe resolver libraries */
385 ldap_pvt_thread_mutex_lock(&ldap_int_resolv_mutex);
388 err = getaddrinfo( host, serv, &hints, &res );
390 #ifdef LDAP_R_COMPILE
391 ldap_pvt_thread_mutex_unlock(&ldap_int_resolv_mutex);
395 osip_debug(ld, "ldap_connect_to_host: getaddrinfo failed: %s\n",
396 AC_GAI_STRERROR(err), 0, 0);
401 for( sai=res; sai != NULL; sai=sai->ai_next) {
402 if( sai->ai_addr == NULL ) {
403 osip_debug(ld, "ldap_connect_to_host: getaddrinfo "
404 "ai_addr is NULL?\n", 0, 0, 0);
408 /* we assume AF_x and PF_x are equal for all x */
409 s = ldap_int_socket( ld, sai->ai_family, socktype );
410 if ( s == AC_SOCKET_INVALID ) {
414 if ( ldap_int_prepare_socket(ld, s, proto ) == -1 ) {
415 ldap_pvt_close_socket(ld, s);
419 switch (sai->ai_family) {
422 char addr[INET6_ADDRSTRLEN];
424 &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
426 osip_debug(ld, "ldap_connect_to_host: Trying %s %s\n",
431 char addr[INET_ADDRSTRLEN];
433 &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
435 osip_debug(ld, "ldap_connect_to_host: Trying %s:%s\n",
440 rc = ldap_pvt_connect( ld, s,
441 sai->ai_addr, sai->ai_addrlen, async );
442 if ( (rc == 0) || (rc == -2) ) {
443 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
446 ldap_pvt_close_socket(ld, s);
451 if (! inet_aton( host, &in ) ) {
453 rc = ldap_pvt_gethostbyname_a( host, &he_buf, &ha_buf,
454 &hp, &local_h_errno );
456 if ( (rc < 0) || (hp == NULL) ) {
458 ldap_pvt_set_errno( WSAGetLastError() );
460 /* not exactly right, but... */
461 ldap_pvt_set_errno( EHOSTUNREACH );
463 if (ha_buf) LDAP_FREE(ha_buf);
471 for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
472 struct sockaddr_in sin;
474 s = ldap_int_socket( ld, PF_INET, socktype );
475 if ( s == AC_SOCKET_INVALID ) {
476 /* use_hp ? continue : break; */
480 if ( ldap_int_prepare_socket( ld, s, proto ) == -1 ) {
481 ldap_pvt_close_socket(ld, s);
485 (void)memset((char *)&sin, '\0', sizeof sin);
486 sin.sin_family = AF_INET;
487 sin.sin_port = htons((short) port);
490 AC_MEMCPY( &sin.sin_addr, hp->h_addr_list[i],
491 sizeof(sin.sin_addr) );
493 AC_MEMCPY( &sin.sin_addr, &in.s_addr,
494 sizeof(sin.sin_addr) );
497 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n",
498 inet_ntoa(sin.sin_addr), port, 0);
500 rc = ldap_pvt_connect(ld, s,
501 (struct sockaddr *)&sin, sizeof(sin),
504 if ( (rc == 0) || (rc == -2) ) {
505 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
509 ldap_pvt_close_socket(ld, s);
513 if (ha_buf) LDAP_FREE(ha_buf);
519 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND ) || \
520 defined( HAVE_CYRUS_SASL )
522 ldap_host_connected_to( Sockbuf *sb, const char *host )
526 struct sockaddr_storage sabuf;
528 struct sockaddr sabuf;
530 struct sockaddr *sa = (struct sockaddr *) &sabuf;
533 (void)memset( (char *)sa, '\0', sizeof sabuf );
536 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
537 if ( getpeername( sd, sa, &len ) == -1 ) {
542 * do a reverse lookup on the addr to get the official hostname.
543 * this is necessary for kerberos to work right, since the official
544 * hostname is used as the kerberos instance.
547 switch (sa->sa_family) {
550 return LDAP_STRDUP( ldap_int_hostname );
555 struct in6_addr localhost = IN6ADDR_LOOPBACK_INIT;
556 if( memcmp ( &((struct sockaddr_in6 *)sa)->sin6_addr,
557 &localhost, sizeof(localhost)) == 0 )
559 return LDAP_STRDUP( ldap_int_hostname );
566 struct in_addr localhost;
567 localhost.s_addr = htonl( INADDR_ANY );
569 if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
570 &localhost, sizeof(localhost) ) == 0 )
572 return LDAP_STRDUP( ldap_int_hostname );
575 #ifdef INADDR_LOOPBACK
576 localhost.s_addr = htonl( INADDR_LOOPBACK );
578 if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
579 &localhost, sizeof(localhost) ) == 0 )
581 return LDAP_STRDUP( ldap_int_hostname );
595 char hbuf[NI_MAXHOST];
596 #elif defined( MAXHOSTNAMELEN
597 char hbuf[MAXHOSTNAMELEN];
603 if (ldap_pvt_get_hname( sa, len, hbuf, sizeof(hbuf), &herr ) == 0
606 return LDAP_STRDUP( hbuf );
610 return host ? LDAP_STRDUP( host ) : NULL;
616 /* for UNIX poll(2) */
619 /* for UNIX select(2) */
623 fd_set si_use_readfds;
624 fd_set si_use_writefds;
629 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
631 struct selectinfo *sip;
634 sip = (struct selectinfo *)ld->ld_selectinfo;
636 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
637 if ( !FD_ISSET( sd, &sip->si_writefds )) {
638 FD_SET( sd, &sip->si_writefds );
644 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
646 struct selectinfo *sip;
649 sip = (struct selectinfo *)ld->ld_selectinfo;
651 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
652 if ( !FD_ISSET( sd, &sip->si_readfds )) {
653 FD_SET( sd, &sip->si_readfds );
659 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
661 struct selectinfo *sip;
664 sip = (struct selectinfo *)ld->ld_selectinfo;
666 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
667 FD_CLR( sd, &sip->si_writefds );
668 FD_CLR( sd, &sip->si_readfds );
673 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
675 struct selectinfo *sip;
678 sip = (struct selectinfo *)ld->ld_selectinfo;
680 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
681 return( FD_ISSET( sd, &sip->si_use_writefds ));
686 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
688 struct selectinfo *sip;
691 sip = (struct selectinfo *)ld->ld_selectinfo;
693 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
694 return( FD_ISSET( sd, &sip->si_use_readfds ));
699 ldap_new_select_info( void )
701 struct selectinfo *sip;
703 if (( sip = (struct selectinfo *)LDAP_CALLOC( 1,
704 sizeof( struct selectinfo ))) != NULL ) {
705 FD_ZERO( &sip->si_readfds );
706 FD_ZERO( &sip->si_writefds );
709 return( (void *)sip );
714 ldap_free_select_info( void *sip )
721 int ldap_int_tblsize = 0;
724 ldap_int_ip_init( void )
728 #if defined( HAVE_SYSCONF )
729 tblsize = sysconf( _SC_OPEN_MAX );
730 #elif defined( HAVE_GETDTABLESIZE )
731 tblsize = getdtablesize();
733 tblsize = FD_SETSIZE;
734 #endif /* !USE_SYSCONF */
737 if( tblsize > FD_SETSIZE ) tblsize = FD_SETSIZE;
738 #endif /* FD_SETSIZE*/
740 ldap_int_tblsize = tblsize;
746 ldap_int_select( LDAP *ld, struct timeval *timeout )
748 struct selectinfo *sip;
751 LDAP_LOG ( CONNECTION, ENTRY, "ldap_int_select\n", 0, 0, 0 );
753 Debug( LDAP_DEBUG_TRACE, "ldap_int_select\n", 0, 0, 0 );
757 if ( ldap_int_tblsize == 0 ) ldap_int_ip_init();
760 sip = (struct selectinfo *)ld->ld_selectinfo;
761 sip->si_use_readfds = sip->si_readfds;
762 sip->si_use_writefds = sip->si_writefds;
768 return( select( ldap_int_tblsize,
769 &sip->si_use_readfds, &sip->si_use_writefds,