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/dns/host_resolver.h"
18 #include "net/dns/single_request_host_resolver.h"
19 #include "net/log/net_log.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 ~SOCKSClientSocket() override
;
40 // StreamSocket implementation.
42 // Does the SOCKS handshake and completes the protocol.
43 int Connect(const CompletionCallback
& callback
) override
;
44 void Disconnect() override
;
45 bool IsConnected() const override
;
46 bool IsConnectedAndIdle() const override
;
47 const BoundNetLog
& NetLog() const override
;
48 void SetSubresourceSpeculation() override
;
49 void SetOmniboxSpeculation() override
;
50 bool WasEverUsed() const override
;
51 bool UsingTCPFastOpen() const override
;
52 bool WasNpnNegotiated() const override
;
53 NextProto
GetNegotiatedProtocol() const override
;
54 bool GetSSLInfo(SSLInfo
* ssl_info
) override
;
55 void GetConnectionAttempts(ConnectionAttempts
* out
) const override
;
56 void ClearConnectionAttempts() override
{}
57 void AddConnectionAttempts(const ConnectionAttempts
& attempts
) override
{}
59 // Socket implementation.
60 int Read(IOBuffer
* buf
,
62 const CompletionCallback
& callback
) override
;
63 int Write(IOBuffer
* buf
,
65 const CompletionCallback
& callback
) override
;
67 int SetReceiveBufferSize(int32 size
) override
;
68 int SetSendBufferSize(int32 size
) override
;
70 int GetPeerAddress(IPEndPoint
* address
) const override
;
71 int GetLocalAddress(IPEndPoint
* address
) const override
;
74 FRIEND_TEST_ALL_PREFIXES(SOCKSClientSocketTest
, CompleteHandshake
);
75 FRIEND_TEST_ALL_PREFIXES(SOCKSClientSocketTest
, SOCKS4AFailedDNS
);
76 FRIEND_TEST_ALL_PREFIXES(SOCKSClientSocketTest
, SOCKS4AIfDomainInIPv6
);
80 STATE_RESOLVE_HOST_COMPLETE
,
81 STATE_HANDSHAKE_WRITE
,
82 STATE_HANDSHAKE_WRITE_COMPLETE
,
84 STATE_HANDSHAKE_READ_COMPLETE
,
88 void DoCallback(int result
);
89 void OnIOComplete(int result
);
90 void OnReadWriteComplete(const CompletionCallback
& callback
, int result
);
92 int DoLoop(int last_io_result
);
94 int DoResolveHostComplete(int result
);
95 int DoHandshakeRead();
96 int DoHandshakeReadComplete(int result
);
97 int DoHandshakeWrite();
98 int DoHandshakeWriteComplete(int result
);
100 const std::string
BuildHandshakeWriteBuffer() const;
102 // Stores the underlying socket.
103 scoped_ptr
<ClientSocketHandle
> transport_
;
107 // Stores the callbacks to the layer above, called on completing Connect().
108 CompletionCallback user_callback_
;
110 // This IOBuffer is used by the class to read and write
111 // SOCKS handshake data. The length contains the expected size to
113 scoped_refptr
<IOBuffer
> handshake_buf_
;
115 // While writing, this buffer stores the complete write handshake data.
116 // While reading, it stores the handshake information received so far.
119 // This becomes true when the SOCKS handshake has completed and the
120 // overlying connection is free to communicate.
121 bool completed_handshake_
;
123 // These contain the bytes sent / received by the SOCKS handshake.
125 size_t bytes_received_
;
127 // This becomes true when the socket is used to send or receive data.
130 // Used to resolve the hostname to which the SOCKS proxy will connect.
131 SingleRequestHostResolver host_resolver_
;
132 AddressList addresses_
;
133 HostResolver::RequestInfo host_request_info_
;
134 RequestPriority priority_
;
136 BoundNetLog net_log_
;
138 DISALLOW_COPY_AND_ASSIGN(SOCKSClientSocket
);
143 #endif // NET_SOCKET_SOCKS_CLIENT_SOCKET_H_