io.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. //
  2. // ssl/detail/io.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 BOOST_ASIO_SSL_DETAIL_IO_HPP
  11. #define BOOST_ASIO_SSL_DETAIL_IO_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. #include <boost/asio/detail/base_from_cancellation_state.hpp>
  17. #include <boost/asio/detail/handler_tracking.hpp>
  18. #include <boost/asio/ssl/detail/engine.hpp>
  19. #include <boost/asio/ssl/detail/stream_core.hpp>
  20. #include <boost/asio/write.hpp>
  21. #include <boost/asio/detail/push_options.hpp>
  22. namespace boost {
  23. namespace asio {
  24. namespace ssl {
  25. namespace detail {
  26. template <typename Stream, typename Operation>
  27. std::size_t io(Stream& next_layer, stream_core& core,
  28. const Operation& op, boost::system::error_code& ec)
  29. {
  30. boost::system::error_code io_ec;
  31. std::size_t bytes_transferred = 0;
  32. do switch (op(core.engine_, ec, bytes_transferred))
  33. {
  34. case engine::want_input_and_retry:
  35. // If the input buffer is empty then we need to read some more data from
  36. // the underlying transport.
  37. if (core.input_.size() == 0)
  38. {
  39. core.input_ = boost::asio::buffer(core.input_buffer_,
  40. next_layer.read_some(core.input_buffer_, io_ec));
  41. if (!ec)
  42. ec = io_ec;
  43. }
  44. // Pass the new input data to the engine.
  45. core.input_ = core.engine_.put_input(core.input_);
  46. // Try the operation again.
  47. continue;
  48. case engine::want_output_and_retry:
  49. // Get output data from the engine and write it to the underlying
  50. // transport.
  51. boost::asio::write(next_layer,
  52. core.engine_.get_output(core.output_buffer_), io_ec);
  53. if (!ec)
  54. ec = io_ec;
  55. // Try the operation again.
  56. continue;
  57. case engine::want_output:
  58. // Get output data from the engine and write it to the underlying
  59. // transport.
  60. boost::asio::write(next_layer,
  61. core.engine_.get_output(core.output_buffer_), io_ec);
  62. if (!ec)
  63. ec = io_ec;
  64. // Operation is complete. Return result to caller.
  65. core.engine_.map_error_code(ec);
  66. return bytes_transferred;
  67. default:
  68. // Operation is complete. Return result to caller.
  69. core.engine_.map_error_code(ec);
  70. return bytes_transferred;
  71. } while (!ec);
  72. // Operation failed. Return result to caller.
  73. core.engine_.map_error_code(ec);
  74. return 0;
  75. }
  76. template <typename Stream, typename Operation, typename Handler>
  77. class io_op
  78. : public boost::asio::detail::base_from_cancellation_state<Handler>
  79. {
  80. public:
  81. io_op(Stream& next_layer, stream_core& core,
  82. const Operation& op, Handler& handler)
  83. : boost::asio::detail::base_from_cancellation_state<Handler>(handler),
  84. next_layer_(next_layer),
  85. core_(core),
  86. op_(op),
  87. start_(0),
  88. want_(engine::want_nothing),
  89. bytes_transferred_(0),
  90. handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler))
  91. {
  92. }
  93. #if defined(BOOST_ASIO_HAS_MOVE)
  94. io_op(const io_op& other)
  95. : boost::asio::detail::base_from_cancellation_state<Handler>(other),
  96. next_layer_(other.next_layer_),
  97. core_(other.core_),
  98. op_(other.op_),
  99. start_(other.start_),
  100. want_(other.want_),
  101. ec_(other.ec_),
  102. bytes_transferred_(other.bytes_transferred_),
  103. handler_(other.handler_)
  104. {
  105. }
  106. io_op(io_op&& other)
  107. : boost::asio::detail::base_from_cancellation_state<Handler>(
  108. BOOST_ASIO_MOVE_CAST(
  109. boost::asio::detail::base_from_cancellation_state<Handler>)(
  110. other)),
  111. next_layer_(other.next_layer_),
  112. core_(other.core_),
  113. op_(BOOST_ASIO_MOVE_CAST(Operation)(other.op_)),
  114. start_(other.start_),
  115. want_(other.want_),
  116. ec_(other.ec_),
  117. bytes_transferred_(other.bytes_transferred_),
  118. handler_(BOOST_ASIO_MOVE_CAST(Handler)(other.handler_))
  119. {
  120. }
  121. #endif // defined(BOOST_ASIO_HAS_MOVE)
  122. void operator()(boost::system::error_code ec,
  123. std::size_t bytes_transferred = ~std::size_t(0), int start = 0)
  124. {
  125. switch (start_ = start)
  126. {
  127. case 1: // Called after at least one async operation.
  128. do
  129. {
  130. switch (want_ = op_(core_.engine_, ec_, bytes_transferred_))
  131. {
  132. case engine::want_input_and_retry:
  133. // If the input buffer already has data in it we can pass it to the
  134. // engine and then retry the operation immediately.
  135. if (core_.input_.size() != 0)
  136. {
  137. core_.input_ = core_.engine_.put_input(core_.input_);
  138. continue;
  139. }
  140. // The engine wants more data to be read from input. However, we
  141. // cannot allow more than one read operation at a time on the
  142. // underlying transport. The pending_read_ timer's expiry is set to
  143. // pos_infin if a read is in progress, and neg_infin otherwise.
  144. if (core_.expiry(core_.pending_read_) == core_.neg_infin())
  145. {
  146. // Prevent other read operations from being started.
  147. core_.pending_read_.expires_at(core_.pos_infin());
  148. BOOST_ASIO_HANDLER_LOCATION((
  149. __FILE__, __LINE__, Operation::tracking_name()));
  150. // Start reading some data from the underlying transport.
  151. next_layer_.async_read_some(
  152. boost::asio::buffer(core_.input_buffer_),
  153. BOOST_ASIO_MOVE_CAST(io_op)(*this));
  154. }
  155. else
  156. {
  157. BOOST_ASIO_HANDLER_LOCATION((
  158. __FILE__, __LINE__, Operation::tracking_name()));
  159. // Wait until the current read operation completes.
  160. core_.pending_read_.async_wait(BOOST_ASIO_MOVE_CAST(io_op)(*this));
  161. }
  162. // Yield control until asynchronous operation completes. Control
  163. // resumes at the "default:" label below.
  164. return;
  165. case engine::want_output_and_retry:
  166. case engine::want_output:
  167. // The engine wants some data to be written to the output. However, we
  168. // cannot allow more than one write operation at a time on the
  169. // underlying transport. The pending_write_ timer's expiry is set to
  170. // pos_infin if a write is in progress, and neg_infin otherwise.
  171. if (core_.expiry(core_.pending_write_) == core_.neg_infin())
  172. {
  173. // Prevent other write operations from being started.
  174. core_.pending_write_.expires_at(core_.pos_infin());
  175. BOOST_ASIO_HANDLER_LOCATION((
  176. __FILE__, __LINE__, Operation::tracking_name()));
  177. // Start writing all the data to the underlying transport.
  178. boost::asio::async_write(next_layer_,
  179. core_.engine_.get_output(core_.output_buffer_),
  180. BOOST_ASIO_MOVE_CAST(io_op)(*this));
  181. }
  182. else
  183. {
  184. BOOST_ASIO_HANDLER_LOCATION((
  185. __FILE__, __LINE__, Operation::tracking_name()));
  186. // Wait until the current write operation completes.
  187. core_.pending_write_.async_wait(BOOST_ASIO_MOVE_CAST(io_op)(*this));
  188. }
  189. // Yield control until asynchronous operation completes. Control
  190. // resumes at the "default:" label below.
  191. return;
  192. default:
  193. // The SSL operation is done and we can invoke the handler, but we
  194. // have to keep in mind that this function might be being called from
  195. // the async operation's initiating function. In this case we're not
  196. // allowed to call the handler directly. Instead, issue a zero-sized
  197. // read so the handler runs "as-if" posted using io_context::post().
  198. if (start)
  199. {
  200. BOOST_ASIO_HANDLER_LOCATION((
  201. __FILE__, __LINE__, Operation::tracking_name()));
  202. next_layer_.async_read_some(
  203. boost::asio::buffer(core_.input_buffer_, 0),
  204. BOOST_ASIO_MOVE_CAST(io_op)(*this));
  205. // Yield control until asynchronous operation completes. Control
  206. // resumes at the "default:" label below.
  207. return;
  208. }
  209. else
  210. {
  211. // Continue on to run handler directly.
  212. break;
  213. }
  214. }
  215. default:
  216. if (bytes_transferred == ~std::size_t(0))
  217. bytes_transferred = 0; // Timer cancellation, no data transferred.
  218. else if (!ec_)
  219. ec_ = ec;
  220. switch (want_)
  221. {
  222. case engine::want_input_and_retry:
  223. // Add received data to the engine's input.
  224. core_.input_ = boost::asio::buffer(
  225. core_.input_buffer_, bytes_transferred);
  226. core_.input_ = core_.engine_.put_input(core_.input_);
  227. // Release any waiting read operations.
  228. core_.pending_read_.expires_at(core_.neg_infin());
  229. // Check for cancellation before continuing.
  230. if (this->cancelled() != cancellation_type::none)
  231. {
  232. ec_ = boost::asio::error::operation_aborted;
  233. break;
  234. }
  235. // Try the operation again.
  236. continue;
  237. case engine::want_output_and_retry:
  238. // Release any waiting write operations.
  239. core_.pending_write_.expires_at(core_.neg_infin());
  240. // Check for cancellation before continuing.
  241. if (this->cancelled() != cancellation_type::none)
  242. {
  243. ec_ = boost::asio::error::operation_aborted;
  244. break;
  245. }
  246. // Try the operation again.
  247. continue;
  248. case engine::want_output:
  249. // Release any waiting write operations.
  250. core_.pending_write_.expires_at(core_.neg_infin());
  251. // Fall through to call handler.
  252. default:
  253. // Pass the result to the handler.
  254. op_.call_handler(handler_,
  255. core_.engine_.map_error_code(ec_),
  256. ec_ ? 0 : bytes_transferred_);
  257. // Our work here is done.
  258. return;
  259. }
  260. } while (!ec_);
  261. // Operation failed. Pass the result to the handler.
  262. op_.call_handler(handler_, core_.engine_.map_error_code(ec_), 0);
  263. }
  264. }
  265. //private:
  266. Stream& next_layer_;
  267. stream_core& core_;
  268. Operation op_;
  269. int start_;
  270. engine::want want_;
  271. boost::system::error_code ec_;
  272. std::size_t bytes_transferred_;
  273. Handler handler_;
  274. };
  275. template <typename Stream, typename Operation, typename Handler>
  276. inline asio_handler_allocate_is_deprecated
  277. asio_handler_allocate(std::size_t size,
  278. io_op<Stream, Operation, Handler>* this_handler)
  279. {
  280. #if defined(BOOST_ASIO_NO_DEPRECATED)
  281. boost_asio_handler_alloc_helpers::allocate(size, this_handler->handler_);
  282. return asio_handler_allocate_is_no_longer_used();
  283. #else // defined(BOOST_ASIO_NO_DEPRECATED)
  284. return boost_asio_handler_alloc_helpers::allocate(
  285. size, this_handler->handler_);
  286. #endif // defined(BOOST_ASIO_NO_DEPRECATED)
  287. }
  288. template <typename Stream, typename Operation, typename Handler>
  289. inline asio_handler_deallocate_is_deprecated
  290. asio_handler_deallocate(void* pointer, std::size_t size,
  291. io_op<Stream, Operation, Handler>* this_handler)
  292. {
  293. boost_asio_handler_alloc_helpers::deallocate(
  294. pointer, size, this_handler->handler_);
  295. #if defined(BOOST_ASIO_NO_DEPRECATED)
  296. return asio_handler_deallocate_is_no_longer_used();
  297. #endif // defined(BOOST_ASIO_NO_DEPRECATED)
  298. }
  299. template <typename Stream, typename Operation, typename Handler>
  300. inline bool asio_handler_is_continuation(
  301. io_op<Stream, Operation, Handler>* this_handler)
  302. {
  303. return this_handler->start_ == 0 ? true
  304. : boost_asio_handler_cont_helpers::is_continuation(this_handler->handler_);
  305. }
  306. template <typename Function, typename Stream,
  307. typename Operation, typename Handler>
  308. inline asio_handler_invoke_is_deprecated
  309. asio_handler_invoke(Function& function,
  310. io_op<Stream, Operation, Handler>* this_handler)
  311. {
  312. boost_asio_handler_invoke_helpers::invoke(
  313. function, this_handler->handler_);
  314. #if defined(BOOST_ASIO_NO_DEPRECATED)
  315. return asio_handler_invoke_is_no_longer_used();
  316. #endif // defined(BOOST_ASIO_NO_DEPRECATED)
  317. }
  318. template <typename Function, typename Stream,
  319. typename Operation, typename Handler>
  320. inline asio_handler_invoke_is_deprecated
  321. asio_handler_invoke(const Function& function,
  322. io_op<Stream, Operation, Handler>* this_handler)
  323. {
  324. boost_asio_handler_invoke_helpers::invoke(
  325. function, this_handler->handler_);
  326. #if defined(BOOST_ASIO_NO_DEPRECATED)
  327. return asio_handler_invoke_is_no_longer_used();
  328. #endif // defined(BOOST_ASIO_NO_DEPRECATED)
  329. }
  330. template <typename Stream, typename Operation, typename Handler>
  331. inline void async_io(Stream& next_layer, stream_core& core,
  332. const Operation& op, Handler& handler)
  333. {
  334. io_op<Stream, Operation, Handler>(
  335. next_layer, core, op, handler)(
  336. boost::system::error_code(), 0, 1);
  337. }
  338. } // namespace detail
  339. } // namespace ssl
  340. template <template <typename, typename> class Associator,
  341. typename Stream, typename Operation,
  342. typename Handler, typename DefaultCandidate>
  343. struct associator<Associator,
  344. ssl::detail::io_op<Stream, Operation, Handler>,
  345. DefaultCandidate>
  346. : Associator<Handler, DefaultCandidate>
  347. {
  348. static typename Associator<Handler, DefaultCandidate>::type
  349. get(const ssl::detail::io_op<Stream, Operation, Handler>& h)
  350. BOOST_ASIO_NOEXCEPT
  351. {
  352. return Associator<Handler, DefaultCandidate>::get(h.handler_);
  353. }
  354. static BOOST_ASIO_AUTO_RETURN_TYPE_PREFIX2(
  355. typename Associator<Handler, DefaultCandidate>::type)
  356. get(const ssl::detail::io_op<Stream, Operation, Handler>& h,
  357. const DefaultCandidate& c) BOOST_ASIO_NOEXCEPT
  358. BOOST_ASIO_AUTO_RETURN_TYPE_SUFFIX((
  359. Associator<Handler, DefaultCandidate>::get(h.handler_, c)))
  360. {
  361. return Associator<Handler, DefaultCandidate>::get(h.handler_, c);
  362. }
  363. };
  364. } // namespace asio
  365. } // namespace boost
  366. #include <boost/asio/detail/pop_options.hpp>
  367. #endif // BOOST_ASIO_SSL_DETAIL_IO_HPP