NetworkConfig.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. SharedPtr<NetworkConfig> NetworkConfig::createTestNetworkConfig(const Address &self)
  32. {
  33. SharedPtr<NetworkConfig> nc(new NetworkConfig());
  34. memset(nc->_etWhitelist,0,sizeof(nc->_etWhitelist));
  35. nc->_etWhitelist[0] |= 1; // allow all
  36. nc->_nwid = ZT_TEST_NETWORK_ID;
  37. nc->_timestamp = 1;
  38. nc->_revision = 1;
  39. nc->_issuedTo = self;
  40. nc->_multicastLimit = ZT_MULTICAST_DEFAULT_LIMIT;
  41. nc->_allowPassiveBridging = false;
  42. nc->_private = false;
  43. nc->_enableBroadcast = true;
  44. nc->_name = "ZT_TEST_NETWORK";
  45. // Make up a V4 IP from 'self' in the 10.0.0.0/8 range -- no
  46. // guarantee of uniqueness but collisions are unlikely.
  47. uint32_t ip = (uint32_t)((self.toInt() & 0x00ffffff) | 0x0a000000); // 10.x.x.x
  48. if ((ip & 0x000000ff) == 0x000000ff) ip ^= 0x00000001; // but not ending in .255
  49. if ((ip & 0x000000ff) == 0x00000000) ip ^= 0x00000001; // or .0
  50. nc->_staticIps.push_back(InetAddress(Utils::hton(ip),8));
  51. return nc;
  52. }
  53. std::vector<unsigned int> NetworkConfig::allowedEtherTypes() const
  54. {
  55. std::vector<unsigned int> ets;
  56. if ((_etWhitelist[0] & 1) != 0) {
  57. ets.push_back(0);
  58. } else {
  59. for(unsigned int i=0;i<sizeof(_etWhitelist);++i) {
  60. if (_etWhitelist[i]) {
  61. unsigned char b = _etWhitelist[i];
  62. unsigned int et = i * 8;
  63. while (b) {
  64. if ((b & 1))
  65. ets.push_back(et);
  66. b >>= 1;
  67. ++et;
  68. }
  69. }
  70. }
  71. }
  72. return ets;
  73. }
  74. void NetworkConfig::_fromDictionary(const Dictionary &d)
  75. {
  76. static const std::string zero("0");
  77. static const std::string one("1");
  78. // NOTE: d.get(name) throws if not found, d.get(name,default) returns default
  79. _nwid = Utils::hexStrToU64(d.get(ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID).c_str());
  80. if (!_nwid)
  81. throw std::invalid_argument("configuration contains zero network ID");
  82. _timestamp = Utils::hexStrToU64(d.get(ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP).c_str());
  83. _revision = Utils::hexStrToU64(d.get(ZT_NETWORKCONFIG_DICT_KEY_REVISION,"1").c_str()); // older controllers don't send this, so default to 1
  84. memset(_etWhitelist,0,sizeof(_etWhitelist));
  85. std::vector<std::string> ets(Utils::split(d.get(ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES).c_str(),",","",""));
  86. for(std::vector<std::string>::const_iterator et(ets.begin());et!=ets.end();++et) {
  87. unsigned int tmp = Utils::hexStrToUInt(et->c_str()) & 0xffff;
  88. _etWhitelist[tmp >> 3] |= (1 << (tmp & 7));
  89. }
  90. _issuedTo = Address(d.get(ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO));
  91. _multicastLimit = Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT,zero).c_str());
  92. if (_multicastLimit == 0) _multicastLimit = ZT_MULTICAST_DEFAULT_LIMIT;
  93. _allowPassiveBridging = (Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_ALLOW_PASSIVE_BRIDGING,zero).c_str()) != 0);
  94. _private = (Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_PRIVATE,one).c_str()) != 0);
  95. _enableBroadcast = (Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST,one).c_str()) != 0);
  96. _name = d.get(ZT_NETWORKCONFIG_DICT_KEY_NAME);
  97. if (_name.length() > ZT_MAX_NETWORK_SHORT_NAME_LENGTH)
  98. throw std::invalid_argument("network short name too long (max: 255 characters)");
  99. // In dictionary IPs are split into V4 and V6 addresses, but we don't really
  100. // need that so merge them here.
  101. std::string ipAddrs(d.get(ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC,std::string()));
  102. {
  103. std::string v6s(d.get(ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC,std::string()));
  104. if (v6s.length()) {
  105. if (ipAddrs.length())
  106. ipAddrs.push_back(',');
  107. ipAddrs.append(v6s);
  108. }
  109. }
  110. std::vector<std::string> ipAddrsSplit(Utils::split(ipAddrs.c_str(),",","",""));
  111. for(std::vector<std::string>::const_iterator ipstr(ipAddrsSplit.begin());ipstr!=ipAddrsSplit.end();++ipstr) {
  112. InetAddress addr(*ipstr);
  113. switch(addr.ss_family) {
  114. case AF_INET:
  115. if ((!addr.netmaskBits())||(addr.netmaskBits() > 32))
  116. continue;
  117. break;
  118. case AF_INET6:
  119. if ((!addr.netmaskBits())||(addr.netmaskBits() > 128))
  120. continue;
  121. break;
  122. default: // ignore unrecognized address types or junk/empty fields
  123. continue;
  124. }
  125. if (addr.isNetwork())
  126. _localRoutes.push_back(addr);
  127. else _staticIps.push_back(addr);
  128. }
  129. if (_localRoutes.size() > ZT_MAX_ZT_ASSIGNED_ADDRESSES) throw std::invalid_argument("too many ZT-assigned routes");
  130. if (_staticIps.size() > ZT_MAX_ZT_ASSIGNED_ADDRESSES) throw std::invalid_argument("too many ZT-assigned IP addresses");
  131. std::sort(_localRoutes.begin(),_localRoutes.end());
  132. _localRoutes.erase(std::unique(_localRoutes.begin(),_localRoutes.end()),_localRoutes.end());
  133. std::sort(_staticIps.begin(),_staticIps.end());
  134. _staticIps.erase(std::unique(_staticIps.begin(),_staticIps.end()),_staticIps.end());
  135. std::vector<std::string> gatewaysSplit(Utils::split(d.get(ZT_NETWORKCONFIG_DICT_KEY_GATEWAYS,"").c_str(),",","",""));
  136. for(std::vector<std::string>::const_iterator gwstr(gatewaysSplit.begin());gwstr!=gatewaysSplit.end();++gwstr) {
  137. InetAddress gw(*gwstr);
  138. if ((std::find(_gateways.begin(),_gateways.end(),gw) == _gateways.end())&&((gw.ss_family == AF_INET)||(gw.ss_family == AF_INET6)))
  139. _gateways.push_back(gw);
  140. }
  141. std::vector<std::string> activeBridgesSplit(Utils::split(d.get(ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES,"").c_str(),",","",""));
  142. for(std::vector<std::string>::const_iterator a(activeBridgesSplit.begin());a!=activeBridgesSplit.end();++a) {
  143. if (a->length() == ZT_ADDRESS_LENGTH_HEX) { // ignore empty or garbage fields
  144. Address tmp(*a);
  145. if (!tmp.isReserved())
  146. _activeBridges.push_back(tmp);
  147. }
  148. }
  149. std::sort(_activeBridges.begin(),_activeBridges.end());
  150. _activeBridges.erase(std::unique(_activeBridges.begin(),_activeBridges.end()),_activeBridges.end());
  151. std::vector<std::string> relaysSplit(Utils::split(d.get(ZT_NETWORKCONFIG_DICT_KEY_RELAYS,"").c_str(),",","",""));
  152. for(std::vector<std::string>::const_iterator r(relaysSplit.begin());r!=relaysSplit.end();++r) {
  153. std::size_t semi(r->find(';')); // address;ip/port,...
  154. if (semi == ZT_ADDRESS_LENGTH_HEX) {
  155. std::pair<Address,InetAddress> relay(
  156. Address(r->substr(0,semi)),
  157. ((r->length() > (semi + 1)) ? InetAddress(r->substr(semi + 1)) : InetAddress()) );
  158. if ((relay.first)&&(!relay.first.isReserved()))
  159. _relays.push_back(relay);
  160. }
  161. }
  162. std::sort(_relays.begin(),_relays.end());
  163. _relays.erase(std::unique(_relays.begin(),_relays.end()),_relays.end());
  164. _com.fromString(d.get(ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP,std::string()));
  165. }
  166. bool NetworkConfig::operator==(const NetworkConfig &nc) const
  167. {
  168. if (_nwid != nc._nwid) return false;
  169. if (_timestamp != nc._timestamp) return false;
  170. if (memcmp(_etWhitelist,nc._etWhitelist,sizeof(_etWhitelist))) return false;
  171. if (_issuedTo != nc._issuedTo) return false;
  172. if (_multicastLimit != nc._multicastLimit) return false;
  173. if (_allowPassiveBridging != nc._allowPassiveBridging) return false;
  174. if (_private != nc._private) return false;
  175. if (_enableBroadcast != nc._enableBroadcast) return false;
  176. if (_name != nc._name) return false;
  177. if (_localRoutes != nc._localRoutes) return false;
  178. if (_staticIps != nc._staticIps) return false;
  179. if (_gateways != nc._gateways) return false;
  180. if (_activeBridges != nc._activeBridges) return false;
  181. if (_relays != nc._relays) return false;
  182. if (_com != nc._com) return false;
  183. return true;
  184. }
  185. } // namespace ZeroTier