2
0

NetworkConfig.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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_NETWORKCONFIG_HPP
  28. #define ZT_NETWORKCONFIG_HPP
  29. #include <stdint.h>
  30. #include <map>
  31. #include <set>
  32. #include <string>
  33. #include <stdexcept>
  34. #include "Constants.hpp"
  35. #include "Dictionary.hpp"
  36. #include "InetAddress.hpp"
  37. #include "AtomicCounter.hpp"
  38. #include "SharedPtr.hpp"
  39. #include "MulticastGroup.hpp"
  40. #include "Address.hpp"
  41. #include "CertificateOfMembership.hpp"
  42. namespace ZeroTier {
  43. // These dictionary keys are short so they don't take up much room in
  44. // netconf response packets.
  45. #define ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES "et"
  46. #define ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID "nwid"
  47. #define ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP "ts"
  48. #define ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO "id"
  49. #define ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_PREFIX_BITS "mpb"
  50. #define ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_DEPTH "md"
  51. #define ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_RATES "mr"
  52. #define ZT_NETWORKCONFIG_DICT_KEY_PRIVATE "p"
  53. #define ZT_NETWORKCONFIG_DICT_KEY_NAME "n"
  54. #define ZT_NETWORKCONFIG_DICT_KEY_DESC "d"
  55. #define ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC "v4s"
  56. #define ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC "v6s"
  57. #define ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP "com"
  58. #define ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST "eb"
  59. /**
  60. * Network configuration received from netconf master nodes
  61. *
  62. * This is designed to work as an immutable value object held in a shared
  63. * pointer so that it can be both updated and used without too much mutex
  64. * boogie.
  65. */
  66. class NetworkConfig
  67. {
  68. public:
  69. friend class SharedPtr<NetworkConfig>;
  70. /**
  71. * Tuple of multicast rate parameters
  72. */
  73. struct MulticastRate
  74. {
  75. MulticastRate() throw() {}
  76. MulticastRate(uint32_t pl,uint32_t maxb,uint32_t acc) throw() : preload(pl),maxBalance(maxb),accrual(acc) {}
  77. uint32_t preload;
  78. uint32_t maxBalance;
  79. uint32_t accrual;
  80. };
  81. /**
  82. * A hard-coded default multicast rate for networks that don't specify
  83. */
  84. static const MulticastRate DEFAULT_MULTICAST_RATE;
  85. /**
  86. * @param d Dictionary containing configuration
  87. * @throws std::invalid_argument Invalid configuration
  88. */
  89. NetworkConfig(const Dictionary &d) { _fromDictionary(d); }
  90. /**
  91. * @param etherType Ethernet frame type to check
  92. * @return True if allowed on this network
  93. */
  94. inline bool permitsEtherType(unsigned int etherType)
  95. throw()
  96. {
  97. if ((!etherType)||(etherType > 0xffff)) // sanity checks
  98. return false;
  99. else if ((_etWhitelist[0] & 1)) // prsence of 0 in set inverts sense: whitelist becomes blacklist
  100. return ((_etWhitelist[etherType >> 3] & (1 << (etherType & 7))) == 0);
  101. else return ((_etWhitelist[etherType >> 3] & (1 << (etherType & 7))) != 0);
  102. }
  103. std::set<unsigned int> allowedEtherTypes() const;
  104. inline uint64_t networkId() const throw() { return _nwid; }
  105. inline uint64_t timestamp() const throw() { return _timestamp; }
  106. inline const Address &issuedTo() const throw() { return _issuedTo; }
  107. inline unsigned int multicastPrefixBits() const throw() { return _multicastPrefixBits; }
  108. inline unsigned int multicastDepth() const throw() { return _multicastDepth; }
  109. inline const std::map<MulticastGroup,MulticastRate> &multicastRates() const throw() { return _multicastRates; }
  110. inline bool isOpen() const throw() { return (!_private); }
  111. inline bool isPrivate() const throw() { return _private; }
  112. inline const std::string &name() const throw() { return _name; }
  113. inline const std::string &description() const throw() { return _description; }
  114. inline const std::set<InetAddress> &staticIps() const throw() { return _staticIps; }
  115. inline const CertificateOfMembership &com() const throw() { return _com; }
  116. inline bool enableBroadcast() const throw() { return _enableBroadcast; }
  117. /**
  118. * @param fromPeer Peer attempting to bridge other Ethernet peers onto network
  119. * @return True if this network allows bridging
  120. */
  121. inline bool permitsBridging(const Address &fromPeer) const
  122. throw()
  123. {
  124. return false; // TODO: bridging not implemented yet
  125. }
  126. /**
  127. * @param mg Multicast group
  128. * @return Multicast rate or DEFAULT_MULTICAST_RATE if not set
  129. */
  130. const MulticastRate &multicastRate(const MulticastGroup &mg) const
  131. throw();
  132. private:
  133. NetworkConfig() {}
  134. ~NetworkConfig() {}
  135. void _fromDictionary(const Dictionary &d);
  136. unsigned char _etWhitelist[65536 / 8];
  137. uint64_t _nwid;
  138. uint64_t _timestamp;
  139. Address _issuedTo;
  140. unsigned int _multicastPrefixBits;
  141. unsigned int _multicastDepth;
  142. bool _private;
  143. bool _enableBroadcast;
  144. std::string _name;
  145. std::string _description;
  146. std::set<InetAddress> _staticIps;
  147. std::map<MulticastGroup,MulticastRate> _multicastRates;
  148. CertificateOfMembership _com;
  149. AtomicCounter __refCount;
  150. };
  151. } // namespace ZeroTier
  152. #endif