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  {
251  IPv6AddressType
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 */
The main namespace for the PcapPlusPlus lib.
const uint8_t * toBytes() const
Definition: IpAddress.h:164
IPv4Address()
Definition: IpAddress.h:32
IPv4Address(const uint8_t bytes[4])
Definition: IpAddress.h:44
AddressType
Definition: IpAddress.h:242
bool isIPv4() const
Definition: IpAddress.h:310
bool isValid() const
Definition: IpAddress.h:74
static const IPv4Address Zero
Definition: IpAddress.h:116
bool operator==(const IPv4Address &rhs) const
Definition: IpAddress.h:79
const uint8_t * toBytes() const
Definition: IpAddress.h:62
bool operator<(const IPAddress &rhs) const
Definition: IpAddress.h:361
bool operator==(const IPAddress &rhs) const
Definition: IpAddress.h:353
bool operator!=(const IPAddress &rhs) const
Definition: IpAddress.h:342
bool operator<(const IPv6Address &rhs) const
Definition: IpAddress.h:185
static const IPv6Address Zero
Definition: IpAddress.h:221
IPv4Address(uint32_t addrAsInt)
Definition: IpAddress.h:38
bool isIPv6() const
Definition: IpAddress.h:315
Definition: IpAddress.h:26
Definition: IpAddress.h:140
const IPv6Address & getIPv6() const
Definition: IpAddress.h:327
const IPv4Address & getIPv4() const
Definition: IpAddress.h:321
bool isValid() const
Definition: IpAddress.h:305
std::string toString() const
Definition: IpAddress.h:300
IPv6Address()
Definition: IpAddress.h:146
bool operator!=(const IPv4Address &rhs) const
Definition: IpAddress.h:89
void copyTo(uint8_t *arr) const
Definition: IpAddress.h:205
IPAddress()
Definition: IpAddress.h:257
bool operator==(const IPv6Address &rhs) const
Definition: IpAddress.h:180
bool operator<(const IPv4Address &rhs) const
Definition: IpAddress.h:84
bool operator!=(const IPv6Address &rhs) const
Definition: IpAddress.h:190
IPAddress(const IPv6Address &addr)
Definition: IpAddress.h:269
IPv6Address(const uint8_t bytes[16])
Definition: IpAddress.h:152
bool isValid() const
Definition: IpAddress.h:175
IPAddress & operator=(const IPv4Address &addr)
Definition: IpAddress.h:372
Definition: IpAddress.h:236
std::string toString() const
bool matchSubnet(const IPv4Address &subnet, const std::string &subnetMask) const
Definition: IpAddress.h:247
IPAddress(const IPv4Address &addr)
Definition: IpAddress.h:263
AddressType getType() const
Definition: IpAddress.h:294
uint32_t toInt() const
Definition: IpAddress.h:125