1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/socket/socks_client_socket.h"
7 #include "base/basictypes.h"
9 #include "base/callback_helpers.h"
10 #include "base/compiler_specific.h"
11 #include "base/profiler/scoped_tracker.h"
12 #include "base/sys_byteorder.h"
13 #include "net/base/io_buffer.h"
14 #include "net/base/net_log.h"
15 #include "net/base/net_util.h"
16 #include "net/socket/client_socket_handle.h"
20 // Every SOCKS server requests a user-id from the client. It is optional
21 // and we send an empty string.
22 static const char kEmptyUserId
[] = "";
24 // For SOCKS4, the client sends 8 bytes plus the size of the user-id.
25 static const unsigned int kWriteHeaderSize
= 8;
27 // For SOCKS4 the server sends 8 bytes for acknowledgement.
28 static const unsigned int kReadHeaderSize
= 8;
30 // Server Response codes for SOCKS.
31 static const uint8 kServerResponseOk
= 0x5A;
32 static const uint8 kServerResponseRejected
= 0x5B;
33 static const uint8 kServerResponseNotReachable
= 0x5C;
34 static const uint8 kServerResponseMismatchedUserId
= 0x5D;
36 static const uint8 kSOCKSVersion4
= 0x04;
37 static const uint8 kSOCKSStreamRequest
= 0x01;
39 // A struct holding the essential details of the SOCKS4 Server Request.
40 // The port in the header is stored in network byte order.
41 struct SOCKS4ServerRequest
{
47 static_assert(sizeof(SOCKS4ServerRequest
) == kWriteHeaderSize
,
48 "socks4 server request struct has incorrect size");
50 // A struct holding details of the SOCKS4 Server Response.
51 struct SOCKS4ServerResponse
{
57 static_assert(sizeof(SOCKS4ServerResponse
) == kReadHeaderSize
,
58 "socks4 server response struct has incorrect size");
60 SOCKSClientSocket::SOCKSClientSocket(
61 scoped_ptr
<ClientSocketHandle
> transport_socket
,
62 const HostResolver::RequestInfo
& req_info
,
63 RequestPriority priority
,
64 HostResolver
* host_resolver
)
65 : transport_(transport_socket
.Pass()),
66 next_state_(STATE_NONE
),
67 completed_handshake_(false),
70 was_ever_used_(false),
71 host_resolver_(host_resolver
),
72 host_request_info_(req_info
),
74 net_log_(transport_
->socket()->NetLog()) {}
76 SOCKSClientSocket::~SOCKSClientSocket() {
80 int SOCKSClientSocket::Connect(const CompletionCallback
& callback
) {
81 DCHECK(transport_
.get());
82 DCHECK(transport_
->socket());
83 DCHECK_EQ(STATE_NONE
, next_state_
);
84 DCHECK(user_callback_
.is_null());
86 // If already connected, then just return OK.
87 if (completed_handshake_
)
90 next_state_
= STATE_RESOLVE_HOST
;
92 net_log_
.BeginEvent(NetLog::TYPE_SOCKS_CONNECT
);
95 if (rv
== ERR_IO_PENDING
) {
96 user_callback_
= callback
;
98 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_SOCKS_CONNECT
, rv
);
103 void SOCKSClientSocket::Disconnect() {
104 completed_handshake_
= false;
105 host_resolver_
.Cancel();
106 transport_
->socket()->Disconnect();
108 // Reset other states to make sure they aren't mistakenly used later.
109 // These are the states initialized by Connect().
110 next_state_
= STATE_NONE
;
111 user_callback_
.Reset();
114 bool SOCKSClientSocket::IsConnected() const {
115 return completed_handshake_
&& transport_
->socket()->IsConnected();
118 bool SOCKSClientSocket::IsConnectedAndIdle() const {
119 return completed_handshake_
&& transport_
->socket()->IsConnectedAndIdle();
122 const BoundNetLog
& SOCKSClientSocket::NetLog() const {
126 void SOCKSClientSocket::SetSubresourceSpeculation() {
127 if (transport_
.get() && transport_
->socket()) {
128 transport_
->socket()->SetSubresourceSpeculation();
134 void SOCKSClientSocket::SetOmniboxSpeculation() {
135 if (transport_
.get() && transport_
->socket()) {
136 transport_
->socket()->SetOmniboxSpeculation();
142 bool SOCKSClientSocket::WasEverUsed() const {
143 return was_ever_used_
;
146 bool SOCKSClientSocket::UsingTCPFastOpen() const {
147 if (transport_
.get() && transport_
->socket()) {
148 return transport_
->socket()->UsingTCPFastOpen();
154 bool SOCKSClientSocket::WasNpnNegotiated() const {
155 if (transport_
.get() && transport_
->socket()) {
156 return transport_
->socket()->WasNpnNegotiated();
162 NextProto
SOCKSClientSocket::GetNegotiatedProtocol() const {
163 if (transport_
.get() && transport_
->socket()) {
164 return transport_
->socket()->GetNegotiatedProtocol();
167 return kProtoUnknown
;
170 bool SOCKSClientSocket::GetSSLInfo(SSLInfo
* ssl_info
) {
171 if (transport_
.get() && transport_
->socket()) {
172 return transport_
->socket()->GetSSLInfo(ssl_info
);
179 // Read is called by the transport layer above to read. This can only be done
180 // if the SOCKS handshake is complete.
181 int SOCKSClientSocket::Read(IOBuffer
* buf
, int buf_len
,
182 const CompletionCallback
& callback
) {
183 DCHECK(completed_handshake_
);
184 DCHECK_EQ(STATE_NONE
, next_state_
);
185 DCHECK(user_callback_
.is_null());
186 DCHECK(!callback
.is_null());
188 int rv
= transport_
->socket()->Read(
190 base::Bind(&SOCKSClientSocket::OnReadWriteComplete
,
191 base::Unretained(this), callback
));
193 was_ever_used_
= true;
197 // Write is called by the transport layer. This can only be done if the
198 // SOCKS handshake is complete.
199 int SOCKSClientSocket::Write(IOBuffer
* buf
, int buf_len
,
200 const CompletionCallback
& callback
) {
201 DCHECK(completed_handshake_
);
202 DCHECK_EQ(STATE_NONE
, next_state_
);
203 DCHECK(user_callback_
.is_null());
204 DCHECK(!callback
.is_null());
206 int rv
= transport_
->socket()->Write(
208 base::Bind(&SOCKSClientSocket::OnReadWriteComplete
,
209 base::Unretained(this), callback
));
211 was_ever_used_
= true;
215 int SOCKSClientSocket::SetReceiveBufferSize(int32 size
) {
216 return transport_
->socket()->SetReceiveBufferSize(size
);
219 int SOCKSClientSocket::SetSendBufferSize(int32 size
) {
220 return transport_
->socket()->SetSendBufferSize(size
);
223 void SOCKSClientSocket::DoCallback(int result
) {
224 DCHECK_NE(ERR_IO_PENDING
, result
);
225 DCHECK(!user_callback_
.is_null());
227 // Since Run() may result in Read being called,
228 // clear user_callback_ up front.
229 DVLOG(1) << "Finished setting up SOCKS handshake";
230 base::ResetAndReturn(&user_callback_
).Run(result
);
233 void SOCKSClientSocket::OnIOComplete(int result
) {
234 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436634 is fixed.
235 tracked_objects::ScopedTracker
tracking_profile(
236 FROM_HERE_WITH_EXPLICIT_FUNCTION(
237 "436634 SOCKSClientSocket::OnIOComplete"));
239 DCHECK_NE(STATE_NONE
, next_state_
);
240 int rv
= DoLoop(result
);
241 if (rv
!= ERR_IO_PENDING
) {
242 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_SOCKS_CONNECT
, rv
);
247 void SOCKSClientSocket::OnReadWriteComplete(const CompletionCallback
& callback
,
249 DCHECK_NE(ERR_IO_PENDING
, result
);
250 DCHECK(!callback
.is_null());
253 was_ever_used_
= true;
254 callback
.Run(result
);
257 int SOCKSClientSocket::DoLoop(int last_io_result
) {
258 DCHECK_NE(next_state_
, STATE_NONE
);
259 int rv
= last_io_result
;
261 State state
= next_state_
;
262 next_state_
= STATE_NONE
;
264 case STATE_RESOLVE_HOST
:
266 rv
= DoResolveHost();
268 case STATE_RESOLVE_HOST_COMPLETE
:
269 rv
= DoResolveHostComplete(rv
);
271 case STATE_HANDSHAKE_WRITE
:
273 rv
= DoHandshakeWrite();
275 case STATE_HANDSHAKE_WRITE_COMPLETE
:
276 rv
= DoHandshakeWriteComplete(rv
);
278 case STATE_HANDSHAKE_READ
:
280 rv
= DoHandshakeRead();
282 case STATE_HANDSHAKE_READ_COMPLETE
:
283 rv
= DoHandshakeReadComplete(rv
);
286 NOTREACHED() << "bad state";
290 } while (rv
!= ERR_IO_PENDING
&& next_state_
!= STATE_NONE
);
294 int SOCKSClientSocket::DoResolveHost() {
295 next_state_
= STATE_RESOLVE_HOST_COMPLETE
;
296 // SOCKS4 only supports IPv4 addresses, so only try getting the IPv4
297 // addresses for the target host.
298 host_request_info_
.set_address_family(ADDRESS_FAMILY_IPV4
);
299 return host_resolver_
.Resolve(
303 base::Bind(&SOCKSClientSocket::OnIOComplete
, base::Unretained(this)),
307 int SOCKSClientSocket::DoResolveHostComplete(int result
) {
309 // Resolving the hostname failed; fail the request rather than automatically
310 // falling back to SOCKS4a (since it can be confusing to see invalid IP
311 // addresses being sent to the SOCKS4 server when it doesn't support 4A.)
315 next_state_
= STATE_HANDSHAKE_WRITE
;
319 // Builds the buffer that is to be sent to the server.
320 const std::string
SOCKSClientSocket::BuildHandshakeWriteBuffer() const {
321 SOCKS4ServerRequest request
;
322 request
.version
= kSOCKSVersion4
;
323 request
.command
= kSOCKSStreamRequest
;
324 request
.nw_port
= base::HostToNet16(host_request_info_
.port());
326 DCHECK(!addresses_
.empty());
327 const IPEndPoint
& endpoint
= addresses_
.front();
329 // We disabled IPv6 results when resolving the hostname, so none of the
330 // results in the list will be IPv6.
331 // TODO(eroman): we only ever use the first address in the list. It would be
332 // more robust to try all the IP addresses we have before
333 // failing the connect attempt.
334 CHECK_EQ(ADDRESS_FAMILY_IPV4
, endpoint
.GetFamily());
335 CHECK_LE(endpoint
.address().size(), sizeof(request
.ip
));
336 memcpy(&request
.ip
, &endpoint
.address()[0], endpoint
.address().size());
338 DVLOG(1) << "Resolved Host is : " << endpoint
.ToStringWithoutPort();
340 std::string
handshake_data(reinterpret_cast<char*>(&request
),
342 handshake_data
.append(kEmptyUserId
, arraysize(kEmptyUserId
));
344 return handshake_data
;
347 // Writes the SOCKS handshake data to the underlying socket connection.
348 int SOCKSClientSocket::DoHandshakeWrite() {
349 next_state_
= STATE_HANDSHAKE_WRITE_COMPLETE
;
351 if (buffer_
.empty()) {
352 buffer_
= BuildHandshakeWriteBuffer();
356 int handshake_buf_len
= buffer_
.size() - bytes_sent_
;
357 DCHECK_GT(handshake_buf_len
, 0);
358 handshake_buf_
= new IOBuffer(handshake_buf_len
);
359 memcpy(handshake_buf_
->data(), &buffer_
[bytes_sent_
],
361 return transport_
->socket()->Write(
362 handshake_buf_
.get(),
364 base::Bind(&SOCKSClientSocket::OnIOComplete
, base::Unretained(this)));
367 int SOCKSClientSocket::DoHandshakeWriteComplete(int result
) {
371 // We ignore the case when result is 0, since the underlying Write
372 // may return spurious writes while waiting on the socket.
374 bytes_sent_
+= result
;
375 if (bytes_sent_
== buffer_
.size()) {
376 next_state_
= STATE_HANDSHAKE_READ
;
378 } else if (bytes_sent_
< buffer_
.size()) {
379 next_state_
= STATE_HANDSHAKE_WRITE
;
381 return ERR_UNEXPECTED
;
387 int SOCKSClientSocket::DoHandshakeRead() {
388 next_state_
= STATE_HANDSHAKE_READ_COMPLETE
;
390 if (buffer_
.empty()) {
394 int handshake_buf_len
= kReadHeaderSize
- bytes_received_
;
395 handshake_buf_
= new IOBuffer(handshake_buf_len
);
396 return transport_
->socket()->Read(
397 handshake_buf_
.get(),
399 base::Bind(&SOCKSClientSocket::OnIOComplete
, base::Unretained(this)));
402 int SOCKSClientSocket::DoHandshakeReadComplete(int result
) {
406 // The underlying socket closed unexpectedly.
408 return ERR_CONNECTION_CLOSED
;
410 if (bytes_received_
+ result
> kReadHeaderSize
) {
411 // TODO(eroman): Describe failure in NetLog.
412 return ERR_SOCKS_CONNECTION_FAILED
;
415 buffer_
.append(handshake_buf_
->data(), result
);
416 bytes_received_
+= result
;
417 if (bytes_received_
< kReadHeaderSize
) {
418 next_state_
= STATE_HANDSHAKE_READ
;
422 const SOCKS4ServerResponse
* response
=
423 reinterpret_cast<const SOCKS4ServerResponse
*>(buffer_
.data());
425 if (response
->reserved_null
!= 0x00) {
426 LOG(ERROR
) << "Unknown response from SOCKS server.";
427 return ERR_SOCKS_CONNECTION_FAILED
;
430 switch (response
->code
) {
431 case kServerResponseOk
:
432 completed_handshake_
= true;
434 case kServerResponseRejected
:
435 LOG(ERROR
) << "SOCKS request rejected or failed";
436 return ERR_SOCKS_CONNECTION_FAILED
;
437 case kServerResponseNotReachable
:
438 LOG(ERROR
) << "SOCKS request failed because client is not running "
439 << "identd (or not reachable from the server)";
440 return ERR_SOCKS_CONNECTION_HOST_UNREACHABLE
;
441 case kServerResponseMismatchedUserId
:
442 LOG(ERROR
) << "SOCKS request failed because client's identd could "
443 << "not confirm the user ID string in the request";
444 return ERR_SOCKS_CONNECTION_FAILED
;
446 LOG(ERROR
) << "SOCKS server sent unknown response";
447 return ERR_SOCKS_CONNECTION_FAILED
;
450 // Note: we ignore the last 6 bytes as specified by the SOCKS protocol
453 int SOCKSClientSocket::GetPeerAddress(IPEndPoint
* address
) const {
454 return transport_
->socket()->GetPeerAddress(address
);
457 int SOCKSClientSocket::GetLocalAddress(IPEndPoint
* address
) const {
458 return transport_
->socket()->GetLocalAddress(address
);