Logo
~Sockets~
~Examples~
~Contact~


Ipv4Address Class Reference

#include <Ipv4Address.h>

Inheritance diagram for Ipv4Address:
Collaboration diagram for Ipv4Address:

List of all members.


Public Member Functions

 Ipv4Address (port_t port=0)
 Create empty Ipv4 address structure.
 Ipv4Address (ipaddr_t a, port_t port)
 Create Ipv4 address structure.
 Ipv4Address (struct in_addr &a, port_t port)
 Create Ipv4 address structure.
 Ipv4Address (const std::string &host, port_t port)
 Create Ipv4 address structure.
 Ipv4Address (struct sockaddr_in &)
 ~Ipv4Address ()
 operator struct sockaddr * ()
 Get a pointer to the address struct.
 operator socklen_t ()
 Get length of address struct.
bool operator== (SocketAddress &)
 Compare two addresses.
void SetPort (port_t port)
 Set port number.
port_t GetPort ()
 Get port number.
void SetAddress (struct sockaddr *sa)
 Set socket address.
int GetFamily ()
 Get address family.
bool IsValid ()
 Address structure is valid.
std::auto_ptr< SocketAddressGetCopy ()
 Get a copy of this SocketAddress object.
std::string Convert (bool include_port=false)
 Convert address struct to text.
std::string Reverse ()
 Reverse lookup of address.

Static Public Member Functions

static bool Resolve (const std::string &hostname, struct in_addr &a)
 Resolve hostname.
static bool Reverse (struct in_addr &a, std::string &name)
 Reverse resolve (IP to hostname).
static std::string Convert (struct in_addr &a)
 Convert address struct to text.

Private Member Functions

 Ipv4Address (const Ipv4Address &)
Ipv4Addressoperator= (const Ipv4Address &)

Private Attributes

struct sockaddr_in m_addr
bool m_valid

Detailed Description

Definition at line 47 of file Ipv4Address.h.


Constructor & Destructor Documentation

Ipv4Address::Ipv4Address ( port_t  port = 0  ) 

Create empty Ipv4 address structure.

Parameters:
port Port number

Definition at line 46 of file Ipv4Address.cpp.

References m_addr.

Referenced by GetCopy().

00046                                     : m_valid(true)
00047 {
00048         memset(&m_addr, 0, sizeof(m_addr));
00049         m_addr.sin_family = AF_INET;
00050         m_addr.sin_port = htons( port );
00051 }

Ipv4Address::Ipv4Address ( ipaddr_t  a,
port_t  port 
)

Create Ipv4 address structure.

Parameters:
a Socket address in network byte order (as returned by Utility::u2ip)
port Port number in host byte order

Definition at line 54 of file Ipv4Address.cpp.

References m_addr.

00054                                                : m_valid(true)
00055 {
00056         memset(&m_addr, 0, sizeof(m_addr));
00057         m_addr.sin_family = AF_INET;
00058         m_addr.sin_port = htons( port );
00059         memcpy(&m_addr.sin_addr, &a, sizeof(struct in_addr));
00060 }

Ipv4Address::Ipv4Address ( struct in_addr &  a,
port_t  port 
)

Create Ipv4 address structure.

Parameters:
a Socket address in network byte order
port Port number in host byte order

Definition at line 63 of file Ipv4Address.cpp.

References m_addr.

00063                                                       : m_valid(true)
00064 {
00065         memset(&m_addr, 0, sizeof(m_addr));
00066         m_addr.sin_family = AF_INET;
00067         m_addr.sin_port = htons( port );
00068         m_addr.sin_addr = a;
00069 }

Ipv4Address::Ipv4Address ( const std::string &  host,
port_t  port 
)

Create Ipv4 address structure.

Parameters:
host Hostname to be resolved
port Port number in host byte order

Definition at line 72 of file Ipv4Address.cpp.

References m_addr, m_valid, and Utility::u2ip().

00072                                                           : m_valid(false)
00073 {
00074         memset(&m_addr, 0, sizeof(m_addr));
00075         m_addr.sin_family = AF_INET;
00076         m_addr.sin_port = htons( port );
00077         {
00078                 ipaddr_t a;
00079                 if (Utility::u2ip(host, a))
00080                 {
00081                         memcpy(&m_addr.sin_addr, &a, sizeof(struct in_addr));
00082                         m_valid = true;
00083                 }
00084         }
00085 }

Ipv4Address::Ipv4Address ( struct sockaddr_in &  sa  ) 

Definition at line 88 of file Ipv4Address.cpp.

References m_addr, and m_valid.

00089 {
00090         m_addr = sa;
00091         m_valid = sa.sin_family == AF_INET;
00092 }

Ipv4Address::~Ipv4Address (  ) 

Definition at line 95 of file Ipv4Address.cpp.

00096 {
00097 }

Ipv4Address::Ipv4Address ( const Ipv4Address  )  [inline, private]

Definition at line 95 of file Ipv4Address.h.

00095 {} // copy constructor


Member Function Documentation

Ipv4Address::operator struct sockaddr * (  )  [virtual]

Get a pointer to the address struct.

Implements SocketAddress.

Definition at line 100 of file Ipv4Address.cpp.

References m_addr.

00101 {
00102         return (struct sockaddr *)&m_addr;
00103 }

Ipv4Address::operator socklen_t (  )  [virtual]

Get length of address struct.

Implements SocketAddress.

Definition at line 106 of file Ipv4Address.cpp.

00107 {
00108         return sizeof(struct sockaddr_in);
00109 }

bool Ipv4Address::operator== ( SocketAddress  )  [virtual]

Compare two addresses.

Implements SocketAddress.

Definition at line 190 of file Ipv4Address.cpp.

References GetFamily(), SocketAddress::GetFamily(), and m_addr.

00191 {
00192         if (a.GetFamily() != GetFamily())
00193                 return false;
00194         if ((socklen_t)a != sizeof(m_addr))
00195                 return false;
00196         struct sockaddr *sa = a;
00197         struct sockaddr_in *p = (struct sockaddr_in *)sa;
00198         if (p -> sin_port != m_addr.sin_port)
00199                 return false;
00200         if (memcmp(&p -> sin_addr, &m_addr.sin_addr, 4))
00201                 return false;
00202         return true;
00203 }

void Ipv4Address::SetPort ( port_t  port  )  [virtual]

Set port number.

Parameters:
port Port number in host byte order

Implements SocketAddress.

Definition at line 112 of file Ipv4Address.cpp.

References m_addr.

00113 {
00114         m_addr.sin_port = htons( port );
00115 }

port_t Ipv4Address::GetPort (  )  [virtual]

Get port number.

Returns:
Port number in host byte order.

Implements SocketAddress.

Definition at line 118 of file Ipv4Address.cpp.

References m_addr.

Referenced by Convert().

00119 {
00120         return ntohs( m_addr.sin_port );
00121 }

void Ipv4Address::SetAddress ( struct sockaddr *  sa  )  [virtual]

Set socket address.

Parameters:
sa Pointer to either 'struct sockaddr_in' or 'struct sockaddr_in6'.

Implements SocketAddress.

Definition at line 172 of file Ipv4Address.cpp.

References m_addr.

00173 {
00174         memcpy(&m_addr, sa, sizeof(struct sockaddr_in));
00175 }

int Ipv4Address::GetFamily (  )  [virtual]

Get address family.

Implements SocketAddress.

Definition at line 178 of file Ipv4Address.cpp.

References m_addr.

Referenced by operator==().

00179 {
00180         return m_addr.sin_family;
00181 }

bool Ipv4Address::IsValid (  )  [virtual]

Address structure is valid.

Implements SocketAddress.

Definition at line 184 of file Ipv4Address.cpp.

References m_valid.

Referenced by UdpSocket::Bind(), ListenSocket< X >::Bind(), UdpSocket::Open(), and UdpSocket::SendToBuf().

00185 {
00186         return m_valid;
00187 }

std::auto_ptr< SocketAddress > Ipv4Address::GetCopy (  )  [virtual]

Get a copy of this SocketAddress object.

Implements SocketAddress.

Definition at line 206 of file Ipv4Address.cpp.

References Ipv4Address(), and m_addr.

00207 {
00208         return std::auto_ptr<SocketAddress>(new Ipv4Address(m_addr));
00209 }

std::string Ipv4Address::Convert ( bool  include_port = false  )  [virtual]

Convert address struct to text.

Implements SocketAddress.

Definition at line 152 of file Ipv4Address.cpp.

References GetPort(), Utility::l2string(), and m_addr.

Referenced by Socket::GetSockAddress().

00153 {
00154         if (include_port)
00155                 return Convert(m_addr.sin_addr) + ":" + Utility::l2string(GetPort());
00156         return Convert(m_addr.sin_addr);
00157 }

std::string Ipv4Address::Reverse (  )  [virtual]

Reverse lookup of address.

Implements SocketAddress.

Definition at line 212 of file Ipv4Address.cpp.

References m_addr.

00213 {
00214         std::string tmp;
00215         Reverse(m_addr.sin_addr, tmp);
00216         return tmp;
00217 }

bool Ipv4Address::Resolve ( const std::string &  hostname,
struct in_addr &  a 
) [static]

Resolve hostname.

Definition at line 124 of file Ipv4Address.cpp.

References AI_NUMERICHOST, Utility::isipv4(), and Utility::u2ip().

00125 {
00126         struct sockaddr_in sa;
00127         memset(&a, 0, sizeof(a));
00128         if (Utility::isipv4(hostname))
00129         {
00130                 if (!Utility::u2ip(hostname, sa, AI_NUMERICHOST))
00131                         return false;
00132                 a = sa.sin_addr;
00133                 return true;
00134         }
00135         if (!Utility::u2ip(hostname, sa))
00136                 return false;
00137         a = sa.sin_addr;
00138         return true;
00139 }

bool Ipv4Address::Reverse ( struct in_addr &  a,
std::string &  name 
) [static]

Reverse resolve (IP to hostname).

Definition at line 142 of file Ipv4Address.cpp.

References Utility::reverse().

00143 {
00144         struct sockaddr_in sa;
00145         memset(&sa, 0, sizeof(sa));
00146         sa.sin_family = AF_INET;
00147         sa.sin_addr = a;
00148         return Utility::reverse((struct sockaddr *)&sa, sizeof(sa), name);
00149 }

std::string Ipv4Address::Convert ( struct in_addr &  a  )  [static]

Convert address struct to text.

Definition at line 160 of file Ipv4Address.cpp.

References NI_NUMERICHOST, and Utility::reverse().

00161 {
00162         struct sockaddr_in sa;
00163         memset(&sa, 0, sizeof(sa));
00164         sa.sin_family = AF_INET;
00165         sa.sin_addr = a;
00166         std::string name;
00167         Utility::reverse((struct sockaddr *)&sa, sizeof(sa), name, NI_NUMERICHOST);
00168         return name;
00169 }

Ipv4Address& Ipv4Address::operator= ( const Ipv4Address  )  [inline, private]

Definition at line 96 of file Ipv4Address.h.

00096 { return *this; } // assignment operator


Member Data Documentation

struct sockaddr_in Ipv4Address::m_addr [read, private]

bool Ipv4Address::m_valid [private]

Definition at line 98 of file Ipv4Address.h.

Referenced by Ipv4Address(), and IsValid().


The documentation for this class was generated from the following files:
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4