From 62335f3693faa0f996e9c1d58a217b3098df0b90 Mon Sep 17 00:00:00 2001 From: DcruBro Date: Thu, 1 Jan 2026 21:27:13 +0100 Subject: [PATCH] Test fixing writing bug --- src/server/main.cpp | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/server/main.cpp b/src/server/main.cpp index 8d4fb9f..151a2e2 100644 --- a/src/server/main.cpp +++ b/src/server/main.cpp @@ -153,15 +153,28 @@ int main(int argc, char** argv) { } const uint8_t* ip = packet.data(); - uint32_t srcIP = ntohl(*(uint32_t*)(ip + 12)); // IPv4 source address offset (packet comes FROM client) + uint32_t srcIP = ntohl(*(uint32_t*)(ip + 12)); // IPv4 source address offset + uint32_t dstIP = ntohl(*(uint32_t*)(ip + 16)); // IPv4 destination address offset - auto session = SessionRegistry::getInstance().getByIP(srcIP); - if (!session) { - Utils::warn("TUN: No session found for source IP " + VirtualInterface::ipv4ToString(srcIP)); + // First, check if destination IP is a registered client (e.g., server responding to client or client-to-client) + auto dstSession = SessionRegistry::getInstance().getByIP(dstIP); + if (dstSession) { + // Destination is a registered client, forward to that client's session + udpServer->sendData(dstSession->sessionID, std::string(packet.begin(), packet.end())); continue; } - udpServer->sendData(session->sessionID, std::string(packet.begin(), packet.end())); + // Destination is not a registered client, check if source is (for external routing) + auto srcSession = SessionRegistry::getInstance().getByIP(srcIP); + if (srcSession) { + // Source is a registered client, write to TUN interface to forward to external destination + tun->writePacket(packet); + continue; + } + + // Neither source nor destination is registered, drop the packet + Utils::warn("TUN: No session found for source IP " + VirtualInterface::ipv4ToString(srcIP) + + " or destination IP " + VirtualInterface::ipv4ToString(dstIP)); } log("Shutting down server...");