stream_core.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //
  2. // ssl/detail/stream_core.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_SSL_DETAIL_STREAM_CORE_HPP
  11. #define BOOST_ASIO_SSL_DETAIL_STREAM_CORE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
  17. # include <boost/asio/deadline_timer.hpp>
  18. #else // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
  19. # include <boost/asio/steady_timer.hpp>
  20. #endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
  21. #include <boost/asio/ssl/detail/engine.hpp>
  22. #include <boost/asio/buffer.hpp>
  23. #include <boost/asio/detail/push_options.hpp>
  24. namespace boost {
  25. namespace asio {
  26. namespace ssl {
  27. namespace detail {
  28. struct stream_core
  29. {
  30. // According to the OpenSSL documentation, this is the buffer size that is
  31. // sufficient to hold the largest possible TLS record.
  32. enum { max_tls_record_size = 17 * 1024 };
  33. template <typename Executor>
  34. stream_core(SSL_CTX* context, const Executor& ex)
  35. : engine_(context),
  36. pending_read_(ex),
  37. pending_write_(ex),
  38. output_buffer_space_(max_tls_record_size),
  39. output_buffer_(boost::asio::buffer(output_buffer_space_)),
  40. input_buffer_space_(max_tls_record_size),
  41. input_buffer_(boost::asio::buffer(input_buffer_space_))
  42. {
  43. pending_read_.expires_at(neg_infin());
  44. pending_write_.expires_at(neg_infin());
  45. }
  46. template <typename Executor>
  47. stream_core(SSL* ssl_impl, const Executor& ex)
  48. : engine_(ssl_impl),
  49. pending_read_(ex),
  50. pending_write_(ex),
  51. output_buffer_space_(max_tls_record_size),
  52. output_buffer_(boost::asio::buffer(output_buffer_space_)),
  53. input_buffer_space_(max_tls_record_size),
  54. input_buffer_(boost::asio::buffer(input_buffer_space_))
  55. {
  56. pending_read_.expires_at(neg_infin());
  57. pending_write_.expires_at(neg_infin());
  58. }
  59. stream_core(stream_core&& other)
  60. : engine_(static_cast<engine&&>(other.engine_)),
  61. #if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
  62. pending_read_(
  63. static_cast<boost::asio::deadline_timer&&>(
  64. other.pending_read_)),
  65. pending_write_(
  66. static_cast<boost::asio::deadline_timer&&>(
  67. other.pending_write_)),
  68. #else // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
  69. pending_read_(
  70. static_cast<boost::asio::steady_timer&&>(
  71. other.pending_read_)),
  72. pending_write_(
  73. static_cast<boost::asio::steady_timer&&>(
  74. other.pending_write_)),
  75. #endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
  76. output_buffer_space_(
  77. static_cast<std::vector<unsigned char>&&>(
  78. other.output_buffer_space_)),
  79. output_buffer_(other.output_buffer_),
  80. input_buffer_space_(
  81. static_cast<std::vector<unsigned char>&&>(
  82. other.input_buffer_space_)),
  83. input_buffer_(other.input_buffer_),
  84. input_(other.input_)
  85. {
  86. other.output_buffer_ = boost::asio::mutable_buffer(0, 0);
  87. other.input_buffer_ = boost::asio::mutable_buffer(0, 0);
  88. other.input_ = boost::asio::const_buffer(0, 0);
  89. }
  90. ~stream_core()
  91. {
  92. }
  93. stream_core& operator=(stream_core&& other)
  94. {
  95. if (this != &other)
  96. {
  97. engine_ = static_cast<engine&&>(other.engine_);
  98. #if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
  99. pending_read_ =
  100. static_cast<boost::asio::deadline_timer&&>(
  101. other.pending_read_);
  102. pending_write_ =
  103. static_cast<boost::asio::deadline_timer&&>(
  104. other.pending_write_);
  105. #else // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
  106. pending_read_ =
  107. static_cast<boost::asio::steady_timer&&>(
  108. other.pending_read_);
  109. pending_write_ =
  110. static_cast<boost::asio::steady_timer&&>(
  111. other.pending_write_);
  112. #endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
  113. output_buffer_space_ =
  114. static_cast<std::vector<unsigned char>&&>(
  115. other.output_buffer_space_);
  116. output_buffer_ = other.output_buffer_;
  117. input_buffer_space_ =
  118. static_cast<std::vector<unsigned char>&&>(
  119. other.input_buffer_space_);
  120. input_buffer_ = other.input_buffer_;
  121. input_ = other.input_;
  122. other.output_buffer_ = boost::asio::mutable_buffer(0, 0);
  123. other.input_buffer_ = boost::asio::mutable_buffer(0, 0);
  124. other.input_ = boost::asio::const_buffer(0, 0);
  125. }
  126. return *this;
  127. }
  128. // The SSL engine.
  129. engine engine_;
  130. #if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
  131. // Timer used for storing queued read operations.
  132. boost::asio::deadline_timer pending_read_;
  133. // Timer used for storing queued write operations.
  134. boost::asio::deadline_timer pending_write_;
  135. // Helper function for obtaining a time value that always fires.
  136. static boost::asio::deadline_timer::time_type neg_infin()
  137. {
  138. return boost::posix_time::neg_infin;
  139. }
  140. // Helper function for obtaining a time value that never fires.
  141. static boost::asio::deadline_timer::time_type pos_infin()
  142. {
  143. return boost::posix_time::pos_infin;
  144. }
  145. // Helper function to get a timer's expiry time.
  146. static boost::asio::deadline_timer::time_type expiry(
  147. const boost::asio::deadline_timer& timer)
  148. {
  149. return timer.expires_at();
  150. }
  151. #else // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
  152. // Timer used for storing queued read operations.
  153. boost::asio::steady_timer pending_read_;
  154. // Timer used for storing queued write operations.
  155. boost::asio::steady_timer pending_write_;
  156. // Helper function for obtaining a time value that always fires.
  157. static boost::asio::steady_timer::time_point neg_infin()
  158. {
  159. return (boost::asio::steady_timer::time_point::min)();
  160. }
  161. // Helper function for obtaining a time value that never fires.
  162. static boost::asio::steady_timer::time_point pos_infin()
  163. {
  164. return (boost::asio::steady_timer::time_point::max)();
  165. }
  166. // Helper function to get a timer's expiry time.
  167. static boost::asio::steady_timer::time_point expiry(
  168. const boost::asio::steady_timer& timer)
  169. {
  170. return timer.expiry();
  171. }
  172. #endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
  173. // Buffer space used to prepare output intended for the transport.
  174. std::vector<unsigned char> output_buffer_space_;
  175. // A buffer that may be used to prepare output intended for the transport.
  176. boost::asio::mutable_buffer output_buffer_;
  177. // Buffer space used to read input intended for the engine.
  178. std::vector<unsigned char> input_buffer_space_;
  179. // A buffer that may be used to read input intended for the engine.
  180. boost::asio::mutable_buffer input_buffer_;
  181. // The buffer pointing to the engine's unconsumed input.
  182. boost::asio::const_buffer input_;
  183. };
  184. } // namespace detail
  185. } // namespace ssl
  186. } // namespace asio
  187. } // namespace boost
  188. #include <boost/asio/detail/pop_options.hpp>
  189. #endif // BOOST_ASIO_SSL_DETAIL_STREAM_CORE_HPP