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. 4 5#ifndef NET_SOCKET_SOCKS_CLIENT_SOCKET_H_ 6#define NET_SOCKET_SOCKS_CLIENT_SOCKET_H_ 7 8#include <string> 9 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/base/net_log.h" 18#include "net/dns/host_resolver.h" 19#include "net/dns/single_request_host_resolver.h" 20#include "net/socket/stream_socket.h" 21 22namespace net { 23 24class ClientSocketHandle; 25class BoundNetLog; 26 27// The SOCKS client socket implementation 28class NET_EXPORT_PRIVATE SOCKSClientSocket : public StreamSocket { 29 public: 30 // Takes ownership of the |transport_socket|, which should already be 31 // connected by the time Connect() is called. 32 // 33 // |req_info| contains the hostname and port to which the socket above will 34 // communicate to via the socks layer. For testing the referrer is optional. 35 SOCKSClientSocket(ClientSocketHandle* transport_socket, 36 const HostResolver::RequestInfo& req_info, 37 HostResolver* host_resolver); 38 39 // Deprecated constructor (http://crbug.com/37810) that takes a StreamSocket. 40 SOCKSClientSocket(StreamSocket* transport_socket, 41 const HostResolver::RequestInfo& req_info, 42 HostResolver* host_resolver); 43 44 // On destruction Disconnect() is called. 45 virtual ~SOCKSClientSocket(); 46 47 // StreamSocket implementation. 48 49 // Does the SOCKS handshake and completes the protocol. 50 virtual int Connect(const CompletionCallback& callback) OVERRIDE; 51 virtual void Disconnect() OVERRIDE; 52 virtual bool IsConnected() const OVERRIDE; 53 virtual bool IsConnectedAndIdle() const OVERRIDE; 54 virtual const BoundNetLog& NetLog() const OVERRIDE; 55 virtual void SetSubresourceSpeculation() OVERRIDE; 56 virtual void SetOmniboxSpeculation() OVERRIDE; 57 virtual bool WasEverUsed() const OVERRIDE; 58 virtual bool UsingTCPFastOpen() const OVERRIDE; 59 virtual bool WasNpnNegotiated() const OVERRIDE; 60 virtual NextProto GetNegotiatedProtocol() const OVERRIDE; 61 virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE; 62 63 // Socket implementation. 64 virtual int Read(IOBuffer* buf, 65 int buf_len, 66 const CompletionCallback& callback) OVERRIDE; 67 virtual int Write(IOBuffer* buf, 68 int buf_len, 69 const CompletionCallback& callback) OVERRIDE; 70 71 virtual bool SetReceiveBufferSize(int32 size) OVERRIDE; 72 virtual bool SetSendBufferSize(int32 size) OVERRIDE; 73 74 virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE; 75 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE; 76 77 private: 78 FRIEND_TEST_ALL_PREFIXES(SOCKSClientSocketTest, CompleteHandshake); 79 FRIEND_TEST_ALL_PREFIXES(SOCKSClientSocketTest, SOCKS4AFailedDNS); 80 FRIEND_TEST_ALL_PREFIXES(SOCKSClientSocketTest, SOCKS4AIfDomainInIPv6); 81 82 enum State { 83 STATE_RESOLVE_HOST, 84 STATE_RESOLVE_HOST_COMPLETE, 85 STATE_HANDSHAKE_WRITE, 86 STATE_HANDSHAKE_WRITE_COMPLETE, 87 STATE_HANDSHAKE_READ, 88 STATE_HANDSHAKE_READ_COMPLETE, 89 STATE_NONE, 90 }; 91 92 void DoCallback(int result); 93 void OnIOComplete(int result); 94 95 int DoLoop(int last_io_result); 96 int DoResolveHost(); 97 int DoResolveHostComplete(int result); 98 int DoHandshakeRead(); 99 int DoHandshakeReadComplete(int result); 100 int DoHandshakeWrite(); 101 int DoHandshakeWriteComplete(int result); 102 103 const std::string BuildHandshakeWriteBuffer() const; 104 105 // Stores the underlying socket. 106 scoped_ptr<ClientSocketHandle> transport_; 107 108 State next_state_; 109 110 // Stores the callback to the layer above, called on completing Connect(). 111 CompletionCallback user_callback_; 112 113 // This IOBuffer is used by the class to read and write 114 // SOCKS handshake data. The length contains the expected size to 115 // read or write. 116 scoped_refptr<IOBuffer> handshake_buf_; 117 118 // While writing, this buffer stores the complete write handshake data. 119 // While reading, it stores the handshake information received so far. 120 std::string buffer_; 121 122 // This becomes true when the SOCKS handshake has completed and the 123 // overlying connection is free to communicate. 124 bool completed_handshake_; 125 126 // These contain the bytes sent / received by the SOCKS handshake. 127 size_t bytes_sent_; 128 size_t bytes_received_; 129 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 135 BoundNetLog net_log_; 136 137 DISALLOW_COPY_AND_ASSIGN(SOCKSClientSocket); 138}; 139 140} // namespace net 141 142#endif // NET_SOCKET_SOCKS_CLIENT_SOCKET_H_ 143