From da5b11bf2d95230d2e1170f68c8bb4fd89a8fde3 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Tue, 3 Mar 2020 07:38:51 -0800 Subject: [PATCH] Rename version constants to something more canonical, and more cleanup. --- CMakeLists.txt | 8 ++--- controller/EmbeddedNetworkController.cpp | 22 ++++++------- node/Buf.hpp | 5 +++ node/Constants.hpp | 8 ++--- node/Node.cpp | 40 +++++++++++------------- node/Peer.cpp | 6 ++-- node/VL1.cpp | 6 ++-- version.h.in | 8 ++--- 8 files changed, 53 insertions(+), 50 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 82cf2bd22..7fbb5e6ff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,10 +15,10 @@ if(WIN32) endif(WIN32) set(CMAKE_OSX_DEPLOYMENT_TARGET "10.9" CACHE STRING "Minimum OS X Deployment Version") -set(ZEROTIER_ONE_VERSION_MAJOR 1 CACHE INTERNAL "") -set(ZEROTIER_ONE_VERSION_MINOR 9 CACHE INTERNAL "") -set(ZEROTIER_ONE_VERSION_REVISION 0 CACHE INTERNAL "") -set(ZEROTIER_ONE_VERSION_BUILD 0 CACHE INTERNAL "") +set(ZEROTIER_VERSION_MAJOR 1 CACHE INTERNAL "") +set(ZEROTIER_VERSION_MINOR 9 CACHE INTERNAL "") +set(ZEROTIER_VERSION_REVISION 0 CACHE INTERNAL "") +set(ZEROTIER_VERSION_BUILD 0 CACHE INTERNAL "") configure_file( ${CMAKE_SOURCE_DIR}/version.h.in diff --git a/controller/EmbeddedNetworkController.cpp b/controller/EmbeddedNetworkController.cpp index cf4a375cd..0cdea25f5 100644 --- a/controller/EmbeddedNetworkController.cpp +++ b/controller/EmbeddedNetworkController.cpp @@ -707,7 +707,7 @@ unsigned int EmbeddedNetworkController::handleControlPlaneHttpPOST( for(unsigned long i=0;i= ZT_CONTROLLER_MAX_ARRAY_SIZE) @@ -870,11 +870,11 @@ unsigned int EmbeddedNetworkController::handleControlPlaneHttpPOST( InetAddress t(target.get().c_str()); InetAddress v; if (via.is_string()) v.fromString(via.get().c_str()); - if ( ((t.ss_family == AF_INET)||(t.ss_family == AF_INET6)) && (t.netmaskBitsValid()) ) { + if ( ((t.family() == AF_INET)||(t.family() == AF_INET6)) && (t.netmaskBitsValid()) ) { json tmp; char tmp2[64]; tmp["target"] = t.toString(tmp2); - if (v.ss_family == t.ss_family) + if (v.family() == t.family()) tmp["via"] = v.toIpString(tmp2); else tmp["via"] = json(); nrts.push_back(tmp); @@ -897,7 +897,7 @@ unsigned int EmbeddedNetworkController::handleControlPlaneHttpPOST( if ((ip.is_object())&&(ip.count("ipRangeStart"))&&(ip.count("ipRangeEnd"))) { InetAddress f(OSUtils::jsonString(ip["ipRangeStart"],"").c_str()); InetAddress t(OSUtils::jsonString(ip["ipRangeEnd"],"").c_str()); - if ( ((f.ss_family == AF_INET)||(f.ss_family == AF_INET6)) && (f.ss_family == t.ss_family) ) { + if ( ((f.family() == AF_INET)||(f.family() == AF_INET6)) && (f.family() == t.family()) ) { json tmp = json::object(); char tmp2[64]; tmp["ipRangeStart"] = f.toIpString(tmp2); @@ -1295,7 +1295,7 @@ void EmbeddedNetworkController::_request( nc->credentialTimeMaxDelta = credentialtmd; nc->revision = OSUtils::jsonInt(network["revision"],0ULL); nc->issuedTo = identity.address(); - memcpy(nc->issuedToIdentityHash,identity.fingerprint(),sizeof(nc->issuedToIdentityHash)); + memcpy(nc->issuedToFingerprintHash,identity.fingerprint().hash(),sizeof(nc->issuedToFingerprintHash)); if (OSUtils::jsonBool(network["enableBroadcast"],true)) nc->flags |= ZT_NETWORKCONFIG_FLAG_ENABLE_BROADCAST; Utils::scopy(nc->name,sizeof(nc->name),OSUtils::jsonString(network["name"],"").c_str()); nc->mtu = std::max(std::min((unsigned int)OSUtils::jsonInt(network["mtu"],ZT_DEFAULT_MTU),(unsigned int)ZT_MAX_MTU),(unsigned int)ZT_MIN_MTU); @@ -1423,10 +1423,10 @@ void EmbeddedNetworkController::_request( const InetAddress t(target.get().c_str()); InetAddress v; if (via.is_string()) v.fromString(via.get().c_str()); - if ((t.ss_family == AF_INET)||(t.ss_family == AF_INET6)) { + if ((t.family() == AF_INET)||(t.family() == AF_INET6)) { ZT_VirtualNetworkRoute *r = &(nc->routes[nc->routeCount]); *(reinterpret_cast(&(r->target))) = t; - if (v.ss_family == t.ss_family) + if (v.family() == t.family()) *(reinterpret_cast(&(r->via))) = v; ++nc->routeCount; } @@ -1471,9 +1471,9 @@ void EmbeddedNetworkController::_request( ip.setPort(routedNetmaskBits); nc->staticIps[nc->staticIpCount++] = ip; } - if (ip.ss_family == AF_INET) + if (ip.family() == AF_INET) haveManagedIpv4AutoAssignment = true; - else if (ip.ss_family == AF_INET6) + else if (ip.family() == AF_INET6) haveManagedIpv6AutoAssignment = true; } } @@ -1488,7 +1488,7 @@ void EmbeddedNetworkController::_request( if (pool.is_object()) { InetAddress ipRangeStart(OSUtils::jsonString(pool["ipRangeStart"],"").c_str()); InetAddress ipRangeEnd(OSUtils::jsonString(pool["ipRangeEnd"],"").c_str()); - if ( (ipRangeStart.ss_family == AF_INET6) && (ipRangeEnd.ss_family == AF_INET6) ) { + if ( (ipRangeStart.family() == AF_INET6) && (ipRangeEnd.family() == AF_INET6) ) { uint64_t s[2],e[2],x[2],xx[2]; memcpy(s,ipRangeStart.rawIpData(),16); memcpy(e,ipRangeEnd.rawIpData(),16); @@ -1552,7 +1552,7 @@ void EmbeddedNetworkController::_request( if (pool.is_object()) { InetAddress ipRangeStartIA(OSUtils::jsonString(pool["ipRangeStart"],"").c_str()); InetAddress ipRangeEndIA(OSUtils::jsonString(pool["ipRangeEnd"],"").c_str()); - if ( (ipRangeStartIA.ss_family == AF_INET) && (ipRangeEndIA.ss_family == AF_INET) ) { + if ( (ipRangeStartIA.family() == AF_INET) && (ipRangeEndIA.family() == AF_INET) ) { uint32_t ipRangeStart = Utils::ntoh((uint32_t)(reinterpret_cast(&ipRangeStartIA)->sin_addr.s_addr)); uint32_t ipRangeEnd = Utils::ntoh((uint32_t)(reinterpret_cast(&ipRangeEndIA)->sin_addr.s_addr)); diff --git a/node/Buf.hpp b/node/Buf.hpp index 2bdc973f5..3722a4582 100644 --- a/node/Buf.hpp +++ b/node/Buf.hpp @@ -179,6 +179,11 @@ public: */ ZT_ALWAYS_INLINE Buf() noexcept : __nextInPool(0),__refCount(0) {} + /** + * Create a new buffer and copy data into it + */ + ZT_ALWAYS_INLINE Buf(const void *const data,const unsigned int len) noexcept : __nextInPool(0),__refCount(0) { memcpy(unsafeData,data,len); } + ZT_ALWAYS_INLINE Buf(const Buf &b2) noexcept : __nextInPool(0),__refCount(0) { memcpy(unsafeData,b2.unsafeData,ZT_BUF_MEM_SIZE); } ZT_ALWAYS_INLINE Buf &operator=(const Buf &b2) noexcept diff --git a/node/Constants.hpp b/node/Constants.hpp index 416a7374e..cac5b7cd5 100644 --- a/node/Constants.hpp +++ b/node/Constants.hpp @@ -20,10 +20,10 @@ #if __has_include("version.h") #include "version.h" #else /* dummy values for use inside IDEs, etc. */ -#define ZEROTIER_ONE_VERSION_MAJOR 255 -#define ZEROTIER_ONE_VERSION_MINOR 255 -#define ZEROTIER_ONE_VERSION_REVISION 255 -#define ZEROTIER_ONE_VERSION_BUILD 255 +#define ZEROTIER_VERSION_MAJOR 255 +#define ZEROTIER_VERSION_MINOR 255 +#define ZEROTIER_VERSION_REVISION 255 +#define ZEROTIER_VERSION_BUILD 255 #endif /** diff --git a/node/Node.cpp b/node/Node.cpp index 98583599b..08497fc22 100644 --- a/node/Node.cpp +++ b/node/Node.cpp @@ -32,7 +32,6 @@ #include "VL2.hpp" #include "Buf.hpp" - namespace ZeroTier { namespace { @@ -809,14 +808,25 @@ void Node::ncSendError(uint64_t nwid,uint64_t requestPacketId,const Address &des } // namespace ZeroTier -// C API exports +// C API -------------------------------------------------------------------------------------------------------------- extern "C" { +// These macros make the idiom of passing buffers to outside code via the API work properly even +// if the first address of Buf does not overlap with its data field, since the C++ standard does +// not absolutely guarantee this. +#define _ZT_PTRTOBUF(p) ((ZeroTier::Buf *)( ((uintptr_t)(p)) - ((uintptr_t)&(((ZeroTier::Buf *)0)->unsafeData[0])) )) +#define _ZT_BUFTOPTR(b) ((void *)(&((b)->unsafeData[0]))) + void *ZT_getBuffer() { + // When external code requests a Buf, grab one from the pool (or freshly allocated) + // and return it with its reference count left at zero. It's the responsibility of + // external code to bring it back via freeBuffer() or one of the processX() calls. + // When this occurs it's either sent back to the pool with Buf's delete operator or + // wrapped in a SharedPtr<> to be passed into the core. try { - return (void *)(new ZeroTier::Buf()); // __refCount is left at zero + return _ZT_BUFTOPTR(new ZeroTier::Buf()); } catch ( ... ) { return nullptr; // can only happen on out of memory condition } @@ -825,7 +835,7 @@ void *ZT_getBuffer() ZT_SDK_API void ZT_freeBuffer(void *b) { if (b) - delete ((ZeroTier::Buf *)b); + delete _ZT_PTRTOBUF(b); } enum ZT_ResultCode ZT_Node_new(ZT_Node **node,void *uptr,void *tptr,const struct ZT_Node_Callbacks *callbacks,int64_t now) @@ -863,13 +873,7 @@ enum ZT_ResultCode ZT_Node_processWirePacket( volatile int64_t *nextBackgroundTaskDeadline) { try { - ZeroTier::SharedPtr buf; - if (isZtBuffer) { - buf.set((ZeroTier::Buf *)packetData); - } else { - buf.set(new ZeroTier::Buf()); - memcpy(buf->unsafeData,packetData,std::min((unsigned int)ZT_BUF_MEM_SIZE,packetLength)); - } + ZeroTier::SharedPtr buf((isZtBuffer) ? _ZT_PTRTOBUF(packetData) : new ZeroTier::Buf(packetData,packetLength & ZT_BUF_MEM_MASK)); return reinterpret_cast(node)->processWirePacket(tptr,now,localSocket,remoteAddress,buf,packetLength,nextBackgroundTaskDeadline); } catch (std::bad_alloc &exc) { return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY; @@ -893,13 +897,7 @@ enum ZT_ResultCode ZT_Node_processVirtualNetworkFrame( volatile int64_t *nextBackgroundTaskDeadline) { try { - ZeroTier::SharedPtr buf; - if (isZtBuffer) { - buf.set((ZeroTier::Buf *)frameData); - } else { - buf.set(new ZeroTier::Buf()); - memcpy(buf->unsafeData,frameData,std::min((unsigned int)ZT_BUF_MEM_SIZE,frameLength)); - } + ZeroTier::SharedPtr buf((isZtBuffer) ? _ZT_PTRTOBUF(frameData) : new ZeroTier::Buf(frameData,frameLength & ZT_BUF_MEM_MASK)); return reinterpret_cast(node)->processVirtualNetworkFrame(tptr,now,nwid,sourceMac,destMac,etherType,vlanId,buf,frameLength,nextBackgroundTaskDeadline); } catch (std::bad_alloc &exc) { return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY; @@ -1078,11 +1076,11 @@ enum ZT_ResultCode ZT_Node_setPhysicalPathConfiguration(ZT_Node *node,const stru void ZT_version(int *major,int *minor,int *revision) { if (major) - *major = ZEROTIER_ONE_VERSION_MAJOR; + *major = ZEROTIER_VERSION_MAJOR; if (minor) - *minor = ZEROTIER_ONE_VERSION_MINOR; + *minor = ZEROTIER_VERSION_MINOR; if (revision) - *revision = ZEROTIER_ONE_VERSION_REVISION; + *revision = ZEROTIER_VERSION_REVISION; } } // extern "C" diff --git a/node/Peer.cpp b/node/Peer.cpp index 8bb000acb..2d3d1c0a7 100644 --- a/node/Peer.cpp +++ b/node/Peer.cpp @@ -213,9 +213,9 @@ unsigned int Peer::sendHELLO(void *tPtr,const int64_t localSocket,const InetAddr Packet outp(_id.address(),RR->identity.address(),Packet::VERB_HELLO); outp.append((unsigned char)ZT_PROTO_VERSION); - outp.append((unsigned char)ZEROTIER_ONE_VERSION_MAJOR); - outp.append((unsigned char)ZEROTIER_ONE_VERSION_MINOR); - outp.append((uint16_t)ZEROTIER_ONE_VERSION_REVISION); + outp.append((unsigned char)ZEROTIER_VERSION_MAJOR); + outp.append((unsigned char)ZEROTIER_VERSION_MINOR); + outp.append((uint16_t)ZEROTIER_VERSION_REVISION); outp.append(now); RR->identity.serialize(outp,false); atAddress.serialize(outp); diff --git a/node/VL1.cpp b/node/VL1.cpp index f86f4938b..89dfdb308 100644 --- a/node/VL1.cpp +++ b/node/VL1.cpp @@ -645,9 +645,9 @@ bool VL1::_HELLO(void *tPtr,const SharedPtr &path,SharedPtr &peer,Bu ok.timestampEcho = p.timestamp; ok.versionProtocol = ZT_PROTO_VERSION; - ok.versionMajor = ZEROTIER_ONE_VERSION_MAJOR; - ok.versionMinor = ZEROTIER_ONE_VERSION_MINOR; - ok.versionRev = ZT_CONST_TO_BE_UINT16(ZEROTIER_ONE_VERSION_REVISION); + ok.versionMajor = ZEROTIER_VERSION_MAJOR; + ok.versionMinor = ZEROTIER_VERSION_MINOR; + ok.versionRev = ZT_CONST_TO_BE_UINT16(ZEROTIER_VERSION_REVISION); int outl = sizeof(Protocol::OK::HELLO); outp.wO(outl,path->address()); diff --git a/version.h.in b/version.h.in index a8830551a..690f7421b 100644 --- a/version.h.in +++ b/version.h.in @@ -17,17 +17,17 @@ /** * Major version */ -#define ZEROTIER_ONE_VERSION_MAJOR @ZEROTIER_ONE_VERSION_MAJOR@ +#define ZEROTIER_VERSION_MAJOR @ZEROTIER_VERSION_MAJOR@ /** * Minor version */ -#define ZEROTIER_ONE_VERSION_MINOR @ZEROTIER_ONE_VERSION_MINOR@ +#define ZEROTIER_VERSION_MINOR @ZEROTIER_VERSION_MINOR@ /** * Revision */ -#define ZEROTIER_ONE_VERSION_REVISION @ZEROTIER_ONE_VERSION_REVISION@ +#define ZEROTIER_VERSION_REVISION @ZEROTIER_VERSION_REVISION@ /** * Build version @@ -36,6 +36,6 @@ * to force a minor update without an actual version number change. It's * not part of the actual release version number. */ -#define ZEROTIER_ONE_VERSION_BUILD @ZEROTIER_ONE_VERSION_BUILD@ +#define ZEROTIER_VERSION_BUILD @ZEROTIER_VERSION_BUILD@ #endif