26 changed files with 3535 additions and 1184 deletions
@ -0,0 +1,288 @@
|
||||
/*
|
||||
* ZeroTier SDK - Network Virtualization Everywhere |
||||
* Copyright (C) 2011-2019 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. |
||||
*/ |
||||
|
||||
/**
|
||||
* @file |
||||
* |
||||
* Useful constants |
||||
*/ |
||||
|
||||
#ifndef LIBZT_CONSTANTS_HPP |
||||
#define LIBZT_CONSTANTS_HPP |
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Error codes returned by libzt API //
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
typedef int zts_err_t; |
||||
|
||||
#define ZTS_ERR_OK 0 |
||||
#define ZTS_ERR_INVALID_ARG -1 // A parameter provided by the user application is invalid (e.g. our of range, NULL, etc)
|
||||
#define ZTS_ERR_SERVICE -2 // The service isn't initialized or is for some other reason currently unavailable
|
||||
#define ZTS_ERR_INVALID_OP -3 // For some reason this API operation is not permitted (perhaps the service is still starting?)
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// libzt config //
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Default port that libzt will use to support all virtual communication |
||||
*/ |
||||
#define ZTS_DEFAULT_PORT 9994 |
||||
|
||||
/**
|
||||
* Maximum port number allowed |
||||
*/ |
||||
#define ZTS_MAX_PORT 65535 |
||||
|
||||
/**
|
||||
* For layer-2 only (this will omit all user-space network stack code) |
||||
*/ |
||||
#define ZTS_NO_STACK 0 |
||||
|
||||
/**
|
||||
* How fast service states are re-checked (in milliseconds) |
||||
*/ |
||||
#define ZTS_WRAPPER_CHECK_INTERVAL 50 |
||||
|
||||
/**
|
||||
* By how much thread I/O and callback loop delays are multiplied (unitless) |
||||
*/ |
||||
#define ZTS_HIBERNATION_MULTIPLIER 50 |
||||
|
||||
/**
|
||||
* Maximum allowed number of networks joined to concurrently |
||||
*/ |
||||
#define ZTS_MAX_JOINED_NETWORKS 64 |
||||
|
||||
/**
|
||||
* Maximum address assignments per network |
||||
*/ |
||||
#define ZTS_MAX_ASSIGNED_ADDRESSES 16 |
||||
|
||||
/**
|
||||
* Maximum routes per network |
||||
*/ |
||||
#define ZTS_MAX_NETWORK_ROUTES 32 |
||||
|
||||
/**
|
||||
* Length of buffer required to hold a ztAddress/nodeID |
||||
*/ |
||||
#define ZTS_ID_LEN 16 |
||||
|
||||
/**
|
||||
* Polling interval (in ms) for file descriptors wrapped in the Phy I/O loop (for raw drivers only) |
||||
*/ |
||||
#define ZTS_PHY_POLL_INTERVAL 1 |
||||
|
||||
/**
|
||||
* Maximum length of libzt/ZeroTier home path (where keys, and config files are stored) |
||||
*/ |
||||
#define ZTS_HOME_PATH_MAX_LEN 256 |
||||
|
||||
/**
|
||||
* Length of human-readable MAC address string |
||||
*/ |
||||
#define ZTS_MAC_ADDRSTRLEN 18 |
||||
|
||||
/**
|
||||
* Interval (in ms) for performing background tasks |
||||
*/ |
||||
#define ZTS_HOUSEKEEPING_INTERVAL 1000 |
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// lwIP driver config //
|
||||
// For more LWIP configuration options see: include/lwipopts.h //
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/*
|
||||
* The following three quantities are related and govern how incoming frames are fed into the
|
||||
* network stack's core: |
||||
|
||||
* Every LWIP_GUARDED_BUF_CHECK_INTERVAL milliseconds, a callback will be called from the core and
|
||||
* will input a maximum of LWIP_FRAMES_HANDLED_PER_CORE_CALL frames before returning control back |
||||
* to the core. Meanwhile, incoming frames from the ZeroTier wire will be allocated and their
|
||||
* pointers will be cached in the receive frame buffer of the size LWIP_MAX_GUARDED_RX_BUF_SZ to
|
||||
* await the next callback from the core |
||||
*/ |
||||
|
||||
#define LWIP_GUARDED_BUF_CHECK_INTERVAL 5 // in ms
|
||||
#define LWIP_MAX_GUARDED_RX_BUF_SZ 1024 // number of frame pointers that can be cached waiting for receipt into core
|
||||
#define LWIP_FRAMES_HANDLED_PER_CORE_CALL 16 // How many frames are handled per call from core
|
||||
|
||||
typedef signed char err_t; |
||||
|
||||
#define ND6_DISCOVERY_INTERVAL 1000 |
||||
#define ARP_DISCOVERY_INTERVAL ARP_TMR_INTERVAL |
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Subset of: ZeroTierOne.h and Constants.hpp //
|
||||
// We redefine a few ZT structures here so that we don't need to drag the //
|
||||
// entire ZeroTierOne.h file into the user application //
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Maximum MTU size for ZeroTier |
||||
*/ |
||||
#define ZT_MAX_MTU 10000 |
||||
|
||||
/**
|
||||
* Maximum number of direct network paths to a given peer |
||||
*/ |
||||
#define ZT_MAX_PEER_NETWORK_PATHS 16 |
||||
|
||||
//
|
||||
// This include file also auto-detects and canonicalizes some environment
|
||||
// information defines:
|
||||
//
|
||||
// __LINUX__
|
||||
// __APPLE__
|
||||
// __BSD__ (OSX also defines this)
|
||||
// __UNIX_LIKE__ (Linux, BSD, etc.)
|
||||
// __WINDOWS__
|
||||
//
|
||||
// Also makes sure __BYTE_ORDER is defined reasonably.
|
||||
//
|
||||
|
||||
// Hack: make sure __GCC__ is defined on old GCC compilers
|
||||
#ifndef __GCC__ |
||||
#if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1) || defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2) || defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) |
||||
#define __GCC__ |
||||
#endif |
||||
#endif |
||||
|
||||
#if defined(__linux__) || defined(linux) || defined(__LINUX__) || defined(__linux) |
||||
#ifndef __LINUX__ |
||||
#define __LINUX__ |
||||
#endif |
||||
#ifndef __UNIX_LIKE__ |
||||
#define __UNIX_LIKE__ |
||||
#endif |
||||
#include <endian.h> |
||||
#endif |
||||
|
||||
#ifdef __APPLE__ |
||||
#define likely(x) __builtin_expect((x),1) |
||||
#define unlikely(x) __builtin_expect((x),0) |
||||
#include <TargetConditionals.h> |
||||
#ifndef __UNIX_LIKE__ |
||||
#define __UNIX_LIKE__ |
||||
#endif |
||||
#ifndef __BSD__ |
||||
#define __BSD__ |
||||
#endif |
||||
#include <machine/endian.h> |
||||
#endif |
||||
|
||||
// Defined this macro to disable "type punning" on a number of targets that
|
||||
// have issues with unaligned memory access.
|
||||
#if defined(__arm__) || defined(__ARMEL__) || (defined(__APPLE__) && ( (defined(TARGET_OS_IPHONE) && (TARGET_OS_IPHONE != 0)) || (defined(TARGET_OS_WATCH) && (TARGET_OS_WATCH != 0)) || (defined(TARGET_IPHONE_SIMULATOR) && (TARGET_IPHONE_SIMULATOR != 0)) ) ) |
||||
#ifndef ZT_NO_TYPE_PUNNING |
||||
#define ZT_NO_TYPE_PUNNING |
||||
#endif |
||||
#endif |
||||
|
||||
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) |
||||
#ifndef __UNIX_LIKE__ |
||||
#define __UNIX_LIKE__ |
||||
#endif |
||||
#ifndef __BSD__ |
||||
#define __BSD__ |
||||
#endif |
||||
#include <machine/endian.h> |
||||
#ifndef __BYTE_ORDER |
||||
#define __BYTE_ORDER _BYTE_ORDER |
||||
#define __LITTLE_ENDIAN _LITTLE_ENDIAN |
||||
#define __BIG_ENDIAN _BIG_ENDIAN |
||||
#endif |
||||
#endif |
||||
|
||||
#if defined(_WIN32) || defined(_WIN64) |
||||
#ifndef __WINDOWS__ |
||||
#define __WINDOWS__ |
||||
#endif |
||||
#ifndef NOMINMAX |
||||
#define NOMINMAX |
||||
#endif |
||||
#pragma warning(disable : 4290) |
||||
#pragma warning(disable : 4996) |
||||
#pragma warning(disable : 4101) |
||||
#undef __UNIX_LIKE__ |
||||
#undef __BSD__ |
||||
#define ZT_PATH_SEPARATOR '\\' |
||||
#define ZT_PATH_SEPARATOR_S "\\" |
||||
#define ZT_EOL_S "\r\n" |
||||
#include <WinSock2.h> |
||||
#include <Windows.h> |
||||
#endif |
||||
|
||||
// Assume little endian if not defined
|
||||
#if (defined(__APPLE__) || defined(__WINDOWS__)) && (!defined(__BYTE_ORDER)) |
||||
#undef __BYTE_ORDER |
||||
#undef __LITTLE_ENDIAN |
||||
#undef __BIG_ENDIAN |
||||
#define __BIG_ENDIAN 4321 |
||||
#define __LITTLE_ENDIAN 1234 |
||||
#define __BYTE_ORDER 1234 |
||||
#endif |
||||
|
||||
#ifdef __UNIX_LIKE__ |
||||
#define ZT_PATH_SEPARATOR '/' |
||||
#define ZT_PATH_SEPARATOR_S "/" |
||||
#define ZT_EOL_S "\n" |
||||
#endif |
||||
|
||||
#ifndef __BYTE_ORDER |
||||
#include <endian.h> |
||||
#endif |
||||
|
||||
#ifdef __NetBSD__ |
||||
#define RTF_MULTICAST 0x20000000 |
||||
#endif |
||||
|
||||
#if (defined(__GNUC__) && (__GNUC__ >= 3)) || (defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 800)) || defined(__clang__) |
||||
#ifndef likely |
||||
#define likely(x) __builtin_expect((x),1) |
||||
#endif |
||||
#ifndef unlikely |
||||
#define unlikely(x) __builtin_expect((x),0) |
||||
#endif |
||||
#else |
||||
#ifndef likely |
||||
#define likely(x) (x) |
||||
#endif |
||||
#ifndef unlikely |
||||
#define unlikely(x) (x) |
||||
#endif |
||||
#endif |
||||
|
||||
#ifdef __WINDOWS__ |
||||
#define ZT_PACKED_STRUCT(D) __pragma(pack(push,1)) D __pragma(pack(pop)) |
||||
#else |
||||
#define ZT_PACKED_STRUCT(D) D __attribute__((packed)) |
||||
#endif |
||||
|
||||
#endif // _H
|
||||
@ -0,0 +1,211 @@
|
||||
/*
|
||||
* ZeroTier SDK - Network Virtualization Everywhere |
||||
* Copyright (C) 2011-2019 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. |
||||
*/ |
||||
|
||||
/**
|
||||
* @file |
||||
* |
||||
* Management of virtual tap interfaces |
||||
*/ |
||||
|
||||
#ifndef LIBZT_DEFS_HPP |
||||
#define LIBZT_DEFS_HPP |
||||
|
||||
#include "Constants.hpp" |
||||
|
||||
#ifndef _USING_LWIP_DEFINITIONS_ |
||||
#include <sys/socket.h> |
||||
#endif |
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Subset of: ZeroTierOne.h //
|
||||
// We redefine a few ZT structures here so that we don't need to drag the //
|
||||
// entire ZeroTierOne.h file into the user application //
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* What trust hierarchy role does this peer have? |
||||
*/ |
||||
enum zts_peer_role |
||||
{ |
||||
ZTS_PEER_ROLE_LEAF = 0, // ordinary node
|
||||
ZTS_PEER_ROLE_MOON = 1, // moon root
|
||||
ZTS_PEER_ROLE_PLANET = 2 // planetary root
|
||||
}; |
||||
|
||||
/**
|
||||
* A structure used to represent a virtual network route |
||||
*/ |
||||
struct zts_virtual_network_route |
||||
{ |
||||
/**
|
||||
* Target network / netmask bits (in port field) or NULL or 0.0.0.0/0 for default |
||||
*/ |
||||
struct sockaddr_storage target; |
||||
|
||||
/**
|
||||
* Gateway IP address (port ignored) or NULL (family == 0) for LAN-local (no gateway) |
||||
*/ |
||||
struct sockaddr_storage via; |
||||
|
||||
/**
|
||||
* Route flags |
||||
*/ |
||||
uint16_t flags; |
||||
|
||||
/**
|
||||
* Route metric (not currently used) |
||||
*/ |
||||
uint16_t metric; |
||||
}; |
||||
|
||||
/**
|
||||
* A structure used to convey network-specific details to the user application |
||||
*/ |
||||
struct zts_network_details |
||||
{ |
||||
/**
|
||||
* Network ID |
||||
*/ |
||||
uint64_t nwid; |
||||
|
||||
/**
|
||||
* Maximum Transmission Unit size for this network |
||||
*/ |
||||
int mtu; |
||||
|
||||
/**
|
||||
* Number of addresses (actually) assigned to the node on this network |
||||
*/ |
||||
short num_addresses; |
||||
|
||||
/**
|
||||
* Array of IPv4 and IPv6 addresses assigned to the node on this network |
||||
*/ |
||||
struct sockaddr_storage addr[ZTS_MAX_ASSIGNED_ADDRESSES]; |
||||
|
||||
/**
|
||||
* Number of routes |
||||
*/ |
||||
unsigned int num_routes; |
||||
|
||||
/**
|
||||
* Array of IPv4 and IPv6 addresses assigned to the node on this network |
||||
*/ |
||||
struct zts_virtual_network_route routes[ZTS_MAX_NETWORK_ROUTES]; |
||||
}; |
||||
|
||||
/**
|
||||
* Physical network path to a peer |
||||
*/ |
||||
struct zts_physical_path |
||||
{ |
||||
/**
|
||||
* Address of endpoint |
||||
*/ |
||||
struct sockaddr_storage address; |
||||
|
||||
/**
|
||||
* Time of last send in milliseconds or 0 for never |
||||
*/ |
||||
uint64_t lastSend; |
||||
|
||||
/**
|
||||
* Time of last receive in milliseconds or 0 for never |
||||
*/ |
||||
uint64_t lastReceive; |
||||
|
||||
/**
|
||||
* Is this a trusted path? If so this will be its nonzero ID. |
||||
*/ |
||||
uint64_t trustedPathId; |
||||
|
||||
/**
|
||||
* Is path expired? |
||||
*/ |
||||
int expired; |
||||
|
||||
/**
|
||||
* Is path preferred? |
||||
*/ |
||||
int preferred; |
||||
}; |
||||
|
||||
/**
|
||||
* Peer status result buffer |
||||
*/ |
||||
struct zts_peer_details |
||||
{ |
||||
/**
|
||||
* ZeroTier address (40 bits) |
||||
*/ |
||||
uint64_t address; |
||||
|
||||
/**
|
||||
* Remote major version or -1 if not known |
||||
*/ |
||||
int versionMajor; |
||||
|
||||
/**
|
||||
* Remote minor version or -1 if not known |
||||
*/ |
||||
int versionMinor; |
||||
|
||||
/**
|
||||
* Remote revision or -1 if not known |
||||
*/ |
||||
int versionRev; |
||||
|
||||
/**
|
||||
* Last measured latency in milliseconds or -1 if unknown |
||||
*/ |
||||
int latency; |
||||
|
||||
/**
|
||||
* What trust hierarchy role does this device have? |
||||
*/ |
||||
enum zts_peer_role role; |
||||
|
||||
/**
|
||||
* Number of paths (size of paths[]) |
||||
*/ |
||||
unsigned int pathCount; |
||||
|
||||
/**
|
||||
* Known network paths to peer |
||||
*/ |
||||
zts_physical_path paths[ZT_MAX_PEER_NETWORK_PATHS]; |
||||
}; |
||||
|
||||
/**
|
||||
* List of peers |
||||
*/ |
||||
struct zts_peer_list |
||||
{ |
||||
zts_peer_details *peers; |
||||
unsigned long peerCount; |
||||
}; |
||||
|
||||
#endif // _H
|
||||
@ -0,0 +1,373 @@
|
||||
/*
|
||||
* ZeroTier SDK - Network Virtualization Everywhere |
||||
* Copyright (C) 2011-2019 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. |
||||
*/ |
||||
|
||||
/**
|
||||
* @file |
||||
* |
||||
* Header for ZeroTier service controls |
||||
*/ |
||||
|
||||
#ifndef LIBZT_SERVICE_CONTROLS_HPP |
||||
#define LIBZT_SERVICE_CONTROLS_HPP |
||||
|
||||
#ifdef _WIN32 |
||||
#ifdef ADD_EXPORTS |
||||
#define ZT_SOCKET_API __declspec(dllexport) |
||||
#else |
||||
#define ZT_SOCKET_API __declspec(dllimport) |
||||
#endif |
||||
#define ZTCALL __cdecl |
||||
#else |
||||
#define ZT_SOCKET_API |
||||
#define ZTCALL |
||||
#endif |
||||
|
||||
void api_sleep(int interval_ms); |
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// ZeroTier Service Controls //
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
/**
|
||||
* @brief (optional) Sets the port for the background libzt service. If this function is called |
||||
* with a port number between 1-65535 it will attempt to bind to that port. If it is called with |
||||
* a port number of 0 it will attempt to randomly search for an available port. If this function |
||||
* is never called, the service will try to bind on LIBZT_DEFAULT_PORT which is 9994. |
||||
* |
||||
* @usage Should be called at the beginning of your application before `zts_startjoin()` |
||||
* @param portno Port number |
||||
* @return 0 if successful; or -1 if failed |
||||
*/ |
||||
ZT_SOCKET_API int ZTCALL zts_set_service_port(int portno); |
||||
|
||||
/**
|
||||
* @brief (optional) Returns the port number used by the ZeroTier service |
||||
* @usage Can be called if a port number was previously assigned |
||||
* @return the port number used by the ZeroTier service |
||||
*/ |
||||
ZT_SOCKET_API int ZTCALL zts_get_service_port(); |
||||
|
||||
/**
|
||||
* @brief Starts the ZeroTier service |
||||
* |
||||
* @usage Should be called at the beginning of your application. Will blocks until all of the following conditions are met: |
||||
* - ZeroTier core service has been initialized |
||||
* - Cryptographic identity has been generated or loaded from directory specified by `path` |
||||
* - Virtual network is successfully joined |
||||
* - IP address is assigned by network controller service |
||||
* @param path path directory where cryptographic identities and network configuration files are stored and retrieved |
||||
* (`identity.public`, `identity.secret`) |
||||
* @param blocking whether or not this call will block until the entire service is up and running |
||||
* @return 0 if successful; or 1 if failed |
||||
*/ |
||||
ZT_SOCKET_API int ZTCALL zts_start(const char *path, int blocking); |
||||
|
||||
/**
|
||||
* @brief Starts the ZeroTier service |
||||
* |
||||
* @usage Should be called at the beginning of your application. Will blocks until all of the following conditions are met: |
||||
* - ZeroTier core service has been initialized |
||||
* - Cryptographic identity has been generated or loaded from directory specified by `path` |
||||
* - Virtual network is successfully joined |
||||
* - IP address is assigned by network controller service |
||||
* @param path path directory where cryptographic identities and network configuration files are stored and retrieved |
||||
* (`identity.public`, `identity.secret`) |
||||
* @param nwid A 16-digit hexidecimal network identifier (e.g. Earth: `8056c2e21c000001`) |
||||
* @return 0 if successful; or 1 if failed |
||||
*/ |
||||
ZT_SOCKET_API int ZTCALL zts_startjoin(const char *path, const uint64_t nwid); |
||||
|
||||
/**
|
||||
* @brief Stops the ZeroTier service, brings down all virtual interfaces in order to stop all traffic processing. |
||||
* |
||||
* @usage This should be called when the application anticipates not needing any sort of traffic processing for a |
||||
* prolonged period of time. The stack driver (with associated timers) will remain active in case future traffic |
||||
* processing is required. Note that the application must tolerate a multi-second startup time if zts_start() |
||||
* zts_startjoin() is called again. To stop this background thread and free all resources use zts_free() instead. |
||||
* @param blocking whether or not this call will block until the entire service is torn down |
||||
* @return Returns 0 on success, -1 on failure |
||||
*/ |
||||
ZT_SOCKET_API int ZTCALL zts_stop(int blocking = 1); |
||||
|
||||
/**
|
||||
* @brief Stops all background services, brings down all interfaces, frees all resources. After calling this function |
||||
* an application restart will be required before the library can be used again. This is a blocking call. |
||||
* |
||||
* @usage This should be called at the end of your program or when you do not anticipate communicating over ZeroTier |
||||
* @return Returns 0 on success, -1 on failure |
||||
*/ |
||||
ZT_SOCKET_API int ZTCALL zts_free(); |
||||
|
||||
/**
|
||||
* @brief Return whether the ZeroTier service is currently running |
||||
* |
||||
* @usage Call this after zts_start() |
||||
* @return 1 if running, 0 if not running |
||||
*/ |
||||
ZT_SOCKET_API int ZTCALL zts_core_running(); |
||||
|
||||
/**
|
||||
* @brief Return whether libzt is ready to handle socket API calls. Alternatively you could
|
||||
* have just called zts_startjoin(path, nwid) |
||||
* |
||||
* @usage Call this after zts_start() |
||||
* @return 1 if running, 0 if not running |
||||
*/ |
||||
ZT_SOCKET_API int ZTCALL zts_ready(); |
||||
|
||||
/**
|
||||
* @brief Return the number of networks currently joined by this node |
||||
* |
||||
* @usage Call this after zts_start(), zts_startjoin() and/or zts_join() |
||||
* @return Number of networks joined by this node |
||||
*/ |
||||
ZT_SOCKET_API zts_err_t ZTCALL zts_get_num_joined_networks(); |
||||
|
||||
/**
|
||||
* @brief Populates a structure with details for a given network |
||||
* |
||||
* @usage Call this from the application thread any time after the node has joined a network |
||||
* @param nwid A 16-digit hexidecimal virtual network ID |
||||
* @param nd Pointer to a zts_network_details structure to populate |
||||
* @return ZTS_ERR_SERVICE if failed, 0 if otherwise |
||||
*/ |
||||
ZT_SOCKET_API zts_err_t ZTCALL zts_get_network_details(uint64_t nwid, struct zts_network_details *nd); |
||||
|
||||
/**
|
||||
* @brief Populates an array of structures with details for any given number of networks |
||||
* |
||||
* @usage Call this from the application thread any time after the node has joined a network |
||||
* @param nds Pointer to an array of zts_network_details structures to populate |
||||
* @param num Number of zts_network_details structures available to copy data into, will be updated |
||||
* to reflect number of structures that were actually populated |
||||
* @return ZTS_ERR_SERVICE if failed, 0 if otherwise |
||||
*/ |
||||
ZT_SOCKET_API zts_err_t ZTCALL zts_get_all_network_details(struct zts_network_details *nds, int *num); |
||||
|
||||
/**
|
||||
* @brief Join a network |
||||
* |
||||
* @usage Call this from application thread. Only after zts_start() has succeeded |
||||
* @param nwid A 16-digit hexidecimal virtual network ID |
||||
* @return 0 if successful, -1 for any failure |
||||
*/ |
||||
ZT_SOCKET_API zts_err_t ZTCALL zts_join(const uint64_t nwid, int blocking = 1); |
||||
|
||||
/**
|
||||
* @brief Leave a network |
||||
* |
||||
* @usage Call this from application thread. Only after zts_start() has succeeded |
||||
* @param nwid A 16-digit hexidecimal virtual network ID |
||||
* @return 0 if successful, -1 for any failure |
||||
*/ |
||||
ZT_SOCKET_API zts_err_t ZTCALL zts_leave(const uint64_t nwid, int blocking = 1); |
||||
|
||||
|
||||
/**
|
||||
* @brief Leaves all networks |
||||
* |
||||
* @usage Call this from application thread. Only after zts_start() has succeeded |
||||
* @param nwid A 16-digit hexidecimal virtual network ID |
||||
* @return 0 if successful, -1 for any failure |
||||
*/ |
||||
ZT_SOCKET_API zts_err_t ZTCALL zts_leave_all(int blocking = 1); |
||||
|
||||
/**
|
||||
* @brief Orbits a given moon (user-defined root server) |
||||
* |
||||
* @usage Call this from application thread. Only after zts_start() has succeeded |
||||
* @param moonWorldId A 16-digit hexidecimal world ID |
||||
* @param moonSeed A 16-digit hexidecimal seed ID |
||||
* @return ZTS_ERR_OK if successful, ZTS_ERR_SERVICE, ZTS_ERR_INVALID_ARG, ZTS_ERR_INVALID_OP if otherwise |
||||
*/ |
||||
ZT_SOCKET_API zts_err_t ZTCALL zts_orbit(uint64_t moonWorldId, uint64_t moonSeed); |
||||
|
||||
/**
|
||||
* @brief De-orbits a given moon (user-defined root server) |
||||
* |
||||
* @usage Call this from application thread. Only after zts_start() has succeeded |
||||
* @param moonWorldId A 16-digit hexidecimal world ID |
||||
* @return ZTS_ERR_OK if successful, ZTS_ERR_SERVICE, ZTS_ERR_INVALID_ARG, ZTS_ERR_INVALID_OP if otherwise |
||||
*/ |
||||
ZT_SOCKET_API zts_err_t ZTCALL zts_deorbit(uint64_t moonWorldId); |
||||
|
||||
/**
|
||||
* @brief Copies the configuration path used by ZeroTier into the provided buffer |
||||
* |
||||
* @usage Use this to determine where ZeroTier is storing identity files |
||||
* @param homePath Path to ZeroTier configuration files |
||||
* @param len Length of destination buffer |
||||
* @return 0 if no error, -1 if invalid argument was supplied |
||||
*/ |
||||
ZT_SOCKET_API zts_err_t ZTCALL zts_get_path(char *homePath, size_t *len); |
||||
|
||||
/**
|
||||
* @brief Returns the node ID of this instance |
||||
* |
||||
* @usage Call this after zts_start() and/or when zts_running() returns true |
||||
* @return |
||||
*/ |
||||
ZT_SOCKET_API uint64_t ZTCALL zts_get_node_id(); |
||||
|
||||
/**
|
||||
* @brief Returns whether any address has been assigned to the SockTap for this network |
||||
* |
||||
* @usage This is used as an indicator of readiness for service for the ZeroTier core and stack |
||||
* @param nwid Network ID |
||||
* @return |
||||
*/ |
||||
ZT_SOCKET_API int ZTCALL zts_has_address(const uint64_t nwid); |
||||
|
||||
|
||||
/**
|
||||
* @brief Returns the number of addresses assigned to this node for the given nwid |
||||
* |
||||
* @param nwid Network ID |
||||
* @return The number of addresses assigned |
||||
*/ |
||||
ZT_SOCKET_API int ZTCALL zts_get_num_assigned_addresses(const uint64_t nwid); |
||||
|
||||
/**
|
||||
* @brief Returns the assigned address located at the given index |
||||
* |
||||
* @usage The indices of each assigned address are not guaranteed and should only |
||||
* be used for iterative purposes. |
||||
* @param nwid Network ID |
||||
* @param index location of assigned address |
||||
* @return The number of addresses assigned |
||||
*/ |
||||
ZT_SOCKET_API int ZTCALL zts_get_address_at_index( |
||||
const uint64_t nwid, const int index, struct sockaddr *addr, socklen_t *addrlen); |
||||
|
||||
/**
|
||||
* @brief Get IP address for this device on a given network |
||||
* |
||||
* @usage FIXME: Only returns first address found, good enough for most cases |
||||
* @param nwid Network ID |
||||
* @param addr Destination structure for address |
||||
* @param addrlen size of destination address buffer, will be changed to size of returned address |
||||
* @return 0 if an address was successfully found, -1 if failure |
||||
*/ |
||||
ZT_SOCKET_API int ZTCALL zts_get_address( |
||||
const uint64_t nwid, struct sockaddr_storage *addr, const int address_family); |
||||
|
||||
/**
|
||||
* @brief Computes a 6PLANE IPv6 address for the given Network ID and Node ID |
||||
* |
||||
* @usage Can call any time |
||||
* @param addr Destination structure for address |
||||
* @param nwid Network ID
|
||||
* @param nodeId Node ID |
||||
* @return |
||||
*/ |
||||
ZT_SOCKET_API void ZTCALL zts_get_6plane_addr( |
||||
struct sockaddr_storage *addr, const uint64_t nwid, const uint64_t nodeId); |
||||
|
||||
/**
|
||||
* @brief Computes a RFC4193 IPv6 address for the given Network ID and Node ID |
||||
* |
||||
* @usage Can call any time |
||||
* @param addr Destination structure for address |
||||
* @param nwid Network ID
|
||||
* @param nodeId Node ID |
||||
* @return |
||||
*/ |
||||
ZT_SOCKET_API void ZTCALL zts_get_rfc4193_addr( |
||||
struct sockaddr_storage *addr, const uint64_t nwid, const uint64_t nodeId); |
||||
|
||||
/**
|
||||
* @brief Return the number of peers |
||||
* |
||||
* @usage Call this after zts_start() has succeeded |
||||
* @return |
||||
*/ |
||||
ZT_SOCKET_API zts_err_t zts_get_peer_count(); |
||||
|
||||
ZT_SOCKET_API zts_err_t zts_get_peers(struct zts_peer_details *pds, int *num); |
||||
|
||||
/**
|
||||
* @brief Enables the HTTP backplane management system |
||||
* |
||||
* @usage Call this after zts_start() has succeeded |
||||
* @return ZTS_ERR_OK if successful, ZTS_ERR_SERVICE if otherwise |
||||
*/ |
||||
ZT_SOCKET_API zts_err_t zts_enable_http_backplane_mgmt(); |
||||
|
||||
/**
|
||||
* @brief Disables the HTTP backplane management system |
||||
* |
||||
* @usage Call this after zts_start() has succeeded |
||||
* @return ZTS_ERR_OK if successful, ZTS_ERR_SERVICE, ZTS_ERR_INVALID_OP if otherwise |
||||
*/ |
||||
ZT_SOCKET_API zts_err_t zts_disable_http_backplane_mgmt(); |
||||
|
||||
|
||||
/**
|
||||
* @brief Starts a ZeroTier service in the background |
||||
* |
||||
* @usage For internal use only. |
||||
* @param |
||||
* @return |
||||
*/ |
||||
#if defined(_WIN32) |
||||
DWORD WINAPI _zts_start_service(LPVOID thread_id); |
||||
#else |
||||
void *_zts_start_service(void *thread_id); |
||||
#endif |
||||
|
||||
/**
|
||||
* @brief [Should not be called from user application] This function must be surrounded by
|
||||
* ZT service locks. It will determine if it is currently safe and allowed to operate on
|
||||
* the service. |
||||
* @usage Can be called at any time |
||||
* @return 1 or 0 |
||||
*/ |
||||
int _zts_can_perform_service_operation(); |
||||
|
||||
/**
|
||||
* @brief [Should not be called from user application] Returns whether or not the node is
|
||||
* online. |
||||
* @usage Can be called at any time |
||||
* @return 1 or 0 |
||||
*/ |
||||
int _zts_node_online(); |
||||
|
||||
/**
|
||||
* @brief [Should not be called from user application] Adjusts the delay multiplier for the |
||||
* network stack driver thread. |
||||
* @usage Can be called at any time |
||||
*/ |
||||
void _hibernate_if_needed(); |
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
|
||||
#endif // _H
|
||||
@ -0,0 +1,140 @@
|
||||
/*
|
||||
* ZeroTier SDK - Network Virtualization Everywhere |
||||
* Copyright (C) 2011-2019 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. |
||||
*/ |
||||
|
||||
/**
|
||||
* @file |
||||
* |
||||
* Management of virtual tap interfaces |
||||
*/ |
||||
|
||||
#include "VirtualTap.hpp" |
||||
#include "OneService.hpp" |
||||
|
||||
namespace ZeroTier { |
||||
|
||||
extern std::vector<void*> vtaps; |
||||
extern Mutex _vtaps_lock; |
||||
|
||||
class VirtualTap; |
||||
|
||||
/**
|
||||
* @brief Static utility class for safely handling VirtualTap(s) |
||||
*/ |
||||
class VirtualTapManager |
||||
{ |
||||
public: |
||||
|
||||
static void add_tap(VirtualTap *tap) { |
||||
_vtaps_lock.lock(); |
||||
vtaps.push_back((void*)tap); |
||||
_vtaps_lock.unlock(); |
||||
} |
||||
|
||||
static VirtualTap *getTapByNWID(uint64_t nwid) |
||||
{ |
||||
_vtaps_lock.lock(); |
||||
VirtualTap *s, *tap = nullptr; |
||||
for (size_t i=0; i<vtaps.size(); i++) { |
||||
s = (VirtualTap*)vtaps[i]; |
||||
if (s->_nwid == nwid) { tap = s; } |
||||
} |
||||
_vtaps_lock.unlock(); |
||||
return tap; |
||||
} |
||||
|
||||
static size_t get_vtaps_size() { |
||||
size_t sz; |
||||
_vtaps_lock.lock(); |
||||
sz = vtaps.size(); |
||||
_vtaps_lock.unlock(); |
||||
return sz; |
||||
} |
||||
|
||||
// TODO: We shouldn't re-apply the reference to everything all the time
|
||||
static void update_service_references(void *serviceRef) { |
||||
_vtaps_lock.lock(); |
||||
for (size_t i=0;i<vtaps.size(); i++) { |
||||
VirtualTap *s = (VirtualTap*)vtaps[i]; |
||||
s->zt1ServiceRef=serviceRef; |
||||
} |
||||
_vtaps_lock.unlock(); |
||||
} |
||||
|
||||
static void remove_by_nwid(uint64_t nwid) { |
||||
_vtaps_lock.lock(); |
||||
for (size_t i=0;i<vtaps.size(); i++) { |
||||
VirtualTap *s = (VirtualTap*)vtaps[i]; |
||||
if (s->_nwid == nwid) { |
||||
vtaps.erase(vtaps.begin() + i); |
||||
} |
||||
} |
||||
_vtaps_lock.unlock(); |
||||
} |
||||
|
||||
static void clear() { |
||||
_vtaps_lock.lock(); |
||||
vtaps.clear(); |
||||
_vtaps_lock.unlock(); |
||||
} |
||||
|
||||
static void get_network_details(uint64_t nwid, struct zts_network_details *nd) |
||||
{ |
||||
VirtualTap *tap; |
||||
socklen_t addrlen; |
||||
_vtaps_lock.lock(); |
||||
for (size_t i=0; i<vtaps.size(); i++) { |
||||
tap = (VirtualTap*)vtaps[i]; |
||||
if (tap->_nwid == nwid) { |
||||
nd->nwid = tap->_nwid; |
||||
nd->mtu = tap->_mtu; |
||||
// assigned addresses
|
||||
nd->num_addresses = tap->_ips.size() < ZTS_MAX_ASSIGNED_ADDRESSES ? tap->_ips.size() : ZTS_MAX_ASSIGNED_ADDRESSES; |
||||
for (int j=0; j<nd->num_addresses; j++) { |
||||
addrlen = tap->_ips[j].isV4() ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6); |
||||
memcpy(&(nd->addr[j]), &(tap->_ips[j]), addrlen); |
||||
} |
||||
// routes
|
||||
nd->num_routes = ZTS_MAX_NETWORK_ROUTES; |
||||
OneService *zt1Service = (OneService*)tap->zt1ServiceRef; |
||||
zt1Service->getRoutes(nwid, (ZT_VirtualNetworkRoute*)&(nd->routes)[0], &(nd->num_routes)); |
||||
break; |
||||
} |
||||
} |
||||
_vtaps_lock.unlock(); |
||||
} |
||||
|
||||
static void get_all_network_details(struct zts_network_details *nds, int *num) |
||||
{ |
||||
VirtualTap *tap; |
||||
*num = get_vtaps_size(); |
||||
for (size_t i=0; i<vtaps.size(); i++) { |
||||
tap = (VirtualTap*)vtaps[i]; |
||||
get_network_details(tap->_nwid, &nds[i]); |
||||
} |
||||
} |
||||
}; |
||||
|
||||
} // namespace ZeroTier
|
||||
@ -1,307 +0,0 @@
|
||||
/*
|
||||
* ZeroTier SDK - Network Virtualization Everywhere |
||||
* Copyright (C) 2011-2018 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. |
||||
*/ |
||||
|
||||
/**
|
||||
* @file |
||||
* |
||||
* Application-facing, partially-POSIX-compliant socket API |
||||
*/ |
||||
|
||||
#ifndef LIBZT_DEFINES_H |
||||
#define LIBZT_DEFINES_H |
||||
|
||||
#define LIBZT_IPV4 1 |
||||
#define LIBZT_IPV6 1 |
||||
|
||||
/**
|
||||
* Default port that libzt will use to support all virtual communication |
||||
*/ |
||||
#define LIBZT_DEFAULT_PORT 9994 |
||||
|
||||
#define NO_STACK 0 // for layer-2 only (this will omit all userspace network stack code)
|
||||
|
||||
/**
|
||||
* Maximum MTU size for ZeroTier |
||||
*/ |
||||
#define ZT_MAX_MTU 10000 |
||||
|
||||
/**
|
||||
* How fast service states are re-checked (in milliseconds) |
||||
*/ |
||||
#define ZTO_WRAPPER_CHECK_INTERVAL 100 |
||||
|
||||
/**
|
||||
* Length of buffer required to hold a ztAddress/nodeID |
||||
*/ |
||||
#define ZTO_ID_LEN 16 |
||||
|
||||
#if defined(_MSC_VER) |
||||
#include <BaseTsd.h> |
||||
typedef SSIZE_T ssize_t; |
||||
#endif |
||||
|
||||
/****************************************************************************/ |
||||
/* For SOCK_RAW support, it will initially be modeled after linux's API, so */ |
||||
/* below are the various things we need to define in order to make this API */ |
||||
/* work on other platforms. Mayber later down the road we will customize */ |
||||
/* this for each different platform. Maybe. */ |
||||
/****************************************************************************/ |
||||
|
||||
#if !defined(__linux__) |
||||
#define SIOCGIFINDEX 101 |
||||
#define SIOCGIFHWADDR 102 |
||||
|
||||
// Normally defined in linux/if_packet.h, defined here so we can offer a linux-like
|
||||
// raw socket API on non-linux platforms
|
||||
struct sockaddr_ll { |
||||
unsigned short sll_family; /* Always AF_PACKET */ |
||||
unsigned short sll_protocol; /* Physical layer protocol */ |
||||
int sll_ifindex; /* Interface number */ |
||||
unsigned short sll_hatype; /* ARP hardware type */ |
||||
unsigned char sll_pkttype; /* Packet type */ |
||||
unsigned char sll_halen; /* Length of address */ |
||||
unsigned char sll_addr[8]; /* Physical layer address */ |
||||
}; |
||||
|
||||
#endif |
||||
|
||||
/****************************************************************************/ |
||||
/* lwIP */ |
||||
/****************************************************************************/ |
||||
|
||||
// For LWIP configuration see: include/lwipopts.h
|
||||
|
||||
/* The following three quantities are related and govern how incoming frames are fed into the
|
||||
network stack's core: |
||||
|
||||
Every LWIP_GUARDED_BUF_CHECK_INTERVAL milliseconds, a callback will be called from the core and
|
||||
will input a maximum of LWIP_FRAMES_HANDLED_PER_CORE_CALL frames before returning control back |
||||
to the core. Meanwhile, incoming frames from the ZeroTier wire will be allocated and their
|
||||
pointers will be cached in the receive frame buffer of the size LWIP_MAX_GUARDED_RX_BUF_SZ to
|
||||
await the next callback from the core */ |
||||
|
||||
#define LWIP_GUARDED_BUF_CHECK_INTERVAL 50 // in ms
|
||||
#define LWIP_MAX_GUARDED_RX_BUF_SZ 1024 // number of frame pointers that can be cached waiting for receipt into core
|
||||
#define LWIP_FRAMES_HANDLED_PER_CORE_CALL 16 // How many frames are handled per call from core
|
||||
|
||||
typedef signed char err_t; |
||||
|
||||
#define ND6_DISCOVERY_INTERVAL 1000 |
||||
#define ARP_DISCOVERY_INTERVAL ARP_TMR_INTERVAL |
||||
|
||||
/**
|
||||
* Specifies the polling interval and the callback function that should |
||||
* be called to poll the application. The interval is specified in |
||||
* number of TCP coarse grained timer shots, which typically occurs |
||||
* twice a second. An interval of 10 means that the application would |
||||
* be polled every 5 seconds. (only for raw lwIP driver) |
||||
*/ |
||||
#define LWIP_APPLICATION_POLL_FREQ 2 |
||||
|
||||
/**
|
||||
* TCP timer interval in milliseconds (only for raw lwIP driver) |
||||
*/ |
||||
#define LWIP_TCP_TIMER_INTERVAL 25 |
||||
|
||||
/**
|
||||
* How often we check VirtualSocket statuses in milliseconds (only for raw lwIP driver) |
||||
*/ |
||||
#define LWIP_STATUS_TMR_INTERVAL 500 |
||||
|
||||
// #define LWIP_CHKSUM <your_checksum_routine>, See: RFC1071 for inspiration
|
||||
|
||||
/****************************************************************************/ |
||||
/* Defines */ |
||||
/****************************************************************************/ |
||||
|
||||
/**
|
||||
* Maximum number of sockets that libzt can administer |
||||
*/ |
||||
#define ZT_MAX_SOCKETS 1024 |
||||
|
||||
/**
|
||||
* Maximum MTU size for libzt (must be less than or equal to ZT_MAX_MTU) |
||||
*/ |
||||
#define ZT_SDK_MTU ZT_MAX_MTU |
||||
|
||||
/**
|
||||
* |
||||
*/ |
||||
#define ZT_LEN_SZ 4 |
||||
|
||||
/**
|
||||
* |
||||
*/ |
||||
#define ZT_ADDR_SZ 128 |
||||
|
||||
/**
|
||||
* Size of message buffer for VirtualSockets |
||||
*/ |
||||
#define ZT_SOCKET_MSG_BUF_SZ ZT_SDK_MTU + ZT_LEN_SZ + ZT_ADDR_SZ |
||||
|
||||
/**
|
||||
* Polling interval (in ms) for file descriptors wrapped in the Phy I/O loop (for raw drivers only) |
||||
*/ |
||||
#define ZT_PHY_POLL_INTERVAL 5 |
||||
|
||||
/**
|
||||
* State check interval (in ms) for VirtualSocket state |
||||
*/ |
||||
#define ZT_ACCEPT_RECHECK_DELAY 50 |
||||
|
||||
/**
|
||||
* State check interval (in ms) for VirtualSocket state |
||||
*/ |
||||
#define ZT_CONNECT_RECHECK_DELAY 50 |
||||
|
||||
/**
|
||||
* State check interval (in ms) for VirtualSocket state |
||||
*/ |
||||
#define ZT_API_CHECK_INTERVAL 50 |
||||
|
||||
/**
|
||||
* Size of TCP TX buffer for VirtualSockets used in raw network stack drivers |
||||
*/ |
||||
#define ZT_TCP_TX_BUF_SZ 1024 * 1024 * 128 |
||||
|
||||
/**
|
||||
* Size of TCP RX buffer for VirtualSockets used in raw network stack drivers |
||||
*/ |
||||
#define ZT_TCP_RX_BUF_SZ 1024 * 1024 * 128 |
||||
|
||||
/**
|
||||
* Size of UDP TX buffer for VirtualSockets used in raw network stack drivers |
||||
*/ |
||||
#define ZT_UDP_TX_BUF_SZ ZT_MAX_MTU |
||||
|
||||
/**
|
||||
* Size of UDP RX buffer for VirtualSockets used in raw network stack drivers |
||||
*/ |
||||
#define ZT_UDP_RX_BUF_SZ ZT_MAX_MTU * 10 |
||||
|
||||
/**
|
||||
* Send buffer size for the network stack |
||||
* By default picoTCP sets them to 16834, this is good for embedded-scale |
||||
* stuff but you might want to consider higher values for desktop and mobile |
||||
* applications. |
||||
*/ |
||||
#define ZT_STACK_TCP_SOCKET_TX_SZ ZT_TCP_TX_BUF_SZ |
||||
|
||||
/**
|
||||
* Receive buffer size for the network stack |
||||
* By default picoTCP sets them to 16834, this is good for embedded-scale |
||||
* stuff but you might want to consider higher values for desktop and mobile |
||||
* applications. |
||||
*/ |
||||
#define ZT_STACK_TCP_SOCKET_RX_SZ ZT_TCP_RX_BUF_SZ |
||||
|
||||
/**
|
||||
* Maximum size we're allowed to read or write from a stack socket |
||||
* This is put in place because picoTCP seems to fail at higher values. |
||||
* If you use another stack you can probably bump this up a bit. |
||||
*/ |
||||
#define ZT_STACK_SOCKET_WR_MAX 4096 |
||||
|
||||
/**
|
||||
* Maximum size of read operation from a network stack |
||||
*/ |
||||
#define ZT_STACK_SOCKET_RD_MAX 4096*4 |
||||
|
||||
/**
|
||||
* Maximum length of libzt/ZeroTier home path (where keys, and config files are stored) |
||||
*/ |
||||
#define ZT_HOME_PATH_MAX_LEN 256 |
||||
|
||||
/**
|
||||
* Length of human-readable MAC address string |
||||
*/ |
||||
#define ZT_MAC_ADDRSTRLEN 18 |
||||
|
||||
/**
|
||||
* Everything is ok |
||||
*/ |
||||
#define ZT_ERR_OK 0 |
||||
|
||||
/**
|
||||
* Value returned during an internal failure at the VirtualSocket/VirtualTap layer |
||||
*/ |
||||
#define ZT_ERR_GENERAL_FAILURE -88 |
||||
|
||||
/**
|
||||
* Whether sockets created will have SO_LINGER set by default |
||||
*/ |
||||
#define ZT_SOCK_BEHAVIOR_LINGER false |
||||
|
||||
/**
|
||||
* Length of time that VirtualSockets should linger (in seconds) |
||||
*/ |
||||
#define ZT_SOCK_BEHAVIOR_LINGER_TIME 3 |
||||
|
||||
/**
|
||||
* Maximum wait time for socket closure if data is still present in the write queue |
||||
*/ |
||||
#define ZT_SDK_CLTIME 60 |
||||
|
||||
/**
|
||||
* Interval for performing background tasks (such as adding routes) on VirtualTap objects (in seconds) |
||||
*/ |
||||
#define ZT_HOUSEKEEPING_INTERVAL 1 |
||||
|
||||
/****************************************************************************/ |
||||
/* Socket API Signatures */ |
||||
/****************************************************************************/ |
||||
|
||||
#define ZT_SETSOCKOPT_SIG int fd, int level, int optname, const void *optval, socklen_t optlen |
||||
#define ZT_GETSOCKOPT_SIG int fd, int level, int optname, void *optval, socklen_t *optlen |
||||
#define ZT_SENDMSG_SIG int fd, const struct msghdr *msg, int flags |
||||
#define ZT_SENDTO_SIG int fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t addrlen |
||||
#define ZT_RECV_SIG int fd, void *buf, size_t len, int flags |
||||
#define ZT_RECVFROM_SIG int fd, void *buf, size_t len, int flags, struct sockaddr *addr, socklen_t *addrlen |
||||
#define ZT_RECVMSG_SIG int fd, struct msghdr *msg,int flags |
||||
#define ZT_SEND_SIG int fd, const void *buf, size_t len, int flags |
||||
#define ZT_READ_SIG int fd, void *buf, size_t len |
||||
#define ZT_WRITE_SIG int fd, const void *buf, size_t len |
||||
#define ZT_SHUTDOWN_SIG int fd, int how |
||||
#define ZT_SOCKET_SIG int socket_family, int socket_type, int protocol |
||||
#define ZT_CONNECT_SIG int fd, const struct sockaddr *addr, socklen_t addrlen |
||||
#define ZT_BIND_SIG int fd, const struct sockaddr *addr, socklen_t addrlen |
||||
#define ZT_LISTEN_SIG int fd, int backlog |
||||
#define ZT_ACCEPT4_SIG int fd, struct sockaddr *addr, socklen_t *addrlen, int flags |
||||
#define ZT_ACCEPT_SIG int fd, struct sockaddr *addr, socklen_t *addrlen |
||||
#define ZT_CLOSE_SIG int fd |
||||
#define ZT_POLL_SIG struct pollfd *fds, nfds_t nfds, int timeout |
||||
#define ZT_SELECT_SIG int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout |
||||
#define ZT_GETSOCKNAME_SIG int fd, struct sockaddr *addr, socklen_t *addrlen |
||||
#define ZT_GETPEERNAME_SIG int fd, struct sockaddr *addr, socklen_t *addrlen |
||||
#define ZT_GETHOSTNAME_SIG char *name, size_t len |
||||
#define ZT_SETHOSTNAME_SIG const char *name, size_t len |
||||
#define ZT_FCNTL_SIG int fd, int cmd, int flags |
||||
#define ZT_IOCTL_SIG int fd, unsigned long request, void *argp |
||||
#define ZT_SYSCALL_SIG long number, ... |
||||
|
||||
#define LIBZT_SERVICE_NOT_STARTED_STR "service not started yet, call zts_startjoin()" |
||||
|
||||
#endif // _H
|
||||
@ -0,0 +1,841 @@
|
||||
/*
|
||||
* ZeroTier SDK - Network Virtualization Everywhere |
||||
* Copyright (C) 2011-2019 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. |
||||
*/ |
||||
|
||||
/**
|
||||
* @file |
||||
* |
||||
* ZeroTier service controls |
||||
*/ |
||||
|
||||
|
||||
|
||||
#include "OneService.hpp" |
||||
#include "Node.hpp" |
||||
|
||||
namespace ZeroTier |
||||
{ |
||||
std::vector<void*> vtaps; |
||||
Mutex _vtaps_lock; |
||||
} |
||||
|
||||
#include "Constants.hpp" |
||||
#include "VirtualTapManager.hpp" |
||||
#include "lwIP.h" |
||||
#include "OSUtils.hpp" |
||||
#include "ServiceControls.hpp" |
||||
|
||||
//#define SDK_JNI 1
|
||||
#ifdef SDK_JNI |
||||
#include <jni.h> |
||||
#endif |
||||
|
||||
ZeroTier::Mutex _service_lock; |
||||
ZeroTier::Mutex _startup_lock; |
||||
|
||||
static ZeroTier::OneService *zt1Service; |
||||
std::string homeDir; |
||||
int servicePort = ZTS_DEFAULT_PORT; |
||||
bool _freeHasBeenCalled = false; |
||||
bool _serviceIsShuttingDown = false; |
||||
bool _startupError = false; |
||||
|
||||
#if defined(_WIN32) |
||||
WSADATA wsaData; |
||||
#include <Windows.h> |
||||
#endif |
||||
|
||||
pthread_t service_thread; |
||||
|
||||
using namespace ZeroTier; |
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Internal ZeroTier Service Controls (user application shall not use these)//
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void api_sleep(int interval_ms) |
||||
{ |
||||
#if defined(_WIN32) |
||||
Sleep(interval_ms); |
||||
#else |
||||
struct timespec sleepValue = {0}; |
||||
sleepValue.tv_nsec = interval_ms * 500000; |
||||
nanosleep(&sleepValue, NULL); |
||||
#endif |
||||
} |
||||
|
||||
int _zts_node_online() |
||||
{ |
||||
return zt1Service && zt1Service->getNode() && zt1Service->getNode()->online(); |
||||
} |
||||
|
||||
int _zts_can_perform_service_operation() |
||||
{ |
||||
return zt1Service && zt1Service->isRunning() && zt1Service->getNode() && zt1Service->getNode()->online() && !_serviceIsShuttingDown; |
||||
} |
||||
|
||||
void _hibernate_if_needed() |
||||
{ |
||||
if (VirtualTapManager::get_vtaps_size()) { |
||||
lwip_wake_driver(); |
||||
} else { |
||||
lwip_hibernate_driver(); |
||||
} |
||||
} |
||||
#ifdef SDK_JNI |
||||
#endif |
||||
|
||||
// Starts a ZeroTier service in the background
|
||||
#if defined(_WIN32) |
||||
DWORD WINAPI _zts_start_service(LPVOID thread_id) |
||||
#else |
||||
void *_zts_start_service(void *thread_id) |
||||
#endif |
||||
{
|
||||
void *retval; |
||||
//DEBUG_INFO("identities are stored in path (%s)", homeDir.c_str());
|
||||
zt1Service = (OneService *)0; |
||||
|
||||
if (!homeDir.length()) { |
||||
DEBUG_ERROR("homeDir is empty, could not construct path"); |
||||
_startupError = true; |
||||
retval = NULL; |
||||
} if (zt1Service) { |
||||
DEBUG_INFO("service already started, doing nothing"); |
||||
retval = NULL; |
||||
} |
||||
|
||||
try { |
||||
std::vector<std::string> hpsp(OSUtils::split(homeDir.c_str(), ZT_PATH_SEPARATOR_S,"","")); |
||||
std::string ptmp; |
||||
if (homeDir[0] == ZT_PATH_SEPARATOR) { |
||||
ptmp.push_back(ZT_PATH_SEPARATOR); |
||||
} |
||||
for (std::vector<std::string>::iterator pi(hpsp.begin());pi!=hpsp.end();++pi) { |
||||
if (ptmp.length() > 0) { |
||||
ptmp.push_back(ZT_PATH_SEPARATOR); |
||||
} |
||||
ptmp.append(*pi); |
||||
if ((*pi != ".")&&(*pi != "..")) { |
||||
if (OSUtils::mkdir(ptmp) == false) { |
||||
DEBUG_ERROR("home path does not exist, and could not create"); |
||||
_startupError = true; |
||||
retval = NULL; |
||||
perror("error\n"); |
||||
} |
||||
} |
||||
} |
||||
if (!_startupError) { |
||||
for(;;) { |
||||
_service_lock.lock(); |
||||
zt1Service = OneService::newInstance(homeDir.c_str(),servicePort); |
||||
_service_lock.unlock(); |
||||
switch(zt1Service->run()) { |
||||
case OneService::ONE_STILL_RUNNING: |
||||
case OneService::ONE_NORMAL_TERMINATION: |
||||
break; |
||||
case OneService::ONE_UNRECOVERABLE_ERROR: |
||||
DEBUG_ERROR("fatal error: %s", zt1Service->fatalErrorMessage().c_str()); |
||||
_startupError = true; |
||||
break; |
||||
case OneService::ONE_IDENTITY_COLLISION: { |
||||
_startupError = true; |
||||
delete zt1Service; |
||||
zt1Service = (OneService *)0; |
||||
std::string oldid; |
||||
OSUtils::readFile((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret").c_str(),oldid); |
||||
if (oldid.length()) { |
||||
OSUtils::writeFile((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret.saved_after_collision").c_str(),oldid); |
||||
OSUtils::rm((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret").c_str()); |
||||
OSUtils::rm((homeDir + ZT_PATH_SEPARATOR_S + "identity.public").c_str()); |
||||
} |
||||
} continue; // restart!
|
||||
} |
||||
break; // terminate loop -- normally we don't keep restarting
|
||||
} |
||||
} |
||||
|
||||
_serviceIsShuttingDown = true; |
||||
_service_lock.lock(); |
||||
delete zt1Service; |
||||
zt1Service = (OneService *)0; |
||||
_service_lock.unlock(); |
||||
_serviceIsShuttingDown = false; |
||||
} catch ( ... ) { |
||||
DEBUG_ERROR("unexpected exception starting ZeroTier instance"); |
||||
} |
||||
pthread_exit(NULL); |
||||
} |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// ZeroTier Service Controls //
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
zts_err_t zts_set_service_port(int portno) |
||||
{ |
||||
zts_err_t retval = ZTS_ERR_OK; |
||||
_service_lock.lock(); |
||||
if (zt1Service) { |
||||
// Stop service before attempting to set a port
|
||||
retval = ZTS_ERR_SERVICE; |
||||
} |
||||
else { |
||||
if (portno > -1 && portno < ZTS_MAX_PORT) { |
||||
// 0 is allowed, signals to ZT service to bind to a random port
|
||||
servicePort = portno; |
||||
retval = ZTS_ERR_OK; |
||||
} |
||||
} |
||||
_service_lock.unlock(); |
||||
return retval; |
||||
} |
||||
#ifdef SDK_JNI |
||||
JNIEXPORT void JNICALL Java_com_zerotier_libzt_ZeroTier_set_1service_1port( |
||||
JNIEnv *env, jobject thisObj, jint port) |
||||
{ |
||||
zts_set_service_port(port); |
||||
} |
||||
#endif |
||||
|
||||
int zts_get_service_port() |
||||
{ |
||||
return servicePort; |
||||
} |
||||
#ifdef SDK_JNI |
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_get_1service_1port( |
||||
JNIEnv *env, jobject thisObj) |
||||
{ |
||||
return zts_get_service_port(); |
||||
} |
||||
#endif |
||||
/*
|
||||
int zts_get_address(const uint64_t nwid, struct sockaddr_storage *addr,
|
||||
const int address_family) |
||||
{ |
||||
int err = -1; |
||||
if (!zt1Service) { |
||||
return ZTS_ERR_SERVICE; |
||||
} |
||||
VirtualTap *tap = getTapByNWID(nwid); |
||||
if (!tap) { |
||||
return -1; |
||||
} |
||||
_vtaps_lock.lock(); |
||||
socklen_t addrlen = address_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6); |
||||
for (size_t i=0; i<tap->_ips.size(); i++) { |
||||
if (address_family == AF_INET) { |
||||
if (tap->_ips[i].isV4()) { |
||||
memcpy(addr, &(tap->_ips[i]), addrlen); |
||||
addr->ss_family = AF_INET; |
||||
err = 0; |
||||
break; |
||||
} |
||||
} |
||||
if (address_family == AF_INET6) { |
||||
if (tap->_ips[i].isV6()) { |
||||
memcpy(addr, &(tap->_ips[i]), addrlen); |
||||
addr->ss_family = AF_INET6; |
||||
err = 0; |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
_vtaps_lock.unlock(); |
||||
return err; // nothing found
|
||||
} |
||||
#ifdef SDK_JNI |
||||
JNIEXPORT jboolean JNICALL Java_com_zerotier_libzt_ZeroTier_get_1address( |
||||
JNIEnv *env, jobject thisObj, jlong nwid, jint address_family, jobject addr) |
||||
{ |
||||
struct sockaddr_storage ss; |
||||
int err = zts_get_address((uint64_t)nwid, &ss, address_family); |
||||
ss2zta(env, &ss, addr); |
||||
return err; |
||||
} |
||||
#endif |
||||
|
||||
int zts_has_address(const uint64_t nwid) |
||||
{ |
||||
struct sockaddr_storage ss; |
||||
memset(&ss, 0, sizeof(ss)); |
||||
zts_get_address(nwid, &ss, AF_INET); |
||||
if (ss.ss_family == AF_INET) { |
||||
return true; |
||||
} |
||||
zts_get_address(nwid, &ss, AF_INET6); |
||||
if (ss.ss_family == AF_INET6) { |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
#ifdef SDK_JNI |
||||
JNIEXPORT jboolean JNICALL Java_com_zerotier_libzt_ZeroTier_has_1address( |
||||
JNIEnv *env, jobject thisObj, jlong nwid) |
||||
{ |
||||
return zts_has_address(nwid); |
||||
} |
||||
#endif |
||||
*/ |
||||
|
||||
/*
|
||||
void zts_get_6plane_addr(struct sockaddr_storage *addr, const uint64_t nwid, const uint64_t nodeId) |
||||
{ |
||||
ZeroTier::InetAddress _6planeAddr = ZeroTier::InetAddress::makeIpv66plane(nwid,nodeId); |
||||
memcpy(addr, _6planeAddr.rawIpData(), sizeof(struct sockaddr_storage)); |
||||
} |
||||
#ifdef SDK_JNI |
||||
JNIEXPORT void JNICALL Java_com_zerotier_libzt_ZeroTier_get_16plane_1addr( |
||||
JNIEnv *env, jobject thisObj, jlong nwid, jlong nodeId, jobject addr) |
||||
{ |
||||
struct sockaddr_storage ss; |
||||
zts_get_6plane_addr(&ss, nwid, nodeId); |
||||
ss2zta(env, &ss, addr); |
||||
} |
||||
#endif |
||||
|
||||
void zts_get_rfc4193_addr(struct sockaddr_storage *addr, const uint64_t nwid, const uint64_t nodeId) |
||||
{ |
||||
ZeroTier::InetAddress _rfc4193Addr = ZeroTier::InetAddress::makeIpv6rfc4193(nwid,nodeId); |
||||
memcpy(addr, _rfc4193Addr.rawIpData(), sizeof(struct sockaddr_storage)); |
||||
} |
||||
#ifdef SDK_JNI |
||||
JNIEXPORT void JNICALL Java_com_zerotier_libzt_ZeroTier_get_1rfc4193_1addr( |
||||
JNIEnv *env, jobject thisObj, jlong nwid, jlong nodeId, jobject addr) |
||||
{ |
||||
struct sockaddr_storage ss; |
||||
zts_get_rfc4193_addr(&ss, nwid, nodeId); |
||||
ss2zta(env, &ss, addr); |
||||
} |
||||
#endif |
||||
*/ |
||||
|
||||
|
||||
zts_err_t zts_join(const uint64_t nwid, int blocking) |
||||
{ |
||||
zts_err_t retval = ZTS_ERR_OK; |
||||
retval = VirtualTapManager::get_vtaps_size() >= ZTS_MAX_JOINED_NETWORKS ? ZTS_ERR_INVALID_OP : ZTS_ERR_OK; |
||||
if (retval == ZTS_ERR_OK) { |
||||
_service_lock.lock(); |
||||
if (blocking) { |
||||
if (!zt1Service) { |
||||
retval = ZTS_ERR_SERVICE; |
||||
} else { |
||||
while (!_zts_node_online()) { |
||||
if (_serviceIsShuttingDown) { |
||||
retval = ZTS_ERR_SERVICE; |
||||
break; |
||||
} |
||||
api_sleep(ZTS_WRAPPER_CHECK_INTERVAL); |
||||
} |
||||
} |
||||
} else { |
||||
if (!zt1Service || !_zts_node_online()) { |
||||
retval = ZTS_ERR_SERVICE; |
||||
} |
||||
} |
||||
if (retval == ZTS_ERR_OK) { |
||||
if (nwid == 0) { |
||||
retval = ZTS_ERR_INVALID_ARG; |
||||
} |
||||
if (zt1Service) { |
||||
zt1Service->getNode()->join(nwid, NULL, NULL); |
||||
} |
||||
VirtualTapManager::update_service_references((void*)zt1Service); |
||||
} |
||||
_service_lock.unlock(); |
||||
_hibernate_if_needed(); |
||||
} |
||||
return retval; |
||||
} |
||||
#ifdef SDK_JNI |
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_join( |
||||
JNIEnv *env, jobject thisObj, jlong nwid) |
||||
{ |
||||
return zts_join((uint64_t)nwid); |
||||
} |
||||
#endif |
||||
|
||||
zts_err_t zts_leave(const uint64_t nwid, int blocking) |
||||
{ |
||||
zts_err_t retval = ZTS_ERR_OK; |
||||
_service_lock.lock(); |
||||
if (blocking) { |
||||
if (!zt1Service) { |
||||
retval = ZTS_ERR_SERVICE; |
||||
} else { |
||||
while (!_zts_node_online()) { |
||||
if (_serviceIsShuttingDown) { |
||||
retval = ZTS_ERR_SERVICE; |
||||
break; |
||||
} |
||||
api_sleep(ZTS_WRAPPER_CHECK_INTERVAL); |
||||
} |
||||
} |
||||
} else { |
||||
if (!zt1Service || !_zts_node_online()) { |
||||
retval = ZTS_ERR_SERVICE; |
||||
} |
||||
} |
||||
if (retval == ZTS_ERR_OK) { |
||||
if (nwid == 0) { |
||||
retval = ZTS_ERR_INVALID_ARG; |
||||
} |
||||
if (zt1Service) { |
||||
zt1Service->getNode()->leave(nwid, NULL, NULL); |
||||
} |
||||
} |
||||
VirtualTapManager::remove_by_nwid(nwid); |
||||
_hibernate_if_needed(); |
||||
_service_lock.unlock(); |
||||
return retval; |
||||
} |
||||
#ifdef SDK_JNI |
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_leave( |
||||
JNIEnv *env, jobject thisObj, jlong nwid) |
||||
{ |
||||
return zts_leave((uint64_t)nwid); |
||||
} |
||||
#endif |
||||
|
||||
zts_err_t zts_leave_all(int blocking) |
||||
{ |
||||
zts_err_t retval = ZTS_ERR_OK; |
||||
if (!zt1Service || _freeHasBeenCalled || _serviceIsShuttingDown) { |
||||
retval = ZTS_ERR_SERVICE; |
||||
} |
||||
else { |
||||
struct zts_network_details nds[ZTS_MAX_JOINED_NETWORKS]; |
||||
int numJoined = ZTS_MAX_JOINED_NETWORKS; |
||||
zts_get_all_network_details(nds, &numJoined); |
||||
for (int i=0; i<numJoined; i++) { |
||||
zts_leave(nds[i].nwid); |
||||
} |
||||
} |
||||
return retval; |
||||
} |
||||
#ifdef SDK_JNI |
||||
#endif |
||||
|
||||
zts_err_t zts_orbit(uint64_t moonWorldId, uint64_t moonSeed) |
||||
{ |
||||
zts_err_t retval = ZTS_ERR_OK; |
||||
void *tptr = NULL; |
||||
_service_lock.lock(); |
||||
if (!zt1Service || _freeHasBeenCalled || _serviceIsShuttingDown) { |
||||
retval = ZTS_ERR_SERVICE; |
||||
} |
||||
else if (_zts_can_perform_service_operation()) { |
||||
zt1Service->getNode()->orbit(tptr, moonWorldId, moonSeed); |
||||
} |
||||
_service_lock.unlock(); |
||||
return retval; |
||||
} |
||||
#ifdef SDK_JNI |
||||
#endif |
||||
|
||||
zts_err_t zts_deorbit(uint64_t moonWorldId) |
||||
{ |
||||
zts_err_t retval = ZTS_ERR_OK; |
||||
void *tptr = NULL; |
||||
_service_lock.lock(); |
||||
if (!zt1Service || _freeHasBeenCalled || _serviceIsShuttingDown) { |
||||
retval = ZTS_ERR_SERVICE; |
||||
} |
||||
else if (_zts_can_perform_service_operation()) { |
||||
zt1Service->getNode()->deorbit(tptr, moonWorldId); |
||||
} |
||||
_service_lock.unlock(); |
||||
return retval; |
||||
} |
||||
#ifdef SDK_JNI |
||||
#endif |
||||
|
||||
int zts_core_running() |
||||
{ |
||||
_service_lock.lock(); |
||||
int retval = zt1Service == NULL ? false : zt1Service->isRunning(); |
||||
_service_lock.unlock(); |
||||
return retval; |
||||
} |
||||
#ifdef SDK_JNI |
||||
JNIEXPORT jboolean JNICALL Java_com_zerotier_libzt_ZeroTier_core_1running( |
||||
JNIEnv *env, jobject thisObj) |
||||
{ |
||||
return zts_core_running(); |
||||
} |
||||
#endif |
||||
|
||||
int zts_ready() |
||||
{ |
||||
_service_lock.lock(); |
||||
bool stackRunning = VirtualTapManager::get_vtaps_size() > 0 ? true : false; |
||||
_service_lock.unlock(); |
||||
return zts_core_running() && stackRunning; |
||||
} |
||||
#ifdef SDK_JNI |
||||
JNIEXPORT jboolean JNICALL Java_com_zerotier_libzt_ZeroTier_ready( |
||||
JNIEnv *env, jobject thisObj) |
||||
{ |
||||
return zts_ready(); |
||||
} |
||||
#endif |
||||
|
||||
zts_err_t zts_start(const char *path, int blocking = false) |
||||
{ |
||||
_startup_lock.lock(); |
||||
zts_err_t retval = ZTS_ERR_OK; |
||||
if (zt1Service) { |
||||
// Service is already initialized
|
||||
retval = ZTS_ERR_SERVICE; |
||||
} |
||||
if (_freeHasBeenCalled) { |
||||
// Stack (presumably lwIP) has been dismantled, an application restart is required now
|
||||
retval = ZTS_ERR_INVALID_OP; |
||||
} |
||||
if (!path) { |
||||
retval = ZTS_ERR_INVALID_ARG; |
||||
} |
||||
if (retval == ZTS_ERR_OK) { |
||||
homeDir = path; |
||||
#if defined(_WIN32) |
||||
// initialize WinSock. Used in Phy for loopback pipe
|
||||
WSAStartup(MAKEWORD(2, 2), &wsaData); |
||||
HANDLE thr = CreateThread(NULL, 0, _zts_start_service, NULL, 0, NULL); |
||||
#else |
||||
_startupError = false; |
||||
retval = pthread_create(&service_thread, NULL, _zts_start_service, NULL); |
||||
// Wait for confirmation that the ZT service has been initialized,
|
||||
// this wait condition is so brief and so rarely used that it should be
|
||||
// acceptable even in a non-blocking context.
|
||||
while(!zt1Service) { |
||||
if (_serviceIsShuttingDown || _startupError) { |
||||
// ZT service startup/binding might have failed for some reason
|
||||
retval = ZTS_ERR_SERVICE; |
||||
break; |
||||
} |
||||
api_sleep(10); |
||||
} |
||||
#endif |
||||
if (blocking && retval == ZTS_ERR_OK) { |
||||
// block to prevent service calls before we're ready
|
||||
// waiting for zerotier service thread to start
|
||||
while (zts_core_running() == false || zt1Service->getNode() == NULL) { |
||||
if (_serviceIsShuttingDown || _startupError) { |
||||
// ZT service startup/binding might have failed for some reason
|
||||
retval = ZTS_ERR_SERVICE; |
||||
break; |
||||
} |
||||
api_sleep(ZTS_WRAPPER_CHECK_INTERVAL); |
||||
} |
||||
if (retval == ZTS_ERR_OK) { |
||||
// waiting for node address assignment
|
||||
while (zt1Service->getNode()->address() <= 0) { |
||||
if (_serviceIsShuttingDown || _startupError) { |
||||
retval = ZTS_ERR_SERVICE; |
||||
break; |
||||
} |
||||
api_sleep(ZTS_WRAPPER_CHECK_INTERVAL); |
||||
} |
||||
} |
||||
if (retval == ZTS_ERR_OK) { |
||||
// Waiting for node to come online. Ensure the node is authorized to join the network
|
||||
while (true) { |
||||
_service_lock.lock(); |
||||
if (_serviceIsShuttingDown || _startupError) { |
||||
retval = ZTS_ERR_SERVICE; |
||||
break; |
||||
} |
||||
if (zt1Service && zt1Service->getNode() && zt1Service->getNode()->online()) { |
||||
// Node is fully online
|
||||
break; |
||||
} |
||||
api_sleep(ZTS_WRAPPER_CHECK_INTERVAL); |
||||
_service_lock.unlock(); |
||||
} |
||||
_service_lock.unlock(); |
||||
} |
||||
} |
||||
} |
||||
_startup_lock.unlock(); |
||||
// if (blocking && retval == ZTS_ERR_OK) { DEBUG_INFO("node=%llx online", (unsigned long long)zts_get_node_id());}
|
||||
_hibernate_if_needed(); |
||||
return retval; |
||||
} |
||||
#ifdef SDK_JNI |
||||
JNIEXPORT void JNICALL Java_com_zerotier_libzt_ZeroTier_start( |
||||
JNIEnv *env, jobject thisObj, jstring path, jboolean blocking) |
||||
{ |
||||
if (path) { |
||||
const char* utf_string = env->GetStringUTFChars(path, NULL); |
||||
zts_start(utf_string, blocking); |
||||
env->ReleaseStringUTFChars(path, utf_string); |
||||
} |
||||
} |
||||
#endif |
||||
|
||||
zts_err_t zts_startjoin(const char *path, const uint64_t nwid) |
||||
{ |
||||
zts_err_t retval = ZTS_ERR_OK; |
||||
if ((retval = zts_start(path, true)) < 0) { |
||||
return retval; |
||||
} |
||||
while (true) { |
||||
try { |
||||
zts_join(nwid); |
||||
break; |
||||
} |
||||
catch( ... ) { |
||||
api_sleep(ZTS_WRAPPER_CHECK_INTERVAL); |
||||
retval = ZTS_ERR_SERVICE; |
||||
} |
||||
} |
||||
_hibernate_if_needed(); |
||||
return retval; |
||||
} |
||||
#ifdef SDK_JNI |
||||
JNIEXPORT void JNICALL Java_com_zerotier_libzt_ZeroTier_startjoin( |
||||
JNIEnv *env, jobject thisObj, jstring path, jlong nwid) |
||||
{ |
||||
if (path && nwid) { |
||||
const char* utf_string = env->GetStringUTFChars(path, NULL); |
||||
zts_startjoin(utf_string, (uint64_t)nwid); |
||||
env->ReleaseStringUTFChars(path, utf_string); |
||||
} |
||||
} |
||||
#endif |
||||
|
||||
zts_err_t zts_stop(int blocking) |
||||
{ |
||||
zts_err_t retval = ZTS_ERR_OK; |
||||
_service_lock.lock(); |
||||
bool didStop = false; |
||||
if (_zts_can_perform_service_operation()) { |
||||
didStop = true; |
||||
zt1Service->terminate(); |
||||
VirtualTapManager::clear(); |
||||
} |
||||
else { |
||||
// Nothing to do
|
||||
retval = ZTS_ERR_SERVICE; |
||||
} |
||||
#if defined(_WIN32) |
||||
WSACleanup(); |
||||
#endif |
||||
_service_lock.unlock(); |
||||
if (blocking && retval == ZTS_ERR_OK && didStop) { |
||||
// Block until ZT service thread successfully exits
|
||||
pthread_join(service_thread, NULL); |
||||
} |
||||
_hibernate_if_needed(); |
||||
return retval; |
||||
} |
||||
#ifdef SDK_JNI |
||||
JNIEXPORT void JNICALL Java_com_zerotier_libzt_ZeroTier_stop( |
||||
JNIEnv *env, jobject thisObj) |
||||
{ |
||||
zts_stop(); |
||||
} |
||||
#endif |
||||
|
||||
zts_err_t zts_free() |
||||
{ |
||||
zts_err_t retval = 0; |
||||
_service_lock.lock(); |
||||
if (_freeHasBeenCalled) { |
||||
retval = ZTS_ERR_INVALID_OP; |
||||
_service_lock.unlock(); |
||||
} else { |
||||
_freeHasBeenCalled = true; |
||||
_service_lock.unlock(); |
||||
retval = zts_stop(); |
||||
} |
||||
// PENDING: add stack shutdown logic
|
||||
return retval; |
||||
} |
||||
#ifdef SDK_JNI |
||||
JNIEXPORT void JNICALL Java_com_zerotier_libzt_ZeroTier_free( |
||||
JNIEnv *env, jobject thisObj) |
||||
{ |
||||
zts_free(); |
||||
} |
||||
#endif |
||||
|
||||
uint64_t zts_get_node_id() |
||||
{ |
||||
uint64_t nodeId = 0; |
||||
_service_lock.lock(); |
||||
if (_zts_can_perform_service_operation()) { |
||||
nodeId = zt1Service->getNode()->address(); |
||||
} |
||||
_service_lock.unlock(); |
||||
return nodeId; |
||||
} |
||||
#ifdef SDK_JNI |
||||
JNIEXPORT jlong JNICALL Java_com_zerotier_libzt_ZeroTier_get_1node_1id( |
||||
JNIEnv *env, jobject thisObj) |
||||
{ |
||||
return zts_get_node_id(); |
||||
} |
||||
#endif |
||||
|
||||
int zts_get_peer_count() |
||||
{ |
||||
unsigned int peerCount = 0; |
||||
_service_lock.lock(); |
||||
if (_zts_can_perform_service_operation()) { |
||||
peerCount = zt1Service->getNode()->peers()->peerCount; |
||||
} else { |
||||
peerCount = ZTS_ERR_SERVICE; |
||||
} |
||||
_service_lock.unlock(); |
||||
return peerCount; |
||||
} |
||||
#ifdef SDK_JNI |
||||
JNIEXPORT jlong JNICALL Java_com_zerotier_libzt_ZeroTier_get_1peer_1count( |
||||
JNIEnv *env, jobject thisObj) |
||||
{ |
||||
return zts_get_peer_count(); |
||||
} |
||||
#endif |
||||
|
||||
int zts_get_peers(struct zts_peer_details *pds, int *num) |
||||
{ |
||||
zts_err_t retval = ZTS_ERR_OK; |
||||
if (!pds || !num) { |
||||
retval = ZTS_ERR_INVALID_ARG; |
||||
} |
||||
if (retval == ZTS_ERR_OK) { |
||||
_service_lock.lock(); |
||||
if (_zts_can_perform_service_operation()) { |
||||
ZT_PeerList *pl = zt1Service->getNode()->peers(); |
||||
if (pl) { |
||||
*num = pl->peerCount; |
||||
for(unsigned long i=0;i<pl->peerCount;++i) { |
||||
memcpy(&(pds[i]), &(pl->peers[i]), sizeof(struct zts_peer_details)); |
||||
} |
||||
} |
||||
} |
||||
else { |
||||
retval = ZTS_ERR_SERVICE; |
||||
} |
||||
_service_lock.unlock(); |
||||
} |
||||
return retval; |
||||
} |
||||
#ifdef SDK_JNI |
||||
#endif |
||||
|
||||
zts_err_t zts_get_num_joined_networks() |
||||
{ |
||||
zts_err_t retval = ZTS_ERR_OK; |
||||
if (!zt1Service || _freeHasBeenCalled || _serviceIsShuttingDown) { |
||||
retval = ZTS_ERR_SERVICE; |
||||
} |
||||
else { |
||||
retval = VirtualTapManager::get_vtaps_size(); |
||||
} |
||||
return retval; |
||||
} |
||||
#ifdef SDK_JNI |
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_get_1num_1joined_1networks( |
||||
JNIEnv *env, jobject thisObj) |
||||
{ |
||||
return zts_get_num_joined_networks(); |
||||
} |
||||
#endif |
||||
|
||||
zts_err_t zts_get_network_details(uint64_t nwid, struct zts_network_details *nd) |
||||
{ |
||||
zts_err_t retval = ZTS_ERR_OK; |
||||
if (!nd || nwid == 0) { |
||||
retval = ZTS_ERR_INVALID_ARG; |
||||
} |
||||
if (!zt1Service || _freeHasBeenCalled || _serviceIsShuttingDown) { |
||||
retval = ZTS_ERR_SERVICE; |
||||
} |
||||
if (retval == ZTS_ERR_OK) { |
||||
VirtualTapManager::get_network_details(nwid, nd); |
||||
} |
||||
return retval; |
||||
} |
||||
#ifdef SDK_JNI |
||||
#endif |
||||
|
||||
zts_err_t zts_get_all_network_details(struct zts_network_details *nds, int *num) |
||||
{ |
||||
zts_err_t retval = ZTS_ERR_OK; |
||||
if (!nds || !num) { |
||||
retval = ZTS_ERR_INVALID_ARG; |
||||
} |
||||
if (!zt1Service || _freeHasBeenCalled || _serviceIsShuttingDown) { |
||||
retval = ZTS_ERR_SERVICE; |
||||
} |
||||
if (retval == ZTS_ERR_OK) { |
||||
VirtualTapManager::get_all_network_details(nds, num); |
||||
} |
||||
return retval;
|
||||
} |
||||
#ifdef SDK_JNI |
||||
#endif |
||||
|
||||
zts_err_t zts_enable_http_backplane_mgmt() |
||||
{ |
||||
zts_err_t retval = ZTS_ERR_OK; |
||||
_service_lock.lock(); |
||||
if (!zt1Service || _freeHasBeenCalled || _serviceIsShuttingDown) { |
||||
retval = ZTS_ERR_SERVICE; |
||||
} |
||||
else { |
||||
zt1Service->allowHttpBackplaneManagement = true; |
||||
} |
||||
_service_lock.unlock(); |
||||
return retval; |
||||
} |
||||
#ifdef SDK_JNI |
||||
#endif |
||||
|
||||
zts_err_t zts_disable_http_backplane_mgmt() |
||||
{ |
||||
zts_err_t retval = ZTS_ERR_OK; |
||||
_service_lock.lock(); |
||||
if (!zt1Service || _freeHasBeenCalled || _serviceIsShuttingDown) { |
||||
retval = ZTS_ERR_SERVICE; |
||||
} |
||||
else { |
||||
zt1Service->allowHttpBackplaneManagement = false; |
||||
} |
||||
_service_lock.unlock(); |
||||
return retval; |
||||
} |
||||
#ifdef SDK_JNI |
||||
#endif |
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
Loading…
Reference in new issue