[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" |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 14 | #include "net/log/net_log.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 15 | #include "net/log/net_log_event_type.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( |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 61 | std::unique_ptr<ClientSocketHandle> transport_socket, |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 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 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 92 | net_log_.BeginEvent(NetLogEventType::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 { |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 98 | net_log_.EndEventWithNetErrorCode(NetLogEventType::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; |
maksim.sisov | 320d596 | 2016-08-15 18:02:51 | [diff] [blame] | 105 | request_.reset(); |
[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 | |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame^] | 122 | const NetLogWithSource& SOCKSClientSocket::NetLog() const { |
[email protected] | e4be2dd | 2010-12-14 00:44:39 | [diff] [blame] | 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] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 146 | bool SOCKSClientSocket::WasNpnNegotiated() const { |
| 147 | if (transport_.get() && transport_->socket()) { |
| 148 | return transport_->socket()->WasNpnNegotiated(); |
| 149 | } |
| 150 | NOTREACHED(); |
| 151 | return false; |
| 152 | } |
| 153 | |
[email protected] | 33661e48 | 2012-04-03 16:16:26 | [diff] [blame] | 154 | NextProto SOCKSClientSocket::GetNegotiatedProtocol() const { |
| 155 | if (transport_.get() && transport_->socket()) { |
| 156 | return transport_->socket()->GetNegotiatedProtocol(); |
| 157 | } |
| 158 | NOTREACHED(); |
| 159 | return kProtoUnknown; |
| 160 | } |
| 161 | |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 162 | bool SOCKSClientSocket::GetSSLInfo(SSLInfo* ssl_info) { |
| 163 | if (transport_.get() && transport_->socket()) { |
| 164 | return transport_->socket()->GetSSLInfo(ssl_info); |
| 165 | } |
| 166 | NOTREACHED(); |
| 167 | return false; |
ttuttle | 23fdb7b | 2015-05-15 01:28:03 | [diff] [blame] | 168 | } |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 169 | |
ttuttle | 23fdb7b | 2015-05-15 01:28:03 | [diff] [blame] | 170 | void SOCKSClientSocket::GetConnectionAttempts(ConnectionAttempts* out) const { |
| 171 | out->clear(); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 172 | } |
| 173 | |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 174 | int64_t SOCKSClientSocket::GetTotalReceivedBytes() const { |
| 175 | return transport_->socket()->GetTotalReceivedBytes(); |
| 176 | } |
| 177 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 178 | // Read is called by the transport layer above to read. This can only be done |
| 179 | // if the SOCKS handshake is complete. |
| 180 | int SOCKSClientSocket::Read(IOBuffer* buf, int buf_len, |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 181 | const CompletionCallback& callback) { |
| 182 | DCHECK(completed_handshake_); |
| 183 | DCHECK_EQ(STATE_NONE, next_state_); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 184 | DCHECK(user_callback_.is_null()); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 185 | DCHECK(!callback.is_null()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 186 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 187 | int rv = transport_->socket()->Read( |
| 188 | buf, buf_len, |
| 189 | base::Bind(&SOCKSClientSocket::OnReadWriteComplete, |
| 190 | base::Unretained(this), callback)); |
| 191 | if (rv > 0) |
| 192 | was_ever_used_ = true; |
| 193 | return rv; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | // Write is called by the transport layer. This can only be done if the |
| 197 | // SOCKS handshake is complete. |
| 198 | int SOCKSClientSocket::Write(IOBuffer* buf, int buf_len, |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 199 | const CompletionCallback& callback) { |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 200 | DCHECK(completed_handshake_); |
| 201 | DCHECK_EQ(STATE_NONE, next_state_); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 202 | DCHECK(user_callback_.is_null()); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 203 | DCHECK(!callback.is_null()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 204 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 205 | int rv = transport_->socket()->Write( |
| 206 | buf, buf_len, |
| 207 | base::Bind(&SOCKSClientSocket::OnReadWriteComplete, |
| 208 | base::Unretained(this), callback)); |
| 209 | if (rv > 0) |
| 210 | was_ever_used_ = true; |
| 211 | return rv; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 212 | } |
| 213 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 214 | int SOCKSClientSocket::SetReceiveBufferSize(int32_t size) { |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 215 | return transport_->socket()->SetReceiveBufferSize(size); |
[email protected] | d3f6657 | 2009-09-09 22:38:04 | [diff] [blame] | 216 | } |
| 217 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 218 | int SOCKSClientSocket::SetSendBufferSize(int32_t size) { |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 219 | return transport_->socket()->SetSendBufferSize(size); |
[email protected] | d3f6657 | 2009-09-09 22:38:04 | [diff] [blame] | 220 | } |
| 221 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 222 | void SOCKSClientSocket::DoCallback(int result) { |
| 223 | DCHECK_NE(ERR_IO_PENDING, result); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 224 | DCHECK(!user_callback_.is_null()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 225 | |
| 226 | // Since Run() may result in Read being called, |
| 227 | // clear user_callback_ up front. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 228 | DVLOG(1) << "Finished setting up SOCKS handshake"; |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 229 | base::ResetAndReturn(&user_callback_).Run(result); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | void SOCKSClientSocket::OnIOComplete(int result) { |
| 233 | DCHECK_NE(STATE_NONE, next_state_); |
| 234 | int rv = DoLoop(result); |
[email protected] | 5a05c47a | 2009-11-02 23:25:19 | [diff] [blame] | 235 | if (rv != ERR_IO_PENDING) { |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 236 | net_log_.EndEventWithNetErrorCode(NetLogEventType::SOCKS_CONNECT, rv); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 237 | DoCallback(rv); |
[email protected] | 5a05c47a | 2009-11-02 23:25:19 | [diff] [blame] | 238 | } |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 239 | } |
| 240 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 241 | void SOCKSClientSocket::OnReadWriteComplete(const CompletionCallback& callback, |
| 242 | int result) { |
| 243 | DCHECK_NE(ERR_IO_PENDING, result); |
| 244 | DCHECK(!callback.is_null()); |
| 245 | |
| 246 | if (result > 0) |
| 247 | was_ever_used_ = true; |
| 248 | callback.Run(result); |
| 249 | } |
| 250 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 251 | int SOCKSClientSocket::DoLoop(int last_io_result) { |
| 252 | DCHECK_NE(next_state_, STATE_NONE); |
| 253 | int rv = last_io_result; |
| 254 | do { |
| 255 | State state = next_state_; |
| 256 | next_state_ = STATE_NONE; |
| 257 | switch (state) { |
| 258 | case STATE_RESOLVE_HOST: |
| 259 | DCHECK_EQ(OK, rv); |
| 260 | rv = DoResolveHost(); |
| 261 | break; |
| 262 | case STATE_RESOLVE_HOST_COMPLETE: |
| 263 | rv = DoResolveHostComplete(rv); |
| 264 | break; |
| 265 | case STATE_HANDSHAKE_WRITE: |
| 266 | DCHECK_EQ(OK, rv); |
| 267 | rv = DoHandshakeWrite(); |
| 268 | break; |
| 269 | case STATE_HANDSHAKE_WRITE_COMPLETE: |
| 270 | rv = DoHandshakeWriteComplete(rv); |
| 271 | break; |
| 272 | case STATE_HANDSHAKE_READ: |
| 273 | DCHECK_EQ(OK, rv); |
| 274 | rv = DoHandshakeRead(); |
| 275 | break; |
| 276 | case STATE_HANDSHAKE_READ_COMPLETE: |
| 277 | rv = DoHandshakeReadComplete(rv); |
| 278 | break; |
| 279 | default: |
| 280 | NOTREACHED() << "bad state"; |
| 281 | rv = ERR_UNEXPECTED; |
| 282 | break; |
| 283 | } |
| 284 | } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); |
| 285 | return rv; |
| 286 | } |
| 287 | |
| 288 | int SOCKSClientSocket::DoResolveHost() { |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 289 | next_state_ = STATE_RESOLVE_HOST_COMPLETE; |
[email protected] | 034d389 | 2011-03-29 04:07:21 | [diff] [blame] | 290 | // SOCKS4 only supports IPv4 addresses, so only try getting the IPv4 |
| 291 | // addresses for the target host. |
| 292 | host_request_info_.set_address_family(ADDRESS_FAMILY_IPV4); |
maksim.sisov | 320d596 | 2016-08-15 18:02:51 | [diff] [blame] | 293 | return host_resolver_->Resolve( |
| 294 | host_request_info_, priority_, &addresses_, |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 295 | base::Bind(&SOCKSClientSocket::OnIOComplete, base::Unretained(this)), |
maksim.sisov | 320d596 | 2016-08-15 18:02:51 | [diff] [blame] | 296 | &request_, net_log_); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | int SOCKSClientSocket::DoResolveHostComplete(int result) { |
[email protected] | 034d389 | 2011-03-29 04:07:21 | [diff] [blame] | 300 | if (result != OK) { |
| 301 | // Resolving the hostname failed; fail the request rather than automatically |
| 302 | // falling back to SOCKS4a (since it can be confusing to see invalid IP |
| 303 | // addresses being sent to the SOCKS4 server when it doesn't support 4A.) |
| 304 | return result; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 305 | } |
| 306 | |
[email protected] | 034d389 | 2011-03-29 04:07:21 | [diff] [blame] | 307 | next_state_ = STATE_HANDSHAKE_WRITE; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 308 | return OK; |
| 309 | } |
| 310 | |
| 311 | // Builds the buffer that is to be sent to the server. |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 312 | const std::string SOCKSClientSocket::BuildHandshakeWriteBuffer() const { |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 313 | SOCKS4ServerRequest request; |
| 314 | request.version = kSOCKSVersion4; |
| 315 | request.command = kSOCKSStreamRequest; |
[email protected] | 9eb7b11b | 2012-03-28 20:19:31 | [diff] [blame] | 316 | request.nw_port = base::HostToNet16(host_request_info_.port()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 317 | |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 318 | DCHECK(!addresses_.empty()); |
| 319 | const IPEndPoint& endpoint = addresses_.front(); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 320 | |
[email protected] | 034d389 | 2011-03-29 04:07:21 | [diff] [blame] | 321 | // We disabled IPv6 results when resolving the hostname, so none of the |
| 322 | // results in the list will be IPv6. |
| 323 | // TODO(eroman): we only ever use the first address in the list. It would be |
| 324 | // more robust to try all the IP addresses we have before |
| 325 | // failing the connect attempt. |
[email protected] | e466aaf | 2012-12-13 01:46:44 | [diff] [blame] | 326 | CHECK_EQ(ADDRESS_FAMILY_IPV4, endpoint.GetFamily()); |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 327 | CHECK_LE(endpoint.address().size(), sizeof(request.ip)); |
martijn | 98acb9b | 2016-01-27 07:14:07 | [diff] [blame] | 328 | memcpy(&request.ip, &endpoint.address().bytes()[0], |
| 329 | endpoint.address().size()); |
[email protected] | 034d389 | 2011-03-29 04:07:21 | [diff] [blame] | 330 | |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 331 | DVLOG(1) << "Resolved Host is : " << endpoint.ToStringWithoutPort(); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 332 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 333 | std::string handshake_data(reinterpret_cast<char*>(&request), |
| 334 | sizeof(request)); |
| 335 | handshake_data.append(kEmptyUserId, arraysize(kEmptyUserId)); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 336 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 337 | return handshake_data; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | // Writes the SOCKS handshake data to the underlying socket connection. |
| 341 | int SOCKSClientSocket::DoHandshakeWrite() { |
| 342 | next_state_ = STATE_HANDSHAKE_WRITE_COMPLETE; |
| 343 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 344 | if (buffer_.empty()) { |
| 345 | buffer_ = BuildHandshakeWriteBuffer(); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 346 | bytes_sent_ = 0; |
| 347 | } |
| 348 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 349 | int handshake_buf_len = buffer_.size() - bytes_sent_; |
| 350 | DCHECK_GT(handshake_buf_len, 0); |
| 351 | handshake_buf_ = new IOBuffer(handshake_buf_len); |
| 352 | memcpy(handshake_buf_->data(), &buffer_[bytes_sent_], |
| 353 | handshake_buf_len); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 354 | return transport_->socket()->Write( |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 355 | handshake_buf_.get(), |
| 356 | handshake_buf_len, |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 357 | base::Bind(&SOCKSClientSocket::OnIOComplete, base::Unretained(this))); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | int SOCKSClientSocket::DoHandshakeWriteComplete(int result) { |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 361 | if (result < 0) |
| 362 | return result; |
| 363 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 364 | // We ignore the case when result is 0, since the underlying Write |
| 365 | // may return spurious writes while waiting on the socket. |
| 366 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 367 | bytes_sent_ += result; |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 368 | if (bytes_sent_ == buffer_.size()) { |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 369 | next_state_ = STATE_HANDSHAKE_READ; |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 370 | buffer_.clear(); |
| 371 | } else if (bytes_sent_ < buffer_.size()) { |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 372 | next_state_ = STATE_HANDSHAKE_WRITE; |
| 373 | } else { |
| 374 | return ERR_UNEXPECTED; |
| 375 | } |
| 376 | |
| 377 | return OK; |
| 378 | } |
| 379 | |
| 380 | int SOCKSClientSocket::DoHandshakeRead() { |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 381 | next_state_ = STATE_HANDSHAKE_READ_COMPLETE; |
| 382 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 383 | if (buffer_.empty()) { |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 384 | bytes_received_ = 0; |
| 385 | } |
| 386 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 387 | int handshake_buf_len = kReadHeaderSize - bytes_received_; |
| 388 | handshake_buf_ = new IOBuffer(handshake_buf_len); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 389 | return transport_->socket()->Read( |
| 390 | handshake_buf_.get(), |
| 391 | handshake_buf_len, |
| 392 | base::Bind(&SOCKSClientSocket::OnIOComplete, base::Unretained(this))); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | int SOCKSClientSocket::DoHandshakeReadComplete(int result) { |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 396 | if (result < 0) |
| 397 | return result; |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 398 | |
| 399 | // The underlying socket closed unexpectedly. |
| 400 | if (result == 0) |
| 401 | return ERR_CONNECTION_CLOSED; |
| 402 | |
[email protected] | d5a30959 | 2010-02-05 02:22:52 | [diff] [blame] | 403 | if (bytes_received_ + result > kReadHeaderSize) { |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 404 | // TODO(eroman): Describe failure in NetLog. |
[email protected] | d5a30959 | 2010-02-05 02:22:52 | [diff] [blame] | 405 | return ERR_SOCKS_CONNECTION_FAILED; |
| 406 | } |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 407 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 408 | buffer_.append(handshake_buf_->data(), result); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 409 | bytes_received_ += result; |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 410 | if (bytes_received_ < kReadHeaderSize) { |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 411 | next_state_ = STATE_HANDSHAKE_READ; |
| 412 | return OK; |
| 413 | } |
| 414 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 415 | const SOCKS4ServerResponse* response = |
| 416 | reinterpret_cast<const SOCKS4ServerResponse*>(buffer_.data()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 417 | |
| 418 | if (response->reserved_null != 0x00) { |
mmenke | 99b5717 | 2016-04-14 20:44:33 | [diff] [blame] | 419 | DVLOG(1) << "Unknown response from SOCKS server."; |
[email protected] | d5a30959 | 2010-02-05 02:22:52 | [diff] [blame] | 420 | return ERR_SOCKS_CONNECTION_FAILED; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 421 | } |
| 422 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 423 | switch (response->code) { |
| 424 | case kServerResponseOk: |
| 425 | completed_handshake_ = true; |
| 426 | return OK; |
| 427 | case kServerResponseRejected: |
mmenke | 99b5717 | 2016-04-14 20:44:33 | [diff] [blame] | 428 | DVLOG(1) << "SOCKS request rejected or failed"; |
[email protected] | d5a30959 | 2010-02-05 02:22:52 | [diff] [blame] | 429 | return ERR_SOCKS_CONNECTION_FAILED; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 430 | case kServerResponseNotReachable: |
mmenke | 99b5717 | 2016-04-14 20:44:33 | [diff] [blame] | 431 | DVLOG(1) << "SOCKS request failed because client is not running " |
| 432 | << "identd (or not reachable from the server)"; |
[email protected] | d5a30959 | 2010-02-05 02:22:52 | [diff] [blame] | 433 | return ERR_SOCKS_CONNECTION_HOST_UNREACHABLE; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 434 | case kServerResponseMismatchedUserId: |
mmenke | 99b5717 | 2016-04-14 20:44:33 | [diff] [blame] | 435 | DVLOG(1) << "SOCKS request failed because client's identd could " |
| 436 | << "not confirm the user ID string in the request"; |
[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 | default: |
mmenke | 99b5717 | 2016-04-14 20:44:33 | [diff] [blame] | 439 | DVLOG(1) << "SOCKS server sent unknown response"; |
[email protected] | d5a30959 | 2010-02-05 02:22:52 | [diff] [blame] | 440 | return ERR_SOCKS_CONNECTION_FAILED; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | // Note: we ignore the last 6 bytes as specified by the SOCKS protocol |
| 444 | } |
| 445 | |
[email protected] | a352869 | 2012-06-08 00:11:42 | [diff] [blame] | 446 | int SOCKSClientSocket::GetPeerAddress(IPEndPoint* address) const { |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 447 | return transport_->socket()->GetPeerAddress(address); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 448 | } |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 449 | |
[email protected] | e7f74da | 2011-04-19 23:49:35 | [diff] [blame] | 450 | int SOCKSClientSocket::GetLocalAddress(IPEndPoint* address) const { |
| 451 | return transport_->socket()->GetLocalAddress(address); |
| 452 | } |
| 453 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 454 | } // namespace net |