ipaddress 1.1.0
Loading...
Searching...
No Matches
ip_network Class Reference

A class that encapsulates both IPv4 and IPv6 network functionalities. More...

#include <ip-any-network.hpp>

Public Types

using ip_address_type = ip_address
 Type alias for ip_address, representing the IP address within the network.
 
using uint_type = uint128_t
 Type alias for a 128-bit unsigned integer type.
 

Public Member Functions

constexpr inline ip_version version () const noexcept
 Retrieves the IP version of the network.
 
constexpr inline size_t prefixlen () const noexcept
 Retrieves the prefix length of this network.
 
constexpr inline ip_address network_address () const noexcept
 Retrieves the network address of this network.
 
constexpr inline ip_address broadcast_address () const noexcept
 Retrieves the broadcast address of this network.
 
constexpr inline ip_address netmask () const noexcept
 Retrieves the netmask of this network.
 
constexpr inline ip_address hostmask () const noexcept
 Retrieves the hostmask 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 bool is_site_local () const noexcept
 Checks if the IPv6 network is site-local.
 
constexpr inline bool is_v4 () const noexcept
 Checks if the IP network is an IPv4 network.
 
constexpr inline bool is_v6 () const noexcept
 Checks if the IP network is an IPv6 network.
 
constexpr inline size_t size () const noexcept
 Retrieves the size of the IP address.
 
constexpr inline size_t hash () const noexcept
 Computes a hash value for the IP address.
 
constexpr inline uint128_t addresses_count () const noexcept
 Calculates the total number of addresses in the network.
 
constexpr inline bool contains (const ip_address &address) const noexcept
 Checks if the given IP address is contained within this network.
 
constexpr inline bool overlaps (const ip_network &other) const noexcept
 Determines if this network overlaps with another network.
 
constexpr inline bool subnet_of (const ip_network &other) const noexcept
 Checks if this network is a subnet of another network.
 
constexpr inline bool supernet_of (const ip_network &other) const noexcept
 Checks if this network is a supernet of another network.
 
constexpr inline ip_network supernet (size_t prefixlen_diff=1, const optional< size_t > &new_prefixlen=nullptr) const
 Generates a supernet from this network.
 
constexpr inline ip_network supernet (error_code &code, size_t prefixlen_diff=1, const optional< size_t > &new_prefixlen=nullptr) const noexcept
 Generates a supernet from this network with error handling.
 
constexpr inline hosts_any_sequence hosts () const noexcept
 Retrieves a sequence of host addresses in the network.
 
constexpr inline subnets_any_sequence< ip_networksubnets (size_t prefixlen_diff=1, const optional< size_t > &new_prefixlen=nullptr) const
 Generates a sequence of subnets from this network.
 
constexpr inline subnets_any_sequence< ip_networksubnets (error_code &code, size_t prefixlen_diff=1, const optional< size_t > &new_prefixlen=nullptr) const noexcept
 Generates a sequence of subnets from this network with error handling.
 
constexpr inline exclude_network_sequence< ip_networkaddress_exclude (const ip_network &other) const
 Computes the network definitions resulting from removing the given network from this one.
 
constexpr inline exclude_network_sequence< ip_networkaddress_exclude (const ip_network &other, error_code &code) const noexcept
 Computes the network definitions resulting from removing the given network from this one, with error handling.
 
constexpr inline optional< ipv4_networkv4 () const noexcept
 Retrieves the IPv4 network.
 
constexpr inline optional< ipv6_networkv6 () const noexcept
 Retrieves the IPv6 network.
 
constexpr inline ip_network () noexcept
 Constructs a new IP network object.
 
constexpr inline ip_network (const ipv4_network &net4) noexcept
 Constructor from an ipv4_network.
 
constexpr inline ip_network (const ipv6_network &net6) noexcept
 Constructor from an ipv6_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 &net) noexcept
 Swaps the contents of this network with another network.
 
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 &rhs) const noexcept
 Equality comparison operator.
 
constexpr inline bool operator!= (const ip_network &rhs) const noexcept
 Inequality comparison operator.
 
constexpr inline bool operator< (const ip_network &rhs) const noexcept
 Less than comparison operator.
 
constexpr inline bool operator> (const ip_network &rhs) const noexcept
 Greater than comparison operator.
 
constexpr inline bool operator<= (const ip_network &rhs) const noexcept
 Less than or equal to comparison operator.
 
constexpr inline bool operator>= (const ip_network &rhs) const noexcept
 Greater than or equal to comparison operator.
 

Static Public Member Functions

static constexpr inline ip_network from_address (const ip_address &address, size_t prefixlen, bool strict=true)
 Creates an ip network object from a given IP address and prefix length.
 
static constexpr inline ip_network from_address (const ip_address &address, error_code &code, size_t prefixlen, bool strict=true) noexcept
 Creates an ip network object from a given IP address and prefix length, with error handling.
 
template<fixed_string FixedString, bool Strict = true>
static consteval inline ip_network parse () noexcept
 Parses a network address and prefix from a fixed string at compile time.
 
static constexpr inline ip_network parse (std::string_view address, bool strict=true)
 Parses a network address and prefix from a string view.
 
static constexpr inline ip_network parse (std::wstring_view address, bool strict=true)
 Parses a network address and prefix from a wide string view.
 
static constexpr inline ip_network parse (std::u8string_view address, bool strict=true)
 Parses a network address and prefix from UTF-8 string view.
 
static constexpr inline ip_network parse (std::u16string_view address, bool strict=true)
 Parses a network address and prefix from UTF-16 string view.
 
static constexpr inline ip_network parse (std::u32string_view address, bool strict=true)
 Parses a network address and prefix from UTF-32 string view.
 
static constexpr inline ip_network 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 inline ip_network 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 inline ip_network 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 inline ip_network 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 inline ip_network 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 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 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.
 

Detailed Description

A class that encapsulates both IPv4 and IPv6 network functionalities.

The ip_network class serves as a versatile tool for network address manipulation, capable of handling both IPv4 and IPv6 networks. It abstracts the complexities associated with the different IP versions and provides a consistent API for network operations. This class is essential for applications that require flexibility in dealing with various IP network types without concern for the underlying IP version.

Constructor & Destructor Documentation

◆ ip_network() [1/3]

constexpr inline ip_network ( )
inlinenoexcept

Constructs a new IP network object.

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

◆ ip_network() [2/3]

constexpr inline ip_network ( const ipv4_network & net4)
inlinenoexcept

Constructor from an ipv4_network.

Constructs an ip_network object with the specified IPv4 network.

Parameters
[in]net4An ipv4_network object to initialize the ip_network.

◆ ip_network() [3/3]

constexpr inline ip_network ( const ipv6_network & net6)
inlinenoexcept

Constructor from an ipv6_network.

Constructs an ip_network object with the specified IPv6 network.

Parameters
[in]net6An ipv6_network object to initialize the ip_network.

Member Function Documentation

◆ version()

constexpr inline ip_version version ( ) const
inlinenoexcept

Retrieves the IP version of the network.

Returns
The IP version as an enumeration (either V4 or V6).

◆ prefixlen()

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.

◆ network_address()

constexpr inline ip_address 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()

constexpr inline ip_address 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()

constexpr inline ip_address 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()

constexpr inline ip_address 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.

◆ is_multicast()

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()

constexpr inline bool is_private ( ) const
inlinenoexcept

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()

constexpr inline bool is_global ( ) const
inlinenoexcept

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()

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()

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()

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()

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.

◆ is_site_local()

constexpr inline bool is_site_local ( ) const
inlinenoexcept

Checks if the IPv6 network is site-local.

This method determines if both the network address and the broadcast address of the IPv6 network are site-local, which are addresses used within a particular organization's intranet and are not routable on the global internet.

Returns
A boolean value indicating whether the network is site-local.

◆ is_v4()

constexpr inline bool is_v4 ( ) const
inlinenoexcept

Checks if the IP network is an IPv4 network.

Returns
true if the IP network is an IPv4 network, false otherwise.

◆ is_v6()

constexpr inline bool is_v6 ( ) const
inlinenoexcept

Checks if the IP network is an IPv6 network.

Returns
true if the IP network is an IPv6 network, false otherwise.

◆ size()

constexpr inline size_t size ( ) const
inlinenoexcept

Retrieves the size of the IP address.

Depending on the IP version, this function returns the size of the IPv4 or IPv6 address.

Returns
The size of the IP address in bytes.
Remarks
This is the number of bytes of the IP address that represents the current network.

◆ hash()

constexpr inline size_t hash ( ) const
inlinenoexcept

Computes a hash value for the IP address.

This function generates a hash value that can be used to uniquely identify the IP address. It can be useful when IP addresses are used as keys in hash tables.

Returns
A size_t hash value of the IP address.

◆ addresses_count()

constexpr inline uint128_t addresses_count ( ) const
inlinenoexcept

Calculates the total number of addresses in the network.

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

◆ contains()

constexpr inline bool contains ( const ip_address & 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 = ip_network::parse("192.0.2.0/28").contains(ip_address::parse("192.0.2.6"));
constexpr auto result2 = ip_network::parse("192.0.2.0/28").contains(ip_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 parse() noexcept
Static method template to parse an IP address from a fixed string at compile time.
Definition ip-any-address.hpp:550
static consteval inline ip_network parse() noexcept
Parses a network address and prefix from a fixed string at compile time.
Definition ip-any-network.hpp:870
constexpr inline bool contains(const ip_address &address) const noexcept
Checks if the given IP address is contained within this network.
Definition ip-any-network.hpp:321
Parameters
[in]addressThe IP address to check.
Returns
true if the address is part of the network, false otherwise.

◆ overlaps()

constexpr inline bool overlaps ( const ip_network & 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 = ip_network::parse("1.2.3.0/24");
constexpr auto b = ip_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 &other) const noexcept
Determines if this network overlaps with another network.
Definition ip-any-network.hpp:348
Parameters
[in]otherThe other ip network object to compare with.
Returns
true if there is an overlap, false otherwise.

◆ subnet_of()

constexpr inline bool subnet_of ( const ip_network & 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 = ip_network::parse("192.168.1.0/24");
constexpr auto b = ip_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 &other) const noexcept
Checks if this network is a subnet of another network.
Definition ip-any-network.hpp:375
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()

constexpr inline bool supernet_of ( const ip_network & 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 = ip_network::parse("192.168.1.0/24");
constexpr auto b = ip_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 &other) const noexcept
Checks if this network is a supernet of another network.
Definition ip-any-network.hpp:402
Parameters
[in]otherThe other ip network object to compare with.
Returns
True if this network is a supernet of the other network, false otherwise.

◆ supernet() [1/2]

constexpr inline ip_network supernet ( size_t prefixlen_diff = 1,
const 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 = ip_network::parse("192.0.2.0/24").supernet(2);
std::cout << supernet << std::endl;
// out:
// 192.0.0.0/22
constexpr inline ip_network supernet(size_t prefixlen_diff=1, const optional< size_t > &new_prefixlen=nullptr) const
Generates a supernet from this network.
Definition ip-any-network.hpp:431
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]

constexpr inline ip_network supernet ( error_code & code,
size_t prefixlen_diff = 1,
const 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 = ip_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
@ no_error
Indicates the absence of any errors.
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.

◆ hosts()

constexpr inline hosts_any_sequence 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 = ip_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_any_sequence hosts() const noexcept
Retrieves a sequence of host addresses in the network.
Definition ip-any-network.hpp:498
Returns
A hosts_sequence object representing the sequence of host addresses.
Return values
Ipv4For ipv4 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.
Ipv6For ipv6 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.

◆ subnets() [1/2]

constexpr inline subnets_any_sequence< ip_network > subnets ( size_t prefixlen_diff = 1,
const 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 = ip_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_any_sequence< ip_network > subnets(size_t prefixlen_diff=1, const optional< size_t > &new_prefixlen=nullptr) const
Generates a sequence of subnets from this network.
Definition ip-any-network.hpp:535
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_any_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_any_sequence uses lazy evaluation to iterate over the subnets.

◆ subnets() [2/2]

constexpr inline subnets_any_sequence< ip_network > subnets ( error_code & code,
size_t prefixlen_diff = 1,
const 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 = ip_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_any_sequence object representing the sequence of subnets, or an empty sequence if an error occurs.
Remarks
subnets_any_sequence uses lazy evaluation to iterate over the subnets.

◆ address_exclude() [1/2]

constexpr inline exclude_network_sequence< ip_network > address_exclude ( const ip_network & other) const
inline

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

constexpr auto a = ip_network::parse("192.0.2.0/28");
constexpr auto b = ip_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]

constexpr inline exclude_network_sequence< ip_network > address_exclude ( const ip_network & 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 = ip_network::parse("192.0.2.0/28");
constexpr auto b = ip_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
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.

◆ v4()

constexpr inline optional< ipv4_network > v4 ( ) const
inlinenoexcept

Retrieves the IPv4 network.

If the IP network is an IPv4 network, this function returns the IPv4 network.

Returns
An optional containing the IPv4 network, or an empty optional if the IP network is not IPv4.

◆ v6()

constexpr inline optional< ipv6_network > v6 ( ) const
inlinenoexcept

Retrieves the IPv6 network.

If the IP network is an IPv6 network, this function returns the IPv6 network.

Returns
An optional containing the IPv6 network, or an empty optional if the IP network is not IPv6.

◆ from_address() [1/2]

static constexpr inline ip_network from_address ( const ip_address & address,
size_t 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]

static constexpr inline ip_network from_address ( const ip_address & address,
error_code & code,
size_t 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.

◆ to_string()

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()

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()

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()

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()

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()

constexpr inline void swap ( ip_network & net)
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]netThe other ip_network object to swap contents with.

◆ parse() [1/13]

template<fixed_string FixedString, bool Strict = true>
static consteval inline ip_network 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]

static constexpr inline ip_network 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]

static constexpr inline ip_network 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]

static constexpr inline ip_network 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]

static constexpr inline ip_network 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]

static constexpr inline ip_network 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]

static constexpr inline ip_network 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]

static constexpr inline ip_network 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]

static constexpr inline ip_network 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]

static constexpr inline ip_network 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]

static constexpr inline ip_network 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 T , size_t N>
static constexpr inline ip_network 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 T , size_t N>
static constexpr inline ip_network 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.

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

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 std::string representation of the ip network object.

◆ operator==()

constexpr inline bool operator== ( const ip_network & 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!=()

constexpr inline bool operator!= ( const ip_network & 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<()

constexpr inline bool operator< ( const ip_network & 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>()

constexpr inline bool operator> ( const ip_network & 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<=()

constexpr inline bool operator<= ( const ip_network & 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>=()

constexpr inline bool operator>= ( const ip_network & 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 file: