1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef NET_SOCKET_SOCKS_CLIENT_SOCKET_H_
#define NET_SOCKET_SOCKS_CLIENT_SOCKET_H_
#include <stddef.h>
#include <stdint.h>
#include <memory>
#include <vector>
#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "net/base/address_list.h"
#include "net/base/completion_once_callback.h"
#include "net/base/host_port_pair.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/base/net_export.h"
#include "net/dns/host_resolver.h"
#include "net/dns/public/resolve_error_info.h"
#include "net/dns/public/secure_dns_policy.h"
#include "net/log/net_log_with_source.h"
#include "net/socket/stream_socket.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
namespace net {
// The SOCKS client socket implementation
class NET_EXPORT_PRIVATE SOCKSClientSocket : public StreamSocket {
public:
// |destination| contains the hostname and port to which the socket above will
// communicate to via the socks layer. For testing the referrer is optional.
// |network_anonymization_key| is used for host resolution.
SOCKSClientSocket(std::unique_ptr<StreamSocket> transport_socket,
const HostPortPair& destination,
const NetworkAnonymizationKey& network_anonymization_key,
RequestPriority priority,
HostResolver* host_resolver,
SecureDnsPolicy secure_dns_policy,
const NetworkTrafficAnnotationTag& traffic_annotation);
SOCKSClientSocket(const SOCKSClientSocket&) = delete;
SOCKSClientSocket& operator=(const SOCKSClientSocket&) = delete;
// On destruction Disconnect() is called.
~SOCKSClientSocket() override;
// StreamSocket implementation.
// Does the SOCKS handshake and completes the protocol.
int Connect(CompletionOnceCallback callback) override;
void Disconnect() override;
bool IsConnected() const override;
bool IsConnectedAndIdle() const override;
const NetLogWithSource& NetLog() const override;
bool WasEverUsed() const override;
NextProto GetNegotiatedProtocol() const override;
bool GetSSLInfo(SSLInfo* ssl_info) override;
int64_t GetTotalReceivedBytes() const override;
void ApplySocketTag(const SocketTag& tag) override;
// Socket implementation.
int Read(IOBuffer* buf,
int buf_len,
CompletionOnceCallback callback) override;
int ReadIfReady(IOBuffer* buf,
int buf_len,
CompletionOnceCallback callback) override;
int CancelReadIfReady() override;
int Write(IOBuffer* buf,
int buf_len,
CompletionOnceCallback callback,
const NetworkTrafficAnnotationTag& traffic_annotation) override;
int SetReceiveBufferSize(int32_t size) override;
int SetSendBufferSize(int32_t size) override;
int GetPeerAddress(IPEndPoint* address) const override;
int GetLocalAddress(IPEndPoint* address) const override;
// Returns error information about any host resolution attempt.
ResolveErrorInfo GetResolveErrorInfo() const;
private:
FRIEND_TEST_ALL_PREFIXES(SOCKSClientSocketTest, CompleteHandshake);
FRIEND_TEST_ALL_PREFIXES(SOCKSClientSocketTest, SOCKS4AFailedDNS);
FRIEND_TEST_ALL_PREFIXES(SOCKSClientSocketTest, SOCKS4AIfDomainInIPv6);
enum State {
STATE_RESOLVE_HOST,
STATE_RESOLVE_HOST_COMPLETE,
STATE_HANDSHAKE_WRITE,
STATE_HANDSHAKE_WRITE_COMPLETE,
STATE_HANDSHAKE_READ,
STATE_HANDSHAKE_READ_COMPLETE,
STATE_NONE,
};
void DoCallback(int result);
void OnIOComplete(int result);
void OnReadWriteComplete(CompletionOnceCallback 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);
std::vector<uint8_t> BuildHandshakeWriteBuffer() const;
// Stores the underlying socket.
std::unique_ptr<StreamSocket> transport_socket_;
State next_state_ = STATE_NONE;
// Stores the callbacks to the layer above, called on completing Connect().
CompletionOnceCallback user_callback_;
// This IOBuffer is used by the class to write the SOCKS client handshake.
scoped_refptr<DrainableIOBuffer> handshake_write_buf_;
// This IOBuffer is used by the class to read the SOCKS handshake data. This
// will be sized to exactly match the size of the expected handshake.
scoped_refptr<GrowableIOBuffer> handshake_read_buf_;
// This becomes true when the SOCKS handshake has completed and the
// overlying connection is free to communicate.
bool completed_handshake_ = false;
// This becomes true when the socket is used to send or receive data.
bool was_ever_used_ = false;
// Used to resolve the hostname to which the SOCKS proxy will connect.
raw_ptr<HostResolver> host_resolver_;
SecureDnsPolicy secure_dns_policy_;
std::unique_ptr<HostResolver::ResolveHostRequest> resolve_host_request_;
const HostPortPair destination_;
const NetworkAnonymizationKey network_anonymization_key_;
RequestPriority priority_;
ResolveErrorInfo resolve_error_info_;
NetLogWithSource net_log_;
// Traffic annotation for socket control.
NetworkTrafficAnnotationTag traffic_annotation_;
};
} // namespace net
#endif // NET_SOCKET_SOCKS_CLIENT_SOCKET_H_
|