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_SOCKS5_CLIENT_SOCKET_H_
6 #define NET_SOCKET_SOCKS5_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/log/net_log.h"
19 #include "net/socket/stream_socket.h"
24 class ClientSocketHandle
;
27 // This StreamSocket is used to setup a SOCKSv5 handshake with a socks proxy.
28 // Currently no SOCKSv5 authentication is supported.
29 class NET_EXPORT_PRIVATE SOCKS5ClientSocket
: public StreamSocket
{
31 // |req_info| contains the hostname and port to which the socket above will
32 // communicate to via the SOCKS layer.
34 // Although SOCKS 5 supports 3 different modes of addressing, we will
35 // always pass it a hostname. This means the DNS resolving is done
37 SOCKS5ClientSocket(scoped_ptr
<ClientSocketHandle
> transport_socket
,
38 const HostResolver::RequestInfo
& req_info
);
40 // On destruction Disconnect() is called.
41 ~SOCKS5ClientSocket() override
;
43 // StreamSocket implementation.
45 // Does the SOCKS handshake and completes the protocol.
46 int Connect(const CompletionCallback
& callback
) override
;
47 void Disconnect() override
;
48 bool IsConnected() const override
;
49 bool IsConnectedAndIdle() const override
;
50 const BoundNetLog
& NetLog() const override
;
51 void SetSubresourceSpeculation() override
;
52 void SetOmniboxSpeculation() override
;
53 bool WasEverUsed() const override
;
54 bool UsingTCPFastOpen() const override
;
55 bool WasNpnNegotiated() const override
;
56 NextProto
GetNegotiatedProtocol() const override
;
57 bool GetSSLInfo(SSLInfo
* ssl_info
) override
;
58 void GetConnectionAttempts(ConnectionAttempts
* out
) const override
;
59 void ClearConnectionAttempts() override
{}
60 void AddConnectionAttempts(const ConnectionAttempts
& attempts
) override
{}
62 // Socket implementation.
63 int Read(IOBuffer
* buf
,
65 const CompletionCallback
& callback
) override
;
66 int Write(IOBuffer
* buf
,
68 const CompletionCallback
& callback
) override
;
70 int SetReceiveBufferSize(int32 size
) override
;
71 int SetSendBufferSize(int32 size
) override
;
73 int GetPeerAddress(IPEndPoint
* address
) const override
;
74 int GetLocalAddress(IPEndPoint
* address
) const override
;
79 STATE_GREET_WRITE_COMPLETE
,
81 STATE_GREET_READ_COMPLETE
,
82 STATE_HANDSHAKE_WRITE
,
83 STATE_HANDSHAKE_WRITE_COMPLETE
,
85 STATE_HANDSHAKE_READ_COMPLETE
,
89 // Addressing type that can be specified in requests or responses.
90 enum SocksEndPointAddressType
{
91 kEndPointDomain
= 0x03,
92 kEndPointResolvedIPv4
= 0x01,
93 kEndPointResolvedIPv6
= 0x04,
96 static const unsigned int kGreetReadHeaderSize
;
97 static const unsigned int kWriteHeaderSize
;
98 static const unsigned int kReadHeaderSize
;
99 static const uint8 kSOCKS5Version
;
100 static const uint8 kTunnelCommand
;
101 static const uint8 kNullByte
;
103 void DoCallback(int result
);
104 void OnIOComplete(int result
);
105 void OnReadWriteComplete(const CompletionCallback
& callback
, int result
);
107 int DoLoop(int last_io_result
);
108 int DoHandshakeRead();
109 int DoHandshakeReadComplete(int result
);
110 int DoHandshakeWrite();
111 int DoHandshakeWriteComplete(int result
);
113 int DoGreetReadComplete(int result
);
115 int DoGreetWriteComplete(int result
);
117 // Writes the SOCKS handshake buffer into |handshake|
118 // and return OK on success.
119 int BuildHandshakeWriteBuffer(std::string
* handshake
) const;
121 CompletionCallback io_callback_
;
123 // Stores the underlying socket.
124 scoped_ptr
<ClientSocketHandle
> transport_
;
128 // Stores the callback to the layer above, called on completing Connect().
129 CompletionCallback user_callback_
;
131 // This IOBuffer is used by the class to read and write
132 // SOCKS handshake data. The length contains the expected size to
134 scoped_refptr
<IOBuffer
> handshake_buf_
;
136 // While writing, this buffer stores the complete write handshake data.
137 // While reading, it stores the handshake information received so far.
140 // This becomes true when the SOCKS handshake has completed and the
141 // overlying connection is free to communicate.
142 bool completed_handshake_
;
144 // These contain the bytes sent / received by the SOCKS handshake.
146 size_t bytes_received_
;
148 size_t read_header_size
;
152 HostResolver::RequestInfo host_request_info_
;
154 BoundNetLog net_log_
;
156 DISALLOW_COPY_AND_ASSIGN(SOCKS5ClientSocket
);
161 #endif // NET_SOCKET_SOCKS5_CLIENT_SOCKET_H_