Here describes various ways to operate with networks and addresses, for example how to get all hosts on a given network, or exclude one network from another, etc.
- Note
- All iteration operations in this library are implemented with lazy evaluation. That is, the list is not generated in advance; instead, each element is generated upon request.
Enumerating network addresses
Below is a demonstration of how to get all hosts on a given network
#include <iostream>
int main() {
for (
const auto& addr :
ip_network::parse(
"192.0.2.0/29").hosts()) {
std::cout << addr << std::endl;
}
constexpr auto hosts_sequence = ip_network::parse("192.0.2.0/29").hosts();
constexpr auto last_host = hosts_sequence.back();
std::cout << last_host << std::endl;
return 0;
}
A class that encapsulates both IPv4 and IPv6 network functionalities.
Definition ip-any-network.hpp:70
The main include file for the ipaddress library.
Namespace dedicated to IP addresses and networks operations.
Definition base-v4.hpp:18
- 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.
- Note
- For 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.
For 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.
Supernet and Subnets
Get the subnets that are combined to create the current network definition.
#include <iostream>
int main() {
std::cout << ip_network::parse("192.0.2.0/24").supernet(2) << std::endl;
for (
const auto& subnet :
ip_network::parse(
"192.0.2.0/24").subnets(2)) {
std::cout << subnet << std::endl;
}
return 0;
}
Removing one network from another network
Calculates the network definitions that arise from subtracting the specified network from the current one.
#include <iostream>
int main() {
constexpr auto a = ip_network::parse("192.0.2.0/28");
constexpr auto b = ip_network::parse("192.0.2.1/32");
for (const auto& net : a.address_exclude(b)) {
std::cout << net << std::endl;
}
return 0;
}
Other operations
This library does not overload arithmetic operators for IP addresses and networks. But what if they need address arithmetic to resolve your problems? You can use integer arithmetic for this.
#include <iostream>
int main() {
constexpr auto ip1 = ipv4_address::parse("192.0.2.0");
constexpr auto ip2 = ipv6_address::parse("2001:db8::");
constexpr auto ip3 = ip_address::parse("2001:db8::");
constexpr auto new_ip1 = ipv4_address::from_uint((std::uint32_t) ip1 + 1000);
constexpr auto new_ip2 = ipv6_address::from_uint((
uint128_t) ip2 + 1);
constexpr auto new_ip3 = ip_address::from_uint((
uint128_t) ip3 + 1);
std::cout << new_ip1 << std::endl;
std::cout << new_ip2 << std::endl;
std::cout << new_ip3 << std::endl;
return 0;
}
Class for representing a 128-bit unsigned integer.
Definition uint128.hpp:52