PcapPlusPlus  20.08+
DnsLayer.h
Go to the documentation of this file.
1 #ifndef PACKETPP_DNS_LAYER
2 #define PACKETPP_DNS_LAYER
3 
4 #include "DnsLayerEnums.h"
5 #include "DnsResource.h"
6 #include "DnsResourceData.h"
7 #include "Layer.h"
8 
10 
15 namespace pcpp
16 {
17 
23 #pragma pack(push, 1)
24  struct dnshdr
25  {
27  uint16_t transactionID;
28 #if (BYTE_ORDER == LITTLE_ENDIAN)
29  uint16_t
33  truncation:1,
37  opcode:4,
41  responseCode:4,
45  authenticData:1,
47  zero:1,
50 #elif (BYTE_ORDER == BIG_ENDIAN)
51  uint16_t
55  opcode:4,
59  truncation:1,
65  zero:1,
67  authenticData:1,
71  responseCode:4;
72 #endif
73 
76  uint16_t numberOfAnswers;
81  };
82 #pragma pack(pop)
83 
84 
85  // forward declarations
86  class DnsQuery;
87  class IDnsResource;
88  class DnsResource;
89  class IDnsResourceData;
90 
91 
97  class DnsLayer : public Layer
98  {
99  friend class IDnsResource;
100  friend class DnsQuery;
101  friend class DnsResource;
102 
103  public:
104 
112  DnsLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
113 
117  DnsLayer();
118 
122  DnsLayer(const DnsLayer& other);
123 
127  DnsLayer& operator=(const DnsLayer& other);
128 
129  virtual ~DnsLayer();
130 
136  dnshdr* getDnsHeader() const { return (dnshdr*)m_Data; }
137 
145  DnsQuery* getQuery(const std::string& name, bool exactMatch) const;
146 
150  DnsQuery* getFirstQuery() const;
151 
157  DnsQuery* getNextQuery(DnsQuery* query) const;
158 
162  size_t getQueryCount() const;
163 
172  DnsQuery* addQuery(const std::string& name, DnsType dnsType, DnsClass dnsClass);
173 
180  DnsQuery* addQuery(DnsQuery* const copyQuery);
181 
188  bool removeQuery(const std::string& queryNameToRemove, bool exactMatch);
189 
195  bool removeQuery(DnsQuery* queryToRemove);
196 
204  DnsResource* getAnswer(const std::string& name, bool exactMatch) const;
205 
209  DnsResource* getFirstAnswer() const;
210 
216  DnsResource* getNextAnswer(DnsResource* answer) const;
217 
221  size_t getAnswerCount() const;
222 
235  DnsResource* addAnswer(const std::string& name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, IDnsResourceData* data);
236 
243  DnsResource* addAnswer(DnsResource* const copyAnswer);
244 
251  bool removeAnswer(const std::string& answerNameToRemove, bool exactMatch);
252 
258  bool removeAnswer(DnsResource* answerToRemove);
259 
260 
268  DnsResource* getAuthority(const std::string& name, bool exactMatch) const;
269 
273  DnsResource* getFirstAuthority() const;
274 
280  DnsResource* getNextAuthority(DnsResource* authority) const;
281 
285  size_t getAuthorityCount() const;
286 
299  DnsResource* addAuthority(const std::string& name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, IDnsResourceData* data);
300 
307  DnsResource* addAuthority(DnsResource* const copyAuthority);
308 
315  bool removeAuthority(const std::string& authorityNameToRemove, bool exactMatch);
316 
322  bool removeAuthority(DnsResource* authorityToRemove);
323 
324 
333  DnsResource* getAdditionalRecord(const std::string& name, bool exactMatch) const;
334 
338  DnsResource* getFirstAdditionalRecord() const;
339 
346  DnsResource* getNextAdditionalRecord(DnsResource* additionalRecord) const;
347 
351  size_t getAdditionalRecordCount() const;
352 
365  DnsResource* addAdditionalRecord(const std::string& name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, IDnsResourceData* data);
366 
380  DnsResource* addAdditionalRecord(const std::string& name, DnsType dnsType, uint16_t customData1, uint32_t customData2, IDnsResourceData* data);
381 
389  DnsResource* addAdditionalRecord(DnsResource* const copyAdditionalRecord);
390 
397  bool removeAdditionalRecord(const std::string& additionalRecordNameToRemove, bool exactMatch);
398 
404  bool removeAdditionalRecord(DnsResource* additionalRecordToRemove);
405 
406  // implement abstract methods
407 
411  void parseNextLayer() {}
412 
417  size_t getHeaderLen() const { return m_DataLen; } //No layer above DNS
418 
423 
424  std::string toString() const;
425 
427 
432  static inline bool isDnsPort(uint16_t port);
433 
434  private:
435  IDnsResource* m_ResourceList;
436  DnsQuery* m_FirstQuery;
437  DnsResource* m_FirstAnswer;
438  DnsResource* m_FirstAuthority;
439  DnsResource* m_FirstAdditional;
440 
441  IDnsResource* getFirstResource(DnsResourceType resType) const;
442  void setFirstResource(DnsResourceType resType, IDnsResource* resource);
443 
444  using Layer::extendLayer;
445  bool extendLayer(int offsetInLayer, size_t numOfBytesToExtend, IDnsResource* resource);
446 
447  using Layer::shortenLayer;
448  bool shortenLayer(int offsetInLayer, size_t numOfBytesToShorten, IDnsResource* resource);
449 
450  IDnsResource* getResourceByName(IDnsResource* startFrom, size_t resourceCount, const std::string& name, bool exactMatch) const;
451 
452  void parseResources();
453 
454  DnsResource* addResource(DnsResourceType resType, const std::string& name, DnsType dnsType, DnsClass dnsClass,
455  uint32_t ttl, IDnsResourceData* data);
456 
457  bool removeResource(IDnsResource* resourceToRemove);
458 
459  };
460 
461 
462  // implementation of inline methods
463 
464  bool DnsLayer::isDnsPort(uint16_t port)
465  {
466  switch (port)
467  {
468  case 53:
469  case 5353:
470  case 5355:
471  return true;
472  default:
473  return false;
474  }
475  }
476 
477 } // namespace pcpp
478 
479 #endif /* PACKETPP_DNS_LAYER */
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:233
uint16_t authoritativeAnswer
Definition: DnsLayer.h:31
uint16_t opcode
Definition: DnsLayer.h:31
uint16_t numberOfAdditional
Definition: DnsLayer.h:80
OsiModelLayer getOsiModelLayer() const
Definition: DnsLayer.h:426
Definition: ProtocolType.h:248
Definition: Layer.h:70
void computeCalculateFields()
Definition: DnsLayer.h:422
Definition: Packet.h:26
uint16_t numberOfAuthority
Definition: DnsLayer.h:78
DnsClass
Definition: DnsLayerEnums.h:127
Definition: DnsResourceData.h:40
uint16_t responseCode
Definition: DnsLayer.h:31
uint16_t queryOrResponse
Definition: DnsLayer.h:31
uint16_t truncation
Definition: DnsLayer.h:31
DnsType
Definition: DnsLayerEnums.h:15
uint16_t checkingDisabled
Definition: DnsLayer.h:31
Definition: DnsLayer.h:97
uint16_t numberOfAnswers
Definition: DnsLayer.h:76
size_t getHeaderLen() const
Definition: DnsLayer.h:417
uint16_t recursionDesired
Definition: DnsLayer.h:31
DnsResourceType
Definition: DnsLayerEnums.h:145
void parseNextLayer()
Definition: DnsLayer.h:411
Definition: DnsLayer.h:24
uint16_t transactionID
Definition: DnsLayer.h:27
dnshdr * getDnsHeader() const
Definition: DnsLayer.h:136
uint16_t numberOfQuestions
Definition: DnsLayer.h:74
uint16_t authenticData
Definition: DnsLayer.h:31
Definition: DnsResource.h:150
Definition: DnsResource.h:30
Definition: DnsResource.h:128
uint16_t zero
Definition: DnsLayer.h:31
static bool isDnsPort(uint16_t port)
Definition: DnsLayer.h:464
uint16_t recursionAvailable
Definition: DnsLayer.h:31