NodeConfig.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 "UdpSocket.hpp"
  39. #include "Buffer.hpp"
  40. #include "Dictionary.hpp"
  41. namespace ZeroTier {
  42. class RuntimeEnvironment;
  43. /**
  44. * Maximum size of a packet for node configuration
  45. */
  46. #define ZT_NODECONFIG_MAX_PACKET_SIZE 4096
  47. /**
  48. * Node configuration endpoint
  49. */
  50. class NodeConfig
  51. {
  52. public:
  53. /**
  54. * @param renv Runtime environment
  55. * @param authToken Configuration authentication token
  56. * @param controlPort Control port for local control packet I/O
  57. * @throws std::runtime_error Unable to bind to local control port
  58. */
  59. NodeConfig(const RuntimeEnvironment *renv,const char *authToken,unsigned int controlPort);
  60. ~NodeConfig();
  61. /**
  62. * Store something in local configuration cache
  63. *
  64. * By convention, keys starting with _ will not be shown in the command bus
  65. * local config functions.
  66. *
  67. * @param key Configuration key
  68. * @param value Configuration value
  69. */
  70. void putLocalConfig(const std::string &key,const char *value);
  71. void putLocalConfig(const std::string &key,const std::string &value);
  72. /**
  73. * @param key Configuration key
  74. * @return Value or empty string if not found
  75. */
  76. std::string getLocalConfig(const std::string &key) const;
  77. /**
  78. * @param nwid Network ID
  79. * @return Network or NULL if no network for that ID
  80. */
  81. inline SharedPtr<Network> network(uint64_t nwid) const
  82. {
  83. Mutex::Lock _l(_networks_m);
  84. std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.find(nwid));
  85. return ((n == _networks.end()) ? SharedPtr<Network>() : n->second);
  86. }
  87. /**
  88. * @return Vector containing all networks
  89. */
  90. inline std::vector< SharedPtr<Network> > networks() const
  91. {
  92. std::vector< SharedPtr<Network> > nwlist;
  93. Mutex::Lock _l(_networks_m);
  94. for(std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.begin());n!=_networks.end();++n)
  95. nwlist.push_back(n->second);
  96. return nwlist;
  97. }
  98. /**
  99. * Perform cleanup and possibly update saved state
  100. */
  101. void clean();
  102. /**
  103. * @param nwid Network ID
  104. * @return True if this network exists
  105. */
  106. inline bool hasNetwork(uint64_t nwid)
  107. {
  108. Mutex::Lock _l(_networks_m);
  109. return (_networks.count(nwid) > 0);
  110. }
  111. /**
  112. * @return Set of network tap device names
  113. */
  114. inline std::set<std::string> networkTapDeviceNames() const
  115. {
  116. std::set<std::string> tapDevs;
  117. Mutex::Lock _l(_networks_m);
  118. for(std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.begin());n!=_networks.end();++n) {
  119. std::string dn(n->second->tapDeviceName());
  120. if (dn.length())
  121. tapDevs.insert(dn);
  122. }
  123. return tapDevs;
  124. }
  125. /**
  126. * Execute a control command (called when stuff comes in via control bus)
  127. *
  128. * @param command Command and arguments separated by whitespace (must already be trimmed of CR+LF, etc.)
  129. * @return One or more command results (lines of output)
  130. */
  131. std::vector<std::string> execute(const char *command);
  132. /**
  133. * Armor payload for control bus
  134. *
  135. * Note that no single element of payload can be longer than the max packet
  136. * size. If this occurs out_of_range is thrown.
  137. *
  138. * @param key 32 byte key
  139. * @param conversationId 32-bit conversation ID (bits beyond 32 are ignored)
  140. * @param payload One or more strings to encode in packet
  141. * @return One or more transport armored packets (if payload too big)
  142. * @throws std::out_of_range An element of payload is too big
  143. */
  144. static std::vector< Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> > encodeControlMessage(const void *key,unsigned long conversationId,const std::vector<std::string> &payload);
  145. /**
  146. * Decode a packet from the control bus
  147. *
  148. * Note that 'payload' is appended to. Existing data is not cleared.
  149. *
  150. * @param key 32 byte key
  151. * @param data Packet data
  152. * @param len Packet length
  153. * @param conversationId Result parameter filled with conversation ID on success
  154. * @param payload Result parameter to which results are appended
  155. * @return True on success, false on invalid packet or packet that failed authentication
  156. */
  157. static bool decodeControlMessagePacket(const void *key,const void *data,unsigned int len,unsigned long &conversationId,std::vector<std::string> &payload);
  158. private:
  159. static void _CBcontrolPacketHandler(UdpSocket *sock,void *arg,const InetAddress &remoteAddr,const void *data,unsigned int len);
  160. void _readLocalConfig();
  161. void _writeLocalConfig();
  162. const RuntimeEnvironment *_r;
  163. unsigned char _controlSocketKey[32];
  164. UdpSocket _controlSocket;
  165. Dictionary _localConfig; // persisted as local.conf
  166. Mutex _localConfig_m;
  167. std::map< uint64_t,SharedPtr<Network> > _networks; // persisted in networks.d/
  168. Mutex _networks_m;
  169. };
  170. } // namespace ZeroTier
  171. #endif