Include tap device name in JSON output for network info.

This commit is contained in:
Adam Ierymenko 2015-04-14 15:55:03 -07:00
parent 347e98dcd2
commit e205e5fdfe
4 changed files with 38 additions and 10 deletions

View file

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

View file

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

View file

@ -52,6 +52,11 @@
#include "One.hpp" #include "One.hpp"
#include "ControlPlane.hpp" #include "ControlPlane.hpp"
#ifdef __APPLE__
#include "../osdep/OSXEthernetTap.hpp"
namespace ZeroTier { typedef OSXEthernetTap EthernetTap; }
#endif
// Sanity limits for HTTP // Sanity limits for HTTP
#define ZT_MAX_HTTP_MESSAGE_SIZE (1024 * 1024 * 8) #define ZT_MAX_HTTP_MESSAGE_SIZE (1024 * 1024 * 8)
#define ZT_MAX_HTTP_CONNECTIONS 64 #define ZT_MAX_HTTP_CONNECTIONS 64
@ -189,9 +194,9 @@ public:
if (_master) if (_master)
_node->setNetconfMaster((void *)_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())); 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) { for(std::vector<std::string>::iterator f(networksDotD.begin());f!=networksDotD.end();++f) {
std::size_t dot = f->find_last_of('.'); std::size_t dot = f->find_last_of('.');
@ -259,6 +264,15 @@ public:
return _fatalErrorMessage; 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() virtual void terminate()
{ {
_run_m.lock(); _run_m.lock();
@ -537,6 +551,9 @@ private:
ControlPlane *_controlPlane; ControlPlane *_controlPlane;
uint64_t _nextBackgroundTaskDeadline; 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 std::map< PhySocket *,HttpConnection > _httpConnections; // no mutex for this since it's done in the main loop thread only
ReasonForTermination _termReason; ReasonForTermination _termReason;

View file

@ -111,6 +111,11 @@ public:
*/ */
virtual std::string fatalErrorMessage() const = 0; 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) * Terminate background service (can be called from other threads)
*/ */