From dfb08ec753018b8f81cd81839b737ef6f75a9901 Mon Sep 17 00:00:00 2001
From: Adam Ierymenko <adam.ierymenko@gmail.com>
Date: Wed, 2 Sep 2015 15:51:28 -0700
Subject: [PATCH] More netcon stuff, and Phy build fix.

---
 netcon/NetconEthernetTap.cpp | 54 +++++++++++++++++++++++++++++++++++-
 netcon/NetconEthernetTap.hpp | 19 +++++++++++++
 osdep/Phy.hpp                |  6 ++++
 3 files changed, 78 insertions(+), 1 deletion(-)

diff --git a/netcon/NetconEthernetTap.cpp b/netcon/NetconEthernetTap.cpp
index 5b71f89e2..578b836e1 100644
--- a/netcon/NetconEthernetTap.cpp
+++ b/netcon/NetconEthernetTap.cpp
@@ -29,6 +29,7 @@
 
 #include "NetconEthernetTap.hpp"
 
+#include "../node/Utils.hpp"
 #include "../osdep/OSUtils.hpp"
 #include "../osdep/Phy.hpp"
 
@@ -43,6 +44,8 @@ NetconEthernetTap::NetconEthernetTap(
 	const char *friendlyName,
 	void (*handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int),
 	void *arg) :
+	_phy(this,false,true),
+	_unixListenSocket((PhySocket *)0),
 	_handler(handler),
 	_arg(arg),
 	_nwid(nwid),
@@ -51,13 +54,27 @@ NetconEthernetTap::NetconEthernetTap(
 	_dev(),
 	_multicastGroups(),
 	_mtu(mtu),
-	_enabled(true)
+	_enabled(true),
+	_run(true)
 {
+	char sockPath[4096];
+	Utils::snprintf(sockPath,sizeof(sockPath),"/tmp/.ztnc_%.16llx",(unsigned long long)nwid);
+	_dev = sockPath;
+
+	_unixListenSocket = _phy.unixListen(sockPath,(void *)this);
+	if (!_unixSocket)
+		throw std::runtime_error(std::string("unable to bind to ")+sockPath);
+
 	_thread = Thread::start(this);
 }
 
 NetconEthernetTap::~NetconEthernetTap()
 {
+	_run = false;
+	_phy.whack();
+	_phy.whack();
+	Thread::join(_thread);
+	_phy.close(_unixListenSocket,false);
 }
 
 void NetconEthernetTap::setEnabled(bool en)
@@ -84,6 +101,8 @@ std::vector<InetAddress> NetconEthernetTap::ips() const
 
 void NetconEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
 {
+	if (!_enabled)
+		return;
 }
 
 std::string NetconEthernetTap::deviceName() const
@@ -101,6 +120,39 @@ void NetconEthernetTap::scanMulticastGroups(std::vector<MulticastGroup> &added,s
 
 void NetconEthernetTap::threadMain()
 	throw()
+{
+	while (_run) {
+		unsigned long pollTimeout = 500;
+
+		// TODO: compute timeout from LWIP stuff
+
+		_phy.poll(pollTimeout);
+	}
+
+	// TODO: cleanup -- destroy LWIP state, kill any clients, unload .so, etc.
+}
+
+// Unused
+void NetconEthernetTap::phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *from,void *data,unsigned long len) {}
+void NetconEthernetTap::phyOnTcpConnect(PhySocket *sock,void **uptr,bool success) {}
+void NetconEthernetTap::phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from) {}
+void NetconEthernetTap::phyOnTcpClose(PhySocket *sock,void **uptr) {}
+void NetconEthernetTap::phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len) {}
+void NetconEthernetTap::phyOnTcpWritable(PhySocket *sock,void **uptr) {}
+
+void NetconEthernetTap::phyOnUnixAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN)
+{
+}
+
+void NetconEthernetTap::phyOnUnixClose(PhySocket *sock,void **uptr)
+{
+}
+
+void NetconEthernetTap::phyOnUnixData(PhySocket *sock,void **uptr,void *data,unsigned long len)
+{
+}
+
+void NetconEthernetTap::phyOnUnixWritable(PhySocket *sock,void **uptr)
 {
 }
 
diff --git a/netcon/NetconEthernetTap.hpp b/netcon/NetconEthernetTap.hpp
index e36ef8121..5453b2505 100644
--- a/netcon/NetconEthernetTap.hpp
+++ b/netcon/NetconEthernetTap.hpp
@@ -39,7 +39,9 @@
 
 #include "../node/Constants.hpp"
 #include "../node/MulticastGroup.hpp"
+#include "../node/Mutex.hpp"
 #include "../osdep/Thread.hpp"
+#include "../osdep/Phy.hpp"
 
 namespace ZeroTier {
 
@@ -48,6 +50,8 @@ namespace ZeroTier {
  */
 class NetconEthernetTap
 {
+	friend class Phy<NetconEthernetTap>;
+
 public:
 	NetconEthernetTap(
 		const char *homePath,
@@ -75,9 +79,23 @@ public:
 		throw();
 
 private:
+	void phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *from,void *data,unsigned long len);
+  void phyOnTcpConnect(PhySocket *sock,void **uptr,bool success);
+  void phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from);
+  void phyOnTcpClose(PhySocket *sock,void **uptr);
+  void phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len);
+  void phyOnTcpWritable(PhySocket *sock,void **uptr);
+  void phyOnUnixAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN);
+  void phyOnUnixClose(PhySocket *sock,void **uptr);
+  void phyOnUnixData(PhySocket *sock,void **uptr,void *data,unsigned long len);
+  void phyOnUnixWritable(PhySocket *sock,void **uptr);
+
 	void (*_handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int);
 	void *_arg;
 
+	Phy<NetconEthernetTap> _phy;
+	PhySocket *_unixListenSocket;
+
 	uint64_t _nwid;
 	Thread _thread;
 	std::string _homePath;
@@ -85,6 +103,7 @@ private:
 	std::vector<MulticastGroup> _multicastGroups;
 	unsigned int _mtu;
 	volatile bool _enabled;
+	volatile bool _run;
 };
 
 } // namespace ZeroTier
diff --git a/osdep/Phy.hpp b/osdep/Phy.hpp
index aad40cd1f..979e6dfa5 100644
--- a/osdep/Phy.hpp
+++ b/osdep/Phy.hpp
@@ -58,6 +58,7 @@
 #include <sys/types.h>
 #include <sys/select.h>
 #include <sys/socket.h>
+#include <sys/un.h>
 #include <arpa/inet.h>
 #include <netinet/in.h>
 #include <netinet/tcp.h>
@@ -919,6 +920,11 @@ public:
 
 		ZT_PHY_CLOSE_SOCKET(sws.sock);
 
+#ifdef __UNIX_LIKE__
+		if (sws.type == ZT_PHY_SOCKET_UNIX_LISTEN)
+			::unlink(((struct sockaddr_un *)(&(sws.saddr)))->sun_path);
+#endif // __UNIX_LIKE__
+
 		if (callHandlers) {
 			switch(sws.type) {
 				case ZT_PHY_SOCKET_TCP_OUT_PENDING: