host_cache.cc 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. // Copyright (c) 2012 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/host_cache.h"
  5. #include <algorithm>
  6. #include <iterator>
  7. #include <map>
  8. #include <ostream>
  9. #include <string>
  10. #include <type_traits>
  11. #include <unordered_set>
  12. #include <utility>
  13. #include <vector>
  14. #include "base/bind.h"
  15. #include "base/check_op.h"
  16. #include "base/metrics/field_trial.h"
  17. #include "base/metrics/histogram_macros.h"
  18. #include "base/numerics/safe_conversions.h"
  19. #include "base/stl_util.h"
  20. #include "base/strings/string_number_conversions.h"
  21. #include "base/strings/string_piece.h"
  22. #include "base/time/default_tick_clock.h"
  23. #include "base/trace_event/trace_event.h"
  24. #include "base/value_iterators.h"
  25. #include "net/base/address_family.h"
  26. #include "net/base/ip_endpoint.h"
  27. #include "net/base/trace_constants.h"
  28. #include "net/dns/host_resolver.h"
  29. #include "net/dns/https_record_rdata.h"
  30. #include "net/dns/public/dns_protocol.h"
  31. #include "net/dns/public/host_resolver_source.h"
  32. #include "net/log/net_log.h"
  33. #include "third_party/abseil-cpp/absl/types/optional.h"
  34. #include "third_party/abseil-cpp/absl/types/variant.h"
  35. #include "url/scheme_host_port.h"
  36. namespace net {
  37. namespace {
  38. #define CACHE_HISTOGRAM_TIME(name, time) \
  39. UMA_HISTOGRAM_LONG_TIMES("DNS.HostCache." name, time)
  40. #define CACHE_HISTOGRAM_COUNT(name, count) \
  41. UMA_HISTOGRAM_COUNTS_1000("DNS.HostCache." name, count)
  42. #define CACHE_HISTOGRAM_ENUM(name, value, max) \
  43. UMA_HISTOGRAM_ENUMERATION("DNS.HostCache." name, value, max)
  44. // String constants for dictionary keys.
  45. const char kSchemeKey[] = "scheme";
  46. const char kHostnameKey[] = "hostname";
  47. const char kPortKey[] = "port";
  48. const char kDnsQueryTypeKey[] = "dns_query_type";
  49. const char kFlagsKey[] = "flags";
  50. const char kHostResolverSourceKey[] = "host_resolver_source";
  51. const char kSecureKey[] = "secure";
  52. const char kNetworkIsolationKeyKey[] = "network_isolation_key";
  53. const char kExpirationKey[] = "expiration";
  54. const char kTtlKey[] = "ttl";
  55. const char kPinnedKey[] = "pinned";
  56. const char kNetworkChangesKey[] = "network_changes";
  57. const char kNetErrorKey[] = "net_error";
  58. const char kIpEndpointsKey[] = "ip_endpoints";
  59. const char kEndpointAddressKey[] = "endpoint_address";
  60. const char kEndpointPortKey[] = "endpoint_port";
  61. const char kEndpointMetadatasKey[] = "endpoint_metadatas";
  62. const char kEndpointMetadataWeightKey[] = "endpoint_metadata_weight";
  63. const char kEndpointMetadataValueKey[] = "endpoint_metadata_value";
  64. const char kAliasesKey[] = "aliases";
  65. const char kAddressesKey[] = "addresses";
  66. const char kTextRecordsKey[] = "text_records";
  67. const char kHostnameResultsKey[] = "hostname_results";
  68. const char kHostPortsKey[] = "host_ports";
  69. const char kCanonicalNamesKey[] = "canonical_names";
  70. base::Value IpEndpointToValue(const IPEndPoint& endpoint) {
  71. base::Value::Dict dictionary;
  72. dictionary.Set(kEndpointAddressKey, endpoint.ToStringWithoutPort());
  73. dictionary.Set(kEndpointPortKey, endpoint.port());
  74. return base::Value(std::move(dictionary));
  75. }
  76. absl::optional<IPEndPoint> IpEndpointFromValue(const base::Value& value) {
  77. if (!value.is_dict())
  78. return absl::nullopt;
  79. const base::Value::Dict& dict = value.GetDict();
  80. const std::string* ip_str = dict.FindString(kEndpointAddressKey);
  81. absl::optional<int> port = dict.FindInt(kEndpointPortKey);
  82. if (!ip_str || !port ||
  83. !base::IsValueInRangeForNumericType<uint16_t>(port.value())) {
  84. return absl::nullopt;
  85. }
  86. IPAddress ip;
  87. if (!ip.AssignFromIPLiteral(*ip_str))
  88. return absl::nullopt;
  89. return IPEndPoint(ip, base::checked_cast<uint16_t>(port.value()));
  90. }
  91. base::Value EndpointMetadataPairToValue(
  92. const std::pair<HttpsRecordPriority, ConnectionEndpointMetadata>& pair) {
  93. base::Value::Dict dictionary;
  94. dictionary.Set(kEndpointMetadataWeightKey, pair.first);
  95. dictionary.Set(kEndpointMetadataValueKey, pair.second.ToValue());
  96. return base::Value(std::move(dictionary));
  97. }
  98. absl::optional<std::pair<HttpsRecordPriority, ConnectionEndpointMetadata>>
  99. EndpointMetadataPairFromValue(const base::Value& value) {
  100. if (!value.is_dict())
  101. return absl::nullopt;
  102. const base::Value::Dict& dict = value.GetDict();
  103. absl::optional<int> priority = dict.FindInt(kEndpointMetadataWeightKey);
  104. const base::Value* metadata_value = dict.Find(kEndpointMetadataValueKey);
  105. if (!priority || !base::IsValueInRangeForNumericType<HttpsRecordPriority>(
  106. priority.value())) {
  107. return absl::nullopt;
  108. }
  109. if (!metadata_value)
  110. return absl::nullopt;
  111. absl::optional<ConnectionEndpointMetadata> metadata =
  112. ConnectionEndpointMetadata::FromValue(*metadata_value);
  113. if (!metadata)
  114. return absl::nullopt;
  115. return std::make_pair(
  116. base::checked_cast<HttpsRecordPriority>(priority.value()),
  117. std::move(metadata).value());
  118. }
  119. bool IPEndPointsFromLegacyAddressListValue(
  120. const base::Value::List& value,
  121. absl::optional<std::vector<IPEndPoint>>* ip_endpoints) {
  122. ip_endpoints->emplace();
  123. for (const auto& it : value) {
  124. IPAddress address;
  125. const std::string* addr_string = it.GetIfString();
  126. if (!addr_string || !address.AssignFromIPLiteral(*addr_string)) {
  127. return false;
  128. }
  129. ip_endpoints->value().emplace_back(address, 0);
  130. }
  131. return true;
  132. }
  133. template <typename T>
  134. void MergeLists(absl::optional<T>* target, const absl::optional<T>& source) {
  135. if (target->has_value() && source) {
  136. target->value().insert(target->value().end(), source.value().begin(),
  137. source.value().end());
  138. } else if (source) {
  139. *target = source;
  140. }
  141. }
  142. template <typename T>
  143. void MergeContainers(absl::optional<T>& target,
  144. const absl::optional<T>& source) {
  145. if (target.has_value() && source.has_value()) {
  146. target->insert(source->begin(), source->end());
  147. } else if (source) {
  148. target = source;
  149. }
  150. }
  151. // Used to reject empty and IP literal (whether or not surrounded by brackets)
  152. // hostnames.
  153. bool IsValidHostname(base::StringPiece hostname) {
  154. if (hostname.empty())
  155. return false;
  156. IPAddress ip_address;
  157. if (ip_address.AssignFromIPLiteral(hostname) ||
  158. ParseURLHostnameToAddress(hostname, &ip_address)) {
  159. return false;
  160. }
  161. return true;
  162. }
  163. const std::string& GetHostname(
  164. const absl::variant<url::SchemeHostPort, std::string>& host) {
  165. const std::string* hostname;
  166. if (absl::holds_alternative<url::SchemeHostPort>(host)) {
  167. hostname = &absl::get<url::SchemeHostPort>(host).host();
  168. } else {
  169. DCHECK(absl::holds_alternative<std::string>(host));
  170. hostname = &absl::get<std::string>(host);
  171. }
  172. DCHECK(IsValidHostname(*hostname));
  173. return *hostname;
  174. }
  175. } // namespace
  176. // Used in histograms; do not modify existing values.
  177. enum HostCache::SetOutcome : int {
  178. SET_INSERT = 0,
  179. SET_UPDATE_VALID = 1,
  180. SET_UPDATE_STALE = 2,
  181. MAX_SET_OUTCOME
  182. };
  183. // Used in histograms; do not modify existing values.
  184. enum HostCache::LookupOutcome : int {
  185. LOOKUP_MISS_ABSENT = 0,
  186. LOOKUP_MISS_STALE = 1,
  187. LOOKUP_HIT_VALID = 2,
  188. LOOKUP_HIT_STALE = 3,
  189. MAX_LOOKUP_OUTCOME
  190. };
  191. // Used in histograms; do not modify existing values.
  192. enum HostCache::EraseReason : int {
  193. ERASE_EVICT = 0,
  194. ERASE_CLEAR = 1,
  195. ERASE_DESTRUCT = 2,
  196. MAX_ERASE_REASON
  197. };
  198. HostCache::Key::Key(absl::variant<url::SchemeHostPort, std::string> host,
  199. DnsQueryType dns_query_type,
  200. HostResolverFlags host_resolver_flags,
  201. HostResolverSource host_resolver_source,
  202. const NetworkIsolationKey& network_isolation_key)
  203. : host(std::move(host)),
  204. dns_query_type(dns_query_type),
  205. host_resolver_flags(host_resolver_flags),
  206. host_resolver_source(host_resolver_source),
  207. network_isolation_key(network_isolation_key) {
  208. DCHECK(IsValidHostname(GetHostname(this->host)));
  209. if (absl::holds_alternative<url::SchemeHostPort>(this->host))
  210. DCHECK(absl::get<url::SchemeHostPort>(this->host).IsValid());
  211. }
  212. HostCache::Key::Key() = default;
  213. HostCache::Key::Key(const Key& key) = default;
  214. HostCache::Key::Key(Key&& key) = default;
  215. HostCache::Key::~Key() = default;
  216. HostCache::Entry::Entry(int error,
  217. Source source,
  218. absl::optional<base::TimeDelta> ttl)
  219. : error_(error), source_(source), ttl_(ttl.value_or(base::Seconds(-1))) {
  220. // If |ttl| has a value, must not be negative.
  221. DCHECK_GE(ttl.value_or(base::TimeDelta()), base::TimeDelta());
  222. DCHECK_NE(OK, error_);
  223. // host_cache.h defines its own `HttpsRecordPriority` due to
  224. // https_record_rdata.h not being allowed in the same places, but the types
  225. // should still be the same thing.
  226. static_assert(std::is_same<net::HttpsRecordPriority,
  227. HostCache::Entry::HttpsRecordPriority>::value,
  228. "`net::HttpsRecordPriority` and "
  229. "`HostCache::Entry::HttpsRecordPriority` must be same type");
  230. }
  231. HostCache::Entry::Entry(const Entry& entry) = default;
  232. HostCache::Entry::Entry(Entry&& entry) = default;
  233. HostCache::Entry::~Entry() = default;
  234. absl::optional<std::vector<HostResolverEndpointResult>>
  235. HostCache::Entry::GetEndpoints() const {
  236. if (!ip_endpoints_.has_value())
  237. return absl::nullopt;
  238. std::vector<HostResolverEndpointResult> endpoints;
  239. if (ip_endpoints_.value().empty())
  240. return endpoints;
  241. absl::optional<std::vector<ConnectionEndpointMetadata>> metadatas =
  242. GetMetadatas();
  243. if (metadatas.has_value() && canonical_names_ &&
  244. (canonical_names_->size() == 1)) {
  245. // Currently Chrome uses HTTPS records only when A and AAAA records are at
  246. // the same canonical name and that matches the HTTPS target name.
  247. for (ConnectionEndpointMetadata& metadata : metadatas.value()) {
  248. if (canonical_names_->find(metadata.target_name) ==
  249. canonical_names_->end()) {
  250. continue;
  251. }
  252. endpoints.emplace_back();
  253. endpoints.back().ip_endpoints = ip_endpoints_.value();
  254. endpoints.back().metadata = std::move(metadata);
  255. }
  256. }
  257. // Add a final non-protocol endpoint at the end.
  258. endpoints.emplace_back();
  259. endpoints.back().ip_endpoints = ip_endpoints_.value();
  260. return endpoints;
  261. }
  262. absl::optional<std::vector<ConnectionEndpointMetadata>>
  263. HostCache::Entry::GetMetadatas() const {
  264. if (!endpoint_metadatas_.has_value())
  265. return absl::nullopt;
  266. std::vector<ConnectionEndpointMetadata> metadatas;
  267. HttpsRecordPriority last_priority = 0;
  268. for (const auto& metadata : endpoint_metadatas_.value()) {
  269. // Ensure metadatas are iterated in priority order.
  270. DCHECK_GE(metadata.first, last_priority);
  271. last_priority = metadata.first;
  272. metadatas.push_back(metadata.second);
  273. }
  274. return metadatas;
  275. }
  276. absl::optional<base::TimeDelta> HostCache::Entry::GetOptionalTtl() const {
  277. if (has_ttl())
  278. return ttl();
  279. else
  280. return absl::nullopt;
  281. }
  282. // static
  283. HostCache::Entry HostCache::Entry::MergeEntries(Entry front, Entry back) {
  284. // Only expected to merge OK or ERR_NAME_NOT_RESOLVED results.
  285. DCHECK(front.error() == OK || front.error() == ERR_NAME_NOT_RESOLVED);
  286. DCHECK(back.error() == OK || back.error() == ERR_NAME_NOT_RESOLVED);
  287. // Build results in |front| to preserve unmerged fields.
  288. front.error_ =
  289. front.error() == OK || back.error() == OK ? OK : ERR_NAME_NOT_RESOLVED;
  290. MergeLists(&front.ip_endpoints_, back.ip_endpoints_);
  291. MergeContainers(front.endpoint_metadatas_, back.endpoint_metadatas_);
  292. MergeContainers(front.aliases_, back.aliases_);
  293. MergeLists(&front.text_records_, back.text_records());
  294. MergeLists(&front.hostnames_, back.hostnames());
  295. MergeLists(&front.https_record_compatibility_,
  296. back.https_record_compatibility_);
  297. MergeContainers(front.canonical_names_, back.canonical_names_);
  298. // Only expected to merge entries from same source.
  299. DCHECK_EQ(front.source(), back.source());
  300. if (front.has_ttl() && back.has_ttl()) {
  301. front.ttl_ = std::min(front.ttl(), back.ttl());
  302. } else if (back.has_ttl()) {
  303. front.ttl_ = back.ttl();
  304. }
  305. front.expires_ = std::min(front.expires(), back.expires());
  306. front.network_changes_ =
  307. std::max(front.network_changes(), back.network_changes());
  308. front.total_hits_ = front.total_hits_ + back.total_hits_;
  309. front.stale_hits_ = front.stale_hits_ + back.stale_hits_;
  310. return front;
  311. }
  312. HostCache::Entry HostCache::Entry::CopyWithDefaultPort(uint16_t port) const {
  313. Entry copy(*this);
  314. if (copy.ip_endpoints_) {
  315. for (IPEndPoint& endpoint : copy.ip_endpoints_.value()) {
  316. if (endpoint.port() == 0)
  317. endpoint = IPEndPoint(endpoint.address(), port);
  318. }
  319. }
  320. if (copy.hostnames_) {
  321. for (HostPortPair& hostname : copy.hostnames_.value()) {
  322. if (hostname.port() == 0)
  323. hostname = HostPortPair(hostname.host(), port);
  324. }
  325. }
  326. return copy;
  327. }
  328. HostCache::Entry& HostCache::Entry::operator=(const Entry& entry) = default;
  329. HostCache::Entry& HostCache::Entry::operator=(Entry&& entry) = default;
  330. HostCache::Entry::Entry(int error,
  331. std::vector<IPEndPoint> ip_endpoints,
  332. std::set<std::string> aliases,
  333. Source source,
  334. absl::optional<base::TimeDelta> ttl)
  335. : error_(error),
  336. ip_endpoints_(std::move(ip_endpoints)),
  337. aliases_(std::move(aliases)),
  338. source_(source),
  339. ttl_(ttl ? ttl.value() : base::Seconds(-1)) {
  340. DCHECK(!ttl || ttl.value() >= base::TimeDelta());
  341. }
  342. HostCache::Entry::Entry(const HostCache::Entry& entry,
  343. base::TimeTicks now,
  344. base::TimeDelta ttl,
  345. int network_changes)
  346. : error_(entry.error()),
  347. ip_endpoints_(entry.ip_endpoints_),
  348. endpoint_metadatas_(entry.endpoint_metadatas_),
  349. aliases_(base::OptionalFromPtr(entry.aliases())),
  350. text_records_(entry.text_records()),
  351. hostnames_(entry.hostnames()),
  352. https_record_compatibility_(entry.https_record_compatibility_),
  353. source_(entry.source()),
  354. pinning_(entry.pinning()),
  355. canonical_names_(entry.canonical_names()),
  356. ttl_(entry.ttl()),
  357. expires_(now + ttl),
  358. network_changes_(network_changes) {}
  359. HostCache::Entry::Entry(
  360. int error,
  361. absl::optional<std::vector<IPEndPoint>> ip_endpoints,
  362. absl::optional<
  363. std::multimap<HttpsRecordPriority, ConnectionEndpointMetadata>>
  364. endpoint_metadatas,
  365. absl::optional<std::set<std::string>> aliases,
  366. absl::optional<std::vector<std::string>>&& text_records,
  367. absl::optional<std::vector<HostPortPair>>&& hostnames,
  368. absl::optional<std::vector<bool>>&& https_record_compatibility,
  369. Source source,
  370. base::TimeTicks expires,
  371. int network_changes)
  372. : error_(error),
  373. ip_endpoints_(std::move(ip_endpoints)),
  374. endpoint_metadatas_(std::move(endpoint_metadatas)),
  375. aliases_(std::move(aliases)),
  376. text_records_(std::move(text_records)),
  377. hostnames_(std::move(hostnames)),
  378. https_record_compatibility_(std::move(https_record_compatibility)),
  379. source_(source),
  380. expires_(expires),
  381. network_changes_(network_changes) {}
  382. void HostCache::Entry::PrepareForCacheInsertion() {
  383. https_record_compatibility_.reset();
  384. }
  385. bool HostCache::Entry::IsStale(base::TimeTicks now, int network_changes) const {
  386. EntryStaleness stale;
  387. stale.expired_by = now - expires_;
  388. stale.network_changes = network_changes - network_changes_;
  389. stale.stale_hits = stale_hits_;
  390. return stale.is_stale();
  391. }
  392. void HostCache::Entry::CountHit(bool hit_is_stale) {
  393. ++total_hits_;
  394. if (hit_is_stale)
  395. ++stale_hits_;
  396. }
  397. void HostCache::Entry::GetStaleness(base::TimeTicks now,
  398. int network_changes,
  399. EntryStaleness* out) const {
  400. DCHECK(out);
  401. out->expired_by = now - expires_;
  402. out->network_changes = network_changes - network_changes_;
  403. out->stale_hits = stale_hits_;
  404. }
  405. base::Value HostCache::Entry::NetLogParams() const {
  406. return base::Value(GetAsValue(false /* include_staleness */));
  407. }
  408. base::Value::Dict HostCache::Entry::GetAsValue(bool include_staleness) const {
  409. base::Value::Dict entry_dict;
  410. if (include_staleness) {
  411. // The kExpirationKey value is using TimeTicks instead of Time used if
  412. // |include_staleness| is false, so it cannot be used to deserialize.
  413. // This is ok as it is used only for netlog.
  414. entry_dict.Set(kExpirationKey, NetLog::TickCountToString(expires()));
  415. entry_dict.Set(kTtlKey, base::saturated_cast<int>(ttl().InMilliseconds()));
  416. entry_dict.Set(kNetworkChangesKey, network_changes());
  417. // The "pinned" status is meaningful only if "network_changes" is also
  418. // preserved.
  419. if (pinning())
  420. entry_dict.Set(kPinnedKey, *pinning());
  421. } else {
  422. // Convert expiration time in TimeTicks to Time for serialization, using a
  423. // string because base::Value doesn't handle 64-bit integers.
  424. base::Time expiration_time =
  425. base::Time::Now() - (base::TimeTicks::Now() - expires());
  426. entry_dict.Set(kExpirationKey,
  427. base::NumberToString(expiration_time.ToInternalValue()));
  428. }
  429. if (error() != OK) {
  430. entry_dict.Set(kNetErrorKey, error());
  431. } else {
  432. if (ip_endpoints_) {
  433. base::Value::List ip_endpoints_list;
  434. for (const IPEndPoint& ip_endpoint : ip_endpoints_.value()) {
  435. ip_endpoints_list.Append(IpEndpointToValue(ip_endpoint));
  436. }
  437. entry_dict.Set(kIpEndpointsKey, std::move(ip_endpoints_list));
  438. }
  439. if (endpoint_metadatas_) {
  440. base::Value::List endpoint_metadatas_list;
  441. for (const auto& endpoint_metadata_pair : endpoint_metadatas_.value()) {
  442. endpoint_metadatas_list.Append(
  443. EndpointMetadataPairToValue(endpoint_metadata_pair));
  444. }
  445. entry_dict.Set(kEndpointMetadatasKey, std::move(endpoint_metadatas_list));
  446. }
  447. if (aliases()) {
  448. base::Value::List alias_list;
  449. for (const std::string& alias : *aliases()) {
  450. alias_list.Append(alias);
  451. }
  452. entry_dict.Set(kAliasesKey, std::move(alias_list));
  453. }
  454. if (text_records()) {
  455. // Append all resolved text records.
  456. base::Value::List text_list_value;
  457. for (const std::string& text_record : text_records().value()) {
  458. text_list_value.Append(text_record);
  459. }
  460. entry_dict.Set(kTextRecordsKey, std::move(text_list_value));
  461. }
  462. if (hostnames()) {
  463. // Append all the resolved hostnames.
  464. base::Value::List hostnames_value;
  465. base::Value::List host_ports_value;
  466. for (const HostPortPair& hostname : hostnames().value()) {
  467. hostnames_value.Append(hostname.host());
  468. host_ports_value.Append(hostname.port());
  469. }
  470. entry_dict.Set(kHostnameResultsKey, std::move(hostnames_value));
  471. entry_dict.Set(kHostPortsKey, std::move(host_ports_value));
  472. }
  473. if (canonical_names()) {
  474. base::Value::List canonical_names_list;
  475. for (const std::string& canonical_name : canonical_names().value()) {
  476. canonical_names_list.Append(canonical_name);
  477. }
  478. entry_dict.Set(kCanonicalNamesKey, std::move(canonical_names_list));
  479. }
  480. }
  481. return entry_dict;
  482. }
  483. // static
  484. const HostCache::EntryStaleness HostCache::kNotStale = {base::Seconds(-1), 0,
  485. 0};
  486. HostCache::HostCache(size_t max_entries)
  487. : max_entries_(max_entries),
  488. tick_clock_(base::DefaultTickClock::GetInstance()) {}
  489. HostCache::~HostCache() {
  490. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  491. }
  492. const std::pair<const HostCache::Key, HostCache::Entry>*
  493. HostCache::Lookup(const Key& key, base::TimeTicks now, bool ignore_secure) {
  494. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  495. if (caching_is_disabled())
  496. return nullptr;
  497. auto* result = LookupInternalIgnoringFields(key, now, ignore_secure);
  498. if (!result)
  499. return nullptr;
  500. auto* entry = &result->second;
  501. if (entry->IsStale(now, network_changes_))
  502. return nullptr;
  503. entry->CountHit(/* hit_is_stale= */ false);
  504. return result;
  505. }
  506. const std::pair<const HostCache::Key, HostCache::Entry>* HostCache::LookupStale(
  507. const Key& key,
  508. base::TimeTicks now,
  509. HostCache::EntryStaleness* stale_out,
  510. bool ignore_secure) {
  511. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  512. if (caching_is_disabled())
  513. return nullptr;
  514. auto* result = LookupInternalIgnoringFields(key, now, ignore_secure);
  515. if (!result)
  516. return nullptr;
  517. auto* entry = &result->second;
  518. bool is_stale = entry->IsStale(now, network_changes_);
  519. entry->CountHit(/* hit_is_stale= */ is_stale);
  520. if (stale_out)
  521. entry->GetStaleness(now, network_changes_, stale_out);
  522. return result;
  523. }
  524. // static
  525. std::pair<const HostCache::Key, HostCache::Entry>*
  526. HostCache::GetLessStaleMoreSecureResult(
  527. base::TimeTicks now,
  528. std::pair<const HostCache::Key, HostCache::Entry>* result1,
  529. std::pair<const HostCache::Key, HostCache::Entry>* result2) {
  530. // Prefer a non-null result if possible.
  531. if (!result1 && !result2)
  532. return nullptr;
  533. if (result1 && !result2)
  534. return result1;
  535. if (!result1 && result2)
  536. return result2;
  537. // Both result1 are result2 are non-null.
  538. EntryStaleness staleness1, staleness2;
  539. result1->second.GetStaleness(now, 0, &staleness1);
  540. result2->second.GetStaleness(now, 0, &staleness2);
  541. if (staleness1.network_changes == staleness2.network_changes) {
  542. // Exactly one of the results should be secure.
  543. DCHECK(result1->first.secure != result2->first.secure);
  544. // If the results have the same number of network changes, prefer a
  545. // non-expired result.
  546. if (staleness1.expired_by.is_negative() &&
  547. staleness2.expired_by >= base::TimeDelta()) {
  548. return result1;
  549. }
  550. if (staleness1.expired_by >= base::TimeDelta() &&
  551. staleness2.expired_by.is_negative()) {
  552. return result2;
  553. }
  554. // Both results are equally stale, so prefer a secure result.
  555. return (result1->first.secure) ? result1 : result2;
  556. }
  557. // Prefer the result with the fewest network changes.
  558. return (staleness1.network_changes < staleness2.network_changes) ? result1
  559. : result2;
  560. }
  561. std::pair<const HostCache::Key, HostCache::Entry>*
  562. HostCache::LookupInternalIgnoringFields(const Key& initial_key,
  563. base::TimeTicks now,
  564. bool ignore_secure) {
  565. std::pair<const HostCache::Key, HostCache::Entry>* preferred_result =
  566. LookupInternal(initial_key);
  567. if (ignore_secure) {
  568. Key effective_key = initial_key;
  569. effective_key.secure = !initial_key.secure;
  570. preferred_result = GetLessStaleMoreSecureResult(
  571. now, preferred_result, LookupInternal(effective_key));
  572. }
  573. return preferred_result;
  574. }
  575. std::pair<const HostCache::Key, HostCache::Entry>* HostCache::LookupInternal(
  576. const Key& key) {
  577. auto it = entries_.find(key);
  578. return (it != entries_.end()) ? &*it : nullptr;
  579. }
  580. void HostCache::Set(const Key& key,
  581. const Entry& entry,
  582. base::TimeTicks now,
  583. base::TimeDelta ttl) {
  584. TRACE_EVENT0(NetTracingCategory(), "HostCache::Set");
  585. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  586. if (caching_is_disabled())
  587. return;
  588. bool has_active_pin = false;
  589. bool result_changed = false;
  590. auto it = entries_.find(key);
  591. if (it != entries_.end()) {
  592. has_active_pin = HasActivePin(it->second);
  593. // TODO(juliatuttle): Remember some old metadata (hit count or frequency or
  594. // something like that) if it's useful for better eviction algorithms?
  595. result_changed = entry.error() == OK && !it->second.ContentsEqual(entry);
  596. entries_.erase(it);
  597. } else {
  598. result_changed = true;
  599. // This loop almost always runs at most once, for total runtime
  600. // O(max_entries_). It only runs more than once if the cache was over-full
  601. // due to pinned entries, and this is the first call to Set() after
  602. // Invalidate(). The amortized cost remains O(size()) per call to Set().
  603. while (size() >= max_entries_ && EvictOneEntry(now)) {
  604. }
  605. }
  606. Entry entry_for_cache(entry, now, ttl, network_changes_);
  607. entry_for_cache.set_pinning(entry.pinning().value_or(has_active_pin));
  608. entry_for_cache.PrepareForCacheInsertion();
  609. AddEntry(key, std::move(entry_for_cache));
  610. if (delegate_ && result_changed)
  611. delegate_->ScheduleWrite();
  612. }
  613. const HostCache::Key* HostCache::GetMatchingKeyForTesting(
  614. base::StringPiece hostname,
  615. HostCache::Entry::Source* source_out,
  616. HostCache::EntryStaleness* stale_out) const {
  617. for (const EntryMap::value_type& entry : entries_) {
  618. if (GetHostname(entry.first.host) == hostname) {
  619. if (source_out != nullptr)
  620. *source_out = entry.second.source();
  621. if (stale_out != nullptr) {
  622. entry.second.GetStaleness(tick_clock_->NowTicks(), network_changes_,
  623. stale_out);
  624. }
  625. return &entry.first;
  626. }
  627. }
  628. return nullptr;
  629. }
  630. void HostCache::AddEntry(const Key& key, Entry&& entry) {
  631. DCHECK_EQ(0u, entries_.count(key));
  632. DCHECK(entry.pinning().has_value());
  633. entries_.emplace(key, std::move(entry));
  634. }
  635. void HostCache::Invalidate() {
  636. ++network_changes_;
  637. }
  638. void HostCache::set_persistence_delegate(PersistenceDelegate* delegate) {
  639. // A PersistenceDelegate shouldn't be added if there already was one, and
  640. // shouldn't be removed (by setting to nullptr) if it wasn't previously there.
  641. DCHECK_NE(delegate == nullptr, delegate_ == nullptr);
  642. delegate_ = delegate;
  643. }
  644. void HostCache::clear() {
  645. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  646. // Don't bother scheduling a write if there's nothing to clear.
  647. if (size() == 0)
  648. return;
  649. entries_.clear();
  650. if (delegate_)
  651. delegate_->ScheduleWrite();
  652. }
  653. void HostCache::ClearForHosts(
  654. const base::RepeatingCallback<bool(const std::string&)>& host_filter) {
  655. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  656. if (host_filter.is_null()) {
  657. clear();
  658. return;
  659. }
  660. bool changed = false;
  661. for (auto it = entries_.begin(); it != entries_.end();) {
  662. auto next_it = std::next(it);
  663. if (host_filter.Run(GetHostname(it->first.host))) {
  664. entries_.erase(it);
  665. changed = true;
  666. }
  667. it = next_it;
  668. }
  669. if (delegate_ && changed)
  670. delegate_->ScheduleWrite();
  671. }
  672. void HostCache::GetList(base::Value::List& entry_list,
  673. bool include_staleness,
  674. SerializationType serialization_type) const {
  675. entry_list.clear();
  676. for (const auto& pair : entries_) {
  677. const Key& key = pair.first;
  678. const Entry& entry = pair.second;
  679. base::Value network_isolation_key_value;
  680. if (serialization_type == SerializationType::kRestorable) {
  681. // Don't save entries associated with ephemeral NetworkIsolationKeys.
  682. if (!key.network_isolation_key.ToValue(&network_isolation_key_value))
  683. continue;
  684. } else {
  685. // ToValue() fails for transient NIKs, since they should never be
  686. // serialized to disk in a restorable format, so use ToDebugString() when
  687. // serializing for debugging instead of for restoring from disk.
  688. network_isolation_key_value =
  689. base::Value(key.network_isolation_key.ToDebugString());
  690. }
  691. base::Value::Dict entry_dict = entry.GetAsValue(include_staleness);
  692. const auto* host = absl::get_if<url::SchemeHostPort>(&key.host);
  693. if (host) {
  694. entry_dict.Set(kSchemeKey, host->scheme());
  695. entry_dict.Set(kHostnameKey, host->host());
  696. entry_dict.Set(kPortKey, host->port());
  697. } else {
  698. entry_dict.Set(kHostnameKey, absl::get<std::string>(key.host));
  699. }
  700. entry_dict.Set(kDnsQueryTypeKey,
  701. base::strict_cast<int>(key.dns_query_type));
  702. entry_dict.Set(kFlagsKey, key.host_resolver_flags);
  703. entry_dict.Set(kHostResolverSourceKey,
  704. base::strict_cast<int>(key.host_resolver_source));
  705. entry_dict.Set(kNetworkIsolationKeyKey,
  706. std::move(network_isolation_key_value));
  707. entry_dict.Set(kSecureKey, key.secure);
  708. entry_list.Append(std::move(entry_dict));
  709. }
  710. }
  711. bool HostCache::RestoreFromListValue(const base::Value::List& old_cache) {
  712. // Reset the restore size to 0.
  713. restore_size_ = 0;
  714. for (const auto& entry : old_cache) {
  715. // If the cache is already full, don't bother prioritizing what to evict,
  716. // just stop restoring.
  717. if (size() == max_entries_)
  718. break;
  719. if (!entry.is_dict())
  720. return false;
  721. const base::Value::Dict& entry_dict = entry.GetDict();
  722. const std::string* hostname_ptr = entry_dict.FindString(kHostnameKey);
  723. if (!hostname_ptr || !IsValidHostname(*hostname_ptr)) {
  724. return false;
  725. }
  726. // Use presence of scheme to determine host type.
  727. const std::string* scheme_ptr = entry_dict.FindString(kSchemeKey);
  728. absl::variant<url::SchemeHostPort, std::string> host;
  729. if (scheme_ptr) {
  730. absl::optional<int> port = entry_dict.FindInt(kPortKey);
  731. if (!port || !base::IsValueInRangeForNumericType<uint16_t>(port.value()))
  732. return false;
  733. url::SchemeHostPort scheme_host_port(*scheme_ptr, *hostname_ptr,
  734. port.value());
  735. if (!scheme_host_port.IsValid())
  736. return false;
  737. host = std::move(scheme_host_port);
  738. } else {
  739. host = *hostname_ptr;
  740. }
  741. const std::string* expiration_ptr = entry_dict.FindString(kExpirationKey);
  742. absl::optional<int> maybe_flags = entry_dict.FindInt(kFlagsKey);
  743. if (expiration_ptr == nullptr || !maybe_flags.has_value())
  744. return false;
  745. std::string expiration(*expiration_ptr);
  746. HostResolverFlags flags = maybe_flags.value();
  747. absl::optional<int> maybe_dns_query_type =
  748. entry_dict.FindInt(kDnsQueryTypeKey);
  749. if (!maybe_dns_query_type.has_value())
  750. return false;
  751. DnsQueryType dns_query_type =
  752. static_cast<DnsQueryType>(maybe_dns_query_type.value());
  753. // HostResolverSource is optional.
  754. int host_resolver_source =
  755. entry_dict.FindInt(kHostResolverSourceKey)
  756. .value_or(base::strict_cast<int>(HostResolverSource::ANY));
  757. const base::Value* network_isolation_key_value =
  758. entry_dict.Find(kNetworkIsolationKeyKey);
  759. NetworkIsolationKey network_isolation_key;
  760. if (!network_isolation_key_value ||
  761. network_isolation_key_value->type() == base::Value::Type::STRING ||
  762. !NetworkIsolationKey::FromValue(*network_isolation_key_value,
  763. &network_isolation_key)) {
  764. return false;
  765. }
  766. bool secure = entry_dict.FindBool(kSecureKey).value_or(false);
  767. int error = OK;
  768. const base::Value::List* ip_endpoints_list = nullptr;
  769. const base::Value::List* endpoint_metadatas_list = nullptr;
  770. const base::Value::List* aliases_list = nullptr;
  771. const base::Value::List* legacy_addresses_list = nullptr;
  772. const base::Value::List* text_records_list = nullptr;
  773. const base::Value::List* hostname_records_list = nullptr;
  774. const base::Value::List* host_ports_list = nullptr;
  775. const base::Value::List* canonical_names_list = nullptr;
  776. absl::optional<int> maybe_error = entry_dict.FindInt(kNetErrorKey);
  777. absl::optional<bool> maybe_pinned = entry_dict.FindBool(kPinnedKey);
  778. if (maybe_error.has_value()) {
  779. error = maybe_error.value();
  780. } else {
  781. ip_endpoints_list = entry_dict.FindList(kIpEndpointsKey);
  782. endpoint_metadatas_list = entry_dict.FindList(kEndpointMetadatasKey);
  783. aliases_list = entry_dict.FindList(kAliasesKey);
  784. legacy_addresses_list = entry_dict.FindList(kAddressesKey);
  785. text_records_list = entry_dict.FindList(kTextRecordsKey);
  786. hostname_records_list = entry_dict.FindList(kHostnameResultsKey);
  787. host_ports_list = entry_dict.FindList(kHostPortsKey);
  788. canonical_names_list = entry_dict.FindList(kCanonicalNamesKey);
  789. if ((hostname_records_list == nullptr && host_ports_list != nullptr) ||
  790. (hostname_records_list != nullptr && host_ports_list == nullptr)) {
  791. return false;
  792. }
  793. }
  794. int64_t time_internal;
  795. if (!base::StringToInt64(expiration, &time_internal))
  796. return false;
  797. base::TimeTicks expiration_time =
  798. tick_clock_->NowTicks() -
  799. (base::Time::Now() - base::Time::FromInternalValue(time_internal));
  800. absl::optional<std::vector<IPEndPoint>> ip_endpoints;
  801. if (ip_endpoints_list) {
  802. ip_endpoints.emplace();
  803. for (const base::Value& ip_endpoint_value : *ip_endpoints_list) {
  804. absl::optional<IPEndPoint> ip_endpoint =
  805. IpEndpointFromValue(ip_endpoint_value);
  806. if (!ip_endpoint)
  807. return false;
  808. ip_endpoints->push_back(std::move(ip_endpoint).value());
  809. }
  810. }
  811. absl::optional<
  812. std::multimap<HttpsRecordPriority, ConnectionEndpointMetadata>>
  813. endpoint_metadatas;
  814. if (endpoint_metadatas_list) {
  815. endpoint_metadatas.emplace();
  816. for (const base::Value& endpoint_metadata_value :
  817. *endpoint_metadatas_list) {
  818. absl::optional<
  819. std::pair<HttpsRecordPriority, ConnectionEndpointMetadata>>
  820. pair = EndpointMetadataPairFromValue(endpoint_metadata_value);
  821. if (!pair)
  822. return false;
  823. endpoint_metadatas->insert(std::move(pair).value());
  824. }
  825. }
  826. absl::optional<std::set<std::string>> aliases;
  827. if (aliases_list) {
  828. aliases.emplace();
  829. for (const base::Value& alias_value : *aliases_list) {
  830. if (!alias_value.is_string())
  831. return false;
  832. aliases->insert(alias_value.GetString());
  833. }
  834. }
  835. // `addresses` field was supported until M105. We keep reading this field
  836. // for backward compatibility for several milestones.
  837. if (legacy_addresses_list) {
  838. if (ip_endpoints)
  839. return false;
  840. if (!IPEndPointsFromLegacyAddressListValue(*legacy_addresses_list,
  841. &ip_endpoints)) {
  842. return false;
  843. }
  844. }
  845. absl::optional<std::vector<std::string>> text_records;
  846. if (text_records_list) {
  847. text_records.emplace();
  848. for (const base::Value& value : *text_records_list) {
  849. if (!value.is_string())
  850. return false;
  851. text_records.value().push_back(value.GetString());
  852. }
  853. }
  854. absl::optional<std::vector<HostPortPair>> hostname_records;
  855. if (hostname_records_list) {
  856. DCHECK(host_ports_list);
  857. if (hostname_records_list->size() != host_ports_list->size()) {
  858. return false;
  859. }
  860. hostname_records.emplace();
  861. for (size_t i = 0; i < hostname_records_list->size(); ++i) {
  862. if (!(*hostname_records_list)[i].is_string() ||
  863. !(*host_ports_list)[i].is_int() ||
  864. !base::IsValueInRangeForNumericType<uint16_t>(
  865. (*host_ports_list)[i].GetInt())) {
  866. return false;
  867. }
  868. hostname_records.value().emplace_back(
  869. (*hostname_records_list)[i].GetString(),
  870. base::checked_cast<uint16_t>((*host_ports_list)[i].GetInt()));
  871. }
  872. }
  873. absl::optional<std::set<std::string>> canonical_names;
  874. if (canonical_names_list) {
  875. canonical_names = std::set<std::string>();
  876. for (const auto& item : *canonical_names_list) {
  877. const std::string* name = item.GetIfString();
  878. if (!name)
  879. return false;
  880. canonical_names->insert(*name);
  881. }
  882. }
  883. // We do not intend to serialize experimental results with the host cache.
  884. absl::optional<std::vector<bool>> experimental_results;
  885. // Assume an empty endpoints list and an empty aliases if we have an address
  886. // type and no results.
  887. if (IsAddressType(dns_query_type) && !text_records && !hostname_records) {
  888. if (!ip_endpoints) {
  889. ip_endpoints.emplace();
  890. }
  891. if (!aliases) {
  892. aliases.emplace();
  893. }
  894. }
  895. Key key(std::move(host), dns_query_type, flags,
  896. static_cast<HostResolverSource>(host_resolver_source),
  897. network_isolation_key);
  898. key.secure = secure;
  899. // If the key is already in the cache, assume it's more recent and don't
  900. // replace the entry.
  901. auto found = entries_.find(key);
  902. if (found == entries_.end()) {
  903. Entry new_entry(error, std::move(ip_endpoints),
  904. std::move(endpoint_metadatas), std::move(aliases),
  905. std::move(text_records), std::move(hostname_records),
  906. std::move(experimental_results), Entry::SOURCE_UNKNOWN,
  907. expiration_time, network_changes_ - 1);
  908. new_entry.set_pinning(maybe_pinned.value_or(false));
  909. new_entry.set_canonical_names(std::move(canonical_names));
  910. AddEntry(key, std::move(new_entry));
  911. restore_size_++;
  912. }
  913. }
  914. return true;
  915. }
  916. size_t HostCache::size() const {
  917. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  918. return entries_.size();
  919. }
  920. size_t HostCache::max_entries() const {
  921. DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  922. return max_entries_;
  923. }
  924. // static
  925. std::unique_ptr<HostCache> HostCache::CreateDefaultCache() {
  926. #if defined(ENABLE_BUILT_IN_DNS)
  927. const size_t kDefaultMaxEntries = 1000;
  928. #else
  929. const size_t kDefaultMaxEntries = 100;
  930. #endif
  931. return std::make_unique<HostCache>(kDefaultMaxEntries);
  932. }
  933. bool HostCache::EvictOneEntry(base::TimeTicks now) {
  934. DCHECK_LT(0u, entries_.size());
  935. absl::optional<net::HostCache::EntryMap::iterator> oldest_it;
  936. for (auto it = entries_.begin(); it != entries_.end(); ++it) {
  937. const Entry& entry = it->second;
  938. if (HasActivePin(entry)) {
  939. continue;
  940. }
  941. if (!oldest_it) {
  942. oldest_it = it;
  943. continue;
  944. }
  945. const Entry& oldest = (*oldest_it)->second;
  946. if ((entry.expires() < oldest.expires()) &&
  947. (entry.IsStale(now, network_changes_) ||
  948. !oldest.IsStale(now, network_changes_))) {
  949. oldest_it = it;
  950. }
  951. }
  952. if (oldest_it) {
  953. entries_.erase(*oldest_it);
  954. return true;
  955. }
  956. return false;
  957. }
  958. bool HostCache::HasActivePin(const Entry& entry) {
  959. return entry.pinning().value_or(false) &&
  960. entry.network_changes() == network_changes();
  961. }
  962. } // namespace net
  963. // Debug logging support
  964. std::ostream& operator<<(std::ostream& out,
  965. const net::HostCache::EntryStaleness& s) {
  966. return out << "EntryStaleness{" << s.expired_by << ", " << s.network_changes
  967. << ", " << s.stale_hits << "}";
  968. }