PcapPlusPlus
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 #if defined(WIN32) || defined(WINx64) || defined(PCAPPP_MINGW_ENV)
11 #include <winsock2.h>
12 #elif LINUX
13 #include <in.h>
14 #endif
15 
17 
22 namespace pcpp
23 {
24 
30 #pragma pack(push, 1)
31  struct dnshdr
32  {
34  uint16_t transactionID;
35 #if (BYTE_ORDER == LITTLE_ENDIAN)
36  uint16_t
40  truncation:1,
44  opcode:4,
48  responseCode:4,
52  authenticData:1,
54  zero:1,
57 #elif (BYTE_ORDER == BIG_ENDIAN)
58  uint16_t
62  opcode:4,
66  truncation:1,
72  zero:1,
74  authenticData:1,
78  responseCode:4;
79 #endif
80 
83  uint16_t numberOfAnswers;
88  };
89 #pragma pack(pop)
90 
91 
92  // forward declarations
93  class DnsQuery;
94  class IDnsResource;
95  class DnsResource;
96  class IDnsResourceData;
97 
98 
104  class DnsLayer : public Layer
105  {
106  friend class IDnsResource;
107  friend class DnsQuery;
108  friend class DnsResource;
109 
110  public:
111 
119  DnsLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
120 
124  DnsLayer();
125 
129  DnsLayer(const DnsLayer& other);
130 
134  DnsLayer& operator=(const DnsLayer& other);
135 
136  virtual ~DnsLayer();
137 
143  inline dnshdr* getDnsHeader() { return (dnshdr*)m_Data; }
144 
152  DnsQuery* getQuery(const std::string& name, bool exactMatch);
153 
158 
164  DnsQuery* getNextQuery(DnsQuery* query);
165 
169  size_t getQueryCount();
170 
179  DnsQuery* addQuery(const std::string& name, DnsType dnsType, DnsClass dnsClass);
180 
187  DnsQuery* addQuery(DnsQuery* const copyQuery);
188 
195  bool removeQuery(const std::string& queryNameToRemove, bool exactMatch);
196 
202  bool removeQuery(DnsQuery* queryToRemove);
203 
211  DnsResource* getAnswer(const std::string& name, bool exactMatch);
212 
217 
224 
228  size_t getAnswerCount();
229 
242  DnsResource* addAnswer(const std::string& name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, IDnsResourceData* data);
243 
250  DnsResource* addAnswer(DnsResource* const copyAnswer);
251 
258  bool removeAnswer(const std::string& answerNameToRemove, bool exactMatch);
259 
265  bool removeAnswer(DnsResource* answerToRemove);
266 
267 
275  DnsResource* getAuthority(const std::string& name, bool exactMatch);
276 
281 
288 
292  size_t getAuthorityCount();
293 
306  DnsResource* addAuthority(const std::string& name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, IDnsResourceData* data);
307 
314  DnsResource* addAuthority(DnsResource* const copyAuthority);
315 
322  bool removeAuthority(const std::string& authorityNameToRemove, bool exactMatch);
323 
329  bool removeAuthority(DnsResource* authorityToRemove);
330 
331 
340  DnsResource* getAdditionalRecord(const std::string& name, bool exactMatch);
341 
346 
353  DnsResource* getNextAdditionalRecord(DnsResource* additionalRecord);
354 
358  size_t getAdditionalRecordCount();
359 
372  DnsResource* addAdditionalRecord(const std::string& name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, IDnsResourceData* data);
373 
387  DnsResource* addAdditionalRecord(const std::string& name, DnsType dnsType, uint16_t customData1, uint32_t customData2, IDnsResourceData* data);
388 
396  DnsResource* addAdditionalRecord(DnsResource* const copyAdditionalRecord);
397 
404  bool removeAdditionalRecord(const std::string& additionalRecordNameToRemove, bool exactMatch);
405 
411  bool removeAdditionalRecord(DnsResource* additionalRecordToRemove);
412 
413  // implement abstract methods
414 
418  void parseNextLayer() {}
419 
424  inline size_t getHeaderLen() { return m_DataLen; } //No layer above DNS
425 
430 
431  std::string toString();
432 
434 
438  static const std::map<uint16_t, bool>* getDNSPortMap();
439  private:
440  IDnsResource* m_ResourceList;
441  DnsQuery* m_FirstQuery;
442  DnsResource* m_FirstAnswer;
443  DnsResource* m_FirstAuthority;
444  DnsResource* m_FirstAdditional;
445 
446  IDnsResource* getFirstResource(DnsResourceType resType);
447  void setFirstResource(DnsResourceType resType, IDnsResource* resource);
448 
449  using Layer::extendLayer;
450  bool extendLayer(int offsetInLayer, size_t numOfBytesToExtend, IDnsResource* resource);
451 
452  using Layer::shortenLayer;
453  bool shortenLayer(int offsetInLayer, size_t numOfBytesToShorten, IDnsResource* resource);
454 
455  IDnsResource* getResourceByName(IDnsResource* startFrom, size_t resourceCount, const std::string& name, bool exactMatch);
456 
457  void parseResources();
458 
459  DnsResource* addResource(DnsResourceType resType, const std::string& name, DnsType dnsType, DnsClass dnsClass,
460  uint32_t ttl, IDnsResourceData* data);
461 
462  bool removeResource(IDnsResource* resourceToRemove);
463 
464  };
465 
466 } // namespace pcpp
467 
468 #endif /* PACKETPP_DNS_LAYER */
bool removeQuery(const std::string &queryNameToRemove, bool exactMatch)
dnshdr * getDnsHeader()
Definition: DnsLayer.h:143
DnsResource * getFirstAdditionalRecord()
uint16_t authoritativeAnswer
Definition: DnsLayer.h:38
DnsResource * addAuthority(const std::string &name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, IDnsResourceData *data)
uint16_t recursionAvailable
Definition: DnsLayer.h:38
uint16_t checkingDisabled
Definition: DnsLayer.h:38
uint16_t numberOfQuestions
Definition: DnsLayer.h:81
bool removeAuthority(const std::string &authorityNameToRemove, bool exactMatch)
size_t getAuthorityCount()
void parseNextLayer()
Definition: DnsLayer.h:418
DnsResource * getAnswer(const std::string &name, bool exactMatch)
DnsResource * addAdditionalRecord(const std::string &name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, IDnsResourceData *data)
DnsClass
Definition: DnsLayerEnums.h:127
uint16_t numberOfAdditional
Definition: DnsLayer.h:87
Definition: DnsLayer.h:31
DnsResource * getAuthority(const std::string &name, bool exactMatch)
DnsResource * getAdditionalRecord(const std::string &name, bool exactMatch)
uint16_t truncation
Definition: DnsLayer.h:38
uint16_t queryOrResponse
Definition: DnsLayer.h:38
uint16_t zero
Definition: DnsLayer.h:38
Definition: Layer.h:70
size_t getQueryCount()
size_t getAdditionalRecordCount()
DnsResource * getFirstAuthority()
DnsQuery * getQuery(const std::string &name, bool exactMatch)
Definition: DnsResource.h:30
Definition: Packet.h:26
DnsResource * getNextAuthority(DnsResource *authority)
static const std::map< uint16_t, bool > * getDNSPortMap()
OsiModelLayer getOsiModelLayer()
Definition: DnsLayer.h:433
DnsResourceType
Definition: DnsLayerEnums.h:145
Definition: ProtocolType.h:228
uint16_t transactionID
Definition: DnsLayer.h:34
uint16_t recursionDesired
Definition: DnsLayer.h:38
OsiModelLayer
Definition: ProtocolType.h:213
DnsQuery * getFirstQuery()
uint16_t authenticData
Definition: DnsLayer.h:38
std::string toString()
size_t getAnswerCount()
DnsResource * getNextAnswer(DnsResource *answer)
DnsQuery * addQuery(const std::string &name, DnsType dnsType, DnsClass dnsClass)
DnsResource * addAnswer(const std::string &name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, IDnsResourceData *data)
uint16_t numberOfAnswers
Definition: DnsLayer.h:83
DnsResource * getNextAdditionalRecord(DnsResource *additionalRecord)
Definition: DnsResource.h:150
bool removeAdditionalRecord(const std::string &additionalRecordNameToRemove, bool exactMatch)
Definition: DnsLayer.h:104
DnsType
Definition: DnsLayerEnums.h:15
DnsResource * getFirstAnswer()
uint16_t responseCode
Definition: DnsLayer.h:38
DnsQuery * getNextQuery(DnsQuery *query)
uint16_t opcode
Definition: DnsLayer.h:38
DnsLayer & operator=(const DnsLayer &other)
uint16_t numberOfAuthority
Definition: DnsLayer.h:85
bool removeAnswer(const std::string &answerNameToRemove, bool exactMatch)
size_t getHeaderLen()
Definition: DnsLayer.h:424
Definition: DnsResource.h:128
void computeCalculateFields()
Definition: DnsLayer.h:429
The main namespace for the PcapPlusPlus lib.
Definition: DnsResourceData.h:34