1 //
2 // ssl/detail/buffered_handshake_op.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2021 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 
11 #ifndef BOOST_ASIO_SSL_DETAIL_BUFFERED_HANDSHAKE_OP_HPP
12 #define BOOST_ASIO_SSL_DETAIL_BUFFERED_HANDSHAKE_OP_HPP
13 
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
18 #include <boost/asio/detail/config.hpp>
19 
20 #include <boost/asio/ssl/detail/engine.hpp>
21 
22 #include <boost/asio/detail/push_options.hpp>
23 
24 namespace boost {
25 namespace asio {
26 namespace ssl {
27 namespace detail {
28 
29 template <typename ConstBufferSequence>
30 class buffered_handshake_op
31 {
32 public:
tracking_name()33   static BOOST_ASIO_CONSTEXPR const char* tracking_name()
34   {
35     return "ssl::stream<>::async_buffered_handshake";
36   }
37 
buffered_handshake_op(stream_base::handshake_type type,const ConstBufferSequence & buffers)38   buffered_handshake_op(stream_base::handshake_type type,
39       const ConstBufferSequence& buffers)
40     : type_(type),
41       buffers_(buffers),
42       total_buffer_size_(boost::asio::buffer_size(buffers_))
43   {
44   }
45 
operator ()(engine & eng,boost::system::error_code & ec,std::size_t & bytes_transferred) const46   engine::want operator()(engine& eng,
47       boost::system::error_code& ec,
48       std::size_t& bytes_transferred) const
49   {
50     return this->process(eng, ec, bytes_transferred,
51         boost::asio::buffer_sequence_begin(buffers_),
52         boost::asio::buffer_sequence_end(buffers_));
53   }
54 
55   template <typename Handler>
call_handler(Handler & handler,const boost::system::error_code & ec,const std::size_t & bytes_transferred) const56   void call_handler(Handler& handler,
57       const boost::system::error_code& ec,
58       const std::size_t& bytes_transferred) const
59   {
60     BOOST_ASIO_MOVE_OR_LVALUE(Handler)(handler)(ec, bytes_transferred);
61   }
62 
63 private:
64   template <typename Iterator>
process(engine & eng,boost::system::error_code & ec,std::size_t & bytes_transferred,Iterator begin,Iterator end) const65   engine::want process(engine& eng,
66       boost::system::error_code& ec,
67       std::size_t& bytes_transferred,
68       Iterator begin, Iterator end) const
69   {
70     Iterator iter = begin;
71     std::size_t accumulated_size = 0;
72 
73     for (;;)
74     {
75       engine::want want = eng.handshake(type_, ec);
76       if (want != engine::want_input_and_retry
77           || bytes_transferred == total_buffer_size_)
78         return want;
79 
80       // Find the next buffer piece to be fed to the engine.
81       while (iter != end)
82       {
83         const_buffer buffer(*iter);
84 
85         // Skip over any buffers which have already been consumed by the engine.
86         if (bytes_transferred >= accumulated_size + buffer.size())
87         {
88           accumulated_size += buffer.size();
89           ++iter;
90           continue;
91         }
92 
93         // The current buffer may have been partially consumed by the engine on
94         // a previous iteration. If so, adjust the buffer to point to the
95         // unused portion.
96         if (bytes_transferred > accumulated_size)
97           buffer = buffer + (bytes_transferred - accumulated_size);
98 
99         // Pass the buffer to the engine, and update the bytes transferred to
100         // reflect the total number of bytes consumed so far.
101         bytes_transferred += buffer.size();
102         buffer = eng.put_input(buffer);
103         bytes_transferred -= buffer.size();
104         break;
105       }
106     }
107   }
108 
109   stream_base::handshake_type type_;
110   ConstBufferSequence buffers_;
111   std::size_t total_buffer_size_;
112 };
113 
114 } // namespace detail
115 } // namespace ssl
116 } // namespace asio
117 } // namespace boost
118 
119 #include <boost/asio/detail/pop_options.hpp>
120 
121 #endif // BOOST_ASIO_SSL_DETAIL_BUFFERED_HANDSHAKE_OP_HPP
122