diff --git a/node/InetAddress.cpp b/node/InetAddress.cpp index 62bb8145f..0fbb2d684 100644 --- a/node/InetAddress.cpp +++ b/node/InetAddress.cpp @@ -287,6 +287,30 @@ InetAddress InetAddress::network() const return r; } +#ifdef ZT_SDK + bool InetAddress::isEqualPrefix(const InetAddress &addr) const + { + if (addr.ss_family == ss_family) { + switch(ss_family) { + case AF_INET6: { + const InetAddress mask(netmask()); + InetAddress addr_mask(addr.netmask()); + const uint8_t *n = reinterpret_cast(reinterpret_cast(&addr_mask)->sin6_addr.s6_addr); + const uint8_t *m = reinterpret_cast(reinterpret_cast(&mask)->sin6_addr.s6_addr); + const uint8_t *a = reinterpret_cast(reinterpret_cast(&addr)->sin6_addr.s6_addr); + const uint8_t *b = reinterpret_cast(reinterpret_cast(this)->sin6_addr.s6_addr); + for(unsigned int i=0;i<16;++i) { + if ((a[i] & m[i]) != (b[i] & n[i])) + return false; + } + return true; + } + } + } + return false; + } +#endif + bool InetAddress::containsAddress(const InetAddress &addr) const { if (addr.ss_family == ss_family) { diff --git a/node/InetAddress.hpp b/node/InetAddress.hpp index 0975a9cf6..4cb9a4dc9 100644 --- a/node/InetAddress.hpp +++ b/node/InetAddress.hpp @@ -355,6 +355,16 @@ struct InetAddress : public sockaddr_storage */ InetAddress network() const; +#ifdef ZT_SDK + /** + * Test whether this IPv6 prefix matches the prefix of a given IPv6 address + * + * @param addr Address to check + * @return True if this IPv6 prefix matches the prefix of a given IPv6 address + */ + bool isEqualPrefix(const InetAddress &addr) const; +#endif + /** * Test whether this IP/netmask contains this address * diff --git a/service/OneService.cpp b/service/OneService.cpp index 9f9cec0af..9bfb310b8 100644 --- a/service/OneService.cpp +++ b/service/OneService.cpp @@ -104,11 +104,12 @@ namespace ZeroTier { typedef TestEthernetTap EthernetTap; } #else -#ifdef ZT_SERVICE_NETCON - -#include "../netcon/NetconEthernetTap.hpp" -namespace ZeroTier { typedef NetconEthernetTap EthernetTap; } - +#ifdef ZT_SDK + #include "../controller/EmbeddedNetworkController.hpp" + #include "../node/Node.hpp" + // Use the virtual netcon endpoint instead of a tun/tap port driver + #include "../src/SocketTap.hpp" + namespace ZeroTier { typedef SocketTap EthernetTap; } #else #ifdef __APPLE__ @@ -989,6 +990,62 @@ public: else return std::string(); } +#ifdef ZT_SDK + virtual void leave(const char *hp) + { + _node->leave(Utils::hexStrToU64(hp),NULL,NULL); + } + + virtual void join(const char *hp) + { + _node->join(Utils::hexStrToU64(hp),NULL,NULL); + } + + virtual std::string givenHomePath() + { + return _homePath; + } + + virtual EthernetTap * getTap(uint64_t nwid) + { + Mutex::Lock _l(_nets_m); + std::map::const_iterator n(_nets.find(nwid)); + if (n == _nets.end()) + return NULL; + return n->second.tap; + } + + virtual EthernetTap *getTap(InetAddress &addr) + { + Mutex::Lock _l(_nets_m); + std::map::iterator it; + for(it = _nets.begin(); it != _nets.end(); it++) { + if(it->second.tap) { + for(int j=0; jsecond.tap->_ips.size(); j++) { + if(it->second.tap->_ips[j].isEqualPrefix(addr) || it->second.tap->_ips[j].ipsEqual(addr)) { + return it->second.tap; + } + } + } + } + return NULL; + } + + virtual Node * getNode() + { + return _node; + } + + virtual void removeNets() + { + Mutex::Lock _l(_nets_m); + std::map::iterator i; + for(i = _nets.begin(); i != _nets.end(); i++) { + delete i->second.tap; + } + } +#endif // ZT_SDK + virtual void terminate() { _run_m.lock(); @@ -1158,9 +1215,11 @@ public: #else settings["portMappingEnabled"] = false; // not supported in build #endif +#ifndef ZT_SDK + settings["softwareUpdate"] = OSUtils::jsonString(settings["softwareUpdate"],ZT_SOFTWARE_UPDATE_DEFAULT); settings["softwareUpdateChannel"] = OSUtils::jsonString(settings["softwareUpdateChannel"],ZT_SOFTWARE_UPDATE_DEFAULT_CHANNEL); - +#endif const World planet(_node->planet()); res["planetWorldId"] = planet.id(); res["planetWorldTimestamp"] = planet.timestamp(); @@ -1508,6 +1567,7 @@ public: _primaryPort = (unsigned int)OSUtils::jsonInt(settings["primaryPort"],(uint64_t)_primaryPort) & 0xffff; _portMappingEnabled = OSUtils::jsonBool(settings["portMappingEnabled"],true); +#ifndef ZT_SDK const std::string up(OSUtils::jsonString(settings["softwareUpdate"],ZT_SOFTWARE_UPDATE_DEFAULT)); const bool udist = OSUtils::jsonBool(settings["softwareUpdateDist"],false); if (((up == "apply")||(up == "download"))||(udist)) { @@ -1521,6 +1581,7 @@ public: _updater = (SoftwareUpdater *)0; _updateAutoApply = false; } +#endif json &ignoreIfs = settings["interfacePrefixBlacklist"]; if (ignoreIfs.is_array()) { diff --git a/service/OneService.hpp b/service/OneService.hpp index f52cd40e9..b770a3c08 100644 --- a/service/OneService.hpp +++ b/service/OneService.hpp @@ -32,6 +32,13 @@ #include "../node/InetAddress.hpp" +#ifdef ZT_SDK + #include "../node/Node.hpp" + // Use the virtual netcon endpoint instead of a tun/tap port driver + #include "../src/SocketTap.hpp" + namespace ZeroTier { typedef SocketTap EthernetTap; } +#endif + namespace ZeroTier { /** @@ -139,6 +146,43 @@ public: */ virtual std::string portDeviceName(uint64_t nwid) const = 0; +#ifdef ZT_SDK + /** + * Leaves a network + */ + virtual void leave(const char *hp) = 0; + + /** + * Joins a network + */ + virtual void join(const char *hp) = 0; + + /** + * Returns the homePath given by the client application + */ + virtual std::string givenHomePath() = 0; + + /* + * Returns a SocketTap that is associated with the given nwid + */ + virtual EthernetTap * getTap(uint64_t nwid) = 0; + + /* + * Returns a SocketTap that cant function as a route to the specified host + */ + virtual EthernetTap * getTap(InetAddress &addr) = 0; + + /* + * Returns a pointer to the Node + */ + virtual Node * getNode() = 0; + + /* + * Delete all SocketTap interfaces + */ + virtual void removeNets() = 0; +#endif + /** * Terminate background service (can be called from other threads) */