DB.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #include "../node/Constants.hpp"
  29. #include "../node/Identity.hpp"
  30. #include "../node/InetAddress.hpp"
  31. #include "../osdep/OSUtils.hpp"
  32. #include "../osdep/BlockingQueue.hpp"
  33. #include <memory>
  34. #include <string>
  35. #include <thread>
  36. #include <unordered_map>
  37. #include <unordered_set>
  38. #include <vector>
  39. #include <atomic>
  40. #include <mutex>
  41. #include "../ext/json/json.hpp"
  42. namespace ZeroTier
  43. {
  44. /**
  45. * Base class with common infrastructure for all controller DB implementations
  46. */
  47. class DB
  48. {
  49. public:
  50. class ChangeListener
  51. {
  52. public:
  53. ChangeListener() {}
  54. virtual ~ChangeListener() {}
  55. virtual void onNetworkUpdate(uint64_t networkId,const nlohmann::json &network) {}
  56. virtual void onNetworkMemberUpdate(uint64_t networkId,uint64_t memberId,const nlohmann::json &member) {}
  57. virtual void onNetworkMemberDeauthorize(uint64_t networkId,uint64_t memberId) {}
  58. };
  59. struct NetworkSummaryInfo
  60. {
  61. NetworkSummaryInfo() : authorizedMemberCount(0),totalMemberCount(0),mostRecentDeauthTime(0) {}
  62. std::vector<Address> activeBridges;
  63. std::vector<InetAddress> allocatedIps;
  64. unsigned long authorizedMemberCount;
  65. unsigned long totalMemberCount;
  66. int64_t mostRecentDeauthTime;
  67. };
  68. static void initNetwork(nlohmann::json &network);
  69. static void initMember(nlohmann::json &member);
  70. static void cleanNetwork(nlohmann::json &network);
  71. static void cleanMember(nlohmann::json &member);
  72. DB(const Identity &myId,const char *path);
  73. virtual ~DB();
  74. virtual bool waitForReady() = 0;
  75. virtual bool isReady() = 0;
  76. inline bool hasNetwork(const uint64_t networkId) const
  77. {
  78. std::lock_guard<std::mutex> l(_networks_l);
  79. return (_networks.find(networkId) != _networks.end());
  80. }
  81. bool get(const uint64_t networkId,nlohmann::json &network);
  82. bool get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member);
  83. bool get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member,NetworkSummaryInfo &info);
  84. bool get(const uint64_t networkId,nlohmann::json &network,std::vector<nlohmann::json> &members);
  85. bool summary(const uint64_t networkId,NetworkSummaryInfo &info);
  86. void networks(std::vector<uint64_t> &networks);
  87. virtual void save(nlohmann::json *orig,nlohmann::json &record) = 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. protected:
  97. struct _Network
  98. {
  99. _Network() : mostRecentDeauthTime(0) {}
  100. nlohmann::json config;
  101. std::unordered_map<uint64_t,nlohmann::json> members;
  102. std::unordered_set<uint64_t> activeBridgeMembers;
  103. std::unordered_set<uint64_t> authorizedMembers;
  104. std::unordered_set<InetAddress,InetAddress::Hasher> allocatedIps;
  105. int64_t mostRecentDeauthTime;
  106. std::mutex lock;
  107. };
  108. void _memberChanged(nlohmann::json &old,nlohmann::json &memberConfig,bool initialized);
  109. void _networkChanged(nlohmann::json &old,nlohmann::json &networkConfig,bool initialized);
  110. void _fillSummaryInfo(const std::shared_ptr<_Network> &nw,NetworkSummaryInfo &info);
  111. const Identity _myId;
  112. const Address _myAddress;
  113. const std::string _path;
  114. std::string _myAddressStr;
  115. std::vector<DB::ChangeListener *> _changeListeners;
  116. std::unordered_map< uint64_t,std::shared_ptr<_Network> > _networks;
  117. std::unordered_multimap< uint64_t,uint64_t > _networkByMember;
  118. mutable std::mutex _changeListeners_l;
  119. mutable std::mutex _networks_l;
  120. };
  121. } // namespace ZeroTier
  122. #endif