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