[email protected] | 5acdce1 | 2011-03-30 13:00:20 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 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/socket/socks_client_socket_pool.h" |
| 6 | |
| 7 | #include "base/time.h" |
[email protected] | ba00b49 | 2010-09-08 14:53:38 | [diff] [blame] | 8 | #include "base/values.h" |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 9 | #include "googleurl/src/gurl.h" |
| 10 | #include "net/base/net_errors.h" |
| 11 | #include "net/socket/client_socket_factory.h" |
| 12 | #include "net/socket/client_socket_handle.h" |
| 13 | #include "net/socket/client_socket_pool_base.h" |
| 14 | #include "net/socket/socks5_client_socket.h" |
| 15 | #include "net/socket/socks_client_socket.h" |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 16 | #include "net/socket/transport_client_socket_pool.h" |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 17 | |
| 18 | namespace net { |
| 19 | |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 20 | SOCKSSocketParams::SOCKSSocketParams( |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 21 | const scoped_refptr<TransportSocketParams>& proxy_server, |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 22 | bool socks_v5, |
| 23 | const HostPortPair& host_port_pair, |
| 24 | RequestPriority priority, |
| 25 | const GURL& referrer) |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 26 | : transport_params_(proxy_server), |
[email protected] | 930cc74 | 2010-09-15 22:54:10 | [diff] [blame] | 27 | destination_(host_port_pair), |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 28 | socks_v5_(socks_v5) { |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 29 | if (transport_params_) |
| 30 | ignore_limits_ = transport_params_->ignore_limits(); |
[email protected] | 5acdce1 | 2011-03-30 13:00:20 | [diff] [blame] | 31 | else |
| 32 | ignore_limits_ = false; |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 33 | // The referrer is used by the DNS prefetch system to correlate resolutions |
| 34 | // with the page that triggered them. It doesn't impact the actual addresses |
| 35 | // that we resolve to. |
| 36 | destination_.set_referrer(referrer); |
| 37 | destination_.set_priority(priority); |
| 38 | } |
| 39 | |
| 40 | SOCKSSocketParams::~SOCKSSocketParams() {} |
| 41 | |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 42 | // SOCKSConnectJobs will time out after this many seconds. Note this is on |
| 43 | // top of the timeout for the transport socket. |
| 44 | static const int kSOCKSConnectJobTimeoutInSeconds = 30; |
| 45 | |
| 46 | SOCKSConnectJob::SOCKSConnectJob( |
| 47 | const std::string& group_name, |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 48 | const scoped_refptr<SOCKSSocketParams>& socks_params, |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 49 | const base::TimeDelta& timeout_duration, |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 50 | TransportClientSocketPool* transport_pool, |
[email protected] | 73c4532 | 2010-10-01 23:57:54 | [diff] [blame] | 51 | HostResolver* host_resolver, |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 52 | Delegate* delegate, |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 53 | NetLog* net_log) |
| 54 | : ConnectJob(group_name, timeout_duration, delegate, |
| 55 | BoundNetLog::Make(net_log, NetLog::SOURCE_CONNECT_JOB)), |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 56 | socks_params_(socks_params), |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 57 | transport_pool_(transport_pool), |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 58 | resolver_(host_resolver), |
| 59 | ALLOW_THIS_IN_INITIALIZER_LIST( |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 60 | callback_(this, &SOCKSConnectJob::OnIOComplete)) { |
| 61 | } |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 62 | |
| 63 | SOCKSConnectJob::~SOCKSConnectJob() { |
| 64 | // We don't worry about cancelling the tcp socket since the destructor in |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 65 | // scoped_ptr<ClientSocketHandle> transport_socket_handle_ will take care of |
| 66 | // it. |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | LoadState SOCKSConnectJob::GetLoadState() const { |
| 70 | switch (next_state_) { |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 71 | case STATE_TRANSPORT_CONNECT: |
| 72 | case STATE_TRANSPORT_CONNECT_COMPLETE: |
| 73 | return transport_socket_handle_->GetLoadState(); |
[email protected] | 81854c4 | 2010-09-22 03:39:36 | [diff] [blame] | 74 | case STATE_SOCKS_CONNECT: |
| 75 | case STATE_SOCKS_CONNECT_COMPLETE: |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 76 | return LOAD_STATE_CONNECTING; |
| 77 | default: |
| 78 | NOTREACHED(); |
| 79 | return LOAD_STATE_IDLE; |
| 80 | } |
| 81 | } |
| 82 | |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 83 | void SOCKSConnectJob::OnIOComplete(int result) { |
| 84 | int rv = DoLoop(result); |
| 85 | if (rv != ERR_IO_PENDING) |
| 86 | NotifyDelegateOfCompletion(rv); // Deletes |this| |
| 87 | } |
| 88 | |
| 89 | int SOCKSConnectJob::DoLoop(int result) { |
[email protected] | 81854c4 | 2010-09-22 03:39:36 | [diff] [blame] | 90 | DCHECK_NE(next_state_, STATE_NONE); |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 91 | |
| 92 | int rv = result; |
| 93 | do { |
| 94 | State state = next_state_; |
[email protected] | 81854c4 | 2010-09-22 03:39:36 | [diff] [blame] | 95 | next_state_ = STATE_NONE; |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 96 | switch (state) { |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 97 | case STATE_TRANSPORT_CONNECT: |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 98 | DCHECK_EQ(OK, rv); |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 99 | rv = DoTransportConnect(); |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 100 | break; |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 101 | case STATE_TRANSPORT_CONNECT_COMPLETE: |
| 102 | rv = DoTransportConnectComplete(rv); |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 103 | break; |
[email protected] | 81854c4 | 2010-09-22 03:39:36 | [diff] [blame] | 104 | case STATE_SOCKS_CONNECT: |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 105 | DCHECK_EQ(OK, rv); |
| 106 | rv = DoSOCKSConnect(); |
| 107 | break; |
[email protected] | 81854c4 | 2010-09-22 03:39:36 | [diff] [blame] | 108 | case STATE_SOCKS_CONNECT_COMPLETE: |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 109 | rv = DoSOCKSConnectComplete(rv); |
| 110 | break; |
| 111 | default: |
| 112 | NOTREACHED() << "bad state"; |
| 113 | rv = ERR_FAILED; |
| 114 | break; |
| 115 | } |
[email protected] | 81854c4 | 2010-09-22 03:39:36 | [diff] [blame] | 116 | } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 117 | |
| 118 | return rv; |
| 119 | } |
| 120 | |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 121 | int SOCKSConnectJob::DoTransportConnect() { |
| 122 | next_state_ = STATE_TRANSPORT_CONNECT_COMPLETE; |
| 123 | transport_socket_handle_.reset(new ClientSocketHandle()); |
| 124 | return transport_socket_handle_->Init(group_name(), |
| 125 | socks_params_->transport_params(), |
| 126 | socks_params_->destination().priority(), |
| 127 | &callback_, |
| 128 | transport_pool_, |
| 129 | net_log()); |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 130 | } |
| 131 | |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 132 | int SOCKSConnectJob::DoTransportConnectComplete(int result) { |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 133 | if (result != OK) |
[email protected] | f7fccee | 2010-09-16 20:53:01 | [diff] [blame] | 134 | return ERR_PROXY_CONNECTION_FAILED; |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 135 | |
| 136 | // Reset the timer to just the length of time allowed for SOCKS handshake |
| 137 | // so that a fast TCP connection plus a slow SOCKS failure doesn't take |
| 138 | // longer to timeout than it should. |
| 139 | ResetTimer(base::TimeDelta::FromSeconds(kSOCKSConnectJobTimeoutInSeconds)); |
[email protected] | 81854c4 | 2010-09-22 03:39:36 | [diff] [blame] | 140 | next_state_ = STATE_SOCKS_CONNECT; |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 141 | return result; |
| 142 | } |
| 143 | |
| 144 | int SOCKSConnectJob::DoSOCKSConnect() { |
[email protected] | 81854c4 | 2010-09-22 03:39:36 | [diff] [blame] | 145 | next_state_ = STATE_SOCKS_CONNECT_COMPLETE; |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 146 | |
| 147 | // Add a SOCKS connection on top of the tcp socket. |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 148 | if (socks_params_->is_socks_v5()) { |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 149 | socket_.reset(new SOCKS5ClientSocket(transport_socket_handle_.release(), |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 150 | socks_params_->destination())); |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 151 | } else { |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 152 | socket_.reset(new SOCKSClientSocket(transport_socket_handle_.release(), |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 153 | socks_params_->destination(), |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 154 | resolver_)); |
| 155 | } |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 156 | return socket_->Connect(&callback_); |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | int SOCKSConnectJob::DoSOCKSConnectComplete(int result) { |
| 160 | if (result != OK) { |
| 161 | socket_->Disconnect(); |
| 162 | return result; |
| 163 | } |
| 164 | |
| 165 | set_socket(socket_.release()); |
| 166 | return result; |
| 167 | } |
| 168 | |
[email protected] | ad74a59 | 2011-01-21 18:40:55 | [diff] [blame] | 169 | int SOCKSConnectJob::ConnectInternal() { |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 170 | next_state_ = STATE_TRANSPORT_CONNECT; |
[email protected] | ad74a59 | 2011-01-21 18:40:55 | [diff] [blame] | 171 | return DoLoop(OK); |
| 172 | } |
| 173 | |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 174 | ConnectJob* SOCKSClientSocketPool::SOCKSConnectJobFactory::NewConnectJob( |
| 175 | const std::string& group_name, |
| 176 | const PoolBase::Request& request, |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 177 | ConnectJob::Delegate* delegate) const { |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 178 | return new SOCKSConnectJob(group_name, |
| 179 | request.params(), |
| 180 | ConnectionTimeout(), |
| 181 | transport_pool_, |
| 182 | host_resolver_, |
| 183 | delegate, |
| 184 | net_log_); |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | base::TimeDelta |
| 188 | SOCKSClientSocketPool::SOCKSConnectJobFactory::ConnectionTimeout() const { |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 189 | return transport_pool_->ConnectionTimeout() + |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 190 | base::TimeDelta::FromSeconds(kSOCKSConnectJobTimeoutInSeconds); |
| 191 | } |
| 192 | |
| 193 | SOCKSClientSocketPool::SOCKSClientSocketPool( |
| 194 | int max_sockets, |
| 195 | int max_sockets_per_group, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 196 | ClientSocketPoolHistograms* histograms, |
[email protected] | 73c4532 | 2010-10-01 23:57:54 | [diff] [blame] | 197 | HostResolver* host_resolver, |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 198 | TransportClientSocketPool* transport_pool, |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 199 | NetLog* net_log) |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 200 | : transport_pool_(transport_pool), |
[email protected] | ba00b49 | 2010-09-08 14:53:38 | [diff] [blame] | 201 | base_(max_sockets, max_sockets_per_group, histograms, |
[email protected] | 82b8c96 | 2011-10-12 09:17:30 | [diff] [blame^] | 202 | ClientSocketPool::unused_idle_socket_timeout(), |
| 203 | ClientSocketPool::used_idle_socket_timeout(), |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 204 | new SOCKSConnectJobFactory(transport_pool, |
| 205 | host_resolver, |
| 206 | net_log)) { |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 207 | } |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 208 | |
| 209 | SOCKSClientSocketPool::~SOCKSClientSocketPool() {} |
| 210 | |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 211 | int SOCKSClientSocketPool::RequestSocket(const std::string& group_name, |
| 212 | const void* socket_params, |
| 213 | RequestPriority priority, |
| 214 | ClientSocketHandle* handle, |
[email protected] | f1f3f0f8 | 2011-10-01 20:38:10 | [diff] [blame] | 215 | OldCompletionCallback* callback, |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 216 | const BoundNetLog& net_log) { |
| 217 | const scoped_refptr<SOCKSSocketParams>* casted_socket_params = |
| 218 | static_cast<const scoped_refptr<SOCKSSocketParams>*>(socket_params); |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 219 | |
| 220 | return base_.RequestSocket(group_name, *casted_socket_params, priority, |
| 221 | handle, callback, net_log); |
| 222 | } |
| 223 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 224 | void SOCKSClientSocketPool::RequestSockets( |
| 225 | const std::string& group_name, |
| 226 | const void* params, |
| 227 | int num_sockets, |
| 228 | const BoundNetLog& net_log) { |
| 229 | const scoped_refptr<SOCKSSocketParams>* casted_params = |
| 230 | static_cast<const scoped_refptr<SOCKSSocketParams>*>(params); |
| 231 | |
| 232 | base_.RequestSockets(group_name, *casted_params, num_sockets, net_log); |
| 233 | } |
| 234 | |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 235 | void SOCKSClientSocketPool::CancelRequest(const std::string& group_name, |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 236 | ClientSocketHandle* handle) { |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 237 | base_.CancelRequest(group_name, handle); |
| 238 | } |
| 239 | |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 240 | void SOCKSClientSocketPool::ReleaseSocket(const std::string& group_name, |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 241 | StreamSocket* socket, int id) { |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 242 | base_.ReleaseSocket(group_name, socket, id); |
| 243 | } |
| 244 | |
| 245 | void SOCKSClientSocketPool::Flush() { |
| 246 | base_.Flush(); |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | void SOCKSClientSocketPool::CloseIdleSockets() { |
| 250 | base_.CloseIdleSockets(); |
| 251 | } |
| 252 | |
[email protected] | 7cf4091 | 2010-12-09 18:25:03 | [diff] [blame] | 253 | int SOCKSClientSocketPool::IdleSocketCount() const { |
| 254 | return base_.idle_socket_count(); |
| 255 | } |
| 256 | |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 257 | int SOCKSClientSocketPool::IdleSocketCountInGroup( |
| 258 | const std::string& group_name) const { |
| 259 | return base_.IdleSocketCountInGroup(group_name); |
| 260 | } |
| 261 | |
| 262 | LoadState SOCKSClientSocketPool::GetLoadState( |
| 263 | const std::string& group_name, const ClientSocketHandle* handle) const { |
| 264 | return base_.GetLoadState(group_name, handle); |
| 265 | } |
| 266 | |
[email protected] | ba00b49 | 2010-09-08 14:53:38 | [diff] [blame] | 267 | DictionaryValue* SOCKSClientSocketPool::GetInfoAsValue( |
| 268 | const std::string& name, |
| 269 | const std::string& type, |
| 270 | bool include_nested_pools) const { |
| 271 | DictionaryValue* dict = base_.GetInfoAsValue(name, type); |
| 272 | if (include_nested_pools) { |
| 273 | ListValue* list = new ListValue(); |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 274 | list->Append(transport_pool_->GetInfoAsValue("transport_socket_pool", |
| 275 | "transport_socket_pool", |
| 276 | false)); |
[email protected] | ba00b49 | 2010-09-08 14:53:38 | [diff] [blame] | 277 | dict->Set("nested_pools", list); |
| 278 | } |
| 279 | return dict; |
| 280 | } |
| 281 | |
[email protected] | 7cf4091 | 2010-12-09 18:25:03 | [diff] [blame] | 282 | base::TimeDelta SOCKSClientSocketPool::ConnectionTimeout() const { |
| 283 | return base_.ConnectionTimeout(); |
| 284 | } |
| 285 | |
| 286 | ClientSocketPoolHistograms* SOCKSClientSocketPool::histograms() const { |
| 287 | return base_.histograms(); |
| 288 | }; |
| 289 | |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 290 | } // namespace net |