1 // Copyright 2018 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/dns_config.h"
6 
7 #include <utility>
8 
9 #include "base/values.h"
10 
11 namespace net {
12 
13 // Default values are taken from glibc resolv.h except |fallback_period| which
14 // is set to |kDnsDefaultFallbackPeriod|.
DnsConfig()15 DnsConfig::DnsConfig() : DnsConfig(std::vector<IPEndPoint>()) {}
16 
17 DnsConfig::DnsConfig(const DnsConfig& other) = default;
18 
19 DnsConfig::DnsConfig(DnsConfig&& other) = default;
20 
DnsConfig(std::vector<IPEndPoint> nameservers)21 DnsConfig::DnsConfig(std::vector<IPEndPoint> nameservers)
22     : nameservers(std::move(nameservers)),
23       dns_over_tls_active(false),
24       dns_over_tls_hostname(std::string()),
25       unhandled_options(false),
26       append_to_multi_label_name(true),
27       ndots(1),
28       fallback_period(kDnsDefaultFallbackPeriod),
29       attempts(2),
30       doh_attempts(1),
31       rotate(false),
32       use_local_ipv6(false),
33       secure_dns_mode(SecureDnsMode::kOff),
34       allow_dns_over_https_upgrade(false) {}
35 
36 DnsConfig::~DnsConfig() = default;
37 
38 DnsConfig& DnsConfig::operator=(const DnsConfig& other) = default;
39 
40 DnsConfig& DnsConfig::operator=(DnsConfig&& other) = default;
41 
Equals(const DnsConfig & d) const42 bool DnsConfig::Equals(const DnsConfig& d) const {
43   return EqualsIgnoreHosts(d) && (hosts == d.hosts);
44 }
45 
operator ==(const DnsConfig & d) const46 bool DnsConfig::operator==(const DnsConfig& d) const {
47   return Equals(d);
48 }
49 
operator !=(const DnsConfig & d) const50 bool DnsConfig::operator!=(const DnsConfig& d) const {
51   return !Equals(d);
52 }
53 
EqualsIgnoreHosts(const DnsConfig & d) const54 bool DnsConfig::EqualsIgnoreHosts(const DnsConfig& d) const {
55   return (nameservers == d.nameservers) &&
56          (dns_over_tls_active == d.dns_over_tls_active) &&
57          (dns_over_tls_hostname == d.dns_over_tls_hostname) &&
58          (search == d.search) && (unhandled_options == d.unhandled_options) &&
59          (append_to_multi_label_name == d.append_to_multi_label_name) &&
60          (ndots == d.ndots) && (fallback_period == d.fallback_period) &&
61          (attempts == d.attempts) && (doh_attempts == d.doh_attempts) &&
62          (rotate == d.rotate) && (use_local_ipv6 == d.use_local_ipv6) &&
63          (dns_over_https_servers == d.dns_over_https_servers) &&
64          (secure_dns_mode == d.secure_dns_mode) &&
65          (allow_dns_over_https_upgrade == d.allow_dns_over_https_upgrade) &&
66          (disabled_upgrade_providers == d.disabled_upgrade_providers);
67 }
68 
CopyIgnoreHosts(const DnsConfig & d)69 void DnsConfig::CopyIgnoreHosts(const DnsConfig& d) {
70   nameservers = d.nameservers;
71   dns_over_tls_active = d.dns_over_tls_active;
72   dns_over_tls_hostname = d.dns_over_tls_hostname;
73   search = d.search;
74   unhandled_options = d.unhandled_options;
75   append_to_multi_label_name = d.append_to_multi_label_name;
76   ndots = d.ndots;
77   fallback_period = d.fallback_period;
78   attempts = d.attempts;
79   doh_attempts = d.doh_attempts;
80   rotate = d.rotate;
81   use_local_ipv6 = d.use_local_ipv6;
82   dns_over_https_servers = d.dns_over_https_servers;
83   secure_dns_mode = d.secure_dns_mode;
84   allow_dns_over_https_upgrade = d.allow_dns_over_https_upgrade;
85   disabled_upgrade_providers = d.disabled_upgrade_providers;
86 }
87 
ToValue() const88 base::Value DnsConfig::ToValue() const {
89   base::Value dict(base::Value::Type::DICTIONARY);
90 
91   base::Value list(base::Value::Type::LIST);
92   for (const auto& nameserver : nameservers)
93     list.Append(nameserver.ToString());
94   dict.SetKey("nameservers", std::move(list));
95 
96   dict.SetBoolKey("dns_over_tls_active", dns_over_tls_active);
97   dict.SetStringKey("dns_over_tls_hostname", dns_over_tls_hostname);
98 
99   list = base::Value(base::Value::Type::LIST);
100   for (const auto& suffix : search)
101     list.Append(suffix);
102   dict.SetKey("search", std::move(list));
103   dict.SetBoolKey("unhandled_options", unhandled_options);
104   dict.SetBoolKey("append_to_multi_label_name", append_to_multi_label_name);
105   dict.SetIntKey("ndots", ndots);
106   dict.SetDoubleKey("timeout", fallback_period.InSecondsF());
107   dict.SetIntKey("attempts", attempts);
108   dict.SetIntKey("doh_attempts", doh_attempts);
109   dict.SetBoolKey("rotate", rotate);
110   dict.SetBoolKey("use_local_ipv6", use_local_ipv6);
111   dict.SetIntKey("num_hosts", hosts.size());
112   list = base::Value(base::Value::Type::LIST);
113   for (auto& server : dns_over_https_servers) {
114     base::Value val(base::Value::Type::DICTIONARY);
115     val.SetStringKey("server_template", server.server_template);
116     val.SetBoolKey("use_post", server.use_post);
117     list.Append(std::move(val));
118   }
119   dict.SetKey("doh_servers", std::move(list));
120   dict.SetIntKey("secure_dns_mode", static_cast<int>(secure_dns_mode));
121   dict.SetBoolKey("allow_dns_over_https_upgrade", allow_dns_over_https_upgrade);
122 
123   list = base::Value(base::Value::Type::LIST);
124   for (const auto& provider : disabled_upgrade_providers)
125     list.Append(provider);
126   dict.SetKey("disabled_upgrade_providers", std::move(list));
127 
128   return dict;
129 }
130 
131 }  // namespace net
132