diff --git a/Makefile b/Makefile index eec7e33..75f074d 100644 --- a/Makefile +++ b/Makefile @@ -36,6 +36,18 @@ endif OSTYPE=$(shell uname -s | tr '[A-Z]' '[a-z]') BUILD=build/$(OSTYPE) +# Windows +ifeq ($(OSTYPE),mingw32_nt-6.2) +ARTOOL=ar +ARFLAGS=rcs +CC=gcc +CXX=g++ +CXXFLAGS+=-Wno-unknown-pragmas -Wno-pointer-arith -Wno-deprecated-declarations -Wno-conversion-null +WINDEFS=-lws2_32 -lshlwapi -liphlpapi -static -static-libgcc -static-libstdc++ +CONTRIBDIR=ext/lwip-contrib +LWIPARCH=$(CONTRIBDIR)/ports/win32 +LWIPARCHINCLUDE=$(LWIPARCH)/include +endif # Darwin ifeq ($(OSTYPE),darwin) ARTOOL=libtool @@ -147,7 +159,7 @@ ifeq ($(SDK_JNI), 1) LIBZT_DEFS+=-DSDK_JNI endif -CXXFLAGS=$(CFLAGS) -Wno-format -fno-rtti -std=c++11 +CXXFLAGS+=$(CFLAGS) -Wno-format -fno-rtti -std=c++11 ZT_DEFS+=-DZT_SDK -DZT_SOFTWARE_UPDATE_DEFAULT="\"disable\"" LIBZT_FILES:=src/VirtualTap.cpp src/libzt.cpp src/Utilities.cpp STATIC_LIB=$(BUILD)/libzt.a @@ -211,9 +223,8 @@ endif STACK_DRIVER_DEFS+=-DLWIP_DONT_PROVIDE_BYTEORDER_FUNCTIONS STACK_DRIVER_DEFS+=-DSTACK_LWIP STACK_DRIVER_FILES:=src/lwIP.cpp -LWIPARCH=$(CONTRIBDIR)/ports/unix LWIPDIR=ext/lwip/src -STACK_INCLUDES+=-Iext/lwip/src/include/lwip \ +STACK_INCLUDES+=$(LWIPARCHINCLUDE) -Iext/lwip/src/include/lwip \ -I$(LWIPDIR)/include \ -I$(LWIPARCH)/include \ -I$(LWIPDIR)/include/ipv4 \ @@ -349,7 +360,7 @@ nativetest: @date +"Build script finished on %F %T" ztproxy: $(CXX) $(CXXFLAGS) $(SANFLAGS) $(LIBZT_INCLUDES) $(LIBZT_DEFS) $(ZT_INCLUDES) \ - examples/ztproxy/ztproxy.cpp -o $(BUILD)/ztproxy $< -L$(BUILD) -lzt + examples/ztproxy/ztproxy.cpp -o $(BUILD)/ztproxy $< -L$(BUILD) -lzt $(WINDEFS) @./check.sh $(BUILD)/ztproxy @date +"Build script finished on %F %T" intercept: diff --git a/examples/ztproxy/ztproxy.cpp b/examples/ztproxy/ztproxy.cpp index 2556f04..d5a913c 100644 --- a/examples/ztproxy/ztproxy.cpp +++ b/examples/ztproxy/ztproxy.cpp @@ -26,12 +26,17 @@ #include #include -#include + +#if defined(__linux__) || defined(__APPLE__) + #include +#endif + +//#include "Winsock2.h" + #include #include #include #include -#include #include #include @@ -39,6 +44,7 @@ #include "RingBuffer.hpp" #include "ztproxy.hpp" +#include "Utilities.h" #include "libzt.h" namespace ZeroTier { @@ -69,7 +75,11 @@ namespace ZeroTier { in4.sin_addr.s_addr = Utils::hton((uint32_t)(0x7f000001)); // listen for TCP @127.0.0.1 in4.sin_port = Utils::hton((uint16_t)proxy_listen_port); _tcpListenSocket = _phy.tcpListen((const struct sockaddr *)&in4,this); + if (!_tcpListenSocket) { + DEBUG_ERROR("Error binding on port %d for IPv4 HTTP listen socket", proxy_listen_port); + } // IPv6 + /* struct sockaddr_in6 in6; memset((void *)&in6,0,sizeof(in6)); in6.sin6_family = AF_INET6; @@ -77,13 +87,12 @@ namespace ZeroTier { in6.sin6_addr.s6_addr[15] = 1; // IPv6 localhost == ::1 in6.sin6_port = Utils::hton((uint16_t)proxy_listen_port); _tcpListenSocket6 = _phy.tcpListen((const struct sockaddr *)&in6,this); - - if (!_tcpListenSocket) { - DEBUG_ERROR("Error binding on port %d for IPv4 HTTP listen socket", proxy_listen_port); - } + */ + /* if (!_tcpListenSocket6) { DEBUG_ERROR("Error binding on port %d for IPv6 HTTP listen socket", proxy_listen_port); } + */ _thread = Thread::start(this); } @@ -117,7 +126,7 @@ namespace ZeroTier { // Moves data between client application socket and libzt VirtualSocket while(_run) { - _phy.poll(5); + _phy.poll(1); conn_m.lock(); // build fd_sets to select upon @@ -212,7 +221,6 @@ namespace ZeroTier { exit(0); } if (conn->zfd < 0) { // no connection yet - DEBUG_INFO("no connection yet, will establish..."); if (host == "") { DEBUG_ERROR("invalid hostname or address (empty)"); return; @@ -256,6 +264,7 @@ namespace ZeroTier { } if (ipv == 6) { //DEBUG_INFO("attempting to proxy [0.0.0.0:%d -> %s:%d]", _proxy_listen_port, host.c_str(), dest_port); + /* struct sockaddr_in6 in6; memset(&in6,0,sizeof(in6)); in6.sin6_family = AF_INET; @@ -265,6 +274,7 @@ namespace ZeroTier { in6.sin6_port = Utils::hton(dest_port); zfd = zts_socket(AF_INET, SOCK_STREAM, 0); err = zts_connect(zfd, (const struct sockaddr *)&in6, sizeof(in6)); + */ } if (zfd < 0 || err < 0) { // now release TX buffer contents we previously saved, since we can't connect @@ -287,9 +297,6 @@ namespace ZeroTier { zmap[zfd] = conn; conn_m.unlock(); } - else { - DEBUG_INFO("connection already established, reusing..."); - } // Write data coming from client TCP connection to its TX buffer, later emptied into libzt by threadMain I/O loop conn->tx_m.lock(); if ((wr = conn->TXbuf->write((const unsigned char *)data, len)) < 0) { @@ -371,7 +378,7 @@ int main(int argc, char **argv) std::string nwid = argv[3]; std::string internal_addr = argv[4]; int internal_port = atoi(argv[5]); - std::string dns_nameserver= argv[6]; + std::string dns_nameserver= "";//argv[6]; ZeroTier::ZTProxy *proxy = new ZeroTier::ZTProxy(proxy_listen_port, nwid, path, internal_addr, internal_port, dns_nameserver); diff --git a/examples/ztproxy/ztproxy.hpp b/examples/ztproxy/ztproxy.hpp index 304ec53..91f49ab 100644 --- a/examples/ztproxy/ztproxy.hpp +++ b/examples/ztproxy/ztproxy.hpp @@ -34,10 +34,13 @@ #include "Phy.hpp" #include "OSUtils.hpp" +#if defined(__linux__) || defined(__APPLE__) + #include +#endif + #include #include #include -#include #define BUF_SZ 1024*1024 diff --git a/ext/lwip/src/include/arch/cc.h b/ext/lwip-contrib/ports/unix/include/cc.h similarity index 100% rename from ext/lwip/src/include/arch/cc.h rename to ext/lwip-contrib/ports/unix/include/cc.h diff --git a/ext/lwip/src/include/arch/perf.h b/ext/lwip-contrib/ports/unix/include/perf.h similarity index 100% rename from ext/lwip/src/include/arch/perf.h rename to ext/lwip-contrib/ports/unix/include/perf.h diff --git a/ext/lwip/src/include/arch/sys_arch.h b/ext/lwip-contrib/ports/unix/include/sys_arch.h similarity index 100% rename from ext/lwip/src/include/arch/sys_arch.h rename to ext/lwip-contrib/ports/unix/include/sys_arch.h diff --git a/ext/lwip-contrib/ports/win32/include/arch/cc.h b/ext/lwip-contrib/ports/win32/include/arch/cc.h index a1a2a70..56e6ebc 100644 --- a/ext/lwip-contrib/ports/win32/include/arch/cc.h +++ b/ext/lwip-contrib/ports/win32/include/arch/cc.h @@ -39,7 +39,7 @@ #pragma warning (disable: 4820) /* 'x' bytes padding added after data member 'y' */ #endif -#define LWIP_PROVIDE_ERRNO +#define LWIP_PROVIDE_ERRNO 0 /* Define platform endianness (might already be defined) */ #ifndef BYTE_ORDER diff --git a/ext/lwip-contrib/ports/win32/include/lwipopts.h b/ext/lwip-contrib/ports/win32/include/lwipopts.h.bak similarity index 100% rename from ext/lwip-contrib/ports/win32/include/lwipopts.h rename to ext/lwip-contrib/ports/win32/include/lwipopts.h.bak diff --git a/ext/lwip-contrib/ports/win32/pcapif.c b/ext/lwip-contrib/ports/win32/pcapif.c.bak similarity index 100% rename from ext/lwip-contrib/ports/win32/pcapif.c rename to ext/lwip-contrib/ports/win32/pcapif.c.bak diff --git a/ext/lwip-contrib/ports/win32/pcapif.h b/ext/lwip-contrib/ports/win32/pcapif.h.bak similarity index 100% rename from ext/lwip-contrib/ports/win32/pcapif.h rename to ext/lwip-contrib/ports/win32/pcapif.h.bak diff --git a/ext/lwip-contrib/ports/win32/pcapif_helper.c b/ext/lwip-contrib/ports/win32/pcapif_helper.c.bak similarity index 100% rename from ext/lwip-contrib/ports/win32/pcapif_helper.c rename to ext/lwip-contrib/ports/win32/pcapif_helper.c.bak diff --git a/ext/lwip-contrib/ports/win32/sio.c b/ext/lwip-contrib/ports/win32/sio.c.bak similarity index 100% rename from ext/lwip-contrib/ports/win32/sio.c rename to ext/lwip-contrib/ports/win32/sio.c.bak diff --git a/ext/lwip-contrib/ports/win32/test.c b/ext/lwip-contrib/ports/win32/test.c.bak similarity index 100% rename from ext/lwip-contrib/ports/win32/test.c rename to ext/lwip-contrib/ports/win32/test.c.bak diff --git a/ext/lwip/src/api/err.c b/ext/lwip/src/api/err.c index 6e9ab76..5c795a4 100755 --- a/ext/lwip/src/api/err.c +++ b/ext/lwip/src/api/err.c @@ -40,7 +40,7 @@ #include "lwip/def.h" #include "lwip/sys.h" -#include "lwip/errno.h" +#include "errno.h" #if !NO_SYS /** Table to quickly map an lwIP error (err_t) to a socket error diff --git a/ext/lwip/src/api/sockets.c b/ext/lwip/src/api/sockets.c index d72724f..387e035 100755 --- a/ext/lwip/src/api/sockets.c +++ b/ext/lwip/src/api/sockets.c @@ -658,10 +658,19 @@ lwip_connect(int s, const struct sockaddr *name, socklen_t namelen) } LWIP_UNUSED_ARG(namelen); - if (name->sa_family == AF_UNSPEC) { + + if ( +#ifdef __MINGW32__ + false +#else + name->sa_family == AF_UNSPEC +#endif + ) + { LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d, AF_UNSPEC)\n", s)); err = netconn_disconnect(sock->conn); - } else { + } + else { ip_addr_t remote_addr; u16_t remote_port; diff --git a/ext/lwip/src/include/lwip/errno.h b/ext/lwip/src/include/lwip/errno.h.bak old mode 100755 new mode 100644 similarity index 97% rename from ext/lwip/src/include/lwip/errno.h rename to ext/lwip/src/include/lwip/errno.h.bak index 47a4ff2..1c770df --- a/ext/lwip/src/include/lwip/errno.h +++ b/ext/lwip/src/include/lwip/errno.h.bak @@ -1,193 +1,193 @@ -/** - * @file - * Posix Errno defines - */ - -/* - * Copyright (c) 2001-2004 Swedish Institute of Computer Science. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT - * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT - * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING - * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY - * OF SUCH DAMAGE. - * - * This file is part of the lwIP TCP/IP stack. - * - * Author: Adam Dunkels - * - */ -#ifndef LWIP_HDR_ERRNO_H -#define LWIP_HDR_ERRNO_H - -#include "lwip/opt.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#ifdef LWIP_PROVIDE_ERRNO - -#define EPERM 1 /* Operation not permitted */ -#define ENOENT 2 /* No such file or directory */ -#define ESRCH 3 /* No such process */ -#define EINTR 4 /* Interrupted system call */ -#define EIO 5 /* I/O error */ -#define ENXIO 6 /* No such device or address */ -#define E2BIG 7 /* Arg list too long */ -#define ENOEXEC 8 /* Exec format error */ -#define EBADF 9 /* Bad file number */ -#define ECHILD 10 /* No child processes */ -#define EAGAIN 11 /* Try again */ -#define ENOMEM 12 /* Out of memory */ -#define EACCES 13 /* Permission denied */ -#define EFAULT 14 /* Bad address */ -#define ENOTBLK 15 /* Block device required */ -#define EBUSY 16 /* Device or resource busy */ -#define EEXIST 17 /* File exists */ -#define EXDEV 18 /* Cross-device link */ -#define ENODEV 19 /* No such device */ -#define ENOTDIR 20 /* Not a directory */ -#define EISDIR 21 /* Is a directory */ -#define EINVAL 22 /* Invalid argument */ -#define ENFILE 23 /* File table overflow */ -#define EMFILE 24 /* Too many open files */ -#define ENOTTY 25 /* Not a typewriter */ -#define ETXTBSY 26 /* Text file busy */ -#define EFBIG 27 /* File too large */ -#define ENOSPC 28 /* No space left on device */ -#define ESPIPE 29 /* Illegal seek */ -#define EROFS 30 /* Read-only file system */ -#define EMLINK 31 /* Too many links */ -#define EPIPE 32 /* Broken pipe */ -#define EDOM 33 /* Math argument out of domain of func */ -#define ERANGE 34 /* Math result not representable */ -#define EDEADLK 35 /* Resource deadlock would occur */ -#define ENAMETOOLONG 36 /* File name too long */ -#define ENOLCK 37 /* No record locks available */ -#define ENOSYS 38 /* Function not implemented */ -#define ENOTEMPTY 39 /* Directory not empty */ -#define ELOOP 40 /* Too many symbolic links encountered */ -#define EWOULDBLOCK EAGAIN /* Operation would block */ -#define ENOMSG 42 /* No message of desired type */ -#define EIDRM 43 /* Identifier removed */ -#define ECHRNG 44 /* Channel number out of range */ -#define EL2NSYNC 45 /* Level 2 not synchronized */ -#define EL3HLT 46 /* Level 3 halted */ -#define EL3RST 47 /* Level 3 reset */ -#define ELNRNG 48 /* Link number out of range */ -#define EUNATCH 49 /* Protocol driver not attached */ -#define ENOCSI 50 /* No CSI structure available */ -#define EL2HLT 51 /* Level 2 halted */ -#define EBADE 52 /* Invalid exchange */ -#define EBADR 53 /* Invalid request descriptor */ -#define EXFULL 54 /* Exchange full */ -#define ENOANO 55 /* No anode */ -#define EBADRQC 56 /* Invalid request code */ -#define EBADSLT 57 /* Invalid slot */ - -#define EDEADLOCK EDEADLK - -#define EBFONT 59 /* Bad font file format */ -#define ENOSTR 60 /* Device not a stream */ -#define ENODATA 61 /* No data available */ -#define ETIME 62 /* Timer expired */ -#define ENOSR 63 /* Out of streams resources */ -#define ENONET 64 /* Machine is not on the network */ -#define ENOPKG 65 /* Package not installed */ -#define EREMOTE 66 /* Object is remote */ -#define ENOLINK 67 /* Link has been severed */ -#define EADV 68 /* Advertise error */ -#define ESRMNT 69 /* Srmount error */ -#define ECOMM 70 /* Communication error on send */ -#define EPROTO 71 /* Protocol error */ -#define EMULTIHOP 72 /* Multihop attempted */ -#define EDOTDOT 73 /* RFS specific error */ -#define EBADMSG 74 /* Not a data message */ -#define EOVERFLOW 75 /* Value too large for defined data type */ -#define ENOTUNIQ 76 /* Name not unique on network */ -#define EBADFD 77 /* File descriptor in bad state */ -#define EREMCHG 78 /* Remote address changed */ -#define ELIBACC 79 /* Can not access a needed shared library */ -#define ELIBBAD 80 /* Accessing a corrupted shared library */ -#define ELIBSCN 81 /* .lib section in a.out corrupted */ -#define ELIBMAX 82 /* Attempting to link in too many shared libraries */ -#define ELIBEXEC 83 /* Cannot exec a shared library directly */ -#define EILSEQ 84 /* Illegal byte sequence */ -#define ERESTART 85 /* Interrupted system call should be restarted */ -#define ESTRPIPE 86 /* Streams pipe error */ -#define EUSERS 87 /* Too many users */ -#define ENOTSOCK 88 /* Socket operation on non-socket */ -#define EDESTADDRREQ 89 /* Destination address required */ -#define EMSGSIZE 90 /* Message too long */ -#define EPROTOTYPE 91 /* Protocol wrong type for socket */ -#define ENOPROTOOPT 92 /* Protocol not available */ -#define EPROTONOSUPPORT 93 /* Protocol not supported */ -#define ESOCKTNOSUPPORT 94 /* Socket type not supported */ -#define EOPNOTSUPP 95 /* Operation not supported on transport endpoint */ -#define EPFNOSUPPORT 96 /* Protocol family not supported */ -#define EAFNOSUPPORT 97 /* Address family not supported by protocol */ -#define EADDRINUSE 98 /* Address already in use */ -#define EADDRNOTAVAIL 99 /* Cannot assign requested address */ -#define ENETDOWN 100 /* Network is down */ -#define ENETUNREACH 101 /* Network is unreachable */ -#define ENETRESET 102 /* Network dropped connection because of reset */ -#define ECONNABORTED 103 /* Software caused connection abort */ -#define ECONNRESET 104 /* Connection reset by peer */ -#define ENOBUFS 105 /* No buffer space available */ -#define EISCONN 106 /* Transport endpoint is already connected */ -#define ENOTCONN 107 /* Transport endpoint is not connected */ -#define ESHUTDOWN 108 /* Cannot send after transport endpoint shutdown */ -#define ETOOMANYREFS 109 /* Too many references: cannot splice */ -#define ETIMEDOUT 110 /* Connection timed out */ -#define ECONNREFUSED 111 /* Connection refused */ -#define EHOSTDOWN 112 /* Host is down */ -#define EHOSTUNREACH 113 /* No route to host */ -#define EALREADY 114 /* Operation already in progress */ -#define EINPROGRESS 115 /* Operation now in progress */ -#define ESTALE 116 /* Stale NFS file handle */ -#define EUCLEAN 117 /* Structure needs cleaning */ -#define ENOTNAM 118 /* Not a XENIX named type file */ -#define ENAVAIL 119 /* No XENIX semaphores available */ -#define EISNAM 120 /* Is a named type file */ -#define EREMOTEIO 121 /* Remote I/O error */ -#define EDQUOT 122 /* Quota exceeded */ - -#define ENOMEDIUM 123 /* No medium found */ -#define EMEDIUMTYPE 124 /* Wrong medium type */ - -#ifndef errno -extern int errno; -#endif - -#else /* LWIP_PROVIDE_ERRNO */ - -/* Define LWIP_ERRNO_INCLUDE to to include the error defines here */ -#ifdef LWIP_ERRNO_INCLUDE -//#include -#endif /* LWIP_ERRNO_INCLUDE */ - -#endif /* LWIP_PROVIDE_ERRNO */ - -#ifdef __cplusplus -} -#endif - -#endif /* LWIP_HDR_ERRNO_H */ +/** + * @file + * Posix Errno defines + */ + +/* + * Copyright (c) 2001-2004 Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT + * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * This file is part of the lwIP TCP/IP stack. + * + * Author: Adam Dunkels + * + */ +#ifndef LWIP_HDR_ERRNO_H +#define LWIP_HDR_ERRNO_H + +#include "lwip/opt.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef LWIP_PROVIDE_ERRNO + +#define EPERM 1 /* Operation not permitted */ +#define ENOENT 2 /* No such file or directory */ +#define ESRCH 3 /* No such process */ +#define EINTR 4 /* Interrupted system call */ +#define EIO 5 /* I/O error */ +#define ENXIO 6 /* No such device or address */ +#define E2BIG 7 /* Arg list too long */ +#define ENOEXEC 8 /* Exec format error */ +#define EBADF 9 /* Bad file number */ +#define ECHILD 10 /* No child processes */ +#define EAGAIN 11 /* Try again */ +#define ENOMEM 12 /* Out of memory */ +#define EACCES 13 /* Permission denied */ +#define EFAULT 14 /* Bad address */ +#define ENOTBLK 15 /* Block device required */ +#define EBUSY 16 /* Device or resource busy */ +#define EEXIST 17 /* File exists */ +#define EXDEV 18 /* Cross-device link */ +#define ENODEV 19 /* No such device */ +#define ENOTDIR 20 /* Not a directory */ +#define EISDIR 21 /* Is a directory */ +#define EINVAL 22 /* Invalid argument */ +#define ENFILE 23 /* File table overflow */ +#define EMFILE 24 /* Too many open files */ +#define ENOTTY 25 /* Not a typewriter */ +#define ETXTBSY 26 /* Text file busy */ +#define EFBIG 27 /* File too large */ +#define ENOSPC 28 /* No space left on device */ +#define ESPIPE 29 /* Illegal seek */ +#define EROFS 30 /* Read-only file system */ +#define EMLINK 31 /* Too many links */ +#define EPIPE 32 /* Broken pipe */ +#define EDOM 33 /* Math argument out of domain of func */ +#define ERANGE 34 /* Math result not representable */ +#define EDEADLK 35 /* Resource deadlock would occur */ +#define ENAMETOOLONG 36 /* File name too long */ +#define ENOLCK 37 /* No record locks available */ +#define ENOSYS 38 /* Function not implemented */ +#define ENOTEMPTY 39 /* Directory not empty */ +#define ELOOP 40 /* Too many symbolic links encountered */ +#define EWOULDBLOCK EAGAIN /* Operation would block */ +#define ENOMSG 42 /* No message of desired type */ +#define EIDRM 43 /* Identifier removed */ +#define ECHRNG 44 /* Channel number out of range */ +#define EL2NSYNC 45 /* Level 2 not synchronized */ +#define EL3HLT 46 /* Level 3 halted */ +#define EL3RST 47 /* Level 3 reset */ +#define ELNRNG 48 /* Link number out of range */ +#define EUNATCH 49 /* Protocol driver not attached */ +#define ENOCSI 50 /* No CSI structure available */ +#define EL2HLT 51 /* Level 2 halted */ +#define EBADE 52 /* Invalid exchange */ +#define EBADR 53 /* Invalid request descriptor */ +#define EXFULL 54 /* Exchange full */ +#define ENOANO 55 /* No anode */ +#define EBADRQC 56 /* Invalid request code */ +#define EBADSLT 57 /* Invalid slot */ + +#define EDEADLOCK EDEADLK + +#define EBFONT 59 /* Bad font file format */ +#define ENOSTR 60 /* Device not a stream */ +#define ENODATA 61 /* No data available */ +#define ETIME 62 /* Timer expired */ +#define ENOSR 63 /* Out of streams resources */ +#define ENONET 64 /* Machine is not on the network */ +#define ENOPKG 65 /* Package not installed */ +#define EREMOTE 66 /* Object is remote */ +#define ENOLINK 67 /* Link has been severed */ +#define EADV 68 /* Advertise error */ +#define ESRMNT 69 /* Srmount error */ +#define ECOMM 70 /* Communication error on send */ +#define EPROTO 71 /* Protocol error */ +#define EMULTIHOP 72 /* Multihop attempted */ +#define EDOTDOT 73 /* RFS specific error */ +#define EBADMSG 74 /* Not a data message */ +#define EOVERFLOW 75 /* Value too large for defined data type */ +#define ENOTUNIQ 76 /* Name not unique on network */ +#define EBADFD 77 /* File descriptor in bad state */ +#define EREMCHG 78 /* Remote address changed */ +#define ELIBACC 79 /* Can not access a needed shared library */ +#define ELIBBAD 80 /* Accessing a corrupted shared library */ +#define ELIBSCN 81 /* .lib section in a.out corrupted */ +#define ELIBMAX 82 /* Attempting to link in too many shared libraries */ +#define ELIBEXEC 83 /* Cannot exec a shared library directly */ +#define EILSEQ 84 /* Illegal byte sequence */ +#define ERESTART 85 /* Interrupted system call should be restarted */ +#define ESTRPIPE 86 /* Streams pipe error */ +#define EUSERS 87 /* Too many users */ +#define ENOTSOCK 88 /* Socket operation on non-socket */ +#define EDESTADDRREQ 89 /* Destination address required */ +#define EMSGSIZE 90 /* Message too long */ +#define EPROTOTYPE 91 /* Protocol wrong type for socket */ +#define ENOPROTOOPT 92 /* Protocol not available */ +#define EPROTONOSUPPORT 93 /* Protocol not supported */ +#define ESOCKTNOSUPPORT 94 /* Socket type not supported */ +#define EOPNOTSUPP 95 /* Operation not supported on transport endpoint */ +#define EPFNOSUPPORT 96 /* Protocol family not supported */ +#define EAFNOSUPPORT 97 /* Address family not supported by protocol */ +#define EADDRINUSE 98 /* Address already in use */ +#define EADDRNOTAVAIL 99 /* Cannot assign requested address */ +#define ENETDOWN 100 /* Network is down */ +#define ENETUNREACH 101 /* Network is unreachable */ +#define ENETRESET 102 /* Network dropped connection because of reset */ +#define ECONNABORTED 103 /* Software caused connection abort */ +#define ECONNRESET 104 /* Connection reset by peer */ +#define ENOBUFS 105 /* No buffer space available */ +#define EISCONN 106 /* Transport endpoint is already connected */ +#define ENOTCONN 107 /* Transport endpoint is not connected */ +#define ESHUTDOWN 108 /* Cannot send after transport endpoint shutdown */ +#define ETOOMANYREFS 109 /* Too many references: cannot splice */ +#define ETIMEDOUT 110 /* Connection timed out */ +#define ECONNREFUSED 111 /* Connection refused */ +#define EHOSTDOWN 112 /* Host is down */ +#define EHOSTUNREACH 113 /* No route to host */ +#define EALREADY 114 /* Operation already in progress */ +#define EINPROGRESS 115 /* Operation now in progress */ +#define ESTALE 116 /* Stale NFS file handle */ +#define EUCLEAN 117 /* Structure needs cleaning */ +#define ENOTNAM 118 /* Not a XENIX named type file */ +#define ENAVAIL 119 /* No XENIX semaphores available */ +#define EISNAM 120 /* Is a named type file */ +#define EREMOTEIO 121 /* Remote I/O error */ +#define EDQUOT 122 /* Quota exceeded */ + +#define ENOMEDIUM 123 /* No medium found */ +#define EMEDIUMTYPE 124 /* Wrong medium type */ + +#ifndef errno +extern int errno; +#endif + +#else /* LWIP_PROVIDE_ERRNO */ + +/* Define LWIP_ERRNO_INCLUDE to to include the error defines here */ +#ifdef LWIP_ERRNO_INCLUDE +//#include +#endif /* LWIP_ERRNO_INCLUDE */ + +#endif /* LWIP_PROVIDE_ERRNO */ + +#ifdef __cplusplus +} +#endif + +#endif /* LWIP_HDR_ERRNO_H */ diff --git a/include/Debug.hpp b/include/Debug.hpp index b306d1e..d807cbb 100644 --- a/include/Debug.hpp +++ b/include/Debug.hpp @@ -34,11 +34,14 @@ #define LIBZT_DEBUG_HPP #include -#include #include #include #include +#if defined(__linux__) +#include +#endif + #include "Platform.h" #define ZT_MSG_ERROR true // Errors @@ -53,7 +56,7 @@ #if defined(__APPLE__) #include "TargetConditionals.h" #endif -#if defined(ZT_COLOR) && !defined(__ANDROID__) && !defined(TARGET_OS_IPHONE) && !defined(TARGET_IPHONE_SIMULATOR) && !defined(__APP_FRAMEWORK__) +#if defined(ZT_COLOR) && !defined(__MINGW32__) && !defined(__ANDROID__) && !defined(TARGET_OS_IPHONE) && !defined(TARGET_IPHONE_SIMULATOR) && !defined(__APP_FRAMEWORK__) #define ZT_RED "\x1B[31m" #define ZT_GRN "\x1B[32m" #define ZT_YEL "\x1B[33m" @@ -88,6 +91,9 @@ extern unsigned int gettid(); // defined in libzt.cpp #elif __APPLE__ #define ZT_THREAD_ID (long)0//(long)gettid() #endif +#ifdef __MINGW32__ + #define ZT_THREAD_ID (long)0 +#endif #if defined(__JNI_LIB__) #include diff --git a/include/Defs.h b/include/Defs.h index 02a7936..8c2776c 100644 --- a/include/Defs.h +++ b/include/Defs.h @@ -48,7 +48,9 @@ */ #define ZTO_ID_LEN 16 +#if !defined(__MINGW32__) typedef uint32_t socklen_t; +#endif /****************************************************************************/ /* For SOCK_RAW support, it will initially be modeled after linux's API, so */ diff --git a/include/Utilities.h b/include/Utilities.h index 625c6db..ae1b6a1 100644 --- a/include/Utilities.h +++ b/include/Utilities.h @@ -35,6 +35,17 @@ #include "InetAddress.hpp" +#if defined(__MINGW32__) + +#define NS_INADDRSZ 4 +#define NS_IN6ADDRSZ 16 +#define NS_INT16SZ 2 + +int inet_pton4(const char *src, void *dst); +int inet_pton6(const char *src, void *dst); +int inet_pton(int af, const char *src, void *dst); +#endif + /** * @brief Returns masked address for subnet comparisons * @@ -62,7 +73,7 @@ char *beautify_eth_proto_nums(int proto); * @param inet * @return */ -void sockaddr2inet(int socket_family, const struct sockaddr *addr, ZeroTier::InetAddress *inet); +//void sockaddr2inet(int socket_family, const struct sockaddr *addr, ZeroTier::InetAddress *inet); /** * @brief Convert a raw MAC address byte array into a human-readable string diff --git a/include/libzt.h b/include/libzt.h index 714a603..5fa678b 100644 --- a/include/libzt.h +++ b/include/libzt.h @@ -33,11 +33,14 @@ #ifndef LIBZT_H #define LIBZT_H -#include #include #include #include +#if defined(__linux__) + #include +#endif + #include "Debug.hpp" #include "Defs.h" @@ -440,7 +443,9 @@ int zts_close(int fd); * @param timeout * @return */ +#if defined(__linux__) int zts_poll(struct pollfd *fds, nfds_t nfds, int timeout); +#endif /** * @brief Monitor multiple file descriptors, waiting until one or more of the file descriptors become "ready" diff --git a/include/lwIP.hpp b/include/lwIP.hpp index 236d1df..1c05742 100644 --- a/include/lwIP.hpp +++ b/include/lwIP.hpp @@ -60,12 +60,12 @@ void lwip_dns_init(); * @usage lwip_driver_init() * @return */ -void lwip_start_dhcp(struct netif *interface); +void lwip_start_dhcp(void *netif); -void general_lwip_init_interface(void *tapref, struct netif *interface, const char *name, const ZeroTier::MAC &mac, +void general_lwip_init_interface(void *tapref, void *netif, const char *name, const ZeroTier::MAC &mac, const ZeroTier::InetAddress &addr, const ZeroTier::InetAddress &nm, const ZeroTier::InetAddress &gw); -void general_turn_on_interface(struct netif *interface); +void general_turn_on_interface(void *netif); /** * @brief Set up an interface in the network stack for the VirtualTap. diff --git a/make-liblwip.mk b/make-liblwip.mk index 04638af..3c738af 100644 --- a/make-liblwip.mk +++ b/make-liblwip.mk @@ -32,21 +32,38 @@ # CONTRIBDIR=ext/lwip-contrib -LWIPARCH=$(CONTRIBDIR)/ports/unix - -#Set this to where you have the lwip core module checked out from CVS -#default assumes it's a dir named lwip at the same level as the contrib module LWIPDIR=ext/lwip/src - CCDEP=clang++ + # Automagically pick clang or gcc, with preference for clang # This is only done if we have not overridden these with an environment or CLI variable -ifeq ($(origin CCX),default) - CCX=$(shell if [ -e /usr/bin/clang++ ]; then echo clang++; else echo g++; fi) +ifeq ($(origin CXX),default) + CXX=$(shell if [ -e /usr/bin/clang++ ]; then echo clang++; else echo g++; fi) endif -LWIPINCLUDES:=-I$(LWIPDIR)/include -I$(LWIPARCH) -I$(LWIPDIR) -I. -Iext -Iinclude -CFLAGS=-Wno-format -Wno-deprecated -O3 -g -Wall -fPIC +OSTYPE=$(shell uname -s | tr '[A-Z]' '[a-z]') +BUILD=build/$(OSTYPE) + +CCX=clang++ + +# Windows +ifeq ($(OSTYPE),mingw32_nt-6.2) +CCX=g++ +WINDEFS=-Wno-c++11-compat -std=c++98 +LWIPARCH=$(CONTRIBDIR)/ports/win32 +endif +ifeq ($(OSTYPE),linux) +LWIPARCH=$(CONTRIBDIR)/ports/unix +endif +ifeq ($(OSTYPE),darwin) +LWIPARCH=$(CONTRIBDIR)/ports/unix +endif +ifeq ($(OSTYPE),freebsd) +LWIPARCH=$(CONTRIBDIR)/ports/unix +endif + +LWIPINCLUDES:=-I$(LWIPDIR)/include -I$(LWIPARCH) -I$(LWIPARCH)/include -I$(LWIPDIR) -I. -Iext -Iinclude +CFLAGS= $(WINDEFS) -Wno-format -Wno-deprecated -O3 -g -Wall -fPIC CFLAGS+=-DLWIP_IPV4 -DLWIP_IPV6=0 -DIPv4 -DLWIP_DEBUG=1 $(LWIPINCLUDES) UNIXLIB=liblwip.a @@ -63,8 +80,6 @@ LWIPNOAPPSFILES+=$(ARCHFILES) LWIPNOAPPSFILESW=$(wildcard $(LWIPNOAPPSFILES)) LWIPNOAPPSOBJS=$(notdir $(LWIPNOAPPSFILESW:.c=.o)) -CCX=clang++ - %.o: $(CCX) $(CFLAGS) -c $(<:.o=.c) diff --git a/src/Platform.cpp b/src/Platform.cpp index 51b2b4f..5cbc1ae 100644 --- a/src/Platform.cpp +++ b/src/Platform.cpp @@ -53,7 +53,8 @@ void handle_general_failure() { inline unsigned int gettid() { #ifdef _WIN32 - return GetCurrentThreadId(); + //return GetCurrentThreadId(); + return 0; #elif defined(__linux__) return static_cast(syscall(__NR_gettid)); #elif defined(__APPLE__) diff --git a/src/Utilities.cpp b/src/Utilities.cpp index 7c1d1bb..d0010e7 100644 --- a/src/Utilities.cpp +++ b/src/Utilities.cpp @@ -32,6 +32,171 @@ #include "InetAddress.hpp" #include "Debug.hpp" +#include "Utilities.h" + +#if defined(__MINGW32__) + +int inet_pton4(const char *src, void *dst) +{ + uint8_t tmp[NS_INADDRSZ], *tp; + + int saw_digit = 0; + int octets = 0; + *(tp = tmp) = 0; + + int ch; + while ((ch = *src++) != '\0') + { + if (ch >= '0' && ch <= '9') + { + uint32_t n = *tp * 10 + (ch - '0'); + + if (saw_digit && *tp == 0) + return 0; + + if (n > 255) + return 0; + + *tp = n; + if (!saw_digit) + { + if (++octets > 4) + return 0; + saw_digit = 1; + } + } + else if (ch == '.' && saw_digit) + { + if (octets == 4) + return 0; + *++tp = 0; + saw_digit = 0; + } + else + return 0; + } + if (octets < 4) + return 0; + + memcpy(dst, tmp, NS_INADDRSZ); + + return 1; +} + +int inet_pton6(const char *src, void *dst) +{ + static const char xdigits[] = "0123456789abcdef"; + uint8_t tmp[NS_IN6ADDRSZ]; + + uint8_t *tp = (uint8_t*) memset(tmp, '\0', NS_IN6ADDRSZ); + uint8_t *endp = tp + NS_IN6ADDRSZ; + uint8_t *colonp = NULL; + + /* Leading :: requires some special handling. */ + if (*src == ':') + { + if (*++src != ':') + return 0; + } + + const char *curtok = src; + int saw_xdigit = 0; + uint32_t val = 0; + int ch; + while ((ch = tolower(*src++)) != '\0') + { + const char *pch = strchr(xdigits, ch); + if (pch != NULL) + { + val <<= 4; + val |= (pch - xdigits); + if (val > 0xffff) + return 0; + saw_xdigit = 1; + continue; + } + if (ch == ':') + { + curtok = src; + if (!saw_xdigit) + { + if (colonp) + return 0; + colonp = tp; + continue; + } + else if (*src == '\0') + { + return 0; + } + if (tp + NS_INT16SZ > endp) + return 0; + *tp++ = (uint8_t) (val >> 8) & 0xff; + *tp++ = (uint8_t) val & 0xff; + saw_xdigit = 0; + val = 0; + continue; + } + if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) && + inet_pton4(curtok, (char*) tp) > 0) + { + tp += NS_INADDRSZ; + saw_xdigit = 0; + break; /* '\0' was seen by inet_pton4(). */ + } + return 0; + } + if (saw_xdigit) + { + if (tp + NS_INT16SZ > endp) + return 0; + *tp++ = (uint8_t) (val >> 8) & 0xff; + *tp++ = (uint8_t) val & 0xff; + } + if (colonp != NULL) + { + /* + * Since some memmove()'s erroneously fail to handle + * overlapping regions, we'll do the shift by hand. + */ + const int n = tp - colonp; + + if (tp == endp) + return 0; + + for (int i = 1; i <= n; i++) + { + endp[-i] = colonp[n - i]; + colonp[n - i] = 0; + } + tp = endp; + } + if (tp != endp) + return 0; + + memcpy(dst, tmp, NS_IN6ADDRSZ); + + return 1; +} + +int inet_pton(int af, const char *src, void *dst) +{ + switch (af) + { + case AF_INET: + return inet_pton4(src, dst); + case AF_INET6: + return inet_pton6(src, dst); + default: + return -1; + } +} + +#endif + + + + char *beautify_eth_proto_nums(int proto) { @@ -120,6 +285,7 @@ bool ipv6_in_subnet(ZeroTier::InetAddress *subnet, ZeroTier::InetAddress *addr) return !strcmp(r.toIpString(b0), b.toIpString(b1)); } +/* void sockaddr2inet(int socket_family, const struct sockaddr *addr, ZeroTier::InetAddress *inet) { char ipstr[INET6_ADDRSTRLEN]; @@ -137,6 +303,7 @@ void sockaddr2inet(int socket_family, const struct sockaddr *addr, ZeroTier::Ine inet->fromString(addrstr); } } +*/ void mac2str(char *macbuf, int len, unsigned char* addr) { diff --git a/src/VirtualSocket.cpp b/src/VirtualSocket.cpp index 9bb0edc..6cc872a 100644 --- a/src/VirtualSocket.cpp +++ b/src/VirtualSocket.cpp @@ -34,7 +34,10 @@ #define ZT_VIRTUALSOCKET_HPP #include + +#if defined(__linux__) || #defined(__APPLE__) #include +#endif #include "VirtualSocket.h" #include "VirtualBindingPair.h" diff --git a/src/VirtualTap.cpp b/src/VirtualTap.cpp index 85cb9b0..c33a0c0 100644 --- a/src/VirtualTap.cpp +++ b/src/VirtualTap.cpp @@ -421,7 +421,7 @@ namespace ZeroTier { target_addr = managed_routes->at(i).target; via_addr = managed_routes->at(i).via; nm = target_addr.netmask(); - for (int j=0; jat(j).target; diff --git a/src/libzt.cpp b/src/libzt.cpp index 662ba2b..2775851 100644 --- a/src/libzt.cpp +++ b/src/libzt.cpp @@ -66,7 +66,7 @@ void sys2lwip(int fd, const struct sockaddr *orig, struct sockaddr *modified) } #if defined(LIBZT_IPV4) if (ss.ss_family == AF_INET) { -#if defined(__linux__) +#if defined(__linux__) || defined(__MINGW32__) struct sockaddr_in *p4 = (struct sockaddr_in *)modified; struct sockaddr_in *addr4 = (struct sockaddr_in*)orig; p4->sin_len = sizeof(struct sockaddr_in); @@ -80,7 +80,7 @@ void sys2lwip(int fd, const struct sockaddr *orig, struct sockaddr *modified) #if defined(LIBZT_IPV6) if (ss.ss_family == AF_INET6) { -#if defined(__linux__) +#if defined(__linux__) || defined(__MINGW32__) struct sockaddr_in6 *p6 = (struct sockaddr_in6 *)modified; struct sockaddr_in6 *addr6 = (struct sockaddr_in6*)orig; p6->sin6_len = sizeof(struct sockaddr_in6); @@ -311,6 +311,7 @@ int zts_close(int fd) return err; } +#if defined(__linux__) int zts_poll(struct pollfd *fds, nfds_t nfds, int timeout) { int err = -1; @@ -324,6 +325,7 @@ int zts_poll(struct pollfd *fds, nfds_t nfds, int timeout) #endif return err; } +#endif int zts_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) diff --git a/src/lwIP.cpp b/src/lwIP.cpp index 429335c..0e3c309 100644 --- a/src/lwIP.cpp +++ b/src/lwIP.cpp @@ -115,6 +115,9 @@ void lwip_driver_init() if (lwip_driver_initialized == true) { return; } +#if defined(__MINGW32__) + sys_init(); // required for win32 initializtion of critical sections +#endif sys_thread_new("main_network_stack_thread", main_network_stack_thread, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO); } @@ -160,7 +163,7 @@ err_t lwip_eth_tx(struct netif *netif, struct pbuf *p) return ERR_OK; } -void general_lwip_init_interface(void *tapref, struct netif *interface, const char *name, const ZeroTier::MAC &mac, const ZeroTier::InetAddress &addr, const ZeroTier::InetAddress &nm, const ZeroTier::InetAddress &gw) +void general_lwip_init_interface(void *tapref, void *netif, const char *name, const ZeroTier::MAC &mac, const ZeroTier::InetAddress &addr, const ZeroTier::InetAddress &nm, const ZeroTier::InetAddress &gw) { #if defined(LIBZT_IPV4) char ipbuf[INET6_ADDRSTRLEN], nmbuf[INET6_ADDRSTRLEN], gwbuf[INET6_ADDRSTRLEN]; @@ -192,7 +195,7 @@ void general_lwip_init_interface(void *tapref, struct netif *interface, const ch #endif } -void general_turn_on_interface(struct netif *interface) +void general_turn_on_interface(void *netif) { //netif_set_up(&n1); //netif_set_default(&n1); @@ -206,9 +209,9 @@ void lwip_dns_init() dns_init(); } -void lwip_start_dhcp(struct netif *interface) +void lwip_start_dhcp(void *netif) { - netifapi_dhcp_start(interface); + netifapi_dhcp_start((struct netif *)netif); } void lwip_init_interface(void *tapref, const ZeroTier::MAC &mac, const ZeroTier::InetAddress &ip)