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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ZT_JSONDB_HPP
|
|
|
|
#define ZT_JSONDB_HPP
|
|
|
|
|
2016-12-15 23:08:47 +00:00
|
|
|
#include <stdio.h>
|
2016-11-04 23:12:44 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-11-03 21:17:46 +00:00
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <vector>
|
|
|
|
#include <algorithm>
|
2017-04-26 13:48:08 +00:00
|
|
|
#include <unordered_map>
|
2016-11-03 21:17:46 +00:00
|
|
|
|
|
|
|
#include "../node/Constants.hpp"
|
|
|
|
#include "../node/Utils.hpp"
|
2017-04-19 00:37:44 +00:00
|
|
|
#include "../node/InetAddress.hpp"
|
|
|
|
#include "../node/Mutex.hpp"
|
2016-11-03 21:17:46 +00:00
|
|
|
#include "../ext/json/json.hpp"
|
|
|
|
#include "../osdep/OSUtils.hpp"
|
2017-04-19 00:37:44 +00:00
|
|
|
#include "../osdep/Http.hpp"
|
2017-04-24 16:09:04 +00:00
|
|
|
#include "../osdep/Thread.hpp"
|
2016-11-03 21:17:46 +00:00
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
/**
|
2017-04-19 00:37:44 +00:00
|
|
|
* Hierarchical JSON store that persists into the filesystem or via HTTP
|
2016-11-03 21:17:46 +00:00
|
|
|
*/
|
|
|
|
class JSONDB
|
|
|
|
{
|
|
|
|
public:
|
2017-04-26 13:48:08 +00:00
|
|
|
struct NetworkSummaryInfo
|
|
|
|
{
|
|
|
|
NetworkSummaryInfo() : authorizedMemberCount(0),activeMemberCount(0),totalMemberCount(0),mostRecentDeauthTime(0) {}
|
|
|
|
std::vector<Address> activeBridges;
|
|
|
|
std::vector<InetAddress> allocatedIps;
|
|
|
|
unsigned long authorizedMemberCount;
|
|
|
|
unsigned long activeMemberCount;
|
|
|
|
unsigned long totalMemberCount;
|
|
|
|
uint64_t mostRecentDeauthTime;
|
|
|
|
};
|
|
|
|
|
2017-04-19 00:37:44 +00:00
|
|
|
JSONDB(const std::string &basePath);
|
2017-04-26 13:48:08 +00:00
|
|
|
~JSONDB();
|
2016-11-04 22:18:31 +00:00
|
|
|
|
2017-03-02 00:33:34 +00:00
|
|
|
bool writeRaw(const std::string &n,const std::string &obj);
|
|
|
|
|
2017-04-27 07:59:36 +00:00
|
|
|
bool hasNetwork(const uint64_t networkId) const;
|
2017-04-26 13:48:08 +00:00
|
|
|
|
2017-04-27 07:59:36 +00:00
|
|
|
bool getNetwork(const uint64_t networkId,nlohmann::json &config) const;
|
2017-04-26 13:48:08 +00:00
|
|
|
|
2017-04-27 07:59:36 +00:00
|
|
|
bool getNetworkSummaryInfo(const uint64_t networkId,NetworkSummaryInfo &ns) const;
|
2017-04-26 13:48:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Bit mask: 0 == none, 1 == network only, 3 == network and member
|
|
|
|
*/
|
2017-04-27 07:59:36 +00:00
|
|
|
int getNetworkAndMember(const uint64_t networkId,const uint64_t nodeId,nlohmann::json &networkConfig,nlohmann::json &memberConfig,NetworkSummaryInfo &ns) const;
|
2017-04-26 13:48:08 +00:00
|
|
|
|
2017-04-27 07:59:36 +00:00
|
|
|
bool getNetworkMember(const uint64_t networkId,const uint64_t nodeId,nlohmann::json &memberConfig) const;
|
2017-04-26 13:48:08 +00:00
|
|
|
|
|
|
|
void saveNetwork(const uint64_t networkId,const nlohmann::json &networkConfig);
|
2016-11-03 21:17:46 +00:00
|
|
|
|
2017-04-26 13:48:08 +00:00
|
|
|
void 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
|
|
|
nlohmann::json eraseNetwork(const uint64_t networkId);
|
2016-11-04 22:18:31 +00:00
|
|
|
|
2017-04-26 13:48:08 +00:00
|
|
|
nlohmann::json eraseNetworkMember(const uint64_t networkId,const uint64_t nodeId,bool recomputeSummaryInfo = true);
|
|
|
|
|
|
|
|
std::vector<uint64_t> networkIds() const
|
|
|
|
{
|
|
|
|
std::vector<uint64_t> r;
|
|
|
|
Mutex::Lock _l(_networks_m);
|
|
|
|
for(std::unordered_map<uint64_t,_NW>::const_iterator n(_networks.begin());n!=_networks.end();++n)
|
|
|
|
r.push_back(n->first);
|
|
|
|
return r;
|
|
|
|
}
|
2016-11-04 22:18:31 +00:00
|
|
|
|
2017-05-08 15:35:55 +00:00
|
|
|
inline unsigned long memberCount(const uint64_t networkId)
|
|
|
|
{
|
|
|
|
Mutex::Lock _l(_networks_m);
|
|
|
|
std::unordered_map<uint64_t,_NW>::const_iterator i(_networks.find(networkId));
|
|
|
|
if (i != _networks.end())
|
|
|
|
return (unsigned long)i->second.members.size();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-03 21:17:46 +00:00
|
|
|
template<typename F>
|
2017-04-26 13:48:08 +00:00
|
|
|
inline void eachMember(const uint64_t networkId,F func)
|
2016-11-03 21:17:46 +00:00
|
|
|
{
|
2017-04-26 13:48:08 +00:00
|
|
|
Mutex::Lock _l(_networks_m);
|
|
|
|
std::unordered_map<uint64_t,_NW>::const_iterator i(_networks.find(networkId));
|
|
|
|
if (i != _networks.end()) {
|
2017-04-28 02:36:03 +00:00
|
|
|
for(std::unordered_map< uint64_t,std::vector<uint8_t> >::const_iterator m(i->second.members.begin());m!=i->second.members.end();++m) {
|
2017-04-26 13:48:08 +00:00
|
|
|
try {
|
2017-04-28 02:36:03 +00:00
|
|
|
func(networkId,m->first,nlohmann::json::from_msgpack(m->second));
|
2017-04-26 13:48:08 +00:00
|
|
|
} catch ( ... ) {}
|
2017-04-25 03:51:02 +00:00
|
|
|
}
|
2016-11-03 21:17:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-01 20:21:26 +00:00
|
|
|
template<typename F>
|
|
|
|
inline void eachId(F func)
|
|
|
|
{
|
|
|
|
Mutex::Lock _l(_networks_m);
|
|
|
|
for(std::unordered_map<uint64_t,_NW>::const_iterator i(_networks.begin());i!=_networks.end();++i) {
|
|
|
|
for(std::unordered_map< uint64_t,std::vector<uint8_t> >::const_iterator m(i->second.members.begin());m!=i->second.members.end();++m) {
|
|
|
|
try {
|
|
|
|
func(i->first,m->first);
|
|
|
|
} catch ( ... ) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-26 13:48:08 +00:00
|
|
|
void threadMain()
|
|
|
|
throw();
|
|
|
|
|
2016-11-03 21:17:46 +00:00
|
|
|
private:
|
2017-04-26 13:48:08 +00:00
|
|
|
bool _load(const std::string &p);
|
|
|
|
void _recomputeSummaryInfo(const uint64_t networkId);
|
2016-11-03 21:17:46 +00:00
|
|
|
std::string _genPath(const std::string &n,bool create);
|
|
|
|
|
2017-04-26 13:48:08 +00:00
|
|
|
std::string _basePath;
|
|
|
|
InetAddress _httpAddr;
|
|
|
|
|
|
|
|
Thread _summaryThread;
|
2017-04-26 17:35:59 +00:00
|
|
|
std::vector<uint64_t> _summaryThreadToDo;
|
|
|
|
volatile bool _summaryThreadRun;
|
2017-04-26 13:48:08 +00:00
|
|
|
Mutex _summaryThread_m;
|
|
|
|
|
|
|
|
struct _NW
|
2016-11-03 21:17:46 +00:00
|
|
|
{
|
2017-04-26 13:48:08 +00:00
|
|
|
_NW() : summaryInfoLastComputed(0) {}
|
2017-04-28 02:36:03 +00:00
|
|
|
std::vector<uint8_t> config;
|
2017-04-26 13:48:08 +00:00
|
|
|
NetworkSummaryInfo summaryInfo;
|
|
|
|
uint64_t summaryInfoLastComputed;
|
2017-04-28 02:36:03 +00:00
|
|
|
std::unordered_map< uint64_t,std::vector<uint8_t> > members;
|
2016-11-03 21:17:46 +00:00
|
|
|
};
|
|
|
|
|
2017-04-26 13:48:08 +00:00
|
|
|
std::unordered_map<uint64_t,_NW> _networks;
|
|
|
|
Mutex _networks_m;
|
2016-11-03 21:17:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|