From e09c1a1c11522c11c6ea81dcb1c52a4e8a2f5c6c Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Thu, 16 Jun 2016 12:28:43 -0700 Subject: [PATCH] Big refactor mostly builds. We now have a uniform backward compatible netconf. --- include/ZeroTierOne.h | 2 + node/CertificateOfMembership.hpp | 1 - node/Dictionary.hpp | 120 ++++-- node/IncomingPacket.cpp | 56 +-- node/Network.cpp | 76 ++-- node/Network.hpp | 5 +- node/NetworkConfig.cpp | 574 ++++++++++++++++++++------ node/NetworkConfig.hpp | 389 +++++------------ node/NetworkConfigRequestMetaData.hpp | 196 --------- node/NetworkController.hpp | 18 +- node/Utils.cpp | 18 + node/Utils.hpp | 21 +- selftest.cpp | 54 ++- version.h | 3 - 14 files changed, 738 insertions(+), 795 deletions(-) delete mode 100644 node/NetworkConfigRequestMetaData.hpp diff --git a/include/ZeroTierOne.h b/include/ZeroTierOne.h index efba90a87..d16e2a686 100644 --- a/include/ZeroTierOne.h +++ b/include/ZeroTierOne.h @@ -412,6 +412,8 @@ enum ZT_VirtualNetworkRuleType */ ZT_NETWORK_RULE_ACTION_REDIRECT = 3, + // <32 == actions + /** * Source ZeroTier address -- analogous to an Ethernet port ID on a switch */ diff --git a/node/CertificateOfMembership.hpp b/node/CertificateOfMembership.hpp index 43c8c0af0..0342bc334 100644 --- a/node/CertificateOfMembership.hpp +++ b/node/CertificateOfMembership.hpp @@ -295,7 +295,6 @@ public: * @param s String to deserialize */ void fromString(const char *s); - inline void fromString(const std::string &s) { fromString(s.c_str()); } #endif // ZT_SUPPORT_OLD_STYLE_NETCONF /** diff --git a/node/Dictionary.hpp b/node/Dictionary.hpp index 66072952f..ccca00c82 100644 --- a/node/Dictionary.hpp +++ b/node/Dictionary.hpp @@ -21,11 +21,12 @@ #include "Constants.hpp" #include "Utils.hpp" +#include "Buffer.hpp" +#include "Address.hpp" #include -#include - +// Can be increased if it's ever needed, but not too much. #define ZT_DICTIONARY_MAX_SIZE 16384 namespace ZeroTier { @@ -45,7 +46,8 @@ namespace ZeroTier { * Keys cannot contain binary data, CR/LF, nulls, or the equals (=) sign. * Adding such a key will result in an invalid entry (but isn't dangerous). * - * There is code to test and fuzz this in selftest.cpp. + * There is code to test and fuzz this in selftest.cpp. Fuzzing a blob of + * pointer tricks like this is important after any modifications. */ class Dictionary { @@ -60,9 +62,32 @@ public: Utils::scopy(_d,sizeof(_d),s); } - inline void load(const char *s) + Dictionary(const char *s,unsigned int len) { - Utils::scopy(_d,sizeof(_d),s); + memcpy(_d,s,(len > ZT_DICTIONARY_MAX_SIZE) ? (unsigned int)ZT_DICTIONARY_MAX_SIZE : len); + _d[ZT_DICTIONARY_MAX_SIZE-1] = (char)0; + } + + Dictionary(const Dictionary &d) + { + Utils::scopy(_d,sizeof(_d),d._d); + } + + inline Dictionary &operator=(const Dictionary &d) + { + Utils::scopy(_d,sizeof(_d),d._d); + return *this; + } + + /** + * Load a dictionary from a C-string + * + * @param s Dictionary in string form + * @return False if 's' was longer than ZT_DICTIONARY_MAX_SIZE + */ + inline bool load(const char *s) + { + return Utils::scopy(_d,sizeof(_d),s); } /** @@ -103,9 +128,7 @@ public: inline int get(const char *key,char *dest,unsigned int destlen) const { const char *p = _d; - const char *const eof = p + ZT_DICTIONARY_MAX_SIZE; const char *k,*s; - unsigned int dptr = 0; bool esc; int j; @@ -185,34 +208,48 @@ public: } /** + * Get the contents of a key into a buffer + * + * @param key Key to get + * @param dest Destination buffer + * @return True if key was found (if false, dest will be empty) + */ + template + inline bool get(const char *key,Buffer &dest) const + { + const int r = this->get(key,const_cast(reinterpret_cast(dest.data())),C); + if (r >= 0) { + dest.setSize((unsigned int)r); + return true; + } else { + dest.clear(); + return false; + } + } + + /** + * Get a boolean value + * * @param key Key to look up - * @param dfl Default value if not found in dictionary (a key with an empty value is considered not found) + * @param dfl Default value if not found in dictionary * @return Boolean value of key or 'dfl' if not found */ - bool getBoolean(const char *key,bool dfl = false) const + bool getB(const char *key,bool dfl = false) const { - char tmp[128]; - if (this->get(key,tmp,sizeof(tmp)) >= 1) { - switch(tmp[0]) { - case '1': - case 't': - case 'T': - case 'y': - case 'Y': - return true; - default: - return false; - } - } + char tmp[4]; + if (this->get(key,tmp,sizeof(tmp)) >= 0) + return ((*tmp == '1')||(*tmp == 't')||(*tmp == 'T')); return dfl; } /** + * Get an unsigned int64 stored as hex in the dictionary + * * @param key Key to look up * @param dfl Default value or 0 if unspecified * @return Decoded hex UInt value or 'dfl' if not found */ - inline uint64_t getHexUInt(const char *key,uint64_t dfl = 0) const + inline uint64_t getUI(const char *key,uint64_t dfl = 0) const { char tmp[128]; if (this->get(key,tmp,sizeof(tmp)) >= 1) @@ -227,6 +264,8 @@ public: * will always be returned by get(). There is no erase(). This is designed * to be generated and shipped, not as an editable data structure. * + * Use the vlen parameter to add binary values. Nulls will be escaped. + * * @param key Key -- nulls, CR/LF, and equals (=) are illegal characters * @param value Value to set * @param vlen Length of value in bytes or -1 to treat value[] as a C-string and look for terminating 0 @@ -249,20 +288,22 @@ public: int k = 0; while ((*p)&&((vlen < 0)||(k < vlen))) { switch(*p) { + case 0: case '\r': case '\n': - case '\0': case '\t': + case '\\': _d[j++] = '\\'; if (j == ZT_DICTIONARY_MAX_SIZE) { _d[i] = (char)0; return false; } switch(*p) { + case 0: _d[j++] = '0'; break; case '\r': _d[j++] = 'r'; break; case '\n': _d[j++] = 'n'; break; - case '\0': _d[j++] = '0'; break; case '\t': _d[j++] = 't'; break; + case '\\': _d[j++] = '\\'; break; } if (j == ZT_DICTIONARY_MAX_SIZE) { _d[i] = (char)0; @@ -290,19 +331,38 @@ public: /** * Add a boolean as a '1' or a '0' */ - inline void add(const char *key,bool value) + inline bool add(const char *key,bool value) { - this->add(key,(value) ? "1" : "0",1); + return this->add(key,(value) ? "1" : "0",1); } /** * Add a 64-bit integer (unsigned) as a hex value */ - inline void add(const char *key,uint64_t value) + inline bool add(const char *key,uint64_t value) { - char tmp[128]; + char tmp[32]; Utils::snprintf(tmp,sizeof(tmp),"%llx",(unsigned long long)value); - this->add(key,tmp,-1); + return this->add(key,tmp,-1); + } + + /** + * Add a 64-bit integer (unsigned) as a hex value + */ + inline bool add(const char *key,const Address &a) + { + char tmp[32]; + Utils::snprintf(tmp,sizeof(tmp),"%.10llx",(unsigned long long)a.toInt()); + return this->add(key,tmp,-1); + } + + /** + * Add a binary buffer + */ + template + inline bool add(const char *key,const Buffer &value) + { + return this->add(key,(const char *)value.data(),(int)value.size()); } /** diff --git a/node/IncomingPacket.cpp b/node/IncomingPacket.cpp index 9e0226b99..f57fa2a8e 100644 --- a/node/IncomingPacket.cpp +++ b/node/IncomingPacket.cpp @@ -403,8 +403,12 @@ bool IncomingPacket::_doOK(const RuntimeEnvironment *RR,const SharedPtr &p if ((nw)&&(nw->controller() == peer->address())) { const unsigned int nclen = at(ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST__OK__IDX_DICT_LEN); if (nclen) { - nw->setConfiguration(field(ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST__OK__IDX_DICT,nclen),nclen,true); - TRACE("got network configuration for network %.16llx from %s",(unsigned long long)nw->id(),source().toString().c_str()); + Dictionary dconf((const char *)field(ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST__OK__IDX_DICT,nclen),nclen); + NetworkConfig nconf; + if (nconf.fromDictionary(dconf)) { + nw->setConfiguration(nconf,true); + TRACE("got network configuration for network %.16llx from %s",(unsigned long long)nw->id(),source().toString().c_str()); + } } } } break; @@ -679,27 +683,8 @@ bool IncomingPacket::_doNETWORK_CONFIG_REQUEST(const RuntimeEnvironment *RR,cons const uint64_t nwid = at(ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_NETWORK_ID); const unsigned int metaDataLength = at(ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_DICT_LEN); - const uint8_t *metaDataBytes = (const uint8_t *)field(ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_DICT,metaDataLength); - - NetworkConfigRequestMetaData metaData; - bool haveNewStyleMetaData = false; - for(unsigned int i=0;i md(metaDataBytes,metaDataLength); - metaData.deserialize(md,0); // the meta-data deserializer automatically skips old-style meta-data - } else { -#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 char *metaDataBytes = (const char *)field(ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_DICT,metaDataLength); + const Dictionary metaData(metaDataBytes,metaDataLength); //const uint64_t haveRevision = ((ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_DICT + metaDataLength + 8) <= size()) ? at(ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_DICT + metaDataLength) : 0ULL; @@ -708,22 +693,21 @@ bool IncomingPacket::_doNETWORK_CONFIG_REQUEST(const RuntimeEnvironment *RR,cons peer->received(_localAddress,_remoteAddress,h,pid,Packet::VERB_NETWORK_CONFIG_REQUEST,0,Packet::VERB_NOP); if (RR->localNetworkController) { - Buffer<8194> netconf; + NetworkConfig netconf; switch(RR->localNetworkController->doNetworkConfigRequest((h > 0) ? InetAddress() : _remoteAddress,RR->identity,peer->identity(),nwid,metaData,netconf)) { case NetworkController::NETCONF_QUERY_OK: { - Packet outp(peer->address(),RR->identity.address(),Packet::VERB_OK); - 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()); - } else { - RR->node->putPacket(_localAddress,_remoteAddress,outp.data(),outp.size()); + Dictionary dconf; + if (netconf.toDictionary(dconf,metaData.getUI(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_VERSION,0) < 6)) { + Packet outp(peer->address(),RR->identity.address(),Packet::VERB_OK); + outp.append((unsigned char)Packet::VERB_NETWORK_CONFIG_REQUEST); + outp.append(pid); + outp.append(nwid); + const unsigned int dlen = dconf.sizeBytes(); + outp.append((uint16_t)dlen); + outp.append((const void *)dconf.data(),dlen); + outp.compress(); + RR->sw->send(outp,true,0); } } break; diff --git a/node/Network.cpp b/node/Network.cpp index 076977a8f..82011c3d5 100644 --- a/node/Network.cpp +++ b/node/Network.cpp @@ -64,9 +64,13 @@ Network::Network(const RuntimeEnvironment *renv,uint64_t nwid,void *uptr) : try { std::string conf(RR->node->dataStoreGet(confn)); if (conf.length()) { - 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 - gotConf = true; + Dictionary dconf(conf.c_str()); + NetworkConfig nconf; + if (nconf.fromDictionary(dconf)) { + this->setConfiguration(nconf,false); + _lastConfigUpdate = 0; // we still want to re-request a new config from the network + gotConf = true; + } } } catch ( ... ) {} // ignore invalids, we'll re-request @@ -177,49 +181,21 @@ bool Network::applyConfiguration(const NetworkConfig &conf) return false; } -int Network::setConfiguration(const void *confBytes,unsigned int confLen,bool saveToDisk) +int Network::setConfiguration(const NetworkConfig &nconf,bool saveToDisk) { try { - if (confLen <= 1) - return 0; - - NetworkConfig newConfig; - - // Find the length of any string-serialized old-style Dictionary, - // including its terminating NULL (if any). If this is before - // the end of the config, that tells us there is a new-style - // binary config which is preferred. - unsigned int dictLen = 0; - while (dictLen < confLen) { - if (!(reinterpret_cast(confBytes)[dictLen++])) - break; - } - - if (dictLen < (confLen - 2)) { - Buffer<8194> tmp(reinterpret_cast(confBytes) + dictLen,confLen - dictLen); - newConfig.deserialize(tmp,0); - } else { -#ifdef ZT_SUPPORT_OLD_STYLE_NETCONF - newConfig.fromDictionary(reinterpret_cast(confBytes),confLen); // throws if invalid -#else - return 0; -#endif - } - - if (!newConfig) - return 0; - { Mutex::Lock _l(_lock); - if (_config == newConfig) + if (_config == nconf) return 1; // OK config, but duplicate of what we already have } - - if (applyConfiguration(newConfig)) { + if (applyConfiguration(nconf)) { if (saveToDisk) { - char n[128]; + char n[64]; Utils::snprintf(n,sizeof(n),"networks.d/%.16llx.conf",_id); - RR->node->dataStorePut(n,confBytes,confLen,true); + Dictionary d; + if (nconf.toDictionary(d,false)) + RR->node->dataStorePut(n,(const void *)d.data(),d.sizeBytes(),true); } return 2; // OK and configuration has changed } @@ -234,12 +210,19 @@ void Network::requestConfiguration() if (_id == ZT_TEST_NETWORK_ID) // pseudo-network-ID, uses locally generated static config return; + Dictionary rmd; + rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_VERSION,(uint64_t)ZT_NETWORKCONFIG_VERSION); + rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_PROTOCOL_VERSION,(uint64_t)ZT_PROTO_VERSION); + rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MAJOR_VERSION,(uint64_t)ZEROTIER_ONE_VERSION_MAJOR); + rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MINOR_VERSION,(uint64_t)ZEROTIER_ONE_VERSION_MINOR); + rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_REVISION,(uint64_t)ZEROTIER_ONE_VERSION_REVISION); + if (controller() == RR->identity.address()) { if (RR->localNetworkController) { - Buffer<8194> tmp; - switch(RR->localNetworkController->doNetworkConfigRequest(InetAddress(),RR->identity,RR->identity,_id,NetworkConfigRequestMetaData(),tmp)) { + NetworkConfig nconf; + switch(RR->localNetworkController->doNetworkConfigRequest(InetAddress(),RR->identity,RR->identity,_id,rmd,nconf)) { case NetworkController::NETCONF_QUERY_OK: - this->setConfiguration(tmp.data(),tmp.size(),true); + this->setConfiguration(nconf,true); return; case NetworkController::NETCONF_QUERY_OBJECT_NOT_FOUND: this->setNotFound(); @@ -258,16 +241,13 @@ void Network::requestConfiguration() TRACE("requesting netconf for network %.16llx from controller %s",(unsigned long long)_id,controller().toString().c_str()); - NetworkConfigRequestMetaData metaData; - metaData.initWithDefaults(); - Buffer<4096> mds; - metaData.serialize(mds); // this always includes legacy fields to support old controllers - Packet outp(controller(),RR->identity.address(),Packet::VERB_NETWORK_CONFIG_REQUEST); outp.append((uint64_t)_id); - outp.append((uint16_t)mds.size()); - outp.append(mds.data(),mds.size()); + const unsigned int rmdSize = rmd.sizeBytes(); + outp.append((uint16_t)rmdSize); + outp.append((const void *)rmd.data(),rmdSize); outp.append((_config) ? (uint64_t)_config.revision : (uint64_t)0); + outp.compress(); RR->sw->send(outp,true,0); } diff --git a/node/Network.hpp b/node/Network.hpp index f316e050f..17eed4bdc 100644 --- a/node/Network.hpp +++ b/node/Network.hpp @@ -151,12 +151,11 @@ public: /** * Set or update this network's configuration * - * @param confBytes Network configuration in old-style Dictionary or new-style serialized format - * @param confLen Length of network configuration in bytes + * @param nconf Network configuration * @param saveToDisk IF true (default), write config to disk * @return 0 -- rejected, 1 -- accepted but not new, 2 -- accepted new config */ - int setConfiguration(const void *confBytes,unsigned int confLen,bool saveToDisk); + int setConfiguration(const NetworkConfig &nconf,bool saveToDisk); /** * Set netconf failure to 'access denied' -- called in IncomingPacket when controller reports this diff --git a/node/NetworkConfig.cpp b/node/NetworkConfig.cpp index 58a48fa0e..0f2223407 100644 --- a/node/NetworkConfig.cpp +++ b/node/NetworkConfig.cpp @@ -23,158 +23,460 @@ namespace ZeroTier { -#ifdef ZT_SUPPORT_OLD_STYLE_NETCONF - -void NetworkConfig::fromDictionary(const char *ds,unsigned int dslen) +bool NetworkConfig::toDictionary(Dictionary &d,bool includeLegacy) const { - static const std::string zero("0"); - static const std::string one("1"); + Buffer tmp; - Dictionary d(ds,dslen); + d.clear(); - memset(this,0,sizeof(NetworkConfig)); + // Try to put the more human-readable fields first - // NOTE: d.get(name) throws if not found, d.get(name,default) returns default + if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_VERSION,(uint64_t)ZT_NETWORKCONFIG_VERSION)) return false; + if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID,this->networkId)) return false; + if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP,this->timestamp)) return false; + if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_REVISION,this->revision)) return false; + if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO,this->issuedTo)) return false; + if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_FLAGS,this->flags)) return false; + if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT,(uint64_t)this->multicastLimit)) return false; + if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_TYPE,(uint64_t)this->type)) return false; + if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_NAME,this->name)) return false; - networkId = Utils::hexStrToU64(d.get(ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID,"0").c_str()); - if (!networkId) - throw std::invalid_argument("configuration contains zero network ID"); +#ifdef ZT_SUPPORT_OLD_STYLE_NETCONF + if (includeLegacy) { + if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_ALLOW_PASSIVE_BRIDGING_OLD,this->allowPassiveBridging())) return false; + if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST_OLD,this->enableBroadcast())) return false; + if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_PRIVATE_OLD,this->isPrivate())) return false; - 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 - issuedTo = Address(d.get(ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO,"0")); + std::string v4s; + for(unsigned int i=0;istaticIps[i].ss_family == AF_INET) { + if (v4s.length() > 0) + v4s.push_back(','); + v4s.append(this->staticIps[i].toString()); + } + } + if (v4s.length() > 0) { + if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC_OLD,v4s.c_str())) return false; + } + std::string v6s; + for(unsigned int i=0;istaticIps[i].ss_family == AF_INET6) { + if (v6s.length() > 0) + v6s.push_back(','); + v6s.append(this->staticIps[i].toString()); + } + } + if (v6s.length() > 0) { + if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC_OLD,v6s.c_str())) return false; + } - multicastLimit = Utils::hexStrToUInt(d.get(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT,zero).c_str()); - 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_ENABLE_BROADCAST,one).c_str()) != 0) ? ZT_NETWORKCONFIG_FLAG_ENABLE_BROADCAST : 0); - - 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,"")); - for(unsigned long i=0;((i activeBridgesSplit(Utils::split(d.get(ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES,"").c_str(),",","","")); - for(std::vector::const_iterator a(activeBridgesSplit.begin());a!=activeBridgesSplit.end();++a) { - if (a->length() == ZT_ADDRESS_LENGTH_HEX) { // ignore empty or garbage fields - Address tmp(*a); - if (!tmp.isReserved()) { - uint64_t specialist = tmp.toInt(); - for(unsigned int i=0;i 0) + ets.push_back(','); + char tmp[16]; + Utils::snprintf(tmp,sizeof(tmp),"%x",et); + ets.append(tmp); } - if ((specialist)&&(specialistCount < ZT_MAX_NETWORK_SPECIALISTS)) - specialists[specialistCount++] = specialist | ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE; + et = 0; + } + lastrt = rt; + } + if (ets.length() > 0) { + if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES_OLD,ets.c_str())) return false; + } + + if (this->com) { + if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP_OLD,this->com.toString().c_str())) return false; + } + + std::string ab; + for(unsigned int i=0;ispecialistCount;++i) { + if ((this->specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE) != 0) { + if (ab.length() > 0) + ab.push_back(','); + ab.append(Address(this->specialists[i]).toString().c_str()); } } - } - - 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())); - if (v6s.length()) { - if (ipAddrs.length()) - ipAddrs.push_back(','); - ipAddrs.append(v6s); + if (ab.length() > 0) { + if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES_OLD,ab.c_str())) return false; } - } - std::vector ipAddrsSplit(Utils::split(ipAddrs.c_str(),",","","")); - for(std::vector::const_iterator ipstr(ipAddrsSplit.begin());ipstr!=ipAddrsSplit.end();++ipstr) { - InetAddress addr(*ipstr); - switch(addr.ss_family) { - case AF_INET: - if ((!addr.netmaskBits())||(addr.netmaskBits() > 32)) - continue; - break; - case AF_INET6: - if ((!addr.netmaskBits())||(addr.netmaskBits() > 128)) - continue; - break; - default: // ignore unrecognized address types or junk/empty fields - continue; - } - if (!addr.isNetwork()) { - if ((staticIpCount < ZT_MAX_ZT_ASSIGNED_ADDRESSES)&&(std::find(&(staticIps[0]),&(staticIps[staticIpCount]),addr) == &(staticIps[staticIpCount]))) - staticIps[staticIpCount++] = addr; - } - } - std::sort(&(staticIps[0]),&(staticIps[staticIpCount])); - /* Old versions don't support gateways anyway, so ignore this in old netconfs - std::vector gatewaysSplit(Utils::split(d.get(ZT_NETWORKCONFIG_DICT_KEY_GATEWAYS,"").c_str(),",","","")); - for(std::vector::const_iterator gwstr(gatewaysSplit.begin());gwstr!=gatewaysSplit.end();++gwstr) { - InetAddress gw(*gwstr); - if ((gw)&&(_gatewayCount < ZT_MAX_NETWORK_GATEWAYS)&&(std::find(&(_gateways[0]),&(_gateways[_gatewayCount]),gw) == &(_gateways[_gatewayCount]))) - _gateways[_gatewayCount++] = gw; - } - std::sort(&(_gateways[0]),&(_gateways[_gatewayCount])); - */ - - std::vector relaysSplit(Utils::split(d.get(ZT_NETWORKCONFIG_DICT_KEY_RELAYS,"").c_str(),",","","")); - for(std::vector::const_iterator r(relaysSplit.begin());r!=relaysSplit.end();++r) { - if (r->length() >= ZT_ADDRESS_LENGTH_HEX) { - Address zt(r->substr(0,ZT_ADDRESS_LENGTH_HEX).c_str()); - InetAddress phy[2]; - unsigned int phyCount = 0; - const std::size_t semi(r->find(';')); - if ((semi > ZT_ADDRESS_LENGTH_HEX)&&(semi < (r->length() - 2))) { - std::vector phySplit(Utils::split(r->substr(semi+1).c_str(),",","","")); - for(std::vector::const_iterator p(phySplit.begin());((p!=phySplit.end())&&(phyCount < 2));++p) { - phy[phyCount] = InetAddress(*p); - if (phy[phyCount]) - ++phyCount; - else phy[phyCount].zero(); - } - } - - uint64_t specialist = zt.toInt(); - for(unsigned int i=0;i rvec(this->relays()); + std::string rl; + for(std::vector::const_iterator i(rvec.begin());i!=rvec.end();++i) { + if (rl.length() > 0) + rl.push_back(','); + rl.append(i->address.toString()); + if (i->phy4) { + rl.push_back(';'); + rl.append(i->phy4.toString()); + } else if (i->phy6) { + rl.push_back(';'); + rl.append(i->phy6.toString()); } } - } - - std::vector ets(Utils::split(d.get(ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES,"").c_str(),",","","")); - for(std::vector::const_iterator et(ets.begin());et!=ets.end();++et) { - unsigned int et2 = Utils::hexStrToUInt(et->c_str()) & 0xffff; - if ((ruleCount + 1) < ZT_MAX_NETWORK_RULES) { - if (et2) { - rules[ruleCount].t = ZT_NETWORK_RULE_MATCH_ETHERTYPE; - rules[ruleCount].v.etherType = (uint16_t)et2; - ++ruleCount; - } - rules[ruleCount++].t = ZT_NETWORK_RULE_ACTION_ACCEPT; + if (rl.length() > 0) { + if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_RELAYS_OLD,rl.c_str())) return false; } } - - this->com.fromString(d.get(ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP,std::string())); -} - #endif // ZT_SUPPORT_OLD_STYLE_NETCONF + // Then add binary blobs + + if (this->com) { + tmp.clear(); + this->com.serialize(tmp); + if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_COM,tmp)) return false; + } + + tmp.clear(); + for(unsigned int i=0;ispecialistCount;++i) { + tmp.append((uint64_t)this->specialists[i]); + } + if (tmp.size()) { + if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_SPECIALISTS,tmp)) return false; + } + + tmp.clear(); + for(unsigned int i=0;irouteCount;++i) { + reinterpret_cast(&(this->routes[i].target))->serialize(tmp); + reinterpret_cast(&(this->routes[i].via))->serialize(tmp); + tmp.append((uint16_t)this->routes[i].flags); + tmp.append((uint16_t)this->routes[i].metric); + } + if (tmp.size()) { + if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_ROUTES,tmp)) return false; + } + + tmp.clear(); + for(unsigned int i=0;istaticIpCount;++i) { + this->staticIps[i].serialize(tmp); + } + if (tmp.size()) { + if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_STATIC_IPS,tmp)) return false; + } + + tmp.clear(); + for(unsigned int i=0;ipinnedCount;++i) { + this->pinned[i].zt.appendTo(tmp); + this->pinned[i].phy.serialize(tmp); + } + if (tmp.size()) { + if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_PINNED,tmp)) return false; + } + + tmp.clear(); + for(unsigned int i=0;iruleCount;++i) { + tmp.append((uint8_t)rules[i].t); + switch((ZT_VirtualNetworkRuleType)(rules[i].t & 0x7f)) { + //case ZT_NETWORK_RULE_ACTION_DROP: + //case ZT_NETWORK_RULE_ACTION_ACCEPT: + default: + tmp.append((uint8_t)0); + break; + case ZT_NETWORK_RULE_ACTION_TEE: + case ZT_NETWORK_RULE_ACTION_REDIRECT: + case ZT_NETWORK_RULE_MATCH_SOURCE_ZEROTIER_ADDRESS: + case ZT_NETWORK_RULE_MATCH_DEST_ZEROTIER_ADDRESS: + tmp.append((uint8_t)5); + Address(rules[i].v.zt).appendTo(tmp); + break; + case ZT_NETWORK_RULE_MATCH_VLAN_ID: + tmp.append((uint8_t)2); + tmp.append((uint16_t)rules[i].v.vlanId); + break; + case ZT_NETWORK_RULE_MATCH_VLAN_PCP: + tmp.append((uint8_t)1); + tmp.append((uint8_t)rules[i].v.vlanPcp); + break; + case ZT_NETWORK_RULE_MATCH_VLAN_DEI: + tmp.append((uint8_t)1); + tmp.append((uint8_t)rules[i].v.vlanDei); + break; + case ZT_NETWORK_RULE_MATCH_ETHERTYPE: + tmp.append((uint8_t)2); + tmp.append((uint16_t)rules[i].v.etherType); + break; + case ZT_NETWORK_RULE_MATCH_MAC_SOURCE: + case ZT_NETWORK_RULE_MATCH_MAC_DEST: + tmp.append((uint8_t)6); + tmp.append(rules[i].v.mac,6); + break; + case ZT_NETWORK_RULE_MATCH_IPV4_SOURCE: + case ZT_NETWORK_RULE_MATCH_IPV4_DEST: + tmp.append((uint8_t)5); + tmp.append(&(rules[i].v.ipv4.ip),4); + tmp.append((uint8_t)rules[i].v.ipv4.mask); + break; + case ZT_NETWORK_RULE_MATCH_IPV6_SOURCE: + case ZT_NETWORK_RULE_MATCH_IPV6_DEST: + tmp.append((uint8_t)17); + tmp.append(rules[i].v.ipv6.ip,16); + tmp.append((uint8_t)rules[i].v.ipv6.mask); + break; + case ZT_NETWORK_RULE_MATCH_IP_TOS: + tmp.append((uint8_t)1); + tmp.append((uint8_t)rules[i].v.ipTos); + break; + case ZT_NETWORK_RULE_MATCH_IP_PROTOCOL: + tmp.append((uint8_t)1); + tmp.append((uint8_t)rules[i].v.ipProtocol); + break; + case ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE: + case ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE: + tmp.append((uint8_t)4); + tmp.append((uint16_t)rules[i].v.port[0]); + tmp.append((uint16_t)rules[i].v.port[1]); + break; + case ZT_NETWORK_RULE_MATCH_CHARACTERISTICS: + tmp.append((uint8_t)8); + tmp.append((uint64_t)rules[i].v.characteristics); + break; + case ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE: + tmp.append((uint8_t)4); + tmp.append((uint16_t)rules[i].v.frameSize[0]); + tmp.append((uint16_t)rules[i].v.frameSize[1]); + break; + case ZT_NETWORK_RULE_MATCH_TCP_RELATIVE_SEQUENCE_NUMBER_RANGE: + tmp.append((uint8_t)8); + tmp.append((uint32_t)rules[i].v.tcpseq[0]); + tmp.append((uint32_t)rules[i].v.tcpseq[1]); + break; + } + } + if (tmp.size()) { + if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_RULES,tmp)) return false; + } + + return true; +} + +bool NetworkConfig::fromDictionary(const Dictionary &d) +{ + try { + Buffer tmp; + char tmp2[ZT_DICTIONARY_MAX_SIZE]; + + memset(this,0,sizeof(NetworkConfig)); + + const uint64_t ver = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_VERSION,0); + + // Fields that are always present, new or old + this->networkId = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID,0); + if (this->networkId) + return false; + this->timestamp = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP,0); + this->revision = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_REVISION,0); + this->issuedTo = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO,0); + if (!this->issuedTo) + return false; + this->multicastLimit = (unsigned int)d.getUI(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT,0); + d.get(ZT_NETWORKCONFIG_DICT_KEY_NAME,this->name,sizeof(this->name)); + + if (ver < ZT_NETWORKCONFIG_VERSION) { + #ifdef ZT_SUPPORT_OLD_STYLE_NETCONF + // Decode legacy fields if version is old + if (d.getB(ZT_NETWORKCONFIG_DICT_KEY_ALLOW_PASSIVE_BRIDGING_OLD)) + this->flags |= ZT_NETWORKCONFIG_FLAG_ALLOW_PASSIVE_BRIDGING; + if (d.getB(ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST_OLD)) + this->flags |= ZT_NETWORKCONFIG_FLAG_ENABLE_BROADCAST; + this->type = (d.getB(ZT_NETWORKCONFIG_DICT_KEY_PRIVATE_OLD,true)) ? ZT_NETWORK_TYPE_PRIVATE : ZT_NETWORK_TYPE_PUBLIC; + + if (d.get(ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC_OLD,tmp2,sizeof(tmp2)) > 0) { + char *saveptr = (char *)0; + for(char *f=Utils::stok(tmp2,",",&saveptr);(f);f=Utils::stok((char *)0,",",&saveptr)) { + if (this->staticIpCount >= ZT_MAX_ZT_ASSIGNED_ADDRESSES) break; + this->staticIps[this->staticIpCount++] = InetAddress(f); + } + } + if (d.get(ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC_OLD,tmp2,sizeof(tmp2)) > 0) { + char *saveptr = (char *)0; + for(char *f=Utils::stok(tmp2,",",&saveptr);(f);f=Utils::stok((char *)0,",",&saveptr)) { + if (this->staticIpCount >= ZT_MAX_ZT_ASSIGNED_ADDRESSES) break; + this->staticIps[this->staticIpCount++] = InetAddress(f); + } + } + + if (d.get(ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP_OLD,tmp2,sizeof(tmp2)) > 0) { + this->com.fromString(tmp2); + } + + if (d.get(ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES_OLD,tmp2,sizeof(tmp2)) > 0) { + char *saveptr = (char *)0; + for(char *f=Utils::stok(tmp2,",",&saveptr);(f);f=Utils::stok((char *)0,",",&saveptr)) { + unsigned int et = Utils::hexStrToUInt(f) & 0xffff; + if ((this->ruleCount + 2) > ZT_MAX_NETWORK_RULES) break; + if (et > 0) { + this->rules[this->ruleCount].t = (uint8_t)ZT_NETWORK_RULE_MATCH_ETHERTYPE; + this->rules[this->ruleCount].v.etherType = (uint16_t)et; + ++this->ruleCount; + } + this->rules[this->ruleCount++].t = (uint8_t)ZT_NETWORK_RULE_ACTION_ACCEPT; + } + } else { + this->rules[0].t = ZT_NETWORK_RULE_ACTION_ACCEPT; + this->ruleCount = 1; + } + + if (d.get(ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES_OLD,tmp2,sizeof(tmp2)) > 0) { + char *saveptr = (char *)0; + for(char *f=Utils::stok(tmp2,",",&saveptr);(f);f=Utils::stok((char *)0,",",&saveptr)) { + this->addSpecialist(Address(f),ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE); + } + } + + if (d.get(ZT_NETWORKCONFIG_DICT_KEY_RELAYS_OLD,tmp2,sizeof(tmp2)) > 0) { + char *saveptr = (char *)0; + for(char *f=Utils::stok(tmp2,",",&saveptr);(f);f=Utils::stok((char *)0,",",&saveptr)) { + char tmp3[256]; + Utils::scopy(tmp3,sizeof(tmp3),f); + + InetAddress phy; + char *semi = tmp3; + while (*semi) { + if (*semi == ';') { + *semi = (char)0; + ++semi; + phy = InetAddress(semi); + } else ++semi; + } + Address zt(tmp3); + + this->addSpecialist(zt,ZT_NETWORKCONFIG_SPECIALIST_TYPE_NETWORK_PREFERRED_RELAY); + if ((phy)&&(this->pinnedCount < ZT_MAX_NETWORK_PINNED)) { + this->pinned[this->pinnedCount].zt = zt; + this->pinned[this->pinnedCount].phy = phy; + ++this->pinnedCount; + } + } + } + #else + return false; + #endif // ZT_SUPPORT_OLD_STYLE_NETCONF + } else { + // Otherwise we can use the new fields + this->flags = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_FLAGS,0); + this->type = (ZT_VirtualNetworkType)d.getUI(ZT_NETWORKCONFIG_DICT_KEY_TYPE,(uint64_t)ZT_NETWORK_TYPE_PRIVATE); + + if (d.get(ZT_NETWORKCONFIG_DICT_KEY_COM,tmp)) { + this->com.deserialize(tmp,0); + } + + if (d.get(ZT_NETWORKCONFIG_DICT_KEY_SPECIALISTS,tmp)) { + unsigned int p = 0; + while (((p + 8) <= tmp.size())&&(specialistCount < ZT_MAX_NETWORK_SPECIALISTS)) { + this->specialists[this->specialistCount++] = tmp.at(p); + p += 8; + } + } + + if (d.get(ZT_NETWORKCONFIG_DICT_KEY_ROUTES,tmp)) { + unsigned int p = 0; + while ((p < tmp.size())&&(routeCount < ZT_MAX_NETWORK_ROUTES)) { + p += reinterpret_cast(&(this->routes[this->routeCount].target))->deserialize(tmp,p); + p += reinterpret_cast(&(this->routes[this->routeCount].via))->deserialize(tmp,p); + this->routes[this->routeCount].flags = tmp.at(p); p += 2; + this->routes[this->routeCount].metric = tmp.at(p); p += 2; + ++this->routeCount; + } + } + + if (d.get(ZT_NETWORKCONFIG_DICT_KEY_STATIC_IPS,tmp)) { + unsigned int p = 0; + while ((p < tmp.size())&&(staticIpCount < ZT_MAX_ZT_ASSIGNED_ADDRESSES)) { + p += this->staticIps[this->staticIpCount++].deserialize(tmp,p); + } + } + + if (d.get(ZT_NETWORKCONFIG_DICT_KEY_PINNED,tmp)) { + unsigned int p = 0; + while ((p < tmp.size())&&(pinnedCount < ZT_MAX_NETWORK_PINNED)) { + this->pinned[this->pinnedCount].zt.setTo(tmp.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH; + p += this->pinned[this->pinnedCount].phy.deserialize(tmp,p); + ++this->pinnedCount; + } + } + + if (d.get(ZT_NETWORKCONFIG_DICT_KEY_RULES,tmp)) { + unsigned int p = 0; + while ((p < tmp.size())&&(ruleCount < ZT_MAX_NETWORK_RULES)) { + rules[ruleCount].t = (uint8_t)tmp[p++]; + unsigned int fieldLen = (unsigned int)tmp[p++]; + switch((ZT_VirtualNetworkRuleType)(rules[ruleCount].t & 0x7f)) { + default: + break; + case ZT_NETWORK_RULE_ACTION_TEE: + case ZT_NETWORK_RULE_ACTION_REDIRECT: + case ZT_NETWORK_RULE_MATCH_SOURCE_ZEROTIER_ADDRESS: + case ZT_NETWORK_RULE_MATCH_DEST_ZEROTIER_ADDRESS: + rules[ruleCount].v.zt = Address(tmp.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH).toInt(); + break; + case ZT_NETWORK_RULE_MATCH_VLAN_ID: + rules[ruleCount].v.vlanId = tmp.at(p); + break; + case ZT_NETWORK_RULE_MATCH_VLAN_PCP: + rules[ruleCount].v.vlanPcp = (uint8_t)tmp[p]; + break; + case ZT_NETWORK_RULE_MATCH_VLAN_DEI: + rules[ruleCount].v.vlanDei = (uint8_t)tmp[p]; + break; + case ZT_NETWORK_RULE_MATCH_ETHERTYPE: + rules[ruleCount].v.etherType = tmp.at(p); + break; + case ZT_NETWORK_RULE_MATCH_MAC_SOURCE: + case ZT_NETWORK_RULE_MATCH_MAC_DEST: + memcpy(rules[ruleCount].v.mac,tmp.field(p,6),6); + break; + case ZT_NETWORK_RULE_MATCH_IPV4_SOURCE: + case ZT_NETWORK_RULE_MATCH_IPV4_DEST: + memcpy(&(rules[ruleCount].v.ipv4.ip),tmp.field(p,4),4); + rules[ruleCount].v.ipv4.mask = (uint8_t)tmp[p + 4]; + break; + case ZT_NETWORK_RULE_MATCH_IPV6_SOURCE: + case ZT_NETWORK_RULE_MATCH_IPV6_DEST: + memcpy(rules[ruleCount].v.ipv6.ip,tmp.field(p,16),16); + rules[ruleCount].v.ipv6.mask = (uint8_t)tmp[p + 16]; + break; + case ZT_NETWORK_RULE_MATCH_IP_TOS: + rules[ruleCount].v.ipTos = (uint8_t)tmp[p]; + break; + case ZT_NETWORK_RULE_MATCH_IP_PROTOCOL: + rules[ruleCount].v.ipProtocol = (uint8_t)tmp[p]; + break; + case ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE: + case ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE: + rules[ruleCount].v.port[0] = tmp.at(p); + rules[ruleCount].v.port[1] = tmp.at(p + 2); + break; + case ZT_NETWORK_RULE_MATCH_CHARACTERISTICS: + rules[ruleCount].v.characteristics = tmp.at(p); + break; + case ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE: + rules[ruleCount].v.frameSize[0] = tmp.at(p); + rules[ruleCount].v.frameSize[0] = tmp.at(p + 2); + break; + case ZT_NETWORK_RULE_MATCH_TCP_RELATIVE_SEQUENCE_NUMBER_RANGE: + rules[ruleCount].v.tcpseq[0] = tmp.at(p); + rules[ruleCount].v.tcpseq[1] = tmp.at(p + 4); + break; + } + p += fieldLen; + ++ruleCount; + } + } + } + return true; + } catch ( ... ) { + return false; + } +} + } // namespace ZeroTier diff --git a/node/NetworkConfig.hpp b/node/NetworkConfig.hpp index bf513df1c..4bc2546b5 100644 --- a/node/NetworkConfig.hpp +++ b/node/NetworkConfig.hpp @@ -35,21 +35,17 @@ #include "MulticastGroup.hpp" #include "Address.hpp" #include "CertificateOfMembership.hpp" - -#ifdef ZT_SUPPORT_OLD_STYLE_NETCONF #include "Dictionary.hpp" -#include -#endif /** * Flag: allow passive bridging (experimental) */ -#define ZT_NETWORKCONFIG_FLAG_ALLOW_PASSIVE_BRIDGING 0x0001 +#define ZT_NETWORKCONFIG_FLAG_ALLOW_PASSIVE_BRIDGING 0x0000000000000001ULL /** * Flag: enable broadcast */ -#define ZT_NETWORKCONFIG_FLAG_ENABLE_BROADCAST 0x0002 +#define ZT_NETWORKCONFIG_FLAG_ENABLE_BROADCAST 0x0000000000000002ULL /** * Device is a network preferred relay @@ -68,18 +64,20 @@ namespace ZeroTier { -#ifdef ZT_SUPPORT_OLD_STYLE_NETCONF +// Network config version +#define ZT_NETWORKCONFIG_VERSION 6 // Fields for meta-data sent with network config requests +#define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_VERSION "v" +#define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_PROTOCOL_VERSION "pv" #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MAJOR_VERSION "majv" #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MINOR_VERSION "minv" #define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_REVISION "revv" -// These dictionary keys are short so they don't take up much room in -// netconf response packets. +// These dictionary keys are short so they don't take up much room. -// integer(hex)[,integer(hex),...] -#define ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES "et" +// network config version +#define ZT_NETWORKCONFIG_DICT_KEY_VERSION "v" // network ID #define ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID "nwid" // integer(hex) @@ -88,34 +86,49 @@ namespace ZeroTier { #define ZT_NETWORKCONFIG_DICT_KEY_REVISION "r" // address of member #define ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO "id" +// flags(hex) +#define ZT_NETWORKCONFIG_DICT_KEY_FLAGS "f" // integer(hex) #define ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT "ml" -// 0/1 -#define ZT_NETWORKCONFIG_DICT_KEY_PRIVATE "p" +// network type (hex) +#define ZT_NETWORKCONFIG_DICT_KEY_TYPE "t" // text #define ZT_NETWORKCONFIG_DICT_KEY_NAME "n" -// text -#define ZT_NETWORKCONFIG_DICT_KEY_DESC "d" -// IP/bits[,IP/bits,...] -// Note that IPs that end in all zeroes are routes with no assignment in them. -#define ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC "v4s" -// IP/bits[,IP/bits,...] -// Note that IPs that end in all zeroes are routes with no assignment in them. -#define ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC "v6s" -// serialized CertificateOfMembership -#define ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP "com" -// 0/1 -#define ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST "eb" -// 0/1 -#define ZT_NETWORKCONFIG_DICT_KEY_ALLOW_PASSIVE_BRIDGING "pb" -// node[,node,...] -#define ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES "ab" -// node;IP/port[,node;IP/port] -#define ZT_NETWORKCONFIG_DICT_KEY_RELAYS "rl" -// IP/metric[,IP/metric,...] -#define ZT_NETWORKCONFIG_DICT_KEY_GATEWAYS "gw" +// binary serialized certificate of membership +#define ZT_NETWORKCONFIG_DICT_KEY_COM "C" +// specialists (binary array of uint64_t) +#define ZT_NETWORKCONFIG_DICT_KEY_SPECIALISTS "S" +// routes (binary blob) +#define ZT_NETWORKCONFIG_DICT_KEY_ROUTES "RT" +// static IPs (binary blob) +#define ZT_NETWORKCONFIG_DICT_KEY_STATIC_IPS "I" +// pinned address physical route mappings (binary blob) +#define ZT_NETWORKCONFIG_DICT_KEY_PINNED "P" +// rules (binary blob) +#define ZT_NETWORKCONFIG_DICT_KEY_RULES "R" -#endif // ZT_SUPPORT_OLD_STYLE_NETCONF +// Legacy fields -- these are obsoleted but are included when older clients query + +// boolean (now a flag) +#define ZT_NETWORKCONFIG_DICT_KEY_ALLOW_PASSIVE_BRIDGING_OLD "pb" +// boolean (now a flag) +#define ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST_OLD "eb" +// IP/bits[,IP/bits,...] +// Note that IPs that end in all zeroes are routes with no assignment in them. +#define ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC_OLD "v4s" +// IP/bits[,IP/bits,...] +// Note that IPs that end in all zeroes are routes with no assignment in them. +#define ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC_OLD "v6s" +// 0/1 +#define ZT_NETWORKCONFIG_DICT_KEY_PRIVATE_OLD "p" +// integer(hex)[,integer(hex),...] +#define ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES_OLD "et" +// string-serialized CertificateOfMembership +#define ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP_OLD "com" +// node[,node,...] +#define ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES_OLD "ab" +// node;IP/port[,node;IP/port] +#define ZT_NETWORKCONFIG_DICT_KEY_RELAYS_OLD "rl" /** * Network configuration received from network controller nodes @@ -214,6 +227,23 @@ public: return false; } + /** + * Write this network config to a dictionary for transport + * + * @param d Dictionary + * @param includeLegacy If true, include legacy fields for old node versions + * @return True if dictionary was successfully created, false if e.g. overflow + */ + bool toDictionary(Dictionary &d,bool includeLegacy) const; + + /** + * Read this network config from a dictionary + * + * @param d Dictionary + * @return True if dictionary was valid and network config successfully initialized + */ + bool fromDictionary(const Dictionary &d); + /** * @return True if passive bridging is allowed (experimental) */ @@ -350,269 +380,6 @@ public: inline bool operator==(const NetworkConfig &nc) const { return (memcmp(this,&nc,sizeof(NetworkConfig)) == 0); } inline bool operator!=(const NetworkConfig &nc) const { return (!(*this == nc)); } - template - inline void serialize(Buffer &b) const - { - b.append((uint16_t)1); // version - - b.append((uint64_t)networkId); - b.append((uint64_t)timestamp); - b.append((uint64_t)revision); - issuedTo.appendTo(b); - b.append((uint64_t)flags); - b.append((uint32_t)multicastLimit); - b.append((uint8_t)type); - - unsigned int nl = (unsigned int)strlen(name); - if (nl > 255) nl = 255; // sanity check - b.append((uint8_t)nl); - b.append((const void *)name,nl); - - b.append((uint16_t)specialistCount); - for(unsigned int i=0;i(&(routes[i].target))->serialize(b); - reinterpret_cast(&(routes[i].via))->serialize(b); - b.append((uint16_t)routes[i].flags); - b.append((uint16_t)routes[i].metric); - } - - b.append((uint16_t)staticIpCount); - for(unsigned int i=0;icom.serialize(b); - - b.append((uint16_t)0); // extended bytes, currently 0 since unused - } - - template - inline unsigned int deserialize(const Buffer &b,unsigned int startAt = 0) - { - memset(this,0,sizeof(NetworkConfig)); - - unsigned int p = startAt; - - if (b.template at(p) != 1) - throw std::invalid_argument("unrecognized version"); - p += 2; - - networkId = b.template at(p); p += 8; - timestamp = b.template at(p); p += 8; - revision = b.template at(p); p += 8; - issuedTo.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH; - flags = b.template at(p); p += 8; - multicastLimit = (unsigned int)b.template at(p); p += 4; - type = (ZT_VirtualNetworkType)b[p++]; - - unsigned int nl = (unsigned int)b[p++]; - memcpy(this->name,b.field(p,nl),std::min(nl,(unsigned int)ZT_MAX_NETWORK_SHORT_NAME_LENGTH)); - p += nl; - // _name will always be null terminated since field size is ZT_MAX_NETWORK_SHORT_NAME_LENGTH + 1 - - specialistCount = (unsigned int)b.template at(p); p += 2; - if (specialistCount > ZT_MAX_NETWORK_SPECIALISTS) - throw std::invalid_argument("overflow (specialists)"); - for(unsigned int i=0;i(p); p += 8; - } - - routeCount = (unsigned int)b.template at(p); p += 2; - if (routeCount > ZT_MAX_NETWORK_ROUTES) - throw std::invalid_argument("overflow (routes)"); - for(unsigned int i=0;i(&(routes[i].target))->deserialize(b,p); - p += reinterpret_cast(&(routes[i].via))->deserialize(b,p); - routes[i].flags = b.template at(p); p += 2; - routes[i].metric = b.template at(p); p += 2; - } - - staticIpCount = (unsigned int)b.template at(p); p += 2; - if (staticIpCount > ZT_MAX_ZT_ASSIGNED_ADDRESSES) - throw std::invalid_argument("overflow (static IPs)"); - for(unsigned int i=0;i(p); p += 2; - if (pinnedCount > ZT_MAX_NETWORK_PINNED) - throw std::invalid_argument("overflow (static addresses)"); - for(unsigned int i=0;i(p); p += 2; - if (ruleCount > ZT_MAX_NETWORK_RULES) - throw std::invalid_argument("overflow (rules)"); - for(unsigned int i=0;i(p); - break; - case ZT_NETWORK_RULE_MATCH_VLAN_PCP: - rules[i].v.vlanPcp = (uint8_t)b[p]; - break; - case ZT_NETWORK_RULE_MATCH_VLAN_DEI: - rules[i].v.vlanDei = (uint8_t)b[p]; - break; - case ZT_NETWORK_RULE_MATCH_ETHERTYPE: - rules[i].v.etherType = b.template at(p); - break; - case ZT_NETWORK_RULE_MATCH_MAC_SOURCE: - case ZT_NETWORK_RULE_MATCH_MAC_DEST: - memcpy(rules[i].v.mac,b.field(p,6),6); - break; - case ZT_NETWORK_RULE_MATCH_IPV4_SOURCE: - case ZT_NETWORK_RULE_MATCH_IPV4_DEST: - memcpy(&(rules[i].v.ipv4.ip),b.field(p,4),4); - rules[i].v.ipv4.mask = (uint8_t)b[p+4]; - break; - case ZT_NETWORK_RULE_MATCH_IPV6_SOURCE: - case ZT_NETWORK_RULE_MATCH_IPV6_DEST: - memcpy(rules[i].v.ipv6.ip,b.field(p,16),16); - rules[i].v.ipv6.mask = (uint8_t)b[p+16]; - break; - case ZT_NETWORK_RULE_MATCH_IP_TOS: - rules[i].v.ipTos = (uint8_t)b[p]; - break; - case ZT_NETWORK_RULE_MATCH_IP_PROTOCOL: - rules[i].v.ipProtocol = (uint8_t)b[p]; - break; - case ZT_NETWORK_RULE_MATCH_IP_SOURCE_PORT_RANGE: - case ZT_NETWORK_RULE_MATCH_IP_DEST_PORT_RANGE: - rules[i].v.port[0] = b.template at(p); - rules[i].v.port[1] = b.template at(p+2); - break; - case ZT_NETWORK_RULE_MATCH_CHARACTERISTICS: - rules[i].v.characteristics = b.template at(p); - break; - case ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE: - rules[i].v.frameSize[0] = b.template at(p); - rules[i].v.frameSize[1] = b.template at(p+2); - break; - case ZT_NETWORK_RULE_MATCH_TCP_RELATIVE_SEQUENCE_NUMBER_RANGE: - rules[i].v.tcpseq[0] = b.template at(p); - rules[i].v.tcpseq[1] = b.template at(p + 4); - break; - } - p += rlen; - } - - p += this->com.deserialize(b,p); - - p += b.template at(p) + 2; - - return (p - startAt); - } - -#ifdef ZT_SUPPORT_OLD_STYLE_NETCONF - void fromDictionary(const char *ds,unsigned int dslen); -#endif - /* inline void dump() const { @@ -629,6 +396,8 @@ public: for(unsigned int i=0;i(&(routes[i].target))->toString().c_str()); printf(" routes[i].via==%s\n",reinterpret_cast(&(routes[i].via))->toIpString().c_str()); + printf(" routes[i].flags==%.4x\n",(unsigned int)routes[i].flags); + printf(" routes[i].metric==%u\n",(unsigned int)routes[i].metric); } printf("staticIpCount==%u\n",staticIpCount); for(unsigned int i=0;i= ZT_MAX_NETWORK_SPECIALISTS) { + specialists[specialistCount++] = f | aint; + return true; + } + return false; + } + /** * Network ID that this configuration applies to */ diff --git a/node/NetworkConfigRequestMetaData.hpp b/node/NetworkConfigRequestMetaData.hpp deleted file mode 100644 index 2516e5e0c..000000000 --- a/node/NetworkConfigRequestMetaData.hpp +++ /dev/null @@ -1,196 +0,0 @@ -/* - * ZeroTier One - Network Virtualization Everywhere - * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/ - * - * 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 . - */ - -#ifndef ZT_NETWORKCONFIGREQUESTMETADATA_HPP -#define ZT_NETWORKCONFIGREQUESTMETADATA_HPP - -#include -#include -#include - -#include "Constants.hpp" -#include "NetworkConfig.hpp" -#include "Buffer.hpp" -#include "Packet.hpp" - -#include "../version.h" - -/** - * Maximum length of the auth field (including terminating NULL, since it's a C-style string) - * - * Actual max length not including NULL is this minus one. - */ -#define ZT_NETWORK_CONFIG_REQUEST_METADATA_MAX_AUTH_LENGTH 2048 - -namespace ZeroTier { - -/** - * Network configuration request meta data - */ -class NetworkConfigRequestMetaData -{ -public: - /** - * Construct an empty meta-data object with zero/null values - */ - NetworkConfigRequestMetaData() - { - memset(this,0,sizeof(NetworkConfigRequestMetaData)); - } - - /** - * Initialize with defaults from this node's config and version - */ - inline void initWithDefaults() - { - memset(this,0,sizeof(NetworkConfigRequestMetaData)); - 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; - protocolVersion = ZT_PROTO_VERSION; - } - - /** - * Zero/null everything - */ - inline void clear() - { - memset(this,0,sizeof(NetworkConfigRequestMetaData)); - } - - template - inline void serialize(Buffer &b) const - { - /* Unlike network config we always send the old fields. Newer network - * controllers will detect the presence of the new serialized data by - * detecting extra data after the terminating NULL. But always sending - * these maintains backward compatibility with old controllers. This - * appends a terminating NULL which seperates the old legacy meta-data - * from the new packed binary format that we send after. */ - b.appendCString("majv=" ZEROTIER_ONE_VERSION_MAJOR_S_HEX "\nminv=" ZEROTIER_ONE_VERSION_MINOR_S_HEX "\nrevv=" ZEROTIER_ONE_VERSION_REVISION_S_HEX "\n"); - - b.append((uint16_t)1); // serialization version - - b.append((uint64_t)buildId); - b.append((uint64_t)flags); - b.append((uint16_t)vendor); - b.append((uint16_t)platform); - b.append((uint16_t)architecture); - b.append((uint16_t)majorVersion); - b.append((uint16_t)minorVersion); - b.append((uint16_t)revision); - b.append((uint16_t)protocolVersion); - - const unsigned int tl = strlen(auth); - b.append((uint16_t)tl); - b.append((const void *)auth,tl); - - b.append((uint16_t)0); // extended bytes, currently 0 since unused - } - - template - inline unsigned int deserialize(const Buffer &b,unsigned int startAt = 0) - { - memset(this,0,sizeof(NetworkConfigRequestMetaData)); - - unsigned int p = startAt; - - // Seek past old style meta-data - while (b[p]) ++p; - ++p; - - if (b.template at(p) != 1) - throw std::invalid_argument("unrecognized version"); - p += 2; - - buildId = b.template at(p); p += 8; - flags = b.template at(p); p += 8; - vendor = (ZT_Vendor)b.template at(p); p += 2; - platform = (ZT_Platform)b.template at(p); p += 2; - architecture = (ZT_Architecture)b.template at(p); p += 2; - majorVersion = b.template at(p); p += 2; - minorVersion = b.template at(p); p += 2; - revision = b.template at(p); p += 2; - protocolVersion = b.template at(p); p += 2; - - const unsigned int tl = b.template at(p); p += 2; - memcpy(auth,b.field(p,tl),std::max(tl,(unsigned int)(ZT_NETWORK_CONFIG_REQUEST_METADATA_MAX_AUTH_LENGTH - 1))); - p += tl; - - p += b.template at(p) + 2; - - return (p - startAt); - } - - /** - * Authentication data (e.g. bearer=) as a C-style string (always null terminated) - */ - char auth[ZT_NETWORK_CONFIG_REQUEST_METADATA_MAX_AUTH_LENGTH]; - - /** - * Build ID (currently unused, must be 0) - */ - uint64_t buildId; - - /** - * Flags (currently unused, must be 0) - */ - uint64_t flags; - - /** - * ZeroTier vendor or 0 for unspecified - */ - ZT_Vendor vendor; - - /** - * ZeroTier platform or 0 for unspecified - */ - ZT_Platform platform; - - /** - * ZeroTier architecture or 0 for unspecified - */ - ZT_Architecture architecture; - - /** - * ZeroTier software major version - */ - unsigned int majorVersion; - - /** - * ZeroTier software minor version - */ - unsigned int minorVersion; - - /** - * ZeroTier software revision - */ - unsigned int revision; - - /** - * ZeroTier protocol version - */ - unsigned int protocolVersion; -}; - -} // namespace ZeroTier - -#endif diff --git a/node/NetworkController.hpp b/node/NetworkController.hpp index 4ab6403a6..b91ada1bd 100644 --- a/node/NetworkController.hpp +++ b/node/NetworkController.hpp @@ -22,15 +22,15 @@ #include #include "Constants.hpp" -#include "InetAddress.hpp" -#include "Address.hpp" -#include "Identity.hpp" -#include "NetworkConfigRequestMetaData.hpp" -#include "Buffer.hpp" namespace ZeroTier { class RuntimeEnvironment; +class NetworkConfig; +class Dictionary; +class Identity; +class Address; +struct InetAddress; /** * Interface for network controller implementations @@ -67,16 +67,16 @@ public: * @param identity Originating peer ZeroTier identity * @param nwid 64-bit network ID * @param metaData Meta-data bundled with request (if any) - * @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 + * @param nc NetworkConfig to fill with results + * @return Returns NETCONF_QUERY_OK if result 'nc' is valid, or an error code on error */ virtual NetworkController::ResultCode doNetworkConfigRequest( const InetAddress &fromAddr, const Identity &signingId, const Identity &identity, uint64_t nwid, - const NetworkConfigRequestMetaData &metaData, - Buffer<8194> &result) = 0; + const Dictionary &metaData, + NetworkConfig &nc) = 0; }; } // namespace ZeroTier diff --git a/node/Utils.cpp b/node/Utils.cpp index 00aeea337..2d9515eed 100644 --- a/node/Utils.cpp +++ b/node/Utils.cpp @@ -262,6 +262,24 @@ std::vector Utils::split(const char *s,const char *const sep,const return fields; } +bool Utils::scopy(char *dest,unsigned int len,const char *src) +{ + if (!len) + return false; // sanity check + if (!src) { + *dest = (char)0; + return true; + } + char *end = dest + len; + while ((*dest++ = *src++)) { + if (dest == end) { + *(--dest) = (char)0; + return false; + } + } + return true; +} + unsigned int Utils::snprintf(char *buf,unsigned int len,const char *fmt,...) throw(std::length_error) { diff --git a/node/Utils.hpp b/node/Utils.hpp index 04838f1cd..cfe565011 100644 --- a/node/Utils.hpp +++ b/node/Utils.hpp @@ -49,7 +49,6 @@ public: * @return True if strings are equal */ static inline bool secureEq(const void *a,const void *b,unsigned int len) - throw() { uint8_t diff = 0; for(unsigned int i=0;i> 1) & (uint32_t)0x55555555); v = (v & (uint32_t)0x33333333) + ((v >> 2) & (uint32_t)0x33333333); @@ -290,7 +272,6 @@ public: * @return True if memory is all zero */ static inline bool isZero(const void *p,unsigned int len) - throw() { for(unsigned int i=0;i= 0) { + if (strcmp(value[r],tmp)) { + std::cout << "FAILED (invalid value)!" << std::endl; + return -1; + } + } else { + std::cout << "FAILED (can't find key)!" << std::endl; + return -1; + } + } + } + int foo = 0; + volatile int *volatile bar = &foo; // force compiler not to optimize out test.get() below + for(int k=0;k<100000;++k) { + int r = rand() % 16384; + unsigned char tmp[16384]; + for(int q=0;q