Add a dummy Ethernet tap and a build option to enable it.

This commit is contained in:
Adam Ierymenko 2017-04-26 08:42:49 -07:00
parent e7cd888c7d
commit 72def658d0
3 changed files with 164 additions and 5 deletions

View File

@ -54,7 +54,7 @@ ifeq ($(ZT_RULES_ENGINE_DEBUGGING),1)
endif
ifeq ($(ZT_DEBUG),1)
DEFS+=-DZT_TRACE
override DEFS+=-DZT_TRACE
override CFLAGS+=-Wall -g -O -pthread $(INCLUDES) $(DEFS)
override CXXFLAGS+=-Wall -g -O -std=c++11 -pthread $(INCLUDES) $(DEFS)
override LDFLAGS+=
@ -72,6 +72,10 @@ else
STRIP+=--strip-all
endif
ifeq ($(ZT_USE_TEST_TAP),1)
override DEFS+=-DZT_USE_TEST_TAP
endif
# Uncomment for gprof profile build
#CFLAGS=-Wall -g -pg -pthread $(INCLUDES) $(DEFS)
#CXXFLAGS=-Wall -g -pg -pthread $(INCLUDES) $(DEFS)

148
osdep/TestEthernetTap.hpp Normal file
View File

@ -0,0 +1,148 @@
/*
* ZeroTier One - Network Virtualization Everywhere
* Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ZT_TESTETHERNETTAP_HPP
#define ZT_TESTETHERNETTAP_HPP
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string>
#include <vector>
#include <stdexcept>
#include <set>
#include "../node/Constants.hpp"
#include "../node/InetAddress.hpp"
#include "../node/MulticastGroup.hpp"
#include "../node/Mutex.hpp"
#include "../node/Utils.hpp"
namespace ZeroTier {
/**
* Dummy test Ethernet tap that does not actually open a device on the system
*/
class TestEthernetTap
{
public:
TestEthernetTap(
const char *homePath,
const MAC &mac,
unsigned int mtu,
unsigned int metric,
uint64_t nwid,
const char *friendlyName,
void (*handler)(void *,void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int),
void *arg) :
_nwid(nwid),
_dev("zt_test_"),
_enabled(true)
{
char tmp[32];
Utils::snprintf(tmp,sizeof(tmp),"%.16llx",(unsigned long long)_nwid);
_dev.append(tmp);
#ifdef ZT_TEST_TAP_REPORT_TO
_reportTo.fromString(ZT_TEST_TAP_REPORT_TO);
if (_reportTo.ss_family == AF_INET)
_reportsock = socket(AF_INET,SOCK_DGRAM,0);
else if (_reportTo.ss_family == AF_INET6)
_reportsock = socket(AF_INET6,SOCK_DGRAM,0);
else _reportsock = -1;
#endif
}
~TestEthernetTap()
{
#ifdef ZT_TEST_TAP_REPORT_TO
if (_reportsock >= 0)
close(_reportsock);
#endif
}
inline void setEnabled(bool en) { _enabled = en; }
inline bool enabled() const { return _enabled; }
inline bool addIp(const InetAddress &ip)
{
Mutex::Lock _l(_lock);
_ips.insert(ip);
return true;
}
inline bool removeIp(const InetAddress &ip)
{
Mutex::Lock _l(_lock);
_ips.erase(ip);
return true;
}
inline std::vector<InetAddress> ips() const
{
Mutex::Lock _l(_lock);
return std::vector<InetAddress>(_ips.begin(),_ips.end());
}
inline void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
{
#ifdef ZT_TEST_TAP_REPORT_TO
char tmp[10000];
if ((_reportsock >= 0)&&(len < (sizeof(tmp) - 22))) {
const uint64_t nwid2 = Utils::hton(_nwid);
memcpy(tmp,&nwid2,8);
from.copyTo(tmp + 8,6);
to.copyTo(tmp + 14,6);
const uint16_t etherType2 = Utils::hton((uint16_t)etherType);
memcpy(tmp + 20,&etherType2,2);
memcpy(tmp + 22,data,len);
sendto(_reportsock,tmp,len + 22,0,reinterpret_cast<const struct sockaddr *>(&_reportTo),(_reportTo.ss_family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6));
}
#endif
}
inline std::string deviceName() const
{
return _dev;
}
inline void setFriendlyName(const char *friendlyName)
{
}
inline void scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed)
{
}
private:
uint64_t _nwid;
std::string _dev;
std::set<InetAddress> _ips;
InetAddress _reportTo;
#ifdef ZT_TEST_TAP_REPORT_TO
int _reportsock;
#endif
bool _enabled;
Mutex _lock;
};
} // namespace ZeroTier
#endif

View File

@ -89,14 +89,19 @@ using json = nlohmann::json;
#include "../controller/EmbeddedNetworkController.hpp"
// Include the right tap device driver for this platform -- add new platforms here
#ifdef ZT_USE_TEST_TAP
#include "../osdep/TestEthernetTap.hpp"
namespace ZeroTier { typedef TestEthernetTap EthernetTap; }
#else
#ifdef ZT_SERVICE_NETCON
// In network containers builds, use the virtual netcon endpoint instead of a tun/tap port driver
#include "../netcon/NetconEthernetTap.hpp"
namespace ZeroTier { typedef NetconEthernetTap EthernetTap; }
#else // not ZT_SERVICE_NETCON so pick a tap driver
#else
#ifdef __APPLE__
#include "../osdep/OSXEthernetTap.hpp"
@ -121,9 +126,11 @@ namespace ZeroTier { typedef BSDEthernetTap EthernetTap; }
#endif // ZT_SERVICE_NETCON
#endif // ZT_USE_TEST_TAP
// Sanity limits for HTTP
#define ZT_MAX_HTTP_MESSAGE_SIZE (1024 * 1024 * 64)
#define ZT_MAX_HTTP_CONNECTIONS 64
#define ZT_MAX_HTTP_CONNECTIONS 65536
// Interface metric for ZeroTier taps -- this ensures that if we are on WiFi and also
// bridged via ZeroTier to the same LAN traffic will (if the OS is sane) prefer WiFi.