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