Functions

INET_Addr.cpp File Reference

#include "ace/INET_Addr.h"
#include "ace/Log_Msg.h"
#include "ace/OS_NS_stdio.h"
#include "ace/OS_NS_errno.h"
#include "ace/OS_NS_stdlib.h"
#include "ace/OS_Memory.h"
#include "ace/OS_NS_arpa_inet.h"
#include "ace/OS_NS_netdb.h"
#include "ace/OS_NS_unistd.h"
#include "ace/OS_NS_sys_socket.h"
Include dependency graph for INET_Addr.cpp:

Go to the source code of this file.

Functions

 ACE_RCSID (ace, INET_Addr,"$Id: INET_Addr.cpp 84183 2009-01-19 08:50:16Z johnnyw $") 1int ACE_INET_Addr
static int get_port_number_from_name (const char port_name[], const char protocol[])

Function Documentation

ACE_RCSID ( ace  ,
INET_Addr  ,
"$Id: INET_Addr.cpp 84183 2009-01-19 08:50:16Z johnnyw $"   
)

Definition at line 21 of file INET_Addr.cpp.

               : INET_Addr.cpp 84183 2009-01-19 08:50:16Z johnnyw $")

ACE_BEGIN_VERSIONED_NAMESPACE_DECL

ACE_ALLOC_HOOK_DEFINE(ACE_INET_Addr)

// Transform the current address into string format.

int
ACE_INET_Addr::addr_to_string (ACE_TCHAR s[],
                               size_t size,
                               int ipaddr_format) const
{
  ACE_TRACE ("ACE_INET_Addr::addr_to_string");

  // XXX Can we (should we) include the scope id for IPv6 addresses?
  char hoststr[MAXHOSTNAMELEN+1];

  bool result = false;
  if (ipaddr_format == 0)
    result = (this->get_host_name (hoststr, MAXHOSTNAMELEN+1) == 0);
  else
    result = (this->get_host_addr (hoststr, MAXHOSTNAMELEN+1) != 0);

  if (!result)
    return -1;

  size_t total_len =
    ACE_OS::strlen (hoststr)
    + 5 // ACE_OS::strlen ("65535"), Assuming the max port number.
    + 1 // sizeof (':'), addr/port sep
    + 1; // sizeof ('\0'), terminating NUL
#if !defined (ACE_WIN32) && defined (ACE_USES_WCHAR)
  ACE_TCHAR const *format = ACE_TEXT("%ls:%d");
#else
  ACE_TCHAR const *format = ACE_TEXT("%s:%d");
#endif /* !ACE_WIN32 && ACE_USES_WCHAR */
#if defined (ACE_HAS_IPV6)
  if (ACE_OS::strchr (hoststr, ACE_TEXT (':')) != 0)
    {
      total_len += 2; // ACE_OS::strlen ("[]") IPv6 addr frames
#  if !defined (ACE_WIN32) && defined (ACE_USES_WCHAR)
      format = ACE_TEXT("[%ls]:%d");
#  else
      format = ACE_TEXT("[%s]:%d");
#  endif /* !ACE_WIN32 && ACE_USES_WCHAR */
    }
#endif // ACE_HAS_IPV6

  if (size < total_len)
    return -1;
  else
      ACE_OS::sprintf (s, format,
                       ACE_TEXT_CHAR_TO_TCHAR (hoststr),
                       this->get_port_number ());
  return 0;
}

static int get_port_number_from_name ( const char  port_name[],
const char  protocol[] 
) [static]

Definition at line 431 of file INET_Addr.cpp.

{
  // Maybe port_name is directly a port number?
  char *endp = 0;
  long port_number = ACE_OS::strtol (port_name, &endp, 10);

  if (*endp == '\0')
    {
      // port_name was really a number, and nothing else.

      // Check for overflow.
      if (port_number < 0 || port_number > ACE_MAX_DEFAULT_PORT)
        return -1;

      // Return the port number.  NOTE: this number must
      // be returned in network byte order!
      u_short n = static_cast<u_short> (port_number);
      n = ACE_HTONS (n);
      return n;
    }

  // We try to resolve port number from its name.

#if defined (ACE_LACKS_GETSERVBYNAME)
  port_number = 0;
  ACE_UNUSED_ARG (port_name);
  ACE_UNUSED_ARG (protocol);
#else
  port_number = -1;
  servent sentry;
  ACE_SERVENT_DATA buf;
  servent *sp = ACE_OS::getservbyname_r (port_name,
                                         protocol,
                                         &sentry,
                                         buf);
  if (sp != 0)
    port_number = sp->s_port;
#endif /* ACE_LACKS_GETSERVBYNAME */

  return port_number;
}

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines