clang-format this branch

This commit is contained in:
Adam Ierymenko 2025-07-03 12:02:18 -04:00
parent 8b77ef538a
commit 342fa9d33f
No known key found for this signature in database
GPG key ID: C8877CF2D7A5D7F3
135 changed files with 42729 additions and 42439 deletions

View file

@ -57,7 +57,7 @@ SpacesInCStyleCastParentheses: 'false'
SpacesInContainerLiterals: 'true' SpacesInContainerLiterals: 'true'
SpacesInParentheses: 'false' SpacesInParentheses: 'false'
SpacesInSquareBrackets: 'false' SpacesInSquareBrackets: 'false'
UseTab: 'Never' UseTab: 'Always'
--- ---
Language: Cpp Language: Cpp

View file

@ -14,7 +14,6 @@
#ifndef ZT_CONNECTION_POOL_H_ #ifndef ZT_CONNECTION_POOL_H_
#define ZT_CONNECTION_POOL_H_ #define ZT_CONNECTION_POOL_H_
#ifndef _DEBUG #ifndef _DEBUG
#define _DEBUG(x) #define _DEBUG(x)
#endif #endif
@ -22,21 +21,21 @@
#include "../node/Metrics.hpp" #include "../node/Metrics.hpp"
#include <deque> #include <deque>
#include <set> #include <exception>
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <exception> #include <set>
#include <string> #include <string>
namespace ZeroTier { namespace ZeroTier {
struct ConnectionUnavailable : std::exception { struct ConnectionUnavailable : std::exception {
char const* what() const throw() { char const* what() const throw()
{
return "Unable to allocate connection"; return "Unable to allocate connection";
}; };
}; };
class Connection { class Connection {
public: public:
virtual ~Connection() {}; virtual ~Connection() {};
@ -53,13 +52,9 @@ struct ConnectionPoolStats {
size_t borrowed_size; size_t borrowed_size;
}; };
template<class T> template <class T> class ConnectionPool {
class ConnectionPool {
public: public:
ConnectionPool(size_t max_pool_size, size_t min_pool_size, std::shared_ptr<ConnectionFactory> factory) ConnectionPool(size_t max_pool_size, size_t min_pool_size, std::shared_ptr<ConnectionFactory> factory) : m_maxPoolSize(max_pool_size), m_minPoolSize(min_pool_size), m_factory(factory)
: m_maxPoolSize(max_pool_size)
, m_minPoolSize(min_pool_size)
, m_factory(factory)
{ {
Metrics::max_pool_size += max_pool_size; Metrics::max_pool_size += max_pool_size;
Metrics::min_pool_size += min_pool_size; Metrics::min_pool_size += min_pool_size;
@ -69,7 +64,8 @@ public:
} }
}; };
ConnectionPoolStats get_stats() { ConnectionPoolStats get_stats()
{
std::unique_lock<std::mutex> lock(m_poolMutex); std::unique_lock<std::mutex> lock(m_poolMutex);
ConnectionPoolStats stats; ConnectionPoolStats stats;
@ -79,8 +75,7 @@ public:
return stats; return stats;
}; };
~ConnectionPool() { ~ConnectionPool() {};
};
/** /**
* Borrow * Borrow
@ -90,7 +85,8 @@ public:
* When done, either (a) call unborrow() to return it, or (b) (if it's bad) just let it go out of scope. This will cause it to automatically be replaced. * When done, either (a) call unborrow() to return it, or (b) (if it's bad) just let it go out of scope. This will cause it to automatically be replaced.
* @retval a shared_ptr to the connection object * @retval a shared_ptr to the connection object
*/ */
std::shared_ptr<T> borrow() { std::shared_ptr<T> borrow()
{
std::unique_lock<std::mutex> l(m_poolMutex); std::unique_lock<std::mutex> l(m_poolMutex);
while ((m_pool.size() + m_borrowed.size()) < m_minPoolSize) { while ((m_pool.size() + m_borrowed.size()) < m_minPoolSize) {
@ -100,18 +96,19 @@ public:
} }
if (m_pool.size() == 0) { if (m_pool.size() == 0) {
if ((m_pool.size() + m_borrowed.size()) < m_maxPoolSize) { if ((m_pool.size() + m_borrowed.size()) < m_maxPoolSize) {
try { try {
std::shared_ptr<Connection> conn = m_factory->create(); std::shared_ptr<Connection> conn = m_factory->create();
m_borrowed.insert(conn); m_borrowed.insert(conn);
Metrics::pool_in_use++; Metrics::pool_in_use++;
return std::static_pointer_cast<T>(conn); return std::static_pointer_cast<T>(conn);
} catch (std::exception &e) { }
catch (std::exception& e) {
Metrics::pool_errors++; Metrics::pool_errors++;
throw ConnectionUnavailable(); throw ConnectionUnavailable();
} }
} else { }
else {
for (auto it = m_borrowed.begin(); it != m_borrowed.end(); ++it) { for (auto it = m_borrowed.begin(); it != m_borrowed.end(); ++it) {
if ((*it).unique()) { if ((*it).unique()) {
// This connection has been abandoned! Destroy it and create a new connection // This connection has been abandoned! Destroy it and create a new connection
@ -122,7 +119,8 @@ public:
m_borrowed.erase(it); m_borrowed.erase(it);
m_borrowed.insert(conn); m_borrowed.insert(conn);
return std::static_pointer_cast<T>(conn); return std::static_pointer_cast<T>(conn);
} catch(std::exception& e) { }
catch (std::exception& e) {
// Error creating a replacement connection // Error creating a replacement connection
Metrics::pool_errors++; Metrics::pool_errors++;
throw ConnectionUnavailable(); throw ConnectionUnavailable();
@ -151,7 +149,8 @@ public:
* Only call this if you are returning a working connection. If the connection was bad, just let it go out of scope (so the connection manager can replace it). * Only call this if you are returning a working connection. If the connection was bad, just let it go out of scope (so the connection manager can replace it).
* @param the connection * @param the connection
*/ */
void unborrow(std::shared_ptr<T> conn) { void unborrow(std::shared_ptr<T> conn)
{
// Lock // Lock
std::unique_lock<std::mutex> lock(m_poolMutex); std::unique_lock<std::mutex> lock(m_poolMutex);
m_borrowed.erase(conn); m_borrowed.erase(conn);
@ -161,6 +160,7 @@ public:
m_pool.push_back(conn); m_pool.push_back(conn);
} }
}; };
protected: protected:
size_t m_maxPoolSize; size_t m_maxPoolSize;
size_t m_minPoolSize; size_t m_minPoolSize;
@ -170,6 +170,6 @@ protected:
std::mutex m_poolMutex; std::mutex m_poolMutex;
}; };
} } // namespace ZeroTier
#endif #endif

View file

@ -12,11 +12,12 @@
/****/ /****/
#include "DB.hpp" #include "DB.hpp"
#include "EmbeddedNetworkController.hpp"
#include "../node/Metrics.hpp"
#include <chrono> #include "../node/Metrics.hpp"
#include "EmbeddedNetworkController.hpp"
#include <algorithm> #include <algorithm>
#include <chrono>
#include <stdexcept> #include <stdexcept>
using json = nlohmann::json; using json = nlohmann::json;
@ -25,60 +26,96 @@ namespace ZeroTier {
void DB::initNetwork(nlohmann::json& network) void DB::initNetwork(nlohmann::json& network)
{ {
if (!network.count("private")) network["private"] = true; if (! network.count("private"))
if (!network.count("creationTime")) network["creationTime"] = OSUtils::now(); network["private"] = true;
if (!network.count("name")) network["name"] = ""; if (! network.count("creationTime"))
if (!network.count("multicastLimit")) network["multicastLimit"] = (uint64_t)32; network["creationTime"] = OSUtils::now();
if (!network.count("enableBroadcast")) network["enableBroadcast"] = true; if (! network.count("name"))
if (!network.count("v4AssignMode")) network["v4AssignMode"] = {{"zt",false}}; network["name"] = "";
if (!network.count("v6AssignMode")) network["v6AssignMode"] = {{"rfc4193",false},{"zt",false},{"6plane",false}}; if (! network.count("multicastLimit"))
if (!network.count("authTokens")) network["authTokens"] = {{}}; network["multicastLimit"] = (uint64_t)32;
if (!network.count("capabilities")) network["capabilities"] = nlohmann::json::array(); if (! network.count("enableBroadcast"))
if (!network.count("tags")) network["tags"] = nlohmann::json::array(); network["enableBroadcast"] = true;
if (!network.count("routes")) network["routes"] = nlohmann::json::array(); if (! network.count("v4AssignMode"))
if (!network.count("ipAssignmentPools")) network["ipAssignmentPools"] = nlohmann::json::array(); network["v4AssignMode"] = { { "zt", false } };
if (!network.count("mtu")) network["mtu"] = ZT_DEFAULT_MTU; if (! network.count("v6AssignMode"))
if (!network.count("remoteTraceTarget")) network["remoteTraceTarget"] = nlohmann::json(); network["v6AssignMode"] = { { "rfc4193", false }, { "zt", false }, { "6plane", false } };
if (!network.count("removeTraceLevel")) network["remoteTraceLevel"] = 0; if (! network.count("authTokens"))
if (!network.count("rulesSource")) network["rulesSource"] = ""; 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;
if (! network.count("rulesSource"))
network["rulesSource"] = "";
if (! network.count("rules")) { if (! network.count("rules")) {
// If unspecified, rules are set to allow anything and behave like a flat L2 segment // If unspecified, rules are set to allow anything and behave like a flat L2 segment
network["rules"] = {{ network["rules"] = { { { "not", false }, { "or", false }, { "type", "ACTION_ACCEPT" } } };
{ "not",false },
{ "or", false },
{ "type","ACTION_ACCEPT" }
}};
} }
if (!network.count("dns")) network["dns"] = nlohmann::json::array(); if (! network.count("dns"))
if (!network.count("ssoEnabled")) network["ssoEnabled"] = false; network["dns"] = nlohmann::json::array();
if (!network.count("clientId")) network["clientId"] = ""; if (! network.count("ssoEnabled"))
if (!network.count("authorizationEndpoint")) network["authorizationEndpoint"] = ""; network["ssoEnabled"] = false;
if (! network.count("clientId"))
network["clientId"] = "";
if (! network.count("authorizationEndpoint"))
network["authorizationEndpoint"] = "";
network["objtype"] = "network"; network["objtype"] = "network";
} }
void DB::initMember(nlohmann::json& member) void DB::initMember(nlohmann::json& member)
{ {
if (!member.count("authorized")) member["authorized"] = false; if (! member.count("authorized"))
if (!member.count("ssoExempt")) member["ssoExempt"] = false; member["authorized"] = false;
if (!member.count("ipAssignments")) member["ipAssignments"] = nlohmann::json::array(); if (! member.count("ssoExempt"))
if (!member.count("activeBridge")) member["activeBridge"] = false; member["ssoExempt"] = false;
if (!member.count("tags")) member["tags"] = nlohmann::json::array(); if (! member.count("ipAssignments"))
if (!member.count("capabilities")) member["capabilities"] = nlohmann::json::array(); member["ipAssignments"] = nlohmann::json::array();
if (!member.count("creationTime")) member["creationTime"] = OSUtils::now(); if (! member.count("activeBridge"))
if (!member.count("noAutoAssignIps")) member["noAutoAssignIps"] = false; member["activeBridge"] = false;
if (!member.count("revision")) member["revision"] = 0ULL; if (! member.count("tags"))
if (!member.count("lastDeauthorizedTime")) member["lastDeauthorizedTime"] = 0ULL; member["tags"] = nlohmann::json::array();
if (!member.count("lastAuthorizedTime")) member["lastAuthorizedTime"] = 0ULL; if (! member.count("capabilities"))
if (!member.count("lastAuthorizedCredentialType")) member["lastAuthorizedCredentialType"] = nlohmann::json(); member["capabilities"] = nlohmann::json::array();
if (!member.count("lastAuthorizedCredential")) member["lastAuthorizedCredential"] = nlohmann::json(); if (! member.count("creationTime"))
if (!member.count("authenticationExpiryTime")) member["authenticationExpiryTime"] = 0LL; member["creationTime"] = OSUtils::now();
if (!member.count("vMajor")) member["vMajor"] = -1; if (! member.count("noAutoAssignIps"))
if (!member.count("vMinor")) member["vMinor"] = -1; member["noAutoAssignIps"] = false;
if (!member.count("vRev")) member["vRev"] = -1; if (! member.count("revision"))
if (!member.count("vProto")) member["vProto"] = -1; member["revision"] = 0ULL;
if (!member.count("remoteTraceTarget")) member["remoteTraceTarget"] = nlohmann::json(); if (! member.count("lastDeauthorizedTime"))
if (!member.count("removeTraceLevel")) member["remoteTraceLevel"] = 0; 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();
if (! member.count("authenticationExpiryTime"))
member["authenticationExpiryTime"] = 0LL;
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"; member["objtype"] = "member";
} }
@ -102,8 +139,12 @@ void DB::cleanMember(nlohmann::json &member)
member.erase("authenticationClientID"); // computed member.erase("authenticationClientID"); // computed
} }
DB::DB() {} DB::DB()
DB::~DB() {} {
}
DB::~DB()
{
}
bool DB::get(const uint64_t networkId, nlohmann::json& network) bool DB::get(const uint64_t networkId, nlohmann::json& network)
{ {
@ -299,7 +340,8 @@ void DB::_memberChanged(nlohmann::json &old,nlohmann::json &memberConfig,bool no
(*i)->onNetworkMemberUpdate(this, networkId, memberId, memberConfig); (*i)->onNetworkMemberUpdate(this, networkId, memberId, memberConfig);
} }
} }
} else if (memberId) { }
else if (memberId) {
if (nw) { if (nw) {
std::unique_lock<std::shared_mutex> l(nw->lock); std::unique_lock<std::shared_mutex> l(nw->lock);
nw->members.erase(memberId); nw->members.erase(memberId);
@ -320,16 +362,19 @@ void DB::_memberChanged(nlohmann::json &old,nlohmann::json &memberConfig,bool no
if (networkId != 0 && memberId != 0 && old.is_object() && ! memberConfig.is_object()) { if (networkId != 0 && memberId != 0 && old.is_object() && ! memberConfig.is_object()) {
// member delete // member delete
Metrics::member_count--; Metrics::member_count--;
} else if (networkId != 0 && memberId != 0 && !old.is_object() && memberConfig.is_object()) { }
else if (networkId != 0 && memberId != 0 && ! old.is_object() && memberConfig.is_object()) {
// new member // new member
Metrics::member_count++; Metrics::member_count++;
} }
if (! wasAuth && isAuth) { if (! wasAuth && isAuth) {
Metrics::member_auths++; Metrics::member_auths++;
} else if (wasAuth && !isAuth) { }
else if (wasAuth && ! isAuth) {
Metrics::member_deauths++; Metrics::member_deauths++;
} else { }
else {
Metrics::member_changes++; Metrics::member_changes++;
} }
} }
@ -348,9 +393,11 @@ void DB::_networkChanged(nlohmann::json &old,nlohmann::json &networkConfig,bool
if (notifyListeners) { if (notifyListeners) {
if (old.is_object() && old.contains("id") && networkConfig.is_object() && networkConfig.contains("id")) { if (old.is_object() && old.contains("id") && networkConfig.is_object() && networkConfig.contains("id")) {
Metrics::network_changes++; Metrics::network_changes++;
} else if (!old.is_object() && networkConfig.is_object() && networkConfig.contains("id")) { }
else if (! old.is_object() && networkConfig.is_object() && networkConfig.contains("id")) {
Metrics::network_count++; Metrics::network_count++;
} else if (old.is_object() && old.contains("id") && !networkConfig.is_object()) { }
else if (old.is_object() && old.contains("id") && ! networkConfig.is_object()) {
Metrics::network_count--; Metrics::network_count--;
} }
} }
@ -378,7 +425,8 @@ void DB::_networkChanged(nlohmann::json &old,nlohmann::json &networkConfig,bool
} }
} }
} }
} else if (old.is_object()) { }
else if (old.is_object()) {
const std::string ids = old["id"]; const std::string ids = old["id"];
const uint64_t networkId = Utils::hexStrToU64(ids.c_str()); const uint64_t networkId = Utils::hexStrToU64(ids.c_str());
if (networkId) { if (networkId) {
@ -395,7 +443,8 @@ void DB::_networkChanged(nlohmann::json &old,nlohmann::json &networkConfig,bool
(*j)->onNetworkMemberDeauthorize(this, networkId, memberId); (*j)->onNetworkMemberDeauthorize(this, networkId, memberId);
} }
} }
} catch (std::exception &e) { }
catch (std::exception& e) {
std::cerr << "Error deauthorizing members on network delete: " << e.what() << std::endl; std::cerr << "Error deauthorizing members on network delete: " << e.what() << std::endl;
} }

View file

@ -19,44 +19,31 @@
#include "../node/Constants.hpp" #include "../node/Constants.hpp"
#include "../node/Identity.hpp" #include "../node/Identity.hpp"
#include "../node/InetAddress.hpp" #include "../node/InetAddress.hpp"
#include "../osdep/OSUtils.hpp"
#include "../osdep/BlockingQueue.hpp" #include "../osdep/BlockingQueue.hpp"
#include "../osdep/OSUtils.hpp"
#include <atomic>
#include <map>
#include <memory> #include <memory>
#include <nlohmann/json.hpp>
#include <prometheus/simpleapi.h>
#include <set>
#include <shared_mutex>
#include <string> #include <string>
#include <thread> #include <thread>
#include <unordered_map> #include <unordered_map>
#include <unordered_set> #include <unordered_set>
#include <vector> #include <vector>
#include <atomic>
#include <shared_mutex>
#include <set>
#include <map>
#include <nlohmann/json.hpp>
#include <prometheus/simpleapi.h>
#define ZT_MEMBER_AUTH_TIMEOUT_NOTIFY_BEFORE 25000 #define ZT_MEMBER_AUTH_TIMEOUT_NOTIFY_BEFORE 25000
namespace ZeroTier namespace ZeroTier {
{
struct AuthInfo struct AuthInfo {
{
public: public:
AuthInfo() AuthInfo() : enabled(false), version(0), authenticationURL(), authenticationExpiryTime(0), issuerURL(), centralAuthURL(), ssoNonce(), ssoState(), ssoClientID(), ssoProvider("default")
: enabled(false) {
, version(0) }
, authenticationURL()
, authenticationExpiryTime(0)
, issuerURL()
, centralAuthURL()
, ssoNonce()
, ssoState()
, ssoClientID()
, ssoProvider("default")
{}
bool enabled; bool enabled;
uint64_t version; uint64_t version;
@ -73,22 +60,31 @@ public:
/** /**
* Base class with common infrastructure for all controller DB implementations * Base class with common infrastructure for all controller DB implementations
*/ */
class DB class DB {
{
public: public:
class ChangeListener class ChangeListener {
{
public: public:
ChangeListener() {} ChangeListener()
virtual ~ChangeListener() {} {
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 ~ChangeListener()
virtual void onNetworkMemberDeauthorize(const void *db,uint64_t networkId,uint64_t memberId) {} {
}
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)
{
}
}; };
struct NetworkSummaryInfo struct NetworkSummaryInfo {
NetworkSummaryInfo() : authorizedMemberCount(0), totalMemberCount(0), mostRecentDeauthTime(0)
{ {
NetworkSummaryInfo() : authorizedMemberCount(0),totalMemberCount(0),mostRecentDeauthTime(0) {} }
std::vector<Address> activeBridges; std::vector<Address> activeBridges;
std::vector<InetAddress> allocatedIps; std::vector<InetAddress> allocatedIps;
unsigned long authorizedMemberCount; unsigned long authorizedMemberCount;
@ -120,8 +116,7 @@ public:
void networks(std::set<uint64_t>& networks); void networks(std::set<uint64_t>& networks);
template<typename F> template <typename F> inline void each(F f)
inline void each(F f)
{ {
nlohmann::json nullJson; nlohmann::json nullJson;
std::unique_lock<std::shared_mutex> lck(_networks_l); std::unique_lock<std::shared_mutex> lck(_networks_l);
@ -138,7 +133,10 @@ public:
virtual void eraseMember(const uint64_t networkId, const uint64_t memberId) = 0; virtual void eraseMember(const uint64_t networkId, const uint64_t memberId) = 0;
virtual void nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress& physicalAddress) = 0; virtual void nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress& physicalAddress) = 0;
virtual AuthInfo getSSOAuthInfo(const nlohmann::json &member, const std::string &redirectURL) { return AuthInfo(); } virtual AuthInfo getSSOAuthInfo(const nlohmann::json& member, const std::string& redirectURL)
{
return AuthInfo();
}
inline void addListener(DB::ChangeListener* const listener) inline void addListener(DB::ChangeListener* const listener)
{ {
@ -169,9 +167,10 @@ protected:
return false; return false;
} }
struct _Network struct _Network {
_Network() : mostRecentDeauthTime(0)
{ {
_Network() : mostRecentDeauthTime(0) {} }
nlohmann::json config; nlohmann::json config;
std::unordered_map<uint64_t, nlohmann::json> members; std::unordered_map<uint64_t, nlohmann::json> members;
std::unordered_set<uint64_t> activeBridgeMembers; std::unordered_set<uint64_t> activeBridgeMembers;

View file

@ -15,12 +15,7 @@
namespace ZeroTier { namespace ZeroTier {
DBMirrorSet::DBMirrorSet(DB::ChangeListener *listener) DBMirrorSet::DBMirrorSet(DB::ChangeListener* listener) : _listener(listener), _running(true), _syncCheckerThread(), _dbs(), _dbs_l()
: _listener(listener)
, _running(true)
, _syncCheckerThread()
, _dbs()
, _dbs_l()
{ {
_syncCheckerThread = std::thread([this]() { _syncCheckerThread = std::thread([this]() {
for (;;) { for (;;) {
@ -52,7 +47,8 @@ DBMirrorSet::DBMirrorSet(DB::ChangeListener *listener)
} }
} }
} }
} else if (member.is_object()) { }
else if (member.is_object()) {
for (auto db2 = dbs.begin(); db2 != dbs.end(); ++db2) { for (auto db2 = dbs.begin(); db2 != dbs.end(); ++db2) {
if (db->get() != db2->get()) { if (db->get() != db2->get()) {
nlohmann::json nw2, m2; nlohmann::json nw2, m2;
@ -64,7 +60,9 @@ DBMirrorSet::DBMirrorSet(DB::ChangeListener *listener)
} }
} }
} }
} catch ( ... ) {} // skip entries that generate JSON errors }
catch (...) {
} // skip entries that generate JSON errors
}); });
} }
} }
@ -181,7 +179,8 @@ bool DBMirrorSet::save(nlohmann::json &record,bool notifyListeners)
return true; return true;
} }
return false; return false;
} else { }
else {
bool modified = false; bool modified = false;
for (auto d = dbs.begin(); d != dbs.end(); ++d) { for (auto d = dbs.begin(); d != dbs.end(); ++d) {
modified |= (*d)->save(record, false); modified |= (*d)->save(record, false);

View file

@ -16,16 +16,15 @@
#include "DB.hpp" #include "DB.hpp"
#include <vector>
#include <memory> #include <memory>
#include <shared_mutex>
#include <set> #include <set>
#include <shared_mutex>
#include <thread> #include <thread>
#include <vector>
namespace ZeroTier { namespace ZeroTier {
class DBMirrorSet : public DB::ChangeListener class DBMirrorSet : public DB::ChangeListener {
{
public: public:
DBMirrorSet(DB::ChangeListener* listener); DBMirrorSet(DB::ChangeListener* listener);
virtual ~DBMirrorSet(); virtual ~DBMirrorSet();

View file

@ -21,33 +21,31 @@
#ifndef _WIN32 #ifndef _WIN32
#include <sys/time.h> #include <sys/time.h>
#endif #endif
#include <sys/types.h>
#include <algorithm>
#include <utility>
#include <stdexcept>
#include <map>
#include <thread>
#include <memory>
#include <iomanip>
#include <sstream>
#include <cctype>
#include "../include/ZeroTierOne.h" #include "../include/ZeroTierOne.h"
#include "../version.h" #include "../version.h"
#include "EmbeddedNetworkController.hpp" #include "EmbeddedNetworkController.hpp"
#include "LFDB.hpp"
#include "FileDB.hpp" #include "FileDB.hpp"
#include "LFDB.hpp"
#include <algorithm>
#include <cctype>
#include <iomanip>
#include <map>
#include <memory>
#include <sstream>
#include <stdexcept>
#include <sys/types.h>
#include <thread>
#include <utility>
#ifdef ZT_CONTROLLER_USE_LIBPQ #ifdef ZT_CONTROLLER_USE_LIBPQ
#include "PostgreSQL.hpp" #include "PostgreSQL.hpp"
#endif #endif
#include "../node/Node.hpp"
#include "../node/CertificateOfMembership.hpp" #include "../node/CertificateOfMembership.hpp"
#include "../node/NetworkConfig.hpp"
#include "../node/Dictionary.hpp" #include "../node/Dictionary.hpp"
#include "../node/MAC.hpp" #include "../node/MAC.hpp"
#include "../node/NetworkConfig.hpp"
#include "../node/Node.hpp"
using json = nlohmann::json; using json = nlohmann::json;
@ -125,12 +123,30 @@ static json _renderRule(ZT_VirtualNetworkRule &rule)
break; break;
case ZT_NETWORK_RULE_MATCH_MAC_SOURCE: case ZT_NETWORK_RULE_MATCH_MAC_SOURCE:
r["type"] = "MATCH_MAC_SOURCE"; r["type"] = "MATCH_MAC_SOURCE";
OSUtils::ztsnprintf(tmp,sizeof(tmp),"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",(unsigned int)rule.v.mac[0],(unsigned int)rule.v.mac[1],(unsigned int)rule.v.mac[2],(unsigned int)rule.v.mac[3],(unsigned int)rule.v.mac[4],(unsigned int)rule.v.mac[5]); OSUtils::ztsnprintf(
tmp,
sizeof(tmp),
"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
(unsigned int)rule.v.mac[0],
(unsigned int)rule.v.mac[1],
(unsigned int)rule.v.mac[2],
(unsigned int)rule.v.mac[3],
(unsigned int)rule.v.mac[4],
(unsigned int)rule.v.mac[5]);
r["mac"] = tmp; r["mac"] = tmp;
break; break;
case ZT_NETWORK_RULE_MATCH_MAC_DEST: case ZT_NETWORK_RULE_MATCH_MAC_DEST:
r["type"] = "MATCH_MAC_DEST"; r["type"] = "MATCH_MAC_DEST";
OSUtils::ztsnprintf(tmp,sizeof(tmp),"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",(unsigned int)rule.v.mac[0],(unsigned int)rule.v.mac[1],(unsigned int)rule.v.mac[2],(unsigned int)rule.v.mac[3],(unsigned int)rule.v.mac[4],(unsigned int)rule.v.mac[5]); OSUtils::ztsnprintf(
tmp,
sizeof(tmp),
"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
(unsigned int)rule.v.mac[0],
(unsigned int)rule.v.mac[1],
(unsigned int)rule.v.mac[2],
(unsigned int)rule.v.mac[3],
(unsigned int)rule.v.mac[4],
(unsigned int)rule.v.mac[5]);
r["mac"] = tmp; r["mac"] = tmp;
break; break;
case ZT_NETWORK_RULE_MATCH_IPV4_SOURCE: case ZT_NETWORK_RULE_MATCH_IPV4_SOURCE:
@ -168,7 +184,8 @@ static json _renderRule(ZT_VirtualNetworkRule &rule)
r["icmpType"] = (unsigned int)rule.v.icmp.type; r["icmpType"] = (unsigned int)rule.v.icmp.type;
if ((rule.v.icmp.flags & 0x01) != 0) if ((rule.v.icmp.flags & 0x01) != 0)
r["icmpCode"] = (unsigned int)rule.v.icmp.code; r["icmpCode"] = (unsigned int)rule.v.icmp.code;
else r["icmpCode"] = json(); else
r["icmpCode"] = json();
break; break;
case ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE: case ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE:
r["type"] = "MATCH_IP_SOURCE_PORT_RANGE"; r["type"] = "MATCH_IP_SOURCE_PORT_RANGE";
@ -262,7 +279,8 @@ static bool _parseRule(json &r,ZT_VirtualNetworkRule &rule)
if (OSUtils::jsonBool(r["not"], false)) if (OSUtils::jsonBool(r["not"], false))
rule.t = 0x80; rule.t = 0x80;
else rule.t = 0x00; else
rule.t = 0x00;
if (OSUtils::jsonBool(r["or"], false)) if (OSUtils::jsonBool(r["or"], false))
rule.t |= 0x40; rule.t |= 0x40;
@ -270,180 +288,221 @@ static bool _parseRule(json &r,ZT_VirtualNetworkRule &rule)
if (t == "ACTION_DROP") { if (t == "ACTION_DROP") {
rule.t |= ZT_NETWORK_RULE_ACTION_DROP; rule.t |= ZT_NETWORK_RULE_ACTION_DROP;
return true; return true;
} else if (t == "ACTION_ACCEPT") { }
else if (t == "ACTION_ACCEPT") {
rule.t |= ZT_NETWORK_RULE_ACTION_ACCEPT; rule.t |= ZT_NETWORK_RULE_ACTION_ACCEPT;
return true; return true;
} else if (t == "ACTION_TEE") { }
else if (t == "ACTION_TEE") {
rule.t |= ZT_NETWORK_RULE_ACTION_TEE; rule.t |= ZT_NETWORK_RULE_ACTION_TEE;
rule.v.fwd.address = Utils::hexStrToU64(OSUtils::jsonString(r["address"], "0").c_str()) & 0xffffffffffULL; rule.v.fwd.address = Utils::hexStrToU64(OSUtils::jsonString(r["address"], "0").c_str()) & 0xffffffffffULL;
rule.v.fwd.flags = (uint32_t)(OSUtils::jsonInt(r["flags"], 0ULL) & 0xffffffffULL); rule.v.fwd.flags = (uint32_t)(OSUtils::jsonInt(r["flags"], 0ULL) & 0xffffffffULL);
rule.v.fwd.length = (uint16_t)(OSUtils::jsonInt(r["length"], 0ULL) & 0xffffULL); rule.v.fwd.length = (uint16_t)(OSUtils::jsonInt(r["length"], 0ULL) & 0xffffULL);
return true; return true;
} else if (t == "ACTION_WATCH") { }
else if (t == "ACTION_WATCH") {
rule.t |= ZT_NETWORK_RULE_ACTION_WATCH; rule.t |= ZT_NETWORK_RULE_ACTION_WATCH;
rule.v.fwd.address = Utils::hexStrToU64(OSUtils::jsonString(r["address"], "0").c_str()) & 0xffffffffffULL; rule.v.fwd.address = Utils::hexStrToU64(OSUtils::jsonString(r["address"], "0").c_str()) & 0xffffffffffULL;
rule.v.fwd.flags = (uint32_t)(OSUtils::jsonInt(r["flags"], 0ULL) & 0xffffffffULL); rule.v.fwd.flags = (uint32_t)(OSUtils::jsonInt(r["flags"], 0ULL) & 0xffffffffULL);
rule.v.fwd.length = (uint16_t)(OSUtils::jsonInt(r["length"], 0ULL) & 0xffffULL); rule.v.fwd.length = (uint16_t)(OSUtils::jsonInt(r["length"], 0ULL) & 0xffffULL);
return true; return true;
} else if (t == "ACTION_REDIRECT") { }
else if (t == "ACTION_REDIRECT") {
rule.t |= ZT_NETWORK_RULE_ACTION_REDIRECT; rule.t |= ZT_NETWORK_RULE_ACTION_REDIRECT;
rule.v.fwd.address = Utils::hexStrToU64(OSUtils::jsonString(r["address"], "0").c_str()) & 0xffffffffffULL; rule.v.fwd.address = Utils::hexStrToU64(OSUtils::jsonString(r["address"], "0").c_str()) & 0xffffffffffULL;
rule.v.fwd.flags = (uint32_t)(OSUtils::jsonInt(r["flags"], 0ULL) & 0xffffffffULL); rule.v.fwd.flags = (uint32_t)(OSUtils::jsonInt(r["flags"], 0ULL) & 0xffffffffULL);
return true; return true;
} else if (t == "ACTION_BREAK") { }
else if (t == "ACTION_BREAK") {
rule.t |= ZT_NETWORK_RULE_ACTION_BREAK; rule.t |= ZT_NETWORK_RULE_ACTION_BREAK;
return true; return true;
} else if (t == "MATCH_SOURCE_ZEROTIER_ADDRESS") { }
else if (t == "MATCH_SOURCE_ZEROTIER_ADDRESS") {
rule.t |= ZT_NETWORK_RULE_MATCH_SOURCE_ZEROTIER_ADDRESS; rule.t |= ZT_NETWORK_RULE_MATCH_SOURCE_ZEROTIER_ADDRESS;
rule.v.zt = Utils::hexStrToU64(OSUtils::jsonString(r["zt"], "0").c_str()) & 0xffffffffffULL; rule.v.zt = Utils::hexStrToU64(OSUtils::jsonString(r["zt"], "0").c_str()) & 0xffffffffffULL;
return true; return true;
} else if (t == "MATCH_DEST_ZEROTIER_ADDRESS") { }
else if (t == "MATCH_DEST_ZEROTIER_ADDRESS") {
rule.t |= ZT_NETWORK_RULE_MATCH_DEST_ZEROTIER_ADDRESS; rule.t |= ZT_NETWORK_RULE_MATCH_DEST_ZEROTIER_ADDRESS;
rule.v.zt = Utils::hexStrToU64(OSUtils::jsonString(r["zt"], "0").c_str()) & 0xffffffffffULL; rule.v.zt = Utils::hexStrToU64(OSUtils::jsonString(r["zt"], "0").c_str()) & 0xffffffffffULL;
return true; return true;
} else if (t == "MATCH_VLAN_ID") { }
else if (t == "MATCH_VLAN_ID") {
rule.t |= ZT_NETWORK_RULE_MATCH_VLAN_ID; rule.t |= ZT_NETWORK_RULE_MATCH_VLAN_ID;
rule.v.vlanId = (uint16_t)(OSUtils::jsonInt(r["vlanId"], 0ULL) & 0xffffULL); rule.v.vlanId = (uint16_t)(OSUtils::jsonInt(r["vlanId"], 0ULL) & 0xffffULL);
return true; return true;
} else if (t == "MATCH_VLAN_PCP") { }
else if (t == "MATCH_VLAN_PCP") {
rule.t |= ZT_NETWORK_RULE_MATCH_VLAN_PCP; rule.t |= ZT_NETWORK_RULE_MATCH_VLAN_PCP;
rule.v.vlanPcp = (uint8_t)(OSUtils::jsonInt(r["vlanPcp"], 0ULL) & 0xffULL); rule.v.vlanPcp = (uint8_t)(OSUtils::jsonInt(r["vlanPcp"], 0ULL) & 0xffULL);
return true; return true;
} else if (t == "MATCH_VLAN_DEI") { }
else if (t == "MATCH_VLAN_DEI") {
rule.t |= ZT_NETWORK_RULE_MATCH_VLAN_DEI; rule.t |= ZT_NETWORK_RULE_MATCH_VLAN_DEI;
rule.v.vlanDei = (uint8_t)(OSUtils::jsonInt(r["vlanDei"], 0ULL) & 0xffULL); rule.v.vlanDei = (uint8_t)(OSUtils::jsonInt(r["vlanDei"], 0ULL) & 0xffULL);
return true; return true;
} else if (t == "MATCH_MAC_SOURCE") { }
else if (t == "MATCH_MAC_SOURCE") {
rule.t |= ZT_NETWORK_RULE_MATCH_MAC_SOURCE; rule.t |= ZT_NETWORK_RULE_MATCH_MAC_SOURCE;
std::string mac(OSUtils::jsonString(r["mac"], "0")); std::string mac(OSUtils::jsonString(r["mac"], "0"));
Utils::cleanMac(mac); Utils::cleanMac(mac);
Utils::unhex(mac.c_str(), (unsigned int)mac.length(), rule.v.mac, 6); Utils::unhex(mac.c_str(), (unsigned int)mac.length(), rule.v.mac, 6);
return true; return true;
} else if (t == "MATCH_MAC_DEST") { }
else if (t == "MATCH_MAC_DEST") {
rule.t |= ZT_NETWORK_RULE_MATCH_MAC_DEST; rule.t |= ZT_NETWORK_RULE_MATCH_MAC_DEST;
std::string mac(OSUtils::jsonString(r["mac"], "0")); std::string mac(OSUtils::jsonString(r["mac"], "0"));
Utils::cleanMac(mac); Utils::cleanMac(mac);
Utils::unhex(mac.c_str(), (unsigned int)mac.length(), rule.v.mac, 6); Utils::unhex(mac.c_str(), (unsigned int)mac.length(), rule.v.mac, 6);
return true; return true;
} else if (t == "MATCH_IPV4_SOURCE") { }
else if (t == "MATCH_IPV4_SOURCE") {
rule.t |= ZT_NETWORK_RULE_MATCH_IPV4_SOURCE; rule.t |= ZT_NETWORK_RULE_MATCH_IPV4_SOURCE;
InetAddress ip(OSUtils::jsonString(r["ip"], "0.0.0.0").c_str()); InetAddress ip(OSUtils::jsonString(r["ip"], "0.0.0.0").c_str());
rule.v.ipv4.ip = reinterpret_cast<struct sockaddr_in*>(&ip)->sin_addr.s_addr; rule.v.ipv4.ip = reinterpret_cast<struct sockaddr_in*>(&ip)->sin_addr.s_addr;
rule.v.ipv4.mask = Utils::ntoh(reinterpret_cast<struct sockaddr_in*>(&ip)->sin_port) & 0xff; rule.v.ipv4.mask = Utils::ntoh(reinterpret_cast<struct sockaddr_in*>(&ip)->sin_port) & 0xff;
if (rule.v.ipv4.mask > 32) rule.v.ipv4.mask = 32; if (rule.v.ipv4.mask > 32)
rule.v.ipv4.mask = 32;
return true; return true;
} else if (t == "MATCH_IPV4_DEST") { }
else if (t == "MATCH_IPV4_DEST") {
rule.t |= ZT_NETWORK_RULE_MATCH_IPV4_DEST; rule.t |= ZT_NETWORK_RULE_MATCH_IPV4_DEST;
InetAddress ip(OSUtils::jsonString(r["ip"], "0.0.0.0").c_str()); InetAddress ip(OSUtils::jsonString(r["ip"], "0.0.0.0").c_str());
rule.v.ipv4.ip = reinterpret_cast<struct sockaddr_in*>(&ip)->sin_addr.s_addr; rule.v.ipv4.ip = reinterpret_cast<struct sockaddr_in*>(&ip)->sin_addr.s_addr;
rule.v.ipv4.mask = Utils::ntoh(reinterpret_cast<struct sockaddr_in*>(&ip)->sin_port) & 0xff; rule.v.ipv4.mask = Utils::ntoh(reinterpret_cast<struct sockaddr_in*>(&ip)->sin_port) & 0xff;
if (rule.v.ipv4.mask > 32) rule.v.ipv4.mask = 32; if (rule.v.ipv4.mask > 32)
rule.v.ipv4.mask = 32;
return true; return true;
} else if (t == "MATCH_IPV6_SOURCE") { }
else if (t == "MATCH_IPV6_SOURCE") {
rule.t |= ZT_NETWORK_RULE_MATCH_IPV6_SOURCE; rule.t |= ZT_NETWORK_RULE_MATCH_IPV6_SOURCE;
InetAddress ip(OSUtils::jsonString(r["ip"], "::0").c_str()); InetAddress ip(OSUtils::jsonString(r["ip"], "::0").c_str());
memcpy(rule.v.ipv6.ip, reinterpret_cast<struct sockaddr_in6*>(&ip)->sin6_addr.s6_addr, 16); memcpy(rule.v.ipv6.ip, reinterpret_cast<struct sockaddr_in6*>(&ip)->sin6_addr.s6_addr, 16);
rule.v.ipv6.mask = Utils::ntoh(reinterpret_cast<struct sockaddr_in6*>(&ip)->sin6_port) & 0xff; rule.v.ipv6.mask = Utils::ntoh(reinterpret_cast<struct sockaddr_in6*>(&ip)->sin6_port) & 0xff;
if (rule.v.ipv6.mask > 128) rule.v.ipv6.mask = 128; if (rule.v.ipv6.mask > 128)
rule.v.ipv6.mask = 128;
return true; return true;
} else if (t == "MATCH_IPV6_DEST") { }
else if (t == "MATCH_IPV6_DEST") {
rule.t |= ZT_NETWORK_RULE_MATCH_IPV6_DEST; rule.t |= ZT_NETWORK_RULE_MATCH_IPV6_DEST;
InetAddress ip(OSUtils::jsonString(r["ip"], "::0").c_str()); InetAddress ip(OSUtils::jsonString(r["ip"], "::0").c_str());
memcpy(rule.v.ipv6.ip, reinterpret_cast<struct sockaddr_in6*>(&ip)->sin6_addr.s6_addr, 16); memcpy(rule.v.ipv6.ip, reinterpret_cast<struct sockaddr_in6*>(&ip)->sin6_addr.s6_addr, 16);
rule.v.ipv6.mask = Utils::ntoh(reinterpret_cast<struct sockaddr_in6*>(&ip)->sin6_port) & 0xff; rule.v.ipv6.mask = Utils::ntoh(reinterpret_cast<struct sockaddr_in6*>(&ip)->sin6_port) & 0xff;
if (rule.v.ipv6.mask > 128) rule.v.ipv6.mask = 128; if (rule.v.ipv6.mask > 128)
rule.v.ipv6.mask = 128;
return true; return true;
} else if (t == "MATCH_IP_TOS") { }
else if (t == "MATCH_IP_TOS") {
rule.t |= ZT_NETWORK_RULE_MATCH_IP_TOS; rule.t |= ZT_NETWORK_RULE_MATCH_IP_TOS;
rule.v.ipTos.mask = (uint8_t)(OSUtils::jsonInt(r["mask"], 0ULL) & 0xffULL); rule.v.ipTos.mask = (uint8_t)(OSUtils::jsonInt(r["mask"], 0ULL) & 0xffULL);
rule.v.ipTos.value[0] = (uint8_t)(OSUtils::jsonInt(r["start"], 0ULL) & 0xffULL); rule.v.ipTos.value[0] = (uint8_t)(OSUtils::jsonInt(r["start"], 0ULL) & 0xffULL);
rule.v.ipTos.value[1] = (uint8_t)(OSUtils::jsonInt(r["end"], 0ULL) & 0xffULL); rule.v.ipTos.value[1] = (uint8_t)(OSUtils::jsonInt(r["end"], 0ULL) & 0xffULL);
return true; return true;
} else if (t == "MATCH_IP_PROTOCOL") { }
else if (t == "MATCH_IP_PROTOCOL") {
rule.t |= ZT_NETWORK_RULE_MATCH_IP_PROTOCOL; rule.t |= ZT_NETWORK_RULE_MATCH_IP_PROTOCOL;
rule.v.ipProtocol = (uint8_t)(OSUtils::jsonInt(r["ipProtocol"], 0ULL) & 0xffULL); rule.v.ipProtocol = (uint8_t)(OSUtils::jsonInt(r["ipProtocol"], 0ULL) & 0xffULL);
return true; return true;
} else if (t == "MATCH_ETHERTYPE") { }
else if (t == "MATCH_ETHERTYPE") {
rule.t |= ZT_NETWORK_RULE_MATCH_ETHERTYPE; rule.t |= ZT_NETWORK_RULE_MATCH_ETHERTYPE;
rule.v.etherType = (uint16_t)(OSUtils::jsonInt(r["etherType"], 0ULL) & 0xffffULL); rule.v.etherType = (uint16_t)(OSUtils::jsonInt(r["etherType"], 0ULL) & 0xffffULL);
return true; return true;
} else if (t == "MATCH_ICMP") { }
else if (t == "MATCH_ICMP") {
rule.t |= ZT_NETWORK_RULE_MATCH_ICMP; rule.t |= ZT_NETWORK_RULE_MATCH_ICMP;
rule.v.icmp.type = (uint8_t)(OSUtils::jsonInt(r["icmpType"], 0ULL) & 0xffULL); rule.v.icmp.type = (uint8_t)(OSUtils::jsonInt(r["icmpType"], 0ULL) & 0xffULL);
json& code = r["icmpCode"]; json& code = r["icmpCode"];
if (code.is_null()) { if (code.is_null()) {
rule.v.icmp.code = 0; rule.v.icmp.code = 0;
rule.v.icmp.flags = 0x00; rule.v.icmp.flags = 0x00;
} else { }
else {
rule.v.icmp.code = (uint8_t)(OSUtils::jsonInt(code, 0ULL) & 0xffULL); rule.v.icmp.code = (uint8_t)(OSUtils::jsonInt(code, 0ULL) & 0xffULL);
rule.v.icmp.flags = 0x01; rule.v.icmp.flags = 0x01;
} }
return true; return true;
} else if (t == "MATCH_IP_SOURCE_PORT_RANGE") { }
else if (t == "MATCH_IP_SOURCE_PORT_RANGE") {
rule.t |= ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE; rule.t |= ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE;
rule.v.port[0] = (uint16_t)(OSUtils::jsonInt(r["start"], 0ULL) & 0xffffULL); rule.v.port[0] = (uint16_t)(OSUtils::jsonInt(r["start"], 0ULL) & 0xffffULL);
rule.v.port[1] = (uint16_t)(OSUtils::jsonInt(r["end"], (uint64_t)rule.v.port[0]) & 0xffffULL); rule.v.port[1] = (uint16_t)(OSUtils::jsonInt(r["end"], (uint64_t)rule.v.port[0]) & 0xffffULL);
return true; return true;
} else if (t == "MATCH_IP_DEST_PORT_RANGE") { }
else if (t == "MATCH_IP_DEST_PORT_RANGE") {
rule.t |= ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE; rule.t |= ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE;
rule.v.port[0] = (uint16_t)(OSUtils::jsonInt(r["start"], 0ULL) & 0xffffULL); rule.v.port[0] = (uint16_t)(OSUtils::jsonInt(r["start"], 0ULL) & 0xffffULL);
rule.v.port[1] = (uint16_t)(OSUtils::jsonInt(r["end"], (uint64_t)rule.v.port[0]) & 0xffffULL); rule.v.port[1] = (uint16_t)(OSUtils::jsonInt(r["end"], (uint64_t)rule.v.port[0]) & 0xffffULL);
return true; return true;
} else if (t == "MATCH_CHARACTERISTICS") { }
else if (t == "MATCH_CHARACTERISTICS") {
rule.t |= ZT_NETWORK_RULE_MATCH_CHARACTERISTICS; rule.t |= ZT_NETWORK_RULE_MATCH_CHARACTERISTICS;
if (r.count("mask")) { if (r.count("mask")) {
json& v = r["mask"]; json& v = r["mask"];
if (v.is_number()) { if (v.is_number()) {
rule.v.characteristics = v; rule.v.characteristics = v;
} else { }
else {
std::string tmp = v; std::string tmp = v;
rule.v.characteristics = Utils::hexStrToU64(tmp.c_str()); rule.v.characteristics = Utils::hexStrToU64(tmp.c_str());
} }
} }
return true; return true;
} else if (t == "MATCH_FRAME_SIZE_RANGE") { }
else if (t == "MATCH_FRAME_SIZE_RANGE") {
rule.t |= ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE; rule.t |= ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE;
rule.v.frameSize[0] = (uint16_t)(OSUtils::jsonInt(r["start"], 0ULL) & 0xffffULL); rule.v.frameSize[0] = (uint16_t)(OSUtils::jsonInt(r["start"], 0ULL) & 0xffffULL);
rule.v.frameSize[1] = (uint16_t)(OSUtils::jsonInt(r["end"], (uint64_t)rule.v.frameSize[0]) & 0xffffULL); rule.v.frameSize[1] = (uint16_t)(OSUtils::jsonInt(r["end"], (uint64_t)rule.v.frameSize[0]) & 0xffffULL);
return true; return true;
} else if (t == "MATCH_RANDOM") { }
else if (t == "MATCH_RANDOM") {
rule.t |= ZT_NETWORK_RULE_MATCH_RANDOM; rule.t |= ZT_NETWORK_RULE_MATCH_RANDOM;
rule.v.randomProbability = (uint32_t)(OSUtils::jsonInt(r["probability"], 0ULL) & 0xffffffffULL); rule.v.randomProbability = (uint32_t)(OSUtils::jsonInt(r["probability"], 0ULL) & 0xffffffffULL);
return true; return true;
} else if (t == "MATCH_TAGS_DIFFERENCE") { }
else if (t == "MATCH_TAGS_DIFFERENCE") {
rule.t |= ZT_NETWORK_RULE_MATCH_TAGS_DIFFERENCE; rule.t |= ZT_NETWORK_RULE_MATCH_TAGS_DIFFERENCE;
tag = true; tag = true;
} else if (t == "MATCH_TAGS_BITWISE_AND") { }
else if (t == "MATCH_TAGS_BITWISE_AND") {
rule.t |= ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_AND; rule.t |= ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_AND;
tag = true; tag = true;
} else if (t == "MATCH_TAGS_BITWISE_OR") { }
else if (t == "MATCH_TAGS_BITWISE_OR") {
rule.t |= ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_OR; rule.t |= ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_OR;
tag = true; tag = true;
} else if (t == "MATCH_TAGS_BITWISE_XOR") { }
else if (t == "MATCH_TAGS_BITWISE_XOR") {
rule.t |= ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_XOR; rule.t |= ZT_NETWORK_RULE_MATCH_TAGS_BITWISE_XOR;
tag = true; tag = true;
} else if (t == "MATCH_TAGS_EQUAL") { }
else if (t == "MATCH_TAGS_EQUAL") {
rule.t |= ZT_NETWORK_RULE_MATCH_TAGS_EQUAL; rule.t |= ZT_NETWORK_RULE_MATCH_TAGS_EQUAL;
tag = true; tag = true;
} else if (t == "MATCH_TAG_SENDER") { }
else if (t == "MATCH_TAG_SENDER") {
rule.t |= ZT_NETWORK_RULE_MATCH_TAG_SENDER; rule.t |= ZT_NETWORK_RULE_MATCH_TAG_SENDER;
tag = true; tag = true;
} else if (t == "MATCH_TAG_RECEIVER") { }
else if (t == "MATCH_TAG_RECEIVER") {
rule.t |= ZT_NETWORK_RULE_MATCH_TAG_RECEIVER; rule.t |= ZT_NETWORK_RULE_MATCH_TAG_RECEIVER;
tag = true; tag = true;
} else if (t == "INTEGER_RANGE") { }
else if (t == "INTEGER_RANGE") {
json& s = r["start"]; json& s = r["start"];
if (s.is_string()) { if (s.is_string()) {
std::string tmp = s; std::string tmp = s;
rule.v.intRange.start = Utils::hexStrToU64(tmp.c_str()); rule.v.intRange.start = Utils::hexStrToU64(tmp.c_str());
} else { }
else {
rule.v.intRange.start = OSUtils::jsonInt(s, 0ULL); rule.v.intRange.start = OSUtils::jsonInt(s, 0ULL);
} }
json& e = r["end"]; json& e = r["end"];
if (e.is_string()) { if (e.is_string()) {
std::string tmp = e; std::string tmp = e;
rule.v.intRange.end = (uint32_t)(Utils::hexStrToU64(tmp.c_str()) - rule.v.intRange.start); rule.v.intRange.end = (uint32_t)(Utils::hexStrToU64(tmp.c_str()) - rule.v.intRange.start);
} else { }
else {
rule.v.intRange.end = (uint32_t)(OSUtils::jsonInt(e, 0ULL) - rule.v.intRange.start); rule.v.intRange.end = (uint32_t)(OSUtils::jsonInt(e, 0ULL) - rule.v.intRange.start);
} }
rule.v.intRange.idx = (uint16_t)OSUtils::jsonInt(r["idx"], 0ULL); rule.v.intRange.idx = (uint16_t)OSUtils::jsonInt(r["idx"], 0ULL);
@ -521,7 +580,8 @@ EmbeddedNetworkController::~EmbeddedNetworkController()
_ssoExpiry.join(); _ssoExpiry.join();
} }
void EmbeddedNetworkController::setSSORedirectURL(const std::string &url) { void EmbeddedNetworkController::setSSORedirectURL(const std::string& url)
{
_ssoRedirectURL = url; _ssoRedirectURL = url;
} }
@ -535,7 +595,8 @@ void EmbeddedNetworkController::init(const Identity &signingId,Sender *sender)
#ifdef ZT_CONTROLLER_USE_LIBPQ #ifdef ZT_CONTROLLER_USE_LIBPQ
if ((_path.length() > 9) && (_path.substr(0, 9) == "postgres:")) { if ((_path.length() > 9) && (_path.substr(0, 9) == "postgres:")) {
_db.addDB(std::shared_ptr<DB>(new PostgreSQL(_signingId, _path.substr(9).c_str(), _listenPort, _rc))); _db.addDB(std::shared_ptr<DB>(new PostgreSQL(_signingId, _path.substr(9).c_str(), _listenPort, _rc)));
} else { }
else {
#endif #endif
_db.addDB(std::shared_ptr<DB>(new FileDB(_path.c_str()))); _db.addDB(std::shared_ptr<DB>(new FileDB(_path.c_str())));
#ifdef ZT_CONTROLLER_USE_LIBPQ #ifdef ZT_CONTROLLER_USE_LIBPQ
@ -575,12 +636,7 @@ void EmbeddedNetworkController::init(const Identity &signingId,Sender *sender)
_db.waitForReady(); _db.waitForReady();
} }
void EmbeddedNetworkController::request( void EmbeddedNetworkController::request(uint64_t nwid, const InetAddress& fromAddr, uint64_t requestPacketId, const Identity& identity, const Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY>& metaData)
uint64_t nwid,
const InetAddress &fromAddr,
uint64_t requestPacketId,
const Identity &identity,
const Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> &metaData)
{ {
if (((! _signingId) || (! _signingId.hasPrivate())) || (_signingId.address().toInt() != (nwid >> 24)) || (! _sender)) if (((! _signingId) || (! _signingId.hasPrivate())) || (_signingId.address().toInt() != (nwid >> 24)) || (! _sender))
return; return;
@ -617,37 +673,48 @@ std::string EmbeddedNetworkController::networkUpdateFromPostData(uint64_t networ
json network; json network;
_db.get(networkID, network); _db.get(networkID, network);
DB::initNetwork(network); DB::initNetwork(network);
if (b.count("name")) network["name"] = OSUtils::jsonString(b["name"],""); if (b.count("name"))
if (b.count("private")) network["private"] = OSUtils::jsonBool(b["private"],true); network["name"] = OSUtils::jsonString(b["name"], "");
if (b.count("enableBroadcast")) network["enableBroadcast"] = OSUtils::jsonBool(b["enableBroadcast"],false); if (b.count("private"))
if (b.count("multicastLimit")) network["multicastLimit"] = OSUtils::jsonInt(b["multicastLimit"],32ULL); network["private"] = OSUtils::jsonBool(b["private"], true);
if (b.count("mtu")) network["mtu"] = std::max(std::min((unsigned int)OSUtils::jsonInt(b["mtu"],ZT_DEFAULT_MTU),(unsigned int)ZT_MAX_MTU),(unsigned int)ZT_MIN_MTU); if (b.count("enableBroadcast"))
network["enableBroadcast"] = OSUtils::jsonBool(b["enableBroadcast"], false);
if (b.count("multicastLimit"))
network["multicastLimit"] = OSUtils::jsonInt(b["multicastLimit"], 32ULL);
if (b.count("mtu"))
network["mtu"] = std::max(std::min((unsigned int)OSUtils::jsonInt(b["mtu"], ZT_DEFAULT_MTU), (unsigned int)ZT_MAX_MTU), (unsigned int)ZT_MIN_MTU);
if (b.count("remoteTraceTarget")) { if (b.count("remoteTraceTarget")) {
const std::string rtt(OSUtils::jsonString(b["remoteTraceTarget"], "")); const std::string rtt(OSUtils::jsonString(b["remoteTraceTarget"], ""));
if (rtt.length() == 10) { if (rtt.length() == 10) {
network["remoteTraceTarget"] = rtt; network["remoteTraceTarget"] = rtt;
} else { }
else {
network["remoteTraceTarget"] = json(); network["remoteTraceTarget"] = json();
} }
} }
if (b.count("remoteTraceLevel")) network["remoteTraceLevel"] = OSUtils::jsonInt(b["remoteTraceLevel"],0ULL); if (b.count("remoteTraceLevel"))
network["remoteTraceLevel"] = OSUtils::jsonInt(b["remoteTraceLevel"], 0ULL);
if (b.count("v4AssignMode")) { if (b.count("v4AssignMode")) {
json nv4m; json nv4m;
json& v4m = b["v4AssignMode"]; json& v4m = b["v4AssignMode"];
if (v4m.is_string()) { // backward compatibility if (v4m.is_string()) { // backward compatibility
nv4m["zt"] = (OSUtils::jsonString(v4m, "") == "zt"); nv4m["zt"] = (OSUtils::jsonString(v4m, "") == "zt");
} else if (v4m.is_object()) { }
else if (v4m.is_object()) {
nv4m["zt"] = OSUtils::jsonBool(v4m["zt"], false); nv4m["zt"] = OSUtils::jsonBool(v4m["zt"], false);
} else nv4m["zt"] = false; }
else
nv4m["zt"] = false;
network["v4AssignMode"] = nv4m; network["v4AssignMode"] = nv4m;
} }
if (b.count("v6AssignMode")) { if (b.count("v6AssignMode")) {
json nv6m; json nv6m;
json& v6m = b["v6AssignMode"]; json& v6m = b["v6AssignMode"];
if (!nv6m.is_object()) nv6m = json::object(); if (! nv6m.is_object())
nv6m = json::object();
if (v6m.is_string()) { // backward compatibility if (v6m.is_string()) { // backward compatibility
std::vector<std::string> v6ms(OSUtils::split(OSUtils::jsonString(v6m, "").c_str(), ",", "", "")); std::vector<std::string> v6ms(OSUtils::split(OSUtils::jsonString(v6m, "").c_str(), ",", "", ""));
std::sort(v6ms.begin(), v6ms.end()); std::sort(v6ms.begin(), v6ms.end());
@ -663,11 +730,16 @@ std::string EmbeddedNetworkController::networkUpdateFromPostData(uint64_t networ
else if (*i == "6plane") else if (*i == "6plane")
nv6m["6plane"] = true; nv6m["6plane"] = true;
} }
} else if (v6m.is_object()) { }
if (v6m.count("rfc4193")) nv6m["rfc4193"] = OSUtils::jsonBool(v6m["rfc4193"],false); else if (v6m.is_object()) {
if (v6m.count("zt")) nv6m["zt"] = OSUtils::jsonBool(v6m["zt"],false); if (v6m.count("rfc4193"))
if (v6m.count("6plane")) nv6m["6plane"] = OSUtils::jsonBool(v6m["6plane"],false); nv6m["rfc4193"] = OSUtils::jsonBool(v6m["rfc4193"], false);
} else { if (v6m.count("zt"))
nv6m["zt"] = OSUtils::jsonBool(v6m["zt"], false);
if (v6m.count("6plane"))
nv6m["6plane"] = OSUtils::jsonBool(v6m["6plane"], false);
}
else {
nv6m["rfc4193"] = false; nv6m["rfc4193"] = false;
nv6m["zt"] = false; nv6m["zt"] = false;
nv6m["6plane"] = false; nv6m["6plane"] = false;
@ -687,14 +759,16 @@ std::string EmbeddedNetworkController::networkUpdateFromPostData(uint64_t networ
if (target.is_string()) { if (target.is_string()) {
InetAddress t(target.get<std::string>().c_str()); InetAddress t(target.get<std::string>().c_str());
InetAddress v; InetAddress v;
if (via.is_string()) v.fromString(via.get<std::string>().c_str()); if (via.is_string())
v.fromString(via.get<std::string>().c_str());
if (((t.ss_family == AF_INET) || (t.ss_family == AF_INET6)) && (t.netmaskBitsValid())) { if (((t.ss_family == AF_INET) || (t.ss_family == AF_INET6)) && (t.netmaskBitsValid())) {
json tmp; json tmp;
char tmp2[64]; char tmp2[64];
tmp["target"] = t.toString(tmp2); tmp["target"] = t.toString(tmp2);
if (v.ss_family == t.ss_family) if (v.ss_family == t.ss_family)
tmp["via"] = v.toIpString(tmp2); tmp["via"] = v.toIpString(tmp2);
else tmp["via"] = json(); else
tmp["via"] = json();
nrts.push_back(tmp); nrts.push_back(tmp);
if (nrts.size() >= ZT_CONTROLLER_MAX_ARRAY_SIZE) if (nrts.size() >= ZT_CONTROLLER_MAX_ARRAY_SIZE)
break; break;
@ -758,7 +832,8 @@ std::string EmbeddedNetworkController::networkUpdateFromPostData(uint64_t networ
nat[t.key()] = t.value(); nat[t.key()] = t.value();
} }
network["authTokens"] = nat; network["authTokens"] = nat;
} else { }
else {
network["authTokens"] = { {} }; network["authTokens"] = { {} };
} }
} }
@ -819,7 +894,8 @@ std::string EmbeddedNetworkController::networkUpdateFromPostData(uint64_t networ
json& dfl = tag["default"]; json& dfl = tag["default"];
if (dfl.is_null()) if (dfl.is_null())
ntag["default"] = dfl; ntag["default"] = dfl;
else ntag["default"] = OSUtils::jsonInt(dfl,0ULL); else
ntag["default"] = OSUtils::jsonInt(dfl, 0ULL);
ntags[tagId] = ntag; ntags[tagId] = ntag;
} }
} }
@ -863,10 +939,7 @@ std::string EmbeddedNetworkController::networkUpdateFromPostData(uint64_t networ
return network.dump(); return network.dump();
} }
void EmbeddedNetworkController::configureHTTPControlPlane( void EmbeddedNetworkController::configureHTTPControlPlane(httplib::Server& s, httplib::Server& sv6, const std::function<void(const httplib::Request&, httplib::Response&, std::string)> setContent)
httplib::Server &s,
httplib::Server &sv6,
const std::function<void(const httplib::Request&, httplib::Response&, std::string)> setContent)
{ {
// Control plane Endpoints // Control plane Endpoints
std::string controllerPath = "/controller"; std::string controllerPath = "/controller";
@ -878,7 +951,6 @@ void EmbeddedNetworkController::configureHTTPControlPlane(
std::string memberListPath2 = "/unstable/controller/network/([0-9a-fA-F]{16})/member"; std::string memberListPath2 = "/unstable/controller/network/([0-9a-fA-F]{16})/member";
std::string memberPath = "/controller/network/([0-9a-fA-F]{16})/member/([0-9a-fA-F]{10})"; std::string memberPath = "/controller/network/([0-9a-fA-F]{16})/member/([0-9a-fA-F]{10})";
auto controllerGet = [&, setContent](const httplib::Request& req, httplib::Response& res) { auto controllerGet = [&, setContent](const httplib::Request& req, httplib::Response& res) {
char tmp[4096]; char tmp[4096];
const bool dbOk = _db.isReady(); const bool dbOk = _db.isReady();
@ -938,7 +1010,9 @@ void EmbeddedNetworkController::configureHTTPControlPlane(
for (auto m = memTmp.begin(); m != memTmp.end(); ++m) { for (auto m = memTmp.begin(); m != memTmp.end(); ++m) {
bool a = OSUtils::jsonBool((*m)["authorized"], 0); bool a = OSUtils::jsonBool((*m)["authorized"], 0);
if (a) { authorizedCount++; } if (a) {
authorizedCount++;
}
} }
auto nwMeta = json::object(); auto nwMeta = json::object();
@ -983,7 +1057,8 @@ void EmbeddedNetworkController::configureHTTPControlPlane(
for (unsigned long k = 0; k < 100000; ++k) { // sanity limit on trials for (unsigned long k = 0; k < 100000; ++k) { // sanity limit on trials
Utils::getSecureRandom(&nwidPostfix, sizeof(nwidPostfix)); Utils::getSecureRandom(&nwidPostfix, sizeof(nwidPostfix));
uint64_t tryNwid = nwidPrefix | (nwidPostfix & 0xffffffULL); uint64_t tryNwid = nwidPrefix | (nwidPostfix & 0xffffffULL);
if ((tryNwid & 0xffffffULL) == 0ULL) tryNwid |= 1ULL; if ((tryNwid & 0xffffffULL) == 0ULL)
tryNwid |= 1ULL;
if (! _db.hasNetwork(tryNwid)) { if (! _db.hasNetwork(tryNwid)) {
nwid = tryNwid; nwid = tryNwid;
break; break;
@ -1015,7 +1090,8 @@ void EmbeddedNetworkController::configureHTTPControlPlane(
for (unsigned long k = 0; k < 100000; ++k) { // sanity limit on trials for (unsigned long k = 0; k < 100000; ++k) { // sanity limit on trials
Utils::getSecureRandom(&nwidPostfix, sizeof(nwidPostfix)); Utils::getSecureRandom(&nwidPostfix, sizeof(nwidPostfix));
uint64_t tryNwid = nwidPrefix | (nwidPostfix & 0xffffffULL); uint64_t tryNwid = nwidPrefix | (nwidPostfix & 0xffffffULL);
if ((tryNwid & 0xffffffULL) == 0ULL) tryNwid |= 1ULL; if ((tryNwid & 0xffffffULL) == 0ULL)
tryNwid |= 1ULL;
if (! _db.hasNetwork(tryNwid)) { if (! _db.hasNetwork(tryNwid)) {
nwid = tryNwid; nwid = tryNwid;
break; break;
@ -1103,7 +1179,9 @@ void EmbeddedNetworkController::configureHTTPControlPlane(
uint64_t totalCount = memTmp.size(); uint64_t totalCount = memTmp.size();
for (auto m = memTmp.begin(); m != memTmp.end(); ++m) { for (auto m = memTmp.begin(); m != memTmp.end(); ++m) {
bool a = OSUtils::jsonBool((*m)["authorized"], 0); bool a = OSUtils::jsonBool((*m)["authorized"], 0);
if (a) { authorizedCount++; } if (a) {
authorizedCount++;
}
} }
meta["totalCount"] = totalCount; meta["totalCount"] = totalCount;
@ -1113,7 +1191,8 @@ void EmbeddedNetworkController::configureHTTPControlPlane(
out["meta"] = meta; out["meta"] = meta;
setContent(req, res, out.dump()); setContent(req, res, out.dump());
} else { }
else {
res.status = 404; res.status = 404;
return; return;
} }
@ -1156,21 +1235,28 @@ void EmbeddedNetworkController::configureHTTPControlPlane(
json b = OSUtils::jsonParse(req.body); json b = OSUtils::jsonParse(req.body);
if (b.count("activeBridge")) member["activeBridge"] = OSUtils::jsonBool(b["activeBridge"], false); if (b.count("activeBridge"))
if (b.count("noAutoAssignIps")) member["noAutoAssignIps"] = OSUtils::jsonBool(b["noAutoAssignIps"], false); member["activeBridge"] = OSUtils::jsonBool(b["activeBridge"], false);
if (b.count("authenticationExpiryTime")) member["authenticationExpiryTime"] = (uint64_t)OSUtils::jsonInt(b["authenticationExpiryTime"], 0ULL); if (b.count("noAutoAssignIps"))
if (b.count("authenticationURL")) member["authenticationURL"] = OSUtils::jsonString(b["authenticationURL"], ""); member["noAutoAssignIps"] = OSUtils::jsonBool(b["noAutoAssignIps"], false);
if (b.count("name")) member["name"] = OSUtils::jsonString(b["name"], ""); if (b.count("authenticationExpiryTime"))
member["authenticationExpiryTime"] = (uint64_t)OSUtils::jsonInt(b["authenticationExpiryTime"], 0ULL);
if (b.count("authenticationURL"))
member["authenticationURL"] = OSUtils::jsonString(b["authenticationURL"], "");
if (b.count("name"))
member["name"] = OSUtils::jsonString(b["name"], "");
if (b.count("remoteTraceTarget")) { if (b.count("remoteTraceTarget")) {
const std::string rtt(OSUtils::jsonString(b["remoteTraceTarget"], "")); const std::string rtt(OSUtils::jsonString(b["remoteTraceTarget"], ""));
if (rtt.length() == 10) { if (rtt.length() == 10) {
member["remoteTraceTarget"] = rtt; member["remoteTraceTarget"] = rtt;
} else { }
else {
member["remoteTraceTarget"] = json(); member["remoteTraceTarget"] = json();
} }
} }
if (b.count("remoteTraceLevel")) member["remoteTraceLevel"] = OSUtils::jsonInt(b["remoteTraceLevel"],0ULL); if (b.count("remoteTraceLevel"))
member["remoteTraceLevel"] = OSUtils::jsonInt(b["remoteTraceLevel"], 0ULL);
if (b.count("authorized")) { if (b.count("authorized")) {
const bool newAuth = OSUtils::jsonBool(b["authorized"], false); const bool newAuth = OSUtils::jsonBool(b["authorized"], false);
@ -1300,15 +1386,26 @@ void EmbeddedNetworkController::handleRemoteTrace(const ZT_RemoteTrace &rt)
++eq; ++eq;
if (*eq) { if (*eq) {
switch (*eq) { switch (*eq) {
case 'r': v.push_back('\r'); break; case 'r':
case 'n': v.push_back('\n'); break; v.push_back('\r');
case '0': v.push_back((char)0); break; break;
case 'e': v.push_back('='); break; case 'n':
default: v.push_back(*eq); break; v.push_back('\n');
break;
case '0':
v.push_back((char)0);
break;
case 'e':
v.push_back('=');
break;
default:
v.push_back(*eq);
break;
} }
++eq; ++eq;
} }
} else { }
else {
v.push_back(*(eq++)); v.push_back(*(eq++));
} }
} }
@ -1324,7 +1421,8 @@ void EmbeddedNetworkController::handleRemoteTrace(const ZT_RemoteTrace &rt)
d["ts"] = now; d["ts"] = now;
d["nodeId"] = Utils::hex10(rt.origin, tmp); d["nodeId"] = Utils::hex10(rt.origin, tmp);
_db.save(d, true); _db.save(d, true);
} catch ( ... ) { }
catch (...) {
// drop invalid trace messages if an error occurs // drop invalid trace messages if an error occurs
} }
} }
@ -1348,7 +1446,9 @@ void EmbeddedNetworkController::onNetworkMemberUpdate(const void *db,uint64_t ne
_MemberStatus& ms = _memberStatus[_MemberStatusKey(networkId, memberId)]; _MemberStatus& ms = _memberStatus[_MemberStatusKey(networkId, memberId)];
if ((ms.online(OSUtils::now())) && (ms.lastRequestMetaData)) if ((ms.online(OSUtils::now())) && (ms.lastRequestMetaData))
request(networkId, InetAddress(), 0, ms.identity, ms.lastRequestMetaData); request(networkId, InetAddress(), 0, ms.identity, ms.lastRequestMetaData);
} catch ( ... ) {} }
catch (...) {
}
} }
void EmbeddedNetworkController::onNetworkMemberDeauthorize(const void* db, uint64_t networkId, uint64_t memberId) void EmbeddedNetworkController::onNetworkMemberDeauthorize(const void* db, uint64_t networkId, uint64_t memberId)
@ -1365,16 +1465,12 @@ void EmbeddedNetworkController::onNetworkMemberDeauthorize(const void *db,uint64
} }
} }
void EmbeddedNetworkController::_request( void EmbeddedNetworkController::_request(uint64_t nwid, const InetAddress& fromAddr, uint64_t requestPacketId, const Identity& identity, const Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY>& metaData)
uint64_t nwid,
const InetAddress &fromAddr,
uint64_t requestPacketId,
const Identity &identity,
const Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> &metaData)
{ {
Metrics::network_config_request++; Metrics::network_config_request++;
auto tid = std::this_thread::get_id(); auto tid = std::this_thread::get_id();
std::stringstream ss; ss << tid; std::stringstream ss;
ss << tid;
std::string threadID = ss.str(); std::string threadID = ss.str();
#ifdef CENTRAL_CONTROLLER_REQUEST_BENCHMARK #ifdef CENTRAL_CONTROLLER_REQUEST_BENCHMARK
auto b1 = _member_status_lookup.Add({ { "thread", threadID } }); auto b1 = _member_status_lookup.Add({ { "thread", threadID } });
@ -1445,14 +1541,16 @@ void EmbeddedNetworkController::_request(
#endif #endif
return; return;
} }
} catch ( ... ) { }
catch (...) {
_sender->ncSendError(nwid, requestPacketId, identity.address(), NetworkController::NC_ERROR_ACCESS_DENIED, nullptr, 0); _sender->ncSendError(nwid, requestPacketId, identity.address(), NetworkController::NC_ERROR_ACCESS_DENIED, nullptr, 0);
#ifdef CENTRAL_CONTROLLER_REQUEST_BENCHMARK #ifdef CENTRAL_CONTROLLER_REQUEST_BENCHMARK
b4.stop(); b4.stop();
#endif #endif
return; return;
} }
} else { }
else {
// If we do not yet know this member's identity, learn it. // If we do not yet know this member's identity, learn it.
char idtmp[1024]; char idtmp[1024];
member["identity"] = identity.toString(false, idtmp); member["identity"] = identity.toString(false, idtmp);
@ -1483,11 +1581,13 @@ void EmbeddedNetworkController::_request(
json autoAuthCredentialType, autoAuthCredential; json autoAuthCredentialType, autoAuthCredential;
if (OSUtils::jsonBool(member["authorized"], false)) { if (OSUtils::jsonBool(member["authorized"], false)) {
authorized = true; authorized = true;
} else if (!OSUtils::jsonBool(network["private"],true)) { }
else if (! OSUtils::jsonBool(network["private"], true)) {
authorized = true; authorized = true;
autoAuthorized = true; autoAuthorized = true;
autoAuthCredentialType = "public"; autoAuthCredentialType = "public";
} else { }
else {
char presentedAuth[512]; char presentedAuth[512];
if (metaData.get(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_AUTH, presentedAuth, sizeof(presentedAuth)) > 0) { if (metaData.get(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_AUTH, presentedAuth, sizeof(presentedAuth)) > 0) {
presentedAuth[511] = (char)0; // sanity check presentedAuth[511] = (char)0; // sanity check
@ -1541,7 +1641,8 @@ void EmbeddedNetworkController::_request(
authInfo.add(ZT_AUTHINFO_DICT_KEY_VERSION, (uint64_t)0ULL); authInfo.add(ZT_AUTHINFO_DICT_KEY_VERSION, (uint64_t)0ULL);
authInfo.add(ZT_AUTHINFO_DICT_KEY_AUTHENTICATION_URL, info.authenticationURL.c_str()); authInfo.add(ZT_AUTHINFO_DICT_KEY_AUTHENTICATION_URL, info.authenticationURL.c_str());
_sender->ncSendError(nwid, requestPacketId, identity.address(), NetworkController::NC_ERROR_AUTHENTICATION_REQUIRED, authInfo.data(), authInfo.sizeBytes()); _sender->ncSendError(nwid, requestPacketId, identity.address(), NetworkController::NC_ERROR_AUTHENTICATION_REQUIRED, authInfo.data(), authInfo.sizeBytes());
} else if (info.version == 1) { }
else if (info.version == 1) {
Dictionary<8192> authInfo; Dictionary<8192> authInfo;
authInfo.add(ZT_AUTHINFO_DICT_KEY_VERSION, info.version); authInfo.add(ZT_AUTHINFO_DICT_KEY_VERSION, info.version);
authInfo.add(ZT_AUTHINFO_DICT_KEY_ISSUER_URL, info.issuerURL.c_str()); authInfo.add(ZT_AUTHINFO_DICT_KEY_ISSUER_URL, info.issuerURL.c_str());
@ -1598,7 +1699,8 @@ void EmbeddedNetworkController::_request(
_expiringSoon.insert(std::pair<int64_t, _MemberStatusKey>(authenticationExpiryTime, msk)); _expiringSoon.insert(std::pair<int64_t, _MemberStatusKey>(authenticationExpiryTime, msk));
} }
} }
} else { }
else {
// If they are not authorized, STOP! // If they are not authorized, STOP!
DB::cleanMember(member); DB::cleanMember(member);
_db.save(member, true); _db.save(member, true);
@ -1638,7 +1740,8 @@ void EmbeddedNetworkController::_request(
nc->credentialTimeMaxDelta = credentialtmd; nc->credentialTimeMaxDelta = credentialtmd;
nc->revision = OSUtils::jsonInt(network["revision"], 0ULL); nc->revision = OSUtils::jsonInt(network["revision"], 0ULL);
nc->issuedTo = identity.address(); nc->issuedTo = identity.address();
if (OSUtils::jsonBool(network["enableBroadcast"],true)) nc->flags |= ZT_NETWORKCONFIG_FLAG_ENABLE_BROADCAST; if (OSUtils::jsonBool(network["enableBroadcast"], true))
nc->flags |= ZT_NETWORKCONFIG_FLAG_ENABLE_BROADCAST;
Utils::scopy(nc->name, sizeof(nc->name), OSUtils::jsonString(network["name"], "").c_str()); Utils::scopy(nc->name, sizeof(nc->name), OSUtils::jsonString(network["name"], "").c_str());
nc->mtu = std::max(std::min((unsigned int)OSUtils::jsonInt(network["mtu"], ZT_DEFAULT_MTU), (unsigned int)ZT_MAX_MTU), (unsigned int)ZT_MIN_MTU); nc->mtu = std::max(std::min((unsigned int)OSUtils::jsonInt(network["mtu"], ZT_DEFAULT_MTU), (unsigned int)ZT_MAX_MTU), (unsigned int)ZT_MIN_MTU);
nc->multicastLimit = (unsigned int)OSUtils::jsonInt(network["multicastLimit"], 32ULL); nc->multicastLimit = (unsigned int)OSUtils::jsonInt(network["multicastLimit"], 32ULL);
@ -1681,11 +1784,13 @@ void EmbeddedNetworkController::_request(
if (rtt.length() == 10) { if (rtt.length() == 10) {
nc->remoteTraceTarget = Address(Utils::hexStrToU64(rtt.c_str())); nc->remoteTraceTarget = Address(Utils::hexStrToU64(rtt.c_str()));
nc->remoteTraceLevel = (Trace::Level)OSUtils::jsonInt(member["remoteTraceLevel"], 0ULL); nc->remoteTraceLevel = (Trace::Level)OSUtils::jsonInt(member["remoteTraceLevel"], 0ULL);
} else { }
else {
rtt = OSUtils::jsonString(network["remoteTraceTarget"], ""); rtt = OSUtils::jsonString(network["remoteTraceTarget"], "");
if (rtt.length() == 10) { if (rtt.length() == 10) {
nc->remoteTraceTarget = Address(Utils::hexStrToU64(rtt.c_str())); nc->remoteTraceTarget = Address(Utils::hexStrToU64(rtt.c_str()));
} else { }
else {
nc->remoteTraceTarget.zero(); nc->remoteTraceTarget.zero();
} }
nc->remoteTraceLevel = (Trace::Level)OSUtils::jsonInt(network["remoteTraceLevel"], 0ULL); nc->remoteTraceLevel = (Trace::Level)OSUtils::jsonInt(network["remoteTraceLevel"], 0ULL);
@ -1714,7 +1819,8 @@ void EmbeddedNetworkController::_request(
// enforce rules on the inbound side. // enforce rules on the inbound side.
nc->ruleCount = 1; nc->ruleCount = 1;
nc->rules[0].t = ZT_NETWORK_RULE_ACTION_ACCEPT; nc->rules[0].t = ZT_NETWORK_RULE_ACTION_ACCEPT;
} else { }
else {
if (rules.is_array()) { if (rules.is_array()) {
for (unsigned long i = 0; i < rules.size(); ++i) { for (unsigned long i = 0; i < rules.size(); ++i) {
if (nc->ruleCount >= ZT_MAX_NETWORK_RULES) if (nc->ruleCount >= ZT_MAX_NETWORK_RULES)
@ -1816,7 +1922,8 @@ void EmbeddedNetworkController::_request(
if (target.is_string()) { if (target.is_string()) {
const InetAddress t(target.get<std::string>().c_str()); const InetAddress t(target.get<std::string>().c_str());
InetAddress v; InetAddress v;
if (via.is_string()) v.fromString(via.get<std::string>().c_str()); if (via.is_string())
v.fromString(via.get<std::string>().c_str());
if ((t.ss_family == AF_INET) || (t.ss_family == AF_INET6)) { if ((t.ss_family == AF_INET) || (t.ss_family == AF_INET6)) {
ZT_VirtualNetworkRoute* r = &(nc->routes[nc->routeCount]); ZT_VirtualNetworkRoute* r = &(nc->routes[nc->routeCount]);
*(reinterpret_cast<InetAddress*>(&(r->target))) = t; *(reinterpret_cast<InetAddress*>(&(r->target))) = t;
@ -1871,7 +1978,8 @@ void EmbeddedNetworkController::_request(
} }
} }
} }
} else { }
else {
ipAssignments = json::array(); ipAssignments = json::array();
} }
@ -1897,15 +2005,18 @@ void EmbeddedNetworkController::_request(
// First see if we can just cram a ZeroTier ID into the higher 64 bits. If so do that. // First see if we can just cram a ZeroTier ID into the higher 64 bits. If so do that.
xx[0] = Utils::hton(x[0]); xx[0] = Utils::hton(x[0]);
xx[1] = Utils::hton(x[1] + identity.address().toInt()); xx[1] = Utils::hton(x[1] + identity.address().toInt());
} else { }
else {
// Otherwise pick random addresses -- this technically doesn't explore the whole range if the lower 64 bit range is >= 1 but that won't matter since that would be huge anyway // Otherwise pick random addresses -- this technically doesn't explore the whole range if the lower 64 bit range is >= 1 but that won't matter since that would be huge anyway
Utils::getSecureRandom((void*)xx, 16); Utils::getSecureRandom((void*)xx, 16);
if ((e[0] > s[0])) if ((e[0] > s[0]))
xx[0] %= (e[0] - s[0]); xx[0] %= (e[0] - s[0]);
else xx[0] = 0; else
xx[0] = 0;
if ((e[1] > s[1])) if ((e[1] > s[1]))
xx[1] %= (e[1] - s[1]); xx[1] %= (e[1] - s[1]);
else xx[1] = 0; else
xx[1] = 0;
xx[0] = Utils::hton(x[0] + xx[0]); xx[0] = Utils::hton(x[0] + xx[0]);
xx[1] = Utils::hton(x[1] + xx[1]); xx[1] = Utils::hton(x[1] + xx[1]);
} }
@ -2010,7 +2121,8 @@ void EmbeddedNetworkController::_request(
nc->dns.server_addr[j] = InetAddress(OSUtils::jsonString(addr, "").c_str()); nc->dns.server_addr[j] = InetAddress(OSUtils::jsonString(addr, "").c_str());
} }
} }
} else { }
else {
dns = json::object(); dns = json::object();
} }
#ifdef CENTRAL_CONTROLLER_REQUEST_BENCHMARK #ifdef CENTRAL_CONTROLLER_REQUEST_BENCHMARK
@ -2036,7 +2148,8 @@ void EmbeddedNetworkController::_request(
CertificateOfMembership com(now, credentialtmd, nwid, identity); CertificateOfMembership com(now, credentialtmd, nwid, identity);
if (com.sign(_signingId)) { if (com.sign(_signingId)) {
nc->com = com; nc->com = com;
} else { }
else {
_sender->ncSendError(nwid, requestPacketId, identity.address(), NetworkController::NC_ERROR_INTERNAL_SERVER_ERROR, nullptr, 0); _sender->ncSendError(nwid, requestPacketId, identity.address(), NetworkController::NC_ERROR_INTERNAL_SERVER_ERROR, nullptr, 0);
#ifdef CENTRAL_CONTROLLER_REQUEST_BENCHMARK #ifdef CENTRAL_CONTROLLER_REQUEST_BENCHMARK
b9.stop(); b9.stop();
@ -2083,13 +2196,16 @@ void EmbeddedNetworkController::_startThreads()
auto timedWaitResult = _queue.get(qe, 1000); auto timedWaitResult = _queue.get(qe, 1000);
if (timedWaitResult == BlockingQueue<_RQEntry*>::STOP) { if (timedWaitResult == BlockingQueue<_RQEntry*>::STOP) {
break; break;
} else if (timedWaitResult == BlockingQueue<_RQEntry *>::OK) { }
else if (timedWaitResult == BlockingQueue<_RQEntry*>::OK) {
if (qe) { if (qe) {
try { try {
_request(qe->nwid, qe->fromAddr, qe->requestPacketId, qe->identity, qe->metaData); _request(qe->nwid, qe->fromAddr, qe->requestPacketId, qe->identity, qe->metaData);
} catch (std::exception &e) { }
catch (std::exception& e) {
fprintf(stderr, "ERROR: exception in controller request handling thread: %s" ZT_EOL_S, e.what()); fprintf(stderr, "ERROR: exception in controller request handling thread: %s" ZT_EOL_S, e.what());
} catch ( ... ) { }
catch (...) {
fprintf(stderr, "ERROR: exception in controller request handling thread: unknown exception" ZT_EOL_S); fprintf(stderr, "ERROR: exception in controller request handling thread: unknown exception" ZT_EOL_S);
} }
delete qe; delete qe;
@ -2102,7 +2218,8 @@ void EmbeddedNetworkController::_startThreads()
} }
} }
void EmbeddedNetworkController::_ssoExpiryThread() { void EmbeddedNetworkController::_ssoExpiryThread()
{
while (_ssoExpiryRunning) { while (_ssoExpiryRunning) {
std::vector<_MemberStatusKey> expired; std::vector<_MemberStatusKey> expired;
nlohmann::json network, member; nlohmann::json network, member;
@ -2123,7 +2240,8 @@ void EmbeddedNetworkController::_ssoExpiryThread() {
} }
} }
s = _expiringSoon.erase(s); s = _expiringSoon.erase(s);
} else { }
else {
// Don't bother going further into the future than necessary. // Don't bother going further into the future than necessary.
break; break;
} }

View file

@ -14,41 +14,37 @@
#ifndef ZT_SQLITENETWORKCONTROLLER_HPP #ifndef ZT_SQLITENETWORKCONTROLLER_HPP
#define ZT_SQLITENETWORKCONTROLLER_HPP #define ZT_SQLITENETWORKCONTROLLER_HPP
#include <stdint.h> #include "../node/Address.hpp"
#include <string>
#include <map>
#include <vector>
#include <set>
#include <list>
#include <thread>
#include <unordered_map>
#include <atomic>
#include "../node/Constants.hpp" #include "../node/Constants.hpp"
#include "../node/InetAddress.hpp"
#include "../node/NetworkController.hpp" #include "../node/NetworkController.hpp"
#include "../node/Utils.hpp" #include "../node/Utils.hpp"
#include "../node/Address.hpp" #include "../osdep/BlockingQueue.hpp"
#include "../node/InetAddress.hpp"
#include "../osdep/OSUtils.hpp" #include "../osdep/OSUtils.hpp"
#include "../osdep/Thread.hpp" #include "../osdep/Thread.hpp"
#include "../osdep/BlockingQueue.hpp"
#include <nlohmann/json.hpp>
#include <cpp-httplib/httplib.h>
#include "DB.hpp" #include "DB.hpp"
#include "DBMirrorSet.hpp" #include "DBMirrorSet.hpp"
#include <atomic>
#include <cpp-httplib/httplib.h>
#include <list>
#include <map>
#include <nlohmann/json.hpp>
#include <set>
#include <stdint.h>
#include <string>
#include <thread>
#include <unordered_map>
#include <vector>
namespace ZeroTier { namespace ZeroTier {
class Node; class Node;
struct RedisConfig; struct RedisConfig;
class EmbeddedNetworkController : public NetworkController,public DB::ChangeListener class EmbeddedNetworkController
{ : public NetworkController
, public DB::ChangeListener {
public: public:
/** /**
* @param node Parent node * @param node Parent node
@ -61,17 +57,9 @@ public:
void setSSORedirectURL(const std::string& url); void setSSORedirectURL(const std::string& url);
virtual void request( virtual void request(uint64_t nwid, const InetAddress& fromAddr, uint64_t requestPacketId, const Identity& identity, const Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY>& metaData);
uint64_t nwid,
const InetAddress &fromAddr,
uint64_t requestPacketId,
const Identity &identity,
const Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> &metaData);
void configureHTTPControlPlane( void configureHTTPControlPlane(httplib::Server& s, httplib::Server& sV6, const std::function<void(const httplib::Request&, httplib::Response&, std::string)>);
httplib::Server &s,
httplib::Server &sV6,
const std::function<void(const httplib::Request&, httplib::Response&, std::string)>);
void handleRemoteTrace(const ZT_RemoteTrace& rt); void handleRemoteTrace(const ZT_RemoteTrace& rt);
@ -86,39 +74,48 @@ private:
std::string networkUpdateFromPostData(uint64_t networkID, const std::string& body); std::string networkUpdateFromPostData(uint64_t networkID, const std::string& body);
struct _RQEntry struct _RQEntry {
{
uint64_t nwid; uint64_t nwid;
uint64_t requestPacketId; uint64_t requestPacketId;
InetAddress fromAddr; InetAddress fromAddr;
Identity identity; Identity identity;
Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> metaData; Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> metaData;
enum { enum { RQENTRY_TYPE_REQUEST = 0 } type;
RQENTRY_TYPE_REQUEST = 0
} type;
}; };
struct _MemberStatusKey struct _MemberStatusKey {
_MemberStatusKey() : networkId(0), nodeId(0)
{ {
_MemberStatusKey() : networkId(0),nodeId(0) {} }
_MemberStatusKey(const uint64_t nwid,const uint64_t nid) : networkId(nwid),nodeId(nid) {} _MemberStatusKey(const uint64_t nwid, const uint64_t nid) : networkId(nwid), nodeId(nid)
{
}
uint64_t networkId; uint64_t networkId;
uint64_t nodeId; uint64_t nodeId;
inline bool operator==(const _MemberStatusKey &k) const { return ((k.networkId == networkId)&&(k.nodeId == nodeId)); } inline bool operator==(const _MemberStatusKey& k) const
inline bool operator<(const _MemberStatusKey &k) const { return (k.networkId < networkId) || ((k.networkId == networkId)&&(k.nodeId < nodeId)); }
};
struct _MemberStatus
{ {
_MemberStatus() : lastRequestTime(0),authenticationExpiryTime(-1),vMajor(-1),vMinor(-1),vRev(-1),vProto(-1) {} return ((k.networkId == networkId) && (k.nodeId == nodeId));
}
inline bool operator<(const _MemberStatusKey& k) const
{
return (k.networkId < networkId) || ((k.networkId == networkId) && (k.nodeId < nodeId));
}
};
struct _MemberStatus {
_MemberStatus() : lastRequestTime(0), authenticationExpiryTime(-1), vMajor(-1), vMinor(-1), vRev(-1), vProto(-1)
{
}
int64_t lastRequestTime; int64_t lastRequestTime;
int64_t authenticationExpiryTime; int64_t authenticationExpiryTime;
int vMajor, vMinor, vRev, vProto; int vMajor, vMinor, vRev, vProto;
Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> lastRequestMetaData; Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> lastRequestMetaData;
Identity identity; Identity identity;
inline bool online(const int64_t now) const { return ((now - lastRequestTime) < (ZT_NETWORK_AUTOCONF_DELAY * 2)); } inline bool online(const int64_t now) const
};
struct _MemberStatusHash
{ {
return ((now - lastRequestTime) < (ZT_NETWORK_AUTOCONF_DELAY * 2));
}
};
struct _MemberStatusHash {
inline std::size_t operator()(const _MemberStatusKey& networkIdNodeId) const inline std::size_t operator()(const _MemberStatusKey& networkIdNodeId) const
{ {
return (std::size_t)(networkIdNodeId.networkId + networkIdNodeId.nodeId); return (std::size_t)(networkIdNodeId.networkId + networkIdNodeId.nodeId);

View file

@ -15,15 +15,9 @@
#include "../node/Metrics.hpp" #include "../node/Metrics.hpp"
namespace ZeroTier namespace ZeroTier {
{
FileDB::FileDB(const char *path) : FileDB::FileDB(const char* path) : DB(), _path(path), _networksPath(_path + ZT_PATH_SEPARATOR_S + "network"), _tracePath(_path + ZT_PATH_SEPARATOR_S + "trace"), _running(true)
DB(),
_path(path),
_networksPath(_path + ZT_PATH_SEPARATOR_S + "network"),
_tracePath(_path + ZT_PATH_SEPARATOR_S + "trace"),
_running(true)
{ {
OSUtils::mkdir(_path.c_str()); OSUtils::mkdir(_path.c_str());
OSUtils::lockDownFile(_path.c_str(), true); OSUtils::lockDownFile(_path.c_str(), true);
@ -55,11 +49,15 @@ FileDB::FileDB(const char *path) :
_memberChanged(nullJson2, member, false); _memberChanged(nullJson2, member, false);
Metrics::member_count++; Metrics::member_count++;
} }
} catch ( ... ) {} }
catch (...) {
} }
} }
} }
} catch ( ... ) {} }
}
catch (...) {
}
} }
} }
} }
@ -71,11 +69,19 @@ FileDB::~FileDB()
_running = false; _running = false;
_online_l.unlock(); _online_l.unlock();
_onlineUpdateThread.join(); _onlineUpdateThread.join();
} catch ( ... ) {} }
catch (...) {
}
} }
bool FileDB::waitForReady() { return true; } bool FileDB::waitForReady()
bool FileDB::isReady() { return true; } {
return true;
}
bool FileDB::isReady()
{
return true;
}
bool FileDB::save(nlohmann::json& record, bool notifyListeners) bool FileDB::save(nlohmann::json& record, bool notifyListeners)
{ {
@ -84,7 +90,6 @@ bool FileDB::save(nlohmann::json &record,bool notifyListeners)
try { try {
const std::string objtype = record["objtype"]; const std::string objtype = record["objtype"];
if (objtype == "network") { if (objtype == "network") {
const uint64_t nwid = OSUtils::jsonIntHex(record["id"], 0ULL); const uint64_t nwid = OSUtils::jsonIntHex(record["id"], 0ULL);
if (nwid) { if (nwid) {
nlohmann::json old; nlohmann::json old;
@ -99,9 +104,8 @@ bool FileDB::save(nlohmann::json &record,bool notifyListeners)
modified = true; modified = true;
} }
} }
}
} else if (objtype == "member") { else if (objtype == "member") {
const uint64_t id = OSUtils::jsonIntHex(record["id"], 0ULL); const uint64_t id = OSUtils::jsonIntHex(record["id"], 0ULL);
const uint64_t nwid = OSUtils::jsonIntHex(record["nwid"], 0ULL); const uint64_t nwid = OSUtils::jsonIntHex(record["nwid"], 0ULL);
if ((id) && (nwid)) { if ((id) && (nwid)) {
@ -123,9 +127,10 @@ bool FileDB::save(nlohmann::json &record,bool notifyListeners)
modified = true; modified = true;
} }
} }
} }
} catch ( ... ) {} // drop invalid records missing fields }
catch (...) {
} // drop invalid records missing fields
return modified; return modified;
} }

View file

@ -16,11 +16,9 @@
#include "DB.hpp" #include "DB.hpp"
namespace ZeroTier namespace ZeroTier {
{
class FileDB : public DB class FileDB : public DB {
{
public: public:
FileDB(const char* path); FileDB(const char* path);
virtual ~FileDB(); virtual ~FileDB();

View file

@ -13,33 +13,34 @@
#include "LFDB.hpp" #include "LFDB.hpp"
#include <thread> #include "../ext/cpp-httplib/httplib.h"
#include "../osdep/OSUtils.hpp"
#include <chrono> #include <chrono>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <thread>
#include "../osdep/OSUtils.hpp" namespace ZeroTier {
#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)
{ : DB()
, _myId(myId)
LFDB::LFDB(const Identity &myId,const char *path,const char *lfOwnerPrivate,const char *lfOwnerPublic,const char *lfNodeHost,int lfNodePort,bool storeOnlineState) : , _lfOwnerPrivate((lfOwnerPrivate) ? lfOwnerPrivate : "")
DB(), , _lfOwnerPublic((lfOwnerPublic) ? lfOwnerPublic : "")
_myId(myId), , _lfNodeHost((lfNodeHost) ? lfNodeHost : "127.0.0.1")
_lfOwnerPrivate((lfOwnerPrivate) ? lfOwnerPrivate : ""), , _lfNodePort(((lfNodePort > 0) && (lfNodePort < 65536)) ? lfNodePort : 9980)
_lfOwnerPublic((lfOwnerPublic) ? lfOwnerPublic : ""), , _running(true)
_lfNodeHost((lfNodeHost) ? lfNodeHost : "127.0.0.1"), , _ready(false)
_lfNodePort(((lfNodePort > 0)&&(lfNodePort < 65536)) ? lfNodePort : 9980), , _storeOnlineState(storeOnlineState)
_running(true),
_ready(false),
_storeOnlineState(storeOnlineState)
{ {
_syncThread = std::thread([this]() { _syncThread = std::thread([this]() {
char controllerAddress[24]; char controllerAddress[24];
const uint64_t controllerAddressInt = _myId.address().toInt(); const uint64_t controllerAddressInt = _myId.address().toInt();
_myId.address().toString(controllerAddress); _myId.address().toString(controllerAddress);
std::string networksSelectorName("com.zerotier.controller.lfdb:"); networksSelectorName.append(controllerAddress); networksSelectorName.append("/network"); std::string networksSelectorName("com.zerotier.controller.lfdb:");
networksSelectorName.append(controllerAddress);
networksSelectorName.append("/network");
// LF record masking key is the first 32 bytes of SHA512(controller private key) in hex, // 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. // hiding record values from anything but the controller or someone who has its key.
@ -71,15 +72,19 @@ LFDB::LFDB(const Identity &myId,const char *path,const char *lfOwnerPrivate,cons
if (resp->status == 200) { if (resp->status == 200) {
ns->second.dirty = false; ns->second.dirty = false;
// printf("SET network %.16llx %s\n",ns->first,resp->body.c_str()); // printf("SET network %.16llx %s\n",ns->first,resp->body.c_str());
} else { }
else {
fprintf(stderr, "ERROR: LFDB: %d from node (create/update network): %s" ZT_EOL_S, resp->status, resp->body.c_str()); fprintf(stderr, "ERROR: LFDB: %d from node (create/update network): %s" ZT_EOL_S, resp->status, resp->body.c_str());
} }
} else { }
else {
fprintf(stderr, "ERROR: LFDB: node is offline" ZT_EOL_S); fprintf(stderr, "ERROR: LFDB: node is offline" ZT_EOL_S);
} }
} catch (std::exception &e) { }
catch (std::exception& e) {
fprintf(stderr, "ERROR: LFDB: unexpected exception querying node (create/update network): %s" ZT_EOL_S, e.what()); fprintf(stderr, "ERROR: LFDB: unexpected exception querying node (create/update network): %s" ZT_EOL_S, e.what());
} catch ( ... ) { }
catch (...) {
fprintf(stderr, "ERROR: LFDB: unexpected exception querying node (create/update network): unknown exception" ZT_EOL_S); fprintf(stderr, "ERROR: LFDB: unexpected exception querying node (create/update network): unknown exception" ZT_EOL_S);
} }
} }
@ -123,15 +128,19 @@ LFDB::LFDB(const Identity &myId,const char *path,const char *lfOwnerPrivate,cons
if (resp->status == 200) { if (resp->status == 200) {
ms->second.lastOnlineDirty = false; ms->second.lastOnlineDirty = false;
// printf("SET member online %.16llx %.10llx %s\n",ns->first,ms->first,resp->body.c_str()); // printf("SET member online %.16llx %.10llx %s\n",ns->first,ms->first,resp->body.c_str());
} else { }
else {
fprintf(stderr, "ERROR: LFDB: %d from node (create/update member online status): %s" ZT_EOL_S, resp->status, resp->body.c_str()); fprintf(stderr, "ERROR: LFDB: %d from node (create/update member online status): %s" ZT_EOL_S, resp->status, resp->body.c_str());
} }
} else { }
else {
fprintf(stderr, "ERROR: LFDB: node is offline" ZT_EOL_S); fprintf(stderr, "ERROR: LFDB: node is offline" ZT_EOL_S);
} }
} catch (std::exception &e) { }
catch (std::exception& e) {
fprintf(stderr, "ERROR: LFDB: unexpected exception querying node (create/update member online status): %s" ZT_EOL_S, e.what()); fprintf(stderr, "ERROR: LFDB: unexpected exception querying node (create/update member online status): %s" ZT_EOL_S, e.what());
} catch ( ... ) { }
catch (...) {
fprintf(stderr, "ERROR: LFDB: unexpected exception querying node (create/update member online status): unknown exception" ZT_EOL_S); fprintf(stderr, "ERROR: LFDB: unexpected exception querying node (create/update member online status): unknown exception" ZT_EOL_S);
} }
} }
@ -157,15 +166,19 @@ LFDB::LFDB(const Identity &myId,const char *path,const char *lfOwnerPrivate,cons
if (resp->status == 200) { if (resp->status == 200) {
ms->second.dirty = false; ms->second.dirty = false;
// printf("SET member %.16llx %.10llx %s\n",ns->first,ms->first,resp->body.c_str()); // printf("SET member %.16llx %.10llx %s\n",ns->first,ms->first,resp->body.c_str());
} else { }
else {
fprintf(stderr, "ERROR: LFDB: %d from node (create/update member): %s" ZT_EOL_S, resp->status, resp->body.c_str()); fprintf(stderr, "ERROR: LFDB: %d from node (create/update member): %s" ZT_EOL_S, resp->status, resp->body.c_str());
} }
} else { }
else {
fprintf(stderr, "ERROR: LFDB: node is offline" ZT_EOL_S); fprintf(stderr, "ERROR: LFDB: node is offline" ZT_EOL_S);
} }
} catch (std::exception &e) { }
catch (std::exception& e) {
fprintf(stderr, "ERROR: LFDB: unexpected exception querying node (create/update member): %s" ZT_EOL_S, e.what()); fprintf(stderr, "ERROR: LFDB: unexpected exception querying node (create/update member): %s" ZT_EOL_S, e.what());
} catch ( ... ) { }
catch (...) {
fprintf(stderr, "ERROR: LFDB: unexpected exception querying node (create/update member): unknown exception" ZT_EOL_S); fprintf(stderr, "ERROR: LFDB: unexpected exception querying node (create/update member): unknown exception" ZT_EOL_S);
} }
} }
@ -176,15 +189,22 @@ LFDB::LFDB(const Identity &myId,const char *path,const char *lfOwnerPrivate,cons
try { try {
std::ostringstream query; std::ostringstream query;
query << query << "{"
"{"
"\"Ranges\":[{" "\"Ranges\":[{"
"\"Name\":\"" << networksSelectorName << "\"," "\"Name\":\""
<< networksSelectorName
<< "\","
"\"Range\":[0,18446744073709551615]" "\"Range\":[0,18446744073709551615]"
"}]," "}],"
"\"TimeRange\":[" << timeRangeStart << ",9223372036854775807]," "\"TimeRange\":["
"\"MaskingKey\":\"" << maskingKey << "\"," << timeRangeStart
"\"Owners\":[\"" << _lfOwnerPublic << "\"]" << ",9223372036854775807],"
"\"MaskingKey\":\""
<< maskingKey
<< "\","
"\"Owners\":[\""
<< _lfOwnerPublic
<< "\"]"
"}"; "}";
auto resp = htcli.Post("/query", query.str(), "application/json"); auto resp = htcli.Post("/query", query.str(), "application/json");
if (resp) { if (resp) {
@ -194,7 +214,6 @@ LFDB::LFDB(const Identity &myId,const char *path,const char *lfOwnerPrivate,cons
for (std::size_t ri = 0; ri < results.size(); ++ri) { for (std::size_t ri = 0; ri < results.size(); ++ri) {
nlohmann::json& rset = results[ri]; nlohmann::json& rset = results[ri];
if ((rset.is_array()) && (! rset.empty())) { if ((rset.is_array()) && (! rset.empty())) {
nlohmann::json& result = rset[0]; nlohmann::json& result = rset[0];
if (result.is_object()) { if (result.is_object()) {
nlohmann::json& record = result["Record"]; nlohmann::json& record = result["Record"];
@ -214,45 +233,55 @@ LFDB::LFDB(const Identity &myId,const char *path,const char *lfOwnerPrivate,cons
if (prevRevision < revision) { if (prevRevision < revision) {
_networkChanged(oldNetwork, network, timeRangeStart > 0); _networkChanged(oldNetwork, network, timeRangeStart > 0);
} }
} else { }
else {
nlohmann::json nullJson; nlohmann::json nullJson;
_networkChanged(nullJson, network, timeRangeStart > 0); _networkChanged(nullJson, network, timeRangeStart > 0);
} }
} }
} }
} }
} }
} }
} }
} }
} else { }
else {
fprintf(stderr, "ERROR: LFDB: %d from node (check for network updates): %s" ZT_EOL_S, resp->status, resp->body.c_str()); fprintf(stderr, "ERROR: LFDB: %d from node (check for network updates): %s" ZT_EOL_S, resp->status, resp->body.c_str());
} }
} else { }
else {
fprintf(stderr, "ERROR: LFDB: node is offline" ZT_EOL_S); fprintf(stderr, "ERROR: LFDB: node is offline" ZT_EOL_S);
} }
} catch (std::exception &e) { }
catch (std::exception& e) {
fprintf(stderr, "ERROR: LFDB: unexpected exception querying node (check for network updates): %s" ZT_EOL_S, e.what()); fprintf(stderr, "ERROR: LFDB: unexpected exception querying node (check for network updates): %s" ZT_EOL_S, e.what());
} catch ( ... ) { }
catch (...) {
fprintf(stderr, "ERROR: LFDB: unexpected exception querying node (check for network updates): unknown exception" ZT_EOL_S); fprintf(stderr, "ERROR: LFDB: unexpected exception querying node (check for network updates): unknown exception" ZT_EOL_S);
} }
try { try {
std::ostringstream query; std::ostringstream query;
query << query << "{"
"{"
"\"Ranges\":[{" "\"Ranges\":[{"
"\"Name\":\"" << networksSelectorName << "\"," "\"Name\":\""
<< networksSelectorName
<< "\","
"\"Range\":[0,18446744073709551615]" "\"Range\":[0,18446744073709551615]"
"},{" "},{"
"\"Name\":\"member\"," "\"Name\":\"member\","
"\"Range\":[0,18446744073709551615]" "\"Range\":[0,18446744073709551615]"
"}]," "}],"
"\"TimeRange\":[" << timeRangeStart << ",9223372036854775807]," "\"TimeRange\":["
"\"MaskingKey\":\"" << maskingKey << "\"," << timeRangeStart
"\"Owners\":[\"" << _lfOwnerPublic << "\"]" << ",9223372036854775807],"
"\"MaskingKey\":\""
<< maskingKey
<< "\","
"\"Owners\":[\""
<< _lfOwnerPublic
<< "\"]"
"}"; "}";
auto resp = htcli.Post("/query", query.str(), "application/json"); auto resp = htcli.Post("/query", query.str(), "application/json");
if (resp) { if (resp) {
@ -262,7 +291,6 @@ LFDB::LFDB(const Identity &myId,const char *path,const char *lfOwnerPrivate,cons
for (std::size_t ri = 0; ri < results.size(); ++ri) { for (std::size_t ri = 0; ri < results.size(); ++ri) {
nlohmann::json& rset = results[ri]; nlohmann::json& rset = results[ri];
if ((rset.is_array()) && (! rset.empty())) { if ((rset.is_array()) && (! rset.empty())) {
nlohmann::json& result = rset[0]; nlohmann::json& result = rset[0];
if (result.is_object()) { if (result.is_object()) {
nlohmann::json& record = result["Record"]; nlohmann::json& record = result["Record"];
@ -283,28 +311,31 @@ LFDB::LFDB(const Identity &myId,const char *path,const char *lfOwnerPrivate,cons
const uint64_t prevRevision = oldMember["revision"]; const uint64_t prevRevision = oldMember["revision"];
if (prevRevision < revision) if (prevRevision < revision)
_memberChanged(oldMember, member, timeRangeStart > 0); _memberChanged(oldMember, member, timeRangeStart > 0);
} else if (hasNetwork(nwid)) { }
else if (hasNetwork(nwid)) {
nlohmann::json nullJson; nlohmann::json nullJson;
_memberChanged(nullJson, member, timeRangeStart > 0); _memberChanged(nullJson, member, timeRangeStart > 0);
} }
} }
} }
} }
} }
} }
} }
} }
} else { }
else {
fprintf(stderr, "ERROR: LFDB: %d from node (check for member updates): %s" ZT_EOL_S, resp->status, resp->body.c_str()); fprintf(stderr, "ERROR: LFDB: %d from node (check for member updates): %s" ZT_EOL_S, resp->status, resp->body.c_str());
} }
} else { }
else {
fprintf(stderr, "ERROR: LFDB: node is offline" ZT_EOL_S); fprintf(stderr, "ERROR: LFDB: node is offline" ZT_EOL_S);
} }
} catch (std::exception &e) { }
catch (std::exception& e) {
fprintf(stderr, "ERROR: LFDB: unexpected exception querying node (check for member updates): %s" ZT_EOL_S, e.what()); fprintf(stderr, "ERROR: LFDB: unexpected exception querying node (check for member updates): %s" ZT_EOL_S, e.what());
} catch ( ... ) { }
catch (...) {
fprintf(stderr, "ERROR: LFDB: unexpected exception querying node (check for member updates): unknown exception" ZT_EOL_S); fprintf(stderr, "ERROR: LFDB: unexpected exception querying node (check for member updates): unknown exception" ZT_EOL_S);
} }
@ -358,7 +389,8 @@ bool LFDB::save(nlohmann::json &record,bool notifyListeners)
modified = true; modified = true;
} }
} }
} else if (objtype == "member") { }
else if (objtype == "member") {
const uint64_t nwid = OSUtils::jsonIntHex(record["nwid"], 0ULL); const uint64_t nwid = OSUtils::jsonIntHex(record["nwid"], 0ULL);
const uint64_t id = OSUtils::jsonIntHex(record["id"], 0ULL); const uint64_t id = OSUtils::jsonIntHex(record["id"], 0ULL);
if ((id) && (nwid)) { if ((id) && (nwid)) {

View file

@ -16,18 +16,17 @@
#include "DB.hpp" #include "DB.hpp"
#include <atomic>
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <atomic>
namespace ZeroTier { namespace ZeroTier {
/** /**
* DB implementation for controller that stores data in LF * DB implementation for controller that stores data in LF
*/ */
class LFDB : public DB class LFDB : public DB {
{
public: public:
/** /**
* @param myId This controller's identity * @param myId This controller's identity
@ -55,23 +54,19 @@ protected:
std::string _lfNodeHost; std::string _lfNodeHost;
int _lfNodePort; int _lfNodePort;
struct _MemberState struct _MemberState {
_MemberState() : lastOnlineAddress(), lastOnlineTime(0), dirty(false), lastOnlineDirty(false)
{ {
_MemberState() : }
lastOnlineAddress(),
lastOnlineTime(0),
dirty(false),
lastOnlineDirty(false) {}
InetAddress lastOnlineAddress; InetAddress lastOnlineAddress;
int64_t lastOnlineTime; int64_t lastOnlineTime;
bool dirty; bool dirty;
bool lastOnlineDirty; bool lastOnlineDirty;
}; };
struct _NetworkState struct _NetworkState {
_NetworkState() : members(), dirty(false)
{ {
_NetworkState() : }
members(),
dirty(false) {}
std::unordered_map<uint64_t, _MemberState> members; std::unordered_map<uint64_t, _MemberState> members;
bool dirty; bool dirty;
}; };

File diff suppressed because it is too large Load diff

View file

@ -20,14 +20,13 @@
#define ZT_CENTRAL_CONTROLLER_COMMIT_THREADS 4 #define ZT_CENTRAL_CONTROLLER_COMMIT_THREADS 4
#include "../node/Metrics.hpp"
#include "ConnectionPool.hpp" #include "ConnectionPool.hpp"
#include <pqxx/pqxx>
#include <memory> #include <memory>
#include <pqxx/pqxx>
#include <redis++/redis++.h> #include <redis++/redis++.h>
#include "../node/Metrics.hpp"
extern "C" { extern "C" {
typedef struct pg_conn PGconn; typedef struct pg_conn PGconn;
} }
@ -40,30 +39,30 @@ namespace ZeroTier {
struct RedisConfig; struct RedisConfig;
class PostgresConnection : public Connection { class PostgresConnection : public Connection {
public: public:
virtual ~PostgresConnection() { virtual ~PostgresConnection()
{
} }
std::shared_ptr<pqxx::connection> c; std::shared_ptr<pqxx::connection> c;
int a; int a;
}; };
class PostgresConnFactory : public ConnectionFactory { class PostgresConnFactory : public ConnectionFactory {
public: public:
PostgresConnFactory(std::string &connString) PostgresConnFactory(std::string& connString) : m_connString(connString)
: m_connString(connString)
{ {
} }
virtual std::shared_ptr<Connection> create() { virtual std::shared_ptr<Connection> create()
{
Metrics::conn_counter++; Metrics::conn_counter++;
auto c = std::shared_ptr<PostgresConnection>(new PostgresConnection()); auto c = std::shared_ptr<PostgresConnection>(new PostgresConnection());
c->c = std::make_shared<pqxx::connection>(m_connString); c->c = std::make_shared<pqxx::connection>(m_connString);
return std::static_pointer_cast<Connection>(c); return std::static_pointer_cast<Connection>(c);
} }
private: private:
std::string m_connString; std::string m_connString;
}; };
@ -73,11 +72,13 @@ class PostgreSQL;
class MemberNotificationReceiver : public pqxx::notification_receiver { class MemberNotificationReceiver : public pqxx::notification_receiver {
public: public:
MemberNotificationReceiver(PostgreSQL* p, pqxx::connection& c, const std::string& channel); MemberNotificationReceiver(PostgreSQL* p, pqxx::connection& c, const std::string& channel);
virtual ~MemberNotificationReceiver() { virtual ~MemberNotificationReceiver()
{
fprintf(stderr, "MemberNotificationReceiver destroyed\n"); fprintf(stderr, "MemberNotificationReceiver destroyed\n");
} }
virtual void operator()(const std::string& payload, int backendPid); virtual void operator()(const std::string& payload, int backendPid);
private: private:
PostgreSQL* _psql; PostgreSQL* _psql;
}; };
@ -85,11 +86,13 @@ private:
class NetworkNotificationReceiver : public pqxx::notification_receiver { class NetworkNotificationReceiver : public pqxx::notification_receiver {
public: public:
NetworkNotificationReceiver(PostgreSQL* p, pqxx::connection& c, const std::string& channel); NetworkNotificationReceiver(PostgreSQL* p, pqxx::connection& c, const std::string& channel);
virtual ~NetworkNotificationReceiver() { virtual ~NetworkNotificationReceiver()
{
fprintf(stderr, "NetworkNotificationReceiver destroyed\n"); fprintf(stderr, "NetworkNotificationReceiver destroyed\n");
}; };
virtual void operator()(const std::string& payload, int packend_pid); virtual void operator()(const std::string& payload, int packend_pid);
private: private:
PostgreSQL* _psql; PostgreSQL* _psql;
}; };
@ -100,10 +103,10 @@ private:
* This is for use with ZeroTier Central. Others are free to build and use it * This is for use with ZeroTier Central. Others are free to build and use it
* but be aware that we might change it at any time. * but be aware that we might change it at any time.
*/ */
class PostgreSQL : public DB class PostgreSQL : public DB {
{
friend class MemberNotificationReceiver; friend class MemberNotificationReceiver;
friend class NetworkNotificationReceiver; friend class NetworkNotificationReceiver;
public: public:
PostgreSQL(const Identity& myId, const char* path, int listenPort, RedisConfig* rc); PostgreSQL(const Identity& myId, const char* path, int listenPort, RedisConfig* rc);
virtual ~PostgreSQL(); virtual ~PostgreSQL();
@ -117,15 +120,19 @@ public:
virtual AuthInfo getSSOAuthInfo(const nlohmann::json& member, const std::string& redirectURL); virtual AuthInfo getSSOAuthInfo(const nlohmann::json& member, const std::string& redirectURL);
protected: protected:
struct _PairHasher struct _PairHasher {
inline std::size_t operator()(const std::pair<uint64_t, uint64_t>& p) const
{ {
inline std::size_t operator()(const std::pair<uint64_t,uint64_t> &p) const { return (std::size_t)(p.first ^ p.second); } return (std::size_t)(p.first ^ p.second);
}
}; };
virtual void _memberChanged(nlohmann::json &old,nlohmann::json &memberConfig,bool notifyListeners) { virtual void _memberChanged(nlohmann::json& old, nlohmann::json& memberConfig, bool notifyListeners)
{
DB::_memberChanged(old, memberConfig, notifyListeners); DB::_memberChanged(old, memberConfig, notifyListeners);
} }
virtual void _networkChanged(nlohmann::json &old,nlohmann::json &networkConfig,bool notifyListeners) { virtual void _networkChanged(nlohmann::json& old, nlohmann::json& networkConfig, bool notifyListeners)
{
DB::_networkChanged(old, networkConfig, notifyListeners); DB::_networkChanged(old, networkConfig, notifyListeners);
} }
@ -145,16 +152,12 @@ private:
void onlineNotificationThread(); void onlineNotificationThread();
void onlineNotification_Postgres(); void onlineNotification_Postgres();
void onlineNotification_Redis(); void onlineNotification_Redis();
uint64_t _doRedisUpdate(sw::redis::Transaction &tx, std::string &controllerId, uint64_t _doRedisUpdate(sw::redis::Transaction& tx, std::string& controllerId, std::unordered_map<std::pair<uint64_t, uint64_t>, std::pair<int64_t, InetAddress>, _PairHasher>& lastOnline);
std::unordered_map< std::pair<uint64_t,uint64_t>,std::pair<int64_t,InetAddress>,_PairHasher > &lastOnline);
void configureSmee(); void configureSmee();
void notifyNewMember(const std::string& networkID, const std::string& memberID); void notifyNewMember(const std::string& networkID, const std::string& memberID);
enum OverrideMode { enum OverrideMode { ALLOW_PGBOUNCER_OVERRIDE = 0, NO_OVERRIDE = 1 };
ALLOW_PGBOUNCER_OVERRIDE = 0,
NO_OVERRIDE = 1
};
std::shared_ptr<ConnectionPool<PostgresConnection> > _pool; std::shared_ptr<ConnectionPool<PostgresConnection> > _pool;

View file

@ -10,6 +10,6 @@ struct RedisConfig {
std::string password; std::string password;
bool clusterMode; bool clusterMode;
}; };
} } // namespace ZeroTier
#endif #endif

View file

@ -25,7 +25,6 @@
#include "Switch.hpp" #include "Switch.hpp"
#include "Trace.hpp" #include "Trace.hpp"
#include "Utils.hpp" #include "Utils.hpp"
#include "Switch.hpp"
namespace ZeroTier { namespace ZeroTier {

View file

@ -495,9 +495,7 @@ void Switch::onLocalEthernet(void* tPtr, const SharedPtr<Network>& network, cons
// this prevents problems related to trying to do rx while inside of doing tx, such as acquiring same lock recursively // this prevents problems related to trying to do rx while inside of doing tx, such as acquiring same lock recursively
// //
std::thread([=]() { std::thread([=]() { RR->node->putFrame(tPtr, network->id(), network->userPtr(), peerMac, from, ZT_ETHERTYPE_IPV6, 0, adv, 72); }).detach();
RR->node->putFrame(tPtr, network->id(), network->userPtr(), peerMac, from, ZT_ETHERTYPE_IPV6, 0, adv, 72);
}).detach();
return; // NDP emulation done. We have forged a "fake" reply, so no need to send actual NDP query. return; // NDP emulation done. We have forged a "fake" reply, so no need to send actual NDP query.
} // else no NDP emulation } // else no NDP emulation
@ -532,9 +530,7 @@ void Switch::onLocalEthernet(void* tPtr, const SharedPtr<Network>& network, cons
// //
// same pattern as putFrame call above // same pattern as putFrame call above
// //
std::thread([=]() { std::thread([=]() { RR->node->putFrame(tPtr, network->id(), network->userPtr(), from, to, etherType, vlanId, data, len); }).detach();
RR->node->putFrame(tPtr, network->id(), network->userPtr(), from, to, etherType, vlanId, data, len);
}).detach();
} }
else if (to[0] == MAC::firstOctetForNetwork(network->id())) { else if (to[0] == MAC::firstOctetForNetwork(network->id())) {
// Destination is another ZeroTier peer on the same network // Destination is another ZeroTier peer on the same network

View file

@ -445,7 +445,6 @@ void BSDEthernetTap::threadMain() throw()
for (unsigned int i = 0; i < _concurrency; ++i) { for (unsigned int i = 0; i < _concurrency; ++i) {
_rxThreads.push_back(std::thread([this, i, pinning] { _rxThreads.push_back(std::thread([this, i, pinning] {
if (pinning) { if (pinning) {
int pinCore = i % _concurrency; int pinCore = i % _concurrency;
fprintf(stderr, "Pinning thread %d to core %d\n", i, pinCore); fprintf(stderr, "Pinning thread %d to core %d\n", i, pinCore);
@ -455,8 +454,7 @@ void BSDEthernetTap::threadMain() throw()
CPU_SET(pinCore, &cpuset); CPU_SET(pinCore, &cpuset);
// int rc = sched_setaffinity(self, sizeof(cpu_set_t), &cpuset); // int rc = sched_setaffinity(self, sizeof(cpu_set_t), &cpuset);
int rc = pthread_setaffinity_np(self, sizeof(cpu_set_t), &cpuset); int rc = pthread_setaffinity_np(self, sizeof(cpu_set_t), &cpuset);
if (rc != 0) if (rc != 0) {
{
fprintf(stderr, "Failed to pin thread %d to core %d: %s\n", i, pinCore, strerror(errno)); fprintf(stderr, "Failed to pin thread %d to core %d: %s\n", i, pinCore, strerror(errno));
exit(1); exit(1);
} }
@ -487,7 +485,8 @@ void BSDEthernetTap::threadMain() throw()
if (n < 0) { if (n < 0) {
if ((errno != EINTR) && (errno != ETIMEDOUT)) if ((errno != EINTR) && (errno != ETIMEDOUT))
break; break;
} else { }
else {
// Some tap drivers like to send the ethernet frame and the // Some tap drivers like to send the ethernet frame and the
// payload in two chunks, so handle that by accumulating // payload in two chunks, so handle that by accumulating
// data until we have at least a frame. // data until we have at least a frame.

View file

@ -19,32 +19,30 @@
// HACK! Will eventually use epoll() or something in Phy<> instead of select(). // HACK! Will eventually use epoll() or something in Phy<> instead of select().
// Also be sure to change ulimit -n and fs.file-max in /etc/sysctl.conf on relays. // Also be sure to change ulimit -n and fs.file-max in /etc/sysctl.conf on relays.
#if defined(__linux__) || defined(__LINUX__) || defined(__LINUX) || defined(LINUX) #if defined(__linux__) || defined(__LINUX__) || defined(__LINUX) || defined(LINUX)
#include <linux/posix_types.h>
#include <bits/types.h> #include <bits/types.h>
#include <linux/posix_types.h>
#undef __FD_SETSIZE #undef __FD_SETSIZE
#define __FD_SETSIZE 1048576 #define __FD_SETSIZE 1048576
#undef FD_SETSIZE #undef FD_SETSIZE
#define FD_SETSIZE 1048576 #define FD_SETSIZE 1048576
#endif #endif
#include "../node/Metrics.hpp"
#include "../osdep/Phy.hpp"
#include <algorithm>
#include <map>
#include <set>
#include <signal.h>
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h>
#include <stdint.h>
#include <unistd.h>
#include <signal.h>
#include <map>
#include <set>
#include <string> #include <string>
#include <algorithm> #include <time.h>
#include <unistd.h>
#include <vector> #include <vector>
#include "../osdep/Phy.hpp"
#include "../node/Metrics.hpp"
#define ZT_TCP_PROXY_CONNECTION_TIMEOUT_SECONDS 300 #define ZT_TCP_PROXY_CONNECTION_TIMEOUT_SECONDS 300
#define ZT_TCP_PROXY_TCP_PORT 443 #define ZT_TCP_PROXY_TCP_PORT 443
@ -88,12 +86,10 @@ using namespace ZeroTier;
*/ */
struct TcpProxyService; struct TcpProxyService;
struct TcpProxyService struct TcpProxyService {
{
Phy<TcpProxyService*>* phy; Phy<TcpProxyService*>* phy;
int udpPortCounter; int udpPortCounter;
struct Client struct Client {
{
char tcpReadBuf[131072]; char tcpReadBuf[131072];
char tcpWriteBuf[131072]; char tcpWriteBuf[131072];
unsigned long tcpWritePtr; unsigned long tcpWritePtr;
@ -215,7 +211,8 @@ struct TcpProxyService
// Right now just sending this means the client is 'new enough' for the IP header // Right now just sending this means the client is 'new enough' for the IP header
c.newVersion = true; c.newVersion = true;
printf("<< TCP %.16llx HELLO\n", (unsigned long long)*uptr); printf("<< TCP %.16llx HELLO\n", (unsigned long long)*uptr);
} else if (mlen >= 7) { }
else if (mlen >= 7) {
char* payload = c.tcpReadBuf + 5; char* payload = c.tcpReadBuf + 5;
unsigned long payloadLen = mlen; unsigned long payloadLen = mlen;
@ -232,7 +229,8 @@ struct TcpProxyService
payload += 2; payload += 2;
payloadLen -= 7; payloadLen -= 7;
} }
} else { }
else {
// For old clients we will just proxy everything to a local ZT instance. The // For old clients we will just proxy everything to a local ZT instance. The
// fact that this will come from 127.0.0.1 will in turn prevent that instance // fact that this will come from 127.0.0.1 will in turn prevent that instance
// from doing unite() with us. It'll just forward. There will not be many of // from doing unite() with us. It'll just forward. There will not be many of
@ -265,7 +263,9 @@ struct TcpProxyService
if (! c.tcpWritePtr) if (! c.tcpWritePtr)
phy->setNotifyWritable(sock, false); phy->setNotifyWritable(sock, false);
} }
} else phy->setNotifyWritable(sock,false); }
else
phy->setNotifyWritable(sock, false);
} }
void doHousekeeping() void doHousekeeping()