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
|
|
|
|
|
|
|
#include "DB.hpp"
|
|
|
|
#include "EmbeddedNetworkController.hpp"
|
2023-04-21 19:12:43 +00:00
|
|
|
#include "../node/Metrics.hpp"
|
2017-11-07 22:44:46 +00:00
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
using json = nlohmann::json;
|
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
2018-01-09 20:39:25 +00:00
|
|
|
void DB::initNetwork(nlohmann::json &network)
|
|
|
|
{
|
|
|
|
if (!network.count("private")) network["private"] = true;
|
|
|
|
if (!network.count("creationTime")) network["creationTime"] = OSUtils::now();
|
|
|
|
if (!network.count("name")) network["name"] = "";
|
|
|
|
if (!network.count("multicastLimit")) network["multicastLimit"] = (uint64_t)32;
|
|
|
|
if (!network.count("enableBroadcast")) network["enableBroadcast"] = true;
|
|
|
|
if (!network.count("v4AssignMode")) network["v4AssignMode"] = {{"zt",false}};
|
|
|
|
if (!network.count("v6AssignMode")) network["v6AssignMode"] = {{"rfc4193",false},{"zt",false},{"6plane",false}};
|
|
|
|
if (!network.count("authTokens")) network["authTokens"] = {{}};
|
|
|
|
if (!network.count("capabilities")) network["capabilities"] = nlohmann::json::array();
|
|
|
|
if (!network.count("tags")) network["tags"] = nlohmann::json::array();
|
|
|
|
if (!network.count("routes")) network["routes"] = nlohmann::json::array();
|
|
|
|
if (!network.count("ipAssignmentPools")) network["ipAssignmentPools"] = nlohmann::json::array();
|
|
|
|
if (!network.count("mtu")) network["mtu"] = ZT_DEFAULT_MTU;
|
|
|
|
if (!network.count("remoteTraceTarget")) network["remoteTraceTarget"] = nlohmann::json();
|
|
|
|
if (!network.count("removeTraceLevel")) network["remoteTraceLevel"] = 0;
|
2019-08-08 22:56:27 +00:00
|
|
|
if (!network.count("rulesSource")) network["rulesSource"] = "";
|
2018-01-09 20:39:25 +00:00
|
|
|
if (!network.count("rules")) {
|
|
|
|
// If unspecified, rules are set to allow anything and behave like a flat L2 segment
|
|
|
|
network["rules"] = {{
|
|
|
|
{ "not",false },
|
|
|
|
{ "or", false },
|
|
|
|
{ "type","ACTION_ACCEPT" }
|
|
|
|
}};
|
|
|
|
}
|
2020-08-04 20:52:57 +00:00
|
|
|
if (!network.count("dns")) network["dns"] = nlohmann::json::array();
|
2021-06-03 21:38:26 +00:00
|
|
|
if (!network.count("ssoEnabled")) network["ssoEnabled"] = false;
|
|
|
|
if (!network.count("clientId")) network["clientId"] = "";
|
|
|
|
if (!network.count("authorizationEndpoint")) network["authorizationEndpoint"] = "";
|
2020-08-04 20:52:57 +00:00
|
|
|
|
2018-01-09 20:39:25 +00:00
|
|
|
network["objtype"] = "network";
|
|
|
|
}
|
|
|
|
|
|
|
|
void DB::initMember(nlohmann::json &member)
|
|
|
|
{
|
|
|
|
if (!member.count("authorized")) member["authorized"] = false;
|
2021-07-23 22:49:00 +00:00
|
|
|
if (!member.count("ssoExempt")) member["ssoExempt"] = false;
|
2018-01-09 20:39:25 +00:00
|
|
|
if (!member.count("ipAssignments")) member["ipAssignments"] = nlohmann::json::array();
|
|
|
|
if (!member.count("activeBridge")) member["activeBridge"] = false;
|
|
|
|
if (!member.count("tags")) member["tags"] = nlohmann::json::array();
|
|
|
|
if (!member.count("capabilities")) member["capabilities"] = nlohmann::json::array();
|
|
|
|
if (!member.count("creationTime")) member["creationTime"] = OSUtils::now();
|
|
|
|
if (!member.count("noAutoAssignIps")) member["noAutoAssignIps"] = false;
|
|
|
|
if (!member.count("revision")) member["revision"] = 0ULL;
|
|
|
|
if (!member.count("lastDeauthorizedTime")) member["lastDeauthorizedTime"] = 0ULL;
|
|
|
|
if (!member.count("lastAuthorizedTime")) member["lastAuthorizedTime"] = 0ULL;
|
|
|
|
if (!member.count("lastAuthorizedCredentialType")) member["lastAuthorizedCredentialType"] = nlohmann::json();
|
|
|
|
if (!member.count("lastAuthorizedCredential")) member["lastAuthorizedCredential"] = nlohmann::json();
|
2021-06-04 20:19:39 +00:00
|
|
|
if (!member.count("authenticationExpiryTime")) member["authenticationExpiryTime"] = 0LL;
|
2018-01-09 20:39:25 +00:00
|
|
|
if (!member.count("vMajor")) member["vMajor"] = -1;
|
|
|
|
if (!member.count("vMinor")) member["vMinor"] = -1;
|
|
|
|
if (!member.count("vRev")) member["vRev"] = -1;
|
|
|
|
if (!member.count("vProto")) member["vProto"] = -1;
|
|
|
|
if (!member.count("remoteTraceTarget")) member["remoteTraceTarget"] = nlohmann::json();
|
|
|
|
if (!member.count("removeTraceLevel")) member["remoteTraceLevel"] = 0;
|
|
|
|
member["objtype"] = "member";
|
|
|
|
}
|
|
|
|
|
|
|
|
void DB::cleanNetwork(nlohmann::json &network)
|
|
|
|
{
|
|
|
|
network.erase("clock");
|
|
|
|
network.erase("authorizedMemberCount");
|
|
|
|
network.erase("activeMemberCount");
|
|
|
|
network.erase("totalMemberCount");
|
|
|
|
network.erase("lastModified");
|
|
|
|
}
|
|
|
|
|
|
|
|
void DB::cleanMember(nlohmann::json &member)
|
|
|
|
{
|
|
|
|
member.erase("clock");
|
|
|
|
member.erase("physicalAddr");
|
|
|
|
member.erase("recentLog");
|
|
|
|
member.erase("lastModified");
|
|
|
|
member.erase("lastRequestMetaData");
|
2021-05-28 21:08:24 +00:00
|
|
|
member.erase("authenticationURL"); // computed
|
|
|
|
member.erase("authenticationClientID"); // computed
|
2018-01-09 20:39:25 +00:00
|
|
|
}
|
|
|
|
|
2019-08-06 15:42:54 +00:00
|
|
|
DB::DB() {}
|
2019-07-27 00:39:00 +00:00
|
|
|
DB::~DB() {}
|
2017-11-07 22:44:46 +00:00
|
|
|
|
|
|
|
bool DB::get(const uint64_t networkId,nlohmann::json &network)
|
|
|
|
{
|
|
|
|
waitForReady();
|
2023-05-23 19:11:26 +00:00
|
|
|
Metrics::db_get_network++;
|
2017-11-07 22:44:46 +00:00
|
|
|
std::shared_ptr<_Network> nw;
|
|
|
|
{
|
2023-05-23 19:11:26 +00:00
|
|
|
std::shared_lock<std::shared_mutex> l(_networks_l);
|
2017-11-07 22:44:46 +00:00
|
|
|
auto nwi = _networks.find(networkId);
|
|
|
|
if (nwi == _networks.end())
|
|
|
|
return false;
|
|
|
|
nw = nwi->second;
|
|
|
|
}
|
|
|
|
{
|
2023-05-23 19:11:26 +00:00
|
|
|
std::shared_lock<std::shared_mutex> l2(nw->lock);
|
2017-11-07 22:44:46 +00:00
|
|
|
network = nw->config;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DB::get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member)
|
|
|
|
{
|
|
|
|
waitForReady();
|
2023-05-23 19:11:26 +00:00
|
|
|
Metrics::db_get_network_and_member++;
|
2017-11-07 22:44:46 +00:00
|
|
|
std::shared_ptr<_Network> nw;
|
|
|
|
{
|
2023-05-23 19:11:26 +00:00
|
|
|
std::shared_lock<std::shared_mutex> l(_networks_l);
|
2017-11-07 22:44:46 +00:00
|
|
|
auto nwi = _networks.find(networkId);
|
|
|
|
if (nwi == _networks.end())
|
|
|
|
return false;
|
|
|
|
nw = nwi->second;
|
|
|
|
}
|
|
|
|
{
|
2023-05-23 19:11:26 +00:00
|
|
|
std::shared_lock<std::shared_mutex> l2(nw->lock);
|
2017-11-07 22:44:46 +00:00
|
|
|
network = nw->config;
|
|
|
|
auto m = nw->members.find(memberId);
|
|
|
|
if (m == nw->members.end())
|
|
|
|
return false;
|
|
|
|
member = m->second;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DB::get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member,NetworkSummaryInfo &info)
|
|
|
|
{
|
|
|
|
waitForReady();
|
2023-05-23 19:11:26 +00:00
|
|
|
Metrics::db_get_network_and_member_and_summary++;
|
2017-11-07 22:44:46 +00:00
|
|
|
std::shared_ptr<_Network> nw;
|
|
|
|
{
|
2023-05-23 19:11:26 +00:00
|
|
|
std::shared_lock<std::shared_mutex> l(_networks_l);
|
2017-11-07 22:44:46 +00:00
|
|
|
auto nwi = _networks.find(networkId);
|
|
|
|
if (nwi == _networks.end())
|
|
|
|
return false;
|
|
|
|
nw = nwi->second;
|
|
|
|
}
|
|
|
|
{
|
2023-05-23 19:11:26 +00:00
|
|
|
std::shared_lock<std::shared_mutex> l2(nw->lock);
|
2017-11-07 22:44:46 +00:00
|
|
|
network = nw->config;
|
|
|
|
_fillSummaryInfo(nw,info);
|
|
|
|
auto m = nw->members.find(memberId);
|
|
|
|
if (m == nw->members.end())
|
|
|
|
return false;
|
|
|
|
member = m->second;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DB::get(const uint64_t networkId,nlohmann::json &network,std::vector<nlohmann::json> &members)
|
|
|
|
{
|
|
|
|
waitForReady();
|
2023-05-23 19:11:26 +00:00
|
|
|
Metrics::db_get_member_list++;
|
2017-11-07 22:44:46 +00:00
|
|
|
std::shared_ptr<_Network> nw;
|
|
|
|
{
|
2023-05-23 19:11:26 +00:00
|
|
|
std::shared_lock<std::shared_mutex> l(_networks_l);
|
2017-11-07 22:44:46 +00:00
|
|
|
auto nwi = _networks.find(networkId);
|
|
|
|
if (nwi == _networks.end())
|
|
|
|
return false;
|
|
|
|
nw = nwi->second;
|
|
|
|
}
|
|
|
|
{
|
2023-05-23 19:11:26 +00:00
|
|
|
std::shared_lock<std::shared_mutex> l2(nw->lock);
|
2017-11-07 22:44:46 +00:00
|
|
|
network = nw->config;
|
2021-05-28 21:08:24 +00:00
|
|
|
for(auto m=nw->members.begin();m!=nw->members.end();++m) {
|
2017-11-07 22:44:46 +00:00
|
|
|
members.push_back(m->second);
|
2021-05-28 21:08:24 +00:00
|
|
|
}
|
2017-11-07 22:44:46 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-06 15:42:54 +00:00
|
|
|
void DB::networks(std::set<uint64_t> &networks)
|
2017-11-07 22:44:46 +00:00
|
|
|
{
|
|
|
|
waitForReady();
|
2023-05-23 19:11:26 +00:00
|
|
|
Metrics::db_get_member_list++;
|
|
|
|
std::shared_lock<std::shared_mutex> l(_networks_l);
|
2017-11-07 22:44:46 +00:00
|
|
|
for(auto n=_networks.begin();n!=_networks.end();++n)
|
2019-08-06 15:42:54 +00:00
|
|
|
networks.insert(n->first);
|
2017-11-07 22:44:46 +00:00
|
|
|
}
|
|
|
|
|
2019-08-06 15:42:54 +00:00
|
|
|
void DB::_memberChanged(nlohmann::json &old,nlohmann::json &memberConfig,bool notifyListeners)
|
2017-11-07 22:44:46 +00:00
|
|
|
{
|
2023-05-23 19:11:26 +00:00
|
|
|
Metrics::db_member_change++;
|
2017-11-07 22:44:46 +00:00
|
|
|
uint64_t memberId = 0;
|
|
|
|
uint64_t networkId = 0;
|
|
|
|
bool isAuth = false;
|
|
|
|
bool wasAuth = false;
|
|
|
|
std::shared_ptr<_Network> nw;
|
|
|
|
|
2018-03-09 06:33:08 +00:00
|
|
|
if (old.is_object()) {
|
|
|
|
memberId = OSUtils::jsonIntHex(old["id"],0ULL);
|
|
|
|
networkId = OSUtils::jsonIntHex(old["nwid"],0ULL);
|
|
|
|
if ((memberId)&&(networkId)) {
|
|
|
|
{
|
2023-05-23 19:11:26 +00:00
|
|
|
std::unique_lock<std::shared_mutex> l(_networks_l);
|
2018-03-09 06:33:08 +00:00
|
|
|
auto nw2 = _networks.find(networkId);
|
2023-04-21 19:12:43 +00:00
|
|
|
if (nw2 != _networks.end()) {
|
2018-03-09 06:33:08 +00:00
|
|
|
nw = nw2->second;
|
2023-04-21 19:12:43 +00:00
|
|
|
}
|
2018-03-09 06:33:08 +00:00
|
|
|
}
|
|
|
|
if (nw) {
|
2023-05-23 19:11:26 +00:00
|
|
|
std::unique_lock<std::shared_mutex> l(nw->lock);
|
2023-04-21 19:12:43 +00:00
|
|
|
if (OSUtils::jsonBool(old["activeBridge"],false)) {
|
2018-03-09 06:33:08 +00:00
|
|
|
nw->activeBridgeMembers.erase(memberId);
|
2023-04-21 19:12:43 +00:00
|
|
|
}
|
2018-03-09 06:33:08 +00:00
|
|
|
wasAuth = OSUtils::jsonBool(old["authorized"],false);
|
2023-04-21 19:12:43 +00:00
|
|
|
if (wasAuth) {
|
2018-03-09 06:33:08 +00:00
|
|
|
nw->authorizedMembers.erase(memberId);
|
2023-04-21 19:12:43 +00:00
|
|
|
}
|
2018-03-09 06:33:08 +00:00
|
|
|
json &ips = old["ipAssignments"];
|
|
|
|
if (ips.is_array()) {
|
|
|
|
for(unsigned long i=0;i<ips.size();++i) {
|
|
|
|
json &ipj = ips[i];
|
|
|
|
if (ipj.is_string()) {
|
|
|
|
const std::string ips = ipj;
|
|
|
|
InetAddress ipa(ips.c_str());
|
|
|
|
ipa.setPort(0);
|
|
|
|
nw->allocatedIps.erase(ipa);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memberConfig.is_object()) {
|
|
|
|
if (!nw) {
|
|
|
|
memberId = OSUtils::jsonIntHex(memberConfig["id"],0ULL);
|
|
|
|
networkId = OSUtils::jsonIntHex(memberConfig["nwid"],0ULL);
|
|
|
|
if ((!memberId)||(!networkId))
|
|
|
|
return;
|
2023-05-23 19:11:26 +00:00
|
|
|
std::unique_lock<std::shared_mutex> l(_networks_l);
|
2018-03-09 06:33:08 +00:00
|
|
|
std::shared_ptr<_Network> &nw2 = _networks[networkId];
|
|
|
|
if (!nw2)
|
|
|
|
nw2.reset(new _Network);
|
|
|
|
nw = nw2;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2023-05-23 19:11:26 +00:00
|
|
|
std::unique_lock<std::shared_mutex> l(nw->lock);
|
2018-03-09 06:33:08 +00:00
|
|
|
|
|
|
|
nw->members[memberId] = memberConfig;
|
|
|
|
|
2023-04-21 19:12:43 +00:00
|
|
|
if (OSUtils::jsonBool(memberConfig["activeBridge"],false)) {
|
2018-03-09 06:33:08 +00:00
|
|
|
nw->activeBridgeMembers.insert(memberId);
|
2023-04-21 19:12:43 +00:00
|
|
|
}
|
2018-03-09 06:33:08 +00:00
|
|
|
isAuth = OSUtils::jsonBool(memberConfig["authorized"],false);
|
2023-04-21 19:12:43 +00:00
|
|
|
if (isAuth) {
|
|
|
|
Metrics::member_auths++;
|
2018-03-09 06:33:08 +00:00
|
|
|
nw->authorizedMembers.insert(memberId);
|
2023-04-21 19:12:43 +00:00
|
|
|
}
|
2018-03-09 06:33:08 +00:00
|
|
|
json &ips = memberConfig["ipAssignments"];
|
|
|
|
if (ips.is_array()) {
|
|
|
|
for(unsigned long i=0;i<ips.size();++i) {
|
|
|
|
json &ipj = ips[i];
|
|
|
|
if (ipj.is_string()) {
|
|
|
|
const std::string ips = ipj;
|
|
|
|
InetAddress ipa(ips.c_str());
|
|
|
|
ipa.setPort(0);
|
|
|
|
nw->allocatedIps.insert(ipa);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isAuth) {
|
|
|
|
const int64_t ldt = (int64_t)OSUtils::jsonInt(memberConfig["lastDeauthorizedTime"],0ULL);
|
|
|
|
if (ldt > nw->mostRecentDeauthTime)
|
|
|
|
nw->mostRecentDeauthTime = ldt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-06 15:42:54 +00:00
|
|
|
if (notifyListeners) {
|
2023-05-23 19:11:26 +00:00
|
|
|
std::unique_lock<std::shared_mutex> ll(_changeListeners_l);
|
2019-07-27 00:39:00 +00:00
|
|
|
for(auto i=_changeListeners.begin();i!=_changeListeners.end();++i) {
|
2019-08-06 12:51:50 +00:00
|
|
|
(*i)->onNetworkMemberUpdate(this,networkId,memberId,memberConfig);
|
2019-07-27 00:39:00 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-09 06:33:08 +00:00
|
|
|
} else if (memberId) {
|
|
|
|
if (nw) {
|
2023-05-23 19:11:26 +00:00
|
|
|
std::unique_lock<std::shared_mutex> l(nw->lock);
|
2018-03-09 06:33:08 +00:00
|
|
|
nw->members.erase(memberId);
|
|
|
|
}
|
|
|
|
if (networkId) {
|
2023-05-23 19:11:26 +00:00
|
|
|
std::unique_lock<std::shared_mutex> l(_networks_l);
|
2018-03-09 06:33:08 +00:00
|
|
|
auto er = _networkByMember.equal_range(memberId);
|
|
|
|
for(auto i=er.first;i!=er.second;++i) {
|
|
|
|
if (i->second == networkId) {
|
|
|
|
_networkByMember.erase(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-21 19:12:43 +00:00
|
|
|
if (notifyListeners) {
|
|
|
|
if(networkId != 0 && memberId != 0 && old.is_object() && !memberConfig.is_object()) {
|
|
|
|
// member delete
|
|
|
|
Metrics::member_count--;
|
|
|
|
} else if (networkId != 0 && memberId != 0 && !old.is_object() && memberConfig.is_object()) {
|
|
|
|
// new member
|
|
|
|
Metrics::member_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!wasAuth && isAuth) {
|
|
|
|
Metrics::member_auths++;
|
|
|
|
} else if (wasAuth && !isAuth) {
|
|
|
|
Metrics::member_deauths++;
|
|
|
|
} else {
|
|
|
|
Metrics::member_changes++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-06 15:42:54 +00:00
|
|
|
if ((notifyListeners)&&((wasAuth)&&(!isAuth)&&(networkId)&&(memberId))) {
|
2023-05-23 19:11:26 +00:00
|
|
|
std::unique_lock<std::shared_mutex> ll(_changeListeners_l);
|
2019-07-27 00:39:00 +00:00
|
|
|
for(auto i=_changeListeners.begin();i!=_changeListeners.end();++i) {
|
2019-08-06 12:51:50 +00:00
|
|
|
(*i)->onNetworkMemberDeauthorize(this,networkId,memberId);
|
2019-07-27 00:39:00 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-07 22:44:46 +00:00
|
|
|
}
|
|
|
|
|
2019-08-06 15:42:54 +00:00
|
|
|
void DB::_networkChanged(nlohmann::json &old,nlohmann::json &networkConfig,bool notifyListeners)
|
2017-11-07 22:44:46 +00:00
|
|
|
{
|
2023-05-23 19:11:26 +00:00
|
|
|
Metrics::db_network_change++;
|
2023-04-21 19:12:43 +00:00
|
|
|
if (notifyListeners) {
|
|
|
|
if (old.is_object() && old.contains("id") && networkConfig.is_object() && networkConfig.contains("id")) {
|
|
|
|
Metrics::network_changes++;
|
|
|
|
} else if (!old.is_object() && networkConfig.is_object() && networkConfig.contains("id")) {
|
|
|
|
Metrics::network_count++;
|
|
|
|
} else if (old.is_object() && old.contains("id") && !networkConfig.is_object()) {
|
|
|
|
Metrics::network_count--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-09 06:33:08 +00:00
|
|
|
if (networkConfig.is_object()) {
|
|
|
|
const std::string ids = networkConfig["id"];
|
2019-07-27 00:39:00 +00:00
|
|
|
const uint64_t networkId = Utils::hexStrToU64(ids.c_str());
|
|
|
|
if (networkId) {
|
2018-03-09 06:33:08 +00:00
|
|
|
std::shared_ptr<_Network> nw;
|
|
|
|
{
|
2023-05-23 19:11:26 +00:00
|
|
|
std::unique_lock<std::shared_mutex> l(_networks_l);
|
2019-07-27 00:39:00 +00:00
|
|
|
std::shared_ptr<_Network> &nw2 = _networks[networkId];
|
2018-03-09 06:33:08 +00:00
|
|
|
if (!nw2)
|
|
|
|
nw2.reset(new _Network);
|
|
|
|
nw = nw2;
|
|
|
|
}
|
|
|
|
{
|
2023-05-23 19:11:26 +00:00
|
|
|
std::unique_lock<std::shared_mutex> l2(nw->lock);
|
2018-03-09 06:33:08 +00:00
|
|
|
nw->config = networkConfig;
|
|
|
|
}
|
2019-08-06 15:42:54 +00:00
|
|
|
if (notifyListeners) {
|
2023-05-23 19:11:26 +00:00
|
|
|
std::unique_lock<std::shared_mutex> ll(_changeListeners_l);
|
2019-07-27 00:39:00 +00:00
|
|
|
for(auto i=_changeListeners.begin();i!=_changeListeners.end();++i) {
|
2019-08-06 12:51:50 +00:00
|
|
|
(*i)->onNetworkUpdate(this,networkId,networkConfig);
|
2019-07-27 00:39:00 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-09 06:33:08 +00:00
|
|
|
}
|
|
|
|
} else if (old.is_object()) {
|
|
|
|
const std::string ids = old["id"];
|
2019-07-27 00:39:00 +00:00
|
|
|
const uint64_t networkId = Utils::hexStrToU64(ids.c_str());
|
|
|
|
if (networkId) {
|
2023-05-23 19:11:26 +00:00
|
|
|
std::unique_lock<std::shared_mutex> l(_networks_l);
|
2019-07-27 00:39:00 +00:00
|
|
|
_networks.erase(networkId);
|
2018-03-09 06:33:08 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-07 22:44:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DB::_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));
|
2018-01-12 18:38:19 +00:00
|
|
|
std::sort(info.activeBridges.begin(),info.activeBridges.end());
|
2017-11-07 22:44:46 +00:00
|
|
|
for(auto ip=nw->allocatedIps.begin();ip!=nw->allocatedIps.end();++ip)
|
|
|
|
info.allocatedIps.push_back(*ip);
|
2018-01-12 18:38:19 +00:00
|
|
|
std::sort(info.allocatedIps.begin(),info.allocatedIps.end());
|
2017-11-07 22:44:46 +00:00
|
|
|
info.authorizedMemberCount = (unsigned long)nw->authorizedMembers.size();
|
|
|
|
info.totalMemberCount = (unsigned long)nw->members.size();
|
|
|
|
info.mostRecentDeauthTime = nw->mostRecentDeauthTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|