NodeConfig.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 ZeroTier Networks LLC
  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. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include <stdint.h>
  31. #include <memory>
  32. #include <string>
  33. #include <map>
  34. #include <set>
  35. #include "Constants.hpp"
  36. #include "NodeConfig.hpp"
  37. #include "RuntimeEnvironment.hpp"
  38. #include "Defaults.hpp"
  39. #include "Utils.hpp"
  40. #include "Logger.hpp"
  41. #include "Topology.hpp"
  42. #include "Packet.hpp"
  43. #include "InetAddress.hpp"
  44. #include "Peer.hpp"
  45. #include "Node.hpp"
  46. #include "SoftwareUpdater.hpp"
  47. namespace ZeroTier {
  48. NodeConfig::NodeConfig(const RuntimeEnvironment *renv) :
  49. RR(renv)
  50. {
  51. {
  52. Mutex::Lock _l(_localConfig_m);
  53. _readLocalConfig();
  54. }
  55. std::string networksFolder(RR->homePath + ZT_PATH_SEPARATOR_S + "networks.d");
  56. std::map<std::string,bool> networksDotD(Utils::listDirectory(networksFolder.c_str()));
  57. std::vector<uint64_t> configuredNets;
  58. for(std::map<std::string,bool>::iterator d(networksDotD.begin());d!=networksDotD.end();++d) {
  59. if (!d->second) {
  60. std::string::size_type dot = d->first.rfind(".conf");
  61. if (dot != std::string::npos) {
  62. uint64_t nwid = Utils::hexStrToU64(d->first.substr(0,dot).c_str());
  63. if ((nwid > 0)&&(std::find(configuredNets.begin(),configuredNets.end(),nwid) == configuredNets.end()))
  64. configuredNets.push_back(nwid);
  65. }
  66. }
  67. }
  68. for(std::vector<uint64_t>::iterator n(configuredNets.begin());n!=configuredNets.end();++n) {
  69. try {
  70. _networks[*n] = Network::newInstance(RR,this,*n);
  71. } catch (std::exception &exc) {
  72. LOG("unable to create network %.16llx: %s",(unsigned long long)*n,exc.what());
  73. } catch ( ... ) {
  74. LOG("unable to create network %.16llx: (unknown exception)",(unsigned long long)*n);
  75. }
  76. }
  77. }
  78. NodeConfig::~NodeConfig()
  79. {
  80. _writeLocalConfig();
  81. }
  82. void NodeConfig::putLocalConfig(const std::string &key,const char *value)
  83. {
  84. Mutex::Lock _l(_localConfig_m);
  85. _localConfig[key] = value;
  86. _writeLocalConfig();
  87. }
  88. void NodeConfig::putLocalConfig(const std::string &key,const std::string &value)
  89. {
  90. Mutex::Lock _l(_localConfig_m);
  91. _localConfig[key] = value;
  92. _writeLocalConfig();
  93. }
  94. std::string NodeConfig::getLocalConfig(const std::string &key) const
  95. {
  96. Mutex::Lock _l(_localConfig_m);
  97. Dictionary::const_iterator i(_localConfig.find(key));
  98. if (i == _localConfig.end())
  99. return std::string();
  100. return i->second;
  101. }
  102. void NodeConfig::clean()
  103. {
  104. Mutex::Lock _l(_networks_m);
  105. for(std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.begin());n!=_networks.end();++n)
  106. n->second->clean();
  107. }
  108. void NodeConfig::_readLocalConfig()
  109. {
  110. // assumes _localConfig_m is locked
  111. std::string localDotConf(RR->homePath + ZT_PATH_SEPARATOR_S + "local.conf");
  112. std::string buf;
  113. if (Utils::readFile(localDotConf.c_str(),buf))
  114. _localConfig.fromString(buf.c_str());
  115. }
  116. void NodeConfig::_writeLocalConfig()
  117. {
  118. // assumes _localConfig_m is locked
  119. Utils::writeFile(((RR->homePath + ZT_PATH_SEPARATOR_S + "local.conf")).c_str(),_localConfig.toString());
  120. }
  121. } // namespace ZeroTier