Logo
~Sockets~
~Examples~
~Contact~


Ipv4Address Class Reference

#include <Ipv4Address.h>

Inheritance diagram for Ipv4Address:

Inheritance graph
[legend]
Collaboration diagram for Ipv4Address:

Collaboration graph
[legend]
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

sockaddr_in m_addr
bool m_valid

Detailed Description

Definition at line 37 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 37 of file Ipv4Address.cpp.

References m_addr.

Referenced by GetCopy().

00037                                     : m_valid(true)
00038 {
00039         memset(&m_addr, 0, sizeof(m_addr));
00040         m_addr.sin_family = AF_INET;
00041         m_addr.sin_port = htons( port );
00042 }

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

References m_addr.

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

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 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         m_addr.sin_addr = a;
00060 }

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

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

00063                                                           : m_valid(false)
00064 {
00065         memset(&m_addr, 0, sizeof(m_addr));
00066         m_addr.sin_family = AF_INET;
00067         m_addr.sin_port = htons( port );
00068         {
00069                 ipaddr_t a;
00070                 if (Utility::u2ip(host, a))
00071                 {
00072                         memcpy(&m_addr.sin_addr, &a, sizeof(struct in_addr));
00073                         m_valid = true;
00074                 }
00075         }
00076 }

Ipv4Address::Ipv4Address ( struct sockaddr_in &   ) 

Definition at line 79 of file Ipv4Address.cpp.

References m_addr, and m_valid.

00080 {
00081         m_addr = sa;
00082         m_valid = sa.sin_family == AF_INET;
00083 }

Ipv4Address::~Ipv4Address (  ) 

Definition at line 86 of file Ipv4Address.cpp.

00087 {
00088 }

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

Definition at line 85 of file Ipv4Address.h.

00085 {} // copy constructor


Member Function Documentation

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

Get a pointer to the address struct.

Implements SocketAddress.

Definition at line 91 of file Ipv4Address.cpp.

References m_addr.

00092 {
00093         return (struct sockaddr *)&m_addr;
00094 }

Ipv4Address::operator socklen_t (  )  [virtual]

Get length of address struct.

Implements SocketAddress.

Definition at line 97 of file Ipv4Address.cpp.

00098 {
00099         return sizeof(struct sockaddr_in);
00100 }

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

Compare two addresses.

Implements SocketAddress.

Definition at line 181 of file Ipv4Address.cpp.

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

00182 {
00183         if (a.GetFamily() != GetFamily())
00184                 return false;
00185         if ((socklen_t)a != sizeof(m_addr))
00186                 return false;
00187         struct sockaddr *sa = a;
00188         struct sockaddr_in *p = (struct sockaddr_in *)sa;
00189         if (p -> sin_port != m_addr.sin_port)
00190                 return false;
00191         if (memcmp(&p -> sin_addr, &m_addr.sin_addr, 4))
00192                 return false;
00193         return true;
00194 }

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

Set port number.

Parameters:
port Port number in host byte order

Implements SocketAddress.

Definition at line 103 of file Ipv4Address.cpp.

References m_addr.

00104 {
00105         m_addr.sin_port = htons( port );
00106 }

port_t Ipv4Address::GetPort (  )  [virtual]

Get port number.

Returns:
Port number in host byte order.

Implements SocketAddress.

Definition at line 109 of file Ipv4Address.cpp.

References m_addr.

Referenced by Convert().

00110 {
00111         return ntohs( m_addr.sin_port );
00112 }

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

References m_addr.

00164 {
00165         memcpy(&m_addr, sa, sizeof(struct sockaddr_in));
00166 }

int Ipv4Address::GetFamily (  )  [virtual]

Get address family.

Implements SocketAddress.

Definition at line 169 of file Ipv4Address.cpp.

References m_addr.

Referenced by operator==().

00170 {
00171         return m_addr.sin_family;
00172 }

bool Ipv4Address::IsValid (  )  [virtual]

Address structure is valid.

Implements SocketAddress.

Definition at line 175 of file Ipv4Address.cpp.

References m_valid.

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

00176 {
00177         return m_valid;
00178 }

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

Get a copy of this SocketAddress object.

Implements SocketAddress.

Definition at line 197 of file Ipv4Address.cpp.

References Ipv4Address(), and m_addr.

00198 {
00199         return std::auto_ptr<SocketAddress>(new Ipv4Address(m_addr));
00200 }

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

Convert address struct to text.

Implements SocketAddress.

Definition at line 143 of file Ipv4Address.cpp.

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

00144 {
00145         if (include_port)
00146                 return Convert(m_addr.sin_addr) + ":" + Utility::l2string(GetPort());
00147         return Convert(m_addr.sin_addr);
00148 }

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

Reverse lookup of address.

Implements SocketAddress.

Definition at line 203 of file Ipv4Address.cpp.

References m_addr.

00204 {
00205         std::string tmp;
00206         Reverse(m_addr.sin_addr, tmp);
00207         return tmp;
00208 }

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

Resolve hostname.

Definition at line 115 of file Ipv4Address.cpp.

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

00116 {
00117         struct sockaddr_in sa;
00118         memset(&a, 0, sizeof(a));
00119         if (Utility::isipv4(hostname))
00120         {
00121                 if (!Utility::u2ip(hostname, sa, AI_NUMERICHOST))
00122                         return false;
00123                 a = sa.sin_addr;
00124                 return true;
00125         }
00126         if (!Utility::u2ip(hostname, sa))
00127                 return false;
00128         a = sa.sin_addr;
00129         return true;
00130 }

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

Reverse resolve (IP to hostname).

Definition at line 133 of file Ipv4Address.cpp.

References Utility::reverse().

00134 {
00135         struct sockaddr_in sa;
00136         memset(&sa, 0, sizeof(sa));
00137         sa.sin_family = AF_INET;
00138         sa.sin_addr = a;
00139         return Utility::reverse((struct sockaddr *)&sa, sizeof(sa), name);
00140 }

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

Convert address struct to text.

Definition at line 151 of file Ipv4Address.cpp.

References NI_NUMERICHOST, and Utility::reverse().

00152 {
00153         struct sockaddr_in sa;
00154         memset(&sa, 0, sizeof(sa));
00155         sa.sin_family = AF_INET;
00156         sa.sin_addr = a;
00157         std::string name;
00158         Utility::reverse((struct sockaddr *)&sa, sizeof(sa), name, NI_NUMERICHOST);
00159         return name;
00160 }

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

Definition at line 86 of file Ipv4Address.h.

00086 { return *this; } // assignment operator


Member Data Documentation

struct sockaddr_in Ipv4Address::m_addr [read, private]

bool Ipv4Address::m_valid [private]

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