2013-07-04 20:56:19 +00:00
|
|
|
/*
|
|
|
|
* ZeroTier One - Global Peer to Peer Ethernet
|
|
|
|
* Copyright (C) 2012-2013 ZeroTier Networks LLC
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*
|
|
|
|
* --
|
|
|
|
*
|
|
|
|
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
|
|
|
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
*
|
|
|
|
* If you would like to embed ZeroTier into a commercial application or
|
|
|
|
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
|
|
|
* LLC. Start here: http://www.zerotier.com/
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2013-08-13 01:25:36 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
#include <utility>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <list>
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
|
2013-08-13 01:25:36 +00:00
|
|
|
#include "Constants.hpp"
|
|
|
|
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
#include <WinSock2.h>
|
|
|
|
#include <Windows.h>
|
|
|
|
#endif
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
#include "Condition.hpp"
|
|
|
|
#include "Node.hpp"
|
|
|
|
#include "Topology.hpp"
|
|
|
|
#include "Demarc.hpp"
|
2013-08-02 21:17:34 +00:00
|
|
|
#include "Packet.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
#include "Switch.hpp"
|
|
|
|
#include "Utils.hpp"
|
|
|
|
#include "EthernetTap.hpp"
|
|
|
|
#include "Logger.hpp"
|
|
|
|
#include "InetAddress.hpp"
|
2013-07-18 15:43:46 +00:00
|
|
|
#include "Salsa20.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
#include "RuntimeEnvironment.hpp"
|
|
|
|
#include "NodeConfig.hpp"
|
|
|
|
#include "Defaults.hpp"
|
|
|
|
#include "SysEnv.hpp"
|
|
|
|
#include "Network.hpp"
|
|
|
|
#include "MulticastGroup.hpp"
|
|
|
|
#include "Mutex.hpp"
|
2013-07-12 02:06:25 +00:00
|
|
|
#include "Multicaster.hpp"
|
2013-07-13 18:28:26 +00:00
|
|
|
#include "CMWC4096.hpp"
|
2013-09-16 17:57:57 +00:00
|
|
|
#include "SHA512.hpp"
|
2013-08-01 21:32:37 +00:00
|
|
|
#include "Service.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-08-10 14:27:53 +00:00
|
|
|
#ifdef __WINDOWS__
|
|
|
|
#include <Windows.h>
|
|
|
|
#else
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#endif
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
#include "../version.h"
|
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
2013-07-18 15:43:46 +00:00
|
|
|
struct _LocalClientImpl
|
|
|
|
{
|
|
|
|
unsigned char key[32];
|
|
|
|
UdpSocket *sock;
|
|
|
|
void (*resultHandler)(void *,unsigned long,const char *);
|
|
|
|
void *arg;
|
2013-09-17 18:47:48 +00:00
|
|
|
unsigned int controlPort;
|
2013-07-18 20:35:52 +00:00
|
|
|
InetAddress localDestAddr;
|
2013-07-18 15:43:46 +00:00
|
|
|
Mutex inUseLock;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void _CBlocalClientHandler(UdpSocket *sock,void *arg,const InetAddress &remoteAddr,const void *data,unsigned int len)
|
|
|
|
{
|
|
|
|
_LocalClientImpl *impl = (_LocalClientImpl *)arg;
|
2013-07-18 20:35:52 +00:00
|
|
|
if (!impl)
|
|
|
|
return;
|
|
|
|
if (!impl->resultHandler)
|
|
|
|
return; // sanity check
|
2013-07-18 15:43:46 +00:00
|
|
|
Mutex::Lock _l(impl->inUseLock);
|
2013-07-18 20:35:52 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
unsigned long convId = 0;
|
|
|
|
std::vector<std::string> results;
|
|
|
|
if (!NodeConfig::decodeControlMessagePacket(impl->key,data,len,convId,results))
|
|
|
|
return;
|
|
|
|
for(std::vector<std::string>::iterator r(results.begin());r!=results.end();++r)
|
|
|
|
impl->resultHandler(impl->arg,convId,r->c_str());
|
|
|
|
} catch ( ... ) {}
|
2013-07-18 15:43:46 +00:00
|
|
|
}
|
|
|
|
|
2013-09-17 18:47:48 +00:00
|
|
|
Node::LocalClient::LocalClient(const char *authToken,unsigned int controlPort,void (*resultHandler)(void *,unsigned long,const char *),void *arg)
|
2013-07-18 15:43:46 +00:00
|
|
|
throw() :
|
|
|
|
_impl((void *)0)
|
|
|
|
{
|
|
|
|
_LocalClientImpl *impl = new _LocalClientImpl;
|
|
|
|
|
|
|
|
UdpSocket *sock = (UdpSocket *)0;
|
|
|
|
for(unsigned int i=0;i<5000;++i) {
|
|
|
|
try {
|
|
|
|
sock = new UdpSocket(true,32768 + (rand() % 20000),false,&_CBlocalClientHandler,impl);
|
|
|
|
break;
|
|
|
|
} catch ( ... ) {
|
|
|
|
sock = (UdpSocket *)0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If socket fails to bind, there's a big problem like missing IPv4 stack
|
|
|
|
if (sock) {
|
2013-09-16 17:57:57 +00:00
|
|
|
{
|
|
|
|
unsigned int csk[64];
|
|
|
|
SHA512::hash(csk,authToken,strlen(authToken));
|
|
|
|
memcpy(impl->key,csk,32);
|
|
|
|
}
|
2013-07-18 15:43:46 +00:00
|
|
|
|
|
|
|
impl->sock = sock;
|
|
|
|
impl->resultHandler = resultHandler;
|
|
|
|
impl->arg = arg;
|
2013-09-17 18:47:48 +00:00
|
|
|
impl->controlPort = (controlPort) ? controlPort : (unsigned int)ZT_DEFAULT_CONTROL_UDP_PORT;
|
2013-07-18 20:35:52 +00:00
|
|
|
impl->localDestAddr = InetAddress::LO4;
|
2013-09-17 18:47:48 +00:00
|
|
|
impl->localDestAddr.setPort(impl->controlPort);
|
2013-07-18 15:43:46 +00:00
|
|
|
_impl = impl;
|
|
|
|
} else delete impl;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node::LocalClient::~LocalClient()
|
|
|
|
{
|
|
|
|
if (_impl) {
|
|
|
|
((_LocalClientImpl *)_impl)->inUseLock.lock();
|
|
|
|
delete ((_LocalClientImpl *)_impl)->sock;
|
|
|
|
((_LocalClientImpl *)_impl)->inUseLock.unlock();
|
|
|
|
delete ((_LocalClientImpl *)_impl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long Node::LocalClient::send(const char *command)
|
|
|
|
throw()
|
|
|
|
{
|
2013-07-18 20:35:52 +00:00
|
|
|
if (!_impl)
|
|
|
|
return 0;
|
|
|
|
_LocalClientImpl *impl = (_LocalClientImpl *)_impl;
|
|
|
|
Mutex::Lock _l(impl->inUseLock);
|
|
|
|
|
|
|
|
try {
|
|
|
|
uint32_t convId = (uint32_t)rand();
|
|
|
|
if (!convId)
|
|
|
|
convId = 1;
|
2013-07-18 15:43:46 +00:00
|
|
|
|
2013-07-18 20:35:52 +00:00
|
|
|
std::vector<std::string> tmp;
|
|
|
|
tmp.push_back(std::string(command));
|
|
|
|
std::vector< Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> > packets(NodeConfig::encodeControlMessage(impl->key,convId,tmp));
|
|
|
|
|
|
|
|
for(std::vector< Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> >::iterator p(packets.begin());p!=packets.end();++p)
|
|
|
|
impl->sock->send(impl->localDestAddr,p->data(),p->size(),-1);
|
|
|
|
|
|
|
|
return convId;
|
|
|
|
} catch ( ... ) {
|
|
|
|
return 0;
|
|
|
|
}
|
2013-07-18 15:43:46 +00:00
|
|
|
}
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
struct _NodeImpl
|
|
|
|
{
|
|
|
|
RuntimeEnvironment renv;
|
2013-09-17 18:47:48 +00:00
|
|
|
unsigned int port;
|
|
|
|
unsigned int controlPort;
|
2013-07-04 20:56:19 +00:00
|
|
|
std::string reasonForTerminationStr;
|
2013-08-30 19:02:12 +00:00
|
|
|
volatile Node::ReasonForTermination reasonForTermination;
|
2013-07-06 18:58:34 +00:00
|
|
|
volatile bool started;
|
|
|
|
volatile bool running;
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-08-30 19:02:12 +00:00
|
|
|
inline Node::ReasonForTermination terminate()
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
|
|
|
RuntimeEnvironment *_r = &renv;
|
2013-08-30 19:02:12 +00:00
|
|
|
LOG("terminating: %s",reasonForTerminationStr.c_str());
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-08-27 22:00:07 +00:00
|
|
|
renv.shutdownInProgress = true;
|
|
|
|
Thread::sleep(500);
|
|
|
|
|
2013-08-30 19:02:12 +00:00
|
|
|
running = false;
|
|
|
|
|
2013-08-27 22:00:07 +00:00
|
|
|
#ifndef __WINDOWS__
|
|
|
|
delete renv.netconfService;
|
|
|
|
#endif
|
|
|
|
delete renv.nc;
|
|
|
|
delete renv.sysEnv;
|
|
|
|
delete renv.topology;
|
|
|
|
delete renv.demarc;
|
|
|
|
delete renv.sw;
|
2013-09-25 21:41:49 +00:00
|
|
|
delete renv.mc;
|
2013-08-27 22:00:07 +00:00
|
|
|
delete renv.prng;
|
|
|
|
delete renv.log;
|
|
|
|
|
2013-08-30 19:02:12 +00:00
|
|
|
return reasonForTermination;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Node::ReasonForTermination terminateBecause(Node::ReasonForTermination r,const char *rstr)
|
|
|
|
{
|
2013-07-04 20:56:19 +00:00
|
|
|
reasonForTerminationStr = rstr;
|
|
|
|
reasonForTermination = r;
|
2013-08-30 19:02:12 +00:00
|
|
|
return terminate();
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-02 21:17:34 +00:00
|
|
|
#ifndef __WINDOWS__
|
2013-08-01 21:32:37 +00:00
|
|
|
static void _netconfServiceMessageHandler(void *renv,Service &svc,const Dictionary &msg)
|
|
|
|
{
|
2013-08-02 21:17:34 +00:00
|
|
|
if (!renv)
|
|
|
|
return; // sanity check
|
|
|
|
const RuntimeEnvironment *_r = (const RuntimeEnvironment *)renv;
|
|
|
|
|
|
|
|
try {
|
2013-08-06 05:28:56 +00:00
|
|
|
//TRACE("from netconf:\n%s",msg.toString().c_str());
|
2013-08-02 21:17:34 +00:00
|
|
|
const std::string &type = msg.get("type");
|
|
|
|
if (type == "netconf-response") {
|
|
|
|
uint64_t inRePacketId = strtoull(msg.get("requestId").c_str(),(char **)0,16);
|
2013-08-06 05:28:56 +00:00
|
|
|
uint64_t nwid = strtoull(msg.get("nwid").c_str(),(char **)0,16);
|
2013-08-02 21:17:34 +00:00
|
|
|
Address peerAddress(msg.get("peer").c_str());
|
|
|
|
|
2013-08-06 05:28:56 +00:00
|
|
|
if (peerAddress) {
|
2013-08-02 21:17:34 +00:00
|
|
|
if (msg.contains("error")) {
|
|
|
|
Packet::ErrorCode errCode = Packet::ERROR_INVALID_REQUEST;
|
|
|
|
const std::string &err = msg.get("error");
|
2013-08-13 01:27:07 +00:00
|
|
|
if (err == "OBJ_NOT_FOUND")
|
|
|
|
errCode = Packet::ERROR_OBJ_NOT_FOUND;
|
2013-10-16 21:47:26 +00:00
|
|
|
else if (err == "ACCESS_DENIED")
|
|
|
|
errCode = Packet::ERROR_NETWORK_ACCESS_DENIED;
|
2013-08-02 21:17:34 +00:00
|
|
|
|
|
|
|
Packet outp(peerAddress,_r->identity.address(),Packet::VERB_ERROR);
|
|
|
|
outp.append((unsigned char)Packet::VERB_NETWORK_CONFIG_REQUEST);
|
|
|
|
outp.append(inRePacketId);
|
|
|
|
outp.append((unsigned char)errCode);
|
2013-08-06 05:28:56 +00:00
|
|
|
outp.append(nwid);
|
2013-08-02 21:17:34 +00:00
|
|
|
_r->sw->send(outp,true);
|
|
|
|
} else if (msg.contains("netconf")) {
|
|
|
|
const std::string &netconf = msg.get("netconf");
|
|
|
|
if (netconf.length() < 2048) { // sanity check
|
|
|
|
Packet outp(peerAddress,_r->identity.address(),Packet::VERB_OK);
|
|
|
|
outp.append((unsigned char)Packet::VERB_NETWORK_CONFIG_REQUEST);
|
|
|
|
outp.append(inRePacketId);
|
2013-08-06 05:28:56 +00:00
|
|
|
outp.append(nwid);
|
2013-08-02 21:17:34 +00:00
|
|
|
outp.append((uint16_t)netconf.length());
|
|
|
|
outp.append(netconf.data(),netconf.length());
|
2013-08-03 16:53:46 +00:00
|
|
|
outp.compress();
|
2013-08-02 21:17:34 +00:00
|
|
|
_r->sw->send(outp,true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-17 09:37:01 +00:00
|
|
|
} else if (type == "netconf-push") {
|
|
|
|
if (msg.contains("to")) {
|
|
|
|
Dictionary to(msg.get("to")); // key: peer address, value: comma-delimited network list
|
|
|
|
for(Dictionary::iterator t(to.begin());t!=to.end();++t) {
|
|
|
|
Address ztaddr(t->first);
|
|
|
|
if (ztaddr) {
|
|
|
|
Packet outp(ztaddr,_r->identity.address(),Packet::VERB_NETWORK_CONFIG_REFRESH);
|
|
|
|
|
|
|
|
char *saveptr = (char *)0;
|
|
|
|
// Note: this loop trashes t->second, which is quasi-legal C++ but
|
|
|
|
// shouldn't break anything as long as we don't try to use 'to'
|
|
|
|
// for anything interesting after doing this.
|
|
|
|
for(char *p=Utils::stok(const_cast<char *>(t->second.c_str()),",",&saveptr);(p);p=Utils::stok((char *)0,",",&saveptr)) {
|
|
|
|
uint64_t nwid = Utils::hexStrToU64(p);
|
|
|
|
if (nwid) {
|
|
|
|
if ((outp.size() + sizeof(uint64_t)) >= ZT_UDP_DEFAULT_PAYLOAD_MTU) {
|
|
|
|
_r->sw->send(outp,true);
|
|
|
|
outp.reset(ztaddr,_r->identity.address(),Packet::VERB_NETWORK_CONFIG_REFRESH);
|
|
|
|
}
|
|
|
|
outp.append(nwid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (outp.payloadLength())
|
|
|
|
_r->sw->send(outp,true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-02 21:17:34 +00:00
|
|
|
}
|
|
|
|
} catch (std::exception &exc) {
|
|
|
|
LOG("unexpected exception parsing response from netconf service: %s",exc.what());
|
|
|
|
} catch ( ... ) {
|
|
|
|
LOG("unexpected exception parsing response from netconf service: unknown exception");
|
|
|
|
}
|
2013-08-01 21:32:37 +00:00
|
|
|
}
|
2013-08-02 21:17:34 +00:00
|
|
|
#endif // !__WINDOWS__
|
2013-08-01 21:32:37 +00:00
|
|
|
|
2013-09-17 18:47:48 +00:00
|
|
|
Node::Node(const char *hp,unsigned int port,unsigned int controlPort)
|
2013-07-04 20:56:19 +00:00
|
|
|
throw() :
|
|
|
|
_impl(new _NodeImpl)
|
|
|
|
{
|
|
|
|
_NodeImpl *impl = (_NodeImpl *)_impl;
|
2013-08-26 21:22:20 +00:00
|
|
|
if ((hp)&&(strlen(hp) > 0))
|
2013-08-24 21:10:34 +00:00
|
|
|
impl->renv.homePath = hp;
|
|
|
|
else impl->renv.homePath = ZT_DEFAULTS.defaultHomePath;
|
2013-09-17 18:47:48 +00:00
|
|
|
impl->port = (port) ? port : (unsigned int)ZT_DEFAULT_UDP_PORT;
|
|
|
|
impl->controlPort = (controlPort) ? controlPort : (unsigned int)ZT_DEFAULT_CONTROL_UDP_PORT;
|
2013-07-04 20:56:19 +00:00
|
|
|
impl->reasonForTermination = Node::NODE_RUNNING;
|
|
|
|
impl->started = false;
|
|
|
|
impl->running = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node::~Node()
|
|
|
|
{
|
2013-08-27 22:00:07 +00:00
|
|
|
delete (_NodeImpl *)_impl;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Node::ReasonForTermination Node::run()
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
_NodeImpl *impl = (_NodeImpl *)_impl;
|
|
|
|
RuntimeEnvironment *_r = (RuntimeEnvironment *)&(impl->renv);
|
|
|
|
|
|
|
|
impl->started = true;
|
|
|
|
impl->running = true;
|
|
|
|
|
|
|
|
try {
|
|
|
|
#ifdef ZT_LOG_STDOUT
|
|
|
|
_r->log = new Logger((const char *)0,(const char *)0,0);
|
|
|
|
#else
|
|
|
|
_r->log = new Logger((_r->homePath + ZT_PATH_SEPARATOR_S + "node.log").c_str(),(const char *)0,131072);
|
|
|
|
#endif
|
|
|
|
|
2013-09-19 19:17:11 +00:00
|
|
|
LOG("starting version %s",versionString());
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-07-17 18:39:34 +00:00
|
|
|
// Create non-crypto PRNG right away in case other code in init wants to use it
|
2013-07-13 18:28:26 +00:00
|
|
|
_r->prng = new CMWC4096();
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
bool gotId = false;
|
|
|
|
std::string identitySecretPath(_r->homePath + ZT_PATH_SEPARATOR_S + "identity.secret");
|
|
|
|
std::string identityPublicPath(_r->homePath + ZT_PATH_SEPARATOR_S + "identity.public");
|
|
|
|
std::string idser;
|
|
|
|
if (Utils::readFile(identitySecretPath.c_str(),idser))
|
|
|
|
gotId = _r->identity.fromString(idser);
|
2013-10-07 19:00:38 +00:00
|
|
|
if ((gotId)&&(!_r->identity.locallyValidate()))
|
|
|
|
gotId = false;
|
2013-07-04 20:56:19 +00:00
|
|
|
if (gotId) {
|
|
|
|
// Make sure identity.public matches identity.secret
|
|
|
|
idser = std::string();
|
|
|
|
Utils::readFile(identityPublicPath.c_str(),idser);
|
|
|
|
std::string pubid(_r->identity.toString(false));
|
|
|
|
if (idser != pubid) {
|
|
|
|
if (!Utils::writeFile(identityPublicPath.c_str(),pubid))
|
|
|
|
return impl->terminateBecause(Node::NODE_UNRECOVERABLE_ERROR,"could not write identity.public (home path not writable?)");
|
|
|
|
}
|
|
|
|
} else {
|
2013-09-17 18:47:48 +00:00
|
|
|
LOG("no identity found or identity invalid, generating one... this might take a few seconds...");
|
2013-07-04 20:56:19 +00:00
|
|
|
_r->identity.generate();
|
|
|
|
LOG("generated new identity: %s",_r->identity.address().toString().c_str());
|
|
|
|
idser = _r->identity.toString(true);
|
|
|
|
if (!Utils::writeFile(identitySecretPath.c_str(),idser))
|
|
|
|
return impl->terminateBecause(Node::NODE_UNRECOVERABLE_ERROR,"could not write identity.secret (home path not writable?)");
|
|
|
|
idser = _r->identity.toString(false);
|
|
|
|
if (!Utils::writeFile(identityPublicPath.c_str(),idser))
|
|
|
|
return impl->terminateBecause(Node::NODE_UNRECOVERABLE_ERROR,"could not write identity.public (home path not writable?)");
|
|
|
|
}
|
|
|
|
Utils::lockDownFile(identitySecretPath.c_str(),false);
|
|
|
|
|
2013-07-17 18:39:34 +00:00
|
|
|
// Clean up some obsolete files if present -- this will be removed later
|
2013-08-08 14:06:39 +00:00
|
|
|
Utils::rm((_r->homePath + ZT_PATH_SEPARATOR_S + "status"));
|
|
|
|
Utils::rm((_r->homePath + ZT_PATH_SEPARATOR_S + "thisdeviceismine"));
|
2013-10-21 14:29:44 +00:00
|
|
|
Utils::rm((_r->homePath + ZT_PATH_SEPARATOR_S + "peer.db"));
|
2013-07-17 18:39:34 +00:00
|
|
|
|
2013-08-06 04:05:39 +00:00
|
|
|
// Make sure networks.d exists
|
2013-08-12 20:57:34 +00:00
|
|
|
#ifdef __WINDOWS__
|
2013-08-14 15:19:21 +00:00
|
|
|
CreateDirectoryA((_r->homePath + ZT_PATH_SEPARATOR_S + "networks.d").c_str(),NULL);
|
2013-08-12 20:57:34 +00:00
|
|
|
#else
|
2013-08-06 04:05:39 +00:00
|
|
|
mkdir((_r->homePath + ZT_PATH_SEPARATOR_S + "networks.d").c_str(),0700);
|
2013-08-12 20:57:34 +00:00
|
|
|
#endif
|
2013-08-06 04:05:39 +00:00
|
|
|
|
2013-07-17 18:39:34 +00:00
|
|
|
// Load or generate config authentication secret
|
|
|
|
std::string configAuthTokenPath(_r->homePath + ZT_PATH_SEPARATOR_S + "authtoken.secret");
|
|
|
|
std::string configAuthToken;
|
|
|
|
if (!Utils::readFile(configAuthTokenPath.c_str(),configAuthToken)) {
|
|
|
|
configAuthToken = "";
|
|
|
|
unsigned int sr = 0;
|
2013-07-13 17:26:27 +00:00
|
|
|
for(unsigned int i=0;i<24;++i) {
|
2013-07-17 18:39:34 +00:00
|
|
|
Utils::getSecureRandom(&sr,sizeof(sr));
|
|
|
|
configAuthToken.push_back("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"[sr % 62]);
|
2013-07-13 17:26:27 +00:00
|
|
|
}
|
2013-07-17 18:39:34 +00:00
|
|
|
if (!Utils::writeFile(configAuthTokenPath.c_str(),configAuthToken))
|
|
|
|
return impl->terminateBecause(Node::NODE_UNRECOVERABLE_ERROR,"could not write authtoken.secret (home path not writable?)");
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
2013-07-17 18:39:34 +00:00
|
|
|
Utils::lockDownFile(configAuthTokenPath.c_str(),false);
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-09-17 18:47:48 +00:00
|
|
|
// Create the objects that make up runtime state.
|
2013-09-25 21:41:49 +00:00
|
|
|
_r->mc = new Multicaster();
|
2013-08-06 05:28:56 +00:00
|
|
|
_r->sw = new Switch(_r);
|
2013-08-27 22:00:07 +00:00
|
|
|
_r->demarc = new Demarc(_r);
|
2013-10-21 14:29:44 +00:00
|
|
|
_r->topology = new Topology(_r);
|
2013-08-06 05:28:56 +00:00
|
|
|
_r->sysEnv = new SysEnv(_r);
|
2013-07-17 18:39:34 +00:00
|
|
|
try {
|
2013-09-17 18:47:48 +00:00
|
|
|
_r->nc = new NodeConfig(_r,configAuthToken.c_str(),impl->controlPort);
|
2013-08-26 21:22:20 +00:00
|
|
|
} catch (std::exception &exc) {
|
2013-09-17 18:47:48 +00:00
|
|
|
char foo[1024];
|
|
|
|
Utils::snprintf(foo,sizeof(foo),"unable to bind to local control port %u: is another instance of ZeroTier One already running?",impl->controlPort);
|
|
|
|
return impl->terminateBecause(Node::NODE_UNRECOVERABLE_ERROR,foo);
|
2013-07-17 18:39:34 +00:00
|
|
|
}
|
2013-08-30 19:02:12 +00:00
|
|
|
_r->node = this;
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-09-17 18:47:48 +00:00
|
|
|
// Bind local port for core I/O
|
|
|
|
if (!_r->demarc->bindLocalUdp(impl->port)) {
|
|
|
|
char foo[1024];
|
|
|
|
Utils::snprintf(foo,sizeof(foo),"unable to bind to global I/O port %u: is another instance of ZeroTier One already running?",impl->controlPort);
|
|
|
|
return impl->terminateBecause(Node::NODE_UNRECOVERABLE_ERROR,foo);
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2013-09-17 18:47:48 +00:00
|
|
|
// Set initial supernode list
|
2013-07-04 20:56:19 +00:00
|
|
|
_r->topology->setSupernodes(ZT_DEFAULTS.supernodes);
|
|
|
|
} catch (std::bad_alloc &exc) {
|
|
|
|
return impl->terminateBecause(Node::NODE_UNRECOVERABLE_ERROR,"memory allocation failure");
|
|
|
|
} catch (std::runtime_error &exc) {
|
|
|
|
return impl->terminateBecause(Node::NODE_UNRECOVERABLE_ERROR,exc.what());
|
|
|
|
} catch ( ... ) {
|
|
|
|
return impl->terminateBecause(Node::NODE_UNRECOVERABLE_ERROR,"unknown exception during initialization");
|
|
|
|
}
|
|
|
|
|
2013-09-17 18:47:48 +00:00
|
|
|
// Start external service subprocesses, which is only used by special nodes
|
|
|
|
// right now and isn't available on Windows.
|
2013-08-01 21:32:37 +00:00
|
|
|
#ifndef __WINDOWS__
|
|
|
|
try {
|
|
|
|
std::string netconfServicePath(_r->homePath + ZT_PATH_SEPARATOR_S + "services.d" + ZT_PATH_SEPARATOR_S + "netconf.service");
|
|
|
|
if (Utils::fileExists(netconfServicePath.c_str())) {
|
|
|
|
LOG("netconf.d/netconfi.service appears to exist, starting...");
|
|
|
|
_r->netconfService = new Service(_r,"netconf",netconfServicePath.c_str(),&_netconfServiceMessageHandler,_r);
|
|
|
|
}
|
|
|
|
} catch ( ... ) {
|
|
|
|
LOG("unexpected exception attempting to start services");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-09-17 18:47:48 +00:00
|
|
|
// Core I/O loop
|
2013-07-04 20:56:19 +00:00
|
|
|
try {
|
2013-10-07 19:00:38 +00:00
|
|
|
uint64_t lastNetworkAutoconfCheck = Utils::now() - 5000; // check autoconf again after 5s for startup
|
2013-07-04 20:56:19 +00:00
|
|
|
uint64_t lastPingCheck = 0;
|
2013-07-30 15:14:53 +00:00
|
|
|
uint64_t lastClean = Utils::now(); // don't need to do this immediately
|
2013-07-04 20:56:19 +00:00
|
|
|
uint64_t lastNetworkFingerprintCheck = 0;
|
|
|
|
uint64_t networkConfigurationFingerprint = _r->sysEnv->getNetworkConfigurationFingerprint();
|
|
|
|
uint64_t lastMulticastCheck = 0;
|
|
|
|
long lastDelayDelta = 0;
|
|
|
|
|
2013-08-30 19:02:12 +00:00
|
|
|
while (impl->reasonForTermination == NODE_RUNNING) {
|
2013-07-04 20:56:19 +00:00
|
|
|
uint64_t now = Utils::now();
|
2013-08-03 16:53:46 +00:00
|
|
|
bool resynchronize = false;
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
// Detect sleep/wake by looking for delay loop pauses that are longer
|
|
|
|
// than we intended to pause.
|
|
|
|
if (lastDelayDelta >= ZT_SLEEP_WAKE_DETECTION_THRESHOLD) {
|
2013-08-03 16:53:46 +00:00
|
|
|
resynchronize = true;
|
2013-07-04 20:56:19 +00:00
|
|
|
LOG("probable suspend/resume detected, pausing a moment for things to settle...");
|
2013-08-08 13:19:36 +00:00
|
|
|
Thread::sleep(ZT_SLEEP_WAKE_SETTLE_TIME);
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Periodically check our network environment, sending pings out to all
|
|
|
|
// our direct links if things look like we got a different address.
|
2013-08-03 16:53:46 +00:00
|
|
|
if ((resynchronize)||((now - lastNetworkFingerprintCheck) >= ZT_NETWORK_FINGERPRINT_CHECK_DELAY)) {
|
2013-07-04 20:56:19 +00:00
|
|
|
lastNetworkFingerprintCheck = now;
|
|
|
|
uint64_t fp = _r->sysEnv->getNetworkConfigurationFingerprint();
|
|
|
|
if (fp != networkConfigurationFingerprint) {
|
2013-07-09 00:36:33 +00:00
|
|
|
LOG("netconf fingerprint change: %.16llx != %.16llx, resyncing with network",networkConfigurationFingerprint,fp);
|
2013-07-04 20:56:19 +00:00
|
|
|
networkConfigurationFingerprint = fp;
|
2013-08-03 16:53:46 +00:00
|
|
|
resynchronize = true;
|
|
|
|
_r->nc->whackAllTaps(); // call whack() on all tap devices -- hack, might go away
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-05 16:34:54 +00:00
|
|
|
// Request configuration for unconfigured nets, or nets with out of date
|
|
|
|
// configuration information.
|
|
|
|
if ((resynchronize)||((now - lastNetworkAutoconfCheck) >= ZT_NETWORK_AUTOCONF_CHECK_DELAY)) {
|
|
|
|
lastNetworkAutoconfCheck = now;
|
|
|
|
std::vector< SharedPtr<Network> > nets(_r->nc->networks());
|
|
|
|
for(std::vector< SharedPtr<Network> >::iterator n(nets.begin());n!=nets.end();++n) {
|
|
|
|
if ((now - (*n)->lastConfigUpdate()) >= ZT_NETWORK_AUTOCONF_DELAY)
|
|
|
|
(*n)->requestConfiguration();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
// Periodically check for changes in our local multicast subscriptions and broadcast
|
|
|
|
// those changes to peers.
|
2013-08-03 16:53:46 +00:00
|
|
|
if ((resynchronize)||((now - lastMulticastCheck) >= ZT_MULTICAST_LOCAL_POLL_PERIOD)) {
|
2013-07-04 20:56:19 +00:00
|
|
|
lastMulticastCheck = now;
|
|
|
|
try {
|
|
|
|
std::map< SharedPtr<Network>,std::set<MulticastGroup> > toAnnounce;
|
2013-10-01 20:01:36 +00:00
|
|
|
std::vector< SharedPtr<Network> > networks(_r->nc->networks());
|
|
|
|
for(std::vector< SharedPtr<Network> >::const_iterator nw(networks.begin());nw!=networks.end();++nw) {
|
|
|
|
if ((*nw)->updateMulticastGroups())
|
|
|
|
toAnnounce.insert(std::pair< SharedPtr<Network>,std::set<MulticastGroup> >(*nw,(*nw)->multicastGroups()));
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
2013-10-01 20:01:36 +00:00
|
|
|
if (toAnnounce.size())
|
2013-07-04 20:56:19 +00:00
|
|
|
_r->sw->announceMulticastGroups(toAnnounce);
|
|
|
|
} catch (std::exception &exc) {
|
|
|
|
LOG("unexpected exception announcing multicast groups: %s",exc.what());
|
|
|
|
} catch ( ... ) {
|
|
|
|
LOG("unexpected exception announcing multicast groups: (unknown)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-03 16:53:46 +00:00
|
|
|
if ((resynchronize)||((now - lastPingCheck) >= ZT_PING_CHECK_DELAY)) {
|
2013-07-04 20:56:19 +00:00
|
|
|
lastPingCheck = now;
|
|
|
|
try {
|
2013-07-23 17:23:55 +00:00
|
|
|
if (_r->topology->amSupernode()) {
|
2013-08-05 16:34:54 +00:00
|
|
|
// Supernodes are so super they don't even have to ping out, since
|
|
|
|
// all nodes ping them. They're also never firewalled so they
|
|
|
|
// don't need firewall openers. They just ping each other.
|
2013-07-04 20:56:19 +00:00
|
|
|
std::vector< SharedPtr<Peer> > sns(_r->topology->supernodePeers());
|
|
|
|
for(std::vector< SharedPtr<Peer> >::const_iterator p(sns.begin());p!=sns.end();++p) {
|
|
|
|
if ((now - (*p)->lastDirectSend()) > ZT_PEER_DIRECT_PING_DELAY)
|
|
|
|
_r->sw->sendHELLO((*p)->address());
|
|
|
|
}
|
|
|
|
} else {
|
2013-10-02 20:12:10 +00:00
|
|
|
if (resynchronize)
|
|
|
|
_r->topology->eachPeer(Topology::PingAllActivePeers(_r,now));
|
|
|
|
else _r->topology->eachPeer(Topology::PingPeersThatNeedPing(_r,now));
|
|
|
|
_r->topology->eachPeer(Topology::OpenPeersThatNeedFirewallOpener(_r,now));
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
} catch (std::exception &exc) {
|
|
|
|
LOG("unexpected exception running ping check cycle: %s",exc.what());
|
|
|
|
} catch ( ... ) {
|
|
|
|
LOG("unexpected exception running ping check cycle: (unkonwn)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-30 15:14:53 +00:00
|
|
|
if ((now - lastClean) >= ZT_DB_CLEAN_PERIOD) {
|
|
|
|
lastClean = now;
|
2013-09-25 21:41:49 +00:00
|
|
|
_r->mc->clean();
|
2013-07-30 15:14:53 +00:00
|
|
|
_r->topology->clean();
|
2013-08-06 04:05:39 +00:00
|
|
|
_r->nc->clean();
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
unsigned long delay = std::min((unsigned long)ZT_MIN_SERVICE_LOOP_INTERVAL,_r->sw->doTimerTasks());
|
|
|
|
uint64_t start = Utils::now();
|
2013-07-13 02:07:48 +00:00
|
|
|
_r->mainLoopWaitCondition.wait(delay);
|
2013-08-05 16:34:54 +00:00
|
|
|
lastDelayDelta = (long)(Utils::now() - start) - (long)delay; // used to detect sleep/wake
|
2013-07-04 20:56:19 +00:00
|
|
|
} catch (std::exception &exc) {
|
|
|
|
LOG("unexpected exception running Switch doTimerTasks: %s",exc.what());
|
|
|
|
} catch ( ... ) {
|
|
|
|
LOG("unexpected exception running Switch doTimerTasks: (unknown)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch ( ... ) {
|
|
|
|
return impl->terminateBecause(Node::NODE_UNRECOVERABLE_ERROR,"unexpected exception during outer main I/O loop");
|
|
|
|
}
|
|
|
|
|
2013-08-30 19:02:12 +00:00
|
|
|
return impl->terminate();
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *Node::reasonForTermination() const
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
if ((!((_NodeImpl *)_impl)->started)||(((_NodeImpl *)_impl)->running))
|
|
|
|
return (const char *)0;
|
|
|
|
return ((_NodeImpl *)_impl)->reasonForTerminationStr.c_str();
|
|
|
|
}
|
|
|
|
|
2013-08-30 19:02:12 +00:00
|
|
|
void Node::terminate(ReasonForTermination reason,const char *reasonText)
|
2013-07-04 20:56:19 +00:00
|
|
|
throw()
|
|
|
|
{
|
2013-08-30 19:02:12 +00:00
|
|
|
((_NodeImpl *)_impl)->reasonForTermination = reason;
|
|
|
|
((_NodeImpl *)_impl)->reasonForTerminationStr = ((reasonText) ? reasonText : "");
|
2013-07-13 02:07:48 +00:00
|
|
|
((_NodeImpl *)_impl)->renv.mainLoopWaitCondition.signal();
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class _VersionStringMaker
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
char vs[32];
|
|
|
|
_VersionStringMaker()
|
|
|
|
{
|
2013-08-30 21:05:43 +00:00
|
|
|
Utils::snprintf(vs,sizeof(vs),"%d.%d.%d",(int)ZEROTIER_ONE_VERSION_MAJOR,(int)ZEROTIER_ONE_VERSION_MINOR,(int)ZEROTIER_ONE_VERSION_REVISION);
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
~_VersionStringMaker() {}
|
|
|
|
};
|
|
|
|
static const _VersionStringMaker __versionString;
|
|
|
|
|
|
|
|
const char *Node::versionString() throw() { return __versionString.vs; }
|
|
|
|
|
|
|
|
unsigned int Node::versionMajor() throw() { return ZEROTIER_ONE_VERSION_MAJOR; }
|
|
|
|
unsigned int Node::versionMinor() throw() { return ZEROTIER_ONE_VERSION_MINOR; }
|
|
|
|
unsigned int Node::versionRevision() throw() { return ZEROTIER_ONE_VERSION_REVISION; }
|
|
|
|
|
|
|
|
// Scanned for by loader and/or updater to determine a binary's version
|
|
|
|
const unsigned char EMBEDDED_VERSION_STAMP[20] = {
|
|
|
|
0x6d,0xfe,0xff,0x01,0x90,0xfa,0x89,0x57,0x88,0xa1,0xaa,0xdc,0xdd,0xde,0xb0,0x33,
|
|
|
|
ZEROTIER_ONE_VERSION_MAJOR,
|
|
|
|
ZEROTIER_ONE_VERSION_MINOR,
|
|
|
|
(unsigned char)(((unsigned int)ZEROTIER_ONE_VERSION_REVISION) & 0xff), /* little-endian */
|
|
|
|
(unsigned char)((((unsigned int)ZEROTIER_ONE_VERSION_REVISION) >> 8) & 0xff)
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
2013-08-26 21:22:20 +00:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
2013-09-17 18:47:48 +00:00
|
|
|
ZeroTier::Node *zeroTierCreateNode(const char *hp,unsigned int port,unsigned int controlPort)
|
2013-08-26 21:22:20 +00:00
|
|
|
{
|
2013-09-17 18:47:48 +00:00
|
|
|
return new ZeroTier::Node(hp,port,controlPort);
|
2013-08-26 21:22:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void zeroTierDeleteNode(ZeroTier::Node *n)
|
|
|
|
{
|
|
|
|
delete n;
|
|
|
|
}
|
|
|
|
|
2013-09-17 18:47:48 +00:00
|
|
|
ZeroTier::Node::LocalClient *zeroTierCreateLocalClient(const char *authToken,unsigned int controlPort,void (*resultHandler)(void *,unsigned long,const char *),void *arg)
|
2013-08-26 21:22:20 +00:00
|
|
|
{
|
2013-09-17 18:47:48 +00:00
|
|
|
return new ZeroTier::Node::LocalClient(authToken,controlPort,resultHandler,arg);
|
2013-08-26 21:22:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void zeroTierDeleteLocalClient(ZeroTier::Node::LocalClient *lc)
|
|
|
|
{
|
|
|
|
delete lc;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // extern "C"
|