JSONDB.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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_JSONDB_HPP
  19. #define ZT_JSONDB_HPP
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <string>
  24. #include <map>
  25. #include <stdexcept>
  26. #include <vector>
  27. #include <algorithm>
  28. #include <unordered_map>
  29. #include "../node/Constants.hpp"
  30. #include "../node/Utils.hpp"
  31. #include "../node/InetAddress.hpp"
  32. #include "../node/Mutex.hpp"
  33. #include "../ext/json/json.hpp"
  34. #include "../osdep/OSUtils.hpp"
  35. #include "../osdep/Http.hpp"
  36. #include "../osdep/Thread.hpp"
  37. namespace ZeroTier {
  38. /**
  39. * Hierarchical JSON store that persists into the filesystem or via HTTP
  40. */
  41. class JSONDB
  42. {
  43. public:
  44. struct NetworkSummaryInfo
  45. {
  46. NetworkSummaryInfo() : authorizedMemberCount(0),activeMemberCount(0),totalMemberCount(0),mostRecentDeauthTime(0) {}
  47. std::vector<Address> activeBridges;
  48. std::vector<InetAddress> allocatedIps;
  49. unsigned long authorizedMemberCount;
  50. unsigned long activeMemberCount;
  51. unsigned long totalMemberCount;
  52. uint64_t mostRecentDeauthTime;
  53. };
  54. JSONDB(const std::string &basePath);
  55. ~JSONDB();
  56. bool writeRaw(const std::string &n,const std::string &obj);
  57. inline bool hasNetwork(const uint64_t networkId) const
  58. {
  59. Mutex::Lock _l(_networks_m);
  60. std::unordered_map<uint64_t,_NW>::const_iterator i(_networks.find(networkId));
  61. return (i != _networks.end());
  62. }
  63. inline bool getNetwork(const uint64_t networkId,nlohmann::json &config) const
  64. {
  65. Mutex::Lock _l(_networks_m);
  66. std::unordered_map<uint64_t,_NW>::const_iterator i(_networks.find(networkId));
  67. if (i == _networks.end())
  68. return false;
  69. config = i->second.config;
  70. return true;
  71. }
  72. inline bool getNetworkSummaryInfo(const uint64_t networkId,NetworkSummaryInfo &ns) const
  73. {
  74. for(;;) {
  75. {
  76. Mutex::Lock _l(_networks_m);
  77. std::unordered_map<uint64_t,_NW>::const_iterator i(_networks.find(networkId));
  78. if (i == _networks.end())
  79. return false;
  80. if (i->second.summaryInfoLastComputed) {
  81. ns = i->second.summaryInfo;
  82. return true;
  83. }
  84. }
  85. Thread::sleep(100); // wait for this to be done the first time, which happens when we start
  86. }
  87. }
  88. /**
  89. * @return Bit mask: 0 == none, 1 == network only, 3 == network and member
  90. */
  91. inline int getNetworkAndMember(const uint64_t networkId,const uint64_t nodeId,nlohmann::json &networkConfig,nlohmann::json &memberConfig,NetworkSummaryInfo &ns) const
  92. {
  93. Mutex::Lock _l(_networks_m);
  94. std::unordered_map<uint64_t,_NW>::const_iterator i(_networks.find(networkId));
  95. if (i == _networks.end())
  96. return 0;
  97. networkConfig = i->second.config;
  98. ns = i->second.summaryInfo;
  99. std::unordered_map<uint64_t,nlohmann::json>::const_iterator j(i->second.members.find(nodeId));
  100. if (j == i->second.members.end())
  101. return 1;
  102. memberConfig = j->second;
  103. return 3;
  104. }
  105. inline bool getNetworkMember(const uint64_t networkId,const uint64_t nodeId,nlohmann::json &memberConfig) const
  106. {
  107. Mutex::Lock _l(_networks_m);
  108. std::unordered_map<uint64_t,_NW>::const_iterator i(_networks.find(networkId));
  109. if (i == _networks.end())
  110. return false;
  111. std::unordered_map<uint64_t,nlohmann::json>::const_iterator j(i->second.members.find(nodeId));
  112. if (j == i->second.members.end())
  113. return false;
  114. memberConfig = j->second;
  115. return true;
  116. }
  117. void saveNetwork(const uint64_t networkId,const nlohmann::json &networkConfig);
  118. void saveNetworkMember(const uint64_t networkId,const uint64_t nodeId,const nlohmann::json &memberConfig);
  119. nlohmann::json eraseNetwork(const uint64_t networkId);
  120. nlohmann::json eraseNetworkMember(const uint64_t networkId,const uint64_t nodeId,bool recomputeSummaryInfo = true);
  121. std::vector<uint64_t> networkIds() const
  122. {
  123. std::vector<uint64_t> r;
  124. Mutex::Lock _l(_networks_m);
  125. for(std::unordered_map<uint64_t,_NW>::const_iterator n(_networks.begin());n!=_networks.end();++n)
  126. r.push_back(n->first);
  127. return r;
  128. }
  129. template<typename F>
  130. inline void eachMember(const uint64_t networkId,F func)
  131. {
  132. Mutex::Lock _l(_networks_m);
  133. std::unordered_map<uint64_t,_NW>::const_iterator i(_networks.find(networkId));
  134. if (i != _networks.end()) {
  135. for(std::unordered_map<uint64_t,nlohmann::json>::const_iterator m(i->second.members.begin());m!=i->second.members.end();++m) {
  136. try {
  137. func(networkId,m->first,m->second);
  138. } catch ( ... ) {}
  139. }
  140. }
  141. }
  142. void threadMain()
  143. throw();
  144. private:
  145. bool _load(const std::string &p);
  146. void _recomputeSummaryInfo(const uint64_t networkId);
  147. std::string _genPath(const std::string &n,bool create);
  148. std::string _basePath;
  149. InetAddress _httpAddr;
  150. Thread _summaryThread;
  151. std::vector<uint64_t> _summaryThreadToDo;
  152. volatile bool _summaryThreadRun;
  153. Mutex _summaryThread_m;
  154. struct _NW
  155. {
  156. _NW() : summaryInfoLastComputed(0) {}
  157. nlohmann::json config;
  158. NetworkSummaryInfo summaryInfo;
  159. uint64_t summaryInfoLastComputed;
  160. std::unordered_map<uint64_t,nlohmann::json> members;
  161. };
  162. std::unordered_map<uint64_t,_NW> _networks;
  163. Mutex _networks_m;
  164. };
  165. } // namespace ZeroTier
  166. #endif