• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_POOL_H_
6 #define NET_SOCKET_SOCKS_CLIENT_SOCKET_POOL_H_
7 
8 #include <string>
9 
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/time/time.h"
15 #include "net/base/host_port_pair.h"
16 #include "net/dns/host_resolver.h"
17 #include "net/socket/client_socket_pool.h"
18 #include "net/socket/client_socket_pool_base.h"
19 #include "net/socket/client_socket_pool_histograms.h"
20 
21 namespace net {
22 
23 class ConnectJobFactory;
24 class TransportClientSocketPool;
25 class TransportSocketParams;
26 
27 class NET_EXPORT_PRIVATE SOCKSSocketParams
28     : public base::RefCounted<SOCKSSocketParams> {
29  public:
30   SOCKSSocketParams(const scoped_refptr<TransportSocketParams>& proxy_server,
31                     bool socks_v5, const HostPortPair& host_port_pair);
32 
transport_params()33   const scoped_refptr<TransportSocketParams>& transport_params() const {
34     return transport_params_;
35   }
destination()36   const HostResolver::RequestInfo& destination() const { return destination_; }
is_socks_v5()37   bool is_socks_v5() const { return socks_v5_; }
ignore_limits()38   bool ignore_limits() const { return ignore_limits_; }
39 
40  private:
41   friend class base::RefCounted<SOCKSSocketParams>;
42   ~SOCKSSocketParams();
43 
44   // The transport (likely TCP) connection must point toward the proxy server.
45   const scoped_refptr<TransportSocketParams> transport_params_;
46   // This is the HTTP destination.
47   HostResolver::RequestInfo destination_;
48   const bool socks_v5_;
49   bool ignore_limits_;
50 
51   DISALLOW_COPY_AND_ASSIGN(SOCKSSocketParams);
52 };
53 
54 // SOCKSConnectJob handles the handshake to a socks server after setting up
55 // an underlying transport socket.
56 class SOCKSConnectJob : public ConnectJob {
57  public:
58   SOCKSConnectJob(const std::string& group_name,
59                   RequestPriority priority,
60                   const scoped_refptr<SOCKSSocketParams>& params,
61                   const base::TimeDelta& timeout_duration,
62                   TransportClientSocketPool* transport_pool,
63                   HostResolver* host_resolver,
64                   Delegate* delegate,
65                   NetLog* net_log);
66   virtual ~SOCKSConnectJob();
67 
68   // ConnectJob methods.
69   virtual LoadState GetLoadState() const OVERRIDE;
70 
71  private:
72   enum State {
73     STATE_TRANSPORT_CONNECT,
74     STATE_TRANSPORT_CONNECT_COMPLETE,
75     STATE_SOCKS_CONNECT,
76     STATE_SOCKS_CONNECT_COMPLETE,
77     STATE_NONE,
78   };
79 
80   void OnIOComplete(int result);
81 
82   // Runs the state transition loop.
83   int DoLoop(int result);
84 
85   int DoTransportConnect();
86   int DoTransportConnectComplete(int result);
87   int DoSOCKSConnect();
88   int DoSOCKSConnectComplete(int result);
89 
90   // Begins the transport connection and the SOCKS handshake.  Returns OK on
91   // success and ERR_IO_PENDING if it cannot immediately service the request.
92   // Otherwise, it returns a net error code.
93   virtual int ConnectInternal() OVERRIDE;
94 
95   scoped_refptr<SOCKSSocketParams> socks_params_;
96   TransportClientSocketPool* const transport_pool_;
97   HostResolver* const resolver_;
98 
99   State next_state_;
100   CompletionCallback callback_;
101   scoped_ptr<ClientSocketHandle> transport_socket_handle_;
102   scoped_ptr<StreamSocket> socket_;
103 
104   DISALLOW_COPY_AND_ASSIGN(SOCKSConnectJob);
105 };
106 
107 class NET_EXPORT_PRIVATE SOCKSClientSocketPool
108     : public ClientSocketPool, public HigherLayeredPool {
109  public:
110   typedef SOCKSSocketParams SocketParams;
111 
112   SOCKSClientSocketPool(
113       int max_sockets,
114       int max_sockets_per_group,
115       ClientSocketPoolHistograms* histograms,
116       HostResolver* host_resolver,
117       TransportClientSocketPool* transport_pool,
118       NetLog* net_log);
119 
120   virtual ~SOCKSClientSocketPool();
121 
122   // ClientSocketPool implementation.
123   virtual int RequestSocket(const std::string& group_name,
124                             const void* connect_params,
125                             RequestPriority priority,
126                             ClientSocketHandle* handle,
127                             const CompletionCallback& callback,
128                             const BoundNetLog& net_log) OVERRIDE;
129 
130   virtual void RequestSockets(const std::string& group_name,
131                               const void* params,
132                               int num_sockets,
133                               const BoundNetLog& net_log) OVERRIDE;
134 
135   virtual void CancelRequest(const std::string& group_name,
136                              ClientSocketHandle* handle) OVERRIDE;
137 
138   virtual void ReleaseSocket(const std::string& group_name,
139                              scoped_ptr<StreamSocket> socket,
140                              int id) OVERRIDE;
141 
142   virtual void FlushWithError(int error) OVERRIDE;
143 
144   virtual void CloseIdleSockets() OVERRIDE;
145 
146   virtual int IdleSocketCount() const OVERRIDE;
147 
148   virtual int IdleSocketCountInGroup(
149       const std::string& group_name) const OVERRIDE;
150 
151   virtual LoadState GetLoadState(
152       const std::string& group_name,
153       const ClientSocketHandle* handle) const OVERRIDE;
154 
155   virtual base::DictionaryValue* GetInfoAsValue(
156       const std::string& name,
157       const std::string& type,
158       bool include_nested_pools) const OVERRIDE;
159 
160   virtual base::TimeDelta ConnectionTimeout() const OVERRIDE;
161 
162   virtual ClientSocketPoolHistograms* histograms() const OVERRIDE;
163 
164   // LowerLayeredPool implementation.
165   virtual bool IsStalled() const OVERRIDE;
166 
167   virtual void AddHigherLayeredPool(HigherLayeredPool* higher_pool) OVERRIDE;
168 
169   virtual void RemoveHigherLayeredPool(HigherLayeredPool* higher_pool) OVERRIDE;
170 
171   // HigherLayeredPool implementation.
172   virtual bool CloseOneIdleConnection() OVERRIDE;
173 
174  private:
175   typedef ClientSocketPoolBase<SOCKSSocketParams> PoolBase;
176 
177   class SOCKSConnectJobFactory : public PoolBase::ConnectJobFactory {
178    public:
SOCKSConnectJobFactory(TransportClientSocketPool * transport_pool,HostResolver * host_resolver,NetLog * net_log)179     SOCKSConnectJobFactory(TransportClientSocketPool* transport_pool,
180                            HostResolver* host_resolver,
181                            NetLog* net_log)
182         : transport_pool_(transport_pool),
183           host_resolver_(host_resolver),
184           net_log_(net_log) {}
185 
~SOCKSConnectJobFactory()186     virtual ~SOCKSConnectJobFactory() {}
187 
188     // ClientSocketPoolBase::ConnectJobFactory methods.
189     virtual scoped_ptr<ConnectJob> NewConnectJob(
190         const std::string& group_name,
191         const PoolBase::Request& request,
192         ConnectJob::Delegate* delegate) const OVERRIDE;
193 
194     virtual base::TimeDelta ConnectionTimeout() const OVERRIDE;
195 
196    private:
197     TransportClientSocketPool* const transport_pool_;
198     HostResolver* const host_resolver_;
199     NetLog* net_log_;
200 
201     DISALLOW_COPY_AND_ASSIGN(SOCKSConnectJobFactory);
202   };
203 
204   TransportClientSocketPool* const transport_pool_;
205   PoolBase base_;
206 
207   DISALLOW_COPY_AND_ASSIGN(SOCKSClientSocketPool);
208 };
209 
210 }  // namespace net
211 
212 #endif  // NET_SOCKET_SOCKS_CLIENT_SOCKET_POOL_H_
213