Add test network support to Network.

This commit is contained in:
Adam Ierymenko 2014-10-03 16:14:34 -07:00
parent 13fc20b0ee
commit aad344bb84
2 changed files with 88 additions and 64 deletions

View File

@ -140,7 +140,7 @@ bool Network::updateMulticastGroups()
} else return false; } else return false;
} }
bool Network::setConfiguration(const Dictionary &conf,bool saveToDisk) bool Network::applyConfiguration(const SharedPtr<NetworkConfig> &conf)
{ {
Mutex::Lock _l(_lock); Mutex::Lock _l(_lock);
@ -148,26 +148,16 @@ bool Network::setConfiguration(const Dictionary &conf,bool saveToDisk)
return false; return false;
try { try {
SharedPtr<NetworkConfig> newConfig(new NetworkConfig(conf)); // throws if invalid if ((conf->networkId() == _id)&&(conf->issuedTo() == RR->identity.address())) {
if ((newConfig->networkId() == _id)&&(newConfig->issuedTo() == RR->identity.address())) {
std::set<InetAddress> oldStaticIps; std::set<InetAddress> oldStaticIps;
if (_config) if (_config)
oldStaticIps = _config->staticIps(); oldStaticIps = _config->staticIps();
_config = newConfig; _config = conf;
_lastConfigUpdate = Utils::now(); _lastConfigUpdate = Utils::now();
_netconfFailure = NETCONF_FAILURE_NONE; _netconfFailure = NETCONF_FAILURE_NONE;
if (saveToDisk) {
std::string confPath(RR->homePath + ZT_PATH_SEPARATOR_S + "networks.d" + ZT_PATH_SEPARATOR_S + idString() + ".conf");
if (!Utils::writeFile(confPath.c_str(),conf.toString())) {
LOG("error: unable to write network configuration file at: %s",confPath.c_str());
} else {
Utils::lockDownFile(confPath.c_str(),false);
}
}
EthernetTap *t = _tap; EthernetTap *t = _tap;
if (t) { if (t) {
char fname[1024]; char fname[1024];
@ -224,8 +214,32 @@ bool Network::setConfiguration(const Dictionary &conf,bool saveToDisk)
return false; return false;
} }
bool Network::setConfiguration(const Dictionary &conf,bool saveToDisk)
{
try {
SharedPtr<NetworkConfig> newConfig(new NetworkConfig(conf)); // throws if invalid
if (applyConfiguration(newConfig)) {
if (saveToDisk) {
std::string confPath(RR->homePath + ZT_PATH_SEPARATOR_S + "networks.d" + ZT_PATH_SEPARATOR_S + idString() + ".conf");
if (!Utils::writeFile(confPath.c_str(),conf.toString())) {
LOG("error: unable to write network configuration file at: %s",confPath.c_str());
} else {
Utils::lockDownFile(confPath.c_str(),false);
}
}
return true;
}
} catch ( ... ) {
LOG("ignored invalid configuration for network %.16llx (dictionary decode failed)",(unsigned long long)_id);
}
return false;
}
void Network::requestConfiguration() void Network::requestConfiguration()
{ {
if (_id == ZT_TEST_NETWORK_ID) // pseudo-network-ID, no netconf master
return;
if (controller() == RR->identity.address()) { if (controller() == RR->identity.address()) {
// netconf master cannot be a member of its own nets // netconf master cannot be a member of its own nets
LOG("unable to request network configuration for network %.16llx: I am the network master, cannot query self",(unsigned long long)_id); LOG("unable to request network configuration for network %.16llx: I am the network master, cannot query self",(unsigned long long)_id);
@ -488,6 +502,13 @@ void Network::_restoreState()
std::string confPath(RR->homePath + ZT_PATH_SEPARATOR_S + "networks.d" + ZT_PATH_SEPARATOR_S + idstr + ".conf"); std::string confPath(RR->homePath + ZT_PATH_SEPARATOR_S + "networks.d" + ZT_PATH_SEPARATOR_S + idstr + ".conf");
std::string mcdbPath(RR->homePath + ZT_PATH_SEPARATOR_S + "networks.d" + ZT_PATH_SEPARATOR_S + idstr + ".mcerts"); std::string mcdbPath(RR->homePath + ZT_PATH_SEPARATOR_S + "networks.d" + ZT_PATH_SEPARATOR_S + idstr + ".mcerts");
if (_id == ZT_TEST_NETWORK_ID) {
applyConfiguration(NetworkConfig::createTestNetworkConfig(RR->identity.address()));
// "Touch" path to this ID to remember test network membership
FILE *tmp = fopen(confPath.c_str(),"w");
if (tmp) fclose(tmp);
} else {
// Read configuration file containing last config from netconf master // Read configuration file containing last config from netconf master
{ {
std::string confs; std::string confs;
@ -497,18 +518,17 @@ void Network::_restoreState()
setConfiguration(Dictionary(confs),false); setConfiguration(Dictionary(confs),false);
} catch ( ... ) {} // ignore invalid config on disk, we will re-request from netconf master } catch ( ... ) {} // ignore invalid config on disk, we will re-request from netconf master
} else { } else {
// If the conf file isn't present, "touch" it so we'll remember // "Touch" path to remember membership in lieu of real config from netconf master
// the existence of this network.
FILE *tmp = fopen(confPath.c_str(),"w"); FILE *tmp = fopen(confPath.c_str(),"w");
if (tmp) if (tmp) fclose(tmp);
fclose(tmp); }
} }
} }
// Read most recent membership cert dump { // Read most recent membership cert dump if there is one
Mutex::Lock _l(_lock);
if ((_config)&&(!_config->isPublic())&&(Utils::fileExists(mcdbPath.c_str()))) { if ((_config)&&(!_config->isPublic())&&(Utils::fileExists(mcdbPath.c_str()))) {
CertificateOfMembership com; CertificateOfMembership com;
Mutex::Lock _l(_lock);
_membershipCertificates.clear(); _membershipCertificates.clear();
@ -544,6 +564,7 @@ void Network::_restoreState()
} }
} }
} }
}
void Network::_dumpMembershipCerts() void Network::_dumpMembershipCerts()
{ {

View File

@ -173,20 +173,23 @@ public:
return (_myMulticastGroups.count(mg) > 0); return (_myMulticastGroups.count(mg) > 0);
} }
/**
* Apply a NetworkConfig to this network
*
* @param conf Configuration in NetworkConfig form
* @return True if configuration was accepted
*/
bool applyConfiguration(const SharedPtr<NetworkConfig> &conf);
/** /**
* Set or update this network's configuration * Set or update this network's configuration
* *
* This is called in IncomingPacket when an update comes over the wire, or * This decodes a network configuration in key=value dictionary form,
* internally when an old config is reloaded from disk. * applies it if valid, and persists it to disk if saveToDisk is true.
*
* This also cancels any netconf failure flags.
*
* The network can't accept configuration when in INITIALIZATION state,
* and so in that state this will just return false.
* *
* @param conf Configuration in key/value dictionary form * @param conf Configuration in key/value dictionary form
* @param saveToDisk IF true (default), write config to disk * @param saveToDisk IF true (default), write config to disk
* @return True if configuration was accepted, false if still initializing or config was not valid * @return True if configuration was accepted
*/ */
bool setConfiguration(const Dictionary &conf,bool saveToDisk = true); bool setConfiguration(const Dictionary &conf,bool saveToDisk = true);