PcapPlusPlus  19.12
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 #include <vector>
9 #include <map>
10 
12 
17 namespace pcpp
18 {
19 
25 #pragma pack(push, 1)
26  struct dnshdr
27  {
29  uint16_t transactionID;
30 #if (BYTE_ORDER == LITTLE_ENDIAN)
31  uint16_t
35  truncation:1,
39  opcode:4,
43  responseCode:4,
47  authenticData:1,
49  zero:1,
52 #elif (BYTE_ORDER == BIG_ENDIAN)
53  uint16_t
57  opcode:4,
61  truncation:1,
67  zero:1,
69  authenticData:1,
73  responseCode:4;
74 #endif
75 
78  uint16_t numberOfAnswers;
83  };
84 #pragma pack(pop)
85 
86 
87  // forward declarations
88  class DnsQuery;
89  class IDnsResource;
90  class DnsResource;
91  class IDnsResourceData;
92 
93 
99  class DnsLayer : public Layer
100  {
101  friend class IDnsResource;
102  friend class DnsQuery;
103  friend class DnsResource;
104 
105  public:
106 
114  DnsLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
115 
119  DnsLayer();
120 
124  DnsLayer(const DnsLayer& other);
125 
129  DnsLayer& operator=(const DnsLayer& other);
130 
131  virtual ~DnsLayer();
132 
138  dnshdr* getDnsHeader() const { return (dnshdr*)m_Data; }
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 
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 
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 
426  std::string toString() const;
427 
429 
433  static const std::map<uint16_t, bool>* getDNSPortMap();
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 } // namespace pcpp
462 
463 #endif /* PACKETPP_DNS_LAYER */
pcpp::DnsLayer::getNextQuery
DnsQuery * getNextQuery(DnsQuery *query) const
pcpp::OsiModelLayer
OsiModelLayer
Definition: ProtocolType.h:224
pcpp::DnsLayer::getHeaderLen
size_t getHeaderLen() const
Definition: DnsLayer.h:419
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:33
pcpp::OsiModelApplicationLayer
Definition: ProtocolType.h:239
pcpp::dnshdr::recursionDesired
uint16_t recursionDesired
Definition: DnsLayer.h:33
DnsLayerEnums.h
pcpp::DnsClass
DnsClass
Definition: DnsLayerEnums.h:127
pcpp::dnshdr::authenticData
uint16_t authenticData
Definition: DnsLayer.h:33
pcpp::dnshdr::checkingDisabled
uint16_t checkingDisabled
Definition: DnsLayer.h:33
Layer.h
pcpp::IDnsResourceData
Definition: DnsResourceData.h:39
pcpp::DnsLayer::getDnsHeader
dnshdr * getDnsHeader() const
Definition: DnsLayer.h:138
pcpp::dnshdr::authoritativeAnswer
uint16_t authoritativeAnswer
Definition: DnsLayer.h:33
pcpp::DnsLayer::getFirstAuthority
DnsResource * getFirstAuthority() const
pcpp::dnshdr::numberOfQuestions
uint16_t numberOfQuestions
Definition: DnsLayer.h:76
pcpp::dnshdr::numberOfAuthority
uint16_t numberOfAuthority
Definition: DnsLayer.h:80
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:33
pcpp::DnsLayer::getAnswer
DnsResource * getAnswer(const std::string &name, bool exactMatch) const
pcpp::dnshdr::recursionAvailable
uint16_t recursionAvailable
Definition: DnsLayer.h:33
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:33
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:78
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:428
pcpp::DnsType
DnsType
Definition: DnsLayerEnums.h:15
pcpp::DnsLayer::getDNSPortMap
static const std::map< uint16_t, bool > * getDNSPortMap()
pcpp::DnsLayer::getAdditionalRecordCount
size_t getAdditionalRecordCount() const
pcpp::DnsLayer::getNextAnswer
DnsResource * getNextAnswer(DnsResource *answer) const
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:26
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:424
pcpp::dnshdr::queryOrResponse
uint16_t queryOrResponse
Definition: DnsLayer.h:33
pcpp::DnsQuery
Definition: DnsResource.h:128
pcpp::DnsLayer::getAnswerCount
size_t getAnswerCount() const
pcpp::dnshdr::transactionID
uint16_t transactionID
Definition: DnsLayer.h:29
pcpp::DnsLayer::getFirstAdditionalRecord
DnsResource * getFirstAdditionalRecord() const
pcpp::DnsResource
Definition: DnsResource.h:150
pcpp::dnshdr::numberOfAdditional
uint16_t numberOfAdditional
Definition: DnsLayer.h:82
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:99
pcpp::DnsLayer::parseNextLayer
void parseNextLayer()
Definition: DnsLayer.h:413