Commit 2da2b91a authored by Dominik Charousset's avatar Dominik Charousset

Have the SSL policy NOT own the socket by default

parent 25d12e95
......@@ -13,6 +13,10 @@
namespace caf::net::ssl {
struct close_on_shutdown_t {};
constexpr close_on_shutdown_t close_on_shutdown = close_on_shutdown_t{};
/// SSL state, shared by multiple connections.
class CAF_NET_EXPORT context {
public:
......@@ -71,8 +75,15 @@ public:
// -- connections ------------------------------------------------------------
/// Creates a new SSL connection on `fd`. The connection does not take
/// ownership of the socket, i.e., does not close the socket when the SSL
/// session ends.
expected<connection> new_connection(stream_socket fd);
/// Creates a new SSL connection on `fd`. The connection takes ownership of
/// the socket, i.e., closes the socket when the SSL session ends.
expected<connection> new_connection(stream_socket fd, close_on_shutdown_t);
// -- certificates and keys --------------------------------------------------
/// Configure the context to use the default locations for loading CA
......
......@@ -132,7 +132,7 @@ size_t connection::buffered() const noexcept {
}
stream_socket connection::fd() const noexcept {
if (auto id = SSL_get_fd(native(pimpl_)))
if (auto id = SSL_get_fd(native(pimpl_)); id != -1)
return stream_socket{static_cast<socket_id>(id)};
else
return stream_socket{invalid_socket_id};
......
......@@ -183,6 +183,21 @@ bool context::has_last_error() noexcept {
// -- connections --------------------------------------------------------------
expected<connection> context::new_connection(stream_socket fd) {
if (auto ptr = SSL_new(native(pimpl_))) {
auto conn = connection::from_native(ptr);
if (auto bio_ptr = BIO_new_socket(fd.id, BIO_NOCLOSE)) {
SSL_set_bio(ptr, bio_ptr, bio_ptr);
return {std::move(conn)};
} else {
return {make_error(sec::logic_error, "BIO_new_socket failed")};
}
} else {
return {make_error(sec::logic_error, "SSL_new returned null")};
}
}
expected<connection> context::new_connection(stream_socket fd,
close_on_shutdown_t) {
if (auto ptr = SSL_new(native(pimpl_))) {
auto conn = connection::from_native(ptr);
if (SSL_set_fd(ptr, fd.id) == 1)
......
......@@ -42,6 +42,10 @@ public:
return caf::none;
}
socket handle() const override {
return policy_.conn.fd();
}
void handle_read_event() override {
if (auto res = advance_handshake(); res > 0) {
owner_->deregister();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment