OLD | NEW |
(Empty) | |
| 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 |
| 5 #include "net/dns/mdns_cache.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/stl_util.h" |
| 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "net/dns/dns_protocol.h" |
| 12 #include "net/dns/record_parsed.h" |
| 13 #include "net/dns/record_rdata.h" |
| 14 |
| 15 // TODO(noamsml): Recursive CNAME closure (backwards and forwards). |
| 16 |
| 17 namespace net { |
| 18 |
| 19 // The effective TTL given to records with a nominal zero TTL. |
| 20 // Allows time for hosts to send updated records, as detailed in RFC 6762 |
| 21 // Section 10.1. |
| 22 static const unsigned kZeroTTLSeconds = 1; |
| 23 |
| 24 MDnsCache::Key::Key(unsigned type, const std::string& name, |
| 25 const std::string& optional) |
| 26 : type_(type), name_(name), optional_(optional) { |
| 27 } |
| 28 |
| 29 MDnsCache::Key::Key( |
| 30 const MDnsCache::Key& other) |
| 31 : type_(other.type_), name_(other.name_), optional_(other.optional_) { |
| 32 } |
| 33 |
| 34 |
| 35 MDnsCache::Key& MDnsCache::Key::operator=( |
| 36 const MDnsCache::Key& other) { |
| 37 type_ = other.type_; |
| 38 name_ = other.name_; |
| 39 optional_ = other.optional_; |
| 40 return *this; |
| 41 } |
| 42 |
| 43 MDnsCache::Key::~Key() { |
| 44 } |
| 45 |
| 46 bool MDnsCache::Key::operator<(const MDnsCache::Key& key) const { |
| 47 if (type_ != key.type_) |
| 48 return type_ < key.type_; |
| 49 |
| 50 if (name_ != key.name_) |
| 51 return name_ < key.name_; |
| 52 |
| 53 if (optional_ != key.optional_) |
| 54 return optional_ < key.optional_; |
| 55 return false; // keys are equal |
| 56 } |
| 57 |
| 58 bool MDnsCache::Key::operator==(const MDnsCache::Key& key) const { |
| 59 return type_ == key.type_ && name_ == key.name_ && optional_ == key.optional_; |
| 60 } |
| 61 |
| 62 MDnsCache::MDnsCache() { |
| 63 } |
| 64 |
| 65 MDnsCache::~MDnsCache() { |
| 66 Clear(); |
| 67 } |
| 68 |
| 69 void MDnsCache::Clear() { |
| 70 next_expiration_ = base::Time(); |
| 71 STLDeleteValues(&mdns_cache_); |
| 72 } |
| 73 |
| 74 MDnsCache::UpdateType MDnsCache::UpdateDnsRecord( |
| 75 scoped_ptr<const RecordParsed> record) { |
| 76 UpdateType type = NoChange; |
| 77 |
| 78 MDnsCache::Key cache_key = MDnsCache::Key( |
| 79 record->type(), |
| 80 record->name(), |
| 81 GetOptionalFieldForRecord(record.get())); |
| 82 |
| 83 base::Time expiration = GetEffectiveExpiration(record.get()); |
| 84 if (next_expiration_ == base::Time() || expiration < next_expiration_) { |
| 85 next_expiration_ = expiration; |
| 86 } |
| 87 |
| 88 std::pair<RecordMap::iterator, bool> insert_result = |
| 89 mdns_cache_.insert(std::make_pair(cache_key, (const RecordParsed*)NULL)); |
| 90 |
| 91 if (insert_result.second) { |
| 92 type = RecordAdded; |
| 93 insert_result.first->second = record.release(); |
| 94 } else { |
| 95 const RecordParsed* other_record = insert_result.first->second; |
| 96 |
| 97 if (!record->IsEqual(other_record, true)) { |
| 98 type = RecordChanged; |
| 99 } |
| 100 delete other_record; |
| 101 insert_result.first->second = record.release(); |
| 102 } |
| 103 |
| 104 return type; |
| 105 } |
| 106 |
| 107 void MDnsCache::CleanupRecords( |
| 108 base::Time now, |
| 109 const RecordRemovedCallback& record_removed_callback) { |
| 110 base::Time next_expiration; |
| 111 |
| 112 // We are guaranteed that |next_expiration_| will be at or before the next |
| 113 // expiration. This allows clients to eagrely call CleanupRecords with |
| 114 // impunity. |
| 115 if (now < next_expiration_) return; |
| 116 |
| 117 for (RecordMap::iterator i = mdns_cache_.begin(); |
| 118 i != mdns_cache_.end(); ) { |
| 119 base::Time expiration = GetEffectiveExpiration(i->second); |
| 120 if (now >= expiration) { |
| 121 record_removed_callback.Run(i->second); |
| 122 delete i->second; |
| 123 mdns_cache_.erase(i++); |
| 124 } else { |
| 125 if (next_expiration == base::Time() || expiration < next_expiration) { |
| 126 next_expiration = expiration; |
| 127 } |
| 128 ++i; |
| 129 } |
| 130 } |
| 131 |
| 132 next_expiration_ = next_expiration; |
| 133 } |
| 134 |
| 135 void MDnsCache::FindDnsRecords(unsigned type, |
| 136 const std::string& name, |
| 137 std::vector<const RecordParsed*>* results, |
| 138 base::Time now) const { |
| 139 DCHECK(results); |
| 140 results->clear(); |
| 141 |
| 142 RecordMap::const_iterator i = mdns_cache_.lower_bound(Key(type, name, "")); |
| 143 for (; i != mdns_cache_.end(); ++i) { |
| 144 if (i->first.type() != type || |
| 145 (!name.empty() && i->first.name() != name)) { |
| 146 break; |
| 147 } |
| 148 |
| 149 const RecordParsed* record = i->second; |
| 150 |
| 151 // Records are deleted only upon request. |
| 152 if (now >= GetEffectiveExpiration(record)) continue; |
| 153 |
| 154 results->push_back(record); |
| 155 } |
| 156 } |
| 157 |
| 158 std::string MDnsCache::GetOptionalFieldForRecord( |
| 159 const RecordParsed* record) const { |
| 160 switch (record->type()) { |
| 161 case PtrRecordRdata::kType: { |
| 162 const PtrRecordRdata* rdata = record->rdata<PtrRecordRdata>(); |
| 163 return rdata->ptrdomain(); |
| 164 } |
| 165 default: // Most records are considered unique for our purposes |
| 166 return ""; |
| 167 } |
| 168 } |
| 169 |
| 170 base::Time MDnsCache::GetEffectiveExpiration(const RecordParsed* record) const { |
| 171 base::TimeDelta ttl; |
| 172 |
| 173 if (record->ttl()) { |
| 174 ttl = base::TimeDelta::FromSeconds(record->ttl()); |
| 175 } else { |
| 176 ttl = base::TimeDelta::FromSeconds(kZeroTTLSeconds); |
| 177 } |
| 178 |
| 179 return record->time_created() + ttl; |
| 180 } |
| 181 |
| 182 } // namespace net |
OLD | NEW |