More netcon stuff, and Phy build fix.

This commit is contained in:
Adam Ierymenko 2015-09-02 15:51:28 -07:00
parent 4626175d11
commit dfb08ec753
3 changed files with 78 additions and 1 deletions

View file

@ -29,6 +29,7 @@
#include "NetconEthernetTap.hpp" #include "NetconEthernetTap.hpp"
#include "../node/Utils.hpp"
#include "../osdep/OSUtils.hpp" #include "../osdep/OSUtils.hpp"
#include "../osdep/Phy.hpp" #include "../osdep/Phy.hpp"
@ -43,6 +44,8 @@ NetconEthernetTap::NetconEthernetTap(
const char *friendlyName, const char *friendlyName,
void (*handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int), void (*handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int),
void *arg) : void *arg) :
_phy(this,false,true),
_unixListenSocket((PhySocket *)0),
_handler(handler), _handler(handler),
_arg(arg), _arg(arg),
_nwid(nwid), _nwid(nwid),
@ -51,13 +54,27 @@ NetconEthernetTap::NetconEthernetTap(
_dev(), _dev(),
_multicastGroups(), _multicastGroups(),
_mtu(mtu), _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); _thread = Thread::start(this);
} }
NetconEthernetTap::~NetconEthernetTap() NetconEthernetTap::~NetconEthernetTap()
{ {
_run = false;
_phy.whack();
_phy.whack();
Thread::join(_thread);
_phy.close(_unixListenSocket,false);
} }
void NetconEthernetTap::setEnabled(bool en) 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) 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 std::string NetconEthernetTap::deviceName() const
@ -101,6 +120,39 @@ void NetconEthernetTap::scanMulticastGroups(std::vector<MulticastGroup> &added,s
void NetconEthernetTap::threadMain() void NetconEthernetTap::threadMain()
throw() 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)
{ {
} }

View file

@ -39,7 +39,9 @@
#include "../node/Constants.hpp" #include "../node/Constants.hpp"
#include "../node/MulticastGroup.hpp" #include "../node/MulticastGroup.hpp"
#include "../node/Mutex.hpp"
#include "../osdep/Thread.hpp" #include "../osdep/Thread.hpp"
#include "../osdep/Phy.hpp"
namespace ZeroTier { namespace ZeroTier {
@ -48,6 +50,8 @@ namespace ZeroTier {
*/ */
class NetconEthernetTap class NetconEthernetTap
{ {
friend class Phy<NetconEthernetTap>;
public: public:
NetconEthernetTap( NetconEthernetTap(
const char *homePath, const char *homePath,
@ -75,9 +79,23 @@ public:
throw(); throw();
private: 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 (*_handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int);
void *_arg; void *_arg;
Phy<NetconEthernetTap> _phy;
PhySocket *_unixListenSocket;
uint64_t _nwid; uint64_t _nwid;
Thread _thread; Thread _thread;
std::string _homePath; std::string _homePath;
@ -85,6 +103,7 @@ private:
std::vector<MulticastGroup> _multicastGroups; std::vector<MulticastGroup> _multicastGroups;
unsigned int _mtu; unsigned int _mtu;
volatile bool _enabled; volatile bool _enabled;
volatile bool _run;
}; };
} // namespace ZeroTier } // namespace ZeroTier

View file

@ -58,6 +58,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/select.h> #include <sys/select.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/un.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <netinet/tcp.h> #include <netinet/tcp.h>
@ -919,6 +920,11 @@ public:
ZT_PHY_CLOSE_SOCKET(sws.sock); 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) { if (callHandlers) {
switch(sws.type) { switch(sws.type) {
case ZT_PHY_SOCKET_TCP_OUT_PENDING: case ZT_PHY_SOCKET_TCP_OUT_PENDING: