2017-11-03 18:39:27 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef ZT_CONTROLLER_USE_RETHINKDB
|
|
|
|
|
2017-11-02 14:05:11 +00:00
|
|
|
#ifndef ZT_CONTROLLER_RETHINKDB_HPP
|
|
|
|
#define ZT_CONTROLLER_RETHINKDB_HPP
|
|
|
|
|
|
|
|
#include "../node/Constants.hpp"
|
|
|
|
#include "../node/Address.hpp"
|
|
|
|
#include "../node/InetAddress.hpp"
|
|
|
|
#include "../osdep/OSUtils.hpp"
|
2017-11-03 18:39:27 +00:00
|
|
|
#include "../osdep/BlockingQueue.hpp"
|
2017-11-02 14:05:11 +00:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <thread>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
|
|
|
#include <vector>
|
2017-11-03 20:59:36 +00:00
|
|
|
#include <atomic>
|
2017-11-02 14:05:11 +00:00
|
|
|
|
|
|
|
#include "../ext/json/json.hpp"
|
|
|
|
|
2017-11-03 18:39:27 +00:00
|
|
|
#define ZT_CONTROLLER_RETHINKDB_COMMIT_THREADS 2
|
|
|
|
|
2017-11-02 14:05:11 +00:00
|
|
|
namespace ZeroTier
|
|
|
|
{
|
|
|
|
|
2017-11-03 18:39:27 +00:00
|
|
|
class EmbeddedNetworkController;
|
|
|
|
|
2017-11-02 14:05:11 +00:00
|
|
|
class RethinkDB
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2017-11-03 18:39:27 +00:00
|
|
|
RethinkDB(EmbeddedNetworkController *const nc,const Address &myAddress,const char *path);
|
2017-11-02 14:05:11 +00:00
|
|
|
~RethinkDB();
|
|
|
|
|
|
|
|
inline void waitForReady() const
|
|
|
|
{
|
|
|
|
while (_ready > 0) {
|
2017-11-03 18:39:27 +00:00
|
|
|
if (!_waitNoticePrinted) {
|
|
|
|
_waitNoticePrinted = true;
|
|
|
|
fprintf(stderr,"NOTICE: controller RethinkDB waiting for initial data download..." ZT_EOL_S);
|
|
|
|
}
|
2017-11-02 14:05:11 +00:00
|
|
|
_readyLock.lock();
|
|
|
|
_readyLock.unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-03 18:39:27 +00:00
|
|
|
inline bool hasNetwork(const uint64_t networkId) const
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> l(_networks_l);
|
|
|
|
return (_networks.find(networkId) != _networks.end());
|
|
|
|
}
|
|
|
|
|
2017-11-02 14:05:11 +00:00
|
|
|
bool get(const uint64_t networkId,nlohmann::json &network);
|
2017-11-03 18:39:27 +00:00
|
|
|
bool get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member);
|
2017-11-02 14:05:11 +00:00
|
|
|
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);
|
2017-11-03 18:39:27 +00:00
|
|
|
|
2017-11-02 14:05:11 +00:00
|
|
|
bool summary(const uint64_t networkId,NetworkSummaryInfo &info);
|
|
|
|
|
2017-11-03 18:39:27 +00:00
|
|
|
void networks(std::vector<uint64_t> &networks);
|
|
|
|
|
|
|
|
void save(const nlohmann::json &record);
|
|
|
|
|
|
|
|
void eraseNetwork(const uint64_t networkId);
|
|
|
|
void eraseMember(const uint64_t networkId,const uint64_t memberId);
|
|
|
|
|
2017-11-02 14:05:11 +00:00
|
|
|
private:
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
void _memberChanged(nlohmann::json &old,nlohmann::json &member);
|
|
|
|
void _networkChanged(nlohmann::json &old,nlohmann::json &network);
|
|
|
|
|
|
|
|
inline void _fillSummaryInfo(const std::shared_ptr<_Network> &nw,NetworkSummaryInfo &info)
|
|
|
|
{
|
|
|
|
for(auto ab=nw->activeBridgeMembers.begin();ab!=nw->activeBridgeMembers.end();++ab)
|
|
|
|
info.activeBridges.push_back(Address(*ab));
|
|
|
|
for(auto ip=nw->allocatedIps.begin();ip!=nw->allocatedIps.end();++ip)
|
|
|
|
info.allocatedIps.push_back(*ip);
|
|
|
|
info.authorizedMemberCount = (unsigned long)nw->authorizedMembers.size();
|
|
|
|
info.totalMemberCount = (unsigned long)nw->members.size();
|
|
|
|
info.mostRecentDeauthTime = nw->mostRecentDeauthTime;
|
|
|
|
}
|
|
|
|
|
2017-11-03 18:39:27 +00:00
|
|
|
EmbeddedNetworkController *const _controller;
|
2017-11-02 14:05:11 +00:00
|
|
|
const Address _myAddress;
|
|
|
|
std::string _myAddressStr;
|
|
|
|
std::string _host;
|
|
|
|
std::string _db;
|
|
|
|
std::string _auth;
|
2017-11-03 18:39:27 +00:00
|
|
|
int _port;
|
2017-11-02 14:05:11 +00:00
|
|
|
|
|
|
|
void *_networksDbWatcherConnection;
|
|
|
|
void *_membersDbWatcherConnection;
|
|
|
|
std::thread _networksDbWatcher;
|
|
|
|
std::thread _membersDbWatcher;
|
|
|
|
|
|
|
|
std::unordered_map< uint64_t,std::shared_ptr<_Network> > _networks;
|
2017-11-03 19:32:56 +00:00
|
|
|
std::unordered_multimap< uint64_t,uint64_t > _networkByMember;
|
2017-11-03 18:39:27 +00:00
|
|
|
mutable std::mutex _networks_l;
|
|
|
|
|
2017-11-03 20:59:36 +00:00
|
|
|
BlockingQueue< nlohmann::json * > _commitQueue;
|
2017-11-03 18:39:27 +00:00
|
|
|
std::thread _commitThread[ZT_CONTROLLER_RETHINKDB_COMMIT_THREADS];
|
2017-11-02 14:05:11 +00:00
|
|
|
|
2017-11-04 00:55:16 +00:00
|
|
|
std::thread _heartbeatThread;
|
|
|
|
|
2017-11-02 14:05:11 +00:00
|
|
|
mutable std::mutex _readyLock; // locked until ready
|
|
|
|
std::atomic<int> _ready;
|
|
|
|
std::atomic<int> _run;
|
2017-11-03 18:39:27 +00:00
|
|
|
mutable volatile bool _waitNoticePrinted;
|
2017-11-02 14:05:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|
2017-11-03 18:39:27 +00:00
|
|
|
|
|
|
|
#endif // ZT_CONTROLLER_USE_RETHINKDB
|