DB.cpp 8.0 KB

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