2
3
4
5
6
7
8
9
10
12#ifndef IPADDRESS_IP_NETWORK_BASE_HPP
13#define IPADDRESS_IP_NETWORK_BASE_HPP
21
22
23
24
25
26
27
28
29
30
34 using ip_address_type =
typename Base::ip_address_type;
35 using uint_type =
typename ip_address_type::uint_type;
38
39
40
41
42
43
47 _netmask(ip_address_type::ip_from_prefix(Base::base_max_prefixlen)),
51#ifdef IPADDRESS_NONTYPE_TEMPLATE_PARAMETER
54
55
56
57
58
59
60
61
62
63
78#if IPADDRESS_CPP_VERSION
>= 17
81
82
83
84
85
86
87
88
89
90
91
92
98
99
100
101
102
103
104
105
106
107
108
109
114#if __cpp_char8_t >= 201811L
117
118
119
120
121
122
123
124
125
126
127
135
136
137
138
139
140
141
142
143
144
145
146
152
153
154
155
156
157
158
159
160
161
162
163
169
170
171
172
173
174
175
176
177
178
179
180
187
188
189
190
191
192
193
194
195
196
197
198
204#if __cpp_char8_t >= 201811L
207
208
209
210
211
212
213
214
215
216
217
226
227
228
229
230
231
232
233
234
235
236
237
244
245
246
247
248
249
250
251
252
253
254
255
264
265
266
267
268
269
270
272 return parse_address_with_prefix(address, strict);
276
277
278
279
280
281
282
284 return parse_address_with_prefix(address, strict);
288
289
290
291
292
293
294
296 return parse_address_with_prefix(address, strict);
300
301
302
303
304
305
306
308 return parse_address_with_prefix(address, strict);
312
313
314
315
316
317
318
321 return parse_address_with_prefix(address, strict, code, value);
325
326
327
328
329
330
331
334 return parse_address_with_prefix(address, strict, code, value);
338
339
340
341
342
343
344
347 return parse_address_with_prefix(address, strict, code, value);
351
352
353
354
355
356
357
360 return parse_address_with_prefix(address, strict, code, value);
366
367
368
369
370
371
372
373
374
375
376
377 template <
typename T, size_t N>
379 #ifdef IPADDRESS_NO_EXCEPTIONS
380 error_code err = error_code::no_error;
381 auto str = make_fixed_string(address, err);
382 if (err != error_code::no_error) {
386 auto str = make_fixed_string(address);
388 return parse_address_with_prefix(str, strict);
392
393
394
395
396
397
398
399
400
401
402
403
404 template <
typename T, size_t N>
406 auto str = make_fixed_string(address, code);
412
413
414
415
416
417
418
419
420
421
422
423
424
426 const ip_address_type& address,
427 size_t prefixlen = ip_address_type::max_prefixlen,
431 auto result = from_address(address, code, prefixlen, strict);
433 char str[ip_address_type::base_max_string_len + 1]{};
435 raise_error(code, 0, str, len);
441
442
443
444
445
446
447
448
449
450
451
453 const ip_address_type& address,
455 size_t prefixlen = ip_address_type::max_prefixlen,
460 const auto netmask = ip_address_type::ip_from_prefix(prefixlen);
463 result._network_address = ip_address_type::strict_netmask(address, netmask, strict, code);
469 result._netmask = netmask;
470 result._prefixlen = prefixlen;
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
495 const auto& ip_bytes = address.bytes();
497 const auto& netmask_bytes =
netmask().bytes();
499 for (size_t i = 0; i < ip_address_type::base_size; ++i) {
500 if ((ip_bytes[i] & netmask_bytes[i]) != address_bytes[i]) {
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
550 return is_subnet_of(*
this, other);
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
573 return is_subnet_of(other, *
this);
577
578
579
580
581
582
583
584
586 return _network_address.to_string(fmt) +
'/' + std::to_string(_prefixlen);
590
591
592
593
594
595
596
597
599 return internal::string_converter<
wchar_t>::convert(to_string(fmt));
603
604
605
606
607
608
609
610
612 return internal::string_converter<
char16_t>::convert(to_string(fmt));
616
617
618
619
620
621
622
623
625 return internal::string_converter<
char32_t>::convert(to_string(fmt));
628#if __cpp_char8_t >= 201811L
631
632
633
634
635
636
637
638
646
647
648
649
650
651
652
654 _network_address.swap(network._network_address);
655 _netmask.swap(network._netmask);
656 auto tmp = _prefixlen;
657 _prefixlen = network._prefixlen;
658 network._prefixlen = tmp;
662
663
664
665
666
667
668
670 return internal::calc_hash(_network_address.hash(), _netmask.hash());
674
675
676
677
678
679
681 return _network_address;
685
686
687
688
689
690
691
693 typename ip_address_type::base_type bytes {};
695 const auto hostmask_bytes =
hostmask().bytes();
696 for (size_t i = 0; i < ip_address_type::base_size; ++i) {
697 bytes[i] = uint8_t(network_address_bytes[i] | hostmask_bytes[i]);
699 return ip_address_type(bytes);
703
704
705
706
707
708
709
715
716
717
718
719
720
722 const auto& netmask_bytes =
netmask().bytes();
723 typename ip_address_type::base_type bytes {};
724 for (size_t i = 0; i < ip_address_type::base_size; ++i) {
725 bytes[i] = uint8_t(netmask_bytes[i] ^ 0xFF);
727 return ip_address_type(bytes);
731
732
733
734
735
736
737
743
744
745
746
747
748
749
750
751
757
758
759
760
761
762
763
764
765
769
770
771
772
773
774
775
776
777
781
782
783
784
785
786
787
793
794
795
796
797
798
799
800
801
807
808
809
810
811
812
813
814
820
821
822
823
824
825
826
827
828
834
835
836
837
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
878 return hosts_sequence<ip_address_type>(network_address(), broadcast_address(), prefixlen(), ip_address_type::base_max_prefixlen);
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
906 const auto result = address_exclude(other, code);
908 raise_error(code, 0,
"", 0);
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
941 auto lhs = Base::remove_scope_id(*
this);
942 auto rhs = Base::remove_scope_id(other);
944 if (!rhs.subnet_of(lhs)) {
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
985 const auto result = subnets(code, prefixlen_diff, new_prefixlen);
987 raise_error(code, 0,
"", 0);
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1025 if (prefixlen() == ip_address_type::base_max_prefixlen) {
1026 return subnets_sequence<ip_network_base<Base>>(address, prefixlen());
1029 if (new_prefixlen) {
1030 if (new_prefixlen.value() < prefixlen()) {
1034 if (prefixlen_diff != 1) {
1038 prefixlen_diff = new_prefixlen.value() - prefixlen();
1041 auto new_prefix = prefixlen() + prefixlen_diff;
1043 if (new_prefix > ip_address_type::base_max_prefixlen) {
1048 return subnets_sequence<ip_network_base<Base>>(address, broadcast_address(), hostmask(), prefixlen_diff, new_prefix);
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1074 const auto result = supernet(code, prefixlen_diff, new_prefixlen);
1076 raise_error(code, 0,
"", 0);
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1108 if (prefixlen() == 0) {
1112 if (new_prefixlen) {
1113 if (new_prefixlen.value() > prefixlen()) {
1117 if (prefixlen_diff != 1) {
1121 prefixlen_diff = prefixlen() - new_prefixlen.value();
1124 if (prefixlen_diff > prefixlen()) {
1129 const auto new_prefix = prefixlen() - prefixlen_diff;
1130 const auto address = ip_address_type::from_uint(network_address().to_uint() & (netmask().to_uint() << prefixlen_diff));
1131 return ip_network_base<Base>::from_address(address, new_prefix);
1135
1136
1137
1138
1139
1140 template <
typename T>
1142 return internal::string_converter<T>::convert(
to_string());
1146
1147
1148
1149
1150
1151
1152
1154 return _network_address == rhs._network_address && _netmask == rhs._netmask;
1158
1159
1160
1161
1162
1163
1164
1166 return !(*
this == rhs);
1169#ifdef IPADDRESS_HAS_SPACESHIP_OPERATOR
1172
1173
1174
1175
1176
1177
1178
1189
1190
1191
1192
1193
1194
1195
1197 if (_network_address != rhs._network_address) {
1198 return _network_address < rhs._network_address;
1200 if (_netmask != rhs._netmask) {
1201 return _netmask < rhs._netmask;
1207
1208
1209
1210
1211
1212
1213
1219
1220
1221
1222
1223
1224
1225
1227 return !(rhs < *
this);
1231
1232
1233
1234
1235
1236
1237
1239 return !(*
this < rhs);
1245 template <
typename Str>
1249 auto result = parse_address_with_prefix(str, strict, code, value);
1251 raise_error(code, value, str.data(), str.size());
1256 template <
typename Str>
1259 auto it = str.data();
1260 auto end = str.data() + str.size();
1261 auto has_slash =
false;
1264 char address[ip_address_type::base_max_string_len + 1] = {};
1266 const auto c = internal::next_char_or_error(it, end, code, code_value);
1283 address[symbol++] = c;
1285 if (symbol > ip_address_type::base_max_string_len) {
1290 auto netmask_result = ip_address_type::parse_netmask(netmask, end, code, code_value);
1297 result._network_address = ip_address_type::parse(address, code);
1298 result._netmask = std::get<0>(netmask_result);
1299 result._prefixlen = std::get<1>(netmask_result);
1305 result._network_address = ip_address_type::strict_netmask(result._network_address, result._netmask, strict, code);
1315 return rhs.network_address() <= lhs.network_address() && rhs.broadcast_address() >= lhs.broadcast_address();
1318 ip_address_type _network_address;
1319 ip_address_type _netmask;
1323#ifndef IPADDRESS_NONTYPE_TEMPLATE_PARAMETER
1327template <
typename Base,
typename TChar, size_t MaxLen>
1329 if (size > MaxLen) {
1330 raise_error(error_code::string_is_too_long, 0, address, size);
1332 TChar str[MaxLen + 1] = {};
1333 for (size_t i = 0; i < size && i < MaxLen; ++i) {
1334 str[i] = address[i];
1343#ifndef IPADDRESS_NO_OVERLOAD_STD
1346 static int i = std::ios_base::xalloc();
1351IPADDRESS_FORCE_INLINE std::basic_istream<T, std::char_traits<T>>& strict(std::basic_istream<T, std::char_traits<T>>& stream) {
1352 stream.iword(network_strict_index()) = 0;
1357IPADDRESS_FORCE_INLINE std::basic_istream<T, std::char_traits<T>>& non_strict(std::basic_istream<T, std::char_traits<T>>& stream) {
1358 stream.iword(network_strict_index()) = 1;
1366#ifndef IPADDRESS_NO_OVERLOAD_STD
1373 return network.hash();
1379 return net1.swap(net2);
1384 return network.to_string();
1389 return network.to_wstring();
1399 auto str = network.to_string(fmt);
1400 if (stream.flags() & ios_base::uppercase) {
1401 auto end = std::find(str.cbegin(), str.cend(),
'%');
1402 std::transform(str.cbegin(), end, str.begin(), [](
char c){
1403 return std::toupper(c);
1412 auto strict = iword == 0;
1415 std::basic_string<T, std::char_traits<T>, std::allocator<T>> str;
1420 stream.setstate(std::ios_base::failbit);
A sequence container for networks excluding specified subnets.
Definition ip-network-iterator.hpp:890
Template base class for representing a network of IP addresses.
Definition ip-network-base.hpp:32
inline std::u16string to_u16string(format fmt=format::compressed) const
Converts the network to a string representation.
Definition ip-network-base.hpp:611
constexpr inline bool operator<(const ip_network_base &rhs) const noexcept
Less than comparison operator.
Definition ip-network-base.hpp:1196
constexpr inline bool is_multicast() const noexcept
Checks if the network is a multicast network.
Definition ip-network-base.hpp:752
constexpr inline bool is_private() const noexcept
Checks if the network is a private network.
Definition ip-networks.hpp:358
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.
Definition ip-network-base.hpp:904
constexpr inline ip_address_type hostmask() const noexcept
Retrieves the hostmask of this network.
Definition ip-network-base.hpp:721
constexpr inline bool operator!=(const ip_network_base &rhs) const noexcept
Inequality comparison operator.
Definition ip-network-base.hpp:1165
static constexpr inline ip_network_base parse(const T(&address)[N], bool strict=true)
Parses a network address and prefix from a character array.
Definition ip-network-base.hpp:378
constexpr inline bool contains(const ip_address_type &address) const noexcept
Checks if the given IP address is contained within this network.
Definition ip-network-base.hpp:494
constexpr inline bool is_reserved() const noexcept
Checks if the network is a reserved network.
Definition ip-network-base.hpp:788
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.
Definition ip-network-base.hpp:1072
constexpr inline bool is_link_local() const noexcept
Checks if the network is a link-local network.
Definition ip-network-base.hpp:815
constexpr inline ip_address_type broadcast_address() const noexcept
Retrieves the broadcast address of this network.
Definition ip-network-base.hpp:692
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.
Definition ip-network-base.hpp:452
constexpr inline hosts_sequence< ip_address_type > hosts() const noexcept
Retrieves a sequence of host addresses in the network.
Definition ip-network-base.hpp:877
constexpr inline uint_type addresses_count() const noexcept
Calculates the total number of addresses in the network.
Definition ip-network-base.hpp:838
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-network-base.hpp:1141
constexpr ip_network_base() noexcept
Constructs a new IP network base object.
Definition ip-network-base.hpp:44
constexpr inline bool overlaps(const ip_network_base &other) const noexcept
Determines if this network overlaps with another network.
Definition ip-network-base.hpp:526
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.
Definition ip-network-base.hpp:983
constexpr inline const ip_address_type & netmask() const noexcept
Retrieves the netmask of this network.
Definition ip-network-base.hpp:710
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.
Definition ip-network-base.hpp:1022
constexpr inline bool operator>(const ip_network_base &rhs) const noexcept
Greater than comparison operator.
Definition ip-network-base.hpp:1214
constexpr inline bool is_unspecified() const noexcept
Checks if the network is an unspecified network.
Definition ip-network-base.hpp:829
constexpr inline bool is_global() const noexcept
Checks if the network is a global network.
Definition ip-networks.hpp:363
constexpr inline bool supernet_of(const ip_network_base &other) const noexcept
Checks if this network is a supernet of another network.
Definition ip-network-base.hpp:572
constexpr inline bool operator<=(const ip_network_base &rhs) const noexcept
Less than or equal to comparison operator.
Definition ip-network-base.hpp:1226
constexpr inline size_t hash() const noexcept
Calculates a hash value for the network.
Definition ip-network-base.hpp:669
constexpr inline bool operator==(const ip_network_base &rhs) const noexcept
Equality comparison operator.
Definition ip-network-base.hpp:1153
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.
Definition ip-network-base.hpp:425
inline std::wstring to_wstring(format fmt=format::compressed) const
Converts the network to a string representation.
Definition ip-network-base.hpp:598
constexpr inline size_t prefixlen() const noexcept
Retrieves the prefix length of this network.
Definition ip-network-base.hpp:738
constexpr inline bool operator>=(const ip_network_base &rhs) const noexcept
Greater than or equal to comparison operator.
Definition ip-network-base.hpp:1238
inline std::u32string to_u32string(format fmt=format::compressed) const
Converts the network to a string representation.
Definition ip-network-base.hpp:624
constexpr inline void swap(ip_network_base &network) noexcept
Swaps the contents of this network with another network.
Definition ip-network-base.hpp:653
constexpr inline bool is_loopback() const noexcept
Checks if the network is a loopback network.
Definition ip-network-base.hpp:802
inline std::string to_string(format fmt=format::compressed) const
Converts the network to a string representation.
Definition ip-network-base.hpp:585
constexpr inline bool subnet_of(const ip_network_base &other) const noexcept
Checks if this network is a subnet of another network.
Definition ip-network-base.hpp:549
constexpr inline const ip_address_type & network_address() const noexcept
Retrieves the network address of this network.
Definition ip-network-base.hpp:680
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,...
Definition ip-network-base.hpp:940
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.
Definition ip-network-base.hpp:405
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.
Definition ip-network-base.hpp:1105
A sequence container for subnet ranges within a network.
Definition ip-network-iterator.hpp:678
#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
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
@ cannot_set_prefixlen_diff_and_new_prefix
Both prefix length difference and new prefix cannot be set simultaneously.
@ not_contained_network
The network is not a subnet of the other network as expected.
@ new_prefix_must_be_shorter
The new prefix length must be shorter for the operation being performed.
@ no_error
Indicates the absence of any errors.
@ empty_netmask
The netmask portion of the address is empty when it should specify a valid netmask.
@ invalid_prefixlen_diff
The difference in prefix length is invalid for the operation being performed.
@ new_prefix_must_be_longer
The new prefix length must be longer for the operation being performed.
@ only_one_slash_permitted
Only one slash character is permitted, used to separate the address from the netmask.