1 // Copyright 2013 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_DNS_RECORD_PARSED_H_ 6 #define NET_DNS_RECORD_PARSED_H_ 7 8 #include <stdint.h> 9 10 #include <memory> 11 #include <string> 12 13 #include "base/check.h" 14 #include "base/time/time.h" 15 #include "net/base/net_export.h" 16 17 namespace net { 18 19 class DnsRecordParser; 20 class RecordRdata; 21 22 // Parsed record. This is a form of DnsResourceRecord where the rdata section 23 // has been parsed into a data structure. 24 class NET_EXPORT_PRIVATE RecordParsed { 25 public: 26 virtual ~RecordParsed(); 27 28 // All records are inherently immutable. Return a const pointer. 29 static std::unique_ptr<const RecordParsed> CreateFrom( 30 DnsRecordParser* parser, 31 base::Time time_created); 32 name()33 const std::string& name() const { return name_; } type()34 uint16_t type() const { return type_; } klass()35 uint16_t klass() const { return klass_; } ttl()36 uint32_t ttl() const { return ttl_; } 37 time_created()38 base::Time time_created() const { return time_created_; } 39 rdata()40 template <class T> const T* rdata() const { 41 if (T::kType != type_) 42 return nullptr; 43 44 // Expect RData should always be parsed for recognized types. 45 DCHECK(rdata_); 46 47 return static_cast<const T*>(rdata_.get()); 48 } 49 50 // Gets the rdata without casting to a known RData type. rdata_for_testing()51 const RecordRdata* rdata_for_testing() const { return rdata_.get(); } 52 53 // Check if two records have the same data. Ignores time_created and ttl. 54 // If |is_mdns| is true, ignore the top bit of the class 55 // (the cache flush bit). 56 bool IsEqual(const RecordParsed* other, bool is_mdns) const; 57 58 private: 59 RecordParsed(const std::string& name, 60 uint16_t type, 61 uint16_t klass, 62 uint32_t ttl, 63 std::unique_ptr<const RecordRdata> rdata, 64 base::Time time_created); 65 66 std::string name_; // in dotted form 67 const uint16_t type_; 68 const uint16_t klass_; 69 const uint32_t ttl_; 70 71 const std::unique_ptr<const RecordRdata> rdata_; 72 73 const base::Time time_created_; 74 }; 75 76 } // namespace net 77 78 #endif // NET_DNS_RECORD_PARSED_H_ 79