Click
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Related Symbols | List of all members
IPAddress Class Reference

An IPv4 address. More...

Public Types

typedef uninitialized_type uninitialized_t
 
typedef IPAddress parameter_type
 

Public Member Functions

 IPAddress ()
 Construct an IPAddress equal to 0.0.0.0.
 
 IPAddress (unsigned x)
 Construct an IPAddress from an integer in network byte order.
 
 IPAddress (int x)
 
 IPAddress (unsigned long x)
 
 IPAddress (long x)
 
 IPAddress (struct in_addr x)
 Construct an IPAddress from a struct in_addr.
 
 IPAddress (const unsigned char *data)
 Construct an IPAddress from data.
 
 IPAddress (const String &x)
 Constructs an IPAddress from a human-readable dotted-quad representation.
 
 IPAddress (const uninitialized_type &unused)
 Construct an uninitialized IPAddress.
 
bool empty () const
 Test if the address is 0.0.0.0.
 
uint32_t addr () const
 Return the address as a uint32_t in network byte order.
 
 operator uint32_t () const
 Return the address as a uint32_t in network byte order.
 
bool is_multicast () const
 Return true iff the address is a multicast address.
 
bool is_link_local () const
 
struct in_addr in_addr () const
 Return a struct in_addr corresponding to the address.
 
 operator struct in_addr () const
 Return a struct in_addr corresponding to the address.
 
unsigned char * data ()
 Return a pointer to the address data.
 
const unsigned char * data () const
 Return a pointer to the address data.
 
uint32_t hashcode () const
 Hash function.
 
int mask_to_prefix_len () const
 Returns the prefix length equivalent to this prefix mask, or -1 if this is not a prefix mask.
 
bool matches_prefix (IPAddress addr, IPAddress mask) const
 Return true iff this address matches the address prefix addr/mask.
 
bool mask_as_specific (IPAddress mask) const
 Return true iff this address, considered as a prefix mask, is at least as specific as mask.
 
bool mask_more_specific (IPAddress mask) const
 Return true iff this prefix mask is more specific than mask.
 
IPAddress & operator&= (IPAddress)
 Assign this address to its bitwise-and with a.
 
IPAddress & operator|= (IPAddress)
 Assign this address to its bitwise-or with a.
 
IPAddress & operator^= (IPAddress)
 Assign this address to its bitwise-xor with a.
 
String unparse () const
 Unparses this address into a dotted-quad format String.
 
String unparse_mask () const
 Unparses this address into IP address mask format: either a prefix length or unparse().
 
String unparse_with_mask (IPAddress) const
 Unparses an address prefix, specified by address and mask, into "address/mask" format.
 
String s () const
 Unparse this address into a dotted-quad format String.
 
 operator String () const
 Unparse this address into a dotted-quad format String.
 

Static Public Member Functions

static IPAddress make_prefix (int prefix_len)
 Return an IPAddress equal to the prefix mask of length prefix.
 
static IPAddress make_broadcast ()
 Return the broadcast IP address, 255.255.255.255.
 
static IPAddress make_random ()
 Return the broadcast IP address, 255.255.255.255.
 

Related Symbols

(Note that these are not member symbols.)

bool operator== (IPAddress a, IPAddress b)
 Compare two IPAddress objects for equality.
 
bool operator== (IPAddress a, uint32_t b)
 Compare an IPAddress with a network-byte-order address value for equality.
 
bool operator!= (IPAddress a, IPAddress b)
 Compare two IPAddress objects for inequality.
 
bool operator!= (IPAddress a, uint32_t b)
 Compare an IPAddress with a network-byte-order address value for inequality.
 
IPAddress operator& (IPAddress a, IPAddress b)
 Calculate the IPAddress representing the bitwise-and of a and b.
 
IPAddress operator| (IPAddress a, IPAddress b)
 Calculate the IPAddress representing the bitwise-or of a and b.
 
IPAddress operator^ (IPAddress a, IPAddress b)
 Calculate the IPAddress representing the bitwise-xor of a and b.
 
IPAddress operator~ (IPAddress a)
 Calculate the IPAddress representing the bitwise complement of a.
 

Detailed Description

An IPv4 address.

The IPAddress type represents an IPv4 address. It supports bitwise operations like & and | and provides methods for unparsing IP addresses into ASCII dotted-quad form.

Constructor & Destructor Documentation

◆ IPAddress() [1/9]

IPAddress::IPAddress ( )
inline

Construct an IPAddress equal to 0.0.0.0.

◆ IPAddress() [2/9]

IPAddress::IPAddress ( unsigned  x)
inline

Construct an IPAddress from an integer in network byte order.

◆ IPAddress() [3/9]

IPAddress::IPAddress ( int  x)
inlineexplicit

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ IPAddress() [4/9]

IPAddress::IPAddress ( unsigned long  x)
inlineexplicit

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ IPAddress() [5/9]

IPAddress::IPAddress ( long  x)
inlineexplicit

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ IPAddress() [6/9]

IPAddress::IPAddress ( struct in_addr  x)
inline

Construct an IPAddress from a struct in_addr.

◆ IPAddress() [7/9]

IPAddress::IPAddress ( const unsigned char *  data)
inlineexplicit

Construct an IPAddress from data.

Parameters
datathe address data, in network byte order

Bytes data[0]...data[3] are used to construct the address.

◆ IPAddress() [8/9]

IPAddress::IPAddress ( const String &  x)
explicit

Constructs an IPAddress from a human-readable dotted-quad representation.

If x is not a valid dotted-quad address, then the IPAddress is initialized to 0.0.0.0.

◆ IPAddress() [9/9]

IPAddress::IPAddress ( const uninitialized_type &  unused)
inline

Construct an uninitialized IPAddress.

Member Function Documentation

◆ make_prefix()

IPAddress IPAddress::make_prefix ( int  prefix_len)
static

Return an IPAddress equal to the prefix mask of length prefix.

Parameters
prefix_lenprefix length; 0 <= prefix_len <= 32

For example, make_prefix(0) is 0.0.0.0, make_prefix(8) is 255.0.0.0, and make_prefix(32) is 255.255.255.255. Causes an assertion failure if prefix_len is out of range.

See also
mask_to_prefix_len

◆ make_broadcast()

static IPAddress IPAddress::make_broadcast ( )
inlinestatic

Return the broadcast IP address, 255.255.255.255.

◆ make_random()

static IPAddress IPAddress::make_random ( )
inlinestatic

Return the broadcast IP address, 255.255.255.255.

◆ empty()

bool IPAddress::empty ( ) const
inline

Test if the address is 0.0.0.0.

◆ addr()

uint32_t IPAddress::addr ( ) const
inline

Return the address as a uint32_t in network byte order.

◆ operator uint32_t()

IPAddress::operator uint32_t ( ) const
inline

Return the address as a uint32_t in network byte order.

Also suitable for use as an operator bool, returning true iff the address is not 0.0.0.0.

◆ is_multicast()

bool IPAddress::is_multicast ( ) const
inline

Return true iff the address is a multicast address.

These are the class D addresses, 224.0.0.0-239.255.255.255.

◆ in_addr()

struct in_addr IPAddress::in_addr ( ) const
inline

Return a struct in_addr corresponding to the address.

◆ operator struct in_addr()

IPAddress::operator struct in_addr ( ) const
inline

Return a struct in_addr corresponding to the address.

◆ data() [1/2]

unsigned char * IPAddress::data ( )
inline

Return a pointer to the address data.

Since the address is stored in network byte order, data()[0] is the top 8 bits of the address, data()[1] the next 8 bits, and so forth.

◆ data() [2/2]

const unsigned char * IPAddress::data ( ) const
inline

Return a pointer to the address data.

Since the address is stored in network byte order, data()[0] is the top 8 bits of the address, data()[1] the next 8 bits, and so forth.

◆ hashcode()

uint32_t IPAddress::hashcode ( ) const
inline

Hash function.

Returns
The hash value of this IPAddress.

Equal IPAddress objects always have equal hashcode() values.

◆ mask_to_prefix_len()

int IPAddress::mask_to_prefix_len ( ) const

Returns the prefix length equivalent to this prefix mask, or -1 if this is not a prefix mask.

Maintains the invariant that make_prefix(prefix_len).mask_to_prefix_len() == prefix_len.

See also
make_prefix

◆ matches_prefix()

bool IPAddress::matches_prefix ( IPAddress  addr,
IPAddress  mask 
) const
inline

Return true iff this address matches the address prefix addr/mask.

Parameters
addrprefix address
maskprefix mask

Equivalent to (addr & mask) == (*this & mask). The prefix address addr may be nonzero outside the mask.

◆ mask_as_specific()

bool IPAddress::mask_as_specific ( IPAddress  mask) const
inline

Return true iff this address, considered as a prefix mask, is at least as specific as mask.

Parameters
maskprefix mask

Longer prefix masks are more specific than shorter ones. For example, make_prefix(20).mask_as_specific(make_prefix(18)) is true, but make_prefix(10).mask_as_specific(make_prefix(14)) is false.

Equivalent to (*this & mask) == mask.

◆ mask_more_specific()

bool IPAddress::mask_more_specific ( IPAddress  mask) const
inline

Return true iff this prefix mask is more specific than mask.

Parameters
maskprefix mask

Both this address and mask must be prefix masks – i.e., mask_to_prefix_len() returns 0-32. Returns true iff this address contains a longer prefix than mask. For example, make_prefix(20).mask_more_specific(make_prefix(18)) is true, but make_prefix(20).mask_more_specific(make_prefix(20)) is false.

◆ operator&=()

IPAddress & IPAddress::operator&= ( IPAddress  a)
inline

Assign this address to its bitwise-and with a.

◆ operator|=()

IPAddress & IPAddress::operator|= ( IPAddress  a)
inline

Assign this address to its bitwise-or with a.

◆ operator^=()

IPAddress & IPAddress::operator^= ( IPAddress  a)
inline

Assign this address to its bitwise-xor with a.

◆ unparse()

String IPAddress::unparse ( ) const

Unparses this address into a dotted-quad format String.

Examples include "0.0.0.0" and "18.26.4.9". Maintains the invariant that, for an IPAddress a, IPAddress(a.unparse()) == a.

◆ unparse_mask()

String IPAddress::unparse_mask ( ) const

Unparses this address into IP address mask format: either a prefix length or unparse().

If mask_to_prefix_len() >= 0, then returns that value as a base-10 String; otherwise, returns unparse(). Example results include "8" (for 255.0.0.0) and "18.26.4.9".

◆ unparse_with_mask()

String IPAddress::unparse_with_mask ( IPAddress  mask) const

Unparses an address prefix, specified by address and mask, into "address/mask" format.

Parameters
maskthe address mask

Equivalent to unparse() + "/" + mask.unparse_mask().

◆ s()

String IPAddress::s ( ) const
inline

Unparse this address into a dotted-quad format String.

Deprecated:
The unparse() function should be preferred to s().
See also
unparse

◆ operator String()

IPAddress::operator String ( ) const
inline

Unparse this address into a dotted-quad format String.

Deprecated:
The unparse() function should be preferred to this cast.
See also
unparse

Friends And Related Symbol Documentation

◆ operator==() [1/2]

bool operator== ( IPAddress  a,
IPAddress  b 
)
related

Compare two IPAddress objects for equality.

◆ operator==() [2/2]

bool operator== ( IPAddress  a,
uint32_t  b 
)
related

Compare an IPAddress with a network-byte-order address value for equality.

Parameters
aan address
ban address value in network byte order

◆ operator!=() [1/2]

bool operator!= ( IPAddress  a,
IPAddress  b 
)
related

Compare two IPAddress objects for inequality.

◆ operator!=() [2/2]

bool operator!= ( IPAddress  a,
uint32_t  b 
)
related

Compare an IPAddress with a network-byte-order address value for inequality.

Parameters
aan address
ban address value in network byte order

◆ operator&()

IPAddress operator& ( IPAddress  a,
IPAddress  b 
)
related

Calculate the IPAddress representing the bitwise-and of a and b.

◆ operator|()

IPAddress operator| ( IPAddress  a,
IPAddress  b 
)
related

Calculate the IPAddress representing the bitwise-or of a and b.

◆ operator^()

IPAddress operator^ ( IPAddress  a,
IPAddress  b 
)
related

Calculate the IPAddress representing the bitwise-xor of a and b.

◆ operator~()

IPAddress operator~ ( IPAddress  a)
related

Calculate the IPAddress representing the bitwise complement of a.


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