PcapPlusPlus  20.08
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
pcpp::IPv6DnsResourceData::IPv6DnsResourceData
IPv6DnsResourceData(const std::string &addrAsString)
Definition: DnsResourceData.h:249
pcpp::IDnsResourceData::isTypeOf
bool isTypeOf() const
Definition: DnsResourceData.h:63
pcpp::IPv4DnsResourceData::toString
std::string toString() const
Definition: DnsResourceData.h:216
pcpp::IPv4Address
Definition: IpAddress.h:26
pcpp::IPv6Address
Definition: IpAddress.h:140
pcpp::MxDnsResourceData::MxData::preference
uint16_t preference
Definition: DnsResourceData.h:284
pcpp::IDnsResourceData::toString
virtual std::string toString() const =0
pcpp::IPv6DnsResourceData
Definition: DnsResourceData.h:225
pcpp::StringDnsResourceData::toByteArr
bool toByteArr(uint8_t *arr, size_t &arrLength, IDnsResource *dnsResource) const
pcpp::StringDnsResourceData::operator==
bool operator==(const StringDnsResourceData &other) const
Definition: DnsResourceData.h:163
pcpp::IDnsResourceData
Definition: DnsResourceData.h:40
pcpp::MxDnsResourceData::getMxData
MxData getMxData() const
Definition: DnsResourceData.h:323
pcpp::StringDnsResourceData
Definition: DnsResourceData.h:135
pcpp::IPv4Address::toString
std::string toString() const
pcpp::MxDnsResourceData::operator==
bool operator==(const MxDnsResourceData &other) const
pcpp::GenericDnsResourceData::operator==
bool operator==(const GenericDnsResourceData &other) const
pcpp::MxDnsResourceData::MxData::mailExchange
std::string mailExchange
Definition: DnsResourceData.h:286
pcpp::IDnsResource
Definition: DnsResource.h:30
pcpp::MxDnsResourceData::toString
std::string toString() const
pcpp::IDnsResourceData::castAs
IDnsResourceDataType * castAs()
Definition: DnsResourceData.h:71
pcpp::IDnsResourceData::toByteArr
virtual bool toByteArr(uint8_t *arr, size_t &arrLength, IDnsResource *dnsResource) const =0
DnsResource.h
pcpp::IPv6DnsResourceData::operator==
bool operator==(const IPv6DnsResourceData &other) const
Definition: DnsResourceData.h:256
pcpp::MxDnsResourceData::MxData
Definition: DnsResourceData.h:281
pcpp::IPv6DnsResourceData::toString
std::string toString() const
Definition: DnsResourceData.h:265
pcpp::DnsResourceDataPtr::isTypeOf
bool isTypeOf() const
Definition: DnsResourceData.h:119
pcpp::IPv4DnsResourceData::IPv4DnsResourceData
IPv4DnsResourceData(const uint8_t *dataPtr, size_t dataLen)
pcpp::IPv4DnsResourceData::operator==
bool operator==(const IPv4DnsResourceData &other) const
Definition: DnsResourceData.h:207
pcpp::IPv4DnsResourceData
Definition: DnsResourceData.h:176
pcpp::DnsResourceDataPtr::DnsResourceDataPtr
DnsResourceDataPtr(IDnsResourceData *ptr)
Definition: DnsResourceData.h:102
pcpp::IPv6DnsResourceData::getIpAddress
IPv6Address getIpAddress() const
Definition: DnsResourceData.h:261
pcpp::DnsResourceDataPtr::castAs
IDnsResourceDataType * castAs()
Definition: DnsResourceData.h:127
pcpp::MxDnsResourceData::toByteArr
bool toByteArr(uint8_t *arr, size_t &arrLength, IDnsResource *dnsResource) const
IpAddress.h
pcpp::IPv4DnsResourceData::IPv4DnsResourceData
IPv4DnsResourceData(const IPv4Address &addr)
Definition: DnsResourceData.h:194
pcpp
The main namespace for the PcapPlusPlus lib.
pcpp::IPv6DnsResourceData::toByteArr
bool toByteArr(uint8_t *arr, size_t &arrLength, IDnsResource *dnsResource) const
pcpp::GenericDnsResourceData::GenericDnsResourceData
GenericDnsResourceData(uint8_t *dataPtr, size_t dataLen)
pcpp::MxDnsResourceData
Definition: DnsResourceData.h:274
pcpp::IDnsResourceData::~IDnsResourceData
virtual ~IDnsResourceData()
Definition: DnsResourceData.h:55
pcpp::GenericDnsResourceData::toString
std::string toString() const
pcpp::MxDnsResourceData::setMxData
void setMxData(uint16_t preference, std::string mailExchange)
pcpp::MxDnsResourceData::MxDnsResourceData
MxDnsResourceData(uint8_t *dataPtr, size_t dataLen, IDnsResource *dnsResource)
pcpp::StringDnsResourceData::toString
std::string toString() const
Definition: DnsResourceData.h:167
pcpp::GenericDnsResourceData::toByteArr
bool toByteArr(uint8_t *arr, size_t &arrLength, IDnsResource *dnsResource) const
pcpp::IPv4DnsResourceData::toByteArr
bool toByteArr(uint8_t *arr, size_t &arrLength, IDnsResource *dnsResource) const
pcpp::IPv4DnsResourceData::IPv4DnsResourceData
IPv4DnsResourceData(const std::string &addrAsString)
Definition: DnsResourceData.h:200
pcpp::StringDnsResourceData::StringDnsResourceData
StringDnsResourceData(const std::string &data)
Definition: DnsResourceData.h:152
pcpp::IPv6DnsResourceData::IPv6DnsResourceData
IPv6DnsResourceData(const uint8_t *dataPtr, size_t dataLen)
pcpp::GenericDnsResourceData
Definition: DnsResourceData.h:352
pcpp::IPv4DnsResourceData::getIpAddress
IPv4Address getIpAddress() const
Definition: DnsResourceData.h:212
pcpp::IPv6Address::toString
std::string toString() const
pcpp::DnsResourceDataPtr
Definition: DnsResourceData.h:94
pcpp::IPv6DnsResourceData::IPv6DnsResourceData
IPv6DnsResourceData(const IPv6Address &addr)
Definition: DnsResourceData.h:243