PcapPlusPlus  21.11
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 
96  class DnsLayer : public Layer
97  {
98  friend class IDnsResource;
99  friend class DnsQuery;
100  friend class DnsResource;
101 
102  public:
103 
111  DnsLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
112 
116  DnsLayer();
117 
122  DnsLayer(const DnsLayer& other);
123 
129  DnsLayer& operator=(const DnsLayer& other);
130 
131  virtual ~DnsLayer();
132 
138  dnshdr* getDnsHeader() const;
139 
147  DnsQuery* getQuery(const std::string& name, bool exactMatch) const;
148 
152  DnsQuery* getFirstQuery() const;
153 
159  DnsQuery* getNextQuery(DnsQuery* query) const;
160 
164  size_t getQueryCount() const;
165 
174  DnsQuery* addQuery(const std::string& name, DnsType dnsType, DnsClass dnsClass);
175 
182  DnsQuery* addQuery(DnsQuery* const copyQuery);
183 
190  bool removeQuery(const std::string& queryNameToRemove, bool exactMatch);
191 
197  bool removeQuery(DnsQuery* queryToRemove);
198 
206  DnsResource* getAnswer(const std::string& name, bool exactMatch) const;
207 
211  DnsResource* getFirstAnswer() const;
212 
218  DnsResource* getNextAnswer(DnsResource* answer) const;
219 
223  size_t getAnswerCount() const;
224 
237  DnsResource* addAnswer(const std::string& name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, IDnsResourceData* data);
238 
245  DnsResource* addAnswer(DnsResource* const copyAnswer);
246 
253  bool removeAnswer(const std::string& answerNameToRemove, bool exactMatch);
254 
260  bool removeAnswer(DnsResource* answerToRemove);
261 
262 
270  DnsResource* getAuthority(const std::string& name, bool exactMatch) const;
271 
275  DnsResource* getFirstAuthority() const;
276 
282  DnsResource* getNextAuthority(DnsResource* authority) const;
283 
287  size_t getAuthorityCount() const;
288 
301  DnsResource* addAuthority(const std::string& name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, IDnsResourceData* data);
302 
309  DnsResource* addAuthority(DnsResource* const copyAuthority);
310 
317  bool removeAuthority(const std::string& authorityNameToRemove, bool exactMatch);
318 
324  bool removeAuthority(DnsResource* authorityToRemove);
325 
326 
335  DnsResource* getAdditionalRecord(const std::string& name, bool exactMatch) const;
336 
340  DnsResource* getFirstAdditionalRecord() const;
341 
348  DnsResource* getNextAdditionalRecord(DnsResource* additionalRecord) const;
349 
353  size_t getAdditionalRecordCount() const;
354 
367  DnsResource* addAdditionalRecord(const std::string& name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, IDnsResourceData* data);
368 
382  DnsResource* addAdditionalRecord(const std::string& name, DnsType dnsType, uint16_t customData1, uint32_t customData2, IDnsResourceData* data);
383 
391  DnsResource* addAdditionalRecord(DnsResource* const copyAdditionalRecord);
392 
399  bool removeAdditionalRecord(const std::string& additionalRecordNameToRemove, bool exactMatch);
400 
406  bool removeAdditionalRecord(DnsResource* additionalRecordToRemove);
407 
408  // implement abstract methods
409 
413  void parseNextLayer() {}
414 
419  size_t getHeaderLen() const { return m_DataLen; } //No layer above DNS
420 
425  virtual void computeCalculateFields() {}
426 
427  std::string toString() const;
428 
430 
436  static inline bool isDnsPort(uint16_t port);
437 
446  static inline bool isDataValid(const uint8_t* data, size_t dataLen, bool dnsOverTcp = false);
447 
448  protected:
449  DnsLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, size_t offsetAdjustment);
450  DnsLayer(size_t offsetAdjustment);
451 
452  private:
453  IDnsResource* m_ResourceList;
454  DnsQuery* m_FirstQuery;
455  DnsResource* m_FirstAnswer;
456  DnsResource* m_FirstAuthority;
457  DnsResource* m_FirstAdditional;
458  uint16_t m_OffsetAdjustment;
459 
460  size_t getBasicHeaderSize();
461  void init(size_t offsetAdjustment, bool callParseResource);
462  void initNewLayer(size_t offsetAdjustment);
463 
464  IDnsResource* getFirstResource(DnsResourceType resType) const;
465  void setFirstResource(DnsResourceType resType, IDnsResource* resource);
466 
467  using Layer::extendLayer;
468  bool extendLayer(int offsetInLayer, size_t numOfBytesToExtend, IDnsResource* resource);
469 
470  using Layer::shortenLayer;
471  bool shortenLayer(int offsetInLayer, size_t numOfBytesToShorten, IDnsResource* resource);
472 
473  IDnsResource* getResourceByName(IDnsResource* startFrom, size_t resourceCount, const std::string& name, bool exactMatch) const;
474 
475  void parseResources();
476 
477  DnsResource* addResource(DnsResourceType resType, const std::string& name, DnsType dnsType, DnsClass dnsClass,
478  uint32_t ttl, IDnsResourceData* data);
479 
480  bool removeResource(IDnsResource* resourceToRemove);
481 
482  };
483 
484 
485 
496  class DnsOverTcpLayer : public DnsLayer
497  {
498  public:
499 
507  DnsOverTcpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
508  : DnsLayer(data, dataLen, prevLayer, packet, sizeof(uint16_t)) {}
509 
513  DnsOverTcpLayer() : DnsLayer(sizeof(uint16_t)) {}
514 
519  DnsOverTcpLayer(const DnsOverTcpLayer& other) : DnsLayer(other) {}
520 
524  uint16_t getTcpMessageLength();
525 
530  void setTcpMessageLength(uint16_t value);
531 
532 
533  // overriden methods
534 
538  void computeCalculateFields();
539  };
540 
541 
542  // implementation of inline methods
543 
544  bool DnsLayer::isDnsPort(uint16_t port)
545  {
546  switch (port)
547  {
548  case 53:
549  case 5353:
550  case 5355:
551  return true;
552  default:
553  return false;
554  }
555  }
556 
557  bool DnsLayer::isDataValid(const uint8_t* data, size_t dataLen, bool dnsOverTcp)
558  {
559  size_t minSize = sizeof(dnshdr) + (dnsOverTcp ? sizeof(uint16_t) : 0);
560  return dataLen >= minSize;
561  }
562 
563 } // namespace pcpp
564 
565 #endif /* PACKETPP_DNS_LAYER */
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:258
DnsOverTcpLayer(const DnsOverTcpLayer &other)
Definition: DnsLayer.h:519
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:429
Definition: ProtocolType.h:273
Definition: Layer.h:70
Definition: Packet.h:26
DnsOverTcpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: DnsLayer.h:507
uint16_t numberOfAuthority
Definition: DnsLayer.h:78
DnsClass
Definition: DnsLayerEnums.h:127
static bool isDataValid(const uint8_t *data, size_t dataLen, bool dnsOverTcp=false)
Definition: DnsLayer.h:557
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:96
uint16_t numberOfAnswers
Definition: DnsLayer.h:76
size_t getHeaderLen() const
Definition: DnsLayer.h:419
virtual void computeCalculateFields()
Definition: DnsLayer.h:425
uint16_t recursionDesired
Definition: DnsLayer.h:31
DnsResourceType
Definition: DnsLayerEnums.h:145
void parseNextLayer()
Definition: DnsLayer.h:413
Definition: DnsLayer.h:24
uint16_t transactionID
Definition: DnsLayer.h:27
Definition: DnsLayer.h:496
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
DnsOverTcpLayer()
Definition: DnsLayer.h:513
static bool isDnsPort(uint16_t port)
Definition: DnsLayer.h:544
uint16_t recursionAvailable
Definition: DnsLayer.h:31