FileDB.cpp 8.0 KB

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