NetworkConfig.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  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. #ifndef ZT_NETWORKCONFIG_HPP
  19. #define ZT_NETWORKCONFIG_HPP
  20. #include <stdint.h>
  21. #include <map>
  22. #include <vector>
  23. #include <string>
  24. #include <stdexcept>
  25. #include <algorithm>
  26. #include "Constants.hpp"
  27. #include "Dictionary.hpp"
  28. #include "InetAddress.hpp"
  29. #include "AtomicCounter.hpp"
  30. #include "SharedPtr.hpp"
  31. #include "MulticastGroup.hpp"
  32. #include "Address.hpp"
  33. #include "CertificateOfMembership.hpp"
  34. namespace ZeroTier {
  35. // Fields for meta-data sent with network config requests
  36. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MAJOR_VERSION "majv"
  37. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MINOR_VERSION "minv"
  38. #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_REVISION "revv"
  39. // These dictionary keys are short so they don't take up much room in
  40. // netconf response packets.
  41. // integer(hex)[,integer(hex),...]
  42. #define ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES "et"
  43. // network ID
  44. #define ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID "nwid"
  45. // integer(hex)
  46. #define ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP "ts"
  47. // integer(hex)
  48. #define ZT_NETWORKCONFIG_DICT_KEY_REVISION "r"
  49. // address of member
  50. #define ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO "id"
  51. // integer(hex)
  52. #define ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT "ml"
  53. // 0/1
  54. #define ZT_NETWORKCONFIG_DICT_KEY_PRIVATE "p"
  55. // text
  56. #define ZT_NETWORKCONFIG_DICT_KEY_NAME "n"
  57. // text
  58. #define ZT_NETWORKCONFIG_DICT_KEY_DESC "d"
  59. // IP/bits[,IP/bits,...]
  60. // Note that IPs that end in all zeroes are routes with no assignment in them.
  61. #define ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC "v4s"
  62. // IP/bits[,IP/bits,...]
  63. // Note that IPs that end in all zeroes are routes with no assignment in them.
  64. #define ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC "v6s"
  65. // serialized CertificateOfMembership
  66. #define ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP "com"
  67. // 0/1
  68. #define ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST "eb"
  69. // 0/1
  70. #define ZT_NETWORKCONFIG_DICT_KEY_ALLOW_PASSIVE_BRIDGING "pb"
  71. // node[,node,...]
  72. #define ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES "ab"
  73. // node;IP/port[,node;IP/port]
  74. #define ZT_NETWORKCONFIG_DICT_KEY_RELAYS "rl"
  75. // IP/metric[,IP/metric,...]
  76. #define ZT_NETWORKCONFIG_DICT_KEY_GATEWAYS "gw"
  77. /**
  78. * Network configuration received from network controller nodes
  79. *
  80. * This is an immutable value object created from a dictionary received from controller.
  81. */
  82. class NetworkConfig
  83. {
  84. friend class SharedPtr<NetworkConfig>;
  85. public:
  86. /**
  87. * Create an instance of a NetworkConfig for the test network ID
  88. *
  89. * The test network ID is defined as ZT_TEST_NETWORK_ID. This is a
  90. * "fake" network with no real controller and default options.
  91. *
  92. * @param self This node's ZT address
  93. * @return Configuration for test network ID
  94. */
  95. static SharedPtr<NetworkConfig> createTestNetworkConfig(const Address &self);
  96. /**
  97. * @param d Dictionary containing configuration
  98. * @throws std::invalid_argument Invalid configuration
  99. */
  100. NetworkConfig(const Dictionary &d) { _fromDictionary(d); }
  101. /**
  102. * @param etherType Ethernet frame type to check
  103. * @return True if allowed on this network
  104. */
  105. inline bool permitsEtherType(unsigned int etherType) const
  106. throw()
  107. {
  108. if ((etherType <= 0)||(etherType > 0xffff)) // sanity checks
  109. return false;
  110. if ((_etWhitelist[0] & 1)) // presence of 0 means allow all
  111. return true;
  112. return ((_etWhitelist[etherType >> 3] & (1 << (etherType & 7))) != 0);
  113. }
  114. /**
  115. * @return Allowed ethernet types or a vector containing only 0 if "all"
  116. */
  117. std::vector<unsigned int> allowedEtherTypes() const;
  118. inline uint64_t networkId() const throw() { return _nwid; }
  119. inline uint64_t timestamp() const throw() { return _timestamp; }
  120. inline uint64_t revision() const throw() { return _revision; }
  121. inline const Address &issuedTo() const throw() { return _issuedTo; }
  122. inline unsigned int multicastLimit() const throw() { return _multicastLimit; }
  123. inline bool allowPassiveBridging() const throw() { return _allowPassiveBridging; }
  124. inline bool isPublic() const throw() { return (!_private); }
  125. inline bool isPrivate() const throw() { return _private; }
  126. inline const std::string &name() const throw() { return _name; }
  127. inline const std::vector<InetAddress> &localRoutes() const throw() { return _localRoutes; }
  128. inline const std::vector<InetAddress> &staticIps() const throw() { return _staticIps; }
  129. inline const std::vector<InetAddress> &gateways() const throw() { return _gateways; }
  130. inline const std::vector<Address> &activeBridges() const throw() { return _activeBridges; }
  131. inline const std::vector< std::pair<Address,InetAddress> > &relays() const throw() { return _relays; }
  132. inline const CertificateOfMembership &com() const throw() { return _com; }
  133. inline bool enableBroadcast() const throw() { return _enableBroadcast; }
  134. /**
  135. * @param fromPeer Peer attempting to bridge other Ethernet peers onto network
  136. * @return True if this network allows bridging
  137. */
  138. inline bool permitsBridging(const Address &fromPeer) const
  139. {
  140. return ( (_allowPassiveBridging) || (std::find(_activeBridges.begin(),_activeBridges.end(),fromPeer) != _activeBridges.end()) );
  141. }
  142. bool operator==(const NetworkConfig &nc) const;
  143. inline bool operator!=(const NetworkConfig &nc) const { return (!(*this == nc)); }
  144. private:
  145. NetworkConfig() {}
  146. ~NetworkConfig() {}
  147. void _fromDictionary(const Dictionary &d);
  148. uint64_t _nwid;
  149. uint64_t _timestamp;
  150. uint64_t _revision;
  151. unsigned char _etWhitelist[65536 / 8];
  152. Address _issuedTo;
  153. unsigned int _multicastLimit;
  154. bool _allowPassiveBridging;
  155. bool _private;
  156. bool _enableBroadcast;
  157. std::string _name;
  158. std::vector<InetAddress> _localRoutes;
  159. std::vector<InetAddress> _staticIps;
  160. std::vector<InetAddress> _gateways;
  161. std::vector<Address> _activeBridges;
  162. std::vector< std::pair<Address,InetAddress> > _relays;
  163. CertificateOfMembership _com;
  164. AtomicCounter __refCount;
  165. };
  166. } // namespace ZeroTier
  167. #endif