2017-11-07 22:44:46 +00:00
|
|
|
/*
|
2019-08-23 16:23:39 +00:00
|
|
|
* Copyright (c)2019 ZeroTier, Inc.
|
2017-11-07 22:44:46 +00:00
|
|
|
*
|
2019-08-23 16:23:39 +00:00
|
|
|
* Use of this software is governed by the Business Source License included
|
|
|
|
* in the LICENSE.TXT file in the project's root directory.
|
2017-11-07 22:44:46 +00:00
|
|
|
*
|
2020-08-20 19:51:39 +00:00
|
|
|
* Change Date: 2025-01-01
|
2017-11-07 22:44:46 +00:00
|
|
|
*
|
2019-08-23 16:23:39 +00:00
|
|
|
* On the date above, in accordance with the Business Source License, use
|
|
|
|
* of this software will be governed by version 2.0 of the Apache License.
|
2017-11-07 22:44:46 +00:00
|
|
|
*/
|
2019-08-23 16:23:39 +00:00
|
|
|
/****/
|
2017-11-07 22:44:46 +00:00
|
|
|
|
|
|
|
#ifndef ZT_CONTROLLER_DB_HPP
|
|
|
|
#define ZT_CONTROLLER_DB_HPP
|
|
|
|
|
2019-08-06 16:01:08 +00:00
|
|
|
//#define ZT_CONTROLLER_USE_LIBPQ
|
2019-08-06 13:51:23 +00:00
|
|
|
|
2017-11-07 22:44:46 +00:00
|
|
|
#include "../node/Constants.hpp"
|
2017-12-07 21:39:25 +00:00
|
|
|
#include "../node/Identity.hpp"
|
2017-11-07 22:44:46 +00:00
|
|
|
#include "../node/InetAddress.hpp"
|
|
|
|
#include "../osdep/OSUtils.hpp"
|
|
|
|
#include "../osdep/BlockingQueue.hpp"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <thread>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
|
|
|
#include <vector>
|
|
|
|
#include <atomic>
|
2019-07-15 20:27:53 +00:00
|
|
|
#include <mutex>
|
2019-08-06 15:42:54 +00:00
|
|
|
#include <set>
|
2017-11-07 22:44:46 +00:00
|
|
|
|
|
|
|
#include "../ext/json/json.hpp"
|
|
|
|
|
|
|
|
namespace ZeroTier
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class with common infrastructure for all controller DB implementations
|
|
|
|
*/
|
|
|
|
class DB
|
|
|
|
{
|
|
|
|
public:
|
2019-07-27 00:39:00 +00:00
|
|
|
class ChangeListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ChangeListener() {}
|
|
|
|
virtual ~ChangeListener() {}
|
2019-08-06 15:42:54 +00:00
|
|
|
virtual void onNetworkUpdate(const void *db,uint64_t networkId,const nlohmann::json &network) {}
|
|
|
|
virtual void onNetworkMemberUpdate(const void *db,uint64_t networkId,uint64_t memberId,const nlohmann::json &member) {}
|
|
|
|
virtual void onNetworkMemberDeauthorize(const void *db,uint64_t networkId,uint64_t memberId) {}
|
2019-07-27 00:39:00 +00:00
|
|
|
};
|
|
|
|
|
2017-11-07 22:44:46 +00:00
|
|
|
struct NetworkSummaryInfo
|
|
|
|
{
|
|
|
|
NetworkSummaryInfo() : authorizedMemberCount(0),totalMemberCount(0),mostRecentDeauthTime(0) {}
|
|
|
|
std::vector<Address> activeBridges;
|
|
|
|
std::vector<InetAddress> allocatedIps;
|
|
|
|
unsigned long authorizedMemberCount;
|
|
|
|
unsigned long totalMemberCount;
|
|
|
|
int64_t mostRecentDeauthTime;
|
|
|
|
};
|
|
|
|
|
2018-01-09 20:39:25 +00:00
|
|
|
static void initNetwork(nlohmann::json &network);
|
|
|
|
static void initMember(nlohmann::json &member);
|
|
|
|
static void cleanNetwork(nlohmann::json &network);
|
|
|
|
static void cleanMember(nlohmann::json &member);
|
|
|
|
|
2019-08-06 15:42:54 +00:00
|
|
|
DB();
|
2017-11-07 22:44:46 +00:00
|
|
|
virtual ~DB();
|
|
|
|
|
|
|
|
virtual bool waitForReady() = 0;
|
2018-07-11 17:42:31 +00:00
|
|
|
virtual bool isReady() = 0;
|
2017-11-07 22:44:46 +00:00
|
|
|
|
|
|
|
inline bool hasNetwork(const uint64_t networkId) const
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> l(_networks_l);
|
|
|
|
return (_networks.find(networkId) != _networks.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool get(const uint64_t networkId,nlohmann::json &network);
|
|
|
|
bool get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member);
|
|
|
|
bool get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member,NetworkSummaryInfo &info);
|
|
|
|
bool get(const uint64_t networkId,nlohmann::json &network,std::vector<nlohmann::json> &members);
|
2019-08-06 12:51:50 +00:00
|
|
|
|
2019-08-06 15:42:54 +00:00
|
|
|
void networks(std::set<uint64_t> &networks);
|
2017-11-07 22:44:46 +00:00
|
|
|
|
2019-08-08 22:20:50 +00:00
|
|
|
template<typename F>
|
|
|
|
inline void each(F f)
|
|
|
|
{
|
|
|
|
nlohmann::json nullJson;
|
|
|
|
std::lock_guard<std::mutex> lck(_networks_l);
|
|
|
|
for(auto nw=_networks.begin();nw!=_networks.end();++nw) {
|
|
|
|
f(nw->first,nw->second->config,0,nullJson); // first provide network with 0 for member ID
|
|
|
|
for(auto m=nw->second->members.begin();m!=nw->second->members.end();++m) {
|
|
|
|
f(nw->first,nw->second->config,m->first,m->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-06 15:42:54 +00:00
|
|
|
virtual bool save(nlohmann::json &record,bool notifyListeners) = 0;
|
2019-08-06 12:51:50 +00:00
|
|
|
|
2017-11-07 22:44:46 +00:00
|
|
|
virtual void eraseNetwork(const uint64_t networkId) = 0;
|
|
|
|
virtual void eraseMember(const uint64_t networkId,const uint64_t memberId) = 0;
|
2019-08-06 12:51:50 +00:00
|
|
|
|
2017-11-09 01:19:46 +00:00
|
|
|
virtual void nodeIsOnline(const uint64_t networkId,const uint64_t memberId,const InetAddress &physicalAddress) = 0;
|
2017-11-08 19:06:14 +00:00
|
|
|
|
2019-07-27 00:39:00 +00:00
|
|
|
inline void addListener(DB::ChangeListener *const listener)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> l(_changeListeners_l);
|
|
|
|
_changeListeners.push_back(listener);
|
|
|
|
}
|
|
|
|
|
2017-11-07 22:44:46 +00:00
|
|
|
protected:
|
2019-08-08 22:20:50 +00:00
|
|
|
static inline bool _compareRecords(const nlohmann::json &a,const nlohmann::json &b)
|
2019-08-08 20:29:13 +00:00
|
|
|
{
|
|
|
|
if (a.is_object() == b.is_object()) {
|
|
|
|
if (a.is_object()) {
|
|
|
|
if (a.size() != b.size())
|
|
|
|
return false;
|
|
|
|
auto amap = a.get<nlohmann::json::object_t>();
|
|
|
|
auto bmap = b.get<nlohmann::json::object_t>();
|
|
|
|
for(auto ai=amap.begin();ai!=amap.end();++ai) {
|
|
|
|
if (ai->first != "revision") { // ignore revision, compare only non-revision-counter fields
|
|
|
|
auto bi = bmap.find(ai->first);
|
|
|
|
if ((bi == bmap.end())||(bi->second != ai->second))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return (a == b);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-07 22:44:46 +00:00
|
|
|
struct _Network
|
|
|
|
{
|
|
|
|
_Network() : mostRecentDeauthTime(0) {}
|
|
|
|
nlohmann::json config;
|
|
|
|
std::unordered_map<uint64_t,nlohmann::json> members;
|
|
|
|
std::unordered_set<uint64_t> activeBridgeMembers;
|
|
|
|
std::unordered_set<uint64_t> authorizedMembers;
|
|
|
|
std::unordered_set<InetAddress,InetAddress::Hasher> allocatedIps;
|
|
|
|
int64_t mostRecentDeauthTime;
|
|
|
|
std::mutex lock;
|
|
|
|
};
|
|
|
|
|
2019-08-06 15:42:54 +00:00
|
|
|
void _memberChanged(nlohmann::json &old,nlohmann::json &memberConfig,bool notifyListeners);
|
|
|
|
void _networkChanged(nlohmann::json &old,nlohmann::json &networkConfig,bool notifyListeners);
|
2017-11-07 22:44:46 +00:00
|
|
|
void _fillSummaryInfo(const std::shared_ptr<_Network> &nw,NetworkSummaryInfo &info);
|
|
|
|
|
2019-07-27 00:39:00 +00:00
|
|
|
std::vector<DB::ChangeListener *> _changeListeners;
|
2017-11-07 22:44:46 +00:00
|
|
|
std::unordered_map< uint64_t,std::shared_ptr<_Network> > _networks;
|
|
|
|
std::unordered_multimap< uint64_t,uint64_t > _networkByMember;
|
2019-07-27 00:39:00 +00:00
|
|
|
mutable std::mutex _changeListeners_l;
|
2017-11-07 22:44:46 +00:00
|
|
|
mutable std::mutex _networks_l;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|