PcapPlusPlus  21.05
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 <memory>
7 #include <string>
8 #include <stdint.h>
9 
11 
16 namespace pcpp
17 {
18 
19  //Visual studio has always been stupid about returning something useful for __cplusplus
20  //Only recently was this fixed - and even then it requires a specific hack to the command line during build
21  //Its easier/more consistent to test _MSC_VER in VS
22  //https://docs.microsoft.com/en-us/cpp/build/reference/zc-cplusplus?view=vs-2017
23 
24  #if __cplusplus > 199711L || _MSC_VER >= 1800 //Maybe this can be 1600 for VS2010
25  #define PCPP_SMART_PTR(T) std::unique_ptr<T>
26  #else
27  #define PCPP_SMART_PTR(T) std::auto_ptr<T>
28  #endif
29 
30  // forward declarations
31  class IDnsResource;
32 
41  {
42  protected:
43 
44  // unimplemented private copy c'tor
45  IDnsResourceData(const IDnsResourceData& other);
46  IDnsResourceData() { }
47 
48  size_t decodeName(const char* encodedName, char* result, IDnsResource* dnsResource) const;
49  void encodeName(const std::string& decodedName, char* result, size_t& resultLen, IDnsResource* dnsResource) const;
50 
51  public:
55  virtual ~IDnsResourceData() { }
56 
62  template <class IDnsResourceDataType>
63  bool isTypeOf() const { return dynamic_cast<const IDnsResourceDataType*>(this) != NULL; }
64 
70  template <class IDnsResourceDataType>
71  IDnsResourceDataType* castAs() { return dynamic_cast<IDnsResourceDataType*>(this); }
72 
76  virtual std::string toString() const = 0;
77 
86  virtual bool toByteArr(uint8_t* arr, size_t& arrLength, IDnsResource* dnsResource) const = 0;
87  };
88 
89 
94  class DnsResourceDataPtr : public PCPP_SMART_PTR(IDnsResourceData)
95  {
96  public:
97 
102  DnsResourceDataPtr(IDnsResourceData* ptr) : PCPP_SMART_PTR(IDnsResourceData)(ptr) {}
103 
104  //Visual studio has always been stupid about returning something useful for __cplusplus
105  //Only recently was this fixed - and even then it requires a specific hack to the command line during build
106  //Its easier/more consistent to test _MSC_VER in VS
107  //https://docs.microsoft.com/en-us/cpp/build/reference/zc-cplusplus?view=vs-2017
108 
109 #if __cplusplus <= 199711L && _MSC_VER < 1800 //Maybe this can be 1600 for VS2010
110  DnsResourceDataPtr(const DnsResourceDataPtr& other) : PCPP_SMART_PTR(IDnsResourceData)((DnsResourceDataPtr&)other) {}
111 #endif
112 
118  template <class IDnsResourceDataType>
119  bool isTypeOf() const { return get()->isTypeOf<IDnsResourceDataType>(); }
120 
126  template <class IDnsResourceDataType>
127  IDnsResourceDataType* castAs() { return get()->castAs<IDnsResourceDataType>();}
128  };
129 
130 
136  {
137  private:
138  std::string m_Data;
139 
140  public:
141 
152  StringDnsResourceData(const std::string& data) { m_Data = data; }
153 
154  StringDnsResourceData(const uint8_t* dataPtr, size_t dataLen, IDnsResource* dnsResource);
155 
157 
163  bool operator==(const StringDnsResourceData& other) const { return m_Data == other.m_Data; }
164 
165  // implement abstract methods
166 
167  std::string toString() const { return m_Data; }
168  bool toByteArr(uint8_t* arr, size_t& arrLength, IDnsResource* dnsResource) const;
169  };
170 
171 
177  {
178  private:
179  IPv4Address m_Data;
180 
181  public:
182 
188  IPv4DnsResourceData(const uint8_t* dataPtr, size_t dataLen);
189 
194  IPv4DnsResourceData(const IPv4Address& addr) : m_Data(addr) {}
195 
200  IPv4DnsResourceData(const std::string& addrAsString) : m_Data(addrAsString) {}
201 
207  bool operator==(const IPv4DnsResourceData& other) const { return m_Data == other.m_Data; }
208 
212  IPv4Address getIpAddress() const { return m_Data; }
213 
214  // implement abstract methods
215 
216  std::string toString() const { return m_Data.toString(); }
217  bool toByteArr(uint8_t* arr, size_t& arrLength, IDnsResource* dnsResource) const;
218  };
219 
220 
226  {
227  private:
228  IPv6Address m_Data;
229 
230  public:
231 
237  IPv6DnsResourceData(const uint8_t* dataPtr, size_t dataLen);
238 
243  IPv6DnsResourceData(const IPv6Address& addr) : m_Data(addr) {}
244 
249  IPv6DnsResourceData(const std::string& addrAsString) : m_Data(addrAsString) {}
250 
256  bool operator==(const IPv6DnsResourceData& other) const { return m_Data == other.m_Data; }
257 
261  IPv6Address getIpAddress() const { return m_Data; }
262 
263  // implement abstract methods
264 
265  std::string toString() const { return m_Data.toString(); }
266  bool toByteArr(uint8_t* arr, size_t& arrLength, IDnsResource* dnsResource) const;
267  };
268 
269 
275  {
276  public:
277 
281  struct MxData
282  {
284  uint16_t preference;
286  std::string mailExchange;
287  };
288 
295  MxDnsResourceData(uint8_t* dataPtr, size_t dataLen, IDnsResource* dnsResource);
296 
308  MxDnsResourceData(const uint16_t& preference, const std::string& mailExchange);
309 
310  ~MxDnsResourceData() {}
311 
318  bool operator==(const MxDnsResourceData& other) const;
319 
323  MxData getMxData() const { return m_Data; }
324 
330  void setMxData(uint16_t preference, std::string mailExchange);
331 
332  // implement abstract methods
333 
338  std::string toString() const;
339 
340  bool toByteArr(uint8_t* arr, size_t& arrLength, IDnsResource* dnsResource) const;
341 
342  private:
343  MxData m_Data;
344  };
345 
346 
353  {
354  private:
355  uint8_t* m_Data;
356  size_t m_DataLen;
357 
358  public:
359 
366  GenericDnsResourceData(uint8_t* dataPtr, size_t dataLen);
367 
372  GenericDnsResourceData(const std::string& dataAsHexString);
373 
379 
380  ~GenericDnsResourceData() { if (m_Data != NULL) delete [] m_Data; }
381 
382  GenericDnsResourceData& operator=(const GenericDnsResourceData& other);
383 
389  bool operator==(const GenericDnsResourceData& other) const;
390 
391  // implement abstract methods
392 
393  std::string toString() const;
394  bool toByteArr(uint8_t* arr, size_t& arrLength, IDnsResource* dnsResource) const;
395  };
396 
397 }
398 
399 #endif // PACKETPP_DNS_RESOURCE_DATA
The main namespace for the PcapPlusPlus lib.
IPv4DnsResourceData(const std::string &addrAsString)
Definition: DnsResourceData.h:200
Definition: DnsResourceData.h:176
bool isTypeOf() const
Definition: DnsResourceData.h:63
StringDnsResourceData(const std::string &data)
Definition: DnsResourceData.h:152
MxData getMxData() const
Definition: DnsResourceData.h:323
DnsResourceDataPtr(IDnsResourceData *ptr)
Definition: DnsResourceData.h:102
Definition: DnsResourceData.h:40
virtual bool toByteArr(uint8_t *arr, size_t &arrLength, IDnsResource *dnsResource) const =0
Definition: DnsResourceData.h:135
std::string toString() const
Definition: DnsResourceData.h:265
virtual std::string toString() const =0
IDnsResourceDataType * castAs()
Definition: DnsResourceData.h:127
IPv6DnsResourceData(const std::string &addrAsString)
Definition: DnsResourceData.h:249
Definition: DnsResourceData.h:274
IDnsResourceDataType * castAs()
Definition: DnsResourceData.h:71
IPv4DnsResourceData(const IPv4Address &addr)
Definition: DnsResourceData.h:194
bool operator==(const IPv4DnsResourceData &other) const
Definition: DnsResourceData.h:207
IPv6DnsResourceData(const IPv6Address &addr)
Definition: DnsResourceData.h:243
Definition: IpAddress.h:26
Definition: IpAddress.h:160
std::string toString() const
bool operator==(const IPv6DnsResourceData &other) const
Definition: DnsResourceData.h:256
Definition: DnsResourceData.h:281
uint16_t preference
Definition: DnsResourceData.h:284
Definition: DnsResourceData.h:352
virtual ~IDnsResourceData()
Definition: DnsResourceData.h:55
IPv4Address getIpAddress() const
Definition: DnsResourceData.h:212
Definition: DnsResourceData.h:225
std::string toString() const
Definition: DnsResourceData.h:167
std::string toString() const
Definition: DnsResourceData.h:216
std::string mailExchange
Definition: DnsResourceData.h:286
IPv6Address getIpAddress() const
Definition: DnsResourceData.h:261
std::string toString() const
Definition: DnsResource.h:30
bool operator==(const StringDnsResourceData &other) const
Definition: DnsResourceData.h:163
Definition: DnsResourceData.h:94
bool isTypeOf() const
Definition: DnsResourceData.h:119