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
33struct net_any_parser {
34 template <
typename Str>
37 const auto net4 = ipv4_network::parse(address, code, strict);
41 return T(ipv6_network::parse(address, strict));
44 template <
typename Str>
47 const auto net4 = ipv4_network::parse(address, code, strict);
51 const auto net6 = ipv6_network::parse(address, code, strict);
62
63
64
65
66
67
68
69
76
77
78
79
85
86
87
88
89
90
91
93 return is_v4() ? _ipv_net.ipv4.prefixlen() : _ipv_net.ipv6.prefixlen();
97
98
99
100
101
102
104 return is_v4() ? ip_address(_ipv_net.ipv4.network_address()) : ip_address(_ipv_net.ipv6.network_address());
108
109
110
111
112
113
114
116 return is_v4() ? ip_address(_ipv_net.ipv4.broadcast_address()) : ip_address(_ipv_net.ipv6.broadcast_address());
120
121
122
123
124
125
126
128 return is_v4() ? ip_address(_ipv_net.ipv4.netmask()) : ip_address(_ipv_net.ipv6.netmask());
132
133
134
135
136
137
139 return is_v4() ? ip_address(_ipv_net.ipv4.hostmask()) : ip_address(_ipv_net.ipv6.hostmask());
143
144
145
146
147
148
149
150
151
153 return is_v4() ? _ipv_net.ipv4.is_multicast() : _ipv_net.ipv6.is_multicast();
157
158
159
160
161
162
163
164
165
167 return is_v4() ? _ipv_net.ipv4.is_private() : _ipv_net.ipv6.is_private();
171
172
173
174
175
176
177
178
179
181 return is_v4() ? _ipv_net.ipv4.is_global() : _ipv_net.ipv6.is_global();
185
186
187
188
189
190
191
193 return is_v4() ? _ipv_net.ipv4.is_reserved() : _ipv_net.ipv6.is_reserved();
197
198
199
200
201
202
203
204
205
207 return is_v4() ? _ipv_net.ipv4.is_loopback() : _ipv_net.ipv6.is_loopback();
211
212
213
214
215
216
217
218
220 return is_v4() ? _ipv_net.ipv4.is_link_local() : _ipv_net.ipv6.is_link_local();
224
225
226
227
228
229
230
231
232
234 return is_v4() ? _ipv_net.ipv4.is_unspecified() : _ipv_net.ipv6.is_unspecified();
238
239
240
241
242
243
244
245
247 return is_v4() ?
false : _ipv_net.ipv6.is_site_local();
251
252
253
254
260
261
262
263
269
270
271
272
273
274
275
277 return is_v4() ? _ipv_net.ipv4.size() : _ipv_net.ipv6.size();
281
282
283
284
285
286
287
289 return is_v4() ? _ipv_net.ipv4.hash() : _ipv_net.ipv6.hash();
293
294
295
296
298 return is_v4() ? uint128_t(_ipv_net.ipv4.addresses_count()) : _ipv_net.ipv6.addresses_count();
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
323 return is_v4() ? _ipv_net.ipv4.contains(address.v4().value()) : _ipv_net.ipv6.contains(address.v6().value());
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
350 return is_v4() ? _ipv_net.ipv4.overlaps(other.v4().value()) : _ipv_net.ipv6.overlaps(other.v6().value());
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
377 return is_v4() ? _ipv_net.ipv4.subnet_of(other.v4().value()) : _ipv_net.ipv6.subnet_of(other.v6().value());
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
404 return is_v4() ? _ipv_net.ipv4.supernet_of(other.v4().value()) : _ipv_net.ipv6.supernet_of(other.v6().value());
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
432 return is_v4() ? ip_network(_ipv_net.ipv4.supernet(prefixlen_diff, new_prefixlen)) : ip_network(_ipv_net.ipv6.supernet(prefixlen_diff, new_prefixlen));
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
460 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));
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
495
496
497
500 const auto sequence = _ipv_net.ipv4.hosts();
501 return hosts_any_sequence(sequence.begin(), sequence.end());
503 const auto sequence = _ipv_net.ipv6.hosts();
504 return hosts_any_sequence(sequence.begin(), sequence.end());
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
537 const auto sequence = _ipv_net.ipv4.subnets(prefixlen_diff, new_prefixlen);
538 return subnets_any_sequence<ip_network>(sequence.begin(), sequence.end());
540 const auto sequence = _ipv_net.ipv6.subnets(prefixlen_diff, new_prefixlen);
541 return subnets_any_sequence<ip_network>(sequence.begin(), sequence.end());
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
572
573
574
577 const auto sequence = _ipv_net.ipv4.subnets(code, prefixlen_diff, new_prefixlen);
578 return subnets_any_sequence<ip_network>(sequence.begin(), sequence.end());
580 const auto sequence = _ipv_net.ipv6.subnets(code, prefixlen_diff, new_prefixlen);
581 return subnets_any_sequence<ip_network>(sequence.begin(), sequence.end());
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
612 raise_error(code, 0,
"", 0);
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
646 if (_version != other._version) {
648 return exclude_network_sequence<ip_network>();
651 auto addr1 = network_address(); addr1.set_scope_id(
"");
652 auto addr2 = other.network_address(); addr2.set_scope_id(
"");
653 auto lhs = ip_network::from_address(addr1, prefixlen());
654 auto rhs = ip_network::from_address(addr2, other.prefixlen());
656 if (!rhs.subnet_of(lhs)) {
658 return exclude_network_sequence<ip_network>();
662 return exclude_network_sequence<ip_network>();
665 return exclude_network_sequence<ip_network>(lhs, rhs);
669
670
671
672
673
674
677 return optional<ipv4_network>(_ipv_net.ipv4);
679 return optional<ipv4_network>();
683
684
685
686
687
688
691 return optional<ipv6_network>(_ipv_net.ipv6);
693 return optional<ipv6_network>();
697
698
699
700
701
702
707
708
709
710
711
712
717
718
719
720
721
722
727
728
729
730
731
732
733
734
735
736
737
738
739
745 return address.is_v4()
746 ? ip_network(ipv4_network::from_address(address.v4().value(), prefixlen, strict))
747 : ip_network(ipv6_network::from_address(address.v6().value(), prefixlen, strict));
751
752
753
754
755
756
757
758
759
760
761
768 return address.is_v4()
769 ? ip_network(ipv4_network::from_address(address.v4().value(), code, prefixlen, strict))
770 : ip_network(ipv6_network::from_address(address.v6().value(), code, prefixlen, strict));
774
775
776
777
778
779
780
781
783 return is_v4() ? _ipv_net.ipv4.to_string(fmt) : _ipv_net.ipv6.to_string(fmt);
787
788
789
790
791
792
793
794
796 return is_v4() ? _ipv_net.ipv4.to_wstring(fmt) : _ipv_net.ipv6.to_wstring(fmt);
800
801
802
803
804
805
806
807
809 return is_v4() ? _ipv_net.ipv4.to_u16string(fmt) : _ipv_net.ipv6.to_u16string(fmt);
813
814
815
816
817
818
819
820
822 return is_v4() ? _ipv_net.ipv4.to_u32string(fmt) : _ipv_net.ipv6.to_u32string(fmt);
825#if __cpp_char8_t >= 201811L
828
829
830
831
832
833
834
835
843
844
845
846
847
848
849
851 const auto tmp = *
this;
856#ifdef IPADDRESS_NONTYPE_TEMPLATE_PARAMETER
859
860
861
862
863
864
865
866
867
868
889#if IPADDRESS_CPP_VERSION
>= 17
892
893
894
895
896
897
898
899
900
901
902
903
909
910
911
912
913
914
915
916
917
918
919
920
925#if __cpp_char8_t >= 201811L
928
929
930
931
932
933
934
935
936
937
938
946
947
948
949
950
951
952
953
954
955
956
957
963
964
965
966
967
968
969
970
971
972
973
974
980
981
982
983
984
985
986
987
988
989
990
991
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1013#if __cpp_char8_t >= 201811L
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1070
1071
1072
1073
1074
1075
1076
1078 return internal::net_any_parser<ip_network>::parse(address, strict);
1082
1083
1084
1085
1086
1087
1088
1090 return internal::net_any_parser<ip_network>::parse(address, strict);
1094
1095
1096
1097
1098
1099
1100
1102 return internal::net_any_parser<ip_network>::parse(address, strict);
1106
1107
1108
1109
1110
1111
1112
1114 return internal::net_any_parser<ip_network>::parse(address, strict);
1118
1119
1120
1121
1122
1123
1124
1126 return internal::net_any_parser<ip_network>::parse(address, code, strict);
1130
1131
1132
1133
1134
1135
1136
1138 return internal::net_any_parser<ip_network>::parse(address, code, strict);
1142
1143
1144
1145
1146
1147
1148
1150 return internal::net_any_parser<ip_network>::parse(address, code, strict);
1154
1155
1156
1157
1158
1159
1160
1162 return internal::net_any_parser<ip_network>::parse(address, code, strict);
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179 template <
typename T, size_t N>
1181 return internal::net_any_parser<ip_network>::parse(address, strict);
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197 template <
typename T, size_t N>
1199 return internal::net_any_parser<ip_network>::parse(address, code, strict);
1203
1204
1205
1206
1207
1208 template <
typename T>
1210 return internal::string_converter<T>::convert(
to_string());
1214
1215
1216
1217
1218
1219
1220
1222 if (_version != rhs._version) {
1225 return _version == ip_version::V4 ? (_ipv_net.ipv4 == rhs._ipv_net.ipv4) : (_ipv_net.ipv6 == rhs._ipv_net.ipv6);
1229
1230
1231
1232
1233
1234
1235
1237 return !(*
this == rhs);
1240#ifdef IPADDRESS_HAS_SPACESHIP_OPERATOR
1243
1244
1245
1246
1247
1248
1249
1261
1262
1263
1264
1265
1266
1267
1269 if (_version < rhs._version) {
1272 if (_version > rhs._version) {
1275 return _version == ip_version::V4 ? (_ipv_net.ipv4 < rhs._ipv_net.ipv4) : (_ipv_net.ipv6 < rhs._ipv_net.ipv6);
1279
1280
1281
1282
1283
1284
1285
1291
1292
1293
1294
1295
1296
1297
1299 return !(rhs
< *
this);
1303
1304
1305
1306
1307
1308
1309
1311 return !(*
this < rhs);
1317 union ip_any_network {
1333#ifdef IPADDRESS_NONTYPE_TEMPLATE_PARAMETER
1336
1337
1338
1339
1340
1341
1342
1343
1344 IPADDRESS_EXPORT
template <fixed_string FixedString>
1345 IPADDRESS_NODISCARD IPADDRESS_CONSTEVAL IPADDRESS_FORCE_INLINE ip_network operator
""_net() IPADDRESS_NOEXCEPT {
1346 return ip_network::parse<FixedString>();
1352
1353
1354
1355
1356
1357
1358
1359
1360
1362 const auto max_len = ipv6_address::base_max_string_len * 2 + 1;
1363 if (size > max_len) {
1364 raise_error(error_code::string_is_too_long, 0, address, size);
1366 char str[max_len + 1] = {};
1367 for (size_t i = 0; i < size && i < max_len; ++i) {
1368 str[i] = address[i];
1370 return ip_network::parse(str);
1374
1375
1376
1377
1378
1379
1380
1381
1382
1384 const auto max_len = ipv6_address::base_max_string_len * 2 + 1;
1385 if (size > max_len) {
1386 raise_error(error_code::string_is_too_long, 0, address, size);
1388 wchar_t str[max_len + 1] = {};
1389 for (size_t i = 0; i < size && i < max_len; ++i) {
1390 str[i] = address[i];
1392 return ip_network::parse(str);
1396
1397
1398
1399
1400
1401
1402
1403
1404
1406 const auto max_len = ipv6_address::base_max_string_len * 2 + 1;
1407 if (size > max_len) {
1408 raise_error(error_code::string_is_too_long, 0, address, size);
1410 char16_t str[max_len + 1] = {};
1411 for (size_t i = 0; i < size && i < max_len; ++i) {
1412 str[i] = address[i];
1414 return ip_network::parse(str);
1418
1419
1420
1421
1422
1423
1424
1425
1426
1428 const auto max_len = ipv6_address::base_max_string_len * 2 + 1;
1429 if (size > max_len) {
1430 raise_error(error_code::string_is_too_long, 0, address, size);
1432 char32_t str[max_len + 1] = {};
1433 for (size_t i = 0; i < size && i < max_len; ++i) {
1434 str[i] = address[i];
1436 return ip_network::parse(str);
1443#ifndef IPADDRESS_NO_OVERLOAD_STD
1450 return network.hash();
1474 if (stream.flags() & ios_base::uppercase) {
1475 auto end = std::find(str.cbegin(), str.cend(),
'%');
1476 std::transform(str.cbegin(), end, str.begin(), [](
char c){
1477 return std::toupper(c);
1486 auto strict = iword == 0;
1489 std::basic_string<T, std::char_traits<T>, std::allocator<T>> str;
1494 stream.setstate(std::ios_base::failbit);
A sequence container for networks excluding specified subnets.
Definition ip-network-iterator.hpp:890
A sequence of host IP addresses.
Definition ip-any-iterator.hpp:479
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:70
constexpr inline void swap(ip_network &net) noexcept
Swaps the contents of this network with another network.
Definition ip-any-network.hpp:850
constexpr inline bool operator>=(const ip_network &rhs) const noexcept
Greater than or equal to comparison operator.
Definition ip-any-network.hpp:1310
constexpr inline bool is_v4() const noexcept
Checks if the IP network is an IPv4 network.
Definition ip-any-network.hpp:255
constexpr inline ip_address hostmask() const noexcept
Retrieves the hostmask of this network.
Definition ip-any-network.hpp:138
inline std::u16string to_u16string(format fmt=format::compressed) const
Converts the network to a string representation.
Definition ip-any-network.hpp:808
constexpr inline bool is_site_local() const noexcept
Checks if the IPv6 network is site-local.
Definition ip-any-network.hpp:246
constexpr inline ip_network(const ipv4_network &net4) noexcept
Constructor from an ipv4_network.
Definition ip-any-network.hpp:713
constexpr inline bool is_multicast() const noexcept
Checks if the network is a multicast network.
Definition ip-any-network.hpp:152
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:762
constexpr inline bool is_private() const noexcept
Checks if the network is a private network.
Definition ip-any-network.hpp:166
constexpr inline bool operator<=(const ip_network &rhs) const noexcept
Less than or equal to comparison operator.
Definition ip-any-network.hpp:1298
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
constexpr inline ip_address netmask() const noexcept
Retrieves the netmask of this network.
Definition ip-any-network.hpp:127
constexpr inline bool is_reserved() const noexcept
Checks if the network is a reserved network.
Definition ip-any-network.hpp:192
constexpr inline uint128_t addresses_count() const noexcept
Calculates the total number of addresses in the network.
Definition ip-any-network.hpp:297
constexpr inline bool is_link_local() const noexcept
Checks if the network is a link-local network.
Definition ip-any-network.hpp:219
constexpr inline bool operator==(const ip_network &rhs) const noexcept
Equality comparison operator.
Definition ip-any-network.hpp:1221
constexpr inline bool operator>(const ip_network &rhs) const noexcept
Greater than comparison operator.
Definition ip-any-network.hpp:1286
constexpr inline ip_address network_address() const noexcept
Retrieves the network address of this network.
Definition ip-any-network.hpp:103
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:1209
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:575
constexpr inline ip_network() noexcept
Constructs a new IP network object.
Definition ip-any-network.hpp:703
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:740
constexpr inline ip_version version() const noexcept
Retrieves the IP version of the network.
Definition ip-any-network.hpp:80
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:459
constexpr inline bool is_unspecified() const noexcept
Checks if the network is an unspecified network.
Definition ip-any-network.hpp:233
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:1198
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
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
constexpr inline bool is_global() const noexcept
Checks if the network is a global network.
Definition ip-any-network.hpp:180
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:1180
constexpr inline ip_address broadcast_address() const noexcept
Retrieves the broadcast address of this network.
Definition ip-any-network.hpp:115
constexpr inline bool operator!=(const ip_network &rhs) const noexcept
Inequality comparison operator.
Definition ip-any-network.hpp:1236
constexpr inline hosts_any_sequence hosts() const noexcept
Retrieves a sequence of host addresses in the network.
Definition ip-any-network.hpp:498
constexpr inline bool overlaps(const ip_network &other) const noexcept
Determines if this network overlaps with another network.
Definition ip-any-network.hpp:348
constexpr inline size_t hash() const noexcept
Computes a hash value for the IP address.
Definition ip-any-network.hpp:288
constexpr inline bool operator<(const ip_network &rhs) const noexcept
Less than comparison operator.
Definition ip-any-network.hpp:1268
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
constexpr inline optional< ipv6_network > v6() const noexcept
Retrieves the IPv6 network.
Definition ip-any-network.hpp:689
inline std::wstring to_wstring(format fmt=format::compressed) const
Converts the network to a string representation.
Definition ip-any-network.hpp:795
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
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:644
constexpr inline size_t prefixlen() const noexcept
Retrieves the prefix length of this network.
Definition ip-any-network.hpp:92
constexpr inline size_t size() const noexcept
Retrieves the size of the IP address.
Definition ip-any-network.hpp:276
inline std::u32string to_u32string(format fmt=format::compressed) const
Converts the network to a string representation.
Definition ip-any-network.hpp:821
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:608
constexpr inline optional< ipv4_network > v4() const noexcept
Retrieves the IPv4 network.
Definition ip-any-network.hpp:675
constexpr inline bool is_loopback() const noexcept
Checks if the network is a loopback network.
Definition ip-any-network.hpp:206
inline std::string to_string(format fmt=format::compressed) const
Converts the network to a string representation.
Definition ip-any-network.hpp:782
constexpr inline bool is_v6() const noexcept
Checks if the IP network is an IPv6 network.
Definition ip-any-network.hpp:264
constexpr inline ip_network(const ipv6_network &net6) noexcept
Constructor from an ipv6_network.
Definition ip-any-network.hpp:723
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:658
inline std::u8string to_u8string(format fmt=format::decimal) const
Converts the uint128_t value to a string representation.
Definition uint128.hpp:402
#define IPADDRESS_NOEXCEPT_WHEN_NO_EXCEPTIONS
Definition config.hpp:93
#define IPADDRESS_EXPORT
Definition config.hpp:42
#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
#define IPADDRESS_NODISCARD_WHEN_NO_EXCEPTIONS
Definition config.hpp:102
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.