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