[email protected] | 9eb7b11b | 2012-03-28 20:19:31 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [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.h" |
| 6 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 9 | #include "base/bind.h" |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 10 | #include "base/callback_helpers.h" |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11 | #include "base/compiler_specific.h" |
[email protected] | 9eb7b11b | 2012-03-28 20:19:31 | [diff] [blame] | 12 | #include "base/sys_byteorder.h" |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 13 | #include "net/base/io_buffer.h" |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 14 | #include "net/base/net_util.h" |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 15 | #include "net/log/net_log.h" |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 16 | #include "net/socket/client_socket_handle.h" |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 17 | |
| 18 | namespace net { |
| 19 | |
| 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[] = ""; |
| 23 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 24 | // For SOCKS4, the client sends 8 bytes plus the size of the user-id. |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 25 | static const unsigned int kWriteHeaderSize = 8; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 26 | |
[email protected] | 034d389 | 2011-03-29 04:07:21 | [diff] [blame] | 27 | // For SOCKS4 the server sends 8 bytes for acknowledgement. |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 28 | static const unsigned int kReadHeaderSize = 8; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 29 | |
| 30 | // Server Response codes for SOCKS. |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 31 | static const uint8_t kServerResponseOk = 0x5A; |
| 32 | static const uint8_t kServerResponseRejected = 0x5B; |
| 33 | static const uint8_t kServerResponseNotReachable = 0x5C; |
| 34 | static const uint8_t kServerResponseMismatchedUserId = 0x5D; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 35 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 36 | static const uint8_t kSOCKSVersion4 = 0x04; |
| 37 | static const uint8_t kSOCKSStreamRequest = 0x01; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 38 | |
[email protected] | 034d389 | 2011-03-29 04:07:21 | [diff] [blame] | 39 | // A struct holding the essential details of the SOCKS4 Server Request. |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 40 | // The port in the header is stored in network byte order. |
| 41 | struct SOCKS4ServerRequest { |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 42 | uint8_t version; |
| 43 | uint8_t command; |
| 44 | uint16_t nw_port; |
| 45 | uint8_t ip[4]; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 46 | }; |
mostynb | 91e0da98 | 2015-01-20 19:17:27 | [diff] [blame] | 47 | static_assert(sizeof(SOCKS4ServerRequest) == kWriteHeaderSize, |
| 48 | "socks4 server request struct has incorrect size"); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 49 | |
[email protected] | 034d389 | 2011-03-29 04:07:21 | [diff] [blame] | 50 | // A struct holding details of the SOCKS4 Server Response. |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 51 | struct SOCKS4ServerResponse { |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 52 | uint8_t reserved_null; |
| 53 | uint8_t code; |
| 54 | uint16_t port; |
| 55 | uint8_t ip[4]; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 56 | }; |
mostynb | 91e0da98 | 2015-01-20 19:17:27 | [diff] [blame] | 57 | static_assert(sizeof(SOCKS4ServerResponse) == kReadHeaderSize, |
| 58 | "socks4 server response struct has incorrect size"); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 59 | |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 60 | SOCKSClientSocket::SOCKSClientSocket( |
| 61 | scoped_ptr<ClientSocketHandle> transport_socket, |
| 62 | const HostResolver::RequestInfo& req_info, |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 63 | RequestPriority priority, |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 64 | HostResolver* host_resolver) |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 65 | : transport_(std::move(transport_socket)), |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 66 | next_state_(STATE_NONE), |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 67 | completed_handshake_(false), |
| 68 | bytes_sent_(0), |
| 69 | bytes_received_(0), |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 70 | was_ever_used_(false), |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 71 | host_resolver_(host_resolver), |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 72 | host_request_info_(req_info), |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 73 | priority_(priority), |
| 74 | net_log_(transport_->socket()->NetLog()) {} |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 75 | |
| 76 | SOCKSClientSocket::~SOCKSClientSocket() { |
| 77 | Disconnect(); |
| 78 | } |
| 79 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 80 | int SOCKSClientSocket::Connect(const CompletionCallback& callback) { |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 81 | DCHECK(transport_.get()); |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 82 | DCHECK(transport_->socket()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 83 | DCHECK_EQ(STATE_NONE, next_state_); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 84 | DCHECK(user_callback_.is_null()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 85 | |
| 86 | // If already connected, then just return OK. |
| 87 | if (completed_handshake_) |
| 88 | return OK; |
| 89 | |
| 90 | next_state_ = STATE_RESOLVE_HOST; |
[email protected] | 5a05c47a | 2009-11-02 23:25:19 | [diff] [blame] | 91 | |
[email protected] | 3aa4af04 | 2012-06-14 21:02:31 | [diff] [blame] | 92 | net_log_.BeginEvent(NetLog::TYPE_SOCKS_CONNECT); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 93 | |
| 94 | int rv = DoLoop(OK); |
[email protected] | 5a05c47a | 2009-11-02 23:25:19 | [diff] [blame] | 95 | if (rv == ERR_IO_PENDING) { |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 96 | user_callback_ = callback; |
[email protected] | 5a05c47a | 2009-11-02 23:25:19 | [diff] [blame] | 97 | } else { |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 98 | net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SOCKS_CONNECT, rv); |
[email protected] | 5a05c47a | 2009-11-02 23:25:19 | [diff] [blame] | 99 | } |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 100 | return rv; |
| 101 | } |
| 102 | |
| 103 | void SOCKSClientSocket::Disconnect() { |
| 104 | completed_handshake_ = false; |
[email protected] | 16a0274 | 2010-01-07 22:50:10 | [diff] [blame] | 105 | host_resolver_.Cancel(); |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 106 | transport_->socket()->Disconnect(); |
[email protected] | 16a0274 | 2010-01-07 22:50:10 | [diff] [blame] | 107 | |
| 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; |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 111 | user_callback_.Reset(); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | bool SOCKSClientSocket::IsConnected() const { |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 115 | return completed_handshake_ && transport_->socket()->IsConnected(); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | bool SOCKSClientSocket::IsConnectedAndIdle() const { |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 119 | return completed_handshake_ && transport_->socket()->IsConnectedAndIdle(); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 120 | } |
| 121 | |
[email protected] | e4be2dd | 2010-12-14 00:44:39 | [diff] [blame] | 122 | const BoundNetLog& SOCKSClientSocket::NetLog() const { |
| 123 | return net_log_; |
| 124 | } |
| 125 | |
[email protected] | 9b5614a | 2010-08-25 20:29:45 | [diff] [blame] | 126 | void SOCKSClientSocket::SetSubresourceSpeculation() { |
| 127 | if (transport_.get() && transport_->socket()) { |
| 128 | transport_->socket()->SetSubresourceSpeculation(); |
| 129 | } else { |
| 130 | NOTREACHED(); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | void SOCKSClientSocket::SetOmniboxSpeculation() { |
| 135 | if (transport_.get() && transport_->socket()) { |
| 136 | transport_->socket()->SetOmniboxSpeculation(); |
| 137 | } else { |
| 138 | NOTREACHED(); |
| 139 | } |
| 140 | } |
| 141 | |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 142 | bool SOCKSClientSocket::WasEverUsed() const { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 143 | return was_ever_used_; |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 144 | } |
| 145 | |
[email protected] | 7f7e9239 | 2010-10-26 18:29:29 | [diff] [blame] | 146 | bool SOCKSClientSocket::UsingTCPFastOpen() const { |
| 147 | if (transport_.get() && transport_->socket()) { |
| 148 | return transport_->socket()->UsingTCPFastOpen(); |
| 149 | } |
| 150 | NOTREACHED(); |
| 151 | return false; |
| 152 | } |
| 153 | |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 154 | bool SOCKSClientSocket::WasNpnNegotiated() const { |
| 155 | if (transport_.get() && transport_->socket()) { |
| 156 | return transport_->socket()->WasNpnNegotiated(); |
| 157 | } |
| 158 | NOTREACHED(); |
| 159 | return false; |
| 160 | } |
| 161 | |
[email protected] | 33661e48 | 2012-04-03 16:16:26 | [diff] [blame] | 162 | NextProto SOCKSClientSocket::GetNegotiatedProtocol() const { |
| 163 | if (transport_.get() && transport_->socket()) { |
| 164 | return transport_->socket()->GetNegotiatedProtocol(); |
| 165 | } |
| 166 | NOTREACHED(); |
| 167 | return kProtoUnknown; |
| 168 | } |
| 169 | |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 170 | bool SOCKSClientSocket::GetSSLInfo(SSLInfo* ssl_info) { |
| 171 | if (transport_.get() && transport_->socket()) { |
| 172 | return transport_->socket()->GetSSLInfo(ssl_info); |
| 173 | } |
| 174 | NOTREACHED(); |
| 175 | return false; |
ttuttle | 23fdb7b | 2015-05-15 01:28:03 | [diff] [blame] | 176 | } |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 177 | |
ttuttle | 23fdb7b | 2015-05-15 01:28:03 | [diff] [blame] | 178 | void SOCKSClientSocket::GetConnectionAttempts(ConnectionAttempts* out) const { |
| 179 | out->clear(); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 180 | } |
| 181 | |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 182 | int64_t SOCKSClientSocket::GetTotalReceivedBytes() const { |
| 183 | return transport_->socket()->GetTotalReceivedBytes(); |
| 184 | } |
| 185 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 186 | // Read is called by the transport layer above to read. This can only be done |
| 187 | // if the SOCKS handshake is complete. |
| 188 | int SOCKSClientSocket::Read(IOBuffer* buf, int buf_len, |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 189 | const CompletionCallback& callback) { |
| 190 | DCHECK(completed_handshake_); |
| 191 | DCHECK_EQ(STATE_NONE, next_state_); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 192 | DCHECK(user_callback_.is_null()); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 193 | DCHECK(!callback.is_null()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 194 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 195 | int rv = transport_->socket()->Read( |
| 196 | buf, buf_len, |
| 197 | base::Bind(&SOCKSClientSocket::OnReadWriteComplete, |
| 198 | base::Unretained(this), callback)); |
| 199 | if (rv > 0) |
| 200 | was_ever_used_ = true; |
| 201 | return rv; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | // Write is called by the transport layer. This can only be done if the |
| 205 | // SOCKS handshake is complete. |
| 206 | int SOCKSClientSocket::Write(IOBuffer* buf, int buf_len, |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 207 | const CompletionCallback& callback) { |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 208 | DCHECK(completed_handshake_); |
| 209 | DCHECK_EQ(STATE_NONE, next_state_); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 210 | DCHECK(user_callback_.is_null()); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 211 | DCHECK(!callback.is_null()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 212 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 213 | int rv = transport_->socket()->Write( |
| 214 | buf, buf_len, |
| 215 | base::Bind(&SOCKSClientSocket::OnReadWriteComplete, |
| 216 | base::Unretained(this), callback)); |
| 217 | if (rv > 0) |
| 218 | was_ever_used_ = true; |
| 219 | return rv; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 220 | } |
| 221 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 222 | int SOCKSClientSocket::SetReceiveBufferSize(int32_t size) { |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 223 | return transport_->socket()->SetReceiveBufferSize(size); |
[email protected] | d3f6657 | 2009-09-09 22:38:04 | [diff] [blame] | 224 | } |
| 225 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 226 | int SOCKSClientSocket::SetSendBufferSize(int32_t size) { |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 227 | return transport_->socket()->SetSendBufferSize(size); |
[email protected] | d3f6657 | 2009-09-09 22:38:04 | [diff] [blame] | 228 | } |
| 229 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 230 | void SOCKSClientSocket::DoCallback(int result) { |
| 231 | DCHECK_NE(ERR_IO_PENDING, result); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 232 | DCHECK(!user_callback_.is_null()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 233 | |
| 234 | // Since Run() may result in Read being called, |
| 235 | // clear user_callback_ up front. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 236 | DVLOG(1) << "Finished setting up SOCKS handshake"; |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 237 | base::ResetAndReturn(&user_callback_).Run(result); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | void SOCKSClientSocket::OnIOComplete(int result) { |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 241 | DCHECK_NE(STATE_NONE, next_state_); |
| 242 | int rv = DoLoop(result); |
[email protected] | 5a05c47a | 2009-11-02 23:25:19 | [diff] [blame] | 243 | if (rv != ERR_IO_PENDING) { |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 244 | net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SOCKS_CONNECT, rv); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 245 | DoCallback(rv); |
[email protected] | 5a05c47a | 2009-11-02 23:25:19 | [diff] [blame] | 246 | } |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 247 | } |
| 248 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 249 | void SOCKSClientSocket::OnReadWriteComplete(const CompletionCallback& callback, |
| 250 | int result) { |
| 251 | DCHECK_NE(ERR_IO_PENDING, result); |
| 252 | DCHECK(!callback.is_null()); |
| 253 | |
| 254 | if (result > 0) |
| 255 | was_ever_used_ = true; |
| 256 | callback.Run(result); |
| 257 | } |
| 258 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 259 | int SOCKSClientSocket::DoLoop(int last_io_result) { |
| 260 | DCHECK_NE(next_state_, STATE_NONE); |
| 261 | int rv = last_io_result; |
| 262 | do { |
| 263 | State state = next_state_; |
| 264 | next_state_ = STATE_NONE; |
| 265 | switch (state) { |
| 266 | case STATE_RESOLVE_HOST: |
| 267 | DCHECK_EQ(OK, rv); |
| 268 | rv = DoResolveHost(); |
| 269 | break; |
| 270 | case STATE_RESOLVE_HOST_COMPLETE: |
| 271 | rv = DoResolveHostComplete(rv); |
| 272 | break; |
| 273 | case STATE_HANDSHAKE_WRITE: |
| 274 | DCHECK_EQ(OK, rv); |
| 275 | rv = DoHandshakeWrite(); |
| 276 | break; |
| 277 | case STATE_HANDSHAKE_WRITE_COMPLETE: |
| 278 | rv = DoHandshakeWriteComplete(rv); |
| 279 | break; |
| 280 | case STATE_HANDSHAKE_READ: |
| 281 | DCHECK_EQ(OK, rv); |
| 282 | rv = DoHandshakeRead(); |
| 283 | break; |
| 284 | case STATE_HANDSHAKE_READ_COMPLETE: |
| 285 | rv = DoHandshakeReadComplete(rv); |
| 286 | break; |
| 287 | default: |
| 288 | NOTREACHED() << "bad state"; |
| 289 | rv = ERR_UNEXPECTED; |
| 290 | break; |
| 291 | } |
| 292 | } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); |
| 293 | return rv; |
| 294 | } |
| 295 | |
| 296 | int SOCKSClientSocket::DoResolveHost() { |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 297 | next_state_ = STATE_RESOLVE_HOST_COMPLETE; |
[email protected] | 034d389 | 2011-03-29 04:07:21 | [diff] [blame] | 298 | // SOCKS4 only supports IPv4 addresses, so only try getting the IPv4 |
| 299 | // addresses for the target host. |
| 300 | host_request_info_.set_address_family(ADDRESS_FAMILY_IPV4); |
[email protected] | ec08bb2 | 2009-08-12 00:25:12 | [diff] [blame] | 301 | return host_resolver_.Resolve( |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 302 | host_request_info_, |
| 303 | priority_, |
| 304 | &addresses_, |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 305 | base::Bind(&SOCKSClientSocket::OnIOComplete, base::Unretained(this)), |
| 306 | net_log_); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | int SOCKSClientSocket::DoResolveHostComplete(int result) { |
[email protected] | 034d389 | 2011-03-29 04:07:21 | [diff] [blame] | 310 | if (result != OK) { |
| 311 | // Resolving the hostname failed; fail the request rather than automatically |
| 312 | // falling back to SOCKS4a (since it can be confusing to see invalid IP |
| 313 | // addresses being sent to the SOCKS4 server when it doesn't support 4A.) |
| 314 | return result; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 315 | } |
| 316 | |
[email protected] | 034d389 | 2011-03-29 04:07:21 | [diff] [blame] | 317 | next_state_ = STATE_HANDSHAKE_WRITE; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 318 | return OK; |
| 319 | } |
| 320 | |
| 321 | // Builds the buffer that is to be sent to the server. |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 322 | const std::string SOCKSClientSocket::BuildHandshakeWriteBuffer() const { |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 323 | SOCKS4ServerRequest request; |
| 324 | request.version = kSOCKSVersion4; |
| 325 | request.command = kSOCKSStreamRequest; |
[email protected] | 9eb7b11b | 2012-03-28 20:19:31 | [diff] [blame] | 326 | request.nw_port = base::HostToNet16(host_request_info_.port()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 327 | |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 328 | DCHECK(!addresses_.empty()); |
| 329 | const IPEndPoint& endpoint = addresses_.front(); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 330 | |
[email protected] | 034d389 | 2011-03-29 04:07:21 | [diff] [blame] | 331 | // We disabled IPv6 results when resolving the hostname, so none of the |
| 332 | // results in the list will be IPv6. |
| 333 | // TODO(eroman): we only ever use the first address in the list. It would be |
| 334 | // more robust to try all the IP addresses we have before |
| 335 | // failing the connect attempt. |
[email protected] | e466aaf | 2012-12-13 01:46:44 | [diff] [blame] | 336 | CHECK_EQ(ADDRESS_FAMILY_IPV4, endpoint.GetFamily()); |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 337 | CHECK_LE(endpoint.address().size(), sizeof(request.ip)); |
martijn | 98acb9b | 2016-01-27 07:14:07 | [diff] [blame^] | 338 | memcpy(&request.ip, &endpoint.address().bytes()[0], |
| 339 | endpoint.address().size()); |
[email protected] | 034d389 | 2011-03-29 04:07:21 | [diff] [blame] | 340 | |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 341 | DVLOG(1) << "Resolved Host is : " << endpoint.ToStringWithoutPort(); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 342 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 343 | std::string handshake_data(reinterpret_cast<char*>(&request), |
| 344 | sizeof(request)); |
| 345 | handshake_data.append(kEmptyUserId, arraysize(kEmptyUserId)); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 346 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 347 | return handshake_data; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | // Writes the SOCKS handshake data to the underlying socket connection. |
| 351 | int SOCKSClientSocket::DoHandshakeWrite() { |
| 352 | next_state_ = STATE_HANDSHAKE_WRITE_COMPLETE; |
| 353 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 354 | if (buffer_.empty()) { |
| 355 | buffer_ = BuildHandshakeWriteBuffer(); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 356 | bytes_sent_ = 0; |
| 357 | } |
| 358 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 359 | int handshake_buf_len = buffer_.size() - bytes_sent_; |
| 360 | DCHECK_GT(handshake_buf_len, 0); |
| 361 | handshake_buf_ = new IOBuffer(handshake_buf_len); |
| 362 | memcpy(handshake_buf_->data(), &buffer_[bytes_sent_], |
| 363 | handshake_buf_len); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 364 | return transport_->socket()->Write( |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 365 | handshake_buf_.get(), |
| 366 | handshake_buf_len, |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 367 | base::Bind(&SOCKSClientSocket::OnIOComplete, base::Unretained(this))); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | int SOCKSClientSocket::DoHandshakeWriteComplete(int result) { |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 371 | if (result < 0) |
| 372 | return result; |
| 373 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 374 | // We ignore the case when result is 0, since the underlying Write |
| 375 | // may return spurious writes while waiting on the socket. |
| 376 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 377 | bytes_sent_ += result; |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 378 | if (bytes_sent_ == buffer_.size()) { |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 379 | next_state_ = STATE_HANDSHAKE_READ; |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 380 | buffer_.clear(); |
| 381 | } else if (bytes_sent_ < buffer_.size()) { |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 382 | next_state_ = STATE_HANDSHAKE_WRITE; |
| 383 | } else { |
| 384 | return ERR_UNEXPECTED; |
| 385 | } |
| 386 | |
| 387 | return OK; |
| 388 | } |
| 389 | |
| 390 | int SOCKSClientSocket::DoHandshakeRead() { |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 391 | next_state_ = STATE_HANDSHAKE_READ_COMPLETE; |
| 392 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 393 | if (buffer_.empty()) { |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 394 | bytes_received_ = 0; |
| 395 | } |
| 396 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 397 | int handshake_buf_len = kReadHeaderSize - bytes_received_; |
| 398 | handshake_buf_ = new IOBuffer(handshake_buf_len); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 399 | return transport_->socket()->Read( |
| 400 | handshake_buf_.get(), |
| 401 | handshake_buf_len, |
| 402 | base::Bind(&SOCKSClientSocket::OnIOComplete, base::Unretained(this))); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | int SOCKSClientSocket::DoHandshakeReadComplete(int result) { |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 406 | if (result < 0) |
| 407 | return result; |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 408 | |
| 409 | // The underlying socket closed unexpectedly. |
| 410 | if (result == 0) |
| 411 | return ERR_CONNECTION_CLOSED; |
| 412 | |
[email protected] | d5a30959 | 2010-02-05 02:22:52 | [diff] [blame] | 413 | if (bytes_received_ + result > kReadHeaderSize) { |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 414 | // TODO(eroman): Describe failure in NetLog. |
[email protected] | d5a30959 | 2010-02-05 02:22:52 | [diff] [blame] | 415 | return ERR_SOCKS_CONNECTION_FAILED; |
| 416 | } |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 417 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 418 | buffer_.append(handshake_buf_->data(), result); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 419 | bytes_received_ += result; |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 420 | if (bytes_received_ < kReadHeaderSize) { |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 421 | next_state_ = STATE_HANDSHAKE_READ; |
| 422 | return OK; |
| 423 | } |
| 424 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 425 | const SOCKS4ServerResponse* response = |
| 426 | reinterpret_cast<const SOCKS4ServerResponse*>(buffer_.data()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 427 | |
| 428 | if (response->reserved_null != 0x00) { |
| 429 | LOG(ERROR) << "Unknown response from SOCKS server."; |
[email protected] | d5a30959 | 2010-02-05 02:22:52 | [diff] [blame] | 430 | return ERR_SOCKS_CONNECTION_FAILED; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 431 | } |
| 432 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 433 | switch (response->code) { |
| 434 | case kServerResponseOk: |
| 435 | completed_handshake_ = true; |
| 436 | return OK; |
| 437 | case kServerResponseRejected: |
| 438 | LOG(ERROR) << "SOCKS request rejected or failed"; |
[email protected] | d5a30959 | 2010-02-05 02:22:52 | [diff] [blame] | 439 | return ERR_SOCKS_CONNECTION_FAILED; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 440 | case kServerResponseNotReachable: |
| 441 | LOG(ERROR) << "SOCKS request failed because client is not running " |
| 442 | << "identd (or not reachable from the server)"; |
[email protected] | d5a30959 | 2010-02-05 02:22:52 | [diff] [blame] | 443 | return ERR_SOCKS_CONNECTION_HOST_UNREACHABLE; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 444 | case kServerResponseMismatchedUserId: |
| 445 | LOG(ERROR) << "SOCKS request failed because client's identd could " |
| 446 | << "not confirm the user ID string in the request"; |
[email protected] | d5a30959 | 2010-02-05 02:22:52 | [diff] [blame] | 447 | return ERR_SOCKS_CONNECTION_FAILED; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 448 | default: |
| 449 | LOG(ERROR) << "SOCKS server sent unknown response"; |
[email protected] | d5a30959 | 2010-02-05 02:22:52 | [diff] [blame] | 450 | return ERR_SOCKS_CONNECTION_FAILED; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | // Note: we ignore the last 6 bytes as specified by the SOCKS protocol |
| 454 | } |
| 455 | |
[email protected] | a352869 | 2012-06-08 00:11:42 | [diff] [blame] | 456 | int SOCKSClientSocket::GetPeerAddress(IPEndPoint* address) const { |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 457 | return transport_->socket()->GetPeerAddress(address); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 458 | } |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 459 | |
[email protected] | e7f74da | 2011-04-19 23:49:35 | [diff] [blame] | 460 | int SOCKSClientSocket::GetLocalAddress(IPEndPoint* address) const { |
| 461 | return transport_->socket()->GetLocalAddress(address); |
| 462 | } |
| 463 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 464 | } // namespace net |