Some missing from merge.

Signed-off-by: Salja <salja2012@hotmail.de>
This commit is contained in:
Salja 2012-08-05 14:54:07 +02:00 committed by Antz
parent ec939a5bce
commit f4be15a7af
1895 changed files with 160408 additions and 53601 deletions

View file

@ -1,3 +1,5 @@
// $Id: INET_Addr.cpp 97355 2013-09-27 22:16:09Z shuston $
// Defines the Internet domain address family address format.
#include "ace/INET_Addr.h"
@ -16,9 +18,6 @@
#include "ace/OS_NS_unistd.h"
#include "ace/OS_NS_sys_socket.h"
#include "ace/Truncate.h"
#if defined (ACE_HAS_ALLOC_HOOKS)
# include "ace/Malloc_Base.h"
#endif /* ACE_HAS_ALLOC_HOOKS */
ACE_BEGIN_VERSIONED_NAMESPACE_DECL
@ -70,9 +69,9 @@ ACE_INET_Addr::addr_to_string (ACE_TCHAR s[],
if (size < total_len)
return -1;
else
ACE_OS::snprintf (s, size, format,
ACE_TEXT_CHAR_TO_TCHAR (hoststr),
this->get_port_number ());
ACE_OS::sprintf (s, format,
ACE_TEXT_CHAR_TO_TCHAR (hoststr),
this->get_port_number ());
return 0;
}
@ -155,38 +154,11 @@ ACE_INET_Addr::hash (void) const
return this->get_ip_address () + this->get_port_number ();
}
bool
ACE_INET_Addr::next (void)
{
if (this->inet_addrs_.empty () ||
this->inet_addrs_iter_ == this->inet_addrs_.end ())
return false;
union ip46 next_a = *this->inet_addrs_iter_++;
this->set_addr (&next_a, sizeof (next_a));
return true;
}
void
ACE_INET_Addr::reset (void)
{
this->inet_addrs_iter_ = this->inet_addrs_.begin ();
this->next ();
}
ACE_INET_Addr::ACE_INET_Addr (void)
: ACE_Addr (determine_type (), sizeof (inet_addr_))
{
// ACE_TRACE ("ACE_INET_Addr::ACE_INET_Addr");
this->reset_i ();
}
ACE_INET_Addr &
ACE_INET_Addr::operator= (const ACE_INET_Addr& rhs)
{
if (this != &rhs)
this->set (rhs);
return *this;
this->reset ();
}
int
@ -206,8 +178,6 @@ ACE_INET_Addr::set (const ACE_INET_Addr &sa)
this->set_type (sa.get_type());
this->set_size (sa.get_size());
this->inet_addrs_ = sa.inet_addrs_;
this->reset ();
}
return 0;
@ -283,11 +253,7 @@ ACE_INET_Addr::string_to_addr (const char s[], int address_family)
result = this->set (port_p, ip_addr);
}
#if defined (ACE_HAS_ALLOC_HOOKS)
ACE_Allocator::instance()->free (ACE_MALLOC_T (ip_buf));
#else
ACE_OS::free (ACE_MALLOC_T (ip_buf));
#endif /* ACE_HAS_ALLOC_HOOKS */
return result;
}
@ -295,7 +261,6 @@ int
ACE_INET_Addr::set (const char address[], int address_family)
{
ACE_TRACE ("ACE_INET_Addr::set");
this->reset_i ();
return this->string_to_addr (address, address_family);
}
@ -303,6 +268,7 @@ ACE_INET_Addr::ACE_INET_Addr (const char address[], int address_family)
: ACE_Addr (determine_type (), sizeof (inet_addr_))
{
ACE_TRACE ("ACE_INET_Addr::ACE_INET_Addr");
this->reset ();
this->set (address, address_family);
}
@ -311,6 +277,7 @@ ACE_INET_Addr::ACE_INET_Addr (const wchar_t address[], int address_family)
: ACE_Addr (determine_type (), sizeof (inet_addr_))
{
ACE_TRACE ("ACE_INET_Addr::ACE_INET_Addr");
this->reset ();
this->set (address, address_family);
}
@ -322,6 +289,7 @@ ACE_INET_Addr::ACE_INET_Addr (const ACE_INET_Addr &sa)
: ACE_Addr (sa.get_type (), sa.get_size())
{
ACE_TRACE ("ACE_INET_Addr::ACE_INET_Addr");
this->reset ();
this->set (sa);
}
@ -335,7 +303,6 @@ ACE_INET_Addr::set (u_short port_number,
int map)
{
ACE_TRACE ("ACE_INET_Addr::set");
this->reset_i ();
this->set_address (reinterpret_cast<const char *> (&inet_address),
sizeof inet_address,
encode, map);
@ -363,98 +330,112 @@ ACE_INET_Addr::set (u_short port_number,
return -1;
}
this->reset_i ();
ACE_OS::memset ((void *) &this->inet_addr_,
0,
sizeof this->inet_addr_);
#if defined ACE_HAS_IPV6 && defined ACE_USES_IPV4_IPV6_MIGRATION
#if defined (ACE_HAS_IPV6)
// Let the IPv4 case fall through to the non-IPv6-capable section.
// We don't need the additional getaddrinfo() capability and the Linux
// getaddrinfo() is substantially slower than gethostbyname() w/
// large vlans.
# if defined (ACE_USES_IPV4_IPV6_MIGRATION)
if (address_family == AF_UNSPEC && !ACE::ipv6_enabled ())
address_family = AF_INET;
#endif /* ACE_HAS_IPV6 && ACE_USES_IPV4_IPV6_MIGRATION */
#ifdef ACE_HAS_IPV6
if (address_family == AF_UNSPEC && ACE::ipv6_enabled ())
address_family = AF_INET6;
if (address_family != AF_INET
&& ACE_OS::inet_pton (AF_INET6, host_name,
&this->inet_addr_.in6_.sin6_addr) == 1)
# endif /* ACE_USES_IPV4_IPV6_MIGRATION */
if (address_family != AF_INET)
{
this->base_set (AF_INET6, sizeof this->inet_addr_.in4_);
# ifdef ACE_HAS_SOCKADDR_IN6_SIN6_LEN
this->inet_addr_.in6_.sin6_len = sizeof this->inet_addr_.in6_;
# endif
this->inet_addr_.in6_.sin6_family = AF_INET6;
this->set_size (sizeof this->inet_addr_.in6_);
this->set_type (AF_INET6);
this->set_port_number (port_number, encode);
return 0;
# if defined (ACE_HAS_GETHOSTBYNAME2)
hostent hentry;
hostent *hp;
ACE_HOSTENT_DATA buf;
int h_error = 0; // Not the same as errno!
if (0 == ::gethostbyname2_r (host_name, AF_INET6, &hentry,
buf, sizeof(buf), &hp, &h_error))
{
if (hp != 0)
{
struct sockaddr_in6 v6;
ACE_OS::memset (&v6, 0, sizeof (v6));
v6.sin6_family = AF_INET6;
(void) ACE_OS::memcpy ((void *) &v6.sin6_addr,
hp->h_addr,
hp->h_length);
this->set_type (hp->h_addrtype);
this->set_addr (&v6, hp->h_length);
this->set_port_number (port_number, encode);
return 0;
}
}
errno = h_error;
if (address_family == AF_INET6)
return -1;
# else
struct addrinfo hints;
struct addrinfo *res = 0;
int error = 0;
ACE_OS::memset (&hints, 0, sizeof (hints));
hints.ai_family = AF_INET6;
if ((error = ::getaddrinfo (host_name, 0, &hints, &res)) == 0)
{
this->set_type (res->ai_family);
this->set_addr (res->ai_addr,
ACE_Utils::truncate_cast<int>(res->ai_addrlen));
this->set_port_number (port_number, encode);
::freeaddrinfo (res);
return 0;
}
if (address_family == AF_INET6)
{
if (res)
::freeaddrinfo(res);
errno = error;
return -1;
}
# endif /* ACE_HAS_GETHOSTBYNAME2 */
// Let AF_UNSPEC try again w/ IPv4.
}
#else
address_family = AF_INET;
#endif /* ACE_HAS_IPV6 */
if (ACE_OS::inet_pton (AF_INET, host_name,
&this->inet_addr_.in4_.sin_addr) == 1)
{
this->base_set (AF_INET, sizeof this->inet_addr_.in4_);
// IPv6 not supported... insure the family is set to IPv4
address_family = AF_INET;
this->set_type (address_family);
this->inet_addr_.in4_.sin_family = static_cast<short> (address_family);
#ifdef ACE_HAS_SOCKADDR_IN_SIN_LEN
this->inet_addr_.in4_.sin_len = sizeof this->inet_addr_.in4_;
this->inet_addr_.in4_.sin_len = sizeof (this->inet_addr_.in4_);
#endif
this->inet_addr_.in4_.sin_family = AF_INET;
this->set_size (sizeof this->inet_addr_.in4_);
this->set_type (AF_INET);
this->set_port_number (port_number, encode);
return 0;
}
addrinfo hints;
ACE_OS::memset (&hints, 0, sizeof hints);
hints.ai_family = address_family;
// The ai_flags used to contain AI_ADDRCONFIG as well but that prevented
// lookups from completing if there is no, or only a loopback, IPv6
// interface configured. See Bugzilla 4211 for more info.
hints.ai_flags = AI_V4MAPPED;
#if defined(ACE_HAS_IPV6) && defined(AI_ALL)
// Without AI_ALL, Windows machines exhibit inconsistent behaviors on
// difference machines we have tested.
hints.ai_flags |= AI_ALL;
#endif
// Note - specify the socktype here to avoid getting multiple entries
// returned with the same address for different socket types or
// protocols. If this causes a problem for some reason (an address that's
// available for TCP but not UDP, or vice-versa) this will need to change
// back to unrestricted hints and weed out the duplicate addresses by
// searching this->inet_addrs_ which would slow things down.
hints.ai_socktype = SOCK_STREAM;
addrinfo *res = 0;
const int error = ACE_OS::getaddrinfo (host_name, 0, &hints, &res);
if (error)
struct in_addr addrv4;
if (ACE_OS::inet_aton (host_name,
&addrv4) == 1)
return this->set (port_number,
encode ? ACE_NTOHL (addrv4.s_addr) : addrv4.s_addr,
encode);
else
{
errno = error;
return -1;
}
hostent hentry;
ACE_HOSTENT_DATA buf;
int h_error = 0; // Not the same as errno!
this->set_type (res->ai_family);
hostent *hp = ACE_OS::gethostbyname_r (host_name, &hentry,
buf, &h_error);
if (hp == 0)
errno = h_error;
for (addrinfo *curr = res; curr; curr = curr->ai_next)
{
ip46 addr;
ACE_OS::memcpy (&addr, curr->ai_addr, curr->ai_addrlen);
#ifdef ACE_HAS_IPV6
if (curr->ai_family == AF_INET6)
addr.in6_.sin6_port = encode ? ACE_NTOHS (port_number) : port_number;
if (hp == 0)
{
return -1;
}
else
#endif
addr.in4_.sin_port = encode ? ACE_NTOHS (port_number) : port_number;
this->inet_addrs_.push_back (addr);
{
(void) ACE_OS::memcpy ((void *) &addrv4.s_addr,
hp->h_addr,
hp->h_length);
return this->set (port_number,
encode ? ACE_NTOHL (addrv4.s_addr) : addrv4.s_addr,
encode);
}
}
ACE_OS::freeaddrinfo (res);
this->reset ();
return 0;
}
// Helper function to get a port number from a port name.
@ -512,7 +493,6 @@ ACE_INET_Addr::set (const char port_name[],
{
ACE_TRACE ("ACE_INET_Addr::set");
this->reset_i ();
int const port_number = get_port_number_from_name (port_name, protocol);
if (port_number == -1)
{
@ -520,9 +500,6 @@ ACE_INET_Addr::set (const char port_name[],
}
int address_family = PF_UNSPEC;
if (ACE_OS::strcmp(protocol, "tcp") == 0)
address_family = AF_INET;
# if defined (ACE_HAS_IPV6)
if (ACE_OS::strcmp (protocol, "tcp6") == 0)
address_family = AF_INET6;
@ -542,7 +519,6 @@ ACE_INET_Addr::set (const char port_name[],
{
ACE_TRACE ("ACE_INET_Addr::set");
this->reset_i ();
int const port_number = get_port_number_from_name (port_name, protocol);
if (port_number == -1)
{
@ -600,7 +576,6 @@ ACE_INET_Addr::set (const sockaddr_in *addr, int len)
{
ACE_TRACE ("ACE_INET_Addr::set");
this->reset_i ();
if (addr->sin_family == AF_INET)
{
int maxlen = static_cast<int> (sizeof (this->inet_addr_.in4_));
@ -636,18 +611,17 @@ ACE_INET_Addr::get_addr (void) const
}
void
ACE_INET_Addr::set_addr (const void *addr, int len)
ACE_INET_Addr::set_addr (void *addr, int len)
{
this->set_addr (addr, len, 0);
}
// Set a pointer to the address.
void
ACE_INET_Addr::set_addr (const void *addr, int /* len */, int map)
ACE_INET_Addr::set_addr (void *addr, int /* len */, int map)
{
ACE_TRACE ("ACE_INET_Addr::set_addr");
const struct sockaddr_in *getfamily =
static_cast<const struct sockaddr_in *> (addr);
struct sockaddr_in *getfamily = static_cast<struct sockaddr_in *> (addr);
if (getfamily->sin_family == AF_INET)
{
@ -665,8 +639,7 @@ ACE_INET_Addr::set_addr (const void *addr, int /* len */, int map)
#if defined (ACE_HAS_IPV6)
else if (getfamily->sin_family == AF_INET6)
{
const struct sockaddr_in6 *in6 =
static_cast<const struct sockaddr_in6*> (addr);
struct sockaddr_in6 *in6 = static_cast<struct sockaddr_in6*> (addr);
this->set_port_number (in6->sin6_port, 0);
this->set_address (reinterpret_cast<const char*> (&in6->sin6_addr),
sizeof (in6->sin6_addr),
@ -682,6 +655,7 @@ ACE_INET_Addr::ACE_INET_Addr (const sockaddr_in *addr, int len)
: ACE_Addr (determine_type (), sizeof (inet_addr_))
{
ACE_TRACE ("ACE_INET_Addr::ACE_INET_Addr");
this->reset ();
this->set (addr, len);
}
@ -692,6 +666,7 @@ ACE_INET_Addr::ACE_INET_Addr (u_short port_number,
: ACE_Addr (determine_type (), sizeof (inet_addr_))
{
ACE_TRACE ("ACE_INET_Addr::ACE_INET_Addr");
this->reset ();
if (this->set (port_number, inet_address) == -1)
ACELIB_ERROR ((LM_ERROR,
ACE_TEXT ("%p\n"),
@ -707,6 +682,7 @@ ACE_INET_Addr::ACE_INET_Addr (const char port_name[],
: ACE_Addr (determine_type (), sizeof (inet_addr_))
{
ACE_TRACE ("ACE_INET_Addr::ACE_INET_Addr");
this->reset ();
if (this->set (port_name,
host_name,
protocol) == -1)
@ -721,6 +697,7 @@ ACE_INET_Addr::ACE_INET_Addr (const wchar_t port_name[],
: ACE_Addr (determine_type (), sizeof (inet_addr_))
{
ACE_TRACE ("ACE_INET_Addr::ACE_INET_Addr");
this->reset ();
if (this->set (port_name,
host_name,
protocol) == -1)
@ -737,6 +714,7 @@ ACE_INET_Addr::ACE_INET_Addr (const char port_name[],
: ACE_Addr (determine_type (), sizeof (inet_addr_))
{
ACE_TRACE ("ACE_INET_Addr::ACE_INET_Addr");
this->reset ();
if (this->set (port_name,
ACE_HTONL (inet_address),
protocol) == -1)
@ -751,6 +729,7 @@ ACE_INET_Addr::ACE_INET_Addr (const wchar_t port_name[],
: ACE_Addr (determine_type (), sizeof (inet_addr_))
{
ACE_TRACE ("ACE_INET_Addr::ACE_INET_Addr");
this->reset ();
if (this->set (port_name,
ACE_HTONL (inet_address),
protocol) == -1)
@ -848,21 +827,7 @@ ACE_INET_Addr::set_port_number (u_short port_number,
this->inet_addr_.in6_.sin6_port = port_number;
else
#endif /* ACE_HAS_IPV6 */
this->inet_addr_.in4_.sin_port = port_number;
if (this->inet_addrs_.empty ())
return;
for (std::vector<union ip46>::iterator i = this->inet_addrs_.begin ();
i != this->inet_addrs_.end ();
i++)
{
#if defined (ACE_HAS_IPV6)
if (this->get_type () == AF_INET6)
i->in6_.sin6_port = port_number;
else
#endif /* ACE_HAS_IPV6 */
i->in4_.sin_port = port_number;
}
this->inet_addr_.in4_.sin_port = port_number;
}
// returns -2 when the hostname is truncated
@ -882,19 +847,64 @@ ACE_INET_Addr::get_host_name_i (char hostname[], size_t len) const
#else
if (this->inet_addr_.in4_.sin_addr.s_addr == INADDR_ANY)
#endif /* ACE_HAS_IPV6 */
return (ACE_OS::hostname (hostname, len) == -1) ? -1 : 0;
{
if (ACE_OS::hostname (hostname, len) == -1)
return -1;
else
return 0;
}
else
{
void* addr = this->ip_addr_pointer ();
int size = this->ip_addr_size ();
int type = this->get_type ();
const ACE_SOCKET_LEN addr_size =
#ifdef ACE_HAS_IPV6
(this->get_type () == PF_INET6) ? sizeof (sockaddr_in6) :
#endif
sizeof (sockaddr_in);
# if defined (ACE_HAS_IPV6) && defined (ACE_HAS_BROKEN_GETHOSTBYADDR_V4MAPPED)
// Most OS can not handle IPv6-mapped-IPv4 addresses (even
// though they are meant to) so map them back to IPv4 addresses
// before trying to resolve them
in_addr demapped_addr;
if (type == PF_INET6 &&
(this->is_ipv4_mapped_ipv6 () || this->is_ipv4_compat_ipv6 ()))
{
ACE_OS::memcpy (&demapped_addr.s_addr, &this->inet_addr_.in6_.sin6_addr.s6_addr[12], 4);
addr = &demapped_addr;
size = sizeof(demapped_addr);
type = PF_INET;
}
# endif /* ACE_HAS_IPV6 */
const int res = ACE_OS::getnameinfo ((const sockaddr *) this->get_addr (),
addr_size, hostname,
static_cast<ACE_SOCKET_LEN> (len),
0, 0, 0);
return (res == 0) ? 0 : -1;
int h_error; // Not the same as errno!
hostent hentry;
ACE_HOSTENT_DATA buf;
hostent * const hp =
ACE_OS::gethostbyaddr_r (static_cast <char *> (addr),
size,
type,
&hentry,
buf,
&h_error);
if (hp == 0 || hp->h_name == 0)
return -1;
if (ACE_OS::strlen (hp->h_name) >= len)
{
// We know the length, so use memcpy
if (len > 0)
{
ACE_OS::memcpy (hostname, hp->h_name, len - 1);
hostname[len-1]= '\0';
}
errno = ENOSPC;
return -2; // -2 Means that we have a good string
// Using errno looks ok, but ENOSPC could be set on
// other places.
}
ACE_OS::strcpy (hostname, hp->h_name);
return 0;
}
}
int ACE_INET_Addr::set_address (const char *ip_addr,
@ -961,20 +971,17 @@ int ACE_INET_Addr::set_address (const char *ip_addr,
sizeof (ip6));
return 0;
}
else
{
// Build up a 128 bit address. An IPv4-mapped IPv6 address
// is defined as 0:0:0:0:0:ffff:IPv4_address. This is defined
// in RFC 1884 */
ACE_OS::memset (&this->inet_addr_.in6_.sin6_addr, 0, 16);
this->inet_addr_.in6_.sin6_addr.s6_addr[10] =
this->inet_addr_.in6_.sin6_addr.s6_addr[11] = 0xff;
ACE_OS::memcpy
(&this->inet_addr_.in6_.sin6_addr.s6_addr[12], &ip4, 4);
}
// Build up a 128 bit address. An IPv4-mapped IPv6 address
// is defined as 0:0:0:0:0:ffff:IPv4_address. This is defined
// in RFC 1884 */
ACE_OS::memset (&this->inet_addr_.in6_.sin6_addr, 0, 16);
this->inet_addr_.in6_.sin6_addr.s6_addr[10] =
this->inet_addr_.in6_.sin6_addr.s6_addr[11] = 0xff;
ACE_OS::memcpy
(&this->inet_addr_.in6_.sin6_addr.s6_addr[12], &ip4, 4);
}
#endif /* ACE_HAS_IPV6 */
return 0;
} /* end if (len == 4) */
#if defined (ACE_HAS_IPV6)
@ -992,6 +999,7 @@ int ACE_INET_Addr::set_address (const char *ip_addr,
this->inet_addr_.in6_.sin6_len = sizeof (this->inet_addr_.in6_);
#endif
ACE_OS::memcpy (&this->inet_addr_.in6_.sin6_addr, ip_addr, len);
return 0;
} /* end len == 16 */
#endif /* ACE_HAS_IPV6 */
@ -1050,11 +1058,12 @@ ACE_INET_Addr::get_host_addr (char *dst, int size) const
//}
# if defined (ACE_WIN32)
sockaddr *sa = reinterpret_cast<sockaddr *>
(const_cast<sockaddr_in6 *> (&this->inet_addr_.in6_));
if (ACE_OS::getnameinfo (sa, this->get_size (), dst, size,
0, 0, // Don't want service name
NI_NUMERICHOST) == 0)
if (0 == ::getnameinfo (reinterpret_cast<const sockaddr*> (&this->inet_addr_.in6_),
this->get_size (),
dst,
size,
0, 0, // Don't want service name
NI_NUMERICHOST))
return dst;
ACE_OS::set_errno_to_wsa_last_error ();
return 0;
@ -1069,8 +1078,7 @@ ACE_INET_Addr::get_host_addr (char *dst, int size) const
this->inet_addr_.in6_.sin6_scope_id != 0)
{
char scope_buf[32];
ACE_OS::snprintf (scope_buf, 32, "%%%u",
this->inet_addr_.in6_.sin6_scope_id);
ACE_OS::sprintf (scope_buf, "%%%u", this->inet_addr_.in6_.sin6_scope_id);
if ((ACE_OS::strlen (ch)+ACE_OS::strlen (scope_buf)) < (size_t)size)
{
ACE_OS::strcat (dst, scope_buf);