ipaddress 1.1.0
Loading...
Searching...
No Matches
ip_network_base< Base > Class Template Reference

Template base class for representing a network of IP addresses. More...

#include <ip-network-base.hpp>

Inheritance diagram for ip_network_base< Base >:

Public Types

using ip_address_type = typename Base::ip_address_type
 The IP address type used by the network.
 
using uint_type = typename ip_address_type::uint_type
 Unsigned integer type used for the underlying IP address representation.
 

Public Member Functions

constexpr ip_network_base () noexcept
 Constructs a new IP network base object.
 
constexpr inline bool contains (const ip_address_type &address) const noexcept
 Checks if the given IP address is contained within this network.
 
constexpr inline bool overlaps (const ip_network_base &other) const noexcept
 Determines if this network overlaps with another network.
 
constexpr inline bool subnet_of (const ip_network_base &other) const noexcept
 Checks if this network is a subnet of another network.
 
constexpr inline bool supernet_of (const ip_network_base &other) const noexcept
 Checks if this network is a supernet of another network.
 
inline std::string to_string (format fmt=format::compressed) const
 Converts the network to a string representation.
 
inline std::wstring to_wstring (format fmt=format::compressed) const
 Converts the network to a string representation.
 
inline std::u16string to_u16string (format fmt=format::compressed) const
 Converts the network to a string representation.
 
inline std::u32string to_u32string (format fmt=format::compressed) const
 Converts the network to a string representation.
 
inline std::u8string to_u8string (format fmt=format::compressed) const
 Converts the network to a string representation.
 
constexpr inline void swap (ip_network_base &network) noexcept
 Swaps the contents of this network with another network.
 
constexpr inline size_t hash () const noexcept
 Calculates a hash value for the network.
 
constexpr inline const ip_address_typenetwork_address () const noexcept
 Retrieves the network address of this network.
 
constexpr inline ip_address_type broadcast_address () const noexcept
 Retrieves the broadcast address of this network.
 
constexpr inline const ip_address_typenetmask () const noexcept
 Retrieves the netmask of this network.
 
constexpr inline ip_address_type hostmask () const noexcept
 Retrieves the hostmask of this network.
 
constexpr inline size_t prefixlen () const noexcept
 Retrieves the prefix length of this network.
 
constexpr inline bool is_multicast () const noexcept
 Checks if the network is a multicast network.
 
constexpr inline bool is_private () const noexcept
 Checks if the network is a private network.
 
constexpr inline bool is_global () const noexcept
 Checks if the network is a global network.
 
constexpr inline bool is_reserved () const noexcept
 Checks if the network is a reserved network.
 
constexpr inline bool is_loopback () const noexcept
 Checks if the network is a loopback network.
 
constexpr inline bool is_link_local () const noexcept
 Checks if the network is a link-local network.
 
constexpr inline bool is_unspecified () const noexcept
 Checks if the network is an unspecified network.
 
constexpr inline uint_type addresses_count () const noexcept
 Calculates the total number of addresses in the network.
 
constexpr inline hosts_sequence< ip_address_typehosts () const noexcept
 Retrieves a sequence of host addresses in the network.
 
constexpr inline exclude_network_sequence< ip_network_base< Base > > address_exclude (const ip_network_base &other) const
 Computes the network definitions resulting from removing the given network from this one.
 
constexpr inline exclude_network_sequence< ip_network_base< Base > > address_exclude (const ip_network_base &other, error_code &code) const noexcept
 Computes the network definitions resulting from removing the given network from this one, with error handling.
 
constexpr inline subnets_sequence< ip_network_base< Base > > subnets (size_t prefixlen_diff=1, optional< size_t > new_prefixlen=nullptr) const
 Generates a sequence of subnets from this network.
 
constexpr inline subnets_sequence< ip_network_base< Base > > subnets (error_code &code, size_t prefixlen_diff=1, optional< size_t > new_prefixlen=nullptr) const noexcept
 Generates a sequence of subnets from this network with error handling.
 
constexpr inline ip_network_base< Base > supernet (size_t prefixlen_diff=1, optional< size_t > new_prefixlen=nullptr) const
 Generates a supernet from this network.
 
constexpr inline ip_network_base< Base > supernet (error_code &code, size_t prefixlen_diff=1, optional< size_t > new_prefixlen=nullptr) const noexcept
 Generates a supernet from this network with error handling.
 
template<typename T >
inline operator std::basic_string< T, std::char_traits< T >, std::allocator< T > > () const
 Converts the ip network object to a std::string.
 
constexpr inline bool operator== (const ip_network_base &rhs) const noexcept
 Equality comparison operator.
 
constexpr inline bool operator!= (const ip_network_base &rhs) const noexcept
 Inequality comparison operator.
 
constexpr inline bool operator< (const ip_network_base &rhs) const noexcept
 Less than comparison operator.
 
constexpr inline bool operator> (const ip_network_base &rhs) const noexcept
 Greater than comparison operator.
 
constexpr inline bool operator<= (const ip_network_base &rhs) const noexcept
 Less than or equal to comparison operator.
 
constexpr inline bool operator>= (const ip_network_base &rhs) const noexcept
 Greater than or equal to comparison operator.
 

Static Public Member Functions

template<fixed_string FixedString, bool Strict = true>
static consteval ip_network_base parse () noexcept
 Parses a network address and prefix from a fixed string at compile time.
 
static constexpr ip_network_base parse (std::string_view address, bool strict=true)
 Parses a network address and prefix from a string view.
 
static constexpr ip_network_base parse (std::wstring_view address, bool strict=true)
 Parses a network address and prefix from a wide string view.
 
static constexpr ip_network_base parse (std::u8string_view address, bool strict=true)
 Parses a network address and prefix from UTF-8 string view.
 
static constexpr ip_network_base parse (std::u16string_view address, bool strict=true)
 Parses a network address and prefix from UTF-16 string view.
 
static constexpr ip_network_base parse (std::u32string_view address, bool strict=true)
 Parses a network address and prefix from UTF-32 string view.
 
static constexpr ip_network_base parse (std::string_view address, error_code &code, bool strict=true) noexcept
 Parses a network address and prefix from a string view with error handling.
 
static constexpr ip_network_base parse (std::wstring_view address, error_code &code, bool strict=true) noexcept
 Parses a network address and prefix from a wide string view with error handling.
 
static constexpr ip_network_base parse (std::u8string_view address, error_code &code, bool strict=true) noexcept
 Parses a network address and prefix from UTF-8 string view with error handling.
 
static constexpr ip_network_base parse (std::u16string_view address, error_code &code, bool strict=true) noexcept
 Parses a network address and prefix from UTF-16 string view with error handling.
 
static constexpr ip_network_base parse (std::u32string_view address, error_code &code, bool strict=true) noexcept
 Parses a network address and prefix from UTF-32 string view with error handling.
 
template<typename T , size_t N>
static constexpr inline ip_network_base parse (const T(&address)[N], bool strict=true)
 Parses a network address and prefix from a character array.
 
template<typename T , size_t N>
static constexpr inline ip_network_base parse (const T(&address)[N], error_code &code, bool strict=true) noexcept
 Parses a network address and prefix from a character array and reports errors through an error code.
 
static constexpr inline ip_network_base from_address (const ip_address_type &address, size_t prefixlen=ip_address_type::max_prefixlen, bool strict=true)
 Creates an ip network object from a given IP address and prefix length.
 
static constexpr inline ip_network_base from_address (const ip_address_type &address, error_code &code, size_t prefixlen=ip_address_type::max_prefixlen, bool strict=true) noexcept
 Creates an ip network object from a given IP address and prefix length, with error handling.
 

Detailed Description

template<typename Base>
class ipaddress::ip_network_base< Base >

Template base class for representing a network of IP addresses.

This class provides the foundational elements for representing a network of IP addresses, offering methods to iterate over them and perform common network-related operations. It is designed to be flexible and work with any IP address type that conforms to a specific interface, making it suitable for both IPv4 and IPv6 networks.

Template Parameters
BaseThe base class from which ip_network_base inherits. Should be a type that represents an individual IP address.

Constructor & Destructor Documentation

◆ ip_network_base()

template<typename Base >
constexpr ip_network_base ( )
inlinenoexcept

Constructs a new IP network base object.

Initializes a new instance of the ip_network_base class with the maximum prefix length for the IP address type, effectively setting the network address to the default address and the netmask to the maximum value.

Member Function Documentation

◆ parse() [1/13]

template<typename Base >
template<fixed_string FixedString, bool Strict = true>
static consteval ip_network_base parse ( )
inlinestaticnoexcept

Parses a network address and prefix from a fixed string at compile time.

This template function allows for the compile-time parsing of an IP network address and prefix length from a fixed string. It is enabled only when non-type template parameters are supported by the compiler.

Template Parameters
FixedStringA string literal representing the IP network in "address/prefix" format.
StrictA boolean flag indicating whether to perform strict validation of the address.
Returns
A consteval ip network object representing the parsed network.

◆ parse() [2/13]

template<typename Base >
static constexpr ip_network_base parse ( std::string_view address,
bool strict = true )
inlinestatic

Parses a network address and prefix from a string view.

This function parses an IP network address and prefix length from a string view. It is available when the compiler supports C++17 or later.

Parameters
[in]addressA string view representing the IP network in "address/prefix" format.
[in]strictA boolean flag indicating whether to perform strict validation of the address.
Returns
An ip network object representing the parsed network.
Exceptions
parse_errorException caused by invalid input string.
Note
This method is available for C++17 and later versions.
Remarks
For C++ versions prior to C++17, member functions with std::string and C-strings will be used instead.

◆ parse() [3/13]

template<typename Base >
static constexpr ip_network_base parse ( std::wstring_view address,
bool strict = true )
inlinestatic

Parses a network address and prefix from a wide string view.

This function parses an IP network address and prefix length from a wide string view. It is available when the compiler supports C++17 or later.

Parameters
[in]addressThe wide string view representing the IP network in "address/prefix" format.
[in]strictA boolean flag indicating whether to perform strict validation of the address.
Returns
An ip network object representing the parsed network.
Exceptions
parse_errorException caused by invalid input string.
Note
This method is available for C++17 and later versions.
Remarks
For C++ versions prior to C++17, member functions with std::wstring and C-strings will be used instead.

◆ parse() [4/13]

template<typename Base >
static constexpr ip_network_base parse ( std::u8string_view address,
bool strict = true )
inlinestatic

Parses a network address and prefix from UTF-8 string view.

This function parses an IP network address and prefix length from UTF-8 string view. It leverages the char8_t type introduced in C++20 to handle UTF-8 strings natively.

Parameters
[in]addressA UTF-8 string view representing the IP network in "address/prefix" format.
[in]strictA boolean flag indicating whether to perform strict validation of the address.
Returns
An ip network object representing the parsed network.
Exceptions
parse_errorException caused by invalid input string.
Note
This method is available for C++20 and later versions where char8_t is supported.

◆ parse() [5/13]

template<typename Base >
static constexpr ip_network_base parse ( std::u16string_view address,
bool strict = true )
inlinestatic

Parses a network address and prefix from UTF-16 string view.

This function parses an IP network address and prefix length from UTF-16 string view. It is available when the compiler supports C++17 or later.

Parameters
[in]addressThe UTF-16 string view representing the IP network in "address/prefix" format.
[in]strictA boolean flag indicating whether to perform strict validation of the address.
Returns
An ip network object representing the parsed network.
Exceptions
parse_errorException caused by invalid input string.
Note
This method is available for C++17 and later versions.
Remarks
For C++ versions prior to C++17, member functions with std::u16string and C-strings will be used instead.

◆ parse() [6/13]

template<typename Base >
static constexpr ip_network_base parse ( std::u32string_view address,
bool strict = true )
inlinestatic

Parses a network address and prefix from UTF-32 string view.

This function parses an IP network address and prefix length from UTF-32 string view. It is available when the compiler supports C++17 or later.

Parameters
[in]addressThe UTF-32 string view representing the IP network in "address/prefix" format.
[in]strictA boolean flag indicating whether to perform strict validation of the address.
Returns
An ip network object representing the parsed network.
Exceptions
parse_errorException caused by invalid input string.
Note
This method is available for C++17 and later versions.
Remarks
For C++ versions prior to C++17, member functions with std::u32string and C-strings will be used instead.

◆ parse() [7/13]

template<typename Base >
static constexpr ip_network_base parse ( std::string_view address,
error_code & code,
bool strict = true )
inlinestaticnoexcept

Parses a network address and prefix from a string view with error handling.

This function parses an IP network address and prefix length from a string view, providing an error code for exception-free error handling.

Parameters
[in]addressThe string view representing the IP network in "address/prefix" format.
[out]codeAn error_code object that will be set if an error occurs during parsing.
[in]strictA boolean flag indicating whether to perform strict validation of the address.
Returns
An ip network object representing the parsed network.
Note
This method is available for C++17 and later versions.
Remarks
For C++ versions prior to C++17, member functions with std::string and C-strings will be used instead.

◆ parse() [8/13]

template<typename Base >
static constexpr ip_network_base parse ( std::wstring_view address,
error_code & code,
bool strict = true )
inlinestaticnoexcept

Parses a network address and prefix from a wide string view with error handling.

This function parses an IP network address and prefix length from a wide string view, providing an error code for exception-free error handling.

Parameters
[in]addressThe wide string view representing the IP network in "address/prefix" format.
[out]codeAn error_code object that will be set if an error occurs during parsing.
[in]strictA boolean flag indicating whether to perform strict validation of the address.
Returns
An ip network object representing the parsed network.
Note
This method is available for C++17 and later versions.
Remarks
For C++ versions prior to C++17, member functions with std::wstring and C-strings will be used instead.

◆ parse() [9/13]

template<typename Base >
static constexpr ip_network_base parse ( std::u8string_view address,
error_code & code,
bool strict = true )
inlinestaticnoexcept

Parses a network address and prefix from UTF-8 string view with error handling.

This function parses an IP network address and prefix length from UTF-8 string view, providing an error code for exception-free error handling.

Parameters
[in]addressThe UTF-8 string view representing the IP network in "address/prefix" format.
[out]codeAn error_code object that will be set if an error occurs during parsing.
[in]strictA boolean flag indicating whether to perform strict validation of the address.
Returns
An ip network object representing the parsed network.
Note
This method is available for C++20 and later versions where char8_t is supported.

◆ parse() [10/13]

template<typename Base >
static constexpr ip_network_base parse ( std::u16string_view address,
error_code & code,
bool strict = true )
inlinestaticnoexcept

Parses a network address and prefix from UTF-16 string view with error handling.

This function parses an IP network address and prefix length from UTF-16 string view, providing an error code for exception-free error handling.

Parameters
[in]addressThe UTF-16 string view representing the IP network in "address/prefix" format.
[out]codeAn error_code object that will be set if an error occurs during parsing.
[in]strictA boolean flag indicating whether to perform strict validation of the address.
Returns
An ip network object representing the parsed network.
Note
This method is available for C++17 and later versions.
Remarks
For C++ versions prior to C++17, member functions with std::u16string and C-strings will be used instead.

◆ parse() [11/13]

template<typename Base >
static constexpr ip_network_base parse ( std::u32string_view address,
error_code & code,
bool strict = true )
inlinestaticnoexcept

Parses a network address and prefix from UTF-32 string view with error handling.

This function parses an IP network address and prefix length from UTF-32 string view, providing an error code for exception-free error handling.

Parameters
[in]addressThe UTF-32 string view representing the IP network in "address/prefix" format.
[out]codeAn error_code object that will be set if an error occurs during parsing.
[in]strictA boolean flag indicating whether to perform strict validation of the address.
Returns
An ip network object representing the parsed network.
Note
This method is available for C++17 and later versions.
Remarks
For C++ versions prior to C++17, member functions with std::u32string and C-strings will be used instead.

◆ parse() [12/13]

template<typename Base >
template<typename T , size_t N>
static constexpr inline ip_network_base parse ( const T(&) address[N],
bool strict = true )
inlinestatic

Parses a network address and prefix from a character array.

This function parses an IP network address and prefix length from a character array of a specified size. Can check and get the result at compile time.

Template Parameters
TThe character type of the array.
NThe size of the character array.
Parameters
[in]addressThe character array representing the IP network in "address/prefix" format.
[in]strictA boolean flag indicating whether to perform strict validation of the address.
Returns
An ip network object representing the parsed network.

◆ parse() [13/13]

template<typename Base >
template<typename T , size_t N>
static constexpr inline ip_network_base parse ( const T(&) address[N],
error_code & code,
bool strict = true )
inlinestaticnoexcept

Parses a network address and prefix from a character array and reports errors through an error code.

This function parses an IP network address and prefix length from a character array of a specified size and provides an error code if the parsing fails.

Template Parameters
TThe character type of the array.
NThe size of the character array.
Parameters
[in]addressThe character array representing the IP network in "address/prefix" format.
[out]codeAn error_code object that will be set if an error occurs during parsing.
[in]strictA boolean flag indicating whether to perform strict validation of the address.
Returns
An ip network object representing the parsed network.

◆ from_address() [1/2]

template<typename Base >
static constexpr inline ip_network_base from_address ( const ip_address_type & address,
size_t prefixlen = ip_address_type::max_prefixlen,
bool strict = true )
inlinestatic

Creates an ip network object from a given IP address and prefix length.

This static method constructs an ip network object representing the network that includes the given IP address, with the specified prefix length. If 'strict' is true, the address is validated against the netmask.

Parameters
[in]addressThe IP address to use for creating the network.
[in]prefixlenThe prefix length for the network's netmask. Defaults to the maximum prefix length.
[in]strictWhether to validate the address against the netmask.
Returns
An ip network object representing the network.
Exceptions
parse_errorException caused by invalid input string.
Remarks
May throw an exception if the address does not conform to the netmask when strict is true.

◆ from_address() [2/2]

template<typename Base >
static constexpr inline ip_network_base from_address ( const ip_address_type & address,
error_code & code,
size_t prefixlen = ip_address_type::max_prefixlen,
bool strict = true )
inlinestaticnoexcept

Creates an ip network object from a given IP address and prefix length, with error handling.

Similar to the from_address method above, but this version allows for error handling without exceptions. It populates the provided error_code object instead of throwing.

Parameters
[in]addressThe IP address to use for creating the network.
[out]codeAn error_code object that will be set if an error occurs.
[in]prefixlenThe prefix length for the network's netmask. Defaults to the maximum prefix length.
[in]strictWhether to validate the address against the netmask.
Returns
An ip network object representing the network, or an object in an error state if an error occurs.

◆ contains()

template<typename Base >
constexpr inline bool contains ( const ip_address_type & address) const
inlinenoexcept

Checks if the given IP address is contained within this network.

This method determines whether the provided IP address is part of the network represented by this ip network object, based on the network address and netmask.

constexpr auto result1 = ipv4_network::parse("192.0.2.0/28").contains(ipv4_address::parse("192.0.2.6"));
constexpr auto result2 = ipv4_network::parse("192.0.2.0/28").contains(ipv4_address::parse("192.0.3.6"));
std::cout << std::boolalpha << result1 << std::endl;
std::cout << std::boolalpha << result2 << std::endl;
// out:
// true
// false
static consteval inline ip_address_base< ipv4_address_base > parse() noexcept
Static method template to parse an IP address from a fixed string at compile time.
Definition ip-address-base.hpp:123
static consteval ip_network_base parse() noexcept
Parses a network address and prefix from a fixed string at compile time.
Definition ip-network-base.hpp:65
constexpr inline bool contains(const ip_address_type &address) const noexcept
Checks if the given IP address is contained within this network.
Definition ip-network-base.hpp:494
Parameters
[in]addressThe IP address to check.
Returns
true if the address is part of the network, false otherwise.

◆ overlaps()

template<typename Base >
constexpr inline bool overlaps ( const ip_network_base< Base > & other) const
inlinenoexcept

Determines if this network overlaps with another network.

This method checks if there is any overlap between the network represented by this ip network object and another network, meaning if any part of one network lies within the other.

constexpr auto a = ipv4_network::parse("1.2.3.0/24");
constexpr auto b = ipv4_network::parse("1.2.3.0/30");
constexpr auto overlaps = a.overlaps(b);
std::cout << std::boolalpha << overlaps << std::endl;
// out:
// true
constexpr inline bool overlaps(const ip_network_base &other) const noexcept
Determines if this network overlaps with another network.
Definition ip-network-base.hpp:526
Parameters
[in]otherThe other ip network object to compare with.
Returns
true if there is an overlap, false otherwise.

◆ subnet_of()

template<typename Base >
constexpr inline bool subnet_of ( const ip_network_base< Base > & other) const
inlinenoexcept

Checks if this network is a subnet of another network.

This method verifies if the network represented by this ip network object is a subnet of the provided network, meaning if this network is entirely contained within the other network.

constexpr auto a = ipv4_network::parse("192.168.1.0/24");
constexpr auto b = ipv4_network::parse("192.168.1.128/30");
constexpr auto subnet_of = a.subnet_of(b);
std::cout << std::boolalpha << subnet_of << std::endl;
// out:
// false
constexpr inline bool subnet_of(const ip_network_base &other) const noexcept
Checks if this network is a subnet of another network.
Definition ip-network-base.hpp:549
Parameters
[in]otherThe other ip network object to compare with.
Returns
true if this network is a subnet of the other network, false otherwise.

◆ supernet_of()

template<typename Base >
constexpr inline bool supernet_of ( const ip_network_base< Base > & other) const
inlinenoexcept

Checks if this network is a supernet of another network.

This method determines if the network represented by this ip network object is a supernet of the provided network, meaning if the other network is entirely contained within this network.

constexpr auto a = ipv4_network::parse("192.168.1.0/24");
constexpr auto b = ipv4_network::parse("192.168.1.128/30");
constexpr auto supernet_of = a.supernet_of(b);
std::cout << std::boolalpha << supernet_of << std::endl;
// out:
// true
constexpr inline bool supernet_of(const ip_network_base &other) const noexcept
Checks if this network is a supernet of another network.
Definition ip-network-base.hpp:572
Parameters
[in]otherThe other ip network object to compare with.
Returns
True if this network is a supernet of the other network, false otherwise.

◆ to_string()

template<typename Base >
inline std::string to_string ( format fmt = format::compressed) const
inline

Converts the network to a string representation.

This method returns a string representation of the network, combining the network address and the prefix length, formatted according to the specified format.

Parameters
[in]fmtThe format to use for the string representation. Defaults to format::compressed.
Returns
A string representation of the network.

◆ to_wstring()

template<typename Base >
inline std::wstring to_wstring ( format fmt = format::compressed) const
inline

Converts the network to a string representation.

This method returns a string representation of the network, combining the network address and the prefix length, formatted according to the specified format.

Parameters
[in]fmtThe format to use for the string representation. Defaults to format::compressed.
Returns
A string representation of the network.

◆ to_u16string()

template<typename Base >
inline std::u16string to_u16string ( format fmt = format::compressed) const
inline

Converts the network to a string representation.

This method returns a string representation of the network, combining the network address and the prefix length, formatted according to the specified format.

Parameters
[in]fmtThe format to use for the string representation. Defaults to format::compressed.
Returns
A string representation of the network.

◆ to_u32string()

template<typename Base >
inline std::u32string to_u32string ( format fmt = format::compressed) const
inline

Converts the network to a string representation.

This method returns a string representation of the network, combining the network address and the prefix length, formatted according to the specified format.

Parameters
[in]fmtThe format to use for the string representation. Defaults to format::compressed.
Returns
A string representation of the network.

◆ to_u8string()

template<typename Base >
inline std::u8string to_u8string ( format fmt = format::compressed) const
inline

Converts the network to a string representation.

This method returns a string representation of the network, combining the network address and the prefix length, formatted according to the specified format.

Parameters
[in]fmtThe format to use for the string representation. Defaults to format::compressed.
Returns
A string representation of the network.

◆ swap()

template<typename Base >
constexpr inline void swap ( ip_network_base< Base > & network)
inlinenoexcept

Swaps the contents of this network with another network.

This method exchanges the network address, netmask, and prefix length with those of another ip network object.

Parameters
[in,out]networkThe other ip network object to swap contents with.

◆ hash()

template<typename Base >
constexpr inline size_t hash ( ) const
inlinenoexcept

Calculates a hash value for the network.

This method computes a hash value for the network, which can be used for storing the object in hash-based containers.

Returns
A size_t value representing the hash of the network.

◆ network_address()

template<typename Base >
constexpr inline const ip_address_type & network_address ( ) const
inlinenoexcept

Retrieves the network address of this network.

This method returns the network address component of the ip network object.

Returns
A reference to the network address.

◆ broadcast_address()

template<typename Base >
constexpr inline ip_address_type broadcast_address ( ) const
inlinenoexcept

Retrieves the broadcast address of this network.

The broadcast address is the last address in the network and is used to communicate with all hosts on this network.

Returns
The broadcast address of the network as an ip address object.

◆ netmask()

template<typename Base >
constexpr inline const ip_address_type & netmask ( ) const
inlinenoexcept

Retrieves the netmask of this network.

This method returns the netmask of the network. The netmask is used to determine which portion of an IP address is the network portion and which is the host portion.

Returns
A reference to the netmask as an ip address object.

◆ hostmask()

template<typename Base >
constexpr inline ip_address_type hostmask ( ) const
inlinenoexcept

Retrieves the hostmask of this network.

The hostmask identifies the portion of the IP address that is allocated for host identifiers.

Returns
The hostmask of the network as an ip address object.

◆ prefixlen()

template<typename Base >
constexpr inline size_t prefixlen ( ) const
inlinenoexcept

Retrieves the prefix length of this network.

This method returns the prefix length of the network. The prefix length is the number of bits in the network address portion of the IP address and is used to calculate the netmask.

Returns
The prefix length of the network as a size_t value.

◆ is_multicast()

template<typename Base >
constexpr inline bool is_multicast ( ) const
inlinenoexcept

Checks if the network is a multicast network.

This method determines whether the network is a multicast network by checking if both the network address and the broadcast address are multicast addresses.

Returns
true if the network is multicast, false otherwise.
See also
RFC 3171 for IPv4.
RFC 2373 for IPv6.

◆ is_private()

template<typename Base >
constexpr inline bool is_private ( ) const
noexcept

Checks if the network is a private network.

This method determines whether the network is a private network. Private networks are not routed on the global internet.

Returns
true if the network is private, false otherwise.
See also
iana-ipv4-special-registry.
iana-ipv6-special-registry.

◆ is_global()

template<typename Base >
constexpr inline bool is_global ( ) const
noexcept

Checks if the network is a global network.

This method determines whether the network is a global network. Global networks are routable on the internet and are not private.

Returns
true if the network is global, false otherwise.
See also
iana-ipv4-special-registry.
iana-ipv6-special-registry.

◆ is_reserved()

template<typename Base >
constexpr inline bool is_reserved ( ) const
inlinenoexcept

Checks if the network is a reserved network.

This method determines whether the network is reserved by checking if both the network address and the broadcast address are reserved addresses.

Returns
true if the network is reserved, false otherwise.

◆ is_loopback()

template<typename Base >
constexpr inline bool is_loopback ( ) const
inlinenoexcept

Checks if the network is a loopback network.

This method determines whether the network is a loopback network by checking if both the network address and the broadcast address are loopback addresses.

Returns
true if the network is loopback, false otherwise.
See also
RFC 3330 for IPv4.
RFC 2373 for IPv6.

◆ is_link_local()

template<typename Base >
constexpr inline bool is_link_local ( ) const
inlinenoexcept

Checks if the network is a link-local network.

This method determines whether the network is a link-local network by checking if both the network address and the broadcast address are link-local addresses.

Returns
true if the network is link-local, false otherwise.
See also
RFC 3927.

◆ is_unspecified()

template<typename Base >
constexpr inline bool is_unspecified ( ) const
inlinenoexcept

Checks if the network is an unspecified network.

This method determines whether the network is unspecified by checking if both the network address and the broadcast address are unspecified addresses.

Returns
true if the network is unspecified, false otherwise.
See also
RFC 5735 for IPv4.
RFC 2373 for IPv6.

◆ addresses_count()

template<typename Base >
constexpr inline uint_type addresses_count ( ) const
inlinenoexcept

Calculates the total number of addresses in the network.

Returns
The total number of addresses in the network as a uint_type value.

◆ hosts()

template<typename Base >
constexpr inline hosts_sequence< ip_address_type > hosts ( ) const
inlinenoexcept

Retrieves a sequence of host addresses in the network.

This method returns a sequence of host addresses within the network, excluding the network and broadcast addresses.

constexpr auto hosts_sequence = ipv4_network::parse("192.0.2.0/29").hosts();
for (const auto& addr : hosts_sequence) {
std::cout << addr << std::endl;
}
// out:
// 192.0.2.1
// 192.0.2.2
// 192.0.2.3
// 192.0.2.4
// 192.0.2.5
// 192.0.2.6
constexpr inline hosts_sequence< ip_address_type > hosts() const noexcept
Retrieves a sequence of host addresses in the network.
Definition ip-network-base.hpp:877
Returns
A hosts_sequence object representing the sequence of host addresses.
Return values
Ipv4The usable hosts are all the IP addresses that belong to the network, except the network address itself and the network broadcast address. For networks with a mask length of 31, the network address and network broadcast address are also included in the result. Networks with a mask of 32 will return a list containing the single host address.
Ipv6The usable hosts are all the IP addresses that belong to the network, except the Subnet-Router anycast address. For networks with a mask length of 127, the Subnet-Router anycast address is also included in the result. Networks with a mask of 128 will return a list containing the single host address.
Warning
Please note that with IPv6, the number of addresses can be so large that iterating through them all may be practically impossible. Therefore, use the hosts() method cautiously to avoid endlessly retrieving addresses.
Remarks
hosts_sequence uses lazy evaluation to iterate addresses.

◆ address_exclude() [1/2]

template<typename Base >
constexpr inline exclude_network_sequence< ip_network_base< Base > > address_exclude ( const ip_network_base< Base > & other) const
inline

Computes the network definitions resulting from removing the given network from this one.

constexpr auto a = ipv4_network::parse("192.0.2.0/28");
constexpr auto b = ipv4_network::parse("192.0.2.1/32");
constexpr auto exclude_sequence = a.address_exclude(b);
for (const auto& net : exclude_sequence) {
std::cout << net << std::endl;
}
// out:
// 192.0.2.8/29
// 192.0.2.4/30
// 192.0.2.2/31
// 192.0.2.0/32
Parameters
[in]otherThe other network to exclude from this network.
Returns
An exclude_network_sequence object representing the remaining address ranges.
Exceptions
logic_errorRaise if network is not completely contained in this network.
Remarks
exclude_network_sequence uses lazy evaluation to iterate networks.

◆ address_exclude() [2/2]

template<typename Base >
constexpr inline exclude_network_sequence< ip_network_base< Base > > address_exclude ( const ip_network_base< Base > & other,
error_code & code ) const
inlinenoexcept

Computes the network definitions resulting from removing the given network from this one, with error handling.

constexpr auto a = ipv4_network::parse("192.0.2.0/28");
constexpr auto b = ipv4_network::parse("192.0.2.1/32");
auto exclude_sequence = a.address_exclude(b, err);
if (err == error_code::no_error) {
for (const auto& net : exclude_sequence) {
std::cout << net << std::endl;
}
}
// out:
// 192.0.2.8/29
// 192.0.2.4/30
// 192.0.2.2/31
// 192.0.2.0/32
@ no_error
Indicates the absence of any errors.
Parameters
[in]otherThe other network to exclude from this network.
[out]codeAn error_code object that will be set if an error occurs during the operation.
Returns
An exclude_network_sequence object representing the remaining address ranges, or an empty sequence if an error occurs.
Remarks
exclude_network_sequence uses lazy evaluation to iterate networks.

◆ subnets() [1/2]

template<typename Base >
constexpr inline subnets_sequence< ip_network_base< Base > > subnets ( size_t prefixlen_diff = 1,
optional< size_t > new_prefixlen = nullptr ) const
inline

Generates a sequence of subnets from this network.

The subnets that join to make the current network definition, depending on the argument values. prefixlen_diff is the amount our prefix length should be increased by. new_prefix is the desired new prefix of the subnets; it must be larger than our prefix. One and only one of prefixlen_diff and new_prefix must be set.

constexpr auto subnets_sequence = ipv4_network::parse("192.0.2.0/24").subnets(2);
for (const auto& net : subnets_sequence) {
std::cout << net << std::endl;
}
// out:
// 192.0.2.0/26
// 192.0.2.64/26
// 192.0.2.128/26
// 192.0.2.192/26
constexpr inline subnets_sequence< ip_network_base< Base > > subnets(size_t prefixlen_diff=1, optional< size_t > new_prefixlen=nullptr) const
Generates a sequence of subnets from this network.
Definition ip-network-base.hpp:983
A sequence container for subnet ranges within a network.
Definition ip-network-iterator.hpp:678
Parameters
[in]prefixlen_diffThe difference in prefix length for the subnets. Defaults to 1.
[in]new_prefixlenAn optional new prefix length for the subnets. If not specified, the prefix length is determined by adding prefixlen_diff to the current prefix length.
Returns
A subnets_sequence object representing the sequence of subnets.
Exceptions
logic_errorRaised if the operation cannot be performed due to invalid parameters or prefix length.
Remarks
subnets_sequence uses lazy evaluation to iterate over the subnets.

◆ subnets() [2/2]

template<typename Base >
constexpr inline subnets_sequence< ip_network_base< Base > > subnets ( error_code & code,
size_t prefixlen_diff = 1,
optional< size_t > new_prefixlen = nullptr ) const
inlinenoexcept

Generates a sequence of subnets from this network with error handling.

The subnets that join to make the current network definition, depending on the argument values. prefixlen_diff is the amount our prefix length should be increased by. new_prefix is the desired new prefix of the subnets; it must be larger than our prefix. One and only one of prefixlen_diff and new_prefix must be set.

auto subnets_sequence = ipv4_network::parse("192.0.2.0/24").subnets(err, 2);
if (err == error_code::no_error) {
for (const auto& net : subnets_sequence) {
std::cout << net << std::endl;
}
}
// out:
// 192.0.2.0/26
// 192.0.2.64/26
// 192.0.2.128/26
// 192.0.2.192/26
Parameters
[out]codeAn error_code object that will be set if an error occurs during the operation.
[in]prefixlen_diffThe difference in prefix length for the subnets. Defaults to 1.
[in]new_prefixlenAn optional new prefix length for the subnets. If not specified, the prefix length is determined by adding prefixlen_diff to the current prefix length.
Returns
A subnets_sequence object representing the sequence of subnets, or an empty sequence if an error occurs.
Remarks
subnets_sequence uses lazy evaluation to iterate over the subnets.

◆ supernet() [1/2]

template<typename Base >
constexpr inline ip_network_base< Base > supernet ( size_t prefixlen_diff = 1,
optional< size_t > new_prefixlen = nullptr ) const
inline

Generates a supernet from this network.

The supernet containing this network definition, depending on the argument values. prefixlen_diff is the amount our prefix length should be decreased by. new_prefix is the desired new prefix of the supernet; it must be smaller than our prefix. One and only one of prefixlen_diff and new_prefix must be set.

constexpr auto supernet = ipv4_network::parse("192.0.2.0/24").supernet(2);
std::cout << supernet << std::endl;
// out:
// 192.0.0.0/22
constexpr inline ip_network_base< Base > supernet(size_t prefixlen_diff=1, optional< size_t > new_prefixlen=nullptr) const
Generates a supernet from this network.
Definition ip-network-base.hpp:1072
Parameters
[in]prefixlen_diffThe amount by which the prefix length should be decreased. Defaults to 1.
[in]new_prefixlenAn optional new prefix length for the supernet. If not specified, the prefix length is determined by subtracting prefixlen_diff from the current prefix length.
Returns
An ip network object representing the supernet, or the current network if an error occurs.
Exceptions
logic_errorRaised if the operation cannot be performed due to invalid parameters or prefix length.

◆ supernet() [2/2]

template<typename Base >
constexpr inline ip_network_base< Base > supernet ( error_code & code,
size_t prefixlen_diff = 1,
optional< size_t > new_prefixlen = nullptr ) const
inlinenoexcept

Generates a supernet from this network with error handling.

The supernet containing this network definition, depending on the argument values. prefixlen_diff is the amount our prefix length should be decreased by. new_prefix is the desired new prefix of the supernet; it must be smaller than our prefix. One and only one of prefixlen_diff and new_prefix must be set.

auto supernet = ipv4_network::parse("192.0.2.0/24").supernet(err, 2);
if (err == error_code::no_error) {
std::cout << supernet << std::endl;
}
// out:
// 192.0.0.0/22
Parameters
[out]codeAn error_code object that will be set if an error occurs during the operation.
[in]prefixlen_diffThe amount by which the prefix length should be decreased. Defaults to 1.
[in]new_prefixlenAn optional new prefix length for the supernet. If not specified, the prefix length is determined by subtracting prefixlen_diff from the current prefix length.
Returns
An ip network object representing the supernet, or the current network if an error occurs.

◆ operator std::basic_string< T, std::char_traits< T >, std::allocator< T > >()

template<typename Base >
template<typename T >
inline operator std::basic_string< T, std::char_traits< T >, std::allocator< T > > ( ) const
inlineexplicit

Converts the ip network object to a std::string.

Template Parameters
TThe character type of the string.
Returns
A string representation of the ip network object.

◆ operator==()

template<typename Base >
constexpr inline bool operator== ( const ip_network_base< Base > & rhs) const
inlinenoexcept

Equality comparison operator.

Compares this ip network object to another for equality based on the network address and netmask.

Parameters
[in]rhsThe other ip network object to compare with.
Returns
true if both objects are equal, false otherwise.

◆ operator!=()

template<typename Base >
constexpr inline bool operator!= ( const ip_network_base< Base > & rhs) const
inlinenoexcept

Inequality comparison operator.

Compares this ip network object to another for inequality.

Parameters
[in]rhsThe other ip network object to compare with.
Returns
true if both objects are not equal, false otherwise.

◆ operator<()

template<typename Base >
constexpr inline bool operator< ( const ip_network_base< Base > & rhs) const
inlinenoexcept

Less than comparison operator.

Determines if this ip network object is less than another by comparing network addresses and netmasks.

Parameters
[in]rhsThe other ip network object to compare with.
Returns
true if this object is less than the other, false otherwise.

◆ operator>()

template<typename Base >
constexpr inline bool operator> ( const ip_network_base< Base > & rhs) const
inlinenoexcept

Greater than comparison operator.

Determines if this ip network object is greater than another.

Parameters
[in]rhsThe other ip network object to compare with.
Returns
true if this object is greater than the other, false otherwise.

◆ operator<=()

template<typename Base >
constexpr inline bool operator<= ( const ip_network_base< Base > & rhs) const
inlinenoexcept

Less than or equal to comparison operator.

Determines if this ip network object is less than or equal to another.

Parameters
[in]rhsThe other ip network object to compare with.
Returns
true if this object is less than or equal to the other, false otherwise.

◆ operator>=()

template<typename Base >
constexpr inline bool operator>= ( const ip_network_base< Base > & rhs) const
inlinenoexcept

Greater than or equal to comparison operator.

Determines if this ip network object is greater than or equal to another.

Parameters
[in]rhsThe other ip network object to compare with.
Returns
true if this object is greater than or equal to the other, false otherwise.

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