socks_client_socket.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #ifndef NET_SOCKET_SOCKS_CLIENT_SOCKET_H_
  5. #define NET_SOCKET_SOCKS_CLIENT_SOCKET_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <memory>
  9. #include <string>
  10. #include "base/gtest_prod_util.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/memory/ref_counted.h"
  13. #include "net/base/address_list.h"
  14. #include "net/base/completion_once_callback.h"
  15. #include "net/base/host_port_pair.h"
  16. #include "net/base/net_errors.h"
  17. #include "net/base/net_export.h"
  18. #include "net/dns/host_resolver.h"
  19. #include "net/dns/public/resolve_error_info.h"
  20. #include "net/dns/public/secure_dns_policy.h"
  21. #include "net/log/net_log_with_source.h"
  22. #include "net/socket/stream_socket.h"
  23. #include "net/traffic_annotation/network_traffic_annotation.h"
  24. namespace net {
  25. // The SOCKS client socket implementation
  26. class NET_EXPORT_PRIVATE SOCKSClientSocket : public StreamSocket {
  27. public:
  28. // |destination| contains the hostname and port to which the socket above will
  29. // communicate to via the socks layer. For testing the referrer is optional.
  30. // |network_isolation_key| is used for host resolution.
  31. SOCKSClientSocket(std::unique_ptr<StreamSocket> transport_socket,
  32. const HostPortPair& destination,
  33. const NetworkIsolationKey& network_isolation_key,
  34. RequestPriority priority,
  35. HostResolver* host_resolver,
  36. SecureDnsPolicy secure_dns_policy,
  37. const NetworkTrafficAnnotationTag& traffic_annotation);
  38. SOCKSClientSocket(const SOCKSClientSocket&) = delete;
  39. SOCKSClientSocket& operator=(const SOCKSClientSocket&) = delete;
  40. // On destruction Disconnect() is called.
  41. ~SOCKSClientSocket() override;
  42. // StreamSocket implementation.
  43. // Does the SOCKS handshake and completes the protocol.
  44. int Connect(CompletionOnceCallback callback) override;
  45. void Disconnect() override;
  46. bool IsConnected() const override;
  47. bool IsConnectedAndIdle() const override;
  48. const NetLogWithSource& NetLog() const override;
  49. bool WasEverUsed() const override;
  50. bool WasAlpnNegotiated() const override;
  51. NextProto GetNegotiatedProtocol() const override;
  52. bool GetSSLInfo(SSLInfo* ssl_info) override;
  53. int64_t GetTotalReceivedBytes() const override;
  54. void ApplySocketTag(const SocketTag& tag) override;
  55. // Socket implementation.
  56. int Read(IOBuffer* buf,
  57. int buf_len,
  58. CompletionOnceCallback callback) override;
  59. int ReadIfReady(IOBuffer* buf,
  60. int buf_len,
  61. CompletionOnceCallback callback) override;
  62. int CancelReadIfReady() override;
  63. int Write(IOBuffer* buf,
  64. int buf_len,
  65. CompletionOnceCallback callback,
  66. const NetworkTrafficAnnotationTag& traffic_annotation) override;
  67. int SetReceiveBufferSize(int32_t size) override;
  68. int SetSendBufferSize(int32_t size) override;
  69. int GetPeerAddress(IPEndPoint* address) const override;
  70. int GetLocalAddress(IPEndPoint* address) const override;
  71. // Returns error information about any host resolution attempt.
  72. ResolveErrorInfo GetResolveErrorInfo() const;
  73. private:
  74. FRIEND_TEST_ALL_PREFIXES(SOCKSClientSocketTest, CompleteHandshake);
  75. FRIEND_TEST_ALL_PREFIXES(SOCKSClientSocketTest, SOCKS4AFailedDNS);
  76. FRIEND_TEST_ALL_PREFIXES(SOCKSClientSocketTest, SOCKS4AIfDomainInIPv6);
  77. enum State {
  78. STATE_RESOLVE_HOST,
  79. STATE_RESOLVE_HOST_COMPLETE,
  80. STATE_HANDSHAKE_WRITE,
  81. STATE_HANDSHAKE_WRITE_COMPLETE,
  82. STATE_HANDSHAKE_READ,
  83. STATE_HANDSHAKE_READ_COMPLETE,
  84. STATE_NONE,
  85. };
  86. void DoCallback(int result);
  87. void OnIOComplete(int result);
  88. void OnReadWriteComplete(CompletionOnceCallback callback, int result);
  89. int DoLoop(int last_io_result);
  90. int DoResolveHost();
  91. int DoResolveHostComplete(int result);
  92. int DoHandshakeRead();
  93. int DoHandshakeReadComplete(int result);
  94. int DoHandshakeWrite();
  95. int DoHandshakeWriteComplete(int result);
  96. const std::string BuildHandshakeWriteBuffer() const;
  97. // Stores the underlying socket.
  98. std::unique_ptr<StreamSocket> transport_socket_;
  99. State next_state_ = STATE_NONE;
  100. // Stores the callbacks to the layer above, called on completing Connect().
  101. CompletionOnceCallback user_callback_;
  102. // This IOBuffer is used by the class to read and write
  103. // SOCKS handshake data. The length contains the expected size to
  104. // read or write.
  105. scoped_refptr<IOBuffer> handshake_buf_;
  106. // While writing, this buffer stores the complete write handshake data.
  107. // While reading, it stores the handshake information received so far.
  108. std::string buffer_;
  109. // This becomes true when the SOCKS handshake has completed and the
  110. // overlying connection is free to communicate.
  111. bool completed_handshake_ = false;
  112. // These contain the bytes sent / received by the SOCKS handshake.
  113. size_t bytes_sent_ = 0;
  114. size_t bytes_received_ = 0;
  115. // This becomes true when the socket is used to send or receive data.
  116. bool was_ever_used_ = false;
  117. // Used to resolve the hostname to which the SOCKS proxy will connect.
  118. raw_ptr<HostResolver> host_resolver_;
  119. SecureDnsPolicy secure_dns_policy_;
  120. std::unique_ptr<HostResolver::ResolveHostRequest> resolve_host_request_;
  121. const HostPortPair destination_;
  122. const NetworkIsolationKey network_isolation_key_;
  123. RequestPriority priority_;
  124. ResolveErrorInfo resolve_error_info_;
  125. NetLogWithSource net_log_;
  126. // Traffic annotation for socket control.
  127. NetworkTrafficAnnotationTag traffic_annotation_;
  128. };
  129. } // namespace net
  130. #endif // NET_SOCKET_SOCKS_CLIENT_SOCKET_H_