PcapPlusPlus  21.05
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 
121  DnsLayer(const DnsLayer& other);
122 
126  DnsLayer& operator=(const DnsLayer& other);
127 
128  virtual ~DnsLayer();
129 
135  dnshdr* getDnsHeader() const;
136 
144  DnsQuery* getQuery(const std::string& name, bool exactMatch) const;
145 
149  DnsQuery* getFirstQuery() const;
150 
156  DnsQuery* getNextQuery(DnsQuery* query) const;
157 
161  size_t getQueryCount() const;
162 
171  DnsQuery* addQuery(const std::string& name, DnsType dnsType, DnsClass dnsClass);
172 
179  DnsQuery* addQuery(DnsQuery* const copyQuery);
180 
187  bool removeQuery(const std::string& queryNameToRemove, bool exactMatch);
188 
194  bool removeQuery(DnsQuery* queryToRemove);
195 
203  DnsResource* getAnswer(const std::string& name, bool exactMatch) const;
204 
208  DnsResource* getFirstAnswer() const;
209 
215  DnsResource* getNextAnswer(DnsResource* answer) const;
216 
220  size_t getAnswerCount() const;
221 
234  DnsResource* addAnswer(const std::string& name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, IDnsResourceData* data);
235 
242  DnsResource* addAnswer(DnsResource* const copyAnswer);
243 
250  bool removeAnswer(const std::string& answerNameToRemove, bool exactMatch);
251 
257  bool removeAnswer(DnsResource* answerToRemove);
258 
259 
267  DnsResource* getAuthority(const std::string& name, bool exactMatch) const;
268 
272  DnsResource* getFirstAuthority() const;
273 
279  DnsResource* getNextAuthority(DnsResource* authority) const;
280 
284  size_t getAuthorityCount() const;
285 
298  DnsResource* addAuthority(const std::string& name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, IDnsResourceData* data);
299 
306  DnsResource* addAuthority(DnsResource* const copyAuthority);
307 
314  bool removeAuthority(const std::string& authorityNameToRemove, bool exactMatch);
315 
321  bool removeAuthority(DnsResource* authorityToRemove);
322 
323 
332  DnsResource* getAdditionalRecord(const std::string& name, bool exactMatch) const;
333 
337  DnsResource* getFirstAdditionalRecord() const;
338 
345  DnsResource* getNextAdditionalRecord(DnsResource* additionalRecord) const;
346 
350  size_t getAdditionalRecordCount() const;
351 
364  DnsResource* addAdditionalRecord(const std::string& name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, IDnsResourceData* data);
365 
379  DnsResource* addAdditionalRecord(const std::string& name, DnsType dnsType, uint16_t customData1, uint32_t customData2, IDnsResourceData* data);
380 
388  DnsResource* addAdditionalRecord(DnsResource* const copyAdditionalRecord);
389 
396  bool removeAdditionalRecord(const std::string& additionalRecordNameToRemove, bool exactMatch);
397 
403  bool removeAdditionalRecord(DnsResource* additionalRecordToRemove);
404 
405  // implement abstract methods
406 
410  void parseNextLayer() {}
411 
416  size_t getHeaderLen() const { return m_DataLen; } //No layer above DNS
417 
421  virtual void computeCalculateFields() {}
422 
423  std::string toString() const;
424 
426 
431  static inline bool isDnsPort(uint16_t port);
432 
441  static inline bool isDataValid(const uint8_t* data, size_t dataLen, bool dnsOverTcp = false);
442 
443  protected:
444  DnsLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, size_t offsetAdjustment);
445  DnsLayer(size_t offsetAdjustment);
446 
447  private:
448  IDnsResource* m_ResourceList;
449  DnsQuery* m_FirstQuery;
450  DnsResource* m_FirstAnswer;
451  DnsResource* m_FirstAuthority;
452  DnsResource* m_FirstAdditional;
453  uint16_t m_OffsetAdjustment;
454 
455  size_t getBasicHeaderSize();
456  void init(size_t offsetAdjustment, bool callParseResource);
457  void initNewLayer(size_t offsetAdjustment);
458 
459  IDnsResource* getFirstResource(DnsResourceType resType) const;
460  void setFirstResource(DnsResourceType resType, IDnsResource* resource);
461 
462  using Layer::extendLayer;
463  bool extendLayer(int offsetInLayer, size_t numOfBytesToExtend, IDnsResource* resource);
464 
465  using Layer::shortenLayer;
466  bool shortenLayer(int offsetInLayer, size_t numOfBytesToShorten, IDnsResource* resource);
467 
468  IDnsResource* getResourceByName(IDnsResource* startFrom, size_t resourceCount, const std::string& name, bool exactMatch) const;
469 
470  void parseResources();
471 
472  DnsResource* addResource(DnsResourceType resType, const std::string& name, DnsType dnsType, DnsClass dnsClass,
473  uint32_t ttl, IDnsResourceData* data);
474 
475  bool removeResource(IDnsResource* resourceToRemove);
476 
477  };
478 
479 
480 
491  class DnsOverTcpLayer : public DnsLayer
492  {
493  public:
494 
502  DnsOverTcpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
503  : DnsLayer(data, dataLen, prevLayer, packet, sizeof(uint16_t)) {}
504 
508  DnsOverTcpLayer() : DnsLayer(sizeof(uint16_t)) {}
509 
513  DnsOverTcpLayer(const DnsOverTcpLayer& other) : DnsLayer(other) {}
514 
518  uint16_t getTcpMessageLength();
519 
524  void setTcpMessageLength(uint16_t value);
525 
526 
527  // overriden methods
528 
532  void computeCalculateFields();
533  };
534 
535 
536  // implementation of inline methods
537 
538  bool DnsLayer::isDnsPort(uint16_t port)
539  {
540  switch (port)
541  {
542  case 53:
543  case 5353:
544  case 5355:
545  return true;
546  default:
547  return false;
548  }
549  }
550 
551  bool DnsLayer::isDataValid(const uint8_t* data, size_t dataLen, bool dnsOverTcp)
552  {
553  size_t minSize = sizeof(dnshdr) + (dnsOverTcp ? sizeof(uint16_t) : 0);
554  return dataLen >= minSize;
555  }
556 
557 } // namespace pcpp
558 
559 #endif /* PACKETPP_DNS_LAYER */
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:253
DnsOverTcpLayer(const DnsOverTcpLayer &other)
Definition: DnsLayer.h:513
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:425
Definition: ProtocolType.h:268
Definition: Layer.h:70
Definition: Packet.h:26
DnsOverTcpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: DnsLayer.h:502
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:551
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:416
virtual void computeCalculateFields()
Definition: DnsLayer.h:421
uint16_t recursionDesired
Definition: DnsLayer.h:31
DnsResourceType
Definition: DnsLayerEnums.h:145
void parseNextLayer()
Definition: DnsLayer.h:410
Definition: DnsLayer.h:24
uint16_t transactionID
Definition: DnsLayer.h:27
Definition: DnsLayer.h:491
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:508
static bool isDnsPort(uint16_t port)
Definition: DnsLayer.h:538
uint16_t recursionAvailable
Definition: DnsLayer.h:31