Improved reliability and performance, better startup and shutdown semantics, HTTP control plane is now disabled by default
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* ZeroTier SDK - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2018 ZeroTier, Inc. https://www.zerotier.com/
|
||||
* 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
|
||||
@@ -13,7 +13,7 @@
|
||||
* 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/>.
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
|
||||
841
src/ServiceControls.cpp
Normal file
841
src/ServiceControls.cpp
Normal file
@@ -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
|
||||
@@ -1,44 +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
|
||||
*
|
||||
* Platform-specific implementations of common functions
|
||||
*/
|
||||
|
||||
#if defined(__linux__) || defined(__APPLE__)
|
||||
#include <sys/socket.h>
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
|
||||
#include "SysUtils.h"
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __linux__
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
@@ -1,258 +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
|
||||
*
|
||||
* Misc utilities
|
||||
*/
|
||||
|
||||
#include "Utilities.h"
|
||||
|
||||
#if defined(_WIN32_FALSE)
|
||||
|
||||
#include <WinSock2.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
int inet_pton4(const char *src, void *dst)
|
||||
{
|
||||
uint8_t tmp[NS_INADDRSZ], *tp;
|
||||
|
||||
int saw_digit = 0;
|
||||
int octets = 0;
|
||||
*(tp = tmp) = 0;
|
||||
|
||||
int ch;
|
||||
while ((ch = *src++) != '\0')
|
||||
{
|
||||
if (ch >= '0' && ch <= '9')
|
||||
{
|
||||
uint32_t n = *tp * 10 + (ch - '0');
|
||||
|
||||
if (saw_digit && *tp == 0)
|
||||
return 0;
|
||||
|
||||
if (n > 255)
|
||||
return 0;
|
||||
|
||||
*tp = n;
|
||||
if (!saw_digit)
|
||||
{
|
||||
if (++octets > 4)
|
||||
return 0;
|
||||
saw_digit = 1;
|
||||
}
|
||||
}
|
||||
else if (ch == '.' && saw_digit)
|
||||
{
|
||||
if (octets == 4)
|
||||
return 0;
|
||||
*++tp = 0;
|
||||
saw_digit = 0;
|
||||
}
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
if (octets < 4)
|
||||
return 0;
|
||||
|
||||
memcpy(dst, tmp, NS_INADDRSZ);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int inet_pton6(const char *src, void *dst)
|
||||
{
|
||||
static const char xdigits[] = "0123456789abcdef";
|
||||
uint8_t tmp[NS_IN6ADDRSZ];
|
||||
|
||||
uint8_t *tp = (uint8_t*) memset(tmp, '\0', NS_IN6ADDRSZ);
|
||||
uint8_t *endp = tp + NS_IN6ADDRSZ;
|
||||
uint8_t *colonp = NULL;
|
||||
|
||||
/* Leading :: requires some special handling. */
|
||||
if (*src == ':')
|
||||
{
|
||||
if (*++src != ':')
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char *curtok = src;
|
||||
int saw_xdigit = 0;
|
||||
uint32_t val = 0;
|
||||
int ch;
|
||||
while ((ch = tolower(*src++)) != '\0')
|
||||
{
|
||||
const char *pch = strchr(xdigits, ch);
|
||||
if (pch != NULL)
|
||||
{
|
||||
val <<= 4;
|
||||
val |= (pch - xdigits);
|
||||
if (val > 0xffff)
|
||||
return 0;
|
||||
saw_xdigit = 1;
|
||||
continue;
|
||||
}
|
||||
if (ch == ':')
|
||||
{
|
||||
curtok = src;
|
||||
if (!saw_xdigit)
|
||||
{
|
||||
if (colonp)
|
||||
return 0;
|
||||
colonp = tp;
|
||||
continue;
|
||||
}
|
||||
else if (*src == '\0')
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (tp + NS_INT16SZ > endp)
|
||||
return 0;
|
||||
*tp++ = (uint8_t) (val >> 8) & 0xff;
|
||||
*tp++ = (uint8_t) val & 0xff;
|
||||
saw_xdigit = 0;
|
||||
val = 0;
|
||||
continue;
|
||||
}
|
||||
if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
|
||||
inet_pton4(curtok, (char*) tp) > 0)
|
||||
{
|
||||
tp += NS_INADDRSZ;
|
||||
saw_xdigit = 0;
|
||||
break; /* '\0' was seen by inet_pton4(). */
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if (saw_xdigit)
|
||||
{
|
||||
if (tp + NS_INT16SZ > endp)
|
||||
return 0;
|
||||
*tp++ = (uint8_t) (val >> 8) & 0xff;
|
||||
*tp++ = (uint8_t) val & 0xff;
|
||||
}
|
||||
if (colonp != NULL)
|
||||
{
|
||||
/*
|
||||
* Since some memmove()'s erroneously fail to handle
|
||||
* overlapping regions, we'll do the shift by hand.
|
||||
*/
|
||||
const int n = tp - colonp;
|
||||
|
||||
if (tp == endp)
|
||||
return 0;
|
||||
|
||||
for (int i = 1; i <= n; i++)
|
||||
{
|
||||
endp[-i] = colonp[n - i];
|
||||
colonp[n - i] = 0;
|
||||
}
|
||||
tp = endp;
|
||||
}
|
||||
if (tp != endp)
|
||||
return 0;
|
||||
|
||||
memcpy(dst, tmp, NS_IN6ADDRSZ);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int inet_pton(int af, const char *src, void *dst)
|
||||
{
|
||||
switch (af)
|
||||
{
|
||||
case AF_INET:
|
||||
return inet_pton4(src, dst);
|
||||
case AF_INET6:
|
||||
return inet_pton6(src, dst);
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
char *beautify_eth_proto_nums(int proto)
|
||||
{
|
||||
if (proto == 0x0800) return (char*)"IPv4";
|
||||
if (proto == 0x0806) return (char*)"ARP";
|
||||
if (proto == 0x0842) return (char*)"Wake-on-LAN";
|
||||
if (proto == 0x22F3) return (char*)"IETF TRILL Protocol";
|
||||
if (proto == 0x22EA) return (char*)"Stream Reservation Protocol";
|
||||
if (proto == 0x6003) return (char*)"DECnet Phase IV";
|
||||
if (proto == 0x8035) return (char*)"Reverse Address Resolution Protocol";
|
||||
if (proto == 0x809B) return (char*)"AppleTalk (Ethertalk)";
|
||||
if (proto == 0x80F3) return (char*)"AppleTalk Address Resolution Protocol (AARP)";
|
||||
if (proto == 0x8100) return (char*)"VLAN-tagged frame (IEEE 802.1Q) and Shortest Path Bridging IEEE 802.1aq with NNI compatibility";
|
||||
if (proto == 0x8137) return (char*)"IPX";
|
||||
if (proto == 0x8204) return (char*)"QNX Qnet";
|
||||
if (proto == 0x86DD) return (char*)"IPv6";
|
||||
if (proto == 0x8808) return (char*)"Ethernet flow control";
|
||||
if (proto == 0x8809) return (char*)"Ethernet Slow Protocols";
|
||||
if (proto == 0x8819) return (char*)"CobraNet";
|
||||
if (proto == 0x8847) return (char*)"MPLS unicast";
|
||||
if (proto == 0x8848) return (char*)"MPLS multicast";
|
||||
if (proto == 0x8863) return (char*)"PPPoE Discovery Stage";
|
||||
if (proto == 0x8864) return (char*)"PPPoE Session Stage";
|
||||
if (proto == 0x886D) return (char*)"Intel Advanced Networking Services";
|
||||
if (proto == 0x8870) return (char*)"Jumbo Frames (Obsoleted draft-ietf-isis-ext-eth-01)";
|
||||
if (proto == 0x887B) return (char*)"HomePlug 1.0 MME";
|
||||
if (proto == 0x888E) return (char*)"EAP over LAN (IEEE 802.1X)";
|
||||
if (proto == 0x8892) return (char*)"PROFINET Protocol";
|
||||
if (proto == 0x889A) return (char*)"HyperSCSI (SCSI over Ethernet)";
|
||||
if (proto == 0x88A2) return (char*)"ATA over Ethernet";
|
||||
if (proto == 0x88A4) return (char*)"EtherCAT Protocol";
|
||||
if (proto == 0x88A8) return (char*)"Provider Bridging (IEEE 802.1ad) & Shortest Path Bridging IEEE 802.1aq";
|
||||
if (proto == 0x88AB) return (char*)"Ethernet Powerlink[citation needed]";
|
||||
if (proto == 0x88B8) return (char*)"GOOSE (Generic Object Oriented Substation event)";
|
||||
if (proto == 0x88B9) return (char*)"GSE (Generic Substation Events) Management Services";
|
||||
if (proto == 0x88BA) return (char*)"SV (Sampled Value Transmission)";
|
||||
if (proto == 0x88CC) return (char*)"Link Layer Discovery Protocol (LLDP)";
|
||||
if (proto == 0x88CD) return (char*)"SERCOS III";
|
||||
if (proto == 0x88DC) return (char*)"WSMP, WAVE Short Message Protocol";
|
||||
if (proto == 0x88E1) return (char*)"HomePlug AV MME[citation needed]";
|
||||
if (proto == 0x88E3) return (char*)"Media Redundancy Protocol (IEC62439-2)";
|
||||
if (proto == 0x88E5) return (char*)"MAC security (IEEE 802.1AE)";
|
||||
if (proto == 0x88E7) return (char*)"Provider Backbone Bridges (PBB) (IEEE 802.1ah)";
|
||||
if (proto == 0x88F7) return (char*)"Precision Time Protocol (PTP) over Ethernet (IEEE 1588)";
|
||||
if (proto == 0x88FB) return (char*)"Parallel Redundancy Protocol (PRP)";
|
||||
if (proto == 0x8902) return (char*)"IEEE 802.1ag Connectivity Fault Management (CFM) Protocol / ITU-T Recommendation Y.1731 (OAM)";
|
||||
if (proto == 0x8906) return (char*)"Fibre Channel over Ethernet (FCoE)";
|
||||
if (proto == 0x8914) return (char*)"FCoE Initialization Protocol";
|
||||
if (proto == 0x8915) return (char*)"RDMA over Converged Ethernet (RoCE)";
|
||||
if (proto == 0x891D) return (char*)"TTEthernet Protocol Control Frame (TTE)";
|
||||
if (proto == 0x892F) return (char*)"High-availability Seamless Redundancy (HSR)";
|
||||
if (proto == 0x9000) return (char*)"Ethernet Configuration Testing Protocol";
|
||||
if (proto == 0x9100) return (char*)"VLAN-tagged (IEEE 802.1Q) frame with double tagging";
|
||||
return (char*)"UNKNOWN";
|
||||
}
|
||||
|
||||
void mac2str(char *macbuf, int len, unsigned char* addr)
|
||||
{
|
||||
snprintf(macbuf, len, "%02x:%02x:%02x:%02x:%02x:%02x",
|
||||
addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* ZeroTier SDK - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2018 ZeroTier, Inc. https://www.zerotier.com/
|
||||
* 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
|
||||
@@ -13,7 +13,7 @@
|
||||
* 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/>.
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
@@ -25,33 +25,25 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* Virtual Ethernet tap device
|
||||
*/
|
||||
|
||||
// Used by raw stack driver implementation
|
||||
struct ip_addr_t;
|
||||
typedef unsigned short u16_t;
|
||||
* @file
|
||||
*
|
||||
* Virtual Ethernet tap device
|
||||
*/
|
||||
|
||||
#include "VirtualTap.hpp"
|
||||
#include "Phy.hpp"
|
||||
|
||||
#include "VirtualTap.h"
|
||||
#include "libzt.h"
|
||||
#include "libztDebug.h"
|
||||
#include "lwIP.h"
|
||||
#include "ZT1Service.h"
|
||||
#include "SysUtils.h"
|
||||
#include "Node.hpp"
|
||||
#include "OSUtils.hpp"
|
||||
|
||||
#include "Mutex.hpp"
|
||||
#include "InetAddress.hpp"
|
||||
#include "OneService.hpp"
|
||||
#include "VirtualTapManager.hpp"
|
||||
#include "lwIP.h"
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#include "Synchapi.h"
|
||||
#endif
|
||||
|
||||
int VirtualTap::devno = 0;
|
||||
namespace ZeroTier {
|
||||
|
||||
VirtualTap::VirtualTap(
|
||||
const char *homePath,
|
||||
@@ -75,17 +67,11 @@ VirtualTap::VirtualTap(
|
||||
_unixListenSocket((PhySocket *)0),
|
||||
_phy(this,false,true)
|
||||
{
|
||||
vtaps.push_back((void*)this);
|
||||
|
||||
// set virtual tap interface name (full)
|
||||
VirtualTapManager::add_tap(this);
|
||||
memset(vtap_full_name, 0, sizeof(vtap_full_name));
|
||||
ifindex = devno;
|
||||
snprintf(vtap_full_name, sizeof(vtap_full_name), "libzt%d-%llx", devno++, (unsigned long long)_nwid);
|
||||
snprintf(vtap_full_name, sizeof(vtap_full_name), "libzt%llx", (unsigned long long)_nwid);
|
||||
_dev = vtap_full_name;
|
||||
DEBUG_INFO("set VirtualTap interface name to: %s", _dev.c_str());
|
||||
// set virtual tap interface name (abbreviated)
|
||||
memset(vtap_abbr_name, 0, sizeof(vtap_abbr_name));
|
||||
snprintf(vtap_abbr_name, sizeof(vtap_abbr_name), "libzt%d", devno);
|
||||
::pipe(_shutdownSignalPipe);
|
||||
lwip_driver_init();
|
||||
// start virtual tap thread and stack I/O loops
|
||||
_thread = Thread::start(this);
|
||||
@@ -93,10 +79,13 @@ VirtualTap::VirtualTap(
|
||||
|
||||
VirtualTap::~VirtualTap()
|
||||
{
|
||||
lwip_driver_set_tap_interfaces_down(this);
|
||||
_run = false;
|
||||
::write(_shutdownSignalPipe[1],"\0",1);
|
||||
_phy.whack();
|
||||
Thread::join(_thread);
|
||||
_phy.close(_unixListenSocket,false);
|
||||
::close(_shutdownSignalPipe[0]);
|
||||
::close(_shutdownSignalPipe[1]);
|
||||
}
|
||||
|
||||
void VirtualTap::setEnabled(bool en)
|
||||
@@ -117,7 +106,7 @@ void VirtualTap::registerIpWithStack(const InetAddress &ip)
|
||||
bool VirtualTap::addIp(const InetAddress &ip)
|
||||
{
|
||||
char ipbuf[INET6_ADDRSTRLEN];
|
||||
DEBUG_EXTRA("addr=%s, nwid=%llx", ip.toString(ipbuf), (unsigned long long)_nwid);
|
||||
// DEBUG_INFO("addr=%s, nwid=%llx", ip.toString(ipbuf), (unsigned long long)_nwid);
|
||||
Mutex::Lock _l(_ips_m);
|
||||
registerIpWithStack(ip);
|
||||
if (std::find(_ips.begin(),_ips.end(),ip) == _ips.end()) {
|
||||
@@ -164,9 +153,9 @@ std::string VirtualTap::deviceName() const
|
||||
std::string VirtualTap::nodeId() const
|
||||
{
|
||||
if (zt1ServiceRef) {
|
||||
char id[ZTO_ID_LEN];
|
||||
char id[ZTS_ID_LEN];
|
||||
memset(id, 0, sizeof(id));
|
||||
sprintf(id, "%llx", (unsigned long long)((ZeroTier::OneService *)zt1ServiceRef)->getNode()->address());
|
||||
sprintf(id, "%llx", (unsigned long long)((OneService *)zt1ServiceRef)->getNode()->address());
|
||||
return std::string(id);
|
||||
}
|
||||
else {
|
||||
@@ -176,7 +165,7 @@ std::string VirtualTap::nodeId() const
|
||||
|
||||
void VirtualTap::setFriendlyName(const char *friendlyName)
|
||||
{
|
||||
DEBUG_EXTRA("%s", friendlyName);
|
||||
DEBUG_INFO("%s", friendlyName);
|
||||
}
|
||||
|
||||
void VirtualTap::scanMulticastGroups(std::vector<MulticastGroup> &added,
|
||||
@@ -211,33 +200,57 @@ void VirtualTap::setMtu(unsigned int mtu)
|
||||
void VirtualTap::threadMain()
|
||||
throw()
|
||||
{
|
||||
fd_set readfds,nullfds;
|
||||
struct timeval tv;
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = 0;
|
||||
FD_ZERO(&readfds);
|
||||
FD_ZERO(&nullfds);
|
||||
int nfds = (int)std::max(_shutdownSignalPipe[0],0) + 1;
|
||||
while (true) {
|
||||
FD_SET(_shutdownSignalPipe[0],&readfds);
|
||||
select(nfds,&readfds,&nullfds,&nullfds,&tv);
|
||||
if (FD_ISSET(_shutdownSignalPipe[0],&readfds)) { // writes to shutdown pipe terminate thread
|
||||
break;
|
||||
}
|
||||
#ifdef _MSC_VER
|
||||
Sleep(ZT_PHY_POLL_INTERVAL);
|
||||
Sleep(ZTS_PHY_POLL_INTERVAL);
|
||||
_phy.poll(0);
|
||||
#else
|
||||
_phy.poll(ZT_PHY_POLL_INTERVAL);
|
||||
_phy.poll(ZTS_PHY_POLL_INTERVAL);
|
||||
#endif
|
||||
Housekeeping();
|
||||
|
||||
uint64_t current_ts = OSUtils::now();
|
||||
if (current_ts > last_housekeeping_ts + ZTS_HOUSEKEEPING_INTERVAL) {
|
||||
Housekeeping();
|
||||
last_housekeeping_ts = OSUtils::now();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void VirtualTap::Housekeeping()
|
||||
{
|
||||
Mutex::Lock _l(_tcpconns_m);
|
||||
uint64_t current_ts = time_now();
|
||||
if (current_ts > last_housekeeping_ts + ZT_HOUSEKEEPING_INTERVAL) {
|
||||
// update managed routes (add/del from network stacks)
|
||||
ZeroTier::OneService *service = ((ZeroTier::OneService *)zt1ServiceRef);
|
||||
if (service) {
|
||||
std::unique_ptr<std::vector<ZT_VirtualNetworkRoute>> managed_routes(service->getRoutes(this->_nwid));
|
||||
ZeroTier::InetAddress target_addr;
|
||||
ZeroTier::InetAddress via_addr;
|
||||
ZeroTier::InetAddress null_addr;
|
||||
ZeroTier::InetAddress nm;
|
||||
OneService *service = ((OneService *)zt1ServiceRef);
|
||||
if (!service) {
|
||||
return;
|
||||
}
|
||||
nd.num_routes = ZTS_MAX_NETWORK_ROUTES;
|
||||
service->getRoutes(this->_nwid, (ZT_VirtualNetworkRoute*)&(nd.routes)[0], &(nd.num_routes));
|
||||
|
||||
/*
|
||||
|
||||
|
||||
InetAddress target_addr;
|
||||
InetAddress via_addr;
|
||||
InetAddress null_addr;
|
||||
InetAddress nm;
|
||||
null_addr.fromString("");
|
||||
bool found;
|
||||
char ipbuf[INET6_ADDRSTRLEN], ipbuf2[INET6_ADDRSTRLEN], ipbuf3[INET6_ADDRSTRLEN];
|
||||
*/
|
||||
|
||||
|
||||
// TODO: Rework this when we have time
|
||||
// check if pushed route exists in tap (add)
|
||||
/*
|
||||
@@ -258,7 +271,7 @@ void VirtualTap::Housekeeping()
|
||||
if (found == false) {
|
||||
if (via_addr.ipsEqual(null_addr) == false) {
|
||||
DEBUG_INFO("adding route <target=%s, nm=%s, via=%s>", target_addr.toString(ipbuf), nm.toString(ipbuf2), via_addr.toString(ipbuf3));
|
||||
routes.push_back(std::pair<ZeroTier::InetAddress,ZeroTier::InetAddress>(target_addr, nm));
|
||||
routes.push_back(std::pair<InetAddress,InetAddress>(target_addr, nm));
|
||||
routeAdd(target_addr, nm, via_addr);
|
||||
}
|
||||
}
|
||||
@@ -281,15 +294,13 @@ void VirtualTap::Housekeeping()
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
//}
|
||||
// TODO: Clean up VirtualSocket objects
|
||||
last_housekeeping_ts = time_now();
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************/
|
||||
/* Not used in this implementation */
|
||||
/****************************************************************************/
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Not used in this implementation //
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void VirtualTap::phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *local_address,
|
||||
const struct sockaddr *from,void *data,unsigned long len) {}
|
||||
@@ -300,3 +311,4 @@ void VirtualTap::phyOnTcpClose(PhySocket *sock,void **uptr) {}
|
||||
void VirtualTap::phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len) {}
|
||||
void VirtualTap::phyOnTcpWritable(PhySocket *sock,void **uptr) {}
|
||||
|
||||
} // namespace ZeroTier
|
||||
@@ -1,563 +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
|
||||
*
|
||||
* ZeroTier One service control wrapper
|
||||
*/
|
||||
|
||||
#include "libzt.h"
|
||||
#include "ZT1Service.h"
|
||||
#include "libztDebug.h"
|
||||
#include "SysUtils.h"
|
||||
|
||||
#include "Phy.hpp"
|
||||
#include "OneService.hpp"
|
||||
#include "InetAddress.hpp"
|
||||
#include "OSUtils.hpp"
|
||||
#include "Mutex.hpp"
|
||||
|
||||
std::vector<void*> vtaps;
|
||||
ZeroTier::Mutex _vtaps_lock;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
static ZeroTier::OneService *zt1Service;
|
||||
|
||||
std::string homeDir; // Platform-specific dir we *must* use internally
|
||||
std::string netDir; // Where network .conf files are to be written
|
||||
|
||||
ZeroTier::Mutex _multiplexer_lock;
|
||||
|
||||
int servicePort = LIBZT_DEFAULT_PORT;
|
||||
|
||||
#if defined(_WIN32)
|
||||
WSADATA wsaData;
|
||||
#include <Windows.h>
|
||||
#endif
|
||||
|
||||
void api_sleep(int interval_ms);
|
||||
|
||||
/****************************************************************************/
|
||||
/* ZeroTier Core helper functions for libzt - DON'T CALL THESE DIRECTLY */
|
||||
/****************************************************************************/
|
||||
|
||||
std::vector<ZT_VirtualNetworkRoute> *zts_get_network_routes(const uint64_t nwid)
|
||||
{
|
||||
return zt1Service->getRoutes(nwid);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
VirtualTap *getTapByAddr(ZeroTier::InetAddress *addr)
|
||||
{
|
||||
_vtaps_lock.lock();
|
||||
VirtualTap *s, *tap = nullptr;
|
||||
//char ipbuf[64], ipbuf2[64], ipbuf3[64];
|
||||
for (size_t i=0; i<vtaps.size(); i++) {
|
||||
s = (VirtualTap*)vtaps[i];
|
||||
// check address schemes
|
||||
for (int j=0; j<(int)(s->_ips.size()); j++) {
|
||||
if ((s->_ips[j].isV4() && addr->isV4()) || (s->_ips[j].isV6() && addr->isV6())) {
|
||||
/* DEBUG_EXTRA("looking at tap %s, <addr=%s> --- for <%s>", s->_dev.c_str(),
|
||||
s->_ips[j].toString(ipbuf), addr->toIpString(ipbuf2)); */
|
||||
if (s->_ips[j].isEqualPrefix(addr)
|
||||
|| s->_ips[j].ipsEqual(addr)
|
||||
|| s->_ips[j].containsAddress(addr)
|
||||
|| (addr->isV6() && _ipv6_in_subnet(&s->_ips[j], addr))
|
||||
)
|
||||
{
|
||||
//DEBUG_EXTRA("selected tap %s, <addr=%s>", s->_dev.c_str(), s->_ips[j].toString(ipbuf));
|
||||
_vtaps_lock.unlock();
|
||||
return s;
|
||||
}
|
||||
}
|
||||
}
|
||||
// check managed routes
|
||||
if (tap == NULL) {
|
||||
std::vector<ZT_VirtualNetworkRoute> *managed_routes = zt1Service->getRoutes(s->_nwid);
|
||||
ZeroTier::InetAddress target, nm, via;
|
||||
for (size_t i=0; i<managed_routes->size(); i++) {
|
||||
target = managed_routes->at(i).target;
|
||||
nm = target.netmask();
|
||||
via = managed_routes->at(i).via;
|
||||
if (target.containsAddress(addr)) {
|
||||
/* DEBUG_EXTRA("chose tap with route <target=%s, nm=%s, via=%s>", target.toString(ipbuf),
|
||||
nm.toString(ipbuf2), via.toString(ipbuf3)); */
|
||||
_vtaps_lock.unlock();
|
||||
return s;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_vtaps_lock.unlock();
|
||||
return tap;
|
||||
}
|
||||
|
||||
VirtualTap *getTapByName(char *ifname)
|
||||
{
|
||||
_vtaps_lock.lock();
|
||||
VirtualTap *s, *tap = nullptr;
|
||||
for (size_t i=0; i<vtaps.size(); i++) {
|
||||
s = (VirtualTap*)vtaps[i];
|
||||
if (strcmp(s->_dev.c_str(), ifname) == false) {
|
||||
tap = s;
|
||||
}
|
||||
}
|
||||
_vtaps_lock.unlock();
|
||||
return tap;
|
||||
}
|
||||
|
||||
VirtualTap *getTapByIndex(size_t index)
|
||||
{
|
||||
_vtaps_lock.lock();
|
||||
VirtualTap *s, *tap = nullptr;
|
||||
for (size_t i=0; i<vtaps.size(); i++) {
|
||||
s = (VirtualTap*)vtaps[i];
|
||||
if (s->ifindex == index) {
|
||||
tap = s;
|
||||
}
|
||||
}
|
||||
_vtaps_lock.unlock();
|
||||
return tap;
|
||||
}
|
||||
|
||||
VirtualTap *getAnyTap()
|
||||
{
|
||||
_vtaps_lock.lock();
|
||||
VirtualTap *vtap = NULL;
|
||||
if (vtaps.size()) {
|
||||
vtap = (VirtualTap *)vtaps[0];
|
||||
}
|
||||
_vtaps_lock.unlock();
|
||||
return vtap;
|
||||
}
|
||||
|
||||
uint64_t zts_get_node_id_from_file(const char *filepath)
|
||||
{
|
||||
std::string fname("identity.public");
|
||||
std::string fpath(filepath);
|
||||
std::string oldid;
|
||||
if (ZeroTier::OSUtils::fileExists((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(), false)) {
|
||||
ZeroTier::OSUtils::readFile((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(), oldid);
|
||||
return Utils::hexStrToU64(oldid.c_str());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 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
|
||||
{
|
||||
DEBUG_INFO("zto-thread, path=%s", homeDir.c_str());
|
||||
// Where network .conf files will be stored
|
||||
netDir = homeDir + "/networks.d";
|
||||
zt1Service = (ZeroTier::OneService *)0;
|
||||
// Construct path for network config and supporting service files
|
||||
if (homeDir.length()) {
|
||||
std::vector<std::string> hpsp(ZeroTier::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 (ZeroTier::OSUtils::mkdir(ptmp) == false) {
|
||||
DEBUG_ERROR("home path does not exist, and could not create");
|
||||
perror("error\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
DEBUG_ERROR("homeDir is empty, could not construct path");
|
||||
return NULL;
|
||||
}
|
||||
if (servicePort <= 0) {
|
||||
DEBUG_INFO("no port specified, will bind to random port. use zts_set_service_port() if you want.");
|
||||
}
|
||||
else {
|
||||
DEBUG_INFO("binding to port=%d", servicePort);
|
||||
}
|
||||
for (;;) {
|
||||
zt1Service = ZeroTier::OneService::newInstance(homeDir.c_str(),servicePort);
|
||||
switch(zt1Service->run()) {
|
||||
case ZeroTier::OneService::ONE_STILL_RUNNING:
|
||||
case ZeroTier::OneService::ONE_NORMAL_TERMINATION:
|
||||
break;
|
||||
case ZeroTier::OneService::ONE_UNRECOVERABLE_ERROR:
|
||||
DEBUG_ERROR("fatal error: %s",zt1Service->fatalErrorMessage().c_str());
|
||||
break;
|
||||
case ZeroTier::OneService::ONE_IDENTITY_COLLISION: {
|
||||
delete zt1Service;
|
||||
zt1Service = (ZeroTier::OneService *)0;
|
||||
std::string oldid;
|
||||
ZeroTier::OSUtils::readFile((homeDir + ZT_PATH_SEPARATOR_S
|
||||
+ "identity.secret").c_str(),oldid);
|
||||
if (oldid.length()) {
|
||||
ZeroTier::OSUtils::writeFile((homeDir + ZT_PATH_SEPARATOR_S
|
||||
+ "identity.secret.saved_after_collision").c_str(),oldid);
|
||||
ZeroTier::OSUtils::rm((homeDir + ZT_PATH_SEPARATOR_S
|
||||
+ "identity.secret").c_str());
|
||||
ZeroTier::OSUtils::rm((homeDir + ZT_PATH_SEPARATOR_S
|
||||
+ "identity.public").c_str());
|
||||
}
|
||||
}
|
||||
continue; // restart!
|
||||
}
|
||||
break; // terminate loop -- normally we don't keep restarting
|
||||
}
|
||||
delete zt1Service;
|
||||
zt1Service = (ZeroTier::OneService *)0;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int zts_get_num_assigned_addresses(const uint64_t nwid)
|
||||
{
|
||||
if (!zt1Service) {
|
||||
return -1;
|
||||
}
|
||||
VirtualTap *tap = getTapByNWID(nwid);
|
||||
if (!tap) {
|
||||
return -1;
|
||||
}
|
||||
int sz = -1;
|
||||
_vtaps_lock.lock();
|
||||
sz = tap->_ips.size();
|
||||
_vtaps_lock.unlock();
|
||||
return sz;
|
||||
}
|
||||
|
||||
int zts_get_address_at_index(
|
||||
const uint64_t nwid, const int index, struct sockaddr *addr, socklen_t *addrlen)
|
||||
{
|
||||
if (!zt1Service) {
|
||||
return -1;
|
||||
}
|
||||
VirtualTap *tap = getTapByNWID(nwid);
|
||||
int err = -1;
|
||||
if (!tap) {
|
||||
return err;
|
||||
}
|
||||
_vtaps_lock.lock();
|
||||
if (index > -1 && index <= (int)tap->_ips.size()) {
|
||||
memcpy(addr, &(tap->_ips[index]), *addrlen);
|
||||
*addrlen = tap->_ips[index].isV4() ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
|
||||
err = 0;
|
||||
}
|
||||
_vtaps_lock.unlock();
|
||||
return err;
|
||||
}
|
||||
|
||||
/****************************************************************************/
|
||||
/* ZeroTier Service Controls */
|
||||
/****************************************************************************/
|
||||
|
||||
int zts_set_service_port(int portno)
|
||||
{
|
||||
if (portno > -1 && portno < 65535) {
|
||||
// 0 is allowed, signals zt service to bind to a random port
|
||||
servicePort = portno;
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int zts_get_address(const uint64_t nwid, struct sockaddr_storage *addr,
|
||||
const int address_family)
|
||||
{
|
||||
int err = -1;
|
||||
if (!zt1Service) {
|
||||
return -1;
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
int zts_join(const uint64_t nwid)
|
||||
{
|
||||
DEBUG_INFO("joining %llx", (unsigned long long)nwid);
|
||||
if (nwid == 0) {
|
||||
return -1;
|
||||
}
|
||||
if (zt1Service) {
|
||||
zt1Service->join(nwid);
|
||||
}
|
||||
// provide ZTO service reference to virtual taps
|
||||
// TODO: This might prove to be unreliable, but it works for now
|
||||
_vtaps_lock.lock();
|
||||
for (size_t i=0;i<vtaps.size(); i++) {
|
||||
VirtualTap *s = (VirtualTap*)vtaps[i];
|
||||
s->zt1ServiceRef=(void*)zt1Service;
|
||||
}
|
||||
_vtaps_lock.unlock();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int zts_leave(const uint64_t nwid)
|
||||
{
|
||||
DEBUG_INFO("leaving %llx", (unsigned long long)nwid);
|
||||
if (nwid == 0) {
|
||||
return -1;
|
||||
}
|
||||
if (zt1Service) {
|
||||
zt1Service->leave(nwid);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int zts_core_running()
|
||||
{
|
||||
return zt1Service == NULL ? false : zt1Service->isRunning();
|
||||
}
|
||||
|
||||
int zts_stack_running()
|
||||
{
|
||||
_vtaps_lock.lock();
|
||||
// TODO: Perhaps a more robust way to check for this
|
||||
int running = vtaps.size() > 0 ? true : false;
|
||||
_vtaps_lock.unlock();
|
||||
return running;
|
||||
}
|
||||
|
||||
int zts_ready()
|
||||
{
|
||||
return zts_core_running() && zts_stack_running();
|
||||
}
|
||||
|
||||
int zts_start(const char *path, int blocking = false)
|
||||
{
|
||||
if (zt1Service) {
|
||||
return 0; // already initialized, ok
|
||||
}
|
||||
if (path) {
|
||||
homeDir = path;
|
||||
}
|
||||
int err = 0;
|
||||
#if defined(_WIN32)
|
||||
WSAStartup(MAKEWORD(2, 2), &wsaData); // initialize WinSock. Used in Phy for loopback pipe
|
||||
HANDLE thr = CreateThread(NULL, 0, zts_start_service, NULL, 0, NULL);
|
||||
#else
|
||||
pthread_t service_thread;
|
||||
err = pthread_create(&service_thread, NULL, zts_start_service, NULL);
|
||||
#endif
|
||||
|
||||
if (blocking) { // block to prevent service calls before we're ready
|
||||
ZT_NodeStatus status;
|
||||
status.online = 0;
|
||||
DEBUG_EXTRA("waiting for zerotier service thread to start");
|
||||
while (zts_core_running() == false || zt1Service->getNode() == NULL) {
|
||||
api_sleep(ZTO_WRAPPER_CHECK_INTERVAL);
|
||||
}
|
||||
DEBUG_EXTRA("waiting for node address assignment");
|
||||
while (zt1Service->getNode()->address() <= 0) {
|
||||
api_sleep(ZTO_WRAPPER_CHECK_INTERVAL);
|
||||
}
|
||||
DEBUG_EXTRA("node=%llx", (unsigned long long)zts_get_node_id());
|
||||
DEBUG_EXTRA("waiting for node to come online. ensure the node is authorized to join the network");
|
||||
while (status.online <= 0) {
|
||||
api_sleep(ZTO_WRAPPER_CHECK_INTERVAL);
|
||||
zt1Service->getNode()->status(&status);
|
||||
}
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
int zts_startjoin(const char *path, const uint64_t nwid)
|
||||
{
|
||||
int err = zts_start(path, true);
|
||||
while (true) {
|
||||
try {
|
||||
zts_join(nwid);
|
||||
break;
|
||||
}
|
||||
catch( ... ) {
|
||||
DEBUG_ERROR("there was a problem joining the virtual network %llx",
|
||||
(unsigned long long)nwid);
|
||||
api_sleep(ZTO_WRAPPER_CHECK_INTERVAL);
|
||||
}
|
||||
}
|
||||
while (zts_has_address(nwid) == false) {
|
||||
api_sleep(ZTO_WRAPPER_CHECK_INTERVAL);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
void zts_stop()
|
||||
{
|
||||
if (zt1Service) {
|
||||
zt1Service->terminate();
|
||||
// disableTaps();
|
||||
}
|
||||
#if defined(_WIN32)
|
||||
WSACleanup();
|
||||
#endif
|
||||
}
|
||||
|
||||
void zts_get_path(char *homePath, size_t len)
|
||||
{
|
||||
if (homeDir.length()) {
|
||||
memset(homePath, 0, len);
|
||||
size_t buf_len = len < homeDir.length() ? len : homeDir.length();
|
||||
memcpy(homePath, homeDir.c_str(), buf_len);
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t zts_get_node_id()
|
||||
{
|
||||
if (zt1Service) {
|
||||
return zt1Service->getNode()->address();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned long zts_get_peer_count()
|
||||
{
|
||||
if (zt1Service) {
|
||||
return zt1Service->getNode()->peers()->peerCount;
|
||||
}
|
||||
else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
bool _ipv6_in_subnet(ZeroTier::InetAddress *subnet, ZeroTier::InetAddress *addr)
|
||||
{
|
||||
ZeroTier::InetAddress r(addr);
|
||||
ZeroTier::InetAddress b(subnet);
|
||||
const unsigned int bits = subnet->netmaskBits();
|
||||
switch(r.ss_family) {
|
||||
case AF_INET:
|
||||
reinterpret_cast<struct sockaddr_in *>(&r)->sin_addr.s_addr &= ZeroTier::Utils::hton((uint32_t)(0xffffffff << (32 - bits)));
|
||||
break;
|
||||
case AF_INET6: {
|
||||
uint64_t nm[2];
|
||||
uint64_t nm2[2];
|
||||
memcpy(nm,reinterpret_cast<struct sockaddr_in6 *>(&r)->sin6_addr.s6_addr,16);
|
||||
memcpy(nm2,reinterpret_cast<struct sockaddr_in6 *>(&b)->sin6_addr.s6_addr,16);
|
||||
|
||||
nm[0] &= ZeroTier::Utils::hton((uint64_t)((bits >= 64) ? 0xffffffffffffffffULL : (0xffffffffffffffffULL << (64 - bits))));
|
||||
nm[1] &= ZeroTier::Utils::hton((uint64_t)((bits <= 64) ? 0ULL : (0xffffffffffffffffULL << (128 - bits))));
|
||||
|
||||
nm2[0] &= ZeroTier::Utils::hton((uint64_t)((bits >= 64) ? 0xffffffffffffffffULL : (0xffffffffffffffffULL << (64 - bits))));
|
||||
nm2[1] &= ZeroTier::Utils::hton((uint64_t)((bits <= 64) ? 0ULL : (0xffffffffffffffffULL << (128 - bits))));
|
||||
|
||||
memcpy(reinterpret_cast<struct sockaddr_in6 *>(&r)->sin6_addr.s6_addr,nm,16);
|
||||
memcpy(reinterpret_cast<struct sockaddr_in6 *>(&b)->sin6_addr.s6_addr,nm2,16);
|
||||
}
|
||||
break;
|
||||
}
|
||||
char b0[64], b1[64];
|
||||
memset(b0, 0, 64);
|
||||
memset(b1, 0, 64);
|
||||
return !strcmp(r.toIpString(b0), b.toIpString(b1));
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
717
src/libzt.cpp
717
src/libzt.cpp
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* ZeroTier SDK - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2018 ZeroTier, Inc. https://www.zerotier.com/
|
||||
* 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
|
||||
@@ -13,7 +13,7 @@
|
||||
* 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/>.
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
@@ -27,99 +27,328 @@
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* Application-facing, socket-like API
|
||||
* ZeroTier Socket API
|
||||
*/
|
||||
|
||||
#include "libztDefs.h"
|
||||
#include <string.h>
|
||||
|
||||
#include "lwip/sockets.h"
|
||||
#include "lwip/ip_addr.h"
|
||||
#include "lwip/netdb.h"
|
||||
#include "Defs.hpp"
|
||||
#include "libzt.h"
|
||||
#include "Debug.hpp"
|
||||
|
||||
#include <string.h>
|
||||
#ifdef SDK_JNI
|
||||
#include <jni.h>
|
||||
#ifndef _MSC_VER
|
||||
//#include <sys/socket.h>
|
||||
//#include <sys/types.h>
|
||||
//#include <sys/select.h>
|
||||
//#include <sys/ioctl.h>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
void ss2zta(JNIEnv *env, struct sockaddr_storage *ss, jobject addr);
|
||||
void zta2ss(JNIEnv *env, struct sockaddr_storage *ss, jobject addr);
|
||||
void ztfdset2fdset(JNIEnv *env, int nfds, jobject src_ztfd_set, fd_set *dest_fd_set);
|
||||
void fdset2ztfdset(JNIEnv *env, int nfds, fd_set *src_fd_set, jobject dest_ztfd_set);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Socket API //
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int platform_adjusted_socket_family(int family);
|
||||
void fix_addr_socket_family(struct sockaddr *addr);
|
||||
bool zts_ready();
|
||||
// lwIP prototypes copied from lwip/src/include/sockets.h
|
||||
// Don't call these directly, call zts_* functions instead
|
||||
int lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen);
|
||||
int lwip_bind(int s, const struct sockaddr *name, socklen_t namelen);
|
||||
int lwip_shutdown(int s, int how);
|
||||
int lwip_getpeername (int s, struct sockaddr *name, socklen_t *namelen);
|
||||
int lwip_getsockname (int s, struct sockaddr *name, socklen_t *namelen);
|
||||
int lwip_getsockopt (int s, int level, int optname, void *optval, socklen_t *optlen);
|
||||
int lwip_setsockopt (int s, int level, int optname, const void *optval, socklen_t optlen);
|
||||
int lwip_close(int s);
|
||||
int lwip_connect(int s, const struct sockaddr *name, socklen_t namelen);
|
||||
int lwip_listen(int s, int backlog);
|
||||
int lwip_recv(int s, void *mem, size_t len, int flags);
|
||||
int lwip_read(int s, void *mem, size_t len);
|
||||
int lwip_recvfrom(int s, void *mem, size_t len, int flags,
|
||||
struct sockaddr *from, socklen_t *fromlen);
|
||||
int lwip_send(int s, const void *dataptr, size_t size, int flags);
|
||||
int lwip_sendmsg(int s, const struct msghdr *message, int flags);
|
||||
int lwip_sendto(int s, const void *dataptr, size_t size, int flags,
|
||||
const struct sockaddr *to, socklen_t tolen);
|
||||
int lwip_socket(int domain, int type, int protocol);
|
||||
int lwip_write(int s, const void *dataptr, size_t size);
|
||||
int lwip_writev(int s, const struct iovec *iov, int iovcnt);
|
||||
int lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
|
||||
struct timeval *timeout);
|
||||
int lwip_ioctl(int s, long cmd, void *argp);
|
||||
int lwip_fcntl(int s, int cmd, int val);
|
||||
|
||||
// Copied from lwip/src/include/sockets.h and renamed to prevent a name collision
|
||||
// with system definitions
|
||||
struct lwip_sockaddr {
|
||||
u8_t sa_len;
|
||||
sa_family_t sa_family;
|
||||
char sa_data[14];
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// ZeroTier Socket API //
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
int zts_ready();
|
||||
|
||||
int zts_socket(int socket_family, int socket_type, int protocol)
|
||||
{
|
||||
int socket_family_adj = platform_adjusted_socket_family(socket_family);
|
||||
return !zts_ready() ? -1 : lwip_socket(socket_family_adj, socket_type, protocol);
|
||||
return !zts_ready() ? ZTS_ERR_SERVICE : lwip_socket(socket_family, socket_type, protocol);
|
||||
}
|
||||
#ifdef SDK_JNI
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_socket(
|
||||
JNIEnv *env, jobject thisObj, jint family, jint type, jint protocol)
|
||||
{
|
||||
return zts_socket(family, type, protocol);
|
||||
}
|
||||
#endif
|
||||
|
||||
int zts_connect(int fd, const struct sockaddr *addr, socklen_t addrlen)
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
memcpy(&ss, addr, addrlen);
|
||||
fix_addr_socket_family((struct sockaddr*)&ss);
|
||||
return !zts_ready() ? -1 : lwip_connect(fd, (struct sockaddr*)&ss, addrlen);
|
||||
if (!addr) {
|
||||
return ZTS_ERR_INVALID_ARG;
|
||||
}
|
||||
if (addrlen > (int)sizeof(struct sockaddr_storage) || addrlen < (int)sizeof(struct sockaddr_in)) {
|
||||
return ZTS_ERR_INVALID_ARG;
|
||||
}
|
||||
return !zts_ready() ? ZTS_ERR_SERVICE : lwip_connect(fd, addr, addrlen);
|
||||
}
|
||||
#ifdef SDK_JNI
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_connect(
|
||||
JNIEnv *env, jobject thisObj, jint fd, jobject addr)
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
zta2ss(env, &ss, addr);
|
||||
socklen_t addrlen = ss.ss_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
|
||||
return zts_connect(fd, (struct sockaddr *)&ss, addrlen);
|
||||
}
|
||||
#endif
|
||||
|
||||
int zts_bind(int fd, const struct sockaddr *addr, socklen_t addrlen)
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
memcpy(&ss, addr, addrlen);
|
||||
fix_addr_socket_family((struct sockaddr*)&ss);
|
||||
return !zts_ready() ? -1 : lwip_bind(fd, (struct sockaddr*)&ss, addrlen);
|
||||
if (!addr) {
|
||||
return ZTS_ERR_INVALID_ARG;
|
||||
}
|
||||
if (addrlen > (int)sizeof(struct sockaddr_storage) || addrlen < (int)sizeof(struct sockaddr_in)) {
|
||||
return ZTS_ERR_INVALID_ARG;
|
||||
}
|
||||
return !zts_ready() ? ZTS_ERR_SERVICE : lwip_bind(fd, addr, addrlen);
|
||||
}
|
||||
#ifdef SDK_JNI
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_bind(
|
||||
JNIEnv *env, jobject thisObj, jint fd, jobject addr)
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
zta2ss(env, &ss, addr);
|
||||
socklen_t addrlen = ss.ss_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
|
||||
return zts_bind(fd, (struct sockaddr*)&ss, addrlen);
|
||||
}
|
||||
#endif
|
||||
|
||||
int zts_listen(int fd, int backlog)
|
||||
{
|
||||
return !zts_ready() ? -1 : lwip_listen(fd, backlog);
|
||||
return !zts_ready() ? ZTS_ERR_SERVICE : lwip_listen(fd, backlog);
|
||||
}
|
||||
#ifdef SDK_JNI
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_listen(
|
||||
JNIEnv *env, jobject thisObj, jint fd, int backlog)
|
||||
{
|
||||
return zts_listen(fd, backlog);
|
||||
}
|
||||
#endif
|
||||
|
||||
int zts_accept(int fd, struct sockaddr *addr, socklen_t *addrlen)
|
||||
{
|
||||
return !zts_ready() ? -1 : lwip_accept(fd, addr, addrlen);
|
||||
return !zts_ready() ? ZTS_ERR_SERVICE : lwip_accept(fd, addr, addrlen);
|
||||
}
|
||||
#ifdef SDK_JNI
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_accept(
|
||||
JNIEnv *env, jobject thisObj, jint fd, jobject addr, jint port)
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
socklen_t addrlen = sizeof(struct sockaddr_storage);
|
||||
int err = zts_accept(fd, (struct sockaddr *)&ss, &addrlen);
|
||||
ss2zta(env, &ss, addr);
|
||||
return err;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(__linux__)
|
||||
int zts_accept4(int fd, struct sockaddr *addr, socklen_t *addrlen, int flags)
|
||||
{
|
||||
return !zts_ready() ? -1 : -1; // lwip_accept4(fd, addr, addrlen, flags);
|
||||
return !zts_ready() ? ZTS_ERR_INVALID_OP : ZTS_ERR_INVALID_OP;
|
||||
}
|
||||
#endif
|
||||
#ifdef SDK_JNI
|
||||
#if defined(__linux__)
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_accept4(
|
||||
JNIEnv *env, jobject thisObj, jint fd, jobject addr, jint port, jint flags)
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
socklen_t addrlen = sizeof(struct sockaddr_storage);
|
||||
int err = zts_accept4(fd, (struct sockaddr *)&ss, &addrlen, flags);
|
||||
ss2zta(env, &ss, addr);
|
||||
return err;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
int zts_setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen)
|
||||
{
|
||||
return !zts_ready() ? -1 : lwip_setsockopt(fd, level, optname, optval, optlen);
|
||||
return !zts_ready() ? ZTS_ERR_SERVICE : lwip_setsockopt(fd, level, optname, optval, optlen);
|
||||
}
|
||||
#ifdef SDK_JNI
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_setsockopt(
|
||||
JNIEnv *env, jobject thisObj, jint fd, jint level, jint optname, jobject optval)
|
||||
{
|
||||
jclass c = (*env).GetObjectClass(optval);
|
||||
if (!c) {
|
||||
return ZTS_ERR_INVALID_OP;
|
||||
}
|
||||
int optval_int = -1;
|
||||
|
||||
if (optname == ZTS_SO_BROADCAST
|
||||
|| optname == ZTS_SO_KEEPALIVE
|
||||
|| optname == ZTS_SO_REUSEADDR
|
||||
|| optname == ZTS_SO_REUSEPORT
|
||||
|| optname == ZTS_TCP_NODELAY)
|
||||
{
|
||||
jfieldID fid = (*env).GetFieldID(c, "booleanValue", "B");
|
||||
optval_int = (int)(*env).GetBooleanField(optval, fid);
|
||||
}
|
||||
if (optname == ZTS_IP_TTL
|
||||
|| optname == ZTS_IP_TOS
|
||||
|| optname == ZTS_SO_LINGER
|
||||
|| optname == ZTS_SO_RCVBUF
|
||||
|| optname == ZTS_SO_SNDBUF)
|
||||
{
|
||||
jfieldID fid = (*env).GetFieldID(c, "integerValue", "I");
|
||||
optval_int = (*env).GetIntField(optval, fid);
|
||||
}
|
||||
int optlen = sizeof(optval_int);
|
||||
return zts_setsockopt(fd, level, optname, &optval_int, optlen);
|
||||
}
|
||||
#endif
|
||||
|
||||
int zts_getsockopt(int fd, int level, int optname, void *optval, socklen_t *optlen)
|
||||
{
|
||||
return !zts_ready() ? -1 : lwip_getsockopt(fd, level, optname, optval, optlen);
|
||||
return !zts_ready() ? ZTS_ERR_SERVICE : lwip_getsockopt(fd, level, optname, optval, optlen);
|
||||
}
|
||||
#ifdef SDK_JNI
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_getsockopt(
|
||||
JNIEnv *env, jobject thisObj, jint fd, jint level, jint optname, jobject optval)
|
||||
{
|
||||
jclass c = (*env).GetObjectClass(optval);
|
||||
if (!c) {
|
||||
return ZTS_ERR_INVALID_OP;
|
||||
}
|
||||
int optval_int;
|
||||
int optlen; // Intentionally not used
|
||||
int err = ZTS_ERR_OK;
|
||||
err = zts_getsockopt(fd, level, optname, &optval_int, &optlen);
|
||||
if (optname == ZTS_SO_BROADCAST
|
||||
|| optname == ZTS_SO_KEEPALIVE
|
||||
|| optname == ZTS_SO_REUSEADDR
|
||||
|| optname == ZTS_SO_REUSEPORT
|
||||
|| optname == ZTS_TCP_NODELAY)
|
||||
{
|
||||
jfieldID fid = (*env).GetFieldID(c, "isBoolean", "B");
|
||||
(*env).SetBooleanField(optval, fid, true);
|
||||
fid = (*env).GetFieldID(c, "booleanValue", "B");
|
||||
(*env).SetBooleanField(optval, fid, (bool)optval_int);
|
||||
}
|
||||
if (optname == ZTS_IP_TTL
|
||||
|| optname == ZTS_IP_TOS
|
||||
|| optname == ZTS_SO_LINGER
|
||||
|| optname == ZTS_SO_RCVBUF
|
||||
|| optname == ZTS_SO_SNDBUF)
|
||||
{
|
||||
jfieldID fid = (*env).GetFieldID(c, "isInteger", "B");
|
||||
(*env).SetBooleanField(optval, fid, true);
|
||||
fid = (*env).GetFieldID(c, "integerValue", "I");
|
||||
(*env).SetIntField(optval, fid, optval_int);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
#endif
|
||||
|
||||
int zts_getsockname(int fd, struct sockaddr *addr, socklen_t *addrlen)
|
||||
{
|
||||
return !zts_ready() ? -1 : lwip_getsockname(fd, addr, addrlen);
|
||||
if (!addr) {
|
||||
return ZTS_ERR_INVALID_ARG;
|
||||
}
|
||||
if (*addrlen > (int)sizeof(struct sockaddr_storage) || *addrlen < (int)sizeof(struct sockaddr_in)) {
|
||||
return ZTS_ERR_INVALID_ARG;
|
||||
}
|
||||
return !zts_ready() ? ZTS_ERR_SERVICE : lwip_getsockname(fd, addr, addrlen);
|
||||
}
|
||||
#ifdef SDK_JNI
|
||||
JNIEXPORT jboolean JNICALL Java_com_zerotier_libzt_ZeroTier_getsockname(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jobject addr)
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
socklen_t addrlen = sizeof(struct sockaddr_storage);
|
||||
int err = zts_getsockname(fd, (struct sockaddr *)&ss, &addrlen);
|
||||
ss2zta(env, &ss, addr);
|
||||
return err;
|
||||
}
|
||||
#endif
|
||||
|
||||
int zts_getpeername(int fd, struct sockaddr *addr, socklen_t *addrlen)
|
||||
{
|
||||
return !zts_ready() ? -1 : lwip_getpeername(fd, addr, addrlen);
|
||||
if (!addr) {
|
||||
return ZTS_ERR_INVALID_ARG;
|
||||
}
|
||||
if (*addrlen > (int)sizeof(struct sockaddr_storage) || *addrlen < (int)sizeof(struct sockaddr_in)) {
|
||||
return ZTS_ERR_INVALID_ARG;
|
||||
}
|
||||
return !zts_ready() ? ZTS_ERR_SERVICE : lwip_getpeername(fd, addr, addrlen);
|
||||
}
|
||||
#ifdef SDK_JNI
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_getpeername(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jobject addr)
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
int err = zts_getpeername(fd, (struct sockaddr *)&ss, (socklen_t *)sizeof(struct sockaddr_storage));
|
||||
ss2zta(env, &ss, addr);
|
||||
return err;
|
||||
}
|
||||
#endif
|
||||
|
||||
int zts_gethostname(char *name, size_t len)
|
||||
{
|
||||
return !zts_ready() ? -1 : -1; // TODO
|
||||
return !zts_ready() ? ZTS_ERR_INVALID_OP : ZTS_ERR_INVALID_OP; // TODO
|
||||
}
|
||||
#ifdef SDK_JNI
|
||||
#endif
|
||||
|
||||
int zts_sethostname(const char *name, size_t len)
|
||||
{
|
||||
return !zts_ready() ? -1 : -1; // TODO
|
||||
return !zts_ready() ? ZTS_ERR_INVALID_OP : ZTS_ERR_INVALID_OP; // TODO
|
||||
}
|
||||
#ifdef SDK_JNI
|
||||
#endif
|
||||
|
||||
struct hostent *zts_gethostbyname(const char *name)
|
||||
{
|
||||
if (zts_ready() == false) {
|
||||
return NULL;
|
||||
}
|
||||
return (struct hostent *)(!zts_ready() ? NULL : NULL);
|
||||
// TODO: Test thread safety
|
||||
/*
|
||||
char buf[256];
|
||||
@@ -138,19 +367,63 @@ struct hostent *zts_gethostbyname(const char *name)
|
||||
|
||||
return lwip_gethostbyname(name);
|
||||
*/
|
||||
return NULL;
|
||||
}
|
||||
#ifdef SDK_JNI
|
||||
#endif
|
||||
|
||||
int zts_close(int fd)
|
||||
{
|
||||
return !zts_ready() ? -1 : lwip_close(fd);
|
||||
return !zts_ready() ? ZTS_ERR_SERVICE : lwip_close(fd);
|
||||
}
|
||||
#ifdef SDK_JNI
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_close(
|
||||
JNIEnv *env, jobject thisObj, jint fd)
|
||||
{
|
||||
return zts_close(fd);
|
||||
}
|
||||
#endif
|
||||
|
||||
int zts_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
|
||||
struct timeval *timeout)
|
||||
{
|
||||
return !zts_ready() ? -1 : lwip_select(nfds, readfds, writefds, exceptfds, timeout);
|
||||
return !zts_ready() ? ZTS_ERR_SERVICE : lwip_select(nfds, readfds, writefds, exceptfds, timeout);
|
||||
}
|
||||
#ifdef SDK_JNI
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_select(JNIEnv *env, jobject thisObj,
|
||||
jint nfds, jobject readfds, jobject writefds, jobject exceptfds, jint timeout_sec, jint timeout_usec)
|
||||
{
|
||||
struct timeval _timeout;
|
||||
_timeout.tv_sec = timeout_sec;
|
||||
_timeout.tv_usec = timeout_usec;
|
||||
fd_set _readfds, _writefds, _exceptfds;
|
||||
fd_set *r = NULL;
|
||||
fd_set *w = NULL;
|
||||
fd_set *e = NULL;
|
||||
if (readfds) {
|
||||
r = &_readfds;
|
||||
ztfdset2fdset(env, nfds, readfds, &_readfds);
|
||||
}
|
||||
if (writefds) {
|
||||
w = &_writefds;
|
||||
ztfdset2fdset(env, nfds, writefds, &_writefds);
|
||||
}
|
||||
if (exceptfds) {
|
||||
e = &_exceptfds;
|
||||
ztfdset2fdset(env, nfds, exceptfds, &_exceptfds);
|
||||
}
|
||||
int err = zts_select(nfds, r, w, e, &_timeout);
|
||||
if (readfds) {
|
||||
fdset2ztfdset(env, nfds, &_readfds, readfds);
|
||||
}
|
||||
if (writefds) {
|
||||
fdset2ztfdset(env, nfds, &_writefds, writefds);
|
||||
}
|
||||
if (exceptfds) {
|
||||
fdset2ztfdset(env, nfds, &_exceptfds, exceptfds);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
#endif
|
||||
|
||||
int zts_fcntl(int fd, int cmd, int flags)
|
||||
{
|
||||
@@ -166,131 +439,369 @@ int zts_fcntl(int fd, int cmd, int flags)
|
||||
translated_flags = 1;
|
||||
}
|
||||
#endif
|
||||
return !zts_ready() ? -1 : lwip_fcntl(fd, cmd, translated_flags);
|
||||
return !zts_ready() ? ZTS_ERR_SERVICE : lwip_fcntl(fd, cmd, translated_flags);
|
||||
}
|
||||
#ifdef SDK_JNI
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_fcntl(
|
||||
JNIEnv *env, jobject thisObj, jint fd, jint cmd, jint flags)
|
||||
{
|
||||
return zts_fcntl(fd, cmd, flags);
|
||||
}
|
||||
#endif
|
||||
|
||||
int zts_ioctl(int fd, unsigned long request, void *argp)
|
||||
{
|
||||
return !zts_ready() ? -1 : lwip_ioctl(fd, request, argp);
|
||||
if (!argp) {
|
||||
return ZTS_ERR_INVALID_ARG;
|
||||
}
|
||||
return !zts_ready() ? ZTS_ERR_SERVICE : lwip_ioctl(fd, request, argp);
|
||||
}
|
||||
#ifdef SDK_JNI
|
||||
JNIEXPORT int JNICALL Java_com_zerotier_libzt_ZeroTier_ioctl(
|
||||
JNIEnv *env, jobject thisObj, jint fd, jlong request, jobject argp)
|
||||
{
|
||||
zts_err_t retval = ZTS_ERR_OK;
|
||||
if (request == FIONREAD) {
|
||||
DEBUG_ERROR("FIONREAD");
|
||||
int bytesRemaining = 0;
|
||||
retval = zts_ioctl(fd, request, &bytesRemaining);
|
||||
// set value in general object
|
||||
jclass c = (*env).GetObjectClass(argp);
|
||||
if (!c) {
|
||||
return ZTS_ERR_INVALID_ARG;
|
||||
}
|
||||
jfieldID fid = (*env).GetFieldID(c, "integer", "I");
|
||||
(*env).SetIntField(argp, fid, bytesRemaining);
|
||||
}
|
||||
if (request == FIONBIO) {
|
||||
// TODO: double check
|
||||
int meaninglessVariable = 0;
|
||||
DEBUG_ERROR("FIONBIO");
|
||||
retval = zts_ioctl(fd, request, &meaninglessVariable);
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
#endif
|
||||
|
||||
ssize_t zts_send(int fd, const void *buf, size_t len, int flags)
|
||||
{
|
||||
if (!buf || len <= 0) {
|
||||
return ZTS_ERR_INVALID_ARG;
|
||||
}
|
||||
return !zts_ready() ? ZTS_ERR_SERVICE : lwip_send(fd, buf, len, flags);
|
||||
}
|
||||
#ifdef SDK_JNI
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_send(
|
||||
JNIEnv *env, jobject thisObj, jint fd, jbyteArray buf, int flags)
|
||||
{
|
||||
void *data = env->GetPrimitiveArrayCritical(buf, NULL);
|
||||
int w = zts_send(fd, data, env->GetArrayLength(buf), flags);
|
||||
env->ReleasePrimitiveArrayCritical(buf, data, 0);
|
||||
return w;
|
||||
}
|
||||
#endif
|
||||
|
||||
ssize_t zts_sendto(int fd, const void *buf, size_t len, int flags,
|
||||
const struct sockaddr *addr, socklen_t addrlen)
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
memcpy(&ss, addr, addrlen);
|
||||
fix_addr_socket_family((struct sockaddr*)&ss);
|
||||
return !zts_ready() ? -1 : lwip_sendto(fd, buf, len, flags, (struct sockaddr*)&ss, addrlen);
|
||||
if (!addr || !buf || len <= 0) {
|
||||
return ZTS_ERR_INVALID_ARG;
|
||||
}
|
||||
if (addrlen > (int)sizeof(struct sockaddr_storage) || addrlen < (int)sizeof(struct sockaddr_in)) {
|
||||
return ZTS_ERR_INVALID_ARG;
|
||||
}
|
||||
return !zts_ready() ? ZTS_ERR_SERVICE : lwip_sendto(fd, buf, len, flags, addr, addrlen);
|
||||
}
|
||||
|
||||
ssize_t zts_send(int fd, const void *buf, size_t len, int flags)
|
||||
#ifdef SDK_JNI
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_sendto(
|
||||
JNIEnv *env, jobject thisObj, jint fd, jbyteArray buf, jint flags, jobject addr)
|
||||
{
|
||||
return !zts_ready() ? -1 : lwip_send(fd, buf, len, flags);
|
||||
void *data = env->GetPrimitiveArrayCritical(buf, NULL);
|
||||
struct sockaddr_storage ss;
|
||||
zta2ss(env, &ss, addr);
|
||||
socklen_t addrlen = ss.ss_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
|
||||
int w = zts_sendto(fd, data, env->GetArrayLength(buf), flags, (struct sockaddr *)&ss, addrlen);
|
||||
env->ReleasePrimitiveArrayCritical(buf, data, 0);
|
||||
return w;
|
||||
}
|
||||
#endif
|
||||
|
||||
ssize_t zts_sendmsg(int fd, const struct msghdr *msg, int flags)
|
||||
{
|
||||
return !zts_ready() ? -1 : lwip_sendmsg(fd, msg, flags);
|
||||
return !zts_ready() ? ZTS_ERR_SERVICE : lwip_sendmsg(fd, msg, flags);
|
||||
}
|
||||
#ifdef SDK_JNI
|
||||
#endif
|
||||
|
||||
ssize_t zts_recv(int fd, void *buf, size_t len, int flags)
|
||||
{
|
||||
return !zts_ready() ? -1 : lwip_recv(fd, buf, len, flags);
|
||||
if (!buf) {
|
||||
return ZTS_ERR_INVALID_ARG;
|
||||
}
|
||||
return !zts_ready() ? ZTS_ERR_SERVICE : lwip_recv(fd, buf, len, flags);
|
||||
}
|
||||
#ifdef SDK_JNI
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_recv(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jbyteArray buf, jint flags)
|
||||
{
|
||||
void *data = env->GetPrimitiveArrayCritical(buf, NULL);
|
||||
int r = zts_recv(fd, data, env->GetArrayLength(buf), flags);
|
||||
env->ReleasePrimitiveArrayCritical(buf, data, 0);
|
||||
return r;
|
||||
}
|
||||
#endif
|
||||
|
||||
ssize_t zts_recvfrom(int fd, void *buf, size_t len, int flags,
|
||||
struct sockaddr *addr, socklen_t *addrlen)
|
||||
{
|
||||
return !zts_ready() ? -1 : lwip_recvfrom(fd, buf, len, flags, addr, addrlen);
|
||||
if (!buf) {
|
||||
return ZTS_ERR_INVALID_ARG;
|
||||
}
|
||||
return !zts_ready() ? ZTS_ERR_SERVICE : lwip_recvfrom(fd, buf, len, flags, addr, addrlen);
|
||||
}
|
||||
#ifdef SDK_JNI
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_recvfrom(
|
||||
JNIEnv *env, jobject thisObj, jint fd, jbyteArray buf, jint flags, jobject addr)
|
||||
{
|
||||
socklen_t addrlen = sizeof(struct sockaddr_storage);
|
||||
struct sockaddr_storage ss;
|
||||
void *data = env->GetPrimitiveArrayCritical(buf, NULL);
|
||||
int r = zts_recvfrom(fd, data, env->GetArrayLength(buf), flags, (struct sockaddr *)&ss, &addrlen);
|
||||
env->ReleasePrimitiveArrayCritical(buf, data, 0);
|
||||
ss2zta(env, &ss, addr);
|
||||
return r;
|
||||
}
|
||||
#endif
|
||||
|
||||
ssize_t zts_recvmsg(int fd, struct msghdr *msg, int flags)
|
||||
{
|
||||
return !zts_ready() ? -1 : -1; // Not currently implemented by stack
|
||||
return !zts_ready() ? ZTS_ERR_SERVICE : -1; // Not currently implemented by stack
|
||||
}
|
||||
#ifdef SDK_JNI
|
||||
#endif
|
||||
|
||||
int zts_read(int fd, void *buf, size_t len)
|
||||
{
|
||||
return !zts_ready() ? -1 : lwip_read(fd, buf, len);
|
||||
if (!buf) {
|
||||
return ZTS_ERR_INVALID_ARG;
|
||||
}
|
||||
return !zts_ready() ? ZTS_ERR_SERVICE : lwip_read(fd, buf, len);
|
||||
}
|
||||
int zts_read_offset(int fd, void *buf, size_t offset, size_t len)
|
||||
{
|
||||
if (!buf) {
|
||||
return ZTS_ERR_INVALID_ARG;
|
||||
}
|
||||
char *cbuf = (char*)buf;
|
||||
return !zts_ready() ? ZTS_ERR_SERVICE : lwip_read(fd, &(cbuf[offset]), len);
|
||||
}
|
||||
#ifdef SDK_JNI
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_read(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jbyteArray buf)
|
||||
{
|
||||
void *data = env->GetPrimitiveArrayCritical(buf, NULL);
|
||||
int r = zts_read(fd, data, env->GetArrayLength(buf));
|
||||
env->ReleasePrimitiveArrayCritical(buf, data, 0);
|
||||
return r;
|
||||
}
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_read_1offset(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jbyteArray buf, jint offset, jint len)
|
||||
{
|
||||
void *data = env->GetPrimitiveArrayCritical(buf, NULL);
|
||||
int r = zts_read_offset(fd, data, offset, len);
|
||||
env->ReleasePrimitiveArrayCritical(buf, data, 0);
|
||||
return r;
|
||||
}
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_read_1length(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jbyteArray buf, jint len)
|
||||
{
|
||||
void *data = env->GetPrimitiveArrayCritical(buf, NULL);
|
||||
int r = zts_read(fd, data, len);
|
||||
env->ReleasePrimitiveArrayCritical(buf, data, 0);
|
||||
return r;
|
||||
}
|
||||
#endif
|
||||
|
||||
int zts_write(int fd, const void *buf, size_t len)
|
||||
{
|
||||
return !zts_ready() ? -1 : lwip_write(fd, buf, len);
|
||||
if (!buf || len <= 0) {
|
||||
return ZTS_ERR_INVALID_ARG;
|
||||
}
|
||||
return !zts_ready() ? ZTS_ERR_SERVICE : lwip_write(fd, buf, len);
|
||||
}
|
||||
#ifdef SDK_JNI
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_write__IB(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jbyteArray buf)
|
||||
{
|
||||
void *data = env->GetPrimitiveArrayCritical(buf, NULL);
|
||||
int w = zts_write(fd, data, env->GetArrayLength(buf));
|
||||
env->ReleasePrimitiveArrayCritical(buf, data, 0);
|
||||
return w;
|
||||
}
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_write_1offset(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jbyteArray buf, jint offset, jint len)
|
||||
{
|
||||
void *data = env->GetPrimitiveArrayCritical(&(buf[offset]), NULL); // PENDING: check?
|
||||
int w = zts_write(fd, data, len);
|
||||
env->ReleasePrimitiveArrayCritical(buf, data, 0);
|
||||
return w;
|
||||
}
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_write_1byte(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jbyte buf)
|
||||
{
|
||||
return zts_write(fd, &buf, 1);
|
||||
}
|
||||
#endif
|
||||
|
||||
int zts_shutdown(int fd, int how)
|
||||
{
|
||||
return !zts_ready() ? -1 : lwip_shutdown(fd, how);
|
||||
return !zts_ready() ? ZTS_ERR_SERVICE : lwip_shutdown(fd, how);
|
||||
}
|
||||
#ifdef SDK_JNI
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_shutdown(
|
||||
JNIEnv *env, jobject thisObj, int fd, int how)
|
||||
{
|
||||
return zts_shutdown(fd, how);
|
||||
}
|
||||
#endif
|
||||
|
||||
int zts_add_dns_nameserver(struct sockaddr *addr)
|
||||
{
|
||||
return !zts_ready() ? -1 : -1; // TODO
|
||||
return !zts_ready() ? ZTS_ERR_SERVICE : -1; // TODO
|
||||
}
|
||||
#ifdef SDK_JNI
|
||||
#endif
|
||||
|
||||
int zts_del_dns_nameserver(struct sockaddr *addr)
|
||||
{
|
||||
return !zts_ready() ? -1 : -1; // TODO
|
||||
return !zts_ready() ? ZTS_ERR_SERVICE : -1; // TODO
|
||||
}
|
||||
#ifdef SDK_JNI
|
||||
#endif
|
||||
|
||||
/* The rationale for the following correctional methods is as follows:
|
||||
|
||||
Since we don't want the user of this library to worry about naming conflicts
|
||||
with their native OS/platform's socket facilities we deliberately isolate what
|
||||
is used by the user-space network stack and stack drivers from the user's
|
||||
application. As a result of this, we must compensate for a few things on our
|
||||
side. For instance, differing values for AF_INET6 on major operating systems, and
|
||||
differing structure definitions for sockaddr.
|
||||
*/
|
||||
|
||||
/* adjust socket_family value (when AF_INET6) for various platforms:
|
||||
linux : 10
|
||||
macOS : 30
|
||||
windows: 23
|
||||
*/
|
||||
int platform_adjusted_socket_family(int family)
|
||||
#ifdef SDK_JNI
|
||||
void ztfdset2fdset(JNIEnv *env, int nfds, jobject src_ztfd_set, fd_set *dest_fd_set)
|
||||
{
|
||||
#if defined(__linux__)
|
||||
return family; // do nothing
|
||||
#endif
|
||||
#if defined(__APPLE__)
|
||||
return family == 30 ? AF_INET6 : family; // 10
|
||||
#endif
|
||||
#if defined(_WIN32)
|
||||
if (family == 23) {
|
||||
return AF_INET6;
|
||||
}
|
||||
if (family == 2) {
|
||||
return AF_INET;
|
||||
}
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
void fix_addr_socket_family(struct sockaddr *addr)
|
||||
{
|
||||
#if defined(__linux__) || defined(_WIN32)
|
||||
/* struct sockaddr on Linux and Windows don't contain an sa_len field
|
||||
so we must adjust it here before feeding it into the stack. */
|
||||
if (addr->sa_len == 2) {
|
||||
if (addr->sa_family == 0) {
|
||||
addr->sa_family = addr->sa_len;
|
||||
addr->sa_len = 0;
|
||||
jclass c = (*env).GetObjectClass(src_ztfd_set);
|
||||
if (!c) {
|
||||
return;
|
||||
}
|
||||
FD_ZERO(dest_fd_set);
|
||||
jfieldID fid = env->GetFieldID(c, "fds_bits", "[B");
|
||||
jobject fdData = (*env).GetObjectField (src_ztfd_set, fid);
|
||||
jbyteArray * arr = reinterpret_cast<jbyteArray*>(&fdData);
|
||||
char *data = (char*)(*env).GetByteArrayElements(*arr, NULL);
|
||||
for (int i=0; i<nfds; i++) {
|
||||
if (data[i] == 0x01) {
|
||||
FD_SET(i, dest_fd_set);
|
||||
}
|
||||
}
|
||||
if (addr->sa_len == 10 || addr->sa_len == 23 || addr->sa_len == 30) {
|
||||
if (addr->sa_family == 0) {
|
||||
addr->sa_family = addr->sa_len;
|
||||
addr->sa_len = 0;
|
||||
(*env).ReleaseByteArrayElements(*arr, (jbyte*)data, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
void fdset2ztfdset(JNIEnv *env, int nfds, fd_set *src_fd_set, jobject dest_ztfd_set)
|
||||
{
|
||||
jclass c = (*env).GetObjectClass(dest_ztfd_set);
|
||||
if (!c) {
|
||||
return;
|
||||
}
|
||||
jfieldID fid = env->GetFieldID(c, "fds_bits", "[B");
|
||||
jobject fdData = (*env).GetObjectField (dest_ztfd_set, fid);
|
||||
jbyteArray * arr = reinterpret_cast<jbyteArray*>(&fdData);
|
||||
char *data = (char*)(*env).GetByteArrayElements(*arr, NULL);
|
||||
for (int i=0; i<nfds; i++) {
|
||||
if (FD_ISSET(i, src_fd_set)) {
|
||||
data[i] = 0x01;
|
||||
}
|
||||
}
|
||||
/* once we've moved the value to its anticipated location, convert it from
|
||||
its platform-specific value to one that the network stack can work with */
|
||||
#endif
|
||||
addr->sa_family = platform_adjusted_socket_family(addr->sa_family);
|
||||
(*env).ReleaseByteArrayElements(*arr, (jbyte*)data, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Helpers (for moving data across the JNI barrier) //
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void ss2zta(JNIEnv *env, struct sockaddr_storage *ss, jobject addr)
|
||||
{
|
||||
jclass c = (*env).GetObjectClass(addr);
|
||||
if (!c) {
|
||||
return;
|
||||
}
|
||||
if(ss->ss_family == AF_INET)
|
||||
{
|
||||
struct sockaddr_in *in4 = (struct sockaddr_in*)ss;
|
||||
jfieldID fid = (*env).GetFieldID(c, "_port", "I");
|
||||
(*env).SetIntField(addr, fid, ntohs(in4->sin_port));
|
||||
fid = (*env).GetFieldID(c,"_family", "I");
|
||||
(*env).SetIntField(addr, fid, (in4->sin_family));
|
||||
fid = env->GetFieldID(c, "_ip4", "[B");
|
||||
jobject ipData = (*env).GetObjectField (addr, fid);
|
||||
jbyteArray * arr = reinterpret_cast<jbyteArray*>(&ipData);
|
||||
char *data = (char*)(*env).GetByteArrayElements(*arr, NULL);
|
||||
memcpy(data, &(in4->sin_addr.s_addr), 4);
|
||||
(*env).ReleaseByteArrayElements(*arr, (jbyte*)data, 0);
|
||||
|
||||
return;
|
||||
}
|
||||
if(ss->ss_family == AF_INET6)
|
||||
{
|
||||
struct sockaddr_in6 *in6 = (struct sockaddr_in6*)ss;
|
||||
jfieldID fid = (*env).GetFieldID(c, "_port", "I");
|
||||
(*env).SetIntField(addr, fid, ntohs(in6->sin6_port));
|
||||
fid = (*env).GetFieldID(c,"_family", "I");
|
||||
(*env).SetIntField(addr, fid, (in6->sin6_family));
|
||||
fid = env->GetFieldID(c, "_ip6", "[B");
|
||||
jobject ipData = (*env).GetObjectField (addr, fid);
|
||||
jbyteArray * arr = reinterpret_cast<jbyteArray*>(&ipData);
|
||||
char *data = (char*)(*env).GetByteArrayElements(*arr, NULL);
|
||||
memcpy(data, &(in6->sin6_addr.s6_addr), 16);
|
||||
(*env).ReleaseByteArrayElements(*arr, (jbyte*)data, 0);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void zta2ss(JNIEnv *env, struct sockaddr_storage *ss, jobject addr)
|
||||
{
|
||||
jclass c = (*env).GetObjectClass(addr);
|
||||
if (!c) {
|
||||
return;
|
||||
}
|
||||
jfieldID fid = (*env).GetFieldID(c, "_family", "I");
|
||||
int family = (*env).GetIntField(addr, fid);
|
||||
if (family == AF_INET)
|
||||
{
|
||||
struct sockaddr_in *in4 = (struct sockaddr_in*)ss;
|
||||
fid = (*env).GetFieldID(c, "_port", "I");
|
||||
in4->sin_port = htons((*env).GetIntField(addr, fid));
|
||||
in4->sin_family = AF_INET;
|
||||
fid = env->GetFieldID(c, "_ip4", "[B");
|
||||
jobject ipData = (*env).GetObjectField (addr, fid);
|
||||
jbyteArray * arr = reinterpret_cast<jbyteArray*>(&ipData);
|
||||
char *data = (char*)(*env).GetByteArrayElements(*arr, NULL);
|
||||
memcpy(&(in4->sin_addr.s_addr), data, 4);
|
||||
(*env).ReleaseByteArrayElements(*arr, (jbyte*)data, 0);
|
||||
return;
|
||||
}
|
||||
if (family == AF_INET6)
|
||||
{
|
||||
struct sockaddr_in6 *in6 = (struct sockaddr_in6*)ss;
|
||||
jfieldID fid = (*env).GetFieldID(c, "_port", "I");
|
||||
in6->sin6_port = htons((*env).GetIntField(addr, fid));
|
||||
fid = (*env).GetFieldID(c,"_family", "I");
|
||||
in6->sin6_family = AF_INET6;
|
||||
fid = env->GetFieldID(c, "_ip6", "[B");
|
||||
jobject ipData = (*env).GetObjectField (addr, fid);
|
||||
jbyteArray * arr = reinterpret_cast<jbyteArray*>(&ipData);
|
||||
char *data = (char*)(*env).GetByteArrayElements(*arr, NULL);
|
||||
memcpy(&(in6->sin6_addr.s6_addr), data, 16);
|
||||
(*env).ReleaseByteArrayElements(*arr, (jbyte*)data, 0);
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endif // JNI
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
519
src/libztJNI.cpp
519
src/libztJNI.cpp
@@ -1,519 +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
|
||||
*
|
||||
* Javs JNI wrapper for POSIX-like socket API
|
||||
* JNI naming convention: Java_PACKAGENAME_CLASSNAME_METHODNAME
|
||||
*/
|
||||
|
||||
#ifdef SDK_JNI
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
//
|
||||
#else
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#endif
|
||||
|
||||
#include "libzt.h"
|
||||
#include "libztDefs.h"
|
||||
|
||||
#include <jni.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
void ss2zta(JNIEnv *env, struct sockaddr_storage *ss, jobject addr);
|
||||
void zta2ss(JNIEnv *env, struct sockaddr_storage *ss, jobject addr);
|
||||
void ztfdset2fdset(JNIEnv *env, int nfds, jobject src_ztfd_set, fd_set *dest_fd_set);
|
||||
void fdset2ztfdset(JNIEnv *env, int nfds, fd_set *src_fd_set, jobject dest_ztfd_set);
|
||||
|
||||
/****************************************************************************/
|
||||
/* ZeroTier service controls */
|
||||
/****************************************************************************/
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL Java_com_zerotier_libzt_ZeroTier_stop(
|
||||
JNIEnv *env, jobject thisObj)
|
||||
{
|
||||
zts_stop();
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL Java_com_zerotier_libzt_ZeroTier_core_1running(
|
||||
JNIEnv *env, jobject thisObj)
|
||||
{
|
||||
return zts_core_running();
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL Java_com_zerotier_libzt_ZeroTier_stack_1running(
|
||||
JNIEnv *env, jobject thisObj)
|
||||
{
|
||||
return zts_stack_running();
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL Java_com_zerotier_libzt_ZeroTier_ready(
|
||||
JNIEnv *env, jobject thisObj)
|
||||
{
|
||||
return zts_ready();
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_join(
|
||||
JNIEnv *env, jobject thisObj, jlong nwid)
|
||||
{
|
||||
return zts_join((uint64_t)nwid);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_leave(
|
||||
JNIEnv *env, jobject thisObj, jlong nwid)
|
||||
{
|
||||
return zts_leave((uint64_t)nwid);
|
||||
}
|
||||
|
||||
JNIEXPORT jstring JNICALL Java_com_zerotier_libzt_ZeroTier_get_1path(
|
||||
JNIEnv *env, jobject thisObj)
|
||||
{
|
||||
char pathBuf[ZT_HOME_PATH_MAX_LEN];
|
||||
zts_get_path(pathBuf, ZT_HOME_PATH_MAX_LEN);
|
||||
return env->NewStringUTF(pathBuf);
|
||||
}
|
||||
|
||||
JNIEXPORT jlong JNICALL Java_com_zerotier_libzt_ZeroTier_get_1node_1id(
|
||||
JNIEnv *env, jobject thisObj)
|
||||
{
|
||||
return zts_get_node_id();
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL Java_com_zerotier_libzt_ZeroTier_get_1num_1assigned_1addresses(
|
||||
JNIEnv *env, jobject thisObj, jlong nwid)
|
||||
{
|
||||
return zts_get_num_assigned_addresses(nwid);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_get_1address_1at_1index(
|
||||
JNIEnv *env, jobject thisObj, jlong nwid, jint index, jobject addr)
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
socklen_t addrlen = sizeof(struct sockaddr_storage);
|
||||
int err = zts_get_address_at_index(nwid, index, (struct sockaddr*)&ss, &addrlen);
|
||||
ss2zta(env, &ss, addr);
|
||||
return err;
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL Java_com_zerotier_libzt_ZeroTier_has_1address(
|
||||
JNIEnv *env, jobject thisObj, jlong nwid)
|
||||
{
|
||||
return zts_has_address(nwid);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
JNIEXPORT jlong JNICALL Java_com_zerotier_libzt_ZeroTier_get_1peer_1count(
|
||||
JNIEnv *env, jobject thisObj)
|
||||
{
|
||||
return zts_get_peer_count();
|
||||
}
|
||||
|
||||
/****************************************************************************/
|
||||
/* ZeroTier Socket API */
|
||||
/****************************************************************************/
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_socket(
|
||||
JNIEnv *env, jobject thisObj, jint family, jint type, jint protocol)
|
||||
{
|
||||
return zts_socket(family, type, protocol);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_connect(
|
||||
JNIEnv *env, jobject thisObj, jint fd, jobject addr)
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
zta2ss(env, &ss, addr);
|
||||
socklen_t addrlen = ss.ss_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
|
||||
return zts_connect(fd, (struct sockaddr *)&ss, addrlen);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_bind(
|
||||
JNIEnv *env, jobject thisObj, jint fd, jobject addr)
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
int err;
|
||||
zta2ss(env, &ss, addr);
|
||||
socklen_t addrlen = ss.ss_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
|
||||
return zts_bind(fd, (struct sockaddr*)&ss, addrlen);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_listen(
|
||||
JNIEnv *env, jobject thisObj, jint fd, int backlog)
|
||||
{
|
||||
return zts_listen(fd, backlog);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_accept(
|
||||
JNIEnv *env, jobject thisObj, jint fd, jobject addr, jint port)
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
socklen_t addrlen = sizeof(struct sockaddr_storage);
|
||||
int err = zts_accept(fd, (struct sockaddr *)&ss, &addrlen);
|
||||
ss2zta(env, &ss, addr);
|
||||
return err;
|
||||
}
|
||||
|
||||
#if defined(__linux__)
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_accept4(
|
||||
JNIEnv *env, jobject thisObj, jint fd, jobject addr, jint port, jint flags)
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
socklen_t addrlen = sizeof(struct sockaddr_storage);
|
||||
int err = zts_accept4(fd, (struct sockaddr *)&ss, &addrlen, flags);
|
||||
ss2zta(env, &ss, addr);
|
||||
return err;
|
||||
}
|
||||
#endif
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_setsockopt(
|
||||
JNIEnv *env, jobject thisObj, jint fd, jint level, jint optname, jint optval, jint optlen)
|
||||
{
|
||||
return zts_setsockopt(fd, level, optname, (void*)(uintptr_t)optval, optlen);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_getsockopt(
|
||||
JNIEnv *env, jobject thisObj, jint fd, jint level, jint optname, jint optval, jint optlen)
|
||||
{
|
||||
return zts_getsockopt(fd, level, optname, (void*)(uintptr_t)optval, (socklen_t *)optlen);
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL Java_com_zerotier_libzt_ZeroTier_getsockname(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jobject addr)
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
socklen_t addrlen = sizeof(struct sockaddr_storage);
|
||||
int err = zts_getsockname(fd, (struct sockaddr *)&ss, &addrlen);
|
||||
ss2zta(env, &ss, addr);
|
||||
return err;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_getpeername(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jobject addr)
|
||||
{
|
||||
struct sockaddr_storage ss;
|
||||
int err = zts_getpeername(fd, (struct sockaddr *)&ss, (socklen_t *)sizeof(struct sockaddr_storage));
|
||||
ss2zta(env, &ss, addr);
|
||||
return err;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_close(
|
||||
JNIEnv *env, jobject thisObj, jint fd)
|
||||
{
|
||||
return zts_close(fd);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_fcntl(
|
||||
JNIEnv *env, jobject thisObj, jint fd, jint cmd, jint flags)
|
||||
{
|
||||
return zts_fcntl(fd, cmd, flags);
|
||||
}
|
||||
|
||||
JNIEXPORT int JNICALL Java_com_zerotier_libzt_ZeroTier_ioctl(jint fd, jlong request, void *argp)
|
||||
{
|
||||
return zts_ioctl(fd, request, argp);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_send(
|
||||
JNIEnv *env, jobject thisObj, jint fd, jbyteArray buf, int flags)
|
||||
{
|
||||
void *data = env->GetPrimitiveArrayCritical(buf, NULL);
|
||||
int w = zts_send(fd, data, env->GetArrayLength(buf), flags);
|
||||
env->ReleasePrimitiveArrayCritical(buf, data, 0);
|
||||
return w;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_sendto(
|
||||
JNIEnv *env, jobject thisObj, jint fd, jbyteArray buf, jint flags, jobject addr)
|
||||
{
|
||||
void *data = env->GetPrimitiveArrayCritical(buf, NULL);
|
||||
struct sockaddr_storage ss;
|
||||
zta2ss(env, &ss, addr);
|
||||
socklen_t addrlen = ss.ss_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
|
||||
int w = zts_sendto(fd, data, env->GetArrayLength(buf), flags, (struct sockaddr *)&ss, addrlen);
|
||||
env->ReleasePrimitiveArrayCritical(buf, data, 0);
|
||||
return w;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_recv(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jbyteArray buf, jint flags)
|
||||
{
|
||||
void *data = env->GetPrimitiveArrayCritical(buf, NULL);
|
||||
int r = zts_recv(fd, data, env->GetArrayLength(buf), flags);
|
||||
env->ReleasePrimitiveArrayCritical(buf, data, 0);
|
||||
return r;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_recvfrom(
|
||||
JNIEnv *env, jobject thisObj, jint fd, jbyteArray buf, jint flags, jobject addr)
|
||||
{
|
||||
socklen_t addrlen = sizeof(struct sockaddr_storage);
|
||||
struct sockaddr_storage ss;
|
||||
void *data = env->GetPrimitiveArrayCritical(buf, NULL);
|
||||
int r = zts_recvfrom(fd, data, env->GetArrayLength(buf), flags, (struct sockaddr *)&ss, &addrlen);
|
||||
env->ReleasePrimitiveArrayCritical(buf, data, 0);
|
||||
ss2zta(env, &ss, addr);
|
||||
return r;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_read(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jbyteArray buf)
|
||||
{
|
||||
void *data = env->GetPrimitiveArrayCritical(buf, NULL);
|
||||
int r = zts_read(fd, data, env->GetArrayLength(buf));
|
||||
env->ReleasePrimitiveArrayCritical(buf, data, 0);
|
||||
return r;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_write(JNIEnv *env, jobject thisObj,
|
||||
jint fd, jbyteArray buf)
|
||||
{
|
||||
void *data = env->GetPrimitiveArrayCritical(buf, NULL);
|
||||
int w = zts_write(fd, data, env->GetArrayLength(buf));
|
||||
env->ReleasePrimitiveArrayCritical(buf, data, 0);
|
||||
return w;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_shutdown(
|
||||
JNIEnv *env, jobject thisObj, int fd, int how)
|
||||
{
|
||||
return zts_shutdown(fd, how);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_select(JNIEnv *env, jobject thisObj,
|
||||
jint nfds, jobject readfds, jobject writefds, jobject exceptfds, jint timeout_sec, jint timeout_usec)
|
||||
{
|
||||
struct timeval _timeout;
|
||||
_timeout.tv_sec = timeout_sec;
|
||||
_timeout.tv_usec = timeout_usec;
|
||||
fd_set _readfds, _writefds, _exceptfds;
|
||||
fd_set *r = NULL;
|
||||
fd_set *w = NULL;
|
||||
fd_set *e = NULL;
|
||||
if (readfds) {
|
||||
r = &_readfds;
|
||||
ztfdset2fdset(env, nfds, readfds, &_readfds);
|
||||
}
|
||||
if (writefds) {
|
||||
w = &_writefds;
|
||||
ztfdset2fdset(env, nfds, writefds, &_writefds);
|
||||
}
|
||||
if (exceptfds) {
|
||||
e = &_exceptfds;
|
||||
ztfdset2fdset(env, nfds, exceptfds, &_exceptfds);
|
||||
}
|
||||
int err = zts_select(nfds, r, w, e, &_timeout);
|
||||
if (readfds) {
|
||||
fdset2ztfdset(env, nfds, &_readfds, readfds);
|
||||
}
|
||||
if (writefds) {
|
||||
fdset2ztfdset(env, nfds, &_writefds, writefds);
|
||||
}
|
||||
if (exceptfds) {
|
||||
fdset2ztfdset(env, nfds, &_exceptfds, exceptfds);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
void ztfdset2fdset(JNIEnv *env, int nfds, jobject src_ztfd_set, fd_set *dest_fd_set)
|
||||
{
|
||||
jclass c = (*env).GetObjectClass(src_ztfd_set);
|
||||
if (!c) {
|
||||
return;
|
||||
}
|
||||
FD_ZERO(dest_fd_set);
|
||||
jfieldID fid = env->GetFieldID(c, "fds_bits", "[B");
|
||||
jobject fdData = (*env).GetObjectField (src_ztfd_set, fid);
|
||||
jbyteArray * arr = reinterpret_cast<jbyteArray*>(&fdData);
|
||||
char *data = (char*)(*env).GetByteArrayElements(*arr, NULL);
|
||||
for (int i=0; i<nfds; i++) {
|
||||
if (data[i] == 0x01) {
|
||||
FD_SET(i, dest_fd_set);
|
||||
}
|
||||
}
|
||||
(*env).ReleaseByteArrayElements(*arr, (jbyte*)data, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
void fdset2ztfdset(JNIEnv *env, int nfds, fd_set *src_fd_set, jobject dest_ztfd_set)
|
||||
{
|
||||
jclass c = (*env).GetObjectClass(dest_ztfd_set);
|
||||
if (!c) {
|
||||
return;
|
||||
}
|
||||
jfieldID fid = env->GetFieldID(c, "fds_bits", "[B");
|
||||
jobject fdData = (*env).GetObjectField (dest_ztfd_set, fid);
|
||||
jbyteArray * arr = reinterpret_cast<jbyteArray*>(&fdData);
|
||||
char *data = (char*)(*env).GetByteArrayElements(*arr, NULL);
|
||||
for (int i=0; i<nfds; i++) {
|
||||
if (FD_ISSET(i, src_fd_set)) {
|
||||
data[i] = 0x01;
|
||||
}
|
||||
}
|
||||
(*env).ReleaseByteArrayElements(*arr, (jbyte*)data, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
/****************************************************************************/
|
||||
/* Helpers (for moving data across the JNI barrier) */
|
||||
/****************************************************************************/
|
||||
|
||||
void ss2zta(JNIEnv *env, struct sockaddr_storage *ss, jobject addr)
|
||||
{
|
||||
jclass c = (*env).GetObjectClass(addr);
|
||||
if (!c) {
|
||||
return;
|
||||
}
|
||||
if(ss->ss_family == AF_INET)
|
||||
{
|
||||
struct sockaddr_in *in4 = (struct sockaddr_in*)ss;
|
||||
jfieldID fid = (*env).GetFieldID(c, "_port", "I");
|
||||
(*env).SetIntField(addr, fid, ntohs(in4->sin_port));
|
||||
fid = (*env).GetFieldID(c,"_family", "I");
|
||||
(*env).SetIntField(addr, fid, (in4->sin_family));
|
||||
fid = env->GetFieldID(c, "_ip4", "[B");
|
||||
jobject ipData = (*env).GetObjectField (addr, fid);
|
||||
jbyteArray * arr = reinterpret_cast<jbyteArray*>(&ipData);
|
||||
char *data = (char*)(*env).GetByteArrayElements(*arr, NULL);
|
||||
memcpy(data, &(in4->sin_addr.s_addr), 4);
|
||||
(*env).ReleaseByteArrayElements(*arr, (jbyte*)data, 0);
|
||||
|
||||
return;
|
||||
}
|
||||
if(ss->ss_family == AF_INET6)
|
||||
{
|
||||
struct sockaddr_in6 *in6 = (struct sockaddr_in6*)ss;
|
||||
jfieldID fid = (*env).GetFieldID(c, "_port", "I");
|
||||
(*env).SetIntField(addr, fid, ntohs(in6->sin6_port));
|
||||
fid = (*env).GetFieldID(c,"_family", "I");
|
||||
(*env).SetIntField(addr, fid, (in6->sin6_family));
|
||||
fid = env->GetFieldID(c, "_ip6", "[B");
|
||||
jobject ipData = (*env).GetObjectField (addr, fid);
|
||||
jbyteArray * arr = reinterpret_cast<jbyteArray*>(&ipData);
|
||||
char *data = (char*)(*env).GetByteArrayElements(*arr, NULL);
|
||||
memcpy(data, &(in6->sin6_addr.s6_addr), 16);
|
||||
(*env).ReleaseByteArrayElements(*arr, (jbyte*)data, 0);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void zta2ss(JNIEnv *env, struct sockaddr_storage *ss, jobject addr)
|
||||
{
|
||||
jclass c = (*env).GetObjectClass(addr);
|
||||
if (!c) {
|
||||
return;
|
||||
}
|
||||
jfieldID fid = (*env).GetFieldID(c, "_family", "I");
|
||||
int family = (*env).GetIntField(addr, fid);
|
||||
if (family == AF_INET)
|
||||
{
|
||||
struct sockaddr_in *in4 = (struct sockaddr_in*)ss;
|
||||
fid = (*env).GetFieldID(c, "_port", "I");
|
||||
in4->sin_port = htons((*env).GetIntField(addr, fid));
|
||||
in4->sin_family = AF_INET;
|
||||
fid = env->GetFieldID(c, "_ip4", "[B");
|
||||
jobject ipData = (*env).GetObjectField (addr, fid);
|
||||
jbyteArray * arr = reinterpret_cast<jbyteArray*>(&ipData);
|
||||
char *data = (char*)(*env).GetByteArrayElements(*arr, NULL);
|
||||
memcpy(&(in4->sin_addr.s_addr), data, 4);
|
||||
(*env).ReleaseByteArrayElements(*arr, (jbyte*)data, 0);
|
||||
return;
|
||||
}
|
||||
if (family == AF_INET6)
|
||||
{
|
||||
struct sockaddr_in6 *in6 = (struct sockaddr_in6*)ss;
|
||||
jfieldID fid = (*env).GetFieldID(c, "_port", "I");
|
||||
in6->sin6_port = htons((*env).GetIntField(addr, fid));
|
||||
fid = (*env).GetFieldID(c,"_family", "I");
|
||||
in6->sin6_family = AF_INET6;
|
||||
fid = env->GetFieldID(c, "_ip6", "[B");
|
||||
jobject ipData = (*env).GetObjectField (addr, fid);
|
||||
jbyteArray * arr = reinterpret_cast<jbyteArray*>(&ipData);
|
||||
char *data = (char*)(*env).GetByteArrayElements(*arr, NULL);
|
||||
memcpy(&(in6->sin6_addr.s6_addr), data, 16);
|
||||
(*env).ReleaseByteArrayElements(*arr, (jbyte*)data, 0);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // SDK_JNI
|
||||
234
src/lwIP.cpp
234
src/lwIP.cpp
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* ZeroTier SDK - Network Virtualization Everywhere
|
||||
* Copyright (C) 2011-2018 ZeroTier, Inc. https://www.zerotier.com/
|
||||
* 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
|
||||
@@ -13,7 +13,7 @@
|
||||
* 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/>.
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
@@ -27,27 +27,16 @@
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* lwIP network stack driver.
|
||||
*
|
||||
* Calls made in this network stack driver may never block since all packet
|
||||
* processing (input and output) as well as timer processing (TCP mainly) is done
|
||||
* in a single execution context.
|
||||
*
|
||||
* lwIP network stack driver
|
||||
*/
|
||||
|
||||
#include "libztDefs.h"
|
||||
#include <vector>
|
||||
|
||||
#include "VirtualTap.h"
|
||||
class VirtualTap;
|
||||
#include "MAC.hpp"
|
||||
|
||||
#include "Mutex.hpp"
|
||||
#include "MAC.hpp"
|
||||
#include "ZeroTierOne.h"
|
||||
|
||||
#include "libzt.h"
|
||||
#include "SysUtils.h"
|
||||
#include "Utilities.h"
|
||||
#include "libztDebug.h"
|
||||
#include "Constants.hpp"
|
||||
#include "VirtualTap.hpp"
|
||||
|
||||
#include "netif/ethernet.h"
|
||||
#include "lwip/netif.h"
|
||||
@@ -76,49 +65,44 @@ void ms_sleep(unsigned long ms)
|
||||
}
|
||||
#endif
|
||||
|
||||
struct netif lwipInterfaces[10];
|
||||
int lwipInterfacesCount = 0;
|
||||
|
||||
ZeroTier::Mutex _rx_input_lock_m;
|
||||
struct pbuf* lwip_frame_rxbuf[LWIP_MAX_GUARDED_RX_BUF_SZ];
|
||||
int lwip_frame_rxbuf_tot = 0;
|
||||
|
||||
|
||||
bool main_loop_exited = false;
|
||||
bool lwip_driver_initialized = false;
|
||||
bool has_already_been_initialized = false;
|
||||
int hibernationDelayMultiplier = 1;
|
||||
|
||||
ZeroTier::Mutex driver_m;
|
||||
|
||||
err_t tapif_init(struct netif *netif)
|
||||
std::vector<struct netif *> lwip_netifs;
|
||||
|
||||
void lwip_hibernate_driver()
|
||||
{
|
||||
// we do the actual initialization in elsewhere
|
||||
return ERR_OK;
|
||||
hibernationDelayMultiplier = ZTS_HIBERNATION_MULTIPLIER;
|
||||
}
|
||||
|
||||
/*
|
||||
static void tcp_timeout(void *data)
|
||||
void lwip_wake_driver()
|
||||
{
|
||||
DEBUG_EXTRA("");
|
||||
LWIP_UNUSED_ARG(data);
|
||||
#if TCP_DEBUG && LWIP_TCP
|
||||
// tcp_debug_print_pcbs();
|
||||
#endif
|
||||
sys_timeout(5000, tcp_timeout, NULL);
|
||||
hibernationDelayMultiplier = 1;
|
||||
}
|
||||
*/
|
||||
|
||||
// callback for when the TCPIP thread has been successfully started
|
||||
// Callback for when the TCPIP thread has been successfully started
|
||||
static void tcpip_init_done(void *arg)
|
||||
{
|
||||
sys_sem_t *sem;
|
||||
sem = (sys_sem_t *)arg;
|
||||
//netif_set_up(&lwipdev);
|
||||
lwip_driver_initialized = true;
|
||||
driver_m.unlock();
|
||||
// sys_timeout(5000, tcp_timeout, NULL);
|
||||
sys_sem_signal(sem);
|
||||
}
|
||||
|
||||
void my_tcpip_callback(void *arg)
|
||||
{
|
||||
if (main_loop_exited) {
|
||||
return;
|
||||
}
|
||||
ZeroTier::Mutex::Lock _l(_rx_input_lock_m);
|
||||
int loop_score = LWIP_FRAMES_HANDLED_PER_CORE_CALL; // max num of packets to read per polling call
|
||||
// TODO: Optimize (use Ringbuffer)
|
||||
@@ -130,42 +114,40 @@ void my_tcpip_callback(void *arg)
|
||||
// Packet routing logic. Inputs packet into correct lwip netif interface depending on protocol type
|
||||
struct ip_hdr *iphdr;
|
||||
switch (((struct eth_hdr *)p->payload)->type)
|
||||
{
|
||||
#ifdef LIBZT_IPV6
|
||||
{
|
||||
case PP_HTONS(ETHTYPE_IPV6): {
|
||||
iphdr = (struct ip_hdr *)((char *)p->payload + SIZEOF_ETH_HDR);
|
||||
for (int i=0; i<lwipInterfacesCount; i++) {
|
||||
if (lwipInterfaces[i].output_ip6 && lwipInterfaces[i].output_ip6 == ethip6_output) {
|
||||
if (lwipInterfaces[i].input(p, &lwipInterfaces[i]) != ERR_OK) {
|
||||
DEBUG_ERROR("packet input error (ipv6, p=%p, netif=%p)", p, &lwipInterfaces[i]);
|
||||
for (size_t i=0; i<lwip_netifs.size(); i++) {
|
||||
if (lwip_netifs[i]->output_ip6 &&
|
||||
lwip_netifs[i]->output_ip6 == ethip6_output) {
|
||||
if (lwip_netifs[i]->input(p, lwip_netifs[i]) != ERR_OK) {
|
||||
DEBUG_ERROR("packet input error (ipv6, p=%p, netif=%p)", p, &lwip_netifs[i]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} break;
|
||||
#endif
|
||||
#ifdef LIBZT_IPV4
|
||||
case PP_HTONS(ETHTYPE_IP): {
|
||||
iphdr = (struct ip_hdr *)((char *)p->payload + SIZEOF_ETH_HDR);
|
||||
for (int i=0; i<lwipInterfacesCount; i++) {
|
||||
if (lwipInterfaces[i].output && lwipInterfaces[i].output == etharp_output) {
|
||||
//if (lwipInterfaces[i].ip_addr.addr == iphdr->dest.addr || ip4_addr_isbroadcast_u32(iphdr->dest.addr, &lwipInterfaces[i])) {
|
||||
if (lwipInterfaces[i].ip_addr.u_addr.ip4.addr == iphdr->dest.addr || ip4_addr_isbroadcast_u32(iphdr->dest.addr, &lwipInterfaces[i])) {
|
||||
if (lwipInterfaces[i].input(p, &lwipInterfaces[i]) != ERR_OK) {
|
||||
DEBUG_ERROR("packet input error (ipv4, p=%p, netif=%p)", p, &lwipInterfaces[i]);
|
||||
for (size_t i=0; i<lwip_netifs.size(); i++) {
|
||||
if (lwip_netifs[i]->output &&
|
||||
lwip_netifs[i]->output == etharp_output) {
|
||||
if (lwip_netifs[i]->ip_addr.u_addr.ip4.addr == iphdr->dest.addr ||
|
||||
ip4_addr_isbroadcast_u32(iphdr->dest.addr, lwip_netifs[i])) {
|
||||
if (lwip_netifs[i]->input(p, lwip_netifs[i]) != ERR_OK) {
|
||||
DEBUG_ERROR("packet input error (ipv4, p=%p, netif=%p)", p, &lwip_netifs[i]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} break;
|
||||
#endif
|
||||
case PP_HTONS(ETHTYPE_ARP): {
|
||||
for (int i=0; i<lwipInterfacesCount; i++) {
|
||||
if (lwipInterfaces[i].state) {
|
||||
for (size_t i=0; i<lwip_netifs.size(); i++) {
|
||||
if (lwip_netifs[i]->state) {
|
||||
pbuf_ref(p);
|
||||
if (lwipInterfaces[i].input(p, &lwipInterfaces[i]) != ERR_OK) {
|
||||
DEBUG_ERROR("packet input error (arp, p=%p, netif=%p)", p, &lwipInterfaces[i]);
|
||||
if (lwip_netifs[i]->input(p, lwip_netifs[i]) != ERR_OK) {
|
||||
DEBUG_ERROR("packet input error (arp, p=%p, netif=%p)", p, &lwip_netifs[i]);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -179,52 +161,106 @@ void my_tcpip_callback(void *arg)
|
||||
loop_score--;
|
||||
}
|
||||
int count_final = lwip_frame_rxbuf_tot;
|
||||
// move pbuf frame pointer address buffer by the number of frames successfully fed into the stack core
|
||||
// Move pbuf frame pointer address buffer by the number of frames successfully fed into the stack core
|
||||
if (count_initial - count_final > 0) {
|
||||
memmove(lwip_frame_rxbuf, lwip_frame_rxbuf + count_final, count_initial - count_final);
|
||||
}
|
||||
}
|
||||
|
||||
// main thread which starts the initialization process
|
||||
static void main_thread(void *arg)
|
||||
static void main_lwip_driver_loop(void *arg)
|
||||
{
|
||||
sys_sem_t sem;
|
||||
LWIP_UNUSED_ARG(arg);
|
||||
if (sys_sem_new(&sem, 0) != ERR_OK) {
|
||||
DEBUG_ERROR("failed to create semaphore");
|
||||
}
|
||||
|
||||
tcpip_init(tcpip_init_done, &sem);
|
||||
has_already_been_initialized = true;
|
||||
sys_sem_wait(&sem);
|
||||
DEBUG_EXTRA("stack thread init complete");
|
||||
//DEBUG_INFO("stack thread init complete");
|
||||
|
||||
while(1) {
|
||||
while(lwip_driver_initialized) {
|
||||
#if defined(_WIN32)
|
||||
ms_sleep(LWIP_GUARDED_BUF_CHECK_INTERVAL);
|
||||
ms_sleep(LWIP_GUARDED_BUF_CHECK_INTERVAL*hibernationDelayMultiplier);
|
||||
#else
|
||||
usleep(LWIP_GUARDED_BUF_CHECK_INTERVAL*1000);
|
||||
usleep(LWIP_GUARDED_BUF_CHECK_INTERVAL*1000*hibernationDelayMultiplier);
|
||||
#endif
|
||||
// Handle incoming packets from the core's thread context.
|
||||
// If you feed frames into the core directly you will violate the core's thread model
|
||||
tcpip_callback_with_block(my_tcpip_callback, NULL, 1);
|
||||
}
|
||||
sys_sem_wait(&sem); // block forever
|
||||
main_loop_exited = true;
|
||||
}
|
||||
|
||||
// initialize the lwIP stack
|
||||
// Initialize the lwIP stack
|
||||
void lwip_driver_init()
|
||||
{
|
||||
driver_m.lock(); // unlocked from callback indicating completion of driver init
|
||||
if (lwip_driver_initialized == true) {
|
||||
driver_m.lock(); // Unlocked from callback indicating completion of driver init
|
||||
if (has_already_been_initialized || lwip_driver_initialized) {
|
||||
// Already initialized, skip
|
||||
driver_m.unlock();
|
||||
return;
|
||||
} if (main_loop_exited) {
|
||||
DEBUG_ERROR("stack has previously been shutdown an cannot be restarted.");
|
||||
driver_m.unlock();
|
||||
return;
|
||||
}
|
||||
#if defined(_WIN32)
|
||||
sys_init(); // required for win32 initializtion of critical sections
|
||||
sys_init(); // Required for win32 init of critical sections
|
||||
#endif
|
||||
sys_thread_new("main_thread", main_thread,
|
||||
void *st = sys_thread_new("main_thread", main_lwip_driver_loop,
|
||||
NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
|
||||
}
|
||||
|
||||
void lwip_driver_shutdown()
|
||||
{
|
||||
if (main_loop_exited) {
|
||||
return;
|
||||
}
|
||||
lwip_driver_initialized = false;
|
||||
// Give the stack time to call the frame feed callback one last time before shutting everything down
|
||||
int callbackInterval = LWIP_GUARDED_BUF_CHECK_INTERVAL*hibernationDelayMultiplier*1000;
|
||||
usleep(callbackInterval*3);
|
||||
while(!main_loop_exited) {
|
||||
usleep(LWIP_GUARDED_BUF_CHECK_INTERVAL*1000);
|
||||
}
|
||||
if (tcpip_shutdown() == ERR_OK) {
|
||||
sys_timeouts_free();
|
||||
}
|
||||
}
|
||||
|
||||
void lwip_driver_set_all_interfaces_down()
|
||||
{
|
||||
for (size_t i=0; i<lwip_netifs.size(); i++) {
|
||||
if (lwip_netifs[i]) {
|
||||
netif_remove(lwip_netifs[i]);
|
||||
netif_set_down(lwip_netifs[i]);
|
||||
netif_set_link_down(lwip_netifs[i]);
|
||||
delete lwip_netifs[i];
|
||||
}
|
||||
}
|
||||
lwip_netifs.clear();
|
||||
}
|
||||
|
||||
void lwip_driver_set_tap_interfaces_down(void *tapref)
|
||||
{
|
||||
int sz_i = lwip_netifs.size();
|
||||
std::vector<struct netif*>::iterator iter;
|
||||
for (iter = lwip_netifs.begin(); iter != lwip_netifs.end(); ) {
|
||||
struct netif *lp = *(iter);
|
||||
if (lp->state == tapref) {
|
||||
netif_remove(lp);
|
||||
netif_set_down(lp);
|
||||
netif_set_link_down(lp);
|
||||
iter = lwip_netifs.erase(iter);
|
||||
}
|
||||
else {
|
||||
++iter;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
err_t lwip_eth_tx(struct netif *netif, struct pbuf *p)
|
||||
{
|
||||
struct pbuf *q;
|
||||
@@ -232,7 +268,7 @@ err_t lwip_eth_tx(struct netif *netif, struct pbuf *p)
|
||||
char *bufptr;
|
||||
int totalLength = 0;
|
||||
|
||||
VirtualTap *tap = (VirtualTap*)netif->state;
|
||||
ZeroTier::VirtualTap *tap = (ZeroTier::VirtualTap*)netif->state;
|
||||
bufptr = buf;
|
||||
for (q = p; q != NULL; q = q->next) {
|
||||
memcpy(bufptr, q->payload, q->len);
|
||||
@@ -255,18 +291,20 @@ err_t lwip_eth_tx(struct netif *netif, struct pbuf *p)
|
||||
if (ZT_MSG_TRANSFER == true) {
|
||||
char flagbuf[32];
|
||||
memset(&flagbuf, 0, 32);
|
||||
char macBuf[ZT_MAC_ADDRSTRLEN], nodeBuf[ZTO_ID_LEN];
|
||||
mac2str(macBuf, ZT_MAC_ADDRSTRLEN, ethhdr->dest.addr);
|
||||
char macBuf[ZTS_MAC_ADDRSTRLEN], nodeBuf[ZTS_ID_LEN];
|
||||
snprintf(macBuf, ZTS_MAC_ADDRSTRLEN, "%02x:%02x:%02x:%02x:%02x:%02x",
|
||||
ethhdr->dest.addr[0], ethhdr->dest.addr[1], ethhdr->dest.addr[2],
|
||||
ethhdr->dest.addr[3], ethhdr->dest.addr[4], ethhdr->dest.addr[5]);
|
||||
ZeroTier::MAC mac;
|
||||
mac.setTo(ethhdr->dest.addr, 6);
|
||||
mac.toAddress(tap->_nwid).toString(nodeBuf);
|
||||
DEBUG_TRANS("len=%5d dst=%s [%s TX <-- %s] proto=0x%04x %s %s", totalLength, macBuf, nodeBuf, tap->nodeId().c_str(),
|
||||
ZeroTier::Utils::ntoh(ethhdr->type), beautify_eth_proto_nums(ZeroTier::Utils::ntoh(ethhdr->type)), flagbuf);
|
||||
DEBUG_TRANS("len=%5d dst=%s [%s TX <-- %s] proto=0x%04x %s", totalLength, macBuf, nodeBuf, tap->nodeId().c_str(),
|
||||
ZeroTier::Utils::ntoh(ethhdr->type), flagbuf);
|
||||
}
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
void lwip_eth_rx(VirtualTap *tap, const ZeroTier::MAC &from, const ZeroTier::MAC &to, unsigned int etherType,
|
||||
void lwip_eth_rx(ZeroTier::VirtualTap *tap, const ZeroTier::MAC &from, const ZeroTier::MAC &to, unsigned int etherType,
|
||||
const void *data, unsigned int len)
|
||||
{
|
||||
struct pbuf *p,*q;
|
||||
@@ -278,13 +316,15 @@ void lwip_eth_rx(VirtualTap *tap, const ZeroTier::MAC &from, const ZeroTier::MAC
|
||||
if (ZT_MSG_TRANSFER == true) {
|
||||
char flagbuf[32];
|
||||
memset(&flagbuf, 0, 32);
|
||||
char macBuf[ZT_MAC_ADDRSTRLEN], nodeBuf[ZTO_ID_LEN];
|
||||
mac2str(macBuf, ZT_MAC_ADDRSTRLEN, ethhdr.dest.addr);
|
||||
char macBuf[ZTS_MAC_ADDRSTRLEN], nodeBuf[ZTS_ID_LEN];
|
||||
snprintf(macBuf, ZTS_MAC_ADDRSTRLEN, "%02x:%02x:%02x:%02x:%02x:%02x",
|
||||
ethhdr.dest.addr[0], ethhdr.dest.addr[1], ethhdr.dest.addr[2],
|
||||
ethhdr.dest.addr[3], ethhdr.dest.addr[4], ethhdr.dest.addr[5]);
|
||||
ZeroTier::MAC mac;
|
||||
mac.setTo(ethhdr.src.addr, 6);
|
||||
mac.toAddress(tap->_nwid).toString(nodeBuf);
|
||||
DEBUG_TRANS("len=%5d dst=%s [%s RX --> %s] proto=0x%04x %s %s", len, macBuf, nodeBuf, tap->nodeId().c_str(),
|
||||
ZeroTier::Utils::ntoh(ethhdr.type), beautify_eth_proto_nums(ZeroTier::Utils::ntoh(ethhdr.type)), flagbuf);
|
||||
DEBUG_TRANS("len=%5d dst=%s [%s RX --> %s] proto=0x%04x %s", len, macBuf, nodeBuf, tap->nodeId().c_str(),
|
||||
ZeroTier::Utils::ntoh(ethhdr.type), flagbuf);
|
||||
}
|
||||
|
||||
p = pbuf_alloc(PBUF_RAW, len+sizeof(struct eth_hdr), PBUF_POOL);
|
||||
@@ -309,16 +349,16 @@ void lwip_eth_rx(VirtualTap *tap, const ZeroTier::MAC &from, const ZeroTier::MAC
|
||||
DEBUG_ERROR("dropped packet: no pbufs available");
|
||||
return;
|
||||
}
|
||||
if (lwipInterfacesCount <= 0) {
|
||||
if (!lwip_netifs.size()) {
|
||||
DEBUG_ERROR("there are no netifs set up to handle this packet. ignoring.");
|
||||
return;
|
||||
}
|
||||
Mutex::Lock _l(_rx_input_lock_m);
|
||||
ZeroTier::Mutex::Lock _l(_rx_input_lock_m);
|
||||
if (lwip_frame_rxbuf_tot == LWIP_MAX_GUARDED_RX_BUF_SZ) {
|
||||
DEBUG_ERROR("dropped packet -- guarded receive buffer full, adjust MAX_GUARDED_RX_BUF_SZ or LWIP_GUARDED_BUF_CHECK_INTERVAL");
|
||||
return;
|
||||
}
|
||||
pbuf_ref(p); // Increment reference to allow user application to copy data from buffer -- Will be automatically deallocated by socket API
|
||||
//pbuf_ref(p); // Increment reference to allow user application to copy data from buffer -- Will be automatically deallocated by socket API
|
||||
lwip_frame_rxbuf[lwip_frame_rxbuf_tot] = p;
|
||||
lwip_frame_rxbuf_tot += 1;
|
||||
}
|
||||
@@ -339,6 +379,7 @@ void lwip_start_dhcp(void *netif)
|
||||
|
||||
static void netif_status_callback(struct netif *netif)
|
||||
{
|
||||
/*
|
||||
DEBUG_INFO("n=%p, %c%c, %d, o=%p, o6=%p, mc=%x:%x:%x:%x:%x:%x, hwln=%d, st=%p, flgs=%d\n",
|
||||
netif,
|
||||
netif->name[0],
|
||||
@@ -356,6 +397,7 @@ static void netif_status_callback(struct netif *netif)
|
||||
netif->state,
|
||||
netif->flags
|
||||
);
|
||||
*/
|
||||
}
|
||||
|
||||
ZeroTier::MAC _mac;
|
||||
@@ -364,7 +406,7 @@ static err_t netif_init_4(struct netif *netif)
|
||||
{
|
||||
netif->hwaddr_len = 6;
|
||||
netif->name[0] = 'e';
|
||||
netif->name[1] = '0'+lwipInterfacesCount;
|
||||
netif->name[1] = '0'+lwip_netifs.size();
|
||||
netif->linkoutput = lwip_eth_tx;
|
||||
netif->output = etharp_output;
|
||||
netif->mtu = ZT_MAX_MTU;
|
||||
@@ -383,7 +425,7 @@ static err_t netif_init_6(struct netif *netif)
|
||||
{
|
||||
netif->hwaddr_len = 6;
|
||||
netif->name[0] = 'e';
|
||||
netif->name[1] = '0'+(char)lwipInterfacesCount;
|
||||
netif->name[1] = '0'+(char)lwip_netifs.size();
|
||||
netif->linkoutput = lwip_eth_tx;
|
||||
netif->output = etharp_output;
|
||||
netif->output_ip6 = ethip6_output;
|
||||
@@ -400,12 +442,15 @@ static err_t netif_init_6(struct netif *netif)
|
||||
|
||||
void lwip_init_interface(void *tapref, const ZeroTier::MAC &mac, const ZeroTier::InetAddress &ip)
|
||||
{
|
||||
char ipbuf[INET6_ADDRSTRLEN], nmbuf[INET6_ADDRSTRLEN];
|
||||
char macbuf[ZT_MAC_ADDRSTRLEN];
|
||||
struct netif *lwipdev = &lwipInterfaces[lwipInterfacesCount];
|
||||
char ipbuf[INET6_ADDRSTRLEN];
|
||||
char macbuf[ZTS_MAC_ADDRSTRLEN];
|
||||
struct netif *lwipdev = new struct netif;
|
||||
lwip_netifs.push_back(lwipdev);
|
||||
|
||||
_mac = mac;
|
||||
|
||||
if (ip.isV4()) {
|
||||
char nmbuf[INET6_ADDRSTRLEN];
|
||||
static ip4_addr_t ipaddr, netmask, gw;
|
||||
IP4_ADDR(&gw,127,0,0,1);
|
||||
ipaddr.addr = *((u32_t *)ip.rawIpData());
|
||||
@@ -413,8 +458,11 @@ void lwip_init_interface(void *tapref, const ZeroTier::MAC &mac, const ZeroTier:
|
||||
netif_set_status_callback(lwipdev, netif_status_callback);
|
||||
netif_add(lwipdev, &ipaddr, &netmask, &gw, NULL, netif_init_4, tcpip_input);
|
||||
lwipdev->state = tapref;
|
||||
mac2str(macbuf, ZT_MAC_ADDRSTRLEN, lwipdev->hwaddr);
|
||||
DEBUG_INFO("initialized netif as [mac=%s, addr=%s, nm=%s]", macbuf, ip.toString(ipbuf), ip.netmask().toString(nmbuf));
|
||||
snprintf(macbuf, ZTS_MAC_ADDRSTRLEN, "%02x:%02x:%02x:%02x:%02x:%02x",
|
||||
lwipdev->hwaddr[0], lwipdev->hwaddr[1], lwipdev->hwaddr[2],
|
||||
lwipdev->hwaddr[3], lwipdev->hwaddr[4], lwipdev->hwaddr[5]);
|
||||
DEBUG_INFO("initialized netif as [mac=%s, addr=%s, nm=%s]",
|
||||
macbuf, ip.toString(ipbuf), ip.netmask().toString(nmbuf));
|
||||
}
|
||||
if (ip.isV6())
|
||||
{
|
||||
@@ -431,8 +479,10 @@ void lwip_init_interface(void *tapref, const ZeroTier::MAC &mac, const ZeroTier:
|
||||
netif_set_default(lwipdev);
|
||||
netif_set_up(lwipdev);
|
||||
netif_set_link_up(lwipdev);
|
||||
mac2str(macbuf, ZT_MAC_ADDRSTRLEN, lwipdev->hwaddr);
|
||||
DEBUG_INFO("initialized netif as [mac=%s, addr=%s]", macbuf, ip.toString(ipbuf));
|
||||
snprintf(macbuf, ZTS_MAC_ADDRSTRLEN, "%02x:%02x:%02x:%02x:%02x:%02x",
|
||||
lwipdev->hwaddr[0], lwipdev->hwaddr[1], lwipdev->hwaddr[2],
|
||||
lwipdev->hwaddr[3], lwipdev->hwaddr[4], lwipdev->hwaddr[5]);
|
||||
DEBUG_INFO("initialized netif as [mac=%s, addr=%s]",
|
||||
macbuf, ip.toString(ipbuf));
|
||||
}
|
||||
lwipInterfacesCount++;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user