stream.hpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. //
  2. // ssl/stream.hpp
  3. // ~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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_STREAM_HPP
  11. #define BOOST_ASIO_SSL_STREAM_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/async_result.hpp>
  17. #include <boost/asio/detail/buffer_sequence_adapter.hpp>
  18. #include <boost/asio/detail/handler_type_requirements.hpp>
  19. #include <boost/asio/detail/non_const_lvalue.hpp>
  20. #include <boost/asio/detail/noncopyable.hpp>
  21. #include <boost/asio/detail/type_traits.hpp>
  22. #include <boost/asio/ssl/context.hpp>
  23. #include <boost/asio/ssl/detail/buffered_handshake_op.hpp>
  24. #include <boost/asio/ssl/detail/handshake_op.hpp>
  25. #include <boost/asio/ssl/detail/io.hpp>
  26. #include <boost/asio/ssl/detail/read_op.hpp>
  27. #include <boost/asio/ssl/detail/shutdown_op.hpp>
  28. #include <boost/asio/ssl/detail/stream_core.hpp>
  29. #include <boost/asio/ssl/detail/write_op.hpp>
  30. #include <boost/asio/ssl/stream_base.hpp>
  31. #include <boost/asio/detail/push_options.hpp>
  32. namespace boost {
  33. namespace asio {
  34. namespace ssl {
  35. /// Provides stream-oriented functionality using SSL.
  36. /**
  37. * The stream class template provides asynchronous and blocking stream-oriented
  38. * functionality using SSL.
  39. *
  40. * @par Thread Safety
  41. * @e Distinct @e objects: Safe.@n
  42. * @e Shared @e objects: Unsafe. The application must also ensure that all
  43. * asynchronous operations are performed within the same implicit or explicit
  44. * strand.
  45. *
  46. * @par Example
  47. * To use the SSL stream template with an ip::tcp::socket, you would write:
  48. * @code
  49. * boost::asio::io_context my_context;
  50. * boost::asio::ssl::context ctx(boost::asio::ssl::context::sslv23);
  51. * boost::asio::ssl::stream<asio:ip::tcp::socket> sock(my_context, ctx);
  52. * @endcode
  53. *
  54. * @par Concepts:
  55. * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
  56. */
  57. template <typename Stream>
  58. class stream :
  59. public stream_base,
  60. private noncopyable
  61. {
  62. public:
  63. /// The native handle type of the SSL stream.
  64. typedef SSL* native_handle_type;
  65. /// Structure for use with deprecated impl_type.
  66. struct impl_struct
  67. {
  68. SSL* ssl;
  69. };
  70. /// The type of the next layer.
  71. typedef typename remove_reference<Stream>::type next_layer_type;
  72. /// The type of the lowest layer.
  73. typedef typename next_layer_type::lowest_layer_type lowest_layer_type;
  74. /// The type of the executor associated with the object.
  75. typedef typename lowest_layer_type::executor_type executor_type;
  76. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  77. /// Construct a stream.
  78. /**
  79. * This constructor creates a stream and initialises the underlying stream
  80. * object.
  81. *
  82. * @param arg The argument to be passed to initialise the underlying stream.
  83. *
  84. * @param ctx The SSL context to be used for the stream.
  85. */
  86. template <typename Arg>
  87. stream(Arg&& arg, context& ctx)
  88. : next_layer_(BOOST_ASIO_MOVE_CAST(Arg)(arg)),
  89. core_(ctx.native_handle(), next_layer_.lowest_layer().get_executor())
  90. {
  91. }
  92. #else // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  93. template <typename Arg>
  94. stream(Arg& arg, context& ctx)
  95. : next_layer_(arg),
  96. core_(ctx.native_handle(), next_layer_.lowest_layer().get_executor())
  97. {
  98. }
  99. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  100. /// Destructor.
  101. /**
  102. * @note A @c stream object must not be destroyed while there are pending
  103. * asynchronous operations associated with it.
  104. */
  105. ~stream()
  106. {
  107. }
  108. /// Get the executor associated with the object.
  109. /**
  110. * This function may be used to obtain the executor object that the stream
  111. * uses to dispatch handlers for asynchronous operations.
  112. *
  113. * @return A copy of the executor that stream will use to dispatch handlers.
  114. */
  115. executor_type get_executor() BOOST_ASIO_NOEXCEPT
  116. {
  117. return next_layer_.lowest_layer().get_executor();
  118. }
  119. /// Get the underlying implementation in the native type.
  120. /**
  121. * This function may be used to obtain the underlying implementation of the
  122. * context. This is intended to allow access to context functionality that is
  123. * not otherwise provided.
  124. *
  125. * @par Example
  126. * The native_handle() function returns a pointer of type @c SSL* that is
  127. * suitable for passing to functions such as @c SSL_get_verify_result and
  128. * @c SSL_get_peer_certificate:
  129. * @code
  130. * boost::asio::ssl::stream<asio:ip::tcp::socket> sock(my_context, ctx);
  131. *
  132. * // ... establish connection and perform handshake ...
  133. *
  134. * if (X509* cert = SSL_get_peer_certificate(sock.native_handle()))
  135. * {
  136. * if (SSL_get_verify_result(sock.native_handle()) == X509_V_OK)
  137. * {
  138. * // ...
  139. * }
  140. * }
  141. * @endcode
  142. */
  143. native_handle_type native_handle()
  144. {
  145. return core_.engine_.native_handle();
  146. }
  147. /// Get a reference to the next layer.
  148. /**
  149. * This function returns a reference to the next layer in a stack of stream
  150. * layers.
  151. *
  152. * @return A reference to the next layer in the stack of stream layers.
  153. * Ownership is not transferred to the caller.
  154. */
  155. const next_layer_type& next_layer() const
  156. {
  157. return next_layer_;
  158. }
  159. /// Get a reference to the next layer.
  160. /**
  161. * This function returns a reference to the next layer in a stack of stream
  162. * layers.
  163. *
  164. * @return A reference to the next layer in the stack of stream layers.
  165. * Ownership is not transferred to the caller.
  166. */
  167. next_layer_type& next_layer()
  168. {
  169. return next_layer_;
  170. }
  171. /// Get a reference to the lowest layer.
  172. /**
  173. * This function returns a reference to the lowest layer in a stack of
  174. * stream layers.
  175. *
  176. * @return A reference to the lowest layer in the stack of stream layers.
  177. * Ownership is not transferred to the caller.
  178. */
  179. lowest_layer_type& lowest_layer()
  180. {
  181. return next_layer_.lowest_layer();
  182. }
  183. /// Get a reference to the lowest layer.
  184. /**
  185. * This function returns a reference to the lowest layer in a stack of
  186. * stream layers.
  187. *
  188. * @return A reference to the lowest layer in the stack of stream layers.
  189. * Ownership is not transferred to the caller.
  190. */
  191. const lowest_layer_type& lowest_layer() const
  192. {
  193. return next_layer_.lowest_layer();
  194. }
  195. /// Set the peer verification mode.
  196. /**
  197. * This function may be used to configure the peer verification mode used by
  198. * the stream. The new mode will override the mode inherited from the context.
  199. *
  200. * @param v A bitmask of peer verification modes. See @ref verify_mode for
  201. * available values.
  202. *
  203. * @throws boost::system::system_error Thrown on failure.
  204. *
  205. * @note Calls @c SSL_set_verify.
  206. */
  207. void set_verify_mode(verify_mode v)
  208. {
  209. boost::system::error_code ec;
  210. set_verify_mode(v, ec);
  211. boost::asio::detail::throw_error(ec, "set_verify_mode");
  212. }
  213. /// Set the peer verification mode.
  214. /**
  215. * This function may be used to configure the peer verification mode used by
  216. * the stream. The new mode will override the mode inherited from the context.
  217. *
  218. * @param v A bitmask of peer verification modes. See @ref verify_mode for
  219. * available values.
  220. *
  221. * @param ec Set to indicate what error occurred, if any.
  222. *
  223. * @note Calls @c SSL_set_verify.
  224. */
  225. BOOST_ASIO_SYNC_OP_VOID set_verify_mode(
  226. verify_mode v, boost::system::error_code& ec)
  227. {
  228. core_.engine_.set_verify_mode(v, ec);
  229. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  230. }
  231. /// Set the peer verification depth.
  232. /**
  233. * This function may be used to configure the maximum verification depth
  234. * allowed by the stream.
  235. *
  236. * @param depth Maximum depth for the certificate chain verification that
  237. * shall be allowed.
  238. *
  239. * @throws boost::system::system_error Thrown on failure.
  240. *
  241. * @note Calls @c SSL_set_verify_depth.
  242. */
  243. void set_verify_depth(int depth)
  244. {
  245. boost::system::error_code ec;
  246. set_verify_depth(depth, ec);
  247. boost::asio::detail::throw_error(ec, "set_verify_depth");
  248. }
  249. /// Set the peer verification depth.
  250. /**
  251. * This function may be used to configure the maximum verification depth
  252. * allowed by the stream.
  253. *
  254. * @param depth Maximum depth for the certificate chain verification that
  255. * shall be allowed.
  256. *
  257. * @param ec Set to indicate what error occurred, if any.
  258. *
  259. * @note Calls @c SSL_set_verify_depth.
  260. */
  261. BOOST_ASIO_SYNC_OP_VOID set_verify_depth(
  262. int depth, boost::system::error_code& ec)
  263. {
  264. core_.engine_.set_verify_depth(depth, ec);
  265. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  266. }
  267. /// Set the callback used to verify peer certificates.
  268. /**
  269. * This function is used to specify a callback function that will be called
  270. * by the implementation when it needs to verify a peer certificate.
  271. *
  272. * @param callback The function object to be used for verifying a certificate.
  273. * The function signature of the handler must be:
  274. * @code bool verify_callback(
  275. * bool preverified, // True if the certificate passed pre-verification.
  276. * verify_context& ctx // The peer certificate and other context.
  277. * ); @endcode
  278. * The return value of the callback is true if the certificate has passed
  279. * verification, false otherwise.
  280. *
  281. * @throws boost::system::system_error Thrown on failure.
  282. *
  283. * @note Calls @c SSL_set_verify.
  284. */
  285. template <typename VerifyCallback>
  286. void set_verify_callback(VerifyCallback callback)
  287. {
  288. boost::system::error_code ec;
  289. this->set_verify_callback(callback, ec);
  290. boost::asio::detail::throw_error(ec, "set_verify_callback");
  291. }
  292. /// Set the callback used to verify peer certificates.
  293. /**
  294. * This function is used to specify a callback function that will be called
  295. * by the implementation when it needs to verify a peer certificate.
  296. *
  297. * @param callback The function object to be used for verifying a certificate.
  298. * The function signature of the handler must be:
  299. * @code bool verify_callback(
  300. * bool preverified, // True if the certificate passed pre-verification.
  301. * verify_context& ctx // The peer certificate and other context.
  302. * ); @endcode
  303. * The return value of the callback is true if the certificate has passed
  304. * verification, false otherwise.
  305. *
  306. * @param ec Set to indicate what error occurred, if any.
  307. *
  308. * @note Calls @c SSL_set_verify.
  309. */
  310. template <typename VerifyCallback>
  311. BOOST_ASIO_SYNC_OP_VOID set_verify_callback(VerifyCallback callback,
  312. boost::system::error_code& ec)
  313. {
  314. core_.engine_.set_verify_callback(
  315. new detail::verify_callback<VerifyCallback>(callback), ec);
  316. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  317. }
  318. /// Perform SSL handshaking.
  319. /**
  320. * This function is used to perform SSL handshaking on the stream. The
  321. * function call will block until handshaking is complete or an error occurs.
  322. *
  323. * @param type The type of handshaking to be performed, i.e. as a client or as
  324. * a server.
  325. *
  326. * @throws boost::system::system_error Thrown on failure.
  327. */
  328. void handshake(handshake_type type)
  329. {
  330. boost::system::error_code ec;
  331. handshake(type, ec);
  332. boost::asio::detail::throw_error(ec, "handshake");
  333. }
  334. /// Perform SSL handshaking.
  335. /**
  336. * This function is used to perform SSL handshaking on the stream. The
  337. * function call will block until handshaking is complete or an error occurs.
  338. *
  339. * @param type The type of handshaking to be performed, i.e. as a client or as
  340. * a server.
  341. *
  342. * @param ec Set to indicate what error occurred, if any.
  343. */
  344. BOOST_ASIO_SYNC_OP_VOID handshake(handshake_type type,
  345. boost::system::error_code& ec)
  346. {
  347. detail::io(next_layer_, core_, detail::handshake_op(type), ec);
  348. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  349. }
  350. /// Perform SSL handshaking.
  351. /**
  352. * This function is used to perform SSL handshaking on the stream. The
  353. * function call will block until handshaking is complete or an error occurs.
  354. *
  355. * @param type The type of handshaking to be performed, i.e. as a client or as
  356. * a server.
  357. *
  358. * @param buffers The buffered data to be reused for the handshake.
  359. *
  360. * @throws boost::system::system_error Thrown on failure.
  361. */
  362. template <typename ConstBufferSequence>
  363. void handshake(handshake_type type, const ConstBufferSequence& buffers)
  364. {
  365. boost::system::error_code ec;
  366. handshake(type, buffers, ec);
  367. boost::asio::detail::throw_error(ec, "handshake");
  368. }
  369. /// Perform SSL handshaking.
  370. /**
  371. * This function is used to perform SSL handshaking on the stream. The
  372. * function call will block until handshaking is complete or an error occurs.
  373. *
  374. * @param type The type of handshaking to be performed, i.e. as a client or as
  375. * a server.
  376. *
  377. * @param buffers The buffered data to be reused for the handshake.
  378. *
  379. * @param ec Set to indicate what error occurred, if any.
  380. */
  381. template <typename ConstBufferSequence>
  382. BOOST_ASIO_SYNC_OP_VOID handshake(handshake_type type,
  383. const ConstBufferSequence& buffers, boost::system::error_code& ec)
  384. {
  385. detail::io(next_layer_, core_,
  386. detail::buffered_handshake_op<ConstBufferSequence>(type, buffers), ec);
  387. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  388. }
  389. /// Start an asynchronous SSL handshake.
  390. /**
  391. * This function is used to asynchronously perform an SSL handshake on the
  392. * stream. This function call always returns immediately.
  393. *
  394. * @param type The type of handshaking to be performed, i.e. as a client or as
  395. * a server.
  396. *
  397. * @param handler The handler to be called when the handshake operation
  398. * completes. Copies will be made of the handler as required. The equivalent
  399. * function signature of the handler must be:
  400. * @code void handler(
  401. * const boost::system::error_code& error // Result of operation.
  402. * ); @endcode
  403. */
  404. template <
  405. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code))
  406. HandshakeHandler
  407. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  408. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(HandshakeHandler,
  409. void (boost::system::error_code))
  410. async_handshake(handshake_type type,
  411. BOOST_ASIO_MOVE_ARG(HandshakeHandler) handler
  412. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  413. {
  414. return async_initiate<HandshakeHandler,
  415. void (boost::system::error_code)>(
  416. initiate_async_handshake(this), handler, type);
  417. }
  418. /// Start an asynchronous SSL handshake.
  419. /**
  420. * This function is used to asynchronously perform an SSL handshake on the
  421. * stream. This function call always returns immediately.
  422. *
  423. * @param type The type of handshaking to be performed, i.e. as a client or as
  424. * a server.
  425. *
  426. * @param buffers The buffered data to be reused for the handshake. Although
  427. * the buffers object may be copied as necessary, ownership of the underlying
  428. * buffers is retained by the caller, which must guarantee that they remain
  429. * valid until the handler is called.
  430. *
  431. * @param handler The handler to be called when the handshake operation
  432. * completes. Copies will be made of the handler as required. The equivalent
  433. * function signature of the handler must be:
  434. * @code void handler(
  435. * const boost::system::error_code& error, // Result of operation.
  436. * std::size_t bytes_transferred // Amount of buffers used in handshake.
  437. * ); @endcode
  438. */
  439. template <typename ConstBufferSequence,
  440. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  441. std::size_t)) BufferedHandshakeHandler
  442. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  443. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(BufferedHandshakeHandler,
  444. void (boost::system::error_code, std::size_t))
  445. async_handshake(handshake_type type, const ConstBufferSequence& buffers,
  446. BOOST_ASIO_MOVE_ARG(BufferedHandshakeHandler) handler
  447. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  448. {
  449. return async_initiate<BufferedHandshakeHandler,
  450. void (boost::system::error_code, std::size_t)>(
  451. initiate_async_buffered_handshake(this), handler, type, buffers);
  452. }
  453. /// Shut down SSL on the stream.
  454. /**
  455. * This function is used to shut down SSL on the stream. The function call
  456. * will block until SSL has been shut down or an error occurs.
  457. *
  458. * @throws boost::system::system_error Thrown on failure.
  459. */
  460. void shutdown()
  461. {
  462. boost::system::error_code ec;
  463. shutdown(ec);
  464. boost::asio::detail::throw_error(ec, "shutdown");
  465. }
  466. /// Shut down SSL on the stream.
  467. /**
  468. * This function is used to shut down SSL on the stream. The function call
  469. * will block until SSL has been shut down or an error occurs.
  470. *
  471. * @param ec Set to indicate what error occurred, if any.
  472. */
  473. BOOST_ASIO_SYNC_OP_VOID shutdown(boost::system::error_code& ec)
  474. {
  475. detail::io(next_layer_, core_, detail::shutdown_op(), ec);
  476. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  477. }
  478. /// Asynchronously shut down SSL on the stream.
  479. /**
  480. * This function is used to asynchronously shut down SSL on the stream. This
  481. * function call always returns immediately.
  482. *
  483. * @param handler The handler to be called when the handshake operation
  484. * completes. Copies will be made of the handler as required. The equivalent
  485. * function signature of the handler must be:
  486. * @code void handler(
  487. * const boost::system::error_code& error // Result of operation.
  488. * ); @endcode
  489. */
  490. template <
  491. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code))
  492. ShutdownHandler
  493. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  494. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ShutdownHandler,
  495. void (boost::system::error_code))
  496. async_shutdown(
  497. BOOST_ASIO_MOVE_ARG(ShutdownHandler) handler
  498. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  499. {
  500. return async_initiate<ShutdownHandler,
  501. void (boost::system::error_code)>(
  502. initiate_async_shutdown(this), handler);
  503. }
  504. /// Write some data to the stream.
  505. /**
  506. * This function is used to write data on the stream. The function call will
  507. * block until one or more bytes of data has been written successfully, or
  508. * until an error occurs.
  509. *
  510. * @param buffers The data to be written.
  511. *
  512. * @returns The number of bytes written.
  513. *
  514. * @throws boost::system::system_error Thrown on failure.
  515. *
  516. * @note The write_some operation may not transmit all of the data to the
  517. * peer. Consider using the @ref write function if you need to ensure that all
  518. * data is written before the blocking operation completes.
  519. */
  520. template <typename ConstBufferSequence>
  521. std::size_t write_some(const ConstBufferSequence& buffers)
  522. {
  523. boost::system::error_code ec;
  524. std::size_t n = write_some(buffers, ec);
  525. boost::asio::detail::throw_error(ec, "write_some");
  526. return n;
  527. }
  528. /// Write some data to the stream.
  529. /**
  530. * This function is used to write data on the stream. The function call will
  531. * block until one or more bytes of data has been written successfully, or
  532. * until an error occurs.
  533. *
  534. * @param buffers The data to be written to the stream.
  535. *
  536. * @param ec Set to indicate what error occurred, if any.
  537. *
  538. * @returns The number of bytes written. Returns 0 if an error occurred.
  539. *
  540. * @note The write_some operation may not transmit all of the data to the
  541. * peer. Consider using the @ref write function if you need to ensure that all
  542. * data is written before the blocking operation completes.
  543. */
  544. template <typename ConstBufferSequence>
  545. std::size_t write_some(const ConstBufferSequence& buffers,
  546. boost::system::error_code& ec)
  547. {
  548. return detail::io(next_layer_, core_,
  549. detail::write_op<ConstBufferSequence>(buffers), ec);
  550. }
  551. /// Start an asynchronous write.
  552. /**
  553. * This function is used to asynchronously write one or more bytes of data to
  554. * the stream. The function call always returns immediately.
  555. *
  556. * @param buffers The data to be written to the stream. Although the buffers
  557. * object may be copied as necessary, ownership of the underlying buffers is
  558. * retained by the caller, which must guarantee that they remain valid until
  559. * the handler is called.
  560. *
  561. * @param handler The handler to be called when the write operation completes.
  562. * Copies will be made of the handler as required. The equivalent function
  563. * signature of the handler must be:
  564. * @code void handler(
  565. * const boost::system::error_code& error, // Result of operation.
  566. * std::size_t bytes_transferred // Number of bytes written.
  567. * ); @endcode
  568. *
  569. * @note The async_write_some operation may not transmit all of the data to
  570. * the peer. Consider using the @ref async_write function if you need to
  571. * ensure that all data is written before the asynchronous operation
  572. * completes.
  573. */
  574. template <typename ConstBufferSequence,
  575. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  576. std::size_t)) WriteHandler
  577. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  578. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteHandler,
  579. void (boost::system::error_code, std::size_t))
  580. async_write_some(const ConstBufferSequence& buffers,
  581. BOOST_ASIO_MOVE_ARG(WriteHandler) handler
  582. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  583. {
  584. return async_initiate<WriteHandler,
  585. void (boost::system::error_code, std::size_t)>(
  586. initiate_async_write_some(this), handler, buffers);
  587. }
  588. /// Read some data from the stream.
  589. /**
  590. * This function is used to read data from the stream. The function call will
  591. * block until one or more bytes of data has been read successfully, or until
  592. * an error occurs.
  593. *
  594. * @param buffers The buffers into which the data will be read.
  595. *
  596. * @returns The number of bytes read.
  597. *
  598. * @throws boost::system::system_error Thrown on failure.
  599. *
  600. * @note The read_some operation may not read all of the requested number of
  601. * bytes. Consider using the @ref read function if you need to ensure that the
  602. * requested amount of data is read before the blocking operation completes.
  603. */
  604. template <typename MutableBufferSequence>
  605. std::size_t read_some(const MutableBufferSequence& buffers)
  606. {
  607. boost::system::error_code ec;
  608. std::size_t n = read_some(buffers, ec);
  609. boost::asio::detail::throw_error(ec, "read_some");
  610. return n;
  611. }
  612. /// Read some data from the stream.
  613. /**
  614. * This function is used to read data from the stream. The function call will
  615. * block until one or more bytes of data has been read successfully, or until
  616. * an error occurs.
  617. *
  618. * @param buffers The buffers into which the data will be read.
  619. *
  620. * @param ec Set to indicate what error occurred, if any.
  621. *
  622. * @returns The number of bytes read. Returns 0 if an error occurred.
  623. *
  624. * @note The read_some operation may not read all of the requested number of
  625. * bytes. Consider using the @ref read function if you need to ensure that the
  626. * requested amount of data is read before the blocking operation completes.
  627. */
  628. template <typename MutableBufferSequence>
  629. std::size_t read_some(const MutableBufferSequence& buffers,
  630. boost::system::error_code& ec)
  631. {
  632. return detail::io(next_layer_, core_,
  633. detail::read_op<MutableBufferSequence>(buffers), ec);
  634. }
  635. /// Start an asynchronous read.
  636. /**
  637. * This function is used to asynchronously read one or more bytes of data from
  638. * the stream. The function call always returns immediately.
  639. *
  640. * @param buffers The buffers into which the data will be read. Although the
  641. * buffers object may be copied as necessary, ownership of the underlying
  642. * buffers is retained by the caller, which must guarantee that they remain
  643. * valid until the handler is called.
  644. *
  645. * @param handler The handler to be called when the read operation completes.
  646. * Copies will be made of the handler as required. The equivalent function
  647. * signature of the handler must be:
  648. * @code void handler(
  649. * const boost::system::error_code& error, // Result of operation.
  650. * std::size_t bytes_transferred // Number of bytes read.
  651. * ); @endcode
  652. *
  653. * @note The async_read_some operation may not read all of the requested
  654. * number of bytes. Consider using the @ref async_read function if you need to
  655. * ensure that the requested amount of data is read before the asynchronous
  656. * operation completes.
  657. */
  658. template <typename MutableBufferSequence,
  659. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  660. std::size_t)) ReadHandler
  661. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  662. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
  663. void (boost::system::error_code, std::size_t))
  664. async_read_some(const MutableBufferSequence& buffers,
  665. BOOST_ASIO_MOVE_ARG(ReadHandler) handler
  666. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  667. {
  668. return async_initiate<ReadHandler,
  669. void (boost::system::error_code, std::size_t)>(
  670. initiate_async_read_some(this), handler, buffers);
  671. }
  672. private:
  673. class initiate_async_handshake
  674. {
  675. public:
  676. typedef typename stream::executor_type executor_type;
  677. explicit initiate_async_handshake(stream* self)
  678. : self_(self)
  679. {
  680. }
  681. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  682. {
  683. return self_->get_executor();
  684. }
  685. template <typename HandshakeHandler>
  686. void operator()(BOOST_ASIO_MOVE_ARG(HandshakeHandler) handler,
  687. handshake_type type) const
  688. {
  689. // If you get an error on the following line it means that your handler
  690. // does not meet the documented type requirements for a HandshakeHandler.
  691. BOOST_ASIO_HANDSHAKE_HANDLER_CHECK(HandshakeHandler, handler) type_check;
  692. boost::asio::detail::non_const_lvalue<HandshakeHandler> handler2(handler);
  693. detail::async_io(self_->next_layer_, self_->core_,
  694. detail::handshake_op(type), handler2.value);
  695. }
  696. private:
  697. stream* self_;
  698. };
  699. class initiate_async_buffered_handshake
  700. {
  701. public:
  702. typedef typename stream::executor_type executor_type;
  703. explicit initiate_async_buffered_handshake(stream* self)
  704. : self_(self)
  705. {
  706. }
  707. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  708. {
  709. return self_->get_executor();
  710. }
  711. template <typename BufferedHandshakeHandler, typename ConstBufferSequence>
  712. void operator()(BOOST_ASIO_MOVE_ARG(BufferedHandshakeHandler) handler,
  713. handshake_type type, const ConstBufferSequence& buffers) const
  714. {
  715. // If you get an error on the following line it means that your
  716. // handler does not meet the documented type requirements for a
  717. // BufferedHandshakeHandler.
  718. BOOST_ASIO_BUFFERED_HANDSHAKE_HANDLER_CHECK(
  719. BufferedHandshakeHandler, handler) type_check;
  720. boost::asio::detail::non_const_lvalue<
  721. BufferedHandshakeHandler> handler2(handler);
  722. detail::async_io(self_->next_layer_, self_->core_,
  723. detail::buffered_handshake_op<ConstBufferSequence>(type, buffers),
  724. handler2.value);
  725. }
  726. private:
  727. stream* self_;
  728. };
  729. class initiate_async_shutdown
  730. {
  731. public:
  732. typedef typename stream::executor_type executor_type;
  733. explicit initiate_async_shutdown(stream* self)
  734. : self_(self)
  735. {
  736. }
  737. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  738. {
  739. return self_->get_executor();
  740. }
  741. template <typename ShutdownHandler>
  742. void operator()(BOOST_ASIO_MOVE_ARG(ShutdownHandler) handler) const
  743. {
  744. // If you get an error on the following line it means that your handler
  745. // does not meet the documented type requirements for a ShutdownHandler.
  746. BOOST_ASIO_HANDSHAKE_HANDLER_CHECK(ShutdownHandler, handler) type_check;
  747. boost::asio::detail::non_const_lvalue<ShutdownHandler> handler2(handler);
  748. detail::async_io(self_->next_layer_, self_->core_,
  749. detail::shutdown_op(), handler2.value);
  750. }
  751. private:
  752. stream* self_;
  753. };
  754. class initiate_async_write_some
  755. {
  756. public:
  757. typedef typename stream::executor_type executor_type;
  758. explicit initiate_async_write_some(stream* self)
  759. : self_(self)
  760. {
  761. }
  762. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  763. {
  764. return self_->get_executor();
  765. }
  766. template <typename WriteHandler, typename ConstBufferSequence>
  767. void operator()(BOOST_ASIO_MOVE_ARG(WriteHandler) handler,
  768. const ConstBufferSequence& buffers) const
  769. {
  770. // If you get an error on the following line it means that your handler
  771. // does not meet the documented type requirements for a WriteHandler.
  772. BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  773. boost::asio::detail::non_const_lvalue<WriteHandler> handler2(handler);
  774. detail::async_io(self_->next_layer_, self_->core_,
  775. detail::write_op<ConstBufferSequence>(buffers), handler2.value);
  776. }
  777. private:
  778. stream* self_;
  779. };
  780. class initiate_async_read_some
  781. {
  782. public:
  783. typedef typename stream::executor_type executor_type;
  784. explicit initiate_async_read_some(stream* self)
  785. : self_(self)
  786. {
  787. }
  788. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  789. {
  790. return self_->get_executor();
  791. }
  792. template <typename ReadHandler, typename MutableBufferSequence>
  793. void operator()(BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
  794. const MutableBufferSequence& buffers) const
  795. {
  796. // If you get an error on the following line it means that your handler
  797. // does not meet the documented type requirements for a ReadHandler.
  798. BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  799. boost::asio::detail::non_const_lvalue<ReadHandler> handler2(handler);
  800. detail::async_io(self_->next_layer_, self_->core_,
  801. detail::read_op<MutableBufferSequence>(buffers), handler2.value);
  802. }
  803. private:
  804. stream* self_;
  805. };
  806. Stream next_layer_;
  807. detail::stream_core core_;
  808. };
  809. } // namespace ssl
  810. } // namespace asio
  811. } // namespace boost
  812. #include <boost/asio/detail/pop_options.hpp>
  813. #endif // BOOST_ASIO_SSL_STREAM_HPP