diff --git a/include/ZeroTier.h b/include/ZeroTier.h index ee42aed..bf92169 100644 --- a/include/ZeroTier.h +++ b/include/ZeroTier.h @@ -466,6 +466,36 @@ struct zts_peer_list extern "C" { #endif +/** + * @brief Enable or disable whether the service will cache network details (enabled by default) + * + * This can potentially shorten (startup) times. This allows the service to nearly instantly + * inform the network stack of an address to use for this peer so that it can + * create an interface. This can be disabled for cases where one may not want network + * config details to be written to storage. This is especially useful for situations where + * address assignments do not change often. + * + * @usage Should be called before zts_start() if you intend on changing its state. + * + * @param enabled Whether or not this feature is enabled + */ +ZT_SOCKET_API void ZTCALL zts_set_network_caching(bool enabled); + +/** + * @brief Enable or disable whether the service will cache peer details (enabled by default) + * + * This can potentially shorten (connection) times. This allows the service to + * re-use previously discovered paths to a peer, this prevents the service from having + * to go through the entire transport-triggered link provisioning process. This is especially + * useful for situations where paths to peers do not change often. This is enabled by default + * and can be disabled for cases where one may not want peer details to be written to storage. + * + * @usage Should be called before zts_start() if you intend on changing its state. + * + * @param enabled Whether or not this feature is enabled + */ +ZT_SOCKET_API void ZTCALL zts_set_peer_caching(bool enabled); + /** * @brief Starts the ZeroTier service and notifies user application of events via callback * diff --git a/src/Controls.cpp b/src/Controls.cpp index c7c5ae9..cc466b3 100644 --- a/src/Controls.cpp +++ b/src/Controls.cpp @@ -79,6 +79,9 @@ bool _run_service = false; bool _run_callbacks = false; bool _run_lwip_tcpip = false; +bool _network_caching_enabled = true; +bool _peer_caching_enabled = true; + //bool _startupError = false; // Global reference to ZeroTier service @@ -501,6 +504,16 @@ int zts_deorbit(uint64_t moonWorldId) #ifdef SDK_JNI #endif +void zts_set_network_caching(bool enabled) +{ + _network_caching_enabled = enabled; +} + +void zts_set_peer_caching(bool enabled) +{ + _peer_caching_enabled = enabled; +} + int zts_start( const char *path, void (*callback)(struct zts_callback_msg*), int port) { @@ -720,7 +733,8 @@ int zts_get_6plane_addr(struct sockaddr_storage *addr, const uint64_t nwid, cons return ZTS_ERR_INVALID_ARG; } InetAddress _6planeAddr = InetAddress::makeIpv66plane(nwid,nodeId); - memcpy(addr, _6planeAddr.rawIpData(), sizeof(struct sockaddr_storage)); + struct sockaddr_in6 *in6 = (struct sockaddr_in6*)addr; + memcpy(in6->sin6_addr.s6_addr, _6planeAddr.rawIpData(), sizeof(struct in6_addr)); } int zts_get_rfc4193_addr(struct sockaddr_storage *addr, const uint64_t nwid, const uint64_t nodeId) @@ -729,7 +743,8 @@ int zts_get_rfc4193_addr(struct sockaddr_storage *addr, const uint64_t nwid, con return ZTS_ERR_INVALID_ARG; } InetAddress _rfc4193Addr = InetAddress::makeIpv6rfc4193(nwid,nodeId); - memcpy(addr, _rfc4193Addr.rawIpData(), sizeof(struct sockaddr_storage)); + struct sockaddr_in6 *in6 = (struct sockaddr_in6*)addr; + memcpy(in6->sin6_addr.s6_addr, _rfc4193Addr.rawIpData(), sizeof(struct in6_addr)); } ////////////////////////////////////////////////////////////////////////////// diff --git a/src/Options.h b/src/Options.h index f4ca86e..74708dd 100644 --- a/src/Options.h +++ b/src/Options.h @@ -77,24 +77,4 @@ */ #define ZTS_LWIP_MAX_RX_QUEUE_LEN 1024 -////////////////////////////////////////////////////////////////////////////// -// Service behaviour // -////////////////////////////////////////////////////////////////////////////// - -/** - * Whether the service will cache peer details (such as known paths). This will - * make startup and reachability time shorter but is generally only effective - * for networks with a somewhat static topology. In other words this would not be - * recommended for use on mobile devices. - */ -#define PEER_CACHING 0 - -/** - * Whether the service will cache network details. This will shorten startup - * times. This allows the service to nearly instantly inform the network stack - * of an address to use for this peer so that it can create an interface. This - * is only recommended for networks whose IP assignments do not change often. - */ -#define NETWORK_CACHING 1 - #endif \ No newline at end of file diff --git a/src/Service.cpp b/src/Service.cpp index 24f14da..b3bc011 100644 --- a/src/Service.cpp +++ b/src/Service.cpp @@ -90,6 +90,8 @@ namespace ZeroTier { typedef VirtualTap EthernetTap; } namespace ZeroTier { extern void postEvent(uint64_t id, int eventCode); +extern bool _network_caching_enabled; +extern bool _peer_caching_enabled; namespace { @@ -422,10 +424,8 @@ public: } } #endif - -#if NETWORK_CACHING // Join existing networks in networks.d - { + if (_network_caching_enabled) { std::vector networksDotD(OSUtils::listDirectory((_homePath + ZT_PATH_SEPARATOR_S "networks.d").c_str())); for(std::vector::iterator f(networksDotD.begin());f!=networksDotD.end();++f) { std::size_t dot = f->find_last_of('.'); @@ -433,8 +433,6 @@ public: _node->join(Utils::hexStrToU64(f->substr(0,dot).c_str()),(void *)0,(void *)0); } } -#endif - // Main I/O loop _nextBackgroundTaskDeadline = 0; int64_t clockShouldBe = OSUtils::now(); @@ -773,13 +771,13 @@ public: *nuptr = (void *)0; delete n.tap; _nets.erase(nwid); -#if NETWORK_CACHING - if (op == ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY) { - char nlcpath[256]; - OSUtils::ztsnprintf(nlcpath,sizeof(nlcpath),"%s" ZT_PATH_SEPARATOR_S "networks.d" ZT_PATH_SEPARATOR_S "%.16llx.local.conf",_homePath.c_str(),nwid); - OSUtils::rm(nlcpath); + if (_network_caching_enabled) { + if (op == ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY) { + char nlcpath[256]; + OSUtils::ztsnprintf(nlcpath,sizeof(nlcpath),"%s" ZT_PATH_SEPARATOR_S "networks.d" ZT_PATH_SEPARATOR_S "%.16llx.local.conf",_homePath.c_str(),nwid); + OSUtils::rm(nlcpath); + } } -#endif } else { _nets.erase(nwid); } @@ -970,19 +968,19 @@ public: case ZT_STATE_OBJECT_PLANET: OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "planet",_homePath.c_str()); break; -#if NETWORK_CACHING case ZT_STATE_OBJECT_NETWORK_CONFIG: - OSUtils::ztsnprintf(dirname,sizeof(dirname),"%s" ZT_PATH_SEPARATOR_S "networks.d",_homePath.c_str()); - OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "%.16llx.conf",dirname,(unsigned long long)id[0]); - secure = true; + if (_network_caching_enabled) { + OSUtils::ztsnprintf(dirname,sizeof(dirname),"%s" ZT_PATH_SEPARATOR_S "networks.d",_homePath.c_str()); + OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "%.16llx.conf",dirname,(unsigned long long)id[0]); + secure = true; + } break; -#endif -#if PEER_CACHING case ZT_STATE_OBJECT_PEER: - OSUtils::ztsnprintf(dirname,sizeof(dirname),"%s" ZT_PATH_SEPARATOR_S "peers.d",_homePath.c_str()); - OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "%.10llx.peer",dirname,(unsigned long long)id[0]); + if (_peer_caching_enabled) { + OSUtils::ztsnprintf(dirname,sizeof(dirname),"%s" ZT_PATH_SEPARATOR_S "peers.d",_homePath.c_str()); + OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "%.10llx.peer",dirname,(unsigned long long)id[0]); + } break; -#endif default: return; } @@ -1032,16 +1030,16 @@ public: case ZT_STATE_OBJECT_PLANET: OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "planet",_homePath.c_str()); break; -#if NETWORK_CACHING case ZT_STATE_OBJECT_NETWORK_CONFIG: - OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "networks.d" ZT_PATH_SEPARATOR_S "%.16llx.conf",_homePath.c_str(),(unsigned long long)id[0]); + if (_network_caching_enabled) { + OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "networks.d" ZT_PATH_SEPARATOR_S "%.16llx.conf",_homePath.c_str(),(unsigned long long)id[0]); + } break; -#endif -#if PEER_CACHING case ZT_STATE_OBJECT_PEER: - OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "peers.d" ZT_PATH_SEPARATOR_S "%.10llx.peer",_homePath.c_str(),(unsigned long long)id[0]); + if (_peer_caching_enabled) { + OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "peers.d" ZT_PATH_SEPARATOR_S "%.10llx.peer",_homePath.c_str(),(unsigned long long)id[0]); + } break; -#endif default: return -1; }