xorp

IPv4 Class Reference

IPv4 address class. More...

#include <ipv4.hh>

List of all members.

Public Types

enum  { AF = AF_INET }
 

Constant for address family.


enum  { IPV = 4 }
 

Constant for IP protocol version.


typedef in_addr InAddrType
typedef sockaddr_in SockAddrType

Public Member Functions

 IPv4 ()
 Default constructor.
 IPv4 (const IPv4 &ipv4)
 Constructor from another IPv4 address.
 IPv4 (uint32_t value)
 Constructor from an integer value.
 IPv4 (const uint8_t *from_uint8)
 Constructor from a (uint8_t *) memory pointer.
 IPv4 (const in_addr &from_in_addr)
 Constructor from in_addr structure.
 IPv4 (const sockaddr &sa) throw (InvalidFamily)
 Constructor from sockaddr structure.
 IPv4 (const sockaddr_storage &ss) throw (InvalidFamily)
 Constructor from sockaddr_storage structure.
 IPv4 (const sockaddr_in &sin) throw (InvalidFamily)
 Constructor from sockaddr_in structure.
 IPv4 (const char *from_string) throw (InvalidString)
 Constructor from a string.
size_t copy_out (uint8_t *to_uint8) const
 Copy the IPv4 raw address to specified memory location.
size_t copy_out (in_addr &to_in_addr) const
 Copy the IP4 raw address to an in_addr structure.
size_t copy_out (sockaddr &to_sockaddr) const
 Copy the IPv4 raw address to a sockaddr structure.
size_t copy_out (sockaddr_storage &to_sockaddr_storage) const
 Copy the IPv4 raw address to a sockaddr_storage structure.
size_t copy_out (sockaddr_in &to_sockaddr_in) const
 Copy the IPv4 raw address to a sockaddr_in structure.
size_t copy_in (const uint8_t *from_uint8)
 Copy a raw IPv4 address from specified memory location into IPv4 structure.
size_t copy_in (const in_addr &from_in_addr)
 Copy a raw IPv4 address from a in_addr structure into IPv4 structure.
size_t copy_in (const sockaddr &from_sockaddr) throw (InvalidFamily)
 Copy a raw address from a sockaddr structure into IPv4 structure.
size_t copy_in (const sockaddr_storage &from_sockaddr_storage) throw (InvalidFamily)
 Copy a raw address from a sockaddr_storage structure into IPv4 structure.
size_t copy_in (const sockaddr_in &from_sockaddr_in) throw (InvalidFamily)
 Copy a raw address from a sockaddr_in structure into IPv4 structure.
IPv4 operator~ () const
 Bitwise-Negation Operator.
IPv4 operator| (const IPv4 &other) const
 OR Operator.
IPv4 operator& (const IPv4 &other) const
 AND Operator.
IPv4 operator^ (const IPv4 &other) const
 XOR Operator.
IPv4 operator<< (uint32_t left_shift) const
 Operator <<.
IPv4 operator>> (uint32_t right_shift) const
 Operator >>
bool operator< (const IPv4 &other) const
 Less-Than Operator.
bool operator== (const IPv4 &other) const
 Equality Operator.
bool operator!= (const IPv4 &other) const
 Not-Equal Operator.
bool operator== (const IPv4Range &rhs) const
 Equality Operator for IPv4 against IPv4Range operand.
bool operator!= (const IPv4Range &rhs) const
 Not-equal Operator for IPv4 against IPv4Range operand.
bool operator< (const IPv4Range &rhs) const
 Less-than comparison for IPv4 against IPv4Range operand.
bool operator<= (const IPv4Range &rhs) const
 Less-than or equal comparison for IPv4 against IPv4Range.
bool operator> (const IPv4Range &rhs) const
 Greater-than comparison for IPv4 against IPv4Range operand.
bool operator>= (const IPv4Range &rhs) const
 Greater-than or equal comparison for IPv4 against IPv4Range.
IPv4operator-- ()
 Decrement Operator.
IPv4operator++ ()
 Increment Operator.
string str () const
 Convert this address from binary form to presentation format.
bool is_zero () const
 Test if this address is numerically zero.
bool is_unicast () const
 Test if this address is a valid unicast address.
bool is_multicast () const
 Test if this address is a valid multicast address.
bool is_class_a () const
 Test if this address belongs to the IPv4 Class A address space (0.0.0.0/1).
bool is_class_b () const
 Test if this address belongs to the IPv4 Class B address space (128.0.0.0/2).
bool is_class_c () const
 Test if this address belongs to the IPv4 Class C address space (192.0.0.0/3).
bool is_experimental () const
 Test if this address belongs to the IPv4 experimental Class E address space (240.0.0.0/4).
bool is_linklocal_unicast () const
 Test if this address is a valid link-local unicast address.
bool is_interfacelocal_multicast () const
 Test if this address is a valid interface-local multicast address.
bool is_nodelocal_multicast () const
 Test if this address is a valid node-local multicast address.
bool is_linklocal_multicast () const
 Test if this address is a valid link-local multicast address.
bool is_loopback () const
 Test if this address is a valid loopback address.
IPv4 mask_by_prefix_len (uint32_t mask_len) const throw (InvalidNetmaskLength)
 Make an IPv4 address prefix.
uint32_t mask_len () const
 Get the mask length.
uint32_t addr () const
 Get the uint32_t raw value of this address.
void set_addr (uint32_t value)
 Set the address value.
uint32_t bits (uint32_t lsb, uint32_t len) const
 Extract bits from an address.
uint32_t bit_count () const
 Count the number of bits that are set in this address.
uint32_t leading_zero_count () const
 Count the number of leading zeroes in this address.

Static Public Member Functions

static size_t addr_bytelen ()
 Get the address octet-size.
static uint32_t addr_bitlen ()
 Get the address bit-length.
static uint32_t ip_multicast_base_address_mask_len ()
 Get the mask length for the multicast base address.
static uint32_t ip_class_a_base_address_mask_len ()
 Get the mask length for the Class A base address.
static uint32_t ip_class_b_base_address_mask_len ()
 Get the mask length for the Class B base address.
static uint32_t ip_class_c_base_address_mask_len ()
 Get the mask length for the Class C base address.
static uint32_t ip_experimental_base_address_mask_len ()
 Get the mask length for the experimental base address.
static IPv4 make_prefix (uint32_t mask_len) throw (InvalidNetmaskLength)
 Make an IPv4 mask prefix.
static int af ()
 Get the address family.
static uint32_t ip_version ()
 Get the IP protocol version.
static const string & ip_version_str ()
 Get the human-readable string with the IP protocol version.
static const IPv4ZERO (int af=AF_INET)
 Pre-defined IPv4 address constants.
static const IPv4ANY (int af=AF_INET)
static const IPv4ALL_ONES (int af=AF_INET)
static const IPv4LOOPBACK (int af=AF_INET)
static const IPv4MULTICAST_BASE (int af=AF_INET)
static const IPv4MULTICAST_ALL_SYSTEMS (int af=AF_INET)
static const IPv4MULTICAST_ALL_ROUTERS (int af=AF_INET)
static const IPv4DVMRP_ROUTERS (int af=AF_INET)
static const IPv4OSPFIGP_ROUTERS (int af=AF_INET)
static const IPv4OSPFIGP_DESIGNATED_ROUTERS (int af=AF_INET)
static const IPv4RIP2_ROUTERS (int af=AF_INET)
static const IPv4PIM_ROUTERS (int af=AF_INET)
static const IPv4SSM_ROUTERS (int af=AF_INET)
static const IPv4CLASS_A_BASE (int af=AF_INET)
static const IPv4CLASS_B_BASE (int af=AF_INET)
static const IPv4CLASS_C_BASE (int af=AF_INET)
static const IPv4EXPERIMENTAL_BASE (int af=AF_INET)

Static Public Attributes

static const uint32_t ADDR_BITLEN = 32
 Number of bits in address as a constant.
static const uint32_t ADDR_BYTELEN = ADDR_BITLEN / 8
 Number of bytes in address as a constant.

Private Attributes

uint32_t _addr

Detailed Description

IPv4 address class.

The IPv4 address class is a trivial class for handling IPv4 addresses and for performing operations on them such as printing and masking.


Constructor & Destructor Documentation

IPv4::IPv4 ( ) [inline]

Default constructor.

The address value is initialized to INADDR_ANY.

IPv4::IPv4 ( const IPv4 ipv4) [inline]

Constructor from another IPv4 address.

Parameters:
ipv4the IPv4 address to assign the address value from.
IPv4::IPv4 ( uint32_t  value) [inline, explicit]

Constructor from an integer value.

Parameters:
value32-bit unsigned integer to assign to the address.
IPv4::IPv4 ( const uint8_t *  from_uint8) [explicit]

Constructor from a (uint8_t *) memory pointer.

Parameters:
from_uint8the pointer to the memory to copy the address value from.
IPv4::IPv4 ( const in_addr &  from_in_addr)

Constructor from in_addr structure.

Parameters:
from_in_addrthe storage to copy the address value from.
IPv4::IPv4 ( const sockaddr &  sa) throw (InvalidFamily)

Constructor from sockaddr structure.

Parameters:
sasockaddr to construct IPv4 addr from.
IPv4::IPv4 ( const sockaddr_storage &  ss) throw (InvalidFamily)

Constructor from sockaddr_storage structure.

Parameters:
sssockaddr_storage to construct IPv4 addr from.
IPv4::IPv4 ( const sockaddr_in &  sin) throw (InvalidFamily)

Constructor from sockaddr_in structure.

Parameters:
sinsockaddr_in to construct IPv4 addr from.
IPv4::IPv4 ( const char *  from_string) throw (InvalidString)

Constructor from a string.

Parameters:
from_cstringC-style string in the IPv4 dotted decimal human-readable format used for initialization.

Member Function Documentation

uint32_t IPv4::addr ( ) const [inline]

Get the uint32_t raw value of this address.

Returns:
the value of this IPv4 address as an unsigned 32-bit integer.
static uint32_t IPv4::addr_bitlen ( ) [inline, static]

Get the address bit-length.

Note that this is a static function and can be used without a particular object. Example: uint32_t my_bitlen = IPv4::addr_bitlen(); uint32_t my_bitlen = ipv4.addr_bitlen();

Returns:
address size in number of bits.
static size_t IPv4::addr_bytelen ( ) [inline, static]

Get the address octet-size.

Note that this is a static function and can be used without a particular object. Example: size_t my_size = IPv4::addr_bytelen(); size_t my_size = ipv4.addr_bytelen();

Returns:
address size in number of octets.
static int IPv4::af ( ) [inline, static]

Get the address family.

Returns:
the address family of this address.
uint32_t IPv4::bit_count ( ) const [inline]

Count the number of bits that are set in this address.

Returns:
the number of bits that are set in this address.
uint32_t IPv4::bits ( uint32_t  lsb,
uint32_t  len 
) const [inline]

Extract bits from an address.

Parameters:
lsbstarting bit position (from the right) to extract.
lennumber of bits to extract. The maximum value is 32.
Returns:
the first len bits starting from the rightmost position lsb. The returned bits are in host order.
size_t IPv4::copy_in ( const sockaddr_storage &  from_sockaddr_storage) throw (InvalidFamily)

Copy a raw address from a sockaddr_storage structure into IPv4 structure.

Copy a raw address from .

Note that the address in the sockaddr_storage structure must be of IPv4 address family.

Parameters:
from_sockaddr_storagethe storage to copy the address from.
Returns:
the number of copied octets.
the number of copied octets.
size_t IPv4::copy_in ( const uint8_t *  from_uint8)

Copy a raw IPv4 address from specified memory location into IPv4 structure.

Copy a raw address from the memory pointed by .

Parameters:
from_uint8the memory address to copy the address from.
Returns:
the number of copied octets.
the number of copied octets.
size_t IPv4::copy_in ( const sockaddr &  from_sockaddr) throw (InvalidFamily)

Copy a raw address from a sockaddr structure into IPv4 structure.

Copy a raw address from .

Note that the address in the sockaddr structure must be of IPv4 address family.

Parameters:
from_sockaddrthe storage to copy the address from.
Returns:
the number of copied octets.
the number of copied octets.
size_t IPv4::copy_in ( const sockaddr_in &  from_sockaddr_in) throw (InvalidFamily)

Copy a raw address from a sockaddr_in structure into IPv4 structure.

Copy a raw address from .

Note that the address in the sockaddr structure must be of IPv4 address family.

Parameters:
from_sockaddr_inthe storage to copy the address from.
Returns:
the number of copied octets.
the number of copied octets.
size_t IPv4::copy_in ( const in_addr &  from_in_addr)

Copy a raw IPv4 address from a in_addr structure into IPv4 structure.

Copy a raw address of family AF_INET from .

Parameters:
from_in_addrthe storage to copy the address from.
Returns:
the number of copied octets.
the number of copied octets.
size_t IPv4::copy_out ( sockaddr_storage &  to_sockaddr_storage) const

Copy the IPv4 raw address to a sockaddr_storage structure.

Copy the raw address to , and assign appropriately the rest of the fields in .

Copy the raw address held within an IPv4 instance to an sockaddr_storage structure and assign appropriately and set fields within sockaddr_storage appropriately.

Parameters:
to_sockaddr_storagethe storage to copy the address to.
Returns:
the number of copied octets.
the number of copied octets.
size_t IPv4::copy_out ( in_addr &  to_in_addr) const

Copy the IP4 raw address to an in_addr structure.

Copy the raw address to .

Parameters:
to_in_addrthe storage to copy the address to.
Returns:
the number of copied octets.
the number of copied octets.
size_t IPv4::copy_out ( sockaddr_in &  to_sockaddr_in) const

Copy the IPv4 raw address to a sockaddr_in structure.

Copy the raw address to , and assign appropriately the rest of the fields in .

Copy the raw address held within an IPv4 instance to an sockaddr_in structure and assign appropriately and set fields within sockaddr_in appropriately.

Parameters:
to_sockaddr_inthe storage to copy the address to.
Returns:
the number of copied octets.
the number of copied octets.
size_t IPv4::copy_out ( sockaddr &  to_sockaddr) const

Copy the IPv4 raw address to a sockaddr structure.

Copy the raw address to , and assign appropriately the rest of the fields in .

Copy the raw address held within an IPv4 instance to an sockaddr structure and assign appropriately and set fields within sockaddr appropriately.

Parameters:
to_sockaddrthe storage to copy the address to.
Returns:
the number of copied octets.
the number of copied octets.
size_t IPv4::copy_out ( uint8_t *  to_uint8) const

Copy the IPv4 raw address to specified memory location.

Copy the raw address to memory pointed by .

Parameters:
to_uint8 the pointer to the memory to copy the address to.
Returns:
the number of copied octets.
the number of copied octets.
static uint32_t IPv4::ip_class_a_base_address_mask_len ( ) [inline, static]

Get the mask length for the Class A base address.

Note that this is a static function and can be used without a particular object. Example: uint32_t my_len = IPv4::ip_class_a_base_address_mask_len(); uint32_t my_len = ipv4.ip_class_a_base_address_mask_len();

Returns:
the Class A base address mask length for family AF_INET.
static uint32_t IPv4::ip_class_b_base_address_mask_len ( ) [inline, static]

Get the mask length for the Class B base address.

Note that this is a static function and can be used without a particular object. Example: uint32_t my_len = IPv4::ip_class_b_base_address_mask_len(); uint32_t my_len = ipv4.ip_class_b_base_address_mask_len();

Returns:
the Class B base address mask length for family AF_INET.
static uint32_t IPv4::ip_class_c_base_address_mask_len ( ) [inline, static]

Get the mask length for the Class C base address.

Note that this is a static function and can be used without a particular object. Example: uint32_t my_len = IPv4::ip_class_c_base_address_mask_len(); uint32_t my_len = ipv4.ip_class_c_base_address_mask_len();

Returns:
the Class C base address mask length for family AF_INET.
static uint32_t IPv4::ip_experimental_base_address_mask_len ( ) [inline, static]

Get the mask length for the experimental base address.

Note that this is a static function and can be used without a particular object. Example: uint32_t my_len = IPv4::ip_experimental_base_address_mask_len(); uint32_t my_len = ipv4.ip_experimental_base_address_mask_len();

Returns:
the experimental base address mask length for family AF_INET.
static uint32_t IPv4::ip_multicast_base_address_mask_len ( ) [inline, static]

Get the mask length for the multicast base address.

Note that this is a static function and can be used without a particular object. Example: uint32_t my_len = IPv4::ip_multicast_base_address_mask_len(); uint32_t my_len = ipv4.ip_multicast_base_address_mask_len();

Returns:
the multicast base address mask length for family AF_INET.
static uint32_t IPv4::ip_version ( ) [inline, static]

Get the IP protocol version.

Returns:
the IP protocol version of this address.
const string & IPv4::ip_version_str ( ) [static]

Get the human-readable string with the IP protocol version.

Returns:
the human-readable string with the IP protocol version of this address.
bool IPv4::is_class_a ( ) const

Test if this address belongs to the IPv4 Class A address space (0.0.0.0/1).

Returns:
true if the address is a valid Class A address.
bool IPv4::is_class_b ( ) const

Test if this address belongs to the IPv4 Class B address space (128.0.0.0/2).

Returns:
true if the address is a valid Class B address.
bool IPv4::is_class_c ( ) const

Test if this address belongs to the IPv4 Class C address space (192.0.0.0/3).

Returns:
true if the address is a valid Class C address.
bool IPv4::is_experimental ( ) const

Test if this address belongs to the IPv4 experimental Class E address space (240.0.0.0/4).

Returns:
true if the address is a valid experimental address.
bool IPv4::is_interfacelocal_multicast ( ) const

Test if this address is a valid interface-local multicast address.

Note that "node-local" multicast addresses were renamed to "interface-local" by RFC-3513.

Returns:
true if the address is a valid multicast address, and the scope of the address is interface-local. XXX: in IPv4 there is no interface-local multicast scope, therefore the return value is always false.
bool IPv4::is_linklocal_multicast ( ) const

Test if this address is a valid link-local multicast address.

Returns:
true if the address is a valid multicast address, and the scope of the address is link-local.
bool IPv4::is_linklocal_unicast ( ) const

Test if this address is a valid link-local unicast address.

Returns:
true if the address is a valid unicast address, and the scope of the address is link-local.
bool IPv4::is_loopback ( ) const

Test if this address is a valid loopback address.

Returns:
true if the address is a valid loopback address.
bool IPv4::is_multicast ( ) const

Test if this address is a valid multicast address.

Returns:
true if the address is a valid multicast address.
bool IPv4::is_nodelocal_multicast ( ) const [inline]

Test if this address is a valid node-local multicast address.

Note that "node-local" multicast addresses were renamed to "interface-local" by RFC-3513. This method is kept for backward compatibility.

Returns:
true if the address is a valid multicast address, and the scope of the address is node-local. XXX: in IPv4 there is no node-local multicast scope, therefore the return value is always false.
bool IPv4::is_unicast ( ) const

Test if this address is a valid unicast address.

Note that the numerically zero address is excluded.

Returns:
true if the address is a valid unicast address.
bool IPv4::is_zero ( ) const [inline]

Test if this address is numerically zero.

Returns:
true if the address is numerically zero.
uint32_t IPv4::leading_zero_count ( ) const [inline]

Count the number of leading zeroes in this address.

Returns:
the number of leading zeroes in this address.
IPv4 IPv4::make_prefix ( uint32_t  mask_len) throw (InvalidNetmaskLength) [static]

Make an IPv4 mask prefix.

Parameters:
mask_lenthe length of the mask to create.
Returns:
a new IPv4 address that contains a mask of length mask_len.
IPv4 IPv4::mask_by_prefix_len ( uint32_t  mask_len) const throw (InvalidNetmaskLength) [inline]

Make an IPv4 address prefix.

Parameters:
prefix_lenthe length of the mask of the prefix to create.
Returns:
a new IPv4 address created by masking this address with a mask of length prefix_len.
uint32_t IPv4::mask_len ( ) const

Get the mask length.

Returns:
the prefix length of the contiguous mask presumably stored as an IPv4 address.
bool IPv4::operator!= ( const IPv4 other) const [inline]

Not-Equal Operator.

Parameters:
otherthe right-hand operand to compare against.
Returns:
true if the left-hand operand is numerically not same as the right-hand operand.
bool IPv4::operator!= ( const IPv4Range rhs) const [inline]

Not-equal Operator for IPv4 against IPv4Range operand.

Parameters:
rhsthe right-hand IPv4Range operand.
Returns:
true if the value of the left-hand operand falls outside the range defined by the right-hand operand.
IPv4 IPv4::operator& ( const IPv4 other) const [inline]

AND Operator.

Parameters:
otherthe right-hand operand to AND with.
Returns:
bitwise AND of two addresses.
IPv4 & IPv4::operator++ ( )

Increment Operator.

The numerical value of this address is incremented by one. However, if the address value before the increment was all-1s, after the increment its value would be all-0s (i.e., it will wrap-around).

Returns:
a reference to this address after it was incremented by one.
IPv4 & IPv4::operator-- ( )

Decrement Operator.

The numerical value of this address is decremented by one. However, if the address value before the decrement was all-0s, after the decrement its value would be all-1s (i.e., it will wrap-around).

Returns:
a reference to this address after it was decremented by one.
bool IPv4::operator< ( const IPv4 other) const

Less-Than Operator.

Parameters:
otherthe right-hand operand to compare against.
Returns:
true if the left-hand operand is numerically smaller than the right-hand operand.
bool IPv4::operator< ( const IPv4Range rhs) const [inline]

Less-than comparison for IPv4 against IPv4Range operand.

Parameters:
rhsthe right-hand IPv4Range operand.
Returns:
true if the value of the left-hand operand is bellow the range defined by the right-hand operand.
IPv4 IPv4::operator<< ( uint32_t  left_shift) const

Operator <<.

Parameters:
left_shiftthe number of bits to shift to the left.
Returns:
IPv4 address that is shift bitwise to the left.
bool IPv4::operator<= ( const IPv4Range rhs) const [inline]

Less-than or equal comparison for IPv4 against IPv4Range.

Parameters:
rhsthe right-hand IPv4Range operand.
Returns:
true if the value of the left-hand operand is bellow or within the range defined by the right-hand operand.
bool IPv4::operator== ( const IPv4Range rhs) const [inline]

Equality Operator for IPv4 against IPv4Range operand.

Parameters:
rhsthe right-hand IPv4Range operand.
Returns:
true if the value of the left-hand operand falls inside the range defined by the right-hand operand.
bool IPv4::operator== ( const IPv4 other) const [inline]

Equality Operator.

Parameters:
otherthe right-hand operand to compare against.
Returns:
true if the left-hand operand is numerically same as the right-hand operand.
bool IPv4::operator> ( const IPv4Range rhs) const [inline]

Greater-than comparison for IPv4 against IPv4Range operand.

Parameters:
rhsthe right-hand IPv4Range operand.
Returns:
true if the value of the left-hand operand is above the range defined by the right-hand operand.
bool IPv4::operator>= ( const IPv4Range rhs) const [inline]

Greater-than or equal comparison for IPv4 against IPv4Range.

Parameters:
rhsthe right-hand IPv4Range operand.
Returns:
true if the value of the left-hand operand is above or within the range defined by the right-hand operand.
IPv4 IPv4::operator>> ( uint32_t  right_shift) const

Operator >>

Parameters:
right_shiftthe number of bits to shift to the right.
Returns:
IPv4 address that is shift bitwise to the right.
IPv4 IPv4::operator^ ( const IPv4 other) const [inline]

XOR Operator.

Returns:
eXclusive-OR of two addresses.
IPv4 IPv4::operator| ( const IPv4 other) const [inline]

OR Operator.

Parameters:
otherthe right-hand operand to OR with.
Returns:
bitwise OR of two addresses.
IPv4 IPv4::operator~ ( ) const [inline]

Bitwise-Negation Operator.

Returns:
address complement (i.e., all 0s become 1s, and vice-versa).
void IPv4::set_addr ( uint32_t  value) [inline]

Set the address value.

Parameters:
valueunsigned 32-bit integer value to set the address to.
string IPv4::str ( ) const

Convert this address from binary form to presentation format.

Returns:
C++ string with the human-readable ASCII representation of the address.

The documentation for this class was generated from the following files:
 All Classes Namespaces Functions Variables Typedefs Enumerations