record_parsed.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #include "net/dns/record_parsed.h"
  5. #include <utility>
  6. #include "base/logging.h"
  7. #include "base/memory/ptr_util.h"
  8. #include "net/dns/dns_response.h"
  9. #include "net/dns/https_record_rdata.h"
  10. #include "net/dns/opt_record_rdata.h"
  11. #include "net/dns/record_rdata.h"
  12. namespace net {
  13. RecordParsed::RecordParsed(const std::string& name,
  14. uint16_t type,
  15. uint16_t klass,
  16. uint32_t ttl,
  17. std::unique_ptr<const RecordRdata> rdata,
  18. base::Time time_created)
  19. : name_(name),
  20. type_(type),
  21. klass_(klass),
  22. ttl_(ttl),
  23. rdata_(std::move(rdata)),
  24. time_created_(time_created) {}
  25. RecordParsed::~RecordParsed() = default;
  26. // static
  27. std::unique_ptr<const RecordParsed> RecordParsed::CreateFrom(
  28. DnsRecordParser* parser,
  29. base::Time time_created) {
  30. DnsResourceRecord record;
  31. std::unique_ptr<const RecordRdata> rdata;
  32. if (!parser->ReadRecord(&record))
  33. return nullptr;
  34. bool unrecognized_type = false;
  35. switch (record.type) {
  36. case ARecordRdata::kType:
  37. rdata = ARecordRdata::Create(record.rdata, *parser);
  38. break;
  39. case AAAARecordRdata::kType:
  40. rdata = AAAARecordRdata::Create(record.rdata, *parser);
  41. break;
  42. case CnameRecordRdata::kType:
  43. rdata = CnameRecordRdata::Create(record.rdata, *parser);
  44. break;
  45. case PtrRecordRdata::kType:
  46. rdata = PtrRecordRdata::Create(record.rdata, *parser);
  47. break;
  48. case SrvRecordRdata::kType:
  49. rdata = SrvRecordRdata::Create(record.rdata, *parser);
  50. break;
  51. case TxtRecordRdata::kType:
  52. rdata = TxtRecordRdata::Create(record.rdata, *parser);
  53. break;
  54. case NsecRecordRdata::kType:
  55. rdata = NsecRecordRdata::Create(record.rdata, *parser);
  56. break;
  57. case OptRecordRdata::kType:
  58. rdata = OptRecordRdata::Create(record.rdata);
  59. break;
  60. case IntegrityRecordRdata::kType:
  61. rdata = IntegrityRecordRdata::Create(record.rdata);
  62. break;
  63. case HttpsRecordRdata::kType:
  64. rdata = HttpsRecordRdata::Parse(record.rdata);
  65. break;
  66. default:
  67. DVLOG(1) << "Unknown RData type for received record: " << record.type;
  68. rdata = nullptr;
  69. unrecognized_type = true;
  70. break;
  71. }
  72. // If a recognized type has a malformed rdata, consider the whole record
  73. // malformed.
  74. if (!rdata.get() && !unrecognized_type)
  75. return nullptr;
  76. return base::WrapUnique(new RecordParsed(record.name, record.type,
  77. record.klass, record.ttl,
  78. std::move(rdata), time_created));
  79. }
  80. bool RecordParsed::IsEqual(const RecordParsed* other, bool is_mdns) const {
  81. DCHECK(other);
  82. uint16_t klass = klass_;
  83. uint16_t other_klass = other->klass_;
  84. if (is_mdns) {
  85. klass &= dns_protocol::kMDnsClassMask;
  86. other_klass &= dns_protocol::kMDnsClassMask;
  87. }
  88. return name_ == other->name_ && klass == other_klass &&
  89. type_ == other->type_ && !!rdata_ == !!other->rdata_ &&
  90. (!rdata_ || rdata_->IsEqual(other->rdata_.get()));
  91. }
  92. } // namespace net