9 #include <boost/asio.hpp>
10 #include <boost/bind/bind.hpp>
16 using boost::asio::async_write;
17 using boost::asio::buffer;
18 using boost::asio::buffer_cast;
19 using boost::asio::mutable_buffer;
20 using boost::asio::mutable_buffers_1;
21 using boost::asio::null_buffers;
22 using boost::asio::placeholders::error;
23 using boost::asio::placeholders::bytes_transferred;
24 using boost::asio::ssl::stream_base;
26 using boost::function;
27 using boost::system::error_code;
32 using namespace boost::placeholders;
40 :
Task(task_id, session->GetSessionInstance()),
41 session_(session), read_fn_(read_fn), buffer_(buffer) {
44 if (session_->IsEstablished()) {
45 session_->ssl_last_read_len_ = BufferSize(buffer_);
47 if (session_->IsReaderDeferred()) {
50 session_->stats_.read_blocked++;
51 session_->server()->stats_.read_blocked++;
53 if (session_->IsSslDisabled()) {
54 session_->AsyncReadStart();
55 }
else if (!session_->IsSslHandShakeInProgress()) {
56 session_->AsyncReadStart();
62 string Description()
const {
return "SslSession::SslReader"; }
71 bool async_read_ready)
73 ssl_socket_(ssl_socket),
74 ssl_handshake_in_progress_(false),
75 ssl_handshake_success_(false),
77 ssl_handshake_delayed_(false),
78 ssl_last_read_len_(0) {
90 size_t bytes_transferred) {
91 Buffer rdbuf(buffer_cast<const uint8_t *>(buffer), bytes_transferred);
137 #if defined(SSL_R_SHORT_READ)
138 if (ec.value() == ERR_PACK(ERR_LIB_SSL, 0, SSL_R_SHORT_READ)) {
141 #elif defined(BOOST_ASIO_SSL_ERROR_STREAM_TRUNCATED)
142 if (ec.value() == boost::asio::ssl::error::stream_truncated) {
156 return ssl_socket_->read_some(mutable_buffers_1(buffer), *error);
161 async_write(*
ssl_socket_.get(), buffer(data, size),
171 const error_code &error) {
172 session->ssl_handshake_in_progress_ =
false;
174 session->SetSslHandShakeSuccess();
176 session->SetSslHandShakeFailure();
179 if (session->socket() != NULL && !(session->IsClosed())) {
186 srand(
static_cast<unsigned>(time(0)));
188 session->ssl_handshake_in_progress_ =
true;
189 if (session->IsServerSession()) {
190 session->ssl_socket_->async_handshake(stream_base::server,
194 session->ssl_socket_->async_handshake(stream_base::client,
boost::asio::io_context * io_service()
SslReader(int task_id, SslSessionPtr session, ReadHandler read_fn, Buffer buffer)
virtual bool Run()
Code to execute in a task. Returns true if task is completed. Return false to reschedule the task.
string Description() const
Gives a description of the task.
function< void(Buffer)> ReadHandler
static bool IsSocketErrorHard(const boost::system::error_code &ec)
size_t ReadSome(boost::asio::mutable_buffer buffer, boost::system::error_code *error)
virtual Socket * socket() const
boost::asio::ssl::stream< boost::asio::ip::tcp::socket > SslSocket
virtual void AsyncReadSome()
void AsyncWrite(const uint8_t *data, std::size_t size)
SslSession(SslServer *server, SslSocket *socket, bool async_read_ready=true)
static void SslHandShakeCallback(SslHandShakeCallbackHandler cb, SslSessionPtr, const boost::system::error_code &error)
size_t ssl_last_read_len_
virtual Task * CreateReaderTask(boost::asio::mutable_buffer, size_t)
boost::scoped_ptr< SslSocket > ssl_socket_
bool ssl_handshake_delayed_
virtual size_t GetReadBufferSize() const
void TriggerSslHandShake(SslHandShakeCallbackHandler)
bool IsSslHandShakeSuccessLocked()
static void TriggerSslHandShakeInternal(SslSessionPtr ptr, SslHandShakeCallbackHandler cb)
bool ssl_handshake_in_progress_
Task is a class to describe a computational task within OpenSDN control plane applications....
EventManager * event_manager()
virtual int reader_task_id() const
virtual void AsyncWrite(const uint8_t *data, std::size_t size)
static bool IsSocketErrorHard(const boost::system::error_code &ec)
void TriggerAsyncReadHandler()
virtual void AsyncReadSome()
virtual void OnRead(Buffer buffer)=0
static void AsyncWriteHandler(TcpSessionPtr session, const boost::system::error_code &error, std::size_t bytes_transferred)
boost::asio::const_buffer Buffer
boost::intrusive_ptr< TcpSession > TcpSessionPtr
static const int kDefaultBufferSize
boost::asio::ip::tcp::socket Socket
virtual size_t ReadSome(boost::asio::mutable_buffer buffer, boost::system::error_code *error)
boost::intrusive_ptr< SslSession > SslSessionPtr
boost::function< void(SslSessionPtr, const boost::system::error_code &error)> SslHandShakeCallbackHandler
static uint64_t UTCTimestampUsec()