2013-10-18 13:48:02 +00:00
|
|
|
/*
|
2015-02-17 21:11:34 +00:00
|
|
|
* ZeroTier One - Network Virtualization Everywhere
|
|
|
|
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
2013-10-18 13:48:02 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* --
|
|
|
|
*
|
|
|
|
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
|
|
|
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
*
|
|
|
|
* If you would like to embed ZeroTier into a commercial application or
|
|
|
|
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
|
|
|
* LLC. Start here: http://www.zerotier.com/
|
|
|
|
*/
|
|
|
|
|
2014-10-07 21:34:00 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2013-10-18 13:48:02 +00:00
|
|
|
#include "NetworkConfig.hpp"
|
2013-10-18 15:01:41 +00:00
|
|
|
#include "Utils.hpp"
|
2013-10-18 13:48:02 +00:00
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
2014-10-03 21:49:11 +00:00
|
|
|
SharedPtr<NetworkConfig> NetworkConfig::createTestNetworkConfig(const Address &self)
|
|
|
|
{
|
|
|
|
SharedPtr<NetworkConfig> nc(new NetworkConfig());
|
|
|
|
|
|
|
|
memset(nc->_etWhitelist,0,sizeof(nc->_etWhitelist));
|
|
|
|
nc->_etWhitelist[0] |= 1; // allow all
|
|
|
|
nc->_nwid = ZT_TEST_NETWORK_ID;
|
2015-04-08 22:26:45 +00:00
|
|
|
nc->_timestamp = 1;
|
2015-03-18 23:10:48 +00:00
|
|
|
nc->_revision = 1;
|
2014-10-03 21:49:11 +00:00
|
|
|
nc->_issuedTo = self;
|
|
|
|
nc->_multicastLimit = ZT_MULTICAST_DEFAULT_LIMIT;
|
|
|
|
nc->_allowPassiveBridging = false;
|
|
|
|
nc->_private = false;
|
|
|
|
nc->_enableBroadcast = true;
|
|
|
|
nc->_name = "ZT_TEST_NETWORK";
|
|
|
|
nc->_description = "Built-in dummy test network";
|
|
|
|
|
2014-10-07 21:34:00 +00:00
|
|
|
// Make up a V4 IP from 'self' in the 10.0.0.0/8 range -- no
|
|
|
|
// guarantee of uniqueness but collisions are unlikely.
|
|
|
|
uint32_t ip = (uint32_t)((self.toInt() & 0x00ffffff) | 0x0a000000); // 10.x.x.x
|
|
|
|
if ((ip & 0x000000ff) == 0x000000ff) ip ^= 0x00000001; // but not ending in .255
|
|
|
|
if ((ip & 0x000000ff) == 0x00000000) ip ^= 0x00000001; // or .0
|
|
|
|
nc->_staticIps.push_back(InetAddress(Utils::hton(ip),8));
|
|
|
|
|
2014-10-03 21:49:11 +00:00
|
|
|
return nc;
|
|
|
|
}
|
|
|
|
|
2014-10-04 20:15:02 +00:00
|
|
|
std::vector<unsigned int> NetworkConfig::allowedEtherTypes() const
|
2013-10-18 13:48:02 +00:00
|
|
|
{
|
2014-10-04 20:15:02 +00:00
|
|
|
std::vector<unsigned int> ets;
|
|
|
|
if ((_etWhitelist[0] & 1) != 0) {
|
|
|
|
ets.push_back(0);
|
|
|
|
} else {
|
|
|
|
for(unsigned int i=0;i<sizeof(_etWhitelist);++i) {
|
|
|
|
if (_etWhitelist[i]) {
|
|
|
|
unsigned char b = _etWhitelist[i];
|
|
|
|
unsigned int et = i * 8;
|
|
|
|
while (b) {
|
|
|
|
if ((b & 1))
|
|
|
|
ets.push_back(et);
|
|
|
|
b >>= 1;
|
|
|
|
++et;
|
|
|
|
}
|
2013-10-18 13:48:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ets;
|
|
|
|
}
|
|
|
|
|
2013-10-18 15:01:41 +00:00
|
|
|
void NetworkConfig::_fromDictionary(const Dictionary &d)
|
|
|
|
{
|
2014-05-23 22:13:34 +00:00
|
|
|
static const std::string zero("0");
|
|
|
|
static const std::string one("1");
|
|
|
|
|
2013-10-18 15:01:41 +00:00
|
|
|
// NOTE: d.get(name) throws if not found, d.get(name,default) returns default
|
|
|
|
|
2015-01-06 01:47:59 +00:00
|
|
|
_nwid = Utils::hexStrToU64(d.get(ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID).c_str());
|
|
|
|
if (!_nwid)
|
|
|
|
throw std::invalid_argument("configuration contains zero network ID");
|
|
|
|
|
|
|
|
_timestamp = Utils::hexStrToU64(d.get(ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP).c_str());
|
2015-04-15 22:12:09 +00:00
|
|
|
_revision = Utils::hexStrToU64(d.get(ZT_NETWORKCONFIG_DICT_KEY_REVISION,"1").c_str()); // older controllers don't send this, so default to 1
|
2015-01-06 01:47:59 +00:00
|
|
|
|
2013-10-18 16:01:48 +00:00
|
|
|
memset(_etWhitelist,0,sizeof(_etWhitelist));
|
|
|
|
std::vector<std::string> ets(Utils::split(d.get(ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES).c_str(),",","",""));
|
|
|
|
for(std::vector<std::string>::const_iterator et(ets.begin());et!=ets.end();++et) {
|
|
|
|
unsigned int tmp = Utils::hexStrToUInt(et->c_str()) & 0xffff;
|
|
|
|
_etWhitelist[tmp >> 3] |= (1 << (tmp & 7));
|
|
|
|
}
|
|
|
|
|
2013-10-18 15:01:41 +00:00
|
|
|
_issuedTo = Address(d.get(ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO));
|
2014-09-24 16:01:58 +00:00
|
|
|
_multicastLimit = Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT,zero).c_str());
|
2014-09-25 22:57:43 +00:00
|
|
|
if (_multicastLimit == 0) _multicastLimit = ZT_MULTICAST_DEFAULT_LIMIT;
|
2014-06-14 20:24:19 +00:00
|
|
|
_allowPassiveBridging = (Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_ALLOW_PASSIVE_BRIDGING,zero).c_str()) != 0);
|
2014-05-23 22:15:44 +00:00
|
|
|
_private = (Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_PRIVATE,one).c_str()) != 0);
|
2014-05-23 22:13:34 +00:00
|
|
|
_enableBroadcast = (Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST,one).c_str()) != 0);
|
2013-10-18 15:01:41 +00:00
|
|
|
_name = d.get(ZT_NETWORKCONFIG_DICT_KEY_NAME);
|
2015-04-06 23:52:52 +00:00
|
|
|
if (_name.length() > ZT1_MAX_NETWORK_SHORT_NAME_LENGTH)
|
|
|
|
throw std::invalid_argument("network short name too long (max: 255 characters)");
|
2013-10-18 15:01:41 +00:00
|
|
|
_description = d.get(ZT_NETWORKCONFIG_DICT_KEY_DESC,std::string());
|
|
|
|
|
2014-09-24 16:01:58 +00:00
|
|
|
// In dictionary IPs are split into V4 and V6 addresses, but we don't really
|
|
|
|
// need that so merge them here.
|
2013-10-18 15:01:41 +00:00
|
|
|
std::string ipAddrs(d.get(ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC,std::string()));
|
2014-09-05 21:56:11 +00:00
|
|
|
{
|
|
|
|
std::string v6s(d.get(ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC,std::string()));
|
|
|
|
if (v6s.length()) {
|
|
|
|
if (ipAddrs.length())
|
|
|
|
ipAddrs.push_back(',');
|
|
|
|
ipAddrs.append(v6s);
|
|
|
|
}
|
2013-10-18 15:01:41 +00:00
|
|
|
}
|
2014-09-05 21:56:11 +00:00
|
|
|
|
|
|
|
std::vector<std::string> ipAddrsSplit(Utils::split(ipAddrs.c_str(),",","",""));
|
|
|
|
for(std::vector<std::string>::const_iterator ipstr(ipAddrsSplit.begin());ipstr!=ipAddrsSplit.end();++ipstr) {
|
2013-10-18 15:01:41 +00:00
|
|
|
InetAddress addr(*ipstr);
|
2015-04-08 22:42:23 +00:00
|
|
|
switch(addr.ss_family) {
|
|
|
|
case AF_INET:
|
2013-10-18 15:01:41 +00:00
|
|
|
if ((!addr.netmaskBits())||(addr.netmaskBits() > 32))
|
2014-09-05 21:56:11 +00:00
|
|
|
continue;
|
2015-06-29 17:21:28 +00:00
|
|
|
else if (addr.isNetwork()) {
|
|
|
|
// TODO: add route to network -- this is a route without an IP assignment
|
|
|
|
continue;
|
|
|
|
}
|
2013-10-18 15:01:41 +00:00
|
|
|
break;
|
2015-04-08 22:42:23 +00:00
|
|
|
case AF_INET6:
|
2013-10-18 15:01:41 +00:00
|
|
|
if ((!addr.netmaskBits())||(addr.netmaskBits() > 128))
|
2014-09-05 21:56:11 +00:00
|
|
|
continue;
|
2015-06-29 17:21:28 +00:00
|
|
|
else if (addr.isNetwork()) {
|
|
|
|
// TODO: add route to network -- this is a route without an IP assignment
|
|
|
|
continue;
|
|
|
|
}
|
2013-10-18 15:01:41 +00:00
|
|
|
break;
|
2014-09-05 21:56:11 +00:00
|
|
|
default: // ignore unrecognized address types or junk/empty fields
|
|
|
|
continue;
|
2013-10-18 15:01:41 +00:00
|
|
|
}
|
2014-10-04 20:15:02 +00:00
|
|
|
_staticIps.push_back(addr);
|
2013-10-18 15:01:41 +00:00
|
|
|
}
|
2015-04-06 23:52:52 +00:00
|
|
|
if (_staticIps.size() > ZT1_MAX_ZT_ASSIGNED_ADDRESSES)
|
2015-06-29 17:21:28 +00:00
|
|
|
throw std::invalid_argument("too many ZT-assigned IP addresses or routes");
|
2014-10-04 20:15:02 +00:00
|
|
|
std::sort(_staticIps.begin(),_staticIps.end());
|
2015-06-29 17:21:28 +00:00
|
|
|
_staticIps.erase(std::unique(_staticIps.begin(),_staticIps.end()),_staticIps.end());
|
2013-10-18 15:01:41 +00:00
|
|
|
|
2015-06-13 11:06:49 +00:00
|
|
|
std::vector<std::string> gatewaysSplit(Utils::split(d.get(ZT_NETWORKCONFIG_DICT_KEY_GATEWAYS,"").c_str(),",","",""));
|
|
|
|
for(std::vector<std::string>::const_iterator gwstr(gatewaysSplit.begin());gwstr!=gatewaysSplit.end();++gwstr) {
|
|
|
|
InetAddress gw(*gwstr);
|
|
|
|
if ((std::find(_gateways.begin(),_gateways.end(),gw) == _gateways.end())&&((gw.ss_family == AF_INET)||(gw.ss_family == AF_INET6)))
|
|
|
|
_gateways.push_back(gw);
|
|
|
|
}
|
|
|
|
|
2014-09-05 21:56:11 +00:00
|
|
|
std::vector<std::string> activeBridgesSplit(Utils::split(d.get(ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES,"").c_str(),",","",""));
|
|
|
|
for(std::vector<std::string>::const_iterator a(activeBridgesSplit.begin());a!=activeBridgesSplit.end();++a) {
|
|
|
|
if (a->length() == ZT_ADDRESS_LENGTH_HEX) { // ignore empty or garbage fields
|
2014-06-10 22:47:20 +00:00
|
|
|
Address tmp(*a);
|
|
|
|
if (!tmp.isReserved())
|
2014-10-04 20:15:02 +00:00
|
|
|
_activeBridges.push_back(tmp);
|
2014-06-10 22:47:20 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-04 20:15:02 +00:00
|
|
|
std::sort(_activeBridges.begin(),_activeBridges.end());
|
2015-06-29 17:21:28 +00:00
|
|
|
_activeBridges.erase(std::unique(_activeBridges.begin(),_activeBridges.end()),_activeBridges.end());
|
2014-06-10 22:47:20 +00:00
|
|
|
|
2015-04-21 00:14:16 +00:00
|
|
|
std::vector<std::string> relaysSplit(Utils::split(d.get(ZT_NETWORKCONFIG_DICT_KEY_RELAYS,"").c_str(),",","",""));
|
|
|
|
for(std::vector<std::string>::const_iterator r(relaysSplit.begin());r!=relaysSplit.end();++r) {
|
|
|
|
std::size_t semi(r->find(';')); // address;ip/port,...
|
2015-06-02 02:05:27 +00:00
|
|
|
if (semi == ZT_ADDRESS_LENGTH_HEX) {
|
|
|
|
std::pair<Address,InetAddress> relay(
|
|
|
|
Address(r->substr(0,semi)),
|
|
|
|
((r->length() > (semi + 1)) ? InetAddress(r->substr(semi + 1)) : InetAddress()) );
|
|
|
|
if ((relay.first)&&(!relay.first.isReserved()))
|
2015-04-21 00:14:16 +00:00
|
|
|
_relays.push_back(relay);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::sort(_relays.begin(),_relays.end());
|
2015-06-29 17:21:28 +00:00
|
|
|
_relays.erase(std::unique(_relays.begin(),_relays.end()),_relays.end());
|
2015-04-21 00:14:16 +00:00
|
|
|
|
2013-10-24 20:57:26 +00:00
|
|
|
_com.fromString(d.get(ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP,std::string()));
|
2013-10-18 15:01:41 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 01:47:59 +00:00
|
|
|
bool NetworkConfig::operator==(const NetworkConfig &nc) const
|
|
|
|
{
|
|
|
|
if (_nwid != nc._nwid) return false;
|
|
|
|
if (_timestamp != nc._timestamp) return false;
|
|
|
|
if (memcmp(_etWhitelist,nc._etWhitelist,sizeof(_etWhitelist))) return false;
|
|
|
|
if (_issuedTo != nc._issuedTo) return false;
|
|
|
|
if (_multicastLimit != nc._multicastLimit) return false;
|
|
|
|
if (_allowPassiveBridging != nc._allowPassiveBridging) return false;
|
|
|
|
if (_private != nc._private) return false;
|
|
|
|
if (_enableBroadcast != nc._enableBroadcast) return false;
|
|
|
|
if (_name != nc._name) return false;
|
|
|
|
if (_description != nc._description) return false;
|
|
|
|
if (_staticIps != nc._staticIps) return false;
|
2015-06-13 11:13:19 +00:00
|
|
|
if (_gateways != nc._gateways) return false;
|
2015-01-06 01:47:59 +00:00
|
|
|
if (_activeBridges != nc._activeBridges) return false;
|
2015-06-13 11:13:19 +00:00
|
|
|
if (_relays != nc._relays) return false;
|
2015-01-06 01:47:59 +00:00
|
|
|
if (_com != nc._com) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-18 13:48:02 +00:00
|
|
|
} // namespace ZeroTier
|
2013-10-18 15:01:41 +00:00
|
|
|
|