1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
/*
* TLS Stream Helper
* (C) 2018-2019 Jack Lloyd
* 2018-2019 Hannes Rantzsch, Tim Oesterreich, Rene Meusel
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#ifndef BOTAN_ASIO_ASYNC_HANDSHAKE_OP_H_
#define BOTAN_ASIO_ASYNC_HANDSHAKE_OP_H_
#include <botan/internal/asio_async_write_op.h>
#include <botan/internal/asio_convert_exceptions.h>
#include <botan/internal/asio_stream_core.h>
#include <botan/internal/asio_includes.h>
namespace Botan {
namespace TLS {
template <class Handler, class StreamLayer, class Channel>
struct AsyncHandshakeOperation
{
template<class HandlerT>
AsyncHandshakeOperation(
HandlerT&& handler,
StreamLayer& nextLayer,
Channel* channel,
StreamCore& core)
: m_handler(std::forward<HandlerT>(handler))
, m_nextLayer(nextLayer)
, m_channel(channel)
, m_core(core) {}
AsyncHandshakeOperation(AsyncHandshakeOperation&&) = default;
void operator()(boost::system::error_code ec,
std::size_t bytesTransferred = 0, int start = 0)
{
// process tls packets from socket first
if(bytesTransferred > 0)
{
boost::asio::const_buffer read_buffer {m_core.input_buffer.data(), bytesTransferred};
try
{
m_channel->received_data(
static_cast<const uint8_t*>(read_buffer.data()),
read_buffer.size());
}
catch(const std::exception&)
{
ec = convertException();
m_handler(ec);
return;
}
}
// send tls packets
if(m_core.hasDataToSend())
{
AsyncWriteOperation<AsyncHandshakeOperation<typename std::decay<Handler>::type, StreamLayer, Channel>>
op{std::move(*this), m_core, 0};
boost::asio::async_write(m_nextLayer, m_core.sendBuffer(), std::move(op));
return;
}
if(!m_channel->is_active() && !ec)
{
// we need more tls data from the socket
m_nextLayer.async_read_some(m_core.input_buffer, std::move(*this));
return;
}
if(start)
{
// don't call the handler directly, similar to io_context.post
m_nextLayer.async_read_some(
boost::asio::mutable_buffer(m_core.input_buffer.data(), 0), std::move(*this));
return;
}
m_handler(ec);
}
private:
Handler m_handler;
StreamLayer& m_nextLayer;
Channel* m_channel;
StreamCore& m_core;
};
} // namespace TLS
} // namespace Botan
#endif
|