1 // Copyright 2018 The Chromium Authors
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/numerics/safe_conversions.h"
10 #include "base/values.h"
11 #include "net/dns/public/dns_over_https_config.h"
12
13 namespace net {
14
15 // Default values are taken from glibc resolv.h except |fallback_period| which
16 // is set to |kDnsDefaultFallbackPeriod|.
DnsConfig()17 DnsConfig::DnsConfig() : DnsConfig(std::vector<IPEndPoint>()) {}
18
19 DnsConfig::DnsConfig(const DnsConfig& other) = default;
20
21 DnsConfig::DnsConfig(DnsConfig&& other) = default;
22
DnsConfig(std::vector<IPEndPoint> nameservers)23 DnsConfig::DnsConfig(std::vector<IPEndPoint> nameservers)
24 : nameservers(std::move(nameservers)) {}
25
26 DnsConfig::~DnsConfig() = default;
27
28 DnsConfig& DnsConfig::operator=(const DnsConfig& other) = default;
29
30 DnsConfig& DnsConfig::operator=(DnsConfig&& other) = default;
31
Equals(const DnsConfig & d) const32 bool DnsConfig::Equals(const DnsConfig& d) const {
33 return EqualsIgnoreHosts(d) && (hosts == d.hosts);
34 }
35
operator ==(const DnsConfig & d) const36 bool DnsConfig::operator==(const DnsConfig& d) const {
37 return Equals(d);
38 }
39
operator !=(const DnsConfig & d) const40 bool DnsConfig::operator!=(const DnsConfig& d) const {
41 return !Equals(d);
42 }
43
EqualsIgnoreHosts(const DnsConfig & d) const44 bool DnsConfig::EqualsIgnoreHosts(const DnsConfig& d) const {
45 return (nameservers == d.nameservers) &&
46 (dns_over_tls_active == d.dns_over_tls_active) &&
47 (dns_over_tls_hostname == d.dns_over_tls_hostname) &&
48 (search == d.search) && (unhandled_options == d.unhandled_options) &&
49 (append_to_multi_label_name == d.append_to_multi_label_name) &&
50 (ndots == d.ndots) && (fallback_period == d.fallback_period) &&
51 (attempts == d.attempts) && (doh_attempts == d.doh_attempts) &&
52 (rotate == d.rotate) && (use_local_ipv6 == d.use_local_ipv6) &&
53 (doh_config == d.doh_config) &&
54 (secure_dns_mode == d.secure_dns_mode) &&
55 (allow_dns_over_https_upgrade == d.allow_dns_over_https_upgrade);
56 }
57
CopyIgnoreHosts(const DnsConfig & d)58 void DnsConfig::CopyIgnoreHosts(const DnsConfig& d) {
59 nameservers = d.nameservers;
60 dns_over_tls_active = d.dns_over_tls_active;
61 dns_over_tls_hostname = d.dns_over_tls_hostname;
62 search = d.search;
63 unhandled_options = d.unhandled_options;
64 append_to_multi_label_name = d.append_to_multi_label_name;
65 ndots = d.ndots;
66 fallback_period = d.fallback_period;
67 attempts = d.attempts;
68 doh_attempts = d.doh_attempts;
69 rotate = d.rotate;
70 use_local_ipv6 = d.use_local_ipv6;
71 doh_config = d.doh_config;
72 secure_dns_mode = d.secure_dns_mode;
73 allow_dns_over_https_upgrade = d.allow_dns_over_https_upgrade;
74 }
75
ToDict() const76 base::Value::Dict DnsConfig::ToDict() const {
77 base::Value::Dict dict;
78
79 base::Value::List nameserver_list;
80 for (const auto& nameserver : nameservers)
81 nameserver_list.Append(nameserver.ToString());
82 dict.Set("nameservers", std::move(nameserver_list));
83
84 dict.Set("dns_over_tls_active", dns_over_tls_active);
85 dict.Set("dns_over_tls_hostname", dns_over_tls_hostname);
86
87 base::Value::List suffix_list;
88 for (const auto& suffix : search)
89 suffix_list.Append(suffix);
90 dict.Set("search", std::move(suffix_list));
91 dict.Set("unhandled_options", unhandled_options);
92 dict.Set("append_to_multi_label_name", append_to_multi_label_name);
93 dict.Set("ndots", ndots);
94 dict.Set("timeout", fallback_period.InSecondsF());
95 dict.Set("attempts", attempts);
96 dict.Set("doh_attempts", doh_attempts);
97 dict.Set("rotate", rotate);
98 dict.Set("use_local_ipv6", use_local_ipv6);
99 dict.Set("num_hosts", static_cast<int>(hosts.size()));
100 dict.Set("doh_config", doh_config.ToValue());
101 dict.Set("secure_dns_mode", base::strict_cast<int>(secure_dns_mode));
102 dict.Set("allow_dns_over_https_upgrade", allow_dns_over_https_upgrade);
103
104 return dict;
105 }
106
107 } // namespace net
108