PcapPlusPlus
DnsResourceData.h
Go to the documentation of this file.
1 #ifndef PACKETPP_DNS_RESOURCE_DATA
2 #define PACKETPP_DNS_RESOURCE_DATA
3 
4 #include "DnsResource.h"
5 #include "IpAddress.h"
6 #include <string>
7 #include <stdint.h>
8 
10 
15 namespace pcpp
16 {
17 
18  #if __cplusplus > 199711L
19  #define PCPP_SMART_PTR(T) std::unique_ptr<T>
20  #else
21  #define PCPP_SMART_PTR(T) std::auto_ptr<T>
22  #endif
23 
24  // forward declarations
25  class IDnsResource;
26 
35  {
36  protected:
37 
38  // unimplemented private copy c'tor
39  IDnsResourceData(const IDnsResourceData &other);
40  IDnsResourceData() { }
41 
42  size_t decodeName(const char* encodedName, char* result, IDnsResource* dnsResource);
43  void encodeName(const std::string& decodedName, char* result, size_t& resultLen, IDnsResource* dnsResource);
44 
45  public:
49  virtual ~IDnsResourceData() { }
50 
56  template <class IDnsResourceDataType>
57  bool isTypeOf() const { return dynamic_cast<const IDnsResourceDataType*>(this) != NULL; }
58 
64  template <class IDnsResourceDataType>
65  IDnsResourceDataType* castAs() { return dynamic_cast<IDnsResourceDataType*>(this);}
66 
70  virtual std::string toString() = 0;
71 
80  virtual bool toByteArr(uint8_t* arr, size_t &arrLength, IDnsResource* dnsResource) = 0;
81  };
82 
83 
88  class DnsResourceDataPtr : public PCPP_SMART_PTR(IDnsResourceData)
89  {
90  public:
91 
96  DnsResourceDataPtr(IDnsResourceData* ptr) : PCPP_SMART_PTR(IDnsResourceData)(ptr) {}
97 
98 #if __cplusplus <= 199711L
99  DnsResourceDataPtr(const DnsResourceDataPtr& other) : PCPP_SMART_PTR(IDnsResourceData)((DnsResourceDataPtr&)other) {}
100 #endif
101 
107  template <class IDnsResourceDataType>
108  bool isTypeOf() const { return get()->isTypeOf<IDnsResourceDataType>(); }
109 
115  template <class IDnsResourceDataType>
116  IDnsResourceDataType* castAs() { return get()->castAs<IDnsResourceDataType>();}
117 
118  };
119 
120 
126  {
127  private:
128  std::string m_Data;
129 
130  public:
131 
142  StringDnsResourceData(const std::string& data) { m_Data = data; }
143 
144  StringDnsResourceData(const uint8_t* dataPtr, size_t dataLen, IDnsResource* dnsResource);
145 
147 
153  bool operator==(const StringDnsResourceData& other) const { return m_Data == other.m_Data; }
154 
155  // implement abstract methods
156 
157  std::string toString() { return m_Data; }
158  bool toByteArr(uint8_t* arr, size_t &arrLength, IDnsResource* dnsResource);
159  };
160 
161 
167  {
168  private:
169  IPv4Address m_Data;
170 
171  public:
172 
178  IPv4DnsResourceData(const uint8_t* dataPtr, size_t dataLen);
179 
184  IPv4DnsResourceData(const IPv4Address& addr) : m_Data(addr) {}
185 
190  IPv4DnsResourceData(const std::string& addrAsString) : m_Data(addrAsString) {}
191 
197  bool operator==(const IPv4DnsResourceData& other) const { return m_Data == other.m_Data; }
198 
202  IPv4Address getIpAddress() { return m_Data; }
203 
204  // implement abstract methods
205 
206  std::string toString() { return m_Data.toString(); }
207  bool toByteArr(uint8_t* arr, size_t &arrLength, IDnsResource* dnsResource);
208  };
209 
210 
216  {
217  private:
218  IPv6Address m_Data;
219 
220  public:
221 
227  IPv6DnsResourceData(const uint8_t* dataPtr, size_t dataLen);
228 
233  IPv6DnsResourceData(const IPv6Address& addr) : m_Data(addr) {}
234 
239  IPv6DnsResourceData(const std::string& addrAsString) : m_Data(addrAsString) {}
240 
246  bool operator==(const IPv6DnsResourceData& other) const { return m_Data == other.m_Data; }
247 
251  IPv6Address getIpAddress() { return m_Data; }
252 
253  // implement abstract methods
254 
255  std::string toString() { return m_Data.toString(); }
256  bool toByteArr(uint8_t* arr, size_t &arrLength, IDnsResource* dnsResource);
257  };
258 
259 
265  {
266  public:
267 
271  struct MxData
272  {
274  uint16_t preference;
276  std::string mailExchange;
277  };
278 
285  MxDnsResourceData(uint8_t* dataPtr, size_t dataLen, IDnsResource* dnsResource);
286 
298  MxDnsResourceData(const uint16_t& preference, const std::string& mailExchange);
299 
300  ~MxDnsResourceData() {}
301 
308  bool operator==(const MxDnsResourceData& other) const;
309 
313  MxData getMxData() { return m_Data; }
314 
320  void setMxData(uint16_t preference, std::string mailExchange);
321 
322  // implement abstract methods
323 
328  std::string toString();
329 
330  bool toByteArr(uint8_t* arr, size_t &arrLength, IDnsResource* dnsResource);
331 
332  private:
333  MxData m_Data;
334  };
335 
336 
343  {
344  private:
345  uint8_t* m_Data;
346  size_t m_DataLen;
347 
348  public:
349 
356  GenericDnsResourceData(uint8_t* dataPtr, size_t dataLen);
357 
362  GenericDnsResourceData(const std::string& dataAsHexString);
363 
364  ~GenericDnsResourceData() { if (m_Data != NULL) delete [] m_Data; }
365 
366  GenericDnsResourceData& operator=(const GenericDnsResourceData& other);
367 
373  bool operator==(const GenericDnsResourceData& other) const;
374 
375  // implement abstract methods
376 
377  std::string toString();
378  bool toByteArr(uint8_t* arr, size_t &arrLength, IDnsResource* dnsResource);
379  };
380 
381 }
382 
383 #endif // PACKETPP_DNS_RESOURCE_DATA
Definition: DnsResourceData.h:264
bool isTypeOf() const
Definition: DnsResourceData.h:108
Definition: DnsResourceData.h:125
bool toByteArr(uint8_t *arr, size_t &arrLength, IDnsResource *dnsResource)
IPv4DnsResourceData(const std::string &addrAsString)
Definition: DnsResourceData.h:190
IDnsResourceDataType * castAs()
Definition: DnsResourceData.h:116
std::string toString()
Definition: DnsResourceData.h:342
IPv4DnsResourceData(const IPv4Address &addr)
Definition: DnsResourceData.h:184
virtual std::string toString()=0
Definition: IpAddress.h:113
IPv4DnsResourceData(const uint8_t *dataPtr, size_t dataLen)
std::string toString()
Definition: DnsResourceData.h:157
Definition: DnsResourceData.h:215
Definition: DnsResourceData.h:88
void setMxData(uint16_t preference, std::string mailExchange)
bool operator==(const GenericDnsResourceData &other) const
StringDnsResourceData(const std::string &data)
Definition: DnsResourceData.h:142
GenericDnsResourceData(uint8_t *dataPtr, size_t dataLen)
Definition: DnsResource.h:30
Definition: DnsResourceData.h:271
Definition: DnsResourceData.h:166
bool operator==(const StringDnsResourceData &other) const
Definition: DnsResourceData.h:153
virtual ~IDnsResourceData()
Definition: DnsResourceData.h:49
bool operator==(const IPv4DnsResourceData &other) const
Definition: DnsResourceData.h:197
IPv6DnsResourceData(const uint8_t *dataPtr, size_t dataLen)
DnsResourceDataPtr(IDnsResourceData *ptr)
Definition: DnsResourceData.h:96
virtual bool toByteArr(uint8_t *arr, size_t &arrLength, IDnsResource *dnsResource)=0
bool toByteArr(uint8_t *arr, size_t &arrLength, IDnsResource *dnsResource)
MxData getMxData()
Definition: DnsResourceData.h:313
std::string mailExchange
Definition: DnsResourceData.h:276
bool operator==(const IPv6DnsResourceData &other) const
Definition: DnsResourceData.h:246
std::string toString()
Definition: DnsResourceData.h:255
IPv6DnsResourceData(const IPv6Address &addr)
Definition: DnsResourceData.h:233
uint16_t preference
Definition: DnsResourceData.h:274
bool toByteArr(uint8_t *arr, size_t &arrLength, IDnsResource *dnsResource)
IPv4Address getIpAddress()
Definition: DnsResourceData.h:202
std::string toString()
Definition: DnsResourceData.h:206
Definition: IpAddress.h:212
IPv6Address getIpAddress()
Definition: DnsResourceData.h:251
IPv6DnsResourceData(const std::string &addrAsString)
Definition: DnsResourceData.h:239
MxDnsResourceData(uint8_t *dataPtr, size_t dataLen, IDnsResource *dnsResource)
bool operator==(const MxDnsResourceData &other) const
bool toByteArr(uint8_t *arr, size_t &arrLength, IDnsResource *dnsResource)
std::string toString() const
Definition: IpAddress.h:68
IDnsResourceDataType * castAs()
Definition: DnsResourceData.h:65
The main namespace for the PcapPlusPlus lib.
Definition: DnsResourceData.h:34
bool toByteArr(uint8_t *arr, size_t &arrLength, IDnsResource *dnsResource)
bool isTypeOf() const
Definition: DnsResourceData.h:57