PcapPlusPlus  22.11
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 #include <ostream>
9 
11 
12 
17 namespace pcpp
18 {
19 
20  // The implementation of the classes is based on document N4771 "Working Draft, C++ Extensions for Networking"
21  // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/n4771.pdf
22 
28  {
29  public:
33  IPv4Address() { memset(m_Bytes, 0, sizeof(m_Bytes)); }
34 
39  IPv4Address(uint32_t addrAsInt) { memcpy(m_Bytes, &addrAsInt, sizeof(m_Bytes)); }
40 
45  IPv4Address(const uint8_t bytes[4]) { memcpy(m_Bytes, bytes, sizeof(m_Bytes)); }
46 
52  IPv4Address(const std::string& addrAsString);
53 
58  inline uint32_t toInt() const;
59 
63  const uint8_t* toBytes() const { return m_Bytes; }
64 
69  std::string toString() const;
70 
75  bool isMulticast() const;
76 
81  bool isValid() const { return toInt() != 0; }
82 
88  bool operator==(const IPv4Address& rhs) const { return toInt() == rhs.toInt(); }
89 
95  bool operator<(const IPv4Address& rhs) const
96  {
97  uint32_t intVal = toInt();
98  std::reverse((uint8_t*)(&intVal), (uint8_t*)(&intVal) + sizeof(intVal));
99 
100  uint32_t rhsIntVal = rhs.toInt();
101  std::reverse((uint8_t*)(&rhsIntVal), (uint8_t*)(&rhsIntVal) + sizeof(rhsIntVal));
102 
103  return intVal < rhsIntVal;
104  }
105 
111  bool operator!=(const IPv4Address& rhs) const { return !(*this == rhs); }
112 
121  bool matchSubnet(const IPv4Address& subnet, const std::string& subnetMask) const;
122 
131  bool matchSubnet(const IPv4Address& subnet, const IPv4Address& subnetMask) const;
132 
138  static const IPv4Address Zero;
139 
147  static const IPv4Address MulticastRangeUpperBound;
148 
149  private:
150  uint8_t m_Bytes[4];
151  }; // class IPv4Address
152 
153 
154  // Implementation of inline methods
155 
156  uint32_t IPv4Address::toInt() const
157  {
158  uint32_t addr;
159  memcpy(&addr, m_Bytes, sizeof(m_Bytes));
160  return addr;
161  }
162 
168  {
169  public:
173  IPv6Address() { memset(m_Bytes, 0, sizeof(m_Bytes)); }
174 
179  IPv6Address(const uint8_t bytes[16]) { memcpy(m_Bytes, bytes, sizeof(m_Bytes)); }
180 
186  IPv6Address(const std::string& addrAsString);
187 
191  const uint8_t* toBytes() const { return m_Bytes; }
192 
197  std::string toString() const;
198 
203  bool isMulticast() const;
204 
208  bool isValid() const { return *this != Zero; }
209 
215  bool operator==(const IPv6Address& rhs) const { return memcmp(toBytes(), rhs.toBytes(), sizeof(m_Bytes)) == 0; }
216 
222  bool operator<(const IPv6Address& rhs) const { return memcmp(toBytes(), rhs.toBytes(), sizeof(m_Bytes)) < 0; }
223 
229  bool operator!=(const IPv6Address &rhs) const { return !(*this == rhs); }
230 
231 
237  void copyTo(uint8_t** arr, size_t& length) const;
238 
244  void copyTo(uint8_t* arr) const { memcpy(arr, m_Bytes, sizeof(m_Bytes)); }
245 
253  bool matchSubnet(const IPv6Address& subnet, uint8_t prefixLength) const;
254 
260  static const IPv6Address Zero;
261 
268 
269  private:
270  uint8_t m_Bytes[16];
271  }; // class IPv6Address
272 
273 
275  // * @class IPAddress
276  // * The class is a version-independent representation for an IP address
277  // */
278  class IPAddress
279  {
280  public:
285  {
293  IPv6AddressType
294  };
295 
299  IPAddress() : m_Type(IPv4AddressType) {}
300 
305  IPAddress(const IPv4Address& addr) : m_Type(IPv4AddressType), m_IPv4(addr) {}
306 
311  IPAddress(const IPv6Address& addr) : m_Type(IPv6AddressType), m_IPv6(addr) {}
312 
318  IPAddress(const std::string& addrAsString);
319 
325  inline IPAddress& operator=(const IPv4Address& addr);
326 
332  inline IPAddress& operator=(const IPv6Address& addr);
333 
338  AddressType getType() const { return static_cast<AddressType>(m_Type); }
339 
344  std::string toString() const { return (getType() == IPv4AddressType) ? m_IPv4.toString() : m_IPv6.toString(); }
345 
349  bool isValid() const { return (getType() == IPv4AddressType) ? m_IPv4.isValid() : m_IPv6.isValid(); }
350 
354  bool isIPv4() const { return getType() == IPv4AddressType; }
355 
359  bool isIPv6() const { return getType() == IPv6AddressType; }
360 
365  bool isMulticast() const { return (getType() == IPv4AddressType) ? m_IPv4.isMulticast() : m_IPv6.isMulticast(); }
366 
371  const IPv4Address& getIPv4() const { return m_IPv4; }
372 
377  const IPv6Address& getIPv6() const { return m_IPv6; }
378 
384  inline bool operator==(const IPAddress& rhs) const;
385 
391  inline bool operator<(const IPAddress& rhs) const;
392 
398  bool operator!=(const IPAddress& rhs) const { return !(*this == rhs); }
399 
400  private:
401  uint8_t m_Type;
402  IPv4Address m_IPv4;
403  IPv6Address m_IPv6;
404  };
405 
406 
407  // implementation of inline methods
408 
409  bool IPAddress::operator==(const IPAddress& rhs) const
410  {
411  if (isIPv4())
412  return rhs.isIPv4() ? (m_IPv4 == rhs.m_IPv4) : false;
413 
414  return rhs.isIPv6() ? m_IPv6 == rhs.m_IPv6 : false;
415  }
416 
417  bool IPAddress::operator<(const IPAddress& rhs) const
418  {
419  if(isIPv4())
420  {
421  // treat IPv4 as less than IPv6
422  // If current obj is IPv4 and other is IPv6 return true
423  return rhs.isIPv4() ? (m_IPv4 < rhs.m_IPv4) : true;
424  }
425  return rhs.isIPv6() ? m_IPv6 < rhs.m_IPv6 : false;
426  }
427 
429  {
430  m_Type = IPv4AddressType;
431  m_IPv4 = addr;
432  return *this;
433  }
434 
436  {
437  m_Type = IPv6AddressType;
438  m_IPv6 = addr;
439  return *this;
440  }
441 
442 } // namespace pcpp
443 
444 inline std::ostream& operator<<(std::ostream& os, const pcpp::IPv4Address& ipv4Address)
445 {
446  os << ipv4Address.toString();
447  return os;
448 }
449 
450 inline std::ostream& operator<<(std::ostream& os, const pcpp::IPv6Address& ipv6Address)
451 {
452  os << ipv6Address.toString();
453  return os;
454 }
455 
456 inline std::ostream& operator<<(std::ostream& os, const pcpp::IPAddress& ipAddress)
457 {
458  os << ipAddress.toString();
459  return os;
460 }
461 
462 #endif /* PCAPPP_IPADDRESS */
The main namespace for the PcapPlusPlus lib.
const uint8_t * toBytes() const
Definition: IpAddress.h:191
IPv4Address()
Definition: IpAddress.h:33
IPv4Address(const uint8_t bytes[4])
Definition: IpAddress.h:45
static const IPv6Address MulticastRangeLowerBound
Definition: IpAddress.h:267
AddressType
Definition: IpAddress.h:284
bool isIPv4() const
Definition: IpAddress.h:354
bool isValid() const
Definition: IpAddress.h:81
static const IPv4Address Zero
Definition: IpAddress.h:138
bool operator==(const IPv4Address &rhs) const
Definition: IpAddress.h:88
const uint8_t * toBytes() const
Definition: IpAddress.h:63
bool operator<(const IPAddress &rhs) const
Definition: IpAddress.h:417
bool operator==(const IPAddress &rhs) const
Definition: IpAddress.h:409
static const IPv4Address MulticastRangeLowerBound
Definition: IpAddress.h:146
bool operator!=(const IPAddress &rhs) const
Definition: IpAddress.h:398
bool operator<(const IPv6Address &rhs) const
Definition: IpAddress.h:222
static const IPv6Address Zero
Definition: IpAddress.h:260
IPv4Address(uint32_t addrAsInt)
Definition: IpAddress.h:39
bool isMulticast() const
Definition: IpAddress.h:365
bool isIPv6() const
Definition: IpAddress.h:359
Definition: IpAddress.h:27
Definition: IpAddress.h:167
const IPv6Address & getIPv6() const
Definition: IpAddress.h:377
std::string toString() const
const IPv4Address & getIPv4() const
Definition: IpAddress.h:371
bool isValid() const
Definition: IpAddress.h:349
std::string toString() const
Definition: IpAddress.h:344
IPv6Address()
Definition: IpAddress.h:173
bool operator!=(const IPv4Address &rhs) const
Definition: IpAddress.h:111
void copyTo(uint8_t *arr) const
Definition: IpAddress.h:244
IPAddress()
Definition: IpAddress.h:299
bool operator==(const IPv6Address &rhs) const
Definition: IpAddress.h:215
bool operator<(const IPv4Address &rhs) const
Definition: IpAddress.h:95
bool operator!=(const IPv6Address &rhs) const
Definition: IpAddress.h:229
IPAddress(const IPv6Address &addr)
Definition: IpAddress.h:311
IPv6Address(const uint8_t bytes[16])
Definition: IpAddress.h:179
bool isValid() const
Definition: IpAddress.h:208
bool isMulticast() const
IPAddress & operator=(const IPv4Address &addr)
Definition: IpAddress.h:428
Definition: IpAddress.h:278
std::string toString() const
bool matchSubnet(const IPv4Address &subnet, const std::string &subnetMask) const
Definition: IpAddress.h:289
IPAddress(const IPv4Address &addr)
Definition: IpAddress.h:305
AddressType getType() const
Definition: IpAddress.h:338
uint32_t toInt() const
Definition: IpAddress.h:156