FileDB.cpp 5.6 KB

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