mdns_cache.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/mdns_cache.h"
  5. #include <algorithm>
  6. #include <tuple>
  7. #include <utility>
  8. #include "base/strings/string_number_conversions.h"
  9. #include "base/strings/string_util.h"
  10. #include "net/dns/public/dns_protocol.h"
  11. #include "net/dns/record_parsed.h"
  12. #include "net/dns/record_rdata.h"
  13. // TODO(noamsml): Recursive CNAME closure (backwards and forwards).
  14. namespace net {
  15. namespace {
  16. constexpr size_t kDefaultEntryLimit = 100'000;
  17. } // namespace
  18. // The effective TTL given to records with a nominal zero TTL.
  19. // Allows time for hosts to send updated records, as detailed in RFC 6762
  20. // Section 10.1.
  21. static const unsigned kZeroTTLSeconds = 1;
  22. MDnsCache::Key::Key(unsigned type,
  23. const std::string& name,
  24. const std::string& optional)
  25. : type_(type),
  26. name_lowercase_(base::ToLowerASCII(name)),
  27. optional_(optional) {}
  28. MDnsCache::Key::Key(const MDnsCache::Key& other) = default;
  29. MDnsCache::Key& MDnsCache::Key::operator=(const MDnsCache::Key& other) =
  30. default;
  31. MDnsCache::Key::~Key() = default;
  32. bool MDnsCache::Key::operator<(const MDnsCache::Key& other) const {
  33. return std::tie(name_lowercase_, type_, optional_) <
  34. std::tie(other.name_lowercase_, other.type_, other.optional_);
  35. }
  36. bool MDnsCache::Key::operator==(const MDnsCache::Key& key) const {
  37. return type_ == key.type_ && name_lowercase_ == key.name_lowercase_ &&
  38. optional_ == key.optional_;
  39. }
  40. // static
  41. MDnsCache::Key MDnsCache::Key::CreateFor(const RecordParsed* record) {
  42. return Key(record->type(),
  43. record->name(),
  44. GetOptionalFieldForRecord(record));
  45. }
  46. MDnsCache::MDnsCache() : entry_limit_(kDefaultEntryLimit) {}
  47. MDnsCache::~MDnsCache() = default;
  48. const RecordParsed* MDnsCache::LookupKey(const Key& key) {
  49. auto found = mdns_cache_.find(key);
  50. if (found != mdns_cache_.end()) {
  51. return found->second.get();
  52. }
  53. return nullptr;
  54. }
  55. MDnsCache::UpdateType MDnsCache::UpdateDnsRecord(
  56. std::unique_ptr<const RecordParsed> record) {
  57. Key cache_key = Key::CreateFor(record.get());
  58. // Ignore "goodbye" packets for records not in cache.
  59. if (record->ttl() == 0 && mdns_cache_.find(cache_key) == mdns_cache_.end())
  60. return NoChange;
  61. base::Time new_expiration = GetEffectiveExpiration(record.get());
  62. if (next_expiration_ != base::Time())
  63. new_expiration = std::min(new_expiration, next_expiration_);
  64. std::pair<RecordMap::iterator, bool> insert_result =
  65. mdns_cache_.insert(std::make_pair(cache_key, nullptr));
  66. UpdateType type = NoChange;
  67. if (insert_result.second) {
  68. type = RecordAdded;
  69. } else {
  70. if (record->ttl() != 0 &&
  71. !record->IsEqual(insert_result.first->second.get(), true)) {
  72. type = RecordChanged;
  73. }
  74. }
  75. insert_result.first->second = std::move(record);
  76. next_expiration_ = new_expiration;
  77. return type;
  78. }
  79. void MDnsCache::CleanupRecords(
  80. base::Time now,
  81. const RecordRemovedCallback& record_removed_callback) {
  82. base::Time next_expiration;
  83. // TODO(crbug.com/946688): Make overfill pruning more intelligent than a bulk
  84. // clearing of everything.
  85. bool clear_cache = IsCacheOverfilled();
  86. // We are guaranteed that |next_expiration_| will be at or before the next
  87. // expiration. This allows clients to eagrely call CleanupRecords with
  88. // impunity.
  89. if (now < next_expiration_ && !clear_cache)
  90. return;
  91. for (auto i = mdns_cache_.begin(); i != mdns_cache_.end();) {
  92. base::Time expiration = GetEffectiveExpiration(i->second.get());
  93. if (clear_cache || now >= expiration) {
  94. record_removed_callback.Run(i->second.get());
  95. i = mdns_cache_.erase(i);
  96. } else {
  97. if (next_expiration == base::Time() || expiration < next_expiration) {
  98. next_expiration = expiration;
  99. }
  100. ++i;
  101. }
  102. }
  103. next_expiration_ = next_expiration;
  104. }
  105. void MDnsCache::FindDnsRecords(unsigned type,
  106. const std::string& name,
  107. std::vector<const RecordParsed*>* results,
  108. base::Time now) const {
  109. DCHECK(results);
  110. results->clear();
  111. const std::string name_lowercase = base::ToLowerASCII(name);
  112. auto i = mdns_cache_.lower_bound(Key(type, name, ""));
  113. for (; i != mdns_cache_.end(); ++i) {
  114. if (i->first.name_lowercase() != name_lowercase ||
  115. (type != 0 && i->first.type() != type)) {
  116. break;
  117. }
  118. const RecordParsed* record = i->second.get();
  119. // Records are deleted only upon request.
  120. if (now >= GetEffectiveExpiration(record)) continue;
  121. results->push_back(record);
  122. }
  123. }
  124. std::unique_ptr<const RecordParsed> MDnsCache::RemoveRecord(
  125. const RecordParsed* record) {
  126. Key key = Key::CreateFor(record);
  127. auto found = mdns_cache_.find(key);
  128. if (found != mdns_cache_.end() && found->second.get() == record) {
  129. std::unique_ptr<const RecordParsed> result = std::move(found->second);
  130. mdns_cache_.erase(key);
  131. return result;
  132. }
  133. return nullptr;
  134. }
  135. bool MDnsCache::IsCacheOverfilled() const {
  136. return mdns_cache_.size() > entry_limit_;
  137. }
  138. // static
  139. std::string MDnsCache::GetOptionalFieldForRecord(const RecordParsed* record) {
  140. switch (record->type()) {
  141. case PtrRecordRdata::kType: {
  142. const PtrRecordRdata* rdata = record->rdata<PtrRecordRdata>();
  143. return rdata->ptrdomain();
  144. }
  145. default: // Most records are considered unique for our purposes
  146. return "";
  147. }
  148. }
  149. // static
  150. base::Time MDnsCache::GetEffectiveExpiration(const RecordParsed* record) {
  151. base::TimeDelta ttl;
  152. if (record->ttl()) {
  153. ttl = base::Seconds(record->ttl());
  154. } else {
  155. ttl = base::Seconds(kZeroTTLSeconds);
  156. }
  157. return record->time_created() + ttl;
  158. }
  159. } // namespace net