2017-11-07 22:44:46 +00:00
|
|
|
/*
|
|
|
|
* ZeroTier One - Network Virtualization Everywhere
|
2019-01-14 18:25:53 +00:00
|
|
|
* Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
|
2017-11-07 22:44:46 +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
|
2019-01-14 18:25:53 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* --
|
|
|
|
*
|
|
|
|
* You can be released from the requirements of the license by purchasing
|
|
|
|
* a commercial license. Buying such a license is mandatory as soon as you
|
|
|
|
* develop commercial closed-source software that incorporates or links
|
|
|
|
* directly against ZeroTier software without disclosing the source code
|
|
|
|
* of your own application.
|
2017-11-07 22:44:46 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "FileDB.hpp"
|
|
|
|
|
|
|
|
namespace ZeroTier
|
|
|
|
{
|
|
|
|
|
2019-08-06 15:42:54 +00:00
|
|
|
FileDB::FileDB(const char *path) :
|
|
|
|
DB(),
|
|
|
|
_path(path),
|
2018-01-10 23:03:39 +00:00
|
|
|
_networksPath(_path + ZT_PATH_SEPARATOR_S + "network"),
|
2019-07-15 20:27:53 +00:00
|
|
|
_tracePath(_path + ZT_PATH_SEPARATOR_S + "trace"),
|
|
|
|
_running(true)
|
2017-11-07 22:44:46 +00:00
|
|
|
{
|
|
|
|
OSUtils::mkdir(_path.c_str());
|
|
|
|
OSUtils::lockDownFile(_path.c_str(),true);
|
2018-01-10 23:03:39 +00:00
|
|
|
OSUtils::mkdir(_networksPath.c_str());
|
|
|
|
OSUtils::mkdir(_tracePath.c_str());
|
2017-11-07 22:44:46 +00:00
|
|
|
|
|
|
|
std::vector<std::string> networks(OSUtils::listDirectory(_networksPath.c_str(),false));
|
|
|
|
std::string buf;
|
|
|
|
for(auto n=networks.begin();n!=networks.end();++n) {
|
|
|
|
buf.clear();
|
|
|
|
if ((n->length() == 21)&&(OSUtils::readFile((_networksPath + ZT_PATH_SEPARATOR_S + *n).c_str(),buf))) {
|
|
|
|
try {
|
|
|
|
nlohmann::json network(OSUtils::jsonParse(buf));
|
|
|
|
const std::string nwids = network["id"];
|
|
|
|
if (nwids.length() == 16) {
|
|
|
|
nlohmann::json nullJson;
|
|
|
|
_networkChanged(nullJson,network,false);
|
|
|
|
std::string membersPath(_networksPath + ZT_PATH_SEPARATOR_S + nwids + ZT_PATH_SEPARATOR_S "member");
|
|
|
|
std::vector<std::string> members(OSUtils::listDirectory(membersPath.c_str(),false));
|
|
|
|
for(auto m=members.begin();m!=members.end();++m) {
|
|
|
|
buf.clear();
|
|
|
|
if ((m->length() == 15)&&(OSUtils::readFile((membersPath + ZT_PATH_SEPARATOR_S + *m).c_str(),buf))) {
|
|
|
|
try {
|
|
|
|
nlohmann::json member(OSUtils::jsonParse(buf));
|
|
|
|
const std::string addrs = member["id"];
|
|
|
|
if (addrs.length() == 10) {
|
|
|
|
nlohmann::json nullJson2;
|
|
|
|
_memberChanged(nullJson2,member,false);
|
|
|
|
}
|
|
|
|
} catch ( ... ) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch ( ... ) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-15 20:27:53 +00:00
|
|
|
FileDB::~FileDB()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
_online_l.lock();
|
|
|
|
_running = false;
|
|
|
|
_online_l.unlock();
|
|
|
|
_onlineUpdateThread.join();
|
|
|
|
} catch ( ... ) {}
|
|
|
|
}
|
2017-11-07 22:44:46 +00:00
|
|
|
|
2018-07-11 17:42:31 +00:00
|
|
|
bool FileDB::waitForReady() { return true; }
|
|
|
|
bool FileDB::isReady() { return true; }
|
2017-11-07 22:44:46 +00:00
|
|
|
|
2019-08-06 15:42:54 +00:00
|
|
|
bool FileDB::save(nlohmann::json &record,bool notifyListeners)
|
2017-11-07 22:44:46 +00:00
|
|
|
{
|
2017-12-07 22:42:33 +00:00
|
|
|
char p1[4096],p2[4096],pb[4096];
|
2019-08-06 15:42:54 +00:00
|
|
|
bool modified = false;
|
2017-11-07 22:44:46 +00:00
|
|
|
try {
|
2018-01-10 22:31:28 +00:00
|
|
|
const std::string objtype = record["objtype"];
|
2017-11-07 22:44:46 +00:00
|
|
|
if (objtype == "network") {
|
2019-08-06 13:51:23 +00:00
|
|
|
|
2018-01-10 22:31:28 +00:00
|
|
|
const uint64_t nwid = OSUtils::jsonIntHex(record["id"],0ULL);
|
2017-11-07 22:44:46 +00:00
|
|
|
if (nwid) {
|
|
|
|
nlohmann::json old;
|
|
|
|
get(nwid,old);
|
2019-08-08 20:29:13 +00:00
|
|
|
if ((!old.is_object())||(!_compareRecords(old,record))) {
|
2019-08-06 13:51:23 +00:00
|
|
|
record["revision"] = OSUtils::jsonInt(record["revision"],0ULL) + 1ULL;
|
2019-07-15 20:27:53 +00:00
|
|
|
OSUtils::ztsnprintf(p1,sizeof(p1),"%s" ZT_PATH_SEPARATOR_S "%.16llx.json",_networksPath.c_str(),nwid);
|
2018-07-03 19:51:41 +00:00
|
|
|
if (!OSUtils::writeFile(p1,OSUtils::jsonDump(record,-1)))
|
|
|
|
fprintf(stderr,"WARNING: controller unable to write to path: %s" ZT_EOL_S,p1);
|
2019-08-06 15:42:54 +00:00
|
|
|
_networkChanged(old,record,notifyListeners);
|
|
|
|
modified = true;
|
2018-07-03 19:51:41 +00:00
|
|
|
}
|
2017-11-07 22:44:46 +00:00
|
|
|
}
|
2019-08-06 13:51:23 +00:00
|
|
|
|
2017-11-07 22:44:46 +00:00
|
|
|
} else if (objtype == "member") {
|
2019-08-06 13:51:23 +00:00
|
|
|
|
2018-01-10 22:31:28 +00:00
|
|
|
const uint64_t id = OSUtils::jsonIntHex(record["id"],0ULL);
|
|
|
|
const uint64_t nwid = OSUtils::jsonIntHex(record["nwid"],0ULL);
|
2017-11-07 22:44:46 +00:00
|
|
|
if ((id)&&(nwid)) {
|
|
|
|
nlohmann::json network,old;
|
|
|
|
get(nwid,network,id,old);
|
2019-08-08 20:29:13 +00:00
|
|
|
if ((!old.is_object())||(!_compareRecords(old,record))) {
|
2019-08-06 13:51:23 +00:00
|
|
|
record["revision"] = OSUtils::jsonInt(record["revision"],0ULL) + 1ULL;
|
2018-07-03 19:51:41 +00:00
|
|
|
OSUtils::ztsnprintf(pb,sizeof(pb),"%s" ZT_PATH_SEPARATOR_S "%.16llx" ZT_PATH_SEPARATOR_S "member",_networksPath.c_str(),(unsigned long long)nwid);
|
2019-07-15 20:27:53 +00:00
|
|
|
OSUtils::ztsnprintf(p1,sizeof(p1),"%s" ZT_PATH_SEPARATOR_S "%.10llx.json",pb,(unsigned long long)id);
|
2018-07-03 19:51:41 +00:00
|
|
|
if (!OSUtils::writeFile(p1,OSUtils::jsonDump(record,-1))) {
|
|
|
|
OSUtils::ztsnprintf(p2,sizeof(p2),"%s" ZT_PATH_SEPARATOR_S "%.16llx",_networksPath.c_str(),(unsigned long long)nwid);
|
|
|
|
OSUtils::mkdir(p2);
|
|
|
|
OSUtils::mkdir(pb);
|
|
|
|
if (!OSUtils::writeFile(p1,OSUtils::jsonDump(record,-1)))
|
|
|
|
fprintf(stderr,"WARNING: controller unable to write to path: %s" ZT_EOL_S,p1);
|
|
|
|
}
|
2019-08-06 15:42:54 +00:00
|
|
|
_memberChanged(old,record,notifyListeners);
|
|
|
|
modified = true;
|
2018-07-03 19:51:41 +00:00
|
|
|
}
|
2017-11-07 22:44:46 +00:00
|
|
|
}
|
2019-08-06 13:51:23 +00:00
|
|
|
|
2017-11-07 22:44:46 +00:00
|
|
|
}
|
|
|
|
} catch ( ... ) {} // drop invalid records missing fields
|
2019-08-06 15:42:54 +00:00
|
|
|
return modified;
|
2017-11-07 22:44:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileDB::eraseNetwork(const uint64_t networkId)
|
|
|
|
{
|
|
|
|
nlohmann::json network,nullJson;
|
|
|
|
get(networkId,network);
|
|
|
|
char p[16384];
|
|
|
|
OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "%.16llx.json",_networksPath.c_str(),networkId);
|
2019-07-15 20:27:53 +00:00
|
|
|
OSUtils::rm(p);
|
|
|
|
OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "%.16llx" ZT_PATH_SEPARATOR_S "member",_networksPath.c_str(),(unsigned long long)networkId);
|
|
|
|
OSUtils::rmDashRf(p);
|
2017-11-07 22:44:46 +00:00
|
|
|
_networkChanged(network,nullJson,true);
|
2019-07-15 20:27:53 +00:00
|
|
|
std::lock_guard<std::mutex> l(this->_online_l);
|
|
|
|
this->_online.erase(networkId);
|
2017-11-07 22:44:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileDB::eraseMember(const uint64_t networkId,const uint64_t memberId)
|
|
|
|
{
|
2018-08-02 21:13:55 +00:00
|
|
|
nlohmann::json network,member,nullJson;
|
|
|
|
get(networkId,network);
|
2019-07-15 20:27:53 +00:00
|
|
|
get(memberId,member);
|
|
|
|
char p[4096];
|
2018-08-02 21:13:55 +00:00
|
|
|
OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "%.16llx" ZT_PATH_SEPARATOR_S "member" ZT_PATH_SEPARATOR_S "%.10llx.json",_networksPath.c_str(),networkId,memberId);
|
2019-07-15 20:27:53 +00:00
|
|
|
OSUtils::rm(p);
|
2018-08-02 21:13:55 +00:00
|
|
|
_memberChanged(member,nullJson,true);
|
2019-07-15 20:27:53 +00:00
|
|
|
std::lock_guard<std::mutex> l(this->_online_l);
|
|
|
|
this->_online[networkId].erase(memberId);
|
2017-11-07 22:44:46 +00:00
|
|
|
}
|
|
|
|
|
2017-11-09 01:19:46 +00:00
|
|
|
void FileDB::nodeIsOnline(const uint64_t networkId,const uint64_t memberId,const InetAddress &physicalAddress)
|
2017-11-08 19:06:14 +00:00
|
|
|
{
|
2019-07-15 20:27:53 +00:00
|
|
|
char mid[32],atmp[64];
|
|
|
|
OSUtils::ztsnprintf(mid,sizeof(mid),"%.10llx",(unsigned long long)memberId);
|
|
|
|
physicalAddress.toString(atmp);
|
2019-08-06 15:42:54 +00:00
|
|
|
std::lock_guard<std::mutex> l(this->_online_l);
|
|
|
|
this->_online[networkId][memberId][OSUtils::now()] = physicalAddress;
|
2017-11-08 19:06:14 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 22:44:46 +00:00
|
|
|
} // namespace ZeroTier
|