FileDB.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2025-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #include "FileDB.hpp"
  14. namespace ZeroTier
  15. {
  16. FileDB::FileDB(const char *path) :
  17. DB(),
  18. _path(path),
  19. _networksPath((_path + ZT_PATH_SEPARATOR_S + "network").c_str()),
  20. _running(true)
  21. {
  22. OSUtils::mkdir(_path.c_str());
  23. OSUtils::lockDownFile(_path.c_str(),true);
  24. OSUtils::mkdir(_networksPath.c_str());
  25. Vector<String> networks(OSUtils::listDirectory(_networksPath.c_str(),false));
  26. String buf;
  27. for(auto n=networks.begin();n!=networks.end();++n) {
  28. buf.clear();
  29. if ((n->length() == 21)&&(OSUtils::readFile((_networksPath + ZT_PATH_SEPARATOR_S + *n).c_str(),buf))) {
  30. try {
  31. nlohmann::json network(DB::jsonParse(std::string(buf.c_str())));
  32. const std::string nwids = network["id"];
  33. if (nwids.length() == 16) {
  34. nlohmann::json nullJson;
  35. _networkChanged(nullJson,network,false);
  36. String membersPath((_networksPath + ZT_PATH_SEPARATOR_S + nwids.c_str() + ZT_PATH_SEPARATOR_S "member").c_str());
  37. Vector<String> members(OSUtils::listDirectory(membersPath.c_str(),false));
  38. for(auto m=members.begin();m!=members.end();++m) {
  39. buf.clear();
  40. if ((m->length() == 15)&&(OSUtils::readFile((membersPath + ZT_PATH_SEPARATOR_S + *m).c_str(),buf))) {
  41. try {
  42. nlohmann::json member(DB::jsonParse(std::string(buf.c_str())));
  43. const std::string addrs = member["id"];
  44. if (addrs.length() == 10) {
  45. nlohmann::json nullJson2;
  46. _memberChanged(nullJson2,member,false);
  47. }
  48. } catch ( ... ) {}
  49. }
  50. }
  51. }
  52. } catch ( ... ) {}
  53. }
  54. }
  55. }
  56. FileDB::~FileDB()
  57. {
  58. try {
  59. _online_l.lock();
  60. _running = false;
  61. _online_l.unlock();
  62. _onlineUpdateThread.join();
  63. } catch ( ... ) {}
  64. }
  65. bool FileDB::waitForReady() { return true; }
  66. bool FileDB::isReady() { return true; }
  67. bool FileDB::save(nlohmann::json &record,bool notifyListeners)
  68. {
  69. char p1[4096],p2[4096],pb[4096];
  70. bool modified = false;
  71. try {
  72. const std::string objtype = record["objtype"];
  73. if (objtype == "network") {
  74. const uint64_t nwid = DB::jsonIntHex(record["id"],0ULL);
  75. if (nwid) {
  76. nlohmann::json old;
  77. get(nwid,old);
  78. if ((!old.is_object())||(!_compareRecords(old,record))) {
  79. record["revision"] = DB::jsonInt(record["revision"],0ULL) + 1ULL;
  80. OSUtils::ztsnprintf(p1,sizeof(p1),"%s" ZT_PATH_SEPARATOR_S "%.16llx.json",_networksPath.c_str(),nwid);
  81. if (!OSUtils::writeFile(p1,DB::jsonDump(record,-1)))
  82. fprintf(stderr,"WARNING: controller unable to write to path: %s" ZT_EOL_S,p1);
  83. _networkChanged(old,record,notifyListeners);
  84. modified = true;
  85. }
  86. }
  87. } else if (objtype == "member") {
  88. const uint64_t id = DB::jsonIntHex(record["id"],0ULL);
  89. const uint64_t nwid = DB::jsonIntHex(record["nwid"],0ULL);
  90. if ((id)&&(nwid)) {
  91. nlohmann::json network,old;
  92. get(nwid,network,id,old);
  93. if ((!old.is_object())||(!_compareRecords(old,record))) {
  94. record["revision"] = DB::jsonInt(record["revision"],0ULL) + 1ULL;
  95. OSUtils::ztsnprintf(pb,sizeof(pb),"%s" ZT_PATH_SEPARATOR_S "%.16llx" ZT_PATH_SEPARATOR_S "member",_networksPath.c_str(),(unsigned long long)nwid);
  96. OSUtils::ztsnprintf(p1,sizeof(p1),"%s" ZT_PATH_SEPARATOR_S "%.10llx.json",pb,(unsigned long long)id);
  97. if (!OSUtils::writeFile(p1,DB::jsonDump(record,-1))) {
  98. OSUtils::ztsnprintf(p2,sizeof(p2),"%s" ZT_PATH_SEPARATOR_S "%.16llx",_networksPath.c_str(),(unsigned long long)nwid);
  99. OSUtils::mkdir(p2);
  100. OSUtils::mkdir(pb);
  101. if (!OSUtils::writeFile(p1,DB::jsonDump(record,-1)))
  102. fprintf(stderr,"WARNING: controller unable to write to path: %s" ZT_EOL_S,p1);
  103. }
  104. _memberChanged(old,record,notifyListeners);
  105. modified = true;
  106. }
  107. }
  108. }
  109. } catch ( ... ) {} // drop invalid records missing fields
  110. return modified;
  111. }
  112. void FileDB::eraseNetwork(const uint64_t networkId)
  113. {
  114. nlohmann::json network,nullJson;
  115. get(networkId,network);
  116. char p[16384];
  117. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "%.16llx.json",_networksPath.c_str(),networkId);
  118. OSUtils::rm(p);
  119. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "%.16llx" ZT_PATH_SEPARATOR_S "member",_networksPath.c_str(),(unsigned long long)networkId);
  120. OSUtils::rmDashRf(p);
  121. _networkChanged(network,nullJson,true);
  122. std::lock_guard<std::mutex> l(this->_online_l);
  123. this->_online.erase(networkId);
  124. }
  125. void FileDB::eraseMember(const uint64_t networkId,const uint64_t memberId)
  126. {
  127. nlohmann::json network,member,nullJson;
  128. get(networkId,network);
  129. get(memberId,member);
  130. char p[4096];
  131. 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);
  132. OSUtils::rm(p);
  133. _memberChanged(member,nullJson,true);
  134. std::lock_guard<std::mutex> l(this->_online_l);
  135. this->_online[networkId].erase(memberId);
  136. }
  137. void FileDB::nodeIsOnline(const uint64_t networkId,const uint64_t memberId,const InetAddress &physicalAddress)
  138. {
  139. char mid[32],atmp[64];
  140. OSUtils::ztsnprintf(mid,sizeof(mid),"%.10llx",(unsigned long long)memberId);
  141. physicalAddress.toString(atmp);
  142. std::lock_guard<std::mutex> l(this->_online_l);
  143. this->_online[networkId][memberId][OSUtils::now()] = physicalAddress;
  144. }
  145. } // namespace ZeroTier