DB.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2018 ZeroTier, Inc.
  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. #ifndef ZT_CONTROLLER_DB_HPP
  19. #define ZT_CONTROLLER_DB_HPP
  20. #include "../node/Constants.hpp"
  21. #include "../node/Identity.hpp"
  22. #include "../node/InetAddress.hpp"
  23. #include "../osdep/OSUtils.hpp"
  24. #include "../osdep/BlockingQueue.hpp"
  25. #include <memory>
  26. #include <string>
  27. #include <thread>
  28. #include <unordered_map>
  29. #include <unordered_set>
  30. #include <vector>
  31. #include <atomic>
  32. #include "../ext/json/json.hpp"
  33. namespace ZeroTier
  34. {
  35. class EmbeddedNetworkController;
  36. /**
  37. * Base class with common infrastructure for all controller DB implementations
  38. */
  39. class DB
  40. {
  41. public:
  42. struct NetworkSummaryInfo
  43. {
  44. NetworkSummaryInfo() : authorizedMemberCount(0),totalMemberCount(0),mostRecentDeauthTime(0) {}
  45. std::vector<Address> activeBridges;
  46. std::vector<InetAddress> allocatedIps;
  47. unsigned long authorizedMemberCount;
  48. unsigned long totalMemberCount;
  49. int64_t mostRecentDeauthTime;
  50. };
  51. /**
  52. * Ensure that all network fields are present
  53. */
  54. static void initNetwork(nlohmann::json &network);
  55. /**
  56. * Ensure that all member fields are present
  57. */
  58. static void initMember(nlohmann::json &member);
  59. /**
  60. * Remove old and temporary network fields
  61. */
  62. static void cleanNetwork(nlohmann::json &network);
  63. /**
  64. * Remove old and temporary member fields
  65. */
  66. static void cleanMember(nlohmann::json &member);
  67. DB(EmbeddedNetworkController *const nc,const Identity &myId,const char *path);
  68. virtual ~DB();
  69. virtual bool waitForReady() = 0;
  70. virtual bool isReady() = 0;
  71. inline bool hasNetwork(const uint64_t networkId) const
  72. {
  73. std::lock_guard<std::mutex> l(_networks_l);
  74. return (_networks.find(networkId) != _networks.end());
  75. }
  76. bool get(const uint64_t networkId,nlohmann::json &network);
  77. bool get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member);
  78. bool get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member,NetworkSummaryInfo &info);
  79. bool get(const uint64_t networkId,nlohmann::json &network,std::vector<nlohmann::json> &members);
  80. bool summary(const uint64_t networkId,NetworkSummaryInfo &info);
  81. void networks(std::vector<uint64_t> &networks);
  82. virtual void save(nlohmann::json *orig,nlohmann::json &record) = 0;
  83. virtual void eraseNetwork(const uint64_t networkId) = 0;
  84. virtual void eraseMember(const uint64_t networkId,const uint64_t memberId) = 0;
  85. virtual void nodeIsOnline(const uint64_t networkId,const uint64_t memberId,const InetAddress &physicalAddress) = 0;
  86. protected:
  87. struct _Network
  88. {
  89. _Network() : mostRecentDeauthTime(0) {}
  90. nlohmann::json config;
  91. std::unordered_map<uint64_t,nlohmann::json> members;
  92. std::unordered_set<uint64_t> activeBridgeMembers;
  93. std::unordered_set<uint64_t> authorizedMembers;
  94. std::unordered_set<InetAddress,InetAddress::Hasher> allocatedIps;
  95. int64_t mostRecentDeauthTime;
  96. std::mutex lock;
  97. };
  98. void _memberChanged(nlohmann::json &old,nlohmann::json &memberConfig,bool push);
  99. void _networkChanged(nlohmann::json &old,nlohmann::json &networkConfig,bool push);
  100. void _fillSummaryInfo(const std::shared_ptr<_Network> &nw,NetworkSummaryInfo &info);
  101. EmbeddedNetworkController *const _controller;
  102. const Identity _myId;
  103. const Address _myAddress;
  104. const std::string _path;
  105. std::string _myAddressStr;
  106. std::unordered_map< uint64_t,std::shared_ptr<_Network> > _networks;
  107. std::unordered_multimap< uint64_t,uint64_t > _networkByMember;
  108. mutable std::mutex _networks_l;
  109. };
  110. } // namespace ZeroTier
  111. #endif