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 45 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 44 of file Ipv4Address.cpp.

References m_addr.

Referenced by GetCopy().

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

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 52 of file Ipv4Address.cpp.

References m_addr.

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

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 61 of file Ipv4Address.cpp.

References m_addr.

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

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 70 of file Ipv4Address.cpp.

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

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

Ipv4Address::Ipv4Address ( struct sockaddr_in &  sa  ) 

Definition at line 86 of file Ipv4Address.cpp.

References m_addr, and m_valid.

00087 {
00088         m_addr = sa;
00089         m_valid = sa.sin_family == AF_INET;
00090 }

Ipv4Address::~Ipv4Address (  ) 

Definition at line 93 of file Ipv4Address.cpp.

00094 {
00095 }

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

Definition at line 93 of file Ipv4Address.h.

00093 {} // copy constructor


Member Function Documentation

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

Get a pointer to the address struct.

Implements SocketAddress.

Definition at line 98 of file Ipv4Address.cpp.

References m_addr.

00099 {
00100         return (struct sockaddr *)&m_addr;
00101 }

Ipv4Address::operator socklen_t (  )  [virtual]

Get length of address struct.

Implements SocketAddress.

Definition at line 104 of file Ipv4Address.cpp.

00105 {
00106         return sizeof(struct sockaddr_in);
00107 }

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

Compare two addresses.

Implements SocketAddress.

Definition at line 188 of file Ipv4Address.cpp.

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

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

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

Set port number.

Parameters:
port Port number in host byte order

Implements SocketAddress.

Definition at line 110 of file Ipv4Address.cpp.

References m_addr.

00111 {
00112         m_addr.sin_port = htons( port );
00113 }

port_t Ipv4Address::GetPort (  )  [virtual]

Get port number.

Returns:
Port number in host byte order.

Implements SocketAddress.

Definition at line 116 of file Ipv4Address.cpp.

References m_addr.

Referenced by Convert().

00117 {
00118         return ntohs( m_addr.sin_port );
00119 }

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 170 of file Ipv4Address.cpp.

References m_addr.

00171 {
00172         memcpy(&m_addr, sa, sizeof(struct sockaddr_in));
00173 }

int Ipv4Address::GetFamily (  )  [virtual]

Get address family.

Implements SocketAddress.

Definition at line 176 of file Ipv4Address.cpp.

References m_addr.

Referenced by operator==().

00177 {
00178         return m_addr.sin_family;
00179 }

bool Ipv4Address::IsValid (  )  [virtual]

Address structure is valid.

Implements SocketAddress.

Definition at line 182 of file Ipv4Address.cpp.

References m_valid.

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

00183 {
00184         return m_valid;
00185 }

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

Get a copy of this SocketAddress object.

Implements SocketAddress.

Definition at line 204 of file Ipv4Address.cpp.

References Ipv4Address(), and m_addr.

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

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

Convert address struct to text.

Implements SocketAddress.

Definition at line 150 of file Ipv4Address.cpp.

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

Referenced by Socket::GetSockAddress().

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

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

Reverse lookup of address.

Implements SocketAddress.

Definition at line 210 of file Ipv4Address.cpp.

References m_addr.

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

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

Resolve hostname.

Definition at line 122 of file Ipv4Address.cpp.

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

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

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

Reverse resolve (IP to hostname).

Definition at line 140 of file Ipv4Address.cpp.

References Utility::reverse().

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

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

Convert address struct to text.

Definition at line 158 of file Ipv4Address.cpp.

References NI_NUMERICHOST, and Utility::reverse().

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

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

Definition at line 94 of file Ipv4Address.h.

00094 { return *this; } // assignment operator


Member Data Documentation

struct sockaddr_in Ipv4Address::m_addr [read, private]

bool Ipv4Address::m_valid [private]

Definition at line 96 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