DB.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 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/Address.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. #define ZT_CONTROLLER_RETHINKDB_COMMIT_THREADS 2
  34. namespace ZeroTier
  35. {
  36. class EmbeddedNetworkController;
  37. /**
  38. * Base class with common infrastructure for all controller DB implementations
  39. */
  40. class DB
  41. {
  42. public:
  43. struct NetworkSummaryInfo
  44. {
  45. NetworkSummaryInfo() : authorizedMemberCount(0),totalMemberCount(0),mostRecentDeauthTime(0) {}
  46. std::vector<Address> activeBridges;
  47. std::vector<InetAddress> allocatedIps;
  48. unsigned long authorizedMemberCount;
  49. unsigned long totalMemberCount;
  50. int64_t mostRecentDeauthTime;
  51. };
  52. DB(EmbeddedNetworkController *const nc,const Address &myAddress,const char *path);
  53. virtual ~DB();
  54. virtual bool waitForReady() = 0;
  55. inline bool hasNetwork(const uint64_t networkId) const
  56. {
  57. std::lock_guard<std::mutex> l(_networks_l);
  58. return (_networks.find(networkId) != _networks.end());
  59. }
  60. bool get(const uint64_t networkId,nlohmann::json &network);
  61. bool get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member);
  62. bool get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member,NetworkSummaryInfo &info);
  63. bool get(const uint64_t networkId,nlohmann::json &network,std::vector<nlohmann::json> &members);
  64. bool summary(const uint64_t networkId,NetworkSummaryInfo &info);
  65. void networks(std::vector<uint64_t> &networks);
  66. virtual void save(const nlohmann::json &record) = 0;
  67. virtual void eraseNetwork(const uint64_t networkId) = 0;
  68. virtual void eraseMember(const uint64_t networkId,const uint64_t memberId) = 0;
  69. protected:
  70. struct _Network
  71. {
  72. _Network() : mostRecentDeauthTime(0) {}
  73. nlohmann::json config;
  74. std::unordered_map<uint64_t,nlohmann::json> members;
  75. std::unordered_set<uint64_t> activeBridgeMembers;
  76. std::unordered_set<uint64_t> authorizedMembers;
  77. std::unordered_set<InetAddress,InetAddress::Hasher> allocatedIps;
  78. int64_t mostRecentDeauthTime;
  79. std::mutex lock;
  80. };
  81. void _memberChanged(nlohmann::json &old,nlohmann::json &member,bool push);
  82. void _networkChanged(nlohmann::json &old,nlohmann::json &network,bool push);
  83. void _fillSummaryInfo(const std::shared_ptr<_Network> &nw,NetworkSummaryInfo &info);
  84. EmbeddedNetworkController *const _controller;
  85. const Address _myAddress;
  86. const std::string _path;
  87. std::string _myAddressStr;
  88. std::unordered_map< uint64_t,std::shared_ptr<_Network> > _networks;
  89. std::unordered_multimap< uint64_t,uint64_t > _networkByMember;
  90. mutable std::mutex _networks_l;
  91. };
  92. } // namespace ZeroTier
  93. #endif