stream_core.hpp 6.5 KB

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