Thread/EPoll lifecycle: - Thread::~Thread() now actually joins/frees its worker thread (was empty - the OS thread kept running against already-freed EPoll/Socket objects on any early teardown, a confirmed "double free" crash) - EPoll::~EPoll() calls stop() if never explicitly stopped, for the same reason - epoll_wait() now uses a 1s timeout instead of blocking forever, so a worker thread can actually notice isStopping() and let itself be joined - EPoll::processCommand() iterated threads by value, silently double-freeing the real vector's Thread the moment each copy went out of scope - ThreadScope gained onThreadStart()/onThreadEnd() hooks (actually wired up now - the constructor that was supposed to store the scope discarded it) for per-thread C-library init (e.g. MySQL client) Socket/TCPSocket/TLS: - Socket::writeSocket(): give up and shut down a connection after 50 consecutive zero-progress sends instead of retrying forever - SSL_ERROR_WANT_READ on a write needs a READ to make progress, but resetSocket() re-arms EPOLLWRNORM regardless, which an already-writable TCP socket satisfies immediately - a real stuck connection generated 55GB, then 12.9GB of debug log this way before being caught - Socket::writeSocket() now honors sendData()'s actual return value (previously discarded, entry popped unconditionally after one call) - a large response could get silently truncated past what fit in one write()/SSL_write() call - TLS::registerSocket()/sendData()/receiveData(): `initialized` now only goes true once SSL_set_fd() has actually succeeded, and sendData()/receiveData() check it first - fixes a real SIGSEGV where a new session's own queued greeting could reach SSL_write() on a different epoll thread before this session's handshake setup had run - TCPServer::connectionRequest() now performs the TLS handshake ON THE ACCEPTED SESSION, not the listening server itself (was building SSL state on the wrong object, leaving every session's own `ssl` uninitialized) - TCPServer::receiveData() always uses the plain (non-TLS) read path - the listening socket is never itself a TLS peer - TCPSocket's block-mode literal reading used an unset `blockLength` field instead of `blockSize`, and could busy-spin forever on a multi-packet literal - TCPSocket line scanning replaced an unbounded strcspn() (assumes a NUL-terminated buffer; read past a non-NUL-terminated receive buffer into adjacent heap memory) with a bounds-checked memchr() scan Protocol/session handling: - CommandList::processRequest() now case-folds the command keyword before lookup - SMTP/POP3/IMAP commands are spec-required to be case-insensitive; a client sending "Mail From:" instead of "MAIL FROM:" previously failed to dispatch at all - TCPSession::protocol() no longer drops blank lines while a handler has grabbed input (e.g. __SMTP_DATA reading a message body, an IMAP APPEND literal) - those handlers depend on seeing blank lines as real content, not commands; this silently corrupted any message body containing a blank line - TCPSession's uuid alias fixed: streaming a raw uuid_t via operator<< triggered libstdc++'s unsigned-char* overload (strlen() on 16 binary bytes, a real stack-buffer-overflow read), and `alias` pointed at a stack-local that was already destroyed by the time the constructor returned - INotify::processEvent() only treats event->name as a NUL-terminated string when event->len > 0 (per inotify(7)) instead of unconditional strlen() on a field the kernel doesn't guarantee is even present - a confirmed heap-buffer-overflow read that had been misdiagnosed as a MySQL thread-safety issue
170 lines
5.8 KiB
C++
170 lines
5.8 KiB
C++
#ifndef __TCPServer_h__
|
|
# define __TCPServer_h__
|
|
|
|
# include "Command.h"
|
|
# include "CommandList.h"
|
|
# include "IPAddressList.h"
|
|
# include "Socket.h"
|
|
# include "SubscriptionManager.h"
|
|
# include "TCPSession.h"
|
|
# include "TCPSocket.h"
|
|
# include "TLSInfo.h"
|
|
|
|
namespace core {
|
|
|
|
///
|
|
/// TCPServer
|
|
///
|
|
/// Manage a socket connection as a TCP server type. Connections to the socket are processed through
|
|
/// the accept functionality.
|
|
///
|
|
/// A list of connections is maintained in a vector object.
|
|
///
|
|
/// This object extends the Command object as well so it can be added to a Console object and
|
|
/// process commands to display status information.
|
|
///
|
|
|
|
class TCPServer : public TCPSocket, public Command {
|
|
|
|
public:
|
|
|
|
///
|
|
/// The constructor for the TCPServer object.
|
|
///
|
|
/// @param ePoll the EPoll instance that manages the socket.
|
|
/// @param url the IP address for the socket to receive connection requests.
|
|
/// @param port the port number that the socket will listen on.
|
|
/// @param commandName the name of the command used to invoke the status display for this object.
|
|
///
|
|
|
|
TCPServer(EPoll &ePoll, IPAddress address, TLSInfo *tlsInfo, std::string delimiter = " ", int depth = 10, std::string text = "");
|
|
|
|
///
|
|
/// The destructor for this object.
|
|
///
|
|
|
|
virtual ~TCPServer();
|
|
|
|
virtual void sessionErrorHandler(std::string errorString, std::stringstream &out);
|
|
|
|
///
|
|
/// getSocketAccept is designed to allow a polymorphic extension of this object to
|
|
/// return a type of object that extends the definition of the server socket.
|
|
/// Returning the appropriate session object that extends from Session provides
|
|
/// the mechanism where the server can select the protocol dialog for the desired
|
|
/// service.
|
|
///
|
|
|
|
virtual TCPSession *getSocketAccept(EPoll &epoll);
|
|
|
|
///
|
|
/// The list of sessions that are currently open and being maintained by this object.
|
|
///
|
|
|
|
std::vector<TCPSession *> sessions;
|
|
|
|
///
|
|
/// The commands object is a CommandList and is used to store Command objects to be
|
|
/// parsed and run as data comes into the session.
|
|
///
|
|
|
|
CommandList commands;
|
|
|
|
///
|
|
/// If not NULL the blacklist object can be assigned to this server socket and the server
|
|
/// IP addresses connecting to the server attempting to accept a socket are contained in
|
|
/// this list then the connection is rejected and no accept is granted.
|
|
///
|
|
|
|
IPAddressList *blackList;
|
|
|
|
///
|
|
/// If not NULL the blacklist object can be assigned to this server socket and the server
|
|
/// IP addresses connecting to the server attempting to accept a socket are contained in
|
|
/// this list then the connection is rejected and no accept is granted.
|
|
///
|
|
|
|
IPAddressList *whiteList;
|
|
|
|
void removeFromSessionList(TCPSession *session);
|
|
|
|
void output(std::stringstream &out); ///< Output the consoles array to the console.
|
|
|
|
///
|
|
///
|
|
///
|
|
|
|
void sendToAll(std::stringstream &out);
|
|
|
|
///
|
|
/// Use this sendToAll method to output the contents of the out stream
|
|
/// to all the connections on the server excluding the sender session.
|
|
///
|
|
|
|
void sendToAll(std::stringstream &out, TCPSession &sender, SessionFilter filter);
|
|
|
|
///
|
|
/// Use this sendToAll method to output the contents of the out stream
|
|
/// to all the connections on the server excluding the sender session
|
|
/// and the entries identified by the passed in filter object.
|
|
///
|
|
|
|
void sendToAll(std::stringstream &out, TCPSession &sender);
|
|
|
|
///
|
|
/// The Subscription Manager tracks all subscriptions on the server.
|
|
///
|
|
|
|
SubscriptionManager subscriptions;
|
|
|
|
///
|
|
/// Use the getSessionByAlias to retrieve a session pointer by the value
|
|
/// of the alias pointer.
|
|
///
|
|
|
|
TCPSession *getSessionByAlias(void *alias);
|
|
|
|
protected:
|
|
///
|
|
/// This method is called by the lower socket when a connection request comes into
|
|
/// the listening and bound server port.
|
|
///
|
|
|
|
void connectionRequest() override;
|
|
|
|
///
|
|
/// The listening socket itself is never a TLS peer - accepting a
|
|
/// connection on it surfaces as a plain read() failing with ENOTCONN
|
|
/// (see Socket::receiveData(), which is what actually detects and
|
|
/// dispatches to connectionRequest() above). When this server is
|
|
/// TLS-configured, TCPSocket::receiveData()'s normal ctx-based
|
|
/// dispatch would instead route that same event through
|
|
/// TLS::receiveData()/SSL_read() against a listening fd that was
|
|
/// never SSL_accept()-ed at all (real per-connection sessions are
|
|
/// the only sockets TLS applies to) - confirmed via a real crash
|
|
/// (SIGSEGV inside SSL_read, called with `this` pointing at this
|
|
/// server object, not a session). Always using the plain path here,
|
|
/// regardless of whether tlsInfo is set, keeps connection-detection
|
|
/// working the same way with or without TLS configured.
|
|
///
|
|
|
|
void receiveData(coreutils::ZString &buffer) override;
|
|
|
|
///
|
|
/// This method is called when the Command associated with this object is requested
|
|
/// because a user has typed in the associated command name on a command entry line.
|
|
///
|
|
/// @param the session object to write the output to.
|
|
///
|
|
|
|
int processCommand(coreutils::ZString &request, TCPSession &session) override;
|
|
|
|
private:
|
|
TCPSession *accept();
|
|
std::mutex lock;
|
|
};
|
|
|
|
}
|
|
|
|
#endif
|