PcapPlusPlus  Next
DnsLayer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "DnsLayerEnums.h"
4 #include "DnsResource.h"
5 #include "DnsResourceData.h"
6 #include "Layer.h"
7 
9 
14 namespace pcpp
15 {
16 
22 #pragma pack(push, 1)
23  struct dnshdr
24  {
26  uint16_t transactionID;
27 #if (BYTE_ORDER == LITTLE_ENDIAN)
28  uint16_t
36  opcode : 4,
46  zero : 1,
49 #elif (BYTE_ORDER == BIG_ENDIAN)
50  uint16_t
52  queryOrResponse : 1,
54  opcode : 4,
58  truncation : 1,
60  recursionDesired : 1,
64  zero : 1,
66  authenticData : 1,
68  checkingDisabled : 1,
70  responseCode : 4;
71 #endif
75  uint16_t numberOfAnswers;
80  };
81 #pragma pack(pop)
82 
83  // forward declarations
84  class DnsQuery;
85  class IDnsResource;
86  class DnsResource;
87  class IDnsResourceData;
88 
93  class DnsLayer : public Layer
94  {
95  friend class IDnsResource;
96  friend class DnsQuery;
97  friend class DnsResource;
98 
99  public:
107  DnsLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
108 
114 
119  DnsLayer(const DnsLayer& other);
120 
126  DnsLayer& operator=(const DnsLayer& other);
127 
128  ~DnsLayer() override;
129 
137 
146  DnsQuery* getQuery(const std::string& name, bool exactMatch) const;
147 
152 
160 
164  size_t getQueryCount() const;
165 
174  DnsQuery* addQuery(const std::string& name, DnsType dnsType, DnsClass dnsClass);
175 
183  DnsQuery* addQuery(DnsQuery* const copyQuery);
184 
192  bool removeQuery(const std::string& queryNameToRemove, bool exactMatch);
193 
200  bool removeQuery(DnsQuery* queryToRemove);
201 
210  DnsResource* getAnswer(const std::string& name, bool exactMatch) const;
211 
216 
224 
228  size_t getAnswerCount() const;
229 
242  DnsResource* addAnswer(const std::string& name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl,
243  IDnsResourceData* data);
244 
252  DnsResource* addAnswer(DnsResource* const copyAnswer);
253 
261  bool removeAnswer(const std::string& answerNameToRemove, bool exactMatch);
262 
269  bool removeAnswer(DnsResource* answerToRemove);
270 
279  DnsResource* getAuthority(const std::string& name, bool exactMatch) const;
280 
285 
293 
297  size_t getAuthorityCount() const;
298 
311  DnsResource* addAuthority(const std::string& name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl,
312  IDnsResourceData* data);
313 
321  DnsResource* addAuthority(DnsResource* const copyAuthority);
322 
331  bool removeAuthority(const std::string& authorityNameToRemove, bool exactMatch);
332 
339  bool removeAuthority(DnsResource* authorityToRemove);
340 
349  DnsResource* getAdditionalRecord(const std::string& name, bool exactMatch) const;
350 
356 
364 
368  size_t getAdditionalRecordCount() const;
369 
382  DnsResource* addAdditionalRecord(const std::string& name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl,
383  IDnsResourceData* data);
384 
401  DnsResource* addAdditionalRecord(const std::string& name, DnsType dnsType, uint16_t customData1,
402  uint32_t customData2, IDnsResourceData* data);
403 
412  DnsResource* addAdditionalRecord(DnsResource* const copyAdditionalRecord);
413 
422  bool removeAdditionalRecord(const std::string& additionalRecordNameToRemove, bool exactMatch);
423 
430  bool removeAdditionalRecord(DnsResource* additionalRecordToRemove);
431 
432  // implement abstract methods
433 
437  void parseNextLayer() override
438  {}
439 
444  size_t getHeaderLen() const override
445  {
446  return m_DataLen;
447  } // No layer above DNS
448 
452  void computeCalculateFields() override
453  {}
454 
455  std::string toString() const override;
456 
458  {
460  }
461 
467  static inline bool isDnsPort(uint16_t port);
468 
477  static inline bool isDataValid(const uint8_t* data, size_t dataLen, bool dnsOverTcp = false);
478 
479  protected:
480  DnsLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, size_t offsetAdjustment);
481  explicit DnsLayer(size_t offsetAdjustment);
482 
483  private:
484  IDnsResource* m_ResourceList;
485  DnsQuery* m_FirstQuery;
486  DnsResource* m_FirstAnswer;
487  DnsResource* m_FirstAuthority;
488  DnsResource* m_FirstAdditional;
489  uint16_t m_OffsetAdjustment;
490 
491  size_t getBasicHeaderSize();
492  void init(size_t offsetAdjustment, bool callParseResource);
493  void initNewLayer(size_t offsetAdjustment);
494 
495  IDnsResource* getFirstResource(DnsResourceType resType) const;
496  void setFirstResource(DnsResourceType resType, IDnsResource* resource);
497 
498  using Layer::extendLayer;
499  bool extendLayer(int offsetInLayer, size_t numOfBytesToExtend, IDnsResource* resource);
500 
501  using Layer::shortenLayer;
502  bool shortenLayer(int offsetInLayer, size_t numOfBytesToShorten, IDnsResource* resource);
503 
504  IDnsResource* getResourceByName(IDnsResource* startFrom, size_t resourceCount, const std::string& name,
505  bool exactMatch) const;
506 
507  void parseResources();
508 
509  DnsResource* addResource(DnsResourceType resType, const std::string& name, DnsType dnsType, DnsClass dnsClass,
510  uint32_t ttl, IDnsResourceData* data);
511 
512  bool removeResource(IDnsResource* resourceToRemove);
513  };
514 
525  class DnsOverTcpLayer : public DnsLayer
526  {
527  public:
535  DnsOverTcpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
536  : DnsLayer(data, dataLen, prevLayer, packet, sizeof(uint16_t))
537  {}
538 
543  DnsOverTcpLayer() : DnsLayer(sizeof(uint16_t))
544  {}
545 
550  DnsOverTcpLayer(const DnsOverTcpLayer& other) : DnsLayer(other)
551  {}
552 
557 
562  void setTcpMessageLength(uint16_t value);
563 
564  // overridden methods
565 
569  void computeCalculateFields() override;
570  };
571 
572  // implementation of inline methods
573 
574  bool DnsLayer::isDnsPort(uint16_t port)
575  {
576  switch (port)
577  {
578  case 53:
579  case 5353:
580  case 5355:
581  return true;
582  default:
583  return false;
584  }
585  }
586 
587  bool DnsLayer::isDataValid(const uint8_t* data, size_t dataLen, bool dnsOverTcp)
588  {
589  size_t minSize = sizeof(dnshdr) + (dnsOverTcp ? sizeof(uint16_t) : 0);
590  return data && dataLen >= minSize;
591  }
592 
593 } // namespace pcpp
Definition: DnsLayer.h:94
DnsResource * getFirstAnswer() const
DnsLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
DnsResource * addAdditionalRecord(const std::string &name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, IDnsResourceData *data)
size_t getHeaderLen() const override
Definition: DnsLayer.h:444
DnsQuery * getQuery(const std::string &name, bool exactMatch) const
DnsResource * addAdditionalRecord(const std::string &name, DnsType dnsType, uint16_t customData1, uint32_t customData2, IDnsResourceData *data)
DnsResource * addAuthority(DnsResource *const copyAuthority)
static bool isDataValid(const uint8_t *data, size_t dataLen, bool dnsOverTcp=false)
Definition: DnsLayer.h:587
DnsQuery * addQuery(DnsQuery *const copyQuery)
DnsResource * getAuthority(const std::string &name, bool exactMatch) const
bool removeAdditionalRecord(DnsResource *additionalRecordToRemove)
DnsResource * addAdditionalRecord(DnsResource *const copyAdditionalRecord)
DnsResource * addAnswer(DnsResource *const copyAnswer)
bool removeQuery(const std::string &queryNameToRemove, bool exactMatch)
DnsResource * getAdditionalRecord(const std::string &name, bool exactMatch) const
size_t getQueryCount() const
DnsResource * getNextAuthority(DnsResource *authority) const
std::string toString() const override
DnsResource * getNextAnswer(DnsResource *answer) const
void computeCalculateFields() override
Definition: DnsLayer.h:452
DnsQuery * addQuery(const std::string &name, DnsType dnsType, DnsClass dnsClass)
DnsLayer(const DnsLayer &other)
DnsQuery * getNextQuery(DnsQuery *query) const
DnsResource * getFirstAdditionalRecord() const
bool removeAuthority(const std::string &authorityNameToRemove, bool exactMatch)
DnsResource * getFirstAuthority() const
bool removeQuery(DnsQuery *queryToRemove)
size_t getAuthorityCount() const
size_t getAnswerCount() const
dnshdr * getDnsHeader() const
void parseNextLayer() override
Definition: DnsLayer.h:437
DnsResource * addAuthority(const std::string &name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, IDnsResourceData *data)
bool removeAnswer(DnsResource *answerToRemove)
DnsResource * getNextAdditionalRecord(DnsResource *additionalRecord) const
DnsQuery * getFirstQuery() const
DnsResource * addAnswer(const std::string &name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, IDnsResourceData *data)
bool removeAdditionalRecord(const std::string &additionalRecordNameToRemove, bool exactMatch)
bool removeAnswer(const std::string &answerNameToRemove, bool exactMatch)
static bool isDnsPort(uint16_t port)
Definition: DnsLayer.h:574
DnsLayer & operator=(const DnsLayer &other)
bool removeAuthority(DnsResource *authorityToRemove)
OsiModelLayer getOsiModelLayer() const override
Definition: DnsLayer.h:457
size_t getAdditionalRecordCount() const
DnsResource * getAnswer(const std::string &name, bool exactMatch) const
Definition: DnsLayer.h:526
DnsOverTcpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: DnsLayer.h:535
void computeCalculateFields() override
void setTcpMessageLength(uint16_t value)
uint16_t getTcpMessageLength()
DnsOverTcpLayer()
Definition: DnsLayer.h:543
DnsOverTcpLayer(const DnsOverTcpLayer &other)
Definition: DnsLayer.h:550
Definition: DnsResource.h:137
Definition: DnsResource.h:166
Definition: DnsResourceData.h:28
Definition: DnsResource.h:30
Definition: Layer.h:69
Definition: Packet.h:27
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:364
@ OsiModelApplicationLayer
Definition: ProtocolType.h:378
DnsClass
Definition: DnsLayerEnums.h:126
DnsResourceType
Definition: DnsLayerEnums.h:143
DnsType
Definition: DnsLayerEnums.h:15
Definition: DnsLayer.h:24
uint16_t responseCode
Definition: DnsLayer.h:40
uint16_t authoritativeAnswer
Definition: DnsLayer.h:34
uint16_t queryOrResponse
Definition: DnsLayer.h:38
uint16_t zero
Definition: DnsLayer.h:46
uint16_t transactionID
Definition: DnsLayer.h:26
uint16_t numberOfAuthority
Definition: DnsLayer.h:77
uint16_t truncation
Definition: DnsLayer.h:32
uint16_t numberOfAdditional
Definition: DnsLayer.h:79
uint16_t checkingDisabled
Definition: DnsLayer.h:42
uint16_t numberOfAnswers
Definition: DnsLayer.h:75
uint16_t authenticData
Definition: DnsLayer.h:44
uint16_t opcode
Definition: DnsLayer.h:36
uint16_t recursionAvailable
Definition: DnsLayer.h:48
uint16_t recursionDesired
Definition: DnsLayer.h:30
uint16_t numberOfQuestions
Definition: DnsLayer.h:73