ZeroTierOne/controller/LFDB.cpp

403 lines
14 KiB
C++
Raw Normal View History

2019-07-22 20:43:06 +00:00
/*
* ZeroTier One - Network Virtualization Everywhere
* Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
*
* 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/>.
*
* --
*
* 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.
*/
#include "LFDB.hpp"
#include <thread>
2019-07-23 16:29:08 +00:00
#include <chrono>
2019-07-22 20:43:06 +00:00
#include <iostream>
#include <sstream>
#include "../osdep/OSUtils.hpp"
#include "../ext/cpp-httplib/httplib.h"
namespace ZeroTier
{
LFDB::LFDB(const Identity &myId,const char *path,const char *lfOwnerPrivate,const char *lfOwnerPublic,const char *lfNodeHost,int lfNodePort,bool storeOnlineState) :
2019-08-06 15:42:54 +00:00
DB(),
2019-07-22 20:43:06 +00:00
_myId(myId),
2019-07-23 23:06:35 +00:00
_lfOwnerPrivate((lfOwnerPrivate) ? lfOwnerPrivate : ""),
_lfOwnerPublic((lfOwnerPublic) ? lfOwnerPublic : ""),
_lfNodeHost((lfNodeHost) ? lfNodeHost : "127.0.0.1"),
_lfNodePort(((lfNodePort > 0)&&(lfNodePort < 65536)) ? lfNodePort : 9980),
2019-07-22 20:43:06 +00:00
_running(true),
_ready(false),
_storeOnlineState(storeOnlineState)
{
_syncThread = std::thread([this]() {
char controllerAddress[24];
2019-07-23 23:06:35 +00:00
const uint64_t controllerAddressInt = _myId.address().toInt();
2019-07-22 20:43:06 +00:00
_myId.address().toString(controllerAddress);
2019-07-23 23:06:35 +00:00
std::string networksSelectorName("com.zerotier.controller.lfdb:"); networksSelectorName.append(controllerAddress); networksSelectorName.append("/network");
std::string membersSelectorName("com.zerotier.controller.lfdb:"); membersSelectorName.append(controllerAddress); membersSelectorName.append("/member");
2019-07-22 20:43:06 +00:00
2019-07-27 00:44:05 +00:00
// LF record masking key is the first 32 bytes of SHA512(controller private key) in hex,
// hiding record values from anything but the controller or someone who has its key.
uint8_t sha512pk[64];
_myId.sha512PrivateKey(sha512pk);
char maskingKey [128];
Utils::hex(sha512pk,32,maskingKey);
2019-07-22 20:43:06 +00:00
httplib::Client htcli(_lfNodeHost.c_str(),_lfNodePort,600);
2019-07-23 16:29:08 +00:00
int64_t timeRangeStart = 0;
2019-08-05 20:30:03 +00:00
while (_running.load()) {
2019-07-23 23:06:35 +00:00
{
std::lock_guard<std::mutex> sl(_state_l);
for(auto ns=_state.begin();ns!=_state.end();++ns) {
if (ns->second.dirty) {
nlohmann::json network;
if (get(ns->first,network)) {
2019-07-26 23:03:21 +00:00
nlohmann::json newrec,selector0;
selector0["Name"] = networksSelectorName;
selector0["Ordinal"] = ns->first;
newrec["Selectors"].push_back(selector0);
2019-07-23 23:06:35 +00:00
newrec["Value"] = network.dump();
newrec["OwnerPrivate"] = _lfOwnerPrivate;
2019-07-27 00:44:05 +00:00
newrec["MaskingKey"] = maskingKey;
2019-07-26 23:03:21 +00:00
newrec["PulseIfUnchanged"] = true;
auto resp = htcli.Post("/makerecord",newrec.dump(),"application/json");
if (resp) {
if (resp->status == 200) {
ns->second.dirty = false;
2019-08-06 18:20:37 +00:00
//printf("SET network %.16llx %s\n",ns->first,resp->body.c_str());
2019-07-26 23:03:21 +00:00
} else {
fprintf(stderr,"ERROR: LFDB: %d from node (create/update network): %s" ZT_EOL_S,resp->status,resp->body.c_str());
}
2019-07-23 23:06:35 +00:00
} else {
2019-07-26 23:03:21 +00:00
fprintf(stderr,"ERROR: LFDB: node is offline" ZT_EOL_S);
2019-07-23 23:06:35 +00:00
}
}
}
for(auto ms=ns->second.members.begin();ms!=ns->second.members.end();++ms) {
2019-07-26 23:03:21 +00:00
if ((_storeOnlineState)&&(ms->second.lastOnlineDirty)&&(ms->second.lastOnlineAddress)) {
nlohmann::json newrec,selector0,selector1,selectors,ip;
2019-07-26 23:03:21 +00:00
char tmp[1024],tmp2[128];
OSUtils::ztsnprintf(tmp,sizeof(tmp),"com.zerotier.controller.lfdb:%s/network/%.16llx/online",controllerAddress,(unsigned long long)ns->first);
ms->second.lastOnlineAddress.toIpString(tmp2);
selector0["Name"] = tmp;
selector0["Ordinal"] = ms->first;
selector1["Name"] = tmp2;
selector1["Ordinal"] = 0;
selectors.push_back(selector0);
selectors.push_back(selector1);
newrec["Selectors"] = selectors;
const uint8_t *const rawip = (const uint8_t *)ms->second.lastOnlineAddress.rawIpData();
switch(ms->second.lastOnlineAddress.ss_family) {
case AF_INET:
for(int j=0;j<4;++j)
ip.push_back((unsigned int)rawip[j]);
break;
case AF_INET6:
for(int j=0;j<16;++j)
ip.push_back((unsigned int)rawip[j]);
break;
default:
ip = tmp2; // should never happen since only IP transport is currently supported
break;
}
newrec["Value"] = ip;
2019-07-26 23:03:21 +00:00
newrec["OwnerPrivate"] = _lfOwnerPrivate;
2019-07-27 00:44:05 +00:00
newrec["MaskingKey"] = maskingKey;
2019-07-26 23:03:21 +00:00
newrec["Timestamp"] = ms->second.lastOnlineTime;
newrec["PulseIfUnchanged"] = true;
auto resp = htcli.Post("/makerecord",newrec.dump(),"application/json");
if (resp) {
if (resp->status == 200) {
ms->second.lastOnlineDirty = false;
2019-08-06 18:20:37 +00:00
//printf("SET member online %.16llx %.10llx %s\n",ns->first,ms->first,resp->body.c_str());
2019-07-26 23:03:21 +00:00
} else {
fprintf(stderr,"ERROR: LFDB: %d from node (create/update member online status): %s" ZT_EOL_S,resp->status,resp->body.c_str());
2019-07-26 23:03:21 +00:00
}
} else {
fprintf(stderr,"ERROR: LFDB: node is offline" ZT_EOL_S);
}
2019-07-23 23:06:35 +00:00
}
if (ms->second.dirty) {
nlohmann::json network,member;
if (get(ns->first,network,ms->first,member)) {
2019-07-26 23:03:21 +00:00
nlohmann::json newrec,selector0,selector1,selectors;
selector0["Name"] = networksSelectorName;
selector0["Ordinal"] = ns->first;
selector1["Name"] = membersSelectorName;
selector1["Ordinal"] = ms->first;
selectors.push_back(selector0);
selectors.push_back(selector1);
newrec["Selectors"] = selectors;
2019-07-23 23:06:35 +00:00
newrec["Value"] = member.dump();
newrec["OwnerPrivate"] = _lfOwnerPrivate;
2019-07-27 00:44:05 +00:00
newrec["MaskingKey"] = maskingKey;
2019-07-26 23:03:21 +00:00
newrec["PulseIfUnchanged"] = true;
auto resp = htcli.Post("/makerecord",newrec.dump(),"application/json");
if (resp) {
if (resp->status == 200) {
ms->second.dirty = false;
2019-08-06 18:20:37 +00:00
//printf("SET member %.16llx %.10llx %s\n",ns->first,ms->first,resp->body.c_str());
2019-07-26 23:03:21 +00:00
} else {
fprintf(stderr,"ERROR: LFDB: %d from node (create/update member): %s" ZT_EOL_S,resp->status,resp->body.c_str());
}
2019-07-23 23:06:35 +00:00
} else {
2019-07-26 23:03:21 +00:00
fprintf(stderr,"ERROR: LFDB: node is offline" ZT_EOL_S);
2019-07-23 23:06:35 +00:00
}
}
}
}
}
}
2019-07-26 23:03:21 +00:00
{
std::ostringstream query;
query
<< '{'
<< "\"Ranges\":[{"
<< "\"Name\":\"" << networksSelectorName << "\","
<< "\"Range\":[0,18446744073709551615]"
<< "}],"
2019-08-05 20:30:03 +00:00
<< "\"TimeRange\":[" << timeRangeStart << ",9223372036854775807],"
2019-07-27 00:44:05 +00:00
<< "\"MaskingKey\":\"" << maskingKey << "\","
2019-07-26 23:03:21 +00:00
<< "\"Owners\":[\"" << _lfOwnerPublic << "\"]"
<< '}';
auto resp = htcli.Post("/query",query.str(),"application/json");
if (resp) {
if (resp->status == 200) {
nlohmann::json results(OSUtils::jsonParse(resp->body));
if ((results.is_array())&&(results.size() > 0)) {
for(std::size_t ri=0;ri<results.size();++ri) {
nlohmann::json &rset = results[ri];
if ((rset.is_array())&&(rset.size() > 0)) {
2019-08-05 20:30:03 +00:00
2019-07-26 23:03:21 +00:00
nlohmann::json &result = rset[0];
if (result.is_object()) {
nlohmann::json &record = result["Record"];
if (record.is_object()) {
const std::string recordValue = result["Value"];
2019-08-06 18:20:37 +00:00
//printf("GET network %s\n",recordValue.c_str());
2019-07-26 23:03:21 +00:00
nlohmann::json network(OSUtils::jsonParse(recordValue));
if (network.is_object()) {
const std::string idstr = network["id"];
const uint64_t id = Utils::hexStrToU64(idstr.c_str());
if ((id >> 24) == controllerAddressInt) { // sanity check
2019-07-26 23:03:21 +00:00
std::lock_guard<std::mutex> sl(_state_l);
_NetworkState &ns = _state[id];
if (!ns.dirty) {
nlohmann::json oldNetwork;
2019-08-05 20:30:03 +00:00
if ((timeRangeStart > 0)&&(get(id,oldNetwork))) {
const uint64_t revision = network["revision"];
const uint64_t prevRevision = oldNetwork["revision"];
if (prevRevision < revision) {
_networkChanged(oldNetwork,network,timeRangeStart > 0);
}
} else {
nlohmann::json nullJson;
_networkChanged(nullJson,network,timeRangeStart > 0);
}
2019-07-26 23:03:21 +00:00
}
2019-07-26 23:03:21 +00:00
}
2019-07-23 23:06:35 +00:00
}
}
2019-07-23 16:29:08 +00:00
}
2019-08-05 20:30:03 +00:00
2019-07-23 16:29:08 +00:00
}
}
}
2019-07-26 23:03:21 +00:00
} else {
fprintf(stderr,"ERROR: LFDB: %d from node: %s" ZT_EOL_S,resp->status,resp->body.c_str());
2019-07-23 16:29:08 +00:00
}
2019-07-26 23:03:21 +00:00
} else {
fprintf(stderr,"ERROR: LFDB: node is offline" ZT_EOL_S);
2019-07-23 16:29:08 +00:00
}
2019-07-22 20:43:06 +00:00
}
2019-07-26 23:03:21 +00:00
{
std::ostringstream query;
query
<< '{'
<< "\"Ranges\":[{"
<< "\"Name\":\"" << networksSelectorName << "\","
<< "\"Range\":[0,18446744073709551615]"
<< "},{"
<< "\"Name\":\"" << membersSelectorName << "\","
<< "\"Range\":[0,18446744073709551615]"
<< "}],"
2019-08-05 20:30:03 +00:00
<< "\"TimeRange\":[" << timeRangeStart << ",9223372036854775807],"
2019-07-27 00:44:05 +00:00
<< "\"MaskingKey\":\"" << maskingKey << "\","
2019-07-26 23:03:21 +00:00
<< "\"Owners\":[\"" << _lfOwnerPublic << "\"]"
<< '}';
auto resp = htcli.Post("/query",query.str(),"application/json");
if (resp) {
if (resp->status == 200) {
nlohmann::json results(OSUtils::jsonParse(resp->body));
if ((results.is_array())&&(results.size() > 0)) {
for(std::size_t ri=0;ri<results.size();++ri) {
nlohmann::json &rset = results[ri];
if ((rset.is_array())&&(rset.size() > 0)) {
2019-08-05 20:30:03 +00:00
2019-07-26 23:03:21 +00:00
nlohmann::json &result = rset[0];
if (result.is_object()) {
nlohmann::json &record = result["Record"];
if (record.is_object()) {
const std::string recordValue = result["Value"];
2019-08-06 18:20:37 +00:00
//printf("GET member %s\n",recordValue.c_str());
2019-07-26 23:03:21 +00:00
nlohmann::json member(OSUtils::jsonParse(recordValue));
if (member.is_object()) {
const std::string nwidstr = member["nwid"];
const std::string idstr = member["id"];
const uint64_t nwid = Utils::hexStrToU64(nwidstr.c_str());
const uint64_t id = Utils::hexStrToU64(idstr.c_str());
if ((id)&&((nwid >> 24) == controllerAddressInt)) { // sanity check
2019-07-26 23:03:21 +00:00
std::lock_guard<std::mutex> sl(_state_l);
auto ns = _state.find(nwid);
if ((ns == _state.end())||(!ns->second.members[id].dirty)) {
nlohmann::json network,oldMember;
2019-08-05 20:30:03 +00:00
if ((timeRangeStart > 0)&&(get(nwid,network,id,oldMember))) {
const uint64_t revision = member["revision"];
const uint64_t prevRevision = oldMember["revision"];
if (prevRevision < revision)
_memberChanged(oldMember,member,timeRangeStart > 0);
2019-07-26 23:03:21 +00:00
}
} else {
nlohmann::json nullJson;
_memberChanged(nullJson,member,timeRangeStart > 0);
2019-07-26 23:03:21 +00:00
}
2019-07-23 23:06:35 +00:00
}
}
}
2019-07-23 16:29:08 +00:00
}
2019-08-05 20:30:03 +00:00
2019-07-23 16:29:08 +00:00
}
}
}
2019-07-26 23:03:21 +00:00
} else {
fprintf(stderr,"ERROR: LFDB: %d from node: %s" ZT_EOL_S,resp->status,resp->body.c_str());
2019-07-23 16:29:08 +00:00
}
2019-07-26 23:03:21 +00:00
} else {
fprintf(stderr,"ERROR: LFDB: node is offline" ZT_EOL_S);
2019-07-23 16:29:08 +00:00
}
}
timeRangeStart = time(nullptr) - 120; // start next query 2m before now to avoid losing updates
2019-08-05 20:30:03 +00:00
_ready.store(true);
2019-07-22 20:43:06 +00:00
2019-08-05 20:30:03 +00:00
for(int k=0;k<4;++k) { // 2s delay between queries for remotely modified networks or members
if (!_running.load())
2019-07-22 20:43:06 +00:00
return;
2019-08-05 20:30:03 +00:00
std::this_thread::sleep_for(std::chrono::milliseconds(500));
2019-07-22 20:43:06 +00:00
}
}
});
}
LFDB::~LFDB()
{
2019-08-05 20:30:03 +00:00
_running.store(false);
2019-07-22 20:43:06 +00:00
_syncThread.join();
}
bool LFDB::waitForReady()
{
2019-08-05 20:30:03 +00:00
while (!_ready.load()) {
std::this_thread::sleep_for(std::chrono::milliseconds(500));
2019-07-23 16:29:08 +00:00
}
2019-07-23 16:30:40 +00:00
return true;
2019-07-22 20:43:06 +00:00
}
bool LFDB::isReady()
{
2019-08-05 20:30:03 +00:00
return (_ready.load());
2019-07-22 20:43:06 +00:00
}
2019-08-06 15:42:54 +00:00
bool LFDB::save(nlohmann::json &record,bool notifyListeners)
2019-07-22 20:43:06 +00:00
{
2019-08-06 15:42:54 +00:00
bool modified = false;
2019-07-22 20:43:06 +00:00
const std::string objtype = record["objtype"];
if (objtype == "network") {
const uint64_t nwid = OSUtils::jsonIntHex(record["id"],0ULL);
if (nwid) {
nlohmann::json old;
get(nwid,old);
if ((!old.is_object())||(old != record)) {
record["revision"] = OSUtils::jsonInt(record["revision"],0ULL) + 1ULL;
2019-08-06 15:42:54 +00:00
_networkChanged(old,record,notifyListeners);
2019-07-23 23:06:35 +00:00
{
std::lock_guard<std::mutex> l(_state_l);
_state[nwid].dirty = true;
}
2019-08-06 15:42:54 +00:00
modified = true;
2019-07-22 20:43:06 +00:00
}
}
} else if (objtype == "member") {
const uint64_t nwid = OSUtils::jsonIntHex(record["nwid"],0ULL);
const uint64_t id = OSUtils::jsonIntHex(record["id"],0ULL);
if ((id)&&(nwid)) {
nlohmann::json network,old;
get(nwid,network,id,old);
if ((!old.is_object())||(old != record)) {
record["revision"] = OSUtils::jsonInt(record["revision"],0ULL) + 1ULL;
2019-08-06 15:42:54 +00:00
_memberChanged(old,record,notifyListeners);
2019-07-23 23:06:35 +00:00
{
std::lock_guard<std::mutex> l(_state_l);
_state[nwid].members[id].dirty = true;
}
2019-08-06 15:42:54 +00:00
modified = true;
2019-07-22 20:43:06 +00:00
}
}
}
2019-08-06 15:42:54 +00:00
return modified;
2019-07-22 20:43:06 +00:00
}
void LFDB::eraseNetwork(const uint64_t networkId)
{
// TODO
}
void LFDB::eraseMember(const uint64_t networkId,const uint64_t memberId)
{
// TODO
}
void LFDB::nodeIsOnline(const uint64_t networkId,const uint64_t memberId,const InetAddress &physicalAddress)
{
2019-08-06 15:42:54 +00:00
std::lock_guard<std::mutex> l(_state_l);
auto nw = _state.find(networkId);
if (nw != _state.end()) {
auto m = nw->second.members.find(memberId);
if (m != nw->second.members.end()) {
m->second.lastOnlineTime = OSUtils::now();
if (physicalAddress)
m->second.lastOnlineAddress = physicalAddress;
m->second.lastOnlineDirty = true;
2019-07-22 20:43:06 +00:00
}
}
}
} // namespace ZeroTier