Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 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_runner.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
|
mmenke
2013/06/11 16:15:35
This is included in header (As is bind, though I g
Deprecated (see juliatuttle)
2013/06/13 14:37:04
Done.
| |
| 9 #include "net/base/ip_endpoint.h" | |
| 10 #include "net/base/net_errors.h" | |
| 11 #include "net/base/net_log.h" | |
| 12 #include "net/base/net_util.h" | |
| 13 #include "net/base/network_change_notifier.h" | |
| 14 #include "net/dns/dns_client.h" | |
| 15 #include "net/dns/dns_config_service.h" | |
| 16 #include "net/dns/dns_protocol.h" | |
| 17 #include "net/dns/dns_response.h" | |
| 18 #include "net/dns/dns_transaction.h" | |
| 19 | |
| 20 using base::TimeDelta; | |
| 21 using net::AddressList; | |
| 22 using net::BoundNetLog; | |
| 23 using net::DnsClient; | |
| 24 using net::DnsConfig; | |
| 25 using net::DnsResponse; | |
| 26 using net::DnsTransaction; | |
| 27 using net::IPAddressNumber; | |
| 28 using net::IPEndPoint; | |
| 29 using net::NetLog; | |
| 30 using net::NetworkChangeNotifier; | |
| 31 using net::ParseIPLiteralToNumber; | |
| 32 | |
| 33 namespace { | |
| 34 | |
| 35 // The public DNS servers used by the DnsProbeService to verify internet | |
| 36 // connectivity. | |
| 37 const char kPublicDnsPrimary[] = "8.8.8.8"; | |
| 38 const char kPublicDnsSecondary[] = "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 // Returns true if the given net_error indicates that we received a response | |
|
mmenke
2013/06/11 16:15:35
nit: |net_error| (References to arguments should
Deprecated (see juliatuttle)
2013/06/13 14:37:04
Done.
| |
| 48 // from the DNS server containing an error, or false if the given net_error | |
| 49 // indicates that we never received a response. | |
| 50 bool DidReceiveDnsResponse(int net_error) { | |
| 51 switch (net_error) { | |
| 52 case net::ERR_NAME_NOT_RESOLVED: // NXDOMAIN maps to this. | |
| 53 case net::ERR_DNS_MALFORMED_RESPONSE: | |
| 54 case net::ERR_DNS_SERVER_REQUIRES_TCP: | |
|
mmenke
2013/06/11 16:15:35
Think it's worth noting that this shouldn't happen
Deprecated (see juliatuttle)
2013/06/13 14:37:04
Done.
| |
| 55 case net::ERR_DNS_SERVER_FAILED: | |
| 56 case net::ERR_DNS_SORT_ERROR: // Can only happen if the server responds. | |
| 57 return true; | |
|
mmenke
2013/06/11 16:15:35
completely optional nit: Just to make the behavio
Deprecated (see juliatuttle)
2013/06/13 14:37:04
Done.
| |
| 58 default: | |
| 59 return false; | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 } // namespace | |
| 64 | |
| 65 namespace chrome_browser_net { | |
| 66 | |
| 67 DnsProbeRunner::DnsProbeRunner() | |
| 68 : weak_factory_(this), | |
| 69 bound_net_log_(BoundNetLog::Make(NULL, NetLog::SOURCE_DNS_PROBER)) { | |
|
mmenke
2013/06/11 16:15:35
Per other comment, not needed.
Deprecated (see juliatuttle)
2013/06/13 14:37:04
Done.
| |
| 70 } | |
| 71 | |
| 72 DnsProbeRunner::~DnsProbeRunner() {} | |
| 73 | |
| 74 void DnsProbeRunner::RunProbe(const ProbeCallback& callback) { | |
| 75 DCHECK(client_.get()); | |
| 76 DCHECK(!is_running()); | |
| 77 DCHECK(!callback.is_null()); | |
| 78 | |
| 79 callback_ = callback; | |
| 80 transaction_ = client_->GetTransactionFactory()->CreateTransaction( | |
| 81 "google.com", | |
|
mmenke
2013/06/11 16:15:35
Suggest putting this up top, with the public DNS I
Deprecated (see juliatuttle)
2013/06/13 14:37:04
Done.
| |
| 82 net::dns_protocol::kTypeA, | |
| 83 base::Bind(&DnsProbeRunner::OnTransactionComplete, | |
| 84 weak_factory_.GetWeakPtr(), | |
| 85 true /* async */), | |
| 86 bound_net_log_); | |
|
mmenke
2013/06/11 16:15:35
BoundNetLog()
Deprecated (see juliatuttle)
2013/06/13 14:37:04
Done.
| |
| 87 | |
| 88 int rv = transaction_->Start(); | |
| 89 if (rv != net::ERR_IO_PENDING) { | |
| 90 OnTransactionComplete(false /* not async */, | |
| 91 transaction_.get(), | |
| 92 rv, | |
| 93 NULL); | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 void DnsProbeRunner::set_client(scoped_ptr<net::DnsClient> client) { | |
| 98 client_ = client.Pass(); | |
| 99 } | |
| 100 | |
| 101 bool DnsProbeRunner::is_running() const { | |
| 102 return transaction_.get(); | |
| 103 } | |
| 104 | |
| 105 void DnsProbeRunner::OnTransactionComplete( | |
| 106 bool async, | |
| 107 DnsTransaction* transaction, | |
| 108 int net_error, | |
| 109 const DnsResponse* response) { | |
| 110 DCHECK(is_running()); | |
| 111 DCHECK_EQ(transaction_.get(), transaction); | |
| 112 | |
| 113 const Result result = EvaluateResponse(async, net_error, response); | |
| 114 transaction_.reset(); | |
| 115 callback_.Run(result); | |
| 116 callback_ = ProbeCallback(); | |
|
mmenke
2013/06/11 16:15:35
To avoid the possibility of any re-entrancy issues
Deprecated (see juliatuttle)
2013/06/13 14:37:04
Done.
| |
| 117 } | |
| 118 | |
| 119 // static | |
| 120 DnsProbeRunner::Result DnsProbeRunner::EvaluateResponse( | |
| 121 bool async, | |
| 122 int net_error, | |
| 123 const DnsResponse* response) { | |
| 124 if (!async) { | |
|
mmenke
2013/06/11 16:15:35
Do we need this case / parameter? Seems like we'd
Deprecated (see juliatuttle)
2013/06/13 14:37:04
I like having it explicit, but I'll drop it if you
mmenke
2013/06/13 15:00:12
I believe it's technically possible to get a sync
szym
2013/06/13 15:10:18
Actually, DnsTransaction can never complete synchr
mmenke
2013/06/13 15:42:12
Thanks! Hmm...That was added to fix a crasher whe
szym
2013/06/13 15:44:44
I'd like to say that it was by design, but yes, it
| |
| 125 return UNREACHABLE; | |
| 126 } | |
| 127 | |
| 128 if (net_error != net::OK) { | |
| 129 if (DidReceiveDnsResponse(net_error)) { | |
| 130 return FAILING; | |
| 131 } else { | |
| 132 return UNREACHABLE; | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 AddressList addr_list; | |
| 137 TimeDelta ttl; | |
| 138 DnsResponse::Result result = response->ParseToAddressList(&addr_list, &ttl); | |
| 139 | |
| 140 if (result != DnsResponse::DNS_PARSE_OK) { | |
| 141 return FAILING; | |
| 142 } | |
| 143 | |
| 144 if (addr_list.empty()) { | |
| 145 return INCORRECT; | |
| 146 } | |
| 147 | |
| 148 return CORRECT; | |
| 149 } | |
| 150 | |
| 151 } // namespace chrome_browser_net | |
| OLD | NEW |