DB.hpp 5.4 KB

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