diff --git a/crypto/libntrup/src/ref/r3_recip.c b/crypto/libntrup/src/ref/r3_recip.c index 12b185e38..0631f1fec 100644 --- a/crypto/libntrup/src/ref/r3_recip.c +++ b/crypto/libntrup/src/ref/r3_recip.c @@ -2,6 +2,9 @@ #include "mod3.h" #include "swap.h" #include "r3.h" +#ifdef _MSC_VER +#include +#endif /* caller must ensure that x-y does not overflow */ static int smaller_mask(int x,int y) diff --git a/crypto/libntrup/src/ref/rq_recip3.c b/crypto/libntrup/src/ref/rq_recip3.c index 54f048428..0eec06ae5 100644 --- a/crypto/libntrup/src/ref/rq_recip3.c +++ b/crypto/libntrup/src/ref/rq_recip3.c @@ -1,6 +1,9 @@ #include "params.h" #include "swap.h" #include "rq.h" +#ifdef _MSC_VER +#include +#endif /* caller must ensure that x-y does not overflow */ static int smaller_mask(int x,int y) diff --git a/include/llarp/handlers/tun.hpp b/include/llarp/handlers/tun.hpp index cf07ac0c5..33ed5cd72 100644 --- a/include/llarp/handlers/tun.hpp +++ b/include/llarp/handlers/tun.hpp @@ -48,7 +48,7 @@ namespace llarp void HandleDataMessage(service::ProtocolMessage* msg); -#ifndef _WIN32 +#ifndef _MINGW32_NO_THREADS /// overrides Endpount bool IsolationFailed() @@ -101,7 +101,7 @@ namespace llarp FlushSend(); private: -#ifndef _WIN32 +#ifndef _MINGW32_NO_THREADS /// handles setup, given value true on success and false on failure to set /// up interface std::promise< bool > m_TunSetupResult; diff --git a/include/llarp/ip.hpp b/include/llarp/ip.hpp index 6ca92e643..668ec3a39 100644 --- a/include/llarp/ip.hpp +++ b/include/llarp/ip.hpp @@ -36,8 +36,8 @@ typedef struct ip_hdr #define iphdr IPV4_HDR #define saddr ip_srcaddr #define daddr ip_destaddr -#define ihl ip_header_len #define check ip_checksum +#define ihl ip_header_len #endif #include #if !defined(__linux__) && !defined(_WIN32) diff --git a/include/llarp/threading.hpp b/include/llarp/threading.hpp index f2f0975fd..e31e5a746 100644 --- a/include/llarp/threading.hpp +++ b/include/llarp/threading.hpp @@ -2,6 +2,7 @@ #define LLARP_THREADING_HPP #include #if defined(__MINGW32__) && !defined(_GLIBCXX_HAS_GTHREADS) +#define _MINGW32_NO_THREADS #include #include #include diff --git a/include/tuntap.h b/include/tuntap.h index 979b93642..64bf9466d 100644 --- a/include/tuntap.h +++ b/include/tuntap.h @@ -32,14 +32,19 @@ #endif #if !defined Windows /* Unix :) */ +#if !defined Linux +#include +#endif #if defined Linux #include #else #include #endif #include +#if defined Linux #include #endif +#endif #include diff --git a/llarp/ev.hpp b/llarp/ev.hpp index 0eba64a3d..08ece5704 100644 --- a/llarp/ev.hpp +++ b/llarp/ev.hpp @@ -65,6 +65,7 @@ namespace llarp return true; #else // writefile + return false; #endif }); /// reset errno diff --git a/llarp/ev_kqueue.hpp b/llarp/ev_kqueue.hpp index 0ab5bc0d1..3b3ad7986 100644 --- a/llarp/ev_kqueue.hpp +++ b/llarp/ev_kqueue.hpp @@ -73,6 +73,67 @@ namespace llarp return sent; } }; + + struct tun : public ev_io + { + llarp_tun_io* t; + device* tunif; + tun(llarp_tun_io* tio) + : ev_io(-1) + , t(tio) + , tunif(tuntap_init()) + + { + + }; + + int + sendto(const sockaddr* to, const void* data, size_t sz) + { + return -1; + } + + void + flush_write() + { + if(t->before_write) + { + t->before_write(t); + ev_io::flush_write(); + } + } + + int + read(void* buf, size_t sz) + { + ssize_t ret = tuntap_read(tunif, buf, sz); + if(ret > 0 && t->recvpkt) + t->recvpkt(t, buf, ret); + return ret; + } + + bool + setup() + { + llarp::LogDebug("set up tunif"); + if(tuntap_start(tunif, TUNTAP_MODE_TUNNEL, 0) == -1) + return false; + llarp::LogDebug("set ifname to ", t->ifname); + if(tuntap_set_ifname(tunif, t->ifname) == -1) + return false; + if(tuntap_set_ip(tunif, t->ifaddr, t->netmask) == -1) + return false; + if(tuntap_up(tunif) == -1) + return false; + fd = tunif->tun_fd; + return fd != -1; + } + + ~tun() + { + } + }; + }; // namespace llarp struct llarp_kqueue_loop : public llarp_ev_loop @@ -91,7 +152,10 @@ struct llarp_kqueue_loop : public llarp_ev_loop llarp::ev_io* create_tun(llarp_tun_io* tun) { - // TODO: implement me + llarp::tun* t = new llarp::tun(tun); + if(t->setup()) + return t; + delete t; return nullptr; } diff --git a/llarp/ev_win32.hpp b/llarp/ev_win32.hpp index 1453f7e79..0c8a18b96 100644 --- a/llarp/ev_win32.hpp +++ b/llarp/ev_win32.hpp @@ -153,6 +153,8 @@ struct llarp_win32_loop : public llarp_ev_loop ++idx; } while(::GetQueuedCompletionStatus(iocpfd, &iolen, &ev_id, &qdata, ms)); + // tick_listeners inlined since win32 does not + // implement ev_tun for(auto& l : udp_listeners) { if(l->tick) diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index 6542aed38..79d214d91 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -1,3 +1,5 @@ +// harmless on other platforms +#define __USE_MINGW_ANSI_STDIO 1 #include #include "router.hpp" @@ -108,7 +110,7 @@ namespace llarp // do network isolation first if(!Endpoint::Start()) return false; -#ifdef _WIN32 +#ifdef _MINGW32_NO_THREADS return SetupNetworking(); #else if(!NetworkIsIsolated()) diff --git a/llarp/ip.cpp b/llarp/ip.cpp index 935e9cd68..7411515ce 100644 --- a/llarp/ip.cpp +++ b/llarp/ip.cpp @@ -1,6 +1,7 @@ #include +#include #include -#include "buffer.hpp" +#include "llarp/buffer.hpp" #include "mem.hpp" namespace llarp diff --git a/llarp/win32_inet.c b/llarp/win32_inet.c index b61af4a64..06f8dfe19 100644 --- a/llarp/win32_inet.c +++ b/llarp/win32_inet.c @@ -10,9 +10,9 @@ // 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 922b2e97a..2f500238d 100644 --- a/llarp/win32_intrnl.c +++ b/llarp/win32_intrnl.c @@ -16,9 +16,9 @@ #endif // these need to be in a specific order -#include #include #include +#include #include "win32_intrnl.h" const PWCHAR TcpFileName = L"\\Device\\Tcp"; @@ -528,11 +528,14 @@ SetThreadName(DWORD dwThreadID, LPCSTR szThreadName) /* 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 */ + hThread = NULL; _SetThreadDescription = (p_SetThreadDescription)GetProcAddress( GetModuleHandle("kernel32"), "SetThreadDescription"); if(_SetThreadDescription) { + /* grab another reference to the thread */ hThread = OpenThread(THREAD_SET_LIMITED_INFORMATION, FALSE, dwThreadID); + /* windows takes unicode, our input is utf-8 or plain ascii */ MultiByteToWideChar(CP_ACP, 0, szThreadName, -1, thr_name_w, 16); if(hThread) _SetThreadDescription(hThread, thr_name_w); @@ -558,5 +561,8 @@ SetThreadName(DWORD dwThreadID, LPCSTR szThreadName) { } } + /* clean up */ + if(hThread) + CloseHandle(hThread); } #endif \ No newline at end of file diff --git a/vendor/libtuntap-master/tuntap-unix-bsd.c b/vendor/libtuntap-master/tuntap-unix-bsd.c index b2d5425cc..35ca009cb 100644 --- a/vendor/libtuntap-master/tuntap-unix-bsd.c +++ b/vendor/libtuntap-master/tuntap-unix-bsd.c @@ -33,13 +33,19 @@ tuntap_sys_set_ipv6(struct device *dev, t_tun_in6_addr *s6, uint32_t bits) { return -1; } +#ifndef SIOCSIFNAME int -tuntap_sys_set_ifname(struct device *dev, const char *ifname, size_t len) { - (void)dev; - (void)ifname; - (void)len; - tuntap_log(TUNTAP_LOG_NOTICE, - "Your system does not support tuntap_set_ifname()"); - return -1; +tuntap_sys_set_ifname(struct device *dev, const char *ifname, size_t len) +{ + (void)dev; + (void)ifname; + (void)len; + tuntap_log(TUNTAP_LOG_NOTICE, + "Your system does not support tuntap_set_ifname()"); + /* just leave it as tunX, there doesn't seem to be any + * practical manner of setting this param in NetBSD and its forks :-( + */ + return 0; } +#endif \ No newline at end of file diff --git a/vendor/libtuntap-master/tuntap-unix-freebsd.c b/vendor/libtuntap-master/tuntap-unix-freebsd.c index 856728de1..42fe9e659 100644 --- a/vendor/libtuntap-master/tuntap-unix-freebsd.c +++ b/vendor/libtuntap-master/tuntap-unix-freebsd.c @@ -283,3 +283,17 @@ tuntap_sys_set_descr(struct device *dev, const char *descr, size_t len) { #endif } +int +tuntap_sys_set_ifname(struct device *dev, const char *ifname, size_t len) { + struct ifreq ifr; + + (void)strncpy(ifr.ifr_name, dev->if_name, IF_NAMESIZE); + (void)strncpy(ifr.ifr_newname, ifname, len); + + if (ioctl(dev->ctrl_sock, SIOCSIFNAME, &ifr) == -1) { + perror(NULL); + tuntap_log(TUNTAP_LOG_ERR, "Can't set interface name"); + return -1; + } + return 0; +} \ No newline at end of file diff --git a/vendor/libtuntap-master/tuntap-unix-sunos.c b/vendor/libtuntap-master/tuntap-unix-sunos.c index 613c06cd7..56664806b 100644 --- a/vendor/libtuntap-master/tuntap-unix-sunos.c +++ b/vendor/libtuntap-master/tuntap-unix-sunos.c @@ -73,12 +73,18 @@ tuntap_sys_set_ipv6(struct device *dev, t_tun_in6_addr *s6, uint32_t imask) } 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) { + struct ifreq ifr; + + (void)strncpy(ifr.ifr_name, dev->if_name, IF_NAMESIZE); + (void)strncpy(ifr.ifr_newname, ifname, len); + + if (ioctl(dev->ctrl_sock, SIOCSIFNAME, &ifr) == -1) { + perror(NULL); + tuntap_log(TUNTAP_LOG_ERR, "Can't set interface name"); + return -1; + } + return 0; } int