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() { return m_IsValid; }
76 
83  static Ptr_t fromString(char* addressAsString);
84 
91  static Ptr_t fromString(std::string addressAsString);
92  };
93 
99  class IPv4Address : public IPAddress
100  {
101  private:
102  in_addr* m_pInAddr;
103  void init(char* addressAsString);
104  public:
110  IPv4Address(uint32_t addressAsInt);
111 
117  IPv4Address(char* addressAsString);
118 
124  IPv4Address(std::string addressAsString);
125 
130  IPv4Address(in_addr* inAddr);
131 
132  ~IPv4Address();
133 
137  IPv4Address(const IPv4Address& other);
138 
142  AddressType getType() const { return IPv4AddressType; }
143 
148  uint32_t toInt() const;
149 
154  in_addr* toInAddr() { return m_pInAddr; }
155 
160  bool operator==(const IPv4Address& other) const { return toInt() == other.toInt(); }
161 
166  bool operator!=(const IPv4Address& other) const { return toInt() != other.toInt(); }
167 
171  IPv4Address& operator=(const IPv4Address& other);
172 
182  bool matchSubnet(const IPv4Address& subnet, const std::string& subnetMask);
183 
188  };
189 
190 
196  class IPv6Address : public IPAddress
197  {
198  private:
199  in6_addr* m_pInAddr;
200  void init(char* addressAsString);
201  public:
202  ~IPv6Address();
203 
209  IPv6Address(uint8_t* addressAsUintArr);
210 
216  IPv6Address(char* addressAsString);
217 
223  IPv6Address(std::string addressAsString);
224 
228  IPv6Address(const IPv6Address& other);
229 
233  AddressType getType() const { return IPv6AddressType; }
234 
239  in6_addr* toIn6Addr() { return m_pInAddr; }
240 
246  void copyTo(uint8_t** arr, size_t& length);
247 
253  void copyTo(uint8_t* arr) const;
254 
259  bool operator==(const IPv6Address& other);
260 
265  bool operator!=(const IPv6Address& other);
266 
270  IPv6Address& operator=(const IPv6Address& other);
271 
277  };
278 
279 } // namespace pcpp
280 
281 #endif /* PCAPPP_IPADDRESS */
uint32_t toInt() const
bool isValid()
Definition: IpAddress.h:75
void copyTo(uint8_t **arr, size_t &length)
Definition: IpAddress.h:196
std::string toString() const
Definition: IpAddress.h:68
bool operator==(const IPv4Address &other) const
Definition: IpAddress.h:160
Definition: IpAddress.h:49
Definition: IpAddress.h:99
virtual AddressType getType() const =0
IPv4Address & operator=(const IPv4Address &other)
bool operator==(const IPv6Address &other)
Definition: IpAddress.h:53
IPv4Address(uint32_t addressAsInt)
static IPv6Address Zero
Definition: IpAddress.h:276
IPv6Address & operator=(const IPv6Address &other)
in_addr * toInAddr()
Definition: IpAddress.h:154
static IPv4Address Zero
Definition: IpAddress.h:187
bool operator!=(const IPv6Address &other)
Definition: IpAddress.h:27
bool operator!=(const IPv4Address &other) const
Definition: IpAddress.h:166
AddressType getType() const
Definition: IpAddress.h:142
bool matchSubnet(const IPv4Address &subnet, const std::string &subnetMask)
The main namespace for the PcapPlusPlus lib.
AddressType getType() const
Definition: IpAddress.h:233
in6_addr * toIn6Addr()
Definition: IpAddress.h:239
IPv6Address(uint8_t *addressAsUintArr)
static Ptr_t fromString(char *addressAsString)
AddressType
Definition: IpAddress.h:45