Browse Source

Include tap device name in JSON output for network info.

Adam Ierymenko 10 years ago
parent
commit
e205e5fdfe
4 changed files with 38 additions and 10 deletions
  1. 11 7
      service/ControlPlane.cpp
  2. 3 1
      service/ControlPlane.hpp
  3. 19 2
      service/One.cpp
  4. 5 0
      service/One.hpp

+ 11 - 7
service/ControlPlane.cpp

@@ -26,6 +26,7 @@
  */
 
 #include "ControlPlane.hpp"
+#include "One.hpp"
 
 #include "../version.h"
 #include "../include/ZeroTierOne.h"
@@ -115,7 +116,7 @@ static std::string _jsonEnumerate(const ZT1_PeerPhysicalPath *pp,unsigned int co
 	buf.push_back(']');
 	return buf;
 }
-static void _jsonAppend(std::string &buf,const ZT1_VirtualNetworkConfig *nc)
+static void _jsonAppend(std::string &buf,const ZT1_VirtualNetworkConfig *nc,const std::string &portDeviceName)
 {
 	char json[65536];
 	const char *nstatus = "",*ntype = "";
@@ -145,7 +146,8 @@ static void _jsonAppend(std::string &buf,const ZT1_VirtualNetworkConfig *nc)
 		"\"portError\": %d,"
 		"\"netconfRevision\": %lu,"
 		"\"multicastSubscriptions\": %s,"
-		"\"assignedAddresses\": %s"
+		"\"assignedAddresses\": %s,"
+		"\"portDeviceName\": \"%s\""
 		"}",
 		nc->nwid,
 		(unsigned int)((nc->mac >> 40) & 0xff),(unsigned int)((nc->mac >> 32) & 0xff),(unsigned int)((nc->mac >> 24) & 0xff),(unsigned int)((nc->mac >> 16) & 0xff),(unsigned int)((nc->mac >> 8) & 0xff),(unsigned int)(nc->mac & 0xff),
@@ -159,7 +161,8 @@ static void _jsonAppend(std::string &buf,const ZT1_VirtualNetworkConfig *nc)
 		nc->portError,
 		nc->netconfRevision,
 		_jsonEnumerate(nc->multicastSubscriptions,nc->multicastSubscriptionCount).c_str(),
-		_jsonEnumerate(nc->assignedAddresses,nc->assignedAddressCount).c_str());
+		_jsonEnumerate(nc->assignedAddresses,nc->assignedAddressCount).c_str(),
+		_jsonEscape(portDeviceName).c_str());
 	buf.append(json);
 }
 static void _jsonAppend(std::string &buf,const ZT1_Peer *peer)
@@ -193,7 +196,8 @@ static void _jsonAppend(std::string &buf,const ZT1_Peer *peer)
 	buf.append(json);
 }
 
-ControlPlane::ControlPlane(Node *n) :
+ControlPlane::ControlPlane(One *svc,Node *n) :
+	_svc(svc),
 	_node(n)
 {
 }
@@ -317,7 +321,7 @@ unsigned int ControlPlane::handleRequest(
 							for(unsigned long i=0;i<nws->networkCount;++i) {
 								if (i > 0)
 									responseBody.push_back(',');
-								_jsonAppend(responseBody,&(nws->networks[i]));
+								_jsonAppend(responseBody,&(nws->networks[i]),_svc->portDeviceName(nws->networks[i].nwid));
 							}
 							responseBody.push_back(']');
 							scode = 200;
@@ -327,7 +331,7 @@ unsigned int ControlPlane::handleRequest(
 							for(unsigned long i=0;i<nws->networkCount;++i) {
 								if (nws->networks[i].nwid == wantnw) {
 									responseContentType = "application/json";
-									_jsonAppend(responseBody,&(nws->networks[i]));
+									_jsonAppend(responseBody,&(nws->networks[i]),_svc->portDeviceName(nws->networks[i].nwid));
 									scode = 200;
 									break;
 								}
@@ -384,7 +388,7 @@ unsigned int ControlPlane::handleRequest(
 							for(unsigned long i=0;i<nws->networkCount;++i) {
 								if (nws->networks[i].nwid == wantnw) {
 									responseContentType = "application/json";
-									_jsonAppend(responseBody,&(nws->networks[i]));
+									_jsonAppend(responseBody,&(nws->networks[i]),_svc->portDeviceName(nws->networks[i].nwid));
 									scode = 200;
 									break;
 								}

+ 3 - 1
service/ControlPlane.hpp

@@ -36,6 +36,7 @@
 
 namespace ZeroTier {
 
+class One;
 class Node;
 struct InetAddress;
 
@@ -45,7 +46,7 @@ struct InetAddress;
 class ControlPlane
 {
 public:
-	ControlPlane(Node *n);
+	ControlPlane(One *svc,Node *n);
 	~ControlPlane();
 
 	/**
@@ -70,6 +71,7 @@ public:
 		std::string &responseContentType);
 
 private:
+	One *const _svc;
 	Node *const _node;
 	std::set<std::string> _authTokens;
 };

+ 19 - 2
service/One.cpp

@@ -52,6 +52,11 @@
 #include "One.hpp"
 #include "ControlPlane.hpp"
 
+#ifdef __APPLE__
+#include "../osdep/OSXEthernetTap.hpp"
+namespace ZeroTier { typedef OSXEthernetTap EthernetTap; }
+#endif
+
 // Sanity limits for HTTP
 #define ZT_MAX_HTTP_MESSAGE_SIZE (1024 * 1024 * 8)
 #define ZT_MAX_HTTP_CONNECTIONS 64
@@ -189,9 +194,9 @@ public:
 			if (_master)
 				_node->setNetconfMaster((void *)_master);
 
-			_controlPlane = new ControlPlane(_node);
+			_controlPlane = new ControlPlane(this,_node);
 
-			{
+			{	// Remember networks from previous session
 				std::vector<std::string> networksDotD(OSUtils::listDirectory((_homePath + ZT_PATH_SEPARATOR_S + "networks.d").c_str()));
 				for(std::vector<std::string>::iterator f(networksDotD.begin());f!=networksDotD.end();++f) {
 					std::size_t dot = f->find_last_of('.');
@@ -259,6 +264,15 @@ public:
 		return _fatalErrorMessage;
 	}
 
+	virtual std::string portDeviceName(uint64_t nwid) const
+	{
+		Mutex::Lock _l(_taps_m);
+		std::map< uint64_t,EthernetTap * >::const_iterator t(_taps.find(nwid));
+		if (t != _taps.end())
+			return t->second->deviceName();
+		return std::string();
+	}
+
 	virtual void terminate()
 	{
 		_run_m.lock();
@@ -537,6 +551,9 @@ private:
 	ControlPlane *_controlPlane;
 	uint64_t _nextBackgroundTaskDeadline;
 
+	std::map< uint64_t,EthernetTap * > _taps;
+	Mutex _taps_m;
+
 	std::map< PhySocket *,HttpConnection > _httpConnections; // no mutex for this since it's done in the main loop thread only
 
 	ReasonForTermination _termReason;

+ 5 - 0
service/One.hpp

@@ -111,6 +111,11 @@ public:
 	 */
 	virtual std::string fatalErrorMessage() const = 0;
 
+	/**
+	 * @return System device name corresponding with a given ZeroTier network ID
+	 */
+	virtual std::string portDeviceName(uint64_t nwid) const = 0;
+
 	/**
 	 * Terminate background service (can be called from other threads)
 	 */