stream_core.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //
  2. // ssl/detail/stream_core.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 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. #if defined(ASIO_HAS_MOVE)
  59. stream_core(stream_core&& other)
  60. : engine_(ASIO_MOVE_CAST(engine)(other.engine_)),
  61. #if defined(ASIO_HAS_BOOST_DATE_TIME)
  62. pending_read_(
  63. ASIO_MOVE_CAST(asio::deadline_timer)(
  64. other.pending_read_)),
  65. pending_write_(
  66. ASIO_MOVE_CAST(asio::deadline_timer)(
  67. other.pending_write_)),
  68. #else // defined(ASIO_HAS_BOOST_DATE_TIME)
  69. pending_read_(
  70. ASIO_MOVE_CAST(asio::steady_timer)(
  71. other.pending_read_)),
  72. pending_write_(
  73. ASIO_MOVE_CAST(asio::steady_timer)(
  74. other.pending_write_)),
  75. #endif // defined(ASIO_HAS_BOOST_DATE_TIME)
  76. output_buffer_space_(
  77. ASIO_MOVE_CAST(std::vector<unsigned char>)(
  78. other.output_buffer_space_)),
  79. output_buffer_(other.output_buffer_),
  80. input_buffer_space_(
  81. ASIO_MOVE_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_ = asio::mutable_buffer(0, 0);
  87. other.input_buffer_ = asio::mutable_buffer(0, 0);
  88. other.input_ = asio::const_buffer(0, 0);
  89. }
  90. #endif // defined(ASIO_HAS_MOVE)
  91. ~stream_core()
  92. {
  93. }
  94. #if defined(ASIO_HAS_MOVE)
  95. stream_core& operator=(stream_core&& other)
  96. {
  97. if (this != &other)
  98. {
  99. engine_ = ASIO_MOVE_CAST(engine)(other.engine_);
  100. #if defined(ASIO_HAS_BOOST_DATE_TIME)
  101. pending_read_ =
  102. ASIO_MOVE_CAST(asio::deadline_timer)(
  103. other.pending_read_);
  104. pending_write_ =
  105. ASIO_MOVE_CAST(asio::deadline_timer)(
  106. other.pending_write_);
  107. #else // defined(ASIO_HAS_BOOST_DATE_TIME)
  108. pending_read_ =
  109. ASIO_MOVE_CAST(asio::steady_timer)(
  110. other.pending_read_);
  111. pending_write_ =
  112. ASIO_MOVE_CAST(asio::steady_timer)(
  113. other.pending_write_);
  114. #endif // defined(ASIO_HAS_BOOST_DATE_TIME)
  115. output_buffer_space_ =
  116. ASIO_MOVE_CAST(std::vector<unsigned char>)(
  117. other.output_buffer_space_);
  118. output_buffer_ = other.output_buffer_;
  119. input_buffer_space_ =
  120. ASIO_MOVE_CAST(std::vector<unsigned char>)(
  121. other.input_buffer_space_);
  122. input_buffer_ = other.input_buffer_;
  123. input_ = other.input_;
  124. other.output_buffer_ = asio::mutable_buffer(0, 0);
  125. other.input_buffer_ = asio::mutable_buffer(0, 0);
  126. other.input_ = asio::const_buffer(0, 0);
  127. }
  128. return *this;
  129. }
  130. #endif // defined(ASIO_HAS_MOVE)
  131. // The SSL engine.
  132. engine engine_;
  133. #if defined(ASIO_HAS_BOOST_DATE_TIME)
  134. // Timer used for storing queued read operations.
  135. asio::deadline_timer pending_read_;
  136. // Timer used for storing queued write operations.
  137. asio::deadline_timer pending_write_;
  138. // Helper function for obtaining a time value that always fires.
  139. static asio::deadline_timer::time_type neg_infin()
  140. {
  141. return boost::posix_time::neg_infin;
  142. }
  143. // Helper function for obtaining a time value that never fires.
  144. static asio::deadline_timer::time_type pos_infin()
  145. {
  146. return boost::posix_time::pos_infin;
  147. }
  148. // Helper function to get a timer's expiry time.
  149. static asio::deadline_timer::time_type expiry(
  150. const asio::deadline_timer& timer)
  151. {
  152. return timer.expires_at();
  153. }
  154. #else // defined(ASIO_HAS_BOOST_DATE_TIME)
  155. // Timer used for storing queued read operations.
  156. asio::steady_timer pending_read_;
  157. // Timer used for storing queued write operations.
  158. asio::steady_timer pending_write_;
  159. // Helper function for obtaining a time value that always fires.
  160. static asio::steady_timer::time_point neg_infin()
  161. {
  162. return (asio::steady_timer::time_point::min)();
  163. }
  164. // Helper function for obtaining a time value that never fires.
  165. static asio::steady_timer::time_point pos_infin()
  166. {
  167. return (asio::steady_timer::time_point::max)();
  168. }
  169. // Helper function to get a timer's expiry time.
  170. static asio::steady_timer::time_point expiry(
  171. const asio::steady_timer& timer)
  172. {
  173. return timer.expiry();
  174. }
  175. #endif // defined(ASIO_HAS_BOOST_DATE_TIME)
  176. // Buffer space used to prepare output intended for the transport.
  177. std::vector<unsigned char> output_buffer_space_;
  178. // A buffer that may be used to prepare output intended for the transport.
  179. asio::mutable_buffer output_buffer_;
  180. // Buffer space used to read input intended for the engine.
  181. std::vector<unsigned char> input_buffer_space_;
  182. // A buffer that may be used to read input intended for the engine.
  183. asio::mutable_buffer input_buffer_;
  184. // The buffer pointing to the engine's unconsumed input.
  185. asio::const_buffer input_;
  186. };
  187. } // namespace detail
  188. } // namespace ssl
  189. } // namespace asio
  190. #include "asio/detail/pop_options.hpp"
  191. #endif // ASIO_SSL_DETAIL_STREAM_CORE_HPP