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