dns_config.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #include "net/dns/dns_config.h"
  5. #include <utility>
  6. #include "base/numerics/safe_conversions.h"
  7. #include "base/values.h"
  8. #include "net/dns/public/dns_over_https_config.h"
  9. namespace net {
  10. // Default values are taken from glibc resolv.h except |fallback_period| which
  11. // is set to |kDnsDefaultFallbackPeriod|.
  12. DnsConfig::DnsConfig() : DnsConfig(std::vector<IPEndPoint>()) {}
  13. DnsConfig::DnsConfig(const DnsConfig& other) = default;
  14. DnsConfig::DnsConfig(DnsConfig&& other) = default;
  15. DnsConfig::DnsConfig(std::vector<IPEndPoint> nameservers)
  16. : nameservers(std::move(nameservers)) {}
  17. DnsConfig::~DnsConfig() = default;
  18. DnsConfig& DnsConfig::operator=(const DnsConfig& other) = default;
  19. DnsConfig& DnsConfig::operator=(DnsConfig&& other) = default;
  20. bool DnsConfig::Equals(const DnsConfig& d) const {
  21. return EqualsIgnoreHosts(d) && (hosts == d.hosts);
  22. }
  23. bool DnsConfig::operator==(const DnsConfig& d) const {
  24. return Equals(d);
  25. }
  26. bool DnsConfig::operator!=(const DnsConfig& d) const {
  27. return !Equals(d);
  28. }
  29. bool DnsConfig::EqualsIgnoreHosts(const DnsConfig& d) const {
  30. return (nameservers == d.nameservers) &&
  31. (dns_over_tls_active == d.dns_over_tls_active) &&
  32. (dns_over_tls_hostname == d.dns_over_tls_hostname) &&
  33. (search == d.search) && (unhandled_options == d.unhandled_options) &&
  34. (append_to_multi_label_name == d.append_to_multi_label_name) &&
  35. (ndots == d.ndots) && (fallback_period == d.fallback_period) &&
  36. (attempts == d.attempts) && (doh_attempts == d.doh_attempts) &&
  37. (rotate == d.rotate) && (use_local_ipv6 == d.use_local_ipv6) &&
  38. (doh_config == d.doh_config) &&
  39. (secure_dns_mode == d.secure_dns_mode) &&
  40. (allow_dns_over_https_upgrade == d.allow_dns_over_https_upgrade);
  41. }
  42. void DnsConfig::CopyIgnoreHosts(const DnsConfig& d) {
  43. nameservers = d.nameservers;
  44. dns_over_tls_active = d.dns_over_tls_active;
  45. dns_over_tls_hostname = d.dns_over_tls_hostname;
  46. search = d.search;
  47. unhandled_options = d.unhandled_options;
  48. append_to_multi_label_name = d.append_to_multi_label_name;
  49. ndots = d.ndots;
  50. fallback_period = d.fallback_period;
  51. attempts = d.attempts;
  52. doh_attempts = d.doh_attempts;
  53. rotate = d.rotate;
  54. use_local_ipv6 = d.use_local_ipv6;
  55. doh_config = d.doh_config;
  56. secure_dns_mode = d.secure_dns_mode;
  57. allow_dns_over_https_upgrade = d.allow_dns_over_https_upgrade;
  58. }
  59. base::Value DnsConfig::ToValue() const {
  60. base::Value::Dict dict;
  61. base::Value::List nameserver_list;
  62. for (const auto& nameserver : nameservers)
  63. nameserver_list.Append(nameserver.ToString());
  64. dict.Set("nameservers", std::move(nameserver_list));
  65. dict.Set("dns_over_tls_active", dns_over_tls_active);
  66. dict.Set("dns_over_tls_hostname", dns_over_tls_hostname);
  67. base::Value::List suffix_list;
  68. for (const auto& suffix : search)
  69. suffix_list.Append(suffix);
  70. dict.Set("search", std::move(suffix_list));
  71. dict.Set("unhandled_options", unhandled_options);
  72. dict.Set("append_to_multi_label_name", append_to_multi_label_name);
  73. dict.Set("ndots", ndots);
  74. dict.Set("timeout", fallback_period.InSecondsF());
  75. dict.Set("attempts", attempts);
  76. dict.Set("doh_attempts", doh_attempts);
  77. dict.Set("rotate", rotate);
  78. dict.Set("use_local_ipv6", use_local_ipv6);
  79. dict.Set("num_hosts", static_cast<int>(hosts.size()));
  80. dict.Set("doh_config", doh_config.ToValue());
  81. dict.Set("secure_dns_mode", base::strict_cast<int>(secure_dns_mode));
  82. dict.Set("allow_dns_over_https_upgrade", allow_dns_over_https_upgrade);
  83. return base::Value(std::move(dict));
  84. }
  85. } // namespace net