2
0

DB.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. #define ZT_MEMBER_AUTH_TIMEOUT_NOTIFY_BEFORE 25000
  33. namespace ZeroTier
  34. {
  35. /**
  36. * Base class with common infrastructure for all controller DB implementations
  37. */
  38. class DB
  39. {
  40. public:
  41. class ChangeListener
  42. {
  43. public:
  44. ChangeListener() {}
  45. virtual ~ChangeListener() {}
  46. virtual void onNetworkUpdate(const void *db,uint64_t networkId,const nlohmann::json &network) {}
  47. virtual void onNetworkMemberUpdate(const void *db,uint64_t networkId,uint64_t memberId,const nlohmann::json &member) {}
  48. virtual void onNetworkMemberDeauthorize(const void *db,uint64_t networkId,uint64_t memberId) {}
  49. };
  50. struct NetworkSummaryInfo
  51. {
  52. NetworkSummaryInfo() : authorizedMemberCount(0),totalMemberCount(0),mostRecentDeauthTime(0) {}
  53. std::vector<Address> activeBridges;
  54. std::vector<InetAddress> allocatedIps;
  55. unsigned long authorizedMemberCount;
  56. unsigned long totalMemberCount;
  57. int64_t mostRecentDeauthTime;
  58. };
  59. static void initNetwork(nlohmann::json &network);
  60. static void initMember(nlohmann::json &member);
  61. static void cleanNetwork(nlohmann::json &network);
  62. static void cleanMember(nlohmann::json &member);
  63. DB();
  64. virtual ~DB();
  65. virtual bool waitForReady() = 0;
  66. virtual bool isReady() = 0;
  67. inline bool hasNetwork(const uint64_t networkId) const
  68. {
  69. std::lock_guard<std::mutex> l(_networks_l);
  70. return (_networks.find(networkId) != _networks.end());
  71. }
  72. bool get(const uint64_t networkId,nlohmann::json &network);
  73. bool get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member);
  74. bool get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member,NetworkSummaryInfo &info);
  75. bool get(const uint64_t networkId,nlohmann::json &network,std::vector<nlohmann::json> &members);
  76. void networks(std::set<uint64_t> &networks);
  77. template<typename F>
  78. inline void each(F f)
  79. {
  80. nlohmann::json nullJson;
  81. std::lock_guard<std::mutex> lck(_networks_l);
  82. for(auto nw=_networks.begin();nw!=_networks.end();++nw) {
  83. f(nw->first,nw->second->config,0,nullJson); // first provide network with 0 for member ID
  84. for(auto m=nw->second->members.begin();m!=nw->second->members.end();++m) {
  85. f(nw->first,nw->second->config,m->first,m->second);
  86. }
  87. }
  88. }
  89. virtual bool save(nlohmann::json &record,bool notifyListeners) = 0;
  90. virtual void eraseNetwork(const uint64_t networkId) = 0;
  91. virtual void eraseMember(const uint64_t networkId,const uint64_t memberId) = 0;
  92. virtual void nodeIsOnline(const uint64_t networkId,const uint64_t memberId,const InetAddress &physicalAddress) = 0;
  93. virtual std::string getSSOAuthURL(const nlohmann::json &member, const std::string &redirectURL) { return ""; }
  94. virtual void networkMemberSSOHasExpired(uint64_t nwid, int64_t ts);
  95. inline void addListener(DB::ChangeListener *const listener)
  96. {
  97. std::lock_guard<std::mutex> l(_changeListeners_l);
  98. _changeListeners.push_back(listener);
  99. }
  100. protected:
  101. static inline bool _compareRecords(const nlohmann::json &a,const nlohmann::json &b)
  102. {
  103. if (a.is_object() == b.is_object()) {
  104. if (a.is_object()) {
  105. if (a.size() != b.size())
  106. return false;
  107. auto amap = a.get<nlohmann::json::object_t>();
  108. auto bmap = b.get<nlohmann::json::object_t>();
  109. for(auto ai=amap.begin();ai!=amap.end();++ai) {
  110. if (ai->first != "revision") { // ignore revision, compare only non-revision-counter fields
  111. auto bi = bmap.find(ai->first);
  112. if ((bi == bmap.end())||(bi->second != ai->second))
  113. return false;
  114. }
  115. }
  116. return true;
  117. }
  118. return (a == b);
  119. }
  120. return false;
  121. }
  122. struct _Network
  123. {
  124. _Network() : mostRecentDeauthTime(0) {}
  125. nlohmann::json config;
  126. std::unordered_map<uint64_t,nlohmann::json> members;
  127. std::unordered_set<uint64_t> activeBridgeMembers;
  128. std::unordered_set<uint64_t> authorizedMembers;
  129. std::unordered_set<InetAddress,InetAddress::Hasher> allocatedIps;
  130. int64_t mostRecentDeauthTime;
  131. std::mutex lock;
  132. };
  133. virtual void _memberChanged(nlohmann::json &old,nlohmann::json &memberConfig,bool notifyListeners);
  134. virtual void _networkChanged(nlohmann::json &old,nlohmann::json &networkConfig,bool notifyListeners);
  135. void _fillSummaryInfo(const std::shared_ptr<_Network> &nw,NetworkSummaryInfo &info);
  136. std::vector<DB::ChangeListener *> _changeListeners;
  137. std::unordered_map< uint64_t,std::shared_ptr<_Network> > _networks;
  138. std::unordered_multimap< uint64_t,uint64_t > _networkByMember;
  139. mutable std::mutex _changeListeners_l;
  140. mutable std::mutex _networks_l;
  141. };
  142. } // namespace ZeroTier
  143. #endif