mirror of
https://github.com/ZeroTier/ZeroTierOne
synced 2025-08-19 21:03:57 -07:00
clang-format
This commit is contained in:
parent
d45f280cb7
commit
ba2a4a605c
140 changed files with 19214 additions and 17403 deletions
|
@ -15,49 +15,45 @@
|
|||
|
||||
#ifdef __APPLE__
|
||||
|
||||
#include "../node/Utils.hpp"
|
||||
#include "../node/Mutex.hpp"
|
||||
#include "../node/Dictionary.hpp"
|
||||
#include "OSUtils.hpp"
|
||||
#include "../node/Mutex.hpp"
|
||||
#include "../node/Utils.hpp"
|
||||
#include "MacDNSHelper.hpp"
|
||||
#include "MacEthernetTap.hpp"
|
||||
#include "MacEthernetTapAgent.h"
|
||||
#include "MacDNSHelper.hpp"
|
||||
#include "OSUtils.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <filesystem>
|
||||
#include <ifaddrs.h>
|
||||
#include <map>
|
||||
#include <net/if.h>
|
||||
#include <net/if_dl.h>
|
||||
#include <net/route.h>
|
||||
#include <netinet/in.h>
|
||||
#include <set>
|
||||
#include <signal.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/select.h>
|
||||
#include <sys/cdefs.h>
|
||||
#include <sys/uio.h>
|
||||
#include <sys/param.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <net/route.h>
|
||||
#include <net/if.h>
|
||||
#include <net/if_dl.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <ifaddrs.h>
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
#include <sys/cdefs.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/param.h>
|
||||
#include <sys/select.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/uio.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static const ZeroTier::MulticastGroup _blindWildcardMulticastGroup(ZeroTier::MAC(0xff),0);
|
||||
static const ZeroTier::MulticastGroup _blindWildcardMulticastGroup(ZeroTier::MAC(0xff), 0);
|
||||
|
||||
#define MACOS_FETH_MAX_MTU_SYSCTL "net.link.fake.max_mtu"
|
||||
|
||||
|
@ -68,45 +64,45 @@ static bool globalTapInitialized = false;
|
|||
static bool fethMaxMtuAdjusted = false;
|
||||
|
||||
MacEthernetTap::MacEthernetTap(
|
||||
const char *homePath,
|
||||
const MAC &mac,
|
||||
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 *data,unsigned int len),
|
||||
void *arg) :
|
||||
_handler(handler),
|
||||
_arg(arg),
|
||||
_nwid(nwid),
|
||||
_homePath(homePath),
|
||||
_mtu(mtu),
|
||||
_metric(metric),
|
||||
_devNo(0),
|
||||
_agentStdin(-1),
|
||||
_agentStdout(-1),
|
||||
_agentStderr(-1),
|
||||
_agentStdin2(-1),
|
||||
_agentStdout2(-1),
|
||||
_agentStderr2(-1),
|
||||
_agentPid(-1),
|
||||
_enabled(true),
|
||||
_lastIfAddrsUpdate(0)
|
||||
const char* friendlyName,
|
||||
void (*handler)(void*, void*, uint64_t, const MAC&, const MAC&, unsigned int, unsigned int, const void* data, unsigned int len),
|
||||
void* arg)
|
||||
: _handler(handler)
|
||||
, _arg(arg)
|
||||
, _nwid(nwid)
|
||||
, _homePath(homePath)
|
||||
, _mtu(mtu)
|
||||
, _metric(metric)
|
||||
, _devNo(0)
|
||||
, _agentStdin(-1)
|
||||
, _agentStdout(-1)
|
||||
, _agentStderr(-1)
|
||||
, _agentStdin2(-1)
|
||||
, _agentStdout2(-1)
|
||||
, _agentStderr2(-1)
|
||||
, _agentPid(-1)
|
||||
, _enabled(true)
|
||||
, _lastIfAddrsUpdate(0)
|
||||
{
|
||||
char ethaddr[64],mtustr[16],devnostr[16],devstr[16],metricstr[16];
|
||||
OSUtils::ztsnprintf(ethaddr,sizeof(ethaddr),"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",(int)mac[0],(int)mac[1],(int)mac[2],(int)mac[3],(int)mac[4],(int)mac[5]);
|
||||
OSUtils::ztsnprintf(mtustr,sizeof(mtustr),"%u",mtu);
|
||||
OSUtils::ztsnprintf(metricstr,sizeof(metricstr),"%u",metric);
|
||||
char ethaddr[64], mtustr[16], devnostr[16], devstr[16], metricstr[16];
|
||||
OSUtils::ztsnprintf(ethaddr, sizeof(ethaddr), "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", (int)mac[0], (int)mac[1], (int)mac[2], (int)mac[3], (int)mac[4], (int)mac[5]);
|
||||
OSUtils::ztsnprintf(mtustr, sizeof(mtustr), "%u", mtu);
|
||||
OSUtils::ztsnprintf(metricstr, sizeof(metricstr), "%u", metric);
|
||||
|
||||
std::string agentPath(homePath);
|
||||
agentPath.push_back(ZT_PATH_SEPARATOR);
|
||||
agentPath.append("MacEthernetTapAgent");
|
||||
if (!OSUtils::fileExists(agentPath.c_str()))
|
||||
if (! OSUtils::fileExists(agentPath.c_str()))
|
||||
throw std::runtime_error("MacEthernetTapAgent not present in ZeroTier home");
|
||||
|
||||
Mutex::Lock _gl(globalTapCreateLock); // only make one at a time
|
||||
Mutex::Lock _gl(globalTapCreateLock); // only make one at a time
|
||||
|
||||
if (!fethMaxMtuAdjusted) {
|
||||
if (! fethMaxMtuAdjusted) {
|
||||
fethMaxMtuAdjusted = true;
|
||||
int old_mtu = 0;
|
||||
size_t old_mtu_len = sizeof(old_mtu);
|
||||
|
@ -116,29 +112,30 @@ MacEthernetTap::MacEthernetTap(
|
|||
|
||||
// Destroy all feth devices on first tap start in case ZeroTier did not exit cleanly last time.
|
||||
// We leave interfaces less than feth100 alone in case something else is messing with feth devices.
|
||||
if (!globalTapInitialized) {
|
||||
if (! globalTapInitialized) {
|
||||
globalTapInitialized = true;
|
||||
struct ifaddrs *ifa = (struct ifaddrs *)0;
|
||||
struct ifaddrs* ifa = (struct ifaddrs*)0;
|
||||
std::set<std::string> deleted;
|
||||
if (!getifaddrs(&ifa)) {
|
||||
struct ifaddrs *p = ifa;
|
||||
if (! getifaddrs(&ifa)) {
|
||||
struct ifaddrs* p = ifa;
|
||||
while (p) {
|
||||
int nameLen = (int)strlen(p->ifa_name);
|
||||
// Delete feth# from feth0 to feth9999, but don't touch >10000.
|
||||
if ((!strncmp(p->ifa_name,"feth",4))&&(nameLen >= 5)&&(nameLen <= 8)&&(deleted.count(std::string(p->ifa_name)) == 0)) {
|
||||
if ((! strncmp(p->ifa_name, "feth", 4)) && (nameLen >= 5) && (nameLen <= 8) && (deleted.count(std::string(p->ifa_name)) == 0)) {
|
||||
deleted.insert(std::string(p->ifa_name));
|
||||
const char *args[4];
|
||||
const char* args[4];
|
||||
args[0] = "/sbin/ifconfig";
|
||||
args[1] = p->ifa_name;
|
||||
args[2] = "destroy";
|
||||
args[3] = (char *)0;
|
||||
args[3] = (char*)0;
|
||||
const pid_t pid = vfork();
|
||||
if (pid == 0) {
|
||||
execv(args[0],const_cast<char **>(args));
|
||||
execv(args[0], const_cast<char**>(args));
|
||||
_exit(-1);
|
||||
} else if (pid > 0) {
|
||||
}
|
||||
else if (pid > 0) {
|
||||
int rv = 0;
|
||||
waitpid(pid,&rv,0);
|
||||
waitpid(pid, &rv, 0);
|
||||
}
|
||||
}
|
||||
p = p->ifa_next;
|
||||
|
@ -148,15 +145,15 @@ MacEthernetTap::MacEthernetTap(
|
|||
}
|
||||
|
||||
unsigned int devNo = 100 + ((nwid ^ (nwid >> 32) ^ (nwid >> 48)) % 4900);
|
||||
for(;;) {
|
||||
OSUtils::ztsnprintf(devnostr,sizeof(devnostr),"%u",devNo);
|
||||
OSUtils::ztsnprintf(devstr,sizeof(devstr),"feth%u",devNo);
|
||||
for (;;) {
|
||||
OSUtils::ztsnprintf(devnostr, sizeof(devnostr), "%u", devNo);
|
||||
OSUtils::ztsnprintf(devstr, sizeof(devstr), "feth%u", devNo);
|
||||
bool duplicate = false;
|
||||
struct ifaddrs *ifa = (struct ifaddrs *)0;
|
||||
if (!getifaddrs(&ifa)) {
|
||||
struct ifaddrs *p = ifa;
|
||||
struct ifaddrs* ifa = (struct ifaddrs*)0;
|
||||
if (! getifaddrs(&ifa)) {
|
||||
struct ifaddrs* p = ifa;
|
||||
while (p) {
|
||||
if (!strcmp(p->ifa_name,devstr)) {
|
||||
if (! strcmp(p->ifa_name, devstr)) {
|
||||
duplicate = true;
|
||||
break;
|
||||
}
|
||||
|
@ -168,7 +165,8 @@ MacEthernetTap::MacEthernetTap(
|
|||
devNo = (devNo + 1) % 5000;
|
||||
if (devNo < 100)
|
||||
devNo = 100;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
_dev = devstr;
|
||||
_devNo = devNo;
|
||||
break;
|
||||
|
@ -196,29 +194,31 @@ MacEthernetTap::MacEthernetTap(
|
|||
long apid = (long)fork();
|
||||
if (apid < 0) {
|
||||
throw std::runtime_error("fork failed");
|
||||
} else if (apid == 0) {
|
||||
::dup2(agentStdin[0],STDIN_FILENO);
|
||||
::dup2(agentStdout[1],STDOUT_FILENO);
|
||||
::dup2(agentStderr[1],STDERR_FILENO);
|
||||
}
|
||||
else if (apid == 0) {
|
||||
::dup2(agentStdin[0], STDIN_FILENO);
|
||||
::dup2(agentStdout[1], STDOUT_FILENO);
|
||||
::dup2(agentStderr[1], STDERR_FILENO);
|
||||
::close(agentStdin[0]);
|
||||
::close(agentStdin[1]);
|
||||
::close(agentStdout[0]);
|
||||
::close(agentStdout[1]);
|
||||
::close(agentStderr[0]);
|
||||
::close(agentStderr[1]);
|
||||
::execl(agentPath.c_str(),agentPath.c_str(),devnostr,ethaddr,mtustr,metricstr,(char *)0);
|
||||
::execl(agentPath.c_str(), agentPath.c_str(), devnostr, ethaddr, mtustr, metricstr, (char*)0);
|
||||
::_exit(-1);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
_agentPid = apid;
|
||||
|
||||
// Wait up to 10 seconds for the subprocess to actually create the device. This prevents
|
||||
// things like routes from being created before the device exists.
|
||||
for(int waitLoops=0;;++waitLoops) {
|
||||
struct ifaddrs *ifa = (struct ifaddrs *)0;
|
||||
if (!getifaddrs(&ifa)) {
|
||||
struct ifaddrs *p = ifa;
|
||||
for (int waitLoops = 0;; ++waitLoops) {
|
||||
struct ifaddrs* ifa = (struct ifaddrs*)0;
|
||||
if (! getifaddrs(&ifa)) {
|
||||
struct ifaddrs* p = ifa;
|
||||
while (p) {
|
||||
if ((p->ifa_name)&&(!strcmp(devstr, p->ifa_name))) {
|
||||
if ((p->ifa_name) && (! strcmp(devstr, p->ifa_name))) {
|
||||
waitLoops = -1;
|
||||
break;
|
||||
}
|
||||
|
@ -228,7 +228,8 @@ MacEthernetTap::MacEthernetTap(
|
|||
}
|
||||
if (waitLoops == -1) {
|
||||
break;
|
||||
} else if (waitLoops >= 100) { // 10 seconds
|
||||
}
|
||||
else if (waitLoops >= 100) { // 10 seconds
|
||||
throw std::runtime_error("feth device creation timed out");
|
||||
}
|
||||
Thread::sleep(100);
|
||||
|
@ -241,61 +242,67 @@ MacEthernetTap::MacEthernetTap(
|
|||
MacEthernetTap::~MacEthernetTap()
|
||||
{
|
||||
char tmp[64];
|
||||
const char *args[4];
|
||||
pid_t pid0,pid1;
|
||||
const char* args[4];
|
||||
pid_t pid0, pid1;
|
||||
|
||||
MacDNSHelper::removeDNS(_nwid);
|
||||
MacDNSHelper::removeIps4(_nwid);
|
||||
MacDNSHelper::removeIps6(_nwid);
|
||||
|
||||
Mutex::Lock _gl(globalTapCreateLock);
|
||||
::write(_shutdownSignalPipe[1],"\0",1); // causes thread to exit
|
||||
::write(_shutdownSignalPipe[1], "\0", 1); // causes thread to exit
|
||||
|
||||
int ec = 0;
|
||||
::kill(_agentPid,SIGKILL);
|
||||
::waitpid(_agentPid,&ec,0);
|
||||
::kill(_agentPid, SIGKILL);
|
||||
::waitpid(_agentPid, &ec, 0);
|
||||
|
||||
args[0] = "/sbin/ifconfig";
|
||||
args[1] = _dev.c_str();
|
||||
args[2] = "destroy";
|
||||
args[3] = (char *)0;
|
||||
args[3] = (char*)0;
|
||||
pid0 = vfork();
|
||||
if (pid0 == 0) {
|
||||
execv(args[0],const_cast<char **>(args));
|
||||
execv(args[0], const_cast<char**>(args));
|
||||
_exit(-1);
|
||||
}
|
||||
|
||||
snprintf(tmp,sizeof(tmp),"feth%u",_devNo + 5000);
|
||||
//args[0] = "/sbin/ifconfig";
|
||||
snprintf(tmp, sizeof(tmp), "feth%u", _devNo + 5000);
|
||||
// args[0] = "/sbin/ifconfig";
|
||||
args[1] = tmp;
|
||||
//args[2] = "destroy";
|
||||
//args[3] = (char *)0;
|
||||
// args[2] = "destroy";
|
||||
// args[3] = (char *)0;
|
||||
pid1 = vfork();
|
||||
if (pid1 == 0) {
|
||||
execv(args[0],const_cast<char **>(args));
|
||||
execv(args[0], const_cast<char**>(args));
|
||||
_exit(-1);
|
||||
}
|
||||
|
||||
if (pid0 > 0) {
|
||||
int rv = 0;
|
||||
waitpid(pid0,&rv,0);
|
||||
waitpid(pid0, &rv, 0);
|
||||
}
|
||||
if (pid1 > 0) {
|
||||
int rv = 0;
|
||||
waitpid(pid1,&rv,0);
|
||||
waitpid(pid1, &rv, 0);
|
||||
}
|
||||
|
||||
Thread::join(_thread);
|
||||
}
|
||||
|
||||
void MacEthernetTap::setEnabled(bool en) { _enabled = en; }
|
||||
bool MacEthernetTap::enabled() const { return _enabled; }
|
||||
void MacEthernetTap::setEnabled(bool en)
|
||||
{
|
||||
_enabled = en;
|
||||
}
|
||||
bool MacEthernetTap::enabled() const
|
||||
{
|
||||
return _enabled;
|
||||
}
|
||||
|
||||
bool MacEthernetTap::addIp(const InetAddress &ip)
|
||||
bool MacEthernetTap::addIp(const InetAddress& ip)
|
||||
{
|
||||
char tmp[128];
|
||||
|
||||
if (!ip)
|
||||
if (! ip)
|
||||
return false;
|
||||
|
||||
std::string cmd;
|
||||
|
@ -309,18 +316,18 @@ bool MacEthernetTap::addIp(const InetAddress &ip)
|
|||
|
||||
uint16_t l = (uint16_t)cmd.length();
|
||||
_putLock.lock();
|
||||
write(_agentStdin,&l,2);
|
||||
write(_agentStdin,cmd.data(),cmd.length());
|
||||
write(_agentStdin, &l, 2);
|
||||
write(_agentStdin, cmd.data(), cmd.length());
|
||||
_putLock.unlock();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MacEthernetTap::removeIp(const InetAddress &ip)
|
||||
bool MacEthernetTap::removeIp(const InetAddress& ip)
|
||||
{
|
||||
char tmp[128];
|
||||
|
||||
if (!ip)
|
||||
if (! ip)
|
||||
return false;
|
||||
|
||||
std::string cmd;
|
||||
|
@ -334,8 +341,8 @@ bool MacEthernetTap::removeIp(const InetAddress &ip)
|
|||
|
||||
uint16_t l = (uint16_t)cmd.length();
|
||||
_putLock.lock();
|
||||
write(_agentStdin,&l,2);
|
||||
write(_agentStdin,cmd.data(),cmd.length());
|
||||
write(_agentStdin, &l, 2);
|
||||
write(_agentStdin, cmd.data(), cmd.length());
|
||||
_putLock.unlock();
|
||||
|
||||
return true;
|
||||
|
@ -350,49 +357,49 @@ std::vector<InetAddress> MacEthernetTap::ips() const
|
|||
}
|
||||
_lastIfAddrsUpdate = now;
|
||||
|
||||
struct ifaddrs *ifa = (struct ifaddrs *)0;
|
||||
struct ifaddrs* ifa = (struct ifaddrs*)0;
|
||||
std::vector<InetAddress> r;
|
||||
|
||||
if (!getifaddrs(&ifa)) {
|
||||
struct ifaddrs *p = ifa;
|
||||
if (! getifaddrs(&ifa)) {
|
||||
struct ifaddrs* p = ifa;
|
||||
while (p) {
|
||||
if ((p->ifa_name)&&(!strcmp(p->ifa_name,_dev.c_str()))&&(p->ifa_addr)) {
|
||||
switch(p->ifa_addr->sa_family) {
|
||||
if ((p->ifa_name) && (! strcmp(p->ifa_name, _dev.c_str())) && (p->ifa_addr)) {
|
||||
switch (p->ifa_addr->sa_family) {
|
||||
case AF_INET: {
|
||||
struct sockaddr_in *sin = (struct sockaddr_in *)p->ifa_addr;
|
||||
struct sockaddr_in *nm = (struct sockaddr_in *)p->ifa_netmask;
|
||||
r.push_back(InetAddress(&(sin->sin_addr.s_addr),4,Utils::countBits((uint32_t)nm->sin_addr.s_addr)));
|
||||
} break;
|
||||
struct sockaddr_in* sin = (struct sockaddr_in*)p->ifa_addr;
|
||||
struct sockaddr_in* nm = (struct sockaddr_in*)p->ifa_netmask;
|
||||
r.push_back(InetAddress(&(sin->sin_addr.s_addr), 4, Utils::countBits((uint32_t)nm->sin_addr.s_addr)));
|
||||
} break;
|
||||
case AF_INET6: {
|
||||
struct sockaddr_in6 *sin = (struct sockaddr_in6 *)p->ifa_addr;
|
||||
struct sockaddr_in6 *nm = (struct sockaddr_in6 *)p->ifa_netmask;
|
||||
struct sockaddr_in6* sin = (struct sockaddr_in6*)p->ifa_addr;
|
||||
struct sockaddr_in6* nm = (struct sockaddr_in6*)p->ifa_netmask;
|
||||
uint32_t b[4];
|
||||
memcpy(b,nm->sin6_addr.s6_addr,sizeof(b));
|
||||
r.push_back(InetAddress(sin->sin6_addr.s6_addr,16,Utils::countBits(b[0]) + Utils::countBits(b[1]) + Utils::countBits(b[2]) + Utils::countBits(b[3])));
|
||||
} break;
|
||||
memcpy(b, nm->sin6_addr.s6_addr, sizeof(b));
|
||||
r.push_back(InetAddress(sin->sin6_addr.s6_addr, 16, Utils::countBits(b[0]) + Utils::countBits(b[1]) + Utils::countBits(b[2]) + Utils::countBits(b[3])));
|
||||
} break;
|
||||
}
|
||||
}
|
||||
p = p->ifa_next;
|
||||
}
|
||||
freeifaddrs(ifa);
|
||||
}
|
||||
std::sort(r.begin(),r.end());
|
||||
r.erase(std::unique(r.begin(),r.end()),r.end());
|
||||
std::sort(r.begin(), r.end());
|
||||
r.erase(std::unique(r.begin(), r.end()), r.end());
|
||||
|
||||
_ifaddrs = r;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
void MacEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
|
||||
void MacEthernetTap::put(const MAC& from, const MAC& to, unsigned int etherType, const void* data, unsigned int len)
|
||||
{
|
||||
struct iovec iov[3];
|
||||
unsigned char hdr[15];
|
||||
uint16_t l;
|
||||
if ((_agentStdin > 0)&&(len <= _mtu)&&(_enabled)) {
|
||||
if ((_agentStdin > 0) && (len <= _mtu) && (_enabled)) {
|
||||
hdr[0] = ZT_MACETHERNETTAPAGENT_STDIN_CMD_PACKET;
|
||||
to.copyTo(hdr + 1,6);
|
||||
from.copyTo(hdr + 7,6);
|
||||
to.copyTo(hdr + 1, 6);
|
||||
from.copyTo(hdr + 7, 6);
|
||||
hdr[13] = (unsigned char)((etherType >> 8) & 0xff);
|
||||
hdr[14] = (unsigned char)(etherType & 0xff);
|
||||
l = (uint16_t)(len + 15);
|
||||
|
@ -400,30 +407,35 @@ void MacEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType,co
|
|||
iov[0].iov_len = 2;
|
||||
iov[1].iov_base = hdr;
|
||||
iov[1].iov_len = 15;
|
||||
iov[2].iov_base = const_cast<void *>(data);
|
||||
iov[2].iov_base = const_cast<void*>(data);
|
||||
iov[2].iov_len = len;
|
||||
_putLock.lock();
|
||||
writev(_agentStdin,iov,3);
|
||||
writev(_agentStdin, iov, 3);
|
||||
_putLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
std::string MacEthernetTap::deviceName() const { return _dev; }
|
||||
void MacEthernetTap::setFriendlyName(const char *friendlyName) {}
|
||||
std::string MacEthernetTap::deviceName() const
|
||||
{
|
||||
return _dev;
|
||||
}
|
||||
void MacEthernetTap::setFriendlyName(const char* friendlyName)
|
||||
{
|
||||
}
|
||||
|
||||
void MacEthernetTap::scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed)
|
||||
void MacEthernetTap::scanMulticastGroups(std::vector<MulticastGroup>& added, std::vector<MulticastGroup>& removed)
|
||||
{
|
||||
std::vector<MulticastGroup> newGroups;
|
||||
|
||||
struct ifmaddrs *ifmap = (struct ifmaddrs *)0;
|
||||
if (!getifmaddrs(&ifmap)) {
|
||||
struct ifmaddrs *p = ifmap;
|
||||
struct ifmaddrs* ifmap = (struct ifmaddrs*)0;
|
||||
if (! getifmaddrs(&ifmap)) {
|
||||
struct ifmaddrs* p = ifmap;
|
||||
while (p) {
|
||||
if (p->ifma_addr->sa_family == AF_LINK) {
|
||||
struct sockaddr_dl *in = (struct sockaddr_dl *)p->ifma_name;
|
||||
struct sockaddr_dl *la = (struct sockaddr_dl *)p->ifma_addr;
|
||||
if ((la->sdl_alen == 6)&&(in->sdl_nlen <= _dev.length())&&(!memcmp(_dev.data(),in->sdl_data,in->sdl_nlen)))
|
||||
newGroups.push_back(MulticastGroup(MAC(la->sdl_data + la->sdl_nlen,6),0));
|
||||
struct sockaddr_dl* in = (struct sockaddr_dl*)p->ifma_name;
|
||||
struct sockaddr_dl* la = (struct sockaddr_dl*)p->ifma_addr;
|
||||
if ((la->sdl_alen == 6) && (in->sdl_nlen <= _dev.length()) && (! memcmp(_dev.data(), in->sdl_data, in->sdl_nlen)))
|
||||
newGroups.push_back(MulticastGroup(MAC(la->sdl_data + la->sdl_nlen, 6), 0));
|
||||
}
|
||||
p = p->ifma_next;
|
||||
}
|
||||
|
@ -431,18 +443,18 @@ void MacEthernetTap::scanMulticastGroups(std::vector<MulticastGroup> &added,std:
|
|||
}
|
||||
|
||||
std::vector<InetAddress> allIps(ips());
|
||||
for(std::vector<InetAddress>::iterator ip(allIps.begin());ip!=allIps.end();++ip)
|
||||
for (std::vector<InetAddress>::iterator ip(allIps.begin()); ip != allIps.end(); ++ip)
|
||||
newGroups.push_back(MulticastGroup::deriveMulticastGroupForAddressResolution(*ip));
|
||||
|
||||
std::sort(newGroups.begin(),newGroups.end());
|
||||
newGroups.erase(std::unique(newGroups.begin(),newGroups.end()),newGroups.end());
|
||||
std::sort(newGroups.begin(), newGroups.end());
|
||||
newGroups.erase(std::unique(newGroups.begin(), newGroups.end()), newGroups.end());
|
||||
|
||||
for(std::vector<MulticastGroup>::iterator m(newGroups.begin());m!=newGroups.end();++m) {
|
||||
if (!std::binary_search(_multicastGroups.begin(),_multicastGroups.end(),*m))
|
||||
for (std::vector<MulticastGroup>::iterator m(newGroups.begin()); m != newGroups.end(); ++m) {
|
||||
if (! std::binary_search(_multicastGroups.begin(), _multicastGroups.end(), *m))
|
||||
added.push_back(*m);
|
||||
}
|
||||
for(std::vector<MulticastGroup>::iterator m(_multicastGroups.begin());m!=_multicastGroups.end();++m) {
|
||||
if (!std::binary_search(newGroups.begin(),newGroups.end(),*m))
|
||||
for (std::vector<MulticastGroup>::iterator m(_multicastGroups.begin()); m != _multicastGroups.end(); ++m) {
|
||||
if (! std::binary_search(newGroups.begin(), newGroups.end(), *m))
|
||||
removed.push_back(*m);
|
||||
}
|
||||
|
||||
|
@ -457,13 +469,13 @@ void MacEthernetTap::setMtu(unsigned int mtu)
|
|||
cmd.push_back((char)ZT_MACETHERNETTAPAGENT_STDIN_CMD_IFCONFIG);
|
||||
cmd.append("mtu");
|
||||
cmd.push_back(0);
|
||||
OSUtils::ztsnprintf(tmp,sizeof(tmp),"%u",mtu);
|
||||
OSUtils::ztsnprintf(tmp, sizeof(tmp), "%u", mtu);
|
||||
cmd.append(tmp);
|
||||
cmd.push_back(0);
|
||||
uint16_t l = (uint16_t)cmd.length();
|
||||
_putLock.lock();
|
||||
write(_agentStdin,&l,2);
|
||||
write(_agentStdin,cmd.data(),cmd.length());
|
||||
write(_agentStdin, &l, 2);
|
||||
write(_agentStdin, cmd.data(), cmd.length());
|
||||
_putLock.unlock();
|
||||
_mtu = mtu;
|
||||
}
|
||||
|
@ -471,61 +483,62 @@ void MacEthernetTap::setMtu(unsigned int mtu)
|
|||
|
||||
#define ZT_MACETHERNETTAP_AGENT_READ_BUF_SIZE 131072
|
||||
|
||||
void MacEthernetTap::threadMain()
|
||||
throw()
|
||||
void MacEthernetTap::threadMain() throw()
|
||||
{
|
||||
char agentReadBuf[ZT_MACETHERNETTAP_AGENT_READ_BUF_SIZE];
|
||||
char agentStderrBuf[256];
|
||||
fd_set readfds,nullfds;
|
||||
MAC to,from;
|
||||
fd_set readfds, nullfds;
|
||||
MAC to, from;
|
||||
|
||||
Thread::sleep(250);
|
||||
|
||||
const int nfds = std::max(std::max(_shutdownSignalPipe[0],_agentStdout),_agentStderr) + 1;
|
||||
const int nfds = std::max(std::max(_shutdownSignalPipe[0], _agentStdout), _agentStderr) + 1;
|
||||
long agentReadPtr = 0;
|
||||
fcntl(_agentStdout,F_SETFL,fcntl(_agentStdout,F_GETFL)|O_NONBLOCK);
|
||||
fcntl(_agentStderr,F_SETFL,fcntl(_agentStderr,F_GETFL)|O_NONBLOCK);
|
||||
fcntl(_agentStdout, F_SETFL, fcntl(_agentStdout, F_GETFL) | O_NONBLOCK);
|
||||
fcntl(_agentStderr, F_SETFL, fcntl(_agentStderr, F_GETFL) | O_NONBLOCK);
|
||||
|
||||
FD_ZERO(&readfds);
|
||||
FD_ZERO(&nullfds);
|
||||
for(;;) {
|
||||
FD_SET(_shutdownSignalPipe[0],&readfds);
|
||||
FD_SET(_agentStdout,&readfds);
|
||||
FD_SET(_agentStderr,&readfds);
|
||||
select(nfds,&readfds,&nullfds,&nullfds,(struct timeval *)0);
|
||||
for (;;) {
|
||||
FD_SET(_shutdownSignalPipe[0], &readfds);
|
||||
FD_SET(_agentStdout, &readfds);
|
||||
FD_SET(_agentStderr, &readfds);
|
||||
select(nfds, &readfds, &nullfds, &nullfds, (struct timeval*)0);
|
||||
|
||||
if (FD_ISSET(_shutdownSignalPipe[0],&readfds))
|
||||
if (FD_ISSET(_shutdownSignalPipe[0], &readfds))
|
||||
break;
|
||||
|
||||
if (FD_ISSET(_agentStdout,&readfds)) {
|
||||
long n = (long)read(_agentStdout,agentReadBuf + agentReadPtr,ZT_MACETHERNETTAP_AGENT_READ_BUF_SIZE - agentReadPtr);
|
||||
if (FD_ISSET(_agentStdout, &readfds)) {
|
||||
long n = (long)read(_agentStdout, agentReadBuf + agentReadPtr, ZT_MACETHERNETTAP_AGENT_READ_BUF_SIZE - agentReadPtr);
|
||||
if (n > 0) {
|
||||
agentReadPtr += n;
|
||||
while (agentReadPtr >= 2) {
|
||||
long len = *((uint16_t *)agentReadBuf);
|
||||
long len = *((uint16_t*)agentReadBuf);
|
||||
if (agentReadPtr >= (len + 2)) {
|
||||
char *msg = agentReadBuf + 2;
|
||||
char* msg = agentReadBuf + 2;
|
||||
|
||||
if ((len > 14)&&(_enabled)) {
|
||||
to.setTo(msg,6);
|
||||
from.setTo(msg + 6,6);
|
||||
_handler(_arg,(void *)0,_nwid,from,to,ntohs(((const uint16_t *)msg)[6]),0,(const void *)(msg + 14),(unsigned int)len - 14);
|
||||
if ((len > 14) && (_enabled)) {
|
||||
to.setTo(msg, 6);
|
||||
from.setTo(msg + 6, 6);
|
||||
_handler(_arg, (void*)0, _nwid, from, to, ntohs(((const uint16_t*)msg)[6]), 0, (const void*)(msg + 14), (unsigned int)len - 14);
|
||||
}
|
||||
|
||||
if (agentReadPtr > (len + 2)) {
|
||||
memmove(agentReadBuf,agentReadBuf + len + 2,agentReadPtr -= (len + 2));
|
||||
} else {
|
||||
memmove(agentReadBuf, agentReadBuf + len + 2, agentReadPtr -= (len + 2));
|
||||
}
|
||||
else {
|
||||
agentReadPtr = 0;
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (FD_ISSET(_agentStderr,&readfds)) {
|
||||
read(_agentStderr,agentStderrBuf,sizeof(agentStderrBuf));
|
||||
if (FD_ISSET(_agentStderr, &readfds)) {
|
||||
read(_agentStderr, agentStderrBuf, sizeof(agentStderrBuf));
|
||||
/*
|
||||
const ssize_t n = read(_agentStderr,agentStderrBuf,sizeof(agentStderrBuf));
|
||||
if (n > 0)
|
||||
|
@ -544,11 +557,11 @@ void MacEthernetTap::threadMain()
|
|||
::close(_shutdownSignalPipe[1]);
|
||||
}
|
||||
|
||||
void MacEthernetTap::setDns(const char *domain, const std::vector<InetAddress> &servers)
|
||||
void MacEthernetTap::setDns(const char* domain, const std::vector<InetAddress>& servers)
|
||||
{
|
||||
MacDNSHelper::setDNS(this->_nwid, domain, servers);
|
||||
}
|
||||
|
||||
} // namespace ZeroTier
|
||||
} // namespace ZeroTier
|
||||
|
||||
#endif // __APPLE__
|
||||
#endif // __APPLE__
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue