PcapPlusPlus  19.12
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  //Visual studio has always been stupid about returning something useful for __cplusplus
19  //Only recently was this fixed - and even then it requires a specific hack to the command line during build
20  //Its easier/more consistent to test _MSC_VER in VS
21  //https://docs.microsoft.com/en-us/cpp/build/reference/zc-cplusplus?view=vs-2017
22 
23  #if __cplusplus > 199711L || _MSC_VER >= 1800 //Maybe this can be 1600 for VS2010
24  #define PCPP_SMART_PTR(T) std::unique_ptr<T>
25  #else
26  #define PCPP_SMART_PTR(T) std::auto_ptr<T>
27  #endif
28 
29  // forward declarations
30  class IDnsResource;
31 
40  {
41  protected:
42 
43  // unimplemented private copy c'tor
44  IDnsResourceData(const IDnsResourceData& other);
45  IDnsResourceData() { }
46 
47  size_t decodeName(const char* encodedName, char* result, IDnsResource* dnsResource) const;
48  void encodeName(const std::string& decodedName, char* result, size_t& resultLen, IDnsResource* dnsResource) const;
49 
50  public:
54  virtual ~IDnsResourceData() { }
55 
61  template <class IDnsResourceDataType>
62  bool isTypeOf() const { return dynamic_cast<const IDnsResourceDataType*>(this) != NULL; }
63 
69  template <class IDnsResourceDataType>
70  IDnsResourceDataType* castAs() { return dynamic_cast<IDnsResourceDataType*>(this); }
71 
75  virtual std::string toString() const = 0;
76 
85  virtual bool toByteArr(uint8_t* arr, size_t& arrLength, IDnsResource* dnsResource) const = 0;
86  };
87 
88 
93  class DnsResourceDataPtr : public PCPP_SMART_PTR(IDnsResourceData)
94  {
95  public:
96 
101  DnsResourceDataPtr(IDnsResourceData* ptr) : PCPP_SMART_PTR(IDnsResourceData)(ptr) {}
102 
103  //Visual studio has always been stupid about returning something useful for __cplusplus
104  //Only recently was this fixed - and even then it requires a specific hack to the command line during build
105  //Its easier/more consistent to test _MSC_VER in VS
106  //https://docs.microsoft.com/en-us/cpp/build/reference/zc-cplusplus?view=vs-2017
107 
108 #if __cplusplus <= 199711L && _MSC_VER < 1800 //Maybe this can be 1600 for VS2010
109  DnsResourceDataPtr(const DnsResourceDataPtr& other) : PCPP_SMART_PTR(IDnsResourceData)((DnsResourceDataPtr&)other) {}
110 #endif
111 
117  template <class IDnsResourceDataType>
118  bool isTypeOf() const { return get()->isTypeOf<IDnsResourceDataType>(); }
119 
125  template <class IDnsResourceDataType>
126  IDnsResourceDataType* castAs() { return get()->castAs<IDnsResourceDataType>();}
127  };
128 
129 
135  {
136  private:
137  std::string m_Data;
138 
139  public:
140 
151  StringDnsResourceData(const std::string& data) { m_Data = data; }
152 
153  StringDnsResourceData(const uint8_t* dataPtr, size_t dataLen, IDnsResource* dnsResource);
154 
156 
162  bool operator==(const StringDnsResourceData& other) const { return m_Data == other.m_Data; }
163 
164  // implement abstract methods
165 
166  std::string toString() const { return m_Data; }
167  bool toByteArr(uint8_t* arr, size_t& arrLength, IDnsResource* dnsResource) const;
168  };
169 
170 
176  {
177  private:
178  IPv4Address m_Data;
179 
180  public:
181 
187  IPv4DnsResourceData(const uint8_t* dataPtr, size_t dataLen);
188 
193  IPv4DnsResourceData(const IPv4Address& addr) : m_Data(addr) {}
194 
199  IPv4DnsResourceData(const std::string& addrAsString) : m_Data(addrAsString) {}
200 
206  bool operator==(const IPv4DnsResourceData& other) const { return m_Data == other.m_Data; }
207 
211  IPv4Address getIpAddress() const { return m_Data; }
212 
213  // implement abstract methods
214 
215  std::string toString() const { return m_Data.toString(); }
216  bool toByteArr(uint8_t* arr, size_t& arrLength, IDnsResource* dnsResource) const;
217  };
218 
219 
225  {
226  private:
227  IPv6Address m_Data;
228 
229  public:
230 
236  IPv6DnsResourceData(const uint8_t* dataPtr, size_t dataLen);
237 
242  IPv6DnsResourceData(const IPv6Address& addr) : m_Data(addr) {}
243 
248  IPv6DnsResourceData(const std::string& addrAsString) : m_Data(addrAsString) {}
249 
255  bool operator==(const IPv6DnsResourceData& other) const { return m_Data == other.m_Data; }
256 
260  IPv6Address getIpAddress() const { return m_Data; }
261 
262  // implement abstract methods
263 
264  std::string toString() const { return m_Data.toString(); }
265  bool toByteArr(uint8_t* arr, size_t& arrLength, IDnsResource* dnsResource) const;
266  };
267 
268 
274  {
275  public:
276 
280  struct MxData
281  {
283  uint16_t preference;
285  std::string mailExchange;
286  };
287 
294  MxDnsResourceData(uint8_t* dataPtr, size_t dataLen, IDnsResource* dnsResource);
295 
307  MxDnsResourceData(const uint16_t& preference, const std::string& mailExchange);
308 
309  ~MxDnsResourceData() {}
310 
317  bool operator==(const MxDnsResourceData& other) const;
318 
322  MxData getMxData() const { return m_Data; }
323 
329  void setMxData(uint16_t preference, std::string mailExchange);
330 
331  // implement abstract methods
332 
337  std::string toString() const;
338 
339  bool toByteArr(uint8_t* arr, size_t& arrLength, IDnsResource* dnsResource) const;
340 
341  private:
342  MxData m_Data;
343  };
344 
345 
352  {
353  private:
354  uint8_t* m_Data;
355  size_t m_DataLen;
356 
357  public:
358 
365  GenericDnsResourceData(uint8_t* dataPtr, size_t dataLen);
366 
371  GenericDnsResourceData(const std::string& dataAsHexString);
372 
373  ~GenericDnsResourceData() { if (m_Data != NULL) delete [] m_Data; }
374 
375  GenericDnsResourceData& operator=(const GenericDnsResourceData& other);
376 
382  bool operator==(const GenericDnsResourceData& other) const;
383 
384  // implement abstract methods
385 
386  std::string toString() const;
387  bool toByteArr(uint8_t* arr, size_t& arrLength, IDnsResource* dnsResource) const;
388  };
389 
390 }
391 
392 #endif // PACKETPP_DNS_RESOURCE_DATA
pcpp::IPv6DnsResourceData::IPv6DnsResourceData
IPv6DnsResourceData(const std::string &addrAsString)
Definition: DnsResourceData.h:248
pcpp::IDnsResourceData::isTypeOf
bool isTypeOf() const
Definition: DnsResourceData.h:62
pcpp::IPv4DnsResourceData::toString
std::string toString() const
Definition: DnsResourceData.h:215
pcpp::IPv4Address
Definition: IpAddress.h:119
pcpp::IPv6Address
Definition: IpAddress.h:229
pcpp::MxDnsResourceData::MxData::preference
uint16_t preference
Definition: DnsResourceData.h:283
pcpp::IDnsResourceData::toString
virtual std::string toString() const =0
pcpp::IPv6DnsResourceData
Definition: DnsResourceData.h:224
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:162
pcpp::IDnsResourceData
Definition: DnsResourceData.h:39
pcpp::MxDnsResourceData::getMxData
MxData getMxData() const
Definition: DnsResourceData.h:322
pcpp::StringDnsResourceData
Definition: DnsResourceData.h:134
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:285
pcpp::IDnsResource
Definition: DnsResource.h:30
pcpp::MxDnsResourceData::toString
std::string toString() const
pcpp::IDnsResourceData::castAs
IDnsResourceDataType * castAs()
Definition: DnsResourceData.h:70
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:255
pcpp::MxDnsResourceData::MxData
Definition: DnsResourceData.h:280
pcpp::IPv6DnsResourceData::toString
std::string toString() const
Definition: DnsResourceData.h:264
pcpp::DnsResourceDataPtr::isTypeOf
bool isTypeOf() const
Definition: DnsResourceData.h:118
pcpp::IPAddress::toString
std::string toString() const
Definition: IpAddress.h:74
pcpp::IPv4DnsResourceData::IPv4DnsResourceData
IPv4DnsResourceData(const uint8_t *dataPtr, size_t dataLen)
pcpp::IPv4DnsResourceData::operator==
bool operator==(const IPv4DnsResourceData &other) const
Definition: DnsResourceData.h:206
pcpp::IPv4DnsResourceData
Definition: DnsResourceData.h:175
pcpp::DnsResourceDataPtr::DnsResourceDataPtr
DnsResourceDataPtr(IDnsResourceData *ptr)
Definition: DnsResourceData.h:101
pcpp::IPv6DnsResourceData::getIpAddress
IPv6Address getIpAddress() const
Definition: DnsResourceData.h:260
pcpp::DnsResourceDataPtr::castAs
IDnsResourceDataType * castAs()
Definition: DnsResourceData.h:126
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:193
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:273
pcpp::IDnsResourceData::~IDnsResourceData
virtual ~IDnsResourceData()
Definition: DnsResourceData.h:54
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:166
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:199
pcpp::StringDnsResourceData::StringDnsResourceData
StringDnsResourceData(const std::string &data)
Definition: DnsResourceData.h:151
pcpp::IPv6DnsResourceData::IPv6DnsResourceData
IPv6DnsResourceData(const uint8_t *dataPtr, size_t dataLen)
pcpp::GenericDnsResourceData
Definition: DnsResourceData.h:351
pcpp::IPv4DnsResourceData::getIpAddress
IPv4Address getIpAddress() const
Definition: DnsResourceData.h:211
pcpp::DnsResourceDataPtr
Definition: DnsResourceData.h:93
pcpp::IPv6DnsResourceData::IPv6DnsResourceData
IPv6DnsResourceData(const IPv6Address &addr)
Definition: DnsResourceData.h:242