Files
columnlynx/src/server/net/tcp/tcp_server.cpp
DcruBro 1d9e1e6165 Version 1.2.1 - Bug Fixes
Critical (4)

C1 — udp_client.hpp / udp_client.cpp: Moved mStartReceive() out of the constructor into start(), after the socket is opened.
C2 — client/main.cpp:187: || → && in the main loop condition, so it exits when the connection drops instead of spinning forever.
C3 — tcp_connection.hpp: Added mConnectionAESKey{}, mConnectionSessionID(0), and mConnectionPublicKey{} to the constructor initializer list.
C4 — tcp_server.hpp / tcp_server.cpp: Added std::mutex mClientsMutex and locked it at all three mClients access sites (insert, erase callback, stop snapshot).
High (5)

H1 — tcp_connection.cpp:284: Replaced hardcoded htonl(0x0A0A0001) with htonl(baseIP + 1).
H2 — session_registry.hpp / session_registry.cpp / udp_server.cpp: Changed get() and getByIP() to return shared_ptr<SessionState> (non-const); removed all four const_cast usages.
H3 — session_registry.cpp:90: Guarded against mask == 0 || mask >= 32 (shift UB) and hostCount < 3 (unsigned underflow).
H4 — virtual_interface.cpp: Added runCommandWin32() using CreateProcess (no shell); replaced both system() calls in resetIP().
H5 — virtual_interface.cpp: Added close(mFd); mFd = -1; before both throw sites in the macOS constructor (ioctl failure and connect failure).
Medium (4)

M1 — tcp_connection.cpp:241: Added explicit find() + end-iterator check for NETWORK and SUBNET_MASK before dereferencing.
M2 — tcp_message_handler.cpp: Added an 8 KB (MAX_MSG_BODY = 8192) cap on incoming message body length; oversized messages close the connection.
M3 — tcp_connection.cpp:232: Capped the session ID generation loop at 16 attempts before disconnecting.
M4 — session_registry.cpp:8: put() now evicts the old IP→session mapping before replacing an entry, preventing stale mIPSessions entries.
2026-06-14 20:02:50 +02:00

82 lines
3.0 KiB
C++

// tcp_server.cpp - TCP Server for ColumnLynx
// Copyright (C) 2026 DcruBro
// Distributed under the terms of the GNU General Public License, either version 2 only or version 3. See LICENSES/ for details.
#include <columnlynx/server/net/tcp/tcp_server.hpp>
#include <cstdint>
#include <string>
#include <unordered_map>
#include <vector>
#include <memory>
#include <unordered_set>
#include <asio.hpp>
#include <columnlynx/common/net/tcp/tcp_message_type.hpp>
#include <columnlynx/common/net/tcp/net_helper.hpp>
#include <columnlynx/common/utils.hpp>
namespace ColumnLynx::Net::TCP {
void TCPServer::mStartAccept() {
mAcceptor.async_accept(
[this](asio::error_code ec, asio::ip::tcp::socket socket) {
if (ec) {
if (ec == asio::error::operation_aborted) {
// Acceptor was cancelled/closed during shutdown
return;
}
Utils::error("Accept failed: " + ec.message());
// Try again only if still running
if (ServerSession::getInstance().isHostRunning() && mAcceptor.is_open())
mStartAccept();
return;
}
auto client = TCPConnection::create(
std::move(socket),
[this](std::shared_ptr<TCPConnection> c) {
std::lock_guard<std::mutex> lock(mClientsMutex);
mClients.erase(c);
Utils::log("Client removed.");
}
);
{
std::lock_guard<std::mutex> lock(mClientsMutex);
mClients.insert(client);
}
client->start();
Utils::log("Accepted new client connection.");
if (ServerSession::getInstance().isHostRunning() && mAcceptor.is_open())
mStartAccept();
}
);
}
void TCPServer::stop() {
// Stop accepting
if (mAcceptor.is_open()) {
asio::error_code ec;
mAcceptor.cancel(ec);
mAcceptor.close(ec);
Utils::log("TCP Acceptor closed.");
}
// Snapshot to avoid iterator invalidation while callbacks erase()
std::vector<std::shared_ptr<TCPConnection>> snapshot;
{
std::lock_guard<std::mutex> lock(mClientsMutex);
snapshot.assign(mClients.begin(), mClients.end());
}
for (auto &client : snapshot) {
try {
client->disconnect(); // should shutdown+close the socket
Utils::log("GRACEFUL_DISCONNECT sent to session: " + std::to_string(client->getSessionID()));
} catch (const std::exception& e) {
Utils::error(std::string("Error disconnecting client: ") + e.what());
}
}
// Let the erase callback run as sockets close
// Do NOT destroy server while io handlers may still reference it
}
}