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