socks5_client_socket.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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_SOCKS5_CLIENT_SOCKET_H_
  5. #define NET_SOCKET_SOCKS5_CLIENT_SOCKET_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <memory>
  9. #include <string>
  10. #include "base/memory/ref_counted.h"
  11. #include "net/base/address_list.h"
  12. #include "net/base/completion_once_callback.h"
  13. #include "net/base/completion_repeating_callback.h"
  14. #include "net/base/host_port_pair.h"
  15. #include "net/base/net_errors.h"
  16. #include "net/base/net_export.h"
  17. #include "net/log/net_log_with_source.h"
  18. #include "net/socket/stream_socket.h"
  19. #include "net/traffic_annotation/network_traffic_annotation.h"
  20. #include "url/gurl.h"
  21. namespace net {
  22. // This StreamSocket is used to setup a SOCKSv5 handshake with a socks proxy.
  23. // Currently no SOCKSv5 authentication is supported.
  24. class NET_EXPORT_PRIVATE SOCKS5ClientSocket : public StreamSocket {
  25. public:
  26. // |destination| contains the hostname and port to which the socket above will
  27. // communicate to via the SOCKS layer.
  28. //
  29. // Although SOCKS 5 supports 3 different modes of addressing, we will
  30. // always pass it a hostname. This means the DNS resolving is done
  31. // proxy side.
  32. SOCKS5ClientSocket(std::unique_ptr<StreamSocket> transport_socket,
  33. const HostPortPair& destination,
  34. const NetworkTrafficAnnotationTag& traffic_annotation);
  35. SOCKS5ClientSocket(const SOCKS5ClientSocket&) = delete;
  36. SOCKS5ClientSocket& operator=(const SOCKS5ClientSocket&) = delete;
  37. // On destruction Disconnect() is called.
  38. ~SOCKS5ClientSocket() override;
  39. // StreamSocket implementation.
  40. // Does the SOCKS handshake and completes the protocol.
  41. int Connect(CompletionOnceCallback callback) override;
  42. void Disconnect() override;
  43. bool IsConnected() const override;
  44. bool IsConnectedAndIdle() const override;
  45. const NetLogWithSource& NetLog() const override;
  46. bool WasEverUsed() const override;
  47. bool WasAlpnNegotiated() const override;
  48. NextProto GetNegotiatedProtocol() const override;
  49. bool GetSSLInfo(SSLInfo* ssl_info) override;
  50. int64_t GetTotalReceivedBytes() const override;
  51. void ApplySocketTag(const SocketTag& tag) override;
  52. // Socket implementation.
  53. int Read(IOBuffer* buf,
  54. int buf_len,
  55. CompletionOnceCallback callback) override;
  56. int Write(IOBuffer* buf,
  57. int buf_len,
  58. CompletionOnceCallback callback,
  59. const NetworkTrafficAnnotationTag& traffic_annotation) override;
  60. int SetReceiveBufferSize(int32_t size) override;
  61. int SetSendBufferSize(int32_t size) override;
  62. int GetPeerAddress(IPEndPoint* address) const override;
  63. int GetLocalAddress(IPEndPoint* address) const override;
  64. private:
  65. enum State {
  66. STATE_GREET_WRITE,
  67. STATE_GREET_WRITE_COMPLETE,
  68. STATE_GREET_READ,
  69. STATE_GREET_READ_COMPLETE,
  70. STATE_HANDSHAKE_WRITE,
  71. STATE_HANDSHAKE_WRITE_COMPLETE,
  72. STATE_HANDSHAKE_READ,
  73. STATE_HANDSHAKE_READ_COMPLETE,
  74. STATE_NONE,
  75. };
  76. // Addressing type that can be specified in requests or responses.
  77. enum SocksEndPointAddressType {
  78. kEndPointDomain = 0x03,
  79. kEndPointResolvedIPv4 = 0x01,
  80. kEndPointResolvedIPv6 = 0x04,
  81. };
  82. static const unsigned int kGreetReadHeaderSize;
  83. static const unsigned int kWriteHeaderSize;
  84. static const unsigned int kReadHeaderSize;
  85. static const uint8_t kSOCKS5Version;
  86. static const uint8_t kTunnelCommand;
  87. static const uint8_t kNullByte;
  88. void DoCallback(int result);
  89. void OnIOComplete(int result);
  90. void OnReadWriteComplete(CompletionOnceCallback callback, int result);
  91. int DoLoop(int last_io_result);
  92. int DoHandshakeRead();
  93. int DoHandshakeReadComplete(int result);
  94. int DoHandshakeWrite();
  95. int DoHandshakeWriteComplete(int result);
  96. int DoGreetRead();
  97. int DoGreetReadComplete(int result);
  98. int DoGreetWrite();
  99. int DoGreetWriteComplete(int result);
  100. // Writes the SOCKS handshake buffer into |handshake|
  101. // and return OK on success.
  102. int BuildHandshakeWriteBuffer(std::string* handshake) const;
  103. CompletionRepeatingCallback io_callback_;
  104. // Stores the underlying socket.
  105. std::unique_ptr<StreamSocket> transport_socket_;
  106. State next_state_ = STATE_NONE;
  107. // Stores the callback to the layer above, called on completing Connect().
  108. CompletionOnceCallback user_callback_;
  109. // This IOBuffer is used by the class to read and write
  110. // SOCKS handshake data. The length contains the expected size to
  111. // read or write.
  112. scoped_refptr<IOBuffer> handshake_buf_;
  113. // While writing, this buffer stores the complete write handshake data.
  114. // While reading, it stores the handshake information received so far.
  115. std::string buffer_;
  116. // This becomes true when the SOCKS handshake has completed and the
  117. // overlying connection is free to communicate.
  118. bool completed_handshake_ = false;
  119. // These contain the bytes sent / received by the SOCKS handshake.
  120. size_t bytes_sent_ = 0;
  121. size_t bytes_received_ = 0;
  122. size_t read_header_size;
  123. bool was_ever_used_ = false;
  124. const HostPortPair destination_;
  125. NetLogWithSource net_log_;
  126. // Traffic annotation for socket control.
  127. NetworkTrafficAnnotationTag traffic_annotation_;
  128. };
  129. } // namespace net
  130. #endif // NET_SOCKET_SOCKS5_CLIENT_SOCKET_H_