DB.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_CONTROLLER_DB_HPP
  27. #define ZT_CONTROLLER_DB_HPP
  28. //#define ZT_CONTROLLER_USE_LIBPQ
  29. #include "../node/Constants.hpp"
  30. #include "../node/Identity.hpp"
  31. #include "../node/InetAddress.hpp"
  32. #include "../osdep/OSUtils.hpp"
  33. #include "../osdep/BlockingQueue.hpp"
  34. #include <memory>
  35. #include <string>
  36. #include <thread>
  37. #include <unordered_map>
  38. #include <unordered_set>
  39. #include <vector>
  40. #include <atomic>
  41. #include <mutex>
  42. #include <set>
  43. #include "../ext/json/json.hpp"
  44. namespace ZeroTier
  45. {
  46. /**
  47. * Base class with common infrastructure for all controller DB implementations
  48. */
  49. class DB
  50. {
  51. public:
  52. class ChangeListener
  53. {
  54. public:
  55. ChangeListener() {}
  56. virtual ~ChangeListener() {}
  57. virtual void onNetworkUpdate(const void *db,uint64_t networkId,const nlohmann::json &network) {}
  58. virtual void onNetworkMemberUpdate(const void *db,uint64_t networkId,uint64_t memberId,const nlohmann::json &member) {}
  59. virtual void onNetworkMemberDeauthorize(const void *db,uint64_t networkId,uint64_t memberId) {}
  60. };
  61. struct NetworkSummaryInfo
  62. {
  63. NetworkSummaryInfo() : authorizedMemberCount(0),totalMemberCount(0),mostRecentDeauthTime(0) {}
  64. std::vector<Address> activeBridges;
  65. std::vector<InetAddress> allocatedIps;
  66. unsigned long authorizedMemberCount;
  67. unsigned long totalMemberCount;
  68. int64_t mostRecentDeauthTime;
  69. };
  70. static void initNetwork(nlohmann::json &network);
  71. static void initMember(nlohmann::json &member);
  72. static void cleanNetwork(nlohmann::json &network);
  73. static void cleanMember(nlohmann::json &member);
  74. DB();
  75. virtual ~DB();
  76. virtual bool waitForReady() = 0;
  77. virtual bool isReady() = 0;
  78. inline bool hasNetwork(const uint64_t networkId) const
  79. {
  80. std::lock_guard<std::mutex> l(_networks_l);
  81. return (_networks.find(networkId) != _networks.end());
  82. }
  83. bool get(const uint64_t networkId,nlohmann::json &network);
  84. bool get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member);
  85. bool get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member,NetworkSummaryInfo &info);
  86. bool get(const uint64_t networkId,nlohmann::json &network,std::vector<nlohmann::json> &members);
  87. void networks(std::set<uint64_t> &networks);
  88. template<typename F>
  89. inline void each(F f)
  90. {
  91. nlohmann::json nullJson;
  92. std::lock_guard<std::mutex> lck(_networks_l);
  93. for(auto nw=_networks.begin();nw!=_networks.end();++nw) {
  94. f(nw->first,nw->second->config,0,nullJson); // first provide network with 0 for member ID
  95. for(auto m=nw->second->members.begin();m!=nw->second->members.end();++m) {
  96. f(nw->first,nw->second->config,m->first,m->second);
  97. }
  98. }
  99. }
  100. virtual bool save(nlohmann::json &record,bool notifyListeners) = 0;
  101. virtual void eraseNetwork(const uint64_t networkId) = 0;
  102. virtual void eraseMember(const uint64_t networkId,const uint64_t memberId) = 0;
  103. virtual void nodeIsOnline(const uint64_t networkId,const uint64_t memberId,const InetAddress &physicalAddress) = 0;
  104. inline void addListener(DB::ChangeListener *const listener)
  105. {
  106. std::lock_guard<std::mutex> l(_changeListeners_l);
  107. _changeListeners.push_back(listener);
  108. }
  109. protected:
  110. static inline bool _compareRecords(const nlohmann::json &a,const nlohmann::json &b)
  111. {
  112. if (a.is_object() == b.is_object()) {
  113. if (a.is_object()) {
  114. if (a.size() != b.size())
  115. return false;
  116. auto amap = a.get<nlohmann::json::object_t>();
  117. auto bmap = b.get<nlohmann::json::object_t>();
  118. for(auto ai=amap.begin();ai!=amap.end();++ai) {
  119. if (ai->first != "revision") { // ignore revision, compare only non-revision-counter fields
  120. auto bi = bmap.find(ai->first);
  121. if ((bi == bmap.end())||(bi->second != ai->second))
  122. return false;
  123. }
  124. }
  125. return true;
  126. }
  127. return (a == b);
  128. }
  129. return false;
  130. }
  131. struct _Network
  132. {
  133. _Network() : mostRecentDeauthTime(0) {}
  134. nlohmann::json config;
  135. std::unordered_map<uint64_t,nlohmann::json> members;
  136. std::unordered_set<uint64_t> activeBridgeMembers;
  137. std::unordered_set<uint64_t> authorizedMembers;
  138. std::unordered_set<InetAddress,InetAddress::Hasher> allocatedIps;
  139. int64_t mostRecentDeauthTime;
  140. std::mutex lock;
  141. };
  142. void _memberChanged(nlohmann::json &old,nlohmann::json &memberConfig,bool notifyListeners);
  143. void _networkChanged(nlohmann::json &old,nlohmann::json &networkConfig,bool notifyListeners);
  144. void _fillSummaryInfo(const std::shared_ptr<_Network> &nw,NetworkSummaryInfo &info);
  145. std::vector<DB::ChangeListener *> _changeListeners;
  146. std::unordered_map< uint64_t,std::shared_ptr<_Network> > _networks;
  147. std::unordered_multimap< uint64_t,uint64_t > _networkByMember;
  148. mutable std::mutex _changeListeners_l;
  149. mutable std::mutex _networks_l;
  150. };
  151. } // namespace ZeroTier
  152. #endif