FileDB.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
  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. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #include "FileDB.hpp"
  27. namespace ZeroTier
  28. {
  29. FileDB::FileDB(EmbeddedNetworkController *const nc,const Identity &myId,const char *path) :
  30. DB(nc,myId,path),
  31. _networksPath(_path + ZT_PATH_SEPARATOR_S + "network"),
  32. _tracePath(_path + ZT_PATH_SEPARATOR_S + "trace")
  33. {
  34. OSUtils::mkdir(_path.c_str());
  35. OSUtils::lockDownFile(_path.c_str(),true);
  36. OSUtils::mkdir(_networksPath.c_str());
  37. OSUtils::mkdir(_tracePath.c_str());
  38. std::vector<std::string> networks(OSUtils::listDirectory(_networksPath.c_str(),false));
  39. std::string buf;
  40. for(auto n=networks.begin();n!=networks.end();++n) {
  41. buf.clear();
  42. if ((n->length() == 21)&&(OSUtils::readFile((_networksPath + ZT_PATH_SEPARATOR_S + *n).c_str(),buf))) {
  43. try {
  44. nlohmann::json network(OSUtils::jsonParse(buf));
  45. const std::string nwids = network["id"];
  46. if (nwids.length() == 16) {
  47. nlohmann::json nullJson;
  48. _networkChanged(nullJson,network,false);
  49. std::string membersPath(_networksPath + ZT_PATH_SEPARATOR_S + nwids + ZT_PATH_SEPARATOR_S "member");
  50. std::vector<std::string> members(OSUtils::listDirectory(membersPath.c_str(),false));
  51. for(auto m=members.begin();m!=members.end();++m) {
  52. buf.clear();
  53. if ((m->length() == 15)&&(OSUtils::readFile((membersPath + ZT_PATH_SEPARATOR_S + *m).c_str(),buf))) {
  54. try {
  55. nlohmann::json member(OSUtils::jsonParse(buf));
  56. const std::string addrs = member["id"];
  57. if (addrs.length() == 10) {
  58. nlohmann::json nullJson2;
  59. _memberChanged(nullJson2,member,false);
  60. }
  61. } catch ( ... ) {}
  62. }
  63. }
  64. }
  65. } catch ( ... ) {}
  66. }
  67. }
  68. }
  69. FileDB::~FileDB() {}
  70. bool FileDB::waitForReady() { return true; }
  71. bool FileDB::isReady() { return true; }
  72. void FileDB::save(nlohmann::json *orig,nlohmann::json &record)
  73. {
  74. char p1[4096],p2[4096],pb[4096];
  75. try {
  76. if (orig) {
  77. if (*orig != record) {
  78. record["revision"] = OSUtils::jsonInt(record["revision"],0ULL) + 1;
  79. }
  80. } else {
  81. record["revision"] = 1;
  82. }
  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())||(old != record)) {
  90. OSUtils::ztsnprintf(p1,sizeof(p1),"%s" ZT_PATH_SEPARATOR_S "%.16llx.json.new",_networksPath.c_str(),nwid);
  91. OSUtils::ztsnprintf(p2,sizeof(p2),"%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. OSUtils::rename(p1,p2);
  95. _networkChanged(old,record,true);
  96. }
  97. }
  98. } else if (objtype == "member") {
  99. const uint64_t id = OSUtils::jsonIntHex(record["id"],0ULL);
  100. const uint64_t nwid = OSUtils::jsonIntHex(record["nwid"],0ULL);
  101. if ((id)&&(nwid)) {
  102. nlohmann::json network,old;
  103. get(nwid,network,id,old);
  104. if ((!old.is_object())||(old != record)) {
  105. OSUtils::ztsnprintf(pb,sizeof(pb),"%s" ZT_PATH_SEPARATOR_S "%.16llx" ZT_PATH_SEPARATOR_S "member",_networksPath.c_str(),(unsigned long long)nwid);
  106. OSUtils::ztsnprintf(p1,sizeof(p1),"%s" ZT_PATH_SEPARATOR_S "%.10llx.json.new",pb,(unsigned long long)id);
  107. if (!OSUtils::writeFile(p1,OSUtils::jsonDump(record,-1))) {
  108. OSUtils::ztsnprintf(p2,sizeof(p2),"%s" ZT_PATH_SEPARATOR_S "%.16llx",_networksPath.c_str(),(unsigned long long)nwid);
  109. OSUtils::mkdir(p2);
  110. OSUtils::mkdir(pb);
  111. if (!OSUtils::writeFile(p1,OSUtils::jsonDump(record,-1)))
  112. fprintf(stderr,"WARNING: controller unable to write to path: %s" ZT_EOL_S,p1);
  113. }
  114. OSUtils::ztsnprintf(p2,sizeof(p2),"%s" ZT_PATH_SEPARATOR_S "%.10llx.json",pb,(unsigned long long)id);
  115. OSUtils::rename(p1,p2);
  116. _memberChanged(old,record,true);
  117. }
  118. }
  119. } else if (objtype == "trace") {
  120. const std::string id = record["id"];
  121. if (id.length() > 0) {
  122. OSUtils::ztsnprintf(p1,sizeof(p1),"%s" ZT_PATH_SEPARATOR_S "%s.json",_tracePath.c_str(),id.c_str());
  123. OSUtils::writeFile(p1,OSUtils::jsonDump(record,-1));
  124. }
  125. }
  126. } catch ( ... ) {} // drop invalid records missing fields
  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. if (OSUtils::fileExists(p,false)){
  135. OSUtils::rm(p);
  136. }
  137. _networkChanged(network,nullJson,true);
  138. }
  139. void FileDB::eraseMember(const uint64_t networkId,const uint64_t memberId)
  140. {
  141. nlohmann::json network,member,nullJson;
  142. get(networkId,network);
  143. get(memberId,member);
  144. char p[16384];
  145. 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);
  146. if (OSUtils::fileExists(p,false)){
  147. OSUtils::rm(p);
  148. }
  149. _memberChanged(member,nullJson,true);
  150. }
  151. void FileDB::nodeIsOnline(const uint64_t networkId,const uint64_t memberId,const InetAddress &physicalAddress)
  152. {
  153. // Nothing to do here right now in the filesystem store mode since we can just get this from the peer list
  154. }
  155. } // namespace ZeroTier