FileDB.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2018 ZeroTier, Inc.
  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. #include "FileDB.hpp"
  19. namespace ZeroTier
  20. {
  21. FileDB::FileDB(EmbeddedNetworkController *const nc,const Identity &myId,const char *path) :
  22. DB(nc,myId,path),
  23. _networksPath(_path + ZT_PATH_SEPARATOR_S + "network"),
  24. _tracePath(_path + ZT_PATH_SEPARATOR_S + "trace")
  25. {
  26. OSUtils::mkdir(_path.c_str());
  27. OSUtils::lockDownFile(_path.c_str(),true);
  28. OSUtils::mkdir(_networksPath.c_str());
  29. OSUtils::mkdir(_tracePath.c_str());
  30. std::vector<std::string> networks(OSUtils::listDirectory(_networksPath.c_str(),false));
  31. std::string buf;
  32. for(auto n=networks.begin();n!=networks.end();++n) {
  33. buf.clear();
  34. if ((n->length() == 21)&&(OSUtils::readFile((_networksPath + ZT_PATH_SEPARATOR_S + *n).c_str(),buf))) {
  35. try {
  36. nlohmann::json network(OSUtils::jsonParse(buf));
  37. const std::string nwids = network["id"];
  38. if (nwids.length() == 16) {
  39. nlohmann::json nullJson;
  40. _networkChanged(nullJson,network,false);
  41. std::string membersPath(_networksPath + ZT_PATH_SEPARATOR_S + nwids + ZT_PATH_SEPARATOR_S "member");
  42. std::vector<std::string> members(OSUtils::listDirectory(membersPath.c_str(),false));
  43. for(auto m=members.begin();m!=members.end();++m) {
  44. buf.clear();
  45. if ((m->length() == 15)&&(OSUtils::readFile((membersPath + ZT_PATH_SEPARATOR_S + *m).c_str(),buf))) {
  46. try {
  47. nlohmann::json member(OSUtils::jsonParse(buf));
  48. const std::string addrs = member["id"];
  49. if (addrs.length() == 10) {
  50. nlohmann::json nullJson2;
  51. _memberChanged(nullJson2,member,false);
  52. }
  53. } catch ( ... ) {}
  54. }
  55. }
  56. }
  57. } catch ( ... ) {}
  58. }
  59. }
  60. }
  61. FileDB::~FileDB()
  62. {
  63. }
  64. bool FileDB::waitForReady()
  65. {
  66. return true;
  67. }
  68. void FileDB::save(nlohmann::json *orig,nlohmann::json &record)
  69. {
  70. char p1[4096],p2[4096],pb[4096];
  71. try {
  72. if (orig) {
  73. if (*orig != record) {
  74. record["revision"] = OSUtils::jsonInt(record["revision"],0ULL) + 1;
  75. }
  76. } else {
  77. record["revision"] = 1;
  78. }
  79. const std::string objtype = record["objtype"];
  80. if (objtype == "network") {
  81. const uint64_t nwid = OSUtils::jsonIntHex(record["id"],0ULL);
  82. if (nwid) {
  83. nlohmann::json old;
  84. get(nwid,old);
  85. OSUtils::ztsnprintf(p1,sizeof(p1),"%s" ZT_PATH_SEPARATOR_S "%.16llx.json.new",_networksPath.c_str(),nwid);
  86. OSUtils::ztsnprintf(p2,sizeof(p2),"%s" ZT_PATH_SEPARATOR_S "%.16llx.json",_networksPath.c_str(),nwid);
  87. if (!OSUtils::writeFile(p1,OSUtils::jsonDump(record,-1)))
  88. fprintf(stderr,"WARNING: controller unable to write to path: %s" ZT_EOL_S,p1);
  89. OSUtils::rename(p1,p2);
  90. _networkChanged(old,record,true);
  91. }
  92. } else if (objtype == "member") {
  93. const uint64_t id = OSUtils::jsonIntHex(record["id"],0ULL);
  94. const uint64_t nwid = OSUtils::jsonIntHex(record["nwid"],0ULL);
  95. if ((id)&&(nwid)) {
  96. nlohmann::json network,old;
  97. get(nwid,network,id,old);
  98. OSUtils::ztsnprintf(pb,sizeof(pb),"%s" ZT_PATH_SEPARATOR_S "%.16llx" ZT_PATH_SEPARATOR_S "member",_networksPath.c_str(),(unsigned long long)nwid);
  99. OSUtils::ztsnprintf(p1,sizeof(p1),"%s" ZT_PATH_SEPARATOR_S "%.10llx.json.new",pb,(unsigned long long)id);
  100. OSUtils::ztsnprintf(p2,sizeof(p2),"%s" ZT_PATH_SEPARATOR_S "%.10llx.json",pb,(unsigned long long)id);
  101. if (!OSUtils::writeFile(p1,OSUtils::jsonDump(record,-1))) {
  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. OSUtils::rename(p1,p2);
  107. _memberChanged(old,record,true);
  108. }
  109. } else if (objtype == "trace") {
  110. const std::string id = record["id"];
  111. if (id.length() > 0) {
  112. OSUtils::ztsnprintf(p1,sizeof(p1),"%s" ZT_PATH_SEPARATOR_S "%s.json",_tracePath.c_str(),id.c_str());
  113. OSUtils::writeFile(p1,OSUtils::jsonDump(record,-1));
  114. }
  115. }
  116. } catch ( ... ) {} // drop invalid records missing fields
  117. }
  118. void FileDB::eraseNetwork(const uint64_t networkId)
  119. {
  120. nlohmann::json network,nullJson;
  121. get(networkId,network);
  122. char p[16384];
  123. OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "%.16llx.json",_networksPath.c_str(),networkId);
  124. if (OSUtils::fileExists(p,false)){
  125. OSUtils::rm(p);
  126. }
  127. _networkChanged(network,nullJson,true);
  128. }
  129. void FileDB::eraseMember(const uint64_t networkId,const uint64_t memberId)
  130. {
  131. nlohmann::json network,member,nullJson;
  132. get(networkId,network);
  133. get(memberId,member);
  134. char p[16384];
  135. 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);
  136. if (OSUtils::fileExists(p,false)){
  137. OSUtils::rm(p);
  138. }
  139. _memberChanged(member,nullJson,true);
  140. }
  141. void FileDB::nodeIsOnline(const uint64_t networkId,const uint64_t memberId,const InetAddress &physicalAddress)
  142. {
  143. // Nothing to do here right now in the filesystem store mode since we can just get this from the peer list
  144. }
  145. } // namespace ZeroTier