record_parsed.h 2.1 KB

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