PcapPlusPlus  20.08
IpAddress.h
Go to the documentation of this file.
1 #ifndef PCAPPP_IP_ADDRESSES
2 #define PCAPPP_IP_ADDRESSES
3 
4 #include <stdint.h>
5 #include <string.h>
6 #include <string>
7 
8 
10 
11 
16 namespace pcpp
17 {
18 
19  // The implementation of the classes is based on document N4771 "Working Draft, C++ Extensions for Networking"
20  // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/n4771.pdf
21 
27  {
28  public:
32  IPv4Address() { memset(m_Bytes, 0, sizeof(m_Bytes)); }
33 
38  IPv4Address(uint32_t addrAsInt) { memcpy(m_Bytes, &addrAsInt, sizeof(m_Bytes)); }
39 
44  IPv4Address(const uint8_t bytes[4]) { memcpy(m_Bytes, bytes, sizeof(m_Bytes)); }
45 
51  IPv4Address(const std::string& addrAsString);
52 
57  inline uint32_t toInt() const;
58 
62  const uint8_t* toBytes() const { return m_Bytes; }
63 
68  std::string toString() const;
69 
74  bool isValid() const { return toInt() != 0; }
75 
79  bool operator==(const IPv4Address& rhs) const { return toInt() == rhs.toInt(); }
80 
84  bool operator<(const IPv4Address& rhs) const { return toInt() < rhs.toInt(); }
85 
89  bool operator!=(const IPv4Address& rhs) const { return !(*this == rhs); }
90 
99  bool matchSubnet(const IPv4Address& subnet, const std::string& subnetMask) const;
100 
109  bool matchSubnet(const IPv4Address& subnet, const IPv4Address& subnetMask) const;
110 
116  static const IPv4Address Zero;
117 
118  private:
119  uint8_t m_Bytes[4];
120  }; // class IPv4Address
121 
122 
123  // Implementation of inline methods
124 
125  uint32_t IPv4Address::toInt() const
126  {
127  uint32_t addr;
128  memcpy(&addr, m_Bytes, sizeof(m_Bytes));
129  return addr;
130  }
131 
132 
133 
134 
135 
141  {
142  public:
146  IPv6Address() { memset(m_Bytes, 0, sizeof(m_Bytes)); }
147 
152  IPv6Address(const uint8_t bytes[16]) { memcpy(m_Bytes, bytes, sizeof(m_Bytes)); }
153 
159  IPv6Address(const std::string& addrAsString);
160 
164  const uint8_t* toBytes() const { return m_Bytes; }
165 
170  std::string toString() const;
171 
175  bool isValid() const { return *this != Zero; }
176 
180  bool operator==(const IPv6Address& rhs) const { return memcmp(toBytes(), rhs.toBytes(), sizeof(m_Bytes)) == 0; }
181 
185  bool operator<(const IPv6Address& rhs) const { return memcmp(toBytes(), rhs.toBytes(), sizeof(m_Bytes)) < 0; }
186 
190  bool operator!=(const IPv6Address &rhs) const { return !(*this == rhs); }
191 
192 
198  void copyTo(uint8_t** arr, size_t& length) const;
199 
205  void copyTo(uint8_t* arr) const { memcpy(arr, m_Bytes, sizeof(m_Bytes)); }
206 
214  bool matchSubnet(const IPv6Address& subnet, uint8_t prefixLength) const;
215 
221  static const IPv6Address Zero;
222 
223  private:
224  uint8_t m_Bytes[16];
225  }; // class IPv6Address
226 
227 
228 
229 
230 
231 
233  // * @class IPAddress
234  // * The class is a version-independent representation for an IP address
235  // */
236  class IPAddress
237  {
238  public:
243  {
252  };
253 
257  IPAddress() : m_Type(IPv4AddressType) {}
258 
263  IPAddress(const IPv4Address& addr) : m_Type(IPv4AddressType), m_IPv4(addr) {}
264 
269  IPAddress(const IPv6Address& addr) : m_Type(IPv6AddressType), m_IPv6(addr) {}
270 
276  IPAddress(const std::string& addrAsString);
277 
282  inline IPAddress& operator=(const IPv4Address& addr);
283 
288  inline IPAddress& operator=(const IPv6Address& addr);
289 
294  AddressType getType() const { return static_cast<AddressType>(m_Type); }
295 
300  std::string toString() const { return (getType() == IPv4AddressType) ? m_IPv4.toString() : m_IPv6.toString(); }
301 
305  bool isValid() const { return (getType() == IPv4AddressType) ? m_IPv4.isValid() : m_IPv6.isValid(); }
306 
310  bool isIPv4() const { return getType() == IPv4AddressType; }
311 
315  bool isIPv6() const { return getType() == IPv6AddressType; }
316 
321  const IPv4Address& getIPv4() const { return m_IPv4; }
322 
327  const IPv6Address& getIPv6() const { return m_IPv6; }
328 
332  inline bool operator==(const IPAddress& rhs) const;
333 
337  inline bool operator<(const IPAddress& rhs) const;
338 
342  bool operator!=(const IPAddress& rhs) const { return !(*this == rhs); }
343 
344  private:
345  uint8_t m_Type;
346  IPv4Address m_IPv4;
347  IPv6Address m_IPv6;
348  };
349 
350 
351  // implementation of inline methods
352 
353  bool IPAddress::operator==(const IPAddress& rhs) const
354  {
355  if (isIPv4())
356  return rhs.isIPv4() ? (m_IPv4 == rhs.m_IPv4) : false;
357 
358  return rhs.isIPv6() ? m_IPv6 == rhs.m_IPv6 : false;
359  }
360 
361  bool IPAddress::operator<(const IPAddress& rhs) const
362  {
363  if(isIPv4())
364  {
365  /* treat IPv4 as less than IPv6
366  If current obj is IPv4 and other is IPv6 return true */
367  return rhs.isIPv4() ? (m_IPv4 < rhs.m_IPv4) : true;
368  }
369  return rhs.isIPv6() ? m_IPv6 < rhs.m_IPv6 : false;
370  }
371 
373  {
374  m_Type = IPv4AddressType;
375  m_IPv4 = addr;
376  return *this;
377  }
378 
380  {
381  m_Type = IPv6AddressType;
382  m_IPv6 = addr;
383  return *this;
384  }
385 
386 } // namespace pcpp
387 
388 #endif /* PCAPPP_IPADDRESS */
pcpp::IPv6Address::copyTo
void copyTo(uint8_t *arr) const
Definition: IpAddress.h:205
pcpp::IPAddress::IPv4AddressType
@ IPv4AddressType
Definition: IpAddress.h:247
pcpp::IPv4Address
Definition: IpAddress.h:26
pcpp::IPv6Address
Definition: IpAddress.h:140
pcpp::IPAddress::IPAddress
IPAddress(const IPv4Address &addr)
Definition: IpAddress.h:263
pcpp::IPAddress::IPv6AddressType
@ IPv6AddressType
Definition: IpAddress.h:251
pcpp::IPv6Address::copyTo
void copyTo(uint8_t **arr, size_t &length) const
pcpp::IPv4Address::toString
std::string toString() const
pcpp::IPv4Address::operator!=
bool operator!=(const IPv4Address &rhs) const
Definition: IpAddress.h:89
pcpp::IPv6Address::operator==
bool operator==(const IPv6Address &rhs) const
Definition: IpAddress.h:180
pcpp::IPAddress
‍**
Definition: IpAddress.h:236
pcpp::IPv6Address::IPv6Address
IPv6Address(const uint8_t bytes[16])
Definition: IpAddress.h:152
pcpp::IPv4Address::matchSubnet
bool matchSubnet(const IPv4Address &subnet, const std::string &subnetMask) const
pcpp::IPv4Address::operator<
bool operator<(const IPv4Address &rhs) const
Definition: IpAddress.h:84
pcpp::IPv4Address::IPv4Address
IPv4Address()
Definition: IpAddress.h:32
pcpp::IPv6Address::operator!=
bool operator!=(const IPv6Address &rhs) const
Definition: IpAddress.h:190
pcpp::IPv6Address::isValid
bool isValid() const
Definition: IpAddress.h:175
pcpp::IPAddress::operator!=
bool operator!=(const IPAddress &rhs) const
Definition: IpAddress.h:342
pcpp::IPAddress::isIPv4
bool isIPv4() const
Definition: IpAddress.h:310
pcpp::IPAddress::operator<
bool operator<(const IPAddress &rhs) const
Definition: IpAddress.h:361
pcpp::IPAddress::isIPv6
bool isIPv6() const
Definition: IpAddress.h:315
pcpp::IPAddress::IPAddress
IPAddress()
Definition: IpAddress.h:257
pcpp::IPAddress::operator==
bool operator==(const IPAddress &rhs) const
Definition: IpAddress.h:353
pcpp::IPAddress::toString
std::string toString() const
Definition: IpAddress.h:300
pcpp::IPv4Address::toInt
uint32_t toInt() const
Definition: IpAddress.h:125
pcpp::IPv6Address::toBytes
const uint8_t * toBytes() const
Definition: IpAddress.h:164
pcpp::IPAddress::isValid
bool isValid() const
Definition: IpAddress.h:305
pcpp::IPv4Address::isValid
bool isValid() const
Definition: IpAddress.h:74
pcpp::IPv4Address::Zero
static const IPv4Address Zero
Definition: IpAddress.h:116
pcpp
The main namespace for the PcapPlusPlus lib.
pcpp::IPv4Address::IPv4Address
IPv4Address(const uint8_t bytes[4])
Definition: IpAddress.h:44
pcpp::IPAddress::getIPv6
const IPv6Address & getIPv6() const
Definition: IpAddress.h:327
pcpp::IPAddress::operator=
IPAddress & operator=(const IPv4Address &addr)
Definition: IpAddress.h:372
pcpp::IPv6Address::operator<
bool operator<(const IPv6Address &rhs) const
Definition: IpAddress.h:185
pcpp::IPv6Address::matchSubnet
bool matchSubnet(const IPv6Address &subnet, uint8_t prefixLength) const
pcpp::IPv6Address::Zero
static const IPv6Address Zero
Definition: IpAddress.h:221
pcpp::IPAddress::getIPv4
const IPv4Address & getIPv4() const
Definition: IpAddress.h:321
pcpp::IPAddress::AddressType
AddressType
Definition: IpAddress.h:242
pcpp::IPv4Address::operator==
bool operator==(const IPv4Address &rhs) const
Definition: IpAddress.h:79
pcpp::IPv4Address::toBytes
const uint8_t * toBytes() const
Definition: IpAddress.h:62
pcpp::IPv4Address::IPv4Address
IPv4Address(uint32_t addrAsInt)
Definition: IpAddress.h:38
pcpp::IPAddress::IPAddress
IPAddress(const IPv6Address &addr)
Definition: IpAddress.h:269
pcpp::IPv6Address::toString
std::string toString() const
pcpp::IPAddress::getType
AddressType getType() const
Definition: IpAddress.h:294
pcpp::IPv6Address::IPv6Address
IPv6Address()
Definition: IpAddress.h:146