Browse Source

updated OneService convenience functions for SDK

Joseph Henry 8 years ago
parent
commit
1f26cb98de
2 changed files with 111 additions and 6 deletions
  1. 67 6
      service/OneService.cpp
  2. 44 0
      service/OneService.hpp

+ 67 - 6
service/OneService.cpp

@@ -104,11 +104,12 @@ namespace ZeroTier { typedef TestEthernetTap EthernetTap; }
 
 
 #else
 #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
 #else
 
 
 #ifdef __APPLE__
 #ifdef __APPLE__
@@ -989,6 +990,62 @@ public:
 		else return std::string();
 		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<uint64_t,NetworkState>::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<uint64_t,NetworkState>::iterator it;
+	    for(it = _nets.begin(); it != _nets.end(); it++) {
+			if(it->second.tap) {
+				for(int j=0; j<it->second.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<uint64_t,NetworkState>::iterator i;
+	    for(i = _nets.begin(); i != _nets.end(); i++) {
+	        delete i->second.tap;
+	    }
+	}
+#endif // ZT_SDK
+
 	virtual void terminate()
 	virtual void terminate()
 	{
 	{
 		_run_m.lock();
 		_run_m.lock();
@@ -1158,9 +1215,11 @@ public:
 #else
 #else
 					settings["portMappingEnabled"] = false; // not supported in build
 					settings["portMappingEnabled"] = false; // not supported in build
 #endif
 #endif
+#ifndef ZT_SDK
+
 					settings["softwareUpdate"] = OSUtils::jsonString(settings["softwareUpdate"],ZT_SOFTWARE_UPDATE_DEFAULT);
 					settings["softwareUpdate"] = OSUtils::jsonString(settings["softwareUpdate"],ZT_SOFTWARE_UPDATE_DEFAULT);
 					settings["softwareUpdateChannel"] = OSUtils::jsonString(settings["softwareUpdateChannel"],ZT_SOFTWARE_UPDATE_DEFAULT_CHANNEL);
 					settings["softwareUpdateChannel"] = OSUtils::jsonString(settings["softwareUpdateChannel"],ZT_SOFTWARE_UPDATE_DEFAULT_CHANNEL);
-
+#endif
 					const World planet(_node->planet());
 					const World planet(_node->planet());
 					res["planetWorldId"] = planet.id();
 					res["planetWorldId"] = planet.id();
 					res["planetWorldTimestamp"] = planet.timestamp();
 					res["planetWorldTimestamp"] = planet.timestamp();
@@ -1508,6 +1567,7 @@ public:
 		_primaryPort = (unsigned int)OSUtils::jsonInt(settings["primaryPort"],(uint64_t)_primaryPort) & 0xffff;
 		_primaryPort = (unsigned int)OSUtils::jsonInt(settings["primaryPort"],(uint64_t)_primaryPort) & 0xffff;
 		_portMappingEnabled = OSUtils::jsonBool(settings["portMappingEnabled"],true);
 		_portMappingEnabled = OSUtils::jsonBool(settings["portMappingEnabled"],true);
 
 
+#ifndef ZT_SDK
 		const std::string up(OSUtils::jsonString(settings["softwareUpdate"],ZT_SOFTWARE_UPDATE_DEFAULT));
 		const std::string up(OSUtils::jsonString(settings["softwareUpdate"],ZT_SOFTWARE_UPDATE_DEFAULT));
 		const bool udist = OSUtils::jsonBool(settings["softwareUpdateDist"],false);
 		const bool udist = OSUtils::jsonBool(settings["softwareUpdateDist"],false);
 		if (((up == "apply")||(up == "download"))||(udist)) {
 		if (((up == "apply")||(up == "download"))||(udist)) {
@@ -1521,6 +1581,7 @@ public:
 			_updater = (SoftwareUpdater *)0;
 			_updater = (SoftwareUpdater *)0;
 			_updateAutoApply = false;
 			_updateAutoApply = false;
 		}
 		}
+#endif
 
 
 		json &ignoreIfs = settings["interfacePrefixBlacklist"];
 		json &ignoreIfs = settings["interfacePrefixBlacklist"];
 		if (ignoreIfs.is_array()) {
 		if (ignoreIfs.is_array()) {

+ 44 - 0
service/OneService.hpp

@@ -32,6 +32,13 @@
 
 
 #include "../node/InetAddress.hpp"
 #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 {
 namespace ZeroTier {
 
 
 /**
 /**
@@ -139,6 +146,43 @@ public:
 	 */
 	 */
 	virtual std::string portDeviceName(uint64_t nwid) const = 0;
 	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)
 	 * Terminate background service (can be called from other threads)
 	 */
 	 */