2016-11-03 21:17:46 +00:00
|
|
|
/*
|
|
|
|
* ZeroTier One - Network Virtualization Everywhere
|
|
|
|
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2017-07-18 20:57:37 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#ifndef __WINDOWS__
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#endif
|
|
|
|
|
2016-11-03 21:17:46 +00:00
|
|
|
#include "JSONDB.hpp"
|
|
|
|
|
2017-04-19 00:37:44 +00:00
|
|
|
#define ZT_JSONDB_HTTP_TIMEOUT 60000
|
|
|
|
|
2016-11-03 21:17:46 +00:00
|
|
|
namespace ZeroTier {
|
|
|
|
|
2016-11-04 22:34:49 +00:00
|
|
|
static const nlohmann::json _EMPTY_JSON(nlohmann::json::object());
|
2017-04-19 00:37:44 +00:00
|
|
|
static const std::map<std::string,std::string> _ZT_JSONDB_GET_HEADERS;
|
|
|
|
|
|
|
|
JSONDB::JSONDB(const std::string &basePath) :
|
2017-04-26 17:35:59 +00:00
|
|
|
_basePath(basePath),
|
2017-07-18 20:57:37 +00:00
|
|
|
_rawInput(-1),
|
|
|
|
_rawOutput(-1),
|
2017-04-26 17:35:59 +00:00
|
|
|
_summaryThreadRun(true)
|
2017-04-19 00:37:44 +00:00
|
|
|
{
|
|
|
|
if ((_basePath.length() > 7)&&(_basePath.substr(0,7) == "http://")) {
|
2017-07-18 20:57:37 +00:00
|
|
|
// If base path is http:// we run in HTTP mode
|
2017-04-19 00:37:44 +00:00
|
|
|
// TODO: this doesn't yet support IPv6 since bracketed address notiation isn't supported.
|
2017-04-26 13:48:08 +00:00
|
|
|
// Typically it's just used with 127.0.0.1 anyway.
|
2017-04-19 00:37:44 +00:00
|
|
|
std::string hn = _basePath.substr(7);
|
|
|
|
std::size_t hnend = hn.find_first_of('/');
|
|
|
|
if (hnend != std::string::npos)
|
|
|
|
hn = hn.substr(0,hnend);
|
|
|
|
std::size_t hnsep = hn.find_last_of(':');
|
|
|
|
if (hnsep != std::string::npos)
|
|
|
|
hn[hnsep] = '/';
|
2017-07-06 23:11:11 +00:00
|
|
|
_httpAddr.fromString(hn.c_str());
|
2017-04-19 00:37:44 +00:00
|
|
|
if (hnend != std::string::npos)
|
|
|
|
_basePath = _basePath.substr(7 + hnend);
|
|
|
|
if (_basePath.length() == 0)
|
|
|
|
_basePath = "/";
|
|
|
|
if (_basePath[0] != '/')
|
|
|
|
_basePath = std::string("/") + _basePath;
|
2017-07-18 20:57:37 +00:00
|
|
|
#ifndef __WINDOWS__
|
|
|
|
} else if (_basePath == "-") {
|
|
|
|
// If base path is "-" we run in stdin/stdout mode and expect our database to be populated on startup via stdin
|
|
|
|
// Not supported on Windows
|
|
|
|
_rawInput = STDIN_FILENO;
|
|
|
|
_rawOutput = STDOUT_FILENO;
|
|
|
|
fcntl(_rawInput,F_SETFL,O_NONBLOCK);
|
|
|
|
#endif
|
2017-04-19 00:37:44 +00:00
|
|
|
} else {
|
2017-07-18 20:57:37 +00:00
|
|
|
// Default mode of operation is to store files in the filesystem
|
2017-04-19 00:37:44 +00:00
|
|
|
OSUtils::mkdir(_basePath.c_str());
|
|
|
|
OSUtils::lockDownFile(_basePath.c_str(),true); // networks might contain auth tokens, etc., so restrict directory permissions
|
|
|
|
}
|
2017-04-26 13:48:08 +00:00
|
|
|
|
2017-07-18 20:57:37 +00:00
|
|
|
if (_rawInput < 0) {
|
|
|
|
unsigned int cnt = 0;
|
|
|
|
while (!_load(_basePath)) {
|
|
|
|
if ((++cnt & 7) == 0)
|
|
|
|
fprintf(stderr,"WARNING: controller still waiting to read '%s'..." ZT_EOL_S,_basePath.c_str());
|
|
|
|
Thread::sleep(250);
|
|
|
|
}
|
2017-04-26 13:48:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for(std::unordered_map<uint64_t,_NW>::iterator n(_networks.begin());n!=_networks.end();++n)
|
|
|
|
_recomputeSummaryInfo(n->first);
|
2017-04-26 19:17:43 +00:00
|
|
|
for(;;) {
|
|
|
|
_summaryThread_m.lock();
|
|
|
|
if (_summaryThreadToDo.empty()) {
|
|
|
|
_summaryThread_m.unlock();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_summaryThread_m.unlock();
|
|
|
|
Thread::sleep(50);
|
|
|
|
}
|
2017-04-26 13:48:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSONDB::~JSONDB()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Mutex::Lock _l(_networks_m);
|
|
|
|
_networks.clear();
|
|
|
|
}
|
2017-04-26 17:35:59 +00:00
|
|
|
Thread t;
|
2017-04-26 13:48:08 +00:00
|
|
|
{
|
|
|
|
Mutex::Lock _l(_summaryThread_m);
|
2017-04-26 17:35:59 +00:00
|
|
|
_summaryThreadRun = false;
|
|
|
|
t = _summaryThread;
|
2017-04-26 13:48:08 +00:00
|
|
|
}
|
2017-04-26 17:35:59 +00:00
|
|
|
if (t)
|
|
|
|
Thread::join(t);
|
2017-04-19 00:37:44 +00:00
|
|
|
}
|
2016-11-03 21:17:46 +00:00
|
|
|
|
2017-03-02 00:33:34 +00:00
|
|
|
bool JSONDB::writeRaw(const std::string &n,const std::string &obj)
|
|
|
|
{
|
2017-07-18 20:57:37 +00:00
|
|
|
if (_rawOutput >= 0) {
|
|
|
|
#ifndef __WINDOWS__
|
|
|
|
if (obj.length() > 0) {
|
|
|
|
Mutex::Lock _l(_rawLock);
|
|
|
|
if (write(_rawOutput,obj.c_str(),obj.length() + 1) > 0)
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
} else if (_httpAddr) {
|
2017-04-19 00:37:44 +00:00
|
|
|
std::map<std::string,std::string> headers;
|
|
|
|
std::string body;
|
|
|
|
std::map<std::string,std::string> reqHeaders;
|
|
|
|
char tmp[64];
|
2017-07-06 23:11:11 +00:00
|
|
|
OSUtils::ztsnprintf(tmp,sizeof(tmp),"%lu",(unsigned long)obj.length());
|
2017-04-19 00:37:44 +00:00
|
|
|
reqHeaders["Content-Length"] = tmp;
|
|
|
|
reqHeaders["Content-Type"] = "application/json";
|
2017-05-02 19:08:53 +00:00
|
|
|
const unsigned int sc = Http::PUT(0,ZT_JSONDB_HTTP_TIMEOUT,reinterpret_cast<const struct sockaddr *>(&_httpAddr),(_basePath+"/"+n).c_str(),reqHeaders,obj.data(),(unsigned long)obj.length(),headers,body);
|
2017-04-19 00:37:44 +00:00
|
|
|
return (sc == 200);
|
|
|
|
} else {
|
|
|
|
const std::string path(_genPath(n,true));
|
|
|
|
if (!path.length())
|
|
|
|
return false;
|
|
|
|
return OSUtils::writeFile(path.c_str(),obj);
|
|
|
|
}
|
2017-03-02 00:33:34 +00:00
|
|
|
}
|
|
|
|
|
2017-04-27 07:59:36 +00:00
|
|
|
bool JSONDB::hasNetwork(const uint64_t networkId) const
|
|
|
|
{
|
|
|
|
Mutex::Lock _l(_networks_m);
|
2017-04-28 02:36:03 +00:00
|
|
|
return (_networks.find(networkId) != _networks.end());
|
2017-04-27 07:59:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool JSONDB::getNetwork(const uint64_t networkId,nlohmann::json &config) const
|
|
|
|
{
|
|
|
|
Mutex::Lock _l(_networks_m);
|
2017-04-28 02:36:03 +00:00
|
|
|
const std::unordered_map<uint64_t,_NW>::const_iterator i(_networks.find(networkId));
|
2017-04-27 07:59:36 +00:00
|
|
|
if (i == _networks.end())
|
|
|
|
return false;
|
2017-04-28 02:36:03 +00:00
|
|
|
config = nlohmann::json::from_msgpack(i->second.config);
|
2017-04-27 07:59:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JSONDB::getNetworkSummaryInfo(const uint64_t networkId,NetworkSummaryInfo &ns) const
|
|
|
|
{
|
|
|
|
Mutex::Lock _l(_networks_m);
|
2017-04-28 02:36:03 +00:00
|
|
|
const std::unordered_map<uint64_t,_NW>::const_iterator i(_networks.find(networkId));
|
2017-04-27 07:59:36 +00:00
|
|
|
if (i == _networks.end())
|
|
|
|
return false;
|
|
|
|
ns = i->second.summaryInfo;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int JSONDB::getNetworkAndMember(const uint64_t networkId,const uint64_t nodeId,nlohmann::json &networkConfig,nlohmann::json &memberConfig,NetworkSummaryInfo &ns) const
|
|
|
|
{
|
|
|
|
Mutex::Lock _l(_networks_m);
|
2017-04-28 02:36:03 +00:00
|
|
|
const std::unordered_map<uint64_t,_NW>::const_iterator i(_networks.find(networkId));
|
2017-04-27 07:59:36 +00:00
|
|
|
if (i == _networks.end())
|
|
|
|
return 0;
|
2017-04-28 02:36:03 +00:00
|
|
|
const std::unordered_map< uint64_t,std::vector<uint8_t> >::const_iterator j(i->second.members.find(nodeId));
|
2017-04-27 07:59:36 +00:00
|
|
|
if (j == i->second.members.end())
|
|
|
|
return 1;
|
2017-04-28 02:36:03 +00:00
|
|
|
networkConfig = nlohmann::json::from_msgpack(i->second.config);
|
|
|
|
memberConfig = nlohmann::json::from_msgpack(j->second);
|
2017-04-27 07:59:36 +00:00
|
|
|
ns = i->second.summaryInfo;
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JSONDB::getNetworkMember(const uint64_t networkId,const uint64_t nodeId,nlohmann::json &memberConfig) const
|
|
|
|
{
|
|
|
|
Mutex::Lock _l(_networks_m);
|
2017-04-28 02:36:03 +00:00
|
|
|
const std::unordered_map<uint64_t,_NW>::const_iterator i(_networks.find(networkId));
|
2017-04-27 07:59:36 +00:00
|
|
|
if (i == _networks.end())
|
|
|
|
return false;
|
2017-04-28 02:36:03 +00:00
|
|
|
const std::unordered_map< uint64_t,std::vector<uint8_t> >::const_iterator j(i->second.members.find(nodeId));
|
2017-04-27 07:59:36 +00:00
|
|
|
if (j == i->second.members.end())
|
|
|
|
return false;
|
2017-04-28 02:36:03 +00:00
|
|
|
memberConfig = nlohmann::json::from_msgpack(j->second);
|
2017-04-27 07:59:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-26 13:48:08 +00:00
|
|
|
void JSONDB::saveNetwork(const uint64_t networkId,const nlohmann::json &networkConfig)
|
2016-11-03 21:17:46 +00:00
|
|
|
{
|
2017-04-27 07:59:36 +00:00
|
|
|
char n[64];
|
2017-07-06 23:11:11 +00:00
|
|
|
OSUtils::ztsnprintf(n,sizeof(n),"network/%.16llx",(unsigned long long)networkId);
|
2017-04-26 13:48:08 +00:00
|
|
|
writeRaw(n,OSUtils::jsonDump(networkConfig));
|
2017-04-25 02:16:36 +00:00
|
|
|
{
|
2017-04-26 13:48:08 +00:00
|
|
|
Mutex::Lock _l(_networks_m);
|
2017-04-28 02:36:03 +00:00
|
|
|
_networks[networkId].config = nlohmann::json::to_msgpack(networkConfig);
|
2017-04-25 02:16:36 +00:00
|
|
|
}
|
2017-04-26 19:17:43 +00:00
|
|
|
_recomputeSummaryInfo(networkId);
|
2016-11-03 21:17:46 +00:00
|
|
|
}
|
|
|
|
|
2017-04-26 13:48:08 +00:00
|
|
|
void JSONDB::saveNetworkMember(const uint64_t networkId,const uint64_t nodeId,const nlohmann::json &memberConfig)
|
2016-11-03 21:17:46 +00:00
|
|
|
{
|
2017-04-26 13:48:08 +00:00
|
|
|
char n[256];
|
2017-07-06 23:11:11 +00:00
|
|
|
OSUtils::ztsnprintf(n,sizeof(n),"network/%.16llx/member/%.10llx",(unsigned long long)networkId,(unsigned long long)nodeId);
|
2017-04-26 13:48:08 +00:00
|
|
|
writeRaw(n,OSUtils::jsonDump(memberConfig));
|
|
|
|
{
|
|
|
|
Mutex::Lock _l(_networks_m);
|
2017-04-28 02:36:03 +00:00
|
|
|
_networks[networkId].members[nodeId] = nlohmann::json::to_msgpack(memberConfig);
|
2017-07-14 21:33:36 +00:00
|
|
|
_members[nodeId].insert(networkId);
|
2017-04-25 03:51:02 +00:00
|
|
|
}
|
2017-04-26 13:48:08 +00:00
|
|
|
_recomputeSummaryInfo(networkId);
|
|
|
|
}
|
2017-04-24 16:09:04 +00:00
|
|
|
|
2017-04-26 13:48:08 +00:00
|
|
|
nlohmann::json JSONDB::eraseNetwork(const uint64_t networkId)
|
|
|
|
{
|
|
|
|
if (!_httpAddr) { // Member deletion is done by Central in harnessed mode, and deleting the cache network entry also deletes all members
|
|
|
|
std::vector<uint64_t> memberIds;
|
|
|
|
{
|
|
|
|
Mutex::Lock _l(_networks_m);
|
2017-04-28 02:36:03 +00:00
|
|
|
const std::unordered_map<uint64_t,_NW>::iterator i(_networks.find(networkId));
|
2017-04-26 13:48:08 +00:00
|
|
|
if (i == _networks.end())
|
|
|
|
return _EMPTY_JSON;
|
2017-04-28 02:36:03 +00:00
|
|
|
for(std::unordered_map< uint64_t,std::vector<uint8_t> >::iterator m(i->second.members.begin());m!=i->second.members.end();++m)
|
2017-04-26 13:48:08 +00:00
|
|
|
memberIds.push_back(m->first);
|
|
|
|
}
|
|
|
|
for(std::vector<uint64_t>::iterator m(memberIds.begin());m!=memberIds.end();++m)
|
|
|
|
eraseNetworkMember(networkId,*m,false);
|
2017-04-25 02:16:36 +00:00
|
|
|
}
|
2016-11-03 21:17:46 +00:00
|
|
|
|
2017-04-26 13:48:08 +00:00
|
|
|
char n[256];
|
2017-07-06 23:11:11 +00:00
|
|
|
OSUtils::ztsnprintf(n,sizeof(n),"network/%.16llx",(unsigned long long)networkId);
|
2017-04-26 13:48:08 +00:00
|
|
|
|
2017-07-18 20:57:37 +00:00
|
|
|
if (_rawOutput >= 0) {
|
|
|
|
// In harnessed mode, deletes occur in Central or other management
|
|
|
|
// software and do not need to be executed this way.
|
|
|
|
} else if (_httpAddr) {
|
|
|
|
std::map<std::string,std::string> headers;
|
|
|
|
std::string body;
|
|
|
|
Http::DEL(0,ZT_JSONDB_HTTP_TIMEOUT,reinterpret_cast<const struct sockaddr *>(&_httpAddr),(_basePath+"/"+n).c_str(),_ZT_JSONDB_GET_HEADERS,headers,body);
|
2017-04-19 00:37:44 +00:00
|
|
|
} else {
|
|
|
|
const std::string path(_genPath(n,false));
|
2017-04-26 13:48:08 +00:00
|
|
|
if (path.length())
|
|
|
|
OSUtils::rm(path.c_str());
|
2017-04-19 00:37:44 +00:00
|
|
|
}
|
2016-11-03 21:17:46 +00:00
|
|
|
|
2017-04-25 03:51:02 +00:00
|
|
|
{
|
2017-04-26 13:48:08 +00:00
|
|
|
Mutex::Lock _l(_networks_m);
|
|
|
|
std::unordered_map<uint64_t,_NW>::iterator i(_networks.find(networkId));
|
|
|
|
if (i == _networks.end())
|
|
|
|
return _EMPTY_JSON; // sanity check, shouldn't happen
|
2017-04-28 02:36:03 +00:00
|
|
|
nlohmann::json tmp(nlohmann::json::from_msgpack(i->second.config));
|
2017-04-26 13:48:08 +00:00
|
|
|
_networks.erase(i);
|
|
|
|
return tmp;
|
2016-11-03 21:17:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-26 13:48:08 +00:00
|
|
|
nlohmann::json JSONDB::eraseNetworkMember(const uint64_t networkId,const uint64_t nodeId,bool recomputeSummaryInfo)
|
2016-11-04 22:18:31 +00:00
|
|
|
{
|
2017-04-26 13:48:08 +00:00
|
|
|
char n[256];
|
2017-07-06 23:11:11 +00:00
|
|
|
OSUtils::ztsnprintf(n,sizeof(n),"network/%.16llx/member/%.10llx",(unsigned long long)networkId,(unsigned long long)nodeId);
|
2017-04-26 13:48:08 +00:00
|
|
|
|
2017-07-18 20:57:37 +00:00
|
|
|
if (_rawOutput >= 0) {
|
|
|
|
// In harnessed mode, deletes occur in Central or other management
|
|
|
|
// software and do not need to be executed this way.
|
|
|
|
} else if (_httpAddr) {
|
|
|
|
std::map<std::string,std::string> headers;
|
|
|
|
std::string body;
|
|
|
|
Http::DEL(0,ZT_JSONDB_HTTP_TIMEOUT,reinterpret_cast<const struct sockaddr *>(&_httpAddr),(_basePath+"/"+n).c_str(),_ZT_JSONDB_GET_HEADERS,headers,body);
|
2017-04-26 13:48:08 +00:00
|
|
|
} else {
|
|
|
|
const std::string path(_genPath(n,false));
|
|
|
|
if (path.length())
|
|
|
|
OSUtils::rm(path.c_str());
|
|
|
|
}
|
|
|
|
|
2017-04-25 03:51:02 +00:00
|
|
|
{
|
2017-04-26 13:48:08 +00:00
|
|
|
Mutex::Lock _l(_networks_m);
|
2017-07-14 21:33:36 +00:00
|
|
|
_members[nodeId].erase(networkId);
|
2017-04-26 13:48:08 +00:00
|
|
|
std::unordered_map<uint64_t,_NW>::iterator i(_networks.find(networkId));
|
|
|
|
if (i == _networks.end())
|
|
|
|
return _EMPTY_JSON;
|
2017-04-28 02:36:03 +00:00
|
|
|
std::unordered_map< uint64_t,std::vector<uint8_t> >::iterator j(i->second.members.find(nodeId));
|
2017-04-26 13:48:08 +00:00
|
|
|
if (j == i->second.members.end())
|
|
|
|
return _EMPTY_JSON;
|
|
|
|
nlohmann::json tmp(j->second);
|
|
|
|
i->second.members.erase(j);
|
|
|
|
if (recomputeSummaryInfo)
|
|
|
|
_recomputeSummaryInfo(networkId);
|
|
|
|
return tmp;
|
2017-04-25 03:51:02 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-04 22:18:31 +00:00
|
|
|
|
2017-04-26 13:48:08 +00:00
|
|
|
void JSONDB::threadMain()
|
|
|
|
throw()
|
2017-04-25 03:51:02 +00:00
|
|
|
{
|
2017-07-18 20:57:37 +00:00
|
|
|
#ifndef __WINDOWS__
|
|
|
|
fd_set readfds,nullfds;
|
|
|
|
char *const readbuf = (_rawInput >= 0) ? (new char[1048576]) : (char *)0;
|
|
|
|
std::string rawInputBuf;
|
|
|
|
FD_ZERO(&readfds);
|
|
|
|
FD_ZERO(&nullfds);
|
|
|
|
#endif
|
|
|
|
|
2017-04-26 17:35:59 +00:00
|
|
|
std::vector<uint64_t> todo;
|
2017-07-18 20:57:37 +00:00
|
|
|
|
2017-04-26 17:35:59 +00:00
|
|
|
while (_summaryThreadRun) {
|
2017-07-18 20:57:37 +00:00
|
|
|
#ifndef __WINDOWS__
|
|
|
|
if (_rawInput < 0) {
|
|
|
|
Thread::sleep(25);
|
|
|
|
} else {
|
|
|
|
FD_SET(_rawInput,&readfds);
|
|
|
|
struct timeval tv;
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 25000;
|
|
|
|
select(_rawInput+1,&readfds,&nullfds,&nullfds,&tv);
|
|
|
|
if (FD_ISSET(_rawInput,&readfds)) {
|
|
|
|
const long rn = (long)read(_rawInput,readbuf,1048576);
|
|
|
|
for(long i=0;i<rn;++i) {
|
|
|
|
if (readbuf[i]) {
|
|
|
|
rawInputBuf.push_back(readbuf[i]);
|
|
|
|
} else if (rawInputBuf.length() > 0) {
|
|
|
|
_add(OSUtils::jsonParse(rawInputBuf));
|
|
|
|
rawInputBuf.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
Thread::sleep(25);
|
|
|
|
#endif
|
2017-04-26 17:35:59 +00:00
|
|
|
|
2017-04-26 13:48:08 +00:00
|
|
|
{
|
2017-04-26 17:35:59 +00:00
|
|
|
Mutex::Lock _l(_summaryThread_m);
|
|
|
|
if (_summaryThreadToDo.empty())
|
|
|
|
continue;
|
|
|
|
else _summaryThreadToDo.swap(todo);
|
|
|
|
}
|
|
|
|
const uint64_t now = OSUtils::now();
|
|
|
|
for(std::vector<uint64_t>::iterator ii(todo.begin());ii!=todo.end();++ii) {
|
|
|
|
const uint64_t networkId = *ii;
|
|
|
|
|
2017-04-26 13:48:08 +00:00
|
|
|
Mutex::Lock _l(_networks_m);
|
|
|
|
std::unordered_map<uint64_t,_NW>::iterator n(_networks.find(networkId));
|
|
|
|
if (n != _networks.end()) {
|
|
|
|
NetworkSummaryInfo &ns = n->second.summaryInfo;
|
|
|
|
ns.activeBridges.clear();
|
|
|
|
ns.allocatedIps.clear();
|
|
|
|
ns.authorizedMemberCount = 0;
|
|
|
|
ns.activeMemberCount = 0;
|
|
|
|
ns.totalMemberCount = 0;
|
|
|
|
ns.mostRecentDeauthTime = 0;
|
|
|
|
|
2017-04-28 02:36:03 +00:00
|
|
|
for(std::unordered_map< uint64_t,std::vector<uint8_t> >::const_iterator m(n->second.members.begin());m!=n->second.members.end();++m) {
|
2017-04-26 13:48:08 +00:00
|
|
|
try {
|
2017-04-28 02:36:03 +00:00
|
|
|
nlohmann::json member(nlohmann::json::from_msgpack(m->second));
|
|
|
|
|
|
|
|
if (OSUtils::jsonBool(member["authorized"],false)) {
|
2017-04-26 13:48:08 +00:00
|
|
|
++ns.authorizedMemberCount;
|
|
|
|
|
|
|
|
try {
|
2017-04-28 02:36:03 +00:00
|
|
|
const nlohmann::json &mlog = member["recentLog"];
|
2017-04-26 13:48:08 +00:00
|
|
|
if ((mlog.is_array())&&(mlog.size() > 0)) {
|
|
|
|
const nlohmann::json &mlog1 = mlog[0];
|
|
|
|
if (mlog1.is_object()) {
|
|
|
|
if ((now - OSUtils::jsonInt(mlog1["ts"],0ULL)) < (ZT_NETWORK_AUTOCONF_DELAY * 2))
|
|
|
|
++ns.activeMemberCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch ( ... ) {}
|
|
|
|
|
|
|
|
try {
|
2017-04-28 02:36:03 +00:00
|
|
|
if (OSUtils::jsonBool(member["activeBridge"],false))
|
2017-04-26 13:48:08 +00:00
|
|
|
ns.activeBridges.push_back(Address(m->first));
|
|
|
|
} catch ( ... ) {}
|
|
|
|
|
|
|
|
try {
|
2017-04-28 02:36:03 +00:00
|
|
|
const nlohmann::json &mips = member["ipAssignments"];
|
2017-04-26 13:48:08 +00:00
|
|
|
if (mips.is_array()) {
|
|
|
|
for(unsigned long i=0;i<mips.size();++i) {
|
2017-07-06 23:11:11 +00:00
|
|
|
InetAddress mip(OSUtils::jsonString(mips[i],"").c_str());
|
2017-04-26 13:48:08 +00:00
|
|
|
if ((mip.ss_family == AF_INET)||(mip.ss_family == AF_INET6))
|
|
|
|
ns.allocatedIps.push_back(mip);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch ( ... ) {}
|
|
|
|
} else {
|
|
|
|
try {
|
2017-04-28 02:36:03 +00:00
|
|
|
ns.mostRecentDeauthTime = std::max(ns.mostRecentDeauthTime,OSUtils::jsonInt(member["lastDeauthorizedTime"],0ULL));
|
2017-04-26 13:48:08 +00:00
|
|
|
} catch ( ... ) {}
|
|
|
|
}
|
|
|
|
++ns.totalMemberCount;
|
|
|
|
} catch ( ... ) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::sort(ns.activeBridges.begin(),ns.activeBridges.end());
|
|
|
|
std::sort(ns.allocatedIps.begin(),ns.allocatedIps.end());
|
|
|
|
|
|
|
|
n->second.summaryInfoLastComputed = now;
|
|
|
|
}
|
|
|
|
}
|
2017-04-26 17:35:59 +00:00
|
|
|
|
|
|
|
todo.clear();
|
2017-04-19 00:37:44 +00:00
|
|
|
}
|
2017-07-18 20:57:37 +00:00
|
|
|
|
|
|
|
#ifndef __WINDOWS__
|
|
|
|
delete [] readbuf;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JSONDB::_add(const nlohmann::json &j)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
if (j.is_object()) {
|
|
|
|
std::string id(OSUtils::jsonString(j["id"],"0"));
|
|
|
|
std::string objtype(OSUtils::jsonString(j["objtype"],""));
|
|
|
|
|
|
|
|
if ((id.length() == 16)&&(objtype == "network")) {
|
|
|
|
const uint64_t nwid = Utils::hexStrToU64(id.c_str());
|
|
|
|
if (nwid) {
|
|
|
|
Mutex::Lock _l(_networks_m);
|
|
|
|
_networks[nwid].config = nlohmann::json::to_msgpack(j);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if ((id.length() == 10)&&(objtype == "member")) {
|
|
|
|
const uint64_t mid = Utils::hexStrToU64(id.c_str());
|
|
|
|
const uint64_t nwid = Utils::hexStrToU64(OSUtils::jsonString(j["nwid"],"0").c_str());
|
|
|
|
if ((mid)&&(nwid)) {
|
|
|
|
Mutex::Lock _l(_networks_m);
|
|
|
|
_networks[nwid].members[mid] = nlohmann::json::to_msgpack(j);
|
|
|
|
_members[mid].insert(nwid);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch ( ... ) {}
|
|
|
|
return false;
|
2016-11-04 22:18:31 +00:00
|
|
|
}
|
|
|
|
|
2017-04-26 13:48:08 +00:00
|
|
|
bool JSONDB::_load(const std::string &p)
|
2016-11-04 22:18:31 +00:00
|
|
|
{
|
2017-07-18 20:57:37 +00:00
|
|
|
// This is not used in stdin/stdout mode. Instead data is populated by
|
|
|
|
// sending it all to stdin.
|
|
|
|
|
2017-04-19 00:37:44 +00:00
|
|
|
if (_httpAddr) {
|
2017-04-28 02:36:03 +00:00
|
|
|
// In HTTP harnessed mode we download our entire working data set on startup.
|
|
|
|
|
2017-04-19 00:37:44 +00:00
|
|
|
std::string body;
|
|
|
|
std::map<std::string,std::string> headers;
|
2017-05-02 19:08:53 +00:00
|
|
|
const unsigned int sc = Http::GET(0,ZT_JSONDB_HTTP_TIMEOUT,reinterpret_cast<const struct sockaddr *>(&_httpAddr),_basePath.c_str(),_ZT_JSONDB_GET_HEADERS,headers,body);
|
2017-04-19 00:37:44 +00:00
|
|
|
if (sc == 200) {
|
|
|
|
try {
|
|
|
|
nlohmann::json dbImg(OSUtils::jsonParse(body));
|
|
|
|
std::string tmp;
|
|
|
|
if (dbImg.is_object()) {
|
2017-04-26 13:48:08 +00:00
|
|
|
Mutex::Lock _l(_networks_m);
|
2017-04-19 00:37:44 +00:00
|
|
|
for(nlohmann::json::iterator i(dbImg.begin());i!=dbImg.end();++i) {
|
2017-07-18 20:57:37 +00:00
|
|
|
try {
|
|
|
|
_add(i.value());
|
|
|
|
} catch ( ... ) {}
|
2017-04-19 00:37:44 +00:00
|
|
|
}
|
2017-04-26 13:48:08 +00:00
|
|
|
return true;
|
2017-04-19 00:37:44 +00:00
|
|
|
}
|
2017-04-24 16:09:04 +00:00
|
|
|
} catch ( ... ) {} // invalid JSON, so maybe incomplete request
|
2017-04-19 00:37:44 +00:00
|
|
|
}
|
2017-04-26 13:48:08 +00:00
|
|
|
return false;
|
2017-04-28 02:36:03 +00:00
|
|
|
|
2017-04-19 00:37:44 +00:00
|
|
|
} else {
|
2017-04-28 02:36:03 +00:00
|
|
|
// In regular mode we recursively read it from controller.d/ on disk
|
|
|
|
|
2017-04-19 00:37:44 +00:00
|
|
|
std::vector<std::string> dl(OSUtils::listDirectory(p.c_str(),true));
|
|
|
|
for(std::vector<std::string>::const_iterator di(dl.begin());di!=dl.end();++di) {
|
|
|
|
if ((di->length() > 5)&&(di->substr(di->length() - 5) == ".json")) {
|
2017-04-26 13:48:08 +00:00
|
|
|
std::string buf;
|
|
|
|
if (OSUtils::readFile((p + ZT_PATH_SEPARATOR_S + *di).c_str(),buf)) {
|
|
|
|
try {
|
2017-07-18 20:57:37 +00:00
|
|
|
_add(OSUtils::jsonParse(buf));
|
2017-04-26 13:48:08 +00:00
|
|
|
} catch ( ... ) {}
|
|
|
|
}
|
2017-04-19 00:37:44 +00:00
|
|
|
} else {
|
2017-04-26 13:48:08 +00:00
|
|
|
this->_load((p + ZT_PATH_SEPARATOR_S + *di));
|
2017-04-19 00:37:44 +00:00
|
|
|
}
|
2016-11-04 22:18:31 +00:00
|
|
|
}
|
2017-04-26 13:48:08 +00:00
|
|
|
return true;
|
2017-04-28 02:36:03 +00:00
|
|
|
|
2016-11-04 22:18:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-26 13:48:08 +00:00
|
|
|
void JSONDB::_recomputeSummaryInfo(const uint64_t networkId)
|
|
|
|
{
|
|
|
|
Mutex::Lock _l(_summaryThread_m);
|
2017-04-26 17:35:59 +00:00
|
|
|
if (std::find(_summaryThreadToDo.begin(),_summaryThreadToDo.end(),networkId) == _summaryThreadToDo.end())
|
|
|
|
_summaryThreadToDo.push_back(networkId);
|
2017-04-26 13:48:08 +00:00
|
|
|
if (!_summaryThread)
|
|
|
|
_summaryThread = Thread::start(this);
|
|
|
|
}
|
|
|
|
|
2016-11-03 21:17:46 +00:00
|
|
|
std::string JSONDB::_genPath(const std::string &n,bool create)
|
|
|
|
{
|
2016-11-18 23:49:28 +00:00
|
|
|
std::vector<std::string> pt(OSUtils::split(n.c_str(),"/","",""));
|
2016-11-03 21:17:46 +00:00
|
|
|
if (pt.size() == 0)
|
|
|
|
return std::string();
|
|
|
|
|
2017-04-19 00:37:44 +00:00
|
|
|
char sep;
|
|
|
|
if (_httpAddr) {
|
|
|
|
sep = '/';
|
|
|
|
create = false;
|
|
|
|
} else {
|
|
|
|
sep = ZT_PATH_SEPARATOR;
|
|
|
|
}
|
|
|
|
|
2016-11-03 21:17:46 +00:00
|
|
|
std::string p(_basePath);
|
|
|
|
if (create) OSUtils::mkdir(p.c_str());
|
2017-01-13 23:19:59 +00:00
|
|
|
for(unsigned long i=0,j=(unsigned long)(pt.size()-1);i<j;++i) {
|
2017-04-19 00:37:44 +00:00
|
|
|
p.push_back(sep);
|
2016-11-03 21:17:46 +00:00
|
|
|
p.append(pt[i]);
|
|
|
|
if (create) OSUtils::mkdir(p.c_str());
|
|
|
|
}
|
|
|
|
|
2017-04-19 00:37:44 +00:00
|
|
|
p.push_back(sep);
|
2016-11-03 21:17:46 +00:00
|
|
|
p.append(pt[pt.size()-1]);
|
|
|
|
p.append(".json");
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|