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 <string.h>
|
2013-07-17 18:10:44 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2013-08-06 05:28:56 +00:00
|
|
|
#include <map>
|
|
|
|
#include <set>
|
2013-07-04 20:56:19 +00:00
|
|
|
|
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 "NodeConfig.hpp"
|
|
|
|
#include "RuntimeEnvironment.hpp"
|
|
|
|
#include "Defaults.hpp"
|
|
|
|
#include "Utils.hpp"
|
|
|
|
#include "Logger.hpp"
|
2013-07-17 18:10:44 +00:00
|
|
|
#include "Topology.hpp"
|
|
|
|
#include "Demarc.hpp"
|
2013-10-18 21:39:48 +00:00
|
|
|
#include "Packet.hpp"
|
2013-07-17 18:10:44 +00:00
|
|
|
#include "InetAddress.hpp"
|
|
|
|
#include "Peer.hpp"
|
|
|
|
#include "Salsa20.hpp"
|
2013-09-16 17:02:10 +00:00
|
|
|
#include "Poly1305.hpp"
|
|
|
|
#include "SHA512.hpp"
|
2013-08-30 19:02:12 +00:00
|
|
|
#include "Node.hpp"
|
2013-12-11 21:00:18 +00:00
|
|
|
#include "SoftwareUpdater.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
2013-10-18 18:16:53 +00:00
|
|
|
NodeConfig::NodeConfig(const RuntimeEnvironment *renv,const char *authToken,unsigned int controlPort) :
|
2013-07-04 20:56:19 +00:00
|
|
|
_r(renv),
|
2013-09-17 18:47:48 +00:00
|
|
|
_controlSocket(true,controlPort,false,&_CBcontrolPacketHandler,this)
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2013-09-16 17:02:10 +00:00
|
|
|
{
|
|
|
|
unsigned int csk[64];
|
2013-09-16 17:57:57 +00:00
|
|
|
SHA512::hash(csk,authToken,strlen(authToken));
|
2013-09-16 17:02:10 +00:00
|
|
|
memcpy(_controlSocketKey,csk,32);
|
|
|
|
}
|
2013-08-06 05:28:56 +00:00
|
|
|
|
2013-09-19 18:40:46 +00:00
|
|
|
std::string networksFolder(_r->homePath + ZT_PATH_SEPARATOR_S + "networks.d");
|
|
|
|
std::map<std::string,bool> networksDotD(Utils::listDirectory(networksFolder.c_str()));
|
2013-08-06 05:28:56 +00:00
|
|
|
std::set<uint64_t> nwids;
|
|
|
|
for(std::map<std::string,bool>::iterator d(networksDotD.begin());d!=networksDotD.end();++d) {
|
|
|
|
if (!d->second) {
|
|
|
|
std::string::size_type dot = d->first.rfind(".conf");
|
|
|
|
if (dot != std::string::npos) {
|
|
|
|
uint64_t nwid = strtoull(d->first.substr(0,dot).c_str(),(char **)0,16);
|
2013-09-19 18:36:37 +00:00
|
|
|
|
2013-10-07 19:00:38 +00:00
|
|
|
// TODO: remove legacy code once out of beta
|
2013-09-19 18:36:37 +00:00
|
|
|
if (nwid == 0x6c92786fee000001ULL) {
|
|
|
|
nwid = 0xbc8f9a8ee3000001ULL;
|
2013-09-19 18:40:46 +00:00
|
|
|
Utils::rm((networksFolder + ZT_PATH_SEPARATOR_S + d->first).c_str());
|
2013-09-19 18:36:37 +00:00
|
|
|
}
|
2013-10-07 19:00:38 +00:00
|
|
|
if (nwid == 0xbc8f9a8ee3000001ULL) {
|
|
|
|
nwid = 0x8D93FBE886000001ULL;
|
|
|
|
Utils::rm((networksFolder + ZT_PATH_SEPARATOR_S + d->first).c_str());
|
|
|
|
}
|
2013-10-20 20:00:41 +00:00
|
|
|
if (nwid == 0x8D93FBE886000001ULL) {
|
|
|
|
nwid = 0x8056c2e21c000001ULL;
|
|
|
|
Utils::rm((networksFolder + ZT_PATH_SEPARATOR_S + d->first).c_str());
|
|
|
|
}
|
2013-09-19 18:36:37 +00:00
|
|
|
|
2013-08-06 05:28:56 +00:00
|
|
|
if (nwid > 0)
|
|
|
|
nwids.insert(nwid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(std::set<uint64_t>::iterator nwid(nwids.begin());nwid!=nwids.end();++nwid) {
|
|
|
|
try {
|
2013-08-06 14:15:05 +00:00
|
|
|
SharedPtr<Network> nw(Network::newInstance(_r,*nwid));
|
2013-08-06 05:28:56 +00:00
|
|
|
_networks[*nwid] = nw;
|
|
|
|
} catch (std::exception &exc) {
|
|
|
|
LOG("unable to create network %.16llx: %s",(unsigned long long)*nwid,exc.what());
|
|
|
|
} catch ( ... ) {
|
|
|
|
LOG("unable to create network %.16llx: (unknown exception)",(unsigned long long)*nwid);
|
|
|
|
}
|
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NodeConfig::~NodeConfig()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-07-17 14:01:46 +00:00
|
|
|
void NodeConfig::whackAllTaps()
|
|
|
|
{
|
|
|
|
std::vector< SharedPtr<Network> > nwlist;
|
|
|
|
Mutex::Lock _l(_networks_m);
|
|
|
|
for(std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.begin());n!=_networks.end();++n)
|
|
|
|
n->second->tap().whack();
|
|
|
|
}
|
|
|
|
|
2013-08-06 04:05:39 +00:00
|
|
|
void NodeConfig::clean()
|
2013-07-30 15:14:53 +00:00
|
|
|
{
|
|
|
|
Mutex::Lock _l(_networks_m);
|
|
|
|
for(std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.begin());n!=_networks.end();++n)
|
|
|
|
n->second->clean();
|
|
|
|
}
|
|
|
|
|
2013-10-16 21:47:26 +00:00
|
|
|
// Macro used in execute() to push lines onto the return packet
|
2013-07-17 18:10:44 +00:00
|
|
|
#undef _P
|
|
|
|
#define _P(f,...) { r.push_back(std::string()); Utils::stdsprintf(r.back(),(f),##__VA_ARGS__); }
|
|
|
|
|
|
|
|
// Used with Topology::eachPeer to dump peer stats
|
|
|
|
class _DumpPeerStatistics
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2013-07-17 18:10:44 +00:00
|
|
|
public:
|
|
|
|
_DumpPeerStatistics(std::vector<std::string> &out) :
|
|
|
|
r(out),
|
|
|
|
_now(Utils::now())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void operator()(Topology &t,const SharedPtr<Peer> &p)
|
|
|
|
{
|
|
|
|
InetAddress v4(p->ipv4ActivePath(_now));
|
|
|
|
InetAddress v6(p->ipv6ActivePath(_now));
|
2013-10-18 17:20:34 +00:00
|
|
|
if ((v4)||(v6)) {
|
|
|
|
_P("200 listpeers %s %s %s %u %s",
|
|
|
|
p->address().toString().c_str(),
|
|
|
|
((v4) ? v4.toString().c_str() : "-"),
|
|
|
|
((v6) ? v6.toString().c_str() : "-"),
|
|
|
|
p->latency(),
|
|
|
|
p->remoteVersion().c_str());
|
|
|
|
} else {
|
|
|
|
_P("200 listpeers %s - - - %s",
|
|
|
|
p->address().toString().c_str(),
|
|
|
|
p->remoteVersion().c_str());
|
|
|
|
}
|
2013-07-17 18:10:44 +00:00
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-07-17 18:10:44 +00:00
|
|
|
private:
|
|
|
|
std::vector<std::string> &r;
|
|
|
|
uint64_t _now;
|
|
|
|
};
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-07-17 18:10:44 +00:00
|
|
|
std::vector<std::string> NodeConfig::execute(const char *command)
|
|
|
|
{
|
|
|
|
std::vector<std::string> r;
|
|
|
|
std::vector<std::string> cmd(Utils::split(command,"\r\n \t","\\","'"));
|
|
|
|
|
2013-10-16 21:47:26 +00:00
|
|
|
/* Not coincidentally, response type codes correspond with HTTP
|
|
|
|
* status codes. Technically a little arbitrary, but would maybe
|
|
|
|
* make things easier if we wanted to slap some kind of web API
|
|
|
|
* in front of this thing. */
|
2013-07-17 18:10:44 +00:00
|
|
|
|
|
|
|
if ((cmd.empty())||(cmd[0] == "help")) {
|
|
|
|
_P("200 help help");
|
2013-10-16 21:47:26 +00:00
|
|
|
_P("200 help info");
|
2013-07-17 18:10:44 +00:00
|
|
|
_P("200 help listpeers");
|
|
|
|
_P("200 help listnetworks");
|
2013-08-06 15:50:56 +00:00
|
|
|
_P("200 help join <network ID>");
|
2013-07-17 18:10:44 +00:00
|
|
|
_P("200 help leave <network ID>");
|
2013-08-30 19:02:12 +00:00
|
|
|
_P("200 help terminate [<reason>]");
|
2013-12-11 21:00:18 +00:00
|
|
|
_P("200 help updatecheck");
|
2013-10-16 21:47:26 +00:00
|
|
|
} else if (cmd[0] == "info") {
|
|
|
|
bool isOnline = false;
|
|
|
|
uint64_t now = Utils::now();
|
|
|
|
std::vector< SharedPtr<Peer> > snp(_r->topology->supernodePeers());
|
|
|
|
for(std::vector< SharedPtr<Peer> >::const_iterator sn(snp.begin());sn!=snp.end();++sn) {
|
|
|
|
if ((*sn)->hasActiveDirectPath(now)) {
|
|
|
|
isOnline = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_P("200 info %s %s %s",_r->identity.address().toString().c_str(),(isOnline ? "ONLINE" : "OFFLINE"),Node::versionString());
|
2013-07-17 18:10:44 +00:00
|
|
|
} else if (cmd[0] == "listpeers") {
|
2013-08-06 15:50:56 +00:00
|
|
|
_P("200 listpeers <ztaddr> <ipv4> <ipv6> <latency> <version>");
|
2013-07-17 18:10:44 +00:00
|
|
|
_r->topology->eachPeer(_DumpPeerStatistics(r));
|
|
|
|
} else if (cmd[0] == "listnetworks") {
|
|
|
|
Mutex::Lock _l(_networks_m);
|
2013-11-21 20:11:22 +00:00
|
|
|
_P("200 listnetworks <nwid> <name> <status> <config age> <type> <dev> <ips>");
|
2013-07-17 18:10:44 +00:00
|
|
|
for(std::map< uint64_t,SharedPtr<Network> >::const_iterator nw(_networks.begin());nw!=_networks.end();++nw) {
|
2013-08-05 21:44:39 +00:00
|
|
|
std::string tmp;
|
|
|
|
std::set<InetAddress> ips(nw->second->tap().ips());
|
|
|
|
for(std::set<InetAddress>::iterator i(ips.begin());i!=ips.end();++i) {
|
|
|
|
if (tmp.length())
|
|
|
|
tmp.push_back(',');
|
|
|
|
tmp.append(i->toString());
|
|
|
|
}
|
2013-10-16 21:47:26 +00:00
|
|
|
|
2013-10-18 17:20:34 +00:00
|
|
|
SharedPtr<NetworkConfig> nconf(nw->second->config2());
|
2013-11-21 20:11:22 +00:00
|
|
|
|
|
|
|
long long age = (nconf) ? ((long long)Utils::now() - (long long)nconf->timestamp()) : (long long)0;
|
|
|
|
if (age < 0)
|
|
|
|
age = 0;
|
|
|
|
age /= 1000;
|
|
|
|
|
|
|
|
_P("200 listnetworks %.16llx %s %s %lld %s %s %s",
|
2013-08-05 21:44:39 +00:00
|
|
|
(unsigned long long)nw->first,
|
2013-11-20 21:16:30 +00:00
|
|
|
((nconf) ? nconf->name().c_str() : "?"),
|
2013-08-08 14:41:17 +00:00
|
|
|
Network::statusString(nw->second->status()),
|
2013-11-21 20:11:22 +00:00
|
|
|
age,
|
2013-10-18 17:20:34 +00:00
|
|
|
((nconf) ? (nconf->isOpen() ? "public" : "private") : "?"),
|
2013-07-17 18:10:44 +00:00
|
|
|
nw->second->tap().deviceName().c_str(),
|
2013-08-08 14:41:17 +00:00
|
|
|
((tmp.length() > 0) ? tmp.c_str() : "-"));
|
2013-07-17 18:10:44 +00:00
|
|
|
}
|
|
|
|
} else if (cmd[0] == "join") {
|
2013-08-06 04:05:39 +00:00
|
|
|
if (cmd.size() > 1) {
|
|
|
|
uint64_t nwid = strtoull(cmd[1].c_str(),(char **)0,16);
|
|
|
|
if (nwid > 0) {
|
|
|
|
Mutex::Lock _l(_networks_m);
|
2013-08-06 05:28:56 +00:00
|
|
|
if (_networks.count(nwid)) {
|
2013-10-16 21:47:26 +00:00
|
|
|
_P("409 already a member of %.16llx",(unsigned long long)nwid);
|
2013-08-06 05:28:56 +00:00
|
|
|
} else {
|
|
|
|
try {
|
2013-08-06 14:15:05 +00:00
|
|
|
SharedPtr<Network> nw(Network::newInstance(_r,nwid));
|
2013-08-06 05:28:56 +00:00
|
|
|
_networks[nwid] = nw;
|
|
|
|
_P("200 join %.16llx OK",(unsigned long long)nwid);
|
|
|
|
} catch (std::exception &exc) {
|
|
|
|
_P("500 join %.16llx ERROR: %s",(unsigned long long)nwid,exc.what());
|
|
|
|
} catch ( ... ) {
|
|
|
|
_P("500 join %.16llx ERROR: (unknown exception)",(unsigned long long)nwid);
|
|
|
|
}
|
2013-08-06 04:05:39 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_P("400 join requires a network ID (>0) in hexadecimal format");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_P("400 join requires a network ID (>0) in hexadecimal format");
|
|
|
|
}
|
2013-07-17 18:10:44 +00:00
|
|
|
} else if (cmd[0] == "leave") {
|
2013-08-06 04:05:39 +00:00
|
|
|
if (cmd.size() > 1) {
|
|
|
|
Mutex::Lock _l(_networks_m);
|
|
|
|
uint64_t nwid = strtoull(cmd[1].c_str(),(char **)0,16);
|
|
|
|
std::map< uint64_t,SharedPtr<Network> >::iterator nw(_networks.find(nwid));
|
|
|
|
if (nw == _networks.end()) {
|
|
|
|
_P("404 leave %.16llx ERROR: not a member of that network",(unsigned long long)nwid);
|
|
|
|
} else {
|
|
|
|
nw->second->destroyOnDelete();
|
|
|
|
_networks.erase(nw);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_P("400 leave requires a network ID (>0) in hexadecimal format");
|
|
|
|
}
|
2013-08-30 19:02:12 +00:00
|
|
|
} else if (cmd[0] == "terminate") {
|
|
|
|
if (cmd.size() > 1)
|
|
|
|
_r->node->terminate(Node::NODE_NORMAL_TERMINATION,cmd[1].c_str());
|
|
|
|
else _r->node->terminate(Node::NODE_NORMAL_TERMINATION,(const char *)0);
|
2013-12-11 21:00:18 +00:00
|
|
|
} else if (cmd[0] == "updatecheck") {
|
|
|
|
if (_r->updater) {
|
|
|
|
_P("200 checking for software updates now at: %s",ZT_DEFAULTS.updateLatestNfoURL.c_str());
|
|
|
|
_r->updater->checkNow();
|
|
|
|
} else {
|
|
|
|
_P("500 software updates are not enabled");
|
|
|
|
}
|
2013-07-17 18:10:44 +00:00
|
|
|
} else {
|
|
|
|
_P("404 %s No such command. Use 'help' for help.",cmd[0].c_str());
|
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-08-05 21:44:39 +00:00
|
|
|
r.push_back(std::string()); // terminate with empty line
|
|
|
|
|
2013-07-17 18:10:44 +00:00
|
|
|
return r;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2013-07-18 15:43:46 +00:00
|
|
|
std::vector< Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> > NodeConfig::encodeControlMessage(const void *key,unsigned long conversationId,const std::vector<std::string> &payload)
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2013-09-16 17:02:10 +00:00
|
|
|
char poly1305tag[ZT_POLY1305_MAC_LEN];
|
|
|
|
char iv[8];
|
2013-07-18 15:43:46 +00:00
|
|
|
char keytmp[32];
|
|
|
|
std::vector< Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> > packets;
|
|
|
|
Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> packet;
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-09-16 17:02:10 +00:00
|
|
|
packet.setSize(16); // room for poly1305 auth tag and IV
|
2013-07-18 15:43:46 +00:00
|
|
|
packet.append((uint32_t)(conversationId & 0xffffffff));
|
2013-08-05 21:44:39 +00:00
|
|
|
|
2013-07-18 15:43:46 +00:00
|
|
|
for(unsigned int i=0;i<payload.size();++i) {
|
|
|
|
packet.append(payload[i]); // will throw if too big
|
|
|
|
packet.append((unsigned char)0);
|
|
|
|
|
|
|
|
if (((i + 1) >= payload.size())||((packet.size() + payload[i + 1].length() + 1) >= packet.capacity())) {
|
2013-09-16 17:02:10 +00:00
|
|
|
Utils::getSecureRandom(iv,8);
|
|
|
|
memcpy(packet.field(8,8),iv,8);
|
2013-07-18 15:43:46 +00:00
|
|
|
|
2013-10-18 21:39:48 +00:00
|
|
|
Salsa20 s20(key,256,iv,ZT_PROTO_SALSA20_ROUNDS);
|
2013-07-18 17:27:46 +00:00
|
|
|
s20.encrypt(packet.field(16,packet.size() - 16),packet.field(16,packet.size() - 16),packet.size() - 16);
|
|
|
|
|
2013-07-18 15:43:46 +00:00
|
|
|
memcpy(keytmp,key,32);
|
2013-09-16 17:02:10 +00:00
|
|
|
for(unsigned int i=0;i<8;++i)
|
|
|
|
keytmp[i] ^= iv[i]; // can't reuse poly1305 keys, so mangle key with IV each time
|
|
|
|
Poly1305::compute(poly1305tag,packet.field(16,packet.size() - 16),packet.size() - 16,keytmp);
|
|
|
|
memcpy(packet.field(0,8),poly1305tag,8);
|
2013-07-18 15:43:46 +00:00
|
|
|
|
|
|
|
packets.push_back(packet);
|
|
|
|
|
2013-09-16 17:02:10 +00:00
|
|
|
packet.setSize(16); // room for poly1305 auth tag and IV
|
2013-07-18 15:43:46 +00:00
|
|
|
packet.append((uint32_t)(conversationId & 0xffffffff));
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
2013-07-18 15:43:46 +00:00
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-07-18 15:43:46 +00:00
|
|
|
return packets;
|
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-07-18 15:43:46 +00:00
|
|
|
bool NodeConfig::decodeControlMessagePacket(const void *key,const void *data,unsigned int len,unsigned long &conversationId,std::vector<std::string> &payload)
|
|
|
|
{
|
2013-09-16 17:02:10 +00:00
|
|
|
char poly1305tag[ZT_POLY1305_MAC_LEN];
|
2013-07-18 15:43:46 +00:00
|
|
|
char keytmp[32];
|
2013-09-16 17:02:10 +00:00
|
|
|
char iv[8];
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-07-18 15:43:46 +00:00
|
|
|
try {
|
|
|
|
if (len < 20)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> packet(data,len);
|
|
|
|
|
|
|
|
memcpy(keytmp,key,32);
|
2013-09-16 17:02:10 +00:00
|
|
|
memcpy(iv,packet.field(8,8),8);
|
|
|
|
for(unsigned int i=0;i<8;++i)
|
|
|
|
keytmp[i] ^= iv[i];
|
|
|
|
Poly1305::compute(poly1305tag,packet.field(16,packet.size() - 16),packet.size() - 16,keytmp);
|
|
|
|
if (!Utils::secureEq(packet.field(0,8),poly1305tag,8))
|
2013-07-18 15:43:46 +00:00
|
|
|
return false;
|
|
|
|
|
2013-10-18 21:39:48 +00:00
|
|
|
Salsa20 s20(key,256,packet.field(8,8),ZT_PROTO_SALSA20_ROUNDS);
|
2013-07-18 15:43:46 +00:00
|
|
|
s20.decrypt(packet.field(16,packet.size() - 16),packet.field(16,packet.size() - 16),packet.size() - 16);
|
|
|
|
|
|
|
|
conversationId = packet.at<uint32_t>(16);
|
|
|
|
|
|
|
|
const char *pl = ((const char *)packet.data()) + 20;
|
|
|
|
unsigned int pll = packet.size() - 20;
|
|
|
|
for(unsigned int i=0;i<pll;) {
|
|
|
|
unsigned int eos = i;
|
|
|
|
while ((eos < pll)&&(pl[eos]))
|
|
|
|
++eos;
|
2013-08-05 21:44:39 +00:00
|
|
|
if (eos >= i) {
|
2013-07-18 15:43:46 +00:00
|
|
|
payload.push_back(std::string(pl + i,eos - i));
|
|
|
|
i = eos + 1;
|
|
|
|
} else break;
|
|
|
|
}
|
2013-07-17 21:35:19 +00:00
|
|
|
|
2013-07-18 15:43:46 +00:00
|
|
|
return true;
|
2013-07-17 18:10:44 +00:00
|
|
|
} catch ( ... ) {
|
2013-07-18 15:43:46 +00:00
|
|
|
return false;
|
2013-07-17 18:10:44 +00:00
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2013-07-18 15:43:46 +00:00
|
|
|
void NodeConfig::_CBcontrolPacketHandler(UdpSocket *sock,void *arg,const InetAddress &remoteAddr,const void *data,unsigned int len)
|
|
|
|
{
|
2013-07-18 20:35:52 +00:00
|
|
|
NodeConfig *nc = (NodeConfig *)arg;
|
2013-10-21 18:12:00 +00:00
|
|
|
#ifdef ZT_TRACE
|
2013-07-20 22:24:56 +00:00
|
|
|
const RuntimeEnvironment *_r = nc->_r;
|
2013-10-21 18:12:00 +00:00
|
|
|
#endif
|
2013-07-20 22:24:56 +00:00
|
|
|
|
2013-07-18 20:35:52 +00:00
|
|
|
try {
|
|
|
|
unsigned long convId = 0;
|
|
|
|
std::vector<std::string> commands;
|
|
|
|
|
2013-07-20 22:24:56 +00:00
|
|
|
if (!decodeControlMessagePacket(nc->_controlSocketKey,data,len,convId,commands)) {
|
|
|
|
TRACE("control bus packet from %s failed decode, discarded",remoteAddr.toString().c_str());
|
2013-07-18 20:35:52 +00:00
|
|
|
return;
|
2013-07-20 22:24:56 +00:00
|
|
|
}
|
|
|
|
TRACE("control bus packet from %s, contains %d commands",remoteAddr.toString().c_str(),(int)commands.size());
|
2013-07-18 20:35:52 +00:00
|
|
|
|
|
|
|
for(std::vector<std::string>::iterator c(commands.begin());c!=commands.end();++c) {
|
|
|
|
std::vector< Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> > resultPackets(encodeControlMessage(nc->_controlSocketKey,convId,nc->execute(c->c_str())));
|
|
|
|
for(std::vector< Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> >::iterator p(resultPackets.begin());p!=resultPackets.end();++p)
|
|
|
|
sock->send(remoteAddr,p->data(),p->size(),-1);
|
|
|
|
}
|
2013-08-05 20:06:16 +00:00
|
|
|
} catch (std::exception &exc) {
|
|
|
|
TRACE("exception handling control bus packet from %s: %s",remoteAddr.toString().c_str(),exc.what());
|
2013-07-24 00:21:34 +00:00
|
|
|
} catch ( ... ) {
|
2013-08-05 20:06:16 +00:00
|
|
|
TRACE("exception handling control bus packet from %s: (unknown)",remoteAddr.toString().c_str());
|
2013-07-24 00:21:34 +00:00
|
|
|
}
|
2013-07-18 15:43:46 +00:00
|
|
|
}
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
} // namespace ZeroTier
|