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
37  opcode:4,
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 
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 
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 */
pcpp::DnsLayer::getNextQuery
DnsQuery * getNextQuery(DnsQuery *query) const
pcpp::OsiModelLayer
OsiModelLayer
Definition: ProtocolType.h:233
pcpp::DnsLayer::getHeaderLen
size_t getHeaderLen() const
Definition: DnsLayer.h:417
pcpp::DnsLayer::removeQuery
bool removeQuery(const std::string &queryNameToRemove, bool exactMatch)
pcpp::Packet
Definition: Packet.h:26
pcpp::dnshdr::opcode
uint16_t opcode
Definition: DnsLayer.h:37
pcpp::OsiModelApplicationLayer
@ OsiModelApplicationLayer
Definition: ProtocolType.h:248
pcpp::dnshdr::recursionDesired
uint16_t recursionDesired
Definition: DnsLayer.h:31
DnsLayerEnums.h
pcpp::DnsClass
DnsClass
Definition: DnsLayerEnums.h:127
pcpp::dnshdr::authenticData
uint16_t authenticData
Definition: DnsLayer.h:45
pcpp::dnshdr::checkingDisabled
uint16_t checkingDisabled
Definition: DnsLayer.h:43
Layer.h
pcpp::IDnsResourceData
Definition: DnsResourceData.h:40
pcpp::DnsLayer::getDnsHeader
dnshdr * getDnsHeader() const
Definition: DnsLayer.h:136
pcpp::dnshdr::authoritativeAnswer
uint16_t authoritativeAnswer
Definition: DnsLayer.h:35
pcpp::DnsLayer::getFirstAuthority
DnsResource * getFirstAuthority() const
pcpp::dnshdr::numberOfQuestions
uint16_t numberOfQuestions
Definition: DnsLayer.h:74
pcpp::dnshdr::numberOfAuthority
uint16_t numberOfAuthority
Definition: DnsLayer.h:78
pcpp::DnsLayer::getAdditionalRecord
DnsResource * getAdditionalRecord(const std::string &name, bool exactMatch) const
pcpp::DnsLayer::addAnswer
DnsResource * addAnswer(const std::string &name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, IDnsResourceData *data)
pcpp::DnsLayer::DnsLayer
DnsLayer()
pcpp::IDnsResource
Definition: DnsResource.h:30
pcpp::DnsLayer::removeAdditionalRecord
bool removeAdditionalRecord(const std::string &additionalRecordNameToRemove, bool exactMatch)
DnsResourceData.h
pcpp::dnshdr::responseCode
uint16_t responseCode
Definition: DnsLayer.h:41
pcpp::DnsLayer::getAnswer
DnsResource * getAnswer(const std::string &name, bool exactMatch) const
pcpp::dnshdr::recursionAvailable
uint16_t recursionAvailable
Definition: DnsLayer.h:49
pcpp::DnsLayer::getFirstQuery
DnsQuery * getFirstQuery() const
DnsResource.h
pcpp::DnsLayer::removeAnswer
bool removeAnswer(const std::string &answerNameToRemove, bool exactMatch)
pcpp::dnshdr::zero
uint16_t zero
Definition: DnsLayer.h:47
pcpp::DnsLayer::toString
std::string toString() const
pcpp::DnsLayer::getAuthority
DnsResource * getAuthority(const std::string &name, bool exactMatch) const
pcpp::dnshdr::numberOfAnswers
uint16_t numberOfAnswers
Definition: DnsLayer.h:76
pcpp::DnsLayer::getNextAuthority
DnsResource * getNextAuthority(DnsResource *authority) const
pcpp::DnsLayer::getAuthorityCount
size_t getAuthorityCount() const
pcpp::DnsLayer::getQuery
DnsQuery * getQuery(const std::string &name, bool exactMatch) const
pcpp::DnsLayer::getFirstAnswer
DnsResource * getFirstAnswer() const
pcpp::DnsLayer::getOsiModelLayer
OsiModelLayer getOsiModelLayer() const
Definition: DnsLayer.h:426
pcpp::DnsType
DnsType
Definition: DnsLayerEnums.h:15
pcpp::DnsLayer::getAdditionalRecordCount
size_t getAdditionalRecordCount() const
pcpp::DnsLayer::getNextAnswer
DnsResource * getNextAnswer(DnsResource *answer) const
pcpp::DnsLayer::isDnsPort
static bool isDnsPort(uint16_t port)
Definition: DnsLayer.h:464
pcpp
The main namespace for the PcapPlusPlus lib.
pcpp::DnsLayer::operator=
DnsLayer & operator=(const DnsLayer &other)
pcpp::DnsLayer::getQueryCount
size_t getQueryCount() const
pcpp::DnsLayer::removeAuthority
bool removeAuthority(const std::string &authorityNameToRemove, bool exactMatch)
pcpp::Layer
Definition: Layer.h:70
pcpp::dnshdr
Definition: DnsLayer.h:24
pcpp::DnsLayer::addAdditionalRecord
DnsResource * addAdditionalRecord(const std::string &name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, IDnsResourceData *data)
pcpp::DnsLayer::addQuery
DnsQuery * addQuery(const std::string &name, DnsType dnsType, DnsClass dnsClass)
pcpp::dnshdr::truncation
uint16_t truncation
Definition: DnsLayer.h:33
pcpp::DnsResourceType
DnsResourceType
Definition: DnsLayerEnums.h:145
pcpp::DnsLayer::computeCalculateFields
void computeCalculateFields()
Definition: DnsLayer.h:422
pcpp::dnshdr::queryOrResponse
uint16_t queryOrResponse
Definition: DnsLayer.h:39
pcpp::DnsQuery
Definition: DnsResource.h:128
pcpp::DnsLayer::getAnswerCount
size_t getAnswerCount() const
pcpp::dnshdr::transactionID
uint16_t transactionID
Definition: DnsLayer.h:27
pcpp::DnsLayer::getFirstAdditionalRecord
DnsResource * getFirstAdditionalRecord() const
pcpp::DnsResource
Definition: DnsResource.h:150
pcpp::dnshdr::numberOfAdditional
uint16_t numberOfAdditional
Definition: DnsLayer.h:80
pcpp::DnsLayer::getNextAdditionalRecord
DnsResource * getNextAdditionalRecord(DnsResource *additionalRecord) const
pcpp::DnsLayer::addAuthority
DnsResource * addAuthority(const std::string &name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, IDnsResourceData *data)
pcpp::DnsLayer
Definition: DnsLayer.h:97
pcpp::DnsLayer::parseNextLayer
void parseNextLayer()
Definition: DnsLayer.h:411