Home | History | Annotate | Download | only in socket
      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_SOCKS5_CLIENT_SOCKET_H_
      6 #define NET_SOCKET_SOCKS5_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/socket/stream_socket.h"
     20 #include "url/gurl.h"
     21 
     22 namespace net {
     23 
     24 class ClientSocketHandle;
     25 class BoundNetLog;
     26 
     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 {
     30  public:
     31   // Takes ownership of the |transport_socket|, which should already be
     32   // connected by the time Connect() is called.
     33   //
     34   // |req_info| contains the hostname and port to which the socket above will
     35   // communicate to via the SOCKS layer.
     36   //
     37   // Although SOCKS 5 supports 3 different modes of addressing, we will
     38   // always pass it a hostname. This means the DNS resolving is done
     39   // proxy side.
     40   SOCKS5ClientSocket(ClientSocketHandle* transport_socket,
     41                      const HostResolver::RequestInfo& req_info);
     42 
     43   // Deprecated constructor (http://crbug.com/37810) that takes a StreamSocket.
     44   SOCKS5ClientSocket(StreamSocket* transport_socket,
     45                      const HostResolver::RequestInfo& req_info);
     46 
     47   // On destruction Disconnect() is called.
     48   virtual ~SOCKS5ClientSocket();
     49 
     50   // StreamSocket implementation.
     51 
     52   // Does the SOCKS handshake and completes the protocol.
     53   virtual int Connect(const CompletionCallback& callback) OVERRIDE;
     54   virtual void Disconnect() OVERRIDE;
     55   virtual bool IsConnected() const OVERRIDE;
     56   virtual bool IsConnectedAndIdle() const OVERRIDE;
     57   virtual const BoundNetLog& NetLog() const OVERRIDE;
     58   virtual void SetSubresourceSpeculation() OVERRIDE;
     59   virtual void SetOmniboxSpeculation() OVERRIDE;
     60   virtual bool WasEverUsed() const OVERRIDE;
     61   virtual bool UsingTCPFastOpen() const OVERRIDE;
     62   virtual bool WasNpnNegotiated() const OVERRIDE;
     63   virtual NextProto GetNegotiatedProtocol() const OVERRIDE;
     64   virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE;
     65 
     66   // Socket implementation.
     67   virtual int Read(IOBuffer* buf,
     68                    int buf_len,
     69                    const CompletionCallback& callback) OVERRIDE;
     70   virtual int Write(IOBuffer* buf,
     71                     int buf_len,
     72                     const CompletionCallback& callback) OVERRIDE;
     73 
     74   virtual bool SetReceiveBufferSize(int32 size) OVERRIDE;
     75   virtual bool SetSendBufferSize(int32 size) OVERRIDE;
     76 
     77   virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE;
     78   virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE;
     79 
     80  private:
     81   enum State {
     82     STATE_GREET_WRITE,
     83     STATE_GREET_WRITE_COMPLETE,
     84     STATE_GREET_READ,
     85     STATE_GREET_READ_COMPLETE,
     86     STATE_HANDSHAKE_WRITE,
     87     STATE_HANDSHAKE_WRITE_COMPLETE,
     88     STATE_HANDSHAKE_READ,
     89     STATE_HANDSHAKE_READ_COMPLETE,
     90     STATE_NONE,
     91   };
     92 
     93   // Addressing type that can be specified in requests or responses.
     94   enum SocksEndPointAddressType {
     95     kEndPointDomain = 0x03,
     96     kEndPointResolvedIPv4 = 0x01,
     97     kEndPointResolvedIPv6 = 0x04,
     98   };
     99 
    100   static const unsigned int kGreetReadHeaderSize;
    101   static const unsigned int kWriteHeaderSize;
    102   static const unsigned int kReadHeaderSize;
    103   static const uint8 kSOCKS5Version;
    104   static const uint8 kTunnelCommand;
    105   static const uint8 kNullByte;
    106 
    107   void DoCallback(int result);
    108   void OnIOComplete(int result);
    109 
    110   int DoLoop(int last_io_result);
    111   int DoHandshakeRead();
    112   int DoHandshakeReadComplete(int result);
    113   int DoHandshakeWrite();
    114   int DoHandshakeWriteComplete(int result);
    115   int DoGreetRead();
    116   int DoGreetReadComplete(int result);
    117   int DoGreetWrite();
    118   int DoGreetWriteComplete(int result);
    119 
    120   // Writes the SOCKS handshake buffer into |handshake|
    121   // and return OK on success.
    122   int BuildHandshakeWriteBuffer(std::string* handshake) const;
    123 
    124   CompletionCallback io_callback_;
    125 
    126   // Stores the underlying socket.
    127   scoped_ptr<ClientSocketHandle> transport_;
    128 
    129   State next_state_;
    130 
    131   // Stores the callback to the layer above, called on completing Connect().
    132   CompletionCallback user_callback_;
    133 
    134   // This IOBuffer is used by the class to read and write
    135   // SOCKS handshake data. The length contains the expected size to
    136   // read or write.
    137   scoped_refptr<IOBuffer> handshake_buf_;
    138 
    139   // While writing, this buffer stores the complete write handshake data.
    140   // While reading, it stores the handshake information received so far.
    141   std::string buffer_;
    142 
    143   // This becomes true when the SOCKS handshake has completed and the
    144   // overlying connection is free to communicate.
    145   bool completed_handshake_;
    146 
    147   // These contain the bytes sent / received by the SOCKS handshake.
    148   size_t bytes_sent_;
    149   size_t bytes_received_;
    150 
    151   size_t read_header_size;
    152 
    153   HostResolver::RequestInfo host_request_info_;
    154 
    155   BoundNetLog net_log_;
    156 
    157   DISALLOW_COPY_AND_ASSIGN(SOCKS5ClientSocket);
    158 };
    159 
    160 }  // namespace net
    161 
    162 #endif  // NET_SOCKET_SOCKS5_CLIENT_SOCKET_H_
    163