LibSWOC++ 1.5.14
Solid Wall of C++
Loading...
Searching...
No Matches
swoc::IP4Addr Class Reference

#include <IPAddr.h>

Collaboration diagram for swoc::IP4Addr:
Collaboration graph

Public Member Functions

constexpr IP4Addr ()=default
 Default constructor - ANY address.
 
 IP4Addr (self_type const &that)=default
 Copy constructor.
 
constexpr IP4Addr (in_addr_t addr)
 
 IP4Addr (sockaddr_in const *s)
 Construct from sockaddr_in.
 
 IP4Addr (string_view const &text)
 
self_typeoperator= (self_type const &that)=default
 Self assignment.
 
self_typeoperator= (in_addr_t ip)
 Assign from IPv4 raw address.
 
self_typeoperator= (sockaddr_in const *sa)
 Set to the address in addr.
 
self_typeoperator++ ()
 Increment address.
 
self_typeoperator-- ()
 Decrement address.
 
uint8_t operator[] (unsigned idx) const
 
self_typeoperator&= (IPMask const &mask)
 Apply mask to address, leaving the network portion.
 
self_typeoperator|= (IPMask const &mask)
 Apply mask to address, creating the broadcast address.
 
sockaddr * copy_to (sockaddr *sa) const
 
sockaddr_in * copy_to (sockaddr_in *sin) const
 
in_addr_t network_order () const
 
in_addr_t host_order () const
 
bool load (string_view const &text)
 
int cmp (self_type const &that) const
 Standard ternary compare.
 
constexpr sa_family_t family () const
 
bool is_any () const
 
bool is_multicast () const
 
bool is_loopback () const
 
bool is_link_local () const
 
bool is_private () const
 
self_typeoperator<<= (unsigned n)
 
self_typeoperator>>= (unsigned n)
 
self_typeoperator&= (self_type const &that)
 
self_typeoperator|= (self_type const &that)
 

Static Public Member Functions

static constexpr in_addr_t reorder (in_addr_t src)
 

Static Public Attributes

static constexpr size_t SIZE = sizeof(in_addr_t)
 Size of IPv4 address in bytes.
 
static constexpr size_t WIDTH = std::numeric_limits<unsigned char>::digits * SIZE
 
static const self_type MIN {INADDR_ANY}
 Minimum value.
 
static const self_type MAX {INADDR_BROADCAST}
 Maximum value.
 
static constexpr sa_family_t AF_value = AF_INET
 Address family type.
 

Protected Types

using bytes = std::array<uint8_t, 4>
 Access by bytes.
 

Protected Attributes

in_addr_t _addr = INADDR_ANY
 Address in host order.
 

Friends

class IP4Range
 
bool operator== (self_type const &lhs, self_type const &rhs)
 Equality.
 
bool operator!= (self_type const &lhs, self_type const &rhs)
 
bool operator< (self_type const &lhs, self_type const &rhs)
 
bool operator<= (self_type const &lhs, self_type const &rhs)
 

Detailed Description

Storage for an IPv4 address. Stored in host order.

Definition at line 27 of file IPAddr.h.

Member Typedef Documentation

◆ bytes

using swoc::IP4Addr::bytes = std::array<uint8_t, 4>
protected

Access by bytes.

Definition at line 185 of file IPAddr.h.

Constructor & Destructor Documentation

◆ IP4Addr() [1/3]

swoc::IP4Addr::IP4Addr ( in_addr_t addr)
inlineexplicitconstexpr

Construct using IPv4 addr (in host order).

Note
Host order seems odd, but all of the standard network macro values such as INADDR_LOOPBACK are in host order.

Definition at line 765 of file IPAddr.h.

◆ IP4Addr() [2/3]

swoc::IP4Addr::IP4Addr ( sockaddr_in const * s)
explicit

Construct from sockaddr_in.

Definition at line 342 of file swoc_ip.cc.

◆ IP4Addr() [3/3]

swoc::IP4Addr::IP4Addr ( string_view const & text)
inline

Construct from text representation. If the text is invalid the result is INADDR_ANY

Definition at line 767 of file IPAddr.h.

Member Function Documentation

◆ cmp()

int swoc::IP4Addr::cmp ( IP4Addr::self_type const & that) const
inline

Standard ternary compare.

Definition at line 916 of file IPAddr.h.

◆ copy_to() [1/2]

sockaddr * swoc::IP4Addr::copy_to ( sockaddr * sa) const
inline

Update socket address with this address.

Parameters
saSocket address.
Returns
See also

sa is assumed to be large enough to hold an IPv4 address.

Definition at line 831 of file IPAddr.h.

◆ copy_to() [2/2]

sockaddr_in * swoc::IP4Addr::copy_to ( sockaddr_in * sin) const

Update socket address with this address.

Parameters
sinIPv4 socket address.
Returns
@sin

Definition at line 351 of file swoc_ip.cc.

◆ family()

sa_family_t swoc::IP4Addr::family ( ) const
inlineconstexpr

Get the IP address family.

Returns
AF_INET
Note
Useful primarily for template classes.

Definition at line 774 of file IPAddr.h.

◆ host_order()

in_addr_t swoc::IP4Addr::host_order ( ) const
inline
Returns
The address in host order.

Definition at line 820 of file IPAddr.h.

◆ is_any()

bool swoc::IP4Addr::is_any ( ) const
inline
Returns
true if this is the "any" address, false if not.

Definition at line 886 of file IPAddr.h.

◆ is_link_local()

bool swoc::IP4Addr::is_link_local ( ) const
inline
Returns
true if the address is in the link local network.

Definition at line 901 of file IPAddr.h.

◆ is_loopback()

bool swoc::IP4Addr::is_loopback ( ) const
inline
Returns
true if this is a loopback address, false if not.

Definition at line 891 of file IPAddr.h.

◆ is_multicast()

bool swoc::IP4Addr::is_multicast ( ) const
inline
Returns
true if this is a multicast address, false if not.

Definition at line 896 of file IPAddr.h.

◆ is_private()

bool swoc::IP4Addr::is_private ( ) const
inline
Returns
true if the address is private.

Definition at line 906 of file IPAddr.h.

◆ load()

bool swoc::IP4Addr::load ( std::string_view const & text)

Parse IPv4 address.

Parameters
textText to parse.
Returns
true if text is a valid IPv4 address, false otherwise.

Whitespace is trimmed from @text before parsing. If the parse fails this is set to INADDR_ANY.

of octets

Definition at line 299 of file swoc_ip.cc.

◆ network_order()

in_addr_t swoc::IP4Addr::network_order ( ) const
inline
Returns
The address in network order.

Definition at line 815 of file IPAddr.h.

◆ operator&=() [1/2]

IP4Addr & swoc::IP4Addr::operator&= ( IPMask const & mask)
inline

Apply mask to address, leaving the network portion.

Definition at line 874 of file IPAddr.h.

◆ operator&=() [2/2]

auto swoc::IP4Addr::operator&= ( self_type const & that)
inline

Bitwise AND.

Parameters
thatSource address.
Returns
this.

The bits in this are set to the bitwise AND of the corresponding bits in this and that.

Definition at line 791 of file IPAddr.h.

◆ operator++()

auto swoc::IP4Addr::operator++ ( )
inline

Increment address.

Definition at line 803 of file IPAddr.h.

◆ operator--()

auto swoc::IP4Addr::operator-- ( )
inline

Decrement address.

Definition at line 809 of file IPAddr.h.

◆ operator<<=()

auto swoc::IP4Addr::operator<<= ( unsigned n)
inline

Left shift.

Parameters
nNumber of bits to shift left.
Returns
this.

Definition at line 778 of file IPAddr.h.

◆ operator=() [1/2]

auto swoc::IP4Addr::operator= ( in_addr_t ip)
inline

Assign from IPv4 raw address.

Definition at line 825 of file IPAddr.h.

◆ operator=() [2/2]

auto swoc::IP4Addr::operator= ( sockaddr_in const * sa)

Set to the address in addr.

Definition at line 345 of file swoc_ip.cc.

◆ operator>>=()

auto swoc::IP4Addr::operator>>= ( unsigned n)
inline

Right shift.

Parameters
nNumber of bits to shift right.
Returns
this.

Definition at line 785 of file IPAddr.h.

◆ operator[]()

uint8_t swoc::IP4Addr::operator[] ( unsigned idx) const
inline

Byte access.

Parameters
idxByte index.
Returns
The byte at idx in the address (network order).

For convenience, this returns in "text order" of the octets.

Definition at line 911 of file IPAddr.h.

◆ operator|=() [1/2]

IP4Addr & swoc::IP4Addr::operator|= ( IPMask const & mask)
inline

Apply mask to address, creating the broadcast address.

Definition at line 880 of file IPAddr.h.

◆ operator|=() [2/2]

auto swoc::IP4Addr::operator|= ( self_type const & that)
inline

Bitwise OR.

Parameters
thatSource address.
Returns
this.

The bits in this are set to the bitwise OR of the corresponding bits in this and that.

Definition at line 797 of file IPAddr.h.

◆ reorder()

in_addr_t swoc::IP4Addr::reorder ( in_addr_t src)
staticconstexpr

Convert between network and host order.

Parameters
srcInput address.
Returns
src with the byte reversed.

This performs the same computation as ntohl and htonl but is constexpr to be usable in situations those two functions are not.

Definition at line 921 of file IPAddr.h.

Friends And Related Symbol Documentation

◆ IP4Range

friend class IP4Range
friend

Definition at line 29 of file IPAddr.h.

◆ operator!=

bool operator!= ( self_type const & lhs,
self_type const & rhs )
friend
Returns
true if lhs is equal to rhs.

Definition at line 845 of file IPAddr.h.

◆ operator<

bool operator< ( self_type const & lhs,
self_type const & rhs )
friend
Returns
true if lhs is less than rhs (host order).

Definition at line 850 of file IPAddr.h.

◆ operator<=

bool operator<= ( self_type const & lhs,
self_type const & rhs )
friend
Returns
true if lhs is less than or equal torhs (host order).

Definition at line 856 of file IPAddr.h.

◆ operator==

bool operator== ( self_type const & lhs,
self_type const & rhs )
friend

Equality.

Definition at line 839 of file IPAddr.h.

Member Data Documentation

◆ _addr

in_addr_t swoc::IP4Addr::_addr = INADDR_ANY
protected

Address in host order.

Definition at line 192 of file IPAddr.h.

◆ AF_value

sa_family_t swoc::IP4Addr::AF_value = AF_INET
staticconstexpr

Address family type.

Definition at line 37 of file IPAddr.h.

◆ MAX

IP4Addr const swoc::IP4Addr::MAX {INADDR_BROADCAST}
static

Maximum value.

Definition at line 36 of file IPAddr.h.

◆ MIN

IP4Addr const swoc::IP4Addr::MIN {INADDR_ANY}
static

Minimum value.

Definition at line 35 of file IPAddr.h.

◆ SIZE

size_t swoc::IP4Addr::SIZE = sizeof(in_addr_t)
staticconstexpr

Size of IPv4 address in bytes.

Definition at line 32 of file IPAddr.h.

◆ WIDTH

size_t swoc::IP4Addr::WIDTH = std::numeric_limits<unsigned char>::digits * SIZE
staticconstexpr

of bits in an address.

Definition at line 33 of file IPAddr.h.


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