PcapPlusPlus  24.09
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:
44  {}
45 
51  template <class IDnsResourceDataType> bool isTypeOf() const
52  {
53  return dynamic_cast<const IDnsResourceDataType*>(this) != nullptr;
54  }
55 
62  template <class IDnsResourceDataType> IDnsResourceDataType* castAs()
63  {
64  return dynamic_cast<IDnsResourceDataType*>(this);
65  }
66 
70  virtual std::string toString() const = 0;
71 
80  virtual bool toByteArr(uint8_t* arr, size_t& arrLength, IDnsResource* dnsResource) const = 0;
81  };
82 
87  class DnsResourceDataPtr : public std::unique_ptr<IDnsResourceData>
88  {
89  public:
94  explicit DnsResourceDataPtr(IDnsResourceData* ptr) : std::unique_ptr<IDnsResourceData>(ptr)
95  {}
96 
102  template <class IDnsResourceDataType> bool isTypeOf() const
103  {
104  return get()->isTypeOf<IDnsResourceDataType>();
105  }
106 
112  template <class IDnsResourceDataType> IDnsResourceDataType* castAs()
113  {
114  return get()->castAs<IDnsResourceDataType>();
115  }
116  };
117 
124  {
125  private:
126  std::string m_Data;
127 
128  public:
140  explicit StringDnsResourceData(const std::string& data) : m_Data(data)
141  {}
142 
143  StringDnsResourceData(const uint8_t* dataPtr, size_t dataLen, IDnsResource* dnsResource);
144 
146  {}
147 
153  bool operator==(const StringDnsResourceData& other) const
154  {
155  return m_Data == other.m_Data;
156  }
157 
158  // implement abstract methods
159 
160  std::string toString() const
161  {
162  return m_Data;
163  }
164  bool toByteArr(uint8_t* arr, size_t& arrLength, IDnsResource* dnsResource) const;
165  };
166 
172  {
173  private:
174  IPv4Address m_Data;
175 
176  public:
182  IPv4DnsResourceData(const uint8_t* dataPtr, size_t dataLen);
183 
188  explicit IPv4DnsResourceData(const IPv4Address& addr) : m_Data(addr)
189  {}
190 
195  explicit IPv4DnsResourceData(const std::string& addrAsString) : m_Data(addrAsString)
196  {}
197 
203  bool operator==(const IPv4DnsResourceData& other) const
204  {
205  return m_Data == other.m_Data;
206  }
207 
212  {
213  return m_Data;
214  }
215 
216  // implement abstract methods
217 
218  std::string toString() const
219  {
220  return m_Data.toString();
221  }
222  bool toByteArr(uint8_t* arr, size_t& arrLength, IDnsResource* dnsResource) const;
223  };
224 
230  {
231  private:
232  IPv6Address m_Data;
233 
234  public:
240  IPv6DnsResourceData(const uint8_t* dataPtr, size_t dataLen);
241 
246  explicit IPv6DnsResourceData(const IPv6Address& addr) : m_Data(addr)
247  {}
248 
253  explicit IPv6DnsResourceData(const std::string& addrAsString) : m_Data(addrAsString)
254  {}
255 
261  bool operator==(const IPv6DnsResourceData& other) const
262  {
263  return m_Data == other.m_Data;
264  }
265 
270  {
271  return m_Data;
272  }
273 
274  // implement abstract methods
275 
276  std::string toString() const
277  {
278  return m_Data.toString();
279  }
280  bool toByteArr(uint8_t* arr, size_t& arrLength, IDnsResource* dnsResource) const;
281  };
282 
288  {
289  public:
293  struct MxData
294  {
296  uint16_t preference;
298  std::string mailExchange;
299  };
300 
307  MxDnsResourceData(uint8_t* dataPtr, size_t dataLen, IDnsResource* dnsResource);
308 
320  MxDnsResourceData(const uint16_t& preference, const std::string& mailExchange);
321 
323  {}
324 
331  bool operator==(const MxDnsResourceData& other) const;
332 
337  {
338  return m_Data;
339  }
340 
346  void setMxData(uint16_t preference, std::string mailExchange);
347 
348  // implement abstract methods
349 
354  std::string toString() const;
355 
356  bool toByteArr(uint8_t* arr, size_t& arrLength, IDnsResource* dnsResource) const;
357 
358  private:
359  MxData m_Data;
360  };
361 
368  {
369  private:
370  uint8_t* m_Data;
371  size_t m_DataLen;
372 
373  public:
380  GenericDnsResourceData(const uint8_t* dataPtr, size_t dataLen);
381 
386  explicit GenericDnsResourceData(const std::string& dataAsHexString);
387 
393 
395  {
396  if (m_Data != nullptr)
397  delete[] m_Data;
398  }
399 
400  GenericDnsResourceData& operator=(const GenericDnsResourceData& other);
401 
407  bool operator==(const GenericDnsResourceData& other) const;
408 
409  // implement abstract methods
410 
411  std::string toString() const;
412  bool toByteArr(uint8_t* arr, size_t& arrLength, IDnsResource* dnsResource) const;
413  };
414 
415 } // namespace pcpp
Definition: DnsResourceData.h:88
IDnsResourceDataType * castAs()
Definition: DnsResourceData.h:112
DnsResourceDataPtr(IDnsResourceData *ptr)
Definition: DnsResourceData.h:94
bool isTypeOf() const
Definition: DnsResourceData.h:102
Definition: DnsResourceData.h:368
bool operator==(const GenericDnsResourceData &other) const
std::string toString() const
GenericDnsResourceData(const std::string &dataAsHexString)
bool toByteArr(uint8_t *arr, size_t &arrLength, IDnsResource *dnsResource) const
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:62
virtual ~IDnsResourceData()
Definition: DnsResourceData.h:43
bool isTypeOf() const
Definition: DnsResourceData.h:51
virtual std::string toString() const =0
Definition: DnsResource.h:30
Definition: IpAddress.h:32
std::string toString() const
Definition: DnsResourceData.h:172
bool toByteArr(uint8_t *arr, size_t &arrLength, IDnsResource *dnsResource) const
IPv4DnsResourceData(const std::string &addrAsString)
Definition: DnsResourceData.h:195
bool operator==(const IPv4DnsResourceData &other) const
Definition: DnsResourceData.h:203
IPv4DnsResourceData(const IPv4Address &addr)
Definition: DnsResourceData.h:188
std::string toString() const
Definition: DnsResourceData.h:218
IPv4DnsResourceData(const uint8_t *dataPtr, size_t dataLen)
IPv4Address getIpAddress() const
Definition: DnsResourceData.h:211
Definition: IpAddress.h:199
std::string toString() const
Definition: DnsResourceData.h:230
IPv6DnsResourceData(const IPv6Address &addr)
Definition: DnsResourceData.h:246
bool toByteArr(uint8_t *arr, size_t &arrLength, IDnsResource *dnsResource) const
std::string toString() const
Definition: DnsResourceData.h:276
bool operator==(const IPv6DnsResourceData &other) const
Definition: DnsResourceData.h:261
IPv6DnsResourceData(const uint8_t *dataPtr, size_t dataLen)
IPv6Address getIpAddress() const
Definition: DnsResourceData.h:269
IPv6DnsResourceData(const std::string &addrAsString)
Definition: DnsResourceData.h:253
Definition: DnsResourceData.h:288
std::string toString() const
bool toByteArr(uint8_t *arr, size_t &arrLength, IDnsResource *dnsResource) const
MxDnsResourceData(uint8_t *dataPtr, size_t dataLen, IDnsResource *dnsResource)
bool operator==(const MxDnsResourceData &other) const
MxData getMxData() const
Definition: DnsResourceData.h:336
MxDnsResourceData(const uint16_t &preference, const std::string &mailExchange)
void setMxData(uint16_t preference, std::string mailExchange)
Definition: DnsResourceData.h:124
bool toByteArr(uint8_t *arr, size_t &arrLength, IDnsResource *dnsResource) const
std::string toString() const
Definition: DnsResourceData.h:160
StringDnsResourceData(const std::string &data)
Definition: DnsResourceData.h:140
bool operator==(const StringDnsResourceData &other) const
Definition: DnsResourceData.h:153
The main namespace for the PcapPlusPlus lib.
Definition: DnsResourceData.h:294
std::string mailExchange
Definition: DnsResourceData.h:298
uint16_t preference
Definition: DnsResourceData.h:296