Avi Drissman | 6459548 | 2022-09-14 20:52:29 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Eric Orth | 5906622 | 2019-03-07 23:52:27 | [diff] [blame] | 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/context_host_resolver.h" |
| 6 | |
Eric Orth | 5233c3a | 2019-04-08 17:01:14 | [diff] [blame] | 7 | #include <string> |
Eric Orth | 5906622 | 2019-03-07 23:52:27 | [diff] [blame] | 8 | #include <utility> |
Cammie Smith Barnes | 958c7f3 | 2020-12-17 16:43:11 | [diff] [blame] | 9 | #include <vector> |
Eric Orth | 5906622 | 2019-03-07 23:52:27 | [diff] [blame] | 10 | |
Hans Wennborg | 0924470b | 2020-04-27 21:08:05 | [diff] [blame] | 11 | #include "base/check_op.h" |
Eric Orth | 5906622 | 2019-03-07 23:52:27 | [diff] [blame] | 12 | #include "base/strings/string_piece.h" |
| 13 | #include "base/time/tick_clock.h" |
Eric Orth | 3ad7bd0 | 2019-09-30 23:38:59 | [diff] [blame] | 14 | #include "net/base/net_errors.h" |
Brianna Goldstein | 548f56ab | 2022-10-03 20:29:52 | [diff] [blame] | 15 | #include "net/base/network_anonymization_key.h" |
Eric Orth | 5906622 | 2019-03-07 23:52:27 | [diff] [blame] | 16 | #include "net/dns/dns_config.h" |
Eric Orth | bccf54969 | 2019-04-16 19:20:15 | [diff] [blame] | 17 | #include "net/dns/host_cache.h" |
Eric Orth | c4cca5d | 2021-07-02 19:59:01 | [diff] [blame] | 18 | #include "net/dns/host_resolver.h" |
Eric Orth | 960e706 | 2019-03-08 18:43:54 | [diff] [blame] | 19 | #include "net/dns/host_resolver_manager.h" |
Eric Orth | 5906622 | 2019-03-07 23:52:27 | [diff] [blame] | 20 | #include "net/dns/host_resolver_proc.h" |
Yoichiro Hibara | 6a4e282 | 2022-09-05 04:20:02 | [diff] [blame] | 21 | #include "net/dns/public/host_resolver_results.h" |
Katharine Daly | 886eef7 | 2019-11-22 02:56:03 | [diff] [blame] | 22 | #include "net/dns/public/resolve_error_info.h" |
Eric Orth | 069a3c6e | 2020-01-31 23:14:24 | [diff] [blame] | 23 | #include "net/dns/resolve_context.h" |
Eric Orth | c4cca5d | 2021-07-02 19:59:01 | [diff] [blame] | 24 | #include "net/log/net_log_with_source.h" |
Eric Orth | 5fb3cbb3 | 2019-03-29 17:06:37 | [diff] [blame] | 25 | #include "net/url_request/url_request_context.h" |
Eric Orth | c4cca5d | 2021-07-02 19:59:01 | [diff] [blame] | 26 | #include "third_party/abseil-cpp/absl/types/optional.h" |
| 27 | #include "url/scheme_host_port.h" |
Eric Orth | 5906622 | 2019-03-07 23:52:27 | [diff] [blame] | 28 | |
| 29 | namespace net { |
| 30 | |
Eric Orth | af82b49a | 2020-02-01 01:48:50 | [diff] [blame] | 31 | ContextHostResolver::ContextHostResolver( |
| 32 | HostResolverManager* manager, |
| 33 | std::unique_ptr<ResolveContext> resolve_context) |
| 34 | : manager_(manager), resolve_context_(std::move(resolve_context)) { |
Eric Orth | 9ded7fe | 2019-03-22 16:32:38 | [diff] [blame] | 35 | DCHECK(manager_); |
Eric Orth | af82b49a | 2020-02-01 01:48:50 | [diff] [blame] | 36 | DCHECK(resolve_context_); |
Eric Orth | 37b1819 | 2019-04-22 19:09:28 | [diff] [blame] | 37 | |
Eric Orth | af82b49a | 2020-02-01 01:48:50 | [diff] [blame] | 38 | manager_->RegisterResolveContext(resolve_context_.get()); |
Eric Orth | 9ded7fe | 2019-03-22 16:32:38 | [diff] [blame] | 39 | } |
Eric Orth | 5906622 | 2019-03-07 23:52:27 | [diff] [blame] | 40 | |
Eric Orth | 9ded7fe | 2019-03-22 16:32:38 | [diff] [blame] | 41 | ContextHostResolver::ContextHostResolver( |
Eric Orth | bccf54969 | 2019-04-16 19:20:15 | [diff] [blame] | 42 | std::unique_ptr<HostResolverManager> owned_manager, |
Eric Orth | af82b49a | 2020-02-01 01:48:50 | [diff] [blame] | 43 | std::unique_ptr<ResolveContext> resolve_context) |
| 44 | : ContextHostResolver(owned_manager.get(), std::move(resolve_context)) { |
| 45 | owned_manager_ = std::move(owned_manager); |
Eric Orth | 9ded7fe | 2019-03-22 16:32:38 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | ContextHostResolver::~ContextHostResolver() { |
Benjamin M. Schwartz | 4a561720 | 2021-12-07 22:56:33 | [diff] [blame] | 49 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
Eric Orth | 9ded7fe | 2019-03-22 16:32:38 | [diff] [blame] | 50 | if (owned_manager_) |
| 51 | DCHECK_EQ(owned_manager_.get(), manager_); |
Eric Orth | 5233c3a | 2019-04-08 17:01:14 | [diff] [blame] | 52 | |
Eric Orth | af82b49a | 2020-02-01 01:48:50 | [diff] [blame] | 53 | // No |resolve_context_| to deregister if OnShutdown() was already called. |
| 54 | if (resolve_context_) |
| 55 | manager_->DeregisterResolveContext(resolve_context_.get()); |
Eric Orth | 9ded7fe | 2019-03-22 16:32:38 | [diff] [blame] | 56 | } |
Eric Orth | 5906622 | 2019-03-07 23:52:27 | [diff] [blame] | 57 | |
Eric Orth | 5d9958c | 2019-09-27 20:43:18 | [diff] [blame] | 58 | void ContextHostResolver::OnShutdown() { |
Eric Orth | 3ad7bd0 | 2019-09-30 23:38:59 | [diff] [blame] | 59 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 60 | |
Eric Orth | 069a3c6e | 2020-01-31 23:14:24 | [diff] [blame] | 61 | DCHECK(resolve_context_); |
Eric Orth | af82b49a | 2020-02-01 01:48:50 | [diff] [blame] | 62 | manager_->DeregisterResolveContext(resolve_context_.get()); |
Eric Orth | 069a3c6e | 2020-01-31 23:14:24 | [diff] [blame] | 63 | resolve_context_.reset(); |
Eric Orth | af82b49a | 2020-02-01 01:48:50 | [diff] [blame] | 64 | |
| 65 | DCHECK(!shutting_down_); |
Eric Orth | 3ad7bd0 | 2019-09-30 23:38:59 | [diff] [blame] | 66 | shutting_down_ = true; |
Eric Orth | 5d9958c | 2019-09-27 20:43:18 | [diff] [blame] | 67 | } |
| 68 | |
Eric Orth | 5906622 | 2019-03-07 23:52:27 | [diff] [blame] | 69 | std::unique_ptr<HostResolver::ResolveHostRequest> |
| 70 | ContextHostResolver::CreateRequest( |
Eric Orth | c4cca5d | 2021-07-02 19:59:01 | [diff] [blame] | 71 | url::SchemeHostPort host, |
Brianna Goldstein | 548f56ab | 2022-10-03 20:29:52 | [diff] [blame] | 72 | NetworkAnonymizationKey network_anonymization_key, |
Eric Orth | c4cca5d | 2021-07-02 19:59:01 | [diff] [blame] | 73 | NetLogWithSource source_net_log, |
| 74 | absl::optional<ResolveHostParameters> optional_parameters) { |
| 75 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 76 | |
Benjamin M. Schwartz | 4a561720 | 2021-12-07 22:56:33 | [diff] [blame] | 77 | if (shutting_down_) |
| 78 | return HostResolver::CreateFailingRequest(ERR_CONTEXT_SHUT_DOWN); |
Eric Orth | c4cca5d | 2021-07-02 19:59:01 | [diff] [blame] | 79 | |
Benjamin M. Schwartz | 4a561720 | 2021-12-07 22:56:33 | [diff] [blame] | 80 | return manager_->CreateRequest( |
Brianna Goldstein | 548f56ab | 2022-10-03 20:29:52 | [diff] [blame] | 81 | Host(std::move(host)), std::move(network_anonymization_key), |
Benjamin M. Schwartz | 4a561720 | 2021-12-07 22:56:33 | [diff] [blame] | 82 | std::move(source_net_log), std::move(optional_parameters), |
| 83 | resolve_context_.get(), resolve_context_->host_cache()); |
Eric Orth | c4cca5d | 2021-07-02 19:59:01 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | std::unique_ptr<HostResolver::ResolveHostRequest> |
| 87 | ContextHostResolver::CreateRequest( |
Eric Orth | 5906622 | 2019-03-07 23:52:27 | [diff] [blame] | 88 | const HostPortPair& host, |
Brianna Goldstein | 548f56ab | 2022-10-03 20:29:52 | [diff] [blame] | 89 | const NetworkAnonymizationKey& network_anonymization_key, |
Eric Orth | 5906622 | 2019-03-07 23:52:27 | [diff] [blame] | 90 | const NetLogWithSource& source_net_log, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 91 | const absl::optional<ResolveHostParameters>& optional_parameters) { |
Eric Orth | 3ad7bd0 | 2019-09-30 23:38:59 | [diff] [blame] | 92 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 93 | |
Benjamin M. Schwartz | 4a561720 | 2021-12-07 22:56:33 | [diff] [blame] | 94 | if (shutting_down_) |
| 95 | return HostResolver::CreateFailingRequest(ERR_CONTEXT_SHUT_DOWN); |
Eric Orth | 3ad7bd0 | 2019-09-30 23:38:59 | [diff] [blame] | 96 | |
Brianna Goldstein | 548f56ab | 2022-10-03 20:29:52 | [diff] [blame] | 97 | return manager_->CreateRequest( |
| 98 | host, network_anonymization_key, source_net_log, optional_parameters, |
| 99 | resolve_context_.get(), resolve_context_->host_cache()); |
Eric Orth | 314e8b4 | 2019-11-12 00:32:21 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | std::unique_ptr<HostResolver::ProbeRequest> |
| 103 | ContextHostResolver::CreateDohProbeRequest() { |
| 104 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 105 | |
Benjamin M. Schwartz | 4a561720 | 2021-12-07 22:56:33 | [diff] [blame] | 106 | if (shutting_down_) |
| 107 | return HostResolver::CreateFailingProbeRequest(ERR_CONTEXT_SHUT_DOWN); |
Eric Orth | 314e8b4 | 2019-11-12 00:32:21 | [diff] [blame] | 108 | |
Benjamin M. Schwartz | 4a561720 | 2021-12-07 22:56:33 | [diff] [blame] | 109 | return manager_->CreateDohProbeRequest(resolve_context_.get()); |
Eric Orth | 5906622 | 2019-03-07 23:52:27 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | std::unique_ptr<HostResolver::MdnsListener> |
| 113 | ContextHostResolver::CreateMdnsListener(const HostPortPair& host, |
| 114 | DnsQueryType query_type) { |
Eric Orth | 9ded7fe | 2019-03-22 16:32:38 | [diff] [blame] | 115 | return manager_->CreateMdnsListener(host, query_type); |
Eric Orth | 5906622 | 2019-03-07 23:52:27 | [diff] [blame] | 116 | } |
| 117 | |
Eric Orth | 5906622 | 2019-03-07 23:52:27 | [diff] [blame] | 118 | HostCache* ContextHostResolver::GetHostCache() { |
Eric Orth | af82b49a | 2020-02-01 01:48:50 | [diff] [blame] | 119 | return resolve_context_->host_cache(); |
Eric Orth | 5906622 | 2019-03-07 23:52:27 | [diff] [blame] | 120 | } |
| 121 | |
Panos Astithas | 8aa1ec8 | 2020-08-31 18:37:41 | [diff] [blame] | 122 | base::Value ContextHostResolver::GetDnsConfigAsValue() const { |
Eric Orth | 9ded7fe | 2019-03-22 16:32:38 | [diff] [blame] | 123 | return manager_->GetDnsConfigAsValue(); |
Eric Orth | 5906622 | 2019-03-07 23:52:27 | [diff] [blame] | 124 | } |
| 125 | |
Eric Orth | 5906622 | 2019-03-07 23:52:27 | [diff] [blame] | 126 | void ContextHostResolver::SetRequestContext( |
| 127 | URLRequestContext* request_context) { |
Eric Orth | 3ad7bd0 | 2019-09-30 23:38:59 | [diff] [blame] | 128 | DCHECK(!shutting_down_); |
Eric Orth | af82b49a | 2020-02-01 01:48:50 | [diff] [blame] | 129 | DCHECK(resolve_context_); |
Eric Orth | 3ad7bd0 | 2019-09-30 23:38:59 | [diff] [blame] | 130 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
Eric Orth | 5fb3cbb3 | 2019-03-29 17:06:37 | [diff] [blame] | 131 | |
Eric Orth | af82b49a | 2020-02-01 01:48:50 | [diff] [blame] | 132 | resolve_context_->set_url_request_context(request_context); |
Eric Orth | 5906622 | 2019-03-07 23:52:27 | [diff] [blame] | 133 | } |
| 134 | |
Eric Orth | 5fb3cbb3 | 2019-03-29 17:06:37 | [diff] [blame] | 135 | HostResolverManager* ContextHostResolver::GetManagerForTesting() { |
| 136 | return manager_; |
| 137 | } |
| 138 | |
| 139 | const URLRequestContext* ContextHostResolver::GetContextForTesting() const { |
Eric Orth | 069a3c6e | 2020-01-31 23:14:24 | [diff] [blame] | 140 | return resolve_context_ ? resolve_context_->url_request_context() : nullptr; |
Eric Orth | 5fb3cbb3 | 2019-03-29 17:06:37 | [diff] [blame] | 141 | } |
| 142 | |
Stefano Duo | 6527ed4 | 2022-07-29 09:25:44 | [diff] [blame] | 143 | handles::NetworkHandle ContextHostResolver::GetTargetNetworkForTesting() const { |
Stefano Duo | bf5a8a9 | 2022-04-22 12:14:10 | [diff] [blame] | 144 | return resolve_context_ ? resolve_context_->GetTargetNetwork() |
Stefano Duo | 6527ed4 | 2022-07-29 09:25:44 | [diff] [blame] | 145 | : handles::kInvalidNetworkHandle; |
Stefano Duo | bf5a8a9 | 2022-04-22 12:14:10 | [diff] [blame] | 146 | } |
| 147 | |
Eric Orth | 5906622 | 2019-03-07 23:52:27 | [diff] [blame] | 148 | size_t ContextHostResolver::LastRestoredCacheSize() const { |
Eric Orth | af82b49a | 2020-02-01 01:48:50 | [diff] [blame] | 149 | return resolve_context_->host_cache() |
| 150 | ? resolve_context_->host_cache()->last_restore_size() |
| 151 | : 0; |
Eric Orth | 5906622 | 2019-03-07 23:52:27 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | size_t ContextHostResolver::CacheSize() const { |
Eric Orth | af82b49a | 2020-02-01 01:48:50 | [diff] [blame] | 155 | return resolve_context_->host_cache() ? resolve_context_->host_cache()->size() |
| 156 | : 0; |
Eric Orth | 5906622 | 2019-03-07 23:52:27 | [diff] [blame] | 157 | } |
| 158 | |
Matthew Denton | 3eef9d7a | 2022-09-29 02:00:33 | [diff] [blame] | 159 | void ContextHostResolver::SetHostResolverSystemParamsForTest( |
| 160 | const HostResolverSystemTask::Params& host_resolver_system_params) { |
| 161 | manager_->set_host_resolver_system_params_for_test( // IN-TEST |
| 162 | host_resolver_system_params); |
Eric Orth | 5906622 | 2019-03-07 23:52:27 | [diff] [blame] | 163 | } |
| 164 | |
Eric Orth | 5906622 | 2019-03-07 23:52:27 | [diff] [blame] | 165 | void ContextHostResolver::SetTickClockForTesting( |
| 166 | const base::TickClock* tick_clock) { |
Eric Orth | 9ded7fe | 2019-03-22 16:32:38 | [diff] [blame] | 167 | manager_->SetTickClockForTesting(tick_clock); |
Eric Orth | af82b49a | 2020-02-01 01:48:50 | [diff] [blame] | 168 | if (resolve_context_->host_cache()) |
| 169 | resolve_context_->host_cache()->set_tick_clock_for_testing(tick_clock); |
Eric Orth | 5906622 | 2019-03-07 23:52:27 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | } // namespace net |