Home | History | Annotate | Download | only in net
      1 // Copyright (c) 2012 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 "chrome/browser/net/dns_probe_service.h"
      6 
      7 #include "base/metrics/field_trial.h"
      8 #include "base/metrics/histogram.h"
      9 #include "base/strings/string_number_conversions.h"
     10 #include "net/base/ip_endpoint.h"
     11 #include "net/base/net_util.h"
     12 #include "net/dns/dns_client.h"
     13 #include "net/dns/dns_config_service.h"
     14 #include "net/dns/dns_protocol.h"
     15 
     16 using base::FieldTrialList;
     17 using base::StringToInt;
     18 using chrome_common_net::DnsProbeStatus;
     19 using net::DnsClient;
     20 using net::DnsConfig;
     21 using net::IPAddressNumber;
     22 using net::IPEndPoint;
     23 using net::ParseIPLiteralToNumber;
     24 using net::NetworkChangeNotifier;
     25 
     26 namespace chrome_browser_net {
     27 
     28 namespace {
     29 
     30 // How long the DnsProbeService will cache the probe result for.
     31 // If it's older than this and we get a probe request, the service expires it
     32 // and starts a new probe.
     33 const int kMaxResultAgeMs = 5000;
     34 
     35 // The public DNS servers used by the DnsProbeService to verify internet
     36 // connectivity.
     37 const char kGooglePublicDns1[] = "8.8.8.8";
     38 const char kGooglePublicDns2[] = "8.8.4.4";
     39 
     40 IPEndPoint MakeDnsEndPoint(const std::string& dns_ip_literal) {
     41   IPAddressNumber dns_ip_number;
     42   bool rv = ParseIPLiteralToNumber(dns_ip_literal, &dns_ip_number);
     43   DCHECK(rv);
     44   return IPEndPoint(dns_ip_number, net::dns_protocol::kDefaultPort);
     45 }
     46 
     47 DnsProbeStatus EvaluateResults(DnsProbeRunner::Result system_result,
     48                                DnsProbeRunner::Result public_result) {
     49   // If the system DNS is working, assume the domain doesn't exist.
     50   if (system_result == DnsProbeRunner::CORRECT)
     51     return chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN;
     52 
     53   // If the system DNS is not working but another public server is, assume the
     54   // DNS config is bad (or perhaps the DNS servers are down or broken).
     55   if (public_result == DnsProbeRunner::CORRECT)
     56     return chrome_common_net::DNS_PROBE_FINISHED_BAD_CONFIG;
     57 
     58   // If the system DNS is not working and another public server is unreachable,
     59   // assume the internet connection is down (note that system DNS may be a
     60   // router on the LAN, so it may be reachable but returning errors.)
     61   if (public_result == DnsProbeRunner::UNREACHABLE)
     62     return chrome_common_net::DNS_PROBE_FINISHED_NO_INTERNET;
     63 
     64   // Otherwise: the system DNS is not working and another public server is
     65   // responding but with errors or incorrect results.  This is an awkward case;
     66   // an invasive captive portal or a restrictive firewall may be intercepting
     67   // or rewriting DNS traffic, or the public server may itself be failing or
     68   // down.
     69   return chrome_common_net::DNS_PROBE_FINISHED_INCONCLUSIVE;
     70 }
     71 
     72 void HistogramProbe(DnsProbeStatus status, base::TimeDelta elapsed) {
     73   DCHECK(chrome_common_net::DnsProbeStatusIsFinished(status));
     74 
     75   UMA_HISTOGRAM_ENUMERATION("DnsProbe.ProbeResult", status,
     76                             chrome_common_net::DNS_PROBE_MAX);
     77   UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.ProbeDuration", elapsed);
     78 }
     79 
     80 }  // namespace
     81 
     82 DnsProbeService::DnsProbeService()
     83     : state_(STATE_NO_RESULT) {
     84   NetworkChangeNotifier::AddDNSObserver(this);
     85   SetSystemClientToCurrentConfig();
     86   SetPublicClientToGooglePublicDns();
     87 }
     88 
     89 DnsProbeService::~DnsProbeService() {
     90   NetworkChangeNotifier::RemoveDNSObserver(this);
     91 }
     92 
     93 void DnsProbeService::ProbeDns(const DnsProbeService::ProbeCallback& callback) {
     94   pending_callbacks_.push_back(callback);
     95 
     96   if (CachedResultIsExpired())
     97     ClearCachedResult();
     98 
     99   switch (state_) {
    100     case STATE_NO_RESULT:
    101       StartProbes();
    102       break;
    103     case STATE_RESULT_CACHED:
    104       CallCallbacks();
    105       break;
    106     case STATE_PROBE_RUNNING:
    107       // Do nothing; probe is already running, and will call the callback.
    108       break;
    109   }
    110 }
    111 
    112 void DnsProbeService::OnDNSChanged() {
    113   ClearCachedResult();
    114   SetSystemClientToCurrentConfig();
    115 }
    116 
    117 void DnsProbeService::SetSystemClientForTesting(
    118     scoped_ptr<DnsClient> system_client) {
    119   system_runner_.SetClient(system_client.Pass());
    120 }
    121 
    122 void DnsProbeService::SetPublicClientForTesting(
    123     scoped_ptr<DnsClient> public_client) {
    124   public_runner_.SetClient(public_client.Pass());
    125 }
    126 
    127 void DnsProbeService::ClearCachedResultForTesting() {
    128   ClearCachedResult();
    129 }
    130 
    131 void DnsProbeService::SetSystemClientToCurrentConfig() {
    132   DnsConfig system_config;
    133   NetworkChangeNotifier::GetDnsConfig(&system_config);
    134   system_config.search.clear();
    135   system_config.attempts = 1;
    136   system_config.randomize_ports = false;
    137 
    138   scoped_ptr<DnsClient> system_client(DnsClient::CreateClient(NULL));
    139   system_client->SetConfig(system_config);
    140 
    141   system_runner_.SetClient(system_client.Pass());
    142 }
    143 
    144 void DnsProbeService::SetPublicClientToGooglePublicDns() {
    145   DnsConfig public_config;
    146   public_config.nameservers.push_back(MakeDnsEndPoint(kGooglePublicDns1));
    147   public_config.nameservers.push_back(MakeDnsEndPoint(kGooglePublicDns2));
    148   public_config.attempts = 1;
    149   public_config.randomize_ports = false;
    150 
    151   scoped_ptr<DnsClient> public_client(DnsClient::CreateClient(NULL));
    152   public_client->SetConfig(public_config);
    153 
    154   public_runner_.SetClient(public_client.Pass());
    155 }
    156 
    157 void DnsProbeService::StartProbes() {
    158   DCHECK_EQ(STATE_NO_RESULT, state_);
    159 
    160   DCHECK(!system_runner_.IsRunning());
    161   DCHECK(!public_runner_.IsRunning());
    162 
    163   const base::Closure callback = base::Bind(&DnsProbeService::OnProbeComplete,
    164                                             base::Unretained(this));
    165   system_runner_.RunProbe(callback);
    166   public_runner_.RunProbe(callback);
    167   probe_start_time_ = base::Time::Now();
    168   state_ = STATE_PROBE_RUNNING;
    169 
    170   DCHECK(system_runner_.IsRunning());
    171   DCHECK(public_runner_.IsRunning());
    172 }
    173 
    174 void DnsProbeService::OnProbeComplete() {
    175   DCHECK_EQ(STATE_PROBE_RUNNING, state_);
    176 
    177   if (system_runner_.IsRunning() || public_runner_.IsRunning())
    178     return;
    179 
    180   cached_result_ = EvaluateResults(system_runner_.result(),
    181                                    public_runner_.result());
    182   state_ = STATE_RESULT_CACHED;
    183 
    184   HistogramProbe(cached_result_, base::Time::Now() - probe_start_time_);
    185 
    186   CallCallbacks();
    187 }
    188 
    189 void DnsProbeService::CallCallbacks() {
    190   DCHECK_EQ(STATE_RESULT_CACHED, state_);
    191   DCHECK(chrome_common_net::DnsProbeStatusIsFinished(cached_result_));
    192   DCHECK(!pending_callbacks_.empty());
    193 
    194   std::vector<ProbeCallback> callbacks;
    195   callbacks.swap(pending_callbacks_);
    196 
    197   for (std::vector<ProbeCallback>::const_iterator i = callbacks.begin();
    198        i != callbacks.end(); ++i) {
    199     i->Run(cached_result_);
    200   }
    201 }
    202 
    203 void DnsProbeService::ClearCachedResult() {
    204   if (state_ == STATE_RESULT_CACHED) {
    205     state_ = STATE_NO_RESULT;
    206     cached_result_ = chrome_common_net::DNS_PROBE_MAX;
    207   }
    208 }
    209 
    210 bool DnsProbeService::CachedResultIsExpired() const {
    211   if (state_ != STATE_RESULT_CACHED)
    212     return false;
    213 
    214   const base::TimeDelta kMaxResultAge =
    215       base::TimeDelta::FromMilliseconds(kMaxResultAgeMs);
    216   return base::Time::Now() - probe_start_time_ > kMaxResultAge;
    217 }
    218 
    219 }  // namespace chrome_browser_net
    220