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
175 return _version == ip_version::V4 ?
false : _ipv.ipv6.is_site_local();
179
180
181
182
188
189
190
191
197
198
199
200
201
202
204 return _version == ip_version::V4 ? _ipv.ipv4.size() : _ipv.ipv6.size();
208
209
210
211
212
213
214
216 return _version == ip_version::V4 ? _ipv.ipv4.hash() : _ipv.ipv6.hash();
220
221
222
223
224
225
226
228 return _version == ip_version::V4 ? optional<ipv4_address>() : _ipv.ipv6.ipv4_mapped();
232
233
234
235
236
237
238
239
241 return _version == ip_version::V4 ? optional<ipv4_address>() : _ipv.ipv6.sixtofour();
245
246
247
248
249
250
251
252
253
254
255
257 return _version == ip_version::V4 ? optional<std::pair<ipv4_address, ipv4_address>>() : _ipv.ipv6.teredo();
261
262
263
264
265
266
269 return optional<ipv4_address>();
271 return optional<ipv4_address>(_ipv.ipv4);
275
276
277
278
279
280
283 return optional<ipv6_address>();
285 return optional<ipv6_address>(_ipv.ipv6);
289
290
291
292
293
294
295
297 return _version == ip_version::V4 ? _ipv.ipv4.data() : _ipv.ipv6.data();
301
302
303
304
309
310
311
312
313
314
319
320
321
322
323
324
329
330
331
332
333
334
339
340
341
342
343
344
349
350
351
352
353
355 return ip_address(bytes);
359
360
361
362
363
365 return ip_address(bytes);
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
389 return version == ip_version::V4 ? ip_address(ipv4_address::from_bytes(bytes, byte_count)) : ip_address(ipv6_address::from_bytes(bytes, byte_count));
393
394
395
396
397
398
400 return ip_address(ipv4_address::from_uint(ip));
404
405
406
407
408
409
411 return ip_address(ipv6_address::from_uint(ip));
415
416
417
418
419
420
421
422
424 return _version == ip_version::V4 ? _ipv.ipv4.to_uint() : uint32_t(_ipv.ipv6.to_uint());
428
429
430
431
432
433
434
435
437 return _version == ip_version::V4 ? uint128_t(_ipv.ipv4.to_uint()) : _ipv.ipv6.to_uint();
441
442
443
444
445
446
447
449 return _version == ip_version::V4 ? _ipv.ipv4.to_string(fmt) : _ipv.ipv6.to_string(fmt);
453
454
455
456
457
458
459
461 return _version == ip_version::V4 ? _ipv.ipv4.to_wstring(fmt) : _ipv.ipv6.to_wstring(fmt);
465
466
467
468
469
470
471
473 return _version == ip_version::V4 ? _ipv.ipv4.to_u16string(fmt) : _ipv.ipv6.to_u16string(fmt);
477
478
479
480
481
482
483
485 return _version == ip_version::V4 ? _ipv.ipv4.to_u32string(fmt) : _ipv.ipv6.to_u32string(fmt);
488#if __cpp_char8_t >= 201811L
491
492
493
494
495
496
497
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
523 return _version == ip_version::V4 ? _ipv.ipv4.reverse_pointer() : _ipv.ipv6.reverse_pointer();
527
528
529
530
531
532
534 const auto tmp = *
this;
539#ifdef IPADDRESS_NONTYPE_TEMPLATE_PARAMETER
542
543
544
545
546
547
548
569#if IPADDRESS_CPP_VERSION
>= 17
572
573
574
575
576
577
578
579
580
581
582
588
589
590
591
592
593
594
595
596
597
598
603#if __cpp_char8_t >= 201811L
606
607
608
609
610
611
612
613
614
615
623
624
625
626
627
628
629
630
631
632
633
639
640
641
642
643
644
645
646
647
648
649
655
656
657
658
659
660
661
662
663
664
670
671
672
673
674
675
676
677
678
679
684#if __cpp_char8_t >= 201811L
687
688
689
690
691
692
693
694
695
703
704
705
706
707
708
709
710
711
712
718
719
720
721
722
723
724
725
726
727
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
776#if __cpp_char8_t >= 201811L
779
780
781
782
783
784
785
786
787
788
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
842
843
844
845
846
847
848
849
850
851
852
853
854
855
863
864
865
866
867
868
869
870
871
872
873
874
875
876
883#if __cpp_char8_t >= 201811L
886
887
888
889
890
891
892
893
894
904
905
906
907
908
909
910
911
912
913
914
915
916
917
925
926
927
928
929
930
931
932
933
934
935
936
937
938
948
949
950
951
952
953
955 return internal::ip_any_parser<ip_address>::parse(address);
959
960
961
962
963
964
966 return internal::ip_any_parser<ip_address>::parse(address);
970
971
972
973
974
975
977 return internal::ip_any_parser<ip_address>::parse(address);
981
982
983
984
985
986
988 return internal::ip_any_parser<ip_address>::parse(address);
992
993
994
995
996
997
999 return internal::ip_any_parser<ip_address>::parse(address, code);
1003
1004
1005
1006
1007
1008
1010 return internal::ip_any_parser<ip_address>::parse(address, code);
1014
1015
1016
1017
1018
1019
1021 return internal::ip_any_parser<ip_address>::parse(address, code);
1025
1026
1027
1028
1029
1030
1032 return internal::ip_any_parser<ip_address>::parse(address, code);
1036
1037
1038
1039
1040
1043 _ipv.ipv6.set_scope_id(scope_id);
1048
1049
1050
1051
1052
1055 _ipv.ipv6.set_scope_id(scope_id);
1060
1061
1062
1063
1064
1067 _ipv.ipv6.set_scope_id(scope_id);
1072
1073
1074
1075
1076
1079 _ipv.ipv6.set_scope_id(scope_id);
1084
1085
1086
1087
1088
1091 _ipv.ipv6.set_scope_id(scope_id, code);
1096
1097
1098
1099
1100
1103 _ipv.ipv6.set_scope_id(scope_id, code);
1108
1109
1110
1111
1112
1115 _ipv.ipv6.set_scope_id(scope_id, code);
1120
1121
1122
1123
1124
1127 _ipv.ipv6.set_scope_id(scope_id, code);
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144 template <
typename T, size_t N>
1146 return internal::ip_any_parser<ip_address>::parse(address);
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161 template <
typename T, size_t N>
1163 return internal::ip_any_parser<ip_address>::parse(address, code);
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177 template <
typename T, size_t N>
1180 _ipv.ipv6.set_scope_id(scope_id);
1185
1186
1187
1188
1189
1190
1191
1192
1195 return fixed_string<IPADDRESS_IPV6_SCOPE_MAX_LENGTH>();
1197 return _ipv.ipv6.get_scope_id();
1201
1202
1203
1204
1205
1206
1207
1208
1214
1215
1216
1217
1218
1219
1220
1221
1223 return to_uint128();
1227
1228
1229
1230
1231
1232
1233
1234 template <
typename T>
1236 return internal::string_converter<T>::convert(
to_string());
1240
1241
1242
1243
1244
1245
1246
1248 if (_version != rhs._version) {
1251 return _version == ip_version::V4 ? (_ipv.ipv4 == rhs._ipv.ipv4) : (_ipv.ipv6 == rhs._ipv.ipv6);
1255
1256
1257
1258
1259
1260
1261
1263 return !(*
this == rhs);
1266#ifdef IPADDRESS_HAS_SPACESHIP_OPERATOR
1269
1270
1271
1272
1273
1274
1275
1287
1288
1289
1290
1291
1292
1293
1295 if (_version < rhs._version) {
1298 if (_version > rhs._version) {
1301 return _version == ip_version::V4 ? (_ipv.ipv4 < rhs._ipv.ipv4) : (_ipv.ipv6 < rhs._ipv.ipv6);
1305
1306
1307
1308
1309
1310
1311
1317
1318
1319
1320
1321
1322
1323
1325 return !(rhs
< *
this);
1329
1330
1331
1332
1333
1334
1335
1337 return !(*
this < rhs);
1343 union ip_any_address {
1365#ifdef IPADDRESS_NONTYPE_TEMPLATE_PARAMETER
1368
1369
1370
1371
1372
1373 IPADDRESS_EXPORT
template <fixed_string FixedString>
1374 IPADDRESS_NODISCARD IPADDRESS_CONSTEVAL IPADDRESS_FORCE_INLINE ip_address operator
""_ip() IPADDRESS_NOEXCEPT {
1375 return ip_address::parse<FixedString>();
1381
1382
1383
1384
1385
1386
1388 if (size > ipv6_address::base_max_string_len) {
1389 raise_error(error_code::string_is_too_long, 0, address, size);
1391 char str[ipv6_address::base_max_string_len + 1] = {};
1392 for (size_t i = 0; i < size && i < ipv6_address::base_max_string_len; ++i) {
1393 str[i] = address[i];
1395 return ip_address::parse(str);
1399
1400
1401
1402
1403
1404
1406 if (size > ipv6_address::base_max_string_len) {
1407 raise_error(error_code::string_is_too_long, 0, address, size);
1409 wchar_t str[ipv6_address::base_max_string_len + 1] = {};
1410 for (size_t i = 0; i < size && i < ipv6_address::base_max_string_len; ++i) {
1411 str[i] = address[i];
1413 return ip_address::parse(str);
1417
1418
1419
1420
1421
1422
1424 if (size > ipv6_address::base_max_string_len) {
1425 raise_error(error_code::string_is_too_long, 0, address, size);
1427 char16_t str[ipv6_address::base_max_string_len + 1] = {};
1428 for (size_t i = 0; i < size && i < ipv6_address::base_max_string_len; ++i) {
1429 str[i] = address[i];
1431 return ip_address::parse(str);
1435
1436
1437
1438
1439
1440
1442 if (size > ipv6_address::base_max_string_len) {
1443 raise_error(error_code::string_is_too_long, 0, address, size);
1445 char32_t str[ipv6_address::base_max_string_len + 1] = {};
1446 for (size_t i = 0; i < size && i < ipv6_address::base_max_string_len; ++i) {
1447 str[i] = address[i];
1449 return ip_address::parse(str);
1456#ifndef IPADDRESS_NO_OVERLOAD_STD
1487 if (stream.flags() & ios_base::uppercase) {
1488 auto end = std::find(str.cbegin(), str.cend(),
'%');
1489 std::transform(str.cbegin(), end, str.begin(), [](
char c){
1490 return std::toupper(c);
1498 std::basic_string<T, std::char_traits<T>, std::allocator<T>> str;
1503 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:1336
constexpr inline bool is_v4() const noexcept
Checks if the IP address is an IPv4 address.
Definition ip-any-address.hpp:183
constexpr inline bool operator<(const ip_address &rhs) const noexcept
Less than comparison operator.
Definition ip-any-address.hpp:1294
inline std::u16string to_u16string(format fmt=format::compressed) const
Converts the IP address to a string.
Definition ip-any-address.hpp:472
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:296
constexpr inline bool is_site_local() const noexcept
Checks if the IPv6 address is a site-local address.
Definition ip-any-address.hpp:174
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:399
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:354
constexpr inline bool operator==(const ip_address &rhs) const noexcept
Equality comparison operator.
Definition ip-any-address.hpp:1247
inline std::string reverse_pointer() const
Generates a reverse DNS lookup pointer for the IP address.
Definition ip-any-address.hpp:522
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:1312
static constexpr inline ip_address parse(const T(&address)[N])
Parses an IP address from a character array.
Definition ip-any-address.hpp:1145
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:1178
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:1209
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:410
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:388
constexpr inline scope get_scope_id() const noexcept
Retrieves the scope identifier of the IPv6 address.
Definition ip-any-address.hpp:1193
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:533
constexpr inline uint32_t to_uint32() const noexcept
Converts the IP address to a uint32_t.
Definition ip-any-address.hpp:423
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:1235
constexpr inline ip_address(const base_type_ipv4 &bytes) noexcept
Constructor from byte array for IPv4.
Definition ip-any-address.hpp:335
constexpr inline uint128_t to_uint128() const noexcept
Converts the IP address to a uint128_t.
Definition ip-any-address.hpp:436
constexpr inline ip_address(const ipv4_address &ipv4) noexcept
Constructor from an ipv4_address.
Definition ip-any-address.hpp:315
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:267
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:1222
constexpr inline ip_address() noexcept
Default constructor.
Definition ip-any-address.hpp:305
constexpr inline optional< ipv6_address > v6() const noexcept
Retrieves the IPv6 address.
Definition ip-any-address.hpp:281
constexpr inline bool operator<=(const ip_address &rhs) const noexcept
Less than or equal to comparison operator.
Definition ip-any-address.hpp:1324
constexpr inline size_t hash() const noexcept
Computes a hash value for the IP address.
Definition ip-any-address.hpp:215
constexpr inline ip_address(const ipv6_address &ipv6) noexcept
Constructor from an ipv6_address.
Definition ip-any-address.hpp:325
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:256
constexpr inline optional< ipv4_address > ipv4_mapped() const noexcept
Determines if the IPv6 address is an IPv4-mapped address.
Definition ip-any-address.hpp:227
constexpr inline bool operator!=(const ip_address &rhs) const noexcept
Inequality comparison operator.
Definition ip-any-address.hpp:1262
inline std::wstring to_wstring(format fmt=format::compressed) const
Converts the IP address to a string.
Definition ip-any-address.hpp:460
constexpr inline optional< ipv4_address > sixtofour() const noexcept
Determines if the IPv6 address is a 6to4 address.
Definition ip-any-address.hpp:240
constexpr inline size_t size() const noexcept
Retrieves the size of the IP address.
Definition ip-any-address.hpp:203
inline std::u32string to_u32string(format fmt=format::compressed) const
Converts the IP address to a string.
Definition ip-any-address.hpp:484
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:1162
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:448
constexpr inline bool is_v6() const noexcept
Checks if the IP address is an IPv6 address.
Definition ip-any-address.hpp:192
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: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
@ no_error
Indicates the absence of any errors.