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/socket/socks_client_socket_pool.h" | 5 #include "net/socket/socks_client_socket_pool.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
14 #include "net/socket/client_socket_factory.h" | 14 #include "net/socket/client_socket_factory.h" |
15 #include "net/socket/client_socket_handle.h" | 15 #include "net/socket/client_socket_handle.h" |
16 #include "net/socket/client_socket_pool_base.h" | 16 #include "net/socket/client_socket_pool_base.h" |
17 #include "net/socket/socks5_client_socket.h" | 17 #include "net/socket/socks5_client_socket.h" |
18 #include "net/socket/socks_client_socket.h" | 18 #include "net/socket/socks_client_socket.h" |
19 #include "net/socket/transport_client_socket_pool.h" | 19 #include "net/socket/transport_client_socket_pool.h" |
20 | 20 |
21 namespace net { | 21 namespace net { |
22 | 22 |
23 SOCKSSocketParams::SOCKSSocketParams( | 23 SOCKSSocketParams::SOCKSSocketParams( |
24 const scoped_refptr<TransportSocketParams>& proxy_server, | 24 const scoped_refptr<TransportSocketParams>& proxy_server, |
25 bool socks_v5, | 25 bool socks_v5, |
26 const HostPortPair& host_port_pair) | 26 const HostPortPair& host_port_pair) |
27 : transport_params_(proxy_server), | 27 : transport_params_(proxy_server), |
28 destination_(host_port_pair), | 28 destination_(host_port_pair), |
29 socks_v5_(socks_v5) { | 29 socks_v5_(socks_v5) { |
30 if (transport_params_.get()) | |
31 ignore_limits_ = transport_params_->ignore_limits(); | |
32 else | |
33 ignore_limits_ = false; | |
34 } | 30 } |
35 | 31 |
36 SOCKSSocketParams::~SOCKSSocketParams() {} | 32 SOCKSSocketParams::~SOCKSSocketParams() {} |
37 | 33 |
38 // SOCKSConnectJobs will time out after this many seconds. Note this is on | 34 // SOCKSConnectJobs will time out after this many seconds. Note this is on |
39 // top of the timeout for the transport socket. | 35 // top of the timeout for the transport socket. |
40 static const int kSOCKSConnectJobTimeoutInSeconds = 30; | 36 static const int kSOCKSConnectJobTimeoutInSeconds = 30; |
41 | 37 |
42 SOCKSConnectJob::SOCKSConnectJob( | 38 SOCKSConnectJob::SOCKSConnectJob( |
43 const std::string& group_name, | 39 const std::string& group_name, |
44 RequestPriority priority, | 40 RequestPriority priority, |
| 41 ClientSocketPool::RespectLimits respect_limits, |
45 const scoped_refptr<SOCKSSocketParams>& socks_params, | 42 const scoped_refptr<SOCKSSocketParams>& socks_params, |
46 const base::TimeDelta& timeout_duration, | 43 const base::TimeDelta& timeout_duration, |
47 TransportClientSocketPool* transport_pool, | 44 TransportClientSocketPool* transport_pool, |
48 HostResolver* host_resolver, | 45 HostResolver* host_resolver, |
49 Delegate* delegate, | 46 Delegate* delegate, |
50 NetLog* net_log) | 47 NetLog* net_log) |
51 : ConnectJob(group_name, timeout_duration, priority, delegate, | 48 : ConnectJob(group_name, |
| 49 timeout_duration, |
| 50 priority, |
| 51 respect_limits, |
| 52 delegate, |
52 BoundNetLog::Make(net_log, NetLog::SOURCE_CONNECT_JOB)), | 53 BoundNetLog::Make(net_log, NetLog::SOURCE_CONNECT_JOB)), |
53 socks_params_(socks_params), | 54 socks_params_(socks_params), |
54 transport_pool_(transport_pool), | 55 transport_pool_(transport_pool), |
55 resolver_(host_resolver), | 56 resolver_(host_resolver), |
56 callback_(base::Bind(&SOCKSConnectJob::OnIOComplete, | 57 callback_( |
57 base::Unretained(this))) { | 58 base::Bind(&SOCKSConnectJob::OnIOComplete, base::Unretained(this))) {} |
58 } | |
59 | 59 |
60 SOCKSConnectJob::~SOCKSConnectJob() { | 60 SOCKSConnectJob::~SOCKSConnectJob() { |
61 // We don't worry about cancelling the tcp socket since the destructor in | 61 // We don't worry about cancelling the tcp socket since the destructor in |
62 // scoped_ptr<ClientSocketHandle> transport_socket_handle_ will take care of | 62 // scoped_ptr<ClientSocketHandle> transport_socket_handle_ will take care of |
63 // it. | 63 // it. |
64 } | 64 } |
65 | 65 |
66 LoadState SOCKSConnectJob::GetLoadState() const { | 66 LoadState SOCKSConnectJob::GetLoadState() const { |
67 switch (next_state_) { | 67 switch (next_state_) { |
68 case STATE_TRANSPORT_CONNECT: | 68 case STATE_TRANSPORT_CONNECT: |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 break; | 111 break; |
112 } | 112 } |
113 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); | 113 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); |
114 | 114 |
115 return rv; | 115 return rv; |
116 } | 116 } |
117 | 117 |
118 int SOCKSConnectJob::DoTransportConnect() { | 118 int SOCKSConnectJob::DoTransportConnect() { |
119 next_state_ = STATE_TRANSPORT_CONNECT_COMPLETE; | 119 next_state_ = STATE_TRANSPORT_CONNECT_COMPLETE; |
120 transport_socket_handle_.reset(new ClientSocketHandle()); | 120 transport_socket_handle_.reset(new ClientSocketHandle()); |
121 return transport_socket_handle_->Init(group_name(), | 121 return transport_socket_handle_->Init( |
122 socks_params_->transport_params(), | 122 group_name(), socks_params_->transport_params(), priority(), |
123 priority(), | 123 respect_limits(), callback_, transport_pool_, net_log()); |
124 callback_, | |
125 transport_pool_, | |
126 net_log()); | |
127 } | 124 } |
128 | 125 |
129 int SOCKSConnectJob::DoTransportConnectComplete(int result) { | 126 int SOCKSConnectJob::DoTransportConnectComplete(int result) { |
130 if (result != OK) | 127 if (result != OK) |
131 return ERR_PROXY_CONNECTION_FAILED; | 128 return ERR_PROXY_CONNECTION_FAILED; |
132 | 129 |
133 // Reset the timer to just the length of time allowed for SOCKS handshake | 130 // Reset the timer to just the length of time allowed for SOCKS handshake |
134 // so that a fast TCP connection plus a slow SOCKS failure doesn't take | 131 // so that a fast TCP connection plus a slow SOCKS failure doesn't take |
135 // longer to timeout than it should. | 132 // longer to timeout than it should. |
136 ResetTimer(base::TimeDelta::FromSeconds(kSOCKSConnectJobTimeoutInSeconds)); | 133 ResetTimer(base::TimeDelta::FromSeconds(kSOCKSConnectJobTimeoutInSeconds)); |
(...skipping 30 matching lines...) Expand all Loading... |
167 int SOCKSConnectJob::ConnectInternal() { | 164 int SOCKSConnectJob::ConnectInternal() { |
168 next_state_ = STATE_TRANSPORT_CONNECT; | 165 next_state_ = STATE_TRANSPORT_CONNECT; |
169 return DoLoop(OK); | 166 return DoLoop(OK); |
170 } | 167 } |
171 | 168 |
172 scoped_ptr<ConnectJob> | 169 scoped_ptr<ConnectJob> |
173 SOCKSClientSocketPool::SOCKSConnectJobFactory::NewConnectJob( | 170 SOCKSClientSocketPool::SOCKSConnectJobFactory::NewConnectJob( |
174 const std::string& group_name, | 171 const std::string& group_name, |
175 const PoolBase::Request& request, | 172 const PoolBase::Request& request, |
176 ConnectJob::Delegate* delegate) const { | 173 ConnectJob::Delegate* delegate) const { |
177 return scoped_ptr<ConnectJob>(new SOCKSConnectJob(group_name, | 174 return scoped_ptr<ConnectJob>(new SOCKSConnectJob( |
178 request.priority(), | 175 group_name, request.priority(), request.respect_limits(), |
179 request.params(), | 176 request.params(), ConnectionTimeout(), transport_pool_, host_resolver_, |
180 ConnectionTimeout(), | 177 delegate, net_log_)); |
181 transport_pool_, | |
182 host_resolver_, | |
183 delegate, | |
184 net_log_)); | |
185 } | 178 } |
186 | 179 |
187 base::TimeDelta | 180 base::TimeDelta |
188 SOCKSClientSocketPool::SOCKSConnectJobFactory::ConnectionTimeout() const { | 181 SOCKSClientSocketPool::SOCKSConnectJobFactory::ConnectionTimeout() const { |
189 return transport_pool_->ConnectionTimeout() + | 182 return transport_pool_->ConnectionTimeout() + |
190 base::TimeDelta::FromSeconds(kSOCKSConnectJobTimeoutInSeconds); | 183 base::TimeDelta::FromSeconds(kSOCKSConnectJobTimeoutInSeconds); |
191 } | 184 } |
192 | 185 |
193 SOCKSClientSocketPool::SOCKSClientSocketPool( | 186 SOCKSClientSocketPool::SOCKSClientSocketPool( |
194 int max_sockets, | 187 int max_sockets, |
(...skipping 10 matching lines...) Expand all Loading... |
205 ClientSocketPool::used_idle_socket_timeout(), | 198 ClientSocketPool::used_idle_socket_timeout(), |
206 new SOCKSConnectJobFactory(transport_pool, host_resolver, net_log)) { | 199 new SOCKSConnectJobFactory(transport_pool, host_resolver, net_log)) { |
207 // We should always have a |transport_pool_| except in unit tests. | 200 // We should always have a |transport_pool_| except in unit tests. |
208 if (transport_pool_) | 201 if (transport_pool_) |
209 base_.AddLowerLayeredPool(transport_pool_); | 202 base_.AddLowerLayeredPool(transport_pool_); |
210 } | 203 } |
211 | 204 |
212 SOCKSClientSocketPool::~SOCKSClientSocketPool() { | 205 SOCKSClientSocketPool::~SOCKSClientSocketPool() { |
213 } | 206 } |
214 | 207 |
215 int SOCKSClientSocketPool::RequestSocket( | 208 int SOCKSClientSocketPool::RequestSocket(const std::string& group_name, |
216 const std::string& group_name, const void* socket_params, | 209 const void* socket_params, |
217 RequestPriority priority, ClientSocketHandle* handle, | 210 RequestPriority priority, |
218 const CompletionCallback& callback, const BoundNetLog& net_log) { | 211 RespectLimits respect_limits, |
| 212 ClientSocketHandle* handle, |
| 213 const CompletionCallback& callback, |
| 214 const BoundNetLog& net_log) { |
219 const scoped_refptr<SOCKSSocketParams>* casted_socket_params = | 215 const scoped_refptr<SOCKSSocketParams>* casted_socket_params = |
220 static_cast<const scoped_refptr<SOCKSSocketParams>*>(socket_params); | 216 static_cast<const scoped_refptr<SOCKSSocketParams>*>(socket_params); |
221 | 217 |
222 return base_.RequestSocket(group_name, *casted_socket_params, priority, | 218 return base_.RequestSocket(group_name, *casted_socket_params, priority, |
223 handle, callback, net_log); | 219 respect_limits, handle, callback, net_log); |
224 } | 220 } |
225 | 221 |
226 void SOCKSClientSocketPool::RequestSockets( | 222 void SOCKSClientSocketPool::RequestSockets( |
227 const std::string& group_name, | 223 const std::string& group_name, |
228 const void* params, | 224 const void* params, |
229 int num_sockets, | 225 int num_sockets, |
230 const BoundNetLog& net_log) { | 226 const BoundNetLog& net_log) { |
231 const scoped_refptr<SOCKSSocketParams>* casted_params = | 227 const scoped_refptr<SOCKSSocketParams>* casted_params = |
232 static_cast<const scoped_refptr<SOCKSSocketParams>*>(params); | 228 static_cast<const scoped_refptr<SOCKSSocketParams>*>(params); |
233 | 229 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 base_.RemoveHigherLayeredPool(higher_pool); | 296 base_.RemoveHigherLayeredPool(higher_pool); |
301 } | 297 } |
302 | 298 |
303 bool SOCKSClientSocketPool::CloseOneIdleConnection() { | 299 bool SOCKSClientSocketPool::CloseOneIdleConnection() { |
304 if (base_.CloseOneIdleSocket()) | 300 if (base_.CloseOneIdleSocket()) |
305 return true; | 301 return true; |
306 return base_.CloseOneIdleConnectionInHigherLayeredPool(); | 302 return base_.CloseOneIdleConnectionInHigherLayeredPool(); |
307 } | 303 } |
308 | 304 |
309 } // namespace net | 305 } // namespace net |
OLD | NEW |