Preparation for C--, removed classes, namespaces, advanced build options are now controlled via include/libztDefs.h
This commit is contained in:
@@ -31,29 +31,27 @@
|
||||
*/
|
||||
|
||||
#include "ZT1Service.h"
|
||||
|
||||
#include "Debug.hpp"
|
||||
#include "libztDebug.h"
|
||||
#include "SysUtils.h"
|
||||
|
||||
#include "Phy.hpp"
|
||||
#include "OneService.hpp"
|
||||
#include "Utilities.h"
|
||||
#include "InetAddress.hpp"
|
||||
#include "OSUtils.hpp"
|
||||
|
||||
std::vector<void*> vtaps;
|
||||
ZeroTier::Mutex _vtaps_lock;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
namespace ZeroTier {
|
||||
std::vector<void*> vtaps;
|
||||
static ZeroTier::OneService *zt1Service;
|
||||
|
||||
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
|
||||
|
||||
std::string homeDir; // Platform-specific dir we *must* use internally
|
||||
std::string netDir; // Where network .conf files are to be written
|
||||
|
||||
ZeroTier::Mutex _vtaps_lock;
|
||||
ZeroTier::Mutex _multiplexer_lock;
|
||||
}
|
||||
ZeroTier::Mutex _multiplexer_lock;
|
||||
|
||||
#if defined(__MINGW32__) || defined(__MINGW64__)
|
||||
WSADATA wsaData;
|
||||
@@ -63,132 +61,152 @@ WSADATA wsaData;
|
||||
/* ZeroTier Core helper functions for libzt - DON'T CALL THESE DIRECTLY */
|
||||
/****************************************************************************/
|
||||
|
||||
std::vector<ZT_VirtualNetworkRoute> *zts_get_network_routes(char *nwid)
|
||||
std::vector<ZT_VirtualNetworkRoute> *zts_get_network_routes(const uint64_t nwid)
|
||||
{
|
||||
uint64_t nwid_int = strtoull(nwid, NULL, 16);
|
||||
return ZeroTier::zt1Service->getRoutes(nwid_int);
|
||||
return zt1Service->getRoutes(nwid);
|
||||
}
|
||||
|
||||
ZeroTier::VirtualTap *getTapByNWID(uint64_t nwid)
|
||||
VirtualTap *getTapByNWID(uint64_t nwid)
|
||||
{
|
||||
ZeroTier::_vtaps_lock.lock();
|
||||
ZeroTier::VirtualTap *s, *tap = nullptr;
|
||||
for (size_t i=0; i<ZeroTier::vtaps.size(); i++) {
|
||||
s = (ZeroTier::VirtualTap*)ZeroTier::vtaps[i];
|
||||
_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; }
|
||||
}
|
||||
ZeroTier::_vtaps_lock.unlock();
|
||||
_vtaps_lock.unlock();
|
||||
return tap;
|
||||
}
|
||||
|
||||
ZeroTier::VirtualTap *getTapByAddr(ZeroTier::InetAddress *addr)
|
||||
VirtualTap *getTapByAddr(ZeroTier::InetAddress *addr)
|
||||
{
|
||||
ZeroTier::_vtaps_lock.lock();
|
||||
ZeroTier::VirtualTap *s, *tap = nullptr;
|
||||
_vtaps_lock.lock();
|
||||
VirtualTap *s, *tap = nullptr;
|
||||
//char ipbuf[64], ipbuf2[64], ipbuf3[64];
|
||||
for (size_t i=0; i<ZeroTier::vtaps.size(); i++) {
|
||||
s = (ZeroTier::VirtualTap*)ZeroTier::vtaps[i];
|
||||
for (size_t i=0; i<vtaps.size(); i++) {
|
||||
s = (VirtualTap*)vtaps[i];
|
||||
// check address schemes
|
||||
for (ssize_t j=0; j<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));
|
||||
/* 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))
|
||||
|| (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));
|
||||
ZeroTier::_vtaps_lock.unlock();
|
||||
_vtaps_lock.unlock();
|
||||
return s;
|
||||
}
|
||||
}
|
||||
}
|
||||
// check managed routes
|
||||
if (tap == NULL) {
|
||||
std::vector<ZT_VirtualNetworkRoute> *managed_routes = ZeroTier::zt1Service->getRoutes(s->_nwid);
|
||||
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));
|
||||
ZeroTier::_vtaps_lock.unlock();
|
||||
/* 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ZeroTier::_vtaps_lock.unlock();
|
||||
_vtaps_lock.unlock();
|
||||
return tap;
|
||||
}
|
||||
|
||||
ZeroTier::VirtualTap *getTapByName(char *ifname)
|
||||
VirtualTap *getTapByName(char *ifname)
|
||||
{
|
||||
ZeroTier::_vtaps_lock.lock();
|
||||
ZeroTier::VirtualTap *s, *tap = nullptr;
|
||||
for (size_t i=0; i<ZeroTier::vtaps.size(); i++) {
|
||||
s = (ZeroTier::VirtualTap*)ZeroTier::vtaps[i];
|
||||
_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;
|
||||
}
|
||||
}
|
||||
ZeroTier::_vtaps_lock.unlock();
|
||||
_vtaps_lock.unlock();
|
||||
return tap;
|
||||
}
|
||||
|
||||
ZeroTier::VirtualTap *getTapByIndex(size_t index)
|
||||
VirtualTap *getTapByIndex(size_t index)
|
||||
{
|
||||
ZeroTier::_vtaps_lock.lock();
|
||||
ZeroTier::VirtualTap *s, *tap = nullptr;
|
||||
for (size_t i=0; i<ZeroTier::vtaps.size(); i++) {
|
||||
s = (ZeroTier::VirtualTap*)ZeroTier::vtaps[i];
|
||||
_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;
|
||||
}
|
||||
}
|
||||
ZeroTier::_vtaps_lock.unlock();
|
||||
_vtaps_lock.unlock();
|
||||
return tap;
|
||||
}
|
||||
|
||||
ZeroTier::VirtualTap *getAnyTap()
|
||||
VirtualTap *getAnyTap()
|
||||
{
|
||||
ZeroTier::_vtaps_lock.lock();
|
||||
ZeroTier::VirtualTap *vtap = NULL;
|
||||
if (ZeroTier::vtaps.size()) {
|
||||
vtap = (ZeroTier::VirtualTap *)ZeroTier::vtaps[0];
|
||||
_vtaps_lock.lock();
|
||||
VirtualTap *vtap = NULL;
|
||||
if (vtaps.size()) {
|
||||
vtap = (VirtualTap *)vtaps[0];
|
||||
}
|
||||
ZeroTier::_vtaps_lock.unlock();
|
||||
_vtaps_lock.unlock();
|
||||
return vtap;
|
||||
}
|
||||
|
||||
int zts_get_id_from_file(const char *filepath, char *devID)
|
||||
/*
|
||||
|
||||
else // Service isn't online, try to read ID from file
|
||||
{
|
||||
std::string fname("identity.public");
|
||||
std::string fpath(homeDir);
|
||||
if (ZeroTier::OSUtils::fileExists((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(),false)) {
|
||||
std::string oldid;
|
||||
ZeroTier::OSUtils::readFile((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(),oldid);
|
||||
memcpy(devID, oldid.c_str(), ZTO_ID_LEN); // first 10 bytes of file
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
int zts_get_id_from_file(const char *filepath, uint64_t *nodeId)
|
||||
{
|
||||
/*
|
||||
DEBUG_EXTRA();
|
||||
std::string fname("identity.public");
|
||||
std::string fpath(filepath);
|
||||
if (ZeroTier::OSUtils::fileExists((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(),false)) {
|
||||
std::string oldid;
|
||||
ZeroTier::OSUtils::readFile((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(),oldid);
|
||||
memcpy(devID, oldid.c_str(), 10); // first 10 bytes of file
|
||||
uint64_t value = Utils::hexStrToU64(oldid);
|
||||
memcpy(nodeId, value, sizeof(uint64_t)); // first 10 bytes of file
|
||||
// TOmorrow
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Starts a ZeroTier service in the background
|
||||
void *zts_start_service(void *thread_id)
|
||||
{
|
||||
DEBUG_INFO("zto-thread, path=%s", ZeroTier::homeDir.c_str());
|
||||
DEBUG_INFO("zto-thread, path=%s", homeDir.c_str());
|
||||
// Where network .conf files will be stored
|
||||
ZeroTier::netDir = ZeroTier::homeDir + "/networks.d";
|
||||
ZeroTier::zt1Service = (ZeroTier::OneService *)0;
|
||||
netDir = homeDir + "/networks.d";
|
||||
zt1Service = (ZeroTier::OneService *)0;
|
||||
// Construct path for network config and supporting service files
|
||||
if (ZeroTier::homeDir.length()) {
|
||||
std::vector<std::string> hpsp(ZeroTier::OSUtils::split(ZeroTier::homeDir.c_str(), ZT_PATH_SEPARATOR_S,"",""));
|
||||
if (homeDir.length()) {
|
||||
std::vector<std::string> hpsp(ZeroTier::OSUtils::split(homeDir.c_str(), ZT_PATH_SEPARATOR_S,"",""));
|
||||
std::string ptmp;
|
||||
if (ZeroTier::homeDir[0] == ZT_PATH_SEPARATOR) {
|
||||
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) {
|
||||
@@ -199,7 +217,6 @@ void *zts_start_service(void *thread_id)
|
||||
if ((*pi != ".")&&(*pi != "..")) {
|
||||
if (ZeroTier::OSUtils::mkdir(ptmp) == false) {
|
||||
DEBUG_ERROR("home path does not exist, and could not create");
|
||||
handle_general_failure();
|
||||
perror("error\n");
|
||||
}
|
||||
}
|
||||
@@ -207,7 +224,6 @@ void *zts_start_service(void *thread_id)
|
||||
}
|
||||
else {
|
||||
DEBUG_ERROR("homeDir is empty, could not construct path");
|
||||
handle_general_failure();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -217,27 +233,27 @@ void *zts_start_service(void *thread_id)
|
||||
// TODO: Better port random range selection
|
||||
int servicePort = 9000 + (randp % 1000);
|
||||
for (;;) {
|
||||
ZeroTier::zt1Service = ZeroTier::OneService::newInstance(ZeroTier::homeDir.c_str(),servicePort);
|
||||
switch(ZeroTier::zt1Service->run()) {
|
||||
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("ZTO service port = %d", servicePort);
|
||||
DEBUG_ERROR("fatal error: %s",ZeroTier::zt1Service->fatalErrorMessage().c_str());
|
||||
DEBUG_ERROR("fatal error: %s",zt1Service->fatalErrorMessage().c_str());
|
||||
break;
|
||||
case ZeroTier::OneService::ONE_IDENTITY_COLLISION: {
|
||||
delete ZeroTier::zt1Service;
|
||||
ZeroTier::zt1Service = (ZeroTier::OneService *)0;
|
||||
delete zt1Service;
|
||||
zt1Service = (ZeroTier::OneService *)0;
|
||||
std::string oldid;
|
||||
ZeroTier::OSUtils::readFile((ZeroTier::homeDir + ZT_PATH_SEPARATOR_S
|
||||
ZeroTier::OSUtils::readFile((homeDir + ZT_PATH_SEPARATOR_S
|
||||
+ "identity.secret").c_str(),oldid);
|
||||
if (oldid.length()) {
|
||||
ZeroTier::OSUtils::writeFile((ZeroTier::homeDir + ZT_PATH_SEPARATOR_S
|
||||
ZeroTier::OSUtils::writeFile((homeDir + ZT_PATH_SEPARATOR_S
|
||||
+ "identity.secret.saved_after_collision").c_str(),oldid);
|
||||
ZeroTier::OSUtils::rm((ZeroTier::homeDir + ZT_PATH_SEPARATOR_S
|
||||
ZeroTier::OSUtils::rm((homeDir + ZT_PATH_SEPARATOR_S
|
||||
+ "identity.secret").c_str());
|
||||
ZeroTier::OSUtils::rm((ZeroTier::homeDir + ZT_PATH_SEPARATOR_S
|
||||
ZeroTier::OSUtils::rm((homeDir + ZT_PATH_SEPARATOR_S
|
||||
+ "identity.public").c_str());
|
||||
}
|
||||
}
|
||||
@@ -245,179 +261,89 @@ void *zts_start_service(void *thread_id)
|
||||
}
|
||||
break; // terminate loop -- normally we don't keep restarting
|
||||
}
|
||||
delete ZeroTier::zt1Service;
|
||||
ZeroTier::zt1Service = (ZeroTier::OneService *)0;
|
||||
delete zt1Service;
|
||||
zt1Service = (ZeroTier::OneService *)0;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void disableTaps()
|
||||
void zts_get_address(const uint64_t nwid, struct sockaddr_storage *addr, const size_t addrlen)
|
||||
{
|
||||
DEBUG_EXTRA();
|
||||
ZeroTier::_vtaps_lock.lock();
|
||||
for (size_t i=0; i<ZeroTier::vtaps.size(); i++) {
|
||||
DEBUG_EXTRA("vt=%p", ZeroTier::vtaps[i]);
|
||||
((ZeroTier::VirtualTap*)ZeroTier::vtaps[i])->_enabled = false;
|
||||
if(!zt1Service) {
|
||||
return;
|
||||
}
|
||||
ZeroTier::_vtaps_lock.unlock();
|
||||
}
|
||||
|
||||
void zts_get_ipv4_address(const char *nwid, char *addrstr, const size_t addrlen)
|
||||
{
|
||||
// DEBUG_EXTRA();
|
||||
if (ZeroTier::zt1Service) {
|
||||
uint64_t nwid_int = strtoull(nwid, NULL, 16);
|
||||
ZeroTier::VirtualTap *tap = getTapByNWID(nwid_int);
|
||||
if (tap && tap->_ips.size()) {
|
||||
for (size_t i=0; i<tap->_ips.size(); i++) {
|
||||
if (tap->_ips[i].isV4()) {
|
||||
char ipbuf[INET_ADDRSTRLEN];
|
||||
std::string addr = tap->_ips[i].toString(ipbuf);
|
||||
int len = addrlen < addr.length() ? addrlen : addr.length();
|
||||
memset(addrstr, 0, len);
|
||||
memcpy(addrstr, addr.c_str(), len);
|
||||
return;
|
||||
}
|
||||
VirtualTap *tap = getTapByNWID(nwid);
|
||||
if (tap && tap->_ips.size()) {
|
||||
for (size_t i=0; i<tap->_ips.size(); i++) {
|
||||
if (tap->_ips[i].isV4()) {
|
||||
memcpy(addr, &(tap->_ips[i]), addrlen);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
memcpy(addrstr, "\0", 1);
|
||||
}
|
||||
|
||||
void zts_get_ipv6_address(const char *nwid, char *addrstr, size_t addrlen)
|
||||
int zts_has_address(const uint64_t nwid)
|
||||
{
|
||||
// DEBUG_EXTRA();
|
||||
if (ZeroTier::zt1Service) {
|
||||
uint64_t nwid_int = strtoull(nwid, NULL, 16);
|
||||
ZeroTier::VirtualTap *tap = getTapByNWID(nwid_int);
|
||||
if (tap && tap->_ips.size()) {
|
||||
for (size_t i=0; i<tap->_ips.size(); i++) {
|
||||
if (tap->_ips[i].isV6()) {
|
||||
char ipbuf[INET6_ADDRSTRLEN];
|
||||
std::string addr = tap->_ips[i].toString(ipbuf);
|
||||
int len = addrlen < addr.length() ? addrlen : addr.length();
|
||||
memset(addrstr, 0, len);
|
||||
memcpy(addrstr, addr.c_str(), len);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
memcpy(addrstr, "\0", 1);
|
||||
struct sockaddr_storage ss;
|
||||
memset(&ss, 0, sizeof(ss));
|
||||
zts_get_address(nwid, &ss, sizeof(ss));
|
||||
return ss.ss_family == AF_INET || ss.ss_family == AF_INET6;
|
||||
}
|
||||
|
||||
int zts_has_ipv4_address(const char *nwid)
|
||||
void zts_get_6plane_addr(struct sockaddr_storage *addr, const uint64_t nwid, const uint64_t nodeId)
|
||||
{
|
||||
// DEBUG_EXTRA();
|
||||
char ipv4_addr[INET_ADDRSTRLEN];
|
||||
memset(ipv4_addr, 0, INET_ADDRSTRLEN);
|
||||
zts_get_ipv4_address(nwid, ipv4_addr, INET_ADDRSTRLEN);
|
||||
return strcmp(ipv4_addr, "\0");
|
||||
ZeroTier::InetAddress _6planeAddr = ZeroTier::InetAddress::makeIpv66plane(nwid,nodeId);
|
||||
memcpy(addr, _6planeAddr.rawIpData(), sizeof(struct sockaddr_storage));
|
||||
}
|
||||
|
||||
int zts_has_ipv6_address(const char *nwid)
|
||||
void zts_get_rfc4193_addr(struct sockaddr_storage *addr, const uint64_t nwid, const uint64_t nodeId)
|
||||
{
|
||||
// DEBUG_EXTRA();
|
||||
char ipv6_addr[INET6_ADDRSTRLEN];
|
||||
memset(ipv6_addr, 0, INET6_ADDRSTRLEN);
|
||||
zts_get_ipv6_address(nwid, ipv6_addr, INET6_ADDRSTRLEN);
|
||||
return strcmp(ipv6_addr, "\0");
|
||||
ZeroTier::InetAddress _rfc4193Addr = ZeroTier::InetAddress::makeIpv6rfc4193(nwid,nodeId);
|
||||
memcpy(addr, _rfc4193Addr.rawIpData(), sizeof(struct sockaddr_storage));
|
||||
}
|
||||
|
||||
int zts_has_address(const char *nwid)
|
||||
{
|
||||
// DEBUG_EXTRA();
|
||||
return zts_has_ipv4_address(nwid) || zts_has_ipv6_address(nwid);
|
||||
}
|
||||
|
||||
void zts_get_6plane_addr(char *addr, const char *nwid, const char *devID)
|
||||
void zts_join(const uint64_t nwid)
|
||||
{
|
||||
DEBUG_EXTRA();
|
||||
ZeroTier::InetAddress _6planeAddr = ZeroTier::InetAddress::makeIpv66plane(
|
||||
ZeroTier::Utils::hexStrToU64(nwid),ZeroTier::Utils::hexStrToU64(devID));
|
||||
char ipbuf[INET6_ADDRSTRLEN];
|
||||
memcpy(addr, _6planeAddr.toIpString(ipbuf), 40);
|
||||
}
|
||||
|
||||
void zts_get_rfc4193_addr(char *addr, const char *nwid, const char *devID)
|
||||
{
|
||||
DEBUG_EXTRA();
|
||||
ZeroTier::InetAddress _6planeAddr = ZeroTier::InetAddress::makeIpv6rfc4193(
|
||||
ZeroTier::Utils::hexStrToU64(nwid),ZeroTier::Utils::hexStrToU64(devID));
|
||||
char ipbuf[INET6_ADDRSTRLEN];
|
||||
memcpy(addr, _6planeAddr.toIpString(ipbuf), 40);
|
||||
}
|
||||
|
||||
void zts_join(const char * nwid)
|
||||
{
|
||||
DEBUG_EXTRA();
|
||||
if (ZeroTier::zt1Service) {
|
||||
std::string confFile = ZeroTier::zt1Service->givenHomePath() + "/networks.d/" + nwid + ".conf";
|
||||
if (ZeroTier::OSUtils::mkdir(ZeroTier::netDir) == false) {
|
||||
DEBUG_ERROR("unable to create: %s", ZeroTier::netDir.c_str());
|
||||
handle_general_failure();
|
||||
if (zt1Service) {
|
||||
std::string confFile = zt1Service->givenHomePath() + "/networks.d/" + std::to_string(nwid) + ".conf";
|
||||
if (ZeroTier::OSUtils::mkdir(netDir) == false) {
|
||||
DEBUG_ERROR("unable to create: %s", netDir.c_str());
|
||||
}
|
||||
if (ZeroTier::OSUtils::writeFile(confFile.c_str(), "") == false) {
|
||||
DEBUG_ERROR("unable to write network conf file: %s", confFile.c_str());
|
||||
handle_general_failure();
|
||||
}
|
||||
ZeroTier::zt1Service->join(nwid);
|
||||
zt1Service->join(nwid);
|
||||
}
|
||||
// provide ZTO service reference to virtual taps
|
||||
// TODO: This might prove to be unreliable, but it works for now
|
||||
for (size_t i=0;i<ZeroTier::vtaps.size(); i++) {
|
||||
ZeroTier::VirtualTap *s = (ZeroTier::VirtualTap*)ZeroTier::vtaps[i];
|
||||
s->zt1ServiceRef=(void*)ZeroTier::zt1Service;
|
||||
for (size_t i=0;i<vtaps.size(); i++) {
|
||||
VirtualTap *s = (VirtualTap*)vtaps[i];
|
||||
s->zt1ServiceRef=(void*)zt1Service;
|
||||
}
|
||||
}
|
||||
|
||||
void zts_join_soft(const char * filepath, const char * nwid)
|
||||
void zts_leave(const uint64_t nwid)
|
||||
{
|
||||
DEBUG_EXTRA();
|
||||
std::string net_dir = std::string(filepath) + "/networks.d/";
|
||||
std::string confFile = net_dir + std::string(nwid) + ".conf";
|
||||
if (ZeroTier::OSUtils::mkdir(net_dir) == false) {
|
||||
DEBUG_ERROR("unable to create: %s", net_dir.c_str());
|
||||
handle_general_failure();
|
||||
}
|
||||
if (ZeroTier::OSUtils::fileExists(confFile.c_str(), false) == false) {
|
||||
if (ZeroTier::OSUtils::writeFile(confFile.c_str(), "") == false) {
|
||||
DEBUG_ERROR("unable to write network conf file: %s", confFile.c_str());
|
||||
handle_general_failure();
|
||||
}
|
||||
if (zt1Service) {
|
||||
zt1Service->leave(nwid);
|
||||
}
|
||||
}
|
||||
|
||||
void zts_leave(const char * nwid)
|
||||
int zts_running()
|
||||
{
|
||||
DEBUG_EXTRA();
|
||||
if (ZeroTier::zt1Service) {
|
||||
ZeroTier::zt1Service->leave(nwid);
|
||||
}
|
||||
}
|
||||
|
||||
void zts_leave_soft(const char * filepath, const char * nwid)
|
||||
{
|
||||
DEBUG_EXTRA();
|
||||
std::string net_dir = std::string(filepath) + "/networks.d/";
|
||||
ZeroTier::OSUtils::rm((net_dir + nwid + ".conf").c_str());
|
||||
}
|
||||
|
||||
int zts_running()
|
||||
{
|
||||
DEBUG_EXTRA();
|
||||
return ZeroTier::zt1Service == NULL ? false : ZeroTier::zt1Service->isRunning();
|
||||
return zt1Service == NULL ? false : zt1Service->isRunning();
|
||||
}
|
||||
|
||||
int zts_start(const char *path, bool blocking = false)
|
||||
{
|
||||
DEBUG_EXTRA();
|
||||
if (ZeroTier::zt1Service) {
|
||||
if (zt1Service) {
|
||||
return 0; // already initialized, ok
|
||||
}
|
||||
if (path) {
|
||||
ZeroTier::homeDir = path;
|
||||
homeDir = path;
|
||||
}
|
||||
#if defined(__MINGW32__) || defined(__MINGW64__)
|
||||
WSAStartup(MAKEWORD(2, 2), &wsaData); // initialize WinSock. Used in Phy for loopback pipe
|
||||
@@ -426,21 +352,21 @@ int zts_start(const char *path, bool blocking = false)
|
||||
int err = pthread_create(&service_thread, NULL, zts_start_service, NULL);
|
||||
if (blocking) { // block to prevent service calls before we're ready
|
||||
ZT_NodeStatus status;
|
||||
while (zts_running() == false || ZeroTier::zt1Service->getNode() == NULL) {
|
||||
while (zts_running() == false || zt1Service->getNode() == NULL) {
|
||||
nanosleep((const struct timespec[]) {{0, (ZTO_WRAPPER_CHECK_INTERVAL * 500000)}}, NULL);
|
||||
}
|
||||
while (ZeroTier::zt1Service->getNode()->address() <= 0) {
|
||||
while (zt1Service->getNode()->address() <= 0) {
|
||||
nanosleep((const struct timespec[]) {{0, (ZTO_WRAPPER_CHECK_INTERVAL * 500000)}}, NULL);
|
||||
}
|
||||
while (status.online <= 0) {
|
||||
nanosleep((const struct timespec[]) {{0, (ZTO_WRAPPER_CHECK_INTERVAL * 500000)}}, NULL);
|
||||
ZeroTier::zt1Service->getNode()->status(&status);
|
||||
zt1Service->getNode()->status(&status);
|
||||
}
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
int zts_startjoin(const char *path, const char *nwid)
|
||||
int zts_startjoin(const char *path, const uint64_t nwid)
|
||||
{
|
||||
DEBUG_EXTRA();
|
||||
int err = zts_start(path, true);
|
||||
@@ -452,7 +378,6 @@ int zts_startjoin(const char *path, const char *nwid)
|
||||
}
|
||||
catch( ... ) {
|
||||
DEBUG_ERROR("there was a problem joining the virtual network %s", nwid);
|
||||
handle_general_failure();
|
||||
}
|
||||
}
|
||||
while (zts_has_address(nwid) == false) {
|
||||
@@ -461,67 +386,53 @@ int zts_startjoin(const char *path, const char *nwid)
|
||||
return err;
|
||||
}
|
||||
|
||||
void zts_stop()
|
||||
void zts_stop()
|
||||
{
|
||||
DEBUG_EXTRA();
|
||||
if (ZeroTier::zt1Service) {
|
||||
ZeroTier::zt1Service->terminate();
|
||||
disableTaps();
|
||||
if (zt1Service) {
|
||||
zt1Service->terminate();
|
||||
// disableTaps();
|
||||
}
|
||||
#if defined(__MINGW32__) || defined(__MINGW64__)
|
||||
WSACleanup(); // clean up WinSock
|
||||
#endif
|
||||
}
|
||||
|
||||
void zts_get_homepath(char *homePath, size_t len)
|
||||
void zts_get_homepath(char *homePath, size_t len)
|
||||
{
|
||||
DEBUG_EXTRA();
|
||||
if (ZeroTier::homeDir.length()) {
|
||||
if (homeDir.length()) {
|
||||
memset(homePath, 0, len);
|
||||
size_t buf_len = len < ZeroTier::homeDir.length() ? len : ZeroTier::homeDir.length();
|
||||
memcpy(homePath, ZeroTier::homeDir.c_str(), buf_len);
|
||||
size_t buf_len = len < homeDir.length() ? len : homeDir.length();
|
||||
memcpy(homePath, homeDir.c_str(), buf_len);
|
||||
}
|
||||
}
|
||||
|
||||
int zts_get_id(char *devID)
|
||||
uint64_t zts_get_node_id()
|
||||
{
|
||||
DEBUG_EXTRA();
|
||||
if (ZeroTier::zt1Service) {
|
||||
char id[ZTO_ID_LEN];
|
||||
sprintf(id, "%lx",ZeroTier::zt1Service->getNode()->address());
|
||||
memcpy(devID, id, ZTO_ID_LEN);
|
||||
return 0;
|
||||
}
|
||||
else // Service isn't online, try to read ID from file
|
||||
{
|
||||
std::string fname("identity.public");
|
||||
std::string fpath(ZeroTier::homeDir);
|
||||
if (ZeroTier::OSUtils::fileExists((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(),false)) {
|
||||
std::string oldid;
|
||||
ZeroTier::OSUtils::readFile((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(),oldid);
|
||||
memcpy(devID, oldid.c_str(), ZTO_ID_LEN); // first 10 bytes of file
|
||||
return 0;
|
||||
}
|
||||
if (zt1Service) {
|
||||
return zt1Service->getNode()->address();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
unsigned long zts_get_peer_count()
|
||||
unsigned long zts_get_peer_count()
|
||||
{
|
||||
DEBUG_EXTRA();
|
||||
if (ZeroTier::zt1Service) {
|
||||
return ZeroTier::zt1Service->getNode()->peers()->peerCount;
|
||||
if (zt1Service) {
|
||||
return zt1Service->getNode()->peers()->peerCount;
|
||||
}
|
||||
else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int zts_get_peer_address(char *peer, const char *devID)
|
||||
int zts_get_peer_address(char *peer, const uint64_t nodeId)
|
||||
{
|
||||
DEBUG_EXTRA();
|
||||
if (ZeroTier::zt1Service) {
|
||||
ZT_PeerList *pl = ZeroTier::zt1Service->getNode()->peers();
|
||||
if (zt1Service) {
|
||||
ZT_PeerList *pl = zt1Service->getNode()->peers();
|
||||
// uint64_t addr;
|
||||
for (size_t i=0; i<pl->peerCount; i++) {
|
||||
// ZT_Peer *p = &(pl->peers[i]);
|
||||
@@ -539,6 +450,37 @@ void zts_allow_http_control(bool allowed)
|
||||
// TODO
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user