PcapPlusPlus  24.09
DnsResource.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "DnsLayer.h"
4 #include "DnsLayerEnums.h"
5 #include "DnsResourceData.h"
6 #include <stdio.h>
7 #include <string>
8 #include <stdint.h>
9 
11 
16 namespace pcpp
17 {
18  // forward declarations
19  class DnsLayer;
20  class IDnsResourceData;
21  class DnsResourceDataPtr;
22 
30  {
31  protected:
32  friend class DnsLayer;
33  friend class IDnsResourceData;
34 
35  protected:
36  DnsLayer* m_DnsLayer;
37  size_t m_OffsetInLayer;
38  IDnsResource* m_NextResource;
39  std::string m_DecodedName;
40  size_t m_NameLength;
41  uint8_t* m_ExternalRawData;
42 
43  IDnsResource(DnsLayer* dnsLayer, size_t offsetInLayer);
44 
45  IDnsResource(uint8_t* emptyRawData);
46 
47  size_t decodeName(const char* encodedName, char* result, int iteration = 1);
48  void encodeName(const std::string& decodedName, char* result, size_t& resultLen);
49 
50  IDnsResource* getNextResource() const
51  {
52  return m_NextResource;
53  }
54  void setNextResource(IDnsResource* next)
55  {
56  m_NextResource = next;
57  }
58 
59  uint8_t* getRawData() const;
60 
61  void setDnsLayer(DnsLayer* dnsLayer, size_t offsetInLayer);
62 
63  public:
64  virtual ~IDnsResource()
65  {}
66 
71 
76  void setDnsType(DnsType newType);
77 
82 
87  void setDnsClass(DnsClass newClass);
88 
92  const std::string& getName() const
93  {
94  return m_DecodedName;
95  }
96 
100  size_t getNameOffset() const
101  {
102  return m_OffsetInLayer;
103  }
104 
118  bool setName(const std::string& newName);
119 
120  // abstract methods
121 
125  virtual size_t getSize() const = 0;
126 
130  virtual DnsResourceType getType() const = 0;
131  };
132 
137  class DnsQuery : public IDnsResource
138  {
139  friend class DnsLayer;
140 
141  private:
142  DnsQuery(DnsLayer* dnsLayer, size_t offsetInLayer) : IDnsResource(dnsLayer, offsetInLayer)
143  {}
144 
145  explicit DnsQuery(uint8_t* emptyRawData) : IDnsResource(emptyRawData)
146  {}
147 
148  public:
149  virtual ~DnsQuery()
150  {}
151 
152  // implementation of abstract methods
153  virtual size_t getSize() const
154  {
155  return m_NameLength + 2 * sizeof(uint16_t);
156  }
157  virtual DnsResourceType getType() const
158  {
159  return DnsQueryType;
160  }
161  };
162 
167  class DnsResource : public IDnsResource
168  {
169  friend class DnsLayer;
170 
171  private:
172  DnsResourceType m_ResourceType;
173 
174  DnsResource(DnsLayer* dnsLayer, size_t offsetInLayer, DnsResourceType resourceType)
175  : IDnsResource(dnsLayer, offsetInLayer)
176  {
177  m_ResourceType = resourceType;
178  }
179 
180  DnsResource(uint8_t* emptyRawData, DnsResourceType resType)
181  : IDnsResource(emptyRawData), m_ResourceType(resType)
182  {}
183 
184  public:
185  virtual ~DnsResource()
186  {}
187 
191  uint32_t getTTL() const;
192 
197  void setTTL(uint32_t newTTL);
198 
202  size_t getDataLength() const;
203 
220 
224  size_t getDataOffset() const;
225 
247 
253  uint16_t getCustomDnsClass() const;
254 
260  void setCustomDnsClass(uint16_t customValue);
261 
262  // implementation of abstract methods
263  virtual size_t getSize() const
264  {
265  return m_NameLength + 3 * sizeof(uint16_t) + sizeof(uint32_t) + getDataLength();
266  }
267  virtual DnsResourceType getType() const
268  {
269  return m_ResourceType;
270  }
271  };
272 
273 } // namespace pcpp
Definition: DnsLayer.h:94
Definition: DnsResource.h:138
virtual DnsResourceType getType() const
Definition: DnsResource.h:157
virtual size_t getSize() const
Definition: DnsResource.h:153
Definition: DnsResourceData.h:88
Definition: DnsResource.h:168
void setTTL(uint32_t newTTL)
virtual size_t getSize() const
Definition: DnsResource.h:263
void setCustomDnsClass(uint16_t customValue)
uint32_t getTTL() const
uint16_t getCustomDnsClass() const
size_t getDataOffset() const
size_t getDataLength() const
DnsResourceDataPtr getData() const
bool setData(IDnsResourceData *data)
virtual DnsResourceType getType() const
Definition: DnsResource.h:267
Definition: DnsResourceData.h:28
Definition: DnsResource.h:30
void setDnsType(DnsType newType)
virtual DnsResourceType getType() const =0
const std::string & getName() const
Definition: DnsResource.h:92
virtual size_t getSize() const =0
DnsClass getDnsClass() const
size_t getNameOffset() const
Definition: DnsResource.h:100
void setDnsClass(DnsClass newClass)
DnsType getDnsType() const
bool setName(const std::string &newName)
The main namespace for the PcapPlusPlus lib.
DnsClass
Definition: DnsLayerEnums.h:126
DnsResourceType
Definition: DnsLayerEnums.h:143
@ DnsQueryType
Definition: DnsLayerEnums.h:145
DnsType
Definition: DnsLayerEnums.h:15