[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 1 | // Copyright (c) 2010 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] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 8 | #include "base/compiler_specific.h" |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 9 | #include "net/base/io_buffer.h" |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 10 | #include "net/base/net_log.h" |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11 | #include "net/base/net_util.h" |
[email protected] | a540c2d | 2009-12-12 00:47:37 | [diff] [blame] | 12 | #include "net/base/sys_addrinfo.h" |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 13 | #include "net/socket/client_socket_handle.h" |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 14 | |
| 15 | namespace net { |
| 16 | |
| 17 | // Every SOCKS server requests a user-id from the client. It is optional |
| 18 | // and we send an empty string. |
| 19 | static const char kEmptyUserId[] = ""; |
| 20 | |
| 21 | // The SOCKS4a implementation suggests to use an invalid IP in case the DNS |
| 22 | // resolution at client fails. |
| 23 | static const uint8 kInvalidIp[] = { 0, 0, 0, 127 }; |
| 24 | |
| 25 | // For SOCKS4, the client sends 8 bytes plus the size of the user-id. |
| 26 | // For SOCKS4A, this increases to accomodate the unresolved hostname. |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 27 | static const unsigned int kWriteHeaderSize = 8; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 28 | |
| 29 | // For SOCKS4 and SOCKS4a, the server sends 8 bytes for acknowledgement. |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 30 | static const unsigned int kReadHeaderSize = 8; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 31 | |
| 32 | // Server Response codes for SOCKS. |
| 33 | static const uint8 kServerResponseOk = 0x5A; |
| 34 | static const uint8 kServerResponseRejected = 0x5B; |
| 35 | static const uint8 kServerResponseNotReachable = 0x5C; |
| 36 | static const uint8 kServerResponseMismatchedUserId = 0x5D; |
| 37 | |
| 38 | static const uint8 kSOCKSVersion4 = 0x04; |
| 39 | static const uint8 kSOCKSStreamRequest = 0x01; |
| 40 | |
| 41 | // A struct holding the essential details of the SOCKS4/4a Server Request. |
| 42 | // The port in the header is stored in network byte order. |
| 43 | struct SOCKS4ServerRequest { |
| 44 | uint8 version; |
| 45 | uint8 command; |
| 46 | uint16 nw_port; |
| 47 | uint8 ip[4]; |
| 48 | }; |
| 49 | COMPILE_ASSERT(sizeof(SOCKS4ServerRequest) == kWriteHeaderSize, |
| 50 | socks4_server_request_struct_wrong_size); |
| 51 | |
| 52 | // A struct holding details of the SOCKS4/4a Server Response. |
| 53 | struct SOCKS4ServerResponse { |
| 54 | uint8 reserved_null; |
| 55 | uint8 code; |
| 56 | uint16 port; |
| 57 | uint8 ip[4]; |
| 58 | }; |
| 59 | COMPILE_ASSERT(sizeof(SOCKS4ServerResponse) == kReadHeaderSize, |
| 60 | socks4_server_response_struct_wrong_size); |
| 61 | |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 62 | SOCKSClientSocket::SOCKSClientSocket(ClientSocketHandle* transport_socket, |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 63 | const HostResolver::RequestInfo& req_info, |
| 64 | HostResolver* host_resolver) |
| 65 | : ALLOW_THIS_IN_INITIALIZER_LIST( |
| 66 | io_callback_(this, &SOCKSClientSocket::OnIOComplete)), |
| 67 | transport_(transport_socket), |
| 68 | next_state_(STATE_NONE), |
| 69 | socks_version_(kSOCKS4Unresolved), |
| 70 | user_callback_(NULL), |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 71 | completed_handshake_(false), |
| 72 | bytes_sent_(0), |
| 73 | bytes_received_(0), |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 74 | host_resolver_(host_resolver), |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 75 | host_request_info_(req_info), |
| 76 | net_log_(transport_socket->socket()->NetLog()) { |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 77 | } |
| 78 | |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 79 | SOCKSClientSocket::SOCKSClientSocket(ClientSocket* transport_socket, |
| 80 | const HostResolver::RequestInfo& req_info, |
| 81 | HostResolver* host_resolver) |
| 82 | : ALLOW_THIS_IN_INITIALIZER_LIST( |
| 83 | io_callback_(this, &SOCKSClientSocket::OnIOComplete)), |
| 84 | transport_(new ClientSocketHandle()), |
| 85 | next_state_(STATE_NONE), |
| 86 | socks_version_(kSOCKS4Unresolved), |
| 87 | user_callback_(NULL), |
| 88 | completed_handshake_(false), |
| 89 | bytes_sent_(0), |
| 90 | bytes_received_(0), |
| 91 | host_resolver_(host_resolver), |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 92 | host_request_info_(req_info), |
| 93 | net_log_(transport_socket->NetLog()) { |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 94 | transport_->set_socket(transport_socket); |
| 95 | } |
| 96 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 97 | SOCKSClientSocket::~SOCKSClientSocket() { |
| 98 | Disconnect(); |
| 99 | } |
| 100 | |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 101 | int SOCKSClientSocket::Connect(CompletionCallback* callback) { |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 102 | DCHECK(transport_.get()); |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 103 | DCHECK(transport_->socket()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 104 | DCHECK_EQ(STATE_NONE, next_state_); |
| 105 | DCHECK(!user_callback_); |
| 106 | |
| 107 | // If already connected, then just return OK. |
| 108 | if (completed_handshake_) |
| 109 | return OK; |
| 110 | |
| 111 | next_state_ = STATE_RESOLVE_HOST; |
[email protected] | 5a05c47a | 2009-11-02 23:25:19 | [diff] [blame] | 112 | |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 113 | net_log_.BeginEvent(NetLog::TYPE_SOCKS_CONNECT, NULL); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 114 | |
| 115 | int rv = DoLoop(OK); |
[email protected] | 5a05c47a | 2009-11-02 23:25:19 | [diff] [blame] | 116 | if (rv == ERR_IO_PENDING) { |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 117 | user_callback_ = callback; |
[email protected] | 5a05c47a | 2009-11-02 23:25:19 | [diff] [blame] | 118 | } else { |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 119 | net_log_.EndEvent(NetLog::TYPE_SOCKS_CONNECT, NULL); |
[email protected] | 5a05c47a | 2009-11-02 23:25:19 | [diff] [blame] | 120 | } |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 121 | return rv; |
| 122 | } |
| 123 | |
| 124 | void SOCKSClientSocket::Disconnect() { |
| 125 | completed_handshake_ = false; |
[email protected] | 16a0274 | 2010-01-07 22:50:10 | [diff] [blame] | 126 | host_resolver_.Cancel(); |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 127 | transport_->socket()->Disconnect(); |
[email protected] | 16a0274 | 2010-01-07 22:50:10 | [diff] [blame] | 128 | |
| 129 | // Reset other states to make sure they aren't mistakenly used later. |
| 130 | // These are the states initialized by Connect(). |
| 131 | next_state_ = STATE_NONE; |
| 132 | user_callback_ = NULL; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | bool SOCKSClientSocket::IsConnected() const { |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 136 | return completed_handshake_ && transport_->socket()->IsConnected(); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | bool SOCKSClientSocket::IsConnectedAndIdle() const { |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 140 | return completed_handshake_ && transport_->socket()->IsConnectedAndIdle(); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 141 | } |
| 142 | |
[email protected] | 9b5614a | 2010-08-25 20:29:45 | [diff] [blame] | 143 | void SOCKSClientSocket::SetSubresourceSpeculation() { |
| 144 | if (transport_.get() && transport_->socket()) { |
| 145 | transport_->socket()->SetSubresourceSpeculation(); |
| 146 | } else { |
| 147 | NOTREACHED(); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void SOCKSClientSocket::SetOmniboxSpeculation() { |
| 152 | if (transport_.get() && transport_->socket()) { |
| 153 | transport_->socket()->SetOmniboxSpeculation(); |
| 154 | } else { |
| 155 | NOTREACHED(); |
| 156 | } |
| 157 | } |
| 158 | |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 159 | bool SOCKSClientSocket::WasEverUsed() const { |
| 160 | if (transport_.get() && transport_->socket()) { |
| 161 | return transport_->socket()->WasEverUsed(); |
| 162 | } |
| 163 | NOTREACHED(); |
| 164 | return false; |
| 165 | } |
| 166 | |
[email protected] | 7f7e9239 | 2010-10-26 18:29:29 | [diff] [blame^] | 167 | bool SOCKSClientSocket::UsingTCPFastOpen() const { |
| 168 | if (transport_.get() && transport_->socket()) { |
| 169 | return transport_->socket()->UsingTCPFastOpen(); |
| 170 | } |
| 171 | NOTREACHED(); |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 176 | // Read is called by the transport layer above to read. This can only be done |
| 177 | // if the SOCKS handshake is complete. |
| 178 | int SOCKSClientSocket::Read(IOBuffer* buf, int buf_len, |
| 179 | CompletionCallback* callback) { |
| 180 | DCHECK(completed_handshake_); |
| 181 | DCHECK_EQ(STATE_NONE, next_state_); |
| 182 | DCHECK(!user_callback_); |
| 183 | |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 184 | return transport_->socket()->Read(buf, buf_len, callback); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | // Write is called by the transport layer. This can only be done if the |
| 188 | // SOCKS handshake is complete. |
| 189 | int SOCKSClientSocket::Write(IOBuffer* buf, int buf_len, |
| 190 | CompletionCallback* callback) { |
| 191 | DCHECK(completed_handshake_); |
| 192 | DCHECK_EQ(STATE_NONE, next_state_); |
| 193 | DCHECK(!user_callback_); |
| 194 | |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 195 | return transport_->socket()->Write(buf, buf_len, callback); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 196 | } |
| 197 | |
[email protected] | d3f6657 | 2009-09-09 22:38:04 | [diff] [blame] | 198 | bool SOCKSClientSocket::SetReceiveBufferSize(int32 size) { |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 199 | return transport_->socket()->SetReceiveBufferSize(size); |
[email protected] | d3f6657 | 2009-09-09 22:38:04 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | bool SOCKSClientSocket::SetSendBufferSize(int32 size) { |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 203 | return transport_->socket()->SetSendBufferSize(size); |
[email protected] | d3f6657 | 2009-09-09 22:38:04 | [diff] [blame] | 204 | } |
| 205 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 206 | void SOCKSClientSocket::DoCallback(int result) { |
| 207 | DCHECK_NE(ERR_IO_PENDING, result); |
| 208 | DCHECK(user_callback_); |
| 209 | |
| 210 | // Since Run() may result in Read being called, |
| 211 | // clear user_callback_ up front. |
| 212 | CompletionCallback* c = user_callback_; |
| 213 | user_callback_ = NULL; |
[email protected] | b30a3f5 | 2010-10-16 01:05:46 | [diff] [blame] | 214 | DVLOG(1) << "Finished setting up SOCKS handshake"; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 215 | c->Run(result); |
| 216 | } |
| 217 | |
| 218 | void SOCKSClientSocket::OnIOComplete(int result) { |
| 219 | DCHECK_NE(STATE_NONE, next_state_); |
| 220 | int rv = DoLoop(result); |
[email protected] | 5a05c47a | 2009-11-02 23:25:19 | [diff] [blame] | 221 | if (rv != ERR_IO_PENDING) { |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 222 | net_log_.EndEvent(NetLog::TYPE_SOCKS_CONNECT, NULL); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 223 | DoCallback(rv); |
[email protected] | 5a05c47a | 2009-11-02 23:25:19 | [diff] [blame] | 224 | } |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | int SOCKSClientSocket::DoLoop(int last_io_result) { |
| 228 | DCHECK_NE(next_state_, STATE_NONE); |
| 229 | int rv = last_io_result; |
| 230 | do { |
| 231 | State state = next_state_; |
| 232 | next_state_ = STATE_NONE; |
| 233 | switch (state) { |
| 234 | case STATE_RESOLVE_HOST: |
| 235 | DCHECK_EQ(OK, rv); |
| 236 | rv = DoResolveHost(); |
| 237 | break; |
| 238 | case STATE_RESOLVE_HOST_COMPLETE: |
| 239 | rv = DoResolveHostComplete(rv); |
| 240 | break; |
| 241 | case STATE_HANDSHAKE_WRITE: |
| 242 | DCHECK_EQ(OK, rv); |
| 243 | rv = DoHandshakeWrite(); |
| 244 | break; |
| 245 | case STATE_HANDSHAKE_WRITE_COMPLETE: |
| 246 | rv = DoHandshakeWriteComplete(rv); |
| 247 | break; |
| 248 | case STATE_HANDSHAKE_READ: |
| 249 | DCHECK_EQ(OK, rv); |
| 250 | rv = DoHandshakeRead(); |
| 251 | break; |
| 252 | case STATE_HANDSHAKE_READ_COMPLETE: |
| 253 | rv = DoHandshakeReadComplete(rv); |
| 254 | break; |
| 255 | default: |
| 256 | NOTREACHED() << "bad state"; |
| 257 | rv = ERR_UNEXPECTED; |
| 258 | break; |
| 259 | } |
| 260 | } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); |
| 261 | return rv; |
| 262 | } |
| 263 | |
| 264 | int SOCKSClientSocket::DoResolveHost() { |
| 265 | DCHECK_EQ(kSOCKS4Unresolved, socks_version_); |
| 266 | |
| 267 | next_state_ = STATE_RESOLVE_HOST_COMPLETE; |
[email protected] | ec08bb2 | 2009-08-12 00:25:12 | [diff] [blame] | 268 | return host_resolver_.Resolve( |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 269 | host_request_info_, &addresses_, &io_callback_, net_log_); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | int SOCKSClientSocket::DoResolveHostComplete(int result) { |
| 273 | DCHECK_EQ(kSOCKS4Unresolved, socks_version_); |
| 274 | |
| 275 | bool ok = (result == OK); |
| 276 | next_state_ = STATE_HANDSHAKE_WRITE; |
| 277 | if (ok) { |
| 278 | DCHECK(addresses_.head()); |
| 279 | |
| 280 | // If the host is resolved to an IPv6 address, we revert to SOCKS4a |
| 281 | // since IPv6 is unsupported by SOCKS4/4a protocol. |
| 282 | struct sockaddr *host_info = addresses_.head()->ai_addr; |
| 283 | if (host_info->sa_family == AF_INET) { |
[email protected] | b30a3f5 | 2010-10-16 01:05:46 | [diff] [blame] | 284 | DVLOG(1) << "Resolved host. Using SOCKS4 to communicate"; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 285 | socks_version_ = kSOCKS4; |
| 286 | } else { |
[email protected] | b30a3f5 | 2010-10-16 01:05:46 | [diff] [blame] | 287 | DVLOG(1) << "Resolved host but to IPv6. Using SOCKS4a to communicate"; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 288 | socks_version_ = kSOCKS4a; |
| 289 | } |
| 290 | } else { |
[email protected] | b30a3f5 | 2010-10-16 01:05:46 | [diff] [blame] | 291 | DVLOG(1) << "Could not resolve host. Using SOCKS4a to communicate"; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 292 | socks_version_ = kSOCKS4a; |
| 293 | } |
| 294 | |
| 295 | // Even if DNS resolution fails, we send OK since the server |
| 296 | // resolves the domain. |
| 297 | return OK; |
| 298 | } |
| 299 | |
| 300 | // Builds the buffer that is to be sent to the server. |
| 301 | // We check whether the SOCKS proxy is 4 or 4A. |
| 302 | // In case it is 4A, the record size increases by size of the hostname. |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 303 | const std::string SOCKSClientSocket::BuildHandshakeWriteBuffer() const { |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 304 | DCHECK_NE(kSOCKS4Unresolved, socks_version_); |
| 305 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 306 | SOCKS4ServerRequest request; |
| 307 | request.version = kSOCKSVersion4; |
| 308 | request.command = kSOCKSStreamRequest; |
| 309 | request.nw_port = htons(host_request_info_.port()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 310 | |
| 311 | if (socks_version_ == kSOCKS4) { |
| 312 | const struct addrinfo* ai = addresses_.head(); |
| 313 | DCHECK(ai); |
| 314 | // If the sockaddr is IPv6, we have already marked the version to socks4a |
| 315 | // and so this step does not get hit. |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 316 | struct sockaddr_in* ipv4_host = |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 317 | reinterpret_cast<struct sockaddr_in*>(ai->ai_addr); |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 318 | memcpy(&request.ip, &(ipv4_host->sin_addr), sizeof(ipv4_host->sin_addr)); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 319 | |
[email protected] | b30a3f5 | 2010-10-16 01:05:46 | [diff] [blame] | 320 | DVLOG(1) << "Resolved Host is : " << NetAddressToString(ai); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 321 | } else if (socks_version_ == kSOCKS4a) { |
| 322 | // invalid IP of the form 0.0.0.127 |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 323 | memcpy(&request.ip, kInvalidIp, arraysize(kInvalidIp)); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 324 | } else { |
| 325 | NOTREACHED(); |
| 326 | } |
| 327 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 328 | std::string handshake_data(reinterpret_cast<char*>(&request), |
| 329 | sizeof(request)); |
| 330 | handshake_data.append(kEmptyUserId, arraysize(kEmptyUserId)); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 331 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 332 | // In case we are passing the domain also, pass the hostname |
| 333 | // terminated with a null character. |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 334 | if (socks_version_ == kSOCKS4a) { |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 335 | handshake_data.append(host_request_info_.hostname()); |
| 336 | handshake_data.push_back('\0'); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 337 | } |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 338 | |
| 339 | return handshake_data; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | // Writes the SOCKS handshake data to the underlying socket connection. |
| 343 | int SOCKSClientSocket::DoHandshakeWrite() { |
| 344 | next_state_ = STATE_HANDSHAKE_WRITE_COMPLETE; |
| 345 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 346 | if (buffer_.empty()) { |
| 347 | buffer_ = BuildHandshakeWriteBuffer(); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 348 | bytes_sent_ = 0; |
| 349 | } |
| 350 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 351 | int handshake_buf_len = buffer_.size() - bytes_sent_; |
| 352 | DCHECK_GT(handshake_buf_len, 0); |
| 353 | handshake_buf_ = new IOBuffer(handshake_buf_len); |
| 354 | memcpy(handshake_buf_->data(), &buffer_[bytes_sent_], |
| 355 | handshake_buf_len); |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 356 | return transport_->socket()->Write(handshake_buf_, handshake_buf_len, |
| 357 | &io_callback_); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | int SOCKSClientSocket::DoHandshakeWriteComplete(int result) { |
| 361 | DCHECK_NE(kSOCKS4Unresolved, socks_version_); |
| 362 | |
| 363 | if (result < 0) |
| 364 | return result; |
| 365 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 366 | // We ignore the case when result is 0, since the underlying Write |
| 367 | // may return spurious writes while waiting on the socket. |
| 368 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 369 | bytes_sent_ += result; |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 370 | if (bytes_sent_ == buffer_.size()) { |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 371 | next_state_ = STATE_HANDSHAKE_READ; |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 372 | buffer_.clear(); |
| 373 | } else if (bytes_sent_ < buffer_.size()) { |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 374 | next_state_ = STATE_HANDSHAKE_WRITE; |
| 375 | } else { |
| 376 | return ERR_UNEXPECTED; |
| 377 | } |
| 378 | |
| 379 | return OK; |
| 380 | } |
| 381 | |
| 382 | int SOCKSClientSocket::DoHandshakeRead() { |
| 383 | DCHECK_NE(kSOCKS4Unresolved, socks_version_); |
| 384 | |
| 385 | next_state_ = STATE_HANDSHAKE_READ_COMPLETE; |
| 386 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 387 | if (buffer_.empty()) { |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 388 | bytes_received_ = 0; |
| 389 | } |
| 390 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 391 | int handshake_buf_len = kReadHeaderSize - bytes_received_; |
| 392 | handshake_buf_ = new IOBuffer(handshake_buf_len); |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 393 | return transport_->socket()->Read(handshake_buf_, handshake_buf_len, |
| 394 | &io_callback_); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | int SOCKSClientSocket::DoHandshakeReadComplete(int result) { |
| 398 | DCHECK_NE(kSOCKS4Unresolved, socks_version_); |
| 399 | |
| 400 | if (result < 0) |
| 401 | return result; |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 402 | |
| 403 | // The underlying socket closed unexpectedly. |
| 404 | if (result == 0) |
| 405 | return ERR_CONNECTION_CLOSED; |
| 406 | |
[email protected] | d5a30959 | 2010-02-05 02:22:52 | [diff] [blame] | 407 | if (bytes_received_ + result > kReadHeaderSize) { |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 408 | // TODO(eroman): Describe failure in NetLog. |
[email protected] | d5a30959 | 2010-02-05 02:22:52 | [diff] [blame] | 409 | return ERR_SOCKS_CONNECTION_FAILED; |
| 410 | } |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 411 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 412 | buffer_.append(handshake_buf_->data(), result); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 413 | bytes_received_ += result; |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 414 | if (bytes_received_ < kReadHeaderSize) { |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 415 | next_state_ = STATE_HANDSHAKE_READ; |
| 416 | return OK; |
| 417 | } |
| 418 | |
[email protected] | 76a51ac8 | 2009-06-28 07:58:58 | [diff] [blame] | 419 | const SOCKS4ServerResponse* response = |
| 420 | reinterpret_cast<const SOCKS4ServerResponse*>(buffer_.data()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 421 | |
| 422 | if (response->reserved_null != 0x00) { |
| 423 | LOG(ERROR) << "Unknown response from SOCKS server."; |
[email protected] | d5a30959 | 2010-02-05 02:22:52 | [diff] [blame] | 424 | return ERR_SOCKS_CONNECTION_FAILED; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 425 | } |
| 426 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 427 | switch (response->code) { |
| 428 | case kServerResponseOk: |
| 429 | completed_handshake_ = true; |
| 430 | return OK; |
| 431 | case kServerResponseRejected: |
| 432 | LOG(ERROR) << "SOCKS request rejected or failed"; |
[email protected] | d5a30959 | 2010-02-05 02:22:52 | [diff] [blame] | 433 | return ERR_SOCKS_CONNECTION_FAILED; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 434 | case kServerResponseNotReachable: |
| 435 | LOG(ERROR) << "SOCKS request failed because client is not running " |
| 436 | << "identd (or not reachable from the server)"; |
[email protected] | d5a30959 | 2010-02-05 02:22:52 | [diff] [blame] | 437 | return ERR_SOCKS_CONNECTION_HOST_UNREACHABLE; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 438 | case kServerResponseMismatchedUserId: |
| 439 | LOG(ERROR) << "SOCKS request failed because client's identd could " |
| 440 | << "not confirm the user ID string in the request"; |
[email protected] | d5a30959 | 2010-02-05 02:22:52 | [diff] [blame] | 441 | return ERR_SOCKS_CONNECTION_FAILED; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 442 | default: |
| 443 | LOG(ERROR) << "SOCKS server sent unknown response"; |
[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 | } |
| 446 | |
| 447 | // Note: we ignore the last 6 bytes as specified by the SOCKS protocol |
| 448 | } |
| 449 | |
[email protected] | ac9eec6 | 2010-02-20 18:50:38 | [diff] [blame] | 450 | int SOCKSClientSocket::GetPeerAddress(AddressList* address) const { |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 451 | return transport_->socket()->GetPeerAddress(address); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 452 | } |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 453 | |
| 454 | } // namespace net |