socket_adapters.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef RTC_BASE_SOCKET_ADAPTERS_H_
  11. #define RTC_BASE_SOCKET_ADAPTERS_H_
  12. #include <string>
  13. #include "api/array_view.h"
  14. #include "rtc_base/async_socket.h"
  15. #include "rtc_base/constructor_magic.h"
  16. #include "rtc_base/crypt_string.h"
  17. namespace rtc {
  18. struct HttpAuthContext;
  19. class ByteBufferReader;
  20. class ByteBufferWriter;
  21. ///////////////////////////////////////////////////////////////////////////////
  22. // Implements a socket adapter that can buffer and process data internally,
  23. // as in the case of connecting to a proxy, where you must speak the proxy
  24. // protocol before commencing normal socket behavior.
  25. class BufferedReadAdapter : public AsyncSocketAdapter {
  26. public:
  27. BufferedReadAdapter(AsyncSocket* socket, size_t buffer_size);
  28. ~BufferedReadAdapter() override;
  29. int Send(const void* pv, size_t cb) override;
  30. int Recv(void* pv, size_t cb, int64_t* timestamp) override;
  31. protected:
  32. int DirectSend(const void* pv, size_t cb) {
  33. return AsyncSocketAdapter::Send(pv, cb);
  34. }
  35. void BufferInput(bool on = true);
  36. virtual void ProcessInput(char* data, size_t* len) = 0;
  37. void OnReadEvent(AsyncSocket* socket) override;
  38. private:
  39. char* buffer_;
  40. size_t buffer_size_, data_len_;
  41. bool buffering_;
  42. RTC_DISALLOW_COPY_AND_ASSIGN(BufferedReadAdapter);
  43. };
  44. ///////////////////////////////////////////////////////////////////////////////
  45. // Implements a socket adapter that performs the client side of a
  46. // fake SSL handshake. Used for "ssltcp" P2P functionality.
  47. class AsyncSSLSocket : public BufferedReadAdapter {
  48. public:
  49. static ArrayView<const uint8_t> SslClientHello();
  50. static ArrayView<const uint8_t> SslServerHello();
  51. explicit AsyncSSLSocket(AsyncSocket* socket);
  52. int Connect(const SocketAddress& addr) override;
  53. protected:
  54. void OnConnectEvent(AsyncSocket* socket) override;
  55. void ProcessInput(char* data, size_t* len) override;
  56. RTC_DISALLOW_COPY_AND_ASSIGN(AsyncSSLSocket);
  57. };
  58. ///////////////////////////////////////////////////////////////////////////////
  59. // Implements a socket adapter that speaks the HTTP/S proxy protocol.
  60. class AsyncHttpsProxySocket : public BufferedReadAdapter {
  61. public:
  62. AsyncHttpsProxySocket(AsyncSocket* socket,
  63. const std::string& user_agent,
  64. const SocketAddress& proxy,
  65. const std::string& username,
  66. const CryptString& password);
  67. ~AsyncHttpsProxySocket() override;
  68. // If connect is forced, the adapter will always issue an HTTP CONNECT to the
  69. // target address. Otherwise, it will connect only if the destination port
  70. // is not port 80.
  71. void SetForceConnect(bool force) { force_connect_ = force; }
  72. int Connect(const SocketAddress& addr) override;
  73. SocketAddress GetRemoteAddress() const override;
  74. int Close() override;
  75. ConnState GetState() const override;
  76. protected:
  77. void OnConnectEvent(AsyncSocket* socket) override;
  78. void OnCloseEvent(AsyncSocket* socket, int err) override;
  79. void ProcessInput(char* data, size_t* len) override;
  80. bool ShouldIssueConnect() const;
  81. void SendRequest();
  82. void ProcessLine(char* data, size_t len);
  83. void EndResponse();
  84. void Error(int error);
  85. private:
  86. SocketAddress proxy_, dest_;
  87. std::string agent_, user_, headers_;
  88. CryptString pass_;
  89. bool force_connect_;
  90. size_t content_length_;
  91. int defer_error_;
  92. bool expect_close_;
  93. enum ProxyState {
  94. PS_INIT,
  95. PS_LEADER,
  96. PS_AUTHENTICATE,
  97. PS_SKIP_HEADERS,
  98. PS_ERROR_HEADERS,
  99. PS_TUNNEL_HEADERS,
  100. PS_SKIP_BODY,
  101. PS_TUNNEL,
  102. PS_WAIT_CLOSE,
  103. PS_ERROR
  104. } state_;
  105. HttpAuthContext* context_;
  106. std::string unknown_mechanisms_;
  107. RTC_DISALLOW_COPY_AND_ASSIGN(AsyncHttpsProxySocket);
  108. };
  109. ///////////////////////////////////////////////////////////////////////////////
  110. // Implements a socket adapter that speaks the SOCKS proxy protocol.
  111. class AsyncSocksProxySocket : public BufferedReadAdapter {
  112. public:
  113. AsyncSocksProxySocket(AsyncSocket* socket,
  114. const SocketAddress& proxy,
  115. const std::string& username,
  116. const CryptString& password);
  117. ~AsyncSocksProxySocket() override;
  118. int Connect(const SocketAddress& addr) override;
  119. SocketAddress GetRemoteAddress() const override;
  120. int Close() override;
  121. ConnState GetState() const override;
  122. protected:
  123. void OnConnectEvent(AsyncSocket* socket) override;
  124. void ProcessInput(char* data, size_t* len) override;
  125. void SendHello();
  126. void SendConnect();
  127. void SendAuth();
  128. void Error(int error);
  129. private:
  130. enum State { SS_INIT, SS_HELLO, SS_AUTH, SS_CONNECT, SS_TUNNEL, SS_ERROR };
  131. State state_;
  132. SocketAddress proxy_, dest_;
  133. std::string user_;
  134. CryptString pass_;
  135. RTC_DISALLOW_COPY_AND_ASSIGN(AsyncSocksProxySocket);
  136. };
  137. } // namespace rtc
  138. #endif // RTC_BASE_SOCKET_ADAPTERS_H_