NetworkConfig.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #include "NetworkConfig.hpp"
  28. #include "Utils.hpp"
  29. namespace ZeroTier {
  30. // This is fast enough for things like Apple's mDNS spam, so it should serve
  31. // as a good default for your average network. It's 64 bytes per second, with
  32. // a starting and max balance of 64k.
  33. const NetworkConfig::MulticastRate NetworkConfig::DEFAULT_MULTICAST_RATE(65535,65535,64);
  34. std::set<unsigned int> NetworkConfig::allowedEtherTypes() const
  35. {
  36. std::set<unsigned int> ets;
  37. for(unsigned int i=0;i<sizeof(_etWhitelist);++i) {
  38. if (_etWhitelist[i]) {
  39. unsigned char b = _etWhitelist[i];
  40. unsigned int et = i * 8;
  41. while (b) {
  42. if ((b & 1))
  43. ets.insert(et);
  44. b >>= 1;
  45. ++et;
  46. }
  47. }
  48. }
  49. return ets;
  50. }
  51. const NetworkConfig::MulticastRate &NetworkConfig::multicastRate(const MulticastGroup &mg) const
  52. throw()
  53. {
  54. std::map<MulticastGroup,MulticastRate>::const_iterator r(_multicastRates.find(mg));
  55. if (r == _multicastRates.end()) {
  56. r = _multicastRates.find(MulticastGroup()); // zero MG signifies network's default rate
  57. if (r == _multicastRates.end())
  58. return DEFAULT_MULTICAST_RATE; // neither specific nor default found in network config
  59. }
  60. return r->second;
  61. }
  62. static const std::string _zero("0");
  63. void NetworkConfig::_fromDictionary(const Dictionary &d)
  64. {
  65. // NOTE: d.get(name) throws if not found, d.get(name,default) returns default
  66. _nwid = Utils::hexStrToU64(d.get(ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID).c_str());
  67. if (!_nwid)
  68. throw std::invalid_argument("configuration contains zero network ID");
  69. _timestamp = Utils::hexStrToU64(d.get(ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP).c_str());
  70. _issuedTo = Address(d.get(ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO));
  71. _multicastPrefixBits = Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_PREFIX_BITS,_zero).c_str());
  72. _multicastDepth = Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_DEPTH,_zero).c_str());
  73. _arpCacheTtl = Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_ARP_CACHE_TTL,_zero).c_str());
  74. _ndpCacheTtl = Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_NDP_CACHE_TTL,_zero).c_str());
  75. _emulateArp = (Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_EMULATE_ARP,_zero).c_str()) != 0);
  76. _emulateNdp = (Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_EMULATE_NDP,_zero).c_str()) != 0);
  77. _isOpen = (Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_IS_OPEN,_zero).c_str()) != 0);
  78. _name = d.get(ZT_NETWORKCONFIG_DICT_KEY_NAME);
  79. _description = d.get(ZT_NETWORKCONFIG_DICT_KEY_DESC,std::string());
  80. std::string ipAddrs(d.get(ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC,std::string()));
  81. std::string v6s(d.get(ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC,std::string()));
  82. if (v6s.length()) {
  83. if (ipAddrs.length())
  84. ipAddrs.push_back(',');
  85. ipAddrs.append(v6s);
  86. }
  87. std::vector<std::string> ipAddrs2(Utils::split(ipAddrs.c_str(),",","",""));
  88. for(std::vector<std::string>::const_iterator ipstr(ipAddrs2.begin());ipstr!=ipAddrs2.end();++ipstr) {
  89. InetAddress addr(*ipstr);
  90. switch(addr.type()) {
  91. case InetAddress::TYPE_IPV4:
  92. if ((!addr.netmaskBits())||(addr.netmaskBits() > 32))
  93. throw std::invalid_argument("static IP address fields contain one or more invalid IP/netmask entries");
  94. break;
  95. case InetAddress::TYPE_IPV6:
  96. if ((!addr.netmaskBits())||(addr.netmaskBits() > 128))
  97. throw std::invalid_argument("static IP address fields contain one or more invalid IP/netmask entries");
  98. break;
  99. default:
  100. throw std::invalid_argument("static IP address fields contain one or more invalid IP/netmask entries");
  101. }
  102. _staticIps.insert(addr);
  103. }
  104. Dictionary mr(d.get(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_RATES,std::string()));
  105. for(Dictionary::const_iterator i(mr.begin());i!=mr.end();++i) {
  106. std::vector<std::string> params(Utils::split(i->second.c_str(),",","",""));
  107. if (params.size() >= 3)
  108. _multicastRates[MulticastGroup(i->first)] = MulticastRate(Utils::hexStrToUInt(params[0].c_str()),Utils::hexStrToUInt(params[1].c_str()),Utils::hexStrToUInt(params[2].c_str()));
  109. }
  110. }
  111. } // namespace ZeroTier