JSONDB.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 <unordered_set>
  30. #include "../node/Constants.hpp"
  31. #include "../node/Utils.hpp"
  32. #include "../node/InetAddress.hpp"
  33. #include "../node/Mutex.hpp"
  34. #include "../ext/json/json.hpp"
  35. #include "../osdep/OSUtils.hpp"
  36. #include "../osdep/Thread.hpp"
  37. namespace ZeroTier {
  38. class EmbeddedNetworkController;
  39. /**
  40. * Hierarchical JSON store that persists into the filesystem or via HTTP
  41. */
  42. class JSONDB
  43. {
  44. public:
  45. struct NetworkSummaryInfo
  46. {
  47. NetworkSummaryInfo() : authorizedMemberCount(0),activeMemberCount(0),totalMemberCount(0),mostRecentDeauthTime(0) {}
  48. std::vector<Address> activeBridges;
  49. std::vector<InetAddress> allocatedIps;
  50. unsigned long authorizedMemberCount;
  51. unsigned long activeMemberCount;
  52. unsigned long totalMemberCount;
  53. int64_t mostRecentDeauthTime;
  54. };
  55. JSONDB(const std::string &basePath,EmbeddedNetworkController *parent);
  56. ~JSONDB();
  57. /**
  58. * Write a JSON object to the data store
  59. *
  60. * It's important that obj contain a valid JSON object with no newlines (jsonDump with -1
  61. * for indentation), since newline-delimited JSON is what nodeJS's IPC speaks and this
  62. * is important in Central-harnessed mode.
  63. *
  64. * @param n Path name of object
  65. * @param obj Object in single-line no-CRs JSON object format (OSUtils::jsonDump(obj,-1))
  66. * @return True if write appears successful
  67. */
  68. bool writeRaw(const std::string &n,const std::string &obj);
  69. bool hasNetwork(const uint64_t networkId) const;
  70. bool getNetwork(const uint64_t networkId,nlohmann::json &config) const;
  71. bool getNetworkSummaryInfo(const uint64_t networkId,NetworkSummaryInfo &ns) const;
  72. /**
  73. * @return Bit mask: 0 == none, 1 == network only, 3 == network and member
  74. */
  75. int getNetworkAndMember(const uint64_t networkId,const uint64_t nodeId,nlohmann::json &networkConfig,nlohmann::json &memberConfig,NetworkSummaryInfo &ns) const;
  76. bool getNetworkMember(const uint64_t networkId,const uint64_t nodeId,nlohmann::json &memberConfig) const;
  77. void saveNetwork(const uint64_t networkId,const nlohmann::json &networkConfig);
  78. void saveNetworkMember(const uint64_t networkId,const uint64_t nodeId,const nlohmann::json &memberConfig);
  79. nlohmann::json eraseNetwork(const uint64_t networkId);
  80. nlohmann::json eraseNetworkMember(const uint64_t networkId,const uint64_t nodeId,bool recomputeSummaryInfo = true);
  81. std::vector<uint64_t> networkIds() const
  82. {
  83. std::vector<uint64_t> r;
  84. Mutex::Lock _l(_networks_m);
  85. for(std::unordered_map<uint64_t,_NW>::const_iterator n(_networks.begin());n!=_networks.end();++n)
  86. r.push_back(n->first);
  87. return r;
  88. }
  89. inline unsigned long memberCount(const uint64_t networkId)
  90. {
  91. Mutex::Lock _l(_networks_m);
  92. std::unordered_map<uint64_t,_NW>::const_iterator i(_networks.find(networkId));
  93. if (i != _networks.end())
  94. return (unsigned long)i->second.members.size();
  95. return 0;
  96. }
  97. template<typename F>
  98. inline void eachMember(const uint64_t networkId,F func)
  99. {
  100. Mutex::Lock _l(_networks_m);
  101. std::unordered_map<uint64_t,_NW>::const_iterator i(_networks.find(networkId));
  102. if (i != _networks.end()) {
  103. for(std::unordered_map< uint64_t,std::vector<uint8_t> >::const_iterator m(i->second.members.begin());m!=i->second.members.end();++m) {
  104. try {
  105. func(networkId,m->first,nlohmann::json::from_msgpack(m->second));
  106. } catch ( ... ) {}
  107. }
  108. }
  109. }
  110. template<typename F>
  111. inline void eachId(F func)
  112. {
  113. Mutex::Lock _l(_networks_m);
  114. for(std::unordered_map<uint64_t,_NW>::const_iterator i(_networks.begin());i!=_networks.end();++i) {
  115. for(std::unordered_map< uint64_t,std::vector<uint8_t> >::const_iterator m(i->second.members.begin());m!=i->second.members.end();++m) {
  116. try {
  117. func(i->first,m->first);
  118. } catch ( ... ) {}
  119. }
  120. }
  121. }
  122. inline std::vector<uint64_t> networksForMember(const uint64_t nodeId)
  123. {
  124. Mutex::Lock _l(_networks_m);
  125. std::unordered_map< uint64_t,std::unordered_set< uint64_t > >::const_iterator m(_members.find(nodeId));
  126. if (m != _members.end()) {
  127. return std::vector<uint64_t>(m->second.begin(),m->second.end());
  128. } else {
  129. return std::vector<uint64_t>();
  130. }
  131. }
  132. void threadMain()
  133. throw();
  134. private:
  135. bool _addOrUpdate(const nlohmann::json &j);
  136. bool _load(const std::string &p);
  137. void _recomputeSummaryInfo(const uint64_t networkId);
  138. std::string _genPath(const std::string &n,bool create);
  139. EmbeddedNetworkController *const _parent;
  140. std::string _basePath;
  141. int _rawInput,_rawOutput;
  142. Mutex _rawLock;
  143. Thread _summaryThread;
  144. std::vector<uint64_t> _summaryThreadToDo;
  145. volatile bool _summaryThreadRun;
  146. Mutex _summaryThread_m;
  147. struct _NW
  148. {
  149. _NW() : summaryInfoLastComputed(0) {}
  150. std::vector<uint8_t> config;
  151. NetworkSummaryInfo summaryInfo;
  152. uint64_t summaryInfoLastComputed;
  153. std::unordered_map< uint64_t,std::vector<uint8_t> > members;
  154. };
  155. std::unordered_map< uint64_t,_NW > _networks;
  156. std::unordered_map< uint64_t,std::unordered_set< uint64_t > > _members;
  157. bool _dataReady;
  158. Mutex _networks_m;
  159. };
  160. } // namespace ZeroTier
  161. #endif