1 #ifndef PCAPPP_IP_ADDRESSES 2 #define PCAPPP_IP_ADDRESSES 10 #ifndef PCPP_DEPRECATED 11 #if defined(__GNUC__) || defined(__clang__) 12 #define PCPP_DEPRECATED __attribute__((deprecated)) 13 #elif defined(_MSC_VER) 14 #define PCPP_DEPRECATED __declspec(deprecated) 16 #pragma message("WARNING: DEPRECATED feature is not implemented for this compiler") 17 #define PCPP_DEPRECATED 54 IPv4Address(uint32_t addrAsInt) { memcpy(m_Bytes, &addrAsInt,
sizeof(m_Bytes)); }
60 IPv4Address(
const uint8_t bytes[4]) { memcpy(m_Bytes, bytes,
sizeof(m_Bytes)); }
73 inline uint32_t
toInt()
const;
78 const uint8_t*
toBytes()
const {
return m_Bytes; }
112 uint32_t intVal =
toInt();
113 std::reverse((uint8_t*)(&intVal), (uint8_t*)(&intVal) +
sizeof(intVal));
115 uint32_t rhsIntVal = rhs.
toInt();
116 std::reverse((uint8_t*)(&rhsIntVal), (uint8_t*)(&rhsIntVal) +
sizeof(rhsIntVal));
118 return intVal < rhsIntVal;
184 memcpy(&addr, m_Bytes,
sizeof(m_Bytes));
204 IPv6Address(
const uint8_t bytes[16]) { memcpy(m_Bytes, bytes,
sizeof(m_Bytes)); }
216 const uint8_t*
toBytes()
const {
return m_Bytes; }
262 void copyTo(uint8_t** arr,
size_t& length)
const;
269 void copyTo(uint8_t* arr)
const { memcpy(arr, m_Bytes,
sizeof(m_Bytes)); }
361 IPAddress(
const std::string& addrAsString);
387 std::string
toString()
const {
return (getType() == IPv4AddressType) ? m_IPv4.toString() : m_IPv6.toString(); }
392 bool isValid()
const {
return (getType() == IPv4AddressType) ? m_IPv4.isValid() : m_IPv6.isValid(); }
397 bool isIPv4()
const {
return getType() == IPv4AddressType; }
402 bool isIPv6()
const {
return getType() == IPv6AddressType; }
408 bool isMulticast()
const {
return (getType() == IPv4AddressType) ? m_IPv4.isMulticast() : m_IPv6.isMulticast(); }
455 return rhs.
isIPv4() ? (m_IPv4 == rhs.m_IPv4) :
false;
457 return rhs.
isIPv6() ? m_IPv6 == rhs.m_IPv6 :
false;
466 return rhs.
isIPv4() ? (m_IPv4 < rhs.m_IPv4) :
true;
468 return rhs.
isIPv6() ? m_IPv6 < rhs.m_IPv6 :
false;
473 m_Type = IPv4AddressType;
480 m_Type = IPv6AddressType;
530 uint8_t getPrefixLen()
const;
558 uint64_t getTotalAddressCount()
const;
580 uint32_t m_NetworkPrefix;
583 bool isValidNetmask(
const std::string& netmask);
584 void initFromAddressAndPrefixLength(
const IPv4Address& address, uint8_t prefixLen);
585 void initFromAddressAndNetmask(
const IPv4Address& address,
const std::string& netmask);
633 uint8_t getPrefixLen()
const;
661 uint64_t getTotalAddressCount()
const;
683 uint8_t m_NetworkPrefix[16];
686 bool isValidNetmask(
const std::string& netmask);
687 void initFromAddressAndPrefixLength(
const IPv6Address& address, uint8_t prefixLen);
688 void initFromAddressAndNetmask(
const IPv6Address& address,
const std::string& netmask);
712 m_IPv6Network =
nullptr;
717 m_IPv4Network =
nullptr;
737 m_IPv6Network =
nullptr;
742 m_IPv4Network =
nullptr;
760 m_IPv4Network =
new IPv4Network(addressAndNetmask);
761 m_IPv6Network =
nullptr;
763 catch (
const std::invalid_argument&)
765 m_IPv6Network =
new IPv6Network(addressAndNetmask);
766 m_IPv4Network =
nullptr;
776 m_IPv4Network =
nullptr;
777 m_IPv6Network =
nullptr;
779 if (other.m_IPv4Network)
781 m_IPv4Network =
new IPv4Network(*other.m_IPv4Network);
784 if (other.m_IPv6Network)
786 m_IPv6Network =
new IPv6Network(*other.m_IPv6Network);
797 delete m_IPv4Network;
802 delete m_IPv6Network;
815 return this->operator=(*other.m_IPv4Network);
819 return this->operator=(*other.m_IPv6Network);
832 delete m_IPv4Network;
833 m_IPv4Network =
nullptr;
838 delete m_IPv6Network;
839 m_IPv6Network =
nullptr;
856 delete m_IPv4Network;
857 m_IPv4Network =
nullptr;
862 delete m_IPv6Network;
863 m_IPv6Network =
nullptr;
877 return (m_IPv4Network !=
nullptr ? m_IPv4Network->getPrefixLen() : m_IPv6Network->getPrefixLen());
885 return (m_IPv4Network !=
nullptr ? m_IPv4Network->getNetmask() : m_IPv6Network->getNetmask());
894 return (m_IPv4Network !=
nullptr ?
IPAddress(m_IPv4Network->getNetworkPrefix()) :
IPAddress(m_IPv6Network->getNetworkPrefix()));
903 return (m_IPv4Network !=
nullptr ?
IPAddress(m_IPv4Network->getLowestAddress()) :
IPAddress(m_IPv6Network->getLowestAddress()));
912 return (m_IPv4Network !=
nullptr ?
IPAddress(m_IPv4Network->getHighestAddress()) :
IPAddress(m_IPv6Network->getHighestAddress()));
922 return (m_IPv4Network !=
nullptr ? m_IPv4Network->getTotalAddressCount() : m_IPv6Network->getTotalAddressCount());
930 return m_IPv4Network !=
nullptr;
938 return m_IPv6Network !=
nullptr;
947 if (m_IPv4Network !=
nullptr)
954 return m_IPv4Network->includes(address.
getIPv4());
963 return m_IPv6Network->includes(address.
getIPv6());
973 if (m_IPv4Network !=
nullptr)
980 return m_IPv4Network->includes(*network.m_IPv4Network);
989 return m_IPv6Network->includes(*network.m_IPv6Network);
999 return (m_IPv4Network !=
nullptr ? m_IPv4Network->toString() : m_IPv6Network->toString());
1008 inline std::ostream& operator<<(std::ostream& os,
const pcpp::IPv4Address& ipv4Address)
1014 inline std::ostream& operator<<(std::ostream& os,
const pcpp::IPv6Address& ipv6Address)
1020 inline std::ostream& operator<<(std::ostream& os,
const pcpp::IPAddress& ipAddress)
1026 inline std::ostream& operator<<(std::ostream& os,
const pcpp::IPv4Network& network)
1032 inline std::ostream& operator<<(std::ostream& os,
const pcpp::IPv6Network& network)
1038 inline std::ostream& operator<<(std::ostream& os,
const pcpp::IPNetwork& network)
The main namespace for the PcapPlusPlus lib.
std::string getNetmask() const
Definition: IpAddress.h:883
const uint8_t * toBytes() const
Definition: IpAddress.h:216
IPv4Address()
Definition: IpAddress.h:48
IPv4Address(const uint8_t bytes[4])
Definition: IpAddress.h:60
uint8_t getPrefixLen() const
Definition: IpAddress.h:875
static const IPv6Address MulticastRangeLowerBound
Definition: IpAddress.h:310
Definition: IpAddress.h:696
AddressType
Definition: IpAddress.h:327
IPv6Address getNetworkPrefix() const
Definition: IpAddress.h:643
IPNetwork(const IPAddress &address, uint8_t prefixLen)
Definition: IpAddress.h:707
bool isIPv4() const
Definition: IpAddress.h:397
uint64_t getTotalAddressCount() const
Definition: IpAddress.h:920
IPNetwork & operator=(const IPNetwork &other)
Definition: IpAddress.h:811
IPNetwork & operator=(const IPv4Network &other)
Definition: IpAddress.h:828
bool isValid() const
Definition: IpAddress.h:96
static const IPv4Address Zero
Definition: IpAddress.h:163
bool operator==(const IPv4Address &rhs) const
Definition: IpAddress.h:103
const uint8_t * toBytes() const
Definition: IpAddress.h:78
bool operator<(const IPAddress &rhs) const
Definition: IpAddress.h:460
bool operator==(const IPAddress &rhs) const
Definition: IpAddress.h:452
static const IPv4Address MulticastRangeLowerBound
Definition: IpAddress.h:171
bool operator!=(const IPAddress &rhs) const
Definition: IpAddress.h:441
bool operator<(const IPv6Address &rhs) const
Definition: IpAddress.h:247
IPNetwork(const std::string &addressAndNetmask)
Definition: IpAddress.h:756
static const IPv6Address Zero
Definition: IpAddress.h:303
IPv4Address(uint32_t addrAsInt)
Definition: IpAddress.h:54
bool isMulticast() const
Definition: IpAddress.h:408
~IPNetwork()
Definition: IpAddress.h:793
bool isIPv6() const
Definition: IpAddress.h:402
bool isIPv6Network() const
Definition: IpAddress.h:936
Definition: IpAddress.h:42
Definition: IpAddress.h:192
const IPv6Address & getIPv6() const
Definition: IpAddress.h:420
std::string toString() const
const IPv4Address & getIPv4() const
Definition: IpAddress.h:414
IPNetwork(const IPNetwork &other)
Definition: IpAddress.h:774
IPAddress getLowestAddress() const
Definition: IpAddress.h:901
bool isValid() const
Definition: IpAddress.h:392
bool includes(const IPNetwork &network) const
Definition: IpAddress.h:971
std::string getNetmask() const
Definition: IpAddress.h:535
std::string toString() const
Definition: IpAddress.h:387
IPv6Address()
Definition: IpAddress.h:198
bool operator!=(const IPv4Address &rhs) const
Definition: IpAddress.h:126
void copyTo(uint8_t *arr) const
Definition: IpAddress.h:269
IPAddress()
Definition: IpAddress.h:342
IPNetwork & operator=(const IPv6Network &other)
Definition: IpAddress.h:852
bool operator==(const IPv6Address &rhs) const
Definition: IpAddress.h:240
std::string getNetmask() const
Definition: IpAddress.h:638
bool operator<(const IPv4Address &rhs) const
Definition: IpAddress.h:110
bool includes(const IPAddress &address) const
Definition: IpAddress.h:945
std::string toString() const
IPv4Address getNetworkPrefix() const
Definition: IpAddress.h:540
bool operator!=(const IPv6Address &rhs) const
Definition: IpAddress.h:254
IPAddress(const IPv6Address &addr)
Definition: IpAddress.h:354
IPv6Address(const uint8_t bytes[16])
Definition: IpAddress.h:204
bool isValid() const
Definition: IpAddress.h:233
bool isIPv4Network() const
Definition: IpAddress.h:928
IPNetwork(const IPAddress &address, const std::string &netmask)
Definition: IpAddress.h:732
IPAddress & operator=(const IPv4Address &addr)
Definition: IpAddress.h:471
Definition: IpAddress.h:321
std::string toString() const
std::string toString() const
Definition: IpAddress.h:997
bool matchNetwork(const IPv4Network &network) const
IPAddress getNetworkPrefix() const
Definition: IpAddress.h:892
bool matchSubnet(const IPv4Address &subnet, const std::string &subnetMask) const
Definition: IpAddress.h:593
Definition: IpAddress.h:490
Definition: IpAddress.h:332
IPAddress getHighestAddress() const
Definition: IpAddress.h:910
IPAddress(const IPv4Address &addr)
Definition: IpAddress.h:348
AddressType getType() const
Definition: IpAddress.h:381
std::string toString() const
uint32_t toInt() const
Definition: IpAddress.h:181