-
Notifications
You must be signed in to change notification settings - Fork 739
Expand file tree
/
Copy pathIpAddress.h
More file actions
934 lines (791 loc) · 35.7 KB
/
IpAddress.h
File metadata and controls
934 lines (791 loc) · 35.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
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
495
496
497
498
499
500
501
502
503
504
505
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
532
533
534
535
536
537
538
539
540
541
542
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
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
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
#pragma once
#include <cstdint>
#include <cstring>
#include <string>
#include <algorithm>
#include <ostream>
#include <array>
#include <memory>
#include "DeprecationUtils.h"
/// @file
/// @namespace pcpp
/// @brief The main namespace for the PcapPlusPlus lib
namespace pcpp
{
// forward declarations
class IPv4Network;
class IPv6Network;
// The implementation of the classes is based on document N4771 "Working Draft, C++ Extensions for Networking"
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/n4771.pdf
/// @class IPv4Address
/// Represents an IPv4 address (of type XXX.XXX.XXX.XXX)
class IPv4Address
{
public:
/// A default constructor that creates an instance of the class with the zero-initialized address
IPv4Address() = default;
/// A constructor that creates an instance of the class out of 4-byte integer value.
/// @param[in] addrAsInt The address as 4-byte integer in network byte order
IPv4Address(const uint32_t addrAsInt)
{
memcpy(m_Bytes.data(), &addrAsInt, sizeof(addrAsInt));
}
/// A constructor that creates an instance of the class out of 4-byte array.
/// @param[in] bytes The address as 4-byte array in network byte order
/// @remarks This constructor assumes that the provided array is exactly 4 bytes long.
/// Prefer using the constructor with size parameter if the array length is not guaranteed to be 4 bytes.
IPv4Address(const uint8_t bytes[4]) : IPv4Address(bytes, 4)
{}
/// A constructor that creates an instance of the class out of a 4-byte array.
/// @param[in] bytes The address as 4-byte array in network byte order
/// @param[in] size The size of the array in bytes
/// @throws std::invalid_argument If the provided bytes pointer is null.
/// @throws std::out_of_range If the provided size is smaller than 4 bytes.
IPv4Address(const uint8_t* bytes, size_t size);
/// A constructor that creates an instance of the class out of a 4-byte standard array.
/// @param[in] bytes The address as 4-byte standard array in network byte order
IPv4Address(const std::array<uint8_t, 4>& bytes) : m_Bytes(bytes)
{}
/// A constructor that creates an instance of the class out of std::string value.
/// @param[in] addrAsString The std::string representation of the address
/// @throws std::invalid_argument The provided string does not represent a valid IPv4 address.
IPv4Address(const std::string& addrAsString);
/// @return A 4-byte integer in network byte order representing the IPv4 address
inline uint32_t toInt() const;
/// @return A non-owning pointer to 4-byte C-style array representing the IPv4 address
const uint8_t* toBytes() const
{
return m_Bytes.data();
}
/// @return A reference to a 4-byte standard array representing the IPv4 address
const std::array<uint8_t, 4>& toByteArray() const
{
return m_Bytes;
}
/// @return A string representation of the address
std::string toString() const;
/// @return True if an address is multicast, false otherwise.
bool isMulticast() const;
/// Overload of the equal-to operator
/// @param[in] rhs The object to compare with
/// @return True if the addresses are equal, false otherwise
bool operator==(const IPv4Address& rhs) const
{
return toInt() == rhs.toInt();
}
/// Overload of the less-than operator
/// @param[in] rhs The object to compare with
/// @return True if the address value is lower than the other address value, false otherwise
bool operator<(const IPv4Address& rhs) const
{
uint32_t intVal = toInt();
std::reverse(reinterpret_cast<uint8_t*>(&intVal), reinterpret_cast<uint8_t*>(&intVal) + sizeof(intVal));
uint32_t rhsIntVal = rhs.toInt();
std::reverse(reinterpret_cast<uint8_t*>(&rhsIntVal),
reinterpret_cast<uint8_t*>(&rhsIntVal) + sizeof(rhsIntVal));
return intVal < rhsIntVal;
}
/// Overload of the not-equal-to operator
/// @param[in] rhs The object to compare with
/// @return True if the addresses are not equal, false otherwise
bool operator!=(const IPv4Address& rhs) const
{
return !(*this == rhs);
}
/// Checks whether the address matches a network.
/// @param network An IPv4Network network
/// @return True if the address matches the network or false otherwise
bool matchNetwork(const IPv4Network& network) const;
/// Checks whether the address matches a network.
/// For example: this method will return true for address 10.1.1.9 and network which is one of:
/// 10.1.1.1/24, 10.1.1.1/255.255.255.0
/// Another example: this method will return false for address 11.1.1.9 and network which is one of:
/// 10.1.1.1/16, 10.1.1.1/255.255.0.0
/// @param[in] network A string in one of these formats:
/// - X.X.X.X/Y where X.X.X.X is a valid IP address and Y is a number between 0 and 32
/// - X.X.X.X/Y.Y.Y.Y where X.X.X.X is a valid IP address and Y.Y.Y.Y is a valid netmask
/// @return True if the address matches the network or false if it doesn't or if the network is invalid
bool matchNetwork(const std::string& network) const;
/// A static method that checks whether a string represents a valid IPv4 address
/// @param[in] addrAsString The std::string representation of the address
/// @return True if the address is valid, false otherwise
static bool isValidIPv4Address(const std::string& addrAsString);
/// A static value representing a zero value of IPv4 address, meaning address of value "0.0.0.0".
static const IPv4Address Zero;
/// A static values representing the lower and upper bound of IPv4 multicast ranges. The bounds are inclusive.
/// MulticastRangeLowerBound is initialized to "224.0.0.0".
/// MulticastRangeUpperBound is initialized to "239.255.255.255".
/// In order to check whether the address is a multicast address the isMulticast method can be used.
static const IPv4Address MulticastRangeLowerBound;
static const IPv4Address MulticastRangeUpperBound;
private:
std::array<uint8_t, 4> m_Bytes = { 0 };
}; // class IPv4Address
// Implementation of inline methods
uint32_t IPv4Address::toInt() const
{
uint32_t addr = 0;
memcpy(&addr, m_Bytes.data(), m_Bytes.size() * sizeof(uint8_t));
return addr;
}
/// @class IPv6Address
/// Represents an IPv6 address (of type xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx).
class IPv6Address
{
public:
/// A default constructor that creates an instance of the class with the zero-initialized address.
IPv6Address() = default;
/// A constructor that creates an instance of the class out of 16-byte array.
/// @param[in] bytes The address as 16-byte array in network byte order
/// @remarks This constructor assumes that the provided array is exactly 16 bytes long.
/// Prefer using the constructor with size parameter if the array length is not guaranteed to be 16 bytes.
IPv6Address(const uint8_t bytes[16]) : IPv6Address(bytes, 16)
{}
/// @brief A constructor that creates an instance of the class out of a 16-byte array.
/// @param bytes The address as 16-byte array in network byte order
/// @param size The size of the array in bytes
/// @throws std::invalid_argument If the provided buffer is null.
/// @throws std::out_of_range If the provided size is smaller than 16 bytes.
IPv6Address(const uint8_t* bytes, size_t size);
/// A constructor that creates an instance of the class out of a 16-byte standard array.
/// @param[in] bytes The address as 16-byte standard array in network byte order
IPv6Address(const std::array<uint8_t, 16>& bytes) : m_Bytes(bytes)
{}
/// A constructor that creates an instance of the class out of std::string value.
/// @param[in] addrAsString The std::string representation of the address
/// @throws std::invalid_argument The provided string does not represent a valid IPv6 address.
IPv6Address(const std::string& addrAsString);
/// Returns a view of the IPv6 address as a 16-byte raw C-style array
/// @return A non-owning pointer to 16-byte array representing the IPv6 address
const uint8_t* toBytes() const
{
return m_Bytes.data();
}
/// Returns a view of the IPv6 address as a std::array of bytes
/// @return A reference to a 16-byte standard array representing the IPv6 address
const std::array<uint8_t, 16>& toByteArray() const
{
return m_Bytes;
}
/// Returns a std::string representation of the address
/// @return A string representation of the address
std::string toString() const;
/// Determine whether the address is a multicast address
/// @return True if an address is multicast
bool isMulticast() const;
/// Overload of the equal-to operator
/// @param[in] rhs The object to compare with
/// @return True if the addresses are equal, false otherwise
bool operator==(const IPv6Address& rhs) const
{
return memcmp(toBytes(), rhs.toBytes(), sizeof(m_Bytes)) == 0;
}
/// Overload of the less-than operator
/// @param[in] rhs The object to compare with
/// @return True if the address value is lower than the other address value, false otherwise
bool operator<(const IPv6Address& rhs) const
{
return memcmp(toBytes(), rhs.toBytes(), sizeof(m_Bytes)) < 0;
}
/// Overload of the not-equal-to operator
/// @param[in] rhs The object to compare with
/// @return True if the addresses are not equal, false otherwise
bool operator!=(const IPv6Address& rhs) const
{
return !(*this == rhs);
}
/// Allocates a byte array and copies address value into it. Array deallocation is user responsibility
/// @param[in] arr A pointer to where array will be allocated
/// @param[out] length Returns the length in bytes of the array that was allocated
/// @throws std::invalid_argument If the provided pointer is null.
/// @deprecated Use copyToNewBuffer instead.
PCPP_DEPRECATED("Use copyToNewBuffer instead.")
void copyTo(uint8_t** arr, size_t& length) const;
/// Gets a pointer to an already allocated byte array and copies the address value to it.
/// This method assumes array allocated size is at least 16 (the size of an IPv6 address)
/// @param[in] arr A pointer to the array which address will be copied to
/// @remarks This method assumes that the provided array is at least 16 bytes long.
/// Prefer using the copyTo(uint8_t* buffer, size_t size) method if the array length is not guaranteed to be 16
/// bytes.
void copyTo(uint8_t* arr) const
{
copyTo(arr, 16);
}
/// @brief Copies the address value to a user-provided buffer.
///
/// This function supports querying. If the buffer is null and size is zero, it returns the required size.
///
/// @param[in] buffer A pointer to the buffer where the address will be copied
/// @param[in] size The size of the buffer in bytes
/// @return The number of bytes copied to the buffer or the number of required bytes, which is always 16 for
/// IPv6 addresses.
/// @throws std::invalid_argument If the provided buffer is null and size is not zero.
size_t copyTo(uint8_t* buffer, size_t size) const;
/// @brief Allocates a new buffer and copies the address value to it.
/// The user is responsible for deallocating the buffer.
///
/// @param buffer A pointer to a pointer where the new buffer will be allocated
/// @param size A reference to a size_t variable that will be updated with the size of the allocated buffer
/// @return True if the buffer was successfully allocated and the address was copied, false otherwise.
/// @throws std::invalid_argument If the buffer pointer is null.
bool copyToNewBuffer(uint8_t** buffer, size_t& size) const;
/// Checks whether the address matches a network.
/// @param network An IPv6Network network
/// @return True if the address matches the network or false otherwise
bool matchNetwork(const IPv6Network& network) const;
/// Checks whether the address matches a network.
/// For example: this method will return true for address d6e5:83dc:0c58:bc5d:1449:5898:: and network
/// which is one of:
/// d6e5:83dc:0c58:bc5d::/64, d6e5:83dc:0c58:bc5d::/ffff:ffff:ffff:ffff::
/// Another example: this method will return false for address d6e5:83dc:: and network which is one of:
/// d6e5:83dc:0c58:bc5d::/64, d6e5:83dc:0c58:bc5d::/ffff:ffff:ffff:ffff::
/// @param[in] network A string in one of these formats:
/// - IPV6_ADDRESS/Y where IPV6_ADDRESS is a valid IPv6 address and Y is a number between 0 and 128
/// - IPV6_ADDRESS/IPV6_NETMASK where IPV6_ADDRESS is a valid IPv6 address and IPV6_NETMASK is a valid
/// IPv6 netmask
/// @return True if the address matches the network or false if it doesn't or if the network is invalid
bool matchNetwork(const std::string& network) const;
/// A static method that checks whether a string represents a valid IPv6 address
/// @param[in] addrAsString The std::string representation of the address
/// @return True if the address is valid, false otherwise
static bool isValidIPv6Address(const std::string& addrAsString);
/// A static value representing a zero value of IPv6 address, meaning address of value
/// "0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0".
static const IPv6Address Zero;
/// A static value representing the lower bound of IPv6 multicast ranges. The bound is inclusive.
/// MulticastRangeLowerBound is initialized to "ff00:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0".
/// In order to check whether the address is a multicast address the isMulticast method can be used.
static const IPv6Address MulticastRangeLowerBound;
private:
std::array<uint8_t, 16> m_Bytes = { 0 };
}; // class IPv6Address
/// @class IPAddress
/// The class is a version-independent representation for an IP address
class IPAddress
{
public:
/// An enum representing the address type: IPv4 or IPv6
enum AddressType : uint8_t
{
/// IPv4 address type
IPv4AddressType,
/// IPv6 address type
IPv6AddressType
};
/// A default constructor that creates an instance of the class with unspecified IPv4 address
IPAddress() : m_Type(IPv4AddressType)
{}
/// A constructor that creates an instance of the class out of IPv4Address.
/// @param[in] addr A const reference to instance of IPv4Address
IPAddress(const IPv4Address& addr) : m_Type(IPv4AddressType), m_IPv4(addr)
{}
/// A constructor that creates an instance of the class out of IPv6Address.
/// @param[in] addr A const reference to instance of IPv6Address
IPAddress(const IPv6Address& addr) : m_Type(IPv6AddressType), m_IPv6(addr)
{}
/// A constructor that creates an instance of the class out of std::string value
/// @param[in] addrAsString The std::string representation of the address
/// @throws std::invalid_argument The provided string does not represent a valid IPv4 or IPv6 address.
IPAddress(const std::string& addrAsString);
/// Overload of an assignment operator.
/// @param[in] addr A const reference to instance of IPv4Address
/// @return A reference to the assignee
inline IPAddress& operator=(const IPv4Address& addr);
/// Overload of an assignment operator.
/// @param[in] addr A const reference to instance of IPv6Address
/// @return A reference to the assignee
inline IPAddress& operator=(const IPv6Address& addr);
/// Gets the address type: IPv4 or IPv6
/// @return The address type
AddressType getType() const
{
return static_cast<AddressType>(m_Type);
}
/// Returns a std::string representation of the address
/// @return A string representation of the address
std::string toString() const
{
return (getType() == IPv4AddressType) ? m_IPv4.toString() : m_IPv6.toString();
}
/// @return Determine whether the object contains an IP version 4 address
bool isIPv4() const
{
return getType() == IPv4AddressType;
}
/// @return Determine whether the object contains an IP version 6 address
bool isIPv6() const
{
return getType() == IPv6AddressType;
}
/// Determine whether the address is a multicast address
/// @return True if an address is multicast
bool isMulticast() const
{
return (getType() == IPv4AddressType) ? m_IPv4.isMulticast() : m_IPv6.isMulticast();
}
/// Get a reference to IPv4 address instance
/// @return The const reference to IPv4Address instance
const IPv4Address& getIPv4() const
{
return m_IPv4;
}
/// Get a reference to IPv6 address instance
/// @return The const reference to IPv6Address instance
const IPv6Address& getIPv6() const
{
return m_IPv6;
}
/// @return True if the address is zero, false otherwise
bool isZero() const
{
return (getType() == IPv4AddressType) ? m_IPv4 == IPv4Address::Zero : m_IPv6 == IPv6Address::Zero;
}
/// Overload of the equal-to operator
/// @param[in] rhs The object to compare with
/// @return True if the addresses are equal, false otherwise
inline bool operator==(const IPAddress& rhs) const;
/// Overload of the less-than operator
/// @param[in] rhs The object to compare with
/// @return True if the address value is lower than the other address value, false otherwise
inline bool operator<(const IPAddress& rhs) const;
/// Overload of the not-equal-to operator
/// @param[in] rhs The object to compare with
/// @return True if the addresses are not equal, false otherwise
bool operator!=(const IPAddress& rhs) const
{
return !(*this == rhs);
}
private:
uint8_t m_Type;
IPv4Address m_IPv4;
IPv6Address m_IPv6;
};
// implementation of inline methods
bool IPAddress::operator==(const IPAddress& rhs) const
{
if (isIPv4())
{
return rhs.isIPv4() ? (m_IPv4 == rhs.m_IPv4) : false;
}
return rhs.isIPv6() ? m_IPv6 == rhs.m_IPv6 : false;
}
bool IPAddress::operator<(const IPAddress& rhs) const
{
if (isIPv4())
{
// treat IPv4 as less than IPv6
// If current obj is IPv4 and other is IPv6 return true
return rhs.isIPv4() ? (m_IPv4 < rhs.m_IPv4) : true;
}
return rhs.isIPv6() ? m_IPv6 < rhs.m_IPv6 : false;
}
IPAddress& IPAddress::operator=(const IPv4Address& addr)
{
m_Type = IPv4AddressType;
m_IPv4 = addr;
return *this;
}
IPAddress& IPAddress::operator=(const IPv6Address& addr)
{
m_Type = IPv6AddressType;
m_IPv6 = addr;
return *this;
}
/// @class IPv4Network
/// A class representing IPv4 network definition
class IPv4Network
{
public:
/// A constructor that creates an instance of the class out of an address and a full prefix length,
/// essentially making a network of consisting of only 1 address.
/// @param address An address representing the network prefix.
explicit IPv4Network(const IPv4Address& address) : IPv4Network(address, 32U)
{}
/// A constructor that creates an instance of the class out of an address representing the network prefix
/// and a prefix length
/// @param address An address representing the network prefix. If the address is invalid std::invalid_argument
/// exception is thrown
/// @param prefixLen A number between 0 and 32 representing the prefix length.
/// @throws std::invalid_argument Prefix length is out of acceptable range.
IPv4Network(const IPv4Address& address, uint8_t prefixLen);
/// A constructor that creates an instance of the class out of an address representing the network prefix
/// and a netmask
/// @param address An address representing the network prefix. If the address is invalid std::invalid_argument
/// exception is thrown
/// @param netmask A string representing a netmask in the format of X.X.X.X, for example: 255.255.0.0.
/// Please notice that netmasks that start with zeros are invalid, for example: 0.0.255.255. The only netmask
/// starting with zeros that is valid is 0.0.0.0.
/// @throws std::invalid_argument The provided netmask is invalid.
IPv4Network(const IPv4Address& address, const std::string& netmask);
/// A constructor that creates an instance of the class out of a string representing the network prefix and
/// a prefix length or a netmask
/// @param addressAndNetmask A string in one of these formats:
/// - X.X.X.X/Y where X.X.X.X is a valid IPv4 address representing the network prefix and Y is a number between
/// 0 and 32 representing the network prefix
/// - X.X.X.X/Y.Y.Y.Y where X.X.X.X is a valid IPv4 address representing the network prefix and Y.Y.Y.Y is
/// a valid netmask
/// @throws std::invalid_argument The provided string does not represent a valid address and netmask format.
IPv4Network(const std::string& addressAndNetmask);
/// @return The prefix length, for example: the prefix length of 10.10.10.10/255.0.0.0 is 8
uint8_t getPrefixLen() const;
/// @return The netmask, for example: the netmask of 10.10.10.10/8 is 255.0.0.0
std::string getNetmask() const
{
return IPv4Address(m_Mask).toString();
}
/// @return The network prefix, for example: the network prefix of 10.10.10.10/16 is 10.10.0.0
IPv4Address getNetworkPrefix() const
{
return m_NetworkPrefix;
}
/// @return The lowest non-reserved IPv4 address in this network, for example: the lowest address
/// in 10.10.10.10/16 is 10.10.0.1
IPv4Address getLowestAddress() const;
/// @return The highest non-reserved IPv4 address in this network, for example: the highest address
/// in 10.10.10.10/16 is 10.10.255.254
IPv4Address getHighestAddress() const;
/// @return The number of addresses in this network including reserved addresses, for example:
/// the number of addresses in 10.10.0.0/24 is 256
uint64_t getTotalAddressCount() const;
/// @param address An IPv4 address
/// @return True is the address belongs to the network, false otherwise or if the address isn't valid
bool includes(const IPv4Address& address) const;
/// @param network An IPv4 network
/// @return True is the input network is completely included within this network, false otherwise, for example:
/// 10.10.10.10/16 includes 10.10.10.10/24 but doesn't include 10.10.10.10/8
bool includes(const IPv4Network& network) const;
/// @return A string representation of the network in a format of NETWORK_PREFIX/PREFIX_LEN, for example:
/// 192.168.0.0/16
std::string toString() const;
private:
uint32_t m_NetworkPrefix{};
uint32_t m_Mask{};
static bool isValidNetmask(const IPv4Address& netmaskAddress);
void initFromAddressAndPrefixLength(const IPv4Address& address, uint8_t prefixLen);
void initFromAddressAndNetmask(const IPv4Address& address, const IPv4Address& netmaskAddress);
};
/// @class IPv6Network
/// A class representing IPv6 network definition
class IPv6Network
{
public:
/// A constructor that creates an instance of the class out of an address and a full prefix length,
/// essentially making a network of consisting of only 1 address.
/// @param address An address representing the network prefix.
explicit IPv6Network(const IPv6Address& address) : IPv6Network(address, 128U)
{}
/// A constructor that creates an instance of the class out of an address representing the network prefix
/// and a prefix length
/// @param address An address representing the network prefix. If the address is invalid std::invalid_argument
/// exception is thrown
/// @param prefixLen A number between 0 and 128 representing the prefix length.
/// @throws std::invalid_argument Prefix length is out of acceptable range.
IPv6Network(const IPv6Address& address, uint8_t prefixLen);
/// A constructor that creates an instance of the class out of an address representing the network prefix
/// and a netmask
/// @param address An address representing the network prefix. If the address is invalid std::invalid_argument
/// exception is thrown
/// @param netmask A string representing a netmask in valid IPv6 format, for example: ffff:ffff::.
/// Please notice that netmasks that start with zeros are invalid, for example: 0:ffff::. The only netmask
/// starting with zeros that is valid is all zeros (::).
/// @throws std::invalid_argument The provided netmask is invalid.
IPv6Network(const IPv6Address& address, const std::string& netmask);
/// A constructor that creates an instance of the class out of a string representing the network prefix and
/// a prefix length or a netmask
/// @param addressAndNetmask A string in one of these formats:
/// - IPV6_ADDRESS/Y where IPV6_ADDRESS is a valid IPv6 address representing the network prefix and Y is
/// a number between 0 and 128 representing the network prefix
/// - IPV6_ADDRESS/IPV6_NETMASK where IPV6_ADDRESS is a valid IPv6 address representing the network prefix
/// and IPV6_NETMASK is a valid IPv6 netmask
/// @throws std::invalid_argument The provided string does not represent a valid address and netmask format.
IPv6Network(const std::string& addressAndNetmask);
/// @return The prefix length, for example: the prefix length of 3546::/ffff:: is 16
uint8_t getPrefixLen() const;
/// @return The netmask, for example: the netmask of 3546::/16 is ffff::
std::string getNetmask() const
{
return IPv6Address(m_Mask).toString();
}
/// @return The network prefix, for example: the network prefix of 3546:f321::/16 is 3546::
IPv6Address getNetworkPrefix() const
{
return { m_NetworkPrefix };
}
/// @return The lowest non-reserved IPv6 address in this network, for example: the lowest address in 3546::/16
/// is 3546::1
IPv6Address getLowestAddress() const;
/// @return The highest IPv6 address in this network, for example: the highest address in 3546::/16 is
/// 3546:ffff:ffff:ffff:ffff:ffff:ffff:ffff
IPv6Address getHighestAddress() const;
/// @return The number of addresses in this network, for example: the number of addresses in 16ff::/120 is 256.
/// If the number of addresses exceeds the size of uint64_t a std::out_of_range exception is thrown
uint64_t getTotalAddressCount() const;
/// @param address An IPv6 address
/// @return True is the address belongs to the network, false otherwise or if the address isn't valid
bool includes(const IPv6Address& address) const;
/// @param network An IPv6 network
/// @return True is the input network is completely included within this network, false otherwise, for example:
/// 3546::/64 includes 3546::/120 but doesn't include 3546::/16
bool includes(const IPv6Network& network) const;
/// @return A string representation of the network in a format of NETWORK_PREFIX/PREFIX_LEN, for example:
/// fda7:9f81:6c23:275::/64
std::string toString() const;
private:
uint8_t m_NetworkPrefix[16]{};
uint8_t m_Mask[16]{};
static bool isValidNetmask(const IPv6Address& netmaskAddress);
void initFromAddressAndPrefixLength(const IPv6Address& address, uint8_t prefixLen);
void initFromAddressAndNetmask(const IPv6Address& address, const IPv6Address& netmaskAddress);
};
/// @class IPNetwork
/// A class representing version independent IP network definition, both IPv4 and IPv6 are included
class IPNetwork
{
public:
/// A constructor that creates an instance of the class out of an IP address and a full prefix length,
/// essentially making a network of consisting of only 1 address.
/// @param address An address representing the network prefix.
explicit IPNetwork(const IPAddress& address) : IPNetwork(address, address.isIPv4() ? 32U : 128U)
{}
/// A constructor that creates an instance of the class out of an address representing the network prefix
/// and a prefix length
/// @param address An address representing the network prefix. If the address is invalid std::invalid_argument
/// exception is thrown
/// @param prefixLen A number representing the prefix length. Allowed ranges are 0 - 32 for IPv4 networks and 0
/// - 128 for IPv6 networks.
/// @throws std::invalid_argument Prefix length is out of acceptable range.
IPNetwork(const IPAddress& address, uint8_t prefixLen)
{
if (address.isIPv4())
{
m_IPv4Network = std::make_unique<IPv4Network>(address.getIPv4(), prefixLen);
}
else
{
m_IPv6Network = std::make_unique<IPv6Network>(address.getIPv6(), prefixLen);
}
}
/// A constructor that creates an instance of the class out of an address representing the network prefix
/// and a netmask
/// @param address An address representing the network prefix. If the address is invalid std::invalid_argument
/// exception is thrown
/// @param netmask A string representing a netmask in valid format, for example: ffff:ffff:: for IPv6 networks
/// or 255.255.0.0 for IPv4 networks.
/// Please notice that netmasks that start with zeros are invalid, for example: 0:ffff:: or 0.255.255.255.
/// The only netmask starting with zeros that is valid is all zeros (:: or 0.0.0.0).
/// @throws std::invalid_argument The provided netmask is invalid.
IPNetwork(const IPAddress& address, const std::string& netmask)
{
if (address.isIPv4())
{
m_IPv4Network = std::make_unique<IPv4Network>(address.getIPv4(), netmask);
}
else
{
m_IPv6Network = std::make_unique<IPv6Network>(address.getIPv6(), netmask);
}
}
/// A constructor that creates an instance of the class out of a string representing the network prefix and
/// a prefix length or a netmask
/// @param addressAndNetmask A string in one of these formats:
/// - IP_ADDRESS/Y where IP_ADDRESS is a valid IP address representing the network prefix and Y is
/// a number representing the network prefix
/// - IP_ADDRESS/NETMASK where IP_ADDRESS is a valid IP address representing the network prefix and NETMASK
/// is a valid netmask for this type of network (IPv4 or IPv6 network)
/// @throws std::invalid_argument The provided string does not represent a valid address and netmask format.
IPNetwork(const std::string& addressAndNetmask)
{
try
{
m_IPv4Network = std::make_unique<IPv4Network>(addressAndNetmask);
}
catch (const std::invalid_argument&)
{
m_IPv6Network = std::make_unique<IPv6Network>(addressAndNetmask);
}
}
/// A copy c'tor for this class
/// @param other The instance to copy from
IPNetwork(const IPNetwork& other)
{
if (other.m_IPv4Network)
{
m_IPv4Network = std::make_unique<IPv4Network>(*other.m_IPv4Network);
}
if (other.m_IPv6Network)
{
m_IPv6Network = std::make_unique<IPv6Network>(*other.m_IPv6Network);
}
}
/// Overload of an assignment operator.
/// @param[in] other An instance of IPNetwork to assign
/// @return A reference to the assignee
IPNetwork& operator=(const IPNetwork& other)
{
// NOLINTBEGIN(cppcoreguidelines-c-copy-assignment-signature,misc-unconventional-assign-operator)
if (other.isIPv4Network())
{
return this->operator=(*other.m_IPv4Network);
}
return this->operator=(*other.m_IPv6Network);
// NOLINTEND(cppcoreguidelines-c-copy-assignment-signature,misc-unconventional-assign-operator)
}
/// Overload of an assignment operator.
/// @param[in] other An instance of IPv4Network to assign
/// @return A reference to the assignee
IPNetwork& operator=(const IPv4Network& other)
{
// Create the new instance first to maintain strong exception guarantee.
m_IPv4Network = std::make_unique<IPv4Network>(other);
m_IPv6Network = nullptr;
return *this;
}
/// Overload of an assignment operator.
/// @param[in] other An instance of IPv6Network to assign
/// @return A reference to the assignee
IPNetwork& operator=(const IPv6Network& other)
{
// Create the new instance first to maintain strong exception guarantee.
m_IPv6Network = std::make_unique<IPv6Network>(other);
m_IPv4Network = nullptr;
return *this;
}
/// @return The prefix length, for example: the prefix length of 3546::/ffff:: is 16, the prefix length of
/// 10.10.10.10/255.0.0.0 is 8
uint8_t getPrefixLen() const
{
return (m_IPv4Network != nullptr ? m_IPv4Network->getPrefixLen() : m_IPv6Network->getPrefixLen());
}
/// @return The netmask, for example: the netmask of 3546::/16 is ffff::, the netmask of 10.10.10.10/8 is
/// 255.0.0.0
std::string getNetmask() const
{
return (m_IPv4Network != nullptr ? m_IPv4Network->getNetmask() : m_IPv6Network->getNetmask());
}
/// @return The network prefix, for example: the network prefix of 3546:f321::/16 is 3546::, the network prefix
/// of 10.10.10.10/16 is 10.10.0.0
IPAddress getNetworkPrefix() const
{
return (m_IPv4Network != nullptr ? IPAddress(m_IPv4Network->getNetworkPrefix())
: IPAddress(m_IPv6Network->getNetworkPrefix()));
}
/// @return The lowest non-reserved IP address in this network, for example: the lowest address in 3546::/16 is
/// 3546::1, the lowest address in 10.10.10.10/16 is 10.10.0.1
IPAddress getLowestAddress() const
{
return (m_IPv4Network != nullptr ? IPAddress(m_IPv4Network->getLowestAddress())
: IPAddress(m_IPv6Network->getLowestAddress()));
}
/// @return The highest non-reserved IP address in this network, for example: the highest address in 3546::/16
/// is 3546:ffff:ffff:ffff:ffff:ffff:ffff:ffff, the highest address in 10.10.10.10/16 is 10.10.255.254
IPAddress getHighestAddress() const
{
return (m_IPv4Network != nullptr ? IPAddress(m_IPv4Network->getHighestAddress())
: IPAddress(m_IPv6Network->getHighestAddress()));
}
/// @return The number of addresses in this network, for example: the number of addresses in 16ff::/120 is 256,
/// the number of addresses in 10.10.0.0/24 is 256. If the number of addresses exceeds the size of uint64_t
/// a std::out_of_range exception is thrown
uint64_t getTotalAddressCount() const
{
return (m_IPv4Network != nullptr ? m_IPv4Network->getTotalAddressCount()
: m_IPv6Network->getTotalAddressCount());
}
/// @return True if this is an IPv4 network, false otherwise
bool isIPv4Network() const
{
return m_IPv4Network != nullptr;
}
/// @return True if this is an IPv6 network, false otherwise
bool isIPv6Network() const
{
return m_IPv6Network != nullptr;
}
/// @param address An IP address
/// @return True is the address belongs to the network, false otherwise or if the address isn't valid
bool includes(const IPAddress& address) const
{
if (m_IPv4Network != nullptr)
{
if (address.isIPv6())
{
return false;
}
return m_IPv4Network->includes(address.getIPv4());
}
if (address.isIPv4())
{
return false;
}
return m_IPv6Network->includes(address.getIPv6());
}
/// @param network An IP network
/// @return True is the input network is completely included within this network, false otherwise
bool includes(const IPNetwork& network) const
{
if (m_IPv4Network != nullptr)
{
if (network.isIPv6Network())
{
return false;
}
return m_IPv4Network->includes(*network.m_IPv4Network);
}
if (network.isIPv4Network())
{
return false;
}
return m_IPv6Network->includes(*network.m_IPv6Network);
}
/// @return A string representation of the network in a format of NETWORK_PREFIX/PREFIX_LEN, for example:
/// fda7:9f81:6c23:275::/64 or 192.168.0.0/16
std::string toString() const
{
return (m_IPv4Network != nullptr ? m_IPv4Network->toString() : m_IPv6Network->toString());
}
private:
std::unique_ptr<IPv4Network> m_IPv4Network;
std::unique_ptr<IPv6Network> m_IPv6Network;
};
namespace literals
{
inline IPv4Address operator""_ipv4(const char* addrString, std::size_t size)
{
return IPv4Address(std::string(addrString, size));
}
inline IPv6Address operator""_ipv6(const char* addrString, std::size_t size)
{
return IPv6Address(std::string(addrString, size));
}
} // namespace literals
inline std::ostream& operator<<(std::ostream& oss, const pcpp::IPv4Address& ipv4Address)
{
oss << ipv4Address.toString();
return oss;
}
inline std::ostream& operator<<(std::ostream& oss, const pcpp::IPv6Address& ipv6Address)
{
oss << ipv6Address.toString();
return oss;
}
inline std::ostream& operator<<(std::ostream& oss, const pcpp::IPAddress& ipAddress)
{
oss << ipAddress.toString();
return oss;
}
inline std::ostream& operator<<(std::ostream& oss, const pcpp::IPv4Network& network)
{
oss << network.toString();
return oss;
}
inline std::ostream& operator<<(std::ostream& oss, const pcpp::IPv6Network& network)
{
oss << network.toString();
return oss;
}
inline std::ostream& operator<<(std::ostream& oss, const pcpp::IPNetwork& network)
{
oss << network.toString();
return oss;
}
} // namespace pcpp