Fixed SSL and TCP Bind.

This commit is contained in:
2025-05-22 07:05:11 -07:00
parent f52afe9c92
commit fbd8464043
5 changed files with 52 additions and 16 deletions

View File

@@ -29,7 +29,7 @@ namespace ehs
{
}
SSL::SSL(const IP type)
SSL::SSL(const IP &type)
: TCP(type), ctx(nullptr), sslHdl(nullptr)
{
SSL::Initialize();
@@ -102,9 +102,14 @@ namespace ehs
if (bound)
return;
OpenSSL_add_ssl_algorithms();
SSL_load_error_strings();
ctx = SSL_CTX_new(TLS_server_method());
TCP::Bind(address, port);
}
void SSL::Listen()
{
OpenSSL_add_ssl_algorithms();
SSL_load_error_strings();
ctx = SSL_CTX_new(TLS_server_method());
SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION);
@@ -118,18 +123,20 @@ namespace ehs
SSL_CTX_set_ecdh_auto(ctx, 1);
#endif
sslHdl = SSL_new(ctx);
SSL_set_fd(sslHdl, hdl);
sslHdl = SSL_new(ctx);
SSL_set_fd(sslHdl, hdl);
TCP::Bind(address, port);
TCP::Listen();
}
SSL* SSL::Accept()
SSL* SSL::Accept()
{
if (!bound)
return nullptr;
TCP* tcp = TCP::Accept();
if (!tcp)
return nullptr;
SSL* client = new SSL(std::move(*tcp));
@@ -151,9 +158,6 @@ namespace ehs
void SSL::Connect(const Str_8& address, const UInt_16 port)
{
if (bound)
return;
TCP::Connect(address, port);
OpenSSL_add_ssl_algorithms();
@@ -192,8 +196,12 @@ namespace ehs
if (written <= 0)
{
int code = SSL_get_error(sslHdl, written);
ERR_print_errors_fp(stderr);
EHS_LOG_INT(LogType::ERR, 0, "Failed to send data with error #" + Str_8::FromNum(code) + ".");
if (code != SSL_ERROR_WANT_WRITE)
{
ERR_print_errors_fp(stderr);
EHS_LOG_INT(LogType::ERR, 0, "Failed to send data with error #" + Str_8::FromNum(code) + ".");
}
return 0;
}
@@ -206,8 +214,12 @@ namespace ehs
if (received <= 0)
{
int code = SSL_get_error(sslHdl, received);
ERR_print_errors_fp(stderr);
EHS_LOG_INT(LogType::ERR, 0, "Failed to receive data with error #" + Str_8::FromNum(code) + ".");
if (code != SSL_ERROR_WANT_READ)
{
ERR_print_errors_fp(stderr);
EHS_LOG_INT(LogType::ERR, 0, "Failed to receive data with error #" + Str_8::FromNum(code) + ".");
}
return 0;
}

View File

@@ -385,6 +385,24 @@ namespace ehs
return result;
}
void TCP::SetReuse(const bool &value)
{
if (!IsValid())
{
EHS_LOG_INT(LogType::WARN, 1, "Attempted to set address and port reuse while socket is not initialized.");
return;
}
const int result = (int)value;
if (setsockopt(hdl, SOL_SOCKET, SO_REUSEPORT, &result, sizeof(int)) == -1)
{
EHS_LOG_INT(LogType::ERR, 2, "Failed to set address and port reuse with error #" + Str_8::FromNum(errno) + ".");
return;
}
EHS_LOG_SUCCESS();
}
bool TCP::IsValid() const
{
return hdl != EHS_INVALID_SOCKET;