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.
220 lines
6.7 KiB
Plaintext
220 lines
6.7 KiB
Plaintext
#include "Socket.h"
|
|
#include "EPoll.h"
|
|
#include "Exception.h"
|
|
#include "Log.h"
|
|
#include "ZString.h"
|
|
|
|
namespace core
|
|
{
|
|
|
|
void sigpipe_handler(int unused) {}
|
|
|
|
Socket::Socket(EPoll &ePoll, std::string text) : ePoll(ePoll), text(text) {
|
|
coreutils::Log(coreutils::LOG_DEBUG_2) << "Socket object created [" << text << "].";
|
|
buffer = (char *)malloc(4096);
|
|
length = 4096;
|
|
}
|
|
|
|
Socket::~Socket() {
|
|
shutDown = true;
|
|
onUnregister();
|
|
disableSocket();
|
|
coreutils::Log(coreutils::LOG_DEBUG_4) << "Free on socket " << descriptor;
|
|
free(buffer);
|
|
if(descriptor == -1)
|
|
return;
|
|
close(descriptor);
|
|
coreutils::Log(coreutils::LOG_DEBUG_1) << text << " has ended (" << descriptor << ").";
|
|
}
|
|
|
|
void Socket::setDescriptor(int descriptor)
|
|
{
|
|
if ((descriptor == -1) && (errno == 24))
|
|
{
|
|
shutdown("Too many files open");
|
|
throw coreutils::Exception("Too many files open. Refusing connection.");
|
|
}
|
|
coreutils::Log(coreutils::LOG_DEBUG_1) << text << " has started (" << descriptor << ").";
|
|
if(descriptor < 3)
|
|
throw coreutils::Exception("Descriptor out of range", __FILE__, __LINE__);
|
|
this->descriptor = descriptor;
|
|
onRegister();
|
|
enableSocket();
|
|
onRegistered();
|
|
}
|
|
|
|
int Socket::getDescriptor()
|
|
{
|
|
return descriptor;
|
|
}
|
|
|
|
void Socket::setBufferSize(int length)
|
|
{
|
|
this->length = length;
|
|
buffer = (char *)realloc(buffer, length);
|
|
}
|
|
|
|
int Socket::getBufferSize()
|
|
{
|
|
return length;
|
|
}
|
|
|
|
void Socket::onRegister() {}
|
|
|
|
void Socket::onRegistered() {}
|
|
|
|
void Socket::onUnregister() {}
|
|
|
|
void Socket::onUnregistered() {}
|
|
|
|
bool Socket::eventReceived(struct epoll_event event, long long eventId) {
|
|
coreutils::Log(coreutils::LOG_DEBUG_1) << "Event process beginning for socket " << getDescriptor();
|
|
if(inHandler)
|
|
coreutils::Log(coreutils::LOG_DEBUG_2) << "inHandler was already true.";
|
|
inHandler = true;
|
|
if(event.events & EPOLLRDHUP) {
|
|
coreutils::Log(coreutils::LOG_DEBUG_2) << "EPOLLRDHUP";
|
|
readHangup = true;
|
|
shutdown("hangup received");
|
|
}
|
|
if(event.events & EPOLLIN) {
|
|
coreutils::Log(coreutils::LOG_DEBUG_2) << "EPOLLIN";
|
|
coreutils::ZString zbuffer(buffer, length);
|
|
lock.lock();
|
|
receiveData(zbuffer);
|
|
lock.unlock();
|
|
}
|
|
if(event.events & EPOLLWRNORM) {
|
|
coreutils::Log(coreutils::LOG_DEBUG_2) << "EPOLLWRNORM";
|
|
writeSocket();
|
|
}
|
|
inHandler = false;
|
|
bool keepAlive = !shutDown;
|
|
|
|
// resetSocket() re-arms this fd via EPOLL_CTL_MOD, which makes it
|
|
// immediately eligible for delivery to ANY worker thread's
|
|
// epoll_wait() - including one already blocked and about to wake
|
|
// up right away. The old code called resetSocket() from inside
|
|
// the branches above while several more statements (an
|
|
// unconditional inHandler write, this Log call, and `return
|
|
// !shutDown`) still touched `this` afterward - a second thread
|
|
// could legitimately receive the re-armed event, finish handling
|
|
// it, and delete this object (Thread::run() does exactly that
|
|
// when eventReceived() returns false) before this thread's own
|
|
// remaining statements ran, a heap-use-after-free confirmed via
|
|
// AddressSanitizer under concurrent load. resetSocket() must be
|
|
// the last thing that ever touches `this`, with the return value
|
|
// already captured in a local (`keepAlive`) beforehand.
|
|
coreutils::Log(coreutils::LOG_DEBUG_1) << "Event process ending for socket " << getDescriptor();
|
|
if(keepAlive)
|
|
resetSocket();
|
|
return keepAlive;
|
|
}
|
|
|
|
void Socket::connectionRequest() {}
|
|
|
|
void Socket::onDataReceived(std::string data) {
|
|
throw coreutils::Exception("Need to override onDataReceived.", __FILE__, __LINE__, -1);
|
|
}
|
|
|
|
void Socket::onDataReceived(coreutils::ZString &data) {
|
|
onDataReceived(std::string(data.getData(), data.getLength()));
|
|
}
|
|
|
|
void Socket::receiveData(coreutils::ZString &buffer) {
|
|
coreutils::ZString blank("");
|
|
if(buffer.getLength() <= 0)
|
|
throw coreutils::Exception("Request to receive data with a zero buffer length.", __FILE__, __LINE__, -1);
|
|
int len;
|
|
int error = -1;
|
|
if((len = ::read(getDescriptor(), buffer.getData(), buffer.getLength())) >= 0) {
|
|
coreutils::ZString zbuffer(buffer.getData(), len);
|
|
// coreutils::Log(coreutils::LOG_DEBUG_1) << zbuffer;
|
|
onDataReceived(zbuffer);
|
|
} else {
|
|
error = errno;
|
|
switch (error) {
|
|
|
|
// When a listening socket receives a connection
|
|
// request we get one of these.
|
|
|
|
case ENOTCONN:
|
|
connectionRequest();
|
|
break;
|
|
|
|
case ECONNRESET:
|
|
break;
|
|
|
|
default:
|
|
throw coreutils::Exception("Error in read of data from socket.", __FILE__, __LINE__, error);
|
|
}
|
|
}
|
|
}
|
|
|
|
int Socket::sendData(const char *data, int length) {
|
|
return ::write(descriptor, data, length);
|
|
}
|
|
|
|
void Socket::writeSocket() {
|
|
outlock.lock();
|
|
// coreutils::Log(coreutils::LOG_DEBUG_3) << "writing data to socket " << getDescriptor();
|
|
if(fifo.size() > 0) {
|
|
if(!shutDown) {
|
|
sendData(fifo.front().c_str(), fifo.front().length());
|
|
fifo.pop();
|
|
}
|
|
}
|
|
outlock.unlock();
|
|
}
|
|
|
|
int Socket::write(std::string data) {
|
|
outlock.lock();
|
|
fifo.emplace(data);
|
|
outlock.unlock();
|
|
if(lock.try_lock()) {
|
|
resetSocket();
|
|
lock.unlock();
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
void Socket::output(std::stringstream &out)
|
|
{
|
|
out << "|" << descriptor << "|";
|
|
}
|
|
|
|
bool Socket::needsToWrite()
|
|
{
|
|
return fifo.size() > 0;
|
|
}
|
|
|
|
void Socket::shutdown(std::string text) {
|
|
coreutils::Log(coreutils::LOG_DEBUG_2) << "Shutdown requested from " << this->text << " (" << descriptor << ") with reason: " << text << ".";
|
|
shutDown = true;
|
|
reset = false;
|
|
}
|
|
|
|
void Socket::enableSocket() {
|
|
struct epoll_event event;
|
|
event.data.ptr = this;
|
|
event.events = EPOLLIN | EPOLLRDHUP | EPOLLONESHOT | EPOLLET;
|
|
epoll_ctl(ePoll.getDescriptor(), EPOLL_CTL_ADD, getDescriptor(), &event);
|
|
}
|
|
|
|
void Socket::disableSocket() {
|
|
epoll_ctl(ePoll.getDescriptor(), EPOLL_CTL_DEL, getDescriptor(), NULL);
|
|
}
|
|
|
|
void Socket::resetSocket() {
|
|
struct epoll_event event;
|
|
event.data.ptr = this;
|
|
event.events = EPOLLIN | EPOLLRDHUP | EPOLLONESHOT | EPOLLET;
|
|
if(fifo.size() > 0)
|
|
event.events |= EPOLLWRNORM;
|
|
if(!shutDown)
|
|
epoll_ctl(ePoll.getDescriptor(), EPOLL_CTL_MOD, getDescriptor(), &event);
|
|
}
|
|
|
|
|
|
}
|