From 548730660be094ddbbd5d7c918e6c5d16b6233b8 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Wed, 11 May 2016 10:19:14 -0700 Subject: [PATCH] Ready to test whole new netconf refactor. --- node/IncomingPacket.cpp | 18 ++++-- node/Network.cpp | 18 +++--- node/NetworkConfig.hpp | 9 +++ node/NetworkConfigRequestMetaData.hpp | 81 +++++++++++++++++---------- version.h | 6 +- 5 files changed, 83 insertions(+), 49 deletions(-) diff --git a/node/IncomingPacket.cpp b/node/IncomingPacket.cpp index 2abd8840c..84df7de32 100644 --- a/node/IncomingPacket.cpp +++ b/node/IncomingPacket.cpp @@ -681,12 +681,18 @@ bool IncomingPacket::_doNETWORK_CONFIG_REQUEST(const RuntimeEnvironment *RR,cons 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(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(); + 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); diff --git a/node/Network.cpp b/node/Network.cpp index a4384dfda..8e9aecbd3 100644 --- a/node/Network.cpp +++ b/node/Network.cpp @@ -256,20 +256,16 @@ void Network::requestConfiguration() TRACE("requesting netconf for network %.16llx from controller %s",(unsigned long long)_id,controller().toString().c_str()); - // TODO: in the future we will include things like join tokens here, etc. - Dictionary metaData; - metaData.setHex(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MAJOR_VERSION,ZEROTIER_ONE_VERSION_MAJOR); - metaData.setHex(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MINOR_VERSION,ZEROTIER_ONE_VERSION_MINOR); - metaData.setHex(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_REVISION,ZEROTIER_ONE_VERSION_REVISION); - std::string mds(metaData.toString()); + 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.length()); - outp.append((const void *)mds.data(),(unsigned int)mds.length()); - if (_config) - outp.append((uint64_t)_config.revision); - else outp.append((uint64_t)0); + outp.append((uint16_t)mds.size()); + outp.append(mds.data(),mds.size()); + outp.append((_config) ? (uint64_t)_config.revision : (uint64_t)0); RR->sw->send(outp,true,0); } diff --git a/node/NetworkConfig.hpp b/node/NetworkConfig.hpp index 5af2c9e72..1bbf6506e 100644 --- a/node/NetworkConfig.hpp +++ b/node/NetworkConfig.hpp @@ -460,6 +460,11 @@ public: b.append((uint16_t)rules[i].v.frameSize[0]); b.append((uint16_t)rules[i].v.frameSize[1]); break; + case ZT_NETWORK_RULE_MATCH_TCP_RELATIVE_SEQUENCE_NUMBER_RANGE: + b.append((uint8_t)8); + b.append((uint32_t)rules[i].v.tcpseq[0]); + b.append((uint32_t)rules[i].v.tcpseq[1]); + break; } } @@ -585,6 +590,10 @@ public: 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; } diff --git a/node/NetworkConfigRequestMetaData.hpp b/node/NetworkConfigRequestMetaData.hpp index 6c6f25430..831ca63a9 100644 --- a/node/NetworkConfigRequestMetaData.hpp +++ b/node/NetworkConfigRequestMetaData.hpp @@ -26,9 +26,17 @@ #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 { /** @@ -37,20 +45,33 @@ namespace ZeroTier { class NetworkConfigRequestMetaData { public: - 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) + /** + * Construct an empty meta-data object with zero/null values + */ + NetworkConfigRequestMetaData() { - memset(auth,0,sizeof(auth)); + memset(this,0,sizeof(NetworkConfigRequestMetaData)); } - NetworkConfigRequestMetaData(bool foo) + /** + * 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)); } @@ -58,13 +79,15 @@ public: 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. - b.appendCString("majv="ZEROTIER_ONE_VERSION_MAJOR_S"\nminv="ZEROTIER_ONE_VERSION_MINOR_S"\nrevv="ZEROTIER_ONE_VERSION_REVISION_S"\n"); + /* 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); // version + b.append((uint16_t)1); // serialization version b.append((uint64_t)buildId); b.append((uint64_t)flags); @@ -74,10 +97,10 @@ public: b.append((uint16_t)majorVersion); b.append((uint16_t)minorVersion); b.append((uint16_t)revision); + b.append((uint16_t)protocolVersion); - unsigned int tl = (unsigned int)strlen(auth); - if (tl > 255) tl = 255; // sanity check - b.append((uint8_t)tl); + 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 @@ -105,10 +128,10 @@ public: 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; - unsigned int tl = (unsigned int)b[p++]; - memcpy(auth,b.field(p,tl),std::max(tl,(unsigned int)ZT_MAX_NETWORK_SHORT_NAME_LENGTH)); - // auth[] is ZT_MAX_NETWORK_SHORT_NAME_LENGTH + 1 and so will always end up null-terminated since we zeroed the structure + 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; @@ -116,10 +139,10 @@ public: return (p - startAt); } - inline void clear() - { - memset(this,0,sizeof(NetworkConfigRequestMetaData)); - } + /** + * 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) @@ -162,9 +185,9 @@ public: unsigned int revision; /** - * Authentication data (e.g. bearer=) + * ZeroTier protocol version */ - char auth[ZT_MAX_NETWORK_SHORT_NAME_LENGTH + 1]; + unsigned int protocolVersion; }; } // namespace ZeroTier diff --git a/version.h b/version.h index 70815daf2..526a3cd9c 100644 --- a/version.h +++ b/version.h @@ -23,18 +23,18 @@ * Major version */ #define ZEROTIER_ONE_VERSION_MAJOR 1 -#define ZEROTIER_ONE_VERSION_MAJOR_S "1" +#define ZEROTIER_ONE_VERSION_MAJOR_S_HEX "1" /** * Minor version */ #define ZEROTIER_ONE_VERSION_MINOR 1 -#define ZEROTIER_ONE_VERSION_MINOR_S "1" +#define ZEROTIER_ONE_VERSION_MINOR_S_HEX "1" /** * Revision */ #define ZEROTIER_ONE_VERSION_REVISION 5 -#define ZEROTIER_ONE_VERSION_REVISION_S "5" +#define ZEROTIER_ONE_VERSION_REVISION_S_HEX "5" #endif