15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef NET_SOCKET_SOCKS_CLIENT_SOCKET_H_ 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define NET_SOCKET_SOCKS_CLIENT_SOCKET_H_ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string> 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h" 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/gtest_prod_util.h" 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h" 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h" 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/address_list.h" 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/completion_callback.h" 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_errors.h" 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_log.h" 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/dns/host_resolver.h" 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/dns/single_request_host_resolver.h" 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/socket/stream_socket.h" 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net { 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ClientSocketHandle; 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BoundNetLog; 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The SOCKS client socket implementation 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class NET_EXPORT_PRIVATE SOCKSClientSocket : public StreamSocket { 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // |req_info| contains the hostname and port to which the socket above will 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // communicate to via the socks layer. For testing the referrer is optional. 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) SOCKSClientSocket(scoped_ptr<ClientSocketHandle> transport_socket, 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const HostResolver::RequestInfo& req_info, 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) RequestPriority priority, 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) HostResolver* host_resolver); 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // On destruction Disconnect() is called. 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual ~SOCKSClientSocket(); 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // StreamSocket implementation. 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Does the SOCKS handshake and completes the protocol. 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual int Connect(const CompletionCallback& callback) OVERRIDE; 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void Disconnect() OVERRIDE; 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool IsConnected() const OVERRIDE; 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool IsConnectedAndIdle() const OVERRIDE; 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual const BoundNetLog& NetLog() const OVERRIDE; 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void SetSubresourceSpeculation() OVERRIDE; 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void SetOmniboxSpeculation() OVERRIDE; 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool WasEverUsed() const OVERRIDE; 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool UsingTCPFastOpen() const OVERRIDE; 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool WasNpnNegotiated() const OVERRIDE; 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual NextProto GetNegotiatedProtocol() const OVERRIDE; 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE; 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Socket implementation. 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual int Read(IOBuffer* buf, 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int buf_len, 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const CompletionCallback& callback) OVERRIDE; 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual int Write(IOBuffer* buf, 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int buf_len, 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const CompletionCallback& callback) OVERRIDE; 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual int SetReceiveBufferSize(int32 size) OVERRIDE; 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual int SetSendBufferSize(int32 size) OVERRIDE; 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE; 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE; 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FRIEND_TEST_ALL_PREFIXES(SOCKSClientSocketTest, CompleteHandshake); 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FRIEND_TEST_ALL_PREFIXES(SOCKSClientSocketTest, SOCKS4AFailedDNS); 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FRIEND_TEST_ALL_PREFIXES(SOCKSClientSocketTest, SOCKS4AIfDomainInIPv6); 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) enum State { 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) STATE_RESOLVE_HOST, 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) STATE_RESOLVE_HOST_COMPLETE, 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) STATE_HANDSHAKE_WRITE, 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) STATE_HANDSHAKE_WRITE_COMPLETE, 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) STATE_HANDSHAKE_READ, 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) STATE_HANDSHAKE_READ_COMPLETE, 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) STATE_NONE, 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) }; 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void DoCallback(int result); 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void OnIOComplete(int result); 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void OnReadWriteComplete(const CompletionCallback& callback, int result); 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int DoLoop(int last_io_result); 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int DoResolveHost(); 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int DoResolveHostComplete(int result); 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int DoHandshakeRead(); 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int DoHandshakeReadComplete(int result); 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int DoHandshakeWrite(); 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int DoHandshakeWriteComplete(int result); 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string BuildHandshakeWriteBuffer() const; 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Stores the underlying socket. 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_ptr<ClientSocketHandle> transport_; 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 102 State next_state_; 103 104 // Stores the callbacks to the layer above, called on completing Connect(). 105 CompletionCallback user_callback_; 106 107 // This IOBuffer is used by the class to read and write 108 // SOCKS handshake data. The length contains the expected size to 109 // read or write. 110 scoped_refptr<IOBuffer> handshake_buf_; 111 112 // While writing, this buffer stores the complete write handshake data. 113 // While reading, it stores the handshake information received so far. 114 std::string buffer_; 115 116 // This becomes true when the SOCKS handshake has completed and the 117 // overlying connection is free to communicate. 118 bool completed_handshake_; 119 120 // These contain the bytes sent / received by the SOCKS handshake. 121 size_t bytes_sent_; 122 size_t bytes_received_; 123 124 // This becomes true when the socket is used to send or receive data. 125 bool was_ever_used_; 126 127 // Used to resolve the hostname to which the SOCKS proxy will connect. 128 SingleRequestHostResolver host_resolver_; 129 AddressList addresses_; 130 HostResolver::RequestInfo host_request_info_; 131 RequestPriority priority_; 132 133 BoundNetLog net_log_; 134 135 DISALLOW_COPY_AND_ASSIGN(SOCKSClientSocket); 136}; 137 138} // namespace net 139 140#endif // NET_SOCKET_SOCKS_CLIENT_SOCKET_H_ 141