blob: 26da332b3ea2da7ebdc0fe5bdb98088b7048575b [file] [log] [blame]
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include "base/basictypes.h"
#include "base/gtest_prod_util.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "net/base/address_list.h"
#include "net/base/completion_callback.h"
#include "net/base/net_errors.h"
#include "net/base/net_log.h"
#include "net/dns/host_resolver.h"
#include "net/dns/single_request_host_resolver.h"
#include "net/socket/stream_socket.h"
namespace net {
class ClientSocketHandle;
class BoundNetLog;
// The SOCKS client socket implementation
class NET_EXPORT_PRIVATE SOCKSClientSocket : public StreamSocket {
// |req_info| contains the hostname and port to which the socket above will
// communicate to via the socks layer. For testing the referrer is optional.
SOCKSClientSocket(scoped_ptr<ClientSocketHandle> transport_socket,
const HostResolver::RequestInfo& req_info,
RequestPriority priority,
HostResolver* host_resolver);
// On destruction Disconnect() is called.
virtual ~SOCKSClientSocket();
// StreamSocket implementation.
// Does the SOCKS handshake and completes the protocol.
virtual int Connect(const CompletionCallback& callback) OVERRIDE;
virtual void Disconnect() OVERRIDE;
virtual bool IsConnected() const OVERRIDE;
virtual bool IsConnectedAndIdle() const OVERRIDE;
virtual const BoundNetLog& NetLog() const OVERRIDE;
virtual void SetSubresourceSpeculation() OVERRIDE;
virtual void SetOmniboxSpeculation() OVERRIDE;
virtual bool WasEverUsed() const OVERRIDE;
virtual bool UsingTCPFastOpen() const OVERRIDE;
virtual bool WasNpnNegotiated() const OVERRIDE;
virtual NextProto GetNegotiatedProtocol() const OVERRIDE;
virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE;
// Socket implementation.
virtual int Read(IOBuffer* buf,
int buf_len,
const CompletionCallback& callback) OVERRIDE;
virtual int Write(IOBuffer* buf,
int buf_len,
const CompletionCallback& callback) OVERRIDE;
virtual int SetReceiveBufferSize(int32 size) OVERRIDE;
virtual int SetSendBufferSize(int32 size) OVERRIDE;
virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE;
virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE;
FRIEND_TEST_ALL_PREFIXES(SOCKSClientSocketTest, CompleteHandshake);
enum State {
void DoCallback(int result);
void OnIOComplete(int result);
void OnReadWriteComplete(const CompletionCallback& callback, int result);
int DoLoop(int last_io_result);
int DoResolveHost();
int DoResolveHostComplete(int result);
int DoHandshakeRead();
int DoHandshakeReadComplete(int result);
int DoHandshakeWrite();
int DoHandshakeWriteComplete(int result);
const std::string BuildHandshakeWriteBuffer() const;
// Stores the underlying socket.
scoped_ptr<ClientSocketHandle> transport_;
State next_state_;
// Stores the callbacks to the layer above, called on completing Connect().
CompletionCallback user_callback_;
// This IOBuffer is used by the class to read and write
// SOCKS handshake data. The length contains the expected size to
// read or write.
scoped_refptr<IOBuffer> handshake_buf_;
// While writing, this buffer stores the complete write handshake data.
// While reading, it stores the handshake information received so far.
std::string buffer_;
// This becomes true when the SOCKS handshake has completed and the
// overlying connection is free to communicate.
bool completed_handshake_;
// These contain the bytes sent / received by the SOCKS handshake.
size_t bytes_sent_;
size_t bytes_received_;
// This becomes true when the socket is used to send or receive data.
bool was_ever_used_;
// Used to resolve the hostname to which the SOCKS proxy will connect.
SingleRequestHostResolver host_resolver_;
AddressList addresses_;
HostResolver::RequestInfo host_request_info_;
RequestPriority priority_;
BoundNetLog net_log_;
} // namespace net