Test fixing writing bug

This commit is contained in:
2026-01-01 21:27:13 +01:00
parent 2d3d6afb07
commit 62335f3693

View File

@@ -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...");