wider selftest coverage

This commit is contained in:
Joseph Henry
2017-05-05 16:46:07 -07:00
parent d631e06d99
commit dcdc13204c
31 changed files with 1613 additions and 303 deletions
+3
View File
@@ -11,6 +11,9 @@
integrations/android/android_jni_lib/proj/local.properties
integrations/android/example_app/local.properties
# examples
examples/java/ZeroTierHelloWorld/lib/libzt.jnilib
# Unity
*.meta
integrations/Unity3D/Library
+15 -7
View File
@@ -11,10 +11,9 @@ Embed ZeroTier directly into your app or service. Connect everything without wor
```
string str = "welcome to the machine";
zts_start("./zt");
while(!zts_service_running())
while(!zts_running())
sleep(1);
zts_join_network(nwid);
int err, sockfd;
zts_join("e5cd7a7b1c0fa971");
while(!zts_has_address(nwid))
sleep(1);
if((fd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) {
@@ -31,7 +30,7 @@ Bindings also exist for [many popular languages]().
## Build Targets
### Static Library
- `make static_lib`: Will output to `build/`
- `make static_lib SDK_IPV4=1`: Will output to `build/`
### Tests
- `make tests`: Will output to `build/tests/`
@@ -40,8 +39,8 @@ Then run the unit test suite with whatever configuration you need. For instance:
To run a single-test IPv4 client/server test:
- Host 1: `./build/test/unit zt1 c7cd7c9e1b0f52a2 simple 4 server 8787`
- Host 2: `./build/test/unit zt2 c7cd7c9e1b0f52a2 simple 4 client 10.9.9.40 8787`
- Host 1: `./build/selftest zt1 c7cd7c9e1b0f52a2 simple 4 server 8787`
- Host 2: `./build/selftest zt2 c7cd7c9e1b0f52a2 simple 4 client 10.9.9.40 8787`
To run a multi-message IPv4 client/server test:
- Host 1: ./build/test/unit zt2 c7cd7c9e1b0f52a2 simple 4 server 8787 n_bytes 100 50'
@@ -49,8 +48,17 @@ To run a multi-message IPv4 client/server test:
- For more unit test examples, see the [testing]() page
## IP version flags
`SDK_IPV4=1`
`SDK_IPV6=1`
## Using Language Bindings
`SDK_LANG_JAVA=1`
`SDK_LANG_JNI=1` - Enable JNI bindings for Java (produces a shared library)
`SDK_LANG_CSHARP=1`
`SDK_LANG_PYTHON=1`
`SDK_LANG_GO=1`
## Debugging flags
`SDK_DEBUG=1` - Enable SDK library debugging
`ZT_DEBUG=1` - Enable core ZeroTier service debugging
+2 -2
View File
@@ -39,13 +39,13 @@ int main()
printf("homePath = %s\n", homePath);
// Wait for ZeroTier service to start
while(!zts_service_running()) {
while(!zts_running()) {
printf("wating for service to start\n");
sleep(1);
}
// Join a network
zts_join_network(nwid);
zts_join(nwid);
// Wait for ZeroTier service to issue an address to the device on the given network
while(!zts_has_address("e5cd7a9e1c0fd272")) {
+416
View File
@@ -0,0 +1,416 @@
/*
* ZeroTier SDK - Network Virtualization Everywhere
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* --
*
* You can be released from the requirements of the license by purchasing
* a commercial license. Buying such a license is mandatory as soon as you
* develop commercial closed-source software that incorporates or links
* directly against ZeroTier software without disclosing the source code
* of your own application.
*/
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Collections.Generic;
using System.Globalization;
// TODO:
/*
* check for mem leaks surrounding managed/unmanaged barrier
* find root of 2X buffer size requirement issue
* check that cross-thread oprations are handled correctly
* check that .IsRunning() doesn't bork the entire system anymore
* Allow max packet size configuration
* Handle exceptions from unmanaged code
* */
// Provides a bare-bones interface to ZeroTier-administered sockets
public class ZTSDK {
// ZeroTier background thread
protected Thread ztThread;
protected List<int> connections = new List<int> ();
protected int MaxPacketSize;
// Only allow one network at a time for BETA
protected bool joined_to_network = false;
protected string nwid = "";
protected string path = "";
// Platform-specific paths and bundle/libary names
#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
const string DLL_PATH = "ZeroTierSDK_Unity3D_OSX";
protected string rpc_path = "/Library/Application\\ Support/ZeroTier/SDK/";
#endif
#if UNITY_IOS || UNITY_IPHONE
const string DLL_PATH = "ZeroTierSDK_Unity3D_iOS";
protected string rpc_path = "ZeroTier/One/";
#endif
#if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
const string DLL_PATH = "ZeroTierSDK_Unity3D_WIN";
protected string rpc_path = "";
#endif
#if UNITY_STANDALONE_LINUX
const string DLL_PATH = "ZeroTierSDK_Unity3D_LINUX";
protected string rpc_path = "";
#endif
#if UNITY_ANDROID
const string DLL_PATH = "ZeroTierSDK_Unity3D_ANDROID";
protected string rpc_path = "ZeroTier/One/";
#endif
#region DLL Imports
// ZeroTier service / debug initialization
[DllImport (DLL_PATH)]
public static extern void SetDebugFunction( IntPtr fp );
[DllImport (DLL_PATH)]
private static extern int unity_start_service(string path);
[DllImport (DLL_PATH)]
private static extern int unity_start_service_and_rpc(string path, string nwid);
[DllImport (DLL_PATH)]
protected static extern bool zts_is_running();
[DllImport (DLL_PATH)]
protected static extern void zts_stop_service();
// Connection calls
[DllImport (DLL_PATH)]
protected static extern int zts_socket(int family, int type, int protocol);
[DllImport (DLL_PATH)]
unsafe protected static extern int zts_bind(int sockfd, System.IntPtr addr, int addrlen);
[DllImport (DLL_PATH)]
unsafe protected static extern int zts_connect(int sockfd, System.IntPtr addr, int addrlen);
[DllImport (DLL_PATH)]
protected static extern int zts_accept(int sockfd);
[DllImport (DLL_PATH)]
protected static extern int zts_listen(int sockfd, int backlog);
[DllImport (DLL_PATH)]
protected static extern int zts_close(int sockfd);
// RX / TX
[DllImport (DLL_PATH)]
unsafe protected static extern int zts_recv(int sockfd, [In, Out] IntPtr buf, int len);
[DllImport (DLL_PATH)]
unsafe protected static extern int zts_send(int sockfd, IntPtr buf, int len);
[DllImport (DLL_PATH)]
unsafe protected static extern int zts_set_nonblock(int sockfd);
[DllImport (DLL_PATH)]
unsafe protected static extern int zts_sendto(int fd, IntPtr buf, int len, int flags, System.IntPtr addr, int addrlen);
[DllImport (DLL_PATH)]
unsafe protected static extern int zts_recvfrom(int fd, [In, Out] IntPtr buf, int len, int flags, System.IntPtr addr, int addrlen);
// ZT Network controls
[DllImport (DLL_PATH)]
protected static extern void zts_join_network(string nwid);
[DllImport (DLL_PATH)]
protected static extern void zts_leave_network(string nwid);
#endregion
// Interop structures
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi)]
public struct sockaddr {
/// u_short->unsigned short
public ushort sa_family;
/// char[14]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=14)]
public string sa_data;
}
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void MyDelegate(string str);
// Debug output callback
static void CallBackFunction(string str) {
Debug.Log("ZeroTier: " + str);
}
// Returns a path for RPC communications to the service
private string rpcCommPath()
{
if(path != "" && nwid != "") {
return path + "nc_" + nwid;
}
return "";
}
// Thread which starts the ZeroTier service
protected void zt_service_thread()
{
// Set up debug callback
MyDelegate callback_delegate = new MyDelegate( CallBackFunction );
IntPtr intptr_delegate = Marshal.GetFunctionPointerForDelegate(callback_delegate);
SetDebugFunction( intptr_delegate );
// Start service
/* This new instance will communicate via a named pipe, so any
* API calls (ZeroTier.Connect(), ZeroTier.Send(), etc) will be sent to the service
* via this pipe.
*/
if(nwid.Length > 0) {
unity_start_service_and_rpc (path, nwid);
}
else {
unity_start_service(rpcCommPath());
}
}
// Returns the nwid of the network you're currently connected to
public string GetNetworkID() {
return nwid;
}
// Returns whether you're currently connected to a network
public bool IsConnected() {
return nwid != "";
}
// Start the ZeroTier service
protected void Init()
{
ztThread = new Thread(() => {
try {
zt_service_thread();
} catch(Exception e) {
Debug.Log(e.Message.ToString());
}
});
ztThread.IsBackground = true; // Allow the thread to be aborted safely
ztThread.Start();
}
// Initialize the ZeroTier service with a given path
public ZTSDK(string path, string nwid) {
Debug.Log("ZTSDK(): " + nwid);
this.path = path;
this.nwid = nwid;
Init();
}
public ZTSDK (string path) {
this.path = path;
Init();
}
// Initialize the ZeroTier service
public ZTSDK() {
Init();
}
#region Network Handling
// Joins a ZeroTier virtual network
public bool JoinNetwork(string nwid)
{
if(!joined_to_network) {
zts_join_network(nwid);
return true;
}
return false;
}
// Leaves a ZeroTier virtual network
public bool LeaveNetwork(string nwid)
{
if(!joined_to_network) {
return false;
}
else {
zts_leave_network(nwid);
return true;
}
}
#endregion
// Creates a new ZeroTier-administered socket
public int Socket(int family, int type, int protocol)
{
return zts_socket (family, type, protocol);
}
// Binds to a specific address
public int Bind(int fd, string addr, int port)
{
GCHandle sockaddr_ptr = Generate_unmananged_sockaddr(addr + ":" + port);
IntPtr pSockAddr = sockaddr_ptr.AddrOfPinnedObject ();
int addrlen = Marshal.SizeOf (pSockAddr);
return zts_bind (fd, pSockAddr, addrlen);
}
// Listens for an incoming connection request
public int Listen(int fd, int backlog)
{
return zts_listen(fd, backlog);
}
// Accepts an incoming connection
public int Accept(int fd)
{
return zts_accept (fd);
}
// Closes a connection
public int Close(int fd)
{
return Close (fd);
}
// Connects to a remote host
public int Connect(int fd, string addr, int port)
{
GCHandle sockaddr_ptr = Generate_unmananged_sockaddr(addr + ":" + port);
IntPtr pSockAddr = sockaddr_ptr.AddrOfPinnedObject ();
int addrlen = Marshal.SizeOf (pSockAddr);
return zts_connect (fd, pSockAddr, addrlen);
}
public int Read(int fd, ref char[] buf, int len)
{
GCHandle handle = GCHandle.Alloc(buf, GCHandleType.Pinned);
IntPtr ptr = handle.AddrOfPinnedObject();
int bytes_read = zts_recv (fd, ptr, len*2);
string str = Marshal.PtrToStringAuto(ptr);
//Marshal.Copy (ptr, buf, 0, bytes_read);
buf = Marshal.PtrToStringAnsi(ptr).ToCharArray();
return bytes_read;
}
public int Write(int fd, char[] buf, int len)
{
GCHandle handle = GCHandle.Alloc(buf, GCHandleType.Pinned);
IntPtr ptr = handle.AddrOfPinnedObject();
//error = 0;
int bytes_written;
// FIXME: Sending a length of 2X the buffer size seems to fix the object pinning issue
if((bytes_written = zts_send(fd, ptr, len*2)) < 0) {
//error = (byte)bytes_written;
}
return bytes_written;
}
// Sends data to an address
public int SendTo(int fd, char[] buf, int len, int flags, string addr, int port)
{
GCHandle handle = GCHandle.Alloc(buf, GCHandleType.Pinned);
IntPtr ptr = handle.AddrOfPinnedObject();
int bytes_written;
// Form address structure
GCHandle sockaddr_ptr = Generate_unmananged_sockaddr(addr + ":" + port);
IntPtr pSockAddr = sockaddr_ptr.AddrOfPinnedObject ();
int addrlen = Marshal.SizeOf (pSockAddr);
if((bytes_written = zts_sendto(fd, ptr, len*2, flags, pSockAddr, addrlen)) < 0) {
//error = (byte)bytes_written;
}
return bytes_written;
}
// Receives data from an address
public int RecvFrom(int fd, ref char[] buf, int len, int flags, string addr, int port)
{
GCHandle handle = GCHandle.Alloc(buf, GCHandleType.Pinned);
IntPtr ptr = handle.AddrOfPinnedObject();
// Form address structure
GCHandle sockaddr_ptr = Generate_unmananged_sockaddr(addr + ":" + port);
IntPtr pSockAddr = sockaddr_ptr.AddrOfPinnedObject ();
int addrlen = Marshal.SizeOf (pSockAddr);
int bytes_read = zts_recvfrom(fd, ptr, len*2, flags, pSockAddr, addrlen);
string str = Marshal.PtrToStringAuto(ptr);
//Marshal.Copy (ptr, buf, 0, bytes_read);
buf = Marshal.PtrToStringAnsi(ptr).ToCharArray();
return bytes_read;
}
#region Service-Related calls
// Returns whether the ZeroTier service is currently running
public bool IsRunning()
{
return zts_is_running ();
}
// Terminates the ZeroTier service
public void Terminate()
{
zts_stop_service ();
}
#endregion
// --- Utilities ---
// Handles IPv4 and IPv6 notation.
public static IPEndPoint CreateIPEndPoint(string endPoint)
{
string[] ep = endPoint.Split(':');
if (ep.Length < 2) throw new FormatException("Invalid endpoint format");
IPAddress ip;
if (ep.Length > 2) {
if (!IPAddress.TryParse(string.Join(":", ep, 0, ep.Length - 1), out ip)) {
throw new FormatException("Invalid ip-adress");
}
}
else {
if (!IPAddress.TryParse(ep[0], out ip)) {
throw new FormatException("Invalid ip-adress");
}
}
int port;
if (!int.TryParse(ep[ep.Length - 1], NumberStyles.None, NumberFormatInfo.CurrentInfo, out port)) {
throw new FormatException("Invalid port");
}
return new IPEndPoint(ip, port);
}
// Generates an unmanaged sockaddr structure from a string-formatted endpoint
public static GCHandle Generate_unmananged_sockaddr(string endpoint_str)
{
IPEndPoint ipEndPoint;
ipEndPoint = CreateIPEndPoint (endpoint_str);
SocketAddress socketAddress = ipEndPoint.Serialize ();
// use an array of bytes instead of the sockaddr structure
byte[] sockAddrStructureBytes = new byte[socketAddress.Size];
GCHandle sockAddrHandle = GCHandle.Alloc (sockAddrStructureBytes, GCHandleType.Pinned);
for (int i = 0; i < socketAddress.Size; ++i) {
sockAddrStructureBytes [i] = socketAddress [i];
}
return sockAddrHandle;
}
public static GCHandle Generate_unmanaged_buffer(byte[] buf)
{
// use an array of bytes instead of the sockaddr structure
GCHandle sockAddrHandle = GCHandle.Alloc (buf, GCHandleType.Pinned);
return sockAddrHandle;
}
}
+4 -2
View File
@@ -1,7 +1,9 @@
Java Examples
======
`make shared_jni_lib SDK_JNI=1 SDK_IPV4=1`
Uses API described in [api/java/README.md]
- Copy `ZeroTier.java` into `your_project/src/zerotier`
- Copy `Address.java` into `your_project/src/zerotier`
- Copy `src/ZeroTier.java` into `your_project/src/zerotier`
- Copy `src/Address.java` into `your_project/src/zerotier`
- Copy `libzt.jnilib` or `libzt.so` from `build/` into `your_project/lib`
@@ -1,2 +1,3 @@
/MyClass$1.class
/zerotier/
/MyClass.class
+77
View File
@@ -0,0 +1,77 @@
/*
* ZeroTier SDK - Network Virtualization Everywhere
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* --
*
* You can be released from the requirements of the license by purchasing
* a commercial license. Buying such a license is mandatory as soon as you
* develop commercial closed-source software that incorporates or links
* directly against ZeroTier software without disclosing the source code
* of your own application.
*/
//
// Implementations located in src/wrappers/swift/XcodeWrapper.cpp
//
#ifndef Example_OSX_IOS_Bridging_Header_h
#define Example_OSX_IOS_Bridging_Header_h
#include <sys/socket.h>
// ZT INTERCEPT/RPC CONTROLS
int zt_init_rpc(const char *path, const char *nwid);
int zt_start_intercept();
void zt_disable_intercept();
void zt_enable_intercept();
// ZT SERVICE CONTROLS
void zt_start_service(const char * path);
void zt_stop_service();
void zt_start_service_and_rpc(const char * path, const char * nwid);
bool zt_service_is_running();
void zt_join_network(const char *nwid);
void zt_leave_network(const char *nwid);
void zt_get_ipv4_address(const char *nwid, char *addrstr);
void zt_get_ipv6_address(const char *nwid, char *addrstr);
// SOCKS5 PROXY CONTROLS
void zt_start_proxy_server(const char *nwid, struct sockaddr_storage addr);
void zt_stop_proxy_server(const char *nwid);
void zt_proxy_is_running(const char *nwid);
void zt_get_proxy_server_address(const char *nwid, struct sockaddr_storage addr);
// SOCKET API
int zt_connect(CONNECT_SIG);
int zt_bind(BIND_SIG);
int zt_accept(ACCEPT_SIG);
int zt_listen(LISTEN_SIG);
int zt_socket(SOCKET_SIG);
int zt_setsockopt(SETSOCKOPT_SIG);
int zt_getsockopt(GETSOCKOPT_SIG);
int zt_close(CLOSE_SIG);
int zt_getsockname(GETSOCKNAME_SIG);
int zt_getpeername(GETPEERNAME_SIG);
int zt_recvfrom(RECVFROM_SIG);
int zt_fcntl(FCNTL_SIG);
int zt_sendto(SENDTO_SIG);
#endif /* Example_OSX_IOS_Bridging_Header_h */
-3
View File
@@ -1,3 +0,0 @@
Swift Language Binding API for the ZeroTier SDK
======
+137
View File
@@ -0,0 +1,137 @@
/*
* ZeroTier SDK - Network Virtualization Everywhere
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* --
*
* You can be released from the requirements of the license by purchasing
* a commercial license. Buying such a license is mandatory as soon as you
* develop commercial closed-source software that incorporates or links
* directly against ZeroTier software without disclosing the source code
* of your own application.
*/
#include "sdk.h"
#include "XcodeWrapper.hpp"
#define INTERCEPT_ENABLED 111
#define INTERCEPT_DISABLED 222
// ZEROTIER CONTROLS
// Starts a ZeroTier service at the specified path
extern "C" void zt_start_service(const char *path, const char *nwid) {
zts_start_service(path);
//init_service(INTERCEPT_DISABLED, path);
}
// Starts a ZeroTier service at the specified path and initializes the RPC mechanism
//extern "C" void zt_start_service_and_rpc(const char *path, const char *nwid) {
// init_service_and_rpc(INTERCEPT_DISABLED, path, nwid);
//}
// Stops the core ZeroTier service
extern "C" void zt_stop_service() {
zts_stop_service();
}
// Returns whether the core ZeroTier service is running
extern "C" bool zt_service_is_running() {
return zts_service_is_running();
}
// Joins a ZeroTier virtual network
extern "C" void zt_join_network(const char *nwid) {
zts_join_network(nwid);
}
// Leaves a ZeroTier virtual network
extern "C" void zt_leave_network(const char *nwid) {
zts_leave_network(nwid);
}
// Returns a list of addresses associated with this device on the given network
extern "C" void zt_get_ipv4_address(const char *nwid, char *addrstr) {
zts_get_ipv4_address(nwid, addrstr);
}
// Returns a list of addresses associated with this device on the given network
extern "C" void zt_get_ipv6_address(const char *nwid, char *addrstr) {
zts_get_ipv6_address(nwid, addrstr);
}
// PROXY SERVER CONTROLS
//
extern "C" void zt_start_proxy_server(const char *homepath, const char *nwid, struct sockaddr_storage *addr) {
zts_start_proxy_server(homepath, nwid, addr);
}
//
extern "C" void zt_stop_proxy_server(const char *nwid) {
zts_stop_proxy_server(nwid);
}
//
extern "C" void zt_proxy_is_running(const char *nwid) {
zts_proxy_is_running(nwid);
}
//
extern "C" void zt_get_proxy_server_address(const char *nwid, struct sockaddr_storage *addr) {
zts_get_proxy_server_address(nwid, addr);
}
// Explicit ZT API wrappers
#if !defined(__IOS__)
// This isn't available for iOS since function interposition isn't as reliable
extern "C" void zt_init_rpc(const char *path, const char *nwid) {
zts_init_rpc(path, nwid);
}
#endif
// SOCKET API
// These are used in ZTSDK.swift to implement the higher-level API
extern "C" int zt_socket(SOCKET_SIG) {
return zts_socket(socket_family, socket_type, protocol);
}
extern "C" int zt_connect(CONNECT_SIG) {
return zts_connect(fd, addr, addrlen);
}
extern "C" int zt_bind(BIND_SIG){
return zts_bind(fd, addr, addrlen);
}
extern "C" int zt_accept(ACCEPT_SIG) {
return zts_accept(fd, addr, addrlen);
}
extern "C" int zt_listen(LISTEN_SIG) {
return zts_listen(fd, backlog);
}
extern "C" int zt_setsockopt(SETSOCKOPT_SIG) {
return zts_setsockopt(fd, level, optname, optval, optlen);
}
extern "C" int zt_getsockopt(GETSOCKOPT_SIG) {
return zts_getsockopt(fd, level, optname, optval, optlen);
}
extern "C" int zt_close(CLOSE_SIG) {
return zts_close(fd);
}
extern "C" int zt_getsockname(GETSOCKNAME_SIG) {
return zts_getsockname(fd, addr, addrlen);
}
extern "C" int zt_getpeername(GETPEERNAME_SIG) {
return zts_getpeername(fd, addr, addrlen);
}
extern "C" int zt_fcntl(FCNTL_SIG) {
return zts_fcntl(fd, cmd, flags);
}
extern "C" ssize_t zt_recvfrom(RECVFROM_SIG) {
return zts_recvfrom(fd, buf, len, flags, addr, addrlen);
}
extern "C" ssize_t zt_sendto(SENDTO_SIG) {
return zts_sendto(fd, buf, len, flags, addr, addrlen);
}
+30
View File
@@ -0,0 +1,30 @@
/*
* ZeroTier SDK - Network Virtualization Everywhere
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* --
*
* You can be released from the requirements of the license by purchasing
* a commercial license. Buying such a license is mandatory as soon as you
* develop commercial closed-source software that incorporates or links
* directly against ZeroTier software without disclosing the source code
* of your own application.
*/
#ifndef SDK_XCODE_WRAPPER_HPP
#define SDK_XCODE_WRAPPER_HPP
#endif // SDK_XCODE_WRAPPER_HPP
+216
View File
@@ -0,0 +1,216 @@
/*
* ZeroTier SDK - Network Virtualization Everywhere
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* --
*
* You can be released from the requirements of the license by purchasing
* a commercial license. Buying such a license is mandatory as soon as you
* develop commercial closed-source software that incorporates or links
* directly against ZeroTier software without disclosing the source code
* of your own application.
*/
import Foundation
// Convenience structure for getting address data to/from the native library
struct ZTAddress
{
var family: Int32
var addr: String
var port: Int16
var data: sockaddr_in?
init(_ family: Int32, _ addr: String, _ port: Int16) {
self.family = family
self.addr = addr
self.port = port
}
func to_sockaddr_in() -> UnsafePointer<sockaddr> {
var data = sockaddr_in(sin_len: UInt8(sizeof(sockaddr_in)),
sin_family: UInt8(AF_INET),
sin_port: UInt16(port).bigEndian,
sin_addr: in_addr(s_addr: 0),
sin_zero: (0,0,0,0,0,0,0,0))
inet_pton(AF_INET, addr, &(data.sin_addr));
return UnsafePointer<sockaddr>([data]);
}
func len() -> UInt8 {
return UInt8(sizeof(sockaddr_in))
}
}
// Convenience wrapper class for ZeroTier/SDK/Proxy controls
// Implemented in terms of SDK_XcodeWrapper.cpp
class ZTSDK : NSObject
{
var service_thread : NSThread!
private func ztnc_start_service(path: String?) {
if(path == nil) {
zt_start_service(
NSSearchPathForDirectoriesInDomains(
NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomainMask.UserDomainMask,true)[0])
return;
}
zt_start_service(path!)
}
// Starts the ZeroTier background service
func start_service(path: String?) {
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
dispatch_async(queue) {
self.ztnc_start_service(path)
}
sleep(3)
while(service_is_running() == false) { /* waiting for service to start */ }
}
// Stops the ZeroTier background service
func stop_service() {
zt_stop_service()
}
// Returns whether the ZeroTier background service is running
func service_is_running() -> Bool {
return zt_service_is_running()
}
// Joins a ZeroTier network
func join_network(nwid: String) {
zt_join_network(nwid)
}
// Leaves a ZeroTier network
func leave_network(nwid: String) {
zt_leave_network(nwid)
}
// Returns the IPV4 address of this device on a given ZeroTier network
func get_ipv4_address(nwid: String, inout _ addrbuf: [Int8]) {
zt_get_ipv4_address(nwid,&addrbuf)
}
// Returns the IPV6 address of this device on a given ZeroTier network
func get_ipv6_address(nwid: String, inout _ addrbuf: [Int8]) {
zt_get_ipv6_address(nwid,&addrbuf)
}
// PROXY SERVER CONTROLS
//
/*
func start_proxy_server(homepath: String, nwid: String, struct sockaddr_storage *addr) {
zt_start_proxy_server(homepath, nwid, addr);
}
//
func stop_proxy_server(nwid: String) {
zt_stop_proxy_server(nwid);
}
//
func proxy_is_running(const char *homepath, const char *nwid, struct sockaddr_storage *addr) {
zt_start_proxy_server(homepath, nwid, addr);
}
//
func get_proxy_server_address(const char *nwid, struct sockaddr_storage *addr) {
zt_get_proxy_server_address(nwid, addr);
}
// Explicit ZT API wrappers
#if !defined(__IOS__)
// This isn't available for iOS since function interposition isn't as reliable
func init_rpc(const char *path, const char *nwid) {
zt_init_rpc(path, nwid);
}
#endif
*/
// TODO: Verify this hasn't been broken. Should check for interface addresses based on
// protocol version. (important)
// SOCKET API
func socket(socket_family: Int32, _ socket_type: Int32, _ socket_protocol: Int32) -> Int32 {
return zt_socket(socket_family, socket_type, socket_protocol);
}
func connect(fd: Int32, _ addr: ZTAddress, _ nwid: String? = nil) -> Int32 {
if(nwid == nil) { // no nwid is provided to check for address, try once and fail
return zt_connect(Int32(fd), addr.to_sockaddr_in(), UInt32(addr.len()));
}
while(true) { // politely wait until an address is provided. simulates a blocking call
var addrbuf = [Int8](count: 16, repeatedValue: 0)
self.get_ipv4_address(nwid!, &addrbuf)
let addr_str:String = String.fromCString(addrbuf)!
if(addr_str != "-1.-1.-1.-1/-1") {
return zt_connect(Int32(fd), addr.to_sockaddr_in(), UInt32(addr.len()));
}
}
}
func bind(fd: Int32, _ addr: ZTAddress, _ nwid: String? = nil) -> Int32 {
if(nwid == nil) { // no nwid is provided to check for address, try once and fail
return zt_bind(Int32(fd), addr.to_sockaddr_in(), UInt32(addr.len()));
}
while(true) { // politely wait until an address is provided. simulates a blocking call
var addrbuf = [Int8](count: 16, repeatedValue: 0)
self.get_ipv4_address(nwid!, &addrbuf)
let addr_str:String = String.fromCString(addrbuf)!
if(addr_str != "-1.-1.-1.-1/-1") {
return zt_bind(Int32(fd), addr.to_sockaddr_in(), UInt32(addr.len()));
}
}
}
func accept(fd: Int32, _ addr: ZTAddress) -> Int32 {
return zt_accept(Int32(fd), UnsafeMutablePointer<sockaddr>([addr.data]), UnsafeMutablePointer<UInt32>([addr.len]));
}
func listen(fd: Int32, _ backlog: Int16) -> Int32 {
return zt_listen(Int32(fd), Int32(backlog));
}
func setsockopt(fd: Int32, _ level: Int32, _ optname: Int32, _ optval: UnsafePointer<Void>, _ optlen: Int32) -> Int32 {
return zt_setsockopt(fd, level, optname, optval, UInt32(optlen));
}
func getsockopt(fd: Int32, _ level: Int32, _ optname: Int32, _ optval: UnsafeMutablePointer<Void>, _ optlen: UInt32) -> Int32 {
return zt_getsockopt(fd, level, optname, optval, UnsafeMutablePointer<UInt32>([optlen]));
}
func close(fd: Int32) -> Int32 {
return zt_close(fd);
}
func getsockname(fd: Int32, _ addr: ZTAddress) -> Int32 {
return zt_getsockname(fd, UnsafeMutablePointer<sockaddr>([addr.data]), UnsafeMutablePointer<UInt32>([addr.len]));
}
func getpeername(fd: Int32, _ addr: ZTAddress) -> Int32 {
return zt_getpeername(fd, UnsafeMutablePointer<sockaddr>([addr.data]), UnsafeMutablePointer<UInt32>([addr.len]));
}
func fcntl(fd: Int32, _ cmd: Int32, _ flags: Int32) -> Int32 {
return zt_fcntl(fd, cmd, flags);
}
func recvfrom(fd: Int32, _ buf: UnsafeMutablePointer<Void>, _ len: Int32, _ flags: Int32, _ addr: ZTAddress) -> Int32 {
return zt_recvfrom(fd, buf, Int(len), flags, UnsafeMutablePointer<sockaddr>([addr.data]), UnsafeMutablePointer<UInt32>([addr.len]));
}
func sendto(fd: Int32, _ buf: UnsafePointer<Void>, _ len: Int32, _ flags: Int32, _ addr: ZTAddress) -> Int32 {
return zt_sendto(fd, buf, Int(len), flags, addr.to_sockaddr_in(), UInt32(addr.len()));
}
}
+11 -1
View File
@@ -9,7 +9,7 @@
#include "pico_config.h"
#include "pico_frame.h"
#define PICO_MAX_TIMERS 20
#define PICO_MAX_TIMERS 128
#define PICO_ETH_MRU (1514u)
#define PICO_IP_MRU (1500u)
@@ -78,6 +78,16 @@ int pico_notify_ttl_expired(struct pico_frame *f);
int pico_notify_frag_expired(struct pico_frame *f);
int pico_notify_pkt_too_big(struct pico_frame *f);
#ifdef __cplusplus
extern "C" {
#endif
int pico_ntimers();
#ifdef __cplusplus
}
#endif
/* Various. */
int pico_source_is_local(struct pico_frame *f);
int pico_frame_dst_is_unicast(struct pico_frame *f);
+5
View File
@@ -871,6 +871,11 @@ int32_t pico_seq_compare(uint32_t a, uint32_t b)
return 0;
}
extern int pico_ntimers()
{
return Timers->n;
}
static void pico_check_timers(void)
{
struct pico_timer *t;
+11 -2
View File
@@ -73,7 +73,7 @@
// zwrite() might cause data loss, for this reason, sockets will SO_LINGER for
// a short period of time by default as a precaution.
#define ZT_SOCK_BEHAVIOR_LINGER true
#define ZT_SOCK_BEHAVIOR_LINGER false
#define ZT_SOCK_BEHAVIOR_LINGER_TIME 2 // s
/****************************************************************************/
@@ -358,10 +358,19 @@ namespace ZeroTier
extern ZeroTier::picoTCP *picostack;
}
/**
* Returns the number of sockets either already provisioned or waiting to be
* Some network stacks may have a limit on the number of sockets that they can
* safely handle due to timer construction, this is a way to check that we
* haven't passed that limit. Someday if multiple stacks are used simultaneously
* the logic for this function should change accordingly.
*/
int zts_nsockets();
/**
* Don't call this directly, use 'zts_start()'
*/
void *_start_service(void *thread_id);
void *zts_start_service(void *thread_id);
/****************************************************************************/
/* Debug */
+27 -17
View File
@@ -1,3 +1,28 @@
#
# ZeroTier SDK - Network Virtualization Everywhere
# Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# --
#
# You can be released from the requirements of the license by purchasing
# a commercial license. Buying such a license is mandatory as soon as you
# develop commercial closed-source software that incorporates or links
# directly against ZeroTier software without disclosing the source code
# of your own application.
#
# 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
@@ -27,8 +52,7 @@ PICO_LIB = $(PICO_DIR)/build/lib/$(PICO_LIB_NAME)
SHARED_JNI_LIB = $(BUILD)/$(JNI_LIB_NAME)
#
TEST_BUILD_DIR = $(BUILD)/test
UNIT_TEST_SRC_DIR = test/unit
DUMB_TEST_SRC_DIR = test/dumb
UNIT_TEST_SRC_DIR = test
##############################################################################
## General Configuration ##
@@ -136,7 +160,7 @@ PICO_OBJS+= ext/picotcp/build/lib/pico_device.o \
all:
tests: dumb_tests unit_tests
tests: unit_tests
##############################################################################
## User-Space Stack ##
@@ -177,20 +201,6 @@ $(TEST_BUILD_DIR)/%: $(UNIT_TEST_SRC_DIR)/%.cpp
unit_tests: $(UNIT_TEST_OBJ_FILES)
##############################################################################
## Non-ZT Client/Server Tests ##
##############################################################################
DUMB_TEST_SRC_FILES := $(wildcard $(DUMB_TEST_SRC_DIR)/*.cpp)
DUMB_TEST_OBJ_FILES := $(addprefix $(TEST_BUILD_DIR)/,$(notdir $(DUMB_TEST_SRC_FILES:.cpp=)))
$(TEST_BUILD_DIR)/%: $(DUMB_TEST_SRC_DIR)/%.cpp
@mkdir -p $(TEST_BUILD_DIR)
@-$(CC) -o $@ $<
@-./check.sh $@
dumb_tests: $(DUMB_TEST_OBJ_FILES)
##############################################################################
## Misc ##
##############################################################################
+27 -17
View File
@@ -1,3 +1,28 @@
#
# ZeroTier SDK - Network Virtualization Everywhere
# Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# --
#
# You can be released from the requirements of the license by purchasing
# a commercial license. Buying such a license is mandatory as soon as you
# develop commercial closed-source software that incorporates or links
# directly against ZeroTier software without disclosing the source code
# of your own application.
#
# 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
@@ -27,8 +52,7 @@ PICO_LIB = $(PICO_DIR)/build/lib/$(PICO_LIB_NAME)
SHARED_JNI_LIB = $(BUILD)/$(JNI_LIB_NAME)
#
TEST_BUILD_DIR = $(BUILD)/test
UNIT_TEST_SRC_DIR = test/unit
DUMB_TEST_SRC_DIR = test/dumb
UNIT_TEST_SRC_DIR = test
##############################################################################
## General Configuration ##
@@ -135,7 +159,7 @@ PICO_OBJS+= ext/picotcp/build/lib/pico_device.o \
all:
tests: dumb_tests unit_tests
tests: unit_tests
##############################################################################
## User-Space Stack ##
@@ -176,20 +200,6 @@ $(TEST_BUILD_DIR)/%: $(UNIT_TEST_SRC_DIR)/%.cpp
unit_tests: $(UNIT_TEST_OBJ_FILES)
##############################################################################
## Non-ZT Client/Server Tests ##
##############################################################################
DUMB_TEST_SRC_FILES := $(wildcard $(DUMB_TEST_SRC_DIR)/*.cpp)
DUMB_TEST_OBJ_FILES := $(addprefix $(TEST_BUILD_DIR)/,$(notdir $(DUMB_TEST_SRC_FILES:.cpp=)))
$(TEST_BUILD_DIR)/%: $(DUMB_TEST_SRC_DIR)/%.cpp
@mkdir -p $(TEST_BUILD_DIR)
@-$(CC) -o $@ $<
@-./check.sh $@
dumb_tests: $(DUMB_TEST_OBJ_FILES)
##############################################################################
## Misc ##
##############################################################################
+13 -2
View File
@@ -98,7 +98,7 @@ namespace ZeroTier {
return _enabled;
}
bool SocketTap::addIp(const InetAddress &ip)
bool SocketTap::registerIpWithStack(const InetAddress &ip)
{
if(picostack) {
if(ip.isV4())
@@ -123,6 +123,17 @@ namespace ZeroTier {
return false;
}
bool SocketTap::addIp(const InetAddress &ip)
{
if(registerIpWithStack(ip))
{
// only start the stack if we successfully registered and initialized a device to
// the given address
return true;
}
return false;
}
bool SocketTap::removeIp(const InetAddress &ip)
{
DEBUG_INFO();
@@ -269,11 +280,11 @@ namespace ZeroTier {
DEBUG_ERROR("invalid connection");
return;
}
picostack->pico_Close(conn);
if(!conn->sock) {
DEBUG_EXTRA("invalid PhySocket");
return;
}
picostack->pico_Close(conn);
// Here we assume _tcpconns_m is already locked by caller
// FIXME: is this assumption still valid
if(conn->sock)
+6
View File
@@ -81,8 +81,14 @@ namespace ZeroTier {
void setEnabled(bool en);
bool enabled() const;
/*
* Registers a device with the given address
*/
bool registerIpWithStack(const InetAddress &ip);
/*
* Adds an address to the userspace stack interface associated with this SocketTap
* - Starts SocketTap main thread ONLY if successful
*/
bool addIp(const InetAddress &ip);
+249 -104
View File
File diff suppressed because it is too large Load Diff
+26 -30
View File
@@ -81,6 +81,10 @@ struct pico_socket * pico_socket_accept(PICO_SOCKET_ACCEPT_SIG);
namespace ZeroTier {
// FIXME: Determine why stack interrupt code fails when picodev is a mmember of a SocketTap
struct pico_device picodev;
bool picoTCP::pico_init_interface(SocketTap *tap, const InetAddress &ip)
{
DEBUG_INFO();
@@ -89,22 +93,22 @@ namespace ZeroTier {
std::sort(tap->_ips.begin(),tap->_ips.end());
if(ip.isV4())
{
tap->picodev = new struct pico_device;
//tap->picodev = new struct pico_device;
struct pico_ip4 ipaddr, netmask;
ipaddr.addr = *((uint32_t *)ip.rawIpData());
netmask.addr = *((uint32_t *)ip.netmask().rawIpData());
tap->picodev->send = pico_eth_send; // tx
tap->picodev->poll = pico_eth_poll; // rx
tap->picodev->mtu = tap->_mtu;
tap->picodev->tap = tap;
picodev.send = pico_eth_send; // tx
picodev.poll = pico_eth_poll; // rx
picodev.mtu = tap->_mtu;
picodev.tap = tap;
uint8_t mac[PICO_SIZE_ETH];
tap->_mac.copyTo(mac, PICO_SIZE_ETH);
if(pico_device_init(tap->picodev, "p4", mac) != 0) {
if(pico_device_init(&picodev, "p4", mac) != 0) {
DEBUG_ERROR("dev init failed");
delete tap->picodev;
delete &picodev;
return false;
}
pico_ipv4_link_add(tap->picodev, ipaddr, netmask);
pico_ipv4_link_add(&picodev, ipaddr, netmask);
DEBUG_INFO("addr = %s", ip.toString().c_str());
return true;
}
@@ -114,22 +118,22 @@ namespace ZeroTier {
inet_ntop(AF_INET6, ip.rawIpData(), ipv6_str, INET6_ADDRSTRLEN);
inet_ntop(AF_INET6, ip.netmask().rawIpData(), nm_str, INET6_ADDRSTRLEN);
tap->picodev6 = new struct pico_device;
//tap->picodev6 = new struct pico_device;
struct pico_ip6 ipaddr, netmask;
pico_string_to_ipv6(ipv6_str, ipaddr.addr);
pico_string_to_ipv6(nm_str, netmask.addr);
tap->picodev6->send = pico_eth_send; // tx
tap->picodev6->poll = pico_eth_poll; // rx
tap->picodev6->mtu = tap->_mtu;
tap->picodev6->tap = tap;
picodev.send = pico_eth_send; // tx
picodev.poll = pico_eth_poll; // rx
picodev.mtu = tap->_mtu;
picodev.tap = tap;
uint8_t mac[PICO_SIZE_ETH];
tap->_mac.copyTo(mac, PICO_SIZE_ETH);
if(pico_device_init(tap->picodev6, "p6", mac) != 0) {
if(pico_device_init(&picodev, "p6", mac) != 0) {
DEBUG_ERROR("dev init failed");
delete tap->picodev6;
delete &picodev;
return false;
}
pico_ipv6_link_add(tap->picodev6, ipaddr, netmask);
pico_ipv6_link_add(&picodev, ipaddr, netmask);
DEBUG_INFO("addr6 = %s", ip.toString().c_str());
return true;
}
@@ -141,7 +145,7 @@ namespace ZeroTier {
{
while(tap->_run)
{
tap->_phy.poll(ZT_PHY_POLL_INTERVAL); // in ms
tap->_phy.poll(ZT_PHY_POLL_INTERVAL);
pico_stack_tick();
}
}
@@ -162,7 +166,6 @@ namespace ZeroTier {
if(avail) {
r = pico_socket_recvfrom(s, conn->rxbuf + (conn->rxsz), ZT_SDK_MTU,
(void *)&peer.ip4.addr, &port);
//tap->_phy.setNotifyWritable(conn->sock, true);
if (r > 0)
conn->rxsz += r;
picostack->pico_Read(tap, conn->sock, conn, true);
@@ -312,16 +315,6 @@ namespace ZeroTier {
conn->_AcceptedConnections.push(newConn);
// For I/O loop participation and referencing the PhySocket's parent Connection in callbacks
newConn->sock = tap->_phy.wrapSocket(newConn->sdk_fd, newConn);
/*
DEBUG_INFO("wrapping newConn->sdk_fd = %d", newConn->sdk_fd);
DEBUG_INFO(" newConn->app_fd = %d", newConn->app_fd);
DEBUG_INFO(" newConn->sock = %p", newConn->sock);
DEBUG_INFO(" conn = %p", conn);
DEBUG_INFO(" newConn = %p", newConn);
DEBUG_INFO(" oldConn->sock = %p", conn->sock);
*/
}
if(conn->state != ZT_SOCK_STATE_LISTENING) {
// set state so socket multiplexer logic will pick this up
@@ -656,6 +649,10 @@ namespace ZeroTier {
void picoTCP::pico_Write(Connection *conn, void *data, ssize_t len)
{
if(!conn) {
DEBUG_ERROR("invalid connection");
return;
}
unsigned char *buf = (unsigned char*)data;
memcpy(conn->txbuf + conn->txsz, buf, len);
conn->txsz += len;
@@ -699,8 +696,7 @@ namespace ZeroTier {
if((err = pico_socket_close(conn->picosock)) < 0) {
errno = pico_err;
DEBUG_ERROR("error closing pico_socket(%p)", (void*)(conn->picosock));
}
return err;
}
return -1;
}
}

Some files were not shown because too many files have changed in this diff Show More