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 NextProto GetNegotiatedProtocol() const override; 61 bool GetSSLInfo(SSLInfo* ssl_info) override; 62 int64_t GetTotalReceivedBytes() const override; 63 void ApplySocketTag(const SocketTag& tag) override; 64 65 // Socket implementation. 66 int Read(IOBuffer* buf, 67 int buf_len, 68 CompletionOnceCallback callback) override; 69 int ReadIfReady(IOBuffer* buf, 70 int buf_len, 71 CompletionOnceCallback callback) override; 72 int CancelReadIfReady() override; 73 int Write(IOBuffer* buf, 74 int buf_len, 75 CompletionOnceCallback callback, 76 const NetworkTrafficAnnotationTag& traffic_annotation) override; 77 78 int SetReceiveBufferSize(int32_t size) override; 79 int SetSendBufferSize(int32_t size) override; 80 81 int GetPeerAddress(IPEndPoint* address) const override; 82 int GetLocalAddress(IPEndPoint* address) const override; 83 84 // Returns error information about any host resolution attempt. 85 ResolveErrorInfo GetResolveErrorInfo() const; 86 87 private: 88 FRIEND_TEST_ALL_PREFIXES(SOCKSClientSocketTest, CompleteHandshake); 89 FRIEND_TEST_ALL_PREFIXES(SOCKSClientSocketTest, SOCKS4AFailedDNS); 90 FRIEND_TEST_ALL_PREFIXES(SOCKSClientSocketTest, SOCKS4AIfDomainInIPv6); 91 92 enum State { 93 STATE_RESOLVE_HOST, 94 STATE_RESOLVE_HOST_COMPLETE, 95 STATE_HANDSHAKE_WRITE, 96 STATE_HANDSHAKE_WRITE_COMPLETE, 97 STATE_HANDSHAKE_READ, 98 STATE_HANDSHAKE_READ_COMPLETE, 99 STATE_NONE, 100 }; 101 102 void DoCallback(int result); 103 void OnIOComplete(int result); 104 void OnReadWriteComplete(CompletionOnceCallback callback, int result); 105 106 int DoLoop(int last_io_result); 107 int DoResolveHost(); 108 int DoResolveHostComplete(int result); 109 int DoHandshakeRead(); 110 int DoHandshakeReadComplete(int result); 111 int DoHandshakeWrite(); 112 int DoHandshakeWriteComplete(int result); 113 114 const std::string BuildHandshakeWriteBuffer() const; 115 116 // Stores the underlying socket. 117 std::unique_ptr<StreamSocket> transport_socket_; 118 119 State next_state_ = STATE_NONE; 120 121 // Stores the callbacks to the layer above, called on completing Connect(). 122 CompletionOnceCallback user_callback_; 123 124 // This IOBuffer is used by the class to read and write 125 // SOCKS handshake data. The length contains the expected size to 126 // read or write. 127 scoped_refptr<IOBuffer> handshake_buf_; 128 129 // While writing, this buffer stores the complete write handshake data. 130 // While reading, it stores the handshake information received so far. 131 std::string buffer_; 132 133 // This becomes true when the SOCKS handshake has completed and the 134 // overlying connection is free to communicate. 135 bool completed_handshake_ = false; 136 137 // These contain the bytes sent / received by the SOCKS handshake. 138 size_t bytes_sent_ = 0; 139 size_t bytes_received_ = 0; 140 141 // This becomes true when the socket is used to send or receive data. 142 bool was_ever_used_ = false; 143 144 // Used to resolve the hostname to which the SOCKS proxy will connect. 145 raw_ptr<HostResolver> host_resolver_; 146 SecureDnsPolicy secure_dns_policy_; 147 std::unique_ptr<HostResolver::ResolveHostRequest> resolve_host_request_; 148 const HostPortPair destination_; 149 const NetworkAnonymizationKey network_anonymization_key_; 150 RequestPriority priority_; 151 ResolveErrorInfo resolve_error_info_; 152 153 NetLogWithSource net_log_; 154 155 // Traffic annotation for socket control. 156 NetworkTrafficAnnotationTag traffic_annotation_; 157 }; 158 159 } // namespace net 160 161 #endif // NET_SOCKET_SOCKS_CLIENT_SOCKET_H_ 162