JSONDB.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 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 <stdio.h>
  19. #include <stdlib.h>
  20. #include <stdint.h>
  21. #ifndef _WIN32
  22. #include <unistd.h>
  23. #include <fcntl.h>
  24. #include <sys/time.h>
  25. #include <sys/types.h>
  26. #include <sys/socket.h>
  27. #include <sys/select.h>
  28. #endif
  29. #include "JSONDB.hpp"
  30. #define ZT_JSONDB_HTTP_TIMEOUT 60000
  31. namespace ZeroTier {
  32. static const nlohmann::json _EMPTY_JSON(nlohmann::json::object());
  33. static const std::map<std::string,std::string> _ZT_JSONDB_GET_HEADERS;
  34. JSONDB::JSONDB(const std::string &basePath) :
  35. _basePath(basePath),
  36. _rawInput(-1),
  37. _rawOutput(-1),
  38. _summaryThreadRun(true),
  39. _dataReady(false)
  40. {
  41. if ((_basePath.length() > 7)&&(_basePath.substr(0,7) == "http://")) {
  42. // If base path is http:// we run in HTTP mode
  43. // TODO: this doesn't yet support IPv6 since bracketed address notiation isn't supported.
  44. // Typically it's just used with 127.0.0.1 anyway.
  45. std::string hn = _basePath.substr(7);
  46. std::size_t hnend = hn.find_first_of('/');
  47. if (hnend != std::string::npos)
  48. hn = hn.substr(0,hnend);
  49. std::size_t hnsep = hn.find_last_of(':');
  50. if (hnsep != std::string::npos)
  51. hn[hnsep] = '/';
  52. _httpAddr.fromString(hn.c_str());
  53. if (hnend != std::string::npos)
  54. _basePath = _basePath.substr(7 + hnend);
  55. if (_basePath.length() == 0)
  56. _basePath = "/";
  57. if (_basePath[0] != '/')
  58. _basePath = std::string("/") + _basePath;
  59. #ifndef __WINDOWS__
  60. } else if (_basePath == "-") {
  61. // If base path is "-" we run in stdin/stdout mode and expect our database to be populated on startup via stdin
  62. // Not supported on Windows
  63. _rawInput = STDIN_FILENO;
  64. _rawOutput = STDOUT_FILENO;
  65. fcntl(_rawInput,F_SETFL,O_NONBLOCK);
  66. #endif
  67. } else {
  68. // Default mode of operation is to store files in the filesystem
  69. OSUtils::mkdir(_basePath.c_str());
  70. OSUtils::lockDownFile(_basePath.c_str(),true); // networks might contain auth tokens, etc., so restrict directory permissions
  71. }
  72. _networks_m.lock(); // locked until data is loaded, etc.
  73. if (_rawInput < 0) {
  74. unsigned int cnt = 0;
  75. while (!_load(_basePath)) {
  76. if ((++cnt & 7) == 0)
  77. fprintf(stderr,"WARNING: controller still waiting to read '%s'..." ZT_EOL_S,_basePath.c_str());
  78. Thread::sleep(250);
  79. }
  80. for(std::unordered_map<uint64_t,_NW>::iterator n(_networks.begin());n!=_networks.end();++n)
  81. _summaryThreadToDo.push_back(n->first);
  82. if (_summaryThreadToDo.size() > 0) {
  83. _summaryThread = Thread::start(this);
  84. } else {
  85. _dataReady = true;
  86. _networks_m.unlock();
  87. }
  88. } else {
  89. // In IPC mode we wait for the first message to start, and we start
  90. // this thread since this thread is responsible for reading from stdin.
  91. _summaryThread = Thread::start(this);
  92. }
  93. }
  94. JSONDB::~JSONDB()
  95. {
  96. Thread t;
  97. {
  98. Mutex::Lock _l(_summaryThread_m);
  99. _summaryThreadRun = false;
  100. t = _summaryThread;
  101. }
  102. if (t)
  103. Thread::join(t);
  104. }
  105. bool JSONDB::writeRaw(const std::string &n,const std::string &obj)
  106. {
  107. if (_rawOutput >= 0) {
  108. #ifndef __WINDOWS__
  109. if (obj.length() > 0) {
  110. Mutex::Lock _l(_rawLock);
  111. //fprintf(stderr,"%s\n",obj.c_str());
  112. if ((long)write(_rawOutput,obj.data(),obj.length()) == (long)obj.length()) {
  113. if (write(_rawOutput,"\n",1) == 1)
  114. return true;
  115. }
  116. } else return true;
  117. #endif
  118. return false;
  119. } else if (_httpAddr) {
  120. std::map<std::string,std::string> headers;
  121. std::string body;
  122. std::map<std::string,std::string> reqHeaders;
  123. char tmp[64];
  124. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%lu",(unsigned long)obj.length());
  125. reqHeaders["Content-Length"] = tmp;
  126. reqHeaders["Content-Type"] = "application/json";
  127. const unsigned int sc = Http::PUT(0,ZT_JSONDB_HTTP_TIMEOUT,reinterpret_cast<const struct sockaddr *>(&_httpAddr),(_basePath+"/"+n).c_str(),reqHeaders,obj.data(),(unsigned long)obj.length(),headers,body);
  128. return (sc == 200);
  129. } else {
  130. const std::string path(_genPath(n,true));
  131. if (!path.length())
  132. return false;
  133. return OSUtils::writeFile(path.c_str(),obj);
  134. }
  135. }
  136. bool JSONDB::hasNetwork(const uint64_t networkId) const
  137. {
  138. Mutex::Lock _l(_networks_m);
  139. return (_networks.find(networkId) != _networks.end());
  140. }
  141. bool JSONDB::getNetwork(const uint64_t networkId,nlohmann::json &config) const
  142. {
  143. Mutex::Lock _l(_networks_m);
  144. const std::unordered_map<uint64_t,_NW>::const_iterator i(_networks.find(networkId));
  145. if (i == _networks.end())
  146. return false;
  147. config = nlohmann::json::from_msgpack(i->second.config);
  148. return true;
  149. }
  150. bool JSONDB::getNetworkSummaryInfo(const uint64_t networkId,NetworkSummaryInfo &ns) const
  151. {
  152. Mutex::Lock _l(_networks_m);
  153. const std::unordered_map<uint64_t,_NW>::const_iterator i(_networks.find(networkId));
  154. if (i == _networks.end())
  155. return false;
  156. ns = i->second.summaryInfo;
  157. return true;
  158. }
  159. int JSONDB::getNetworkAndMember(const uint64_t networkId,const uint64_t nodeId,nlohmann::json &networkConfig,nlohmann::json &memberConfig,NetworkSummaryInfo &ns) const
  160. {
  161. Mutex::Lock _l(_networks_m);
  162. const std::unordered_map<uint64_t,_NW>::const_iterator i(_networks.find(networkId));
  163. if (i == _networks.end())
  164. return 0;
  165. const std::unordered_map< uint64_t,std::vector<uint8_t> >::const_iterator j(i->second.members.find(nodeId));
  166. if (j == i->second.members.end())
  167. return 1;
  168. networkConfig = nlohmann::json::from_msgpack(i->second.config);
  169. memberConfig = nlohmann::json::from_msgpack(j->second);
  170. ns = i->second.summaryInfo;
  171. return 3;
  172. }
  173. bool JSONDB::getNetworkMember(const uint64_t networkId,const uint64_t nodeId,nlohmann::json &memberConfig) const
  174. {
  175. Mutex::Lock _l(_networks_m);
  176. const std::unordered_map<uint64_t,_NW>::const_iterator i(_networks.find(networkId));
  177. if (i == _networks.end())
  178. return false;
  179. const std::unordered_map< uint64_t,std::vector<uint8_t> >::const_iterator j(i->second.members.find(nodeId));
  180. if (j == i->second.members.end())
  181. return false;
  182. memberConfig = nlohmann::json::from_msgpack(j->second);
  183. return true;
  184. }
  185. void JSONDB::saveNetwork(const uint64_t networkId,const nlohmann::json &networkConfig)
  186. {
  187. char n[64];
  188. OSUtils::ztsnprintf(n,sizeof(n),"network/%.16llx",(unsigned long long)networkId);
  189. writeRaw(n,OSUtils::jsonDump(networkConfig,-1));
  190. {
  191. Mutex::Lock _l(_networks_m);
  192. _networks[networkId].config = nlohmann::json::to_msgpack(networkConfig);
  193. }
  194. _recomputeSummaryInfo(networkId);
  195. }
  196. void JSONDB::saveNetworkMember(const uint64_t networkId,const uint64_t nodeId,const nlohmann::json &memberConfig)
  197. {
  198. char n[256];
  199. OSUtils::ztsnprintf(n,sizeof(n),"network/%.16llx/member/%.10llx",(unsigned long long)networkId,(unsigned long long)nodeId);
  200. writeRaw(n,OSUtils::jsonDump(memberConfig,-1));
  201. {
  202. Mutex::Lock _l(_networks_m);
  203. _networks[networkId].members[nodeId] = nlohmann::json::to_msgpack(memberConfig);
  204. _members[nodeId].insert(networkId);
  205. }
  206. _recomputeSummaryInfo(networkId);
  207. }
  208. nlohmann::json JSONDB::eraseNetwork(const uint64_t networkId)
  209. {
  210. if (!_httpAddr) { // Member deletion is done by Central in harnessed mode, and deleting the cache network entry also deletes all members
  211. std::vector<uint64_t> memberIds;
  212. {
  213. Mutex::Lock _l(_networks_m);
  214. const std::unordered_map<uint64_t,_NW>::iterator i(_networks.find(networkId));
  215. if (i == _networks.end())
  216. return _EMPTY_JSON;
  217. for(std::unordered_map< uint64_t,std::vector<uint8_t> >::iterator m(i->second.members.begin());m!=i->second.members.end();++m)
  218. memberIds.push_back(m->first);
  219. }
  220. for(std::vector<uint64_t>::iterator m(memberIds.begin());m!=memberIds.end();++m)
  221. eraseNetworkMember(networkId,*m,false);
  222. }
  223. char n[256];
  224. OSUtils::ztsnprintf(n,sizeof(n),"network/%.16llx",(unsigned long long)networkId);
  225. if (_rawOutput >= 0) {
  226. // In harnessed mode, deletes occur in Central or other management
  227. // software and do not need to be executed this way.
  228. } else if (_httpAddr) {
  229. std::map<std::string,std::string> headers;
  230. std::string body;
  231. Http::DEL(0,ZT_JSONDB_HTTP_TIMEOUT,reinterpret_cast<const struct sockaddr *>(&_httpAddr),(_basePath+"/"+n).c_str(),_ZT_JSONDB_GET_HEADERS,headers,body);
  232. } else {
  233. const std::string path(_genPath(n,false));
  234. if (path.length())
  235. OSUtils::rm(path.c_str());
  236. }
  237. {
  238. Mutex::Lock _l(_networks_m);
  239. std::unordered_map<uint64_t,_NW>::iterator i(_networks.find(networkId));
  240. if (i == _networks.end())
  241. return _EMPTY_JSON; // sanity check, shouldn't happen
  242. nlohmann::json tmp(nlohmann::json::from_msgpack(i->second.config));
  243. _networks.erase(i);
  244. return tmp;
  245. }
  246. }
  247. nlohmann::json JSONDB::eraseNetworkMember(const uint64_t networkId,const uint64_t nodeId,bool recomputeSummaryInfo)
  248. {
  249. char n[256];
  250. OSUtils::ztsnprintf(n,sizeof(n),"network/%.16llx/member/%.10llx",(unsigned long long)networkId,(unsigned long long)nodeId);
  251. if (_rawOutput >= 0) {
  252. // In harnessed mode, deletes occur in Central or other management
  253. // software and do not need to be executed this way.
  254. } else if (_httpAddr) {
  255. std::map<std::string,std::string> headers;
  256. std::string body;
  257. Http::DEL(0,ZT_JSONDB_HTTP_TIMEOUT,reinterpret_cast<const struct sockaddr *>(&_httpAddr),(_basePath+"/"+n).c_str(),_ZT_JSONDB_GET_HEADERS,headers,body);
  258. } else {
  259. const std::string path(_genPath(n,false));
  260. if (path.length())
  261. OSUtils::rm(path.c_str());
  262. }
  263. {
  264. Mutex::Lock _l(_networks_m);
  265. _members[nodeId].erase(networkId);
  266. std::unordered_map<uint64_t,_NW>::iterator i(_networks.find(networkId));
  267. if (i == _networks.end())
  268. return _EMPTY_JSON;
  269. std::unordered_map< uint64_t,std::vector<uint8_t> >::iterator j(i->second.members.find(nodeId));
  270. if (j == i->second.members.end())
  271. return _EMPTY_JSON;
  272. nlohmann::json tmp(j->second);
  273. i->second.members.erase(j);
  274. if (recomputeSummaryInfo)
  275. _recomputeSummaryInfo(networkId);
  276. return tmp;
  277. }
  278. }
  279. void JSONDB::threadMain()
  280. throw()
  281. {
  282. #ifndef __WINDOWS__
  283. fd_set readfds,nullfds;
  284. char *const readbuf = (_rawInput >= 0) ? (new char[1048576]) : (char *)0;
  285. std::string rawInputBuf;
  286. FD_ZERO(&readfds);
  287. FD_ZERO(&nullfds);
  288. struct timeval tv;
  289. #endif
  290. std::vector<uint64_t> todo;
  291. while (_summaryThreadRun) {
  292. #ifndef __WINDOWS__
  293. if (_rawInput < 0) {
  294. // In HTTP and filesystem mode we just wait for summary to-do items
  295. Thread::sleep(25);
  296. } else {
  297. // In IPC mode we wait but also select() on STDIN to read database updates
  298. FD_SET(_rawInput,&readfds);
  299. tv.tv_sec = 0;
  300. tv.tv_usec = 25000;
  301. select(_rawInput+1,&readfds,&nullfds,&nullfds,&tv);
  302. if (FD_ISSET(_rawInput,&readfds)) {
  303. const long rn = (long)read(_rawInput,readbuf,1048576);
  304. bool gotMessage = false;
  305. for(long i=0;i<rn;++i) {
  306. if ((readbuf[i] != '\n')&&(readbuf[i] != '\r')&&(readbuf[i] != 0)) { // compatible with nodeJS IPC
  307. rawInputBuf.push_back(readbuf[i]);
  308. } else if (rawInputBuf.length() > 0) {
  309. try {
  310. const nlohmann::json obj(OSUtils::jsonParse(rawInputBuf));
  311. gotMessage = true;
  312. if (!_dataReady) {
  313. _dataReady = true;
  314. _networks_m.unlock();
  315. }
  316. if (obj.is_array()) {
  317. for(unsigned long i=0;i<obj.size();++i)
  318. _add(obj[i]);
  319. } else if (obj.is_object()) {
  320. _add(obj);
  321. }
  322. } catch ( ... ) {} // ignore malformed JSON
  323. rawInputBuf.clear();
  324. }
  325. }
  326. if (!gotMessage) // select() again immediately until we get at least one full message
  327. continue;
  328. }
  329. }
  330. #else
  331. Thread::sleep(25);
  332. #endif
  333. {
  334. Mutex::Lock _l(_summaryThread_m);
  335. if (_summaryThreadToDo.empty())
  336. continue;
  337. else _summaryThreadToDo.swap(todo);
  338. }
  339. if (!_dataReady) {
  340. _dataReady = true;
  341. _networks_m.unlock();
  342. }
  343. const uint64_t now = OSUtils::now();
  344. try {
  345. Mutex::Lock _l(_networks_m);
  346. for(std::vector<uint64_t>::iterator ii(todo.begin());ii!=todo.end();++ii) {
  347. const uint64_t networkId = *ii;
  348. std::unordered_map<uint64_t,_NW>::iterator n(_networks.find(networkId));
  349. if (n != _networks.end()) {
  350. NetworkSummaryInfo &ns = n->second.summaryInfo;
  351. ns.activeBridges.clear();
  352. ns.allocatedIps.clear();
  353. ns.authorizedMemberCount = 0;
  354. ns.activeMemberCount = 0;
  355. ns.totalMemberCount = 0;
  356. ns.mostRecentDeauthTime = 0;
  357. for(std::unordered_map< uint64_t,std::vector<uint8_t> >::const_iterator m(n->second.members.begin());m!=n->second.members.end();++m) {
  358. try {
  359. nlohmann::json member(nlohmann::json::from_msgpack(m->second));
  360. if (OSUtils::jsonBool(member["authorized"],false)) {
  361. ++ns.authorizedMemberCount;
  362. try {
  363. const nlohmann::json &mlog = member["recentLog"];
  364. if ((mlog.is_array())&&(mlog.size() > 0)) {
  365. const nlohmann::json &mlog1 = mlog[0];
  366. if (mlog1.is_object()) {
  367. if ((now - OSUtils::jsonInt(mlog1["ts"],0ULL)) < (ZT_NETWORK_AUTOCONF_DELAY * 2))
  368. ++ns.activeMemberCount;
  369. }
  370. }
  371. } catch ( ... ) {}
  372. try {
  373. if (OSUtils::jsonBool(member["activeBridge"],false))
  374. ns.activeBridges.push_back(Address(m->first));
  375. } catch ( ... ) {}
  376. try {
  377. const nlohmann::json &mips = member["ipAssignments"];
  378. if (mips.is_array()) {
  379. for(unsigned long i=0;i<mips.size();++i) {
  380. InetAddress mip(OSUtils::jsonString(mips[i],"").c_str());
  381. if ((mip.ss_family == AF_INET)||(mip.ss_family == AF_INET6))
  382. ns.allocatedIps.push_back(mip);
  383. }
  384. }
  385. } catch ( ... ) {}
  386. } else {
  387. try {
  388. ns.mostRecentDeauthTime = std::max(ns.mostRecentDeauthTime,OSUtils::jsonInt(member["lastDeauthorizedTime"],0ULL));
  389. } catch ( ... ) {}
  390. }
  391. ++ns.totalMemberCount;
  392. } catch ( ... ) {}
  393. }
  394. std::sort(ns.activeBridges.begin(),ns.activeBridges.end());
  395. std::sort(ns.allocatedIps.begin(),ns.allocatedIps.end());
  396. n->second.summaryInfoLastComputed = now;
  397. }
  398. }
  399. } catch ( ... ) {}
  400. todo.clear();
  401. }
  402. if (!_dataReady) // sanity check
  403. _networks_m.unlock();
  404. #ifndef __WINDOWS__
  405. delete [] readbuf;
  406. #endif
  407. }
  408. bool JSONDB::_add(const nlohmann::json &j)
  409. {
  410. try {
  411. if (j.is_object()) {
  412. std::string id(OSUtils::jsonString(j["id"],"0"));
  413. std::string objtype(OSUtils::jsonString(j["objtype"],""));
  414. if ((id.length() == 16)&&(objtype == "network")) {
  415. const uint64_t nwid = Utils::hexStrToU64(id.c_str());
  416. if (nwid) {
  417. Mutex::Lock _l(_networks_m);
  418. _networks[nwid].config = nlohmann::json::to_msgpack(j);
  419. return true;
  420. }
  421. } else if ((id.length() == 10)&&(objtype == "member")) {
  422. const uint64_t mid = Utils::hexStrToU64(id.c_str());
  423. const uint64_t nwid = Utils::hexStrToU64(OSUtils::jsonString(j["nwid"],"0").c_str());
  424. if ((mid)&&(nwid)) {
  425. Mutex::Lock _l(_networks_m);
  426. _networks[nwid].members[mid] = nlohmann::json::to_msgpack(j);
  427. _members[mid].insert(nwid);
  428. return true;
  429. }
  430. }
  431. }
  432. } catch ( ... ) {}
  433. return false;
  434. }
  435. bool JSONDB::_load(const std::string &p)
  436. {
  437. // This is not used in stdin/stdout mode. Instead data is populated by
  438. // sending it all to stdin.
  439. if (_httpAddr) {
  440. // In HTTP harnessed mode we download our entire working data set on startup.
  441. std::string body;
  442. std::map<std::string,std::string> headers;
  443. const unsigned int sc = Http::GET(0,ZT_JSONDB_HTTP_TIMEOUT,reinterpret_cast<const struct sockaddr *>(&_httpAddr),_basePath.c_str(),_ZT_JSONDB_GET_HEADERS,headers,body);
  444. if (sc == 200) {
  445. try {
  446. nlohmann::json dbImg(OSUtils::jsonParse(body));
  447. std::string tmp;
  448. if (dbImg.is_object()) {
  449. Mutex::Lock _l(_networks_m);
  450. for(nlohmann::json::iterator i(dbImg.begin());i!=dbImg.end();++i) {
  451. try {
  452. _add(i.value());
  453. } catch ( ... ) {}
  454. }
  455. return true;
  456. }
  457. } catch ( ... ) {} // invalid JSON, so maybe incomplete request
  458. }
  459. return false;
  460. } else {
  461. // In regular mode we recursively read it from controller.d/ on disk
  462. std::vector<std::string> dl(OSUtils::listDirectory(p.c_str(),true));
  463. for(std::vector<std::string>::const_iterator di(dl.begin());di!=dl.end();++di) {
  464. if ((di->length() > 5)&&(di->substr(di->length() - 5) == ".json")) {
  465. std::string buf;
  466. if (OSUtils::readFile((p + ZT_PATH_SEPARATOR_S + *di).c_str(),buf)) {
  467. try {
  468. _add(OSUtils::jsonParse(buf));
  469. } catch ( ... ) {}
  470. }
  471. } else {
  472. this->_load((p + ZT_PATH_SEPARATOR_S + *di));
  473. }
  474. }
  475. return true;
  476. }
  477. }
  478. void JSONDB::_recomputeSummaryInfo(const uint64_t networkId)
  479. {
  480. Mutex::Lock _l(_summaryThread_m);
  481. if (std::find(_summaryThreadToDo.begin(),_summaryThreadToDo.end(),networkId) == _summaryThreadToDo.end())
  482. _summaryThreadToDo.push_back(networkId);
  483. if (!_summaryThread)
  484. _summaryThread = Thread::start(this);
  485. }
  486. std::string JSONDB::_genPath(const std::string &n,bool create)
  487. {
  488. std::vector<std::string> pt(OSUtils::split(n.c_str(),"/","",""));
  489. if (pt.size() == 0)
  490. return std::string();
  491. char sep;
  492. if (_httpAddr) {
  493. sep = '/';
  494. create = false;
  495. } else {
  496. sep = ZT_PATH_SEPARATOR;
  497. }
  498. std::string p(_basePath);
  499. if (create) OSUtils::mkdir(p.c_str());
  500. for(unsigned long i=0,j=(unsigned long)(pt.size()-1);i<j;++i) {
  501. p.push_back(sep);
  502. p.append(pt[i]);
  503. if (create) OSUtils::mkdir(p.c_str());
  504. }
  505. p.push_back(sep);
  506. p.append(pt[pt.size()-1]);
  507. p.append(".json");
  508. return p;
  509. }
  510. } // namespace ZeroTier