Simplify a bunch of NetworkConfig stuff by eliminating accessors, also makes network controller easier to refactor.

This commit is contained in:
Adam Ierymenko 2016-05-06 16:13:11 -07:00
parent 529515d1d1
commit 8b9519f0af
12 changed files with 379 additions and 339 deletions

View File

@ -353,7 +353,7 @@ SqliteNetworkController::~SqliteNetworkController()
} }
} }
NetworkController::ResultCode SqliteNetworkController::doNetworkConfigRequest(const InetAddress &fromAddr,const Identity &signingId,const Identity &identity,uint64_t nwid,const Dictionary &metaData,Dictionary &netconf) NetworkController::ResultCode SqliteNetworkController::doNetworkConfigRequest(const InetAddress &fromAddr,const Identity &signingId,const Identity &identity,uint64_t nwid,const NetworkConfigRequestMetaData &metaData,Buffer<8194> &netconf)
{ {
Mutex::Lock _l(_lock); Mutex::Lock _l(_lock);
return _doNetworkConfigRequest(fromAddr,signingId,identity,nwid,metaData,netconf); return _doNetworkConfigRequest(fromAddr,signingId,identity,nwid,metaData,netconf);
@ -1647,25 +1647,24 @@ unsigned int SqliteNetworkController::_doCPGet(
return 404; return 404;
} }
NetworkController::ResultCode SqliteNetworkController::_doNetworkConfigRequest(const InetAddress &fromAddr,const Identity &signingId,const Identity &identity,uint64_t nwid,const Dictionary &metaData,Dictionary &netconf) NetworkController::ResultCode SqliteNetworkController::_doNetworkConfigRequest(const InetAddress &fromAddr,const Identity &signingId,const Identity &identity,uint64_t nwid,const NetworkConfigRequestMetaData &metaData,Buffer<8194> &netconf)
{ {
// Assumes _lock is locked // Assumes _lock is locked
// Decode some stuff from metaData const bool clientIs104 = (Utils::compareVersion(metaData.majorVersion,metaData.minorVersion,metaData.revision,1,0,4) >= 0);
const unsigned int clientMajorVersion = (unsigned int)metaData.getHexUInt(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MAJOR_VERSION,0);
const unsigned int clientMinorVersion = (unsigned int)metaData.getHexUInt(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MINOR_VERSION,0);
const unsigned int clientRevision = (unsigned int)metaData.getHexUInt(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_REVISION,0);
const bool clientIs104 = (Utils::compareVersion(clientMajorVersion,clientMinorVersion,clientRevision,1,0,4) >= 0);
// Note: we can't reuse prepared statements that return const char * pointers without // Note: we can't reuse prepared statements that return const char * pointers without
// making our own copy in e.g. a std::string first. // making our own copy in e.g. a std::string first.
Dictionary legacy;
NetworkConfig nc;
if ((!signingId)||(!signingId.hasPrivate())) { if ((!signingId)||(!signingId.hasPrivate())) {
netconf["error"] = "signing identity invalid or lacks private key"; //netconf["error"] = "signing identity invalid or lacks private key";
return NetworkController::NETCONF_QUERY_INTERNAL_SERVER_ERROR; return NetworkController::NETCONF_QUERY_INTERNAL_SERVER_ERROR;
} }
if (signingId.address().toInt() != (nwid >> 24)) { if (signingId.address().toInt() != (nwid >> 24)) {
netconf["error"] = "signing identity address does not match most significant 40 bits of network ID"; //netconf["error"] = "signing identity address does not match most significant 40 bits of network ID";
return NetworkController::NETCONF_QUERY_INTERNAL_SERVER_ERROR; return NetworkController::NETCONF_QUERY_INTERNAL_SERVER_ERROR;
} }

View File

@ -62,8 +62,8 @@ public:
const Identity &signingId, const Identity &signingId,
const Identity &identity, const Identity &identity,
uint64_t nwid, uint64_t nwid,
const Dictionary &metaData, const NetworkConfigRequestMetaData &metaData,
Dictionary &netconf); Buffer<8194> &netconf);
unsigned int handleControlPlaneHttpGET( unsigned int handleControlPlaneHttpGET(
const std::vector<std::string> &path, const std::vector<std::string> &path,
@ -111,8 +111,8 @@ private:
const Identity &signingId, const Identity &signingId,
const Identity &identity, const Identity &identity,
uint64_t nwid, uint64_t nwid,
const Dictionary &metaData, const NetworkConfigRequestMetaData &metaData,
Dictionary &netconf); Buffer<8194> &netconf);
static void _circuitTestCallback(ZT_Node *node,ZT_CircuitTest *test,const ZT_CircuitTestReport *report); static void _circuitTestCallback(ZT_Node *node,ZT_CircuitTest *test,const ZT_CircuitTestReport *report);

View File

@ -99,7 +99,7 @@ extern "C" {
/** /**
* Maximum number of static physical to ZeroTier address mappings (typically relays, etc.) * Maximum number of static physical to ZeroTier address mappings (typically relays, etc.)
*/ */
#define ZT_MAX_NETWORK_STATIC_PHYSICAL_ADDRESSES 16 #define ZT_MAX_NETWORK_PINNED 16
/** /**
* Maximum number of rule table entries per network (can be increased) * Maximum number of rule table entries per network (can be increased)

View File

@ -146,13 +146,11 @@ bool IncomingPacket::_doERROR(const RuntimeEnvironment *RR,const SharedPtr<Peer>
* who asks. We won't communicate unless we also get a certificate * who asks. We won't communicate unless we also get a certificate
* from the remote that agrees. */ * from the remote that agrees. */
SharedPtr<Network> network(RR->node->network(at<uint64_t>(ZT_PROTO_VERB_ERROR_IDX_PAYLOAD))); SharedPtr<Network> network(RR->node->network(at<uint64_t>(ZT_PROTO_VERB_ERROR_IDX_PAYLOAD)));
if (network) { if ((network)&&(network->hasConfig())&&(network->config().com)) {
if ((network->hasConfig())&&(network->config().com())) { Packet outp(peer->address(),RR->identity.address(),Packet::VERB_NETWORK_MEMBERSHIP_CERTIFICATE);
Packet outp(peer->address(),RR->identity.address(),Packet::VERB_NETWORK_MEMBERSHIP_CERTIFICATE); network->config().com.serialize(outp);
network->config().com().serialize(outp); outp.armor(peer->key(),true);
outp.armor(peer->key(),true); RR->node->putPacket(_localAddress,_remoteAddress,outp.data(),outp.size());
RR->node->putPacket(_localAddress,_remoteAddress,outp.data(),outp.size());
}
} }
} break; } break;
@ -679,8 +677,24 @@ bool IncomingPacket::_doNETWORK_CONFIG_REQUEST(const RuntimeEnvironment *RR,cons
{ {
try { try {
const uint64_t nwid = at<uint64_t>(ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_NETWORK_ID); const uint64_t nwid = at<uint64_t>(ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_NETWORK_ID);
const unsigned int metaDataLength = at<uint16_t>(ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_DICT_LEN); const unsigned int metaDataLength = at<uint16_t>(ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_DICT_LEN);
const Dictionary metaData((const char *)field(ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_DICT,metaDataLength),metaDataLength); const uint8_t *metaDataBytes = (const uint8_t *)field(ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_DICT,metaDataLength);
NetworkConfigRequestMetaData metaData(false);
try {
Buffer<8194> md(metaDataBytes,metaDataLength);
metaData.deserialize(md,0);
} catch ( ... ) { // will throw if new-style meta-data is missing or invalid
metaData.clear();
#ifdef ZT_SUPPORT_OLD_STYLE_NETCONF
const Dictionary oldStyleMetaData((const char *)metaDataBytes,metaDataLength);
metaData.majorVersion = (unsigned int)oldStyleMetaData.getHexUInt(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MAJOR_VERSION,0);
metaData.minorVersion = (unsigned int)oldStyleMetaData.getHexUInt(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MINOR_VERSION,0);
metaData.revision = (unsigned int)oldStyleMetaData.getHexUInt(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_REVISION,0);
#endif
}
//const uint64_t haveRevision = ((ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_DICT + metaDataLength + 8) <= size()) ? at<uint64_t>(ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_DICT + metaDataLength) : 0ULL; //const uint64_t haveRevision = ((ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_DICT + metaDataLength + 8) <= size()) ? at<uint64_t>(ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_DICT + metaDataLength) : 0ULL;
const unsigned int h = hops(); const unsigned int h = hops();
@ -688,27 +702,22 @@ bool IncomingPacket::_doNETWORK_CONFIG_REQUEST(const RuntimeEnvironment *RR,cons
peer->received(_localAddress,_remoteAddress,h,pid,Packet::VERB_NETWORK_CONFIG_REQUEST,0,Packet::VERB_NOP); peer->received(_localAddress,_remoteAddress,h,pid,Packet::VERB_NETWORK_CONFIG_REQUEST,0,Packet::VERB_NOP);
if (RR->localNetworkController) { if (RR->localNetworkController) {
Dictionary netconf; Buffer<8194> netconf;
switch(RR->localNetworkController->doNetworkConfigRequest((h > 0) ? InetAddress() : _remoteAddress,RR->identity,peer->identity(),nwid,metaData,netconf)) { switch(RR->localNetworkController->doNetworkConfigRequest((h > 0) ? InetAddress() : _remoteAddress,RR->identity,peer->identity(),nwid,metaData,netconf)) {
case NetworkController::NETCONF_QUERY_OK: { case NetworkController::NETCONF_QUERY_OK: {
const std::string netconfStr(netconf.toString()); Packet outp(peer->address(),RR->identity.address(),Packet::VERB_OK);
if (netconfStr.length() > 0xffff) { // sanity check since field ix 16-bit outp.append((unsigned char)Packet::VERB_NETWORK_CONFIG_REQUEST);
outp.append(pid);
outp.append(nwid);
outp.append((uint16_t)netconf.size());
outp.append(netconf.data(),(unsigned int)netconf.size());
outp.compress();
outp.armor(peer->key(),true);
if (outp.size() > ZT_PROTO_MAX_PACKET_LENGTH) { // sanity check
TRACE("NETWORK_CONFIG_REQUEST failed: internal error: netconf size %u is too large",(unsigned int)netconfStr.length()); TRACE("NETWORK_CONFIG_REQUEST failed: internal error: netconf size %u is too large",(unsigned int)netconfStr.length());
} else { } else {
Packet outp(peer->address(),RR->identity.address(),Packet::VERB_OK); RR->node->putPacket(_localAddress,_remoteAddress,outp.data(),outp.size());
outp.append((unsigned char)Packet::VERB_NETWORK_CONFIG_REQUEST);
outp.append(pid);
outp.append(nwid);
outp.append((uint16_t)netconfStr.length());
outp.append(netconfStr.data(),(unsigned int)netconfStr.length());
outp.compress();
outp.armor(peer->key(),true);
if (outp.size() > ZT_PROTO_MAX_PACKET_LENGTH) { // sanity check
TRACE("NETWORK_CONFIG_REQUEST failed: internal error: netconf size %u is too large",(unsigned int)netconfStr.length());
} else {
RR->node->putPacket(_localAddress,_remoteAddress,outp.data(),outp.size());
}
} }
} break; } break;
@ -1047,7 +1056,7 @@ bool IncomingPacket::_doCIRCUIT_TEST(const RuntimeEnvironment *RR,const SharedPt
SharedPtr<Network> nw(RR->node->network(originatorCredentialNetworkId)); SharedPtr<Network> nw(RR->node->network(originatorCredentialNetworkId));
if ((nw)&&(nw->hasConfig())) { if ((nw)&&(nw->hasConfig())) {
originatorCredentialNetworkConfig = nw->config(); originatorCredentialNetworkConfig = nw->config();
if ( ( (originatorCredentialNetworkConfig.isPublic()) || (peer->address() == originatorAddress) || ((originatorCredentialNetworkConfig.com())&&(previousHopCom)&&(originatorCredentialNetworkConfig.com().agreesWith(previousHopCom))) ) ) { if ( ( (originatorCredentialNetworkConfig.isPublic()) || (peer->address() == originatorAddress) || ((originatorCredentialNetworkConfig.com)&&(previousHopCom)&&(originatorCredentialNetworkConfig.com.agreesWith(previousHopCom))) ) ) {
TRACE("CIRCUIT_TEST %.16llx received from hop %s(%s) and originator %s with valid network ID credential %.16llx (verified from originator and next hop)",testId,source().toString().c_str(),_remoteAddress.toString().c_str(),originatorAddress.toString().c_str(),originatorCredentialNetworkId); TRACE("CIRCUIT_TEST %.16llx received from hop %s(%s) and originator %s with valid network ID credential %.16llx (verified from originator and next hop)",testId,source().toString().c_str(),_remoteAddress.toString().c_str(),originatorAddress.toString().c_str(),originatorCredentialNetworkId);
} else { } else {
TRACE("dropped CIRCUIT_TEST from %s(%s): originator %s specified network ID %.16llx as credential, and previous hop %s did not supply a valid COM",source().toString().c_str(),_remoteAddress.toString().c_str(),originatorAddress.toString().c_str(),originatorCredentialNetworkId,peer->address().toString().c_str()); TRACE("dropped CIRCUIT_TEST from %s(%s): originator %s specified network ID %.16llx as credential, and previous hop %s did not supply a valid COM",source().toString().c_str(),_remoteAddress.toString().c_str(),originatorAddress.toString().c_str(),originatorCredentialNetworkId,peer->address().toString().c_str());
@ -1122,9 +1131,9 @@ bool IncomingPacket::_doCIRCUIT_TEST(const RuntimeEnvironment *RR,const SharedPt
outp.append(field(ZT_PACKET_IDX_PAYLOAD,lengthOfSignedPortionAndSignature),lengthOfSignedPortionAndSignature); outp.append(field(ZT_PACKET_IDX_PAYLOAD,lengthOfSignedPortionAndSignature),lengthOfSignedPortionAndSignature);
const unsigned int previousHopCredentialPos = outp.size(); const unsigned int previousHopCredentialPos = outp.size();
outp.append((uint16_t)0); // no previous hop credentials: default outp.append((uint16_t)0); // no previous hop credentials: default
if ((originatorCredentialNetworkConfig)&&(!originatorCredentialNetworkConfig.isPublic())&&(originatorCredentialNetworkConfig.com())) { if ((originatorCredentialNetworkConfig)&&(!originatorCredentialNetworkConfig.isPublic())&&(originatorCredentialNetworkConfig.com)) {
outp.append((uint8_t)0x01); // COM outp.append((uint8_t)0x01); // COM
originatorCredentialNetworkConfig.com().serialize(outp); originatorCredentialNetworkConfig.com.serialize(outp);
outp.setAt<uint16_t>(previousHopCredentialPos,(uint16_t)(outp.size() - (previousHopCredentialPos + 2))); outp.setAt<uint16_t>(previousHopCredentialPos,(uint16_t)(outp.size() - (previousHopCredentialPos + 2)));
} }
if (remainingHopsPtr < size()) if (remainingHopsPtr < size())

View File

@ -238,8 +238,8 @@ void Multicaster::send(
const CertificateOfMembership *com = (CertificateOfMembership *)0; const CertificateOfMembership *com = (CertificateOfMembership *)0;
{ {
SharedPtr<Network> nw(RR->node->network(nwid)); SharedPtr<Network> nw(RR->node->network(nwid));
if ((nw)&&(nw->hasConfig())&&(nw->config().com())&&(nw->config().isPrivate())&&(p->needsOurNetworkMembershipCertificate(nwid,now,true))) if ((nw)&&(nw->hasConfig())&&(nw->config().com)&&(nw->config().isPrivate())&&(p->needsOurNetworkMembershipCertificate(nwid,now,true)))
com = &(nw->config().com()); com = &(nw->config().com);
} }
Packet outp(p->address(),RR->identity.address(),Packet::VERB_MULTICAST_GATHER); Packet outp(p->address(),RR->identity.address(),Packet::VERB_MULTICAST_GATHER);

View File

@ -153,7 +153,7 @@ bool Network::applyConfiguration(const NetworkConfig &conf)
if (_destroyed) // sanity check if (_destroyed) // sanity check
return false; return false;
try { try {
if ((conf.networkId() == _id)&&(conf.issuedTo() == RR->identity.address())) { if ((conf.networkId == _id)&&(conf.issuedTo == RR->identity.address())) {
ZT_VirtualNetworkConfig ctmp; ZT_VirtualNetworkConfig ctmp;
bool portInitialized; bool portInitialized;
{ {
@ -234,12 +234,11 @@ void Network::requestConfiguration()
if (controller() == RR->identity.address()) { if (controller() == RR->identity.address()) {
if (RR->localNetworkController) { if (RR->localNetworkController) {
Dictionary newconf; Buffer<8194> tmp;
switch(RR->localNetworkController->doNetworkConfigRequest(InetAddress(),RR->identity,RR->identity,_id,Dictionary(),newconf)) { switch(RR->localNetworkController->doNetworkConfigRequest(InetAddress(),RR->identity,RR->identity,_id,NetworkConfigRequestMetaData(),tmp)) {
case NetworkController::NETCONF_QUERY_OK: { case NetworkController::NETCONF_QUERY_OK:
std::string tmp(newconf.toString()); this->setConfiguration(tmp.data(),tmp.size(),true);
this->setConfiguration((const void *)tmp.data(),(unsigned int)tmp.length(),true); return;
} return;
case NetworkController::NETCONF_QUERY_OBJECT_NOT_FOUND: case NetworkController::NETCONF_QUERY_OBJECT_NOT_FOUND:
this->setNotFound(); this->setNotFound();
return; return;
@ -269,7 +268,7 @@ void Network::requestConfiguration()
outp.append((uint16_t)mds.length()); outp.append((uint16_t)mds.length());
outp.append((const void *)mds.data(),(unsigned int)mds.length()); outp.append((const void *)mds.data(),(unsigned int)mds.length());
if (_config) if (_config)
outp.append((uint64_t)_config.revision()); outp.append((uint64_t)_config.revision);
else outp.append((uint64_t)0); else outp.append((uint64_t)0);
RR->sw->send(outp,true,0); RR->sw->send(outp,true,0);
} }
@ -380,7 +379,7 @@ void Network::_externalConfig(ZT_VirtualNetworkConfig *ec) const
ec->nwid = _id; ec->nwid = _id;
ec->mac = _mac.toInt(); ec->mac = _mac.toInt();
if (_config) if (_config)
Utils::scopy(ec->name,sizeof(ec->name),_config.name()); Utils::scopy(ec->name,sizeof(ec->name),_config.name);
else ec->name[0] = (char)0; else ec->name[0] = (char)0;
ec->status = _status(); ec->status = _status();
ec->type = (_config) ? (_config.isPrivate() ? ZT_NETWORK_TYPE_PRIVATE : ZT_NETWORK_TYPE_PUBLIC) : ZT_NETWORK_TYPE_PRIVATE; ec->type = (_config) ? (_config.isPrivate() ? ZT_NETWORK_TYPE_PRIVATE : ZT_NETWORK_TYPE_PUBLIC) : ZT_NETWORK_TYPE_PRIVATE;
@ -391,7 +390,7 @@ void Network::_externalConfig(ZT_VirtualNetworkConfig *ec) const
ec->broadcastEnabled = (_config) ? (_config.enableBroadcast() ? 1 : 0) : 0; ec->broadcastEnabled = (_config) ? (_config.enableBroadcast() ? 1 : 0) : 0;
ec->portError = _portError; ec->portError = _portError;
ec->enabled = (_enabled) ? 1 : 0; ec->enabled = (_enabled) ? 1 : 0;
ec->netconfRevision = (_config) ? (unsigned long)_config.revision() : 0; ec->netconfRevision = (_config) ? (unsigned long)_config.revision : 0;
ec->multicastSubscriptionCount = std::min((unsigned int)_myMulticastGroups.size(),(unsigned int)ZT_MAX_NETWORK_MULTICAST_SUBSCRIPTIONS); ec->multicastSubscriptionCount = std::min((unsigned int)_myMulticastGroups.size(),(unsigned int)ZT_MAX_NETWORK_MULTICAST_SUBSCRIPTIONS);
for(unsigned int i=0;i<ec->multicastSubscriptionCount;++i) { for(unsigned int i=0;i<ec->multicastSubscriptionCount;++i) {
@ -399,13 +398,14 @@ void Network::_externalConfig(ZT_VirtualNetworkConfig *ec) const
ec->multicastSubscriptions[i].adi = _myMulticastGroups[i].adi(); ec->multicastSubscriptions[i].adi = _myMulticastGroups[i].adi();
} }
std::vector<InetAddress> sips(_config.staticIps());
ec->assignedAddressCount = 0; ec->assignedAddressCount = 0;
for(unsigned long i=0;i<ZT_MAX_ZT_ASSIGNED_ADDRESSES;++i) { for(unsigned int i=0;i<ZT_MAX_ZT_ASSIGNED_ADDRESSES;++i) {
if (i < sips.size()) { if (i < _config.staticIpCount) {
memcpy(&(ec->assignedAddresses[i]),&(sips[i]),sizeof(struct sockaddr_storage)); memcpy(&(ec->assignedAddresses[i]),&(_config.staticIps[i]),sizeof(struct sockaddr_storage));
++ec->assignedAddressCount; ++ec->assignedAddressCount;
} else memset(&(ec->assignedAddresses[i]),0,sizeof(struct sockaddr_storage)); } else {
memset(&(ec->assignedAddresses[i]),0,sizeof(struct sockaddr_storage));
}
} }
} }
@ -417,7 +417,7 @@ bool Network::_isAllowed(const SharedPtr<Peer> &peer) const
return false; return false;
if (_config.isPublic()) if (_config.isPublic())
return true; return true;
return ((_config.com())&&(peer->networkMembershipCertificatesAgree(_id,_config.com()))); return ((_config.com)&&(peer->networkMembershipCertificatesAgree(_id,_config.com)));
} catch (std::exception &exc) { } catch (std::exception &exc) {
TRACE("isAllowed() check failed for peer %s: unexpected exception: %s",peer->address().toString().c_str(),exc.what()); TRACE("isAllowed() check failed for peer %s: unexpected exception: %s",peer->address().toString().c_str(),exc.what());
} catch ( ... ) { } catch ( ... ) {
@ -469,9 +469,9 @@ void Network::_announceMulticastGroupsTo(const SharedPtr<Peer> &peer,const std::
// We push COMs ahead of MULTICAST_LIKE since they're used for access control -- a COM is a public // We push COMs ahead of MULTICAST_LIKE since they're used for access control -- a COM is a public
// credential so "over-sharing" isn't really an issue (and we only do so with roots). // credential so "over-sharing" isn't really an issue (and we only do so with roots).
if ((_config)&&(_config.com())&&(!_config.isPublic())&&(peer->needsOurNetworkMembershipCertificate(_id,RR->node->now(),true))) { if ((_config)&&(_config.com)&&(!_config.isPublic())&&(peer->needsOurNetworkMembershipCertificate(_id,RR->node->now(),true))) {
Packet outp(peer->address(),RR->identity.address(),Packet::VERB_NETWORK_MEMBERSHIP_CERTIFICATE); Packet outp(peer->address(),RR->identity.address(),Packet::VERB_NETWORK_MEMBERSHIP_CERTIFICATE);
_config.com().serialize(outp); _config.com.serialize(outp);
RR->sw->send(outp,true,0); RR->sw->send(outp,true,0);
} }

View File

@ -36,25 +36,25 @@ void NetworkConfig::fromDictionary(const char *ds,unsigned int dslen)
// NOTE: d.get(name) throws if not found, d.get(name,default) returns default // NOTE: d.get(name) throws if not found, d.get(name,default) returns default
_nwid = Utils::hexStrToU64(d.get(ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID,"0").c_str()); networkId = Utils::hexStrToU64(d.get(ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID,"0").c_str());
if (!_nwid) if (!networkId)
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,"0").c_str()); timestamp = Utils::hexStrToU64(d.get(ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP,"0").c_str());
_revision = Utils::hexStrToU64(d.get(ZT_NETWORKCONFIG_DICT_KEY_REVISION,"1").c_str()); // older controllers don't send this, so default to 1 revision = Utils::hexStrToU64(d.get(ZT_NETWORKCONFIG_DICT_KEY_REVISION,"1").c_str()); // older controllers don't send this, so default to 1
_issuedTo = Address(d.get(ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO,"0")); issuedTo = Address(d.get(ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO,"0"));
_multicastLimit = Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT,zero).c_str()); multicastLimit = Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT,zero).c_str());
if (_multicastLimit == 0) _multicastLimit = ZT_MULTICAST_DEFAULT_LIMIT; if (multicastLimit == 0) multicastLimit = ZT_MULTICAST_DEFAULT_LIMIT;
_flags |= ((Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_ALLOW_PASSIVE_BRIDGING,zero).c_str()) != 0) ? ZT_NETWORKCONFIG_FLAG_ALLOW_PASSIVE_BRIDGING : 0); flags |= ((Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_ALLOW_PASSIVE_BRIDGING,zero).c_str()) != 0) ? ZT_NETWORKCONFIG_FLAG_ALLOW_PASSIVE_BRIDGING : 0);
_flags |= ((Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST,one).c_str()) != 0) ? ZT_NETWORKCONFIG_FLAG_ENABLE_BROADCAST : 0); flags |= ((Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST,one).c_str()) != 0) ? ZT_NETWORKCONFIG_FLAG_ENABLE_BROADCAST : 0);
_type = (Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_PRIVATE,one).c_str()) != 0) ? ZT_NETWORK_TYPE_PRIVATE : ZT_NETWORK_TYPE_PUBLIC; this->type = (Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_PRIVATE,one).c_str()) != 0) ? ZT_NETWORK_TYPE_PRIVATE : ZT_NETWORK_TYPE_PUBLIC;
std::string nametmp(d.get(ZT_NETWORKCONFIG_DICT_KEY_NAME,"")); std::string nametmp(d.get(ZT_NETWORKCONFIG_DICT_KEY_NAME,""));
for(unsigned long i=0;((i<ZT_MAX_NETWORK_SHORT_NAME_LENGTH)&&(i<nametmp.length()));++i) for(unsigned long i=0;((i<ZT_MAX_NETWORK_SHORT_NAME_LENGTH)&&(i<nametmp.length()));++i)
_name[i] = (char)nametmp[i]; name[i] = (char)nametmp[i];
// we zeroed the entire structure above and _name is ZT_MAX_NETWORK_SHORT_NAME_LENGTH+1, so it will always null-terminate // we zeroed the entire structure above and _name is ZT_MAX_NETWORK_SHORT_NAME_LENGTH+1, so it will always null-terminate
std::vector<std::string> activeBridgesSplit(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(),",","",""));
@ -63,15 +63,15 @@ void NetworkConfig::fromDictionary(const char *ds,unsigned int dslen)
Address tmp(*a); Address tmp(*a);
if (!tmp.isReserved()) { if (!tmp.isReserved()) {
uint64_t specialist = tmp.toInt(); uint64_t specialist = tmp.toInt();
for(unsigned int i=0;i<_specialistCount;++i) { for(unsigned int i=0;i<specialistCount;++i) {
if ((_specialists[i] & 0xffffffffffULL) == specialist) { if ((specialists[i] & 0xffffffffffULL) == specialist) {
_specialists[i] |= ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE; specialists[i] |= ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE;
specialist = 0; specialist = 0;
break; break;
} }
} }
if ((specialist)&&(_specialistCount < ZT_MAX_NETWORK_SPECIALISTS)) if ((specialist)&&(specialistCount < ZT_MAX_NETWORK_SPECIALISTS))
_specialists[_specialistCount++] = specialist | ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE; specialists[specialistCount++] = specialist | ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE;
} }
} }
} }
@ -101,11 +101,11 @@ void NetworkConfig::fromDictionary(const char *ds,unsigned int dslen)
continue; continue;
} }
if (!addr.isNetwork()) { if (!addr.isNetwork()) {
if ((_staticIpCount < ZT_MAX_ZT_ASSIGNED_ADDRESSES)&&(std::find(&(_staticIps[0]),&(_staticIps[_staticIpCount]),addr) == &(_staticIps[_staticIpCount]))) if ((staticIpCount < ZT_MAX_ZT_ASSIGNED_ADDRESSES)&&(std::find(&(staticIps[0]),&(staticIps[staticIpCount]),addr) == &(staticIps[staticIpCount])))
_staticIps[_staticIpCount++] = addr; staticIps[staticIpCount++] = addr;
} }
} }
std::sort(&(_staticIps[0]),&(_staticIps[_staticIpCount])); std::sort(&(staticIps[0]),&(staticIps[staticIpCount]));
/* Old versions don't support gateways anyway, so ignore this in old netconfs /* Old versions don't support gateways anyway, so ignore this in old netconfs
std::vector<std::string> gatewaysSplit(Utils::split(d.get(ZT_NETWORKCONFIG_DICT_KEY_GATEWAYS,"").c_str(),",","","")); std::vector<std::string> gatewaysSplit(Utils::split(d.get(ZT_NETWORKCONFIG_DICT_KEY_GATEWAYS,"").c_str(),",","",""));
@ -135,26 +135,26 @@ void NetworkConfig::fromDictionary(const char *ds,unsigned int dslen)
} }
uint64_t specialist = zt.toInt(); uint64_t specialist = zt.toInt();
for(unsigned int i=0;i<_specialistCount;++i) { for(unsigned int i=0;i<specialistCount;++i) {
if ((_specialists[i] & 0xffffffffffULL) == specialist) { if ((specialists[i] & 0xffffffffffULL) == specialist) {
_specialists[i] |= ZT_NETWORKCONFIG_SPECIALIST_TYPE_NETWORK_PREFERRED_RELAY; specialists[i] |= ZT_NETWORKCONFIG_SPECIALIST_TYPE_NETWORK_PREFERRED_RELAY;
specialist = 0; specialist = 0;
break; break;
} }
} }
if ((specialist)&&(_specialistCount < ZT_MAX_NETWORK_SPECIALISTS)) if ((specialist)&&(specialistCount < ZT_MAX_NETWORK_SPECIALISTS))
_specialists[_specialistCount++] = specialist | ZT_NETWORKCONFIG_SPECIALIST_TYPE_NETWORK_PREFERRED_RELAY; specialists[specialistCount++] = specialist | ZT_NETWORKCONFIG_SPECIALIST_TYPE_NETWORK_PREFERRED_RELAY;
if ((phy[0])&&(_staticCount < ZT_MAX_NETWORK_STATIC_PHYSICAL_ADDRESSES)) { if ((phy[0])&&(pinnedCount < ZT_MAX_NETWORK_PINNED)) {
_static[_staticCount].zt = zt; pinned[pinnedCount].zt = zt;
_static[_staticCount].phy = phy[0]; pinned[pinnedCount].phy = phy[0];
++_staticCount; ++pinnedCount;
} }
if ((phy[1])&&(_staticCount < ZT_MAX_NETWORK_STATIC_PHYSICAL_ADDRESSES)) { if ((phy[1])&&(pinnedCount < ZT_MAX_NETWORK_PINNED)) {
_static[_staticCount].zt = zt; pinned[pinnedCount].zt = zt;
_static[_staticCount].phy = phy[0]; pinned[pinnedCount].phy = phy[0];
++_staticCount; ++pinnedCount;
} }
} }
} }
@ -162,18 +162,18 @@ void NetworkConfig::fromDictionary(const char *ds,unsigned int dslen)
std::vector<std::string> ets(Utils::split(d.get(ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES,"").c_str(),",","","")); 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) { for(std::vector<std::string>::const_iterator et(ets.begin());et!=ets.end();++et) {
unsigned int et2 = Utils::hexStrToUInt(et->c_str()) & 0xffff; unsigned int et2 = Utils::hexStrToUInt(et->c_str()) & 0xffff;
if ((_ruleCount + 1) < ZT_MAX_NETWORK_RULES) { if ((ruleCount + 1) < ZT_MAX_NETWORK_RULES) {
if (et2) { if (et2) {
_rules[_ruleCount].t = ZT_NETWORK_RULE_MATCH_ETHERTYPE; rules[ruleCount].t = ZT_NETWORK_RULE_MATCH_ETHERTYPE;
_rules[_ruleCount].v.etherType = (uint16_t)et2; rules[ruleCount].v.etherType = (uint16_t)et2;
++_ruleCount; ++ruleCount;
} }
_rules[_ruleCount].t = ZT_NETWORK_RULE_ACTION_ACCEPT; rules[ruleCount].t = ZT_NETWORK_RULE_ACTION_ACCEPT;
++_ruleCount; ++ruleCount;
} }
} }
_com.fromString(d.get(ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP,std::string())); this->com.fromString(d.get(ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP,std::string()));
} }
#endif // ZT_SUPPORT_OLD_STYLE_NETCONF #endif // ZT_SUPPORT_OLD_STYLE_NETCONF

View File

@ -128,6 +128,8 @@ class NetworkConfig
public: public:
/** /**
* Network preferred relay with optional physical endpoint addresses * Network preferred relay with optional physical endpoint addresses
*
* This is used by the convenience relays() method.
*/ */
struct Relay struct Relay
{ {
@ -148,30 +150,30 @@ public:
{ {
NetworkConfig nc; NetworkConfig nc;
nc._nwid = ZT_TEST_NETWORK_ID; nc.networkId = ZT_TEST_NETWORK_ID;
nc._timestamp = 1; nc.timestamp = 1;
nc._revision = 1; nc.revision = 1;
nc._issuedTo = self; nc.issuedTo = self;
nc._multicastLimit = ZT_MULTICAST_DEFAULT_LIMIT; nc.multicastLimit = ZT_MULTICAST_DEFAULT_LIMIT;
nc._flags = ZT_NETWORKCONFIG_FLAG_ENABLE_BROADCAST; nc.flags = ZT_NETWORKCONFIG_FLAG_ENABLE_BROADCAST;
nc._type = ZT_NETWORK_TYPE_PUBLIC; nc.type = ZT_NETWORK_TYPE_PUBLIC;
nc._rules[nc._ruleCount].t = ZT_NETWORK_RULE_ACTION_ACCEPT; nc.rules[0].t = ZT_NETWORK_RULE_ACTION_ACCEPT;
nc._ruleCount = 1; nc.ruleCount = 1;
Utils::snprintf(nc._name,sizeof(nc._name),"ZT_TEST_NETWORK"); Utils::snprintf(nc.name,sizeof(nc.name),"ZT_TEST_NETWORK");
// Make up a V4 IP from 'self' in the 10.0.0.0/8 range -- no // Make up a V4 IP from 'self' in the 10.0.0.0/8 range -- no
// guarantee of uniqueness but collisions are unlikely. // guarantee of uniqueness but collisions are unlikely.
uint32_t ip = (uint32_t)((self.toInt() & 0x00ffffff) | 0x0a000000); // 10.x.x.x 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) == 0x000000ff) ip ^= 0x00000001; // but not ending in .255
if ((ip & 0x000000ff) == 0x00000000) ip ^= 0x00000001; // or .0 if ((ip & 0x000000ff) == 0x00000000) ip ^= 0x00000001; // or .0
nc._staticIps[0] = InetAddress(Utils::hton(ip),8); nc.staticIps[0] = InetAddress(Utils::hton(ip),8);
// Assign an RFC4193-compliant IPv6 address -- will never collide // Assign an RFC4193-compliant IPv6 address -- will never collide
nc._staticIps[1] = InetAddress::makeIpv6rfc4193(ZT_TEST_NETWORK_ID,self.toInt()); nc.staticIps[1] = InetAddress::makeIpv6rfc4193(ZT_TEST_NETWORK_ID,self.toInt());
nc._staticIpCount = 2; nc.staticIpCount = 2;
return nc; return nc;
} }
@ -199,10 +201,10 @@ public:
inline bool permitsEtherType(unsigned int etherType) const inline bool permitsEtherType(unsigned int etherType) const
{ {
unsigned int et = 0; unsigned int et = 0;
for(unsigned int i=0;i<_ruleCount;++i) { for(unsigned int i=0;i<ruleCount;++i) {
ZT_VirtualNetworkRuleType rt = (ZT_VirtualNetworkRuleType)(_rules[i].t & 0x7f); ZT_VirtualNetworkRuleType rt = (ZT_VirtualNetworkRuleType)(rules[i].t & 0x7f);
if (rt == ZT_NETWORK_RULE_MATCH_ETHERTYPE) { if (rt == ZT_NETWORK_RULE_MATCH_ETHERTYPE) {
et = _rules[i].v.etherType; et = rules[i].v.etherType;
} else if (rt == ZT_NETWORK_RULE_ACTION_ACCEPT) { } else if (rt == ZT_NETWORK_RULE_ACTION_ACCEPT) {
if ((!et)||(et == etherType)) if ((!et)||(et == etherType))
return true; return true;
@ -212,76 +214,25 @@ public:
return false; return false;
} }
/**
* @return Network ID that this config applies to
*/
inline uint64_t networkId() const throw() { return _nwid; }
/**
* @return Timestamp of this config (controller-side)
*/
inline uint64_t timestamp() const throw() { return _timestamp; }
/**
* @return Config revision number
*/
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; }
/**
* @return Maximum number of multicast recipients or 0 to disable multicast
*/
inline unsigned int multicastLimit() const throw() { return _multicastLimit; }
/** /**
* @return True if passive bridging is allowed (experimental) * @return True if passive bridging is allowed (experimental)
*/ */
inline bool allowPassiveBridging() const throw() { return ((_flags & ZT_NETWORKCONFIG_FLAG_ALLOW_PASSIVE_BRIDGING) != 0); } inline bool allowPassiveBridging() const throw() { return ((this->flags & ZT_NETWORKCONFIG_FLAG_ALLOW_PASSIVE_BRIDGING) != 0); }
/** /**
* @return True if broadcast (ff:ff:ff:ff:ff:ff) address should work on this network * @return True if broadcast (ff:ff:ff:ff:ff:ff) address should work on this network
*/ */
inline bool enableBroadcast() const throw() { return ((_flags & ZT_NETWORKCONFIG_FLAG_ENABLE_BROADCAST) != 0); } inline bool enableBroadcast() const throw() { return ((this->flags & ZT_NETWORKCONFIG_FLAG_ENABLE_BROADCAST) != 0); }
/**
* @return Type of network (currently public or private)
*/
inline ZT_VirtualNetworkType type() const throw() { return _type; }
/** /**
* @return Network type is public (no access control) * @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 (this->type == ZT_NETWORK_TYPE_PUBLIC); }
/** /**
* @return Network type is private (certificate access control) * @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 (this->type == ZT_NETWORK_TYPE_PRIVATE); }
/**
* @return Short network 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; }
/**
* @return ZeroTier-managed static IPs assigned to this device on this network
*/
inline std::vector<InetAddress> staticIps() const
{
std::vector<InetAddress> r;
for(unsigned int i=0;i<_staticIpCount;++i)
r.push_back(_staticIps[i]);
return r;
}
/** /**
* @return ZeroTier addresses of devices on this network designated as active bridges * @return ZeroTier addresses of devices on this network designated as active bridges
@ -289,9 +240,9 @@ public:
inline std::vector<Address> activeBridges() const inline std::vector<Address> activeBridges() const
{ {
std::vector<Address> r; std::vector<Address> r;
for(unsigned int i=0;i<_specialistCount;++i) { for(unsigned int i=0;i<specialistCount;++i) {
if ((_specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE) != 0) if ((specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE) != 0)
r.push_back(Address(_specialists[i])); r.push_back(Address(specialists[i]));
} }
return r; return r;
} }
@ -302,26 +253,26 @@ public:
inline std::vector<Address> anchors() const inline std::vector<Address> anchors() const
{ {
std::vector<Address> r; std::vector<Address> r;
for(unsigned int i=0;i<_specialistCount;++i) { for(unsigned int i=0;i<specialistCount;++i) {
if ((_specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ANCHOR) != 0) if ((specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ANCHOR) != 0)
r.push_back(Address(_specialists[i])); r.push_back(Address(specialists[i]));
} }
return r; return r;
} }
/** /**
* Look up a static physical address for a given ZeroTier address * Get pinned physical address for a given ZeroTier address, if any
* *
* @param zt ZeroTier address * @param zt ZeroTier address
* @param af Address family (e.g. AF_INET) or 0 for the first we find of any type * @param af Address family (e.g. AF_INET) or 0 for the first we find of any type
* @return Physical address, if any * @return Physical address, if any
*/ */
inline InetAddress staticPhysicalAddress(const Address &zt,unsigned int af) const inline InetAddress findPinnedAddress(const Address &zt,unsigned int af) const
{ {
for(unsigned int i=0;i<_staticCount;++i) { for(unsigned int i=0;i<pinnedCount;++i) {
if (_static[i].zt == zt) { if (pinned[i].zt == zt) {
if ((af == 0)||((unsigned int)_static[i].phy.ss_family == af)) if ((af == 0)||((unsigned int)pinned[i].phy.ss_family == af))
return _static[i].phy; return pinned[i].phy;
} }
} }
return InetAddress(); return InetAddress();
@ -335,12 +286,12 @@ public:
inline std::vector<Relay> relays() const inline std::vector<Relay> relays() const
{ {
std::vector<Relay> r; std::vector<Relay> r;
for(unsigned int i=0;i<_specialistCount;++i) { for(unsigned int i=0;i<specialistCount;++i) {
if ((_specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_NETWORK_PREFERRED_RELAY) != 0) { if ((specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_NETWORK_PREFERRED_RELAY) != 0) {
r.push_back(Relay()); r.push_back(Relay());
r.back().address = _specialists[i]; r.back().address = specialists[i];
r.back().phy4 = staticPhysicalAddress(r.back().address,AF_INET); r.back().phy4 = findPinnedAddress(r.back().address,AF_INET);
r.back().phy6 = staticPhysicalAddress(r.back().address,AF_INET6); r.back().phy6 = findPinnedAddress(r.back().address,AF_INET6);
} }
} }
return r; return r;
@ -352,10 +303,10 @@ public:
*/ */
inline bool permitsBridging(const Address &fromPeer) const inline bool permitsBridging(const Address &fromPeer) const
{ {
if ((_flags & ZT_NETWORKCONFIG_FLAG_ALLOW_PASSIVE_BRIDGING) != 0) if ((flags & ZT_NETWORKCONFIG_FLAG_ALLOW_PASSIVE_BRIDGING) != 0)
return true; return true;
for(unsigned int i=0;i<_specialistCount;++i) { for(unsigned int i=0;i<specialistCount;++i) {
if ((fromPeer == _specialists[i])&&((_specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE) != 0)) if ((fromPeer == specialists[i])&&((specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE) != 0))
return true; return true;
} }
return false; return false;
@ -369,9 +320,9 @@ public:
*/ */
Address nextRelay(unsigned int &ptr) const Address nextRelay(unsigned int &ptr) const
{ {
while (ptr < _specialistCount) { while (ptr < specialistCount) {
if ((_specialists[ptr] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_NETWORK_PREFERRED_RELAY) != 0) { if ((specialists[ptr] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_NETWORK_PREFERRED_RELAY) != 0) {
return Address(_specialists[ptr]); return Address(specialists[ptr]);
} }
++ptr; ++ptr;
} }
@ -384,8 +335,8 @@ public:
*/ */
bool isRelay(const Address &zt) const bool isRelay(const Address &zt) const
{ {
for(unsigned int i=0;i<_specialistCount;++i) { for(unsigned int i=0;i<specialistCount;++i) {
if ((zt == _specialists[i])&&((_specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_NETWORK_PREFERRED_RELAY) != 0)) if ((zt == specialists[i])&&((specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_NETWORK_PREFERRED_RELAY) != 0))
return true; return true;
} }
return false; return false;
@ -394,7 +345,7 @@ public:
/** /**
* @return True if this network config is non-NULL * @return True if this network config is non-NULL
*/ */
inline operator bool() const throw() { return (_nwid != 0); } inline operator bool() const throw() { return (networkId != 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); }
inline bool operator!=(const NetworkConfig &nc) const { return (!(*this == nc)); } inline bool operator!=(const NetworkConfig &nc) const { return (!(*this == nc)); }
@ -404,43 +355,43 @@ public:
{ {
b.append((uint16_t)1); // version b.append((uint16_t)1); // version
b.append((uint64_t)_nwid); b.append((uint64_t)networkId);
b.append((uint64_t)_timestamp); b.append((uint64_t)timestamp);
b.append((uint64_t)_revision); b.append((uint64_t)revision);
_issuedTo.appendTo(b); issuedTo.appendTo(b);
b.append((uint32_t)_multicastLimit); b.append((uint32_t)multicastLimit);
b.append((uint32_t)_flags); b.append((uint32_t)flags);
b.append((uint8_t)_type); b.append((uint8_t)type);
unsigned int nl = (unsigned int)strlen(_name); unsigned int nl = (unsigned int)strlen(name);
if (nl > 255) nl = 255; // sanity check if (nl > 255) nl = 255; // sanity check
b.append((uint8_t)nl); b.append((uint8_t)nl);
b.append((const void *)_name,nl); b.append((const void *)name,nl);
b.append((uint16_t)_specialistCount); b.append((uint16_t)specialistCount);
for(unsigned int i=0;i<_specialistCount;++i) for(unsigned int i=0;i<specialistCount;++i)
b.append((uint64_t)_specialists[i]); b.append((uint64_t)specialists[i]);
b.append((uint16_t)_routeCount); b.append((uint16_t)routeCount);
for(unsigned int i=0;i<_routeCount;++i) { for(unsigned int i=0;i<routeCount;++i) {
reinterpret_cast<const InetAddress *>(&(_routes[i].target))->serialize(b); reinterpret_cast<const InetAddress *>(&(routes[i].target))->serialize(b);
reinterpret_cast<const InetAddress *>(&(_routes[i].via))->serialize(b); reinterpret_cast<const InetAddress *>(&(routes[i].via))->serialize(b);
} }
b.append((uint16_t)_staticIpCount); b.append((uint16_t)staticIpCount);
for(unsigned int i=0;i<_staticIpCount;++i) for(unsigned int i=0;i<staticIpCount;++i)
_staticIps[i].serialize(b); staticIps[i].serialize(b);
b.append((uint16_t)_staticCount); b.append((uint16_t)pinnedCount);
for(unsigned int i=0;i<_staticCount;++i) { for(unsigned int i=0;i<pinnedCount;++i) {
_static[i].zt.appendTo(b); pinned[i].zt.appendTo(b);
_static[i].phy.serialize(b); pinned[i].phy.serialize(b);
} }
b.append((uint16_t)_ruleCount); b.append((uint16_t)ruleCount);
for(unsigned int i=0;i<_ruleCount;++i) { for(unsigned int i=0;i<ruleCount;++i) {
b.append((uint8_t)_rules[i].t); b.append((uint8_t)rules[i].t);
switch((ZT_VirtualNetworkRuleType)(_rules[i].t & 0x7f)) { switch((ZT_VirtualNetworkRuleType)(rules[i].t & 0x7f)) {
//case ZT_NETWORK_RULE_ACTION_DROP: //case ZT_NETWORK_RULE_ACTION_DROP:
//case ZT_NETWORK_RULE_ACTION_ACCEPT: //case ZT_NETWORK_RULE_ACTION_ACCEPT:
default: default:
@ -451,68 +402,68 @@ public:
case ZT_NETWORK_RULE_MATCH_SOURCE_ZEROTIER_ADDRESS: case ZT_NETWORK_RULE_MATCH_SOURCE_ZEROTIER_ADDRESS:
case ZT_NETWORK_RULE_MATCH_DEST_ZEROTIER_ADDRESS: case ZT_NETWORK_RULE_MATCH_DEST_ZEROTIER_ADDRESS:
b.append((uint8_t)5); b.append((uint8_t)5);
Address(_rules[i].v.zt).appendTo(b); Address(rules[i].v.zt).appendTo(b);
break; break;
case ZT_NETWORK_RULE_MATCH_VLAN_ID: case ZT_NETWORK_RULE_MATCH_VLAN_ID:
b.append((uint8_t)2); b.append((uint8_t)2);
b.append((uint16_t)_rules[i].v.vlanId); b.append((uint16_t)rules[i].v.vlanId);
break; break;
case ZT_NETWORK_RULE_MATCH_VLAN_PCP: case ZT_NETWORK_RULE_MATCH_VLAN_PCP:
b.append((uint8_t)1); b.append((uint8_t)1);
b.append((uint8_t)_rules[i].v.vlanPcp); b.append((uint8_t)rules[i].v.vlanPcp);
break; break;
case ZT_NETWORK_RULE_MATCH_VLAN_DEI: case ZT_NETWORK_RULE_MATCH_VLAN_DEI:
b.append((uint8_t)1); b.append((uint8_t)1);
b.append((uint8_t)_rules[i].v.vlanDei); b.append((uint8_t)rules[i].v.vlanDei);
break; break;
case ZT_NETWORK_RULE_MATCH_ETHERTYPE: case ZT_NETWORK_RULE_MATCH_ETHERTYPE:
b.append((uint8_t)2); b.append((uint8_t)2);
b.append((uint16_t)_rules[i].v.etherType); b.append((uint16_t)rules[i].v.etherType);
break; break;
case ZT_NETWORK_RULE_MATCH_MAC_SOURCE: case ZT_NETWORK_RULE_MATCH_MAC_SOURCE:
case ZT_NETWORK_RULE_MATCH_MAC_DEST: case ZT_NETWORK_RULE_MATCH_MAC_DEST:
b.append((uint8_t)6); b.append((uint8_t)6);
b.append(_rules[i].v.mac,6); b.append(rules[i].v.mac,6);
break; break;
case ZT_NETWORK_RULE_MATCH_IPV4_SOURCE: case ZT_NETWORK_RULE_MATCH_IPV4_SOURCE:
case ZT_NETWORK_RULE_MATCH_IPV4_DEST: case ZT_NETWORK_RULE_MATCH_IPV4_DEST:
b.append((uint8_t)5); b.append((uint8_t)5);
b.append(&(_rules[i].v.ipv4.ip),4); b.append(&(rules[i].v.ipv4.ip),4);
b.append((uint8_t)_rules[i].v.ipv4.mask); b.append((uint8_t)rules[i].v.ipv4.mask);
break; break;
case ZT_NETWORK_RULE_MATCH_IPV6_SOURCE: case ZT_NETWORK_RULE_MATCH_IPV6_SOURCE:
case ZT_NETWORK_RULE_MATCH_IPV6_DEST: case ZT_NETWORK_RULE_MATCH_IPV6_DEST:
b.append((uint8_t)17); b.append((uint8_t)17);
b.append(_rules[i].v.ipv6.ip,16); b.append(rules[i].v.ipv6.ip,16);
b.append((uint8_t)_rules[i].v.ipv6.mask); b.append((uint8_t)rules[i].v.ipv6.mask);
break; break;
case ZT_NETWORK_RULE_MATCH_IP_TOS: case ZT_NETWORK_RULE_MATCH_IP_TOS:
b.append((uint8_t)1); b.append((uint8_t)1);
b.append((uint8_t)_rules[i].v.ipTos); b.append((uint8_t)rules[i].v.ipTos);
break; break;
case ZT_NETWORK_RULE_MATCH_IP_PROTOCOL: case ZT_NETWORK_RULE_MATCH_IP_PROTOCOL:
b.append((uint8_t)1); b.append((uint8_t)1);
b.append((uint8_t)_rules[i].v.ipProtocol); b.append((uint8_t)rules[i].v.ipProtocol);
break; break;
case ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE: case ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE:
case ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE: case ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE:
b.append((uint8_t)4); b.append((uint8_t)4);
b.append((uint16_t)_rules[i].v.port[0]); b.append((uint16_t)rules[i].v.port[0]);
b.append((uint16_t)_rules[i].v.port[1]); b.append((uint16_t)rules[i].v.port[1]);
break; break;
case ZT_NETWORK_RULE_MATCH_CHARACTERISTICS: case ZT_NETWORK_RULE_MATCH_CHARACTERISTICS:
b.append((uint8_t)8); b.append((uint8_t)8);
b.append((uint64_t)_rules[i].v.characteristics); b.append((uint64_t)rules[i].v.characteristics);
break; break;
case ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE: case ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE:
b.append((uint8_t)4); b.append((uint8_t)4);
b.append((uint16_t)_rules[i].v.frameSize[0]); b.append((uint16_t)rules[i].v.frameSize[0]);
b.append((uint16_t)_rules[i].v.frameSize[1]); b.append((uint16_t)rules[i].v.frameSize[1]);
break; break;
} }
} }
_com.serialize(b); this->com.serialize(b);
b.append((uint16_t)0); // extended bytes, currently 0 since unused b.append((uint16_t)0); // extended bytes, currently 0 since unused
} }
@ -528,56 +479,56 @@ public:
throw std::invalid_argument("unrecognized version"); throw std::invalid_argument("unrecognized version");
p += 2; p += 2;
_nwid = b.template at<uint64_t>(p); p += 8; networkId = b.template at<uint64_t>(p); p += 8;
_timestamp = b.template at<uint64_t>(p); p += 8; timestamp = b.template at<uint64_t>(p); p += 8;
_revision = b.template at<uint64_t>(p); p += 8; revision = b.template at<uint64_t>(p); p += 8;
_issuedTo.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH; issuedTo.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
_multicastLimit = (unsigned int)b.template at<uint32_t>(p); p += 4; multicastLimit = (unsigned int)b.template at<uint32_t>(p); p += 4;
_flags = (unsigned int)b.template at<uint32_t>(p); p += 4; flags = (unsigned int)b.template at<uint32_t>(p); p += 4;
_type = (ZT_VirtualNetworkType)b[p++]; type = (ZT_VirtualNetworkType)b[p++];
unsigned int nl = (unsigned int)b[p++]; unsigned int nl = (unsigned int)b[p++];
memcpy(_name,b.field(p,nl),std::max(nl,(unsigned int)ZT_MAX_NETWORK_SHORT_NAME_LENGTH)); memcpy(this->name,b.field(p,nl),std::max(nl,(unsigned int)ZT_MAX_NETWORK_SHORT_NAME_LENGTH));
p += nl; p += nl;
// _name will always be null terminated since field size is ZT_MAX_NETWORK_SHORT_NAME_LENGTH + 1 // _name will always be null terminated since field size is ZT_MAX_NETWORK_SHORT_NAME_LENGTH + 1
_specialistCount = (unsigned int)b.template at<uint16_t>(p); p += 2; specialistCount = (unsigned int)b.template at<uint16_t>(p); p += 2;
if (_specialistCount > ZT_MAX_NETWORK_SPECIALISTS) if (specialistCount > ZT_MAX_NETWORK_SPECIALISTS)
throw std::invalid_argument("overflow (specialists)"); throw std::invalid_argument("overflow (specialists)");
for(unsigned int i=0;i<_specialistCount;++i) { for(unsigned int i=0;i<specialistCount;++i) {
_specialists[i] = b.template at<uint64_t>(p); p += 8; specialists[i] = b.template at<uint64_t>(p); p += 8;
} }
_routeCount = (unsigned int)b.template at<uint16_t>(p); p += 2; routeCount = (unsigned int)b.template at<uint16_t>(p); p += 2;
if (_routeCount > ZT_MAX_NETWORK_ROUTES) if (routeCount > ZT_MAX_NETWORK_ROUTES)
throw std::invalid_argument("overflow (routes)"); throw std::invalid_argument("overflow (routes)");
for(unsigned int i=0;i<_routeCount;++i) { for(unsigned int i=0;i<routeCount;++i) {
p += reinterpret_cast<InetAddress *>(&(_routes[i].target))->deserialize(b,p); p += reinterpret_cast<InetAddress *>(&(routes[i].target))->deserialize(b,p);
p += reinterpret_cast<InetAddress *>(&(_routes[i].via))->deserialize(b,p); p += reinterpret_cast<InetAddress *>(&(routes[i].via))->deserialize(b,p);
} }
_staticIpCount = (unsigned int)b.template at<uint16_t>(p); p += 2; staticIpCount = (unsigned int)b.template at<uint16_t>(p); p += 2;
if (_staticIpCount > ZT_MAX_ZT_ASSIGNED_ADDRESSES) if (staticIpCount > ZT_MAX_ZT_ASSIGNED_ADDRESSES)
throw std::invalid_argument("overflow (static IPs)"); throw std::invalid_argument("overflow (static IPs)");
for(unsigned int i=0;i<_staticIpCount;++i) { for(unsigned int i=0;i<staticIpCount;++i) {
p += _staticIps[i].deserialize(b,p); p += staticIps[i].deserialize(b,p);
} }
_staticCount = (unsigned int)b.template at<uint16_t>(p); p += 2; pinnedCount = (unsigned int)b.template at<uint16_t>(p); p += 2;
if (_staticCount > ZT_MAX_NETWORK_STATIC_PHYSICAL_ADDRESSES) if (pinnedCount > ZT_MAX_NETWORK_PINNED)
throw std::invalid_argument("overflow (static addresses)"); throw std::invalid_argument("overflow (static addresses)");
for(unsigned int i=0;i<_staticCount;++i) { for(unsigned int i=0;i<pinnedCount;++i) {
_static[i].zt.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH; pinned[i].zt.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH;
p += _static[i].phy.deserialize(b,p); p += pinned[i].phy.deserialize(b,p);
} }
_ruleCount = (unsigned int)b.template at<uint16_t>(p); p += 2; ruleCount = (unsigned int)b.template at<uint16_t>(p); p += 2;
if (_ruleCount > ZT_MAX_NETWORK_RULES) if (ruleCount > ZT_MAX_NETWORK_RULES)
throw std::invalid_argument("overflow (rules)"); throw std::invalid_argument("overflow (rules)");
for(unsigned int i=0;i<_ruleCount;++i) { for(unsigned int i=0;i<ruleCount;++i) {
_rules[i].t = (uint8_t)b[p++]; rules[i].t = (uint8_t)b[p++];
unsigned int rlen = (unsigned int)b[p++]; unsigned int rlen = (unsigned int)b[p++];
switch((ZT_VirtualNetworkRuleType)(_rules[i].t & 0x7f)) { switch((ZT_VirtualNetworkRuleType)(rules[i].t & 0x7f)) {
//case ZT_NETWORK_RULE_ACTION_DROP: //case ZT_NETWORK_RULE_ACTION_DROP:
//case ZT_NETWORK_RULE_ACTION_ACCEPT: //case ZT_NETWORK_RULE_ACTION_ACCEPT:
default: default:
@ -588,57 +539,57 @@ public:
case ZT_NETWORK_RULE_MATCH_DEST_ZEROTIER_ADDRESS: { case ZT_NETWORK_RULE_MATCH_DEST_ZEROTIER_ADDRESS: {
Address tmp; Address tmp;
tmp.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); tmp.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH);
_rules[i].v.zt = tmp.toInt(); rules[i].v.zt = tmp.toInt();
} break; } break;
case ZT_NETWORK_RULE_MATCH_VLAN_ID: case ZT_NETWORK_RULE_MATCH_VLAN_ID:
_rules[i].v.vlanId = b.template at<uint16_t>(p); rules[i].v.vlanId = b.template at<uint16_t>(p);
break; break;
case ZT_NETWORK_RULE_MATCH_VLAN_PCP: case ZT_NETWORK_RULE_MATCH_VLAN_PCP:
_rules[i].v.vlanPcp = (uint8_t)b[p]; rules[i].v.vlanPcp = (uint8_t)b[p];
break; break;
case ZT_NETWORK_RULE_MATCH_VLAN_DEI: case ZT_NETWORK_RULE_MATCH_VLAN_DEI:
_rules[i].v.vlanDei = (uint8_t)b[p]; rules[i].v.vlanDei = (uint8_t)b[p];
break; break;
case ZT_NETWORK_RULE_MATCH_ETHERTYPE: case ZT_NETWORK_RULE_MATCH_ETHERTYPE:
_rules[i].v.etherType = b.template at<uint16_t>(p); rules[i].v.etherType = b.template at<uint16_t>(p);
break; break;
case ZT_NETWORK_RULE_MATCH_MAC_SOURCE: case ZT_NETWORK_RULE_MATCH_MAC_SOURCE:
case ZT_NETWORK_RULE_MATCH_MAC_DEST: case ZT_NETWORK_RULE_MATCH_MAC_DEST:
memcpy(_rules[i].v.mac,b.field(p,6),6); memcpy(rules[i].v.mac,b.field(p,6),6);
break; break;
case ZT_NETWORK_RULE_MATCH_IPV4_SOURCE: case ZT_NETWORK_RULE_MATCH_IPV4_SOURCE:
case ZT_NETWORK_RULE_MATCH_IPV4_DEST: case ZT_NETWORK_RULE_MATCH_IPV4_DEST:
memcpy(&(_rules[i].v.ipv4.ip),b.field(p,4),4); memcpy(&(rules[i].v.ipv4.ip),b.field(p,4),4);
_rules[i].v.ipv4.mask = (uint8_t)b[p+4]; rules[i].v.ipv4.mask = (uint8_t)b[p+4];
break; break;
case ZT_NETWORK_RULE_MATCH_IPV6_SOURCE: case ZT_NETWORK_RULE_MATCH_IPV6_SOURCE:
case ZT_NETWORK_RULE_MATCH_IPV6_DEST: case ZT_NETWORK_RULE_MATCH_IPV6_DEST:
memcpy(_rules[i].v.ipv6.ip,b.field(p,16),16); memcpy(rules[i].v.ipv6.ip,b.field(p,16),16);
_rules[i].v.ipv6.mask = (uint8_t)b[p+16]; rules[i].v.ipv6.mask = (uint8_t)b[p+16];
break; break;
case ZT_NETWORK_RULE_MATCH_IP_TOS: case ZT_NETWORK_RULE_MATCH_IP_TOS:
_rules[i].v.ipTos = (uint8_t)b[p]; rules[i].v.ipTos = (uint8_t)b[p];
break; break;
case ZT_NETWORK_RULE_MATCH_IP_PROTOCOL: case ZT_NETWORK_RULE_MATCH_IP_PROTOCOL:
_rules[i].v.ipProtocol = (uint8_t)b[p]; rules[i].v.ipProtocol = (uint8_t)b[p];
break; break;
case ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE: case ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE:
case ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE: case ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE:
_rules[i].v.port[0] = b.template at<uint16_t>(p); rules[i].v.port[0] = b.template at<uint16_t>(p);
_rules[i].v.port[1] = b.template at<uint16_t>(p+2); rules[i].v.port[1] = b.template at<uint16_t>(p+2);
break; break;
case ZT_NETWORK_RULE_MATCH_CHARACTERISTICS: case ZT_NETWORK_RULE_MATCH_CHARACTERISTICS:
_rules[i].v.characteristics = b.template at<uint64_t>(p); rules[i].v.characteristics = b.template at<uint64_t>(p);
break; break;
case ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE: case ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE:
_rules[i].v.frameSize[0] = b.template at<uint16_t>(p); rules[i].v.frameSize[0] = b.template at<uint16_t>(p);
_rules[i].v.frameSize[1] = b.template at<uint16_t>(p+2); rules[i].v.frameSize[1] = b.template at<uint16_t>(p+2);
break; break;
} }
p += rlen; p += rlen;
} }
p += _com.deserialize(b,p); p += this->com.deserialize(b,p);
p += b.template at<uint16_t>(p) + 2; p += b.template at<uint16_t>(p) + 2;
@ -649,40 +600,109 @@ public:
void fromDictionary(const char *ds,unsigned int dslen); void fromDictionary(const char *ds,unsigned int dslen);
#endif #endif
protected: // protected so that a subclass can fill this out in network controller code /**
uint64_t _nwid; * Network ID that this configuration applies to
uint64_t _timestamp; */
uint64_t _revision; uint64_t networkId;
Address _issuedTo;
unsigned int _multicastLimit;
unsigned int _flags;
ZT_VirtualNetworkType _type;
char _name[ZT_MAX_NETWORK_SHORT_NAME_LENGTH + 1]; /**
* Controller-side time of config generation/issue
*/
uint64_t timestamp;
// Special ZeroTier addresses -- most significant 40 bits are address, least 24 are specialist type flags /**
uint64_t _specialists[ZT_MAX_NETWORK_SPECIALISTS]; * Controller-side revision counter for this configuration
*/
uint64_t revision;
// ZeroTier-managed IPs and routing table entries and stuff /**
ZT_VirtualNetworkRoute _routes[ZT_MAX_NETWORK_ROUTES]; * Address of device to which this config is issued
InetAddress _staticIps[ZT_MAX_ZT_ASSIGNED_ADDRESSES]; */
Address issuedTo;
// ZeroTier to physical static mappings /**
* Maximum number of recipients per multicast (not including active bridges)
*/
unsigned int multicastLimit;
/**
* Flags (32-bit)
*/
unsigned int flags;
/**
* Number of specialists
*/
unsigned int specialistCount;
/**
* Number of routes
*/
unsigned int routeCount;
/**
* Number of ZT-managed static IP assignments
*/
unsigned int staticIpCount;
/**
* Number of pinned devices (devices with physical address hints)
*/
unsigned int pinnedCount;
/**
* Number of rule table entries
*/
unsigned int ruleCount;
/**
* Specialist devices
*
* For each entry the least significant 40 bits are the device's ZeroTier
* address and the most significant 24 bits are flags indicating its role.
*/
uint64_t specialists[ZT_MAX_NETWORK_SPECIALISTS];
/**
* Statically defined "pushed" routes (including default gateways)
*/
ZT_VirtualNetworkRoute routes[ZT_MAX_NETWORK_ROUTES];
/**
* Static IP assignments
*/
InetAddress staticIps[ZT_MAX_ZT_ASSIGNED_ADDRESSES];
/**
* Pinned devices with physical address hints
*
* These can be used to specify a physical address where a given device
* can be reached. It's usually used with network relays (specialists).
*/
struct { struct {
Address zt; Address zt;
InetAddress phy; InetAddress phy;
} _static[ZT_MAX_NETWORK_STATIC_PHYSICAL_ADDRESSES]; } pinned[ZT_MAX_NETWORK_PINNED];
// Network rules (only Ethernet type filtering is currently supported) /**
ZT_VirtualNetworkRule _rules[ZT_MAX_NETWORK_RULES]; * Rules table
*/
ZT_VirtualNetworkRule rules[ZT_MAX_NETWORK_RULES];
unsigned int _specialistCount; /**
unsigned int _routeCount; * Network type (currently just public or private)
unsigned int _staticIpCount; */
unsigned int _staticCount; ZT_VirtualNetworkType type;
unsigned int _ruleCount;
CertificateOfMembership _com; /**
* Network short name or empty string if not defined
*/
char name[ZT_MAX_NETWORK_SHORT_NAME_LENGTH + 1];
/**
* Certficiate of membership (for private networks)
*/
CertificateOfMembership com;
}; };
} // namespace ZeroTier } // namespace ZeroTier

View File

@ -29,11 +29,6 @@
#include "../version.h" #include "../version.h"
#ifdef ZT_SUPPORT_OLD_STYLE_NETCONF
#include <string>
#include "Dictionary.hpp"
#endif
namespace ZeroTier { namespace ZeroTier {
/** /**
@ -42,7 +37,20 @@ namespace ZeroTier {
class NetworkConfigRequestMetaData class NetworkConfigRequestMetaData
{ {
public: public:
NetworkConfigRequestMetaData() NetworkConfigRequestMetaData() :
buildId(0),
flags(0),
vendor(ZT_VENDOR_ZEROTIER),
platform(ZT_PLATFORM_UNSPECIFIED),
architecture(ZT_ARCHITECTURE_UNSPECIFIED),
majorVersion(ZEROTIER_ONE_VERSION_MAJOR),
minorVersion(ZEROTIER_ONE_VERSION_MINOR),
revision(ZEROTIER_ONE_VERSION_REVISION)
{
memset(auth,0,sizeof(auth));
}
NetworkConfigRequestMetaData(bool foo)
{ {
memset(this,0,sizeof(NetworkConfigRequestMetaData)); memset(this,0,sizeof(NetworkConfigRequestMetaData));
} }
@ -67,7 +75,7 @@ public:
b.append((uint16_t)minorVersion); b.append((uint16_t)minorVersion);
b.append((uint16_t)revision); b.append((uint16_t)revision);
unsigned int tl = (unsigned int)strlen(_auth); unsigned int tl = (unsigned int)strlen(auth);
if (tl > 255) tl = 255; // sanity check if (tl > 255) tl = 255; // sanity check
b.append((uint8_t)tl); b.append((uint8_t)tl);
b.append((const void *)auth,tl); b.append((const void *)auth,tl);
@ -108,6 +116,11 @@ public:
return (p - startAt); return (p - startAt);
} }
inline void clear()
{
memset(this,0,sizeof(NetworkConfigRequestMetaData));
}
/** /**
* Build ID (currently unused, must be 0) * Build ID (currently unused, must be 0)
*/ */

View File

@ -23,9 +23,10 @@
#include "Constants.hpp" #include "Constants.hpp"
#include "InetAddress.hpp" #include "InetAddress.hpp"
#include "Dictionary.hpp"
#include "Address.hpp" #include "Address.hpp"
#include "Identity.hpp" #include "Identity.hpp"
#include "NetworkConfigRequestMetaData.hpp"
#include "Buffer.hpp"
namespace ZeroTier { namespace ZeroTier {
@ -65,8 +66,8 @@ public:
* @param signingId Identity that should be used to sign results -- must include private key * @param signingId Identity that should be used to sign results -- must include private key
* @param identity Originating peer ZeroTier identity * @param identity Originating peer ZeroTier identity
* @param nwid 64-bit network ID * @param nwid 64-bit network ID
* @param metaData Meta-data bundled with request (empty if none) * @param metaData Meta-data bundled with request (if any)
* @param result Dictionary to receive resulting signed netconf on success * @param result Buffer to receive serialized network configuration data (any existing data in buffer is preserved)
* @return Returns NETCONF_QUERY_OK if result dictionary is valid, or an error code on error * @return Returns NETCONF_QUERY_OK if result dictionary is valid, or an error code on error
*/ */
virtual NetworkController::ResultCode doNetworkConfigRequest( virtual NetworkController::ResultCode doNetworkConfigRequest(
@ -74,8 +75,8 @@ public:
const Identity &signingId, const Identity &signingId,
const Identity &identity, const Identity &identity,
uint64_t nwid, uint64_t nwid,
const Dictionary &metaData, const NetworkConfigRequestMetaData &metaData,
Dictionary &result) = 0; Buffer<8194> &result) = 0;
}; };
} // namespace ZeroTier } // namespace ZeroTier

View File

@ -677,11 +677,9 @@ bool Node::shouldUsePathForZeroTierTraffic(const InetAddress &localAddress,const
Mutex::Lock _l(_networks_m); Mutex::Lock _l(_networks_m);
for(std::vector< std::pair< uint64_t, SharedPtr<Network> > >::const_iterator i=_networks.begin();i!=_networks.end();++i) { for(std::vector< std::pair< uint64_t, SharedPtr<Network> > >::const_iterator i=_networks.begin();i!=_networks.end();++i) {
if (i->second->hasConfig()) { if (i->second->hasConfig()) {
std::vector<InetAddress> sips(i->second->config().staticIps()); for(unsigned int k=0;k<i->second->config().staticIpCount;++k) {
for(std::vector<InetAddress>::const_iterator a(sips.begin());a!=sips.end();++a) { if (i->second->config().staticIps[k].containsAddress(remoteAddress))
if (a->containsAddress(remoteAddress)) {
return false; return false;
}
} }
} }
} }

View File

@ -361,8 +361,8 @@ void Switch::onLocalEthernet(const SharedPtr<Network> &network,const MAC &from,c
* themselves, they can look these addresses up with NDP and it will * themselves, they can look these addresses up with NDP and it will
* work just fine. */ * work just fine. */
if ((reinterpret_cast<const uint8_t *>(data)[6] == 0x3a)&&(reinterpret_cast<const uint8_t *>(data)[40] == 0x87)) { // ICMPv6 neighbor solicitation if ((reinterpret_cast<const uint8_t *>(data)[6] == 0x3a)&&(reinterpret_cast<const uint8_t *>(data)[40] == 0x87)) { // ICMPv6 neighbor solicitation
std::vector<InetAddress> sips(network->config().staticIps()); for(unsigned int sipk=0;sipk<network->config().staticIpCount;++sipk) {
for(std::vector<InetAddress>::const_iterator sip(sips.begin());sip!=sips.end();++sip) { const InetAddress *sip = &(network->config().staticIps[sipk]);
if ((sip->ss_family == AF_INET6)&&(Utils::ntoh((uint16_t)reinterpret_cast<const struct sockaddr_in6 *>(&(*sip))->sin6_port) == 88)) { if ((sip->ss_family == AF_INET6)&&(Utils::ntoh((uint16_t)reinterpret_cast<const struct sockaddr_in6 *>(&(*sip))->sin6_port) == 88)) {
const uint8_t *my6 = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(&(*sip))->sin6_addr.s6_addr); const uint8_t *my6 = reinterpret_cast<const uint8_t *>(reinterpret_cast<const struct sockaddr_in6 *>(&(*sip))->sin6_addr.s6_addr);
if ((my6[0] == 0xfd)&&(my6[9] == 0x99)&&(my6[10] == 0x93)) { // ZT-RFC4193 == fd__:____:____:____:__99:93__:____:____ / 88 if ((my6[0] == 0xfd)&&(my6[9] == 0x99)&&(my6[10] == 0x93)) { // ZT-RFC4193 == fd__:____:____:____:__99:93__:____:____ / 88
@ -425,8 +425,8 @@ void Switch::onLocalEthernet(const SharedPtr<Network> &network,const MAC &from,c
//TRACE("%.16llx: MULTICAST %s -> %s %s %u",network->id(),from.toString().c_str(),mg.toString().c_str(),etherTypeName(etherType),len); //TRACE("%.16llx: MULTICAST %s -> %s %s %u",network->id(),from.toString().c_str(),mg.toString().c_str(),etherTypeName(etherType),len);
RR->mc->send( RR->mc->send(
((!network->config().isPublic())&&(network->config().com())) ? &(network->config().com()) : (const CertificateOfMembership *)0, ((!network->config().isPublic())&&(network->config().com)) ? &(network->config().com) : (const CertificateOfMembership *)0,
network->config().multicastLimit(), network->config().multicastLimit,
RR->node->now(), RR->node->now(),
network->id(), network->id(),
network->config().activeBridges(), network->config().activeBridges(),
@ -444,13 +444,13 @@ void Switch::onLocalEthernet(const SharedPtr<Network> &network,const MAC &from,c
Address toZT(to.toAddress(network->id())); // since in-network MACs are derived from addresses and network IDs, we can reverse this Address toZT(to.toAddress(network->id())); // since in-network MACs are derived from addresses and network IDs, we can reverse this
SharedPtr<Peer> toPeer(RR->topology->getPeer(toZT)); SharedPtr<Peer> toPeer(RR->topology->getPeer(toZT));
const bool includeCom = ( (network->config().isPrivate()) && (network->config().com()) && ((!toPeer)||(toPeer->needsOurNetworkMembershipCertificate(network->id(),RR->node->now(),true))) ); const bool includeCom = ( (network->config().isPrivate()) && (network->config().com) && ((!toPeer)||(toPeer->needsOurNetworkMembershipCertificate(network->id(),RR->node->now(),true))) );
if ((fromBridged)||(includeCom)) { if ((fromBridged)||(includeCom)) {
Packet outp(toZT,RR->identity.address(),Packet::VERB_EXT_FRAME); Packet outp(toZT,RR->identity.address(),Packet::VERB_EXT_FRAME);
outp.append(network->id()); outp.append(network->id());
if (includeCom) { if (includeCom) {
outp.append((unsigned char)0x01); // 0x01 -- COM included outp.append((unsigned char)0x01); // 0x01 -- COM included
network->config().com().serialize(outp); network->config().com.serialize(outp);
} else { } else {
outp.append((unsigned char)0x00); outp.append((unsigned char)0x00);
} }
@ -513,9 +513,9 @@ void Switch::onLocalEthernet(const SharedPtr<Network> &network,const MAC &from,c
SharedPtr<Peer> bridgePeer(RR->topology->getPeer(bridges[b])); SharedPtr<Peer> bridgePeer(RR->topology->getPeer(bridges[b]));
Packet outp(bridges[b],RR->identity.address(),Packet::VERB_EXT_FRAME); Packet outp(bridges[b],RR->identity.address(),Packet::VERB_EXT_FRAME);
outp.append(network->id()); outp.append(network->id());
if ( (network->config().isPrivate()) && (network->config().com()) && ((!bridgePeer)||(bridgePeer->needsOurNetworkMembershipCertificate(network->id(),RR->node->now(),true))) ) { if ( (network->config().isPrivate()) && (network->config().com) && ((!bridgePeer)||(bridgePeer->needsOurNetworkMembershipCertificate(network->id(),RR->node->now(),true))) ) {
outp.append((unsigned char)0x01); // 0x01 -- COM included outp.append((unsigned char)0x01); // 0x01 -- COM included
network->config().com().serialize(outp); network->config().com.serialize(outp);
} else { } else {
outp.append((unsigned char)0); outp.append((unsigned char)0);
} }