Files
ServerCore/TCPSocket.cpp.pre-iolock-fix-20260831
T
Brad Arant 3609ed603e Merge tls into master: bring in subscription ownership, keep all TLS/stability work
tls and master diverged from a July 2024 fork point and never
reconciled - tls carries ~19 months of real socket/TLS stability
fixes (uninitialized tlsInfo, TCPServer::connectionRequest()
unconditionally initializing TLS, the WANT_READ write busy-spin, the
console authentication fix from this session), while master
independently picked up a Subscription/SubscriptionManager rework
(MString-based ownership tracking: owner/ownership/selection/time
fields and a working subscriptionOwner() method) that tls never had.

Every file both branches touched was resolved by keeping tls's side
entirely (Command.h, Socket.cpp, TCPServer.h, TCPSession.cpp/h,
TCPSocket.h were pure reformatting on master's side, or an
alternate/superseded fix for a bug tls already fixed a different way -
e.g. master's TCPSession uuid/alias handling vs tls's existing
uuid_unparse-based fix), with two deliberate exceptions:

- TCPServer.cpp's constructor was missing the
  commands.add(subscriptions, "publish"/"subscribe"/"catalog"/...)
  wiring master had - without it, SubscriptionManager existed but was
  completely unreachable through the normal command dispatch. Restored
  on top of tls's TLS-aware constructor.

- SubscriptionManager.cpp's "invite" command handler had a real
  incompatibility: master's version assigned into an `invitee` member
  typed as coreutils::MString via a stray `(coreutils::MString *)`
  cast that doesn't match the member's actual (non-pointer) type, and
  called session.server.getSessionByAlias() against a TCPServer
  reference - tls made TCPSession::server a pointer (part of removing
  the separate TCPSession2 class), so that call site no longer
  compiles as written. Took tls's cleaner local-variable version
  instead (functionally identical - looks up a session by alias and
  forwards an invite message) and removed the now-unused `invitee`
  member from SubscriptionManager.h.

One additional compile fix needed after resolving conflicts:
Subscription::subscriptionOwners is an MString (master's ownership
design), but TCPSession::alias is void* (tls's identity mechanism,
unchanged and depended on elsewhere) - MString has an implicit
constructor from void* already used successfully elsewhere in this
same file (the "publish" handler's Subscription constructor call),
but operator== doesn't accept the same implicit conversion, so
removeSessionSubscriptions()'s ownership check needed an explicit
coreutils::MString(session.alias) construction to compile.

Also drops output/main (a stray checked-in compiled binary, already
removed on tls) and the doxygen docs/html output (already removed on
tls, was carried on master since the fork).

Verified: full ServerCore rebuild (./compile) succeeds with zero
errors or warnings beyond the one compile fix documented above.
2026-09-06 13:09:15 +00:00

110 lines
3.7 KiB
Plaintext

#include "TCPSocket.h"
#include "EPoll.h"
#include "Log.h"
#include "Exception.h"
#include "errno.h"
namespace core {
TCPSocket::TCPSocket(EPoll &ePoll) : Socket(ePoll) {}
TCPSocket::TCPSocket(EPoll &ePoll, std::string text) : Socket(ePoll, text) {}
TCPSocket::TCPSocket(EPoll &ePoll, TLSInfo *tlsInfo, std::string text) : Socket(ePoll, text), TLS(tlsInfo), tlsInfo(tlsInfo) {}
TCPSocket::TCPSocket(EPoll &ePoll, SSL_CTX *ctx, std::string text) : Socket(ePoll, text), TLS(ctx) {}
TCPSocket::~TCPSocket() {}
void TCPSocket::connect(IPAddress &address) {
setDescriptor(socket(AF_INET, SOCK_STREAM, 0));
if(::connect(getDescriptor(), (struct sockaddr *)&address.addr, address.addressLength) == -1)
throw coreutils::Exception("Error on connect to TCP socket." + errno);
coreutils::Log(coreutils::LOG_DEBUG_3) << "Connected to IP..." << address.getClientAddressAndPort();
}
void TCPSocket::output(std::stringstream &out) {
out << "|" << ipAddress.getClientAddressAndPort();
}
void TCPSocket::receiveData(coreutils::ZString &buffer) {
// Dispatch on `ctx`, not `tlsInfo` - the SSL_CTX*-taking constructor
// (used for every accepted TCPSession) never sets `tlsInfo` at all,
// leaving it as uninitialized garbage that intermittently (whenever
// that garbage happened to be non-zero) routed a plain, non-TLS
// session's data through SSL_read()/SSL_write() against an
// uninitialized SSL*, segfaulting inside libssl. `ctx` is the
// reliable signal: real and non-NULL for an actual TLS server/
// session (set in TLS::createContext(), inherited by sessions via
// TLS(SSL_CTX*)), safely NULL by default otherwise (see TLS.h).
if(ctx) {
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(ctx)
return TLS::sendData(data, length);
else
return Socket::sendData(data, length);
}
void TCPSocket::performTLSHandshake() {
registerSocket(getDescriptor());
acceptSocket();
}
void TCPSocket::onDataReceived(coreutils::ZString &data) {
if (data.getLength() > 0) {
lineBuffer = (char *)realloc(lineBuffer, lineBufferSize + data.getLength());
memcpy(lineBuffer + lineBufferSize, data.getData(), data.getLength());
lineBufferSize += data.getLength();
while (lineBufferSize > 0) {
if (blockSize == 0) {
lineLength = strcspn(lineBuffer, "\r\n");
if (lineLength == lineBufferSize)
break;
coreutils::ZString zLine(lineBuffer, lineLength);
onLineReceived(zLine);
if (lineBuffer[lineLength] == '\r')
++lineLength;
if (lineBuffer[lineLength] == '\n')
++lineLength;
lineBufferSize -= lineLength;
if (lineBufferSize > 0)
memmove(lineBuffer, lineBuffer + lineLength, lineBufferSize);
lineBuffer = (char *)realloc(lineBuffer, lineBufferSize);
}
else if (lineBufferSize >= blockLength) {
coreutils::ZString zBlock(lineBuffer, blockLength);
onBlockReceived(zBlock);
lineBufferSize -= blockLength;
if (lineBufferSize > 0)
memmove(lineBuffer, lineBuffer + blockLength, lineBufferSize);
lineBuffer = (char *)realloc(lineBuffer, lineBufferSize);
}
}
}
}
void TCPSocket::onLineReceived(coreutils::ZString &line) {
if (term)
shutdown("termination requested");
}
void TCPSocket::onBlockReceived(coreutils::ZString &block) {}
void TCPSocket::setBlockSize(int blockSize) {
this->blockSize = blockSize;
}
}