FileDB.cpp 5.7 KB

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