NetworkConfig.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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_NETWORKCONFIG_HPP
  28. #define _ZT_NETWORKCONFIG_HPP
  29. #include <set>
  30. #include <string>
  31. #include <stdexcept>
  32. #include "Dictionary.hpp"
  33. #include "InetAddress.hpp"
  34. #include "AtomicCounter.hpp"
  35. #include "SharedPtr.hpp"
  36. namespace ZeroTier {
  37. // These are short to fit in packets with plenty of room to spare
  38. #define ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES "et"
  39. #define ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID "nwid"
  40. #define ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP "ts"
  41. #define ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO "id"
  42. #define ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_PREFIX_BITS "mpb"
  43. #define ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_DEPTH "md"
  44. #define ZT_NETWORKCONFIG_DICT_KEY_ARP_CACHE_TTL "cARP"
  45. #define ZT_NETWORKCONFIG_DICT_KEY_NDP_CACHE_TTL "cNDP"
  46. #define ZT_NETWORKCONFIG_DICT_KEY_EMULATE_ARP "eARP"
  47. #define ZT_NETWORKCONFIG_DICT_KEY_EMULATE_NDP "eNDP"
  48. #define ZT_NETWORKCONFIG_DICT_KEY_IS_OPEN "o"
  49. #define ZT_NETWORKCONFIG_DICT_KEY_NAME "name"
  50. #define ZT_NETWORKCONFIG_DICT_KEY_DESC "desc"
  51. #define ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC "v4s"
  52. #define ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC "v6s"
  53. #define ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_RATES "mr"
  54. /**
  55. * Network configuration received from netconf master nodes
  56. *
  57. * This is designed to work as an immutable value object held in a shared
  58. * pointer so that it can be both updated and used without too much mutex
  59. * boogie.
  60. */
  61. class NetworkConfig
  62. {
  63. public:
  64. friend class SharedPtr<NetworkConfig>;
  65. /**
  66. * @param d Dictionary containing configuration
  67. * @throws std::invalid_argument Invalid configuration
  68. */
  69. NetworkConfig(const Dictionary &d)
  70. throw(std::invalid_argument)
  71. {
  72. _fromDictionary(d);
  73. }
  74. /**
  75. * @param etherType Ethernet frame type to check
  76. * @return True if allowed on this network
  77. */
  78. inline bool allowsEtherType(unsigned int etherType)
  79. throw()
  80. {
  81. if ((!etherType)||(etherType > 0xffff)) // sanity checks
  82. return false;
  83. else if ((_etWhitelist[0] & 1)) // prsence of 0 in set inverts sense: whitelist becomes blacklist
  84. return (!(_etWhitelist[etherType >> 3] & (1 << (etherType & 7))));
  85. else return ((_etWhitelist[etherType >> 3] & (1 << (etherType & 7))));
  86. }
  87. std::set<unsigned int> allowedEtherTypes() const;
  88. inline uint64_t networkId() const throw() { return _nwid; }
  89. inline uint64_t timestamp() const throw() { return _timestamp; }
  90. inline const Address &issuedTo() const throw() { return _issuedTo; }
  91. inline unsigned int multicastPrefixBits() const throw() { return _multicastPrefixBits; }
  92. inline unsigned int multicastDepth() const throw() { return _multicastDepth; }
  93. inline unsigned int arpCacheTtl() const throw() { return _arpCacheTtl; }
  94. inline unsigned int ndpCacheTtl() const throw() { return _ndpCacheTtl; }
  95. inline bool emulateArp() const throw() { return _emulateArp; }
  96. inline bool emulateNdp() const throw() { return _emulateNdp; }
  97. inline bool isOpen() const throw() { return _isOpen; }
  98. inline const std::string &name() const throw() { return _name; }
  99. inline const std::string &description() const throw() { return _description; }
  100. inline const std::set<InetAddress> &staticIps() const throw() { return _staticIps; }
  101. inline const MulticastRateTable &multicastRates() const throw() { return _multicastRates; }
  102. private:
  103. NetworkConfig() {}
  104. ~NetworkConfig() {}
  105. void _fromDictionary(const Dictionary &d)
  106. throw(std::invalid_argument);
  107. unsigned char _etWhitelist[65536 / 8];
  108. uint64_t _nwid;
  109. uint64_t _timestamp;
  110. Address _issuedTo;
  111. unsigned int _multicastPrefixBits;
  112. unsigned int _multicastDepth;
  113. unsigned int _arpCacheTtl;
  114. unsigned int _ndpCacheTtl;
  115. bool _emulateArp;
  116. bool _emulateNdp;
  117. bool _isOpen;
  118. std::string _name;
  119. std::string _description;
  120. std::set<InetAddress> _staticIps;
  121. MulticastRateTable _multicastRates;
  122. AtomicCounter __refCount;
  123. };
  124. } // namespace ZeroTier
  125. #endif