37 memcpy(m_Bytes.data(), &addrAsInt,
sizeof(addrAsInt));
44 memcpy(m_Bytes.data(), bytes, 4 *
sizeof(uint8_t));
49 IPv4Address(
const std::array<uint8_t, 4>& bytes) : m_Bytes(bytes)
58 inline uint32_t
toInt()
const;
63 return m_Bytes.data();
91 uint32_t intVal =
toInt();
92 std::reverse(
reinterpret_cast<uint8_t*
>(&intVal),
reinterpret_cast<uint8_t*
>(&intVal) +
sizeof(intVal));
94 uint32_t rhsIntVal = rhs.
toInt();
95 std::reverse(
reinterpret_cast<uint8_t*
>(&rhsIntVal),
96 reinterpret_cast<uint8_t*
>(&rhsIntVal) +
sizeof(rhsIntVal));
98 return intVal < rhsIntVal;
106 return !(*
this == rhs);
141 std::array<uint8_t, 4> m_Bytes = { 0 };
149 memcpy(&addr, m_Bytes.data(), m_Bytes.size() *
sizeof(uint8_t));
165 memcpy(m_Bytes.data(), bytes, 16 *
sizeof(uint8_t));
170 IPv6Address(
const std::array<uint8_t, 16>& bytes) : m_Bytes(bytes)
182 return m_Bytes.data();
221 return !(*
this == rhs);
227 void copyTo(uint8_t** arr,
size_t& length)
const;
234 memcpy(arr, m_Bytes.data(), m_Bytes.size() *
sizeof(uint8_t));
270 std::array<uint8_t, 16> m_Bytes = { 0 };
384 return !(*
this == rhs);
399 return rhs.
isIPv4() ? (m_IPv4 == rhs.m_IPv4) :
false;
402 return rhs.
isIPv6() ? m_IPv6 == rhs.m_IPv6 :
false;
411 return rhs.
isIPv4() ? (m_IPv4 < rhs.m_IPv4) :
true;
413 return rhs.
isIPv6() ? m_IPv6 < rhs.m_IPv6 :
false;
481 return m_NetworkPrefix;
510 uint32_t m_NetworkPrefix{};
513 static bool isValidNetmask(
const IPv4Address& netmaskAddress);
514 void initFromAddressAndPrefixLength(
const IPv4Address& address, uint8_t prefixLen);
515 void initFromAddressAndNetmask(
const IPv4Address& address,
const IPv4Address& netmaskAddress);
569 return { m_NetworkPrefix };
598 uint8_t m_NetworkPrefix[16]{};
599 uint8_t m_Mask[16]{};
601 static bool isValidNetmask(
const IPv6Address& netmaskAddress);
602 void initFromAddressAndPrefixLength(
const IPv6Address& address, uint8_t prefixLen);
603 void initFromAddressAndNetmask(
const IPv6Address& address,
const IPv6Address& netmaskAddress);
628 m_IPv4Network = std::unique_ptr<IPv4Network>(
new IPv4Network(address.
getIPv4(), prefixLen));
632 m_IPv6Network = std::unique_ptr<IPv6Network>(
new IPv6Network(address.
getIPv6(), prefixLen));
649 m_IPv4Network = std::unique_ptr<IPv4Network>(
new IPv4Network(address.
getIPv4(), netmask));
653 m_IPv6Network = std::unique_ptr<IPv6Network>(
new IPv6Network(address.
getIPv6(), netmask));
669 m_IPv4Network = std::unique_ptr<IPv4Network>(
new IPv4Network(addressAndNetmask));
671 catch (
const std::invalid_argument&)
673 m_IPv6Network = std::unique_ptr<IPv6Network>(
new IPv6Network(addressAndNetmask));
681 if (other.m_IPv4Network)
683 m_IPv4Network = std::unique_ptr<IPv4Network>(
new IPv4Network(*other.m_IPv4Network));
686 if (other.m_IPv6Network)
688 m_IPv6Network = std::unique_ptr<IPv6Network>(
new IPv6Network(*other.m_IPv6Network));
700 return this->
operator=(*other.m_IPv4Network);
703 return this->
operator=(*other.m_IPv6Network);
713 m_IPv4Network = std::unique_ptr<IPv4Network>(
new IPv4Network(other));
714 m_IPv6Network =
nullptr;
724 m_IPv6Network = std::unique_ptr<IPv6Network>(
new IPv6Network(other));
725 m_IPv4Network =
nullptr;
733 return (m_IPv4Network !=
nullptr ? m_IPv4Network->getPrefixLen() : m_IPv6Network->getPrefixLen());
740 return (m_IPv4Network !=
nullptr ? m_IPv4Network->getNetmask() : m_IPv6Network->getNetmask());
747 return (m_IPv4Network !=
nullptr ?
IPAddress(m_IPv4Network->getNetworkPrefix())
748 :
IPAddress(m_IPv6Network->getNetworkPrefix()));
755 return (m_IPv4Network !=
nullptr ?
IPAddress(m_IPv4Network->getLowestAddress())
756 :
IPAddress(m_IPv6Network->getLowestAddress()));
763 return (m_IPv4Network !=
nullptr ?
IPAddress(m_IPv4Network->getHighestAddress())
764 :
IPAddress(m_IPv6Network->getHighestAddress()));
772 return (m_IPv4Network !=
nullptr ? m_IPv4Network->getTotalAddressCount()
773 : m_IPv6Network->getTotalAddressCount());
779 return m_IPv4Network !=
nullptr;
785 return m_IPv6Network !=
nullptr;
792 if (m_IPv4Network !=
nullptr)
799 return m_IPv4Network->includes(address.
getIPv4());
807 return m_IPv6Network->includes(address.
getIPv6());
814 if (m_IPv4Network !=
nullptr)
821 return m_IPv4Network->includes(*network.m_IPv4Network);
829 return m_IPv6Network->includes(*network.m_IPv6Network);
836 return (m_IPv4Network !=
nullptr ? m_IPv4Network->toString() : m_IPv6Network->toString());
840 std::unique_ptr<IPv4Network> m_IPv4Network;
841 std::unique_ptr<IPv6Network> m_IPv6Network;
844 inline std::ostream& operator<<(std::ostream& oss,
const pcpp::IPv4Address& ipv4Address)
850 inline std::ostream& operator<<(std::ostream& oss,
const pcpp::IPv6Address& ipv6Address)
856 inline std::ostream& operator<<(std::ostream& oss,
const pcpp::IPAddress& ipAddress)
862 inline std::ostream& operator<<(std::ostream& oss,
const pcpp::IPv4Network& network)
868 inline std::ostream& operator<<(std::ostream& oss,
const pcpp::IPv6Network& network)
874 inline std::ostream& operator<<(std::ostream& oss,
const pcpp::IPNetwork& network)
Definition: IpAddress.h:276
IPAddress()
A default constructor that creates an instance of the class with unspecified IPv4 address.
Definition: IpAddress.h:288
std::string toString() const
Definition: IpAddress.h:325
AddressType
An enum representing the address type: IPv4 or IPv6.
Definition: IpAddress.h:280
@ IPv6AddressType
IPv6 address type.
Definition: IpAddress.h:284
@ IPv4AddressType
IPv4 address type.
Definition: IpAddress.h:282
const IPv4Address & getIPv4() const
Definition: IpAddress.h:351
bool operator!=(const IPAddress &rhs) const
Definition: IpAddress.h:382
const IPv6Address & getIPv6() const
Definition: IpAddress.h:358
IPAddress(const std::string &addrAsString)
IPAddress(const IPv4Address &addr)
Definition: IpAddress.h:293
AddressType getType() const
Definition: IpAddress.h:318
bool operator<(const IPAddress &rhs) const
Definition: IpAddress.h:405
bool isIPv4() const
Definition: IpAddress.h:331
bool isZero() const
Definition: IpAddress.h:364
IPAddress & operator=(const IPv4Address &addr)
Definition: IpAddress.h:416
bool isIPv6() const
Definition: IpAddress.h:337
bool isMulticast() const
Definition: IpAddress.h:344
bool operator==(const IPAddress &rhs) const
Definition: IpAddress.h:395
IPAddress(const IPv6Address &addr)
Definition: IpAddress.h:298
Definition: IpAddress.h:609
IPAddress getHighestAddress() const
Definition: IpAddress.h:761
IPNetwork & operator=(const IPv6Network &other)
Definition: IpAddress.h:721
IPAddress getLowestAddress() const
Definition: IpAddress.h:753
bool includes(const IPAddress &address) const
Definition: IpAddress.h:790
bool isIPv6Network() const
Definition: IpAddress.h:783
bool includes(const IPNetwork &network) const
Definition: IpAddress.h:812
std::string toString() const
Definition: IpAddress.h:834
bool isIPv4Network() const
Definition: IpAddress.h:777
IPNetwork & operator=(const IPNetwork &other)
Definition: IpAddress.h:695
IPNetwork(const IPNetwork &other)
Definition: IpAddress.h:679
IPAddress getNetworkPrefix() const
Definition: IpAddress.h:745
IPNetwork(const IPAddress &address, const std::string &netmask)
Definition: IpAddress.h:645
uint64_t getTotalAddressCount() const
Definition: IpAddress.h:770
std::string getNetmask() const
Definition: IpAddress.h:738
uint8_t getPrefixLen() const
Definition: IpAddress.h:731
IPNetwork & operator=(const IPv4Network &other)
Definition: IpAddress.h:710
IPNetwork(const IPAddress &address)
Definition: IpAddress.h:614
IPNetwork(const std::string &addressAndNetmask)
Definition: IpAddress.h:665
IPNetwork(const IPAddress &address, uint8_t prefixLen)
Definition: IpAddress.h:624
Definition: IpAddress.h:28
static bool isValidIPv4Address(const std::string &addrAsString)
bool matchNetwork(const std::string &network) const
static const IPv4Address MulticastRangeLowerBound
Definition: IpAddress.h:137
IPv4Address(const std::array< uint8_t, 4 > &bytes)
Definition: IpAddress.h:49
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:61
bool operator!=(const IPv4Address &rhs) const
Definition: IpAddress.h:104
IPv4Address(const uint32_t addrAsInt)
Definition: IpAddress.h:35
IPv4Address(const uint8_t bytes[4])
Definition: IpAddress.h:42
bool operator==(const IPv4Address &rhs) const
Definition: IpAddress.h:81
bool operator<(const IPv4Address &rhs) const
Definition: IpAddress.h:89
const std::array< uint8_t, 4 > & toByteArray() const
Definition: IpAddress.h:67
uint32_t toInt() const
Definition: IpAddress.h:146
static const IPv4Address Zero
A static value representing a zero value of IPv4 address, meaning address of value "0....
Definition: IpAddress.h:131
Definition: IpAddress.h:433
IPv4Network(const IPv4Address &address)
Definition: IpAddress.h:438
IPv4Address getLowestAddress() const
std::string getNetmask() const
Definition: IpAddress.h:473
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:479
IPv4Network(const std::string &addressAndNetmask)
bool includes(const IPv4Network &network) const
Definition: IpAddress.h:156
static const IPv6Address MulticastRangeLowerBound
Definition: IpAddress.h:267
void copyTo(uint8_t *arr) const
Definition: IpAddress.h:232
void copyTo(uint8_t **arr, size_t &length) const
const uint8_t * toBytes() const
Definition: IpAddress.h:180
bool matchNetwork(const IPv6Network &network) const
IPv6Address(const uint8_t bytes[16])
Definition: IpAddress.h:163
static const IPv6Address Zero
Definition: IpAddress.h:262
bool matchNetwork(const std::string &network) const
bool operator<(const IPv6Address &rhs) const
Definition: IpAddress.h:211
bool operator!=(const IPv6Address &rhs) const
Definition: IpAddress.h:219
const std::array< uint8_t, 16 > & toByteArray() const
Definition: IpAddress.h:187
IPv6Address(const std::string &addrAsString)
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:203
IPv6Address(const std::array< uint8_t, 16 > &bytes)
Definition: IpAddress.h:170
Definition: IpAddress.h:521
IPv6Network(const std::string &addressAndNetmask)
IPv6Address getHighestAddress() const
uint8_t getPrefixLen() const
std::string getNetmask() const
Definition: IpAddress.h:561
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:567
std::string toString() const
bool includes(const IPv6Address &address) const
IPv6Network(const IPv6Address &address)
Definition: IpAddress.h:526
The main namespace for the PcapPlusPlus lib.