DB.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. #include "DB.hpp"
  19. #include "EmbeddedNetworkController.hpp"
  20. #include <chrono>
  21. #include <algorithm>
  22. #include <stdexcept>
  23. using json = nlohmann::json;
  24. namespace ZeroTier {
  25. DB::DB(EmbeddedNetworkController *const nc,const Address &myAddress,const char *path) :
  26. _controller(nc),
  27. _myAddress(myAddress),
  28. _path((path) ? path : "")
  29. {
  30. {
  31. char tmp[32];
  32. _myAddress.toString(tmp);
  33. _myAddressStr = tmp;
  34. }
  35. }
  36. DB::~DB()
  37. {
  38. }
  39. bool DB::get(const uint64_t networkId,nlohmann::json &network)
  40. {
  41. waitForReady();
  42. std::shared_ptr<_Network> nw;
  43. {
  44. std::lock_guard<std::mutex> l(_networks_l);
  45. auto nwi = _networks.find(networkId);
  46. if (nwi == _networks.end())
  47. return false;
  48. nw = nwi->second;
  49. }
  50. {
  51. std::lock_guard<std::mutex> l2(nw->lock);
  52. network = nw->config;
  53. }
  54. return true;
  55. }
  56. bool DB::get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member)
  57. {
  58. waitForReady();
  59. std::shared_ptr<_Network> nw;
  60. {
  61. std::lock_guard<std::mutex> l(_networks_l);
  62. auto nwi = _networks.find(networkId);
  63. if (nwi == _networks.end())
  64. return false;
  65. nw = nwi->second;
  66. }
  67. {
  68. std::lock_guard<std::mutex> l2(nw->lock);
  69. network = nw->config;
  70. auto m = nw->members.find(memberId);
  71. if (m == nw->members.end())
  72. return false;
  73. member = m->second;
  74. }
  75. return true;
  76. }
  77. bool DB::get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member,NetworkSummaryInfo &info)
  78. {
  79. waitForReady();
  80. std::shared_ptr<_Network> nw;
  81. {
  82. std::lock_guard<std::mutex> l(_networks_l);
  83. auto nwi = _networks.find(networkId);
  84. if (nwi == _networks.end())
  85. return false;
  86. nw = nwi->second;
  87. }
  88. {
  89. std::lock_guard<std::mutex> l2(nw->lock);
  90. network = nw->config;
  91. _fillSummaryInfo(nw,info);
  92. auto m = nw->members.find(memberId);
  93. if (m == nw->members.end())
  94. return false;
  95. member = m->second;
  96. }
  97. return true;
  98. }
  99. bool DB::get(const uint64_t networkId,nlohmann::json &network,std::vector<nlohmann::json> &members)
  100. {
  101. waitForReady();
  102. std::shared_ptr<_Network> nw;
  103. {
  104. std::lock_guard<std::mutex> l(_networks_l);
  105. auto nwi = _networks.find(networkId);
  106. if (nwi == _networks.end())
  107. return false;
  108. nw = nwi->second;
  109. }
  110. {
  111. std::lock_guard<std::mutex> l2(nw->lock);
  112. network = nw->config;
  113. for(auto m=nw->members.begin();m!=nw->members.end();++m)
  114. members.push_back(m->second);
  115. }
  116. return true;
  117. }
  118. bool DB::summary(const uint64_t networkId,NetworkSummaryInfo &info)
  119. {
  120. waitForReady();
  121. std::shared_ptr<_Network> nw;
  122. {
  123. std::lock_guard<std::mutex> l(_networks_l);
  124. auto nwi = _networks.find(networkId);
  125. if (nwi == _networks.end())
  126. return false;
  127. nw = nwi->second;
  128. }
  129. {
  130. std::lock_guard<std::mutex> l2(nw->lock);
  131. _fillSummaryInfo(nw,info);
  132. }
  133. return true;
  134. }
  135. void DB::networks(std::vector<uint64_t> &networks)
  136. {
  137. waitForReady();
  138. std::lock_guard<std::mutex> l(_networks_l);
  139. networks.reserve(_networks.size() + 1);
  140. for(auto n=_networks.begin();n!=_networks.end();++n)
  141. networks.push_back(n->first);
  142. }
  143. void DB::_memberChanged(nlohmann::json &old,nlohmann::json &member,bool push)
  144. {
  145. uint64_t memberId = 0;
  146. uint64_t networkId = 0;
  147. bool isAuth = false;
  148. bool wasAuth = false;
  149. std::shared_ptr<_Network> nw;
  150. if (old.is_object()) {
  151. json &config = old["config"];
  152. if (config.is_object()) {
  153. memberId = OSUtils::jsonIntHex(config["id"],0ULL);
  154. networkId = OSUtils::jsonIntHex(config["nwid"],0ULL);
  155. if ((memberId)&&(networkId)) {
  156. {
  157. std::lock_guard<std::mutex> l(_networks_l);
  158. auto nw2 = _networks.find(networkId);
  159. if (nw2 != _networks.end())
  160. nw = nw2->second;
  161. }
  162. if (nw) {
  163. std::lock_guard<std::mutex> l(nw->lock);
  164. if (OSUtils::jsonBool(config["activeBridge"],false))
  165. nw->activeBridgeMembers.erase(memberId);
  166. wasAuth = OSUtils::jsonBool(config["authorized"],false);
  167. if (wasAuth)
  168. nw->authorizedMembers.erase(memberId);
  169. json &ips = config["ipAssignments"];
  170. if (ips.is_array()) {
  171. for(unsigned long i=0;i<ips.size();++i) {
  172. json &ipj = ips[i];
  173. if (ipj.is_string()) {
  174. const std::string ips = ipj;
  175. InetAddress ipa(ips.c_str());
  176. ipa.setPort(0);
  177. nw->allocatedIps.erase(ipa);
  178. }
  179. }
  180. }
  181. }
  182. }
  183. }
  184. }
  185. if (member.is_object()) {
  186. json &config = member["config"];
  187. if (config.is_object()) {
  188. if (!nw) {
  189. memberId = OSUtils::jsonIntHex(config["id"],0ULL);
  190. networkId = OSUtils::jsonIntHex(config["nwid"],0ULL);
  191. if ((!memberId)||(!networkId))
  192. return;
  193. std::lock_guard<std::mutex> l(_networks_l);
  194. std::shared_ptr<_Network> &nw2 = _networks[networkId];
  195. if (!nw2)
  196. nw2.reset(new _Network);
  197. nw = nw2;
  198. }
  199. {
  200. std::lock_guard<std::mutex> l(nw->lock);
  201. nw->members[memberId] = config;
  202. if (OSUtils::jsonBool(config["activeBridge"],false))
  203. nw->activeBridgeMembers.insert(memberId);
  204. isAuth = OSUtils::jsonBool(config["authorized"],false);
  205. if (isAuth)
  206. nw->authorizedMembers.insert(memberId);
  207. json &ips = config["ipAssignments"];
  208. if (ips.is_array()) {
  209. for(unsigned long i=0;i<ips.size();++i) {
  210. json &ipj = ips[i];
  211. if (ipj.is_string()) {
  212. const std::string ips = ipj;
  213. InetAddress ipa(ips.c_str());
  214. ipa.setPort(0);
  215. nw->allocatedIps.insert(ipa);
  216. }
  217. }
  218. }
  219. if (!isAuth) {
  220. const int64_t ldt = (int64_t)OSUtils::jsonInt(config["lastDeauthorizedTime"],0ULL);
  221. if (ldt > nw->mostRecentDeauthTime)
  222. nw->mostRecentDeauthTime = ldt;
  223. }
  224. }
  225. if (push)
  226. _controller->onNetworkMemberUpdate(networkId,memberId);
  227. }
  228. } else if (memberId) {
  229. if (nw) {
  230. std::lock_guard<std::mutex> l(nw->lock);
  231. nw->members.erase(memberId);
  232. }
  233. if (networkId) {
  234. std::lock_guard<std::mutex> l(_networks_l);
  235. auto er = _networkByMember.equal_range(memberId);
  236. for(auto i=er.first;i!=er.second;++i) {
  237. if (i->second == networkId) {
  238. _networkByMember.erase(i);
  239. break;
  240. }
  241. }
  242. }
  243. }
  244. if ((push)&&((wasAuth)&&(!isAuth)&&(networkId)&&(memberId)))
  245. _controller->onNetworkMemberDeauthorize(networkId,memberId);
  246. }
  247. void DB::_networkChanged(nlohmann::json &old,nlohmann::json &network,bool push)
  248. {
  249. if (network.is_object()) {
  250. json &config = network["config"];
  251. if (config.is_object()) {
  252. const std::string ids = config["id"];
  253. const uint64_t id = Utils::hexStrToU64(ids.c_str());
  254. if (id) {
  255. std::shared_ptr<_Network> nw;
  256. {
  257. std::lock_guard<std::mutex> l(_networks_l);
  258. std::shared_ptr<_Network> &nw2 = _networks[id];
  259. if (!nw2)
  260. nw2.reset(new _Network);
  261. nw = nw2;
  262. }
  263. {
  264. std::lock_guard<std::mutex> l2(nw->lock);
  265. nw->config = config;
  266. }
  267. if (push)
  268. _controller->onNetworkUpdate(id);
  269. }
  270. }
  271. } else if (old.is_object()) {
  272. const std::string ids = old["id"];
  273. const uint64_t id = Utils::hexStrToU64(ids.c_str());
  274. if (id) {
  275. std::lock_guard<std::mutex> l(_networks_l);
  276. _networks.erase(id);
  277. }
  278. }
  279. }
  280. void DB::_fillSummaryInfo(const std::shared_ptr<_Network> &nw,NetworkSummaryInfo &info)
  281. {
  282. for(auto ab=nw->activeBridgeMembers.begin();ab!=nw->activeBridgeMembers.end();++ab)
  283. info.activeBridges.push_back(Address(*ab));
  284. for(auto ip=nw->allocatedIps.begin();ip!=nw->allocatedIps.end();++ip)
  285. info.allocatedIps.push_back(*ip);
  286. info.authorizedMemberCount = (unsigned long)nw->authorizedMembers.size();
  287. info.totalMemberCount = (unsigned long)nw->members.size();
  288. info.mostRecentDeauthTime = nw->mostRecentDeauthTime;
  289. }
  290. } // namespace ZeroTier