2
3
4
5
6
7
8
9
10
11
13#ifndef IPADDRESS_IP_NETWORKS_HPP
14#define IPADDRESS_IP_NETWORKS_HPP
25#if __cpp_constexpr
< 201304L
26 static const ipv4_network ipv4_private_networks[];
27 static const ipv6_network ipv6_private_networks[];
29 static const ipv4_network ipv4_is_public_network;
31 static const ipv4_network ipv4_reserved_network;
32 static const ipv6_network ipv6_reserved_networks[];
34 static const ipv4_network ipv4_is_multicast;
35 static const ipv6_network ipv6_is_multicast;
37 static const ipv4_network ipv4_is_loopback;
39 static const ipv4_network ipv4_is_link_local;
40 static const ipv6_network ipv6_is_link_local;
42 static const ipv6_network ipv6_is_site_local;
47#if __cpp_constexpr
>= 201304L
48static constexpr ipv4_network
50template <
typename T>
const ipv4_network networks<T>::
53 ipv4_private_networks[] = {
54 ipv4_network::parse(
"0.0.0.0/8"),
55 ipv4_network::parse(
"10.0.0.0/8"),
56 ipv4_network::parse(
"127.0.0.0/8"),
57 ipv4_network::parse(
"169.254.0.0/16"),
58 ipv4_network::parse(
"172.16.0.0/12"),
59 ipv4_network::parse(
"192.0.0.0/29"),
60 ipv4_network::parse(
"192.0.0.170/31"),
61 ipv4_network::parse(
"192.0.2.0/24"),
62 ipv4_network::parse(
"192.168.0.0/16"),
63 ipv4_network::parse(
"198.18.0.0/15"),
64 ipv4_network::parse(
"198.51.100.0/24"),
65 ipv4_network::parse(
"203.0.113.0/24"),
66 ipv4_network::parse(
"240.0.0.0/4"),
67 ipv4_network::parse(
"255.255.255.255/32")
70#if __cpp_constexpr
>= 201304L
71static constexpr ipv6_network
73template <
typename T>
const ipv6_network networks<T>::
76 ipv6_private_networks[] = {
77 ipv6_network::parse(
"::1/128"),
78 ipv6_network::parse(
"::/128"),
79 ipv6_network::parse(
"::ffff:0:0/96"),
80 ipv6_network::parse(
"100::/64"),
81 ipv6_network::parse(
"2001::/23"),
82 ipv6_network::parse(
"2001:2::/48"),
83 ipv6_network::parse(
"2001:db8::/32"),
84 ipv6_network::parse(
"2001:10::/28"),
85 ipv6_network::parse(
"fc00::/7"),
86 ipv6_network::parse(
"fe80::/10")
90#if __cpp_constexpr
>= 201304L
91static constexpr ipv4_network
93template <
typename T>
const ipv4_network networks<T>::
96 ipv4_is_public_network = ipv4_network::parse(
"100.64.0.0/10");
99#if __cpp_constexpr
>= 201304L
100static constexpr ipv4_network
102template <
typename T>
const ipv4_network networks<T>::
105 ipv4_reserved_network = ipv4_network::parse(
"240.0.0.0/4");
107#if __cpp_constexpr
>= 201304L
108static constexpr ipv6_network
110template <
typename T>
const ipv6_network networks<T>::
113 ipv6_reserved_networks[] = {
114 ipv6_network::parse(
"::/8"),
115 ipv6_network::parse(
"100::/8"),
116 ipv6_network::parse(
"200::/7"),
117 ipv6_network::parse(
"400::/6"),
118 ipv6_network::parse(
"800::/5"),
119 ipv6_network::parse(
"1000::/4"),
120 ipv6_network::parse(
"4000::/3"),
121 ipv6_network::parse(
"6000::/3"),
122 ipv6_network::parse(
"8000::/3"),
123 ipv6_network::parse(
"A000::/3"),
124 ipv6_network::parse(
"C000::/3"),
125 ipv6_network::parse(
"E000::/4"),
126 ipv6_network::parse(
"F000::/5"),
127 ipv6_network::parse(
"F800::/6"),
128 ipv6_network::parse(
"FE00::/9")
132#if __cpp_constexpr
>= 201304L
133static constexpr ipv4_network
135template <
typename T>
const ipv4_network networks<T>::
138 ipv4_is_multicast = ipv4_network::parse(
"224.0.0.0/4");
140#if __cpp_constexpr
>= 201304L
141static constexpr ipv6_network
143template <
typename T>
const ipv6_network networks<T>::
146 ipv6_is_multicast = ipv6_network::parse(
"ff00::/8");
149#if __cpp_constexpr
>= 201304L
150static constexpr ipv4_network
152template <
typename T>
const ipv4_network networks<T>::
155 ipv4_is_loopback = ipv4_network::parse(
"127.0.0.0/8");
158#if __cpp_constexpr
>= 201304L
159static constexpr ipv4_network
161template <
typename T>
const ipv4_network networks<T>::
164 ipv4_is_link_local = ipv4_network::parse(
"169.254.0.0/16");
166#if __cpp_constexpr
>= 201304L
167static constexpr ipv6_network
169template <
typename T>
const ipv6_network networks<T>::
172 ipv6_is_link_local = ipv6_network::parse(
"fe80::/10");
175#if __cpp_constexpr
>= 201304L
176static constexpr ipv6_network
178template <
typename T>
const ipv6_network networks<T>::
181 ipv6_is_site_local = ipv6_network::parse(
"fec0::/10");
183#if __cpp_constexpr
>= 201304L
187constexpr ipv4_network networks<T>::ipv4_private_networks[];
190constexpr ipv6_network networks<T>::ipv6_private_networks[];
193constexpr ipv4_network networks<T>::ipv4_is_public_network;
196constexpr ipv4_network networks<T>::ipv4_reserved_network;
199constexpr ipv6_network networks<T>::ipv6_reserved_networks[];
202constexpr ipv4_network networks<T>::ipv4_is_multicast;
205constexpr ipv6_network networks<T>::ipv6_is_multicast;
208constexpr ipv4_network networks<T>::ipv4_is_loopback;
211constexpr ipv4_network networks<T>::ipv4_is_link_local;
214constexpr ipv6_network networks<T>::ipv6_is_link_local;
217constexpr ipv6_network networks<T>::ipv6_is_site_local;
221using nets = networks<
int>;
223template <
typename T,
int N>
232struct props<ipv4_network> {
234 const auto& address = net.network_address();
235 const auto broadcast = net.broadcast_address();
236 constexpr auto count = array_size(nets::ipv4_private_networks);
237 for (
int i = 0; i < count; ++i) {
238 if (nets::ipv4_private_networks[i].contains(address) && nets::ipv4_private_networks[i].contains(broadcast)) {
246 const auto& network = nets::ipv4_is_public_network;
247 const auto& address = net.network_address();
248 const auto broadcast = net.broadcast_address();
249 return !(network.contains(address) && network.contains(broadcast)) && !is_private(net);
254struct props<ipv6_network> {
256 const auto& address = net.network_address();
257 const auto broadcast = net.broadcast_address();
258 constexpr auto count = array_size(nets::ipv6_private_networks);
259 for (
int i = 0; i < count; ++i) {
260 if (nets::ipv6_private_networks[i].contains(address) && nets::ipv6_private_networks[i].contains(broadcast)) {
268 return !is_private(net);
273struct props<ipv4_address> {
275 constexpr auto count = array_size(nets::ipv4_private_networks);
276 for (
int i = 0; i < count; ++i) {
277 if (nets::ipv4_private_networks[i].contains(ip)) {
285 return !nets::ipv4_is_public_network.contains(ip) && !is_private(ip);
289 return nets::ipv4_is_multicast.contains(ip);
293 return nets::ipv4_reserved_network.contains(ip);
297 return nets::ipv4_is_loopback.contains(ip);
301 return nets::ipv4_is_link_local.contains(ip);
306struct props<ipv6_address> {
308 const auto ipv4 = ip.ipv4_mapped();
311 return ipv4->is_private();
314 constexpr auto count = array_size(nets::ipv6_private_networks);
315 for (
int i = 0; i < count; ++i) {
316 if (nets::ipv6_private_networks[i].contains(ip)) {
325 return !is_private(ip);
329 return nets::ipv6_is_multicast.contains(ip);
333 constexpr auto count = array_size(nets::ipv6_reserved_networks);
334 for (
int i = 0; i < count; ++i) {
335 if (nets::ipv6_reserved_networks[i].contains(ip)) {
343 const auto& b = ip.bytes();
344 return b[0] == 0 && b[1] == 0 && b[2] == 0 && b[3] == 0
345 && b[4] == 0 && b[5] == 0 && b[6] == 0 && b[7] == 0
346 && b[8] == 0 && b[9] == 0 && b[10] == 0 && b[11] == 0
347 && b[12] == 0 && b[13] == 0 && b[14] == 0 && b[15] == 1;
351 return nets::ipv6_is_link_local.contains(ip);
357template <
typename Base>
362template <
typename Base>
367template <
typename Base>
372template <
typename Base>
377template <
typename Base>
382template <
typename Base>
387template <
typename Base>
392template <
typename Base>
398 ipv6_address address(bytes());
399 return internal::nets::ipv6_is_site_local.contains(address);
A template base class for IP address representations.
Definition ip-address-base.hpp:56
constexpr inline bool is_multicast() const noexcept
Checks if the IP address is a multicast address.
Definition ip-networks.hpp:378
constexpr inline bool is_private() const noexcept
Checks if the IP address is a private address.
Definition ip-networks.hpp:368
constexpr inline bool is_reserved() const noexcept
Checks if the IP address is a reserved address.
Definition ip-networks.hpp:383
constexpr inline bool is_link_local() const noexcept
Checks if the IP address is a link-local address.
Definition ip-networks.hpp:393
constexpr inline bool is_global() const noexcept
Checks if the IP address is a global address.
Definition ip-networks.hpp:373
constexpr inline bool is_loopback() const noexcept
Checks if the IP address is a loopback address.
Definition ip-networks.hpp:388
Template base class for representing a network of IP addresses.
Definition ip-network-base.hpp:32
constexpr inline bool is_private() const noexcept
Checks if the network is a private network.
Definition ip-networks.hpp:358
constexpr inline bool is_global() const noexcept
Checks if the network is a global network.
Definition ip-networks.hpp:363
Represents the base class for IPv6 address manipulation.
Definition ipv6-address.hpp:272
constexpr inline bool is_site_local() const noexcept
Checks if the IPv6 address is a site-local address.
Definition ip-networks.hpp:397
#define IPADDRESS_NODISCARD
Definition config.hpp:98
#define IPADDRESS_FORCE_INLINE
Definition config.hpp:112
#define IPADDRESS_NAMESPACE
Definition config.hpp:38
#define IPADDRESS_NOEXCEPT
Definition config.hpp:89