Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/dns/dns_socket_pool.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/rand_util.h" | |
| 9 #include "net/base/ip_endpoint.h" | |
| 10 #include "net/base/net_errors.h" | |
| 11 #include "net/base/rand_callback.h" | |
| 12 #include "net/socket/client_socket_factory.h" | |
| 13 #include "net/udp/datagram_client_socket.h" | |
| 14 | |
| 15 namespace net { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 // On Windows, we can't request specific (random) ports, since that will | |
| 20 // trigger firewall prompts, so request default ones, but keep a pile of | |
| 21 // them. Everywhere else, request fresh, random ports each time. | |
| 22 #if defined(OS_WIN) | |
| 23 const DatagramSocket::BindType kBindType = DatagramSocket::DEFAULT_BIND; | |
| 24 const unsigned kInitialPoolSize = 256; | |
|
szym
2012/09/26 11:16:12
Some comment explaining how these constants will b
Deprecated (see juliatuttle)
2012/09/26 20:44:50
Done.
| |
| 25 const unsigned kAllocateMinSize = 256; | |
| 26 const unsigned kRetainMaxSize = 0; | |
| 27 #else | |
| 28 const DatagramSocket::BindType kBindType = DatagramSocket::RANDOM_BIND; | |
| 29 const unsigned kInitialPoolSize = 0; | |
| 30 const unsigned kAllocateMinSize = 0; | |
| 31 const unsigned kRetainMaxSize = 0; | |
| 32 #endif | |
| 33 | |
| 34 } // namespace | |
| 35 | |
| 36 DnsSocketPool::DnsSocketPool(ClientSocketFactory* socket_factory) | |
| 37 : socket_factory_(socket_factory), | |
| 38 net_log_(NULL), | |
| 39 nameservers_(NULL), | |
| 40 initialized_(false) { | |
| 41 } | |
| 42 | |
| 43 void DnsSocketPool::InitializeInternal( | |
| 44 NetLog* net_log, | |
| 45 const std::vector<IPEndPoint>* nameservers) { | |
| 46 // net_log can be NULL if we don't want to log | |
|
szym
2012/09/26 11:16:12
If you want to keep this comment put it in the dec
Deprecated (see juliatuttle)
2012/09/26 20:44:50
Deleting it instead.
| |
| 47 DCHECK(nameservers); | |
| 48 | |
| 49 CHECK(!initialized_); | |
|
szym
2012/09/26 11:16:12
A DCHECK would probably suffice for now.
Deprecated (see juliatuttle)
2012/09/26 20:44:50
Done.
| |
| 50 | |
| 51 net_log_ = net_log; | |
| 52 nameservers_ = nameservers; | |
| 53 initialized_ = true; | |
| 54 } | |
| 55 | |
| 56 scoped_ptr<DatagramClientSocket> DnsSocketPool::CreateConnectedSocket( | |
| 57 unsigned server_index) { | |
| 58 DCHECK_LT(server_index, nameservers_->size()); | |
| 59 | |
| 60 scoped_ptr<DatagramClientSocket> socket; | |
| 61 | |
| 62 NetLog::Source no_source; | |
| 63 socket.reset(socket_factory_->CreateDatagramClientSocket( | |
| 64 kBindType, base::Bind(&base::RandInt), net_log_, no_source)); | |
| 65 | |
| 66 if (socket.get()) { | |
| 67 int rv = socket->Connect((*nameservers_)[server_index]); | |
| 68 if (rv != OK) { | |
| 69 LOG(WARNING) << "Failed to connect socket: " << rv; | |
| 70 socket.reset(); | |
| 71 } | |
| 72 } else { | |
| 73 LOG(WARNING) << "Failed to create socket."; | |
| 74 } | |
| 75 | |
| 76 return socket.Pass(); | |
| 77 } | |
| 78 | |
| 79 class DefaultDnsSocketPool : public DnsSocketPool { | |
| 80 public: | |
| 81 DefaultDnsSocketPool(ClientSocketFactory* factory) | |
| 82 : DnsSocketPool(factory) { | |
| 83 }; | |
| 84 | |
|
szym
2012/09/26 11:16:12
IMPORTANT: all the retained sockets are leaking ou
Deprecated (see juliatuttle)
2012/09/26 20:44:50
Done.
| |
| 85 virtual void Initialize( | |
| 86 NetLog* net_log, | |
| 87 const std::vector<IPEndPoint>* nameservers) OVERRIDE; | |
| 88 | |
| 89 virtual scoped_ptr<DatagramClientSocket> AllocateSocket( | |
| 90 unsigned server_index) OVERRIDE; | |
| 91 | |
| 92 virtual void FreeSocket( | |
| 93 unsigned server_index, | |
| 94 scoped_ptr<DatagramClientSocket> socket) OVERRIDE; | |
| 95 | |
| 96 private: | |
| 97 std::vector<std::vector<DatagramClientSocket*> > pools_; | |
|
szym
2012/09/26 11:16:12
vector<DatagramClientSocket*> is used 4 times. I s
Deprecated (see juliatuttle)
2012/09/26 20:44:50
Done.
| |
| 98 | |
| 99 DISALLOW_COPY_AND_ASSIGN(DefaultDnsSocketPool); | |
| 100 }; | |
| 101 | |
| 102 // static | |
| 103 scoped_ptr<DnsSocketPool> DnsSocketPool::CreateDefault( | |
| 104 ClientSocketFactory* factory) { | |
| 105 return scoped_ptr<DnsSocketPool>(new DefaultDnsSocketPool(factory)); | |
| 106 } | |
| 107 | |
| 108 void DefaultDnsSocketPool::Initialize( | |
| 109 NetLog* net_log, | |
| 110 const std::vector<IPEndPoint>* nameservers) OVERRIDE { | |
| 111 DCHECK(pools_.empty()); | |
| 112 | |
| 113 InitializeInternal(net_log, nameservers); | |
| 114 | |
| 115 const unsigned num_servers = nameservers->size(); | |
| 116 pools_.resize(num_servers); | |
| 117 for (unsigned server_index = 0; server_index < num_servers; ++server_index) { | |
| 118 std::vector<DatagramClientSocket*>& pool = pools_[server_index]; | |
| 119 for (unsigned pool_index = 0; pool_index < kInitialPoolSize; ++pool_index) { | |
| 120 DatagramClientSocket* socket = | |
| 121 CreateConnectedSocket(server_index).release(); | |
| 122 if (!socket) | |
| 123 break; | |
| 124 pool.push_back(socket); | |
| 125 } | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 scoped_ptr<DatagramClientSocket> DefaultDnsSocketPool::AllocateSocket( | |
| 130 unsigned server_index) { | |
| 131 DCHECK_LT(server_index, pools_.size()); | |
| 132 std::vector<DatagramClientSocket*>& pool = pools_[server_index]; | |
| 133 | |
| 134 // Allocate one socket more than the minimum, since we're about to remove one | |
|
szym
2012/09/26 11:16:12
I'm guessing this is so that if kAllocateMinSize =
Deprecated (see juliatuttle)
2012/09/26 20:44:50
Done.
| |
| 135 while (pool.size() < kAllocateMinSize + 1) { | |
| 136 DatagramClientSocket* socket = | |
| 137 CreateConnectedSocket(server_index).release(); | |
| 138 if (!socket) { | |
|
szym
2012/09/26 11:16:12
nit: no need for {} (at least for consistency with
Deprecated (see juliatuttle)
2012/09/26 20:44:50
Done.
| |
| 139 break; | |
| 140 } | |
| 141 pool.push_back(socket); | |
| 142 } | |
| 143 | |
| 144 if (pool.size() < kAllocateMinSize) { | |
|
szym
2012/09/26 11:16:12
No +1 here, so a bit confused. If you removed +1,
Deprecated (see juliatuttle)
2012/09/26 20:44:50
Done.
| |
| 145 LOG(WARNING) << "Low DNS port entropy: wanted " << kAllocateMinSize | |
| 146 << " sockets to choose from, but only have " << pool.size(); | |
| 147 } | |
| 148 | |
| 149 unsigned socket_index = base::RandInt(0, pool.size() - 1); | |
| 150 DatagramClientSocket* socket = pool[socket_index]; | |
| 151 pool[socket_index] = pool.back(); | |
| 152 pool.pop_back(); | |
| 153 | |
| 154 return scoped_ptr<DatagramClientSocket>(socket); | |
| 155 } | |
| 156 | |
| 157 void DefaultDnsSocketPool::FreeSocket( | |
| 158 unsigned server_index, | |
| 159 scoped_ptr<DatagramClientSocket> socket) { | |
| 160 DCHECK_LT(server_index, pools_.size()); | |
| 161 std::vector<DatagramClientSocket*>& pool = pools_[server_index]; | |
| 162 | |
| 163 if (pool.size() < kRetainMaxSize) | |
| 164 pool.push_back(socket.release()); | |
| 165 } | |
| 166 | |
| 167 } // namespace net | |
| OLD | NEW |