Bit of network config parser cleanup.

This commit is contained in:
Adam Ierymenko 2014-09-05 14:56:11 -07:00
parent 6e1339fedf
commit 3afc629ac5

View file

@ -82,57 +82,60 @@ void NetworkConfig::_fromDictionary(const Dictionary &d)
_nwid = Utils::hexStrToU64(d.get(ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID).c_str()); _nwid = Utils::hexStrToU64(d.get(ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID).c_str());
if (!_nwid) if (!_nwid)
throw std::invalid_argument("configuration contains zero network ID"); throw std::invalid_argument("configuration contains zero network ID");
_timestamp = Utils::hexStrToU64(d.get(ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP).c_str()); _timestamp = Utils::hexStrToU64(d.get(ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP).c_str());
_issuedTo = Address(d.get(ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO)); _issuedTo = Address(d.get(ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO));
_multicastPrefixBits = Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_PREFIX_BITS,zero).c_str()); _multicastPrefixBits = Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_PREFIX_BITS,zero).c_str());
if (!_multicastPrefixBits)
_multicastPrefixBits = ZT_DEFAULT_MULTICAST_PREFIX_BITS;
_multicastDepth = Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_DEPTH,zero).c_str()); _multicastDepth = Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_DEPTH,zero).c_str());
if (!_multicastDepth)
_multicastDepth = ZT_DEFAULT_MULTICAST_DEPTH;
_allowPassiveBridging = (Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_ALLOW_PASSIVE_BRIDGING,zero).c_str()) != 0); _allowPassiveBridging = (Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_ALLOW_PASSIVE_BRIDGING,zero).c_str()) != 0);
_private = (Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_PRIVATE,one).c_str()) != 0); _private = (Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_PRIVATE,one).c_str()) != 0);
_enableBroadcast = (Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST,one).c_str()) != 0); _enableBroadcast = (Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST,one).c_str()) != 0);
_name = d.get(ZT_NETWORKCONFIG_DICT_KEY_NAME); _name = d.get(ZT_NETWORKCONFIG_DICT_KEY_NAME);
_description = d.get(ZT_NETWORKCONFIG_DICT_KEY_DESC,std::string()); _description = d.get(ZT_NETWORKCONFIG_DICT_KEY_DESC,std::string());
if (!_multicastPrefixBits)
_multicastPrefixBits = ZT_DEFAULT_MULTICAST_PREFIX_BITS;
if (!_multicastDepth)
_multicastDepth = ZT_DEFAULT_MULTICAST_DEPTH;
std::string ipAddrs(d.get(ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC,std::string())); std::string ipAddrs(d.get(ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC,std::string()));
{
std::string v6s(d.get(ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC,std::string())); std::string v6s(d.get(ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC,std::string()));
if (v6s.length()) { if (v6s.length()) {
if (ipAddrs.length()) if (ipAddrs.length())
ipAddrs.push_back(','); ipAddrs.push_back(',');
ipAddrs.append(v6s); ipAddrs.append(v6s);
} }
std::vector<std::string> ipAddrs2(Utils::split(ipAddrs.c_str(),",","","")); }
for(std::vector<std::string>::const_iterator ipstr(ipAddrs2.begin());ipstr!=ipAddrs2.end();++ipstr) {
std::vector<std::string> ipAddrsSplit(Utils::split(ipAddrs.c_str(),",","",""));
for(std::vector<std::string>::const_iterator ipstr(ipAddrsSplit.begin());ipstr!=ipAddrsSplit.end();++ipstr) {
InetAddress addr(*ipstr); InetAddress addr(*ipstr);
switch(addr.type()) { switch(addr.type()) {
case InetAddress::TYPE_IPV4: case InetAddress::TYPE_IPV4:
if ((!addr.netmaskBits())||(addr.netmaskBits() > 32)) if ((!addr.netmaskBits())||(addr.netmaskBits() > 32))
throw std::invalid_argument("static IP address fields contain one or more invalid IP/netmask entries"); continue;
break; break;
case InetAddress::TYPE_IPV6: case InetAddress::TYPE_IPV6:
if ((!addr.netmaskBits())||(addr.netmaskBits() > 128)) if ((!addr.netmaskBits())||(addr.netmaskBits() > 128))
throw std::invalid_argument("static IP address fields contain one or more invalid IP/netmask entries"); continue;
break; break;
default: default: // ignore unrecognized address types or junk/empty fields
throw std::invalid_argument("static IP address fields contain one or more invalid IP/netmask entries"); continue;
} }
_staticIps.insert(addr); _staticIps.insert(addr);
} }
std::vector<std::string> ab(Utils::split(d.get(ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES,"").c_str(),",","","")); 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(ab.begin());a!=ab.end();++a) { for(std::vector<std::string>::const_iterator a(activeBridgesSplit.begin());a!=activeBridgesSplit.end();++a) {
if (a->length() == ZT_ADDRESS_LENGTH_HEX) { if (a->length() == ZT_ADDRESS_LENGTH_HEX) { // ignore empty or garbage fields
Address tmp(*a); Address tmp(*a);
if (!tmp.isReserved()) if (!tmp.isReserved())
_activeBridges.insert(tmp); _activeBridges.insert(tmp);
} }
} }
Dictionary mr(d.get(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_RATES,std::string())); Dictionary multicastRateEntries(d.get(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_RATES,std::string()));
for(Dictionary::const_iterator i(mr.begin());i!=mr.end();++i) { for(Dictionary::const_iterator i(multicastRateEntries.begin());i!=multicastRateEntries.end();++i) {
std::vector<std::string> params(Utils::split(i->second.c_str(),",","","")); std::vector<std::string> params(Utils::split(i->second.c_str(),",","",""));
if (params.size() >= 3) if (params.size() >= 3)
_multicastRates[MulticastGroup(i->first)] = MulticastRate(Utils::hexStrToUInt(params[0].c_str()),Utils::hexStrToUInt(params[1].c_str()),Utils::hexStrToUInt(params[2].c_str())); _multicastRates[MulticastGroup(i->first)] = MulticastRate(Utils::hexStrToUInt(params[0].c_str()),Utils::hexStrToUInt(params[1].c_str()),Utils::hexStrToUInt(params[2].c_str()));