| OLD | NEW |
| (Empty) |
| 1 // Copyright 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 "content/public/browser/browser_thread.h" | |
| 9 #include "net/base/address_list.h" | |
| 10 #include "net/base/ip_endpoint.h" | |
| 11 #include "net/base/net_errors.h" | |
| 12 #include "net/base/net_log.h" | |
| 13 #include "net/base/net_util.h" | |
| 14 #include "net/base/network_change_notifier.h" | |
| 15 #include "net/dns/dns_client.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 content::BrowserThread; | |
| 22 using net::AddressList; | |
| 23 using net::BoundNetLog; | |
| 24 using net::DnsClient; | |
| 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 chrome_browser_net { | |
| 34 | |
| 35 const char* DnsProbeRunner::kKnownGoodHostname = "google.com"; | |
| 36 | |
| 37 namespace { | |
| 38 | |
| 39 DnsProbeRunner::Result EvaluateResponse( | |
| 40 int net_error, | |
| 41 const DnsResponse* response) { | |
| 42 switch (net_error) { | |
| 43 case net::OK: | |
| 44 break; | |
| 45 | |
| 46 // ERR_NAME_NOT_RESOLVED maps to NXDOMAIN, which means the server is working | |
| 47 // but gave us a wrong answer. | |
| 48 case net::ERR_NAME_NOT_RESOLVED: | |
| 49 return DnsProbeRunner::INCORRECT; | |
| 50 | |
| 51 // These results mean we heard *something* from the DNS server, but it was | |
| 52 // unsuccessful (SERVFAIL) or malformed. | |
| 53 case net::ERR_DNS_MALFORMED_RESPONSE: | |
| 54 case net::ERR_DNS_SERVER_REQUIRES_TCP: // Shouldn't happen; DnsTransaction | |
| 55 // will retry with TCP. | |
| 56 case net::ERR_DNS_SERVER_FAILED: | |
| 57 case net::ERR_DNS_SORT_ERROR: // Can only happen if the server responds. | |
| 58 return DnsProbeRunner::FAILING; | |
| 59 | |
| 60 // Any other error means we never reached the DNS server in the first place. | |
| 61 case net::ERR_DNS_TIMED_OUT: | |
| 62 default: | |
| 63 // Something else happened, probably at a network level. | |
| 64 return DnsProbeRunner::UNREACHABLE; | |
| 65 } | |
| 66 | |
| 67 AddressList addr_list; | |
| 68 TimeDelta ttl; | |
| 69 DnsResponse::Result result = response->ParseToAddressList(&addr_list, &ttl); | |
| 70 | |
| 71 if (result != DnsResponse::DNS_PARSE_OK) { | |
| 72 return DnsProbeRunner::FAILING; | |
| 73 } | |
| 74 | |
| 75 if (addr_list.empty()) { | |
| 76 return DnsProbeRunner::INCORRECT; | |
| 77 } | |
| 78 | |
| 79 return DnsProbeRunner::CORRECT; | |
| 80 } | |
| 81 | |
| 82 } // namespace | |
| 83 | |
| 84 DnsProbeRunner::DnsProbeRunner() : weak_factory_(this), result_(UNKNOWN) {} | |
| 85 | |
| 86 DnsProbeRunner::~DnsProbeRunner() {} | |
| 87 | |
| 88 void DnsProbeRunner::SetClient(scoped_ptr<net::DnsClient> client) { | |
| 89 client_ = client.Pass(); | |
| 90 } | |
| 91 | |
| 92 void DnsProbeRunner::RunProbe(const base::Closure& callback) { | |
| 93 DCHECK(!callback.is_null()); | |
| 94 DCHECK(client_.get()); | |
| 95 DCHECK(callback_.is_null()); | |
| 96 DCHECK(!transaction_.get()); | |
| 97 | |
| 98 callback_ = callback; | |
| 99 transaction_ = client_->GetTransactionFactory()->CreateTransaction( | |
| 100 kKnownGoodHostname, | |
| 101 net::dns_protocol::kTypeA, | |
| 102 base::Bind(&DnsProbeRunner::OnTransactionComplete, | |
| 103 weak_factory_.GetWeakPtr()), | |
| 104 BoundNetLog()); | |
| 105 | |
| 106 int rv = transaction_->Start(); | |
| 107 if (rv != net::ERR_IO_PENDING) | |
| 108 OnTransactionComplete(transaction_.get(), rv, NULL); | |
| 109 } | |
| 110 | |
| 111 bool DnsProbeRunner::IsRunning() const { | |
| 112 return !callback_.is_null(); | |
| 113 } | |
| 114 | |
| 115 void DnsProbeRunner::OnTransactionComplete( | |
| 116 DnsTransaction* transaction, | |
| 117 int net_error, | |
| 118 const DnsResponse* response) { | |
| 119 DCHECK(!callback_.is_null()); | |
| 120 DCHECK(transaction_.get()); | |
| 121 DCHECK_EQ(transaction_.get(), transaction); | |
| 122 | |
| 123 result_ = EvaluateResponse(net_error, response); | |
| 124 transaction_.reset(); | |
| 125 | |
| 126 BrowserThread::PostTask( | |
| 127 BrowserThread::IO, | |
| 128 FROM_HERE, | |
| 129 base::Bind(&DnsProbeRunner::CallCallback, | |
| 130 weak_factory_.GetWeakPtr())); | |
| 131 } | |
| 132 | |
| 133 void DnsProbeRunner::CallCallback() { | |
| 134 DCHECK(!callback_.is_null()); | |
| 135 DCHECK(!transaction_.get()); | |
| 136 | |
| 137 // Clear callback in case it starts a new probe immediately. | |
| 138 const base::Closure callback = callback_; | |
| 139 callback_.Reset(); | |
| 140 callback.Run(); | |
| 141 } | |
| 142 | |
| 143 } // namespace chrome_browser_net | |
| OLD | NEW |