DB.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 "../ext/json/json.hpp"
  43. namespace ZeroTier
  44. {
  45. /**
  46. * Base class with common infrastructure for all controller DB implementations
  47. */
  48. class DB
  49. {
  50. public:
  51. class ChangeListener
  52. {
  53. public:
  54. ChangeListener() {}
  55. virtual ~ChangeListener() {}
  56. virtual void onNetworkUpdate(const DB *db,uint64_t networkId,const nlohmann::json &network) {}
  57. virtual void onNetworkMemberUpdate(const DB *db,uint64_t networkId,uint64_t memberId,const nlohmann::json &member) {}
  58. virtual void onNetworkMemberDeauthorize(const DB *db,uint64_t networkId,uint64_t memberId) {}
  59. virtual void onNetworkMemberOnline(const DB *db,uint64_t networkId,uint64_t memberId,const InetAddress &physicalAddress) {}
  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(const Identity &myId,const char *path);
  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. bool summary(const uint64_t networkId,NetworkSummaryInfo &info);
  88. void networks(std::vector<uint64_t> &networks);
  89. virtual void save(nlohmann::json &record) = 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. inline void addListener(DB::ChangeListener *const listener)
  94. {
  95. std::lock_guard<std::mutex> l(_changeListeners_l);
  96. _changeListeners.push_back(listener);
  97. }
  98. protected:
  99. struct _Network
  100. {
  101. _Network() : mostRecentDeauthTime(0) {}
  102. nlohmann::json config;
  103. std::unordered_map<uint64_t,nlohmann::json> members;
  104. std::unordered_set<uint64_t> activeBridgeMembers;
  105. std::unordered_set<uint64_t> authorizedMembers;
  106. std::unordered_set<InetAddress,InetAddress::Hasher> allocatedIps;
  107. int64_t mostRecentDeauthTime;
  108. std::mutex lock;
  109. };
  110. void _memberChanged(nlohmann::json &old,nlohmann::json &memberConfig,bool initialized);
  111. void _networkChanged(nlohmann::json &old,nlohmann::json &networkConfig,bool initialized);
  112. void _fillSummaryInfo(const std::shared_ptr<_Network> &nw,NetworkSummaryInfo &info);
  113. const Identity _myId;
  114. const Address _myAddress;
  115. const std::string _path;
  116. std::string _myAddressStr;
  117. std::vector<DB::ChangeListener *> _changeListeners;
  118. std::unordered_map< uint64_t,std::shared_ptr<_Network> > _networks;
  119. std::unordered_multimap< uint64_t,uint64_t > _networkByMember;
  120. mutable std::mutex _changeListeners_l;
  121. mutable std::mutex _networks_l;
  122. };
  123. } // namespace ZeroTier
  124. #endif