PcapPlusPlus  19.12
IpAddress.h
Go to the documentation of this file.
1 #ifndef PCAPPP_IPADDRESS
2 #define PCAPPP_IPADDRESS
3 
4 #include <memory>
5 #include <stdint.h>
6 #include <string>
7 
8 #define MAX_ADDR_STRING_LEN 40 //xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx\0
9 #define MAX_IPV4_STRING_LEN 16 //xxx.xxx.xxx.xxx\0
10 
12 
13 struct in_addr;
14 struct in6_addr;
15 
20 namespace pcpp
21 {
22 
28  class IPAddress
29  {
30  protected:
31  bool m_IsValid;
32  char m_AddressAsString[MAX_ADDR_STRING_LEN];
33 
34  // protected c'tor
35  IPAddress() : m_IsValid(false) { m_AddressAsString[0] = '\0'; }
36  public:
37  //Visual studio has always been stupid about returning something useful for __cplusplus
38  //Only recently was this fixed - and even then it requires a specific hack to the command line during build
39  //Its easier/more consistent to test _MSC_VER in VS
40  //https://docs.microsoft.com/en-us/cpp/build/reference/zc-cplusplus?view=vs-2017
41 
42 #if __cplusplus > 199711L || _MSC_VER >= 1800 //Maybe this can be 1600 for VS2010
43  typedef std::unique_ptr<IPAddress> Ptr_t;
44 #else
45  typedef std::auto_ptr<IPAddress> Ptr_t;
46 #endif
47 
51  enum AddressType {
60  };
61 
62  virtual ~IPAddress() {}
63 
68  virtual AddressType getType() const = 0;
69 
74  std::string toString() const { return std::string(m_AddressAsString); }
75 
81  bool isValid() const { return m_IsValid; }
82 
87  virtual IPAddress* clone() const = 0;
88 
95  bool equals(const IPAddress* other) const;
96 
103  static Ptr_t fromString(char* addressAsString);
104 
111  static Ptr_t fromString(std::string addressAsString);
112  };
113 
119  class IPv4Address : public IPAddress
120  {
121  private:
122  in_addr* m_pInAddr;
123  void init(const char* addressAsString);
124  public:
130  IPv4Address(uint32_t addressAsInt);
131 
137  IPv4Address(const char* addressAsString);
138 
144  IPv4Address(std::string addressAsString);
145 
150  IPv4Address(in_addr* inAddr);
151 
152  ~IPv4Address();
153 
157  IPv4Address(const IPv4Address& other);
158 
162  AddressType getType() const { return IPv4AddressType; }
163 
164  IPAddress* clone() const;
165 
170  uint32_t toInt() const;
171 
176  in_addr* toInAddr() const { return m_pInAddr; }
177 
182  bool operator==(const IPv4Address& other) const { return toInt() == other.toInt(); }
183 
188  bool operator!=(const IPv4Address& other) const { return toInt() != other.toInt(); }
189 
193  IPv4Address& operator=(const IPv4Address& other);
194 
204  bool matchSubnet(const IPv4Address& subnet, const std::string& subnetMask) const;
205 
215  bool matchSubnet(const IPv4Address& subnet, const IPv4Address& subnetMask) const;
216 
221  };
222 
223 
229  class IPv6Address : public IPAddress
230  {
231  private:
232  in6_addr* m_pInAddr;
233  void init(char* addressAsString);
234  public:
235  ~IPv6Address();
236 
242  IPv6Address(uint8_t* addressAsUintArr);
243 
249  IPv6Address(char* addressAsString);
250 
256  IPv6Address(std::string addressAsString);
257 
261  IPv6Address(const IPv6Address& other);
262 
266  AddressType getType() const { return IPv6AddressType; }
267 
268  IPAddress* clone() const;
269 
274  in6_addr* toIn6Addr() const { return m_pInAddr; }
275 
281  void copyTo(uint8_t** arr, size_t& length) const;
282 
288  void copyTo(uint8_t* arr) const;
289 
294  bool operator==(const IPv6Address& other) const;
295 
300  bool operator!=(const IPv6Address& other) const;
301 
305  IPv6Address& operator=(const IPv6Address& other);
306 
312  };
313 
314 } // namespace pcpp
315 
316 #endif /* PCAPPP_IPADDRESS */
pcpp::IPAddress::IPv4AddressType
Definition: IpAddress.h:55
pcpp::IPv4Address
Definition: IpAddress.h:119
pcpp::IPv6Address
Definition: IpAddress.h:229
pcpp::IPAddress::equals
bool equals(const IPAddress *other) const
pcpp::IPAddress::IPv6AddressType
Definition: IpAddress.h:59
pcpp::IPv6Address::copyTo
void copyTo(uint8_t **arr, size_t &length) const
pcpp::IPv4Address::Zero
static IPv4Address Zero
Definition: IpAddress.h:220
pcpp::IPAddress
Definition: IpAddress.h:28
pcpp::IPv6Address::operator!=
bool operator!=(const IPv6Address &other) const
pcpp::IPv4Address::matchSubnet
bool matchSubnet(const IPv4Address &subnet, const std::string &subnetMask) const
pcpp::IPv4Address::IPv4Address
IPv4Address(uint32_t addressAsInt)
pcpp::IPv6Address::operator==
bool operator==(const IPv6Address &other) const
pcpp::IPv6Address::clone
IPAddress * clone() const
pcpp::IPv6Address::Zero
static IPv6Address Zero
Definition: IpAddress.h:311
pcpp::IPAddress::toString
std::string toString() const
Definition: IpAddress.h:74
pcpp::IPv4Address::toInt
uint32_t toInt() const
pcpp::IPv6Address::toIn6Addr
in6_addr * toIn6Addr() const
Definition: IpAddress.h:274
pcpp::IPAddress::getType
virtual AddressType getType() const =0
pcpp::IPv6Address::getType
AddressType getType() const
Definition: IpAddress.h:266
pcpp::IPv6Address::operator=
IPv6Address & operator=(const IPv6Address &other)
pcpp::IPAddress::isValid
bool isValid() const
Definition: IpAddress.h:81
pcpp
The main namespace for the PcapPlusPlus lib.
pcpp::IPv4Address::operator==
bool operator==(const IPv4Address &other) const
Definition: IpAddress.h:182
pcpp::IPv6Address::IPv6Address
IPv6Address(uint8_t *addressAsUintArr)
pcpp::IPAddress::fromString
static Ptr_t fromString(char *addressAsString)
pcpp::IPAddress::AddressType
AddressType
Definition: IpAddress.h:51
pcpp::IPv4Address::clone
IPAddress * clone() const
pcpp::IPv4Address::toInAddr
in_addr * toInAddr() const
Definition: IpAddress.h:176
pcpp::IPv4Address::getType
AddressType getType() const
Definition: IpAddress.h:162
pcpp::IPv4Address::operator=
IPv4Address & operator=(const IPv4Address &other)
pcpp::IPv4Address::operator!=
bool operator!=(const IPv4Address &other) const
Definition: IpAddress.h:188
pcpp::IPAddress::clone
virtual IPAddress * clone() const =0