DB.cpp 8.0 KB

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