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-2003 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
24 #include <ac/stdlib.h>
27 #include <ac/socket.h>
28 #include <ac/string.h>
30 #include <ac/unistd.h>
34 #endif /* HAVE_IO_H */
38 int ldap_int_tblsize = 0;
40 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
42 int ldap_int_inet4or6 = AF_UNSPEC;
44 int ldap_int_inet4or6 = AF_INET;
49 * nonblock connect code
50 * written by Lars Uffmann, <lars.uffmann@mediaway.net>.
52 * Copyright 1999, Lars Uffmann, All rights reserved.
53 * This software is not subject to any license of my employer
56 * OpenLDAP COPYING RESTRICTIONS APPLY, see COPYRIGHT file
58 * Read about the rationale in ldap_connect_timeout:
59 * ftp://koobera.math.uic.edu/www/docs/connect.html.
64 #define osip_debug(ld,fmt,arg1,arg2,arg3) \
66 ldap_log_printf(NULL, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
71 #define osip_debug(ld,fmt,arg1,arg2,arg3) ((void)0)
73 #endif /* LDAP_DEBUG */
76 ldap_pvt_set_errno(int err)
82 ldap_int_timeval_dup( struct timeval **dest, const struct timeval *src )
86 assert( dest != NULL );
93 new = (struct timeval *) LDAP_MALLOC(sizeof(struct timeval));
100 AC_MEMCPY( (char *) new, (const char *) src, sizeof(struct timeval));
107 ldap_pvt_ndelay_on(LDAP *ld, int fd)
109 osip_debug(ld, "ldap_ndelay_on: %d\n",fd,0,0);
110 return ber_pvt_socket_set_nonblock( fd, 1 );
114 ldap_pvt_ndelay_off(LDAP *ld, int fd)
116 osip_debug(ld, "ldap_ndelay_off: %d\n",fd,0,0);
117 return ber_pvt_socket_set_nonblock( fd, 0 );
121 ldap_int_socket(LDAP *ld, int family, int type )
123 ber_socket_t s = socket(family, type, 0);
124 osip_debug(ld, "ldap_new_socket: %d\n",s,0,0);
129 ldap_pvt_close_socket(LDAP *ld, int s)
131 osip_debug(ld, "ldap_close_socket: %d\n",s,0,0);
136 ldap_int_prepare_socket(LDAP *ld, int s, int proto )
138 osip_debug(ld, "ldap_prepare_socket: %d\n", s,0,0);
141 if( proto == LDAP_PROTO_TCP ) {
143 if ( setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
144 (char*) &dummy, sizeof(dummy) ) == AC_SOCKET_ERROR )
146 osip_debug(ld, "ldap_prepare_socket: "
147 "setsockopt(%d, TCP_NODELAY) failed (ignored).\n",
161 "ldap_is_socket_ready: error on socket %d: errno: %d (%s)\n", \
164 sock_errstr(errno) ); \
168 * check the socket for errors after select returned.
171 ldap_pvt_is_socket_ready(LDAP *ld, int s)
173 osip_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
175 #if defined( notyet ) /* && defined( SO_ERROR ) */
178 socklen_t dummy = sizeof(so_errno);
179 if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
185 ldap_pvt_set_errno(so_errno);
195 struct sockaddr_storage sin;
197 struct sockaddr_in sin;
200 socklen_t dummy = sizeof(sin);
201 if ( getpeername( s, (struct sockaddr *) &sin, &dummy )
204 /* XXX: needs to be replace with ber_stream_read() */
216 #endif /* HAVE_WINSOCK */
219 ldap_pvt_connect(LDAP *ld, ber_socket_t s,
220 struct sockaddr *sin, socklen_t addrlen,
224 struct timeval tv, *opt_tv=NULL;
225 fd_set wfds, *z=NULL;
230 #ifdef LDAP_CONNECTIONLESS
231 /* We could do a connect() but that would interfere with
232 * attempts to poll a broadcast address
234 if (LDAP_IS_UDP(ld)) {
235 if (ld->ld_options.ldo_peer)
236 ldap_memfree(ld->ld_options.ldo_peer);
237 ld->ld_options.ldo_peer=ldap_memalloc(sizeof(struct sockaddr));
238 AC_MEMCPY(ld->ld_options.ldo_peer,sin,sizeof(struct sockaddr));
242 if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
243 tv.tv_usec = opt_tv->tv_usec;
244 tv.tv_sec = opt_tv->tv_sec;
247 osip_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
248 s, opt_tv ? tv.tv_sec : -1L, async);
250 if ( ldap_pvt_ndelay_on(ld, s) == -1 )
253 if ( connect(s, sin, addrlen) != AC_SOCKET_ERROR ) {
254 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
260 ldap_pvt_set_errno( WSAGetLastError() );
263 if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
268 if ( async ) return ( -2 );
280 rc = select(ldap_int_tblsize, z, &wfds,
286 opt_tv ? &tv : NULL);
287 } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
288 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
290 if( rc == AC_SOCKET_ERROR ) return rc;
293 /* This means the connection failed */
294 if ( FD_ISSET(s, &efds) ) {
296 int dummy = sizeof(so_errno);
297 if ( getsockopt( s, SOL_SOCKET, SO_ERROR,
298 (char *) &so_errno, &dummy ) == AC_SOCKET_ERROR || !so_errno )
301 so_errno = WSAGetLastError();
303 ldap_pvt_set_errno(so_errno);
304 osip_debug(ld, "ldap_pvt_connect: error on socket %d: "
305 "errno: %d (%s)\n", s, errno, sock_errstr(errno));
309 if ( FD_ISSET(s, &wfds) ) {
311 if ( ldap_pvt_is_socket_ready(ld, s) == -1 )
314 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
318 osip_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
319 ldap_pvt_set_errno( ETIMEDOUT );
323 #ifndef HAVE_INET_ATON
325 ldap_pvt_inet_aton( const char *host, struct in_addr *in)
327 unsigned long u = inet_addr( host );
328 if ( u != 0xffffffff || u != (unsigned long) -1 ) {
338 ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
340 const char *host, int port,
345 ber_socket_t s = AC_SOCKET_INVALID;
347 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
350 struct addrinfo hints, *res, *sai;
354 struct hostent *hp = NULL;
355 struct hostent he_buf;
360 if( host == NULL ) host = "localhost";
363 case LDAP_PROTO_TCP: socktype = SOCK_STREAM;
365 "ldap_connect_to_host: TCP %s:%d\n",
368 case LDAP_PROTO_UDP: socktype = SOCK_DGRAM;
370 "ldap_connect_to_host: UDP %s:%d\n",
374 osip_debug( ld, "ldap_connect_to_host: unknown proto: %d\n",
379 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
380 memset( &hints, '\0', sizeof(hints) );
382 hints.ai_flags = AI_ADDRCONFIG;
384 hints.ai_family = ldap_int_inet4or6;
385 hints.ai_socktype = socktype;
386 snprintf(serv, sizeof serv, "%d", port );
388 #ifdef LDAP_R_COMPILE
389 /* most getaddrinfo(3) use non-threadsafe resolver libraries */
390 ldap_pvt_thread_mutex_lock(&ldap_int_resolv_mutex);
393 err = getaddrinfo( host, serv, &hints, &res );
395 #ifdef LDAP_R_COMPILE
396 ldap_pvt_thread_mutex_unlock(&ldap_int_resolv_mutex);
400 osip_debug(ld, "ldap_connect_to_host: getaddrinfo failed: %s\n",
401 AC_GAI_STRERROR(err), 0, 0);
406 for( sai=res; sai != NULL; sai=sai->ai_next) {
407 if( sai->ai_addr == NULL ) {
408 osip_debug(ld, "ldap_connect_to_host: getaddrinfo "
409 "ai_addr is NULL?\n", 0, 0, 0);
413 /* we assume AF_x and PF_x are equal for all x */
414 s = ldap_int_socket( ld, sai->ai_family, socktype );
415 if ( s == AC_SOCKET_INVALID ) {
419 if ( ldap_int_prepare_socket(ld, s, proto ) == -1 ) {
420 ldap_pvt_close_socket(ld, s);
424 switch (sai->ai_family) {
427 char addr[INET6_ADDRSTRLEN];
429 &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
431 osip_debug(ld, "ldap_connect_to_host: Trying %s %s\n",
436 char addr[INET_ADDRSTRLEN];
438 &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
440 osip_debug(ld, "ldap_connect_to_host: Trying %s:%s\n",
445 rc = ldap_pvt_connect( ld, s,
446 sai->ai_addr, sai->ai_addrlen, async );
447 if ( (rc == 0) || (rc == -2) ) {
448 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
451 ldap_pvt_close_socket(ld, s);
456 if (! inet_aton( host, &in ) ) {
458 rc = ldap_pvt_gethostbyname_a( host, &he_buf, &ha_buf,
459 &hp, &local_h_errno );
461 if ( (rc < 0) || (hp == NULL) ) {
463 ldap_pvt_set_errno( WSAGetLastError() );
465 /* not exactly right, but... */
466 ldap_pvt_set_errno( EHOSTUNREACH );
468 if (ha_buf) LDAP_FREE(ha_buf);
476 for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
477 struct sockaddr_in sin;
479 s = ldap_int_socket( ld, PF_INET, socktype );
480 if ( s == AC_SOCKET_INVALID ) {
481 /* use_hp ? continue : break; */
485 if ( ldap_int_prepare_socket( ld, s, proto ) == -1 ) {
486 ldap_pvt_close_socket(ld, s);
490 (void)memset((char *)&sin, '\0', sizeof sin);
491 sin.sin_family = AF_INET;
492 sin.sin_port = htons((short) port);
495 AC_MEMCPY( &sin.sin_addr, hp->h_addr_list[i],
496 sizeof(sin.sin_addr) );
498 AC_MEMCPY( &sin.sin_addr, &in.s_addr,
499 sizeof(sin.sin_addr) );
502 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n",
503 inet_ntoa(sin.sin_addr), port, 0);
505 rc = ldap_pvt_connect(ld, s,
506 (struct sockaddr *)&sin, sizeof(sin),
509 if ( (rc == 0) || (rc == -2) ) {
510 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
514 ldap_pvt_close_socket(ld, s);
518 if (ha_buf) LDAP_FREE(ha_buf);
524 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND ) || \
525 defined( HAVE_CYRUS_SASL )
527 ldap_host_connected_to( Sockbuf *sb, const char *host )
531 struct sockaddr_storage sabuf;
533 struct sockaddr sabuf;
535 struct sockaddr *sa = (struct sockaddr *) &sabuf;
539 (void)memset( (char *)sa, '\0', sizeof sabuf );
542 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
543 if ( getpeername( sd, sa, &len ) == -1 ) {
548 * do a reverse lookup on the addr to get the official hostname.
549 * this is necessary for kerberos to work right, since the official
550 * hostname is used as the kerberos instance.
553 switch (sa->sa_family) {
556 return LDAP_STRDUP( ldap_int_hostname );
561 struct in6_addr localhost = IN6ADDR_LOOPBACK_INIT;
562 if( memcmp ( &((struct sockaddr_in6 *)sa)->sin6_addr,
563 &localhost, sizeof(localhost)) == 0 )
565 return LDAP_STRDUP( ldap_int_hostname );
572 struct in_addr localhost;
573 localhost.s_addr = htonl( INADDR_ANY );
575 if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
576 &localhost, sizeof(localhost) ) == 0 )
578 return LDAP_STRDUP( ldap_int_hostname );
581 #ifdef INADDR_LOOPBACK
582 localhost.s_addr = htonl( INADDR_LOOPBACK );
584 if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
585 &localhost, sizeof(localhost) ) == 0 )
587 return LDAP_STRDUP( ldap_int_hostname );
601 char hbuf[NI_MAXHOST];
604 if (ldap_pvt_get_hname( sa, len, hbuf, sizeof(hbuf), &herr ) == 0
607 return LDAP_STRDUP( hbuf );
612 return host ? LDAP_STRDUP( host ) : NULL;
621 fd_set si_use_readfds;
622 fd_set si_use_writefds;
627 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
629 struct selectinfo *sip;
632 sip = (struct selectinfo *)ld->ld_selectinfo;
634 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
635 if ( !FD_ISSET( sd, &sip->si_writefds )) {
636 FD_SET( sd, &sip->si_writefds );
642 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
644 struct selectinfo *sip;
647 sip = (struct selectinfo *)ld->ld_selectinfo;
649 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
650 if ( !FD_ISSET( sd, &sip->si_readfds )) {
651 FD_SET( sd, &sip->si_readfds );
657 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
659 struct selectinfo *sip;
662 sip = (struct selectinfo *)ld->ld_selectinfo;
664 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
665 FD_CLR( sd, &sip->si_writefds );
666 FD_CLR( sd, &sip->si_readfds );
671 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
673 struct selectinfo *sip;
676 sip = (struct selectinfo *)ld->ld_selectinfo;
678 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
679 return( FD_ISSET( sd, &sip->si_use_writefds ));
684 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
686 struct selectinfo *sip;
689 sip = (struct selectinfo *)ld->ld_selectinfo;
691 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
692 return( FD_ISSET( sd, &sip->si_use_readfds ));
697 ldap_new_select_info( void )
699 struct selectinfo *sip;
701 if (( sip = (struct selectinfo *)LDAP_CALLOC( 1,
702 sizeof( struct selectinfo ))) != NULL ) {
703 FD_ZERO( &sip->si_readfds );
704 FD_ZERO( &sip->si_writefds );
707 return( (void *)sip );
712 ldap_free_select_info( void *sip )
719 ldap_int_ip_init( void )
722 #if defined( HAVE_SYSCONF )
723 tblsize = sysconf( _SC_OPEN_MAX );
724 #elif defined( HAVE_GETDTABLESIZE )
725 tblsize = getdtablesize();
727 tblsize = FD_SETSIZE;
728 #endif /* !USE_SYSCONF */
731 if( tblsize > FD_SETSIZE )
732 tblsize = FD_SETSIZE;
733 #endif /* FD_SETSIZE*/
734 ldap_int_tblsize = tblsize;
739 ldap_int_select( LDAP *ld, struct timeval *timeout )
741 struct selectinfo *sip;
744 LDAP_LOG ( CONNECTION, ENTRY, "ldap_int_select\n", 0, 0, 0 );
746 Debug( LDAP_DEBUG_TRACE, "ldap_int_select\n", 0, 0, 0 );
749 if ( ldap_int_tblsize == 0 )
752 sip = (struct selectinfo *)ld->ld_selectinfo;
753 sip->si_use_readfds = sip->si_readfds;
754 sip->si_use_writefds = sip->si_writefds;
756 return( select( ldap_int_tblsize,
757 &sip->si_use_readfds, &sip->si_use_writefds,