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.

00023                : INET_Addr.cpp 84183 2009-01-19 08:50:16Z johnnyw $")
00024 
00025 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00026 
00027 ACE_ALLOC_HOOK_DEFINE(ACE_INET_Addr)
00028 
00029 // Transform the current address into string format.
00030 
00031 int
00032 ACE_INET_Addr::addr_to_string (ACE_TCHAR s[],
00033                                size_t size,
00034                                int ipaddr_format) const
00035 {
00036   ACE_TRACE ("ACE_INET_Addr::addr_to_string");
00037 
00038   // XXX Can we (should we) include the scope id for IPv6 addresses?
00039   char hoststr[MAXHOSTNAMELEN+1];
00040 
00041   bool result = false;
00042   if (ipaddr_format == 0)
00043     result = (this->get_host_name (hoststr, MAXHOSTNAMELEN+1) == 0);
00044   else
00045     result = (this->get_host_addr (hoststr, MAXHOSTNAMELEN+1) != 0);
00046 
00047   if (!result)
00048     return -1;
00049 
00050   size_t total_len =
00051     ACE_OS::strlen (hoststr)
00052     + 5 // ACE_OS::strlen ("65535"), Assuming the max port number.
00053     + 1 // sizeof (':'), addr/port sep
00054     + 1; // sizeof ('\0'), terminating NUL
00055 #if !defined (ACE_WIN32) && defined (ACE_USES_WCHAR)
00056   ACE_TCHAR const *format = ACE_TEXT("%ls:%d");
00057 #else
00058   ACE_TCHAR const *format = ACE_TEXT("%s:%d");
00059 #endif /* !ACE_WIN32 && ACE_USES_WCHAR */
00060 #if defined (ACE_HAS_IPV6)
00061   if (ACE_OS::strchr (hoststr, ACE_TEXT (':')) != 0)
00062     {
00063       total_len += 2; // ACE_OS::strlen ("[]") IPv6 addr frames
00064 #  if !defined (ACE_WIN32) && defined (ACE_USES_WCHAR)
00065       format = ACE_TEXT("[%ls]:%d");
00066 #  else
00067       format = ACE_TEXT("[%s]:%d");
00068 #  endif /* !ACE_WIN32 && ACE_USES_WCHAR */
00069     }
00070 #endif // ACE_HAS_IPV6
00071 
00072   if (size < total_len)
00073     return -1;
00074   else
00075       ACE_OS::sprintf (s, format,
00076                        ACE_TEXT_CHAR_TO_TCHAR (hoststr),
00077                        this->get_port_number ());
00078   return 0;
00079 }

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

Definition at line 431 of file INET_Addr.cpp.

00433 {
00434   // Maybe port_name is directly a port number?
00435   char *endp = 0;
00436   long port_number = ACE_OS::strtol (port_name, &endp, 10);
00437 
00438   if (*endp == '\0')
00439     {
00440       // port_name was really a number, and nothing else.
00441 
00442       // Check for overflow.
00443       if (port_number < 0 || port_number > ACE_MAX_DEFAULT_PORT)
00444         return -1;
00445 
00446       // Return the port number.  NOTE: this number must
00447       // be returned in network byte order!
00448       u_short n = static_cast<u_short> (port_number);
00449       n = ACE_HTONS (n);
00450       return n;
00451     }
00452 
00453   // We try to resolve port number from its name.
00454 
00455 #if defined (ACE_LACKS_GETSERVBYNAME)
00456   port_number = 0;
00457   ACE_UNUSED_ARG (port_name);
00458   ACE_UNUSED_ARG (protocol);
00459 #else
00460   port_number = -1;
00461   servent sentry;
00462   ACE_SERVENT_DATA buf;
00463   servent *sp = ACE_OS::getservbyname_r (port_name,
00464                                          protocol,
00465                                          &sentry,
00466                                          buf);
00467   if (sp != 0)
00468     port_number = sp->s_port;
00469 #endif /* ACE_LACKS_GETSERVBYNAME */
00470 
00471   return port_number;
00472 }

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Sat Nov 21 23:12:31 2009 for ACE by  doxygen 1.6.1