More refactoring to remove old Dictionary dependencies.

This commit is contained in:
Adam Ierymenko 2016-04-26 08:20:03 -07:00
parent 246f86dad3
commit 90e1262a8b
5 changed files with 114 additions and 19 deletions

View file

@ -403,10 +403,9 @@ bool IncomingPacket::_doOK(const RuntimeEnvironment *RR,const SharedPtr<Peer> &p
case Packet::VERB_NETWORK_CONFIG_REQUEST: { case Packet::VERB_NETWORK_CONFIG_REQUEST: {
const SharedPtr<Network> nw(RR->node->network(at<uint64_t>(ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST__OK__IDX_NETWORK_ID))); const SharedPtr<Network> nw(RR->node->network(at<uint64_t>(ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST__OK__IDX_NETWORK_ID)));
if ((nw)&&(nw->controller() == peer->address())) { if ((nw)&&(nw->controller() == peer->address())) {
const unsigned int dictlen = at<uint16_t>(ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST__OK__IDX_DICT_LEN); const unsigned int nclen = at<uint16_t>(ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST__OK__IDX_DICT_LEN);
const std::string dict((const char *)field(ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST__OK__IDX_DICT,dictlen),dictlen); if (nclen) {
if (dict.length()) { nw->setConfiguration(field(ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST__OK__IDX_DICT,nclen),nclen,true);
nw->setConfiguration(Dictionary(dict));
TRACE("got network configuration for network %.16llx from %s",(unsigned long long)nw->id(),source().toString().c_str()); TRACE("got network configuration for network %.16llx from %s",(unsigned long long)nw->id(),source().toString().c_str());
} }
} }

View file

@ -65,7 +65,7 @@ Network::Network(const RuntimeEnvironment *renv,uint64_t nwid,void *uptr) :
try { try {
std::string conf(RR->node->dataStoreGet(confn)); std::string conf(RR->node->dataStoreGet(confn));
if (conf.length()) { if (conf.length()) {
setConfiguration(Dictionary(conf),false); this->setConfiguration((const void *)conf.data(),(unsigned int)conf.length(),false);
_lastConfigUpdate = 0; // we still want to re-request a new config from the network _lastConfigUpdate = 0; // we still want to re-request a new config from the network
gotConf = true; gotConf = true;
} }
@ -178,26 +178,40 @@ bool Network::applyConfiguration(const NetworkConfig &conf)
return false; return false;
} }
int Network::setConfiguration(const Dictionary &conf,bool saveToDisk) int Network::setConfiguration(const void *confBytes,unsigned int confLen,bool saveToDisk)
{ {
try { try {
if (!confLen)
return 0;
NetworkConfig newConfig; NetworkConfig newConfig;
newConfig.fromDictionary(conf); // throws if invalid if (reinterpret_cast<const uint8_t *>(confBytes)[0] == ZT_NETWORKCONFIG_V2_MARKER_BYTE) {
// TODO: deserialize new binary format netconf
return 0;
} else {
#ifdef ZT_SUPPORT_OLD_STYLE_NETCONF
newConfig.fromDictionary(reinterpret_cast<const char *>(confBytes),confLen); // throws if invalid
#else
return 0;
#endif
}
{ {
Mutex::Lock _l(_lock); Mutex::Lock _l(_lock);
if (_config == newConfig) if (_config == newConfig)
return 1; // OK config, but duplicate of what we already have return 1; // OK config, but duplicate of what we already have
} }
if (applyConfiguration(newConfig)) { if (applyConfiguration(newConfig)) {
if (saveToDisk) { if (saveToDisk) {
char n[128]; char n[128];
Utils::snprintf(n,sizeof(n),"networks.d/%.16llx.conf",_id); Utils::snprintf(n,sizeof(n),"networks.d/%.16llx.conf",_id);
RR->node->dataStorePut(n,conf.toString(),true); RR->node->dataStorePut(n,confBytes,confLen,true);
} }
return 2; // OK and configuration has changed return 2; // OK and configuration has changed
} }
} catch ( ... ) { } catch ( ... ) {
TRACE("ignored invalid configuration for network %.16llx (dictionary decode failed)",(unsigned long long)_id); TRACE("ignored invalid configuration for network %.16llx",(unsigned long long)_id);
} }
return 0; return 0;
} }
@ -211,9 +225,10 @@ void Network::requestConfiguration()
if (RR->localNetworkController) { if (RR->localNetworkController) {
Dictionary newconf; Dictionary newconf;
switch(RR->localNetworkController->doNetworkConfigRequest(InetAddress(),RR->identity,RR->identity,_id,Dictionary(),newconf)) { switch(RR->localNetworkController->doNetworkConfigRequest(InetAddress(),RR->identity,RR->identity,_id,Dictionary(),newconf)) {
case NetworkController::NETCONF_QUERY_OK: case NetworkController::NETCONF_QUERY_OK: {
this->setConfiguration(newconf,true); std::string tmp(newconf.toString());
return; this->setConfiguration((const void *)tmp.data(),(unsigned int)tmp.length(),true);
} return;
case NetworkController::NETCONF_QUERY_OBJECT_NOT_FOUND: case NetworkController::NETCONF_QUERY_OBJECT_NOT_FOUND:
this->setNotFound(); this->setNotFound();
return; return;

View file

@ -151,14 +151,12 @@ public:
/** /**
* Set or update this network's configuration * Set or update this network's configuration
* *
* This decodes a network configuration in key=value dictionary form, * @param confBytes Network configuration in old-style Dictionary or new-style serialized format
* applies it if valid, and persists it to disk if saveToDisk is true. * @param confLen Length of network configuration in bytes
*
* @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 0 -- rejected, 1 -- accepted but not new, 2 -- accepted new config * @return 0 -- rejected, 1 -- accepted but not new, 2 -- accepted new config
*/ */
int setConfiguration(const Dictionary &conf,bool saveToDisk = true); int setConfiguration(const void *confBytes,unsigned int confLen,bool saveToDisk);
/** /**
* Set netconf failure to 'access denied' -- called in IncomingPacket when controller reports this * Set netconf failure to 'access denied' -- called in IncomingPacket when controller reports this

View file

@ -80,8 +80,10 @@ NetworkConfig NetworkConfig::createTestNetworkConfig(const Address &self)
#ifdef ZT_SUPPORT_OLD_STYLE_NETCONF #ifdef ZT_SUPPORT_OLD_STYLE_NETCONF
void NetworkConfig::fromDictionary(const Dictionary &d) void NetworkConfig::fromDictionary(const char *ds,unsigned int dslen)
{ {
Dictionary d(ds,dslen);
static const std::string zero("0"); static const std::string zero("0");
static const std::string one("1"); static const std::string one("1");

View file

@ -39,6 +39,13 @@
#include "Address.hpp" #include "Address.hpp"
#include "CertificateOfMembership.hpp" #include "CertificateOfMembership.hpp"
/**
* First byte of V2 binary-serialized network configs
*
* This will never begin a Dictionary, so it serves to distinguish.
*/
#define ZT_NETWORKCONFIG_V2_MARKER_BYTE 0x00
namespace ZeroTier { namespace ZeroTier {
#ifdef ZT_SUPPORT_OLD_STYLE_NETCONF #ifdef ZT_SUPPORT_OLD_STYLE_NETCONF
@ -147,24 +154,76 @@ public:
/** /**
* Parse an old-style dictionary and fill in structure * Parse an old-style dictionary and fill in structure
* *
* @param ds String-serialized dictionary
* @param dslen Length of dictionary in bytes
* @throws std::invalid_argument Invalid dictionary * @throws std::invalid_argument Invalid dictionary
*/ */
void fromDictionary(const Dictionary &d); void fromDictionary(const char *ds,unsigned int dslen);
#endif #endif
/**
* @return Network ID that this config applies to
*/
inline uint64_t networkId() const throw() { return _nwid; } inline uint64_t networkId() const throw() { return _nwid; }
/**
* @return Timestamp of this config (controller-side)
*/
inline uint64_t timestamp() const throw() { return _timestamp; } inline uint64_t timestamp() const throw() { return _timestamp; }
/**
* @return Config revision number
*/
inline uint64_t revision() const throw() { return _revision; } inline uint64_t revision() const throw() { return _revision; }
/**
* @return ZeroTier address of device to which this config was issued
*/
inline const Address &issuedTo() const throw() { return _issuedTo; } inline const Address &issuedTo() const throw() { return _issuedTo; }
/**
* @return Maximum number of multicast recipients or 0 to disable multicast
*/
inline unsigned int multicastLimit() const throw() { return _multicastLimit; } inline unsigned int multicastLimit() const throw() { return _multicastLimit; }
/**
* @return True if passive bridging is allowed (experimental)
*/
inline bool allowPassiveBridging() const throw() { return _allowPassiveBridging; } inline bool allowPassiveBridging() const throw() { return _allowPassiveBridging; }
/**
* @return True if broadcast (ff:ff:ff:ff:ff:ff) address should work on this network
*/
inline bool enableBroadcast() const throw() { return _enableBroadcast; } inline bool enableBroadcast() const throw() { return _enableBroadcast; }
/**
* @return Type of network (currently public or private)
*/
inline ZT_VirtualNetworkType type() const throw() { return _type; } inline ZT_VirtualNetworkType type() const throw() { return _type; }
/**
* @return Network type is public (no access control)
*/
inline bool isPublic() const throw() { return (_type == ZT_NETWORK_TYPE_PUBLIC); } inline bool isPublic() const throw() { return (_type == ZT_NETWORK_TYPE_PUBLIC); }
/**
* @return Network type is private (certificate access control)
*/
inline bool isPrivate() const throw() { return (_type == ZT_NETWORK_TYPE_PRIVATE); } inline bool isPrivate() const throw() { return (_type == ZT_NETWORK_TYPE_PRIVATE); }
/**
* @return Short network name
*/
inline const char *name() const throw() { return _name; } inline const char *name() const throw() { return _name; }
/**
* @return Network certificate of membership or NULL COM object if none (public network)
*/
inline const CertificateOfMembership &com() const throw() { return _com; } inline const CertificateOfMembership &com() const throw() { return _com; }
/**
* @return Network/netmask routes that are considered local to this virtual LAN interface
*/
inline std::vector<InetAddress> localRoutes() const inline std::vector<InetAddress> localRoutes() const
{ {
std::vector<InetAddress> r; std::vector<InetAddress> r;
@ -173,6 +232,9 @@ public:
return r; return r;
} }
/**
* @return ZeroTier-managed static IPs assigned to this device on this network
*/
inline std::vector<InetAddress> staticIps() const inline std::vector<InetAddress> staticIps() const
{ {
std::vector<InetAddress> r; std::vector<InetAddress> r;
@ -181,6 +243,9 @@ public:
return r; return r;
} }
/**
* @return ZeroTier-managed default gateways (for full tunnel) available on this network
*/
inline std::vector<InetAddress> gateways() const inline std::vector<InetAddress> gateways() const
{ {
std::vector<InetAddress> r; std::vector<InetAddress> r;
@ -189,6 +254,9 @@ public:
return r; return r;
} }
/**
* @return ZeroTier addresses of devices on this network designated as active bridges
*/
inline std::vector<Address> activeBridges() const inline std::vector<Address> activeBridges() const
{ {
std::vector<Address> r; std::vector<Address> r;
@ -197,6 +265,9 @@ public:
return r; return r;
} }
/**
* @return Network-preferred relays for this network (if none, only roots will be used)
*/
inline std::vector<ZT_VirtualNetworkStaticDevice> relays() const inline std::vector<ZT_VirtualNetworkStaticDevice> relays() const
{ {
std::vector<ZT_VirtualNetworkStaticDevice> r; std::vector<ZT_VirtualNetworkStaticDevice> r;
@ -207,7 +278,14 @@ public:
return r; return r;
} }
/**
* @return Static device at index [i] (warning: no bounds checking! see staticDeviceCount() for count)
*/
const ZT_VirtualNetworkStaticDevice &staticDevice(unsigned int i) const { return _static[i]; } const ZT_VirtualNetworkStaticDevice &staticDevice(unsigned int i) const { return _static[i]; }
/**
* @return Number of static devices defined in this network config
*/
unsigned int staticDeviceCount() const { return _staticCount; } unsigned int staticDeviceCount() const { return _staticCount; }
/** /**
@ -225,6 +303,9 @@ public:
return false; return false;
} }
/**
* @return True if this network config is non-NULL
*/
inline operator bool() const throw() { return (_nwid != 0); } inline operator bool() const throw() { return (_nwid != 0); }
inline bool operator==(const NetworkConfig &nc) const { return (memcmp(this,&nc,sizeof(NetworkConfig)) == 0); } inline bool operator==(const NetworkConfig &nc) const { return (memcmp(this,&nc,sizeof(NetworkConfig)) == 0); }