IPv4 Internet address. More...

#include <senf/Socket/Protocols/INet/INet4Address.hh>

Inheritance diagram for senf::INet4Address:

Public Types

typedef uint32_t address_type
 Address representation as number in host byte order. More...
 
typedef uint32_t inaddr_type
 Legacy address representation in network byte order. More...
 

Static Public Attributes

static INet4Address const None
 The empty (0) address. More...
 
static INet4Address const Loopback
 The loopback (127.0.0.1) address. More...
 
static INet4Address const Broadcast
 

Related Functions

(Note that these are not member functions.)

std::ostream & operator<< (std::ostream &os, INet4Address const &addr)
 Output INet4Address instance as it's string representation. More...
 
std::istream & operator>> (std::istream &os, INet4Address &addr)
 Initialize INet4Address instance from a string representation sets std::ios::failbit on the stream if an error occurred. More...
 

Structors and default members

 INet4Address ()
 Construct an empty address. More...
 
 INet4Address (senf::NoInit_t)
 Construct uninitialized (!) address. More...
 
 INet4Address (address_type value)
 Construct an address constant. More...
 
static INet4Address from_string (std::string const &s)
 Convert string to address. More...
 
template<class InputIterator >
static INet4Address from_data (InputIterator i)
 Construct address from 4 bytes of raw data. More...
 
static INet4Address from_inaddr (inaddr_type v)
 Construct address from integer in network byte order. More...
 

Accessors

bool local () const
 true, if address is locally administered More...
 
bool loopback () const
 true, if address is within the loopback network More...
 
bool multicast () const
 true, if address is a multicast address More...
 
bool broadcast () const
 true, if address is 255.255.255.255 More...
 
bool boolean_test () const
 true, if address is non-empty (!= 0.0.0.0) More...
 
inaddr_type inaddr () const
 Return the raw network byte order address. More...
 
address_type address () const
 Return address represented as integer number. More...
 

Additional Inherited Members

- Public Member Functions inherited from senf::comparable_safe_bool< INet4Address >
 operator bool_type () const
 
bool operator! () const
 
- Protected Types inherited from senf::safe_bool_base
typedef void(safe_bool_base::* bool_type) () const
 
- Protected Member Functions inherited from senf::comparable_safe_bool< INet4Address >
 ~comparable_safe_bool ()
 
- Protected Member Functions inherited from senf::safe_bool_base
void this_type_does_not_support_comparisons () const
 
 safe_bool_base ()
 
 safe_bool_base (const safe_bool_base &)
 
safe_bool_baseoperator= (const safe_bool_base &)
 
 ~safe_bool_base ()
 

Detailed Description

IPv4 Internet address.

INet4Address represents a simple IP address. It is modelled as a fixed-size container/sequence of 4 bytes.

The following statements all create the same INet4 address 211.194.177.160

// Used to construct constant INet4 addresses
INet4Address(0xD3C2B1A0)
// Construct an INet4 address from it's string representation. All the standard address
// representations are supported
INet4Address::from_string("211.194.177.160")
INet4Address::from_string("211.12759456")
// Construct an INet4 address from raw data. 'from_data' takes an arbitrary iterator (e.g. a
// pointer) as argument. Here we use a fixed array but normally you will need this to build
// an INet4 address in a packet parser
char rawBytes[] = { 0xD3, 0xC2, 0xB1, 0xA0 };
// Construct an INet4 address from the standard POSIX representation: a 32-bit integer in
// network byte oder. This is used to interface with POSIX routines
struct sockaddr_in saddr = ...;
INet4Address::from_inaddr(saddr.sin_addr.s_addr)
    Since INet4Address is based on \c boost::array, you can access the raw data bytes of the
    address (in network byte order) using \c begin(), \c end() or \c operator[]
INet4Address ina = ...;
Packet::iterator i = ...;
std::copy(ina.begin(), ina.end(), i); // Copies 4 bytes
    \see CheckINet4Network \n INet4Network

    \par "Implementation note:" We awkwardly need to use static named constructors (<tt>from_</tt> members)
        instead of ordinarily overloaded constructors for one simple reason: <tt>char *</tt>
        doubles as string literal and as arbitrary data iterator. The iterator constructor can
        therefore not be distinguished from initialization with a string literal. Therefore we
        need to disambiguate using the named constructors.

Definition at line 78 of file INet4Address.hh.

Member Typedef Documentation

◆ address_type

Address representation as number in host byte order.

Definition at line 86 of file INet4Address.hh.

◆ inaddr_type

Legacy address representation in network byte order.

Definition at line 87 of file INet4Address.hh.

Constructor & Destructor Documentation

◆ INet4Address() [1/3]

senf::INet4Address::INet4Address ( )

Construct an empty address.

◆ INet4Address() [2/3]

senf::INet4Address::INet4Address ( senf::NoInit_t  )
explicit

Construct uninitialized (!) address.

◆ INet4Address() [3/3]

senf::INet4Address::INet4Address ( address_type  value)
explicit

Construct an address constant.

Definition at line 36 of file INet4Address.cc.

Member Function Documentation

◆ address()

senf::INet4Address::address_type senf::INet4Address::address ( ) const

Return address represented as integer number.

This member returns the address as an integer number in host byte order. This representation allows simple network math operations.

Definition at line 103 of file INet4Address.cc.

◆ boolean_test()

bool senf::INet4Address::boolean_test ( ) const

true, if address is non-empty (!= 0.0.0.0)

◆ broadcast()

bool senf::INet4Address::broadcast ( ) const

true, if address is 255.255.255.255

◆ from_data()

template<class InputIterator >
static INet4Address senf::INet4Address::from_data ( InputIterator  i)
static

Construct address from 4 bytes of raw data.

from_data will build an address from 4 bytes of raw data as accessed by the iterator. The data must be in network byte order.

◆ from_inaddr()

static INet4Address senf::INet4Address::from_inaddr ( inaddr_type  v)
static

Construct address from integer in network byte order.

This call is used when interfacing with other legacy code to convert a network byte order address in an integer number into an INet4Address.

◆ from_string()

senf::INet4Address senf::INet4Address::from_string ( std::string const &  s)
static

Convert string to address.

This member will try to convert the given string into an IP address. from_string() supports all standard IP literal representations as well as hostnames.

Attention
This call may block if s represents a hostname which must be looked up via some network protocol like DNS or NIS
Exceptions
AddressSyntaxExceptionif the address cannot be converted for some reason
UnknownHostnameExceptionif the hostname cannot be resolved
Parameters
[in]sAddress literal or hostname

Definition at line 41 of file INet4Address.cc.

◆ inaddr()

inaddr_type senf::INet4Address::inaddr ( ) const

Return the raw network byte order address.

This member is used to interact with legacy code.

Returns

◆ local()

bool senf::INet4Address::local ( ) const

true, if address is locally administered

This call checks, if the address is within one of the IANA private ranges.

Definition at line 80 of file INet4Address.cc.

◆ loopback()

bool senf::INet4Address::loopback ( ) const

true, if address is within the loopback network

Checks, whether the address is in the IANA loopback network 10.0.0.0/8

Definition at line 91 of file INet4Address.cc.

◆ multicast()

bool senf::INet4Address::multicast ( ) const

true, if address is a multicast address

Checks, whether the address is in the 224.0.0.0/4 network reserved for multicast addresses by the IANA.

Definition at line 97 of file INet4Address.cc.

Friends And Related Function Documentation

◆ operator<<()

std::ostream & operator<< ( std::ostream &  os,
INet4Address const &  addr 
)
related

Output INet4Address instance as it's string representation.

◆ operator>>()

std::istream & operator>> ( std::istream &  os,
INet4Address addr 
)
related

Initialize INet4Address instance from a string representation sets std::ios::failbit on the stream if an error occurred.

See also
INet4Address from_string()

Member Data Documentation

◆ Broadcast

senf::INet4Address const senf::INet4Address::Broadcast
static

Definition at line 91 of file INet4Address.hh.

◆ Loopback

senf::INet4Address const senf::INet4Address::Loopback
static

The loopback (127.0.0.1) address.

Definition at line 90 of file INet4Address.hh.

◆ None

senf::INet4Address const senf::INet4Address::None
static

The empty (0) address.

Definition at line 89 of file INet4Address.hh.


The documentation for this class was generated from the following files: