From 7d6c1dc2909828bcdb2014a94c6551597929b36f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Friedrich=20Scho=CC=88ller?= Date: Tue, 14 May 2013 02:47:37 +0200 Subject: [PATCH] Fixed netsh call on windows --- src/tun.cpp | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/tun.cpp b/src/tun.cpp index f66b0d1..b4104f9 100644 --- a/src/tun.cpp +++ b/src/tun.cpp @@ -30,10 +30,28 @@ #include #include +#ifdef WIN32 +#include +#endif + typedef ip IpHeader; using namespace std; +#ifdef WIN32 +void winsystem(char *cmd) +{ + STARTUPINFO info = { sizeof(info) }; + PROCESS_INFORMATION processInfo; + if (CreateProcess(NULL, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo)) + { + WaitForSingleObject(processInfo.hProcess, INFINITE); + CloseHandle(processInfo.hProcess); + CloseHandle(processInfo.hThread); + } +} +#endif + Tun::Tun(const char *device, int mtu) { char cmdline[512]; @@ -56,8 +74,7 @@ Tun::Tun(const char *device, int mtu) #ifdef WIN32 snprintf(cmdline, sizeof(cmdline), "netsh interface ipv4 set subinterface \"%s\" mtu=%d", this->device, mtu); - if (system(cmdline) != 0) - syslog(LOG_ERR, "could not set tun device mtu"); + winsystem(cmdline); #else snprintf(cmdline, sizeof(cmdline), "/sbin/ifconfig %s mtu %u", this->device, mtu); if (system(cmdline) != 0) @@ -79,8 +96,7 @@ void Tun::setIp(uint32_t ip, uint32_t destIp, bool includeSubnet) #ifdef WIN32 snprintf(cmdline, sizeof(cmdline), "netsh interface ip set address name=\"%s\" " "static %s 255.255.255.0", device, ips.c_str()); - if (system(cmdline) != 0) - syslog(LOG_ERR, "could not set tun device ip address"); + winsystem(cmdline); if (!tun_set_ip(fd, ip, ip & 0xffffff00, 0xffffff00)) syslog(LOG_ERR, "could not set tun device driver ip address: %s", tun_last_error());