DB.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2025-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_CONTROLLER_DB_HPP
  14. #define ZT_CONTROLLER_DB_HPP
  15. //#define ZT_CONTROLLER_USE_LIBPQ
  16. #include "../node/Constants.hpp"
  17. #include "../node/Identity.hpp"
  18. #include "../node/InetAddress.hpp"
  19. #include "../osdep/OSUtils.hpp"
  20. #include "../osdep/BlockingQueue.hpp"
  21. #include <memory>
  22. #include <string>
  23. #include <thread>
  24. #include <unordered_map>
  25. #include <unordered_set>
  26. #include <vector>
  27. #include <atomic>
  28. #include <mutex>
  29. #include <set>
  30. #include <map>
  31. #include "../ext/json/json.hpp"
  32. namespace ZeroTier
  33. {
  34. /**
  35. * Base class with common infrastructure for all controller DB implementations
  36. */
  37. class DB
  38. {
  39. public:
  40. class ChangeListener
  41. {
  42. public:
  43. ChangeListener() {}
  44. virtual ~ChangeListener() {}
  45. virtual void onNetworkUpdate(const void *db,uint64_t networkId,const nlohmann::json &network) {}
  46. virtual void onNetworkMemberUpdate(const void *db,uint64_t networkId,uint64_t memberId,const nlohmann::json &member) {}
  47. virtual void onNetworkMemberDeauthorize(const void *db,uint64_t networkId,uint64_t memberId) {}
  48. };
  49. struct NetworkSummaryInfo
  50. {
  51. NetworkSummaryInfo() : authorizedMemberCount(0),totalMemberCount(0),mostRecentDeauthTime(0) {}
  52. std::vector<Address> activeBridges;
  53. std::vector<InetAddress> allocatedIps;
  54. unsigned long authorizedMemberCount;
  55. unsigned long totalMemberCount;
  56. int64_t mostRecentDeauthTime;
  57. };
  58. static void initNetwork(nlohmann::json &network);
  59. static void initMember(nlohmann::json &member);
  60. static void cleanNetwork(nlohmann::json &network);
  61. static void cleanMember(nlohmann::json &member);
  62. DB();
  63. virtual ~DB();
  64. virtual bool waitForReady() = 0;
  65. virtual bool isReady() = 0;
  66. inline bool hasNetwork(const uint64_t networkId) const
  67. {
  68. std::lock_guard<std::mutex> l(_networks_l);
  69. return (_networks.find(networkId) != _networks.end());
  70. }
  71. bool get(const uint64_t networkId,nlohmann::json &network);
  72. bool get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member);
  73. bool get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member,NetworkSummaryInfo &info);
  74. bool get(const uint64_t networkId,nlohmann::json &network,std::vector<nlohmann::json> &members);
  75. void networks(std::set<uint64_t> &networks);
  76. template<typename F>
  77. inline void each(F f)
  78. {
  79. nlohmann::json nullJson;
  80. std::lock_guard<std::mutex> lck(_networks_l);
  81. for(auto nw=_networks.begin();nw!=_networks.end();++nw) {
  82. f(nw->first,nw->second->config,0,nullJson); // first provide network with 0 for member ID
  83. for(auto m=nw->second->members.begin();m!=nw->second->members.end();++m) {
  84. f(nw->first,nw->second->config,m->first,m->second);
  85. }
  86. }
  87. }
  88. virtual bool save(nlohmann::json &record,bool notifyListeners) = 0;
  89. virtual void eraseNetwork(const uint64_t networkId) = 0;
  90. virtual void eraseMember(const uint64_t networkId,const uint64_t memberId) = 0;
  91. virtual void nodeIsOnline(const uint64_t networkId,const uint64_t memberId,const InetAddress &physicalAddress) = 0;
  92. virtual std::string getSSOAuthURL(const nlohmann::json &member, const std::string &redirectURL) { return ""; }
  93. inline void addListener(DB::ChangeListener *const listener)
  94. {
  95. std::lock_guard<std::mutex> l(_changeListeners_l);
  96. _changeListeners.push_back(listener);
  97. }
  98. protected:
  99. static inline bool _compareRecords(const nlohmann::json &a,const nlohmann::json &b)
  100. {
  101. if (a.is_object() == b.is_object()) {
  102. if (a.is_object()) {
  103. if (a.size() != b.size())
  104. return false;
  105. auto amap = a.get<nlohmann::json::object_t>();
  106. auto bmap = b.get<nlohmann::json::object_t>();
  107. for(auto ai=amap.begin();ai!=amap.end();++ai) {
  108. if (ai->first != "revision") { // ignore revision, compare only non-revision-counter fields
  109. auto bi = bmap.find(ai->first);
  110. if ((bi == bmap.end())||(bi->second != ai->second))
  111. return false;
  112. }
  113. }
  114. return true;
  115. }
  116. return (a == b);
  117. }
  118. return false;
  119. }
  120. struct _Network
  121. {
  122. _Network() : mostRecentDeauthTime(0) {}
  123. nlohmann::json config;
  124. std::unordered_map<uint64_t,nlohmann::json> members;
  125. std::unordered_set<uint64_t> activeBridgeMembers;
  126. std::unordered_set<uint64_t> authorizedMembers;
  127. std::unordered_set<InetAddress,InetAddress::Hasher> allocatedIps;
  128. int64_t mostRecentDeauthTime;
  129. std::mutex lock;
  130. };
  131. virtual void _memberChanged(nlohmann::json &old,nlohmann::json &memberConfig,bool notifyListeners);
  132. virtual void _networkChanged(nlohmann::json &old,nlohmann::json &networkConfig,bool notifyListeners);
  133. void _fillSummaryInfo(const std::shared_ptr<_Network> &nw,NetworkSummaryInfo &info);
  134. std::vector<DB::ChangeListener *> _changeListeners;
  135. std::unordered_map< uint64_t,std::shared_ptr<_Network> > _networks;
  136. std::unordered_multimap< uint64_t,uint64_t > _networkByMember;
  137. mutable std::mutex _changeListeners_l;
  138. mutable std::mutex _networks_l;
  139. };
  140. } // namespace ZeroTier
  141. #endif