support configuring SSL TCP sockets for non-blocking operations;

82-dvmbridge---implement-notch-filter-for-2175hz-trc-guard-tone
Bryan Biedenkapp 1 year ago
parent d6cf8b1155
commit f3c86d2a8e

@ -4,7 +4,7 @@
* GPLv2 Open Source. Use is subject to license terms. * GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* Copyright (C) 2024 Bryan Biedenkapp, N2PLL * Copyright (C) 2024-2025 Bryan Biedenkapp, N2PLL
* *
*/ */
/** /**
@ -56,8 +56,9 @@ namespace network
* @param sslCtx Instance of the OpenSSL context. * @param sslCtx Instance of the OpenSSL context.
* @param client Address for client. * @param client Address for client.
* @param clientLen Length of sockaddr_in structure. * @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_sockaddr(),
m_pSSL(nullptr), m_pSSL(nullptr),
m_pSSLCtx(nullptr) m_pSSLCtx(nullptr)
@ -135,6 +136,7 @@ namespace network
} while (status == 1 && !SSL_is_init_finished(m_pSSL)); } while (status == 1 && !SSL_is_init_finished(m_pSSL));
// reset socket blocking operations // reset socket blocking operations
if (!nonBlocking) {
flags = fcntl(fd, F_GETFL, 0); flags = fcntl(fd, F_GETFL, 0);
if (flags < 0) { if (flags < 0) {
LogError(LOG_NET, "failed fcntl(F_GETFL), err: %d", errno); LogError(LOG_NET, "failed fcntl(F_GETFL), err: %d", errno);
@ -146,12 +148,14 @@ namespace network
throw std::runtime_error("Cannot accept SSL client"); throw std::runtime_error("Cannot accept SSL client");
} }
} }
}
/** /**
* @brief Initializes a new instance of the SecureTcpClient class. * @brief Initializes a new instance of the SecureTcpClient class.
* @param address IP Address. * @param address IP Address.
* @param port Port. * @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_pSSL(nullptr),
m_pSSLCtx(nullptr) m_pSSLCtx(nullptr)
{ {
@ -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); 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"); 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. * @brief Finalizes a instance of the SecureTcpClient class.

@ -7,7 +7,7 @@
* @package DVM / Common Library * @package DVM / Common Library
* @license GPLv2 License (https://opensource.org/licenses/GPL-2.0) * @license GPLv2 License (https://opensource.org/licenses/GPL-2.0)
* *
* Copyright (C) 2024 Bryan Biedenkapp, N2PLL * Copyright (C) 2024-2025 Bryan Biedenkapp, N2PLL
* *
*/ */
/** /**
@ -109,9 +109,10 @@ namespace network
/** /**
* @brief Accept a new TCP connection either secure or unsecure. * @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. * @returns SecureTcpClient* Newly accepted TCP connection.
*/ */
[[nodiscard]] SecureTcpClient* accept() [[nodiscard]] SecureTcpClient* accept(bool nonBlocking = false)
{ {
sockaddr_in client = {}; sockaddr_in client = {};
socklen_t clientLen = sizeof(client); socklen_t clientLen = sizeof(client);
@ -120,7 +121,7 @@ namespace network
return nullptr; return nullptr;
} }
return new SecureTcpClient(fd, m_pSSLCtx, client, clientLen); return new SecureTcpClient(fd, m_pSSLCtx, client, clientLen, nonBlocking);
} }
private: private:

Loading…
Cancel
Save

Powered by TurnKey Linux.