ipaddress 1.1.0
|
Template base class for representing a network of IP addresses. More...
#include <ip-network-base.hpp>
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_type & | network_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_type & | netmask () 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_type > | hosts () 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. | |
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.
Base | The base class from which ip_network_base inherits. Should be a type that represents an individual IP address. |
|
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.
|
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.
FixedString | A string literal representing the IP network in "address/prefix" format. |
Strict | A boolean flag indicating whether to perform strict validation of the address. |
|
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.
[in] | address | A string view representing the IP network in "address/prefix" format. |
[in] | strict | A boolean flag indicating whether to perform strict validation of the address. |
parse_error | Exception caused by invalid input string. |
std::string
and C-strings will be used instead.
|
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.
[in] | address | The wide string view representing the IP network in "address/prefix" format. |
[in] | strict | A boolean flag indicating whether to perform strict validation of the address. |
parse_error | Exception caused by invalid input string. |
std::wstring
and C-strings will be used instead.
|
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.
[in] | address | A UTF-8 string view representing the IP network in "address/prefix" format. |
[in] | strict | A boolean flag indicating whether to perform strict validation of the address. |
parse_error | Exception caused by invalid input string. |
char8_t
is supported.
|
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.
[in] | address | The UTF-16 string view representing the IP network in "address/prefix" format. |
[in] | strict | A boolean flag indicating whether to perform strict validation of the address. |
parse_error | Exception caused by invalid input string. |
std::u16string
and C-strings will be used instead.
|
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.
[in] | address | The UTF-32 string view representing the IP network in "address/prefix" format. |
[in] | strict | A boolean flag indicating whether to perform strict validation of the address. |
parse_error | Exception caused by invalid input string. |
std::u32string
and C-strings will be used instead.
|
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.
[in] | address | The string view representing the IP network in "address/prefix" format. |
[out] | code | An error_code object that will be set if an error occurs during parsing. |
[in] | strict | A boolean flag indicating whether to perform strict validation of the address. |
std::string
and C-strings will be used instead.
|
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.
[in] | address | The wide string view representing the IP network in "address/prefix" format. |
[out] | code | An error_code object that will be set if an error occurs during parsing. |
[in] | strict | A boolean flag indicating whether to perform strict validation of the address. |
std::wstring
and C-strings will be used instead.
|
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.
[in] | address | The UTF-8 string view representing the IP network in "address/prefix" format. |
[out] | code | An error_code object that will be set if an error occurs during parsing. |
[in] | strict | A boolean flag indicating whether to perform strict validation of the address. |
char8_t
is supported.
|
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.
[in] | address | The UTF-16 string view representing the IP network in "address/prefix" format. |
[out] | code | An error_code object that will be set if an error occurs during parsing. |
[in] | strict | A boolean flag indicating whether to perform strict validation of the address. |
std::u16string
and C-strings will be used instead.
|
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.
[in] | address | The UTF-32 string view representing the IP network in "address/prefix" format. |
[out] | code | An error_code object that will be set if an error occurs during parsing. |
[in] | strict | A boolean flag indicating whether to perform strict validation of the address. |
std::u32string
and C-strings will be used instead.
|
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.
T | The character type of the array. |
N | The size of the character array. |
[in] | address | The character array representing the IP network in "address/prefix" format. |
[in] | strict | A boolean flag indicating whether to perform strict validation of the address. |
|
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.
T | The character type of the array. |
N | The size of the character array. |
[in] | address | The character array representing the IP network in "address/prefix" format. |
[out] | code | An error_code object that will be set if an error occurs during parsing. |
[in] | strict | A boolean flag indicating whether to perform strict validation of the address. |
|
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.
[in] | address | The IP address to use for creating the network. |
[in] | prefixlen | The prefix length for the network's netmask. Defaults to the maximum prefix length. |
[in] | strict | Whether to validate the address against the netmask. |
parse_error | Exception caused by invalid input string. |
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.
[in] | address | The IP address to use for creating the network. |
[out] | code | An error_code object that will be set if an error occurs. |
[in] | prefixlen | The prefix length for the network's netmask. Defaults to the maximum prefix length. |
[in] | strict | Whether to validate the address against the netmask. |
|
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.
[in] | address | The IP address to check. |
true
if the address is part of the network, false
otherwise.
|
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.
[in] | other | The other ip network object to compare with. |
true
if there is an overlap, false
otherwise.
|
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.
[in] | other | The other ip network object to compare with. |
true
if this network is a subnet of the other network, false
otherwise.
|
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.
[in] | other | The other ip network object to compare with. |
|
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.
[in] | fmt | The format to use for the string representation. Defaults to format::compressed. |
|
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.
[in] | fmt | The format to use for the string representation. Defaults to format::compressed. |
|
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.
[in] | fmt | The format to use for the string representation. Defaults to format::compressed. |
|
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.
[in] | fmt | The format to use for the string representation. Defaults to format::compressed. |
|
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.
[in] | fmt | The format to use for the string representation. Defaults to format::compressed. |
|
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.
[in,out] | network | The other ip network object to swap contents with. |
|
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.
|
inlinenoexcept |
Retrieves the network address of this network.
This method returns the network address component of the ip network object.
|
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.
|
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.
|
inlinenoexcept |
Retrieves the hostmask of this network.
The hostmask identifies the portion of the IP address that is allocated for host identifiers.
|
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.
|
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.
true
if the network is multicast, false
otherwise.
|
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.
true
if the network is private, false
otherwise.
|
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.
true
if the network is global, false
otherwise.
|
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.
true
if the network is reserved, false
otherwise.
|
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.
true
if the network is loopback, false
otherwise.
|
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.
true
if the network is link-local, false
otherwise.
|
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.
true
if the network is unspecified, false
otherwise.
|
inlinenoexcept |
Calculates the total number of addresses in the network.
uint_type
value.
|
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.
hosts_sequence
object representing the sequence of host addresses. Ipv4 | The 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. |
Ipv6 | The 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. |
hosts_sequence
uses lazy evaluation to iterate addresses.
|
inline |
Computes the network definitions resulting from removing the given network from this one.
[in] | other | The other network to exclude from this network. |
exclude_network_sequence
object representing the remaining address ranges. logic_error | Raise if network is not completely contained in this network. |
exclude_network_sequence
uses lazy evaluation to iterate networks.
|
inlinenoexcept |
Computes the network definitions resulting from removing the given network from this one, with error handling.
[in] | other | The other network to exclude from this network. |
[out] | code | An error_code object that will be set if an error occurs during the operation. |
exclude_network_sequence
object representing the remaining address ranges, or an empty sequence if an error occurs. exclude_network_sequence
uses lazy evaluation to iterate networks.
|
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.
[in] | prefixlen_diff | The difference in prefix length for the subnets. Defaults to 1. |
[in] | new_prefixlen | An optional new prefix length for the subnets. If not specified, the prefix length is determined by adding prefixlen_diff to the current prefix length. |
subnets_sequence
object representing the sequence of subnets. logic_error | Raised if the operation cannot be performed due to invalid parameters or prefix length. |
subnets_sequence
uses lazy evaluation to iterate over the subnets.
|
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.
[out] | code | An error_code object that will be set if an error occurs during the operation. |
[in] | prefixlen_diff | The difference in prefix length for the subnets. Defaults to 1. |
[in] | new_prefixlen | An optional new prefix length for the subnets. If not specified, the prefix length is determined by adding prefixlen_diff to the current prefix length. |
subnets_sequence
object representing the sequence of subnets, or an empty sequence if an error occurs. subnets_sequence
uses lazy evaluation to iterate over the subnets.
|
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.
[in] | prefixlen_diff | The amount by which the prefix length should be decreased. Defaults to 1. |
[in] | new_prefixlen | An optional new prefix length for the supernet. If not specified, the prefix length is determined by subtracting prefixlen_diff from the current prefix length. |
logic_error | Raised if the operation cannot be performed due to invalid parameters or prefix length. |
|
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.
[out] | code | An error_code object that will be set if an error occurs during the operation. |
[in] | prefixlen_diff | The amount by which the prefix length should be decreased. Defaults to 1. |
[in] | new_prefixlen | An optional new prefix length for the supernet. If not specified, the prefix length is determined by subtracting prefixlen_diff from the current prefix length. |
|
inlineexplicit |
Converts the ip network object to a std::string.
T | The character type of the string. |
|
inlinenoexcept |
Equality comparison operator.
Compares this ip network object to another for equality based on the network address and netmask.
[in] | rhs | The other ip network object to compare with. |
true
if both objects are equal, false
otherwise.
|
inlinenoexcept |
Inequality comparison operator.
Compares this ip network object to another for inequality.
[in] | rhs | The other ip network object to compare with. |
true
if both objects are not equal, false
otherwise.
|
inlinenoexcept |
Less than comparison operator.
Determines if this ip network object is less than another by comparing network addresses and netmasks.
[in] | rhs | The other ip network object to compare with. |
true
if this object is less than the other, false
otherwise.
|
inlinenoexcept |
Greater than comparison operator.
Determines if this ip network object is greater than another.
[in] | rhs | The other ip network object to compare with. |
true
if this object is greater than the other, false
otherwise.
|
inlinenoexcept |
Less than or equal to comparison operator.
Determines if this ip network object is less than or equal to another.
[in] | rhs | The other ip network object to compare with. |
true
if this object is less than or equal to the other, false
otherwise.
|
inlinenoexcept |
Greater than or equal to comparison operator.
Determines if this ip network object is greater than or equal to another.
[in] | rhs | The other ip network object to compare with. |
true
if this object is greater than or equal to the other, false
otherwise.