Fix TLS read/write dispatch on tls branch

TCPSocket inherits from both Socket and TLS, both declaring receiveData()
as unrelated sibling methods - only Socket::receiveData was virtual, so
TLS::receiveData (SSL_read) was never reached regardless of TLS config,
and even when reached its result was never forwarded to onDataReceived
(that call was commented out). Added an explicit TCPSocket::receiveData
override dispatching to TLS::receiveData or Socket::receiveData based on
tlsInfo, and completed TLS::receiveData to actually return/forward the
decrypted bytes.

Write path did not exist at all. Added Socket::sendData() as a new
virtual hook mirroring receiveData, routed Socket::writeSocket() through
it instead of a raw ::write(), added TLS::sendData() (SSL_write, same
error-handling style as the existing TLS methods), and TCPSocket::sendData()
dispatching the same way the read side does.

Also fixed: a queued write in writeSocket() was being popped off the fifo
as if sent even when shutDown was true and nothing was actually written;
removed a dead empty if(tlsInfo){} stub in onDataReceived, no longer
needed now dispatch happens one level up; replaced leftover debug
std::cout lines with proper Log() calls matching the rest of the file.

Verified via full local compile: all 20 .cpp files build clean, link
into libServerCore.a, and nm confirms the new/fixed symbols are correctly
defined in the right object files.
This commit is contained in:
Brad Arant committed 2026-08-22 19:51:08 -07:00
1 parent 5feabd0fde
commit 701d03dcd9
6 files changed
+92 -11

No files matched your search

+8 -3
View File
@@ -70,7 +70,7 @@ namespace core
bool Socket::eventReceived(struct epoll_event event, long long eventId) { bool Socket::eventReceived(struct epoll_event event, long long eventId) {
coreutils::Log(coreutils::LOG_DEBUG_1) << "Event process beginning for socket " << getDescriptor(); coreutils::Log(coreutils::LOG_DEBUG_1) << "Event process beginning for socket " << getDescriptor();
if(inHandler) if(inHandler)
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ coreutils::Log(coreutils::LOG_DEBUG_2) << "inHandler was already true."; coreutils::Log(coreutils::LOG_DEBUG_2) << "inHandler was already true.";
inHandler = true; inHandler = true;
if(event.events & EPOLLRDHUP) { if(event.events & EPOLLRDHUP) {
coreutils::Log(coreutils::LOG_DEBUG_2) << "EPOLLRDHUP"; coreutils::Log(coreutils::LOG_DEBUG_2) << "EPOLLRDHUP";
@@ -139,14 +139,19 @@ namespace core
} }
} }
int Socket::sendData(const char *data, int length) {
return ::write(descriptor, data, length);
}
void Socket::writeSocket() { void Socket::writeSocket() {
outlock.lock(); outlock.lock();
// coreutils::Log(coreutils::LOG_DEBUG_3) << "writing data to socket " << getDescriptor(); // coreutils::Log(coreutils::LOG_DEBUG_3) << "writing data to socket " << getDescriptor();
if(fifo.size() > 0) { if(fifo.size() > 0) {
if(!shutDown) if(!shutDown) {
int rc = ::write(descriptor, fifo.front().c_str(), fifo.front().length()); sendData(fifo.front().c_str(), fifo.front().length());
fifo.pop(); fifo.pop();
} }
}
outlock.unlock(); outlock.unlock();
} }
+9
View File
@@ -173,6 +173,15 @@ namespace core {
virtual void receiveData(coreutils::ZString &buffer); virtual void receiveData(coreutils::ZString &buffer);
///
/// sendData writes data to the socket descriptor. TLS layer overrides this to
/// write through SSL instead of a raw socket write.
///
/// @return The number of bytes written, or -1 on error.
///
virtual int sendData(const char *data, int length);
private: private:
std::string text; std::string text;
+18 -3
View File
@@ -27,11 +27,26 @@ namespace core {
out << "|" << ipAddress.getClientAddressAndPort(); out << "|" << ipAddress.getClientAddressAndPort();
} }
void TCPSocket::onDataReceived(coreutils::ZString &data) { void TCPSocket::receiveData(coreutils::ZString &buffer) {
if(tlsInfo) { if(tlsInfo) {
int len = TLS::receiveData(buffer);
if(len >= 0) {
coreutils::ZString zbuffer(buffer.getData(), len);
onDataReceived(zbuffer);
} }
} else {
Socket::receiveData(buffer);
}
}
int TCPSocket::sendData(const char *data, int length) {
if(tlsInfo)
return TLS::sendData(data, length);
else
return Socket::sendData(data, length);
}
void TCPSocket::onDataReceived(coreutils::ZString &data) {
if (data.getLength() > 0) { if (data.getLength() > 0) {
lineBuffer = (char *)realloc(lineBuffer, lineBufferSize + data.getLength()); lineBuffer = (char *)realloc(lineBuffer, lineBufferSize + data.getLength());
+19
View File
@@ -104,6 +104,25 @@ namespace core {
void setBlockSize(int size = 0); void setBlockSize(int size = 0);
///
/// Dispatches to TLS::receiveData (SSL_read) when this socket was constructed with a
/// TLSInfo/SSL_CTX, otherwise falls back to Socket::receiveData (plain ::read). Needed
/// because Socket and TLS both declare receiveData as unrelated sibling methods -
/// Socket::receiveData is virtual but TLS::receiveData does not override it, so without
/// this explicit override TLS::receiveData would never be reached and encrypted bytes
/// would be handed to onDataReceived as if they were plaintext.
///
virtual void receiveData(coreutils::ZString &buffer) override;
///
/// Dispatches to TLS::sendData (SSL_write) when this socket was constructed with a
/// TLSInfo/SSL_CTX, otherwise falls back to Socket::sendData (plain ::write). Mirrors
/// the receiveData override above for the write path.
///
virtual int sendData(const char *data, int length) override;
protected: protected:
bool term = false; bool term = false;
TLSInfo *tlsInfo; TLSInfo *tlsInfo;
+36 -4
View File
@@ -134,14 +134,14 @@ namespace core {
} }
void TLS::receiveData(coreutils::ZString &buffer) { int TLS::receiveData(coreutils::ZString &buffer) {
int len; int len;
std::cout << "receiveData TLS" << std::endl; coreutils::Log(coreutils::LOG_DEBUG_3) << "receiveData TLS on socket.";
if((len = ::SSL_read(ssl, buffer.getData(), buffer.getLength())) >= 0) { if((len = ::SSL_read(ssl, buffer.getData(), buffer.getLength())) >= 0) {
std::cout << "receiveData TLS...len=" << len << ":" << buffer << std::endl; coreutils::Log(coreutils::LOG_DEBUG_3) << "receiveData TLS...len=" << len;
// onDataReceived(buffer); return len;
} }
else { else {
switch (SSL_get_error(ssl, len)) { switch (SSL_get_error(ssl, len)) {
@@ -161,6 +161,38 @@ namespace core {
coreutils::Log(coreutils::LOG_DEBUG_3) << "Unknown ERROR on ssl_read."; coreutils::Log(coreutils::LOG_DEBUG_3) << "Unknown ERROR on ssl_read.";
break; break;
} }
return -1;
}
}
int TLS::sendData(const char *data, int length) {
int len;
if((len = ::SSL_write(ssl, data, length)) >= 0) {
coreutils::Log(coreutils::LOG_DEBUG_3) << "sendData TLS...len=" << len;
return len;
}
else {
switch (SSL_get_error(ssl, len)) {
case SSL_ERROR_SSL:
coreutils::Log(coreutils::LOG_DEBUG_3) << "ERROR_SSL on ssl_write. error=" << errno;
break;
case SSL_ERROR_WANT_READ:
coreutils::Log(coreutils::LOG_DEBUG_3) << "ERROR_WANT_READ on ssl_write.";
break;
case SSL_ERROR_WANT_WRITE:
coreutils::Log(coreutils::LOG_DEBUG_3) << "ERROR_WANT_WRITE on ssl_write.";
break;
case SSL_ERROR_SYSCALL:
coreutils::Log(coreutils::LOG_DEBUG_3) << "ERROR_SYSCALL on ssl_write. errno=" << errno;
break;
default:
coreutils::Log(coreutils::LOG_DEBUG_3) << "Unknown ERROR on ssl_write.";
break;
}
return -1;
} }
} }
+2 -1
View File
@@ -49,7 +49,8 @@ namespace core {
SSL_CTX *ctx; SSL_CTX *ctx;
protected: protected:
void receiveData(coreutils::ZString &buffer); int receiveData(coreutils::ZString &buffer);
int sendData(const char *data, int length);
void registerSocket(int fd); void registerSocket(int fd);
void acceptSocket(); void acceptSocket();