Added wrapper for openssl types.
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
#endif // !MIJIN_ENABLE_OPENSSL
|
||||
|
||||
#include <memory>
|
||||
#include "./openssl_wrappers.hpp"
|
||||
#include "../io/stream.hpp"
|
||||
|
||||
namespace mijin
|
||||
@@ -18,10 +19,16 @@ class SSLStream : public Stream
|
||||
{
|
||||
private:
|
||||
Stream* base_ = nullptr;
|
||||
void* ssl_ = nullptr;
|
||||
void* bioA_ = nullptr;
|
||||
void* bioB_ = nullptr;
|
||||
ossl::Ssl ssl_;
|
||||
ossl::Bio externalBio_;
|
||||
public:
|
||||
~SSLStream() noexcept override
|
||||
{
|
||||
if (base_ != nullptr)
|
||||
{
|
||||
close();
|
||||
}
|
||||
}
|
||||
StreamError open(Stream& base, const std::string& hostname) noexcept;
|
||||
void close() noexcept;
|
||||
|
||||
@@ -35,6 +42,51 @@ public:
|
||||
private:
|
||||
StreamError bioToBase() noexcept;
|
||||
StreamError baseToBio() noexcept;
|
||||
|
||||
|
||||
template<typename TFunc, typename... TArgs>
|
||||
auto runIOLoop(TFunc&& func, TArgs&&... args) -> std::decay_t<std::invoke_result_t<TFunc, ossl::Ssl&, TArgs...>>
|
||||
{
|
||||
using result_t = std::decay_t<std::invoke_result_t<TFunc, ossl::Ssl&, TArgs...>>;
|
||||
while (true)
|
||||
{
|
||||
auto result = std::invoke(std::forward<TFunc>(func), ssl_, std::forward<TArgs>(args)...);
|
||||
ossl::Error error;
|
||||
if constexpr (std::is_same_v<result_t, ossl::Error>)
|
||||
{
|
||||
if (error.isSuccess())
|
||||
{
|
||||
return error;
|
||||
}
|
||||
error = result;
|
||||
}
|
||||
else
|
||||
{
|
||||
// assume result type
|
||||
if (result.isSuccess())
|
||||
{
|
||||
return result;
|
||||
}
|
||||
error = result.getError();
|
||||
}
|
||||
switch (error.sslError)
|
||||
{
|
||||
case SSL_ERROR_WANT_READ:
|
||||
case SSL_ERROR_WANT_WRITE:
|
||||
if(const StreamError streamError = baseToBio(); streamError != StreamError::SUCCESS)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
if (const StreamError streamError = bioToBase(); streamError != StreamError::SUCCESS)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return error;
|
||||
}
|
||||
}
|
||||
}
|
||||
// mijin::Task<StreamError> c_readRaw(std::span<std::uint8_t> buffer, const ReadOptions& options = {}, std::size_t* outBytesRead = nullptr) override;
|
||||
// mijin::Task<StreamError> c_writeRaw(std::span<const std::uint8_t> buffer) override;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user