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 #include <algorithm>
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 isMulticast() const;
75 
80  bool isValid() const { return toInt() != 0; }
81 
85  bool operator==(const IPv4Address& rhs) const { return toInt() == rhs.toInt(); }
86 
90  bool operator<(const IPv4Address& rhs) const
91  {
92  uint32_t intVal = toInt();
93  std::reverse((uint8_t*)(&intVal), (uint8_t*)(&intVal) + sizeof(intVal));
94 
95  uint32_t rhsIntVal = rhs.toInt();
96  std::reverse((uint8_t*)(&rhsIntVal), (uint8_t*)(&rhsIntVal) + sizeof(rhsIntVal));
97 
98  return intVal < rhsIntVal;
99  }
100 
104  bool operator!=(const IPv4Address& rhs) const { return !(*this == rhs); }
105 
114  bool matchSubnet(const IPv4Address& subnet, const std::string& subnetMask) const;
115 
124  bool matchSubnet(const IPv4Address& subnet, const IPv4Address& subnetMask) const;
125 
131  static const IPv4Address Zero;
132 
140  static const IPv4Address MulticastRangeUpperBound;
141 
142  private:
143  uint8_t m_Bytes[4];
144  }; // class IPv4Address
145 
146 
147  // Implementation of inline methods
148 
149  uint32_t IPv4Address::toInt() const
150  {
151  uint32_t addr;
152  memcpy(&addr, m_Bytes, sizeof(m_Bytes));
153  return addr;
154  }
155 
161  {
162  public:
166  IPv6Address() { memset(m_Bytes, 0, sizeof(m_Bytes)); }
167 
172  IPv6Address(const uint8_t bytes[16]) { memcpy(m_Bytes, bytes, sizeof(m_Bytes)); }
173 
179  IPv6Address(const std::string& addrAsString);
180 
184  const uint8_t* toBytes() const { return m_Bytes; }
185 
190  std::string toString() const;
191 
196  bool isMulticast() const;
197 
201  bool isValid() const { return *this != Zero; }
202 
206  bool operator==(const IPv6Address& rhs) const { return memcmp(toBytes(), rhs.toBytes(), sizeof(m_Bytes)) == 0; }
207 
211  bool operator<(const IPv6Address& rhs) const { return memcmp(toBytes(), rhs.toBytes(), sizeof(m_Bytes)) < 0; }
212 
216  bool operator!=(const IPv6Address &rhs) const { return !(*this == rhs); }
217 
218 
224  void copyTo(uint8_t** arr, size_t& length) const;
225 
231  void copyTo(uint8_t* arr) const { memcpy(arr, m_Bytes, sizeof(m_Bytes)); }
232 
240  bool matchSubnet(const IPv6Address& subnet, uint8_t prefixLength) const;
241 
247  static const IPv6Address Zero;
248 
255 
256  private:
257  uint8_t m_Bytes[16];
258  }; // class IPv6Address
259 
260 
262  // * @class IPAddress
263  // * The class is a version-independent representation for an IP address
264  // */
265  class IPAddress
266  {
267  public:
272  {
280  IPv6AddressType
281  };
282 
286  IPAddress() : m_Type(IPv4AddressType) {}
287 
292  IPAddress(const IPv4Address& addr) : m_Type(IPv4AddressType), m_IPv4(addr) {}
293 
298  IPAddress(const IPv6Address& addr) : m_Type(IPv6AddressType), m_IPv6(addr) {}
299 
305  IPAddress(const std::string& addrAsString);
306 
311  inline IPAddress& operator=(const IPv4Address& addr);
312 
317  inline IPAddress& operator=(const IPv6Address& addr);
318 
323  AddressType getType() const { return static_cast<AddressType>(m_Type); }
324 
329  std::string toString() const { return (getType() == IPv4AddressType) ? m_IPv4.toString() : m_IPv6.toString(); }
330 
334  bool isValid() const { return (getType() == IPv4AddressType) ? m_IPv4.isValid() : m_IPv6.isValid(); }
335 
339  bool isIPv4() const { return getType() == IPv4AddressType; }
340 
344  bool isIPv6() const { return getType() == IPv6AddressType; }
345 
350  bool isMulticast() const { return (getType() == IPv4AddressType) ? m_IPv4.isMulticast() : m_IPv6.isMulticast(); }
351 
356  const IPv4Address& getIPv4() const { return m_IPv4; }
357 
362  const IPv6Address& getIPv6() const { return m_IPv6; }
363 
367  inline bool operator==(const IPAddress& rhs) const;
368 
372  inline bool operator<(const IPAddress& rhs) const;
373 
377  bool operator!=(const IPAddress& rhs) const { return !(*this == rhs); }
378 
379  private:
380  uint8_t m_Type;
381  IPv4Address m_IPv4;
382  IPv6Address m_IPv6;
383  };
384 
385 
386  // implementation of inline methods
387 
388  bool IPAddress::operator==(const IPAddress& rhs) const
389  {
390  if (isIPv4())
391  return rhs.isIPv4() ? (m_IPv4 == rhs.m_IPv4) : false;
392 
393  return rhs.isIPv6() ? m_IPv6 == rhs.m_IPv6 : false;
394  }
395 
396  bool IPAddress::operator<(const IPAddress& rhs) const
397  {
398  if(isIPv4())
399  {
400  /* treat IPv4 as less than IPv6
401  If current obj is IPv4 and other is IPv6 return true */
402  return rhs.isIPv4() ? (m_IPv4 < rhs.m_IPv4) : true;
403  }
404  return rhs.isIPv6() ? m_IPv6 < rhs.m_IPv6 : false;
405  }
406 
408  {
409  m_Type = IPv4AddressType;
410  m_IPv4 = addr;
411  return *this;
412  }
413 
415  {
416  m_Type = IPv6AddressType;
417  m_IPv6 = addr;
418  return *this;
419  }
420 
421 } // namespace pcpp
422 
423 #endif /* PCAPPP_IPADDRESS */
The main namespace for the PcapPlusPlus lib.
const uint8_t * toBytes() const
Definition: IpAddress.h:184
IPv4Address()
Definition: IpAddress.h:32
IPv4Address(const uint8_t bytes[4])
Definition: IpAddress.h:44
static const IPv6Address MulticastRangeLowerBound
Definition: IpAddress.h:254
AddressType
Definition: IpAddress.h:271
bool isIPv4() const
Definition: IpAddress.h:339
bool isValid() const
Definition: IpAddress.h:80
static const IPv4Address Zero
Definition: IpAddress.h:131
bool operator==(const IPv4Address &rhs) const
Definition: IpAddress.h:85
const uint8_t * toBytes() const
Definition: IpAddress.h:62
bool operator<(const IPAddress &rhs) const
Definition: IpAddress.h:396
bool operator==(const IPAddress &rhs) const
Definition: IpAddress.h:388
static const IPv4Address MulticastRangeLowerBound
Definition: IpAddress.h:139
bool operator!=(const IPAddress &rhs) const
Definition: IpAddress.h:377
bool operator<(const IPv6Address &rhs) const
Definition: IpAddress.h:211
static const IPv6Address Zero
Definition: IpAddress.h:247
IPv4Address(uint32_t addrAsInt)
Definition: IpAddress.h:38
bool isMulticast() const
Definition: IpAddress.h:350
bool isIPv6() const
Definition: IpAddress.h:344
Definition: IpAddress.h:26
Definition: IpAddress.h:160
const IPv6Address & getIPv6() const
Definition: IpAddress.h:362
const IPv4Address & getIPv4() const
Definition: IpAddress.h:356
bool isValid() const
Definition: IpAddress.h:334
std::string toString() const
Definition: IpAddress.h:329
IPv6Address()
Definition: IpAddress.h:166
bool operator!=(const IPv4Address &rhs) const
Definition: IpAddress.h:104
void copyTo(uint8_t *arr) const
Definition: IpAddress.h:231
IPAddress()
Definition: IpAddress.h:286
bool operator==(const IPv6Address &rhs) const
Definition: IpAddress.h:206
bool operator<(const IPv4Address &rhs) const
Definition: IpAddress.h:90
bool operator!=(const IPv6Address &rhs) const
Definition: IpAddress.h:216
IPAddress(const IPv6Address &addr)
Definition: IpAddress.h:298
IPv6Address(const uint8_t bytes[16])
Definition: IpAddress.h:172
bool isValid() const
Definition: IpAddress.h:201
bool isMulticast() const
IPAddress & operator=(const IPv4Address &addr)
Definition: IpAddress.h:407
Definition: IpAddress.h:265
std::string toString() const
bool matchSubnet(const IPv4Address &subnet, const std::string &subnetMask) const
Definition: IpAddress.h:276
IPAddress(const IPv4Address &addr)
Definition: IpAddress.h:292
AddressType getType() const
Definition: IpAddress.h:323
uint32_t toInt() const
Definition: IpAddress.h:149