From 8923f45356f61907b7a283db07f88e2e2c33eac3 Mon Sep 17 00:00:00 2001 From: DcruBro Date: Mon, 29 Dec 2025 19:33:57 +0100 Subject: [PATCH] Add routeset to win32 --- src/common/virtual_interface.cpp | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/common/virtual_interface.cpp b/src/common/virtual_interface.cpp index ee571ed..9db549c 100644 --- a/src/common/virtual_interface.cpp +++ b/src/common/virtual_interface.cpp @@ -327,7 +327,15 @@ namespace ColumnLynx::Net { ); system(cmd); #elif defined(_WIN32) - char cmd[256]; + char cmd[512]; + // Remove any persistent routes associated with this interface + snprintf(cmd, sizeof(cmd), + "netsh routing ip delete persistentroute all name=\"%s\"", + mIfName.c_str() + ); + system(cmd); + + // Reset to DHCP snprintf(cmd, sizeof(cmd), "netsh interface ip set address name=\"%s\" dhcp", mIfName.c_str() @@ -420,7 +428,11 @@ namespace ColumnLynx::Net { uint32_t maskInt = (prefixLen == 0) ? 0 : (0xFFFFFFFF << (32 - prefixLen)); mask = ipv4ToString(maskInt); - char cmd[256]; + // Calculate network address from IP and mask + uint32_t networkInt = (clientIP & maskInt); + std::string network = ipv4ToString(networkInt); + + char cmd[512]; // 1. Set the static IP + mask + gateway snprintf(cmd, sizeof(cmd), @@ -436,6 +448,14 @@ namespace ColumnLynx::Net { ); system(cmd); + // 3. Add route for the VPN network to go through the TUN interface + // This is critical: tells Windows to send packets destined for the server/network through the TUN interface + snprintf(cmd, sizeof(cmd), + "netsh routing ip add persistentroute dest=%s/%d name=\"%s\" nexthopcfg=%s", + network.c_str(), prefixLen, mIfName.c_str(), gw.c_str() + ); + system(cmd); + return true; #else return false;