FileDB.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
  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. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #include "FileDB.hpp"
  27. namespace ZeroTier
  28. {
  29. FileDB::FileDB(EmbeddedNetworkController *const nc,const Identity &myId,const char *path) :
  30. DB(nc,myId,path),
  31. _networksPath(_path + ZT_PATH_SEPARATOR_S + "network"),
  32. _tracePath(_path + ZT_PATH_SEPARATOR_S + "trace"),
  33. _onlineChanged(false),
  34. _running(true)
  35. {
  36. OSUtils::mkdir(_path.c_str());
  37. OSUtils::lockDownFile(_path.c_str(),true);
  38. OSUtils::mkdir(_networksPath.c_str());
  39. OSUtils::mkdir(_tracePath.c_str());
  40. std::vector<std::string> networks(OSUtils::listDirectory(_networksPath.c_str(),false));
  41. std::string buf;
  42. for(auto n=networks.begin();n!=networks.end();++n) {
  43. buf.clear();
  44. if ((n->length() == 21)&&(OSUtils::readFile((_networksPath + ZT_PATH_SEPARATOR_S + *n).c_str(),buf))) {
  45. try {
  46. nlohmann::json network(OSUtils::jsonParse(buf));
  47. const std::string nwids = network["id"];
  48. if (nwids.length() == 16) {
  49. nlohmann::json nullJson;
  50. _networkChanged(nullJson,network,false);
  51. std::string membersPath(_networksPath + ZT_PATH_SEPARATOR_S + nwids + ZT_PATH_SEPARATOR_S "member");
  52. std::vector<std::string> members(OSUtils::listDirectory(membersPath.c_str(),false));
  53. for(auto m=members.begin();m!=members.end();++m) {
  54. buf.clear();
  55. if ((m->length() == 15)&&(OSUtils::readFile((membersPath + ZT_PATH_SEPARATOR_S + *m).c_str(),buf))) {
  56. try {
  57. nlohmann::json member(OSUtils::jsonParse(buf));
  58. const std::string addrs = member["id"];
  59. if (addrs.length() == 10) {
  60. nlohmann::json nullJson2;
  61. _memberChanged(nullJson2,member,false);
  62. }
  63. } catch ( ... ) {}
  64. }
  65. }
  66. }
  67. } catch ( ... ) {}
  68. }
  69. }
  70. _onlineUpdateThread = std::thread([this]() {
  71. unsigned int cnt = 0;
  72. while (this->_running) {
  73. usleep(250);
  74. if ((++cnt % 20) == 0) { // 5 seconds
  75. std::lock_guard<std::mutex> l(this->_online_l);
  76. if (!this->_running) return;
  77. if (this->_onlineChanged) {
  78. char p[4096],atmp[64];
  79. for(auto nw=this->_online.begin();nw!=this->_online.end();++nw) {
  80. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "%.16llx-online.json",_networksPath.c_str(),(unsigned long long)nw->first);
  81. FILE *f = fopen(p,"wb");
  82. if (f) {
  83. fprintf(f,"{");
  84. const char *memberPrefix = "";
  85. for(auto m=nw->second.begin();m!=nw->second.end();++m) {
  86. fprintf(f,"%s\"%.10llx\":{" ZT_EOL_S,memberPrefix,(unsigned long long)m->first);
  87. memberPrefix = ",";
  88. InetAddress lastAddr;
  89. const char *timestampPrefix = " ";
  90. int cnt = 0;
  91. for(auto ts=m->second.rbegin();ts!=m->second.rend();) {
  92. if (cnt < 25) {
  93. if (lastAddr != ts->second) {
  94. lastAddr = ts->second;
  95. fprintf(f,"%s\"%lld\":\"%s\"" ZT_EOL_S,timestampPrefix,(long long)ts->first,ts->second.toString(atmp));
  96. timestampPrefix = ",";
  97. ++cnt;
  98. ++ts;
  99. } else {
  100. ts = std::map<int64_t,InetAddress>::reverse_iterator(m->second.erase(std::next(ts).base()));
  101. }
  102. } else {
  103. ts = std::map<int64_t,InetAddress>::reverse_iterator(m->second.erase(std::next(ts).base()));
  104. }
  105. }
  106. fprintf(f,"}");
  107. }
  108. fprintf(f,"}" ZT_EOL_S);
  109. fclose(f);
  110. }
  111. }
  112. this->_onlineChanged = false;
  113. }
  114. }
  115. }
  116. });
  117. }
  118. FileDB::~FileDB()
  119. {
  120. try {
  121. _online_l.lock();
  122. _running = false;
  123. _online_l.unlock();
  124. _onlineUpdateThread.join();
  125. } catch ( ... ) {}
  126. }
  127. bool FileDB::waitForReady() { return true; }
  128. bool FileDB::isReady() { return true; }
  129. void FileDB::save(nlohmann::json *orig,nlohmann::json &record)
  130. {
  131. char p1[4096],p2[4096],pb[4096];
  132. try {
  133. if (orig) {
  134. if (*orig != record) {
  135. record["revision"] = OSUtils::jsonInt(record["revision"],0ULL) + 1;
  136. }
  137. } else {
  138. record["revision"] = 1;
  139. }
  140. const std::string objtype = record["objtype"];
  141. if (objtype == "network") {
  142. const uint64_t nwid = OSUtils::jsonIntHex(record["id"],0ULL);
  143. if (nwid) {
  144. nlohmann::json old;
  145. get(nwid,old);
  146. if ((!old.is_object())||(old != record)) {
  147. OSUtils::ztsnprintf(p1,sizeof(p1),"%s" ZT_PATH_SEPARATOR_S "%.16llx.json",_networksPath.c_str(),nwid);
  148. if (!OSUtils::writeFile(p1,OSUtils::jsonDump(record,-1)))
  149. fprintf(stderr,"WARNING: controller unable to write to path: %s" ZT_EOL_S,p1);
  150. _networkChanged(old,record,true);
  151. }
  152. }
  153. } else if (objtype == "member") {
  154. const uint64_t id = OSUtils::jsonIntHex(record["id"],0ULL);
  155. const uint64_t nwid = OSUtils::jsonIntHex(record["nwid"],0ULL);
  156. if ((id)&&(nwid)) {
  157. nlohmann::json network,old;
  158. get(nwid,network,id,old);
  159. if ((!old.is_object())||(old != record)) {
  160. OSUtils::ztsnprintf(pb,sizeof(pb),"%s" ZT_PATH_SEPARATOR_S "%.16llx" ZT_PATH_SEPARATOR_S "member",_networksPath.c_str(),(unsigned long long)nwid);
  161. OSUtils::ztsnprintf(p1,sizeof(p1),"%s" ZT_PATH_SEPARATOR_S "%.10llx.json",pb,(unsigned long long)id);
  162. if (!OSUtils::writeFile(p1,OSUtils::jsonDump(record,-1))) {
  163. OSUtils::ztsnprintf(p2,sizeof(p2),"%s" ZT_PATH_SEPARATOR_S "%.16llx",_networksPath.c_str(),(unsigned long long)nwid);
  164. OSUtils::mkdir(p2);
  165. OSUtils::mkdir(pb);
  166. if (!OSUtils::writeFile(p1,OSUtils::jsonDump(record,-1)))
  167. fprintf(stderr,"WARNING: controller unable to write to path: %s" ZT_EOL_S,p1);
  168. }
  169. _memberChanged(old,record,true);
  170. }
  171. }
  172. } else if (objtype == "trace") {
  173. const std::string id = record["id"];
  174. if (id.length() > 0) {
  175. OSUtils::ztsnprintf(p1,sizeof(p1),"%s" ZT_PATH_SEPARATOR_S "%s.json",_tracePath.c_str(),id.c_str());
  176. OSUtils::writeFile(p1,OSUtils::jsonDump(record,-1));
  177. }
  178. }
  179. } catch ( ... ) {} // drop invalid records missing fields
  180. }
  181. void FileDB::eraseNetwork(const uint64_t networkId)
  182. {
  183. nlohmann::json network,nullJson;
  184. get(networkId,network);
  185. char p[16384];
  186. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "%.16llx.json",_networksPath.c_str(),networkId);
  187. OSUtils::rm(p);
  188. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "%.16llx-online.json",_networksPath.c_str(),networkId);
  189. OSUtils::rm(p);
  190. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "%.16llx" ZT_PATH_SEPARATOR_S "member",_networksPath.c_str(),(unsigned long long)networkId);
  191. OSUtils::rmDashRf(p);
  192. _networkChanged(network,nullJson,true);
  193. std::lock_guard<std::mutex> l(this->_online_l);
  194. this->_online.erase(networkId);
  195. this->_onlineChanged = true;
  196. }
  197. void FileDB::eraseMember(const uint64_t networkId,const uint64_t memberId)
  198. {
  199. nlohmann::json network,member,nullJson;
  200. get(networkId,network);
  201. get(memberId,member);
  202. char p[4096];
  203. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "%.16llx" ZT_PATH_SEPARATOR_S "member" ZT_PATH_SEPARATOR_S "%.10llx.json",_networksPath.c_str(),networkId,memberId);
  204. OSUtils::rm(p);
  205. _memberChanged(member,nullJson,true);
  206. std::lock_guard<std::mutex> l(this->_online_l);
  207. this->_online[networkId].erase(memberId);
  208. this->_onlineChanged = true;
  209. }
  210. void FileDB::nodeIsOnline(const uint64_t networkId,const uint64_t memberId,const InetAddress &physicalAddress)
  211. {
  212. char mid[32],atmp[64];
  213. OSUtils::ztsnprintf(mid,sizeof(mid),"%.10llx",(unsigned long long)memberId);
  214. physicalAddress.toString(atmp);
  215. std::lock_guard<std::mutex> l(this->_online_l);
  216. this->_online[networkId][memberId][OSUtils::now()] = physicalAddress;
  217. this->_onlineChanged = true;
  218. }
  219. } // namespace ZeroTier