Public Member Functions | Public Attributes | Private Member Functions | Private Attributes

ACE_INET_Addr Class Reference

Defines a C++ wrapper facade for the Internet domain address family format. More...

#include <INET_Addr.h>

Inheritance diagram for ACE_INET_Addr:
Inheritance graph
[legend]
Collaboration diagram for ACE_INET_Addr:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 ACE_INET_Addr (void)
 Default constructor.
 ACE_INET_Addr (const ACE_INET_Addr &)
 Copy constructor.
 ACE_INET_Addr (const sockaddr_in *addr, int len)
 Creates an ACE_INET_Addr from a sockaddr_in structure.
 ACE_INET_Addr (u_short port_number, const char host_name[], int address_family=AF_UNSPEC)
 ACE_INET_Addr (const char address[], int address_family=AF_UNSPEC)
 ACE_INET_Addr (u_short port_number, ACE_UINT32 ip_addr=INADDR_ANY)
 ACE_INET_Addr (const char port_name[], const char host_name[], const char protocol[]="tcp")
 ACE_INET_Addr (const char port_name[], ACE_UINT32 ip_addr, const char protocol[]="tcp")
 ~ACE_INET_Addr (void)
 Default dtor.
int set (const ACE_INET_Addr &)
 Initializes from another ACE_INET_Addr.
int set (u_short port_number, const char host_name[], int encode=1, int address_family=AF_UNSPEC)
int set (u_short port_number, ACE_UINT32 ip_addr=INADDR_ANY, int encode=1, int map=0)
int set (const char port_name[], const char host_name[], const char protocol[]="tcp")
int set (const char port_name[], ACE_UINT32 ip_addr, const char protocol[]="tcp")
int set (const char addr[], int address_family=AF_UNSPEC)
int set (const sockaddr_in *, int len)
 Creates an ACE_INET_Addr from a sockaddr_in structure.
virtual void * get_addr (void) const
 Return a pointer to the underlying network address.
int get_addr_size (void) const
virtual void set_addr (void *, int len)
 Set a pointer to the address.
virtual void set_addr (void *, int len, int map)
 Set a pointer to the address.
virtual int addr_to_string (ACE_TCHAR buffer[], size_t size, int ipaddr_format=1) const
virtual int string_to_addr (const char address[], int address_family=AF_UNSPEC)
void set_port_number (u_short, int encode=1)
int set_address (const char *ip_addr, int len, int encode=1, int map=0)
u_short get_port_number (void) const
 Return the port number, converting it into host byte-order.
int get_host_name (char hostname[], size_t hostnamelen) const
const char * get_host_name (void) const
const char * get_host_addr (char *addr, int addr_size) const
const char * get_host_addr (void) const
ACE_UINT32 get_ip_address (void) const
bool is_any (void) const
 Return true if the IP address is INADDR_ANY or IN6ADDR_ANY.
bool is_loopback (void) const
 Return true if the IP address is IPv4/IPv6 loopback address.
bool is_multicast (void) const
 Return true if the IP address is IPv4/IPv6 multicast address.
bool operator< (const ACE_INET_Addr &rhs) const
bool operator== (const ACE_INET_Addr &SAP) const
bool operator!= (const ACE_INET_Addr &SAP) const
 Compare two addresses for inequality.
bool is_ip_equal (const ACE_INET_Addr &SAP) const
virtual u_long hash (void) const
 Computes and returns hash value.
void dump (void) const
 Dump the state of an object.

Public Attributes

 ACE_ALLOC_HOOK_DECLARE
 Declare the dynamic allocation hooks.
sockaddr_in in4_

Private Member Functions

int get_host_name_i (char hostname[], size_t hostnamelen) const
 Insure that hostname is properly null-terminated.
void * ip_addr_pointer (void) const
int ip_addr_size (void) const
int determine_type (void) const
void reset (void)
 Initialize underlying inet_addr_ to default values.

Private Attributes

union {
   sockaddr_in   in4_
inet_addr_

Detailed Description

Defines a C++ wrapper facade for the Internet domain address family format.

Definition at line 33 of file INET_Addr.h.


Constructor & Destructor Documentation

ACE_INET_Addr::ACE_INET_Addr ( void   ) 

Default constructor.

Definition at line 160 of file INET_Addr.cpp.

  : ACE_Addr (determine_type (), sizeof (inet_addr_))
{
  // ACE_TRACE ("ACE_INET_Addr::ACE_INET_Addr");
  this->reset ();
}

ACE_INET_Addr::ACE_INET_Addr ( const ACE_INET_Addr sa  ) 

Copy constructor.

Definition at line 291 of file INET_Addr.cpp.

  : ACE_Addr (sa.get_type (), sa.get_size())
{
  ACE_TRACE ("ACE_INET_Addr::ACE_INET_Addr");
  this->reset ();
  this->set (sa);
}

ACE_INET_Addr::ACE_INET_Addr ( const sockaddr_in *  addr,
int  len 
)

Creates an ACE_INET_Addr from a sockaddr_in structure.

Definition at line 642 of file INET_Addr.cpp.

  : ACE_Addr (determine_type (), sizeof (inet_addr_))
{
  ACE_TRACE ("ACE_INET_Addr::ACE_INET_Addr");
  this->reset ();
  this->set (addr, len);
}

ACE_INET_Addr::ACE_INET_Addr ( u_short  port_number,
const char  host_name[],
int  address_family = AF_UNSPEC 
)

Creates an ACE_INET_Addr from a port_number and the remote host_name. The port number is assumed to be in host byte order. To set a port already in network byte order, please

See also:
set(). Use address_family to select IPv6 (PF_INET6) vs. IPv4 (PF_INET).

Definition at line 523 of file INET_Addr.cpp.

  : ACE_Addr (determine_type (), sizeof (inet_addr_))
{
  ACE_TRACE ("ACE_INET_Addr::ACE_INET_Addr");
  ACE_OS::memset (&this->inet_addr_, 0, sizeof (this->inet_addr_));
  if (this->set (port_number,
                 host_name,
                 1,
                 address_family) == -1)
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("ACE_INET_Addr::ACE_INET_Addr: %p\n"),
                ACE_TEXT_CHAR_TO_TCHAR ((host_name == 0) ?
                                        "<unknown>" : host_name)));
}

ACE_INET_Addr::ACE_INET_Addr ( const char  address[],
int  address_family = AF_UNSPEC 
) [explicit]

Initializes an ACE_INET_Addr from the address, which can be "ip-number:port-number" (e.g., "tango.cs.wustl.edu:1234" or "128.252.166.57:1234"). If there is no ':' in the address it is assumed to be a port number, with the IP address being INADDR_ANY.

Definition at line 270 of file INET_Addr.cpp.

  : ACE_Addr (determine_type (), sizeof (inet_addr_))
{
  ACE_TRACE ("ACE_INET_Addr::ACE_INET_Addr");
  this->reset ();
  this->set (address, address_family);
}

ACE_INET_Addr::ACE_INET_Addr ( u_short  port_number,
ACE_UINT32  ip_addr = INADDR_ANY 
) [explicit]

Creates an ACE_INET_Addr from a port_number and an Internet ip_addr. This method assumes that port_number and ip_addr are in host byte order. If you have addressing information in network byte order,

See also:
set().

Definition at line 652 of file INET_Addr.cpp.

  : 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)
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("%p\n"),
                ACE_TEXT ("ACE_INET_Addr::ACE_INET_Addr")));
}

ACE_INET_Addr::ACE_INET_Addr ( const char  port_name[],
const char  host_name[],
const char  protocol[] = "tcp" 
)

Uses getservbyname() to create an ACE_INET_Addr from a port_name, the remote host_name, and the protocol.

Definition at line 667 of file INET_Addr.cpp.

  : 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)
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("ACE_INET_Addr::ACE_INET_Addr")));
}

ACE_INET_Addr::ACE_INET_Addr ( const char  port_name[],
ACE_UINT32  ip_addr,
const char  protocol[] = "tcp" 
)

Uses getservbyname() to create an ACE_INET_Addr from a port_name, an Internet ip_addr, and the protocol. This method assumes that ip_addr is in host byte order.

Definition at line 699 of file INET_Addr.cpp.

  : 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)
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("ACE_INET_Addr::ACE_INET_Addr")));
}

ACE_INET_Addr::~ACE_INET_Addr ( void   ) 

Default dtor.

Definition at line 729 of file INET_Addr.cpp.

{
}


Member Function Documentation

virtual int ACE_INET_Addr::addr_to_string ( ACE_TCHAR  buffer[],
size_t  size,
int  ipaddr_format = 1 
) const [virtual]

Transform the current ACE_INET_Addr address into string format. If ipaddr_format is ttrue this produces "ip-number:port-number" (e.g., "128.252.166.57:1234"), whereas if ipaddr_format is false this produces "ip-name:port-number" (e.g., "tango.cs.wustl.edu:1234"). Returns -1 if the size of the buffer is too small, else 0.

int ACE_INET_Addr::determine_type ( void   )  const [inline, private]

Definition at line 35 of file INET_Addr.inl.

{
#if defined (ACE_HAS_IPV6)
#  if defined (ACE_USES_IPV4_IPV6_MIGRATION)
  return ACE::ipv6_enabled () ? AF_INET6 : AF_INET;
#  else
  return AF_INET6;
#  endif /* ACE_USES_IPV4_IPV6_MIGRATION */
#else
  return AF_INET;
#endif /* ACE_HAS_IPV6 */
}

void ACE_INET_Addr::dump ( void   )  const

Dump the state of an object.

Reimplemented from ACE_Addr.

Definition at line 82 of file INET_Addr.cpp.

{
#if defined (ACE_HAS_DUMP)
  ACE_TRACE ("ACE_INET_Addr::dump");

  ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));

  ACE_TCHAR s[ACE_MAX_FULLY_QUALIFIED_NAME_LEN + 16];
  this->addr_to_string(s, ACE_MAX_FULLY_QUALIFIED_NAME_LEN + 16);
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%s"), s));
  ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
#endif /* ACE_HAS_DUMP */
}

void * ACE_INET_Addr::get_addr ( void   )  const [virtual]

Return a pointer to the underlying network address.

Reimplemented from ACE_Addr.

Definition at line 595 of file INET_Addr.cpp.

{
  ACE_TRACE ("ACE_INET_Addr::get_addr");
  return (void*)&this->inet_addr_;
}

int ACE_INET_Addr::get_addr_size ( void   )  const [inline]

Definition at line 102 of file INET_Addr.inl.

{
  ACE_TRACE ("ACE_INET_Addr::get_addr_size");
#if defined (ACE_HAS_IPV6)
  if (this->get_type () == PF_INET)
    return sizeof this->inet_addr_.in4_;
  else
    return sizeof this->inet_addr_.in6_;
#else
  return sizeof this->inet_addr_.in4_;
#endif /* ACE_HAS_IPV6 */
}

const char * ACE_INET_Addr::get_host_addr ( char *  addr,
int  addr_size 
) const

Return the "dotted decimal" Internet address representation of the hostname storing it in the addr (which is assumed to be addr_size bytes long). This version is reentrant.

Definition at line 1048 of file INET_Addr.cpp.

{
#if defined (ACE_HAS_IPV6)
  if (this->get_type () == AF_INET6)
    {
      // mcorino@remedy.nl - Aug-26, 2005
      // I don't think this should be done because it results in a decimal address
      // representation which is not distinguishable from the IPv4 form which makes
      // it impossible to resolve back to an IPv6 INET_Addr without prior knowledge
      // that this was such an address to begin with.

      //if (IN6_IS_ADDR_V4MAPPED (&this->inet_addr_.in6_.sin6_addr))
      //{
      //  ACE_UINT32 addr;
      //  addr = this->get_ip_address();
      //  addr = ACE_HTONL (addr);
      //  return ACE_OS::inet_ntop (AF_INET, &addr, dst, size);
      //}

#  if defined (ACE_WIN32)
      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;
#  else
      const char *ch = ACE_OS::inet_ntop (AF_INET6,
                                          &this->inet_addr_.in6_.sin6_addr,
                                          dst,
                                          size);
#if defined (__linux__)
      if ((IN6_IS_ADDR_LINKLOCAL (&this->inet_addr_.in6_.sin6_addr) ||
           IN6_IS_ADDR_MC_LINKLOCAL (&this->inet_addr_.in6_.sin6_addr)) &&
          this->inet_addr_.in6_.sin6_scope_id != 0)
        {
          char scope_buf[32];
          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);
            }
        }
#endif
      return ch;
#  endif /* ACE_WIN32 */
    }
#endif /* ACE_HAS_IPV6 */

  return ACE_OS::inet_ntop (AF_INET,
          &this->inet_addr_.in4_.sin_addr,
          dst,
          size);
}

const char * ACE_INET_Addr::get_host_addr ( void   )  const

Return the "dotted decimal" Internet address representation of the hostname. This version is non-reentrant since it returns a pointer to a static data area. You should therefore either (1) do a "deep copy" of the address returned by get_host_addr(), e.g., using strdup() or (2) use the "reentrant" version of get_host_addr() described above.

Definition at line 1108 of file INET_Addr.cpp.

{
  ACE_TRACE ("ACE_INET_Addr::get_host_addr");
#if defined (ACE_HAS_IPV6)
  static char buf[INET6_ADDRSTRLEN];
  return this->get_host_addr (buf, INET6_ADDRSTRLEN);
#else /* ACE_HAS_IPV6 */
  static char buf[INET_ADDRSTRLEN];
  return this->get_host_addr (buf, INET_ADDRSTRLEN);
#endif /* !ACE_HAS_IPV6 */
}

int ACE_INET_Addr::get_host_name ( char  hostname[],
size_t  hostnamelen 
) const

Return the character representation of the name of the host, storing it in the hostname (which is assumed to be hostnamelen bytes long). This version is reentrant. If hostnamelen is greater than 0 then hostname will be NUL-terminated even if -1 is returned.

Definition at line 734 of file INET_Addr.cpp.

{
  ACE_TRACE ("ACE_INET_Addr::get_host_name");

  int result;
  if (len > 1)
    {
      result = this->get_host_name_i (hostname,len);
      if (result < 0)
        {
          if (result == -2)
            // We know that hostname is nul-terminated
            result = -1;
          else
            {
              //result == -1;
              // This could be worse than hostname[len -1] = '\0'?
              hostname[0] = '\0';
            }
        }
    }
  else
    {
      if (len == 1)
        hostname[0] = '\0';
      result = -1;
    }

  return result;
}

const char * ACE_INET_Addr::get_host_name ( void   )  const

Return the character representation of the hostname. This version is non-reentrant since it returns a pointer to a static data area. You should therefore either (1) do a "deep copy" of the address returned by get_host_name(), e.g., using strdup() or (2) use the "reentrant" version of get_host_name() described above.

Definition at line 794 of file INET_Addr.cpp.

{
  ACE_TRACE ("ACE_INET_Addr::get_host_name");

  static char name[MAXHOSTNAMELEN + 1];
  if (this->get_host_name (name, MAXHOSTNAMELEN + 1) == -1)
    ACE_OS::strcpy (name, "<unknown>");
  return name;
}

int ACE_INET_Addr::get_host_name_i ( char  hostname[],
size_t  hostnamelen 
) const [private]

Insure that hostname is properly null-terminated.

Definition at line 823 of file INET_Addr.cpp.

{
  ACE_TRACE ("ACE_INET_Addr::get_host_name_i");

#if defined (ACE_HAS_IPV6)
  if ((this->get_type () == PF_INET6 &&
       0 == ACE_OS::memcmp (&this->inet_addr_.in6_.sin6_addr,
                            &in6addr_any,
                            sizeof (this->inet_addr_.in6_.sin6_addr)))
      ||
      (this->get_type () == PF_INET &&
       this->inet_addr_.in4_.sin_addr.s_addr == INADDR_ANY))
#else
  if (this->inet_addr_.in4_.sin_addr.s_addr == INADDR_ANY)
#endif /* ACE_HAS_IPV6 */
    {
      if (ACE_OS::hostname (hostname, len) == -1)
        return -1;
      else
        return 0;
    }
  else
    {
#if defined (ACE_VXWORKS) && defined (ACE_LACKS_GETHOSTBYADDR)
      ACE_UNUSED_ARG (len);
      int error =
        ::hostGetByAddr ((int) this->inet_addr_.in4_.sin_addr.s_addr,
                         hostname);
      if (error == OK)
        return 0;
      else
        {
          errno = error;
          return -1;
        }
#else
      void* addr = this->ip_addr_pointer ();
      int   size = this->ip_addr_size ();
      int   type = this->get_type ();

#  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 */

#  if defined (DIGITAL_UNIX) && defined (__GNUC__)
      hostent * const hp =
        ACE_OS::gethostbyaddr (static_cast <char *> (addr), size, type);
#  else
      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);
#  endif /* DIGITAL_UNIX */

      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;
#endif /* ACE_VXWORKS */
    }
}

ACE_UINT32 ACE_INET_Addr::get_ip_address ( void   )  const

Return the 4-byte IP address, converting it into host byte order.

Definition at line 1122 of file INET_Addr.cpp.

{
  ACE_TRACE ("ACE_INET_Addr::get_ip_address");
#if defined (ACE_HAS_IPV6)
  if (this->get_type () == AF_INET6)
    {
      if (IN6_IS_ADDR_V4MAPPED (&this->inet_addr_.in6_.sin6_addr) ||
          IN6_IS_ADDR_V4COMPAT (&this->inet_addr_.in6_.sin6_addr)    )
        {
          ACE_UINT32 addr;
          // Return the last 32 bits of the address
          char *thisaddrptr = (char*)this->ip_addr_pointer ();
          thisaddrptr += 128/8 - 32/8;
          ACE_OS::memcpy (&addr, thisaddrptr, sizeof (addr));
          return ACE_NTOHL (addr);
        }

      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("ACE_INET_Addr::get_ip_address: address is a IPv6 address not IPv4\n")));
      errno = EAFNOSUPPORT;
      return 0;
    }
#endif /* ACE_HAS_IPV6 */
  return ACE_NTOHL (ACE_UINT32 (this->inet_addr_.in4_.sin_addr.s_addr));
}

u_short ACE_INET_Addr::get_port_number ( void   )  const [inline]

Return the port number, converting it into host byte-order.

Definition at line 88 of file INET_Addr.inl.

{
  ACE_TRACE ("ACE_INET_Addr::get_port_number");
#if defined (ACE_HAS_IPV6)
  if (this->get_type () == PF_INET)
    return ACE_NTOHS (this->inet_addr_.in4_.sin_port);
  else
    return ACE_NTOHS (this->inet_addr_.in6_.sin6_port);
#else
  return ACE_NTOHS (this->inet_addr_.in4_.sin_port);
#endif /* ACE_HAS_IPV6 */
}

u_long ACE_INET_Addr::hash ( void   )  const [virtual]

Computes and returns hash value.

Reimplemented from ACE_Addr.

Definition at line 147 of file INET_Addr.cpp.

{
#if defined (ACE_HAS_IPV6)
  if (this->get_type () == PF_INET6)
    {
      const unsigned int *addr = (const unsigned int*)this->ip_addr_pointer();
      return addr[0] + addr[1] + addr[2] + addr[3] + this->get_port_number();
    }
  else
#endif /* ACE_HAS_IPV6 */
  return this->get_ip_address () + this->get_port_number ();
}

void * ACE_INET_Addr::ip_addr_pointer ( void   )  const [inline, private]

Definition at line 49 of file INET_Addr.inl.

{
#if defined (ACE_HAS_IPV6)
  if (this->get_type () == PF_INET)
    return (void*)&this->inet_addr_.in4_.sin_addr;
  else
    return (void*)&this->inet_addr_.in6_.sin6_addr;
#else
  return (void*)&this->inet_addr_.in4_.sin_addr;
#endif
}

int ACE_INET_Addr::ip_addr_size ( void   )  const [inline, private]

Definition at line 62 of file INET_Addr.inl.

{
  // Since this size value is used to pass to other host db-type
  // functions (gethostbyaddr, etc.) the length is of int type.
  // Thus, cast all these sizes back to int. They're all well
  // within the range of an int anyway.
#if defined (ACE_HAS_IPV6)
  if (this->get_type () == PF_INET)
    return static_cast<int> (sizeof this->inet_addr_.in4_.sin_addr);
  else
    return static_cast<int> (sizeof this->inet_addr_.in6_.sin6_addr);
#else
  // These _UNICOS changes were picked up from pre-IPv6 code in
  // get_host_name_i... the IPv6 section above may need something
  // similar, so keep an eye out for it.
#  if !defined(_UNICOS)
  return static_cast<int> (sizeof this->inet_addr_.in4_.sin_addr.s_addr);
#  else /* _UNICOS */
  return static_cast<int> (sizeof this->inet_addr_.in4_.sin_addr);
#  endif /* ! _UNICOS */
#endif /* ACE_HAS_IPV6 */
}

bool ACE_INET_Addr::is_any ( void   )  const [inline]

Return true if the IP address is INADDR_ANY or IN6ADDR_ANY.

Definition at line 187 of file INET_Addr.inl.

{
#if defined (ACE_HAS_IPV6)
  if (this->get_type () == AF_INET6)
      return IN6_IS_ADDR_UNSPECIFIED (&this->inet_addr_.in6_.sin6_addr);
#endif /* ACE_HAS_IPV6 */

  return (this->inet_addr_.in4_.sin_addr.s_addr == INADDR_ANY);
}

bool ACE_INET_Addr::is_ip_equal ( const ACE_INET_Addr SAP  )  const

A variation of the equality operator, this method only compares the IP address and ignores the port number.

Definition at line 122 of file INET_Addr.cpp.

{
    if (this->get_type () != sap.get_type ()
      || this->get_size () != sap.get_size ())
    return false;

#if defined (ACE_HAS_IPV6)
  if (this->get_type () == PF_INET6)
    {
      const unsigned int *addr =
        reinterpret_cast<const unsigned int*>(this->ip_addr_pointer());
      const unsigned int *saddr =
        reinterpret_cast<const unsigned int*>(sap.ip_addr_pointer());
      return (addr[0] == saddr[0] &&
              addr[1] == saddr[1] &&
              addr[2] == saddr[2] &&
              addr[3] == saddr[3]);
    }
  else
#endif /* ACE_HAS_IPV6 */
  return this->get_ip_address () == sap.get_ip_address();
}

bool ACE_INET_Addr::is_loopback ( void   )  const [inline]

Return true if the IP address is IPv4/IPv6 loopback address.

Definition at line 199 of file INET_Addr.inl.

{
#if defined (ACE_HAS_IPV6)
  if (this->get_type () == AF_INET6)
      return IN6_IS_ADDR_LOOPBACK (&this->inet_addr_.in6_.sin6_addr);
#endif /* ACE_HAS_IPV6 */

  // RFC 3330 defines loopback as any address with 127.x.x.x
  return ((this->get_ip_address () & 0XFF000000) == (INADDR_LOOPBACK & 0XFF000000));
}

bool ACE_INET_Addr::is_multicast ( void   )  const [inline]

Return true if the IP address is IPv4/IPv6 multicast address.

Definition at line 212 of file INET_Addr.inl.

{
#if defined (ACE_HAS_IPV6)
  if (this->get_type() == AF_INET6)
    return this->inet_addr_.in6_.sin6_addr.s6_addr[0] == 0xFF;
#endif /* ACE_HAS_IPV6 */
  return
    (*static_cast<const unsigned char*> (
        static_cast<const void*> (&this->inet_addr_.in4_.sin_addr.s_addr)) & 0xf0) == 0xe0;
}

bool ACE_INET_Addr::operator!= ( const ACE_INET_Addr SAP  )  const

Compare two addresses for inequality.

Definition at line 99 of file INET_Addr.cpp.

{
  ACE_TRACE ("ACE_INET_Addr::operator !=");
  return !((*this) == sap);
}

bool ACE_INET_Addr::operator< ( const ACE_INET_Addr rhs  )  const [inline]

Returns true if this is less than rhs. In this context, "less than" is defined in terms of IP address and TCP port number. This operator makes it possible to use ACE_INET_Addrs in STL maps.

Definition at line 116 of file INET_Addr.inl.

{
#if defined (ACE_HAS_IPV6)
  if (this->get_type() != rhs.get_type())
  {
    return this->get_type() < rhs.get_type();
  }

  if (this->get_type() == PF_INET6)
  {
    int memval = ACE_OS::memcmp (this->ip_addr_pointer(),
                                 rhs.ip_addr_pointer(),
                                 this->ip_addr_size());

    return memval < 0
            || (memval == 0
                && (this->get_port_number() < rhs.get_port_number()
                    || (this->get_port_number() == rhs.get_port_number()
                        && this->inet_addr_.in6_.sin6_scope_id <
                            rhs.inet_addr_.in6_.sin6_scope_id)));
  }
#endif

  return this->get_ip_address () < rhs.get_ip_address ()
    || (this->get_ip_address () == rhs.get_ip_address ()
        && this->get_port_number () < rhs.get_port_number ());
}

bool ACE_INET_Addr::operator== ( const ACE_INET_Addr SAP  )  const

Compare two addresses for equality. The addresses are considered equal if they contain the same IP address and port number.

Definition at line 108 of file INET_Addr.cpp.

{
  ACE_TRACE ("ACE_INET_Addr::operator ==");

  if (this->get_type () != sap.get_type ()
      || this->get_size () != sap.get_size ())
    return false;

  return (ACE_OS::memcmp (&this->inet_addr_,
                          &sap.inet_addr_,
                          this->get_size ()) == 0);
}

void ACE_INET_Addr::reset ( void   )  [inline, private]

Initialize underlying inet_addr_ to default values.

Definition at line 13 of file INET_Addr.inl.

{
  ACE_OS::memset (&this->inet_addr_, 0, sizeof (this->inet_addr_));
  if (this->get_type() == AF_INET)
    {
#ifdef ACE_HAS_SOCKADDR_IN_SIN_LEN
      this->inet_addr_.in4_.sin_len = sizeof (this->inet_addr_.in4_);
#endif
      this->inet_addr_.in4_.sin_family = AF_INET;
    }
#if defined (ACE_HAS_IPV6)
  else if (this->get_type() == AF_INET6)
    {
#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;
    }
#endif  /* ACE_HAS_IPV6 */
}

int ACE_INET_Addr::set ( const char  port_name[],
const char  host_name[],
const char  protocol[] = "tcp" 
)

Uses <getservbyname> to initialize an ACE_INET_Addr from a <port_name>, the remote host_name, and the protocol.

Definition at line 478 of file INET_Addr.cpp.

{
  ACE_TRACE ("ACE_INET_Addr::set");

  int const port_number = get_port_number_from_name (port_name, protocol);
  if (port_number == -1)
    {
      ACE_NOTSUP_RETURN (-1);
    }

  int address_family = PF_UNSPEC;
#  if defined (ACE_HAS_IPV6)
  if (ACE_OS::strcmp (protocol, "tcp6") == 0)
    address_family = AF_INET6;
#  endif /* ACE_HAS_IPV6 */

  return this->set (static_cast<u_short> (port_number),
                    host_name, 0, address_family);
}

int ACE_INET_Addr::set ( u_short  port_number,
ACE_UINT32  ip_addr = INADDR_ANY,
int  encode = 1,
int  map = 0 
)

Initializes an ACE_INET_Addr from a port_number and an Internet ip_addr. If encode is non-zero then the port number and IP address are converted into network byte order, otherwise they are assumed to be in network byte order already and are passed straight through.

If <map> is non-zero and IPv6 support has been compiled in, then this address will be set to the IPv4-mapped IPv6 address of it.

Definition at line 303 of file INET_Addr.cpp.

{
  ACE_TRACE ("ACE_INET_Addr::set");
  this->set_address (reinterpret_cast<const char *> (&inet_address),
                     sizeof inet_address,
                     encode, map);
  this->set_port_number (port_number, encode);

  return 0;
}

int ACE_INET_Addr::set ( u_short  port_number,
const char  host_name[],
int  encode = 1,
int  address_family = AF_UNSPEC 
)

Initializes an ACE_INET_Addr from a port_number and the remote host_name. If encode is non-zero then port_number is converted into network byte order, otherwise it is assumed to be in network byte order already and are passed straight through. address_family can be used to select IPv4/IPv6 if the OS has IPv6 capability (ACE_HAS_IPV6 is defined). To specify IPv6, use the value AF_INET6. To specify IPv4, use AF_INET.

Definition at line 322 of file INET_Addr.cpp.

{
  ACE_TRACE ("ACE_INET_Addr::set");

  // Yow, someone gave us a NULL host_name!
  if (host_name == 0)
    {
      errno = EINVAL;
      return -1;
    }

  ACE_OS::memset ((void *) &this->inet_addr_,
                  0,
                  sizeof this->inet_addr_);

#if defined (ACE_HAS_IPV6)
  struct addrinfo hints;
  struct addrinfo *res = 0;
  int error = 0;
  ACE_OS::memset (&hints, 0, sizeof (hints));
# if defined (ACE_USES_IPV4_IPV6_MIGRATION)
  if (address_family == AF_UNSPEC && !ACE::ipv6_enabled())
    address_family = AF_INET;
# endif /* ACE_USES_IPV4_IPV6_MIGRATION */
  if (address_family == AF_UNSPEC || address_family == AF_INET6)
    {
      hints.ai_family = AF_INET6;
      error = ::getaddrinfo (host_name, 0, &hints, &res);
      if (error)
        {
          if (address_family == AF_INET6)
            {
              if (res)
                ::freeaddrinfo(res);
              errno = error;
              return -1;
            }
          address_family = AF_INET;
        }
    }
  if (address_family == AF_INET)
    {
      hints.ai_family = AF_INET;
      error = ::getaddrinfo (host_name, 0, &hints, &res);
      if (error)
        {
          if (res)
            ::freeaddrinfo(res);
          errno = error;
          return -1;
        }
    }
  this->set_type (res->ai_family);
  this->set_addr (res->ai_addr, res->ai_addrlen);
  this->set_port_number (port_number, encode);
  ::freeaddrinfo (res);
  return 0;
#else /* ACE_HAS_IPV6 */

  // 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_);
#endif
  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
    {
#  if defined (ACE_VXWORKS) && defined (ACE_LACKS_GETHOSTBYNAME)
      hostent *hp = ACE_OS::gethostbyname (host_name);
#  else
      hostent hentry;
      ACE_HOSTENT_DATA buf;
      int h_error = 0;  // Not the same as errno!

      hostent *hp = ACE_OS::gethostbyname_r (host_name, &hentry,
                                             buf, &h_error);
      if (hp == 0)
        errno = h_error;
#  endif /* ACE_VXWORKS */

      if (hp == 0)
        {
          return -1;
        }
      else
        {
          (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);
        }
    }
#endif /* ACE_HAS_IPV6 */
}

int ACE_INET_Addr::set ( const char  port_name[],
ACE_UINT32  ip_addr,
const char  protocol[] = "tcp" 
)

Uses <getservbyname> to initialize an ACE_INET_Addr from a <port_name>, an ip_addr, and the protocol. This assumes that ip_addr is already in network byte order.

Definition at line 504 of file INET_Addr.cpp.

{
  ACE_TRACE ("ACE_INET_Addr::set");

  int const port_number = get_port_number_from_name (port_name, protocol);
  if (port_number == -1)
    {
      ACE_NOTSUP_RETURN (-1);
    }

  return this->set (static_cast<u_short> (port_number),
                    inet_address, 0);
}

int ACE_INET_Addr::set ( const ACE_INET_Addr sa  ) 

Initializes from another ACE_INET_Addr.

Definition at line 168 of file INET_Addr.cpp.

{
  ACE_TRACE ("ACE_INET_Addr::set");

  if (sa.get_type () == AF_ANY)
    // Ugh, this is really a base class, so don't copy it.
    ACE_OS::memset (&this->inet_addr_, 0, sizeof (this->inet_addr_));
  else
    {
      // It's ok to make the copy.
      ACE_OS::memcpy (&this->inet_addr_,
                      &sa.inet_addr_,
                      sa.get_size ());

      this->set_type (sa.get_type());
      this->set_size (sa.get_size());
    }

  return 0;
}

int ACE_INET_Addr::set ( const sockaddr_in *  addr,
int  len 
)

Creates an ACE_INET_Addr from a sockaddr_in structure.

Definition at line 563 of file INET_Addr.cpp.

{
  ACE_TRACE ("ACE_INET_Addr::set");

  if (addr->sin_family == AF_INET)
    {
      int maxlen = static_cast<int> (sizeof (this->inet_addr_.in4_));
      if (len > maxlen)
        len = maxlen;
      ACE_OS::memcpy (&this->inet_addr_.in4_, addr, len);
      this->base_set (AF_INET, len);
      return 0;
    }
#if defined (ACE_HAS_IPV6)
  else if (addr->sin_family == AF_INET6)
    {
      int maxlen = static_cast<int> (sizeof (this->inet_addr_.in6_));
      if (len > maxlen)
        len = maxlen;
      ACE_OS::memcpy (&this->inet_addr_.in6_, addr, len);
      this->base_set (AF_INET6, len);
      return 0;
    }
#endif /* ACE_HAS_IPV6 */

  errno = EAFNOSUPPORT;
  return -1;
}

int ACE_INET_Addr::set ( const char  addr[],
int  address_family = AF_UNSPEC 
)

Initializes an ACE_INET_Addr from the addr, which can be "ip-number:port-number" (e.g., "tango.cs.wustl.edu:1234" or "128.252.166.57:1234"). If there is no ':' in the address it is assumed to be a port number, with the IP address being INADDR_ANY.

Definition at line 264 of file INET_Addr.cpp.

{
  ACE_TRACE ("ACE_INET_Addr::set");
  return this->string_to_addr (address, address_family);
}

void ACE_INET_Addr::set_addr ( void *  addr,
int  len,
int  map 
) [virtual]

Set a pointer to the address.

Definition at line 609 of file INET_Addr.cpp.

{
  ACE_TRACE ("ACE_INET_Addr::set_addr");
  struct sockaddr_in *getfamily = static_cast<struct sockaddr_in *> (addr);

  if (getfamily->sin_family == AF_INET)
    {
#if defined (ACE_HAS_IPV6)
      if (map)
        this->set_type (AF_INET6);
      else
#endif /* ACE_HAS_IPV6 */
        this->set_type (AF_INET);
      this->set_port_number (getfamily->sin_port, 0);
      this->set_address (reinterpret_cast<const char*> (&getfamily->sin_addr),
                         sizeof (getfamily->sin_addr),
                         0, map);
    }
#if defined (ACE_HAS_IPV6)
  else if (getfamily->sin_family == AF_INET6)
    {
      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),
                         0);
      this->inet_addr_.in6_.sin6_scope_id = in6->sin6_scope_id;
    }
#endif // ACE_HAS_IPV6
}

void ACE_INET_Addr::set_addr ( void *  addr,
int  len 
) [virtual]

Set a pointer to the address.

Reimplemented from ACE_Addr.

Definition at line 602 of file INET_Addr.cpp.

{
  this->set_addr (addr, len, 0);
}

int ACE_INET_Addr::set_address ( const char *  ip_addr,
int  len,
int  encode = 1,
int  map = 0 
)

Sets the address without affecting the port number. If encode is enabled then ip_addr is converted into network byte order, otherwise it is assumed to be in network byte order already and are passed straight through. The size of the address is specified in the len parameter. If map is non-zero, IPv6 support has been compiled in, and ip_addr is an IPv4 address, then this address is set to the IPv4-mapped IPv6 address of it.

Definition at line 917 of file INET_Addr.cpp.

{
  ACE_TRACE ("ACE_INET_Addr::set_address");
  // This is really intended for IPv4. If the object is IPv4, or the type
  // hasn't been set but it's a 4-byte address, go ahead. If this is an
  // IPv6 object and <encode> is requested, refuse.
  if (encode && len != 4)
    {
      errno = EAFNOSUPPORT;
      return -1;
    }

  if (len == 4)
    {
      ACE_UINT32 ip4 = *reinterpret_cast<const ACE_UINT32 *> (ip_addr);
      if (encode)
        ip4 = ACE_HTONL (ip4);


      if (this->get_type () == AF_INET && map == 0) {
        this->base_set (AF_INET, sizeof (this->inet_addr_.in4_));
#ifdef ACE_HAS_SOCKADDR_IN_SIN_LEN
        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_));
        ACE_OS::memcpy (&this->inet_addr_.in4_.sin_addr,
                        &ip4,
                        len);
      }
#if defined (ACE_HAS_IPV6)
      else if (map == 0)
        {
          // this->set_type (AF_INET);
          this->base_set (AF_INET, sizeof (this->inet_addr_.in4_));
#ifdef ACE_HAS_SOCKADDR_IN_SIN_LEN
          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_));
          ACE_OS::memcpy (&this->inet_addr_.in4_.sin_addr,
                          &ip4, len);
        }
      // If given an IPv4 address to copy to an IPv6 object, map it to
      // an IPv4-mapped IPv6 address.
      else
        {
          this->base_set (AF_INET6, sizeof (this->inet_addr_.in6_));
#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_));
          if (ip4 == ACE_HTONL (INADDR_ANY))
            {
              in6_addr const ip6 = in6addr_any;
              ACE_OS::memcpy (&this->inet_addr_.in6_.sin6_addr,
                              &ip6,
                              sizeof (ip6));
              return 0;
            }

          // 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)
  else if (len == 16)
    {
      if (this->get_type () != PF_INET6)
        {
          errno = EAFNOSUPPORT;
          return -1;
        }
      // We protect ourselves up above so IPv6 must be possible here.
      this->base_set (AF_INET6, sizeof (this->inet_addr_.in6_));
      this->inet_addr_.in6_.sin6_family = AF_INET6;
#ifdef ACE_HAS_SOCKADDR_IN6_SIN6_LEN
      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 */

  // Here with an unrecognized length.
  errno = EAFNOSUPPORT;
  return -1;

}

void ACE_INET_Addr::set_port_number ( u_short  port_number,
int  encode = 1 
)

Sets the port number without affecting the host name. If encode is enabled then port_number is converted into network byte order, otherwise it is assumed to be in network byte order already and are passed straight through.

Reimplemented in ACE_Multihomed_INET_Addr.

Definition at line 805 of file INET_Addr.cpp.

{
  ACE_TRACE ("ACE_INET_Addr::set_port_number");

  if (encode)
    port_number = ACE_HTONS (port_number);

#if defined (ACE_HAS_IPV6)
  if (this->get_type () == AF_INET6)
    this->inet_addr_.in6_.sin6_port = port_number;
  else
#endif /* ACE_HAS_IPV6 */
  this->inet_addr_.in4_.sin_port = port_number;
}

int ACE_INET_Addr::string_to_addr ( const char  address[],
int  address_family = AF_UNSPEC 
) [virtual]

Initializes an ACE_INET_Addr from the address, which can be "ip-addr:port-number" (e.g., "tango.cs.wustl.edu:1234"), "ip-addr:port-name" (e.g., "tango.cs.wustl.edu:telnet"), "ip-number:port-number" (e.g., "128.252.166.57:1234"), or "ip-number:port-name" (e.g., "128.252.166.57:telnet"). If there is no ':' in the address it is assumed to be a port number, with the IP address being INADDR_ANY.

Definition at line 192 of file INET_Addr.cpp.

{
  ACE_TRACE ("ACE_INET_Addr::string_to_addr");
  int result;
  char *ip_buf = 0;
  char *ip_addr = 0;

  // Need to make a duplicate since we'll be overwriting the string.
  ACE_ALLOCATOR_RETURN (ip_buf,
                        ACE_OS::strdup (s),
                        -1);
  ip_addr = ip_buf;
  // We use strrchr because of IPv6 addresses.
  char *port_p = ACE_OS::strrchr (ip_addr, ':');
#if defined (ACE_HAS_IPV6)
  // Check for extended IPv6 format : '[' <ipv6 address> ']' ':' <port>
  if (ip_addr[0] == '[')
    {
      // find closing bracket
      char *cp_pos = ACE_OS::strchr (ip_addr, ']');
      // check for port separator after closing bracket
      // if not found leave it, error will come later
      if (cp_pos)
        {
          *cp_pos = '\0'; // blank out ']'
          ++ip_addr; // skip over '['
          if (cp_pos[1] == ':')
            port_p = cp_pos + 1;
          else
            port_p = cp_pos; // leads to error on missing port
        }
    }
#endif /* ACE_HAS_IPV6 */

  if (port_p == 0) // Assume it's a port number.
    {
      char *endp = 0;
      long const port = ACE_OS::strtol (ip_addr, &endp, 10);

      if (*endp == '\0')    // strtol scanned the entire string - all digits
        {
          if (port < 0 || port > ACE_MAX_DEFAULT_PORT)
            result = -1;
          else
            result = this->set (u_short (port), ACE_UINT32 (INADDR_ANY));
        }
      else // port name
        result = this->set (ip_addr, ACE_UINT32 (INADDR_ANY));
    }
  else
    {
      *port_p = '\0'; ++port_p; // skip over ':'

      char *endp = 0;
      long port = ACE_OS::strtol (port_p, &endp, 10);

      if (*endp == '\0')    // strtol scanned the entire string - all digits
        {
          if (port < 0 || port > ACE_MAX_DEFAULT_PORT)
            result = -1;
          else
            result = this->set (u_short (port), ip_addr, 1, address_family);
        }
      else
        result = this->set (port_p, ip_addr);
    }

  ACE_OS::free (ACE_MALLOC_T (ip_buf));
  return result;
}


Member Data Documentation

Declare the dynamic allocation hooks.

Reimplemented from ACE_Addr.

Definition at line 356 of file INET_Addr.h.

sockaddr_in ACE_INET_Addr::in4_

Definition at line 376 of file INET_Addr.h.

union { ... } ACE_INET_Addr::inet_addr_ [private]

Underlying representation. This union uses the knowledge that the two structures share the first member, sa_family (as all sockaddr structures do).


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines