NodeConfig.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 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 <map>
  30. #include <set>
  31. #include <string>
  32. #include <vector>
  33. #include <stdexcept>
  34. #include <stdint.h>
  35. #include "SharedPtr.hpp"
  36. #include "Network.hpp"
  37. #include "Utils.hpp"
  38. #include "Http.hpp"
  39. #include "UdpSocket.hpp"
  40. #include "Buffer.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. * Packet format for local UDP configuration packets:
  51. * [16] first 16 bytes of HMAC-SHA-256 of payload
  52. * [ -- begin HMAC'ed envelope -- ]
  53. * [8] random initialization vector
  54. * [ -- begin cryptographic envelope -- ]
  55. * [4] arbitrary tag, echoed in response
  56. * [...] payload
  57. *
  58. * For requests, the payload consists of a single ASCII command. For
  59. * responses, the payload consists of one or more response lines delimited
  60. * by NULL (0) characters. The tag field is replicated in the result
  61. * packet.
  62. */
  63. class NodeConfig
  64. {
  65. public:
  66. /**
  67. * @param renv Runtime environment
  68. * @param authToken Configuration authentication token
  69. * @throws std::runtime_error Unable to bind to local control port
  70. */
  71. NodeConfig(const RuntimeEnvironment *renv,const char *authToken)
  72. throw(std::runtime_error);
  73. ~NodeConfig();
  74. /**
  75. * @param nwid Network ID
  76. * @return Network or NULL if no network for that ID
  77. */
  78. inline SharedPtr<Network> network(uint64_t nwid) const
  79. {
  80. Mutex::Lock _l(_networks_m);
  81. std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.find(nwid));
  82. return ((n == _networks.end()) ? SharedPtr<Network>() : n->second);
  83. }
  84. /**
  85. * @return Vector containing all networks
  86. */
  87. inline std::vector< SharedPtr<Network> > networks() const
  88. {
  89. std::vector< SharedPtr<Network> > nwlist;
  90. Mutex::Lock _l(_networks_m);
  91. for(std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.begin());n!=_networks.end();++n)
  92. nwlist.push_back(n->second);
  93. return nwlist;
  94. }
  95. /**
  96. * Call whack() on all networks' tap devices
  97. */
  98. void whackAllTaps();
  99. /**
  100. * @param nwid Network ID
  101. * @return True if this network exists
  102. */
  103. inline bool hasNetwork(uint64_t nwid)
  104. {
  105. Mutex::Lock _l(_networks_m);
  106. return (_networks.count(nwid) > 0);
  107. }
  108. /**
  109. * @return Set of network tap device names
  110. */
  111. inline std::set<std::string> networkTapDeviceNames() const
  112. {
  113. std::set<std::string> tapDevs;
  114. Mutex::Lock _l(_networks_m);
  115. for(std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.begin());n!=_networks.end();++n)
  116. tapDevs.insert(n->second->tap().deviceName());
  117. return tapDevs;
  118. }
  119. /**
  120. * Execute a command
  121. *
  122. * @param command Command and arguments separated by whitespace (must already be trimmed of CR+LF, etc.)
  123. * @return One or more command results (lines of output)
  124. */
  125. std::vector<std::string> execute(const char *command);
  126. /**
  127. * Armor payload for control bus
  128. *
  129. * Note that no single element of payload can be longer than the max packet
  130. * size. If this occurs out_of_range is thrown.
  131. *
  132. * @param key 32 byte key
  133. * @param conversationId 32-bit conversation ID (bits beyond 32 are ignored)
  134. * @param payload One or more strings to encode in packet
  135. * @return One or more transport armored packets (if payload too big)
  136. * @throws std::out_of_range An element of payload is too big
  137. */
  138. static std::vector< Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> > encodeControlMessage(const void *key,unsigned long conversationId,const std::vector<std::string> &payload)
  139. throw(std::out_of_range);
  140. /**
  141. * Decode a packet from the control bus
  142. *
  143. * Note that 'payload' is appended to. Existing data is not cleared.
  144. *
  145. * @param key 32 byte key
  146. * @param data Packet data
  147. * @param len Packet length
  148. * @param conversationId Result parameter filled with conversation ID on success
  149. * @param payload Result parameter to which results are appended
  150. * @return True on success, false on invalid packet or packet that failed authentication
  151. */
  152. static bool decodeControlMessagePacket(const void *key,const void *data,unsigned int len,unsigned long &conversationId,std::vector<std::string> &payload);
  153. private:
  154. static void _CBcontrolPacketHandler(UdpSocket *sock,void *arg,const InetAddress &remoteAddr,const void *data,unsigned int len);
  155. const RuntimeEnvironment *_r;
  156. unsigned char _controlSocketKey[32];
  157. UdpSocket _controlSocket;
  158. std::map< uint64_t,SharedPtr<Network> > _networks;
  159. Mutex _networks_m;
  160. };
  161. } // namespace ZeroTier
  162. #endif