NodeConfig.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #ifndef ZT_NODECONFIG_HPP
  28. #define ZT_NODECONFIG_HPP
  29. #include <stdint.h>
  30. #include <map>
  31. #include <set>
  32. #include <string>
  33. #include <vector>
  34. #include <stdexcept>
  35. #include "IpcListener.hpp"
  36. #include "IpcConnection.hpp"
  37. #include "SharedPtr.hpp"
  38. #include "Network.hpp"
  39. #include "Utils.hpp"
  40. #include "Buffer.hpp"
  41. #include "Dictionary.hpp"
  42. namespace ZeroTier {
  43. class RuntimeEnvironment;
  44. /**
  45. * Node configuration endpoint
  46. */
  47. class NodeConfig
  48. {
  49. public:
  50. /**
  51. * @param renv Runtime environment
  52. * @param authToken Configuration authentication token
  53. * @throws std::runtime_error Unable to initialize or listen for IPC connections
  54. */
  55. NodeConfig(const RuntimeEnvironment *renv,const char *authToken);
  56. ~NodeConfig();
  57. /**
  58. * Store something in local configuration cache
  59. *
  60. * By convention, keys starting with _ will not be shown in the command bus
  61. * local config functions.
  62. *
  63. * @param key Configuration key
  64. * @param value Configuration value
  65. */
  66. void putLocalConfig(const std::string &key,const char *value);
  67. void putLocalConfig(const std::string &key,const std::string &value);
  68. /**
  69. * @param key Configuration key
  70. * @return Value or empty string if not found
  71. */
  72. std::string getLocalConfig(const std::string &key) const;
  73. /**
  74. * @param nwid Network ID
  75. * @return Network or NULL if no network for that ID
  76. */
  77. inline SharedPtr<Network> network(uint64_t nwid) const
  78. {
  79. Mutex::Lock _l(_networks_m);
  80. std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.find(nwid));
  81. return ((n == _networks.end()) ? SharedPtr<Network>() : n->second);
  82. }
  83. /**
  84. * @return Vector containing all networks
  85. */
  86. inline std::vector< SharedPtr<Network> > networks() const
  87. {
  88. std::vector< SharedPtr<Network> > nwlist;
  89. Mutex::Lock _l(_networks_m);
  90. for(std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.begin());n!=_networks.end();++n)
  91. nwlist.push_back(n->second);
  92. return nwlist;
  93. }
  94. /**
  95. * Perform cleanup and possibly persist saved state
  96. */
  97. void clean();
  98. /**
  99. * @param nwid Network ID
  100. * @return True if this network exists
  101. */
  102. inline bool hasNetwork(uint64_t nwid)
  103. {
  104. Mutex::Lock _l(_networks_m);
  105. return (_networks.count(nwid) > 0);
  106. }
  107. /**
  108. * @return Set of network tap device names from our virtual networks (not other taps on system)
  109. */
  110. inline std::set<std::string> networkTapDeviceNames() const
  111. {
  112. std::set<std::string> tapDevs;
  113. Mutex::Lock _l(_networks_m);
  114. for(std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.begin());n!=_networks.end();++n) {
  115. std::string dn(n->second->tapDeviceName());
  116. if (dn.length())
  117. tapDevs.insert(dn);
  118. }
  119. return tapDevs;
  120. }
  121. private:
  122. static void _CBcommandHandler(void *arg,IpcConnection *ipcc,IpcConnection::EventType event,const char *commandLine);
  123. void _doCommand(IpcConnection *ipcc,const char *commandLine);
  124. void _readLocalConfig();
  125. void _writeLocalConfig();
  126. const RuntimeEnvironment *_r;
  127. IpcListener _ipcListener;
  128. std::string _authToken;
  129. std::map< IpcConnection *,bool > _connections;
  130. Mutex _connections_m;
  131. Dictionary _localConfig; // persisted as local.conf
  132. Mutex _localConfig_m;
  133. std::map< uint64_t,SharedPtr<Network> > _networks; // persisted in networks.d/
  134. Mutex _networks_m;
  135. };
  136. } // namespace ZeroTier
  137. #endif