NetworkConfig.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. #include <stdint.h>
  28. #include "NetworkConfig.hpp"
  29. #include "Utils.hpp"
  30. namespace ZeroTier {
  31. // This is fast enough for things like Apple's mDNS spam, so it should serve
  32. // as a good default for your average network.
  33. const NetworkConfig::MulticastRate NetworkConfig::DEFAULT_MULTICAST_RATE(40000,60000,80);
  34. SharedPtr<NetworkConfig> NetworkConfig::createTestNetworkConfig(const Address &self)
  35. {
  36. SharedPtr<NetworkConfig> nc(new NetworkConfig());
  37. memset(nc->_etWhitelist,0,sizeof(nc->_etWhitelist));
  38. nc->_etWhitelist[0] |= 1; // allow all
  39. nc->_nwid = ZT_TEST_NETWORK_ID;
  40. nc->_timestamp = Utils::now();
  41. nc->_revision = 1;
  42. nc->_issuedTo = self;
  43. nc->_multicastLimit = ZT_MULTICAST_DEFAULT_LIMIT;
  44. nc->_allowPassiveBridging = false;
  45. nc->_private = false;
  46. nc->_enableBroadcast = true;
  47. nc->_name = "ZT_TEST_NETWORK";
  48. nc->_description = "Built-in dummy test network";
  49. // Make up a V4 IP from 'self' in the 10.0.0.0/8 range -- no
  50. // guarantee of uniqueness but collisions are unlikely.
  51. uint32_t ip = (uint32_t)((self.toInt() & 0x00ffffff) | 0x0a000000); // 10.x.x.x
  52. if ((ip & 0x000000ff) == 0x000000ff) ip ^= 0x00000001; // but not ending in .255
  53. if ((ip & 0x000000ff) == 0x00000000) ip ^= 0x00000001; // or .0
  54. nc->_staticIps.push_back(InetAddress(Utils::hton(ip),8));
  55. return nc;
  56. }
  57. std::vector<unsigned int> NetworkConfig::allowedEtherTypes() const
  58. {
  59. std::vector<unsigned int> ets;
  60. if ((_etWhitelist[0] & 1) != 0) {
  61. ets.push_back(0);
  62. } else {
  63. for(unsigned int i=0;i<sizeof(_etWhitelist);++i) {
  64. if (_etWhitelist[i]) {
  65. unsigned char b = _etWhitelist[i];
  66. unsigned int et = i * 8;
  67. while (b) {
  68. if ((b & 1))
  69. ets.push_back(et);
  70. b >>= 1;
  71. ++et;
  72. }
  73. }
  74. }
  75. }
  76. return ets;
  77. }
  78. const NetworkConfig::MulticastRate &NetworkConfig::multicastRate(const MulticastGroup &mg) const
  79. throw()
  80. {
  81. std::map<MulticastGroup,MulticastRate>::const_iterator r(_multicastRates.find(mg));
  82. if (r == _multicastRates.end()) {
  83. r = _multicastRates.find(MulticastGroup()); // zero MG signifies network's default rate
  84. if (r == _multicastRates.end())
  85. return DEFAULT_MULTICAST_RATE; // neither specific nor default found in network config
  86. }
  87. return r->second;
  88. }
  89. void NetworkConfig::_fromDictionary(const Dictionary &d)
  90. {
  91. static const std::string zero("0");
  92. static const std::string one("1");
  93. // NOTE: d.get(name) throws if not found, d.get(name,default) returns default
  94. _nwid = Utils::hexStrToU64(d.get(ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID).c_str());
  95. if (!_nwid)
  96. throw std::invalid_argument("configuration contains zero network ID");
  97. _timestamp = Utils::hexStrToU64(d.get(ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP).c_str());
  98. _revision = Utils::hexStrToU64(d.get(ZT_NETWORKCONFIG_DICT_KEY_REVISION,"1").c_str()); // older netconf masters don't send this, so default to 1
  99. memset(_etWhitelist,0,sizeof(_etWhitelist));
  100. std::vector<std::string> ets(Utils::split(d.get(ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES).c_str(),",","",""));
  101. for(std::vector<std::string>::const_iterator et(ets.begin());et!=ets.end();++et) {
  102. unsigned int tmp = Utils::hexStrToUInt(et->c_str()) & 0xffff;
  103. _etWhitelist[tmp >> 3] |= (1 << (tmp & 7));
  104. }
  105. _issuedTo = Address(d.get(ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO));
  106. _multicastLimit = Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT,zero).c_str());
  107. if (_multicastLimit == 0) _multicastLimit = ZT_MULTICAST_DEFAULT_LIMIT;
  108. _allowPassiveBridging = (Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_ALLOW_PASSIVE_BRIDGING,zero).c_str()) != 0);
  109. _private = (Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_PRIVATE,one).c_str()) != 0);
  110. _enableBroadcast = (Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST,one).c_str()) != 0);
  111. _name = d.get(ZT_NETWORKCONFIG_DICT_KEY_NAME);
  112. _description = d.get(ZT_NETWORKCONFIG_DICT_KEY_DESC,std::string());
  113. // In dictionary IPs are split into V4 and V6 addresses, but we don't really
  114. // need that so merge them here.
  115. std::string ipAddrs(d.get(ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC,std::string()));
  116. {
  117. std::string v6s(d.get(ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC,std::string()));
  118. if (v6s.length()) {
  119. if (ipAddrs.length())
  120. ipAddrs.push_back(',');
  121. ipAddrs.append(v6s);
  122. }
  123. }
  124. std::vector<std::string> ipAddrsSplit(Utils::split(ipAddrs.c_str(),",","",""));
  125. for(std::vector<std::string>::const_iterator ipstr(ipAddrsSplit.begin());ipstr!=ipAddrsSplit.end();++ipstr) {
  126. InetAddress addr(*ipstr);
  127. switch(addr.type()) {
  128. case InetAddress::TYPE_IPV4:
  129. if ((!addr.netmaskBits())||(addr.netmaskBits() > 32))
  130. continue;
  131. break;
  132. case InetAddress::TYPE_IPV6:
  133. if ((!addr.netmaskBits())||(addr.netmaskBits() > 128))
  134. continue;
  135. break;
  136. default: // ignore unrecognized address types or junk/empty fields
  137. continue;
  138. }
  139. _staticIps.push_back(addr);
  140. }
  141. std::sort(_staticIps.begin(),_staticIps.end());
  142. std::unique(_staticIps.begin(),_staticIps.end());
  143. std::vector<std::string> activeBridgesSplit(Utils::split(d.get(ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES,"").c_str(),",","",""));
  144. for(std::vector<std::string>::const_iterator a(activeBridgesSplit.begin());a!=activeBridgesSplit.end();++a) {
  145. if (a->length() == ZT_ADDRESS_LENGTH_HEX) { // ignore empty or garbage fields
  146. Address tmp(*a);
  147. if (!tmp.isReserved())
  148. _activeBridges.push_back(tmp);
  149. }
  150. }
  151. std::sort(_activeBridges.begin(),_activeBridges.end());
  152. std::unique(_activeBridges.begin(),_activeBridges.end());
  153. Dictionary multicastRateEntries(d.get(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_RATES,std::string()));
  154. for(Dictionary::const_iterator i(multicastRateEntries.begin());i!=multicastRateEntries.end();++i) {
  155. std::vector<std::string> params(Utils::split(i->second.c_str(),",","",""));
  156. if (params.size() >= 3)
  157. _multicastRates[MulticastGroup(i->first)] = MulticastRate(Utils::hexStrToUInt(params[0].c_str()),Utils::hexStrToUInt(params[1].c_str()),Utils::hexStrToUInt(params[2].c_str()));
  158. }
  159. _com.fromString(d.get(ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP,std::string()));
  160. }
  161. bool NetworkConfig::operator==(const NetworkConfig &nc) const
  162. {
  163. if (_nwid != nc._nwid) return false;
  164. if (_timestamp != nc._timestamp) return false;
  165. if (memcmp(_etWhitelist,nc._etWhitelist,sizeof(_etWhitelist))) return false;
  166. if (_issuedTo != nc._issuedTo) return false;
  167. if (_multicastLimit != nc._multicastLimit) return false;
  168. if (_allowPassiveBridging != nc._allowPassiveBridging) return false;
  169. if (_private != nc._private) return false;
  170. if (_enableBroadcast != nc._enableBroadcast) return false;
  171. if (_name != nc._name) return false;
  172. if (_description != nc._description) return false;
  173. if (_staticIps != nc._staticIps) return false;
  174. if (_activeBridges != nc._activeBridges) return false;
  175. if (_multicastRates != nc._multicastRates) return false;
  176. if (_com != nc._com) return false;
  177. return true;
  178. }
  179. } // namespace ZeroTier