PcapPlusPlus  Next
DnsLayer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "DnsLayerEnums.h"
4 #include "DnsResource.h"
5 #include "DnsResourceData.h"
6 #include "Layer.h"
7 
9 
12 namespace pcpp
13 {
17 #pragma pack(push, 1)
18  struct dnshdr
19  {
21  uint16_t transactionID;
22 #if (BYTE_ORDER == LITTLE_ENDIAN)
23  uint16_t
31  opcode : 4,
41  zero : 1,
44 #elif (BYTE_ORDER == BIG_ENDIAN)
45  uint16_t
47  queryOrResponse : 1,
49  opcode : 4,
53  truncation : 1,
55  recursionDesired : 1,
59  zero : 1,
61  authenticData : 1,
63  checkingDisabled : 1,
65  responseCode : 4;
66 #endif
70  uint16_t numberOfAnswers;
75  };
76 #pragma pack(pop)
77  static_assert(sizeof(dnshdr) == 12, "dnshdr size is not 12 bytes");
78 
79  // forward declarations
80  class DnsQuery;
81  class IDnsResource;
82  class DnsResource;
83  class IDnsResourceData;
84 
87  class DnsLayer : public Layer
88  {
89  friend class IDnsResource;
90  friend class DnsQuery;
91  friend class DnsResource;
92 
93  public:
99  DnsLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
100 
104 
107  DnsLayer(const DnsLayer& other);
108 
112  DnsLayer& operator=(const DnsLayer& other);
113 
114  ~DnsLayer() override;
115 
121 
128  DnsQuery* getQuery(const std::string& name, bool exactMatch) const;
129 
132 
138 
140  size_t getQueryCount() const;
141 
148  DnsQuery* addQuery(const std::string& name, DnsType dnsType, DnsClass dnsClass);
149 
155  DnsQuery* addQuery(DnsQuery* const copyQuery);
156 
162  bool removeQuery(const std::string& queryNameToRemove, bool exactMatch);
163 
168  bool removeQuery(DnsQuery* queryToRemove);
169 
176  DnsResource* getAnswer(const std::string& name, bool exactMatch) const;
177 
180 
186 
188  size_t getAnswerCount() const;
189 
200  DnsResource* addAnswer(const std::string& name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl,
201  IDnsResourceData* data);
202 
208  DnsResource* addAnswer(DnsResource* const copyAnswer);
209 
215  bool removeAnswer(const std::string& answerNameToRemove, bool exactMatch);
216 
221  bool removeAnswer(DnsResource* answerToRemove);
222 
229  DnsResource* getAuthority(const std::string& name, bool exactMatch) const;
230 
233 
239 
241  size_t getAuthorityCount() const;
242 
253  DnsResource* addAuthority(const std::string& name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl,
254  IDnsResourceData* data);
255 
261  DnsResource* addAuthority(DnsResource* const copyAuthority);
262 
269  bool removeAuthority(const std::string& authorityNameToRemove, bool exactMatch);
270 
275  bool removeAuthority(DnsResource* authorityToRemove);
276 
283  DnsResource* getAdditionalRecord(const std::string& name, bool exactMatch) const;
284 
288 
294 
296  size_t getAdditionalRecordCount() const;
297 
308  DnsResource* addAdditionalRecord(const std::string& name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl,
309  IDnsResourceData* data);
310 
325  DnsResource* addAdditionalRecord(const std::string& name, DnsType dnsType, uint16_t customData1,
326  uint32_t customData2, IDnsResourceData* data);
327 
334  DnsResource* addAdditionalRecord(DnsResource* const copyAdditionalRecord);
335 
342  bool removeAdditionalRecord(const std::string& additionalRecordNameToRemove, bool exactMatch);
343 
348  bool removeAdditionalRecord(DnsResource* additionalRecordToRemove);
349 
350  // implement abstract methods
351 
353  void parseNextLayer() override
354  {}
355 
358  size_t getHeaderLen() const override
359  {
360  return m_DataLen;
361  } // No layer above DNS
362 
364  void computeCalculateFields() override
365  {}
366 
367  std::string toString() const override;
368 
370  {
372  }
373 
377  static inline bool isDnsPort(uint16_t port);
378 
385  static inline bool isDataValid(const uint8_t* data, size_t dataLen, bool dnsOverTcp = false);
386 
390  {
392  static std::vector<FieldDescriptor> all()
393  {
394  auto result = Layer::SerializedFields::all();
395  std::initializer_list<FieldDescriptor> extra{ TransactionID, IsResponse, Opcode,
400  std::copy(extra.begin(), extra.end(), std::back_inserter(result));
401  return result;
402  }
403 
406  struct QueryObject : ObjectFieldDescriptor<QueryObject>
407  {
409 
411  static std::vector<FieldDescriptor> all()
412  {
413  return { Name, Type, TypeName, Class };
414  }
415 
417  static const FieldDescriptor Name;
418 
420  static const FieldDescriptor Type;
421 
424  static const FieldDescriptor TypeName;
425 
427  static const FieldDescriptor Class;
428  };
429 
432  struct ResourceObject : ObjectFieldDescriptor<ResourceObject>
433  {
435 
437  static std::vector<FieldDescriptor> all()
438  {
439  return { Name, Type, TypeName, Class, Ttl, DataLength, Data };
440  }
441 
443  static const FieldDescriptor Name;
444 
446  static const FieldDescriptor Type;
447 
450  static const FieldDescriptor TypeName;
451 
454  static const FieldDescriptor Class;
455 
458  static const FieldDescriptor Ttl;
459 
462 
466  static const FieldDescriptor Data;
467  };
468 
471 
474 
476  static const FieldDescriptor Opcode;
477 
480 
483 
486 
489 
492 
495 
498 
500  static const FieldDescriptor Queries;
501 
503  static const FieldDescriptor Answers;
504 
507 
510 
512  static constexpr uint16_t MaxID = Layer::SerializedFields::MaxID + 14;
513  };
514 
515  protected:
516  DnsLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, size_t offsetAdjustment);
517  explicit DnsLayer(size_t offsetAdjustment);
518 
519  void serializeLayer(ObjectScope& serializer) const override;
520 
521  private:
522  IDnsResource* m_ResourceList;
523  DnsQuery* m_FirstQuery;
524  DnsResource* m_FirstAnswer;
525  DnsResource* m_FirstAuthority;
526  DnsResource* m_FirstAdditional;
527  uint16_t m_OffsetAdjustment;
528 
529  size_t getBasicHeaderSize();
530  void init(size_t offsetAdjustment, bool callParseResource);
531  void initNewLayer(size_t offsetAdjustment);
532 
533  IDnsResource* getFirstResource(DnsResourceType resType) const;
534  void setFirstResource(DnsResourceType resType, IDnsResource* resource);
535 
536  using Layer::extendLayer;
537  bool extendLayer(int offsetInLayer, size_t numOfBytesToExtend, IDnsResource* resource);
538 
539  using Layer::shortenLayer;
540  bool shortenLayer(int offsetInLayer, size_t numOfBytesToShorten, IDnsResource* resource);
541 
542  IDnsResource* getResourceByName(IDnsResource* startFrom, size_t resourceCount, const std::string& name,
543  bool exactMatch) const;
544 
545  void parseResources();
546 
547  DnsResource* addResource(DnsResourceType resType, const std::string& name, DnsType dnsType, DnsClass dnsClass,
548  uint32_t ttl, IDnsResourceData* data);
549 
550  bool removeResource(IDnsResource* resourceToRemove);
551  };
552 
558 
561  class DnsOverTcpLayer : public DnsLayer
562  {
563  public:
569  DnsOverTcpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
570  : DnsLayer(data, dataLen, prevLayer, packet, sizeof(uint16_t))
571  {}
572 
575  DnsOverTcpLayer() : DnsLayer(sizeof(uint16_t))
576  {}
577 
580  DnsOverTcpLayer(const DnsOverTcpLayer& other) : DnsLayer(other)
581  {}
582 
585 
588  void setTcpMessageLength(uint16_t value);
589 
590  // overridden methods
591 
593  void computeCalculateFields() override;
594 
598  {
599  // cppcheck-suppress duplInheritedMember
601  static std::vector<FieldDescriptor> all()
602  {
603  auto result = DnsLayer::SerializedFields::all();
604  result.push_back(TcpMessageLength);
605  return result;
606  }
607 
610  };
611 
612  protected:
613  void serializeLayer(ObjectScope& serializer) const override;
614  };
615 
616  // implementation of inline methods
617 
618  bool DnsLayer::isDnsPort(uint16_t port)
619  {
620  switch (port)
621  {
622  case 53:
623  case 5353:
624  case 5355:
625  return true;
626  default:
627  return false;
628  }
629  }
630 
631  bool DnsLayer::isDataValid(const uint8_t* data, size_t dataLen, bool dnsOverTcp)
632  {
633  size_t minSize = sizeof(dnshdr) + (dnsOverTcp ? sizeof(uint16_t) : 0);
634  return data && dataLen >= minSize;
635  }
636 
637 } // namespace pcpp
Definition: DnsLayer.h:88
DnsResource * getFirstAnswer() const
DnsLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
DnsResource * addAdditionalRecord(const std::string &name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, IDnsResourceData *data)
size_t getHeaderLen() const override
Definition: DnsLayer.h:358
DnsQuery * getQuery(const std::string &name, bool exactMatch) const
DnsResource * addAdditionalRecord(const std::string &name, DnsType dnsType, uint16_t customData1, uint32_t customData2, IDnsResourceData *data)
DnsResource * addAuthority(DnsResource *const copyAuthority)
static bool isDataValid(const uint8_t *data, size_t dataLen, bool dnsOverTcp=false)
Definition: DnsLayer.h:631
DnsQuery * addQuery(DnsQuery *const copyQuery)
DnsResource * getAuthority(const std::string &name, bool exactMatch) const
bool removeAdditionalRecord(DnsResource *additionalRecordToRemove)
DnsResource * addAdditionalRecord(DnsResource *const copyAdditionalRecord)
DnsResource * addAnswer(DnsResource *const copyAnswer)
bool removeQuery(const std::string &queryNameToRemove, bool exactMatch)
DnsResource * getAdditionalRecord(const std::string &name, bool exactMatch) const
size_t getQueryCount() const
DnsResource * getNextAuthority(DnsResource *authority) const
std::string toString() const override
DnsResource * getNextAnswer(DnsResource *answer) const
void computeCalculateFields() override
Does nothing for this layer.
Definition: DnsLayer.h:364
DnsQuery * addQuery(const std::string &name, DnsType dnsType, DnsClass dnsClass)
DnsLayer(const DnsLayer &other)
DnsQuery * getNextQuery(DnsQuery *query) const
DnsResource * getFirstAdditionalRecord() const
bool removeAuthority(const std::string &authorityNameToRemove, bool exactMatch)
DnsResource * getFirstAuthority() const
bool removeQuery(DnsQuery *queryToRemove)
size_t getAuthorityCount() const
void serializeLayer(ObjectScope &serializer) const override
size_t getAnswerCount() const
dnshdr * getDnsHeader() const
void parseNextLayer() override
Does nothing for this layer (DnsLayer is always last)
Definition: DnsLayer.h:353
DnsResource * addAuthority(const std::string &name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, IDnsResourceData *data)
bool removeAnswer(DnsResource *answerToRemove)
DnsResource * getNextAdditionalRecord(DnsResource *additionalRecord) const
DnsQuery * getFirstQuery() const
DnsResource * addAnswer(const std::string &name, DnsType dnsType, DnsClass dnsClass, uint32_t ttl, IDnsResourceData *data)
bool removeAdditionalRecord(const std::string &additionalRecordNameToRemove, bool exactMatch)
bool removeAnswer(const std::string &answerNameToRemove, bool exactMatch)
static bool isDnsPort(uint16_t port)
Definition: DnsLayer.h:618
DnsLayer & operator=(const DnsLayer &other)
bool removeAuthority(DnsResource *authorityToRemove)
OsiModelLayer getOsiModelLayer() const override
Definition: DnsLayer.h:369
size_t getAdditionalRecordCount() const
DnsResource * getAnswer(const std::string &name, bool exactMatch) const
Definition: DnsLayer.h:562
DnsOverTcpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: DnsLayer.h:569
void computeCalculateFields() override
Calculate the TCP message length field.
void serializeLayer(ObjectScope &serializer) const override
void setTcpMessageLength(uint16_t value)
uint16_t getTcpMessageLength()
DnsOverTcpLayer()
Definition: DnsLayer.h:575
DnsOverTcpLayer(const DnsOverTcpLayer &other)
Definition: DnsLayer.h:580
Definition: DnsResource.h:113
Definition: DnsResource.h:140
Definition: DnsResourceData.h:26
Definition: DnsResource.h:26
Definition: Layer.h:117
Definition: Serializers.h:230
Definition: Packet.h:49
The main namespace for the PcapPlusPlus lib.
Definition: AssertionUtils.h:19
OsiModelLayer
An enum representing OSI model layers.
Definition: ProtocolType.h:430
@ OsiModelApplicationLayer
Application layer (layer 7)
Definition: ProtocolType.h:446
DnsClass
An enum for all possible DNS classes.
Definition: DnsLayerEnums.h:120
DnsResourceType
An enum for representing the 4 types of possible DNS records.
Definition: DnsLayerEnums.h:135
DnsType
An enum for all possible DNS record types.
Definition: DnsLayerEnums.h:11
static std::vector< FieldDescriptor > all()
Definition: DnsLayer.h:411
static const FieldDescriptor Name
Field descriptor for the queried name, written as a string.
Definition: DnsLayer.h:417
static const FieldDescriptor Type
Field descriptor for the DNS type, written as a number (see DnsType).
Definition: DnsLayer.h:420
static const FieldDescriptor TypeName
Field descriptor for the DNS type name (for example "A", "AAAA" or "CNAME"), written as a string,...
Definition: DnsLayer.h:424
static const FieldDescriptor Class
Field descriptor for the DNS class, written as a number (see DnsClass).
Definition: DnsLayer.h:427
static const FieldDescriptor Name
Field descriptor for the record name, written as a string.
Definition: DnsLayer.h:443
static const FieldDescriptor Ttl
Field descriptor for the TTL, written as a number. For records that don't have a TTL (such as OPT rec...
Definition: DnsLayer.h:458
static const FieldDescriptor Class
Field descriptor for the DNS class, written as a number (see DnsClass). For records that don't have a...
Definition: DnsLayer.h:454
static const FieldDescriptor Data
Field descriptor for the record data, written as a string (see IDnsResourceData::toString())....
Definition: DnsLayer.h:466
static const FieldDescriptor DataLength
Field descriptor for the record data length in bytes, written as a number.
Definition: DnsLayer.h:461
static std::vector< FieldDescriptor > all()
Definition: DnsLayer.h:437
static const FieldDescriptor Type
Field descriptor for the DNS type, written as a number (see DnsType).
Definition: DnsLayer.h:446
static const FieldDescriptor TypeName
Field descriptor for the DNS type name (for example "A", "AAAA" or "CNAME"), written as a string,...
Definition: DnsLayer.h:450
Definition: DnsLayer.h:390
static std::vector< FieldDescriptor > all()
Definition: DnsLayer.h:392
static const FieldDescriptor AdditionalRecords
Field descriptor for the array of additional records; elements described by ResourceObject.
Definition: DnsLayer.h:509
static const FieldDescriptor Opcode
Field descriptor for the operation code, written as a number.
Definition: DnsLayer.h:476
static const FieldDescriptor CheckingDisabled
Field descriptor for the checking disabled flag, written as a boolean.
Definition: DnsLayer.h:494
static constexpr uint16_t MaxID
Maximum field ID used by the layer.
Definition: DnsLayer.h:512
static const FieldDescriptor IsResponse
Field descriptor for the query/response flag, written as a boolean (true for a response).
Definition: DnsLayer.h:473
static const FieldDescriptor RecursionAvailable
Field descriptor for the recursion available flag, written as a boolean.
Definition: DnsLayer.h:488
static const FieldDescriptor Queries
Field descriptor for the array of queries; elements described by QueryObject.
Definition: DnsLayer.h:500
static const FieldDescriptor Answers
Field descriptor for the array of answers; elements described by ResourceObject.
Definition: DnsLayer.h:503
static const FieldDescriptor AuthenticData
Field descriptor for the authenticated data flag, written as a boolean.
Definition: DnsLayer.h:491
static const FieldDescriptor TransactionID
Field descriptor for the transaction ID, written as a hex value.
Definition: DnsLayer.h:470
static const FieldDescriptor AuthoritativeAnswer
Field descriptor for the authoritative answer flag, written as a boolean.
Definition: DnsLayer.h:479
static const FieldDescriptor RecursionDesired
Field descriptor for the recursion desired flag, written as a boolean.
Definition: DnsLayer.h:485
static const FieldDescriptor Truncation
Field descriptor for the truncation flag, written as a boolean.
Definition: DnsLayer.h:482
static const FieldDescriptor ResponseCode
Field descriptor for the response code, written as a number.
Definition: DnsLayer.h:497
static const FieldDescriptor Authorities
Field descriptor for the array of authorities; elements described by ResourceObject.
Definition: DnsLayer.h:506
Definition: DnsLayer.h:598
static std::vector< FieldDescriptor > all()
Definition: DnsLayer.h:601
static const FieldDescriptor TcpMessageLength
Field descriptor for the TCP message length, written as a number.
Definition: DnsLayer.h:609
Definition: Serializers.h:18
Definition: Layer.h:212
static std::vector< FieldDescriptor > all()
Definition: Layer.h:214
static constexpr uint16_t MaxID
Maximum field ID used by the layer.
Definition: Layer.h:229
Definition: Serializers.h:49
ObjectFieldDescriptor(uint16_t fieldId, std::string fieldName)
Definition: Serializers.h:53
Definition: DnsLayer.h:19
uint16_t responseCode
Return Code.
Definition: DnsLayer.h:35
uint16_t authoritativeAnswer
Authoritative answer flag.
Definition: DnsLayer.h:29
uint16_t queryOrResponse
Query/Response flag.
Definition: DnsLayer.h:33
uint16_t zero
Zero flag (Reserved)
Definition: DnsLayer.h:41
uint16_t transactionID
DNS query identification.
Definition: DnsLayer.h:21
uint16_t numberOfAuthority
Number of authority records in packet.
Definition: DnsLayer.h:72
uint16_t truncation
Truncated flag.
Definition: DnsLayer.h:27
uint16_t numberOfAdditional
Number of additional records in packet.
Definition: DnsLayer.h:74
uint16_t checkingDisabled
Checking disabled flag.
Definition: DnsLayer.h:37
uint16_t numberOfAnswers
Number of DNS answer records in packet.
Definition: DnsLayer.h:70
uint16_t authenticData
Authenticated data flag.
Definition: DnsLayer.h:39
uint16_t opcode
Operation Code.
Definition: DnsLayer.h:31
uint16_t recursionAvailable
Recursion available flag.
Definition: DnsLayer.h:43
uint16_t recursionDesired
Recursion desired flag.
Definition: DnsLayer.h:25
uint16_t numberOfQuestions
Number of DNS query records in packet.
Definition: DnsLayer.h:68