Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/dns/dns_session.h" | 5 #include "net/dns/dns_session.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/rand_util.h" | |
| 10 #include "base/stl_util.h" | |
| 9 #include "base/time.h" | 11 #include "base/time.h" |
| 10 #include "net/base/ip_endpoint.h" | 12 #include "net/base/ip_endpoint.h" |
| 13 #include "net/base/net_errors.h" | |
| 11 #include "net/dns/dns_config_service.h" | 14 #include "net/dns/dns_config_service.h" |
| 12 #include "net/socket/client_socket_factory.h" | 15 #include "net/socket/client_socket_factory.h" |
| 13 | 16 |
| 14 namespace net { | 17 namespace net { |
| 15 | 18 |
| 19 DnsSession::SocketLease::SocketLease(scoped_refptr<DnsSession> session, | |
| 20 int server_index, | |
| 21 scoped_ptr<DatagramClientSocket> socket) | |
| 22 : session_(session), server_index_(server_index), socket_(socket.Pass()) {} | |
| 23 | |
| 24 DnsSession::SocketLease::~SocketLease() { | |
| 25 session_->FreeSocket(server_index_, socket_.Pass()); | |
| 26 } | |
| 27 | |
| 16 DnsSession::DnsSession(const DnsConfig& config, | 28 DnsSession::DnsSession(const DnsConfig& config, |
| 17 ClientSocketFactory* factory, | 29 ClientSocketFactory* factory, |
| 18 const RandIntCallback& rand_int_callback, | 30 const RandIntCallback& rand_int_callback, |
| 31 bool bypass_pools, | |
| 19 NetLog* net_log) | 32 NetLog* net_log) |
| 20 : config_(config), | 33 : config_(config), |
| 21 socket_factory_(factory), | 34 socket_factory_(factory), |
| 22 rand_callback_(base::Bind(rand_int_callback, 0, kuint16max)), | 35 rand_callback_(base::Bind(rand_int_callback, 0, kuint16max)), |
| 36 bypass_pools_(bypass_pools), | |
| 23 net_log_(net_log), | 37 net_log_(net_log), |
| 38 pools_(config.nameservers.size()), | |
| 24 server_index_(0) { | 39 server_index_(0) { |
| 25 } | 40 } |
| 26 | 41 |
| 27 int DnsSession::NextQueryId() const { | 42 int DnsSession::NextQueryId() const { |
| 28 return rand_callback_.Run(); | 43 return rand_callback_.Run(); |
| 29 } | 44 } |
| 30 | 45 |
| 31 int DnsSession::NextFirstServerIndex() { | 46 int DnsSession::NextFirstServerIndex() { |
| 32 int index = server_index_; | 47 int index = server_index_; |
| 33 if (config_.rotate) | 48 if (config_.rotate) |
| 34 server_index_ = (server_index_ + 1) % config_.nameservers.size(); | 49 server_index_ = (server_index_ + 1) % config_.nameservers.size(); |
| 35 return index; | 50 return index; |
| 36 } | 51 } |
| 37 | 52 |
| 38 base::TimeDelta DnsSession::NextTimeout(int attempt) { | 53 base::TimeDelta DnsSession::NextTimeout(int attempt) { |
| 39 // The timeout doubles every full round (each nameserver once). | 54 // The timeout doubles every full round (each nameserver once). |
| 40 // TODO(szym): Adapt timeout to observed RTT. http://crbug.com/110197 | 55 // TODO(szym): Adapt timeout to observed RTT. http://crbug.com/110197 |
| 41 return config_.timeout * (1 << (attempt / config_.nameservers.size())); | 56 return config_.timeout * (1 << (attempt / config_.nameservers.size())); |
| 42 } | 57 } |
| 43 | 58 |
| 44 DnsSession::~DnsSession() {} | 59 DnsSession::~DnsSession() { |
| 60 for (size_t i = 0; i < pools_.size(); ++i) | |
| 61 STLDeleteElements(&pools_[i]); | |
| 62 } | |
| 63 | |
| 64 // TODO(ttuttle): Pool and reuse sockets. | |
| 65 | |
| 66 #if defined(OS_WIN) | |
| 67 // Don't request a specific port, since that will trigger firewall prompts. | |
| 68 static const DatagramSocket::BindType kBindType = DatagramSocket::DEFAULT_BIND; | |
| 69 // Since we can't request random ports, keep a pool of the arbitrary ones | |
| 70 // we've been given, to add entropy. | |
| 71 static const size_t kMinPoolSize = 512; | |
| 72 static const size_t kMaxPoolSize = 1024; | |
| 73 #else | |
| 74 static const DatagramSocket::BindType kBindType = DatagramSocket::RANDOM_BIND; | |
| 75 static const size_t kMinPoolSize = 0; | |
| 76 static const size_t kMaxPoolSize = 0; | |
| 77 #endif | |
| 78 | |
| 79 // Allocate a socket, already connected to the server address. | |
| 80 scoped_ptr<DnsSession::SocketLease> DnsSession::AllocateSocket(int index) { | |
| 81 scoped_ptr<DatagramClientSocket> socket; | |
| 82 | |
| 83 std::vector<DatagramClientSocket*>& pool = pools_[index]; | |
| 84 | |
| 85 if (!bypass_pools_ && (pool.size() > kMinPoolSize)) { | |
| 86 int pool_index = 0; // TODO(ttuttle): Random. | |
| 87 socket.reset(pool[pool_index]); | |
| 88 pool[pool_index] = pool.back(); | |
| 89 pool.pop_back(); | |
| 90 } | |
| 91 | |
| 92 if (!socket.get()) { | |
| 93 socket.reset( | |
| 94 socket_factory_->CreateDatagramClientSocket( | |
| 95 kBindType, | |
| 96 base::Bind(&base::RandInt), | |
| 97 net_log_, | |
| 98 NetLog::Source())); | |
| 99 | |
| 100 int rv = socket->Connect(config_.nameservers[index]); | |
| 101 if (rv != OK) | |
| 102 socket.reset(NULL); | |
|
cbentzel
2012/08/29 16:48:06
Is this always guaranteed to complete synchronousl
szym
2012/08/29 16:57:08
Connect? Yes. Note that it does not take a Complet
| |
| 103 } | |
| 104 | |
| 105 if (!socket.get()) | |
| 106 return scoped_ptr<SocketLease>(NULL); | |
| 107 | |
| 108 | |
| 109 return scoped_ptr<SocketLease>(new SocketLease(this, index, socket.Pass())); | |
| 110 } | |
| 111 | |
| 112 // Release a socket. | |
| 113 void DnsSession::FreeSocket(int index, | |
| 114 scoped_ptr<DatagramClientSocket> socket) { | |
| 115 std::vector<DatagramClientSocket*>& pool = pools_[index]; | |
| 116 | |
| 117 if (!bypass_pools_ && (pool.size() < kMaxPoolSize)) | |
| 118 pool.push_back(socket.release()); | |
| 119 } | |
| 120 | |
| 45 | 121 |
| 46 } // namespace net | 122 } // namespace net |
| 47 | 123 |
| OLD | NEW |