Fixed SSL and TCP Bind.

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

View File

@ -150,6 +150,8 @@ namespace ehs
virtual bool IsIPv6Only() const = 0; virtual bool IsIPv6Only() const = 0;
virtual void SetReuse(const bool &enabled) = 0;
/// Retrieves whether or not this socket was initialized. /// Retrieves whether or not this socket was initialized.
/// @returns The result. /// @returns The result.
virtual bool IsValid() const = 0; virtual bool IsValid() const = 0;

View File

@ -23,7 +23,7 @@ namespace ehs
SSL(); SSL();
SSL(const IP type); SSL(const IP &type);
SSL(TCP&& tcp) noexcept; SSL(TCP&& tcp) noexcept;
@ -39,6 +39,8 @@ namespace ehs
void Bind(const Str_8& address, unsigned short port) override; void Bind(const Str_8& address, unsigned short port) override;
void Listen() override;
SSL* Accept() override; SSL* Accept() override;
void Connect(const Str_8& address, const UInt_16 port) override; void Connect(const Str_8& address, const UInt_16 port) override;

View File

@ -84,6 +84,8 @@ namespace ehs
bool IsIPv6Only() const override; bool IsIPv6Only() const override;
void SetReuse(const bool &value) override;
bool IsValid() const override; bool IsValid() const override;
private: private:

View File

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

View File

@ -385,6 +385,24 @@ namespace ehs
return result; 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 bool TCP::IsValid() const
{ {
return hdl != EHS_INVALID_SOCKET; return hdl != EHS_INVALID_SOCKET;