PcapPlusPlus  Next
DnsResourceData.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "DnsResource.h"
4 #include "IpAddress.h"
5 #include <memory>
6 #include <string>
7 #include <stdint.h>
8 
10 
15 namespace pcpp
16 {
17  // forward declarations
18  class IDnsResource;
19 
28  {
29  protected:
30  // unimplemented private copy c'tor
31  IDnsResourceData(const IDnsResourceData& other);
33  {}
34 
35  size_t decodeName(const char* encodedName, char* result, IDnsResource* dnsResource) const;
36  void encodeName(const std::string& decodedName, char* result, size_t& resultLen,
37  IDnsResource* dnsResource) const;
38 
39  public:
43  virtual ~IDnsResourceData() = default;
44 
50  template <class IDnsResourceDataType> bool isTypeOf() const
51  {
52  return dynamic_cast<const IDnsResourceDataType*>(this) != nullptr;
53  }
54 
61  template <class IDnsResourceDataType> IDnsResourceDataType* castAs()
62  {
63  return dynamic_cast<IDnsResourceDataType*>(this);
64  }
65 
69  virtual std::string toString() const = 0;
70 
79  virtual bool toByteArr(uint8_t* arr, size_t& arrLength, IDnsResource* dnsResource) const = 0;
80  };
81 
86  class DnsResourceDataPtr : public std::unique_ptr<IDnsResourceData>
87  {
88  public:
93  explicit DnsResourceDataPtr(IDnsResourceData* ptr) : std::unique_ptr<IDnsResourceData>(ptr)
94  {}
95 
101  template <class IDnsResourceDataType> bool isTypeOf() const
102  {
103  return get()->isTypeOf<IDnsResourceDataType>();
104  }
105 
111  template <class IDnsResourceDataType> IDnsResourceDataType* castAs()
112  {
113  return get()->castAs<IDnsResourceDataType>();
114  }
115  };
116 
123  {
124  private:
125  std::string m_Data;
126 
127  public:
139  explicit StringDnsResourceData(const std::string& data) : m_Data(data)
140  {}
141 
142  StringDnsResourceData(const uint8_t* dataPtr, size_t dataLen, IDnsResource* dnsResource);
143 
144  ~StringDnsResourceData() override = default;
145 
151  bool operator==(const StringDnsResourceData& other) const
152  {
153  return m_Data == other.m_Data;
154  }
155 
156  // implement abstract methods
157 
158  std::string toString() const override
159  {
160  return m_Data;
161  }
162  bool toByteArr(uint8_t* arr, size_t& arrLength, IDnsResource* dnsResource) const override;
163  };
164 
170  {
171  private:
172  IPv4Address m_Data;
173 
174  public:
180  IPv4DnsResourceData(const uint8_t* dataPtr, size_t dataLen);
181 
186  explicit IPv4DnsResourceData(const IPv4Address& addr) : m_Data(addr)
187  {}
188 
193  explicit IPv4DnsResourceData(const std::string& addrAsString) : m_Data(addrAsString)
194  {}
195 
201  bool operator==(const IPv4DnsResourceData& other) const
202  {
203  return m_Data == other.m_Data;
204  }
205 
210  {
211  return m_Data;
212  }
213 
214  // implement abstract methods
215 
216  std::string toString() const override
217  {
218  return m_Data.toString();
219  }
220  bool toByteArr(uint8_t* arr, size_t& arrLength, IDnsResource* dnsResource) const override;
221  };
222 
228  {
229  private:
230  IPv6Address m_Data;
231 
232  public:
238  IPv6DnsResourceData(const uint8_t* dataPtr, size_t dataLen);
239 
244  explicit IPv6DnsResourceData(const IPv6Address& addr) : m_Data(addr)
245  {}
246 
251  explicit IPv6DnsResourceData(const std::string& addrAsString) : m_Data(addrAsString)
252  {}
253 
259  bool operator==(const IPv6DnsResourceData& other) const
260  {
261  return m_Data == other.m_Data;
262  }
263 
268  {
269  return m_Data;
270  }
271 
272  // implement abstract methods
273 
274  std::string toString() const override
275  {
276  return m_Data.toString();
277  }
278  bool toByteArr(uint8_t* arr, size_t& arrLength, IDnsResource* dnsResource) const override;
279  };
280 
286  {
287  public:
291  struct MxData
292  {
294  uint16_t preference;
296  std::string mailExchange;
297  };
298 
305  MxDnsResourceData(uint8_t* dataPtr, size_t dataLen, IDnsResource* dnsResource);
306 
318  MxDnsResourceData(const uint16_t& preference, const std::string& mailExchange);
319 
320  ~MxDnsResourceData() override = default;
321 
328  bool operator==(const MxDnsResourceData& other) const;
329 
334  {
335  return m_Data;
336  }
337 
343  void setMxData(uint16_t preference, std::string mailExchange);
344 
345  // implement abstract methods
346 
351  std::string toString() const override;
352 
353  bool toByteArr(uint8_t* arr, size_t& arrLength, IDnsResource* dnsResource) const override;
354 
355  private:
356  MxData m_Data;
357  };
358 
365  {
366  private:
367  uint8_t* m_Data;
368  size_t m_DataLen;
369 
370  public:
377  GenericDnsResourceData(const uint8_t* dataPtr, size_t dataLen);
378 
383  explicit GenericDnsResourceData(const std::string& dataAsHexString);
384 
390 
391  ~GenericDnsResourceData() override
392  {
393  if (m_Data != nullptr)
394  delete[] m_Data;
395  }
396 
397  GenericDnsResourceData& operator=(const GenericDnsResourceData& other);
398 
404  bool operator==(const GenericDnsResourceData& other) const;
405 
406  // implement abstract methods
407 
408  std::string toString() const override;
409  bool toByteArr(uint8_t* arr, size_t& arrLength, IDnsResource* dnsResource) const override;
410  };
411 
412 } // namespace pcpp
Definition: DnsResourceData.h:87
IDnsResourceDataType * castAs()
Definition: DnsResourceData.h:111
DnsResourceDataPtr(IDnsResourceData *ptr)
Definition: DnsResourceData.h:93
bool isTypeOf() const
Definition: DnsResourceData.h:101
Definition: DnsResourceData.h:365
bool operator==(const GenericDnsResourceData &other) const
GenericDnsResourceData(const std::string &dataAsHexString)
bool toByteArr(uint8_t *arr, size_t &arrLength, IDnsResource *dnsResource) const override
std::string toString() const override
GenericDnsResourceData(const uint8_t *dataPtr, size_t dataLen)
GenericDnsResourceData(const GenericDnsResourceData &other)
Definition: DnsResourceData.h:28
virtual bool toByteArr(uint8_t *arr, size_t &arrLength, IDnsResource *dnsResource) const =0
IDnsResourceDataType * castAs()
Definition: DnsResourceData.h:61
bool isTypeOf() const
Definition: DnsResourceData.h:50
virtual std::string toString() const =0
virtual ~IDnsResourceData()=default
Definition: DnsResource.h:30
Definition: IpAddress.h:32
std::string toString() const
Definition: DnsResourceData.h:170
bool toByteArr(uint8_t *arr, size_t &arrLength, IDnsResource *dnsResource) const override
IPv4DnsResourceData(const std::string &addrAsString)
Definition: DnsResourceData.h:193
bool operator==(const IPv4DnsResourceData &other) const
Definition: DnsResourceData.h:201
IPv4DnsResourceData(const IPv4Address &addr)
Definition: DnsResourceData.h:186
IPv4DnsResourceData(const uint8_t *dataPtr, size_t dataLen)
std::string toString() const override
Definition: DnsResourceData.h:216
IPv4Address getIpAddress() const
Definition: DnsResourceData.h:209
Definition: IpAddress.h:199
std::string toString() const
Definition: DnsResourceData.h:228
IPv6DnsResourceData(const IPv6Address &addr)
Definition: DnsResourceData.h:244
std::string toString() const override
Definition: DnsResourceData.h:274
bool operator==(const IPv6DnsResourceData &other) const
Definition: DnsResourceData.h:259
IPv6DnsResourceData(const uint8_t *dataPtr, size_t dataLen)
bool toByteArr(uint8_t *arr, size_t &arrLength, IDnsResource *dnsResource) const override
IPv6Address getIpAddress() const
Definition: DnsResourceData.h:267
IPv6DnsResourceData(const std::string &addrAsString)
Definition: DnsResourceData.h:251
Definition: DnsResourceData.h:286
std::string toString() const override
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) const override
MxData getMxData() const
Definition: DnsResourceData.h:333
MxDnsResourceData(const uint16_t &preference, const std::string &mailExchange)
void setMxData(uint16_t preference, std::string mailExchange)
Definition: DnsResourceData.h:123
std::string toString() const override
Definition: DnsResourceData.h:158
bool toByteArr(uint8_t *arr, size_t &arrLength, IDnsResource *dnsResource) const override
StringDnsResourceData(const std::string &data)
Definition: DnsResourceData.h:139
bool operator==(const StringDnsResourceData &other) const
Definition: DnsResourceData.h:151
The main namespace for the PcapPlusPlus lib.
Definition: DnsResourceData.h:292
std::string mailExchange
Definition: DnsResourceData.h:296
uint16_t preference
Definition: DnsResourceData.h:294