NodeConfig.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 "SharedPtr.hpp"
  36. #include "Network.hpp"
  37. #include "Utils.hpp"
  38. #include "Buffer.hpp"
  39. #include "Dictionary.hpp"
  40. namespace ZeroTier {
  41. class RuntimeEnvironment;
  42. /**
  43. * Node configuration endpoint
  44. */
  45. class NodeConfig
  46. {
  47. public:
  48. /**
  49. * @param renv Runtime environment
  50. * @param authToken Configuration authentication token
  51. * @throws std::runtime_error Unable to initialize or listen for IPC connections
  52. */
  53. NodeConfig(const RuntimeEnvironment *renv,const char *authToken);
  54. ~NodeConfig();
  55. /**
  56. * Store something in local configuration cache
  57. *
  58. * By convention, keys starting with _ will not be shown in the command bus
  59. * local config functions.
  60. *
  61. * @param key Configuration key
  62. * @param value Configuration value
  63. */
  64. void putLocalConfig(const std::string &key,const char *value);
  65. void putLocalConfig(const std::string &key,const std::string &value);
  66. /**
  67. * @param key Configuration key
  68. * @return Value or empty string if not found
  69. */
  70. std::string getLocalConfig(const std::string &key) const;
  71. /**
  72. * @param nwid Network ID
  73. * @return Network or NULL if no network for that ID
  74. */
  75. inline SharedPtr<Network> network(uint64_t nwid) const
  76. {
  77. Mutex::Lock _l(_networks_m);
  78. std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.find(nwid));
  79. return ((n == _networks.end()) ? SharedPtr<Network>() : n->second);
  80. }
  81. /**
  82. * @return Vector containing all networks
  83. */
  84. inline std::vector< SharedPtr<Network> > networks() const
  85. {
  86. std::vector< SharedPtr<Network> > nwlist;
  87. Mutex::Lock _l(_networks_m);
  88. for(std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.begin());n!=_networks.end();++n)
  89. nwlist.push_back(n->second);
  90. return nwlist;
  91. }
  92. /**
  93. * Join a network or return existing network if already joined
  94. *
  95. * @param nwid Network ID to join
  96. * @return New network instance
  97. */
  98. inline SharedPtr<Network> join(uint64_t nwid)
  99. {
  100. Mutex::Lock _l(_networks_m);
  101. SharedPtr<Network> &nw = _networks[nwid];
  102. if (nw)
  103. return nw;
  104. else return (nw = Network::newInstance(_r,this,nwid));
  105. }
  106. /**
  107. * Leave a network
  108. *
  109. * @param nwid Network ID
  110. * @return True if network was left, false if we were not a member of this network
  111. */
  112. inline bool leave(uint64_t nwid)
  113. {
  114. Mutex::Lock _l(_networks_m);
  115. std::map< uint64_t,SharedPtr<Network> >::iterator n(_networks.find(nwid));
  116. if (n != _networks.end()) {
  117. n->second->destroy();
  118. _networks.erase(n);
  119. return true;
  120. } else return false;
  121. }
  122. /**
  123. * Perform cleanup and possibly persist saved state
  124. */
  125. void clean();
  126. /**
  127. * @param nwid Network ID
  128. * @return True if this network exists
  129. */
  130. inline bool hasNetwork(uint64_t nwid)
  131. {
  132. Mutex::Lock _l(_networks_m);
  133. return (_networks.count(nwid) > 0);
  134. }
  135. /**
  136. * @return Sorted vector of network tap device names from our virtual networks (not other taps on system)
  137. */
  138. inline std::vector<std::string> networkTapDeviceNames() const
  139. {
  140. std::vector<std::string> tapDevs;
  141. Mutex::Lock _l(_networks_m);
  142. for(std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.begin());n!=_networks.end();++n) {
  143. std::string dn(n->second->tapDeviceName());
  144. if (dn.length())
  145. tapDevs.push_back(dn);
  146. }
  147. return tapDevs;
  148. }
  149. private:
  150. /*
  151. static void _CBcommandHandler(void *arg,IpcConnection *ipcc,IpcConnection::EventType event,const char *commandLine);
  152. void _doCommand(IpcConnection *ipcc,const char *commandLine);
  153. */
  154. void _readLocalConfig();
  155. void _writeLocalConfig();
  156. const RuntimeEnvironment *_r;
  157. /*
  158. IpcListener _ipcListener;
  159. std::string _authToken;
  160. std::map< IpcConnection *,bool > _connections;
  161. Mutex _connections_m;
  162. */
  163. Dictionary _localConfig; // persisted as local.conf
  164. Mutex _localConfig_m;
  165. std::map< uint64_t,SharedPtr<Network> > _networks; // persisted in networks.d/
  166. Mutex _networks_m;
  167. };
  168. } // namespace ZeroTier
  169. #endif