PcapPlusPlus
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
9 
11 
12 struct in_addr;
13 struct in6_addr;
14 
19 namespace pcpp
20 {
21 
27  class IPAddress
28  {
29  protected:
30  bool m_IsValid;
31  char m_AddressAsString[MAX_ADDR_STRING_LEN];
32 
33  // protected c'tor
34  IPAddress() : m_IsValid(false) {}
35  public:
36 #if __cplusplus > 199711L
37  typedef std::unique_ptr<IPAddress> Ptr_t;
38 #else
39  typedef std::auto_ptr<IPAddress> Ptr_t;
40 #endif
41 
45  enum AddressType {
54  };
55 
56  virtual ~IPAddress();
57 
62  virtual AddressType getType() const = 0;
63 
68  std::string toString() const { return std::string(m_AddressAsString); }
69 
75  bool isValid() const { return m_IsValid; }
76 
81  virtual IPAddress* clone() const = 0;
82 
89  bool equals(const IPAddress* other);
90 
97  static Ptr_t fromString(char* addressAsString);
98 
105  static Ptr_t fromString(std::string addressAsString);
106  };
107 
113  class IPv4Address : public IPAddress
114  {
115  private:
116  in_addr* m_pInAddr;
117  void init(char* addressAsString);
118  public:
124  IPv4Address(uint32_t addressAsInt);
125 
131  IPv4Address(char* addressAsString);
132 
138  IPv4Address(std::string addressAsString);
139 
144  IPv4Address(in_addr* inAddr);
145 
146  ~IPv4Address();
147 
151  IPv4Address(const IPv4Address& other);
152 
156  AddressType getType() const { return IPv4AddressType; }
157 
158  IPAddress* clone() const;
159 
164  uint32_t toInt() const;
165 
170  in_addr* toInAddr() { return m_pInAddr; }
171 
176  bool operator==(const IPv4Address& other) const { return toInt() == other.toInt(); }
177 
182  bool operator!=(const IPv4Address& other) const { return toInt() != other.toInt(); }
183 
187  IPv4Address& operator=(const IPv4Address& other);
188 
198  bool matchSubnet(const IPv4Address& subnet, const std::string& subnetMask);
199 
204  };
205 
206 
212  class IPv6Address : public IPAddress
213  {
214  private:
215  in6_addr* m_pInAddr;
216  void init(char* addressAsString);
217  public:
218  ~IPv6Address();
219 
225  IPv6Address(uint8_t* addressAsUintArr);
226 
232  IPv6Address(char* addressAsString);
233 
239  IPv6Address(std::string addressAsString);
240 
244  IPv6Address(const IPv6Address& other);
245 
249  AddressType getType() const { return IPv6AddressType; }
250 
251  IPAddress* clone() const;
252 
257  in6_addr* toIn6Addr() { return m_pInAddr; }
258 
264  void copyTo(uint8_t** arr, size_t& length);
265 
271  void copyTo(uint8_t* arr) const;
272 
277  bool operator==(const IPv6Address& other) const;
278 
283  bool operator!=(const IPv6Address& other);
284 
288  IPv6Address& operator=(const IPv6Address& other);
289 
295  };
296 
297 } // namespace pcpp
298 
299 #endif /* PCAPPP_IPADDRESS */
AddressType getType() const
Definition: IpAddress.h:249
bool operator!=(const IPv6Address &other)
IPv6Address(uint8_t *addressAsUintArr)
Definition: IpAddress.h:49
bool operator!=(const IPv4Address &other) const
Definition: IpAddress.h:182
static Ptr_t fromString(char *addressAsString)
uint32_t toInt() const
Definition: IpAddress.h:113
bool matchSubnet(const IPv4Address &subnet, const std::string &subnetMask)
virtual IPAddress * clone() const =0
IPv4Address & operator=(const IPv4Address &other)
bool isValid() const
Definition: IpAddress.h:75
static IPv6Address Zero
Definition: IpAddress.h:294
bool operator==(const IPv4Address &other) const
Definition: IpAddress.h:176
Definition: IpAddress.h:27
IPAddress * clone() const
in_addr * toInAddr()
Definition: IpAddress.h:170
Definition: IpAddress.h:212
Definition: IpAddress.h:53
static IPv4Address Zero
Definition: IpAddress.h:203
bool operator==(const IPv6Address &other) const
AddressType getType() const
Definition: IpAddress.h:156
IPv6Address & operator=(const IPv6Address &other)
virtual AddressType getType() const =0
IPAddress * clone() const
AddressType
Definition: IpAddress.h:45
std::string toString() const
Definition: IpAddress.h:68
The main namespace for the PcapPlusPlus lib.
in6_addr * toIn6Addr()
Definition: IpAddress.h:257
IPv4Address(uint32_t addressAsInt)
bool equals(const IPAddress *other)
void copyTo(uint8_t **arr, size_t &length)