diff --git a/CMakeLists.txt b/CMakeLists.txt index fa90f3b33..7ab825b5b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,8 +47,10 @@ else() set(THREAD_LIB pthread) endif() +if (NOT MSVC) add_cflags("-march=native") add_cxxflags("-march=native") +endif(NOT MSVC) if(STATIC_LINK) add_cflags("-static -Wl,--whole-archive -lpthread -Wl,--no-whole-archive") @@ -165,12 +167,12 @@ if(UNIX) set(LIBTUNTAP_IMPL ${TT_ROOT}/tuntap-unix-openbsd.c ${TT_ROOT}/tuntap-unix-bsd.c) elseif (${CMAKE_SYSTEM_NAME} MATCHES "NetBSD") set(LIBTUNTAP_IMPL ${TT_ROOT}/tuntap-unix-netbsd.c ${TT_ROOT}/tuntap-unix-bsd.c) - elseif (${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD") - set(LIBTUNTAP_IMPL ${TT_ROOT}/tuntap-unix-freebsd.c ${TT_ROOT}/tuntap-unix-bsd.c) - elseif (${CMAKE_SYSTEM_NAME} MATCHES "DragonFly") + elseif (${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD" OR ${CMAKE_SYSTEM_NAME} MATCHES "DragonFly") set(LIBTUNTAP_IMPL ${TT_ROOT}/tuntap-unix-freebsd.c ${TT_ROOT}/tuntap-unix-bsd.c) elseif (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") set(LIBTUNTAP_IMPL ${TT_ROOT}/tuntap-unix-darwin.c ${TT_ROOT}/tuntap-unix-bsd.c) + elseif (${CMAKE_SYSTEM_NAME} MATCHES "SunOS") + set(LIBTUNTAP_IMPL ${TT_ROOT}/tuntap-unix-sunos.c) else() message(FATAL_ERROR "Your operating system is not supported yet") endif() diff --git a/crypto/libntrup/src/ntru.cpp b/crypto/libntrup/src/ntru.cpp index bea012d44..915b3e865 100644 --- a/crypto/libntrup/src/ntru.cpp +++ b/crypto/libntrup/src/ntru.cpp @@ -1,5 +1,6 @@ #include #include +#include #if __AVX2__ #include diff --git a/crypto/libntrup/src/ref/params.h b/crypto/libntrup/src/ref/params.h index 655e6ec09..7458ed8c5 100644 --- a/crypto/libntrup/src/ref/params.h +++ b/crypto/libntrup/src/ref/params.h @@ -6,6 +6,9 @@ #define qshift 2295 #define p 761 +#ifdef _MSC_VER +#define LOOPS 2*p+1 +#endif #define w 286 #define rq_encode_len 1218 diff --git a/crypto/libntrup/src/ref/r3_recip.c b/crypto/libntrup/src/ref/r3_recip.c index 3c56b7fe9..12b185e38 100644 --- a/crypto/libntrup/src/ref/r3_recip.c +++ b/crypto/libntrup/src/ref/r3_recip.c @@ -40,8 +40,13 @@ int r3_recip(small *r,const small *s) int loop; small f[p + 1]; small g[p + 1]; +#ifdef _MSC_VER + small u[LOOPS + 1]; + small v[LOOPS + 1]; +#else small u[loops + 1]; small v[loops + 1]; +#endif small c; int i; int d = p; diff --git a/crypto/libntrup/src/ref/rq_recip3.c b/crypto/libntrup/src/ref/rq_recip3.c index 925b10fc6..54f048428 100644 --- a/crypto/libntrup/src/ref/rq_recip3.c +++ b/crypto/libntrup/src/ref/rq_recip3.c @@ -39,8 +39,13 @@ int rq_recip3(modq *r,const small *s) int loop; modq f[p + 1]; modq g[p + 1]; +#ifdef _MSC_VER + modq u[LOOPS + 1]; + modq v[LOOPS + 1]; +#else modq u[loops + 1]; modq v[loops + 1]; +#endif modq c; int i; int d = p; diff --git a/include/llarp/codel.hpp b/include/llarp/codel.hpp index 2715c2422..a4e64aea2 100644 --- a/include/llarp/codel.hpp +++ b/include/llarp/codel.hpp @@ -2,6 +2,9 @@ #define LLARP_CODEL_QUEUE_HPP #ifdef _MSC_VER #define NOMINMAX +#ifdef min +#undef min +#endif #endif #include #include diff --git a/include/llarp/endian.h b/include/llarp/endian.h index 5a195f4e5..6405dd97b 100644 --- a/include/llarp/endian.h +++ b/include/llarp/endian.h @@ -8,6 +8,26 @@ #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) #include +#elif defined(__sun) +#include +#define htobe16(x) htons(x) +#define htole16(x) (x) +#define be16toh(x) ntohs(x) +#define le16toh(x) (x) + +#define htobe32(x) htonl(x) +#define htole32(x) (x) +#define be32toh(x) ntohl(x) +#define le32toh(x) (x) + +#define htobe64(x) \ + (((uint64_t)htonl(((uint32_t)(((uint64_t)(x)) >> 32)))) \ + | (((uint64_t)htonl(((uint32_t)(x)))) << 32)) +#define htole64(x) (x) +#define be64toh(x) \ + (((uint64_t)ntohl(((uint32_t)(((uint64_t)(x)) >> 32)))) \ + | (((uint64_t)ntohl(((uint32_t)(x)))) << 32)) +#define le64toh(x) (x) #elif defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__GLIBC__) #include #elif defined(__APPLE__) && defined(__MACH__) diff --git a/include/llarp/ip.hpp b/include/llarp/ip.hpp index 8fc46fe5c..e7183f2ae 100644 --- a/include/llarp/ip.hpp +++ b/include/llarp/ip.hpp @@ -2,8 +2,47 @@ #define LLARP_IP_HPP #include #include +#ifndef _WIN32 +#include #include +#else +#include +// Apparently this does not seem to be located _anywhere_ in the windows sdk??? +// -despair86 +typedef struct ip_hdr +{ + unsigned char + ip_header_len : 4; // 4-bit header length (in 32-bit words) normally=5 + // (Means 20 Bytes may be 24 also) + unsigned char ip_version : 4; // 4-bit IPv4 version + unsigned char ip_tos; // IP type of service + unsigned short ip_total_length; // Total length + unsigned short ip_id; // Unique identifier + + unsigned char ip_frag_offset : 5; // Fragment offset field + + unsigned char ip_more_fragment : 1; + unsigned char ip_dont_fragment : 1; + unsigned char ip_reserved_zero : 1; + + unsigned char ip_frag_offset1; // fragment offset + + unsigned char ip_ttl; // Time to live + unsigned char ip_protocol; // Protocol(TCP,UDP etc) + unsigned short ip_checksum; // IP checksum + unsigned int ip_srcaddr; // Source address + unsigned int ip_destaddr; // Source address +} IPV4_HDR; +#define iphdr IPV4_HDR +#define saddr ip_srcaddr; +#define daddr ip_destaddr; +#endif #include +#if !defined(__linux__) && !defined(_WIN32) +#define iphdr ip +#define saddr ip_src.s_addr +#define daddr ip_dst.s_addr +#endif namespace llarp { diff --git a/include/llarp/iwp/server.hpp b/include/llarp/iwp/server.hpp index ce349761e..ba5bf790d 100644 --- a/include/llarp/iwp/server.hpp +++ b/include/llarp/iwp/server.hpp @@ -5,6 +5,12 @@ #include "router.hpp" #include "session.hpp" +#ifndef __linux__ +#ifdef saddr +#undef saddr +#endif +#endif + #include #include diff --git a/include/tuntap.h b/include/tuntap.h index cb42d20c7..8199f4d76 100644 --- a/include/tuntap.h +++ b/include/tuntap.h @@ -37,8 +37,8 @@ #else #include #endif -#include #include +#include #endif #include @@ -56,7 +56,7 @@ */ #if defined ETH_ALEN /* Linux */ #define ETHER_ADDR_LEN ETH_ALEN -#elif defined Windows +#elif defined Windows || defined __sun__ #define ETHER_ADDR_LEN 6 #endif @@ -86,7 +86,7 @@ */ #if defined Windows #include -#include +#include typedef HANDLE t_tun; typedef IN_ADDR t_tun_in_addr; typedef IN6_ADDR t_tun_in6_addr; diff --git a/llarp/context.cpp b/llarp/context.cpp index b797b86f5..e3185bca4 100644 --- a/llarp/context.cpp +++ b/llarp/context.cpp @@ -20,7 +20,7 @@ extern "C" void SetThreadName(DWORD dwThreadID, LPCSTR szThreadName); #endif -#ifdef _WIN32 +#if _WIN32 || __sun #define wmin(x, y) (((x) < (y)) ? (x) : (y)) #define MIN wmin #endif diff --git a/llarp/ev.cpp b/llarp/ev.cpp index 795311e0c..67d4ec255 100644 --- a/llarp/ev.cpp +++ b/llarp/ev.cpp @@ -4,7 +4,8 @@ #define EV_TICK_INTERVAL 100 -#ifdef __linux__ +// apparently current Solaris will emulate epoll. +#if __linux__ || __sun__ #include "ev_epoll.hpp" #endif #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \ @@ -18,7 +19,7 @@ void llarp_ev_loop_alloc(struct llarp_ev_loop **ev) { -#ifdef __linux__ +#if __linux__ || __sun__ *ev = new llarp_epoll_loop; #endif #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \ @@ -27,12 +28,6 @@ llarp_ev_loop_alloc(struct llarp_ev_loop **ev) #endif #if defined(_WIN32) || defined(_WIN64) || defined(__NT__) *ev = new llarp_win32_loop; -#endif - // a) I assume that the libre fork of Solaris is still - // 5.10, and b) the current commercial version is 5.11, naturally. - // -despair86 -#if defined(__SunOS_5_10) || defined(__SunOS_5_11) - *ev = new llarp_sun_iocp_loop; #endif (*ev)->init(); } @@ -116,4 +111,4 @@ bool llarp_ev_tun_async_write(struct llarp_tun_io *tun, const void *pkt, size_t sz) { return static_cast< llarp::ev_io * >(tun->impl)->queue_write(pkt, sz); -} \ No newline at end of file +} diff --git a/llarp/ev.hpp b/llarp/ev.hpp index 6237a1246..46cdea5e6 100644 --- a/llarp/ev.hpp +++ b/llarp/ev.hpp @@ -52,7 +52,12 @@ namespace llarp flush_write() { m_writeq.Process([this](const std::unique_ptr< WriteBuffer >& buffer) { + // todo: wtf??? +#ifndef _WIN32 write(fd, buffer->buf, buffer->bufsz); +#else + // writefile +#endif }); /// reset errno errno = 0; diff --git a/llarp/ev_win32.hpp b/llarp/ev_win32.hpp index 38768568f..1453f7e79 100644 --- a/llarp/ev_win32.hpp +++ b/llarp/ev_win32.hpp @@ -21,12 +21,6 @@ namespace llarp WSAOVERLAPPED portfds[2] = {0}; size_t iosz; - // the unique completion key that helps us to - // identify the object instance for which we receive data - // Here, we'll use the address of the udp_listener instance, converted to - // its literal int/int64 representation. - ULONG_PTR listener_id = 0; - udp_listener(SOCKET fd, llarp_udp_io* u) : ev_io(fd), udp(u){}; ~udp_listener() @@ -97,8 +91,6 @@ struct llarp_win32_loop : public llarp_ev_loop { HANDLE iocpfd; - bool _running = false; - llarp_win32_loop() : iocpfd(INVALID_HANDLE_VALUE) { WSADATA wsockd; @@ -127,7 +119,7 @@ struct llarp_win32_loop : public llarp_ev_loop if(iocpfd == INVALID_HANDLE_VALUE) return false; - _running = true; + return true; } @@ -300,7 +292,7 @@ struct llarp_win32_loop : public llarp_ev_loop } bool - add_ev(llarp::ev_io* ev) + add_ev(llarp::ev_io* ev, bool write) { ev->listener_id = reinterpret_cast< ULONG_PTR >(ev); if(!::CreateIoCompletionPort(reinterpret_cast< HANDLE >(ev->fd), iocpfd, @@ -331,13 +323,13 @@ struct llarp_win32_loop : public llarp_ev_loop bool running() const { - return _running; + return iocpfd != INVALID_HANDLE_VALUE; } void stop() { - _running = false; + // still does nothing } }; diff --git a/llarp/fs.hpp b/llarp/fs.hpp index ad6d06dd3..9b0e71889 100644 --- a/llarp/fs.hpp +++ b/llarp/fs.hpp @@ -12,7 +12,7 @@ // win32 is the only one that doesn't use cpp17::filesystem // because cpp17::filesystem is unimplemented for Windows // -despair86 -#if defined(__MINGW32__) || defined(_MSC_VER) +#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__sun) namespace fs = std::experimental::filesystem; #else namespace fs = std::filesystem; diff --git a/llarp/threadpool.cpp b/llarp/threadpool.cpp index 530b7a191..75bd5a365 100644 --- a/llarp/threadpool.cpp +++ b/llarp/threadpool.cpp @@ -41,9 +41,9 @@ namespace llarp pthread_setname_np(name); #elif(__FreeBSD__) || (__OpenBSD__) || (__NetBSD__) pthread_set_name_np(pthread_self(), name); -#elif !defined(_MSC_VER) || !defined(_WIN32) +#elif(__linux__) || (__MINGW32__) pthread_setname_np(pthread_self(), name); -#else +#elif defined(_MSC_VER) SetThreadName(GetCurrentThreadId(), name); #endif } diff --git a/llarp/win32_inet.c b/llarp/win32_inet.c index 83f16f7f6..06f8dfe19 100644 --- a/llarp/win32_inet.c +++ b/llarp/win32_inet.c @@ -7,10 +7,12 @@ * * -despair86 30/07/18 */ + +// these need to be in a specific order #include -#include #include #include +#include #include "win32_intrnl.h" const char * diff --git a/llarp/win32_intrnl.c b/llarp/win32_intrnl.c index 7a9e65ddf..3ceaae52e 100644 --- a/llarp/win32_intrnl.c +++ b/llarp/win32_intrnl.c @@ -2,8 +2,9 @@ /* * All the user-mode scaffolding necessary to backport GetAdaptersAddresses(2)) * to the NT 5.x series. See further comments for any limitations. - * - * -despair86 30/07/18 + * NOTE: this is dead code, i haven't had time to debug it yet due to illness. + * For now, downlevel platforms use GetAdaptersInfo(2) which is inet4 only. + * -despair86 20/08/18 */ #include #include @@ -14,9 +15,10 @@ #undef WIN32_LEAN_AND_MEAN #endif -#include +// these need to be in a specific order #include #include +#include #include "win32_intrnl.h" const PWCHAR TcpFileName = L"\\Device\\Tcp"; @@ -501,7 +503,10 @@ getInterfaceIndexTable(void) */ #ifdef _MSC_VER #include + +typedef HRESULT(FAR PASCAL *p_SetThreadDescription)(void *, const wchar_t *); #define EXCEPTION_SET_THREAD_NAME ((DWORD)0x406D1388) + typedef struct _THREADNAME_INFO { DWORD dwType; /* must be 0x1000 */ @@ -515,20 +520,43 @@ SetThreadName(DWORD dwThreadID, LPCSTR szThreadName) { THREADNAME_INFO info; DWORD infosize; - - info.dwType = 0x1000; - info.szName = szThreadName; - info.dwThreadID = dwThreadID; - info.dwFlags = 0; - - infosize = sizeof(info) / sizeof(DWORD); - - __try + HANDLE hThread; + /* because loonix is SHIT and limits thread names to 16 bytes */ + wchar_t thr_name_w[16]; + p_SetThreadDescription _SetThreadDescription; + + /* current win10 flights now have a new named-thread API, let's try to use + * that first! */ + /* first, dlsym(2) the new call from system library */ + _SetThreadDescription = (p_SetThreadDescription)GetProcAddress( + GetModuleHandle("kernel32"), "SetThreadDescription"); + if(_SetThreadDescription) { - RaiseException(EXCEPTION_SET_THREAD_NAME, 0, infosize, (DWORD *)&info); + hThread = OpenThread(THREAD_SET_LIMITED_INFORMATION, FALSE, dwThreadID); + MultiByteToWideChar(CP_ACP, 0, szThreadName, -1, thr_name_w, 16); + if(hThread) + _SetThreadDescription(hThread, thr_name_w); + else + goto old; /* for whatever reason, we couldn't get a handle to the thread. + Just use the old method. */ } - __except(EXCEPTION_EXECUTE_HANDLER) + else { + old: + info.dwType = 0x1000; + info.szName = szThreadName; + info.dwThreadID = dwThreadID; + info.dwFlags = 0; + + infosize = sizeof(info) / sizeof(DWORD); + + __try + { + RaiseException(EXCEPTION_SET_THREAD_NAME, 0, infosize, (DWORD *)&info); + } + __except(EXCEPTION_EXECUTE_HANDLER) + { + } } } #endif \ No newline at end of file diff --git a/vendor/cppbackport-master/lib/filesystem.h b/vendor/cppbackport-master/lib/filesystem.h index 9d925f2a0..94cdddb5b 100644 --- a/vendor/cppbackport-master/lib/filesystem.h +++ b/vendor/cppbackport-master/lib/filesystem.h @@ -39,7 +39,7 @@ #include "version.h" #if defined(CPP17) && defined(USE_CXX17_FILESYSTEM) -#if defined(__MINGW32__) || defined(_MSC_VER) +#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__sun) // win32 needs experimental #include #else diff --git a/vendor/libtuntap-master/tuntap-unix-sunos.c b/vendor/libtuntap-master/tuntap-unix-sunos.c index 129040eee..613c06cd7 100644 --- a/vendor/libtuntap-master/tuntap-unix-sunos.c +++ b/vendor/libtuntap-master/tuntap-unix-sunos.c @@ -34,49 +34,58 @@ #include "tuntap.h" +/* TODO(despair): port all this shit */ + static int -tuntap_sys_create_dev(struct device *dev, int tun) { - return -1; +tuntap_sys_create_dev(struct device *dev, int tun) +{ + return -1; } int -tuntap_sys_start(struct device *dev, int mode, int tun) { - return -1; +tuntap_sys_start(struct device *dev, int mode, int tun) +{ + return -1; } void -tuntap_sys_destroy(struct device *dev) { - return -1; +tuntap_sys_destroy(struct device *dev) +{ + return /*-1*/; } int -tuntap_sys_set_hwaddr(struct device *dev, struct ether_addr *eth_addr) { - return -1; +tuntap_sys_set_hwaddr(struct device *dev, struct ether_addr *eth_addr) +{ + return -1; } int -tuntap_sys_set_ipv4(struct device *dev, t_tun_in_addr *s4, uint32_t imask) { - return -1; +tuntap_sys_set_ipv4(struct device *dev, t_tun_in_addr *s4, uint32_t imask) +{ + return -1; } int -tuntap_sys_set_ipv6(struct device *dev, t_tun_in6_addr *s6, uint32_t imask) { - return -1; +tuntap_sys_set_ipv6(struct device *dev, t_tun_in6_addr *s6, uint32_t imask) +{ + return -1; } int -tuntap_sys_set_ifname(struct device *dev, const char *ifname, size_t len) { - (void)dev; - (void)ifname; - (void)len; - return -1; +tuntap_sys_set_ifname(struct device *dev, const char *ifname, size_t len) +{ + (void)dev; + (void)ifname; + (void)len; + return -1; } int -tuntap_sys_set_descr(struct device *dev, const char *descr, size_t len) { - (void)dev; - (void)descr; - (void)len; - return -1; +tuntap_sys_set_descr(struct device *dev, const char *descr, size_t len) +{ + (void)dev; + (void)descr; + (void)len; + return -1; } - diff --git a/vendor/libtuntap-master/tuntap-unix.c b/vendor/libtuntap-master/tuntap-unix.c index e22364698..fa0e5feaf 100644 --- a/vendor/libtuntap-master/tuntap-unix.c +++ b/vendor/libtuntap-master/tuntap-unix.c @@ -25,6 +25,11 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#ifdef __sun +#define BSD_COMP +#define TUNSDEBUG _IOW('t', 90, int) +#endif + #include #include #include