From a472aafb3eb75eec4bc84b8c976cdb06b5f2f0c8 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Tue, 16 Jun 2020 21:21:24 -0700 Subject: [PATCH] Windows now builds at least to the point of running zt_core_tests. Go will need a revision to the command in CMake files. --- build.bat | 4 ++++ core/AES.cpp | 10 ++++++++++ core/Constants.hpp | 2 ++ core/InetAddress.cpp | 3 +-- core/OS.hpp | 3 +++ core/Tests.cpp | 9 ++------- core/Utils.cpp | 1 + osdep/OSUtils.cpp | 20 +++++++++---------- osdep/OSUtils.hpp | 11 +++-------- osdep/WindowsEthernetTap.cpp | 27 +++++++++++++------------ serviceiocore/GoGlue.cpp | 38 ++++++++++++++++++++++++------------ 11 files changed, 76 insertions(+), 52 deletions(-) create mode 100644 build.bat diff --git a/build.bat b/build.bat new file mode 100644 index 000000000..4f1ea6bab --- /dev/null +++ b/build.bat @@ -0,0 +1,4 @@ +echo off +mkdir build +cd build +cmake .. -G "MinGW Makefiles" diff --git a/core/AES.cpp b/core/AES.cpp index aecd2c159..4aee9e5df 100644 --- a/core/AES.cpp +++ b/core/AES.cpp @@ -512,6 +512,9 @@ void AES::GMAC::finish(uint8_t tag[16]) noexcept #ifdef ZT_AES_AESNI +#if !defined(__WINDOWS__) + +#define ZT_AES_VAES512 static __attribute__((__target__("sse4,avx,avx2,vaes,avx512f,avx512bw"))) void p_aesCtrInnerVAES512(unsigned int &len, const uint64_t c0, uint64_t &c1, const uint8_t *&in, uint8_t *&out, const __m128i *const k) noexcept @@ -562,6 +565,7 @@ void p_aesCtrInnerVAES512(unsigned int &len, const uint64_t c0, uint64_t &c1, co } while (len >= 64); } +#define ZT_AES_VAES256 static __attribute__((__target__("sse4,avx,avx2,vaes"))) void p_aesCtrInnerVAES256(unsigned int &len, uint64_t &c0, uint64_t &c1, const uint8_t *&in, uint8_t *&out, const __m128i *const k) noexcept @@ -630,6 +634,8 @@ void p_aesCtrInnerVAES256(unsigned int &len, uint64_t &c0, uint64_t &c1, const u } while (len >= 64); } +#endif + static void p_aesCtrInner128(unsigned int &len, uint64_t &c0, uint64_t &c1, const uint8_t *&in, uint8_t *&out, const __m128i *const k) noexcept { const __m128i k0 = k[0]; @@ -787,6 +793,7 @@ void AES::CTR::crypt(const void *const input, unsigned int len) noexcept _len = totalLen + len; if (likely(len >= 64)) { +#if defined(ZT_AES_VAES512) && defined(ZT_AES_VAES256) if (Utils::CPUID.vaes) { if ((!Utils::CPUID.avx512f) || ((len < 1024))) { p_aesCtrInnerVAES256(len, c0, c1, in, out, k); @@ -794,8 +801,11 @@ void AES::CTR::crypt(const void *const input, unsigned int len) noexcept p_aesCtrInnerVAES512(len, c0, c1, in, out, k); } } else { +#endif p_aesCtrInner128(len, c0, c1, in, out, k); +#if defined(ZT_AES_VAES512) && defined(ZT_AES_VAES256) } +#endif } while (len >= 16) { diff --git a/core/Constants.hpp b/core/Constants.hpp index 693ba02d8..f8e1a562d 100644 --- a/core/Constants.hpp +++ b/core/Constants.hpp @@ -17,7 +17,9 @@ #include "zerotier.h" #include "OS.hpp" +#if __has_include("version.h") #include "version.h" +#endif /** * Version bit packed into four 16-bit fields in a 64-bit unsigned integer. diff --git a/core/InetAddress.cpp b/core/InetAddress.cpp index ad2385097..a87b15d36 100644 --- a/core/InetAddress.cpp +++ b/core/InetAddress.cpp @@ -11,8 +11,7 @@ */ /****/ -#include -#include +#define _WIN32_WINNT 0x06010000 #include "Constants.hpp" #include "InetAddress.hpp" diff --git a/core/OS.hpp b/core/OS.hpp index 93e2804e4..9b6e473e7 100644 --- a/core/OS.hpp +++ b/core/OS.hpp @@ -37,7 +37,10 @@ #undef __UNIX_LIKE__ #undef __BSD__ #include +#include #include +#include +#include #include #endif diff --git a/core/Tests.cpp b/core/Tests.cpp index f87f2e586..38f7a9f47 100644 --- a/core/Tests.cpp +++ b/core/Tests.cpp @@ -57,13 +57,8 @@ static int64_t now() { #ifdef __WINDOWS__ FILETIME ft; - SYSTEMTIME st; - ULARGE_INTEGER tmp; - GetSystemTime(&st); - SystemTimeToFileTime(&st,&ft); - tmp.LowPart = ft.dwLowDateTime; - tmp.HighPart = ft.dwHighDateTime; - return (int64_t)( ((tmp.QuadPart - 116444736000000000LL) / 10000L) + st.wMilliseconds ); + GetSystemTimeAsFileTime(&ft); + return (((LONGLONG)ft.dwLowDateTime + ((LONGLONG)(ft.dwHighDateTime) << 32)) / 10000LL) - 116444736000000000LL; #else timeval tv; // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init) gettimeofday(&tv,nullptr); diff --git a/core/Utils.cpp b/core/Utils.cpp index 51d9a0c90..c36ecc2a9 100644 --- a/core/Utils.cpp +++ b/core/Utils.cpp @@ -27,6 +27,7 @@ #include #ifdef __WINDOWS__ +#include #include #endif diff --git a/osdep/OSUtils.cpp b/osdep/OSUtils.cpp index 31f14a397..f1d56c307 100644 --- a/osdep/OSUtils.cpp +++ b/osdep/OSUtils.cpp @@ -16,11 +16,9 @@ #include "../core/Containers.hpp" #include "OSUtils.hpp" -#ifdef __WINDOWS__ -#include -#include -#include -#else +#include + +#ifndef __WINDOWS__ #include #include #endif @@ -339,13 +337,15 @@ ZeroTier::String OSUtils::platformDefaultHomePath() #ifdef __WINDOWS__ // Look up app data folder on Windows, e.g. C:\ProgramData\... char buf[16384]; - if (SUCCEEDED(SHGetFolderPathA(NULL,CSIDL_COMMON_APPDATA,NULL,0,buf))) - return (ZeroTier::String(buf) + "\\ZeroTier"); - else return ZeroTier::String("C:\\ZeroTier"); + if (SUCCEEDED(SHGetFolderPathA(NULL,CSIDL_COMMON_APPDATA,NULL,0,buf))) { + ZeroTier::String tmp(buf); + tmp.append("\\ZeroTier"); + return tmp; + } else { + return ZeroTier::String("C:\\ZeroTier"); + } #else - return (ZeroTier::String(ZT_PATH_SEPARATOR_S) + "ZeroTier"); // UNKNOWN PLATFORM - #endif #endif // __UNIX_LIKE__ or not... diff --git a/osdep/OSUtils.hpp b/osdep/OSUtils.hpp index c065249ea..1807d831c 100644 --- a/osdep/OSUtils.hpp +++ b/osdep/OSUtils.hpp @@ -21,7 +21,7 @@ #include #ifndef __WINDOWS__ -#include // NOLINT(modernize-deprecated-headers) +#include #include #include #include @@ -154,13 +154,8 @@ public: { #ifdef __WINDOWS__ FILETIME ft; - SYSTEMTIME st; - ULARGE_INTEGER tmp; - GetSystemTime(&st); - SystemTimeToFileTime(&st,&ft); - tmp.LowPart = ft.dwLowDateTime; - tmp.HighPart = ft.dwHighDateTime; - return (int64_t)( ((tmp.QuadPart - 116444736000000000LL) / 10000L) + st.wMilliseconds ); + GetSystemTimeAsFileTime(&ft); + return (((LONGLONG)ft.dwLowDateTime + ((LONGLONG)(ft.dwHighDateTime) << 32)) / 10000LL) - 116444736000000000LL; #else #ifdef __LINUX__ timespec ts; diff --git a/osdep/WindowsEthernetTap.cpp b/osdep/WindowsEthernetTap.cpp index 1440c703c..c29760d2c 100644 --- a/osdep/WindowsEthernetTap.cpp +++ b/osdep/WindowsEthernetTap.cpp @@ -11,6 +11,8 @@ */ /****/ +#define _WIN32_WINNT 0x06010000 + #include "../core/Constants.hpp" #ifdef __WINDOWS__ @@ -30,7 +32,8 @@ #include #include #include -#include +#include +//#include #include #include #include @@ -46,7 +49,7 @@ #include "WindowsEthernetTap.hpp" #include "OSUtils.hpp" -#include "..\windows\TapDriver6\tap-windows.h" +#include "../installsupport/windows/tap-windows-ndis6/src/tap-windows-ndis6/tap-windows.h" #include @@ -790,10 +793,10 @@ void WindowsEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherTyp _injectPending.emplace(); _injectPending.back().len = len + 14; char *const d = _injectPending.back().data; - to.copyTo(d,6); - from.copyTo(d + 6,6); - d[12] = (char)((etherType >> 8) & 0xff); - d[13] = (char)(etherType & 0xff); + to.copyTo((uint8_t *)d); + from.copyTo((uint8_t *)(d + 6)); + d[12] = (char)((etherType >> 8U) & 0xffU); + d[13] = (char)(etherType & 0xffU); memcpy(d + 14,data,len); ReleaseSemaphore(_injectSemaphore,1,NULL); @@ -893,7 +896,7 @@ void WindowsEthernetTap::scanMulticastGroups(std::vector &added, MAC mac; DWORD i = 0; while ((i + 6) <= bytesReturned) { - mac.setTo(mcastbuf + i,6); + mac.setTo((uint8_t *)(mcastbuf + i)); i += 6; if ((mac.isMulticast())&&(!mac.isBroadcast())) { // exclude the nulls that may be returned or any other junk Windows puts in there @@ -1049,7 +1052,7 @@ void WindowsEthernetTap::threadMain() nr.NextHop.si_family = AF_INET; nr.NextHop.Ipv4.sin_addr.s_addr = fakeIp; nr.Metric = 9999; // do not use as real default route - nr.Protocol = MIB_IPPROTO_NETMGMT; + nr.Protocol = (NL_ROUTE_PROTOCOL)MIB_IPPROTO_NETMGMT; DWORD result = CreateIpForwardEntry2(&nr); if (result != NO_ERROR) Sleep(250); @@ -1128,11 +1131,11 @@ void WindowsEthernetTap::threadMain() DWORD bytesRead = 0; if (GetOverlappedResult(_tap,&tapOvlRead,&bytesRead,FALSE)) { if ((bytesRead > 14)&&(_enabled)) { - MAC to(tapReadBuf,6); - MAC from(tapReadBuf + 6,6); - unsigned int etherType = ((((unsigned int)tapReadBuf[12]) & 0xff) << 8) | (((unsigned int)tapReadBuf[13]) & 0xff); + MAC to((uint8_t *)tapReadBuf); + MAC from((uint8_t *)(tapReadBuf + 6)); + unsigned int etherType = ((((unsigned int)tapReadBuf[12]) & 0xffU) << 8U) | (((unsigned int)tapReadBuf[13]) & 0xffU); try { - _handler(_arg,(void *)0,_nwid,from,to,etherType,0,tapReadBuf + 14,bytesRead - 14); + _handler(_arg,nullptr,_nwid,from,to,etherType,0,tapReadBuf + 14,bytesRead - 14); } catch ( ... ) {} // handlers should not throw } } diff --git a/serviceiocore/GoGlue.cpp b/serviceiocore/GoGlue.cpp index ef34989c1..7a68f8bf2 100644 --- a/serviceiocore/GoGlue.cpp +++ b/serviceiocore/GoGlue.cpp @@ -11,6 +11,8 @@ */ /****/ +#define _WIN32_WINNT 0x06010000 + #include "GoGlue.h" #include "../core/Constants.hpp" @@ -198,19 +200,19 @@ static ZT_INLINE void doUdpSend(ZT_SOCKET sock, const struct sockaddr_storage *a #else int tmp = (int)ipTTL; #endif - setsockopt(sock, IPPROTO_IP, IP_TTL, &tmp, sizeof(tmp)); - sendto(sock, data, len, MSG_DONTWAIT, (const sockaddr *)addr, sizeof(struct sockaddr_in)); + setsockopt(sock, IPPROTO_IP, IP_TTL, reinterpret_cast(&tmp), sizeof(tmp)); + sendto(sock, reinterpret_cast(data), len, MSG_DONTWAIT, (const sockaddr *)addr, sizeof(struct sockaddr_in)); tmp = 255; - setsockopt(sock, IPPROTO_IP, IP_TTL, &tmp, sizeof(tmp)); + setsockopt(sock, IPPROTO_IP, IP_TTL, reinterpret_cast(&tmp), sizeof(tmp)); } else { - sendto(sock, data, len, MSG_DONTWAIT, (const sockaddr *)addr, sizeof(struct sockaddr_in)); + sendto(sock, reinterpret_cast(data), len, MSG_DONTWAIT, (const sockaddr *)addr, sizeof(struct sockaddr_in)); } break; case AF_INET6: // The ipTTL option isn't currently used with IPv6. It's only used // with IPv4 "firewall opener" / "NAT buster" preamble packets as part // of IPv4 NAT traversal. - sendto(sock, data, len, MSG_DONTWAIT, (const sockaddr *)addr, sizeof(struct sockaddr_in6)); + sendto(sock, reinterpret_cast(data), len, MSG_DONTWAIT, (const sockaddr *)addr, sizeof(struct sockaddr_in6)); break; } } @@ -368,8 +370,13 @@ extern "C" void ZT_GoNode_delete(ZT_GoNode *gn) gn->threads_l.lock(); for (auto t = gn->threads.begin(); t != gn->threads.end(); ++t) { t->second.run = false; +#ifdef __WINDOWS__ + shutdown(t->first, SD_BOTH); + closesocket(t->first); +#else shutdown(t->first, SHUT_RDWR); close(t->first); +#endif t->second.thr.join(); } gn->threads_l.unlock(); @@ -412,23 +419,23 @@ static void setCommonUdpSocketSettings(ZT_SOCKET udpSock, const char *dev) #ifdef SO_REUSEPORT fl = SETSOCKOPT_FLAG_TRUE; - setsockopt(udpSock, SOL_SOCKET, SO_REUSEPORT, (void *)&fl, sizeof(fl)); + setsockopt(udpSock, SOL_SOCKET, SO_REUSEPORT, &fl, sizeof(fl)); #endif #ifndef __LINUX__ // linux wants just SO_REUSEPORT fl = SETSOCKOPT_FLAG_TRUE; - setsockopt(udpSock, SOL_SOCKET, SO_REUSEADDR, (void *)&fl, sizeof(fl)); + setsockopt(udpSock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&fl), sizeof(fl)); #endif fl = SETSOCKOPT_FLAG_TRUE; - setsockopt(udpSock, SOL_SOCKET, SO_BROADCAST, (void *)&fl, sizeof(fl)); + setsockopt(udpSock, SOL_SOCKET, SO_BROADCAST, reinterpret_cast(&fl), sizeof(fl)); #ifdef IP_DONTFRAG fl = SETSOCKOPT_FLAG_FALSE; - setsockopt(udpSock,IPPROTO_IP,IP_DONTFRAG,(void *)&fl,sizeof(fl)); + setsockopt(udpSock,IPPROTO_IP,IP_DONTFRAG,&fl,sizeof(fl)); #endif #ifdef IP_MTU_DISCOVER fl = SETSOCKOPT_FLAG_FALSE; - setsockopt(udpSock,IPPROTO_IP,IP_MTU_DISCOVER,(void *)&fl,sizeof(fl)); + setsockopt(udpSock,IPPROTO_IP,IP_MTU_DISCOVER,&fl,sizeof(fl)); #endif #ifdef SO_BINDTODEVICE @@ -462,7 +469,7 @@ extern "C" int ZT_GoNode_phyStartListen(ZT_GoNode *gn, const char *dev, const ch setsockopt(udpSock, IPPROTO_IPV6, IPV6_V6ONLY, (const char *)&fl, sizeof(fl)); #ifdef IPV6_DONTFRAG fl = SETSOCKOPT_FLAG_FALSE; - setsockopt(udpSock,IPPROTO_IPV6,IPV6_DONTFRAG,&fl,sizeof(fl)); + setsockopt(udpSock, IPPROTO_IPV6, IPV6_DONTFRAG, reinterpret_cast(&fl), sizeof(fl)); #endif if (bind(udpSock, reinterpret_cast(&in6), sizeof(in6)) != 0) @@ -483,7 +490,7 @@ extern "C" int ZT_GoNode_phyStartListen(ZT_GoNode *gn, const char *dev, const ch salen = sizeof(in6); void *buf = ZT_getBuffer(); if (buf) { - int s = (int)recvfrom(udpSock, buf, 16384, 0, reinterpret_cast(&in6), &salen); + int s = (int)recvfrom(udpSock, reinterpret_cast(buf), 16384, 0, reinterpret_cast(&in6), &salen); if (s > 0) { ZT_Node_processWirePacket( reinterpret_cast(gn->node), @@ -538,7 +545,7 @@ extern "C" int ZT_GoNode_phyStartListen(ZT_GoNode *gn, const char *dev, const ch salen = sizeof(in4); void *buf = ZT_getBuffer(); if (buf) { - int s = (int)recvfrom(udpSock, buf, sizeof(buf), 0, reinterpret_cast(&in4), &salen); + int s = (int)recvfrom(udpSock, reinterpret_cast(buf), sizeof(buf), 0, reinterpret_cast(&in4), &salen); if (s > 0) { ZT_Node_processWirePacket( reinterpret_cast(gn->node), @@ -570,8 +577,13 @@ extern "C" int ZT_GoNode_phyStopListen(ZT_GoNode *gn, const char *dev, const cha for (auto t = gn->threads.begin(); t != gn->threads.end();) { if ((t->second.ip == ip) && (t->second.port == port)) { t->second.run = false; +#ifdef __WINDOWS__ + shutdown(t->first, SD_BOTH); + closesocket(t->first); +#else shutdown(t->first, SHUT_RDWR); close(t->first); +#endif t->second.thr.join(); gn->threads.erase(t++); } else ++t;