Delete unmasked credit cards when clearing data.
[chromium-blink-merge.git] / net / socket / socks_client_socket.cc
blob75eb36a76d73d8a9b6968ea6b31810a2d6effd22
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.
5 #include "net/socket/socks_client_socket.h"
7 #include "base/basictypes.h"
8 #include "base/bind.h"
9 #include "base/callback_helpers.h"
10 #include "base/compiler_specific.h"
11 #include "base/profiler/scoped_tracker.h"
12 #include "base/sys_byteorder.h"
13 #include "net/base/io_buffer.h"
14 #include "net/base/net_log.h"
15 #include "net/base/net_util.h"
16 #include "net/socket/client_socket_handle.h"
18 namespace net {
20 // Every SOCKS server requests a user-id from the client. It is optional
21 // and we send an empty string.
22 static const char kEmptyUserId[] = "";
24 // For SOCKS4, the client sends 8 bytes plus the size of the user-id.
25 static const unsigned int kWriteHeaderSize = 8;
27 // For SOCKS4 the server sends 8 bytes for acknowledgement.
28 static const unsigned int kReadHeaderSize = 8;
30 // Server Response codes for SOCKS.
31 static const uint8 kServerResponseOk = 0x5A;
32 static const uint8 kServerResponseRejected = 0x5B;
33 static const uint8 kServerResponseNotReachable = 0x5C;
34 static const uint8 kServerResponseMismatchedUserId = 0x5D;
36 static const uint8 kSOCKSVersion4 = 0x04;
37 static const uint8 kSOCKSStreamRequest = 0x01;
39 // A struct holding the essential details of the SOCKS4 Server Request.
40 // The port in the header is stored in network byte order.
41 struct SOCKS4ServerRequest {
42 uint8 version;
43 uint8 command;
44 uint16 nw_port;
45 uint8 ip[4];
47 static_assert(sizeof(SOCKS4ServerRequest) == kWriteHeaderSize,
48 "socks4 server request struct has incorrect size");
50 // A struct holding details of the SOCKS4 Server Response.
51 struct SOCKS4ServerResponse {
52 uint8 reserved_null;
53 uint8 code;
54 uint16 port;
55 uint8 ip[4];
57 static_assert(sizeof(SOCKS4ServerResponse) == kReadHeaderSize,
58 "socks4 server response struct has incorrect size");
60 SOCKSClientSocket::SOCKSClientSocket(
61 scoped_ptr<ClientSocketHandle> transport_socket,
62 const HostResolver::RequestInfo& req_info,
63 RequestPriority priority,
64 HostResolver* host_resolver)
65 : transport_(transport_socket.Pass()),
66 next_state_(STATE_NONE),
67 completed_handshake_(false),
68 bytes_sent_(0),
69 bytes_received_(0),
70 was_ever_used_(false),
71 host_resolver_(host_resolver),
72 host_request_info_(req_info),
73 priority_(priority),
74 net_log_(transport_->socket()->NetLog()) {}
76 SOCKSClientSocket::~SOCKSClientSocket() {
77 Disconnect();
80 int SOCKSClientSocket::Connect(const CompletionCallback& callback) {
81 DCHECK(transport_.get());
82 DCHECK(transport_->socket());
83 DCHECK_EQ(STATE_NONE, next_state_);
84 DCHECK(user_callback_.is_null());
86 // If already connected, then just return OK.
87 if (completed_handshake_)
88 return OK;
90 next_state_ = STATE_RESOLVE_HOST;
92 net_log_.BeginEvent(NetLog::TYPE_SOCKS_CONNECT);
94 int rv = DoLoop(OK);
95 if (rv == ERR_IO_PENDING) {
96 user_callback_ = callback;
97 } else {
98 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SOCKS_CONNECT, rv);
100 return rv;
103 void SOCKSClientSocket::Disconnect() {
104 completed_handshake_ = false;
105 host_resolver_.Cancel();
106 transport_->socket()->Disconnect();
108 // Reset other states to make sure they aren't mistakenly used later.
109 // These are the states initialized by Connect().
110 next_state_ = STATE_NONE;
111 user_callback_.Reset();
114 bool SOCKSClientSocket::IsConnected() const {
115 return completed_handshake_ && transport_->socket()->IsConnected();
118 bool SOCKSClientSocket::IsConnectedAndIdle() const {
119 return completed_handshake_ && transport_->socket()->IsConnectedAndIdle();
122 const BoundNetLog& SOCKSClientSocket::NetLog() const {
123 return net_log_;
126 void SOCKSClientSocket::SetSubresourceSpeculation() {
127 if (transport_.get() && transport_->socket()) {
128 transport_->socket()->SetSubresourceSpeculation();
129 } else {
130 NOTREACHED();
134 void SOCKSClientSocket::SetOmniboxSpeculation() {
135 if (transport_.get() && transport_->socket()) {
136 transport_->socket()->SetOmniboxSpeculation();
137 } else {
138 NOTREACHED();
142 bool SOCKSClientSocket::WasEverUsed() const {
143 return was_ever_used_;
146 bool SOCKSClientSocket::UsingTCPFastOpen() const {
147 if (transport_.get() && transport_->socket()) {
148 return transport_->socket()->UsingTCPFastOpen();
150 NOTREACHED();
151 return false;
154 bool SOCKSClientSocket::WasNpnNegotiated() const {
155 if (transport_.get() && transport_->socket()) {
156 return transport_->socket()->WasNpnNegotiated();
158 NOTREACHED();
159 return false;
162 NextProto SOCKSClientSocket::GetNegotiatedProtocol() const {
163 if (transport_.get() && transport_->socket()) {
164 return transport_->socket()->GetNegotiatedProtocol();
166 NOTREACHED();
167 return kProtoUnknown;
170 bool SOCKSClientSocket::GetSSLInfo(SSLInfo* ssl_info) {
171 if (transport_.get() && transport_->socket()) {
172 return transport_->socket()->GetSSLInfo(ssl_info);
174 NOTREACHED();
175 return false;
179 // Read is called by the transport layer above to read. This can only be done
180 // if the SOCKS handshake is complete.
181 int SOCKSClientSocket::Read(IOBuffer* buf, int buf_len,
182 const CompletionCallback& callback) {
183 DCHECK(completed_handshake_);
184 DCHECK_EQ(STATE_NONE, next_state_);
185 DCHECK(user_callback_.is_null());
186 DCHECK(!callback.is_null());
188 int rv = transport_->socket()->Read(
189 buf, buf_len,
190 base::Bind(&SOCKSClientSocket::OnReadWriteComplete,
191 base::Unretained(this), callback));
192 if (rv > 0)
193 was_ever_used_ = true;
194 return rv;
197 // Write is called by the transport layer. This can only be done if the
198 // SOCKS handshake is complete.
199 int SOCKSClientSocket::Write(IOBuffer* buf, int buf_len,
200 const CompletionCallback& callback) {
201 DCHECK(completed_handshake_);
202 DCHECK_EQ(STATE_NONE, next_state_);
203 DCHECK(user_callback_.is_null());
204 DCHECK(!callback.is_null());
206 int rv = transport_->socket()->Write(
207 buf, buf_len,
208 base::Bind(&SOCKSClientSocket::OnReadWriteComplete,
209 base::Unretained(this), callback));
210 if (rv > 0)
211 was_ever_used_ = true;
212 return rv;
215 int SOCKSClientSocket::SetReceiveBufferSize(int32 size) {
216 return transport_->socket()->SetReceiveBufferSize(size);
219 int SOCKSClientSocket::SetSendBufferSize(int32 size) {
220 return transport_->socket()->SetSendBufferSize(size);
223 void SOCKSClientSocket::DoCallback(int result) {
224 DCHECK_NE(ERR_IO_PENDING, result);
225 DCHECK(!user_callback_.is_null());
227 // Since Run() may result in Read being called,
228 // clear user_callback_ up front.
229 DVLOG(1) << "Finished setting up SOCKS handshake";
230 base::ResetAndReturn(&user_callback_).Run(result);
233 void SOCKSClientSocket::OnIOComplete(int result) {
234 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436634 is fixed.
235 tracked_objects::ScopedTracker tracking_profile(
236 FROM_HERE_WITH_EXPLICIT_FUNCTION(
237 "436634 SOCKSClientSocket::OnIOComplete"));
239 DCHECK_NE(STATE_NONE, next_state_);
240 int rv = DoLoop(result);
241 if (rv != ERR_IO_PENDING) {
242 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SOCKS_CONNECT, rv);
243 DoCallback(rv);
247 void SOCKSClientSocket::OnReadWriteComplete(const CompletionCallback& callback,
248 int result) {
249 DCHECK_NE(ERR_IO_PENDING, result);
250 DCHECK(!callback.is_null());
252 if (result > 0)
253 was_ever_used_ = true;
254 callback.Run(result);
257 int SOCKSClientSocket::DoLoop(int last_io_result) {
258 DCHECK_NE(next_state_, STATE_NONE);
259 int rv = last_io_result;
260 do {
261 State state = next_state_;
262 next_state_ = STATE_NONE;
263 switch (state) {
264 case STATE_RESOLVE_HOST:
265 DCHECK_EQ(OK, rv);
266 rv = DoResolveHost();
267 break;
268 case STATE_RESOLVE_HOST_COMPLETE:
269 rv = DoResolveHostComplete(rv);
270 break;
271 case STATE_HANDSHAKE_WRITE:
272 DCHECK_EQ(OK, rv);
273 rv = DoHandshakeWrite();
274 break;
275 case STATE_HANDSHAKE_WRITE_COMPLETE:
276 rv = DoHandshakeWriteComplete(rv);
277 break;
278 case STATE_HANDSHAKE_READ:
279 DCHECK_EQ(OK, rv);
280 rv = DoHandshakeRead();
281 break;
282 case STATE_HANDSHAKE_READ_COMPLETE:
283 rv = DoHandshakeReadComplete(rv);
284 break;
285 default:
286 NOTREACHED() << "bad state";
287 rv = ERR_UNEXPECTED;
288 break;
290 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
291 return rv;
294 int SOCKSClientSocket::DoResolveHost() {
295 next_state_ = STATE_RESOLVE_HOST_COMPLETE;
296 // SOCKS4 only supports IPv4 addresses, so only try getting the IPv4
297 // addresses for the target host.
298 host_request_info_.set_address_family(ADDRESS_FAMILY_IPV4);
299 return host_resolver_.Resolve(
300 host_request_info_,
301 priority_,
302 &addresses_,
303 base::Bind(&SOCKSClientSocket::OnIOComplete, base::Unretained(this)),
304 net_log_);
307 int SOCKSClientSocket::DoResolveHostComplete(int result) {
308 if (result != OK) {
309 // Resolving the hostname failed; fail the request rather than automatically
310 // falling back to SOCKS4a (since it can be confusing to see invalid IP
311 // addresses being sent to the SOCKS4 server when it doesn't support 4A.)
312 return result;
315 next_state_ = STATE_HANDSHAKE_WRITE;
316 return OK;
319 // Builds the buffer that is to be sent to the server.
320 const std::string SOCKSClientSocket::BuildHandshakeWriteBuffer() const {
321 SOCKS4ServerRequest request;
322 request.version = kSOCKSVersion4;
323 request.command = kSOCKSStreamRequest;
324 request.nw_port = base::HostToNet16(host_request_info_.port());
326 DCHECK(!addresses_.empty());
327 const IPEndPoint& endpoint = addresses_.front();
329 // We disabled IPv6 results when resolving the hostname, so none of the
330 // results in the list will be IPv6.
331 // TODO(eroman): we only ever use the first address in the list. It would be
332 // more robust to try all the IP addresses we have before
333 // failing the connect attempt.
334 CHECK_EQ(ADDRESS_FAMILY_IPV4, endpoint.GetFamily());
335 CHECK_LE(endpoint.address().size(), sizeof(request.ip));
336 memcpy(&request.ip, &endpoint.address()[0], endpoint.address().size());
338 DVLOG(1) << "Resolved Host is : " << endpoint.ToStringWithoutPort();
340 std::string handshake_data(reinterpret_cast<char*>(&request),
341 sizeof(request));
342 handshake_data.append(kEmptyUserId, arraysize(kEmptyUserId));
344 return handshake_data;
347 // Writes the SOCKS handshake data to the underlying socket connection.
348 int SOCKSClientSocket::DoHandshakeWrite() {
349 next_state_ = STATE_HANDSHAKE_WRITE_COMPLETE;
351 if (buffer_.empty()) {
352 buffer_ = BuildHandshakeWriteBuffer();
353 bytes_sent_ = 0;
356 int handshake_buf_len = buffer_.size() - bytes_sent_;
357 DCHECK_GT(handshake_buf_len, 0);
358 handshake_buf_ = new IOBuffer(handshake_buf_len);
359 memcpy(handshake_buf_->data(), &buffer_[bytes_sent_],
360 handshake_buf_len);
361 return transport_->socket()->Write(
362 handshake_buf_.get(),
363 handshake_buf_len,
364 base::Bind(&SOCKSClientSocket::OnIOComplete, base::Unretained(this)));
367 int SOCKSClientSocket::DoHandshakeWriteComplete(int result) {
368 if (result < 0)
369 return result;
371 // We ignore the case when result is 0, since the underlying Write
372 // may return spurious writes while waiting on the socket.
374 bytes_sent_ += result;
375 if (bytes_sent_ == buffer_.size()) {
376 next_state_ = STATE_HANDSHAKE_READ;
377 buffer_.clear();
378 } else if (bytes_sent_ < buffer_.size()) {
379 next_state_ = STATE_HANDSHAKE_WRITE;
380 } else {
381 return ERR_UNEXPECTED;
384 return OK;
387 int SOCKSClientSocket::DoHandshakeRead() {
388 next_state_ = STATE_HANDSHAKE_READ_COMPLETE;
390 if (buffer_.empty()) {
391 bytes_received_ = 0;
394 int handshake_buf_len = kReadHeaderSize - bytes_received_;
395 handshake_buf_ = new IOBuffer(handshake_buf_len);
396 return transport_->socket()->Read(
397 handshake_buf_.get(),
398 handshake_buf_len,
399 base::Bind(&SOCKSClientSocket::OnIOComplete, base::Unretained(this)));
402 int SOCKSClientSocket::DoHandshakeReadComplete(int result) {
403 if (result < 0)
404 return result;
406 // The underlying socket closed unexpectedly.
407 if (result == 0)
408 return ERR_CONNECTION_CLOSED;
410 if (bytes_received_ + result > kReadHeaderSize) {
411 // TODO(eroman): Describe failure in NetLog.
412 return ERR_SOCKS_CONNECTION_FAILED;
415 buffer_.append(handshake_buf_->data(), result);
416 bytes_received_ += result;
417 if (bytes_received_ < kReadHeaderSize) {
418 next_state_ = STATE_HANDSHAKE_READ;
419 return OK;
422 const SOCKS4ServerResponse* response =
423 reinterpret_cast<const SOCKS4ServerResponse*>(buffer_.data());
425 if (response->reserved_null != 0x00) {
426 LOG(ERROR) << "Unknown response from SOCKS server.";
427 return ERR_SOCKS_CONNECTION_FAILED;
430 switch (response->code) {
431 case kServerResponseOk:
432 completed_handshake_ = true;
433 return OK;
434 case kServerResponseRejected:
435 LOG(ERROR) << "SOCKS request rejected or failed";
436 return ERR_SOCKS_CONNECTION_FAILED;
437 case kServerResponseNotReachable:
438 LOG(ERROR) << "SOCKS request failed because client is not running "
439 << "identd (or not reachable from the server)";
440 return ERR_SOCKS_CONNECTION_HOST_UNREACHABLE;
441 case kServerResponseMismatchedUserId:
442 LOG(ERROR) << "SOCKS request failed because client's identd could "
443 << "not confirm the user ID string in the request";
444 return ERR_SOCKS_CONNECTION_FAILED;
445 default:
446 LOG(ERROR) << "SOCKS server sent unknown response";
447 return ERR_SOCKS_CONNECTION_FAILED;
450 // Note: we ignore the last 6 bytes as specified by the SOCKS protocol
453 int SOCKSClientSocket::GetPeerAddress(IPEndPoint* address) const {
454 return transport_->socket()->GetPeerAddress(address);
457 int SOCKSClientSocket::GetLocalAddress(IPEndPoint* address) const {
458 return transport_->socket()->GetLocalAddress(address);
461 } // namespace net