forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdns_config_service_android.cc
207 lines (170 loc) · 6.27 KB
/
dns_config_service_android.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright 2021 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/dns_config_service_android.h"
#include <sys/system_properties.h>
#include <memory>
#include <string>
#include <utility>
#include "base/android/build_info.h"
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/sequence_checker.h"
#include "base/time/time.h"
#include "net/android/network_library.h"
#include "net/base/address_tracker_linux.h"
#include "net/base/ip_address.h"
#include "net/base/ip_endpoint.h"
#include "net/base/network_change_notifier.h"
#include "net/base/network_interfaces.h"
#include "net/dns/dns_config.h"
#include "net/dns/dns_config_service.h"
#include "net/dns/public/dns_protocol.h"
#include "net/dns/serial_worker.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
namespace net {
namespace internal {
namespace {
constexpr base::FilePath::CharType kFilePathHosts[] =
FILE_PATH_LITERAL("/system/etc/hosts");
bool IsVpnPresent() {
NetworkInterfaceList networks;
if (!GetNetworkList(&networks, INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES))
return false;
for (NetworkInterface network : networks) {
if (AddressTrackerLinux::IsTunnelInterfaceName(network.name.c_str()))
return true;
}
return false;
}
} // namespace
// static
constexpr base::TimeDelta DnsConfigServiceAndroid::kConfigChangeDelay;
class DnsConfigServiceAndroid::Watcher
: public DnsConfigService::Watcher,
public NetworkChangeNotifier::NetworkChangeObserver {
public:
explicit Watcher(DnsConfigServiceAndroid& service)
: DnsConfigService::Watcher(service) {}
~Watcher() override {
NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
}
Watcher(const Watcher&) = delete;
Watcher& operator=(const Watcher&) = delete;
// DnsConfigService::Watcher:
bool Watch() override {
CheckOnCorrectSequence();
// On Android, assume DNS config may have changed on every network change.
NetworkChangeNotifier::AddNetworkChangeObserver(this);
// Hosts file should never change on Android (and watching it is
// problematic; see http://crbug.com/600442), so don't watch it.
return true;
}
// NetworkChangeNotifier::NetworkChangeObserver:
void OnNetworkChanged(NetworkChangeNotifier::ConnectionType type) override {
if (type != NetworkChangeNotifier::CONNECTION_NONE)
OnConfigChanged(true);
}
};
class DnsConfigServiceAndroid::ConfigReader : public SerialWorker {
public:
explicit ConfigReader(DnsConfigServiceAndroid& service,
android::DnsServerGetter dns_server_getter)
: dns_server_getter_(std::move(dns_server_getter)), service_(&service) {}
ConfigReader(const ConfigReader&) = delete;
ConfigReader& operator=(const ConfigReader&) = delete;
void DoWork() override {
dns_config_.emplace();
dns_config_->unhandled_options = false;
if (base::android::BuildInfo::GetInstance()->sdk_int() >=
base::android::SDK_VERSION_MARSHMALLOW) {
if (!dns_server_getter_.Run(
&dns_config_->nameservers, &dns_config_->dns_over_tls_active,
&dns_config_->dns_over_tls_hostname, &dns_config_->search)) {
dns_config_.reset();
}
return;
}
if (IsVpnPresent()) {
dns_config_->unhandled_options = true;
}
// NOTE(pauljensen): __system_property_get and the net.dns1/2 properties are
// not supported APIs, but they're only read on pre-Marshmallow Android
// which was released years ago and isn't changing.
char property_value[PROP_VALUE_MAX];
__system_property_get("net.dns1", property_value);
std::string dns1_string = property_value;
__system_property_get("net.dns2", property_value);
std::string dns2_string = property_value;
if (dns1_string.empty() && dns2_string.empty()) {
dns_config_.reset();
return;
}
IPAddress dns1_address;
IPAddress dns2_address;
bool parsed1 = dns1_address.AssignFromIPLiteral(dns1_string);
bool parsed2 = dns2_address.AssignFromIPLiteral(dns2_string);
if (!parsed1 && !parsed2) {
dns_config_.reset();
return;
}
if (parsed1) {
IPEndPoint dns1(dns1_address, dns_protocol::kDefaultPort);
dns_config_->nameservers.push_back(dns1);
}
if (parsed2) {
IPEndPoint dns2(dns2_address, dns_protocol::kDefaultPort);
dns_config_->nameservers.push_back(dns2);
}
}
void OnWorkFinished() override {
DCHECK(!IsCancelled());
if (dns_config_.has_value()) {
service_->OnConfigRead(std::move(dns_config_).value());
} else {
LOG(WARNING) << "Failed to read DnsConfig.";
}
}
private:
~ConfigReader() override = default;
android::DnsServerGetter dns_server_getter_;
// Raw pointer to owning DnsConfigService. This must never be accessed inside
// DoWork(), since service may be destroyed while SerialWorker is running
// on worker thread.
DnsConfigServiceAndroid* const service_;
// Written in DoWork, read in OnWorkFinished, no locking necessary.
absl::optional<DnsConfig> dns_config_;
};
DnsConfigServiceAndroid::DnsConfigServiceAndroid()
: DnsConfigService(kFilePathHosts, kConfigChangeDelay) {
// Allow constructing on one thread and living on another.
DETACH_FROM_SEQUENCE(sequence_checker_);
dns_server_getter_ = base::BindRepeating(&android::GetDnsServers);
}
DnsConfigServiceAndroid::~DnsConfigServiceAndroid() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (config_reader_)
config_reader_->Cancel();
}
void DnsConfigServiceAndroid::ReadConfigNow() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!config_reader_) {
DCHECK(dns_server_getter_);
config_reader_ = base::MakeRefCounted<ConfigReader>(
*this, std::move(dns_server_getter_));
}
config_reader_->WorkNow();
}
bool DnsConfigServiceAndroid::StartWatching() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// TODO(crbug.com/116139): re-start watcher if that makes sense.
watcher_ = std::make_unique<Watcher>(*this);
return watcher_->Watch();
}
} // namespace internal
// static
std::unique_ptr<DnsConfigService> DnsConfigService::CreateSystemService() {
return std::make_unique<internal::DnsConfigServiceAndroid>();
}
} // namespace net