DB.hpp 4.7 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. //#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. 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. inline void addListener(DB::ChangeListener *const listener)
  93. {
  94. std::lock_guard<std::mutex> l(_changeListeners_l);
  95. _changeListeners.push_back(listener);
  96. }
  97. protected:
  98. struct _Network
  99. {
  100. _Network() : mostRecentDeauthTime(0) {}
  101. nlohmann::json config;
  102. std::unordered_map<uint64_t,nlohmann::json> members;
  103. std::unordered_set<uint64_t> activeBridgeMembers;
  104. std::unordered_set<uint64_t> authorizedMembers;
  105. std::unordered_set<InetAddress,InetAddress::Hasher> allocatedIps;
  106. int64_t mostRecentDeauthTime;
  107. std::mutex lock;
  108. };
  109. void _memberChanged(nlohmann::json &old,nlohmann::json &memberConfig,bool notifyListeners);
  110. void _networkChanged(nlohmann::json &old,nlohmann::json &networkConfig,bool notifyListeners);
  111. void _fillSummaryInfo(const std::shared_ptr<_Network> &nw,NetworkSummaryInfo &info);
  112. std::vector<DB::ChangeListener *> _changeListeners;
  113. std::unordered_map< uint64_t,std::shared_ptr<_Network> > _networks;
  114. std::unordered_multimap< uint64_t,uint64_t > _networkByMember;
  115. mutable std::mutex _changeListeners_l;
  116. mutable std::mutex _networks_l;
  117. };
  118. } // namespace ZeroTier
  119. #endif