Windows Support

This commit is contained in:
Joseph Henry
2017-10-09 17:56:40 -07:00
parent 06a09edd6d
commit 2900f1f995
28 changed files with 486 additions and 241 deletions

View File

@@ -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:

View File

@@ -26,12 +26,17 @@
#include <unistd.h>
#include <string.h>
#if defined(__linux__) || defined(__APPLE__)
#include <netdb.h>
#endif
//#include "Winsock2.h"
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <fcntl.h>
#include <regex.h>
#include <vector>
#include <algorithm>
@@ -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);

View File

@@ -34,10 +34,13 @@
#include "Phy.hpp"
#include "OSUtils.hpp"
#if defined(__linux__) || defined(__APPLE__)
#include <sys/select.h>
#endif
#include <queue>
#include <vector>
#include <stdio.h>
#include <sys/select.h>
#define BUF_SZ 1024*1024

View File

@@ -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

View File

@@ -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

View File

@@ -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;

View File

@@ -34,11 +34,14 @@
#define LIBZT_DEBUG_HPP
#include <pthread.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include <cstring>
#if defined(__linux__)
#include <sys/syscall.h>
#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 <jni.h>

View File

@@ -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 */

View File

@@ -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

View File

@@ -33,11 +33,14 @@
#ifndef LIBZT_H
#define LIBZT_H
#include <poll.h>
#include <stdlib.h>
#include <stdint.h>
#include <vector>
#if defined(__linux__)
#include <poll.h>
#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"

View File

@@ -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.

View File

@@ -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)

View File

@@ -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<unsigned int>(syscall(__NR_gettid));
#elif defined(__APPLE__)

View File

@@ -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)
{

View File

@@ -34,7 +34,10 @@
#define ZT_VIRTUALSOCKET_HPP
#include <ctime>
#if defined(__linux__) || #defined(__APPLE__)
#include <sys/socket.h>
#endif
#include "VirtualSocket.h"
#include "VirtualBindingPair.h"

View File

@@ -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; j<routes.size(); j++) {
for (size_t j=0; j<routes.size(); j++) {
if (via_addr.ipsEqual(null_addr) || target_addr.ipsEqual(null_addr)) {
found=true;
continue;
@@ -439,7 +439,7 @@ namespace ZeroTier {
}
}
// check if route exists in tap but not in pushed routes (remove)
for (int i=0; i<routes.size(); i++) {
for (size_t i=0; i<routes.size(); i++) {
found = false;
for (int j=0; j<ZT_MAX_NETWORK_ROUTES; j++) {
target_addr = managed_routes->at(j).target;

View File

@@ -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)

View File

@@ -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)