2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
21#ifndef IPADDRESS_IP_ANY_ADDRESS_HPP
22#define IPADDRESS_IP_ANY_ADDRESS_HPP
32 template <
typename Str>
35 const auto ipv4 = ipv4_address::parse(address, code);
39 return T(ipv6_address::parse(address));
42 template <
typename Str>
45 const auto ipv4 = ipv4_address::parse(address, code);
49 const auto ipv6 = ipv6_address::parse(address, code);
60
61
62
63
64
65
66
67
68
69
70
71
72
82
83
84
85
91
92
93
94
95
96
98 return _version == ip_version::V4 ? _ipv.ipv4.is_multicast() : _ipv.ipv6.is_multicast();
102
103
104
105
106
107
109 return _version == ip_version::V4 ? _ipv.ipv4.is_private() : _ipv.ipv6.is_private();
113
114
115
116
117
118
120 return _version == ip_version::V4 ? _ipv.ipv4.is_global() : _ipv.ipv6.is_global();
124
125
126
127
129 return _version == ip_version::V4 ? _ipv.ipv4.is_reserved() : _ipv.ipv6.is_reserved();
133
134
135
136
137
138
140 return _version == ip_version::V4 ? _ipv.ipv4.is_loopback() : _ipv.ipv6.is_loopback();
144
145
146
147
149 return _version == ip_version::V4 ? _ipv.ipv4.is_link_local() : _ipv.ipv6.is_link_local();
153
154
155
156
157
158
159
160
161
162
164 return _version == ip_version::V4 ? _ipv.ipv4.is_unspecified() : _ipv.ipv6.is_unspecified();
168
169
170
171
172
173
174
175
176
177
178
179
181 return _version == ip_version::V4 ?
false : _ipv.ipv6.is_site_local();
185
186
187
188
194
195
196
197
203
204
205
206
207
208
210 return _version == ip_version::V4 ? _ipv.ipv4.size() : _ipv.ipv6.size();
214
215
216
217
218
219
220
222 return _version == ip_version::V4 ? _ipv.ipv4.hash() : _ipv.ipv6.hash();
226
227
228
229
230
231
232
234 return _version == ip_version::V4 ? optional<ipv4_address>() : _ipv.ipv6.ipv4_mapped();
238
239
240
241
242
243
244
245
246
247
248
250 return _version == ip_version::V4 ? _ipv.ipv4.ipv6_mapped() : _ipv.ipv6;
254
255
256
257
258
259
260
261
263 return _version == ip_version::V4 ? optional<ipv4_address>() : _ipv.ipv6.sixtofour();
267
268
269
270
271
272
273
274
275
276
277
279 return _version == ip_version::V4 ? optional<std::pair<ipv4_address, ipv4_address>>() : _ipv.ipv6.teredo();
283
284
285
286
287
288
291 return optional<ipv4_address>();
293 return optional<ipv4_address>(_ipv.ipv4);
297
298
299
300
301
302
305 return optional<ipv6_address>();
307 return optional<ipv6_address>(_ipv.ipv6);
311
312
313
314
315
316
317
319 return _version == ip_version::V4 ? _ipv.ipv4.data() : _ipv.ipv6.data();
323
324
325
326
331
332
333
334
335
336
341
342
343
344
345
346
351
352
353
354
355
356
361
362
363
364
365
366
371
372
373
374
375
377 return ip_address(bytes);
381
382
383
384
385
387 return ip_address(bytes);
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
411 return version == ip_version::V4 ? ip_address(ipv4_address::from_bytes(bytes, byte_count)) : ip_address(ipv6_address::from_bytes(bytes, byte_count));
415
416
417
418
419
420
422 return ip_address(ipv4_address::from_uint(ip));
426
427
428
429
430
431
433 return ip_address(ipv6_address::from_uint(ip));
437
438
439
440
441
442
443
444
446 return _version == ip_version::V4 ? _ipv.ipv4.to_uint() : uint32_t(_ipv.ipv6.to_uint());
450
451
452
453
454
455
456
457
459 return _version == ip_version::V4 ? uint128_t(_ipv.ipv4.to_uint()) : _ipv.ipv6.to_uint();
463
464
465
466
467
468
469
471 return _version == ip_version::V4 ? _ipv.ipv4.to_string(fmt) : _ipv.ipv6.to_string(fmt);
475
476
477
478
479
480
481
483 return _version == ip_version::V4 ? _ipv.ipv4.to_wstring(fmt) : _ipv.ipv6.to_wstring(fmt);
487
488
489
490
491
492
493
495 return _version == ip_version::V4 ? _ipv.ipv4.to_u16string(fmt) : _ipv.ipv6.to_u16string(fmt);
499
500
501
502
503
504
505
507 return _version == ip_version::V4 ? _ipv.ipv4.to_u32string(fmt) : _ipv.ipv6.to_u32string(fmt);
510#if __cpp_char8_t >= 201811L
513
514
515
516
517
518
519
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
545 return _version == ip_version::V4 ? _ipv.ipv4.reverse_pointer() : _ipv.ipv6.reverse_pointer();
549
550
551
552
553
554
556 const auto tmp = *
this;
561#ifdef IPADDRESS_NONTYPE_TEMPLATE_PARAMETER
564
565
566
567
568
569
570
591#if IPADDRESS_CPP_VERSION
>= 17
594
595
596
597
598
599
600
601
602
603
604
610
611
612
613
614
615
616
617
618
619
620
625#if __cpp_char8_t >= 201811L
628
629
630
631
632
633
634
635
636
637
645
646
647
648
649
650
651
652
653
654
655
661
662
663
664
665
666
667
668
669
670
671
677
678
679
680
681
682
683
684
685
686
692
693
694
695
696
697
698
699
700
701
706#if __cpp_char8_t >= 201811L
709
710
711
712
713
714
715
716
717
725
726
727
728
729
730
731
732
733
734
740
741
742
743
744
745
746
747
748
749
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
808#if __cpp_char8_t >= 201811L
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
940#if __cpp_char8_t >= 201811L
943
944
945
946
947
948
949
950
951
952
953
954
955
956
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1020
1021
1022
1023
1024
1025
1027 return internal::ip_any_parser<ip_address>::parse(address);
1031
1032
1033
1034
1035
1036
1038 return internal::ip_any_parser<ip_address>::parse(address);
1042
1043
1044
1045
1046
1047
1049 return internal::ip_any_parser<ip_address>::parse(address);
1053
1054
1055
1056
1057
1058
1060 return internal::ip_any_parser<ip_address>::parse(address);
1064
1065
1066
1067
1068
1069
1071 return internal::ip_any_parser<ip_address>::parse(address, code);
1075
1076
1077
1078
1079
1080
1082 return internal::ip_any_parser<ip_address>::parse(address, code);
1086
1087
1088
1089
1090
1091
1093 return internal::ip_any_parser<ip_address>::parse(address, code);
1097
1098
1099
1100
1101
1102
1104 return internal::ip_any_parser<ip_address>::parse(address, code);
1108
1109
1110
1111
1112
1115 _ipv.ipv6.set_scope_id(scope_id);
1120
1121
1122
1123
1124
1127 _ipv.ipv6.set_scope_id(scope_id);
1132
1133
1134
1135
1136
1139 _ipv.ipv6.set_scope_id(scope_id);
1144
1145
1146
1147
1148
1151 _ipv.ipv6.set_scope_id(scope_id);
1156
1157
1158
1159
1160
1163 _ipv.ipv6.set_scope_id(scope_id, code);
1168
1169
1170
1171
1172
1175 _ipv.ipv6.set_scope_id(scope_id, code);
1180
1181
1182
1183
1184
1187 _ipv.ipv6.set_scope_id(scope_id, code);
1192
1193
1194
1195
1196
1199 _ipv.ipv6.set_scope_id(scope_id, code);
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216 template <
typename T, size_t N>
1218 return internal::ip_any_parser<ip_address>::parse(address);
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233 template <
typename T, size_t N>
1235 return internal::ip_any_parser<ip_address>::parse(address, code);
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250 template <
typename T, size_t N>
1253 _ipv.ipv6.set_scope_id(scope_id);
1258
1259
1260
1261
1262
1263
1264
1265
1268 return fixed_string<IPADDRESS_IPV6_SCOPE_MAX_LENGTH>();
1270 return _ipv.ipv6.get_scope_id();
1274
1275
1276
1277
1278
1279
1280
1281
1287
1288
1289
1290
1291
1292
1293
1294
1296 return to_uint128();
1300
1301
1302
1303
1304
1305
1306
1307 template <
typename T>
1309 return internal::string_converter<T>::convert(
to_string());
1313
1314
1315
1316
1317
1318
1319
1321 if (_version != rhs._version) {
1324 return _version == ip_version::V4 ? (_ipv.ipv4 == rhs._ipv.ipv4) : (_ipv.ipv6 == rhs._ipv.ipv6);
1328
1329
1330
1331
1332
1333
1334
1336 return !(*
this == rhs);
1339#ifdef IPADDRESS_HAS_SPACESHIP_OPERATOR
1342
1343
1344
1345
1346
1347
1348
1360
1361
1362
1363
1364
1365
1366
1368 if (_version < rhs._version) {
1371 if (_version > rhs._version) {
1374 return _version == ip_version::V4 ? (_ipv.ipv4 < rhs._ipv.ipv4) : (_ipv.ipv6 < rhs._ipv.ipv6);
1378
1379
1380
1381
1382
1383
1384
1390
1391
1392
1393
1394
1395
1396
1398 return !(rhs
< *
this);
1402
1403
1404
1405
1406
1407
1408
1410 return !(*
this < rhs);
1416 union ip_any_address {
1438#ifdef IPADDRESS_NONTYPE_TEMPLATE_PARAMETER
1441
1442
1443
1444
1445
1446 IPADDRESS_EXPORT
template <fixed_string FixedString>
1447 IPADDRESS_NODISCARD IPADDRESS_CONSTEVAL IPADDRESS_FORCE_INLINE ip_address operator
""_ip() IPADDRESS_NOEXCEPT {
1448 return ip_address::parse<FixedString>();
1454
1455
1456
1457
1458
1459
1461 if (size > ipv6_address::base_max_string_len) {
1462 raise_error(error_code::string_is_too_long, 0, address, size);
1464 char str[ipv6_address::base_max_string_len + 1] = {};
1465 for (size_t i = 0; i < size && i < ipv6_address::base_max_string_len; ++i) {
1466 str[i] = address[i];
1468 return ip_address::parse(str);
1472
1473
1474
1475
1476
1477
1479 if (size > ipv6_address::base_max_string_len) {
1480 raise_error(error_code::string_is_too_long, 0, address, size);
1482 wchar_t str[ipv6_address::base_max_string_len + 1] = {};
1483 for (size_t i = 0; i < size && i < ipv6_address::base_max_string_len; ++i) {
1484 str[i] = address[i];
1486 return ip_address::parse(str);
1490
1491
1492
1493
1494
1495
1497 if (size > ipv6_address::base_max_string_len) {
1498 raise_error(error_code::string_is_too_long, 0, address, size);
1500 char16_t str[ipv6_address::base_max_string_len + 1] = {};
1501 for (size_t i = 0; i < size && i < ipv6_address::base_max_string_len; ++i) {
1502 str[i] = address[i];
1504 return ip_address::parse(str);
1508
1509
1510
1511
1512
1513
1515 if (size > ipv6_address::base_max_string_len) {
1516 raise_error(error_code::string_is_too_long, 0, address, size);
1518 char32_t str[ipv6_address::base_max_string_len + 1] = {};
1519 for (size_t i = 0; i < size && i < ipv6_address::base_max_string_len; ++i) {
1520 str[i] = address[i];
1522 return ip_address::parse(str);
1529#ifndef IPADDRESS_NO_OVERLOAD_STD
1560 if (stream.flags() & ios_base::uppercase) {
1561 auto end = std::find(str.cbegin(), str.cend(),
'%');
1562 std::transform(str.cbegin(), end, str.begin(), [](
char c){
1563 return std::toupper(c);
1571 std::basic_string<T, std::char_traits<T>, std::allocator<T>> str;
1576 stream.setstate(std::ios_base::failbit);
A class that represents an IP address, supporting both IPv4 and IPv6 formats.
Definition ip-any-address.hpp:73
constexpr inline bool operator>=(const ip_address &rhs) const noexcept
Greater than or equal to comparison operator.
Definition ip-any-address.hpp:1409
constexpr inline bool is_v4() const noexcept
Checks if the IP address is an IPv4 address.
Definition ip-any-address.hpp:189
constexpr inline bool operator<(const ip_address &rhs) const noexcept
Less than comparison operator.
Definition ip-any-address.hpp:1367
inline std::u16string to_u16string(format fmt=format::compressed) const
Converts the IP address to a string.
Definition ip-any-address.hpp:494
constexpr inline const uint8_t * data() const noexcept
Retrieves the raw data of the IP address in network byte order (big-endian).
Definition ip-any-address.hpp:318
constexpr inline bool is_site_local() const noexcept
Checks if the IPv6 address is a site-local address.
Definition ip-any-address.hpp:180
static constexpr inline ip_address from_uint(uint_type_ipv4 ip) noexcept
Static factory method to create an ip_address from a uint_type_ipv4.
Definition ip-any-address.hpp:421
static constexpr inline ip_address from_bytes(const base_type_ipv4 &bytes) noexcept
Static factory method to create an ip_address from byte array for IPv4.
Definition ip-any-address.hpp:376
constexpr inline bool operator==(const ip_address &rhs) const noexcept
Equality comparison operator.
Definition ip-any-address.hpp:1320
inline std::string reverse_pointer() const
Generates a reverse DNS lookup pointer for the IP address.
Definition ip-any-address.hpp:544
constexpr inline bool is_multicast() const noexcept
Checks if the IP address is a multicast address.
Definition ip-any-address.hpp:97
constexpr inline bool operator>(const ip_address &rhs) const noexcept
Greater than comparison operator.
Definition ip-any-address.hpp:1385
static constexpr inline ip_address parse(const T(&address)[N])
Parses an IP address from a character array.
Definition ip-any-address.hpp:1217
constexpr inline void set_scope_id(const T(&scope_id)[N]) noexcept(noexcept(ipv6_address().set_scope_id(scope_id)))
Sets the scope identifier of the IPv6 address.
Definition ip-any-address.hpp:1251
constexpr inline bool is_private() const noexcept
Checks if the IP address is a private address.
Definition ip-any-address.hpp:108
constexpr inline operator uint32_t() const noexcept
Converts the IP address to a uint32_t.
Definition ip-any-address.hpp:1282
constexpr inline bool is_reserved() const noexcept
Checks if the IP address is a reserved address.
Definition ip-any-address.hpp:128
static constexpr inline ip_address from_uint(const uint_type_ipv6 &ip) noexcept
Static factory method to create an ip_address from a uint_type_ipv6.
Definition ip-any-address.hpp:432
static constexpr inline ip_address from_bytes(const uint8_t *bytes, size_t byte_count, ip_version version) noexcept
Static factory method to create an ip_address from raw bytes.
Definition ip-any-address.hpp:410
constexpr inline scope get_scope_id() const noexcept
Retrieves the scope identifier of the IPv6 address.
Definition ip-any-address.hpp:1266
constexpr inline bool is_link_local() const noexcept
Checks if the IP address is link-local.
Definition ip-any-address.hpp:148
constexpr inline void swap(ip_address &ip) noexcept
Swaps the contents of this ip_address with another ip_address.
Definition ip-any-address.hpp:555
constexpr inline uint32_t to_uint32() const noexcept
Converts the IP address to a uint32_t.
Definition ip-any-address.hpp:445
inline operator std::basic_string< T, std::char_traits< T >, std::allocator< T > >() const
Converts the IP address to a string.
Definition ip-any-address.hpp:1308
constexpr inline ip_address(const base_type_ipv4 &bytes) noexcept
Constructor from byte array for IPv4.
Definition ip-any-address.hpp:357
constexpr inline uint128_t to_uint128() const noexcept
Converts the IP address to a uint128_t.
Definition ip-any-address.hpp:458
constexpr inline ip_address(const ipv4_address &ipv4) noexcept
Constructor from an ipv4_address.
Definition ip-any-address.hpp:337
constexpr inline ip_version version() const noexcept
Retrieves the version of the IP address.
Definition ip-any-address.hpp:86
constexpr inline bool is_unspecified() const noexcept
Checks if the IP address is unspecified.
Definition ip-any-address.hpp:163
constexpr inline optional< ipv4_address > v4() const noexcept
Retrieves the IPv4 address.
Definition ip-any-address.hpp:289
constexpr inline ipv6_address ipv6_mapped() const noexcept
Obtains the IPv6-mapped representation of the current IP address.
Definition ip-any-address.hpp:249
constexpr inline bool is_global() const noexcept
Checks if the IP address is a global address.
Definition ip-any-address.hpp:119
constexpr inline operator uint128_t() const noexcept
Converts the IP address to a uint128_t.
Definition ip-any-address.hpp:1295
constexpr inline ip_address() noexcept
Default constructor.
Definition ip-any-address.hpp:327
constexpr inline optional< ipv6_address > v6() const noexcept
Retrieves the IPv6 address.
Definition ip-any-address.hpp:303
constexpr inline bool operator<=(const ip_address &rhs) const noexcept
Less than or equal to comparison operator.
Definition ip-any-address.hpp:1397
constexpr inline size_t hash() const noexcept
Computes a hash value for the IP address.
Definition ip-any-address.hpp:221
constexpr inline ip_address(const ipv6_address &ipv6) noexcept
Constructor from an ipv6_address.
Definition ip-any-address.hpp:347
constexpr inline optional< std::pair< ipv4_address, ipv4_address > > teredo() const noexcept
Determines if the IPv6 address is a Teredo address.
Definition ip-any-address.hpp:278
constexpr inline optional< ipv4_address > ipv4_mapped() const noexcept
Determines if the IPv6 address is an IPv4-mapped address.
Definition ip-any-address.hpp:233
constexpr inline bool operator!=(const ip_address &rhs) const noexcept
Inequality comparison operator.
Definition ip-any-address.hpp:1335
inline std::wstring to_wstring(format fmt=format::compressed) const
Converts the IP address to a string.
Definition ip-any-address.hpp:482
constexpr inline optional< ipv4_address > sixtofour() const noexcept
Determines if the IPv6 address is a 6to4 address.
Definition ip-any-address.hpp:262
constexpr inline size_t size() const noexcept
Retrieves the size of the IP address.
Definition ip-any-address.hpp:209
inline std::u32string to_u32string(format fmt=format::compressed) const
Converts the IP address to a string.
Definition ip-any-address.hpp:506
static constexpr inline ip_address parse(const T(&address)[N], error_code &code) noexcept
Parses an IP address from a character array and reports errors through an error code.
Definition ip-any-address.hpp:1234
constexpr inline bool is_loopback() const noexcept
Checks if the IP address is a loopback address.
Definition ip-any-address.hpp:139
inline std::string to_string(format fmt=format::compressed) const
Converts the IP address to a string.
Definition ip-any-address.hpp:470
constexpr inline bool is_v6() const noexcept
Checks if the IP address is an IPv6 address.
Definition ip-any-address.hpp:198
A template class to manage an optional contained value.
Definition optional.hpp:35
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
@ no_error
Indicates the absence of any errors.