NetworkConfig.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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_NETWORKCONFIG_HPP
  28. #define ZT_NETWORKCONFIG_HPP
  29. #include <stdint.h>
  30. #include <map>
  31. #include <vector>
  32. #include <string>
  33. #include <stdexcept>
  34. #include <algorithm>
  35. #include "Constants.hpp"
  36. #include "Dictionary.hpp"
  37. #include "InetAddress.hpp"
  38. #include "AtomicCounter.hpp"
  39. #include "SharedPtr.hpp"
  40. #include "MulticastGroup.hpp"
  41. #include "Address.hpp"
  42. #include "CertificateOfMembership.hpp"
  43. namespace ZeroTier {
  44. // These dictionary keys are short so they don't take up much room in
  45. // netconf response packets.
  46. // integer(hex)[,integer(hex),...]
  47. #define ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES "et"
  48. // network ID
  49. #define ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID "nwid"
  50. // integer(hex)
  51. #define ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP "ts"
  52. // integer(hex)
  53. #define ZT_NETWORKCONFIG_DICT_KEY_REVISION "r"
  54. // address of member
  55. #define ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO "id"
  56. // integer(hex)
  57. #define ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT "ml"
  58. // dictionary of one or more of: MAC/ADI=preload,maxbalance,accrual
  59. #define ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_RATES "mr"
  60. // 0/1
  61. #define ZT_NETWORKCONFIG_DICT_KEY_PRIVATE "p"
  62. // text
  63. #define ZT_NETWORKCONFIG_DICT_KEY_NAME "n"
  64. // text
  65. #define ZT_NETWORKCONFIG_DICT_KEY_DESC "d"
  66. // IP/bits[,IP/bits,...]
  67. #define ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC "v4s"
  68. // IP/bits[,IP/bits,...]
  69. #define ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC "v6s"
  70. // serialized CertificateOfMembership
  71. #define ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP "com"
  72. // 0/1
  73. #define ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST "eb"
  74. // 0/1
  75. #define ZT_NETWORKCONFIG_DICT_KEY_ALLOW_PASSIVE_BRIDGING "pb"
  76. // node[,node,...]
  77. #define ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES "ab"
  78. // node;IP/port[,node;IP/port]
  79. #define ZT_NETWORKCONFIG_DICT_KEY_RELAYS "rl"
  80. // IP/metric[,IP/metric,...]
  81. #define ZT_NETWORKCONFIG_DICT_KEY_GATEWAYS "gw"
  82. /**
  83. * Network configuration received from network controller nodes
  84. *
  85. * This is an immutable value object created from a dictionary received from controller.
  86. */
  87. class NetworkConfig
  88. {
  89. friend class SharedPtr<NetworkConfig>;
  90. public:
  91. /**
  92. * Tuple of multicast rate parameters
  93. */
  94. struct MulticastRate
  95. {
  96. MulticastRate() throw() {}
  97. MulticastRate(uint32_t pl,uint32_t maxb,uint32_t acc) throw() : preload(pl),maxBalance(maxb),accrual(acc) {}
  98. uint32_t preload;
  99. uint32_t maxBalance;
  100. uint32_t accrual;
  101. inline bool operator==(const MulticastRate &mr) const { return ((preload == mr.preload)&&(maxBalance == mr.maxBalance)&&(accrual == mr.accrual)); }
  102. inline bool operator!=(const MulticastRate &mr) const { return (!(*this == mr)); }
  103. };
  104. /**
  105. * A hard-coded default multicast rate for networks that don't specify
  106. */
  107. static const MulticastRate DEFAULT_MULTICAST_RATE;
  108. /**
  109. * Create an instance of a NetworkConfig for the test network ID
  110. *
  111. * The test network ID is defined as ZT_TEST_NETWORK_ID. This is a
  112. * "fake" network with no real controller and default options.
  113. *
  114. * @param self This node's ZT address
  115. * @return Configuration for test network ID
  116. */
  117. static SharedPtr<NetworkConfig> createTestNetworkConfig(const Address &self);
  118. /**
  119. * @param d Dictionary containing configuration
  120. * @throws std::invalid_argument Invalid configuration
  121. */
  122. NetworkConfig(const Dictionary &d) { _fromDictionary(d); }
  123. /**
  124. * @param etherType Ethernet frame type to check
  125. * @return True if allowed on this network
  126. */
  127. inline bool permitsEtherType(unsigned int etherType) const
  128. throw()
  129. {
  130. if ((etherType <= 0)||(etherType > 0xffff)) // sanity checks
  131. return false;
  132. if ((_etWhitelist[0] & 1)) // presence of 0 means allow all
  133. return true;
  134. return ((_etWhitelist[etherType >> 3] & (1 << (etherType & 7))) != 0);
  135. }
  136. /**
  137. * @return Allowed ethernet types or a vector containing only 0 if "all"
  138. */
  139. std::vector<unsigned int> allowedEtherTypes() const;
  140. inline uint64_t networkId() const throw() { return _nwid; }
  141. inline uint64_t timestamp() const throw() { return _timestamp; }
  142. inline uint64_t revision() const throw() { return _revision; }
  143. inline const Address &issuedTo() const throw() { return _issuedTo; }
  144. inline unsigned int multicastLimit() const throw() { return _multicastLimit; }
  145. inline const std::map<MulticastGroup,MulticastRate> &multicastRates() const throw() { return _multicastRates; }
  146. inline bool allowPassiveBridging() const throw() { return _allowPassiveBridging; }
  147. inline bool isPublic() const throw() { return (!_private); }
  148. inline bool isPrivate() const throw() { return _private; }
  149. inline const std::string &name() const throw() { return _name; }
  150. inline const std::string &description() const throw() { return _description; }
  151. inline const std::vector<InetAddress> &staticIps() const throw() { return _staticIps; }
  152. inline const std::vector<Address> &activeBridges() const throw() { return _activeBridges; }
  153. inline const std::vector< std::pair<Address,InetAddress> > &relays() const throw() { return _relays; }
  154. inline const CertificateOfMembership &com() const throw() { return _com; }
  155. inline bool enableBroadcast() const throw() { return _enableBroadcast; }
  156. /**
  157. * @param fromPeer Peer attempting to bridge other Ethernet peers onto network
  158. * @return True if this network allows bridging
  159. */
  160. inline bool permitsBridging(const Address &fromPeer) const
  161. {
  162. return ( (_allowPassiveBridging) || (std::find(_activeBridges.begin(),_activeBridges.end(),fromPeer) != _activeBridges.end()) );
  163. }
  164. /**
  165. * @param mg Multicast group
  166. * @return Multicast rate or DEFAULT_MULTICAST_RATE if not set
  167. */
  168. const MulticastRate &multicastRate(const MulticastGroup &mg) const
  169. throw();
  170. bool operator==(const NetworkConfig &nc) const;
  171. inline bool operator!=(const NetworkConfig &nc) const { return (!(*this == nc)); }
  172. private:
  173. NetworkConfig() {}
  174. ~NetworkConfig() {}
  175. void _fromDictionary(const Dictionary &d);
  176. uint64_t _nwid;
  177. uint64_t _timestamp;
  178. uint64_t _revision;
  179. unsigned char _etWhitelist[65536 / 8];
  180. Address _issuedTo;
  181. unsigned int _multicastLimit;
  182. bool _allowPassiveBridging;
  183. bool _private;
  184. bool _enableBroadcast;
  185. std::string _name;
  186. std::string _description;
  187. std::vector<InetAddress> _staticIps;
  188. std::vector<Address> _activeBridges;
  189. std::vector< std::pair<Address,InetAddress> > _relays;
  190. std::map<MulticastGroup,MulticastRate> _multicastRates;
  191. CertificateOfMembership _com;
  192. AtomicCounter __refCount;
  193. };
  194. } // namespace ZeroTier
  195. #endif