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

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..36efbb71e27c3a8e5398e143b703338390e659a2
--- /dev/null
+++ b/net/dns/mdns_cache.cc
@@ -0,0 +1,169 @@
+// 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/stl_util.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.
szym 2013/05/17 05:04:34 I suggest referring to RFC 6762 Section 10.1 here.
Noam Samuel 2013/05/17 22:26:05 Done.
+static const unsigned kZeroTTLSeconds = 1;
+
+MDnsCache::DnsRecordCacheKey::DnsRecordCacheKey(unsigned 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(
+ const MDnsCache::RecordRemovedCallback& record_removed_callback)
+ : record_removed_callback_(record_removed_callback) {
+}
+
+MDnsCache::~MDnsCache() {
+ Clear();
+}
+
+void MDnsCache::Clear() {
+ next_expiration_ = base::Time();
+ STLDeleteValues(&mdns_cache_);
+}
+
+MDnsCache::UpdateType MDnsCache::UpdateDnsRecord(
+ scoped_ptr<const RecordParsed> record) {
+ UpdateType type = NoChange;
+
+ MDnsCache::DnsRecordCacheKey cache_key = MDnsCache::DnsRecordCacheKey(
+ record->type(),
+ record->name(),
+ GetOptionalFieldForRecord(record.get()));
+
+ base::Time expiration = GetEffectiveExpiration(record.get());
+ if (next_expiration_ == base::Time() || expiration < next_expiration_) {
+ next_expiration_ = expiration;
+ }
+
+ MDnsCacheMap::iterator i = mdns_cache_.find(cache_key);
szym 2013/05/17 05:04:34 I'd suggest: const RecordParsed*& found = mdns_ca
Noam Samuel 2013/05/17 22:26:05 Done.
+ if (i != mdns_cache_.end()) {
+ if (!record->IsEqual(i->second, true)) {
+ type = RecordChanged;
szym 2013/05/17 05:04:34 IMPORTANT: There is no RecordParsed::IsEqual.
Noam Samuel 2013/05/17 22:26:05 Oh whoops. Uploaded the patch wrong. This upload i
+ }
+ delete i->second;
+ i->second = record.release();
+ } else {
+ mdns_cache_[cache_key] = record.release();
+ type = RecordAdded;
+ }
+
+ return type;
+}
+
+void MDnsCache::CleanupRecords(
szym 2013/05/17 05:04:34 nit: fits on a single line.
Noam Samuel 2013/05/17 22:26:05 Done.
+ base::Time now) {
+ base::Time next_expiration;
+
+ // We are guaranteed that next_expiration_ will be at or before the next
szym 2013/05/17 05:04:34 nit: || around variable names.
Noam Samuel 2013/05/17 22:26:05 Done.
+ // expiration. This allows clients to eagrely call CleanupRecords with
+ // impunity.
+ if (now < next_expiration_) return;
+
+ std::vector<MDnsCacheMap::iterator> to_delete;
szym 2013/05/17 05:04:34 Instead of keeping a handle on the iterators, remo
Noam Samuel 2013/05/17 22:26:05 Done.
+ for (MDnsCacheMap::iterator i = mdns_cache_.begin();
+ i != mdns_cache_.end(); i++) {
+ base::Time expiration = GetEffectiveExpiration(i->second);
+ if (now >= expiration) {
+ to_delete.push_back(i);
+ } else {
+ if (next_expiration == base::Time() || expiration < next_expiration) {
+ next_expiration = expiration;
+ }
+ }
+ }
+
+ for (std::vector<MDnsCacheMap::iterator>::iterator i = to_delete.begin();
+ i != to_delete.end(); ++i) {
+ record_removed_callback_.Run((*i)->second);
+ delete (*i)->second;
+ mdns_cache_.erase(*i);
+ }
+
+ next_expiration_ = next_expiration;
+}
+
+void MDnsCache::FindDnsRecords(unsigned type,
+ const std::string& name,
+ std::vector<const RecordParsed*>* results,
+ base::Time now) {
+ results->clear();
szym 2013/05/17 05:04:34 DCHECK(results)
Noam Samuel 2013/05/17 22:26:05 Done.
+
+ MDnsCacheMap::iterator i =
+ mdns_cache_.lower_bound(DnsRecordCacheKey(type, name, ""));
+ for (; i != mdns_cache_.end(); ++i) {
+ if (i->first.type() != type ||
+ (!name.empty() && i->first.name() != name)) {
+ break;
+ }
+
+ const RecordParsed* record = i->second;
+
+ // Records are deleted only upon request to make this op idempotent
+ if (now >= GetEffectiveExpiration(record)) continue;
+
+ results->push_back(record);
+ }
+}
+
+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;
+}
+
+} // namespace net
« net/dns/mdns_cache.h ('K') | « net/dns/mdns_cache.h ('k') | net/dns/mdns_cache_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698