NodeConfig.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. namespace ZeroTier {
  41. class RuntimeEnvironment;
  42. /**
  43. * Node configuration endpoint
  44. *
  45. * Packet format for local UDP configuration packets:
  46. * [8] random initialization vector
  47. * [16] first 16 bytes of HMAC-SHA-256 of payload
  48. * [4] arbitrary tag, echoed in response
  49. * [...] payload
  50. *
  51. * For requests, the payload consists of a single ASCII command. For
  52. * responses, the payload consists of one or more response lines delimited
  53. * by NULL (0) characters. The tag field is replicated in the result
  54. * packet.
  55. *
  56. * TODO: further document use of keys, encryption...
  57. */
  58. class NodeConfig
  59. {
  60. public:
  61. /**
  62. * @param renv Runtime environment
  63. * @param authToken Configuration authentication token
  64. * @throws std::runtime_error Unable to bind to local control port
  65. */
  66. NodeConfig(const RuntimeEnvironment *renv,const char *authToken)
  67. throw(std::runtime_error);
  68. ~NodeConfig();
  69. /**
  70. * @param nwid Network ID
  71. * @return Network or NULL if no network for that ID
  72. */
  73. inline SharedPtr<Network> network(uint64_t nwid) const
  74. {
  75. Mutex::Lock _l(_networks_m);
  76. std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.find(nwid));
  77. return ((n == _networks.end()) ? SharedPtr<Network>() : n->second);
  78. }
  79. /**
  80. * @return Vector containing all networks
  81. */
  82. inline std::vector< SharedPtr<Network> > networks() const
  83. {
  84. std::vector< SharedPtr<Network> > nwlist;
  85. Mutex::Lock _l(_networks_m);
  86. for(std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.begin());n!=_networks.end();++n)
  87. nwlist.push_back(n->second);
  88. return nwlist;
  89. }
  90. /**
  91. * Call whack() on all networks' tap devices
  92. */
  93. void whackAllTaps();
  94. /**
  95. * @param nwid Network ID
  96. * @return True if this network exists
  97. */
  98. inline bool hasNetwork(uint64_t nwid)
  99. {
  100. Mutex::Lock _l(_networks_m);
  101. return (_networks.count(nwid) > 0);
  102. }
  103. /**
  104. * @return Set of network tap device names
  105. */
  106. inline std::set<std::string> networkTapDeviceNames() const
  107. {
  108. std::set<std::string> tapDevs;
  109. Mutex::Lock _l(_networks_m);
  110. for(std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.begin());n!=_networks.end();++n)
  111. tapDevs.insert(n->second->tap().deviceName());
  112. return tapDevs;
  113. }
  114. /**
  115. * Execute a command
  116. *
  117. * @param command Command and arguments separated by whitespace (must already be trimmed of CR+LF, etc.)
  118. * @return One or more command results (lines of output)
  119. */
  120. std::vector<std::string> execute(const char *command);
  121. private:
  122. static void _CBcontrolPacketHandler(UdpSocket *sock,void *arg,const InetAddress &remoteAddr,const void *data,unsigned int len);
  123. const RuntimeEnvironment *_r;
  124. const std::string _authToken;
  125. unsigned char _keys[64]; // Salsa20 key, HMAC key
  126. UdpSocket _controlSocket;
  127. std::map< uint64_t,SharedPtr<Network> > _networks;
  128. Mutex _networks_m;
  129. };
  130. } // namespace ZeroTier
  131. #endif