Skip to content

Commit

Permalink
support configuring SSL TCP sockets for non-blocking operations;
Browse files Browse the repository at this point in the history
  • Loading branch information
gatekeep committed Jan 27, 2025
1 parent d6cf8b1 commit f3c86d2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
40 changes: 29 additions & 11 deletions src/common/network/tcp/SecureTcpClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright (C) 2024 Bryan Biedenkapp, N2PLL
* Copyright (C) 2024-2025 Bryan Biedenkapp, N2PLL
*
*/
/**
Expand Down Expand Up @@ -56,8 +56,9 @@ namespace network
* @param sslCtx Instance of the OpenSSL context.
* @param client Address for client.
* @param clientLen Length of sockaddr_in structure.
* @param nonBlocking Flag indicating socket operations should be non-blocking.
*/
SecureTcpClient(const int fd, SSL_CTX* sslCtx, sockaddr_in& client, int clientLen) : Socket(fd),
SecureTcpClient(const int fd, SSL_CTX* sslCtx, sockaddr_in& client, int clientLen, bool nonBlocking) : Socket(fd),
m_sockaddr(),
m_pSSL(nullptr),
m_pSSLCtx(nullptr)
Expand Down Expand Up @@ -135,23 +136,26 @@ namespace network
} while (status == 1 && !SSL_is_init_finished(m_pSSL));

// reset socket blocking operations
flags = fcntl(fd, F_GETFL, 0);
if (flags < 0) {
LogError(LOG_NET, "failed fcntl(F_GETFL), err: %d", errno);
throw std::runtime_error("Cannot accept SSL client");
}
if (!nonBlocking) {
flags = fcntl(fd, F_GETFL, 0);
if (flags < 0) {
LogError(LOG_NET, "failed fcntl(F_GETFL), err: %d", errno);
throw std::runtime_error("Cannot accept SSL client");
}

if (fcntl(fd, F_SETFL, flags & (~O_NONBLOCK)) < 0) {
LogError(LOG_NET, "failed fcntl(F_SETFL), err: %d", errno);
throw std::runtime_error("Cannot accept SSL client");
if (fcntl(fd, F_SETFL, flags & (~O_NONBLOCK)) < 0) {
LogError(LOG_NET, "failed fcntl(F_SETFL), err: %d", errno);
throw std::runtime_error("Cannot accept SSL client");
}
}
}
/**
* @brief Initializes a new instance of the SecureTcpClient class.
* @param address IP Address.
* @param port Port.
* @param nonBlocking Flag indicating socket operations should be non-blocking.
*/
SecureTcpClient(const std::string& address, const uint16_t port) :
SecureTcpClient(const std::string& address, const uint16_t port, bool nonBlocking) :
m_pSSL(nullptr),
m_pSSLCtx(nullptr)
{
Expand Down Expand Up @@ -180,6 +184,20 @@ namespace network
LogError(LOG_NET, "Failed to connect to server, %s err: %d", ERR_error_string(ERR_get_error(), NULL), errno);
throw std::runtime_error("Failed to SSL connect to server");
}

// setup socket for non-blocking operations
if (nonBlocking) {
int flags = fcntl(m_fd, F_GETFL, 0);
if (flags < 0) {
LogError(LOG_NET, "failed fcntl(F_GETFL), err: %d", errno);
throw std::runtime_error("Failed to set SSL server connection to non-blocking");
}

if (fcntl(m_fd, F_SETFL, flags | O_NONBLOCK) < 0) {
LogError(LOG_NET, "failed fcntl(F_SETFL), err: %d", errno);
throw std::runtime_error("Failed to set SSL server connection to non-blocking");
}
}
}
/**
* @brief Finalizes a instance of the SecureTcpClient class.
Expand Down
7 changes: 4 additions & 3 deletions src/common/network/tcp/SecureTcpListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @package DVM / Common Library
* @license GPLv2 License (https://opensource.org/licenses/GPL-2.0)
*
* Copyright (C) 2024 Bryan Biedenkapp, N2PLL
* Copyright (C) 2024-2025 Bryan Biedenkapp, N2PLL
*
*/
/**
Expand Down Expand Up @@ -109,9 +109,10 @@ namespace network

/**
* @brief Accept a new TCP connection either secure or unsecure.
* @param nonBlocking Flag indicating accepted TCP connections should use non-blocking sockets.
* @returns SecureTcpClient* Newly accepted TCP connection.
*/
[[nodiscard]] SecureTcpClient* accept()
[[nodiscard]] SecureTcpClient* accept(bool nonBlocking = false)
{
sockaddr_in client = {};
socklen_t clientLen = sizeof(client);
Expand All @@ -120,7 +121,7 @@ namespace network
return nullptr;
}

return new SecureTcpClient(fd, m_pSSLCtx, client, clientLen);
return new SecureTcpClient(fd, m_pSSLCtx, client, clientLen, nonBlocking);
}

private:
Expand Down

0 comments on commit f3c86d2

Please sign in to comment.