RethinkDB.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #ifdef ZT_CONTROLLER_USE_RETHINKDB
  19. #ifndef ZT_CONTROLLER_RETHINKDB_HPP
  20. #define ZT_CONTROLLER_RETHINKDB_HPP
  21. #include "../node/Constants.hpp"
  22. #include "../node/Address.hpp"
  23. #include "../node/InetAddress.hpp"
  24. #include "../osdep/OSUtils.hpp"
  25. #include "../osdep/BlockingQueue.hpp"
  26. #include <memory>
  27. #include <string>
  28. #include <thread>
  29. #include <unordered_map>
  30. #include <unordered_set>
  31. #include <vector>
  32. #include "../ext/json/json.hpp"
  33. #define ZT_CONTROLLER_RETHINKDB_COMMIT_THREADS 2
  34. namespace ZeroTier
  35. {
  36. class EmbeddedNetworkController;
  37. class RethinkDB
  38. {
  39. public:
  40. struct NetworkSummaryInfo
  41. {
  42. NetworkSummaryInfo() : authorizedMemberCount(0),totalMemberCount(0),mostRecentDeauthTime(0) {}
  43. std::vector<Address> activeBridges;
  44. std::vector<InetAddress> allocatedIps;
  45. unsigned long authorizedMemberCount;
  46. unsigned long totalMemberCount;
  47. int64_t mostRecentDeauthTime;
  48. };
  49. RethinkDB(EmbeddedNetworkController *const nc,const Address &myAddress,const char *path);
  50. ~RethinkDB();
  51. inline void waitForReady() const
  52. {
  53. while (_ready > 0) {
  54. if (!_waitNoticePrinted) {
  55. _waitNoticePrinted = true;
  56. fprintf(stderr,"NOTICE: controller RethinkDB waiting for initial data download..." ZT_EOL_S);
  57. }
  58. _readyLock.lock();
  59. _readyLock.unlock();
  60. }
  61. }
  62. inline bool hasNetwork(const uint64_t networkId) const
  63. {
  64. std::lock_guard<std::mutex> l(_networks_l);
  65. return (_networks.find(networkId) != _networks.end());
  66. }
  67. bool get(const uint64_t networkId,nlohmann::json &network);
  68. bool get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member);
  69. bool get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member,NetworkSummaryInfo &info);
  70. bool get(const uint64_t networkId,nlohmann::json &network,std::vector<nlohmann::json> &members);
  71. bool summary(const uint64_t networkId,NetworkSummaryInfo &info);
  72. void networks(std::vector<uint64_t> &networks);
  73. void save(const nlohmann::json &record);
  74. void eraseNetwork(const uint64_t networkId);
  75. void eraseMember(const uint64_t networkId,const uint64_t memberId);
  76. private:
  77. struct _Network
  78. {
  79. _Network() : mostRecentDeauthTime(0) {}
  80. nlohmann::json config;
  81. std::unordered_map<uint64_t,nlohmann::json> members;
  82. std::unordered_set<uint64_t> activeBridgeMembers;
  83. std::unordered_set<uint64_t> authorizedMembers;
  84. std::unordered_set<InetAddress,InetAddress::Hasher> allocatedIps;
  85. int64_t mostRecentDeauthTime;
  86. std::mutex lock;
  87. };
  88. void _memberChanged(nlohmann::json &old,nlohmann::json &member);
  89. void _networkChanged(nlohmann::json &old,nlohmann::json &network);
  90. inline void _fillSummaryInfo(const std::shared_ptr<_Network> &nw,NetworkSummaryInfo &info)
  91. {
  92. for(auto ab=nw->activeBridgeMembers.begin();ab!=nw->activeBridgeMembers.end();++ab)
  93. info.activeBridges.push_back(Address(*ab));
  94. for(auto ip=nw->allocatedIps.begin();ip!=nw->allocatedIps.end();++ip)
  95. info.allocatedIps.push_back(*ip);
  96. info.authorizedMemberCount = (unsigned long)nw->authorizedMembers.size();
  97. info.totalMemberCount = (unsigned long)nw->members.size();
  98. info.mostRecentDeauthTime = nw->mostRecentDeauthTime;
  99. }
  100. EmbeddedNetworkController *const _controller;
  101. const Address _myAddress;
  102. std::string _myAddressStr;
  103. std::string _host;
  104. std::string _db;
  105. std::string _auth;
  106. int _port;
  107. void *_networksDbWatcherConnection;
  108. void *_membersDbWatcherConnection;
  109. std::thread _networksDbWatcher;
  110. std::thread _membersDbWatcher;
  111. std::unordered_map< uint64_t,std::shared_ptr<_Network> > _networks;
  112. std::unordered_multimap< uint64_t,uint64_t > _networkByMember;
  113. mutable std::mutex _networks_l;
  114. BlockingQueue< std::unique_ptr<nlohmann::json> > _commitQueue;
  115. std::thread _commitThread[ZT_CONTROLLER_RETHINKDB_COMMIT_THREADS];
  116. mutable std::mutex _readyLock; // locked until ready
  117. std::atomic<int> _ready;
  118. std::atomic<int> _run;
  119. mutable volatile bool _waitNoticePrinted;
  120. };
  121. } // namespace ZeroTier
  122. #endif
  123. #endif // ZT_CONTROLLER_USE_RETHINKDB