1
1
#pragma once
2
2
3
- #include < stdint.h >
4
- #include < string.h >
3
+ #include < cstdint >
4
+ #include < cstring >
5
5
#include < string>
6
6
#include < algorithm>
7
7
#include < ostream>
@@ -145,7 +145,7 @@ namespace pcpp
145
145
146
146
uint32_t IPv4Address::toInt () const
147
147
{
148
- uint32_t addr;
148
+ uint32_t addr = 0 ;
149
149
memcpy (&addr, m_Bytes.data (), m_Bytes.size () * sizeof (uint8_t ));
150
150
return addr;
151
151
}
@@ -276,7 +276,7 @@ namespace pcpp
276
276
{
277
277
public:
278
278
// / An enum representing the address type: IPv4 or IPv6
279
- enum AddressType
279
+ enum AddressType : uint8_t
280
280
{
281
281
// / IPv4 address type
282
282
IPv4AddressType,
@@ -395,7 +395,9 @@ namespace pcpp
395
395
bool IPAddress::operator ==(const IPAddress& rhs) const
396
396
{
397
397
if (isIPv4 ())
398
+ {
398
399
return rhs.isIPv4 () ? (m_IPv4 == rhs.m_IPv4 ) : false ;
400
+ }
399
401
400
402
return rhs.isIPv6 () ? m_IPv6 == rhs.m_IPv6 : false ;
401
403
}
@@ -433,7 +435,7 @@ namespace pcpp
433
435
// / A constructor that creates an instance of the class out of an address and a full prefix length,
434
436
// / essentially making a network of consisting of only 1 address.
435
437
// / @param address An address representing the network prefix.
436
- explicit IPv4Network (const IPv4Address& address) : IPv4Network(address, 32u )
438
+ explicit IPv4Network (const IPv4Address& address) : IPv4Network(address, 32U )
437
439
{}
438
440
439
441
// / A constructor that creates an instance of the class out of an address representing the network prefix
@@ -476,7 +478,7 @@ namespace pcpp
476
478
// / @return The network prefix, for example: the network prefix of 10.10.10.10/16 is 10.10.0.0
477
479
IPv4Address getNetworkPrefix () const
478
480
{
479
- return IPv4Address ( m_NetworkPrefix) ;
481
+ return m_NetworkPrefix;
480
482
}
481
483
482
484
// / @return The lowest non-reserved IPv4 address in this network, for example: the lowest address
@@ -505,10 +507,10 @@ namespace pcpp
505
507
std::string toString () const ;
506
508
507
509
private:
508
- uint32_t m_NetworkPrefix;
509
- uint32_t m_Mask;
510
+ uint32_t m_NetworkPrefix{} ;
511
+ uint32_t m_Mask{} ;
510
512
511
- bool isValidNetmask (const IPv4Address& netmaskAddress);
513
+ static bool isValidNetmask (const IPv4Address& netmaskAddress);
512
514
void initFromAddressAndPrefixLength (const IPv4Address& address, uint8_t prefixLen);
513
515
void initFromAddressAndNetmask (const IPv4Address& address, const IPv4Address& netmaskAddress);
514
516
};
@@ -521,7 +523,7 @@ namespace pcpp
521
523
// / A constructor that creates an instance of the class out of an address and a full prefix length,
522
524
// / essentially making a network of consisting of only 1 address.
523
525
// / @param address An address representing the network prefix.
524
- explicit IPv6Network (const IPv6Address& address) : IPv6Network(address, 128u )
526
+ explicit IPv6Network (const IPv6Address& address) : IPv6Network(address, 128U )
525
527
{}
526
528
527
529
// / A constructor that creates an instance of the class out of an address representing the network prefix
@@ -564,7 +566,7 @@ namespace pcpp
564
566
// / @return The network prefix, for example: the network prefix of 3546:f321::/16 is 3546::
565
567
IPv6Address getNetworkPrefix () const
566
568
{
567
- return IPv6Address ( m_NetworkPrefix) ;
569
+ return { m_NetworkPrefix } ;
568
570
}
569
571
570
572
// / @return The lowest non-reserved IPv6 address in this network, for example: the lowest address in 3546::/16
@@ -593,10 +595,10 @@ namespace pcpp
593
595
std::string toString () const ;
594
596
595
597
private:
596
- uint8_t m_NetworkPrefix[16 ];
597
- uint8_t m_Mask[16 ];
598
+ uint8_t m_NetworkPrefix[16 ]{} ;
599
+ uint8_t m_Mask[16 ]{} ;
598
600
599
- bool isValidNetmask (const IPv6Address& netmaskAddress);
601
+ static bool isValidNetmask (const IPv6Address& netmaskAddress);
600
602
void initFromAddressAndPrefixLength (const IPv6Address& address, uint8_t prefixLen);
601
603
void initFromAddressAndNetmask (const IPv6Address& address, const IPv6Address& netmaskAddress);
602
604
};
@@ -609,7 +611,7 @@ namespace pcpp
609
611
// / A constructor that creates an instance of the class out of an IP address and a full prefix length,
610
612
// / essentially making a network of consisting of only 1 address.
611
613
// / @param address An address representing the network prefix.
612
- explicit IPNetwork (const IPAddress& address) : IPNetwork(address, address.isIPv4() ? 32u : 128u )
614
+ explicit IPNetwork (const IPAddress& address) : IPNetwork(address, address.isIPv4() ? 32U : 128U )
613
615
{}
614
616
615
617
// / A constructor that creates an instance of the class out of an address representing the network prefix
@@ -692,14 +694,14 @@ namespace pcpp
692
694
// / @return A reference to the assignee
693
695
IPNetwork& operator =(const IPNetwork& other)
694
696
{
697
+ // NOLINTBEGIN(cppcoreguidelines-c-copy-assignment-signature,misc-unconventional-assign-operator)
695
698
if (other.isIPv4Network ())
696
699
{
697
700
return this ->operator =(*other.m_IPv4Network );
698
701
}
699
- else
700
- {
701
- return this ->operator =(*other.m_IPv6Network );
702
- }
702
+
703
+ return this ->operator =(*other.m_IPv6Network );
704
+ // NOLINTEND(cppcoreguidelines-c-copy-assignment-signature,misc-unconventional-assign-operator)
703
705
}
704
706
705
707
// / Overload of an assignment operator.
@@ -796,15 +798,13 @@ namespace pcpp
796
798
797
799
return m_IPv4Network->includes (address.getIPv4 ());
798
800
}
799
- else
800
- {
801
- if (address.isIPv4 ())
802
- {
803
- return false ;
804
- }
805
801
806
- return m_IPv6Network->includes (address.getIPv6 ());
802
+ if (address.isIPv4 ())
803
+ {
804
+ return false ;
807
805
}
806
+
807
+ return m_IPv6Network->includes (address.getIPv6 ());
808
808
}
809
809
810
810
// / @param network An IP network
@@ -820,15 +820,13 @@ namespace pcpp
820
820
821
821
return m_IPv4Network->includes (*network.m_IPv4Network );
822
822
}
823
- else
824
- {
825
- if (network.isIPv4Network ())
826
- {
827
- return false ;
828
- }
829
823
830
- return m_IPv6Network->includes (*network.m_IPv6Network );
824
+ if (network.isIPv4Network ())
825
+ {
826
+ return false ;
831
827
}
828
+
829
+ return m_IPv6Network->includes (*network.m_IPv6Network );
832
830
}
833
831
834
832
// / @return A string representation of the network in a format of NETWORK_PREFIX/PREFIX_LEN, for example:
@@ -843,40 +841,40 @@ namespace pcpp
843
841
std::unique_ptr<IPv6Network> m_IPv6Network;
844
842
};
845
843
846
- inline std::ostream& operator <<(std::ostream& os , const pcpp::IPv4Address& ipv4Address)
844
+ inline std::ostream& operator <<(std::ostream& oss , const pcpp::IPv4Address& ipv4Address)
847
845
{
848
- os << ipv4Address.toString ();
849
- return os ;
846
+ oss << ipv4Address.toString ();
847
+ return oss ;
850
848
}
851
849
852
- inline std::ostream& operator <<(std::ostream& os , const pcpp::IPv6Address& ipv6Address)
850
+ inline std::ostream& operator <<(std::ostream& oss , const pcpp::IPv6Address& ipv6Address)
853
851
{
854
- os << ipv6Address.toString ();
855
- return os ;
852
+ oss << ipv6Address.toString ();
853
+ return oss ;
856
854
}
857
855
858
- inline std::ostream& operator <<(std::ostream& os , const pcpp::IPAddress& ipAddress)
856
+ inline std::ostream& operator <<(std::ostream& oss , const pcpp::IPAddress& ipAddress)
859
857
{
860
- os << ipAddress.toString ();
861
- return os ;
858
+ oss << ipAddress.toString ();
859
+ return oss ;
862
860
}
863
861
864
- inline std::ostream& operator <<(std::ostream& os , const pcpp::IPv4Network& network)
862
+ inline std::ostream& operator <<(std::ostream& oss , const pcpp::IPv4Network& network)
865
863
{
866
- os << network.toString ();
867
- return os ;
864
+ oss << network.toString ();
865
+ return oss ;
868
866
}
869
867
870
- inline std::ostream& operator <<(std::ostream& os , const pcpp::IPv6Network& network)
868
+ inline std::ostream& operator <<(std::ostream& oss , const pcpp::IPv6Network& network)
871
869
{
872
- os << network.toString ();
873
- return os ;
870
+ oss << network.toString ();
871
+ return oss ;
874
872
}
875
873
876
- inline std::ostream& operator <<(std::ostream& os , const pcpp::IPNetwork& network)
874
+ inline std::ostream& operator <<(std::ostream& oss , const pcpp::IPNetwork& network)
877
875
{
878
- os << network.toString ();
879
- return os ;
876
+ oss << network.toString ();
877
+ return oss ;
880
878
}
881
879
882
880
} // namespace pcpp
0 commit comments