Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement full shutdown #78

Draft
wants to merge 19 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .github/workflows/unittest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
fail-fast: false
matrix:
boost_version: ["1.75.0", "1.76.0", "1.77.0", "1.78.0", "1.79.0"]
boost_version: ["1.75.0", "1.76.0", "1.77.0", "1.78.0", "1.79.0", "1.80.0", "1.81.0"]
os: [windows-2019, windows-2022]
toolset: [v141, v142, v143, ClangCL]
build_type: [Debug, Release]
Expand All @@ -32,6 +32,8 @@ jobs:
- { boost_version: "1.77.0", toolset: v143 }
- { boost_version: "1.78.0", toolset: v141 }
- { boost_version: "1.79.0", toolset: v141 }
- { boost_version: "1.80.0", toolset: v141 }
- { boost_version: "1.81.0", toolset: v141 }
include:
- boost_version: "1.79.0"
os: windows-2022
Expand Down Expand Up @@ -62,8 +64,9 @@ jobs:
with:
fetch-depth: 0

# For Boost Versions >= 1.78, the toolset parameter has to be specified to install-boost.
- name: Add boost toolset to environment
if: contains(fromJson('["1.78.0", "1.79.0"]'), matrix.boost_version)
if: contains(fromJson('["1.78.0", "1.79.0", "1.80.0", "1.81.0"]'), matrix.boost_version)
run: echo BOOST_TOOLSET=$([[ "${{matrix.generator}}" == "MinGW Makefiles" ]] && echo "mingw" || echo "msvc") >> $GITHUB_ENV

# The platform_version passed to boost-install determines the msvc toolset version for which static libs are installed.
Expand Down
103 changes: 37 additions & 66 deletions include/boost/wintls/detail/async_handshake.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <boost/wintls/handshake_type.hpp>

#include <boost/wintls/detail/post_self.hpp>
#include <boost/wintls/detail/sspi_handshake.hpp>

#include <boost/asio/coroutine.hpp>
Expand All @@ -18,17 +19,24 @@ namespace boost {
namespace wintls {
namespace detail {

template <typename NextLayer>
template<typename NextLayer>
struct async_handshake : boost::asio::coroutine {
async_handshake(NextLayer& next_layer, detail::sspi_handshake& handshake, handshake_type type)
: next_layer_(next_layer)
, handshake_(handshake)
, entry_count_(0)
, state_(state::idle) {
handshake_(type);
async_handshake(NextLayer& next_layer, detail::sspi_handshake& handshake, handshake_mode mode)
: next_layer_(next_layer)
, handshake_(handshake)
, mode_(mode)
, entry_count_(0) {
switch (mode) {
case handshake_mode::init:
handshake_.init();
break;
case handshake_mode::shutdown:
handshake_.shutdown();
break;
}
}

template <typename Self>
template<typename Self>
void operator()(Self& self, boost::system::error_code ec = {}, std::size_t length = 0) {
if (ec) {
self.complete(ec);
Expand All @@ -40,94 +48,57 @@ struct async_handshake : boost::asio::coroutine {
return entry_count_ > 1;
};

switch(state_) {
case state::reading:
handshake_.size_read(length);
state_ = state::idle;
break;
case state::writing:
handshake_.size_written(length);
state_ = state::idle;
break;
case state::idle:
break;
}

detail::sspi_handshake::state handshake_state;
sspi_handshake::state handshake_state;
BOOST_ASIO_CORO_REENTER(*this) {
while((handshake_state = handshake_()) != detail::sspi_handshake::state::done) {
if (handshake_state == detail::sspi_handshake::state::data_needed) {
while (true) {
handshake_state = handshake_();
if (handshake_state == sspi_handshake::state::data_needed) {
BOOST_ASIO_CORO_YIELD {
state_ = state::reading;
next_layer_.async_read_some(handshake_.in_buffer(), std::move(self));
}
handshake_.size_read(length);
continue;
}

if (handshake_state == detail::sspi_handshake::state::data_available) {
if (handshake_state == sspi_handshake::state::data_available) {
BOOST_ASIO_CORO_YIELD {
state_ = state::writing;
net::async_write(next_layer_, handshake_.out_buffer(), std::move(self));
}
handshake_.size_written(length);
continue;
}

if (handshake_state == detail::sspi_handshake::state::error) {
if (!is_continuation()) {
BOOST_ASIO_CORO_YIELD {
auto e = self.get_executor();
net::post(e, [self = std::move(self), ec, length]() mutable { self(ec, length); });
}
}
self.complete(handshake_.last_error());
return;
}

if (handshake_state == detail::sspi_handshake::state::done_with_data) {
BOOST_ASIO_CORO_YIELD {
state_ = state::writing;
net::async_write(next_layer_, handshake_.out_buffer(), std::move(self));
}
if (handshake_state == sspi_handshake::state::error) {
ec = handshake_.last_error();
break;
}

if (handshake_state == detail::sspi_handshake::state::error_with_data) {
BOOST_ASIO_CORO_YIELD {
state_ = state::writing;
net::async_write(next_layer_, handshake_.out_buffer(), std::move(self));
}
if (!is_continuation()) {
BOOST_ASIO_CORO_YIELD {
auto e = self.get_executor();
net::post(e, [self = std::move(self), ec, length]() mutable { self(ec, length); });
}
if (handshake_state == sspi_handshake::state::done) {
BOOST_ASSERT(!handshake_.last_error());
if (mode_ == handshake_mode::init) {
ec = error::make_error_code(handshake_.manual_auth());
}
self.complete(handshake_.last_error());
return;
break;
}
}

if (ec == net::error::eof) {
ec = wintls::error::stream_truncated;
}
if (!is_continuation()) {
BOOST_ASIO_CORO_YIELD {
auto e = self.get_executor();
net::post(e, [self = std::move(self), ec, length]() mutable { self(ec, length); });
post_self(self, next_layer_, ec, length);
}
}
BOOST_ASSERT(!handshake_.last_error());
self.complete(handshake_.last_error());
self.complete(ec);
}
}

private:
NextLayer& next_layer_;
detail::sspi_handshake& handshake_;
sspi_handshake& handshake_;
handshake_mode mode_;
int entry_count_;
std::vector<char> input_;
enum class state {
idle,
reading,
writing
} state_;
};

} // namespace detail
Expand Down
4 changes: 2 additions & 2 deletions include/boost/wintls/detail/async_read.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#ifndef BOOST_WINTLS_DETAIL_ASYNC_READ_HPP
#define BOOST_WINTLS_DETAIL_ASYNC_READ_HPP

#include <boost/wintls/detail/post_self.hpp>
#include <boost/wintls/detail/sspi_decrypt.hpp>

#include <boost/asio/coroutine.hpp>
Expand Down Expand Up @@ -50,8 +51,7 @@ struct async_read : boost::asio::coroutine {
if (state == detail::sspi_decrypt::state::error) {
if (!is_continuation()) {
BOOST_ASIO_CORO_YIELD {
auto e = self.get_executor();
net::post(e, [self = std::move(self), ec, size_read]() mutable { self(ec, size_read); });
post_self(self, next_layer_, ec, size_read);
}
}
ec = decrypt_.last_error();
Expand Down
72 changes: 0 additions & 72 deletions include/boost/wintls/detail/async_shutdown.hpp

This file was deleted.

48 changes: 48 additions & 0 deletions include/boost/wintls/detail/post_self.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// Copyright (c) 2020 Kasper Laudrup (laudrup at stacktrace dot dk)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#ifndef BOOST_WINTLS_DETAIL_POST_SELF_HPP
#define BOOST_WINTLS_DETAIL_POST_SELF_HPP

#if BOOST_VERSION >= 108000
#include <boost/asio/append.hpp>
#endif
#include <boost/asio/post.hpp>
#include <boost/core/ignore_unused.hpp>
#include <boost/version.hpp>

namespace boost {
namespace wintls {
namespace detail {

// If a composed asynchronous operation completes immediately (due to an error)
// we do not want to call self.complete() directly as this may produce an infinite recursion in some cases.
// Instead, we post the intermediate completion handler (self) once.
// To achieve consistent behavior to non-erroneous cases, we post to the executor of the I/O object.
// Note that this only got accessible through self by get_io_executor since boost 1.81.
template<typename Self, typename IoObject, typename... Args>
auto post_self(Self& self, IoObject& io_object, boost::system::error_code ec, std::size_t length) {
#if BOOST_VERSION >= 108100
boost::ignore_unused(io_object);
auto ex = self.get_io_executor();
return boost::asio::post(ex, boost::asio::append(std::move(self), ec, length));
#elif BOOST_VERSION >= 108000
return boost::asio::post(io_object.get_executor(), boost::asio::append(std::move(self), ec, length));
#else
auto ex = io_object.get_executor();
// If the completion token associated with self had an associated executor,
// allocator or cancellation slot, we loose these here.
// Therefore, above solutions are better!
return boost::asio::post(ex, [self = std::move(self), ec, length]() mutable { self(ec, length); });
#endif
}

} // namespace detail
} // namespace wintls
} // namespace boost

#endif //BOOST_WINTLS_DETAIL_POST_SELF_HPP
Loading