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