| 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, |
| 19 NetLog* net_log) | 31 NetLog* net_log) |
| 20 : config_(config), | 32 : config_(config), |
| 21 socket_factory_(factory), | 33 socket_factory_(factory), |
| 22 rand_callback_(base::Bind(rand_int_callback, 0, kuint16max)), | 34 rand_callback_(base::Bind(rand_int_callback, 0, kuint16max)), |
| 23 net_log_(net_log), | 35 net_log_(net_log), |
| 36 sockets_(config.nameservers.size()), |
| 24 server_index_(0) { | 37 server_index_(0) { |
| 25 } | 38 } |
| 26 | 39 |
| 27 int DnsSession::NextQueryId() const { | 40 int DnsSession::NextQueryId() const { |
| 28 return rand_callback_.Run(); | 41 return rand_callback_.Run(); |
| 29 } | 42 } |
| 30 | 43 |
| 31 int DnsSession::NextFirstServerIndex() { | 44 int DnsSession::NextFirstServerIndex() { |
| 32 int index = server_index_; | 45 int index = server_index_; |
| 33 if (config_.rotate) | 46 if (config_.rotate) |
| 34 server_index_ = (server_index_ + 1) % config_.nameservers.size(); | 47 server_index_ = (server_index_ + 1) % config_.nameservers.size(); |
| 35 return index; | 48 return index; |
| 36 } | 49 } |
| 37 | 50 |
| 38 base::TimeDelta DnsSession::NextTimeout(int attempt) { | 51 base::TimeDelta DnsSession::NextTimeout(int attempt) { |
| 39 // The timeout doubles every full round (each nameserver once). | 52 // The timeout doubles every full round (each nameserver once). |
| 40 // TODO(szym): Adapt timeout to observed RTT. http://crbug.com/110197 | 53 // TODO(szym): Adapt timeout to observed RTT. http://crbug.com/110197 |
| 41 return config_.timeout * (1 << (attempt / config_.nameservers.size())); | 54 return config_.timeout * (1 << (attempt / config_.nameservers.size())); |
| 42 } | 55 } |
| 43 | 56 |
| 44 DnsSession::~DnsSession() {} | 57 // Allocate a socket, already connected to the server address. |
| 58 scoped_ptr<DnsSession::SocketLease> DnsSession::AllocateSocket(int index) { |
| 59 #if defined(OS_WIN) |
| 60 // TODO(szym): Require N sockets for entropy. (N = 1000?) |
| 61 if (!sockets_[index].empty()) { |
| 62 // TODO(szym): Pick a random socket. |
| 63 return scoped_ptr<SocketLease>(new SocketLease(this, index, |
| 64 sockets_[index].pop_back())); |
| 65 } |
| 66 // Allocate a socket. |
| 67 scoped_ptr<DatagramClientSocket> socket( |
| 68 socket_factory_->CreateDatagramClientSocket( |
| 69 DatagramSocket::DEFAULT_BIND, |
| 70 base::Bind(&base::RandInt), |
| 71 net_log_, |
| 72 NetLog::Source())); |
| 73 |
| 74 #else |
| 75 scoped_ptr<DatagramClientSocket> socket( |
| 76 socket_factory_->CreateDatagramClientSocket( |
| 77 DatagramSocket::RANDOM_BIND, |
| 78 base::Bind(&base::RandInt), |
| 79 net_log_, |
| 80 NetLog::Source())); |
| 81 #endif |
| 82 |
| 83 if (!socket.get()) |
| 84 return scoped_ptr<SocketLease>(NULL); |
| 85 |
| 86 int rv = socket->Connect(config_.nameservers[index]); |
| 87 if (rv != OK) |
| 88 return scoped_ptr<SocketLease>(NULL); |
| 89 |
| 90 return scoped_ptr<SocketLease>(new SocketLease(this, index, socket.Pass())); |
| 91 } |
| 92 |
| 93 // Release a socket. |
| 94 void DnsSession::FreeSocket(int index, |
| 95 scoped_ptr<DatagramClientSocket> socket) { |
| 96 #if defined(OS_WIN) |
| 97 // TODO(szym): DCHECK(config.nameservers[index] == socket.GetPeerAddress()) |
| 98 sockets_[index].push_back(socket.release()); |
| 99 #endif |
| 100 // else: Do nothing. This destroys the socket. |
| 101 } |
| 102 |
| 103 DnsSession::~DnsSession() { |
| 104 for (size_t i = 0; i < sockets_.size(); ++i) { |
| 105 STLDeleteElements(&sockets_[i]); |
| 106 } |
| 107 } |
| 108 |
| 45 | 109 |
| 46 } // namespace net | 110 } // namespace net |
| 47 | 111 |
| OLD | NEW |