2013-07-04 20:56:19 +00:00
|
|
|
/*
|
|
|
|
* ZeroTier One - Global Peer to Peer Ethernet
|
2014-02-16 20:40:22 +00:00
|
|
|
* Copyright (C) 2011-2014 ZeroTier Networks LLC
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
|
|
|
* 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>
|
2014-01-21 00:16:01 +00:00
|
|
|
#include <ShlObj.h>
|
2014-03-19 20:56:48 +00:00
|
|
|
#else
|
2014-01-26 18:59:33 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#endif
|
|
|
|
|
2014-04-10 21:22:25 +00:00
|
|
|
#include "../version.h"
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
#include "Node.hpp"
|
2014-03-19 20:56:48 +00:00
|
|
|
#include "RuntimeEnvironment.hpp"
|
|
|
|
#include "Logger.hpp"
|
|
|
|
#include "Utils.hpp"
|
|
|
|
#include "Defaults.hpp"
|
|
|
|
#include "Identity.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
#include "Topology.hpp"
|
2014-03-18 19:21:22 +00:00
|
|
|
#include "SocketManager.hpp"
|
2014-04-10 23:30:15 +00:00
|
|
|
#include "Packet.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
#include "Switch.hpp"
|
|
|
|
#include "EthernetTap.hpp"
|
2014-03-19 20:56:48 +00:00
|
|
|
#include "CMWC4096.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
#include "NodeConfig.hpp"
|
|
|
|
#include "Network.hpp"
|
|
|
|
#include "MulticastGroup.hpp"
|
2014-09-30 23:28:25 +00:00
|
|
|
#include "Multicaster.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
#include "Mutex.hpp"
|
2013-08-01 21:32:37 +00:00
|
|
|
#include "Service.hpp"
|
2013-12-11 21:00:18 +00:00
|
|
|
#include "SoftwareUpdater.hpp"
|
2014-03-18 21:33:57 +00:00
|
|
|
#include "Buffer.hpp"
|
2014-04-10 21:22:25 +00:00
|
|
|
#include "AntiRecursion.hpp"
|
2014-07-31 21:09:32 +00:00
|
|
|
#include "RoutingTable.hpp"
|
2014-08-16 03:37:35 +00:00
|
|
|
#include "HttpClient.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
struct _NodeImpl
|
|
|
|
{
|
|
|
|
RuntimeEnvironment renv;
|
2014-07-31 21:09:32 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
std::string reasonForTerminationStr;
|
2013-08-30 19:02:12 +00:00
|
|
|
volatile Node::ReasonForTermination reasonForTermination;
|
2014-08-16 03:37:35 +00:00
|
|
|
|
2013-07-06 18:58:34 +00:00
|
|
|
volatile bool started;
|
|
|
|
volatile bool running;
|
2014-01-30 01:24:55 +00:00
|
|
|
volatile bool resynchronize;
|
2014-09-03 18:56:36 +00:00
|
|
|
volatile bool disableRootTopologyUpdates;
|
2014-10-03 18:59:50 +00:00
|
|
|
std::string overrideRootTopology;
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2014-07-31 21:09:32 +00:00
|
|
|
// This function performs final node tear-down
|
2013-08-30 19:02:12 +00:00
|
|
|
inline Node::ReasonForTermination terminate()
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2014-09-24 20:53:03 +00:00
|
|
|
RuntimeEnvironment *RR = &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-30 19:02:12 +00:00
|
|
|
running = false;
|
|
|
|
|
2013-08-27 22:00:07 +00:00
|
|
|
#ifndef __WINDOWS__
|
|
|
|
delete renv.netconfService;
|
|
|
|
#endif
|
2014-07-31 21:09:32 +00:00
|
|
|
delete renv.updater; renv.updater = (SoftwareUpdater *)0;
|
|
|
|
delete renv.nc; renv.nc = (NodeConfig *)0; // shut down all networks, close taps, etc.
|
|
|
|
delete renv.topology; renv.topology = (Topology *)0; // now we no longer need routing info
|
2014-09-30 23:28:25 +00:00
|
|
|
delete renv.mc; renv.mc = (Multicaster *)0;
|
2014-07-31 21:09:32 +00:00
|
|
|
delete renv.antiRec; renv.antiRec = (AntiRecursion *)0;
|
2014-10-29 20:57:37 +00:00
|
|
|
delete renv.sw; renv.sw = (Switch *)0; // order matters less from here down
|
2014-08-16 16:08:52 +00:00
|
|
|
delete renv.http; renv.http = (HttpClient *)0;
|
2014-07-31 21:09:32 +00:00
|
|
|
delete renv.prng; renv.prng = (CMWC4096 *)0;
|
|
|
|
delete renv.log; renv.log = (Logger *)0; // but stop logging last of all
|
2013-08-27 22:00:07 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-07-31 21:09:32 +00:00
|
|
|
#ifndef __WINDOWS__ // "services" are not supported on 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
|
2014-09-24 20:53:03 +00:00
|
|
|
const RuntimeEnvironment *RR = (const RuntimeEnvironment *)renv;
|
2013-08-02 21:17:34 +00:00
|
|
|
|
|
|
|
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");
|
2013-10-25 17:04:42 +00:00
|
|
|
if (type == "ready") {
|
|
|
|
LOG("received 'ready' from netconf.service, sending netconf-init with identity information...");
|
|
|
|
Dictionary initMessage;
|
|
|
|
initMessage["type"] = "netconf-init";
|
2014-09-24 20:53:03 +00:00
|
|
|
initMessage["netconfId"] = RR->identity.toString(true);
|
|
|
|
RR->netconfService->send(initMessage);
|
2013-10-25 17:04:42 +00:00
|
|
|
} else if (type == "netconf-response") {
|
2013-08-02 21:17:34 +00:00
|
|
|
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")
|
2014-01-18 18:23:44 +00:00
|
|
|
errCode = Packet::ERROR_NETWORK_ACCESS_DENIED_;
|
2013-08-02 21:17:34 +00:00
|
|
|
|
2014-09-24 20:53:03 +00:00
|
|
|
Packet outp(peerAddress,RR->identity.address(),Packet::VERB_ERROR);
|
2013-08-02 21:17:34 +00:00
|
|
|
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);
|
2014-09-24 20:53:03 +00:00
|
|
|
RR->sw->send(outp,true);
|
2013-08-02 21:17:34 +00:00
|
|
|
} else if (msg.contains("netconf")) {
|
|
|
|
const std::string &netconf = msg.get("netconf");
|
|
|
|
if (netconf.length() < 2048) { // sanity check
|
2014-09-24 20:53:03 +00:00
|
|
|
Packet outp(peerAddress,RR->identity.address(),Packet::VERB_OK);
|
2013-08-02 21:17:34 +00:00
|
|
|
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();
|
2014-09-24 20:53:03 +00:00
|
|
|
RR->sw->send(outp,true);
|
2013-08-02 21:17:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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) {
|
2014-09-24 20:53:03 +00:00
|
|
|
Packet outp(ztaddr,RR->identity.address(),Packet::VERB_NETWORK_CONFIG_REFRESH);
|
2013-10-17 09:37:01 +00:00
|
|
|
|
|
|
|
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) {
|
2014-09-24 20:53:03 +00:00
|
|
|
RR->sw->send(outp,true);
|
|
|
|
outp.reset(ztaddr,RR->identity.address(),Packet::VERB_NETWORK_CONFIG_REFRESH);
|
2013-10-17 09:37:01 +00:00
|
|
|
}
|
|
|
|
outp.append(nwid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (outp.payloadLength())
|
2014-09-24 20:53:03 +00:00
|
|
|
RR->sw->send(outp,true);
|
2013-10-17 09:37:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2014-07-31 21:09:32 +00:00
|
|
|
Node::Node(
|
|
|
|
const char *hp,
|
|
|
|
EthernetTapFactory *tf,
|
|
|
|
RoutingTable *rt,
|
2014-10-21 22:18:50 +00:00
|
|
|
SocketManager *sm,
|
2014-10-03 18:59:50 +00:00
|
|
|
bool resetIdentity,
|
|
|
|
const char *overrideRootTopology) throw() :
|
2013-07-04 20:56:19 +00:00
|
|
|
_impl(new _NodeImpl)
|
|
|
|
{
|
|
|
|
_NodeImpl *impl = (_NodeImpl *)_impl;
|
2014-03-19 20:56:48 +00:00
|
|
|
|
|
|
|
if ((hp)&&(hp[0]))
|
2013-08-24 21:10:34 +00:00
|
|
|
impl->renv.homePath = hp;
|
|
|
|
else impl->renv.homePath = ZT_DEFAULTS.defaultHomePath;
|
2014-03-19 20:56:48 +00:00
|
|
|
|
2014-07-31 21:09:32 +00:00
|
|
|
impl->renv.tapFactory = tf;
|
|
|
|
impl->renv.routingTable = rt;
|
2014-10-21 22:18:50 +00:00
|
|
|
impl->renv.sm = sm;
|
2014-07-31 21:09:32 +00:00
|
|
|
|
2014-03-19 20:56:48 +00:00
|
|
|
if (resetIdentity) {
|
|
|
|
// Forget identity and peer database, peer keys, etc.
|
|
|
|
Utils::rm((impl->renv.homePath + ZT_PATH_SEPARATOR_S + "identity.public").c_str());
|
|
|
|
Utils::rm((impl->renv.homePath + ZT_PATH_SEPARATOR_S + "identity.secret").c_str());
|
|
|
|
Utils::rm((impl->renv.homePath + ZT_PATH_SEPARATOR_S + "peers.persist").c_str());
|
|
|
|
|
|
|
|
// Truncate network config information in networks.d but leave the files since we
|
2014-07-31 21:09:32 +00:00
|
|
|
// still want to remember any networks we have joined. This will force those networks
|
|
|
|
// to be reconfigured with our newly regenerated identity after startup.
|
2014-03-19 20:56:48 +00:00
|
|
|
std::string networksDotD(impl->renv.homePath + ZT_PATH_SEPARATOR_S + "networks.d");
|
|
|
|
std::map< std::string,bool > nwfiles(Utils::listDirectory(networksDotD.c_str()));
|
|
|
|
for(std::map<std::string,bool>::iterator nwf(nwfiles.begin());nwf!=nwfiles.end();++nwf) {
|
2014-07-31 21:09:32 +00:00
|
|
|
FILE *trun = fopen((networksDotD + ZT_PATH_SEPARATOR_S + nwf->first).c_str(),"w");
|
|
|
|
if (trun)
|
|
|
|
fclose(trun);
|
2014-03-19 20:56:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
impl->reasonForTermination = Node::NODE_RUNNING;
|
|
|
|
impl->started = false;
|
|
|
|
impl->running = false;
|
2014-01-30 01:24:55 +00:00
|
|
|
impl->resynchronize = false;
|
2014-10-03 18:59:50 +00:00
|
|
|
|
|
|
|
if (overrideRootTopology) {
|
|
|
|
impl->disableRootTopologyUpdates = true;
|
|
|
|
impl->overrideRootTopology = overrideRootTopology;
|
|
|
|
} else {
|
|
|
|
impl->disableRootTopologyUpdates = false;
|
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Node::~Node()
|
|
|
|
{
|
2013-08-27 22:00:07 +00:00
|
|
|
delete (_NodeImpl *)_impl;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2014-03-19 20:56:48 +00:00
|
|
|
static void _CBztTraffic(const SharedPtr<Socket> &fromSock,void *arg,const InetAddress &from,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> &data)
|
|
|
|
{
|
2014-10-29 20:57:37 +00:00
|
|
|
((const RuntimeEnvironment *)arg)->sw->onRemotePacket(fromSock,from,data);
|
2014-03-19 20:56:48 +00:00
|
|
|
}
|
|
|
|
|
2014-08-16 16:08:52 +00:00
|
|
|
static void _cbHandleGetRootTopology(void *arg,int code,const std::string &url,const std::string &body)
|
2014-08-16 03:37:35 +00:00
|
|
|
{
|
2014-09-24 20:53:03 +00:00
|
|
|
RuntimeEnvironment *RR = (RuntimeEnvironment *)arg;
|
2014-08-16 03:37:35 +00:00
|
|
|
|
|
|
|
if ((code != 200)||(body.length() == 0)) {
|
2014-08-19 21:11:02 +00:00
|
|
|
TRACE("failed to retrieve %s",url.c_str());
|
2014-08-16 03:37:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
Dictionary rt(body);
|
|
|
|
if (!Topology::authenticateRootTopology(rt)) {
|
|
|
|
LOG("discarded invalid root topology update from %s (signature check failed)",url.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2014-09-24 20:53:03 +00:00
|
|
|
std::string rootTopologyPath(RR->homePath + ZT_PATH_SEPARATOR_S + "root-topology");
|
2014-08-16 03:37:35 +00:00
|
|
|
std::string rootTopology;
|
|
|
|
if (Utils::readFile(rootTopologyPath.c_str(),rootTopology)) {
|
|
|
|
Dictionary alreadyHave(rootTopology);
|
|
|
|
if (alreadyHave == rt) {
|
2014-08-19 21:11:02 +00:00
|
|
|
TRACE("retrieved root topology from %s but no change (same as on disk)",url.c_str());
|
2014-08-16 03:37:35 +00:00
|
|
|
return;
|
|
|
|
} else if (alreadyHave.signatureTimestamp() > rt.signatureTimestamp()) {
|
|
|
|
TRACE("retrieved root topology from %s but no change (ours is newer)",url.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Utils::writeFile(rootTopologyPath.c_str(),body);
|
|
|
|
}
|
|
|
|
|
2014-09-24 20:53:03 +00:00
|
|
|
RR->topology->setSupernodes(Dictionary(rt.get("supernodes")));
|
2014-08-16 03:37:35 +00:00
|
|
|
} catch ( ... ) {
|
|
|
|
LOG("discarded invalid root topology update from %s (format invalid)",url.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
Node::ReasonForTermination Node::run()
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
_NodeImpl *impl = (_NodeImpl *)_impl;
|
2014-09-24 20:53:03 +00:00
|
|
|
RuntimeEnvironment *RR = (RuntimeEnvironment *)&(impl->renv);
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
impl->started = true;
|
|
|
|
impl->running = true;
|
|
|
|
|
|
|
|
try {
|
|
|
|
#ifdef ZT_LOG_STDOUT
|
2014-09-24 20:53:03 +00:00
|
|
|
RR->log = new Logger((const char *)0,(const char *)0,0);
|
2013-07-04 20:56:19 +00:00
|
|
|
#else
|
2014-09-24 20:53:03 +00:00
|
|
|
RR->log = new Logger((RR->homePath + ZT_PATH_SEPARATOR_S + "node.log").c_str(),(const char *)0,131072);
|
2013-07-04 20:56:19 +00:00
|
|
|
#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
|
2014-09-24 20:53:03 +00:00
|
|
|
RR->prng = new CMWC4096();
|
2013-07-13 18:28:26 +00:00
|
|
|
|
2014-09-03 18:56:36 +00:00
|
|
|
// Read identity public and secret, generating if not present
|
2014-09-15 23:32:55 +00:00
|
|
|
{
|
|
|
|
bool gotId = false;
|
2014-09-24 20:53:03 +00:00
|
|
|
std::string identitySecretPath(RR->homePath + ZT_PATH_SEPARATOR_S + "identity.secret");
|
|
|
|
std::string identityPublicPath(RR->homePath + ZT_PATH_SEPARATOR_S + "identity.public");
|
2014-09-15 23:32:55 +00:00
|
|
|
std::string idser;
|
|
|
|
if (Utils::readFile(identitySecretPath.c_str(),idser))
|
2014-09-24 20:53:03 +00:00
|
|
|
gotId = RR->identity.fromString(idser);
|
|
|
|
if ((gotId)&&(!RR->identity.locallyValidate()))
|
2014-09-15 23:32:55 +00:00
|
|
|
gotId = false;
|
|
|
|
if (gotId) {
|
|
|
|
// Make sure identity.public matches identity.secret
|
|
|
|
idser = std::string();
|
|
|
|
Utils::readFile(identityPublicPath.c_str(),idser);
|
2014-09-24 20:53:03 +00:00
|
|
|
std::string pubid(RR->identity.toString(false));
|
2014-09-15 23:32:55 +00:00
|
|
|
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 {
|
|
|
|
LOG("no identity found or identity invalid, generating one... this might take a few seconds...");
|
2014-09-24 20:53:03 +00:00
|
|
|
RR->identity.generate();
|
|
|
|
LOG("generated new identity: %s",RR->identity.address().toString().c_str());
|
|
|
|
idser = RR->identity.toString(true);
|
2014-09-15 23:32:55 +00:00
|
|
|
if (!Utils::writeFile(identitySecretPath.c_str(),idser))
|
|
|
|
return impl->terminateBecause(Node::NODE_UNRECOVERABLE_ERROR,"could not write identity.secret (home path not writable?)");
|
2014-09-24 20:53:03 +00:00
|
|
|
idser = RR->identity.toString(false);
|
2014-09-15 23:32:55 +00:00
|
|
|
if (!Utils::writeFile(identityPublicPath.c_str(),idser))
|
2013-07-04 20:56:19 +00:00
|
|
|
return impl->terminateBecause(Node::NODE_UNRECOVERABLE_ERROR,"could not write identity.public (home path not writable?)");
|
|
|
|
}
|
2014-09-15 23:32:55 +00:00
|
|
|
Utils::lockDownFile(identitySecretPath.c_str(),false);
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2014-10-13 21:12:51 +00:00
|
|
|
// Make sure networks.d exists (used by NodeConfig to remember networks)
|
2014-01-26 18:21:43 +00:00
|
|
|
{
|
2014-09-24 20:53:03 +00:00
|
|
|
std::string networksDotD(RR->homePath + ZT_PATH_SEPARATOR_S + "networks.d");
|
2013-08-12 20:57:34 +00:00
|
|
|
#ifdef __WINDOWS__
|
2014-01-26 18:21:43 +00:00
|
|
|
CreateDirectoryA(networksDotD.c_str(),NULL);
|
2013-08-12 20:57:34 +00:00
|
|
|
#else
|
2014-01-26 18:21:43 +00:00
|
|
|
mkdir(networksDotD.c_str(),0700);
|
2013-08-12 20:57:34 +00:00
|
|
|
#endif
|
2014-01-26 18:21:43 +00:00
|
|
|
}
|
2014-10-13 21:12:51 +00:00
|
|
|
// Make sure iddb.d exists (used by Topology to remember identities)
|
|
|
|
{
|
|
|
|
std::string iddbDotD(RR->homePath + ZT_PATH_SEPARATOR_S + "iddb.d");
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
CreateDirectoryA(iddbDotD.c_str(),NULL);
|
|
|
|
#else
|
|
|
|
mkdir(iddbDotD.c_str(),0700);
|
|
|
|
#endif
|
|
|
|
}
|
2013-08-06 04:05:39 +00:00
|
|
|
|
2014-09-24 20:53:03 +00:00
|
|
|
RR->http = new HttpClient();
|
2014-10-01 19:41:48 +00:00
|
|
|
RR->sw = new Switch(RR);
|
2014-10-29 20:57:37 +00:00
|
|
|
RR->mc = new Multicaster(RR);
|
|
|
|
RR->antiRec = new AntiRecursion();
|
2014-10-13 21:12:51 +00:00
|
|
|
RR->topology = new Topology(RR);
|
2013-07-17 18:39:34 +00:00
|
|
|
try {
|
2014-10-01 19:41:48 +00:00
|
|
|
RR->nc = new NodeConfig(RR);
|
2013-08-26 21:22:20 +00:00
|
|
|
} catch (std::exception &exc) {
|
2014-03-19 20:56:48 +00:00
|
|
|
return impl->terminateBecause(Node::NODE_UNRECOVERABLE_ERROR,"unable to initialize IPC socket: is ZeroTier One already running?");
|
2013-07-17 18:39:34 +00:00
|
|
|
}
|
2014-09-24 20:53:03 +00:00
|
|
|
RR->node = this;
|
2014-09-12 23:57:37 +00:00
|
|
|
|
2013-12-11 21:00:18 +00:00
|
|
|
#ifdef ZT_AUTO_UPDATE
|
2014-01-07 23:06:02 +00:00
|
|
|
if (ZT_DEFAULTS.updateLatestNfoURL.length()) {
|
2014-10-01 19:41:48 +00:00
|
|
|
RR->updater = new SoftwareUpdater(RR);
|
2014-09-24 20:53:03 +00:00
|
|
|
RR->updater->cleanOldUpdates(); // clean out updates.d on startup
|
2014-01-07 23:06:02 +00:00
|
|
|
} else {
|
2013-12-27 04:57:17 +00:00
|
|
|
LOG("WARNING: unable to enable software updates: latest .nfo URL from ZT_DEFAULTS is empty (does this platform actually support software updates?)");
|
|
|
|
}
|
2013-12-11 21:00:18 +00:00
|
|
|
#endif
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2014-09-03 18:56:36 +00:00
|
|
|
// Initialize root topology from defaults or root-toplogy file in home path on disk
|
2014-10-03 18:59:50 +00:00
|
|
|
if (impl->overrideRootTopology.length() == 0) {
|
2014-09-24 20:53:03 +00:00
|
|
|
std::string rootTopologyPath(RR->homePath + ZT_PATH_SEPARATOR_S + "root-topology");
|
2014-09-15 23:32:55 +00:00
|
|
|
std::string rootTopology;
|
|
|
|
if (!Utils::readFile(rootTopologyPath.c_str(),rootTopology))
|
|
|
|
rootTopology = ZT_DEFAULTS.defaultRootTopology;
|
|
|
|
try {
|
|
|
|
Dictionary rt(rootTopology);
|
|
|
|
|
|
|
|
if (Topology::authenticateRootTopology(rt)) {
|
|
|
|
// Set supernodes if root topology signature is valid
|
2014-09-24 20:53:03 +00:00
|
|
|
RR->topology->setSupernodes(Dictionary(rt.get("supernodes",""))); // set supernodes from root-topology
|
2014-09-15 23:32:55 +00:00
|
|
|
|
|
|
|
// If root-topology contains noupdate=1, disable further updates and only use what was on disk
|
|
|
|
impl->disableRootTopologyUpdates = (Utils::strToInt(rt.get("noupdate","0").c_str()) > 0);
|
|
|
|
} else {
|
|
|
|
// Revert to built-in defaults if root topology fails signature check
|
|
|
|
LOG("%s failed signature check, using built-in defaults instead",rootTopologyPath.c_str());
|
|
|
|
Utils::rm(rootTopologyPath.c_str());
|
2014-09-24 20:53:03 +00:00
|
|
|
RR->topology->setSupernodes(Dictionary(Dictionary(ZT_DEFAULTS.defaultRootTopology).get("supernodes","")));
|
2014-09-15 23:32:55 +00:00
|
|
|
impl->disableRootTopologyUpdates = false;
|
|
|
|
}
|
|
|
|
} catch ( ... ) {
|
|
|
|
return impl->terminateBecause(Node::NODE_UNRECOVERABLE_ERROR,"invalid root-topology format");
|
2014-08-16 21:01:25 +00:00
|
|
|
}
|
2014-10-03 18:59:50 +00:00
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
Dictionary rt(impl->overrideRootTopology);
|
|
|
|
RR->topology->setSupernodes(Dictionary(rt.get("supernodes","")));
|
|
|
|
impl->disableRootTopologyUpdates = true;
|
|
|
|
} catch ( ... ) {
|
|
|
|
return impl->terminateBecause(Node::NODE_UNRECOVERABLE_ERROR,"invalid root-topology format");
|
|
|
|
}
|
2014-08-14 23:52:22 +00:00
|
|
|
}
|
2014-10-13 21:12:51 +00:00
|
|
|
|
|
|
|
// Delete peers.persist if it exists -- legacy file, just takes up space
|
|
|
|
Utils::rm(std::string(RR->homePath + ZT_PATH_SEPARATOR_S + "peers.persist").c_str());
|
2013-07-04 20:56:19 +00:00
|
|
|
} 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 {
|
2014-09-24 20:53:03 +00:00
|
|
|
std::string netconfServicePath(RR->homePath + ZT_PATH_SEPARATOR_S + "services.d" + ZT_PATH_SEPARATOR_S + "netconf.service");
|
2013-08-01 21:32:37 +00:00
|
|
|
if (Utils::fileExists(netconfServicePath.c_str())) {
|
2013-10-25 17:04:42 +00:00
|
|
|
LOG("netconf.d/netconf.service appears to exist, starting...");
|
2014-10-01 19:41:48 +00:00
|
|
|
RR->netconfService = new Service(RR,"netconf",netconfServicePath.c_str(),&_netconfServiceMessageHandler,RR);
|
2013-10-24 20:19:53 +00:00
|
|
|
Dictionary initMessage;
|
|
|
|
initMessage["type"] = "netconf-init";
|
2014-09-24 20:53:03 +00:00
|
|
|
initMessage["netconfId"] = RR->identity.toString(true);
|
|
|
|
RR->netconfService->send(initMessage);
|
2013-08-01 21:32:37 +00:00
|
|
|
}
|
|
|
|
} 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 {
|
2014-01-07 23:06:02 +00:00
|
|
|
/* Shut down if this file exists but fails to open. This is used on Mac to
|
|
|
|
* shut down automatically on .app deletion by symlinking this to the
|
|
|
|
* Info.plist file inside the ZeroTier One application. This causes the
|
|
|
|
* service to die when the user throws away the app, allowing uninstallation
|
|
|
|
* in the natural Mac way. */
|
2014-09-24 20:53:03 +00:00
|
|
|
std::string shutdownIfUnreadablePath(RR->homePath + ZT_PATH_SEPARATOR_S + "shutdownIfUnreadable");
|
2014-01-07 23:06:02 +00:00
|
|
|
|
2014-02-15 00:23:03 +00:00
|
|
|
uint64_t lastNetworkAutoconfCheck = Utils::now() - 5000ULL; // 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 lastMulticastCheck = 0;
|
2014-04-03 21:36:52 +00:00
|
|
|
uint64_t lastSupernodePingCheck = 0;
|
2014-04-10 23:30:15 +00:00
|
|
|
uint64_t lastBeacon = 0;
|
2014-08-16 03:37:35 +00:00
|
|
|
uint64_t lastRootTopologyFetch = 0;
|
2014-09-17 21:19:58 +00:00
|
|
|
uint64_t lastShutdownIfUnreadableCheck = 0;
|
2014-03-28 00:02:52 +00:00
|
|
|
long lastDelayDelta = 0;
|
2014-01-29 22:09:12 +00:00
|
|
|
|
2014-04-01 21:12:54 +00:00
|
|
|
uint64_t networkConfigurationFingerprint = 0;
|
2014-09-24 20:53:03 +00:00
|
|
|
RR->timeOfLastResynchronize = Utils::now();
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2014-09-15 23:32:55 +00:00
|
|
|
// We are up and running
|
2014-09-24 20:53:03 +00:00
|
|
|
RR->initialized = true;
|
2014-09-15 23:32:55 +00:00
|
|
|
|
2013-08-30 19:02:12 +00:00
|
|
|
while (impl->reasonForTermination == NODE_RUNNING) {
|
2014-09-17 21:19:58 +00:00
|
|
|
uint64_t now = Utils::now();
|
|
|
|
bool resynchronize = false;
|
|
|
|
|
2014-04-09 18:55:24 +00:00
|
|
|
/* This is how the service automatically shuts down when the OSX .app is
|
|
|
|
* thrown in the trash. It's not used on any other platform for now but
|
|
|
|
* could do similar things. It's disabled on Windows since it doesn't really
|
|
|
|
* work there. */
|
|
|
|
#ifdef __UNIX_LIKE__
|
2014-09-17 21:19:58 +00:00
|
|
|
if ((now - lastShutdownIfUnreadableCheck) > 10000) {
|
|
|
|
lastShutdownIfUnreadableCheck = now;
|
|
|
|
if (Utils::fileExists(shutdownIfUnreadablePath.c_str(),false)) {
|
|
|
|
int tmpfd = ::open(shutdownIfUnreadablePath.c_str(),O_RDONLY,0);
|
2014-09-18 17:01:30 +00:00
|
|
|
if (tmpfd < 0) {
|
2014-09-17 21:19:58 +00:00
|
|
|
return impl->terminateBecause(Node::NODE_NORMAL_TERMINATION,"shutdownIfUnreadable exists but is not readable");
|
2014-09-18 17:01:30 +00:00
|
|
|
} else ::close(tmpfd);
|
2014-09-17 21:19:58 +00:00
|
|
|
}
|
2013-11-21 21:34:27 +00:00
|
|
|
}
|
2014-04-09 18:55:24 +00:00
|
|
|
#endif
|
2013-11-21 21:34:27 +00:00
|
|
|
|
2014-01-29 00:12:24 +00:00
|
|
|
// If it looks like the computer slept and woke, resynchronize.
|
2013-07-04 20:56:19 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-01-30 22:23:52 +00:00
|
|
|
// If our network environment looks like it changed, resynchronize.
|
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;
|
2014-09-24 20:53:03 +00:00
|
|
|
uint64_t fp = RR->routingTable->networkEnvironmentFingerprint(RR->nc->networkTapDeviceNames());
|
2013-07-04 20:56:19 +00:00
|
|
|
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;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-02 01:39:10 +00:00
|
|
|
// Supernodes do not resynchronize unless explicitly ordered via SIGHUP.
|
2014-09-24 20:53:03 +00:00
|
|
|
if ((resynchronize)&&(RR->topology->amSupernode()))
|
2014-04-02 01:39:10 +00:00
|
|
|
resynchronize = false;
|
|
|
|
|
|
|
|
// Check for SIGHUP / force resync.
|
|
|
|
if (impl->resynchronize) {
|
|
|
|
impl->resynchronize = false;
|
|
|
|
resynchronize = true;
|
|
|
|
LOG("resynchronize forced by user, syncing with network");
|
|
|
|
}
|
|
|
|
|
2014-04-10 00:08:35 +00:00
|
|
|
if (resynchronize) {
|
2014-09-24 20:53:03 +00:00
|
|
|
RR->tcpTunnelingEnabled = false; // turn off TCP tunneling master switch at first, will be reenabled on persistent UDP failure
|
|
|
|
RR->timeOfLastResynchronize = now;
|
2014-04-10 00:08:35 +00:00
|
|
|
}
|
2014-03-28 00:02:52 +00:00
|
|
|
|
2014-04-10 00:08:35 +00:00
|
|
|
/* Supernodes are pinged separately and more aggressively. The
|
|
|
|
* ZT_STARTUP_AGGRO parameter sets a limit on how rapidly they are
|
|
|
|
* tried, while PingSupernodesThatNeedPing contains the logic for
|
|
|
|
* determining if they need PING. */
|
2014-04-03 21:36:52 +00:00
|
|
|
if ((now - lastSupernodePingCheck) >= ZT_STARTUP_AGGRO) {
|
|
|
|
lastSupernodePingCheck = now;
|
2014-04-10 00:08:35 +00:00
|
|
|
|
|
|
|
uint64_t lastReceiveFromAnySupernode = 0; // function object result paramter
|
2014-09-24 20:53:03 +00:00
|
|
|
RR->topology->eachSupernodePeer(Topology::FindMostRecentDirectReceiveTimestamp(lastReceiveFromAnySupernode));
|
2014-04-10 00:08:35 +00:00
|
|
|
|
|
|
|
// Turn on TCP tunneling master switch if we haven't heard anything since before
|
|
|
|
// the last resynchronize and we've been trying long enough.
|
2014-09-24 20:53:03 +00:00
|
|
|
uint64_t tlr = RR->timeOfLastResynchronize;
|
2014-04-10 00:08:35 +00:00
|
|
|
if ((lastReceiveFromAnySupernode < tlr)&&((now - tlr) >= ZT_TCP_TUNNEL_FAILOVER_TIMEOUT)) {
|
|
|
|
TRACE("network still unreachable after %u ms, TCP TUNNELING ENABLED",(unsigned int)ZT_TCP_TUNNEL_FAILOVER_TIMEOUT);
|
2014-09-24 20:53:03 +00:00
|
|
|
RR->tcpTunnelingEnabled = true;
|
2014-04-10 00:08:35 +00:00
|
|
|
}
|
|
|
|
|
2014-09-24 20:53:03 +00:00
|
|
|
RR->topology->eachSupernodePeer(Topology::PingSupernodesThatNeedPing(RR,now));
|
2014-01-29 22:09:12 +00:00
|
|
|
}
|
|
|
|
|
2014-01-29 00:12:24 +00:00
|
|
|
if (resynchronize) {
|
2014-04-03 23:19:41 +00:00
|
|
|
/* Send NOP to all peers on resynchronize, directly to supernodes and
|
|
|
|
* indirectly to regular nodes (to trigger RENDEZVOUS). Also clear
|
|
|
|
* learned paths since they're likely no longer valid, and close
|
|
|
|
* TCP sockets since they're also likely invalid. */
|
2014-09-24 20:53:03 +00:00
|
|
|
RR->sm->closeTcpSockets();
|
|
|
|
RR->topology->eachPeer(Topology::ResetActivePeers(RR,now));
|
2014-01-29 00:12:24 +00:00
|
|
|
} else {
|
2014-03-28 00:02:52 +00:00
|
|
|
/* Periodically check for changes in our local multicast subscriptions
|
|
|
|
* and broadcast those changes to directly connected peers. */
|
2014-01-29 00:12:24 +00:00
|
|
|
if ((now - lastMulticastCheck) >= ZT_MULTICAST_LOCAL_POLL_PERIOD) {
|
|
|
|
lastMulticastCheck = now;
|
|
|
|
try {
|
2014-09-24 20:53:03 +00:00
|
|
|
std::vector< SharedPtr<Network> > networks(RR->nc->networks());
|
2014-09-30 15:38:03 +00:00
|
|
|
for(std::vector< SharedPtr<Network> >::const_iterator nw(networks.begin());nw!=networks.end();++nw)
|
2014-10-04 01:27:42 +00:00
|
|
|
(*nw)->rescanMulticastGroups();
|
2014-01-29 00:12:24 +00:00
|
|
|
} catch (std::exception &exc) {
|
|
|
|
LOG("unexpected exception announcing multicast groups: %s",exc.what());
|
|
|
|
} catch ( ... ) {
|
|
|
|
LOG("unexpected exception announcing multicast groups: (unknown)");
|
|
|
|
}
|
2013-08-05 16:34:54 +00:00
|
|
|
}
|
|
|
|
|
2014-03-28 00:02:52 +00:00
|
|
|
/* Periodically ping all our non-stale direct peers unless we're a supernode.
|
|
|
|
* Supernodes only ping each other (which is done above). */
|
2014-09-24 20:53:03 +00:00
|
|
|
if ((!RR->topology->amSupernode())&&((now - lastPingCheck) >= ZT_PING_CHECK_DELAY)) {
|
2014-07-31 21:09:32 +00:00
|
|
|
lastPingCheck = now;
|
|
|
|
try {
|
2014-09-24 20:53:03 +00:00
|
|
|
RR->topology->eachPeer(Topology::PingPeersThatNeedPing(RR,now));
|
2014-07-31 21:09:32 +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-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-28 00:02:52 +00:00
|
|
|
// Update network configurations when needed.
|
2014-01-29 00:12:24 +00:00
|
|
|
if ((resynchronize)||((now - lastNetworkAutoconfCheck) >= ZT_NETWORK_AUTOCONF_CHECK_DELAY)) {
|
|
|
|
lastNetworkAutoconfCheck = now;
|
2014-09-24 20:53:03 +00:00
|
|
|
std::vector< SharedPtr<Network> > nets(RR->nc->networks());
|
2014-01-29 00:12:24 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-28 00:02:52 +00:00
|
|
|
// Do periodic tasks in submodules.
|
2013-07-30 15:14:53 +00:00
|
|
|
if ((now - lastClean) >= ZT_DB_CLEAN_PERIOD) {
|
|
|
|
lastClean = now;
|
2014-10-01 21:05:25 +00:00
|
|
|
RR->topology->clean(now);
|
|
|
|
RR->mc->clean(now);
|
2014-09-24 20:53:03 +00:00
|
|
|
RR->nc->clean();
|
|
|
|
if (RR->updater)
|
|
|
|
RR->updater->checkIfMaxIntervalExceeded(now);
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2014-04-10 23:30:15 +00:00
|
|
|
// Send beacons to physical local LANs
|
|
|
|
if ((resynchronize)||((now - lastBeacon) >= ZT_BEACON_INTERVAL)) {
|
|
|
|
lastBeacon = now;
|
|
|
|
char bcn[ZT_PROTO_BEACON_LENGTH];
|
2014-06-17 19:54:35 +00:00
|
|
|
void *bcnptr = bcn;
|
2014-09-24 20:53:03 +00:00
|
|
|
*((uint32_t *)(bcnptr)) = RR->prng->next32();
|
2014-06-17 19:54:35 +00:00
|
|
|
bcnptr = bcn + 4;
|
2014-09-24 20:53:03 +00:00
|
|
|
*((uint32_t *)(bcnptr)) = RR->prng->next32();
|
|
|
|
RR->identity.address().copyTo(bcn + ZT_PROTO_BEACON_IDX_ADDRESS,ZT_ADDRESS_LENGTH);
|
2014-04-10 23:30:15 +00:00
|
|
|
TRACE("sending LAN beacon to %s",ZT_DEFAULTS.v4Broadcast.toString().c_str());
|
2014-09-24 20:53:03 +00:00
|
|
|
RR->antiRec->logOutgoingZT(bcn,ZT_PROTO_BEACON_LENGTH);
|
|
|
|
RR->sm->send(ZT_DEFAULTS.v4Broadcast,false,false,bcn,ZT_PROTO_BEACON_LENGTH);
|
2014-04-10 23:30:15 +00:00
|
|
|
}
|
|
|
|
|
2014-09-03 18:56:36 +00:00
|
|
|
// Check for updates to root topology (supernodes) periodically
|
2014-08-16 03:37:35 +00:00
|
|
|
if ((now - lastRootTopologyFetch) >= ZT_UPDATE_ROOT_TOPOLOGY_CHECK_INTERVAL) {
|
|
|
|
lastRootTopologyFetch = now;
|
2014-09-03 18:56:36 +00:00
|
|
|
if (!impl->disableRootTopologyUpdates) {
|
|
|
|
TRACE("fetching root topology from %s",ZT_DEFAULTS.rootTopologyUpdateURL.c_str());
|
2014-10-01 19:41:48 +00:00
|
|
|
RR->http->GET(ZT_DEFAULTS.rootTopologyUpdateURL,HttpClient::NO_HEADERS,60,&_cbHandleGetRootTopology,RR);
|
2014-09-03 18:56:36 +00:00
|
|
|
}
|
2014-08-16 03:37:35 +00:00
|
|
|
}
|
|
|
|
|
2014-01-29 00:12:24 +00:00
|
|
|
// Sleep for loop interval or until something interesting happens.
|
2013-07-04 20:56:19 +00:00
|
|
|
try {
|
2014-09-24 20:53:03 +00:00
|
|
|
unsigned long delay = std::min((unsigned long)ZT_MAX_SERVICE_LOOP_INTERVAL,RR->sw->doTimerTasks());
|
2013-07-04 20:56:19 +00:00
|
|
|
uint64_t start = Utils::now();
|
2014-10-21 22:18:50 +00:00
|
|
|
RR->sm->poll(delay,&_CBztTraffic,RR);
|
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 ( ... ) {
|
2014-01-30 06:04:23 +00:00
|
|
|
LOG("FATAL: unexpected exception in core loop: unknown exception");
|
2013-07-04 20:56:19 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-09-12 23:57:37 +00:00
|
|
|
const char *Node::terminationMessage() const
|
2013-07-04 20:56:19 +00:00
|
|
|
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 : "");
|
2014-03-19 20:56:48 +00:00
|
|
|
((_NodeImpl *)_impl)->renv.sm->whack();
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2014-01-30 01:24:55 +00:00
|
|
|
void Node::resync()
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
((_NodeImpl *)_impl)->resynchronize = true;
|
2014-03-19 20:56:48 +00:00
|
|
|
((_NodeImpl *)_impl)->renv.sm->whack();
|
2014-01-30 01:24:55 +00:00
|
|
|
}
|
|
|
|
|
2014-09-12 23:57:37 +00:00
|
|
|
bool Node::online()
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
_NodeImpl *impl = (_NodeImpl *)_impl;
|
2014-09-24 20:53:03 +00:00
|
|
|
RuntimeEnvironment *RR = (RuntimeEnvironment *)&(impl->renv);
|
2014-10-28 21:17:39 +00:00
|
|
|
if ((!RR)||(!RR->initialized))
|
|
|
|
return false;
|
2014-09-12 23:57:37 +00:00
|
|
|
uint64_t now = Utils::now();
|
2014-09-24 20:53:03 +00:00
|
|
|
uint64_t since = RR->timeOfLastResynchronize;
|
|
|
|
std::vector< SharedPtr<Peer> > snp(RR->topology->supernodePeers());
|
2014-09-12 23:57:37 +00:00
|
|
|
for(std::vector< SharedPtr<Peer> >::const_iterator sn(snp.begin());sn!=snp.end();++sn) {
|
|
|
|
uint64_t lastRec = (*sn)->lastDirectReceive();
|
|
|
|
if ((lastRec)&&(lastRec > since)&&((now - lastRec) < ZT_PEER_PATH_ACTIVITY_TIMEOUT))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-15 23:32:55 +00:00
|
|
|
bool Node::started()
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
_NodeImpl *impl = (_NodeImpl *)_impl;
|
|
|
|
return impl->started;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Node::running()
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
_NodeImpl *impl = (_NodeImpl *)_impl;
|
|
|
|
return impl->running;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Node::initialized()
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
_NodeImpl *impl = (_NodeImpl *)_impl;
|
2014-09-24 20:53:03 +00:00
|
|
|
RuntimeEnvironment *RR = (RuntimeEnvironment *)&(impl->renv);
|
2014-10-01 19:41:48 +00:00
|
|
|
return ((RR)&&(RR->initialized));
|
2014-09-15 23:32:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t Node::address()
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
_NodeImpl *impl = (_NodeImpl *)_impl;
|
2014-09-24 20:53:03 +00:00
|
|
|
RuntimeEnvironment *RR = (RuntimeEnvironment *)&(impl->renv);
|
2014-10-01 19:41:48 +00:00
|
|
|
if ((!RR)||(!RR->initialized))
|
2014-09-15 23:32:55 +00:00
|
|
|
return 0;
|
2014-09-24 20:53:03 +00:00
|
|
|
return RR->identity.address().toInt();
|
2014-09-15 23:32:55 +00:00
|
|
|
}
|
|
|
|
|
2014-09-12 23:57:37 +00:00
|
|
|
void Node::join(uint64_t nwid)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
_NodeImpl *impl = (_NodeImpl *)_impl;
|
2014-09-24 20:53:03 +00:00
|
|
|
RuntimeEnvironment *RR = (RuntimeEnvironment *)&(impl->renv);
|
2014-10-28 21:17:39 +00:00
|
|
|
if ((RR)&&(RR->initialized))
|
|
|
|
RR->nc->join(nwid);
|
2014-09-12 23:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Node::leave(uint64_t nwid)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
_NodeImpl *impl = (_NodeImpl *)_impl;
|
2014-09-24 20:53:03 +00:00
|
|
|
RuntimeEnvironment *RR = (RuntimeEnvironment *)&(impl->renv);
|
2014-10-28 21:17:39 +00:00
|
|
|
if ((RR)&&(RR->initialized))
|
|
|
|
RR->nc->leave(nwid);
|
2014-09-12 23:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct GatherPeerStatistics
|
|
|
|
{
|
|
|
|
uint64_t now;
|
|
|
|
ZT1_Node_Status *status;
|
|
|
|
inline void operator()(Topology &t,const SharedPtr<Peer> &p)
|
|
|
|
{
|
|
|
|
++status->knownPeers;
|
|
|
|
if (p->hasActiveDirectPath(now))
|
|
|
|
++status->directlyConnectedPeers;
|
|
|
|
if (p->alive(now))
|
|
|
|
++status->alivePeers;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
void Node::status(ZT1_Node_Status *status)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
_NodeImpl *impl = (_NodeImpl *)_impl;
|
2014-09-24 20:53:03 +00:00
|
|
|
RuntimeEnvironment *RR = (RuntimeEnvironment *)&(impl->renv);
|
2014-09-12 23:57:37 +00:00
|
|
|
|
|
|
|
memset(status,0,sizeof(ZT1_Node_Status));
|
|
|
|
|
2014-10-28 21:17:39 +00:00
|
|
|
if ((!RR)||(!RR->initialized))
|
|
|
|
return;
|
|
|
|
|
2014-09-24 20:53:03 +00:00
|
|
|
Utils::scopy(status->publicIdentity,sizeof(status->publicIdentity),RR->identity.toString(false).c_str());
|
|
|
|
RR->identity.address().toString(status->address,sizeof(status->address));
|
|
|
|
status->rawAddress = RR->identity.address().toInt();
|
2014-09-12 23:57:37 +00:00
|
|
|
|
|
|
|
status->knownPeers = 0;
|
2014-09-24 20:53:03 +00:00
|
|
|
status->supernodes = RR->topology->numSupernodes();
|
2014-09-12 23:57:37 +00:00
|
|
|
status->directlyConnectedPeers = 0;
|
|
|
|
status->alivePeers = 0;
|
|
|
|
GatherPeerStatistics gps;
|
|
|
|
gps.now = Utils::now();
|
|
|
|
gps.status = status;
|
2014-09-24 20:53:03 +00:00
|
|
|
RR->topology->eachPeer<GatherPeerStatistics &>(gps);
|
2014-09-12 23:57:37 +00:00
|
|
|
|
|
|
|
if (status->alivePeers > 0) {
|
|
|
|
double dlsr = (double)status->directlyConnectedPeers / (double)status->alivePeers;
|
|
|
|
if (dlsr > 1.0) dlsr = 1.0;
|
|
|
|
if (dlsr < 0.0) dlsr = 0.0;
|
|
|
|
status->directLinkSuccessRate = (float)dlsr;
|
|
|
|
} else status->directLinkSuccessRate = 1.0f; // no connections to no active peers == 100% success at nothing
|
|
|
|
|
|
|
|
status->online = online();
|
|
|
|
status->running = impl->running;
|
2014-10-28 21:17:39 +00:00
|
|
|
status->initialized = true;
|
2014-09-12 23:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct CollectPeersAndPaths
|
|
|
|
{
|
|
|
|
std::vector< std::pair< SharedPtr<Peer>,std::vector<Path> > > data;
|
2014-09-17 19:54:39 +00:00
|
|
|
inline void operator()(Topology &t,const SharedPtr<Peer> &p) { this->data.push_back(std::pair< SharedPtr<Peer>,std::vector<Path> >(p,p->paths())); }
|
2014-09-12 23:57:37 +00:00
|
|
|
};
|
|
|
|
struct SortPeersAndPathsInAscendingAddressOrder
|
|
|
|
{
|
|
|
|
inline bool operator()(const std::pair< SharedPtr<Peer>,std::vector<Path> > &a,const std::pair< SharedPtr<Peer>,std::vector<Path> > &b) const { return (a.first->address() < b.first->address()); }
|
|
|
|
};
|
|
|
|
ZT1_Node_PeerList *Node::listPeers()
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
_NodeImpl *impl = (_NodeImpl *)_impl;
|
2014-09-24 20:53:03 +00:00
|
|
|
RuntimeEnvironment *RR = (RuntimeEnvironment *)&(impl->renv);
|
2014-09-12 23:57:37 +00:00
|
|
|
|
2014-10-28 21:17:39 +00:00
|
|
|
if ((!RR)||(!RR->initialized))
|
|
|
|
return (ZT1_Node_PeerList *)0;
|
|
|
|
|
2014-09-12 23:57:37 +00:00
|
|
|
CollectPeersAndPaths pp;
|
2014-09-24 20:53:03 +00:00
|
|
|
RR->topology->eachPeer<CollectPeersAndPaths &>(pp);
|
2014-09-12 23:57:37 +00:00
|
|
|
std::sort(pp.data.begin(),pp.data.end(),SortPeersAndPathsInAscendingAddressOrder());
|
|
|
|
|
|
|
|
unsigned int returnBufSize = sizeof(ZT1_Node_PeerList);
|
|
|
|
for(std::vector< std::pair< SharedPtr<Peer>,std::vector<Path> > >::iterator p(pp.data.begin());p!=pp.data.end();++p)
|
2014-10-19 19:56:39 +00:00
|
|
|
returnBufSize += sizeof(ZT1_Node_Peer) + (sizeof(ZT1_Node_PhysicalPath) * (unsigned int)p->second.size());
|
2014-09-12 23:57:37 +00:00
|
|
|
|
|
|
|
char *buf = (char *)::malloc(returnBufSize);
|
|
|
|
if (!buf)
|
|
|
|
return (ZT1_Node_PeerList *)0;
|
|
|
|
memset(buf,0,returnBufSize);
|
|
|
|
|
|
|
|
ZT1_Node_PeerList *pl = (ZT1_Node_PeerList *)buf;
|
|
|
|
buf += sizeof(ZT1_Node_PeerList);
|
|
|
|
|
|
|
|
pl->peers = (ZT1_Node_Peer *)buf;
|
|
|
|
buf += (sizeof(ZT1_Node_Peer) * pp.data.size());
|
|
|
|
pl->numPeers = 0;
|
|
|
|
|
|
|
|
uint64_t now = Utils::now();
|
|
|
|
for(std::vector< std::pair< SharedPtr<Peer>,std::vector<Path> > >::iterator p(pp.data.begin());p!=pp.data.end();++p) {
|
|
|
|
ZT1_Node_Peer *prec = &(pl->peers[pl->numPeers++]);
|
|
|
|
if (p->first->remoteVersionKnown())
|
|
|
|
Utils::snprintf(prec->remoteVersion,sizeof(prec->remoteVersion),"%u.%u.%u",p->first->remoteVersionMajor(),p->first->remoteVersionMinor(),p->first->remoteVersionRevision());
|
|
|
|
p->first->address().toString(prec->address,sizeof(prec->address));
|
|
|
|
prec->rawAddress = p->first->address().toInt();
|
|
|
|
prec->latency = p->first->latency();
|
2014-10-19 20:07:50 +00:00
|
|
|
prec->role = RR->topology->isSupernode(p->first->address()) ? ZT1_Node_Peer_SUPERNODE : ZT1_Node_Peer_NODE;
|
2014-09-12 23:57:37 +00:00
|
|
|
|
|
|
|
prec->paths = (ZT1_Node_PhysicalPath *)buf;
|
|
|
|
buf += sizeof(ZT1_Node_PhysicalPath) * p->second.size();
|
|
|
|
|
|
|
|
prec->numPaths = 0;
|
|
|
|
for(std::vector<Path>::iterator pi(p->second.begin());pi!=p->second.end();++pi) {
|
|
|
|
ZT1_Node_PhysicalPath *path = &(prec->paths[prec->numPaths++]);
|
2014-10-19 19:56:39 +00:00
|
|
|
path->type = (ZT1_Node_PhysicalPathType)pi->type();
|
2014-09-12 23:57:37 +00:00
|
|
|
if (pi->address().isV6()) {
|
2014-10-19 20:07:50 +00:00
|
|
|
path->address.type = ZT1_Node_PhysicalAddress_TYPE_IPV6;
|
2014-09-12 23:57:37 +00:00
|
|
|
memcpy(path->address.bits,pi->address().rawIpData(),16);
|
|
|
|
// TODO: zoneIndex not supported yet, but should be once echo-location works w/V6
|
|
|
|
} else {
|
2014-10-19 20:07:50 +00:00
|
|
|
path->address.type = ZT1_Node_PhysicalAddress_TYPE_IPV4;
|
2014-09-12 23:57:37 +00:00
|
|
|
memcpy(path->address.bits,pi->address().rawIpData(),4);
|
|
|
|
}
|
|
|
|
path->address.port = pi->address().port();
|
|
|
|
Utils::scopy(path->address.ascii,sizeof(path->address.ascii),pi->address().toIpString().c_str());
|
|
|
|
path->lastSend = (pi->lastSend() > 0) ? ((long)(now - pi->lastSend())) : (long)-1;
|
|
|
|
path->lastReceive = (pi->lastReceived() > 0) ? ((long)(now - pi->lastReceived())) : (long)-1;
|
|
|
|
path->lastPing = (pi->lastPing() > 0) ? ((long)(now - pi->lastPing())) : (long)-1;
|
|
|
|
path->active = pi->active(now);
|
|
|
|
path->fixed = pi->fixed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pl;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fills out everything but ips[] and numIps, which must be done more manually
|
|
|
|
static void _fillNetworkQueryResultBuffer(const SharedPtr<Network> &network,const SharedPtr<NetworkConfig> &nconf,ZT1_Node_Network *nbuf)
|
|
|
|
{
|
|
|
|
nbuf->nwid = network->id();
|
|
|
|
Utils::snprintf(nbuf->nwidHex,sizeof(nbuf->nwidHex),"%.16llx",(unsigned long long)network->id());
|
|
|
|
if (nconf) {
|
|
|
|
Utils::scopy(nbuf->name,sizeof(nbuf->name),nconf->name().c_str());
|
|
|
|
Utils::scopy(nbuf->description,sizeof(nbuf->description),nconf->description().c_str());
|
|
|
|
}
|
|
|
|
Utils::scopy(nbuf->device,sizeof(nbuf->device),network->tapDeviceName().c_str());
|
|
|
|
Utils::scopy(nbuf->statusStr,sizeof(nbuf->statusStr),Network::statusString(network->status()));
|
|
|
|
network->mac().toString(nbuf->macStr,sizeof(nbuf->macStr));
|
|
|
|
network->mac().copyTo(nbuf->mac,sizeof(nbuf->mac));
|
|
|
|
uint64_t lcu = network->lastConfigUpdate();
|
|
|
|
if (lcu > 0)
|
|
|
|
nbuf->configAge = (long)(Utils::now() - lcu);
|
|
|
|
else nbuf->configAge = -1;
|
2014-10-19 19:56:39 +00:00
|
|
|
nbuf->status = (ZT1_Node_NetworkStatus)network->status();
|
2014-09-12 23:57:37 +00:00
|
|
|
nbuf->enabled = network->enabled();
|
|
|
|
nbuf->isPrivate = (nconf) ? nconf->isPrivate() : true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ZT1_Node_Network *Node::getNetworkStatus(uint64_t nwid)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
_NodeImpl *impl = (_NodeImpl *)_impl;
|
2014-09-24 20:53:03 +00:00
|
|
|
RuntimeEnvironment *RR = (RuntimeEnvironment *)&(impl->renv);
|
2014-09-12 23:57:37 +00:00
|
|
|
|
2014-10-28 21:17:39 +00:00
|
|
|
if ((!RR)||(!RR->initialized))
|
|
|
|
return (ZT1_Node_Network *)0;
|
|
|
|
|
2014-09-24 20:53:03 +00:00
|
|
|
SharedPtr<Network> network(RR->nc->network(nwid));
|
2014-09-12 23:57:37 +00:00
|
|
|
if (!network)
|
|
|
|
return (ZT1_Node_Network *)0;
|
|
|
|
SharedPtr<NetworkConfig> nconf(network->config2());
|
|
|
|
std::set<InetAddress> ips(network->ips());
|
|
|
|
|
|
|
|
char *buf = (char *)::malloc(sizeof(ZT1_Node_Network) + (sizeof(ZT1_Node_PhysicalAddress) * ips.size()));
|
|
|
|
if (!buf)
|
|
|
|
return (ZT1_Node_Network *)0;
|
|
|
|
memset(buf,0,sizeof(ZT1_Node_Network) + (sizeof(ZT1_Node_PhysicalAddress) * ips.size()));
|
|
|
|
|
|
|
|
ZT1_Node_Network *nbuf = (ZT1_Node_Network *)buf;
|
|
|
|
buf += sizeof(ZT1_Node_Network);
|
|
|
|
|
|
|
|
_fillNetworkQueryResultBuffer(network,nconf,nbuf);
|
|
|
|
|
|
|
|
nbuf->ips = (ZT1_Node_PhysicalAddress *)buf;
|
|
|
|
nbuf->numIps = 0;
|
|
|
|
for(std::set<InetAddress>::iterator ip(ips.begin());ip!=ips.end();++ip) {
|
|
|
|
ZT1_Node_PhysicalAddress *ipb = &(nbuf->ips[nbuf->numIps++]);
|
|
|
|
if (ip->isV6()) {
|
2014-10-19 20:07:50 +00:00
|
|
|
ipb->type = ZT1_Node_PhysicalAddress_TYPE_IPV6;
|
2014-09-12 23:57:37 +00:00
|
|
|
memcpy(ipb->bits,ip->rawIpData(),16);
|
|
|
|
} else {
|
2014-10-19 20:07:50 +00:00
|
|
|
ipb->type = ZT1_Node_PhysicalAddress_TYPE_IPV4;
|
2014-09-12 23:57:37 +00:00
|
|
|
memcpy(ipb->bits,ip->rawIpData(),4);
|
|
|
|
}
|
|
|
|
ipb->port = ip->port();
|
|
|
|
Utils::scopy(ipb->ascii,sizeof(ipb->ascii),ip->toIpString().c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
return nbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
ZT1_Node_NetworkList *Node::listNetworks()
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
_NodeImpl *impl = (_NodeImpl *)_impl;
|
2014-09-24 20:53:03 +00:00
|
|
|
RuntimeEnvironment *RR = (RuntimeEnvironment *)&(impl->renv);
|
2014-09-12 23:57:37 +00:00
|
|
|
|
2014-10-28 21:17:39 +00:00
|
|
|
if ((!RR)||(!RR->initialized))
|
|
|
|
return (ZT1_Node_NetworkList *)0;
|
|
|
|
|
2014-09-24 20:53:03 +00:00
|
|
|
std::vector< SharedPtr<Network> > networks(RR->nc->networks());
|
2014-09-12 23:57:37 +00:00
|
|
|
std::vector< SharedPtr<NetworkConfig> > nconfs(networks.size());
|
|
|
|
std::vector< std::set<InetAddress> > ipsv(networks.size());
|
|
|
|
|
|
|
|
unsigned long returnBufSize = sizeof(ZT1_Node_NetworkList);
|
|
|
|
for(unsigned long i=0;i<networks.size();++i) {
|
|
|
|
nconfs[i] = networks[i]->config2();
|
|
|
|
ipsv[i] = networks[i]->ips();
|
2014-10-19 19:56:39 +00:00
|
|
|
returnBufSize += sizeof(ZT1_Node_Network) + (sizeof(ZT1_Node_PhysicalAddress) * (unsigned int)ipsv[i].size());
|
2014-09-12 23:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *buf = (char *)::malloc(returnBufSize);
|
|
|
|
if (!buf)
|
|
|
|
return (ZT1_Node_NetworkList *)0;
|
|
|
|
memset(buf,0,returnBufSize);
|
|
|
|
|
|
|
|
ZT1_Node_NetworkList *nl = (ZT1_Node_NetworkList *)buf;
|
|
|
|
buf += sizeof(ZT1_Node_NetworkList);
|
|
|
|
|
|
|
|
nl->networks = (ZT1_Node_Network *)buf;
|
|
|
|
buf += sizeof(ZT1_Node_Network) * networks.size();
|
|
|
|
|
|
|
|
for(unsigned long i=0;i<networks.size();++i) {
|
|
|
|
ZT1_Node_Network *nbuf = &(nl->networks[nl->numNetworks++]);
|
|
|
|
|
|
|
|
_fillNetworkQueryResultBuffer(networks[i],nconfs[i],nbuf);
|
|
|
|
|
|
|
|
nbuf->ips = (ZT1_Node_PhysicalAddress *)buf;
|
|
|
|
buf += sizeof(ZT1_Node_PhysicalAddress);
|
|
|
|
|
|
|
|
nbuf->numIps = 0;
|
|
|
|
for(std::set<InetAddress>::iterator ip(ipsv[i].begin());ip!=ipsv[i].end();++ip) {
|
|
|
|
ZT1_Node_PhysicalAddress *ipb = &(nbuf->ips[nbuf->numIps++]);
|
|
|
|
if (ip->isV6()) {
|
2014-10-19 20:07:50 +00:00
|
|
|
ipb->type = ZT1_Node_PhysicalAddress_TYPE_IPV6;
|
2014-09-12 23:57:37 +00:00
|
|
|
memcpy(ipb->bits,ip->rawIpData(),16);
|
|
|
|
} else {
|
2014-10-19 20:07:50 +00:00
|
|
|
ipb->type = ZT1_Node_PhysicalAddress_TYPE_IPV4;
|
2014-09-12 23:57:37 +00:00
|
|
|
memcpy(ipb->bits,ip->rawIpData(),4);
|
|
|
|
}
|
|
|
|
ipb->port = ip->port();
|
|
|
|
Utils::scopy(ipb->ascii,sizeof(ipb->ascii),ip->toIpString().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Node::freeQueryResult(void *qr)
|
|
|
|
throw()
|
|
|
|
{
|
2014-09-15 23:32:55 +00:00
|
|
|
if (qr)
|
|
|
|
::free(qr);
|
2014-09-12 23:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Node::updateCheck()
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
_NodeImpl *impl = (_NodeImpl *)_impl;
|
2014-09-24 20:53:03 +00:00
|
|
|
RuntimeEnvironment *RR = (RuntimeEnvironment *)&(impl->renv);
|
|
|
|
if (RR->updater) {
|
|
|
|
RR->updater->checkNow();
|
2014-09-12 23:57:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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; }
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|