From a5aea2f3bb370173bbfa7021780728dcd008bb5c Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Fri, 10 Jan 2020 21:09:13 -0800 Subject: [PATCH] Everything but the Go code builds now! --- CMakeLists.txt | 77 ++++++++++++++--------------------------- node/AES.hpp | 2 +- node/Address.hpp | 10 +++--- node/Locator.cpp | 2 +- node/Membership.hpp | 15 -------- node/Network.cpp | 10 +++--- node/Node.cpp | 10 +++--- node/Node.hpp | 1 - node/Peer.cpp | 7 ++++ node/Peer.hpp | 7 ++++ node/SHA512.hpp | 12 +++---- node/Utils.hpp | 84 ++++++++++++++++++++------------------------- osdep/OSUtils.cpp | 18 ---------- osdep/OSUtils.hpp | 1 - osdep/Thread.hpp | 2 +- root/root.cpp | 12 ++----- 16 files changed, 106 insertions(+), 164 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 010d70515..ad09fdf49 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,5 @@ cmake_minimum_required (VERSION 3.8) +project(zerotier DESCRIPTION "ZeroTier Network Hypervisor" LANGUAGES CXX C) if(${CMAKE_VERSION} VERSION_LESS 3.15) cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}) @@ -7,31 +8,32 @@ else() endif() if(WIN32) - # If building on Windows, set minimum target to Windows 7 set(CMAKE_SYSTEM_VERSION "7" CACHE STRING INTERNAL FORCE) endif(WIN32) +set(CMAKE_OSX_DEPLOYMENT_TARGET "10.9" CACHE STRING "Minimum OS X Deployment Version") set(ZEROTIER_ONE_VERSION_MAJOR 2 CACHE INTERNAL "") set(ZEROTIER_ONE_VERSION_MINOR 0 CACHE INTERNAL "") set(ZEROTIER_ONE_VERSION_REVISION 0 CACHE INTERNAL "") set(ZEROTIER_ONE_VERSION_BUILD 0 CACHE INTERNAL "") +configure_file( + ${CMAKE_SOURCE_DIR}/version.h.in + ${CMAKE_BINARY_DIR}/version.h +) + set(default_build_type "Release") -if(EXISTS "${CMAKE_SOURCE_DIR}/.git") - set(default_build_type "Debug") -endif() +#if(EXISTS "${CMAKE_SOURCE_DIR}/.git") +# set(default_build_type "Debug") +#endif() if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) message(STATUS "Setting build type to '${default_build_type}' as none was specified.") - set(CMAKE_BUILD_TYPE "${default_build_type}" CACHE - STRING "Choose the type of build." FORCE) - # Set the possible values of build type for cmake-gui - set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS - "Debug" "Release" "MinSizeRel" "RelWithDebInfo") + set(CMAKE_BUILD_TYPE "${default_build_type}" CACHE STRING "Choose the type of build." FORCE) + set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo") endif() option(BUILD_CENTRAL_CONTROLLER "Build ZeroTier Central Controller" OFF) - if (BUILD_CENTRAL_CONTROLLER) find_package(PostgreSQL REQUIRED) set(ENABLE_SSL_SUPPORT OFF) @@ -43,22 +45,17 @@ if (BUILD_CENTRAL_CONTROLLER) add_subdirectory("ext/librabbitmq") endif(BUILD_CENTRAL_CONTROLLER) -set(CMAKE_OSX_DEPLOYMENT_TARGET "10.9" CACHE STRING "Minimum OS X Deployment Version") - if(CMAKE_BUILD_TYPE STREQUAL "Debug") add_definitions(-DZT_TRACE) endif(CMAKE_BUILD_TYPE STREQUAL "Debug") -project(zerotier - DESCRIPTION "ZeroTier Network Hypervisor" - LANGUAGES CXX C) - if(WIN32) + message("++ Setting Windows Compiler Flags ${CMAKE_BUILD_TYPE}") add_definitions(-DNOMINMAX) else(WIN32) if(APPLE) - message("Setting macOS Compiler Flags ${CMAKE_BUILD_TYPE}") + message("++ Setting MacOS Compiler Flags ${CMAKE_BUILD_TYPE}") add_compile_options( -Wall -Wno-deprecated @@ -77,14 +74,9 @@ else(WIN32) $<$:-flto> ) - elseif ( - CMAKE_SYSTEM_NAME MATCHES "Linux" OR - CMAKE_SYSTEM_NAME MATCHES "FreeBSD" OR - CMAKE_SYSTEM_NAME MATCHES "OpenBSD" OR - CMAKE_SYSTEM_NAME MATCHES "NetBSD" - ) + else(APPLE) - message("Setting Linux/BSD Compiler Flags (${CMAKE_BUILD_TYPE})") + message("++ Setting Linux/BSD/Posix Compiler Flags (${CMAKE_BUILD_TYPE})") add_compile_options( -Wall -Wno-deprecated @@ -108,10 +100,9 @@ if ( CMAKE_SYSTEM_PROCESSOR MATCHES "i586" OR CMAKE_SYSTEM_PROCESSOR MATCHES "i686" ) - message("Adding SSE and AES-NI flags for processor ${CMAKE_SYSTEM_PROCESSOR}") + message("++ Adding SSE and AES-NI flags for processor ${CMAKE_SYSTEM_PROCESSOR}") add_compile_options( -maes - -mmmx -mrdrnd -mpclmul -msse @@ -124,9 +115,6 @@ endif() if(ZT_TRACE) add_definitions(-DZT_TRACE) endif() -if(ZT_DEBUG_TRACE) - add_definitions(-DZT_DEBUG_TRACE) -endif() add_subdirectory(node) add_subdirectory(controller) @@ -134,12 +122,6 @@ add_subdirectory(osdep) add_subdirectory(root) add_subdirectory(go/native) -#if(WIN32) -# add_subdirectory("windows/WinUI") -# add_subdirectory("windows/copyutil") -# add_definitions(-DNOMINMAX) -#endif(WIN32) - set( zt_osdep zt_core @@ -147,25 +129,18 @@ set( zt_go_native ) -configure_file( - ${CMAKE_SOURCE_DIR}/version.h.in - ${CMAKE_BINARY_DIR}/version.h -) - -#set(src -# one.cpp -# "ext/http-parser/http_parser.c" -#) -#set(headers -# "ext/http-parser/http_parser.h" -#) - if(WIN32) set(libs ${libs} wsock32 ws2_32 rpcrt4 iphlpapi) else(WIN32) set(libs ${libs} pthread) endif(WIN32) +#if(WIN32) +# add_subdirectory("windows/WinUI") +# add_subdirectory("windows/copyutil") +# add_definitions(-DNOMINMAX) +#endif(WIN32) + #if(WIN32) # set(libs ${libs} wsock32 ws2_32 rpcrt4 iphlpapi) # set(src @@ -201,6 +176,6 @@ add_custom_command( ) add_custom_target(build_zerotier ALL DEPENDS zerotier) -add_executable(zerotier-selftest selftest.cpp) -target_link_libraries(zerotier-selftest ${libs} zt_core zt_osdep) -target_compile_features(zerotier-selftest PUBLIC cxx_std_11) +#add_executable(zerotier-selftest selftest.cpp) +#target_link_libraries(zerotier-selftest ${libs} zt_core zt_osdep) +#target_compile_features(zerotier-selftest PUBLIC cxx_std_11) diff --git a/node/AES.hpp b/node/AES.hpp index 009f363c9..a1f5661ef 100644 --- a/node/AES.hpp +++ b/node/AES.hpp @@ -487,7 +487,7 @@ private: _mm_storeu_si128((__m128i *)out,_mm_aesenclast_si128(tmp,_k.ni.k[14])); } - ZT_ALWAYS_INLINE inline __m128i _mult_block_aesni(__m128i shuf,__m128i h,__m128i y) + static ZT_ALWAYS_INLINE inline __m128i _mult_block_aesni(__m128i shuf,__m128i h,__m128i y) { y = _mm_shuffle_epi8(y,shuf); __m128i t1 = _mm_clmulepi64_si128(h,y,0x00); diff --git a/node/Address.hpp b/node/Address.hpp index e442e7110..d91373c04 100644 --- a/node/Address.hpp +++ b/node/Address.hpp @@ -130,10 +130,10 @@ public: */ ZT_ALWAYS_INLINE uint8_t operator[](unsigned int i) const { return (uint8_t)(_a >> (32 - (i * 8))); } - explicit ZT_ALWAYS_INLINE operator bool() const { return (_a != 0); } - explicit ZT_ALWAYS_INLINE operator unsigned int() const { return (unsigned int)_a; } - explicit ZT_ALWAYS_INLINE operator unsigned long() const { return (unsigned long)_a; } - explicit ZT_ALWAYS_INLINE operator unsigned long long() const { return (unsigned long long)_a; } + ZT_ALWAYS_INLINE operator bool() const { return (_a != 0); } + ZT_ALWAYS_INLINE operator unsigned int() const { return (unsigned int)_a; } + ZT_ALWAYS_INLINE operator unsigned long() const { return (unsigned long)_a; } + ZT_ALWAYS_INLINE operator unsigned long long() const { return (unsigned long long)_a; } ZT_ALWAYS_INLINE void zero() { _a = 0; } @@ -151,6 +151,7 @@ public: ZT_ALWAYS_INLINE bool operator>=(const Address &a) const { return (_a >= a._a); } ZT_ALWAYS_INLINE bool operator<=(const Address &a) const { return (_a <= a._a); } +#if 0 /** * Create a list of the first N bits of a list of unique addresses with N as the minimum unique size * @@ -232,6 +233,7 @@ try_additional_bits: { return l; } +#endif private: uint64_t _a; diff --git a/node/Locator.cpp b/node/Locator.cpp index 66b145e2e..dba50b86f 100644 --- a/node/Locator.cpp +++ b/node/Locator.cpp @@ -37,7 +37,7 @@ bool Locator::verify(const Identity &id) const return id.verify(signData,signLen,_signature,_signatureLength); } -int Locator::marshal(uint8_t data[ZT_LOCATOR_MARSHAL_SIZE_MAX],const bool excludeSignature = false) const +int Locator::marshal(uint8_t data[ZT_LOCATOR_MARSHAL_SIZE_MAX],const bool excludeSignature) const { if ((_endpointCount > ZT_LOCATOR_MAX_ENDPOINTS)||(_signatureLength > ZT_SIGNATURE_BUFFER_SIZE)) return -1; diff --git a/node/Membership.hpp b/node/Membership.hpp index fd47f6dad..260373a4b 100644 --- a/node/Membership.hpp +++ b/node/Membership.hpp @@ -70,21 +70,6 @@ public: */ ZT_ALWAYS_INLINE int64_t lastPushedCredentials() const { return _lastPushedCredentials; } - /** - * Check whether we should push MULTICAST_LIKEs to this peer, and update last sent time if true - * - * @param now Current time - * @return True if we should update multicasts - */ - ZT_ALWAYS_INLINE bool multicastLikeGate(const int64_t now) - { - if ((now - _lastUpdatedMulticast) >= ZT_MULTICAST_ANNOUNCE_PERIOD) { - _lastUpdatedMulticast = now; - return true; - } - return false; - } - /** * Check whether the peer represented by this Membership should be allowed on this network at all * diff --git a/node/Network.cpp b/node/Network.cpp index a2762ab03..bc256454b 100644 --- a/node/Network.cpp +++ b/node/Network.cpp @@ -1018,10 +1018,6 @@ bool Network::gate(void *tPtr,const SharedPtr &peer) if ( (_config.isPublic()) || ((m)&&(m->isAllowedOnNetwork(_config))) ) { if (!m) m = &(_memberships[peer->address()]); - if (m->multicastLikeGate(now)) { - Mutex::Lock l2(_myMulticastGroups_l); - _announceMulticastGroupsTo(tPtr,peer->address(),_allMulticastGroups()); - } return true; } } @@ -1051,6 +1047,8 @@ void Network::doPeriodicTasks(void *tPtr,const int64_t now) { Mutex::Lock l2(_myMulticastGroups_l); + // TODO + /* Hashtable< MulticastGroup,uint64_t >::Iterator i(_multicastGroupsBehindMe); MulticastGroup *mg = (MulticastGroup *)0; uint64_t *ts = (uint64_t *)0; @@ -1060,6 +1058,7 @@ void Network::doPeriodicTasks(void *tPtr,const int64_t now) } _announceMulticastGroups(tPtr,false); + */ } } } @@ -1374,11 +1373,14 @@ void Network::_announceMulticastGroups(void *tPtr,bool force) Membership *m = (Membership *)0; Hashtable::Iterator i(_memberships); while (i.next(a,m)) { + // TODO + /* bool announce = m->multicastLikeGate(now); // force this to be called even if 'force' is true since it updates last push time if ((!announce)&&(force)) announce = true; if ((announce)&&(m->isAllowedOnNetwork(_config))) _announceMulticastGroupsTo(tPtr,*a,groups); + */ } } } diff --git a/node/Node.cpp b/node/Node.cpp index c5c979f7f..1ad2aeaec 100644 --- a/node/Node.cpp +++ b/node/Node.cpp @@ -47,7 +47,6 @@ Node::Node(void *uPtr, void *tPtr, const struct ZT_Node_Callbacks *callbacks, in _lastPing(0), _lastHousekeepingRun(0), _lastNetworkHousekeepingRun(0), - _lastDynamicRootUpdate(0), _online(false) { memset(_expectingRepliesToBucketPtr,0,sizeof(_expectingRepliesToBucketPtr)); @@ -408,13 +407,13 @@ ZT_PeerList *Node::peers() const p->versionMinor = -1; p->versionRev = -1; } - p->latency = (int)(*pi)->latency(now); + p->latency = (int)(*pi)->latency(); if (p->latency >= 0xffff) p->latency = -1; p->role = RR->topology->isRoot((*pi)->identity()) ? ZT_PEER_ROLE_ROOT : ZT_PEER_ROLE_LEAF; - std::vector< SharedPtr > paths((*pi)->paths(now)); - SharedPtr bestp((*pi)->getAppropriatePath(now,false)); + std::vector< SharedPtr > paths; + (*pi)->getAllPaths(paths); p->pathCount = 0; for(std::vector< SharedPtr >::iterator path(paths.begin());path!=paths.end();++path) { memcpy(&(p->paths[p->pathCount].address),&((*path)->address()),sizeof(struct sockaddr_storage)); @@ -422,8 +421,7 @@ ZT_PeerList *Node::peers() const p->paths[p->pathCount].lastReceive = (*path)->lastIn(); p->paths[p->pathCount].trustedPathId = RR->topology->getOutboundPathTrust((*path)->address()); p->paths[p->pathCount].alive = (*path)->alive(now) ? 1 : 0; - p->paths[p->pathCount].preferred = ((*path) == bestp) ? 1 : 0; - + p->paths[p->pathCount].preferred = (p->pathCount == 0) ? 1 : 0; ++p->pathCount; } diff --git a/node/Node.hpp b/node/Node.hpp index 7ac224c7c..e09279fb4 100644 --- a/node/Node.hpp +++ b/node/Node.hpp @@ -270,7 +270,6 @@ private: int64_t _lastPing; int64_t _lastHousekeepingRun; int64_t _lastNetworkHousekeepingRun; - int64_t _lastDynamicRootUpdate; bool _online; }; diff --git a/node/Peer.cpp b/node/Peer.cpp index 116d3e0f4..8ebb86011 100644 --- a/node/Peer.cpp +++ b/node/Peer.cpp @@ -260,4 +260,11 @@ void Peer::resetWithinScope(void *tPtr,InetAddress::IpScope scope,int inetAddres */ } +void Peer::getAllPaths(std::vector< SharedPtr > &paths) +{ + RWMutex::RLock l(_paths_l); + paths.clear(); + paths.assign(_paths,_paths + _pathCount); +} + } // namespace ZeroTier diff --git a/node/Peer.hpp b/node/Peer.hpp index c3d527f87..b59a0d514 100644 --- a/node/Peer.hpp +++ b/node/Peer.hpp @@ -278,6 +278,13 @@ public: return _paths[0]; } + /** + * Get all paths + * + * @param paths Vector of paths with the first path being the current preferred path + */ + void getAllPaths(std::vector< SharedPtr > &paths); + private: uint8_t _key[ZT_PEER_SECRET_KEY_LENGTH]; diff --git a/node/SHA512.hpp b/node/SHA512.hpp index 4ecc00fc5..5120dec79 100644 --- a/node/SHA512.hpp +++ b/node/SHA512.hpp @@ -42,21 +42,21 @@ namespace ZeroTier { #ifdef __APPLE__ #define ZT_HAVE_NATIVE_SHA512 1 -static ZT_ALWAYS_INLINE void SHA512(void *digest,const void *data,unsigned int len) +ZT_ALWAYS_INLINE void SHA512(void *digest,const void *data,unsigned int len) { CC_SHA512_CTX ctx; CC_SHA512_Init(&ctx); CC_SHA512_Update(&ctx,data,len); CC_SHA512_Final(reinterpret_cast(digest),&ctx); } -static ZT_ALWAYS_INLINE void SHA384(void *digest,const void *data,unsigned int len) +ZT_ALWAYS_INLINE void SHA384(void *digest,const void *data,unsigned int len) { CC_SHA512_CTX ctx; CC_SHA384_Init(&ctx); CC_SHA384_Update(&ctx,data,len); CC_SHA384_Final(reinterpret_cast(digest),&ctx); } -static ZT_ALWAYS_INLINE void SHA384(void *digest,const void *data0,unsigned int len0,const void *data1,unsigned int len1) +ZT_ALWAYS_INLINE void SHA384(void *digest,const void *data0,unsigned int len0,const void *data1,unsigned int len1) { CC_SHA512_CTX ctx; CC_SHA384_Init(&ctx); @@ -69,21 +69,21 @@ static ZT_ALWAYS_INLINE void SHA384(void *digest,const void *data0,unsigned int #ifndef ZT_HAVE_NATIVE_SHA512 #ifdef ZT_USE_LIBCRYPTO #define ZT_HAVE_NATIVE_SHA512 1 -static ZT_ALWAYS_INLINE void SHA512(void *digest,const void *data,unsigned int len) +ZT_ALWAYS_INLINE void SHA512(void *digest,const void *data,unsigned int len) { SHA512_CTX ctx; SHA512_Init(&ctx); SHA512_Update(&ctx,data,len); SHA512_Final(reinterpret_cast(digest),&ctx); } -static ZT_ALWAYS_INLINE void SHA384(void *digest,const void *data,unsigned int len) +ZT_ALWAYS_INLINE void SHA384(void *digest,const void *data,unsigned int len) { SHA512_CTX ctx; SHA384_Init(&ctx); SHA384_Update(&ctx,data,len); SHA384_Final(reinterpret_cast(digest),&ctx); } -static ZT_ALWAYS_INLINE void SHA384(void *digest,const void *data0,unsigned int len0,const void *data1,unsigned int len1) +ZT_ALWAYS_INLINE void SHA384(void *digest,const void *data0,unsigned int len0,const void *data1,unsigned int len1) { SHA512_CTX ctx; SHA384_Init(&ctx); diff --git a/node/Utils.hpp b/node/Utils.hpp index 606eac638..93c3a3237 100644 --- a/node/Utils.hpp +++ b/node/Utils.hpp @@ -174,14 +174,6 @@ uint64_t random(); */ bool scopy(char *dest,unsigned int len,const char *src); -static ZT_ALWAYS_INLINE float normalize(float value, int64_t bigMin, int64_t bigMax, int32_t targetMin, int32_t targetMax) -{ - int64_t bigSpan = bigMax - bigMin; - int64_t smallSpan = targetMax - targetMin; - float valueScaled = (value - (float)bigMin) / (float)bigSpan; - return (float)targetMin + valueScaled * (float)smallSpan; -} - /** * Tokenize a string (alias for strtok_r or strtok_s depending on platform) * @@ -189,7 +181,7 @@ static ZT_ALWAYS_INLINE float normalize(float value, int64_t bigMin, int64_t big * @param delim Delimiters * @param saveptr Pointer to a char * for temporary reentrant storage */ -static ZT_ALWAYS_INLINE char *stok(char *str,const char *delim,char **saveptr) +ZT_ALWAYS_INLINE char *stok(char *str,const char *delim,char **saveptr) { #ifdef __WINDOWS__ return strtok_s(str,delim,saveptr); @@ -198,11 +190,11 @@ static ZT_ALWAYS_INLINE char *stok(char *str,const char *delim,char **saveptr) #endif } -static ZT_ALWAYS_INLINE unsigned int strToUInt(const char *s) { return (unsigned int)strtoul(s,(char **)0,10); } -static ZT_ALWAYS_INLINE int strToInt(const char *s) { return (int)strtol(s,(char **)0,10); } -static ZT_ALWAYS_INLINE unsigned long strToULong(const char *s) { return strtoul(s,(char **)0,10); } -static ZT_ALWAYS_INLINE long strToLong(const char *s) { return strtol(s,(char **)0,10); } -static ZT_ALWAYS_INLINE unsigned long long strToU64(const char *s) +ZT_ALWAYS_INLINE unsigned int strToUInt(const char *s) { return (unsigned int)strtoul(s,(char **)0,10); } +ZT_ALWAYS_INLINE int strToInt(const char *s) { return (int)strtol(s,(char **)0,10); } +ZT_ALWAYS_INLINE unsigned long strToULong(const char *s) { return strtoul(s,(char **)0,10); } +ZT_ALWAYS_INLINE long strToLong(const char *s) { return strtol(s,(char **)0,10); } +ZT_ALWAYS_INLINE unsigned long long strToU64(const char *s) { #ifdef __WINDOWS__ return (unsigned long long)_strtoui64(s,(char **)0,10); @@ -210,7 +202,7 @@ static ZT_ALWAYS_INLINE unsigned long long strToU64(const char *s) return strtoull(s,(char **)0,10); #endif } -static ZT_ALWAYS_INLINE long long strTo64(const char *s) +ZT_ALWAYS_INLINE long long strTo64(const char *s) { #ifdef __WINDOWS__ return (long long)_strtoi64(s,(char **)0,10); @@ -218,11 +210,11 @@ static ZT_ALWAYS_INLINE long long strTo64(const char *s) return strtoll(s,(char **)0,10); #endif } -static ZT_ALWAYS_INLINE unsigned int hexStrToUInt(const char *s) { return (unsigned int)strtoul(s,(char **)0,16); } -static ZT_ALWAYS_INLINE int hexStrToInt(const char *s) { return (int)strtol(s,(char **)0,16); } -static ZT_ALWAYS_INLINE unsigned long hexStrToULong(const char *s) { return strtoul(s,(char **)0,16); } -static ZT_ALWAYS_INLINE long hexStrToLong(const char *s) { return strtol(s,(char **)0,16); } -static ZT_ALWAYS_INLINE unsigned long long hexStrToU64(const char *s) +ZT_ALWAYS_INLINE unsigned int hexStrToUInt(const char *s) { return (unsigned int)strtoul(s,(char **)0,16); } +ZT_ALWAYS_INLINE int hexStrToInt(const char *s) { return (int)strtol(s,(char **)0,16); } +ZT_ALWAYS_INLINE unsigned long hexStrToULong(const char *s) { return strtoul(s,(char **)0,16); } +ZT_ALWAYS_INLINE long hexStrToLong(const char *s) { return strtol(s,(char **)0,16); } +ZT_ALWAYS_INLINE unsigned long long hexStrToU64(const char *s) { #ifdef __WINDOWS__ return (unsigned long long)_strtoui64(s,(char **)0,16); @@ -230,7 +222,7 @@ static ZT_ALWAYS_INLINE unsigned long long hexStrToU64(const char *s) return strtoull(s,(char **)0,16); #endif } -static ZT_ALWAYS_INLINE long long hexStrTo64(const char *s) +ZT_ALWAYS_INLINE long long hexStrTo64(const char *s) { #ifdef __WINDOWS__ return (long long)_strtoi64(s,(char **)0,16); @@ -246,7 +238,7 @@ static ZT_ALWAYS_INLINE long long hexStrTo64(const char *s) * @param len Length in bytes * @return Non-cryptographic hash suitable for use in a hash table */ -static unsigned long ZT_ALWAYS_INLINE hashString(const void *restrict key,const unsigned int len) +ZT_ALWAYS_INLINE unsigned long hashString(const void *restrict key,const unsigned int len) { const uint8_t *p = reinterpret_cast(key); unsigned long h = 0; @@ -262,13 +254,13 @@ static unsigned long ZT_ALWAYS_INLINE hashString(const void *restrict key,const } #ifdef __GNUC__ -static ZT_ALWAYS_INLINE unsigned int countBits(const uint8_t v) { return (unsigned int)__builtin_popcount((unsigned int)v); } -static ZT_ALWAYS_INLINE unsigned int countBits(const uint16_t v) { return (unsigned int)__builtin_popcount((unsigned int)v); } -static ZT_ALWAYS_INLINE unsigned int countBits(const uint32_t v) { return (unsigned int)__builtin_popcountl((unsigned long)v); } -static ZT_ALWAYS_INLINE unsigned int countBits(const uint64_t v) { return (unsigned int)__builtin_popcountll((unsigned long long)v); } +ZT_ALWAYS_INLINE unsigned int countBits(const uint8_t v) { return (unsigned int)__builtin_popcount((unsigned int)v); } +ZT_ALWAYS_INLINE unsigned int countBits(const uint16_t v) { return (unsigned int)__builtin_popcount((unsigned int)v); } +ZT_ALWAYS_INLINE unsigned int countBits(const uint32_t v) { return (unsigned int)__builtin_popcountl((unsigned long)v); } +ZT_ALWAYS_INLINE unsigned int countBits(const uint64_t v) { return (unsigned int)__builtin_popcountll((unsigned long long)v); } #else template -static ZT_ALWAYS_INLINE unsigned int countBits(T v) +ZT_ALWAYS_INLINE unsigned int countBits(T v) { v = v - ((v >> 1) & (T)~(T)0/3); v = (v & (T)~(T)0/15*3) + ((v >> 2) & (T)~(T)0/15*3); @@ -278,9 +270,9 @@ static ZT_ALWAYS_INLINE unsigned int countBits(T v) #endif #if __BYTE_ORDER == __LITTLE_ENDIAN -static ZT_ALWAYS_INLINE uint8_t hton(uint8_t n) { return n; } -static ZT_ALWAYS_INLINE int8_t hton(int8_t n) { return n; } -static ZT_ALWAYS_INLINE uint16_t hton(uint16_t n) +ZT_ALWAYS_INLINE uint8_t hton(uint8_t n) { return n; } +ZT_ALWAYS_INLINE int8_t hton(int8_t n) { return n; } +ZT_ALWAYS_INLINE uint16_t hton(uint16_t n) { #if defined(__GNUC__) #if defined(__FreeBSD__) @@ -292,8 +284,8 @@ static ZT_ALWAYS_INLINE uint16_t hton(uint16_t n) return htons(n); #endif } -static ZT_ALWAYS_INLINE int16_t hton(int16_t n) { return (int16_t)Utils::hton((uint16_t)n); } -static ZT_ALWAYS_INLINE uint32_t hton(uint32_t n) +ZT_ALWAYS_INLINE int16_t hton(int16_t n) { return (int16_t)Utils::hton((uint16_t)n); } +ZT_ALWAYS_INLINE uint32_t hton(uint32_t n) { #if defined(__GNUC__) #if defined(__FreeBSD__) @@ -305,8 +297,8 @@ static ZT_ALWAYS_INLINE uint32_t hton(uint32_t n) return htonl(n); #endif } -static ZT_ALWAYS_INLINE int32_t hton(int32_t n) { return (int32_t)Utils::hton((uint32_t)n); } -static ZT_ALWAYS_INLINE uint64_t hton(uint64_t n) +ZT_ALWAYS_INLINE int32_t hton(int32_t n) { return (int32_t)Utils::hton((uint32_t)n); } +ZT_ALWAYS_INLINE uint64_t hton(uint64_t n) { #if defined(__GNUC__) #if defined(__FreeBSD__) @@ -327,16 +319,16 @@ static ZT_ALWAYS_INLINE uint64_t hton(uint64_t n) ); #endif } -static ZT_ALWAYS_INLINE int64_t hton(int64_t n) { return (int64_t)hton((uint64_t)n); } +ZT_ALWAYS_INLINE int64_t hton(int64_t n) { return (int64_t)hton((uint64_t)n); } #else template static ZT_ALWAYS_INLINE T hton(T n) { return n; } #endif #if __BYTE_ORDER == __LITTLE_ENDIAN -static ZT_ALWAYS_INLINE uint8_t ntoh(uint8_t n) { return n; } -static ZT_ALWAYS_INLINE int8_t ntoh(int8_t n) { return n; } -static ZT_ALWAYS_INLINE uint16_t ntoh(uint16_t n) +ZT_ALWAYS_INLINE uint8_t ntoh(uint8_t n) { return n; } +ZT_ALWAYS_INLINE int8_t ntoh(int8_t n) { return n; } +ZT_ALWAYS_INLINE uint16_t ntoh(uint16_t n) { #if defined(__GNUC__) #if defined(__FreeBSD__) @@ -348,8 +340,8 @@ static ZT_ALWAYS_INLINE uint16_t ntoh(uint16_t n) return htons(n); #endif } -static ZT_ALWAYS_INLINE int16_t ntoh(int16_t n) { return (int16_t)Utils::ntoh((uint16_t)n); } -static ZT_ALWAYS_INLINE uint32_t ntoh(uint32_t n) +ZT_ALWAYS_INLINE int16_t ntoh(int16_t n) { return (int16_t)Utils::ntoh((uint16_t)n); } +ZT_ALWAYS_INLINE uint32_t ntoh(uint32_t n) { #if defined(__GNUC__) #if defined(__FreeBSD__) @@ -361,8 +353,8 @@ static ZT_ALWAYS_INLINE uint32_t ntoh(uint32_t n) return ntohl(n); #endif } -static ZT_ALWAYS_INLINE int32_t ntoh(int32_t n) { return (int32_t)Utils::ntoh((uint32_t)n); } -static ZT_ALWAYS_INLINE uint64_t ntoh(uint64_t n) +ZT_ALWAYS_INLINE int32_t ntoh(int32_t n) { return (int32_t)Utils::ntoh((uint32_t)n); } +ZT_ALWAYS_INLINE uint64_t ntoh(uint64_t n) { #if defined(__GNUC__) #if defined(__FreeBSD__) @@ -383,13 +375,13 @@ static ZT_ALWAYS_INLINE uint64_t ntoh(uint64_t n) ); #endif } -static ZT_ALWAYS_INLINE int64_t ntoh(int64_t n) { return (int64_t)ntoh((uint64_t)n); } +ZT_ALWAYS_INLINE int64_t ntoh(int64_t n) { return (int64_t)ntoh((uint64_t)n); } #else template -static ZT_ALWAYS_INLINE T ntoh(T n) { return n; } +ZT_ALWAYS_INLINE T ntoh(T n) { return n; } #endif -static ZT_ALWAYS_INLINE uint64_t readUInt64(const void *const p) +ZT_ALWAYS_INLINE uint64_t readUInt64(const void *const p) { #ifdef ZT_NO_TYPE_PUNNING const uint8_t *const b = reinterpret_cast(p); @@ -407,7 +399,7 @@ static ZT_ALWAYS_INLINE uint64_t readUInt64(const void *const p) #endif } -static ZT_ALWAYS_INLINE void putUInt64(void *const p,const uint64_t i) +ZT_ALWAYS_INLINE void putUInt64(void *const p,const uint64_t i) { #ifdef ZT_NO_TYPE_PUNNING uint8_t *const b = reinterpret_cast(p); diff --git a/osdep/OSUtils.cpp b/osdep/OSUtils.cpp index 142f7cc12..61305e8ff 100644 --- a/osdep/OSUtils.cpp +++ b/osdep/OSUtils.cpp @@ -510,24 +510,6 @@ std::string OSUtils::jsonString(const nlohmann::json &jv,const char *dfl) return std::string((dfl) ? dfl : ""); } -std::string OSUtils::jsonBinFromHex(const nlohmann::json &jv) -{ - std::string s(jsonString(jv,"")); - if (s.length() > 0) { - unsigned int buflen = (unsigned int)((s.length() / 2) + 1); - char *buf = new char[buflen]; - try { - unsigned int l = Utils::unhex(s.c_str(),buf,buflen); - std::string b(buf,l); - delete [] buf; - return b; - } catch ( ... ) { - delete [] buf; - } - } - return std::string(); -} - #endif // OMIT_JSON_SUPPORT // Used to convert HTTP header names to ASCII lower case diff --git a/osdep/OSUtils.hpp b/osdep/OSUtils.hpp index 30b34115c..f76c70c97 100644 --- a/osdep/OSUtils.hpp +++ b/osdep/OSUtils.hpp @@ -292,7 +292,6 @@ public: static uint64_t jsonIntHex(const nlohmann::json &jv,const uint64_t dfl); static bool jsonBool(const nlohmann::json &jv,const bool dfl); static std::string jsonString(const nlohmann::json &jv,const char *dfl); - static std::string jsonBinFromHex(const nlohmann::json &jv); #endif // OMIT_JSON_SUPPORT private: diff --git a/osdep/Thread.hpp b/osdep/Thread.hpp index 4cd569941..ccfc6d0f3 100644 --- a/osdep/Thread.hpp +++ b/osdep/Thread.hpp @@ -149,7 +149,7 @@ public: pthread_attr_init(&tattr); // This corrects for systems with abnormally small defaults (musl) and also // shrinks the stack on systems with large defaults to save a bit of memory. - pthread_attr_setstacksize(&tattr,ZT_THREAD_MIN_STACK_SIZE); + pthread_attr_setstacksize(&tattr,1048576); if (pthread_create(&t._tid,&tattr,&___zt_threadMain,instance)) { pthread_attr_destroy(&tattr); throw std::runtime_error("pthread_create() failed, unable to create thread"); diff --git a/root/root.cpp b/root/root.cpp index 8db77c5de..33916faa4 100644 --- a/root/root.cpp +++ b/root/root.cpp @@ -230,7 +230,7 @@ static ZT_ALWAYS_INLINE std::array< uint64_t,2 > ip6ToH128(const InetAddress &ip static void handlePacket(const int v4s,const int v6s,const InetAddress *const ip,Packet &pkt) { - char ipstr[128],ipstr2[128],astr[32],astr2[32],tmpstr[256]; + char ipstr[128]; const bool fragment = pkt[ZT_PACKET_FRAGMENT_IDX_FRAGMENT_INDICATOR] == ZT_PACKET_FRAGMENT_INDICATOR; const Address source(pkt.source()); const Address dest(pkt.destination()); @@ -249,10 +249,8 @@ static void handlePacket(const int v4s,const int v6s,const InetAddress *const ip { std::lock_guard pbi_l(s_peersByIdentity_l); auto pById = s_peersByIdentity.find(id); - if (pById != s_peersByIdentity.end()) { + if (pById != s_peersByIdentity.end()) peer = pById->second; - //printf("%s has %s (known (1))" ZT_EOL_S,ip->toString(ipstr),source().toString(astr)); - } } if (peer) { if (!pkt.dearmor(peer->key)) { @@ -503,13 +501,11 @@ static void handlePacket(const int v4s,const int v6s,const InetAddress *const ip bool introduce = false; if (fragment) { if ((hops = (int)reinterpret_cast(&pkt)->incrementHops()) > s_relayMaxHops) { - //printf("%s refused to forward to %s: max hop count exceeded" ZT_EOL_S,ip->toString(ipstr),dest.toString(astr)); s_discardedForwardRate.log(now,pkt.size()); return; } } else { if ((hops = (int)pkt.incrementHops()) > s_relayMaxHops) { - //printf("%s refused to forward to %s: max hop count exceeded" ZT_EOL_S,ip->toString(ipstr),dest.toString(astr)); s_discardedForwardRate.log(now,pkt.size()); return; } @@ -552,7 +548,6 @@ static void handlePacket(const int v4s,const int v6s,const InetAddress *const ip for(auto a=sources->second.begin();a!=sources->second.end();++a) { for(auto b=toAddrs.begin();b!=toAddrs.end();++b) { if (((*a)->ip6)&&(b->second->ip6)) { - //printf("* introducing %s(%s) to %s(%s)" ZT_EOL_S,ip->toString(ipstr),source.toString(astr),b->second->ip6.toString(ipstr2),dest.toString(astr2)); // Introduce source to destination (V6) Packet outp(source,s_self.address(),Packet::VERB_RENDEZVOUS); @@ -581,7 +576,6 @@ static void handlePacket(const int v4s,const int v6s,const InetAddress *const ip b->second->lastSend = now; } if (((*a)->ip4)&&(b->second->ip4)) { - //printf("* introducing %s(%s) to %s(%s)" ZT_EOL_S,ip->toString(ipstr),source.toString(astr),b->second->ip4.toString(ipstr2),dest.toString(astr2)); // Introduce source to destination (V4) Packet outp(source,s_self.address(),Packet::VERB_RENDEZVOUS); @@ -1134,7 +1128,7 @@ int main(int argc,char **argv) for(auto a=s_multicastSubscriptions.begin();a!=s_multicastSubscriptions.end();) { for(auto b=a->second.begin();b!=a->second.end();) { for(auto c=b->second.begin();c!=b->second.end();) { - if ((now - c->second) > ZT_MULTICAST_LIKE_EXPIRE) + if ((now - c->second) > ZT_PEER_ACTIVITY_TIMEOUT) b->second.erase(c++); else ++c; }