PcapPlusPlus  Next
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() = default;
65 
70 
75  void setDnsType(DnsType newType);
76 
81 
86  void setDnsClass(DnsClass newClass);
87 
91  const std::string& getName() const
92  {
93  return m_DecodedName;
94  }
95 
99  size_t getNameOffset() const
100  {
101  return m_OffsetInLayer;
102  }
103 
117  bool setName(const std::string& newName);
118 
119  // abstract methods
120 
124  virtual size_t getSize() const = 0;
125 
129  virtual DnsResourceType getType() const = 0;
130  };
131 
136  class DnsQuery : public IDnsResource
137  {
138  friend class DnsLayer;
139 
140  private:
141  DnsQuery(DnsLayer* dnsLayer, size_t offsetInLayer) : IDnsResource(dnsLayer, offsetInLayer)
142  {}
143 
144  explicit DnsQuery(uint8_t* emptyRawData) : IDnsResource(emptyRawData)
145  {}
146 
147  public:
148  ~DnsQuery() override = default;
149 
150  // implementation of abstract methods
151  size_t getSize() const override
152  {
153  return m_NameLength + 2 * sizeof(uint16_t);
154  }
155  DnsResourceType getType() const override
156  {
157  return DnsQueryType;
158  }
159  };
160 
165  class DnsResource : public IDnsResource
166  {
167  friend class DnsLayer;
168 
169  private:
170  DnsResourceType m_ResourceType;
171 
172  DnsResource(DnsLayer* dnsLayer, size_t offsetInLayer, DnsResourceType resourceType)
173  : IDnsResource(dnsLayer, offsetInLayer)
174  {
175  m_ResourceType = resourceType;
176  }
177 
178  DnsResource(uint8_t* emptyRawData, DnsResourceType resType)
179  : IDnsResource(emptyRawData), m_ResourceType(resType)
180  {}
181 
182  public:
183  ~DnsResource() override = default;
184 
188  uint32_t getTTL() const;
189 
194  void setTTL(uint32_t newTTL);
195 
199  size_t getDataLength() const;
200 
217 
221  size_t getDataOffset() const;
222 
244 
250  uint16_t getCustomDnsClass() const;
251 
257  void setCustomDnsClass(uint16_t customValue);
258 
259  // implementation of abstract methods
260  size_t getSize() const override
261  {
262  return m_NameLength + 3 * sizeof(uint16_t) + sizeof(uint32_t) + getDataLength();
263  }
264  DnsResourceType getType() const override
265  {
266  return m_ResourceType;
267  }
268  };
269 
270 } // namespace pcpp
Definition: DnsLayer.h:94
Definition: DnsResource.h:137
size_t getSize() const override
Definition: DnsResource.h:151
DnsResourceType getType() const override
Definition: DnsResource.h:155
Definition: DnsResourceData.h:87
Definition: DnsResource.h:166
void setTTL(uint32_t newTTL)
void setCustomDnsClass(uint16_t customValue)
size_t getSize() const override
Definition: DnsResource.h:260
uint32_t getTTL() const
uint16_t getCustomDnsClass() const
DnsResourceType getType() const override
Definition: DnsResource.h:264
size_t getDataOffset() const
size_t getDataLength() const
DnsResourceDataPtr getData() const
bool setData(IDnsResourceData *data)
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:91
virtual size_t getSize() const =0
DnsClass getDnsClass() const
size_t getNameOffset() const
Definition: DnsResource.h:99
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