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 #ifndef NET_SOCKET_SOCKS_CLIENT_SOCKET_H_
6 #define NET_SOCKET_SOCKS_CLIENT_SOCKET_H_
10 #include "base/basictypes.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "net/base/address_list.h"
15 #include "net/base/completion_callback.h"
16 #include "net/base/net_errors.h"
17 #include "net/base/net_log.h"
18 #include "net/dns/host_resolver.h"
19 #include "net/dns/single_request_host_resolver.h"
20 #include "net/socket/stream_socket.h"
24 class ClientSocketHandle
;
27 // The SOCKS client socket implementation
28 class NET_EXPORT_PRIVATE SOCKSClientSocket
: public StreamSocket
{
30 // |req_info| contains the hostname and port to which the socket above will
31 // communicate to via the socks layer. For testing the referrer is optional.
32 SOCKSClientSocket(scoped_ptr
<ClientSocketHandle
> transport_socket
,
33 const HostResolver::RequestInfo
& req_info
,
34 RequestPriority priority
,
35 HostResolver
* host_resolver
);
37 // On destruction Disconnect() is called.
38 virtual ~SOCKSClientSocket();
40 // StreamSocket implementation.
42 // Does the SOCKS handshake and completes the protocol.
43 virtual int Connect(const CompletionCallback
& callback
) OVERRIDE
;
44 virtual void Disconnect() OVERRIDE
;
45 virtual bool IsConnected() const OVERRIDE
;
46 virtual bool IsConnectedAndIdle() const OVERRIDE
;
47 virtual const BoundNetLog
& NetLog() const OVERRIDE
;
48 virtual void SetSubresourceSpeculation() OVERRIDE
;
49 virtual void SetOmniboxSpeculation() OVERRIDE
;
50 virtual bool WasEverUsed() const OVERRIDE
;
51 virtual bool UsingTCPFastOpen() const OVERRIDE
;
52 virtual bool WasNpnNegotiated() const OVERRIDE
;
53 virtual NextProto
GetNegotiatedProtocol() const OVERRIDE
;
54 virtual bool GetSSLInfo(SSLInfo
* ssl_info
) OVERRIDE
;
56 // Socket implementation.
57 virtual int Read(IOBuffer
* buf
,
59 const CompletionCallback
& callback
) OVERRIDE
;
60 virtual int Write(IOBuffer
* buf
,
62 const CompletionCallback
& callback
) OVERRIDE
;
64 virtual bool SetReceiveBufferSize(int32 size
) OVERRIDE
;
65 virtual bool SetSendBufferSize(int32 size
) OVERRIDE
;
67 virtual int GetPeerAddress(IPEndPoint
* address
) const OVERRIDE
;
68 virtual int GetLocalAddress(IPEndPoint
* address
) const OVERRIDE
;
71 FRIEND_TEST_ALL_PREFIXES(SOCKSClientSocketTest
, CompleteHandshake
);
72 FRIEND_TEST_ALL_PREFIXES(SOCKSClientSocketTest
, SOCKS4AFailedDNS
);
73 FRIEND_TEST_ALL_PREFIXES(SOCKSClientSocketTest
, SOCKS4AIfDomainInIPv6
);
77 STATE_RESOLVE_HOST_COMPLETE
,
78 STATE_HANDSHAKE_WRITE
,
79 STATE_HANDSHAKE_WRITE_COMPLETE
,
81 STATE_HANDSHAKE_READ_COMPLETE
,
85 void DoCallback(int result
);
86 void OnIOComplete(int result
);
88 int DoLoop(int last_io_result
);
90 int DoResolveHostComplete(int result
);
91 int DoHandshakeRead();
92 int DoHandshakeReadComplete(int result
);
93 int DoHandshakeWrite();
94 int DoHandshakeWriteComplete(int result
);
96 const std::string
BuildHandshakeWriteBuffer() const;
98 // Stores the underlying socket.
99 scoped_ptr
<ClientSocketHandle
> transport_
;
103 // Stores the callback to the layer above, called on completing Connect().
104 CompletionCallback user_callback_
;
106 // This IOBuffer is used by the class to read and write
107 // SOCKS handshake data. The length contains the expected size to
109 scoped_refptr
<IOBuffer
> handshake_buf_
;
111 // While writing, this buffer stores the complete write handshake data.
112 // While reading, it stores the handshake information received so far.
115 // This becomes true when the SOCKS handshake has completed and the
116 // overlying connection is free to communicate.
117 bool completed_handshake_
;
119 // These contain the bytes sent / received by the SOCKS handshake.
121 size_t bytes_received_
;
123 // Used to resolve the hostname to which the SOCKS proxy will connect.
124 SingleRequestHostResolver host_resolver_
;
125 AddressList addresses_
;
126 HostResolver::RequestInfo host_request_info_
;
127 RequestPriority priority_
;
129 BoundNetLog net_log_
;
131 DISALLOW_COPY_AND_ASSIGN(SOCKSClientSocket
);
136 #endif // NET_SOCKET_SOCKS_CLIENT_SOCKET_H_