PcapPlusPlus
DnsLayer.h
Go to the documentation of this file.
1 #ifndef PACKETPP_DNS_LAYER
2 #define PACKETPP_DNS_LAYER
3 
4 #include "Layer.h"
5 #include <vector>
6 #include <map>
7 #if defined(WIN32) || defined(WINx64)
8 #include <winsock2.h>
9 #elif LINUX
10 #include <in.h>
11 #endif
12 
14 
19 namespace pcpp
20 {
21 
27 #pragma pack(push, 1)
28  struct dnshdr
29  {
31  uint16_t transactionID;
32 #if (BYTE_ORDER == LITTLE_ENDIAN)
33  uint16_t
37  truncation:1,
41  opcode:4,
45  responseCode:4,
49  authenticData:1,
51  zero:1,
54 #elif (BYTE_ORDER == BIG_ENDIAN)
55  uint16_t
59  opcode:4,
63  truncation:1,
69  zero:1,
71  authenticData:1,
75  responseCode:4;
76 #endif
77 
80  uint16_t numberOfAnswers;
85  };
86 #pragma pack(pop)
87 
88 
92  enum DnsType
93  {
198  };
199 
200 
204  enum DnsClass
205  {
216  };
217 
218 
219  class DnsLayer;
220 
221 
228  {
229  protected:
230  friend class DnsLayer;
231 
232  protected:
233  DnsLayer* m_DnsLayer;
234  size_t m_OffsetInLayer;
235  IDnsResource* m_NextResource;
236  std::string m_DecodedName;
237  size_t m_NameLength;
238  uint8_t* m_ExternalRawData;
239 
240  IDnsResource(DnsLayer* dnsLayer, size_t offsetInLayer);
241 
242  IDnsResource(uint8_t* emptyRawData);
243 
244  size_t decodeName(const char* encodedName, std::string& result, int iteration = 1);
245  void encodeName(const std::string& decodedName, char* result, size_t& resultLen);
246 
247  inline IDnsResource* getNextResource() { return m_NextResource; }
248  inline void setNexResource(IDnsResource* next) { m_NextResource = next; }
249 
250  uint8_t* getRawData();
251 
252  void setDnsLayer(DnsLayer* dnsLayer, size_t offsetInLayer);
253 
254  public:
259  {
261  DnsQuery = 0,
268  };
269 
270  virtual ~IDnsResource() {}
271 
276 
281  void setDnsType(DnsType newType);
282 
287 
292  void setDnsClass(DnsClass newClass);
293 
297  std::string getName() { return m_DecodedName; }
298 
304  bool setName(const std::string& newName);
305 
306 
307  // abstract methods
308 
312  virtual size_t getSize() = 0;
313 
317  virtual ResourceType getType() = 0;
318  };
319 
320 
325  class DnsQuery : public IDnsResource
326  {
327  friend class DnsLayer;
328 
329  private:
330  DnsQuery(DnsLayer* dnsLayer, size_t offsetInLayer) : IDnsResource(dnsLayer, offsetInLayer) {}
331 
332  DnsQuery(uint8_t* emptyRawData) : IDnsResource(emptyRawData) {}
333 
334  public:
335  virtual ~DnsQuery() {}
336 
337  // abstract methods
338  virtual size_t getSize() { return m_NameLength + 2*sizeof(uint16_t); }
340  };
341 
342 
347  class DnsResource : public IDnsResource
348  {
349  friend class DnsLayer;
350 
351  private:
352  ResourceType m_ResourceType;
353 
354  DnsResource(DnsLayer* dnsLayer, size_t offsetInLayer, ResourceType resourceType) : IDnsResource(dnsLayer, offsetInLayer) { m_ResourceType = resourceType; }
355 
356  DnsResource(uint8_t* emptyRawData, ResourceType resType) : IDnsResource(emptyRawData), m_ResourceType(resType) {}
357 
358  public:
359  virtual ~DnsResource() {}
360 
364  uint32_t getTTL();
365 
370  void setTTL(uint32_t newTTL);
371 
375  size_t getDataLength();
376 
385  std::string getDataAsString();
386 
401  bool setData(const std::string& dataAsString);
402 
408  uint16_t getCustomDnsClass();
409 
415  void setCustomDnsClass(uint16_t customValue);
416 
417  // abstract methods
418  virtual size_t getSize() { return m_NameLength + 3*sizeof(uint16_t) + sizeof(uint32_t) + getDataLength(); }
419  virtual ResourceType getType() { return m_ResourceType; }
420 
421  };
422 
423 
429  class DnsLayer : public Layer
430  {
431  friend class IDnsResource;
432  friend class DnsQuery;
433  friend class DnsResource;
434 
435  public:
436 
444  DnsLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
445 
449  DnsLayer();
450 
454  DnsLayer(const DnsLayer& other);
455 
459  DnsLayer& operator=(const DnsLayer& other);
460 
461  virtual ~DnsLayer();
462 
468  inline dnshdr* getDnsHeader() { return (dnshdr*)m_Data; }
469 
477  DnsQuery* getQuery(const std::string& name, bool exactMatch);
478 
483 
489  DnsQuery* getNextQuery(DnsQuery* query);
490 
494  size_t getQueryCount();
495 
504  DnsQuery* addQuery(const std::string& name, DnsType dnsType, DnsClass dnsClass);
505 
512  DnsQuery* addQuery(DnsQuery* const copyQuery);
513 
520  bool removeQuery(const std::string& queryNameToRemove, bool exactMatch);
521 
527  bool removeQuery(DnsQuery* queryToRemove);
528 
536  DnsResource* getAnswer(const std::string& name, bool exactMatch);
537 
542 
549 
553  size_t getAnswerCount();
554 
565  DnsResource* addAnswer(const std::string& name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, const std::string& data);
566 
573  DnsResource* addAnswer(DnsResource* const copyAnswer);
574 
581  bool removeAnswer(const std::string& answerNameToRemove, bool exactMatch);
582 
588  bool removeAnswer(DnsResource* answerToRemove);
589 
590 
598  DnsResource* getAuthority(const std::string& name, bool exactMatch);
599 
604 
611 
615  size_t getAuthorityCount();
616 
627  DnsResource* addAuthority(const std::string& name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, const std::string& data);
628 
635  DnsResource* addAuthority(DnsResource* const copyAuthority);
636 
643  bool removeAuthority(const std::string& authorityNameToRemove, bool exactMatch);
644 
650  bool removeAuthority(DnsResource* authorityToRemove);
651 
652 
661  DnsResource* getAdditionalRecord(const std::string& name, bool exactMatch);
662 
667 
674  DnsResource* getNextAdditionalRecord(DnsResource* additionalRecord);
675 
679  size_t getAdditionalRecordCount();
680 
691  DnsResource* addAdditionalRecord(const std::string& name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, const std::string& data);
692 
704  DnsResource* addAdditionalRecord(const std::string& name, DnsType dnsType, uint16_t customData1, uint32_t customData2, const std::string& data);
705 
713  DnsResource* addAdditionalRecord(DnsResource* const copyAdditionalRecord);
714 
721  bool removeAdditionalRecord(const std::string& additionalRecordNameToRemove, bool exactMatch);
722 
728  bool removeAdditionalRecord(DnsResource* additionalRecordToRemove);
729 
730  // implement abstract methods
731 
735  void parseNextLayer() {}
736 
741  inline size_t getHeaderLen() { return m_DataLen; } //No layer above DNS
742 
747 
748  std::string toString();
749 
751 
755  static const std::map<uint16_t, bool>* getDNSPortMap();
756  private:
757  IDnsResource* m_ResourceList;
758  DnsQuery* m_FirstQuery;
759  DnsResource* m_FirstAnswer;
760  DnsResource* m_FirstAuthority;
761  DnsResource* m_FirstAdditional;
762 
763  IDnsResource* getFirstResource(IDnsResource::ResourceType resType);
764  void setFirstResource(IDnsResource::ResourceType resType, IDnsResource* resource);
765 
766  using Layer::extendLayer;
767  bool extendLayer(int offsetInLayer, size_t numOfBytesToExtend, IDnsResource* resource);
768 
769  using Layer::shortenLayer;
770  bool shortenLayer(int offsetInLayer, size_t numOfBytesToShorten, IDnsResource* resource);
771 
772  IDnsResource* getResourceByName(IDnsResource* startFrom, size_t resourceCount, const std::string& name, bool exactMatch);
773 
774  void parseResources();
775 
776  DnsResource* addResource(IDnsResource::ResourceType resType, const std::string& name, DnsType dnsType, DnsClass dnsClass,
777  uint32_t ttl, const std::string& data);
778 
779  bool removeResource(IDnsResource* resourceToRemove);
780 
781  };
782 
783 } // namespace pcpp
784 
785 #endif /* PACKETPP_DNS_LAYER */
size_t getAuthorityCount()
Definition: DnsLayer.h:209
size_t getHeaderLen()
Definition: DnsLayer.h:741
Definition: DnsLayer.h:127
Definition: DnsLayer.h:117
uint16_t authoritativeAnswer
Definition: DnsLayer.h:35
void setCustomDnsClass(uint16_t customValue)
uint16_t zero
Definition: DnsLayer.h:35
Definition: DnsLayer.h:263
DnsResource * getNextAnswer(DnsResource *answer)
DnsResource * getNextAuthority(DnsResource *authority)
uint16_t recursionAvailable
Definition: DnsLayer.h:35
Definition: DnsLayer.h:99
std::string toString()
bool removeAnswer(const std::string &answerNameToRemove, bool exactMatch)
uint16_t responseCode
Definition: DnsLayer.h:35
DnsType
Definition: DnsLayer.h:92
void setDnsClass(DnsClass newClass)
Definition: DnsLayer.h:113
Definition: DnsLayer.h:143
void setDnsType(DnsType newType)
Definition: DnsLayer.h:165
DnsResource * getAnswer(const std::string &name, bool exactMatch)
uint16_t recursionDesired
Definition: DnsLayer.h:35
ResourceType
Definition: DnsLayer.h:258
Definition: DnsLayer.h:267
Definition: DnsLayer.h:153
Definition: DnsLayer.h:213
void parseNextLayer()
Definition: DnsLayer.h:735
Definition: DnsLayer.h:207
Definition: DnsLayer.h:141
Definition: DnsLayer.h:227
Definition: ProtocolType.h:218
uint16_t transactionID
Definition: DnsLayer.h:31
Definition: DnsLayer.h:133
Definition: DnsLayer.h:161
dnshdr * getDnsHeader()
Definition: DnsLayer.h:468
Definition: DnsLayer.h:179
DnsResource * getFirstAuthority()
Definition: DnsLayer.h:125
Definition: DnsLayer.h:129
virtual size_t getSize()
Definition: DnsLayer.h:418
Definition: DnsLayer.h:177
DnsResource * addAuthority(const std::string &name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, const std::string &data)
DnsResource * getFirstAnswer()
uint16_t numberOfQuestions
Definition: DnsLayer.h:78
DnsClass getDnsClass()
Definition: DnsLayer.h:121
Definition: DnsLayer.h:173
Definition: DnsLayer.h:97
OsiModelLayer
Definition: ProtocolType.h:203
Definition: DnsLayer.h:103
uint16_t opcode
Definition: DnsLayer.h:35
virtual ResourceType getType()=0
Definition: DnsLayer.h:197
DnsType getDnsType()
Definition: DnsLayer.h:105
Definition: DnsLayer.h:145
Definition: DnsLayer.h:211
uint16_t queryOrResponse
Definition: DnsLayer.h:35
Definition: DnsLayer.h:175
Definition: DnsLayer.h:155
virtual size_t getSize()=0
Definition: DnsLayer.h:147
size_t getQueryCount()
Definition: Packet.h:26
Definition: DnsLayer.h:187
Definition: DnsLayer.h:131
Definition: DnsLayer.h:151
DnsQuery * addQuery(const std::string &name, DnsType dnsType, DnsClass dnsClass)
size_t getDataLength()
Definition: DnsLayer.h:347
Definition: DnsLayer.h:163
DnsResource * getFirstAdditionalRecord()
DnsResource * getNextAdditionalRecord(DnsResource *additionalRecord)
Definition: DnsLayer.h:429
DnsClass
Definition: DnsLayer.h:204
uint16_t numberOfAdditional
Definition: DnsLayer.h:84
uint16_t numberOfAnswers
Definition: DnsLayer.h:80
uint16_t getCustomDnsClass()
virtual size_t getSize()
Definition: DnsLayer.h:338
std::string getName()
Definition: DnsLayer.h:297
Definition: DnsLayer.h:171
uint16_t authenticData
Definition: DnsLayer.h:35
bool setName(const std::string &newName)
bool removeQuery(const std::string &queryNameToRemove, bool exactMatch)
Definition: DnsLayer.h:215
Definition: DnsLayer.h:183
size_t getAnswerCount()
OsiModelLayer getOsiModelLayer()
Definition: DnsLayer.h:750
DnsQuery * getFirstQuery()
Definition: DnsLayer.h:265
Definition: DnsLayer.h:119
Definition: DnsLayer.h:109
Definition: DnsLayer.h:185
Definition: DnsLayer.h:181
uint16_t checkingDisabled
Definition: DnsLayer.h:35
DnsQuery * getQuery(const std::string &name, bool exactMatch)
Definition: Layer.h:52
virtual ResourceType getType()
Definition: DnsLayer.h:339
Definition: DnsLayer.h:137
Definition: DnsLayer.h:157
Definition: DnsLayer.h:107
size_t getAdditionalRecordCount()
DnsQuery * getNextQuery(DnsQuery *query)
Definition: DnsLayer.h:325
bool removeAdditionalRecord(const std::string &additionalRecordNameToRemove, bool exactMatch)
Definition: DnsLayer.h:28
DnsResource * addAdditionalRecord(const std::string &name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, const std::string &data)
Definition: DnsLayer.h:193
Definition: DnsLayer.h:195
std::string getDataAsString()
void computeCalculateFields()
Definition: DnsLayer.h:746
uint32_t getTTL()
Definition: DnsLayer.h:159
DnsResource * getAuthority(const std::string &name, bool exactMatch)
Definition: DnsLayer.h:135
Definition: DnsLayer.h:139
static const std::map< uint16_t, bool > * getDNSPortMap()
uint16_t numberOfAuthority
Definition: DnsLayer.h:82
Definition: DnsLayer.h:111
bool setData(const std::string &dataAsString)
Definition: DnsLayer.h:169
Definition: DnsLayer.h:191
Definition: DnsLayer.h:101
Definition: DnsLayer.h:189
bool removeAuthority(const std::string &authorityNameToRemove, bool exactMatch)
Definition: DnsLayer.h:261
The main namespace for the PcapPlusPlus lib.
DnsLayer & operator=(const DnsLayer &other)
DnsResource * getAdditionalRecord(const std::string &name, bool exactMatch)
Definition: DnsLayer.h:115
Definition: DnsLayer.h:123
Definition: DnsLayer.h:167
DnsResource * addAnswer(const std::string &name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, const std::string &data)
uint16_t truncation
Definition: DnsLayer.h:35
Definition: DnsLayer.h:149
Definition: DnsLayer.h:95
virtual ResourceType getType()
Definition: DnsLayer.h:419
void setTTL(uint32_t newTTL)