PcapPlusPlus  23.09
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 
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)
15 #else
16 #pragma message("WARNING: DEPRECATED feature is not implemented for this compiler")
17 #define PCPP_DEPRECATED
18 #endif
19 #endif
20 
22 
23 
28 namespace pcpp
29 {
30 
31  // forward declarations
32  class IPv4Network;
33  class IPv6Network;
34 
35  // The implementation of the classes is based on document N4771 "Working Draft, C++ Extensions for Networking"
36  // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/n4771.pdf
37 
43  {
44  public:
48  IPv4Address() { memset(m_Bytes, 0, sizeof(m_Bytes)); }
49 
54  IPv4Address(uint32_t addrAsInt) { memcpy(m_Bytes, &addrAsInt, sizeof(m_Bytes)); }
55 
60  IPv4Address(const uint8_t bytes[4]) { memcpy(m_Bytes, bytes, sizeof(m_Bytes)); }
61 
67  IPv4Address(const std::string& addrAsString);
68 
73  inline uint32_t toInt() const;
74 
78  const uint8_t* toBytes() const { return m_Bytes; }
79 
84  std::string toString() const;
85 
90  bool isMulticast() const;
91 
96  bool isValid() const { return toInt() != 0; }
97 
103  bool operator==(const IPv4Address& rhs) const { return toInt() == rhs.toInt(); }
104 
110  bool operator<(const IPv4Address& rhs) const
111  {
112  uint32_t intVal = toInt();
113  std::reverse((uint8_t*)(&intVal), (uint8_t*)(&intVal) + sizeof(intVal));
114 
115  uint32_t rhsIntVal = rhs.toInt();
116  std::reverse((uint8_t*)(&rhsIntVal), (uint8_t*)(&rhsIntVal) + sizeof(rhsIntVal));
117 
118  return intVal < rhsIntVal;
119  }
120 
126  bool operator!=(const IPv4Address& rhs) const { return !(*this == rhs); }
127 
133  bool matchNetwork(const IPv4Network& network) const;
134 
146  bool matchNetwork(const std::string& network) const;
147 
151  PCPP_DEPRECATED bool matchSubnet(const IPv4Address& subnet, const std::string& subnetMask) const;
152 
156  PCPP_DEPRECATED bool matchSubnet(const IPv4Address& subnet, const IPv4Address& subnetMask) const;
157 
163  static const IPv4Address Zero;
164 
172  static const IPv4Address MulticastRangeUpperBound;
173 
174  private:
175  uint8_t m_Bytes[4];
176  }; // class IPv4Address
177 
178 
179  // Implementation of inline methods
180 
181  uint32_t IPv4Address::toInt() const
182  {
183  uint32_t addr;
184  memcpy(&addr, m_Bytes, sizeof(m_Bytes));
185  return addr;
186  }
187 
193  {
194  public:
198  IPv6Address() { memset(m_Bytes, 0, sizeof(m_Bytes)); }
199 
204  IPv6Address(const uint8_t bytes[16]) { memcpy(m_Bytes, bytes, sizeof(m_Bytes)); }
205 
211  IPv6Address(const std::string& addrAsString);
212 
216  const uint8_t* toBytes() const { return m_Bytes; }
217 
222  std::string toString() const;
223 
228  bool isMulticast() const;
229 
233  bool isValid() const { return *this != Zero; }
234 
240  bool operator==(const IPv6Address& rhs) const { return memcmp(toBytes(), rhs.toBytes(), sizeof(m_Bytes)) == 0; }
241 
247  bool operator<(const IPv6Address& rhs) const { return memcmp(toBytes(), rhs.toBytes(), sizeof(m_Bytes)) < 0; }
248 
254  bool operator!=(const IPv6Address &rhs) const { return !(*this == rhs); }
255 
256 
262  void copyTo(uint8_t** arr, size_t& length) const;
263 
269  void copyTo(uint8_t* arr) const { memcpy(arr, m_Bytes, sizeof(m_Bytes)); }
270 
276  bool matchNetwork(const IPv6Network& network) const;
277 
291  bool matchNetwork(const std::string& network) const;
292 
296  PCPP_DEPRECATED bool matchSubnet(const IPv6Address& subnet, uint8_t prefixLength) const;
297 
303  static const IPv6Address Zero;
304 
311 
312  private:
313  uint8_t m_Bytes[16];
314  }; // class IPv6Address
315 
316 
321  class IPAddress
322  {
323  public:
328  {
336  IPv6AddressType
337  };
338 
342  IPAddress() : m_Type(IPv4AddressType) {}
343 
348  IPAddress(const IPv4Address& addr) : m_Type(IPv4AddressType), m_IPv4(addr) {}
349 
354  IPAddress(const IPv6Address& addr) : m_Type(IPv6AddressType), m_IPv6(addr) {}
355 
361  IPAddress(const std::string& addrAsString);
362 
368  inline IPAddress& operator=(const IPv4Address& addr);
369 
375  inline IPAddress& operator=(const IPv6Address& addr);
376 
381  AddressType getType() const { return static_cast<AddressType>(m_Type); }
382 
387  std::string toString() const { return (getType() == IPv4AddressType) ? m_IPv4.toString() : m_IPv6.toString(); }
388 
392  bool isValid() const { return (getType() == IPv4AddressType) ? m_IPv4.isValid() : m_IPv6.isValid(); }
393 
397  bool isIPv4() const { return getType() == IPv4AddressType; }
398 
402  bool isIPv6() const { return getType() == IPv6AddressType; }
403 
408  bool isMulticast() const { return (getType() == IPv4AddressType) ? m_IPv4.isMulticast() : m_IPv6.isMulticast(); }
409 
414  const IPv4Address& getIPv4() const { return m_IPv4; }
415 
420  const IPv6Address& getIPv6() const { return m_IPv6; }
421 
427  inline bool operator==(const IPAddress& rhs) const;
428 
434  inline bool operator<(const IPAddress& rhs) const;
435 
441  bool operator!=(const IPAddress& rhs) const { return !(*this == rhs); }
442 
443  private:
444  uint8_t m_Type;
445  IPv4Address m_IPv4;
446  IPv6Address m_IPv6;
447  };
448 
449 
450  // implementation of inline methods
451 
452  bool IPAddress::operator==(const IPAddress& rhs) const
453  {
454  if (isIPv4())
455  return rhs.isIPv4() ? (m_IPv4 == rhs.m_IPv4) : false;
456 
457  return rhs.isIPv6() ? m_IPv6 == rhs.m_IPv6 : false;
458  }
459 
460  bool IPAddress::operator<(const IPAddress& rhs) const
461  {
462  if(isIPv4())
463  {
464  // treat IPv4 as less than IPv6
465  // If current obj is IPv4 and other is IPv6 return true
466  return rhs.isIPv4() ? (m_IPv4 < rhs.m_IPv4) : true;
467  }
468  return rhs.isIPv6() ? m_IPv6 < rhs.m_IPv6 : false;
469  }
470 
472  {
473  m_Type = IPv4AddressType;
474  m_IPv4 = addr;
475  return *this;
476  }
477 
479  {
480  m_Type = IPv6AddressType;
481  m_IPv6 = addr;
482  return *this;
483  }
484 
485 
491  {
492  public:
501  IPv4Network(const IPv4Address& address, uint8_t prefixLen);
502 
513  IPv4Network(const IPv4Address& address, const std::string& netmask);
514 
525  IPv4Network(const std::string& addressAndNetmask);
526 
530  uint8_t getPrefixLen() const;
531 
535  std::string getNetmask() const { return IPv4Address(m_Mask).toString(); }
536 
540  IPv4Address getNetworkPrefix() const { return IPv4Address(m_NetworkPrefix); }
541 
546  IPv4Address getLowestAddress() const;
547 
552  IPv4Address getHighestAddress() const;
553 
558  uint64_t getTotalAddressCount() const;
559 
564  bool includes(const IPv4Address& address) const;
565 
571  bool includes(const IPv4Network& network) const;
572 
577  std::string toString() const;
578 
579  private:
580  uint32_t m_NetworkPrefix;
581  uint32_t m_Mask;
582 
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);
586  };
587 
588 
594  {
595  public:
604  IPv6Network(const IPv6Address& address, uint8_t prefixLen);
605 
616  IPv6Network(const IPv6Address& address, const std::string& netmask);
617 
628  IPv6Network(const std::string& addressAndNetmask);
629 
633  uint8_t getPrefixLen() const;
634 
638  std::string getNetmask() const { return IPv6Address(m_Mask).toString(); }
639 
643  IPv6Address getNetworkPrefix() const { return IPv6Address(m_NetworkPrefix); }
644 
649  IPv6Address getLowestAddress() const;
650 
655  IPv6Address getHighestAddress() const;
656 
661  uint64_t getTotalAddressCount() const;
662 
667  bool includes(const IPv6Address& address) const;
668 
674  bool includes(const IPv6Network& network) const;
675 
680  std::string toString() const;
681 
682  private:
683  uint8_t m_NetworkPrefix[16];
684  uint8_t m_Mask[16];
685 
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);
689  };
690 
691 
696  class IPNetwork
697  {
698  public:
707  IPNetwork(const IPAddress& address, uint8_t prefixLen)
708  {
709  if (address.isIPv4())
710  {
711  m_IPv4Network = new IPv4Network(address.getIPv4(), prefixLen);
712  m_IPv6Network = nullptr;
713  }
714  else
715  {
716  m_IPv6Network = new IPv6Network(address.getIPv6(), prefixLen);
717  m_IPv4Network = nullptr;
718  }
719  }
720 
732  IPNetwork(const IPAddress& address, const std::string& netmask)
733  {
734  if (address.isIPv4())
735  {
736  m_IPv4Network = new IPv4Network(address.getIPv4(), netmask);
737  m_IPv6Network = nullptr;
738  }
739  else
740  {
741  m_IPv6Network = new IPv6Network(address.getIPv6(), netmask);
742  m_IPv4Network = nullptr;
743  }
744  }
745 
756  IPNetwork(const std::string& addressAndNetmask)
757  {
758  try
759  {
760  m_IPv4Network = new IPv4Network(addressAndNetmask);
761  m_IPv6Network = nullptr;
762  }
763  catch (const std::invalid_argument&)
764  {
765  m_IPv6Network = new IPv6Network(addressAndNetmask);
766  m_IPv4Network = nullptr;
767  }
768  }
769 
774  IPNetwork(const IPNetwork& other)
775  {
776  m_IPv4Network = nullptr;
777  m_IPv6Network = nullptr;
778 
779  if (other.m_IPv4Network)
780  {
781  m_IPv4Network = new IPv4Network(*other.m_IPv4Network);
782  }
783 
784  if (other.m_IPv6Network)
785  {
786  m_IPv6Network = new IPv6Network(*other.m_IPv6Network);
787  }
788  }
789 
794  {
795  if (m_IPv4Network)
796  {
797  delete m_IPv4Network;
798  }
799 
800  if (m_IPv6Network)
801  {
802  delete m_IPv6Network;
803  }
804  }
805 
812  {
813  if (other.isIPv4Network())
814  {
815  return this->operator=(*other.m_IPv4Network);
816  }
817  else
818  {
819  return this->operator=(*other.m_IPv6Network);
820  }
821  }
822 
829  {
830  if (m_IPv4Network)
831  {
832  delete m_IPv4Network;
833  m_IPv4Network = nullptr;
834  }
835 
836  if (m_IPv6Network)
837  {
838  delete m_IPv6Network;
839  m_IPv6Network = nullptr;
840  }
841 
842  m_IPv4Network = new IPv4Network(other);
843 
844  return *this;
845  }
846 
853  {
854  if (m_IPv4Network)
855  {
856  delete m_IPv4Network;
857  m_IPv4Network = nullptr;
858  }
859 
860  if (m_IPv6Network)
861  {
862  delete m_IPv6Network;
863  m_IPv6Network = nullptr;
864  }
865 
866  m_IPv6Network = new IPv6Network(other);
867 
868  return *this;
869  }
870 
875  uint8_t getPrefixLen() const
876  {
877  return (m_IPv4Network != nullptr ? m_IPv4Network->getPrefixLen() : m_IPv6Network->getPrefixLen());
878  }
879 
883  std::string getNetmask() const
884  {
885  return (m_IPv4Network != nullptr ? m_IPv4Network->getNetmask() : m_IPv6Network->getNetmask());
886  }
887 
893  {
894  return (m_IPv4Network != nullptr ? IPAddress(m_IPv4Network->getNetworkPrefix()) : IPAddress(m_IPv6Network->getNetworkPrefix()));
895  }
896 
902  {
903  return (m_IPv4Network != nullptr ? IPAddress(m_IPv4Network->getLowestAddress()) : IPAddress(m_IPv6Network->getLowestAddress()));
904  }
905 
911  {
912  return (m_IPv4Network != nullptr ? IPAddress(m_IPv4Network->getHighestAddress()) : IPAddress(m_IPv6Network->getHighestAddress()));
913  }
914 
920  uint64_t getTotalAddressCount() const
921  {
922  return (m_IPv4Network != nullptr ? m_IPv4Network->getTotalAddressCount() : m_IPv6Network->getTotalAddressCount());
923  }
924 
928  bool isIPv4Network() const
929  {
930  return m_IPv4Network != nullptr;
931  }
932 
936  bool isIPv6Network() const
937  {
938  return m_IPv6Network != nullptr;
939  }
940 
945  bool includes(const IPAddress& address) const
946  {
947  if (m_IPv4Network != nullptr)
948  {
949  if (address.isIPv6())
950  {
951  return false;
952  }
953 
954  return m_IPv4Network->includes(address.getIPv4());
955  }
956  else
957  {
958  if (address.isIPv4())
959  {
960  return false;
961  }
962 
963  return m_IPv6Network->includes(address.getIPv6());
964  }
965  }
966 
971  bool includes(const IPNetwork& network) const
972  {
973  if (m_IPv4Network != nullptr)
974  {
975  if (network.isIPv6Network())
976  {
977  return false;
978  }
979 
980  return m_IPv4Network->includes(*network.m_IPv4Network);
981  }
982  else
983  {
984  if (network.isIPv4Network())
985  {
986  return false;
987  }
988 
989  return m_IPv6Network->includes(*network.m_IPv6Network);
990  }
991  }
992 
997  std::string toString() const
998  {
999  return (m_IPv4Network != nullptr ? m_IPv4Network->toString() : m_IPv6Network->toString());
1000  }
1001 
1002  private:
1003  IPv4Network* m_IPv4Network;
1004  IPv6Network* m_IPv6Network;
1005  };
1006 } // namespace pcpp
1007 
1008 inline std::ostream& operator<<(std::ostream& os, const pcpp::IPv4Address& ipv4Address)
1009 {
1010  os << ipv4Address.toString();
1011  return os;
1012 }
1013 
1014 inline std::ostream& operator<<(std::ostream& os, const pcpp::IPv6Address& ipv6Address)
1015 {
1016  os << ipv6Address.toString();
1017  return os;
1018 }
1019 
1020 inline std::ostream& operator<<(std::ostream& os, const pcpp::IPAddress& ipAddress)
1021 {
1022  os << ipAddress.toString();
1023  return os;
1024 }
1025 
1026 inline std::ostream& operator<<(std::ostream& os, const pcpp::IPv4Network& network)
1027 {
1028  os << network.toString();
1029  return os;
1030 }
1031 
1032 inline std::ostream& operator<<(std::ostream& os, const pcpp::IPv6Network& network)
1033 {
1034  os << network.toString();
1035  return os;
1036 }
1037 
1038 inline std::ostream& operator<<(std::ostream& os, const pcpp::IPNetwork& network)
1039 {
1040  os << network.toString();
1041  return os;
1042 }
1043 
1044 #endif /* PCAPPP_IPADDRESS */
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
bool isMulticast() const
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