39 memcpy(m_Bytes.data(), &addrAsInt,
sizeof(addrAsInt));
58 IPv4Address(
const std::array<uint8_t, 4>& bytes) : m_Bytes(bytes)
67 inline uint32_t
toInt()
const;
72 return m_Bytes.data();
100 uint32_t intVal =
toInt();
101 std::reverse(
reinterpret_cast<uint8_t*
>(&intVal),
reinterpret_cast<uint8_t*
>(&intVal) +
sizeof(intVal));
103 uint32_t rhsIntVal = rhs.
toInt();
104 std::reverse(
reinterpret_cast<uint8_t*
>(&rhsIntVal),
105 reinterpret_cast<uint8_t*
>(&rhsIntVal) +
sizeof(rhsIntVal));
107 return intVal < rhsIntVal;
115 return !(*
this == rhs);
150 std::array<uint8_t, 4> m_Bytes = { 0 };
158 memcpy(&addr, m_Bytes.data(), m_Bytes.size() *
sizeof(uint8_t));
186 IPv6Address(
const std::array<uint8_t, 16>& bytes) : m_Bytes(bytes)
198 return m_Bytes.data();
237 return !(*
this == rhs);
245 PCPP_DEPRECATED(
"Use copyToNewBuffer instead.")
246 void
copyTo(uint8_t** arr,
size_t& length) const;
268 size_t copyTo(uint8_t* buffer,
size_t size)
const;
312 std::array<uint8_t, 16> m_Bytes = { 0 };
426 return !(*
this == rhs);
441 return rhs.
isIPv4() ? (m_IPv4 == rhs.m_IPv4) :
false;
444 return rhs.
isIPv6() ? m_IPv6 == rhs.m_IPv6 :
false;
453 return rhs.
isIPv4() ? (m_IPv4 < rhs.m_IPv4) :
true;
455 return rhs.
isIPv6() ? m_IPv6 < rhs.m_IPv6 :
false;
523 return m_NetworkPrefix;
552 uint32_t m_NetworkPrefix{};
555 static bool isValidNetmask(
const IPv4Address& netmaskAddress);
556 void initFromAddressAndPrefixLength(
const IPv4Address& address, uint8_t prefixLen);
557 void initFromAddressAndNetmask(
const IPv4Address& address,
const IPv4Address& netmaskAddress);
611 return { m_NetworkPrefix };
640 uint8_t m_NetworkPrefix[16]{};
641 uint8_t m_Mask[16]{};
643 static bool isValidNetmask(
const IPv6Address& netmaskAddress);
644 void initFromAddressAndPrefixLength(
const IPv6Address& address, uint8_t prefixLen);
645 void initFromAddressAndNetmask(
const IPv6Address& address,
const IPv6Address& netmaskAddress);
670 m_IPv4Network = std::make_unique<IPv4Network>(address.
getIPv4(), prefixLen);
674 m_IPv6Network = std::make_unique<IPv6Network>(address.
getIPv6(), prefixLen);
691 m_IPv4Network = std::make_unique<IPv4Network>(address.
getIPv4(), netmask);
695 m_IPv6Network = std::make_unique<IPv6Network>(address.
getIPv6(), netmask);
711 m_IPv4Network = std::make_unique<IPv4Network>(addressAndNetmask);
713 catch (
const std::invalid_argument&)
715 m_IPv6Network = std::make_unique<IPv6Network>(addressAndNetmask);
723 if (other.m_IPv4Network)
725 m_IPv4Network = std::make_unique<IPv4Network>(*other.m_IPv4Network);
728 if (other.m_IPv6Network)
730 m_IPv6Network = std::make_unique<IPv6Network>(*other.m_IPv6Network);
742 return this->
operator=(*other.m_IPv4Network);
745 return this->
operator=(*other.m_IPv6Network);
755 m_IPv4Network = std::make_unique<IPv4Network>(other);
756 m_IPv6Network =
nullptr;
766 m_IPv6Network = std::make_unique<IPv6Network>(other);
767 m_IPv4Network =
nullptr;
775 return (m_IPv4Network !=
nullptr ? m_IPv4Network->getPrefixLen() : m_IPv6Network->getPrefixLen());
782 return (m_IPv4Network !=
nullptr ? m_IPv4Network->getNetmask() : m_IPv6Network->getNetmask());
789 return (m_IPv4Network !=
nullptr ?
IPAddress(m_IPv4Network->getNetworkPrefix())
790 :
IPAddress(m_IPv6Network->getNetworkPrefix()));
797 return (m_IPv4Network !=
nullptr ?
IPAddress(m_IPv4Network->getLowestAddress())
798 :
IPAddress(m_IPv6Network->getLowestAddress()));
805 return (m_IPv4Network !=
nullptr ?
IPAddress(m_IPv4Network->getHighestAddress())
806 :
IPAddress(m_IPv6Network->getHighestAddress()));
814 return (m_IPv4Network !=
nullptr ? m_IPv4Network->getTotalAddressCount()
815 : m_IPv6Network->getTotalAddressCount());
821 return m_IPv4Network !=
nullptr;
827 return m_IPv6Network !=
nullptr;
834 if (m_IPv4Network !=
nullptr)
841 return m_IPv4Network->includes(address.
getIPv4());
849 return m_IPv6Network->includes(address.
getIPv6());
856 if (m_IPv4Network !=
nullptr)
863 return m_IPv4Network->includes(*network.m_IPv4Network);
871 return m_IPv6Network->includes(*network.m_IPv6Network);
878 return (m_IPv4Network !=
nullptr ? m_IPv4Network->toString() : m_IPv6Network->toString());
882 std::unique_ptr<IPv4Network> m_IPv4Network;
883 std::unique_ptr<IPv6Network> m_IPv6Network;
886 inline std::ostream& operator<<(std::ostream& oss,
const pcpp::IPv4Address& ipv4Address)
892 inline std::ostream& operator<<(std::ostream& oss,
const pcpp::IPv6Address& ipv6Address)
898 inline std::ostream& operator<<(std::ostream& oss,
const pcpp::IPAddress& ipAddress)
904 inline std::ostream& operator<<(std::ostream& oss,
const pcpp::IPv4Network& network)
910 inline std::ostream& operator<<(std::ostream& oss,
const pcpp::IPv6Network& network)
916 inline std::ostream& operator<<(std::ostream& oss,
const pcpp::IPNetwork& network)
Definition: IpAddress.h:318
IPAddress()
A default constructor that creates an instance of the class with unspecified IPv4 address.
Definition: IpAddress.h:330
std::string toString() const
Definition: IpAddress.h:367
AddressType
An enum representing the address type: IPv4 or IPv6.
Definition: IpAddress.h:322
@ IPv6AddressType
IPv6 address type.
Definition: IpAddress.h:326
@ IPv4AddressType
IPv4 address type.
Definition: IpAddress.h:324
const IPv4Address & getIPv4() const
Definition: IpAddress.h:393
bool operator!=(const IPAddress &rhs) const
Definition: IpAddress.h:424
const IPv6Address & getIPv6() const
Definition: IpAddress.h:400
IPAddress(const std::string &addrAsString)
IPAddress(const IPv4Address &addr)
Definition: IpAddress.h:335
AddressType getType() const
Definition: IpAddress.h:360
bool operator<(const IPAddress &rhs) const
Definition: IpAddress.h:447
bool isIPv4() const
Definition: IpAddress.h:373
bool isZero() const
Definition: IpAddress.h:406
IPAddress & operator=(const IPv4Address &addr)
Definition: IpAddress.h:458
bool isIPv6() const
Definition: IpAddress.h:379
bool isMulticast() const
Definition: IpAddress.h:386
bool operator==(const IPAddress &rhs) const
Definition: IpAddress.h:437
IPAddress(const IPv6Address &addr)
Definition: IpAddress.h:340
Definition: IpAddress.h:651
IPAddress getHighestAddress() const
Definition: IpAddress.h:803
IPNetwork & operator=(const IPv6Network &other)
Definition: IpAddress.h:763
IPAddress getLowestAddress() const
Definition: IpAddress.h:795
bool includes(const IPAddress &address) const
Definition: IpAddress.h:832
bool isIPv6Network() const
Definition: IpAddress.h:825
bool includes(const IPNetwork &network) const
Definition: IpAddress.h:854
std::string toString() const
Definition: IpAddress.h:876
bool isIPv4Network() const
Definition: IpAddress.h:819
IPNetwork & operator=(const IPNetwork &other)
Definition: IpAddress.h:737
IPNetwork(const IPNetwork &other)
Definition: IpAddress.h:721
IPAddress getNetworkPrefix() const
Definition: IpAddress.h:787
IPNetwork(const IPAddress &address, const std::string &netmask)
Definition: IpAddress.h:687
uint64_t getTotalAddressCount() const
Definition: IpAddress.h:812
std::string getNetmask() const
Definition: IpAddress.h:780
uint8_t getPrefixLen() const
Definition: IpAddress.h:773
IPNetwork & operator=(const IPv4Network &other)
Definition: IpAddress.h:752
IPNetwork(const IPAddress &address)
Definition: IpAddress.h:656
IPNetwork(const std::string &addressAndNetmask)
Definition: IpAddress.h:707
IPNetwork(const IPAddress &address, uint8_t prefixLen)
Definition: IpAddress.h:666
Definition: IpAddress.h:30
static bool isValidIPv4Address(const std::string &addrAsString)
bool matchNetwork(const std::string &network) const
static const IPv4Address MulticastRangeLowerBound
Definition: IpAddress.h:146
IPv4Address(const std::array< uint8_t, 4 > &bytes)
Definition: IpAddress.h:58
bool matchNetwork(const IPv4Network &network) const
IPv4Address()=default
A default constructor that creates an instance of the class with the zero-initialized address.
std::string toString() const
IPv4Address(const std::string &addrAsString)
const uint8_t * toBytes() const
Definition: IpAddress.h:70
bool operator!=(const IPv4Address &rhs) const
Definition: IpAddress.h:113
IPv4Address(const uint8_t *bytes, size_t size)
IPv4Address(const uint32_t addrAsInt)
Definition: IpAddress.h:37
IPv4Address(const uint8_t bytes[4])
Definition: IpAddress.h:46
bool operator==(const IPv4Address &rhs) const
Definition: IpAddress.h:90
bool operator<(const IPv4Address &rhs) const
Definition: IpAddress.h:98
const std::array< uint8_t, 4 > & toByteArray() const
Definition: IpAddress.h:76
uint32_t toInt() const
Definition: IpAddress.h:155
static const IPv4Address Zero
A static value representing a zero value of IPv4 address, meaning address of value "0....
Definition: IpAddress.h:140
Definition: IpAddress.h:475
IPv4Network(const IPv4Address &address)
Definition: IpAddress.h:480
IPv4Address getLowestAddress() const
std::string getNetmask() const
Definition: IpAddress.h:515
IPv4Network(const IPv4Address &address, const std::string &netmask)
IPv4Address getHighestAddress() const
std::string toString() const
uint8_t getPrefixLen() const
bool includes(const IPv4Address &address) const
uint64_t getTotalAddressCount() const
IPv4Network(const IPv4Address &address, uint8_t prefixLen)
IPv4Address getNetworkPrefix() const
Definition: IpAddress.h:521
IPv4Network(const std::string &addressAndNetmask)
bool includes(const IPv4Network &network) const
Definition: IpAddress.h:165
static const IPv6Address MulticastRangeLowerBound
Definition: IpAddress.h:309
void copyTo(uint8_t **arr, size_t &length) const
const uint8_t * toBytes() const
Definition: IpAddress.h:196
bool matchNetwork(const IPv6Network &network) const
IPv6Address(const uint8_t bytes[16])
Definition: IpAddress.h:174
IPv6Address(const uint8_t *bytes, size_t size)
A constructor that creates an instance of the class out of a 16-byte array.
static const IPv6Address Zero
Definition: IpAddress.h:304
bool matchNetwork(const std::string &network) const
bool operator<(const IPv6Address &rhs) const
Definition: IpAddress.h:227
bool operator!=(const IPv6Address &rhs) const
Definition: IpAddress.h:235
const std::array< uint8_t, 16 > & toByteArray() const
Definition: IpAddress.h:203
IPv6Address(const std::string &addrAsString)
bool copyToNewBuffer(uint8_t **buffer, size_t &size) const
Allocates a new buffer and copies the address value to it. The user is responsible for deallocating t...
size_t copyTo(uint8_t *buffer, size_t size) const
Copies the address value to a user-provided buffer.
IPv6Address()=default
A default constructor that creates an instance of the class with the zero-initialized address.
static bool isValidIPv6Address(const std::string &addrAsString)
std::string toString() const
bool operator==(const IPv6Address &rhs) const
Definition: IpAddress.h:219
IPv6Address(const std::array< uint8_t, 16 > &bytes)
Definition: IpAddress.h:186
Definition: IpAddress.h:563
IPv6Network(const std::string &addressAndNetmask)
IPv6Address getHighestAddress() const
uint8_t getPrefixLen() const
std::string getNetmask() const
Definition: IpAddress.h:603
bool includes(const IPv6Network &network) const
IPv6Address getLowestAddress() const
IPv6Network(const IPv6Address &address, const std::string &netmask)
IPv6Network(const IPv6Address &address, uint8_t prefixLen)
uint64_t getTotalAddressCount() const
IPv6Address getNetworkPrefix() const
Definition: IpAddress.h:609
std::string toString() const
bool includes(const IPv6Address &address) const
IPv6Network(const IPv6Address &address)
Definition: IpAddress.h:568
The main namespace for the PcapPlusPlus lib.