• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef NET_SOCKET_SOCKS_CLIENT_SOCKET_H_
6 #define NET_SOCKET_SOCKS_CLIENT_SOCKET_H_
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #include <memory>
12 #include <string>
13 
14 #include "base/gtest_prod_util.h"
15 #include "base/memory/raw_ptr.h"
16 #include "base/memory/scoped_refptr.h"
17 #include "net/base/address_list.h"
18 #include "net/base/completion_once_callback.h"
19 #include "net/base/host_port_pair.h"
20 #include "net/base/net_errors.h"
21 #include "net/base/net_export.h"
22 #include "net/dns/host_resolver.h"
23 #include "net/dns/public/resolve_error_info.h"
24 #include "net/dns/public/secure_dns_policy.h"
25 #include "net/log/net_log_with_source.h"
26 #include "net/socket/stream_socket.h"
27 #include "net/traffic_annotation/network_traffic_annotation.h"
28 
29 namespace net {
30 
31 // The SOCKS client socket implementation
32 class NET_EXPORT_PRIVATE SOCKSClientSocket : public StreamSocket {
33  public:
34   // |destination| contains the hostname and port to which the socket above will
35   // communicate to via the socks layer. For testing the referrer is optional.
36   // |network_anonymization_key| is used for host resolution.
37   SOCKSClientSocket(std::unique_ptr<StreamSocket> transport_socket,
38                     const HostPortPair& destination,
39                     const NetworkAnonymizationKey& network_anonymization_key,
40                     RequestPriority priority,
41                     HostResolver* host_resolver,
42                     SecureDnsPolicy secure_dns_policy,
43                     const NetworkTrafficAnnotationTag& traffic_annotation);
44 
45   SOCKSClientSocket(const SOCKSClientSocket&) = delete;
46   SOCKSClientSocket& operator=(const SOCKSClientSocket&) = delete;
47 
48   // On destruction Disconnect() is called.
49   ~SOCKSClientSocket() override;
50 
51   // StreamSocket implementation.
52 
53   // Does the SOCKS handshake and completes the protocol.
54   int Connect(CompletionOnceCallback callback) override;
55   void Disconnect() override;
56   bool IsConnected() const override;
57   bool IsConnectedAndIdle() const override;
58   const NetLogWithSource& NetLog() const override;
59   bool WasEverUsed() const override;
60   bool WasAlpnNegotiated() const override;
61   NextProto GetNegotiatedProtocol() const override;
62   bool GetSSLInfo(SSLInfo* ssl_info) override;
63   int64_t GetTotalReceivedBytes() const override;
64   void ApplySocketTag(const SocketTag& tag) override;
65 
66   // Socket implementation.
67   int Read(IOBuffer* buf,
68            int buf_len,
69            CompletionOnceCallback callback) override;
70   int ReadIfReady(IOBuffer* buf,
71                   int buf_len,
72                   CompletionOnceCallback callback) override;
73   int CancelReadIfReady() override;
74   int Write(IOBuffer* buf,
75             int buf_len,
76             CompletionOnceCallback callback,
77             const NetworkTrafficAnnotationTag& traffic_annotation) override;
78 
79   int SetReceiveBufferSize(int32_t size) override;
80   int SetSendBufferSize(int32_t size) override;
81 
82   int GetPeerAddress(IPEndPoint* address) const override;
83   int GetLocalAddress(IPEndPoint* address) const override;
84 
85   // Returns error information about any host resolution attempt.
86   ResolveErrorInfo GetResolveErrorInfo() const;
87 
88  private:
89   FRIEND_TEST_ALL_PREFIXES(SOCKSClientSocketTest, CompleteHandshake);
90   FRIEND_TEST_ALL_PREFIXES(SOCKSClientSocketTest, SOCKS4AFailedDNS);
91   FRIEND_TEST_ALL_PREFIXES(SOCKSClientSocketTest, SOCKS4AIfDomainInIPv6);
92 
93   enum State {
94     STATE_RESOLVE_HOST,
95     STATE_RESOLVE_HOST_COMPLETE,
96     STATE_HANDSHAKE_WRITE,
97     STATE_HANDSHAKE_WRITE_COMPLETE,
98     STATE_HANDSHAKE_READ,
99     STATE_HANDSHAKE_READ_COMPLETE,
100     STATE_NONE,
101   };
102 
103   void DoCallback(int result);
104   void OnIOComplete(int result);
105   void OnReadWriteComplete(CompletionOnceCallback callback, int result);
106 
107   int DoLoop(int last_io_result);
108   int DoResolveHost();
109   int DoResolveHostComplete(int result);
110   int DoHandshakeRead();
111   int DoHandshakeReadComplete(int result);
112   int DoHandshakeWrite();
113   int DoHandshakeWriteComplete(int result);
114 
115   const std::string BuildHandshakeWriteBuffer() const;
116 
117   // Stores the underlying socket.
118   std::unique_ptr<StreamSocket> transport_socket_;
119 
120   State next_state_ = STATE_NONE;
121 
122   // Stores the callbacks to the layer above, called on completing Connect().
123   CompletionOnceCallback user_callback_;
124 
125   // This IOBuffer is used by the class to read and write
126   // SOCKS handshake data. The length contains the expected size to
127   // read or write.
128   scoped_refptr<IOBuffer> handshake_buf_;
129 
130   // While writing, this buffer stores the complete write handshake data.
131   // While reading, it stores the handshake information received so far.
132   std::string buffer_;
133 
134   // This becomes true when the SOCKS handshake has completed and the
135   // overlying connection is free to communicate.
136   bool completed_handshake_ = false;
137 
138   // These contain the bytes sent / received by the SOCKS handshake.
139   size_t bytes_sent_ = 0;
140   size_t bytes_received_ = 0;
141 
142   // This becomes true when the socket is used to send or receive data.
143   bool was_ever_used_ = false;
144 
145   // Used to resolve the hostname to which the SOCKS proxy will connect.
146   raw_ptr<HostResolver> host_resolver_;
147   SecureDnsPolicy secure_dns_policy_;
148   std::unique_ptr<HostResolver::ResolveHostRequest> resolve_host_request_;
149   const HostPortPair destination_;
150   const NetworkAnonymizationKey network_anonymization_key_;
151   RequestPriority priority_;
152   ResolveErrorInfo resolve_error_info_;
153 
154   NetLogWithSource net_log_;
155 
156   // Traffic annotation for socket control.
157   NetworkTrafficAnnotationTag traffic_annotation_;
158 };
159 
160 }  // namespace net
161 
162 #endif  // NET_SOCKET_SOCKS_CLIENT_SOCKET_H_
163