Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(654)

Unified Diff: net/dns/mdns_cache.cc

Issue 14697022: Cache for mDNS records (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@record_parsed_klassbit
Patch Set: Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/dns/mdns_cache.cc
diff --git a/net/dns/mdns_cache.cc b/net/dns/mdns_cache.cc
new file mode 100644
index 0000000000000000000000000000000000000000..df549ab5e9ef36d745222d82f01aec92bfa9569d
--- /dev/null
+++ b/net/dns/mdns_cache.cc
@@ -0,0 +1,199 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/dns/mdns_cache.h"
+
+#include "base/strings/string_number_conversions.h"
+#include "net/dns/dns_protocol.h"
+#include "net/dns/record_parsed.h"
+#include "net/dns/record_rdata.h"
+
+// TODO(noamsml): Recursive CNAME closure (backwards and forwards).
+
+namespace net {
+
+// The effective TTL given to records with a nominal zero TTL.
+// Allows time for hosts to send updated records.
+static const unsigned kZeroTTLSeconds = 1;
+
+MDnsCache::DnsRecordCacheKey::DnsRecordCacheKey(uint16 type,
+ const std::string& name,
+ const std::string& optional)
+ : type_(type), name_(name), optional_(optional) {
+}
+
+MDnsCache::DnsRecordCacheKey::~DnsRecordCacheKey() {
+}
+
+bool MDnsCache::DnsRecordCacheKey::operator<(
+ const MDnsCache::DnsRecordCacheKey& key) const {
+ if (type_ != key.type_)
+ return type_ < key.type_;
+
+ if (name_ != key.name_)
+ return name_ < key.name_;
+
+ if (optional_ != key.optional_)
+ return optional_ < key.optional_;
+ return false; // keys are equal
+}
+
+bool MDnsCache::DnsRecordCacheKey::operator==(
+ const MDnsCache::DnsRecordCacheKey& key) const {
+ return type_ == key.type_ && name_ == key.name_ && optional_ == key.optional_;
+}
+
+MDnsCache::MDnsCache() {
+}
+
+MDnsCache::~MDnsCache() {
+}
+
+void MDnsCache::Clear() {
+ next_expiration_ = base::Time();
+ mdns_cache_.clear();
+}
+
+MDnsCache::UpdateType MDnsCache::UpdateDnsRecord(
+ const RecordParsed* record) {
+ UpdateType type = NoChange;
+ linked_ptr<const RecordParsed> record_linked(record);
+
+ MDnsCache::DnsRecordCacheKey cache_key = MDnsCache::DnsRecordCacheKey(
+ record_linked->type(),
+ record_linked->name(),
+ GetOptionalFieldForRecord(record_linked.get()));
+
+ base::Time expiration = GetEffectiveExpiration(record_linked.get());
+ if (next_expiration_ == base::Time() || expiration < next_expiration_) {
+ next_expiration_ = expiration;
+ }
+
+ MDnsCacheMap::iterator i = mdns_cache_.find(cache_key);
+ if (i != mdns_cache_.end()) {
+ if (!record->IsEqual(i->second.get(), true)) {
+ type = RecordChanged;
+ }
+ i->second = record_linked;
+ } else {
+ mdns_cache_[cache_key] = record_linked;
+ type = RecordAdded;
+ }
+
+ return type;
+}
+
+void MDnsCache::CleanupRecords(
+ std::list<linked_ptr<const RecordParsed> >* records_removed,
+ base::Time now) {
+ base::Time next_expiration;
+
+ // We are guaranteed that next_expiration_ will be at or before the next
+ // expiration. This allows clients to eagrely call CleanupRecords with
+ // impunity.
+ if (now < next_expiration_) return;
+
+ std::list<MDnsCacheMap::iterator> to_delete;
+ for (MDnsCacheMap::iterator i = mdns_cache_.begin();
+ i != mdns_cache_.end(); i++) {
+ base::Time expiration = GetEffectiveExpiration(i->second.get());
+ if (now >= expiration) {
+ records_removed->push_back(i->second);
+ to_delete.push_back(i);
+ } else {
+ if (next_expiration == base::Time() || expiration < next_expiration) {
+ next_expiration = expiration;
+ }
+ }
+ }
+
+ for (std::list<MDnsCacheMap::iterator>::iterator i = to_delete.begin();
+ i != to_delete.end(); ++i) {
+ mdns_cache_.erase(*i);
+ }
+
+ next_expiration_ = next_expiration;
+}
+
+bool MDnsCache::FindDnsRecords(const Query& query,
+ std::list<const RecordParsed*>* results,
+ base::Time now) {
+ bool found = false;
+
+ MDnsCacheMap::iterator i =
+ mdns_cache_.lower_bound(query.GetLowerBoundKey());
+ for (; i != mdns_cache_.end() && query.IsMatching(i->first); ++i) {
+ const RecordParsed* record = i->second.get();
+
+ // Records are deleted only upon request to make this op idempotent
+ if (now >= GetEffectiveExpiration(record)) continue;
+
+ if (!found) {
+ // Make sure we clear output before writing first result.
+ results->clear();
+ found = true;
+ }
+
+ results->push_back(record);
+ }
+ return found;
+}
+
+std::string MDnsCache::GetOptionalFieldForRecord(const RecordParsed* record) {
+ switch (record->type()) {
+ case PtrRecordRdata::kType: {
+ const PtrRecordRdata* rdata = record->rdata<PtrRecordRdata>();
+ return rdata->ptrdomain();
+ }
+ default: // Most records are considered unique for our purposes
+ return "";
+ }
+}
+
+base::Time MDnsCache::GetEffectiveExpiration(const RecordParsed* record) {
+ base::TimeDelta ttl;
+
+ if (record->ttl()) {
+ ttl = base::TimeDelta::FromSeconds(record->ttl());
+ } else {
+ ttl = base::TimeDelta::FromSeconds(kZeroTTLSeconds);
+ }
+
+ return record->time_created() + ttl;
+}
+
+MDnsCache::TypeOnlyQuery::TypeOnlyQuery(uint16 type) : type_(type) {
+}
+
+MDnsCache::TypeOnlyQuery::~TypeOnlyQuery() {
+}
+
+MDnsCache::DnsRecordCacheKey MDnsCache::TypeOnlyQuery::GetLowerBoundKey(
+) const {
+ return MDnsCache::DnsRecordCacheKey(type_, "", "");
+}
+
+bool MDnsCache::TypeOnlyQuery::IsMatching(
+ const MDnsCache::DnsRecordCacheKey& key) const {
+ return key.type() == type_;
+}
+
+MDnsCache::TypeNameQuery::TypeNameQuery(
+ uint16 type, const std::string& name) : type_(type), name_(name) {
+}
+
+MDnsCache::TypeNameQuery::~TypeNameQuery() {
+}
+
+MDnsCache::DnsRecordCacheKey MDnsCache::TypeNameQuery::GetLowerBoundKey(
+) const {
+ return MDnsCache::DnsRecordCacheKey(type_, name_, "");
+}
+
+bool MDnsCache::TypeNameQuery::IsMatching(
+ const MDnsCache::DnsRecordCacheKey& key) const {
+ return key.type() == type_ && key.name() == name_;
+}
+
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698