2
0

FileDB.cpp 5.5 KB

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