2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
20#ifndef IPADDRESS_IP_ANY_NETWORK_HPP
21#define IPADDRESS_IP_ANY_NETWORK_HPP
30struct net_any_parser {
31 template <
typename Str>
34 const auto net4 = ipv4_network::parse(address, code, strict);
38 return T(ipv6_network::parse(address, strict));
41 template <
typename Str>
44 const auto net4 = ipv4_network::parse(address, code, strict);
48 const auto net6 = ipv6_network::parse(address, code, strict);
59
60
61
62
63
64
65
66
73
74
75
76
82
83
84
85
86
87
88
90 return is_v4() ? _ipv_net.ipv4.prefixlen() : _ipv_net.ipv6.prefixlen();
94
95
96
97
98
99
101 return is_v4() ? ip_address(_ipv_net.ipv4.network_address()) : ip_address(_ipv_net.ipv6.network_address());
105
106
107
108
109
110
111
113 return is_v4() ? ip_address(_ipv_net.ipv4.broadcast_address()) : ip_address(_ipv_net.ipv6.broadcast_address());
117
118
119
120
121
122
123
125 return is_v4() ? ip_address(_ipv_net.ipv4.netmask()) : ip_address(_ipv_net.ipv6.netmask());
129
130
131
132
133
134
136 return is_v4() ? ip_address(_ipv_net.ipv4.hostmask()) : ip_address(_ipv_net.ipv6.hostmask());
140
141
142
143
144
145
146
147
148
150 return is_v4() ? _ipv_net.ipv4.is_multicast() : _ipv_net.ipv6.is_multicast();
154
155
156
157
158
159
160
161
162
164 return is_v4() ? _ipv_net.ipv4.is_private() : _ipv_net.ipv6.is_private();
168
169
170
171
172
173
174
175
176
178 return is_v4() ? _ipv_net.ipv4.is_global() : _ipv_net.ipv6.is_global();
182
183
184
185
186
187
188
190 return is_v4() ? _ipv_net.ipv4.is_reserved() : _ipv_net.ipv6.is_reserved();
194
195
196
197
198
199
200
201
202
204 return is_v4() ? _ipv_net.ipv4.is_loopback() : _ipv_net.ipv6.is_loopback();
208
209
210
211
212
213
214
215
217 return is_v4() ? _ipv_net.ipv4.is_link_local() : _ipv_net.ipv6.is_link_local();
221
222
223
224
225
226
227
228
229
231 return is_v4() ? _ipv_net.ipv4.is_unspecified() : _ipv_net.ipv6.is_unspecified();
235
236
237
238
239
240
241
242
244 return is_v4() ?
false : _ipv_net.ipv6.is_site_local();
248
249
250
251
257
258
259
260
266
267
268
269
270
271
272
274 return is_v4() ? _ipv_net.ipv4.size() : _ipv_net.ipv6.size();
278
279
280
281
282
283
284
286 return is_v4() ? _ipv_net.ipv4.hash() : _ipv_net.ipv6.hash();
290
291
292
293
295 return is_v4() ? uint128_t(_ipv_net.ipv4.addresses_count()) : _ipv_net.ipv6.addresses_count();
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
320 return is_v4() ? _ipv_net.ipv4.contains(address.v4().value()) : _ipv_net.ipv6.contains(address.v6().value());
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
347 return is_v4() ? _ipv_net.ipv4.overlaps(other.v4().value()) : _ipv_net.ipv6.overlaps(other.v6().value());
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
374 return is_v4() ? _ipv_net.ipv4.subnet_of(other.v4().value()) : _ipv_net.ipv6.subnet_of(other.v6().value());
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
401 return is_v4() ? _ipv_net.ipv4.supernet_of(other.v4().value()) : _ipv_net.ipv6.supernet_of(other.v6().value());
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
429 return is_v4() ? ip_network(_ipv_net.ipv4.supernet(prefixlen_diff, new_prefixlen)) : ip_network(_ipv_net.ipv6.supernet(prefixlen_diff, new_prefixlen));
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
457 return is_v4() ? ip_network(_ipv_net.ipv4.supernet(code, prefixlen_diff, new_prefixlen)) : ip_network(_ipv_net.ipv6.supernet(code, prefixlen_diff, new_prefixlen));
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
497 const auto sequence = _ipv_net.ipv4.hosts();
498 return hosts_any_sequence(sequence.begin(), sequence.end());
500 const auto sequence = _ipv_net.ipv6.hosts();
501 return hosts_any_sequence(sequence.begin(), sequence.end());
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
534 const auto sequence = _ipv_net.ipv4.subnets(prefixlen_diff, new_prefixlen);
535 return subnets_any_sequence<ip_network>(sequence.begin(), sequence.end());
537 const auto sequence = _ipv_net.ipv6.subnets(prefixlen_diff, new_prefixlen);
538 return subnets_any_sequence<ip_network>(sequence.begin(), sequence.end());
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
574 const auto sequence = _ipv_net.ipv4.subnets(code, prefixlen_diff, new_prefixlen);
575 return subnets_any_sequence<ip_network>(sequence.begin(), sequence.end());
577 const auto sequence = _ipv_net.ipv6.subnets(code, prefixlen_diff, new_prefixlen);
578 return subnets_any_sequence<ip_network>(sequence.begin(), sequence.end());
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
609 raise_error(code, 0,
"", 0);
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
643 if (_version != other._version) {
645 return exclude_network_sequence<ip_network>();
648 auto addr1 = network_address(); addr1.set_scope_id(
"");
649 auto addr2 = other.network_address(); addr2.set_scope_id(
"");
650 auto lhs = ip_network::from_address(addr1, prefixlen());
651 auto rhs = ip_network::from_address(addr2, other.prefixlen());
653 if (!rhs.subnet_of(lhs)) {
655 return exclude_network_sequence<ip_network>();
659 return exclude_network_sequence<ip_network>();
662 return exclude_network_sequence<ip_network>(lhs, rhs);
666
667
668
669
670
671
674 return optional<ipv4_network>(_ipv_net.ipv4);
676 return optional<ipv4_network>();
680
681
682
683
684
685
688 return optional<ipv6_network>(_ipv_net.ipv6);
690 return optional<ipv6_network>();
694
695
696
697
698
699
704
705
706
707
708
709
714
715
716
717
718
719
724
725
726
727
728
729
730
731
732
733
734
735
736
742 return address.is_v4()
743 ? ip_network(ipv4_network::from_address(address.v4().value(), prefixlen, strict))
744 : ip_network(ipv6_network::from_address(address.v6().value(), prefixlen, strict));
748
749
750
751
752
753
754
755
756
757
758
765 return address.is_v4()
766 ? ip_network(ipv4_network::from_address(address.v4().value(), code, prefixlen, strict))
767 : ip_network(ipv6_network::from_address(address.v6().value(), code, prefixlen, strict));
771
772
773
774
775
776
777
778
780 return is_v4() ? _ipv_net.ipv4.to_string(fmt) : _ipv_net.ipv6.to_string(fmt);
784
785
786
787
788
789
790
791
793 return is_v4() ? _ipv_net.ipv4.to_wstring(fmt) : _ipv_net.ipv6.to_wstring(fmt);
797
798
799
800
801
802
803
804
806 return is_v4() ? _ipv_net.ipv4.to_u16string(fmt) : _ipv_net.ipv6.to_u16string(fmt);
810
811
812
813
814
815
816
817
819 return is_v4() ? _ipv_net.ipv4.to_u32string(fmt) : _ipv_net.ipv6.to_u32string(fmt);
822#if __cpp_char8_t >= 201811L
825
826
827
828
829
830
831
832
840
841
842
843
844
845
846
848 const auto tmp = *
this;
853#ifdef IPADDRESS_NONTYPE_TEMPLATE_PARAMETER
856
857
858
859
860
861
862
863
864
865
886#if IPADDRESS_CPP_VERSION
>= 17
889
890
891
892
893
894
895
896
897
898
899
900
906
907
908
909
910
911
912
913
914
915
916
917
922#if __cpp_char8_t >= 201811L
925
926
927
928
929
930
931
932
933
934
935
943
944
945
946
947
948
949
950
951
952
953
954
960
961
962
963
964
965
966
967
968
969
970
971
977
978
979
980
981
982
983
984
985
986
987
988
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1010#if __cpp_char8_t >= 201811L
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1067
1068
1069
1070
1071
1072
1073
1075 return internal::net_any_parser<ip_network>::parse(address, strict);
1079
1080
1081
1082
1083
1084
1085
1087 return internal::net_any_parser<ip_network>::parse(address, strict);
1091
1092
1093
1094
1095
1096
1097
1099 return internal::net_any_parser<ip_network>::parse(address, strict);
1103
1104
1105
1106
1107
1108
1109
1111 return internal::net_any_parser<ip_network>::parse(address, strict);
1115
1116
1117
1118
1119
1120
1121
1123 return internal::net_any_parser<ip_network>::parse(address, code, strict);
1127
1128
1129
1130
1131
1132
1133
1135 return internal::net_any_parser<ip_network>::parse(address, code, strict);
1139
1140
1141
1142
1143
1144
1145
1147 return internal::net_any_parser<ip_network>::parse(address, code, strict);
1151
1152
1153
1154
1155
1156
1157
1159 return internal::net_any_parser<ip_network>::parse(address, code, strict);
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176 template <
typename T, size_t N>
1178 return internal::net_any_parser<ip_network>::parse(address, strict);
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194 template <
typename T, size_t N>
1196 return internal::net_any_parser<ip_network>::parse(address, code, strict);
1200
1201
1202
1203
1204
1205 template <
typename T>
1207 return internal::string_converter<T>::convert(
to_string());
1211
1212
1213
1214
1215
1216
1217
1219 if (_version != rhs._version) {
1222 return _version == ip_version::V4 ? (_ipv_net.ipv4 == rhs._ipv_net.ipv4) : (_ipv_net.ipv6 == rhs._ipv_net.ipv6);
1226
1227
1228
1229
1230
1231
1232
1234 return !(*
this == rhs);
1237#ifdef IPADDRESS_HAS_SPACESHIP_OPERATOR
1240
1241
1242
1243
1244
1245
1246
1258
1259
1260
1261
1262
1263
1264
1266 if (_version < rhs._version) {
1269 if (_version > rhs._version) {
1272 return _version == ip_version::V4 ? (_ipv_net.ipv4 < rhs._ipv_net.ipv4) : (_ipv_net.ipv6 < rhs._ipv_net.ipv6);
1276
1277
1278
1279
1280
1281
1282
1288
1289
1290
1291
1292
1293
1294
1296 return !(rhs
< *
this);
1300
1301
1302
1303
1304
1305
1306
1308 return !(*
this < rhs);
1314 union ip_any_network {
1330#ifdef IPADDRESS_NONTYPE_TEMPLATE_PARAMETER
1333
1334
1335
1336
1337
1338
1339
1340
1341 IPADDRESS_EXPORT
template <fixed_string FixedString>
1342 IPADDRESS_NODISCARD IPADDRESS_CONSTEVAL IPADDRESS_FORCE_INLINE ip_network operator
""_net() IPADDRESS_NOEXCEPT {
1343 return ip_network::parse<FixedString>();
1349
1350
1351
1352
1353
1354
1355
1356
1357
1359 const auto max_len = ipv6_address::base_max_string_len * 2 + 1;
1360 if (size > max_len) {
1361 raise_error(error_code::string_is_too_long, 0, address, size);
1363 char str[max_len + 1] = {};
1364 for (size_t i = 0; i < size && i < max_len; ++i) {
1365 str[i] = address[i];
1367 return ip_network::parse(str);
1371
1372
1373
1374
1375
1376
1377
1378
1379
1381 const auto max_len = ipv6_address::base_max_string_len * 2 + 1;
1382 if (size > max_len) {
1383 raise_error(error_code::string_is_too_long, 0, address, size);
1385 wchar_t str[max_len + 1] = {};
1386 for (size_t i = 0; i < size && i < max_len; ++i) {
1387 str[i] = address[i];
1389 return ip_network::parse(str);
1393
1394
1395
1396
1397
1398
1399
1400
1401
1403 const auto max_len = ipv6_address::base_max_string_len * 2 + 1;
1404 if (size > max_len) {
1405 raise_error(error_code::string_is_too_long, 0, address, size);
1407 char16_t str[max_len + 1] = {};
1408 for (size_t i = 0; i < size && i < max_len; ++i) {
1409 str[i] = address[i];
1411 return ip_network::parse(str);
1415
1416
1417
1418
1419
1420
1421
1422
1423
1425 const auto max_len = ipv6_address::base_max_string_len * 2 + 1;
1426 if (size > max_len) {
1427 raise_error(error_code::string_is_too_long, 0, address, size);
1429 char32_t str[max_len + 1] = {};
1430 for (size_t i = 0; i < size && i < max_len; ++i) {
1431 str[i] = address[i];
1433 return ip_network::parse(str);
1440#ifndef IPADDRESS_NO_OVERLOAD_STD
1447 return network.hash();
1471 if (stream.flags() & ios_base::uppercase) {
1472 auto end = std::find(str.cbegin(), str.cend(),
'%');
1473 std::transform(str.cbegin(), end, str.begin(), [](
char c){
1474 return std::toupper(c);
1483 auto strict = iword == 0;
1486 std::basic_string<T, std::char_traits<T>, std::allocator<T>> str;
1491 stream.setstate(std::ios_base::failbit);
A sequence container for networks excluding specified subnets.
Definition ip-network-iterator.hpp:1065
A sequence of host IP addresses.
Definition ip-any-iterator.hpp:603
A class that represents an IP address, supporting both IPv4 and IPv6 formats.
Definition ip-any-address.hpp:73
constexpr inline ip_version version() const noexcept
Retrieves the version of the IP address.
Definition ip-any-address.hpp:86
A class that encapsulates both IPv4 and IPv6 network functionalities.
Definition ip-any-network.hpp:67
constexpr inline void swap(ip_network &net) noexcept
Swaps the contents of this network with another network.
Definition ip-any-network.hpp:847
constexpr inline bool operator>=(const ip_network &rhs) const noexcept
Greater than or equal to comparison operator.
Definition ip-any-network.hpp:1307
constexpr inline bool is_v4() const noexcept
Checks if the IP network is an IPv4 network.
Definition ip-any-network.hpp:252
constexpr inline ip_address hostmask() const noexcept
Retrieves the hostmask of this network.
Definition ip-any-network.hpp:135
inline std::u16string to_u16string(format fmt=format::compressed) const
Converts the network to a string representation.
Definition ip-any-network.hpp:805
constexpr inline bool is_site_local() const noexcept
Checks if the IPv6 network is site-local.
Definition ip-any-network.hpp:243
constexpr inline ip_network(const ipv4_network &net4) noexcept
Constructor from an ipv4_network.
Definition ip-any-network.hpp:710
constexpr inline bool is_multicast() const noexcept
Checks if the network is a multicast network.
Definition ip-any-network.hpp:149
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.
Definition ip-any-network.hpp:759
constexpr inline bool is_private() const noexcept
Checks if the network is a private network.
Definition ip-any-network.hpp:163
constexpr inline bool operator<=(const ip_network &rhs) const noexcept
Less than or equal to comparison operator.
Definition ip-any-network.hpp:1295
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:372
constexpr inline ip_address netmask() const noexcept
Retrieves the netmask of this network.
Definition ip-any-network.hpp:124
constexpr inline bool is_reserved() const noexcept
Checks if the network is a reserved network.
Definition ip-any-network.hpp:189
constexpr inline uint128_t addresses_count() const noexcept
Calculates the total number of addresses in the network.
Definition ip-any-network.hpp:294
constexpr inline bool is_link_local() const noexcept
Checks if the network is a link-local network.
Definition ip-any-network.hpp:216
constexpr inline bool operator==(const ip_network &rhs) const noexcept
Equality comparison operator.
Definition ip-any-network.hpp:1218
constexpr inline bool operator>(const ip_network &rhs) const noexcept
Greater than comparison operator.
Definition ip-any-network.hpp:1283
constexpr inline ip_address network_address() const noexcept
Retrieves the network address of this network.
Definition ip-any-network.hpp:100
inline operator std::basic_string< T, std::char_traits< T >, std::allocator< T > >() const
Converts the ip network object to a std::string.
Definition ip-any-network.hpp:1206
constexpr inline subnets_any_sequence< ip_network > subnets(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.
Definition ip-any-network.hpp:572
constexpr inline ip_network() noexcept
Constructs a new IP network object.
Definition ip-any-network.hpp:700
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.
Definition ip-any-network.hpp:737
constexpr inline ip_version version() const noexcept
Retrieves the IP version of the network.
Definition ip-any-network.hpp:77
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.
Definition ip-any-network.hpp:456
constexpr inline bool is_unspecified() const noexcept
Checks if the network is an unspecified network.
Definition ip-any-network.hpp:230
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.
Definition ip-any-network.hpp:1195
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:318
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:399
constexpr inline bool is_global() const noexcept
Checks if the network is a global network.
Definition ip-any-network.hpp:177
static constexpr inline ip_network parse(const T(&address)[N], bool strict=true)
Parses a network address and prefix from a character array.
Definition ip-any-network.hpp:1177
constexpr inline ip_address broadcast_address() const noexcept
Retrieves the broadcast address of this network.
Definition ip-any-network.hpp:112
constexpr inline bool operator!=(const ip_network &rhs) const noexcept
Inequality comparison operator.
Definition ip-any-network.hpp:1233
constexpr inline hosts_any_sequence hosts() const noexcept
Retrieves a sequence of host addresses in the network.
Definition ip-any-network.hpp:495
constexpr inline bool overlaps(const ip_network &other) const noexcept
Determines if this network overlaps with another network.
Definition ip-any-network.hpp:345
constexpr inline size_t hash() const noexcept
Computes a hash value for the IP address.
Definition ip-any-network.hpp:285
constexpr inline bool operator<(const ip_network &rhs) const noexcept
Less than comparison operator.
Definition ip-any-network.hpp:1265
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:428
constexpr inline optional< ipv6_network > v6() const noexcept
Retrieves the IPv6 network.
Definition ip-any-network.hpp:686
inline std::wstring to_wstring(format fmt=format::compressed) const
Converts the network to a string representation.
Definition ip-any-network.hpp:792
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:532
constexpr inline exclude_network_sequence< ip_network > address_exclude(const ip_network &other, error_code &code) const noexcept
Computes the network definitions resulting from removing the given network from this one,...
Definition ip-any-network.hpp:641
constexpr inline size_t prefixlen() const noexcept
Retrieves the prefix length of this network.
Definition ip-any-network.hpp:89
constexpr inline size_t size() const noexcept
Retrieves the size of the IP address.
Definition ip-any-network.hpp:273
inline std::u32string to_u32string(format fmt=format::compressed) const
Converts the network to a string representation.
Definition ip-any-network.hpp:818
constexpr inline exclude_network_sequence< ip_network > address_exclude(const ip_network &other) const
Computes the network definitions resulting from removing the given network from this one.
Definition ip-any-network.hpp:605
constexpr inline optional< ipv4_network > v4() const noexcept
Retrieves the IPv4 network.
Definition ip-any-network.hpp:672
constexpr inline bool is_loopback() const noexcept
Checks if the network is a loopback network.
Definition ip-any-network.hpp:203
inline std::string to_string(format fmt=format::compressed) const
Converts the network to a string representation.
Definition ip-any-network.hpp:779
constexpr inline bool is_v6() const noexcept
Checks if the IP network is an IPv6 network.
Definition ip-any-network.hpp:261
constexpr inline ip_network(const ipv6_network &net6) noexcept
Constructor from an ipv6_network.
Definition ip-any-network.hpp:720
A template class to manage an optional contained value.
Definition optional.hpp:35
A sequence container for subnet ranges within a network.
Definition ip-any-iterator.hpp:782
inline std::u8string to_u8string(format fmt=format::decimal) const
Converts the uint128_t value to a string representation.
Definition uint128.hpp:403
#define IPADDRESS_NOEXCEPT_WHEN_NO_EXCEPTIONS
Definition config.hpp:96
#define IPADDRESS_EXPORT
Definition config.hpp:45
#define IPADDRESS_NODISCARD
Definition config.hpp:101
#define IPADDRESS_FORCE_INLINE
Definition config.hpp:115
#define IPADDRESS_NAMESPACE
Definition config.hpp:41
#define IPADDRESS_NOEXCEPT
Definition config.hpp:92
#define IPADDRESS_NODISCARD_WHEN_NO_EXCEPTIONS
Definition config.hpp:105
ip_version
Enumerates the IP address versions.
Definition ip-address-base.hpp:29
@ V4
IPv4 version identifier.
@ V6
IPv6 version identifier.
format
Enumerates the formatting options for IP address strings.
Definition ip-address-base.hpp:40
@ compressed
Compressed format with maximal omission of segments or octets.
error_code
Enumeration of error codes for IP address parsing and validation.
Definition errors.hpp:52
@ invalid_version
The IP address version does not match the expected version.
@ not_contained_network
The network is not a subnet of the other network as expected.
@ no_error
Indicates the absence of any errors.