diff --git a/include/frnetlib/SSLSocket.h b/include/frnetlib/SSLSocket.h index d619e11..303eb7c 100644 --- a/include/frnetlib/SSLSocket.h +++ b/include/frnetlib/SSLSocket.h @@ -21,9 +21,9 @@ namespace fr public: explicit SSLSocket(std::shared_ptr ssl_context) noexcept; ~SSLSocket() override; - SSLSocket(SSLSocket &&) = delete; + SSLSocket(SSLSocket &&); SSLSocket(const SSLSocket &) = delete; - void operator=(SSLSocket &&)=delete; + SSLSocket& operator=(SSLSocket &&); void operator=(const SSLSocket &)=delete; /*! diff --git a/include/frnetlib/Util.h b/include/frnetlib/Util.h new file mode 100644 index 0000000..a76a2dd --- /dev/null +++ b/include/frnetlib/Util.h @@ -0,0 +1,52 @@ +// +// Created by rexy712 on 06/25/2021. +// + +#ifndef FRNETLIB_UTIL_H +#define FRNETLIB_UTIL_H + +#include +#include //size_t +#include //memcpy, memset +#include //move + +namespace fr +{ + template + constexpr void swap(T& a, T& b) + { + T tmp = std::move(a); + a = std::move(b); + b = std::move(tmp); + } + + template + constexpr void swap(T (&a)[N], T (&b)[N]) + { + for(size_t i = 0;i < N;++i) + { + swap(a[i], b[i]); + } + } + + constexpr mbedtls_ssl_config move_mbedtls_ssl_config(mbedtls_ssl_config &&src) + { + mbedtls_ssl_config retval = {}; + swap(retval.ciphersuite_list, src.ciphersuite_list); + swap(retval.p_dbg, src.p_dbg); + swap(retval.f_get_cache, src.f_get_cache); + swap(retval.f_set_cache, src.f_set_cache); + swap(retval.p_cache, src.p_cache); + retval.max_major_ver = src.max_major_ver; + retval.max_minor_ver = src.max_minor_ver; + retval.min_major_ver = src.min_major_ver; + retval.min_minor_ver = src.min_minor_ver; + retval.endpoint = src.endpoint; + retval.transport = src.transport; + retval.authmode = src.authmode; + retval.allow_legacy_renegotiation = src.allow_legacy_renegotiation; + return retval; + } +} + +#endif diff --git a/src/SSLSocket.cpp b/src/SSLSocket.cpp index 5202299..8fe2952 100644 --- a/src/SSLSocket.cpp +++ b/src/SSLSocket.cpp @@ -3,6 +3,7 @@ // #include "frnetlib/SSLSocket.h" +#include "frnetlib/Util.h" #include #include @@ -58,6 +59,27 @@ namespace fr mbedtls_ssl_config_free(&conf); } + SSLSocket::SSLSocket(SSLSocket &&s) + : ssl_context(std::move(s.ssl_context)), + ssl_socket_descriptor(std::move(s.ssl_socket_descriptor)), + ssl(std::move(s.ssl)), + conf(move_mbedtls_ssl_config(std::move(s.conf))), + should_verify(s.should_verify), + receive_timeout(s.receive_timeout), + is_blocking(s.is_blocking){} + + SSLSocket& SSLSocket::operator=(SSLSocket &&s) + { + ssl_context = std::move(s.ssl_context); + ssl_socket_descriptor = std::move(s.ssl_socket_descriptor); + ssl = std::move(s.ssl); + conf = move_mbedtls_ssl_config(std::move(s.conf)); + should_verify = s.should_verify; + receive_timeout = s.receive_timeout; + is_blocking = s.is_blocking; + return *this; + } + void SSLSocket::close_socket() { ssl = nullptr;