summaryrefslogtreecommitdiffstats
path: root/chrome/browser/net/dns_probe_service.cc
blob: 3a14bbc8a8cdf56d5fc73019548e6e1bb3a6938d (plain)
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
208
209
210
211
// Copyright (c) 2012 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 "chrome/browser/net/dns_probe_service.h"

#include "base/metrics/histogram.h"
#include "chrome/browser/net/dns_probe_job.h"
#include "net/base/ip_endpoint.h"
#include "net/base/net_util.h"
#include "net/base/network_change_notifier.h"
#include "net/dns/dns_client.h"
#include "net/dns/dns_config_service.h"
#include "net/dns/dns_protocol.h"

using net::DnsClient;
using net::DnsConfig;
using net::IPAddressNumber;
using net::IPEndPoint;
using net::ParseIPLiteralToNumber;
using net::NetworkChangeNotifier;

namespace chrome_browser_net {

namespace {

const char kPublicDnsPrimary[]   = "8.8.8.8";
const char kPublicDnsSecondary[] = "8.8.4.4";

IPEndPoint MakeDnsEndPoint(const std::string& dns_ip_literal) {
  IPAddressNumber dns_ip_number;
  bool rv = ParseIPLiteralToNumber(dns_ip_literal, &dns_ip_number);
  DCHECK(rv);
  return IPEndPoint(dns_ip_number, net::dns_protocol::kDefaultPort);
}

const int kMaxResultAgeMs = 5000;

}  // namespace

DnsProbeService::DnsProbeService()
    : system_result_(DnsProbeJob::SERVERS_UNKNOWN),
      public_result_(DnsProbeJob::SERVERS_UNKNOWN),
      state_(STATE_NO_RESULTS),
      result_(PROBE_UNKNOWN) {
}

DnsProbeService::~DnsProbeService() {
}

void DnsProbeService::ProbeDns(const DnsProbeService::CallbackType& callback) {
  callbacks_.push_back(callback);

  if (state_ == STATE_RESULTS_CACHED && ResultsExpired())
    ExpireResults();

  switch (state_) {
  case STATE_NO_RESULTS:
    StartProbes();
    break;
  case STATE_RESULTS_CACHED:
    CallCallbacks();
    break;
  case STATE_PROBE_RUNNING:
    // do nothing; probe is already running, and will call the callback
    break;
  }
}

scoped_ptr<DnsProbeJob> DnsProbeService::CreateSystemProbeJob(
    const DnsProbeJob::CallbackType& job_callback) {
  DnsConfig system_config;
  GetSystemDnsConfig(&system_config);
  return CreateProbeJob(system_config, job_callback);
}

scoped_ptr<DnsProbeJob> DnsProbeService::CreatePublicProbeJob(
    const DnsProbeJob::CallbackType& job_callback) {
  DnsConfig public_config;
  GetPublicDnsConfig(&public_config);
  return CreateProbeJob(public_config, job_callback);
}

void DnsProbeService::ExpireResults() {
  DCHECK_EQ(STATE_RESULTS_CACHED, state_);

  state_ = STATE_NO_RESULTS;
  result_ = PROBE_UNKNOWN;
}

void DnsProbeService::StartProbes() {
  DCHECK_NE(STATE_PROBE_RUNNING, state_);
  DCHECK(!system_job_.get());
  DCHECK(!public_job_.get());

  DnsProbeJob::CallbackType job_callback =
      base::Bind(&DnsProbeService::OnProbeJobComplete,
                 base::Unretained(this));

  // TODO(ttuttle): Do we want to keep explicit flags for "job done"?
  //                Or maybe DnsProbeJob should have a "finished" flag?
  system_result_ = DnsProbeJob::SERVERS_UNKNOWN;
  public_result_ = DnsProbeJob::SERVERS_UNKNOWN;

  system_job_ = CreateSystemProbeJob(job_callback);
  public_job_ = CreatePublicProbeJob(job_callback);

  state_ = STATE_PROBE_RUNNING;
  probe_start_time_ = base::Time::Now();
}

void DnsProbeService::OnProbesComplete() {
  DCHECK_EQ(STATE_PROBE_RUNNING, state_);

  base::TimeDelta probe_elapsed = base::Time::Now() - probe_start_time_;
  UMA_HISTOGRAM_ENUMERATION("DnsProbe.Probe.Result", result_, MAX_RESULT);
  UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Probe.Elapsed", probe_elapsed);

  state_ = STATE_RESULTS_CACHED;
  result_ = EvaluateResults();

  CallCallbacks();
}

DnsProbeService::Result DnsProbeService::EvaluateResults() {
  DCHECK_NE(DnsProbeJob::SERVERS_UNKNOWN, system_result_);
  DCHECK_NE(DnsProbeJob::SERVERS_UNKNOWN, public_result_);

  // If the system DNS is working, assume the domain doesn't exist.
  if (system_result_ == DnsProbeJob::SERVERS_CORRECT)
    return PROBE_NXDOMAIN;

  // If the system DNS is not working but another public server is, assume the
  // DNS config is bad (or perhaps the DNS servers are down or broken).
  if (public_result_ == DnsProbeJob::SERVERS_CORRECT)
    return PROBE_BAD_CONFIG;

  // If the system DNS is not working and another public server is unreachable,
  // assume the internet connection is down (note that system DNS may be a
  // router on the LAN, so it may be reachable but returning errors.)
  if (public_result_ == DnsProbeJob::SERVERS_UNREACHABLE)
    return PROBE_NO_INTERNET;

  // Otherwise: the system DNS is not working and another public server is
  // responding but with errors or incorrect results.  This is an awkward case;
  // an invasive captive portal or a restrictive firewall may be intercepting
  // or rewriting DNS traffic, or the public server may itself be failing or
  // down.
  return PROBE_UNKNOWN;
}

void DnsProbeService::CallCallbacks() {
  DCHECK_EQ(STATE_RESULTS_CACHED, state_);
  DCHECK(!callbacks_.empty());

  std::vector<CallbackType> callbacks = callbacks_;
  callbacks_.clear();

  for (std::vector<CallbackType>::const_iterator i = callbacks.begin();
       i != callbacks.end(); ++i) {
    i->Run(result_);
  }
}

scoped_ptr<DnsProbeJob> DnsProbeService::CreateProbeJob(
    const DnsConfig& dns_config,
    const DnsProbeJob::CallbackType& job_callback) {
  scoped_ptr<DnsClient> dns_client(DnsClient::CreateClient(NULL));
  dns_client->SetConfig(dns_config);
  return DnsProbeJob::CreateJob(dns_client.Pass(), job_callback, NULL);
}

void DnsProbeService::OnProbeJobComplete(DnsProbeJob* job,
                                         DnsProbeJob::Result result) {
  DCHECK_EQ(STATE_PROBE_RUNNING, state_);

  if (job == system_job_.get()) {
    system_result_ = result;
    system_job_.reset();
  } else if (job == public_job_.get()) {
    public_result_ = result;
    public_job_.reset();
  } else {
    NOTREACHED();
    return;
  }

  if (system_result_ != DnsProbeJob::SERVERS_UNKNOWN &&
      public_result_ != DnsProbeJob::SERVERS_UNKNOWN) {
    OnProbesComplete();
  }
}

void DnsProbeService::GetSystemDnsConfig(DnsConfig* config) {
  // TODO(ttuttle): Make sure we handle missing config properly
  NetworkChangeNotifier::GetDnsConfig(config);
}

void DnsProbeService::GetPublicDnsConfig(DnsConfig* config) {
  *config = DnsConfig();
  config->nameservers.push_back(MakeDnsEndPoint(kPublicDnsPrimary));
  config->nameservers.push_back(MakeDnsEndPoint(kPublicDnsSecondary));
}

bool DnsProbeService::ResultsExpired() {
  const base::TimeDelta kMaxResultAge =
      base::TimeDelta::FromMilliseconds(kMaxResultAgeMs);
  return base::Time::Now() - probe_start_time_ > kMaxResultAge;
}

} // namespace chrome_browser_net