mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-06-06 12:33:44 +02:00
Style match
This commit is contained in:
parent
cd191778c2
commit
269c8d415a
2 changed files with 437 additions and 328 deletions
|
@ -1,11 +1,26 @@
|
||||||
#include <sys/times.h>
|
/*
|
||||||
#include <fcntl.h>
|
* Copyright (c)2019 ZeroTier, Inc.
|
||||||
#include <unistd.h>
|
*
|
||||||
#include <iostream>
|
* Use of this software is governed by the Business Source License included
|
||||||
|
* in the LICENSE.TXT file in the project's root directory.
|
||||||
|
*
|
||||||
|
* Change Date: 2026-01-01
|
||||||
|
*
|
||||||
|
* On the date above, in accordance with the Business Source License, use
|
||||||
|
* of this software will be governed by version 2.0 of the Apache License.
|
||||||
|
*/
|
||||||
|
/****/
|
||||||
|
|
||||||
#include "ExtOsdep.hpp"
|
#include "ExtOsdep.hpp"
|
||||||
#include <list>
|
|
||||||
#include "../node/AtomicCounter.hpp"
|
#include "../node/AtomicCounter.hpp"
|
||||||
|
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include <list>
|
||||||
|
#include <sys/times.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#define ZT_TAP_BUF_SIZE 16384
|
#define ZT_TAP_BUF_SIZE 16384
|
||||||
|
|
||||||
namespace ZeroTier {
|
namespace ZeroTier {
|
||||||
|
@ -22,18 +37,24 @@ struct EodRoute {
|
||||||
};
|
};
|
||||||
static std::list<EodRoute> allRoutes;
|
static std::list<EodRoute> allRoutes;
|
||||||
|
|
||||||
template<typename T> static void __eodSend(const T &t) {
|
template <typename T> static void __eodSend(const T& t)
|
||||||
|
{
|
||||||
write(eodFd, &t, sizeof(t));
|
write(eodFd, &t, sizeof(t));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void strncpyx(char *dest, const char *src, size_t n) {
|
static void strncpyx(char* dest, const char* src, size_t n)
|
||||||
|
{
|
||||||
strncpy(dest, src, n);
|
strncpy(dest, src, n);
|
||||||
if (n > 1) dest[n - 1] = 0;
|
if (n > 1) {
|
||||||
|
dest[n - 1] = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int __eodWait(unsigned char msg, unsigned char *d, unsigned l,
|
static int __eodWait(unsigned char msg, unsigned char* d, unsigned l, unsigned maxl = 0, int* recvfd = nullptr)
|
||||||
unsigned maxl = 0, int *recvfd = nullptr) {
|
{
|
||||||
if (!maxl) maxl = l;
|
if (! maxl) {
|
||||||
|
maxl = l;
|
||||||
|
}
|
||||||
auto start = times(NULL);
|
auto start = times(NULL);
|
||||||
while (1) {
|
while (1) {
|
||||||
msghdr mh;
|
msghdr mh;
|
||||||
|
@ -56,10 +77,7 @@ static int __eodWait(unsigned char msg, unsigned char *d, unsigned l,
|
||||||
|
|
||||||
int r = recvmsg(eodFd, &mh, MSG_TRUNC | MSG_CMSG_CLOEXEC);
|
int r = recvmsg(eodFd, &mh, MSG_TRUNC | MSG_CMSG_CLOEXEC);
|
||||||
if (r > 0) {
|
if (r > 0) {
|
||||||
if (recvfd && mh.msg_controllen >= sizeof(cmsg)
|
if (recvfd && mh.msg_controllen >= sizeof(cmsg) && cmsg.cmsg_len == sizeof(cmsg) && cmsg.cmsg_level == SOL_SOCKET && cmsg.cmsg_type == SCM_RIGHTS) {
|
||||||
&& cmsg.cmsg_len == sizeof(cmsg)
|
|
||||||
&& cmsg.cmsg_level == SOL_SOCKET
|
|
||||||
&& cmsg.cmsg_type == SCM_RIGHTS) {
|
|
||||||
*recvfd = cmsg.fd;
|
*recvfd = cmsg.fd;
|
||||||
fprintf(stderr, "eodWait: received fd %d\n", *recvfd);
|
fprintf(stderr, "eodWait: received fd %d\n", *recvfd);
|
||||||
}
|
}
|
||||||
|
@ -82,27 +100,32 @@ static int __eodWait(unsigned char msg, unsigned char *d, unsigned l,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T> static bool __eodWait(unsigned msg, T &t) {
|
template <typename T> static bool __eodWait(unsigned msg, T& t)
|
||||||
|
{
|
||||||
return __eodWait(msg, (unsigned char*)&t, sizeof(T)) == (int)sizeof(T);
|
return __eodWait(msg, (unsigned char*)&t, sizeof(T)) == (int)sizeof(T);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename M, typename R> static bool __eodXchg(const M &m, unsigned rm, R &r) {
|
template <typename M, typename R> static bool __eodXchg(const M& m, unsigned rm, R& r)
|
||||||
|
{
|
||||||
__eodSend(m);
|
__eodSend(m);
|
||||||
return __eodWait(rm, r);
|
return __eodWait(rm, r);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename M, typename R> static bool eodXchg(const M &m, unsigned rm, R &r) {
|
template <typename M, typename R> static bool eodXchg(const M& m, unsigned rm, R& r)
|
||||||
|
{
|
||||||
Mutex::Lock l(eodMutex);
|
Mutex::Lock l(eodMutex);
|
||||||
return __eodXchg(m, rm, r);
|
return __eodXchg(m, rm, r);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ExtOsdep::init(int fd1, int fd2) {
|
void ExtOsdep::init(int fd1, int fd2)
|
||||||
|
{
|
||||||
eodFd = fd1;
|
eodFd = fd1;
|
||||||
eodMgmtFd = fd2;
|
eodMgmtFd = fd2;
|
||||||
fcntl(eodMgmtFd, F_SETFL, O_NONBLOCK);
|
fcntl(eodMgmtFd, F_SETFL, O_NONBLOCK);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ExtOsdep::started(int *f, void **cp) {
|
void ExtOsdep::started(int* f, void** cp)
|
||||||
|
{
|
||||||
*f = eodMgmtFd;
|
*f = eodMgmtFd;
|
||||||
*cp = (void*)eodMgmtFd;
|
*cp = (void*)eodMgmtFd;
|
||||||
|
|
||||||
|
@ -114,30 +137,42 @@ void ExtOsdep::started(int *f, void **cp) {
|
||||||
static std::string mgmtrd;
|
static std::string mgmtrd;
|
||||||
static std::string mgmtwr;
|
static std::string mgmtwr;
|
||||||
|
|
||||||
bool ExtOsdep::mgmtWritable(void *cookie) {
|
bool ExtOsdep::mgmtWritable(void* cookie)
|
||||||
if (cookie != (void *)eodMgmtFd) return false;
|
{
|
||||||
if (mgmtwr.size() == 0) return true;
|
if (cookie != (void*)eodMgmtFd) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (mgmtwr.size() == 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
auto sz = write(eodMgmtFd, mgmtwr.data(), mgmtwr.size());
|
auto sz = write(eodMgmtFd, mgmtwr.data(), mgmtwr.size());
|
||||||
if (sz <= 0) return false;
|
if (sz <= 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
mgmtwr.erase(mgmtwr.begin(), mgmtwr.begin() + sz);
|
mgmtwr.erase(mgmtwr.begin(), mgmtwr.begin() + sz);
|
||||||
return mgmtwr.empty();
|
return mgmtwr.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ExtOsdep::mgmtRecv(void *cookie, void *data, unsigned long len,
|
bool ExtOsdep::mgmtRecv(void* cookie, void* data, unsigned long len, std::function<unsigned(unsigned, const std::string&, const std::string&, std::string&)> cb)
|
||||||
std::function<unsigned (unsigned, const std::string &, const std::string &, std::string &)> cb) {
|
{
|
||||||
if (cookie != (void *)eodMgmtFd) return false;
|
if (cookie != (void*)eodMgmtFd) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
mgmtrd.append((char*)data, len);
|
mgmtrd.append((char*)data, len);
|
||||||
while (1) {
|
while (1) {
|
||||||
auto req = (zt_eod_mgmt_req*)mgmtrd.data();
|
auto req = (zt_eod_mgmt_req*)mgmtrd.data();
|
||||||
if (mgmtrd.size() < sizeof(*req)) break;
|
if (mgmtrd.size() < sizeof(*req)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
unsigned reqsz = sizeof(*req) + req->pathlen + req->datalen;
|
unsigned reqsz = sizeof(*req) + req->pathlen + req->datalen;
|
||||||
if (mgmtrd.size() < reqsz) break;
|
if (mgmtrd.size() < reqsz) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
std::string resp;
|
std::string resp;
|
||||||
char* p = (char*)req->data;
|
char* p = (char*)req->data;
|
||||||
zt_eod_mgmt_reply rep;
|
zt_eod_mgmt_reply rep;
|
||||||
rep.scode = cb(req->method, std::string(p, p + req->pathlen),
|
rep.scode = cb(req->method, std::string(p, p + req->pathlen), std::string(p + req->pathlen, p + req->pathlen + req->datalen), resp);
|
||||||
std::string(p + req->pathlen, p + req->pathlen + req->datalen), resp);
|
|
||||||
rep.datalen = resp.size();
|
rep.datalen = resp.size();
|
||||||
|
|
||||||
mgmtrd.erase(mgmtrd.begin(), mgmtrd.begin() + reqsz);
|
mgmtrd.erase(mgmtrd.begin(), mgmtrd.begin() + reqsz);
|
||||||
|
@ -146,34 +181,41 @@ bool ExtOsdep::mgmtRecv(void *cookie, void *data, unsigned long len,
|
||||||
mgmtwr.append(resp);
|
mgmtwr.append(resp);
|
||||||
|
|
||||||
auto sz = write(eodMgmtFd, mgmtwr.data(), mgmtwr.size());
|
auto sz = write(eodMgmtFd, mgmtwr.data(), mgmtwr.size());
|
||||||
if (sz > 0) mgmtwr.erase(mgmtwr.begin(), mgmtwr.begin() + sz);
|
if (sz > 0) {
|
||||||
|
mgmtwr.erase(mgmtwr.begin(), mgmtwr.begin() + sz);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return ! mgmtwr.empty();
|
return ! mgmtwr.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ExtOsdep::routeAddDel(bool add, const InetAddress &target, const InetAddress &via, const InetAddress &src, const char *ifname) {
|
void ExtOsdep::routeAddDel(bool add, const InetAddress& target, const InetAddress& via, const InetAddress& src, const char* ifname)
|
||||||
|
{
|
||||||
Mutex::Lock l(eodMutex);
|
Mutex::Lock l(eodMutex);
|
||||||
|
|
||||||
std::string ifn;
|
std::string ifn;
|
||||||
if (ifname) ifn = ifname;
|
if (ifname) {
|
||||||
|
ifn = ifname;
|
||||||
|
}
|
||||||
if (add) {
|
if (add) {
|
||||||
for (auto x = allRoutes.begin(); x != allRoutes.end(); ++x) {
|
for (auto x = allRoutes.begin(); x != allRoutes.end(); ++x) {
|
||||||
if (x->target == target && x->via == via
|
if (x->target == target && x->via == via && x->src == src && x->ifname == ifn) {
|
||||||
&& x->src == src && x->ifname == ifn) return;
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
allRoutes.push_back({ target, via, src, ifn });
|
allRoutes.push_back({ target, via, src, ifn });
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
bool found = false;
|
bool found = false;
|
||||||
for (auto x = allRoutes.begin(); x != allRoutes.end(); ++x) {
|
for (auto x = allRoutes.begin(); x != allRoutes.end(); ++x) {
|
||||||
if (x->target == target && x->via == via
|
if (x->target == target && x->via == via && x->src == src && x->ifname == ifn) {
|
||||||
&& x->src == src && x->ifname == ifn) {
|
|
||||||
allRoutes.erase(x);
|
allRoutes.erase(x);
|
||||||
found = true;
|
found = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!found) return;
|
if (! found) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
zt_eod_msg_route req;
|
zt_eod_msg_route req;
|
||||||
|
@ -182,15 +224,22 @@ void ExtOsdep::routeAddDel(bool add, const InetAddress &target, const InetAddres
|
||||||
req.afi = target.isV4() ? 1 : 2;
|
req.afi = target.isV4() ? 1 : 2;
|
||||||
req.dstlen = target.netmaskBits();
|
req.dstlen = target.netmaskBits();
|
||||||
memcpy(req.dst, target.rawIpData(), target.isV4() ? 4 : 16);
|
memcpy(req.dst, target.rawIpData(), target.isV4() ? 4 : 16);
|
||||||
if (ifname) strncpyx(req.dev, ifname, sizeof(req.dev));
|
if (ifname) {
|
||||||
if (via) memcpy(req.gw, via.rawIpData(), target.isV4() ? 4 : 16);
|
strncpyx(req.dev, ifname, sizeof(req.dev));
|
||||||
if (src) memcpy(req.src, src.rawIpData(), target.isV4() ? 4 : 16);
|
}
|
||||||
|
if (via) {
|
||||||
|
memcpy(req.gw, via.rawIpData(), target.isV4() ? 4 : 16);
|
||||||
|
}
|
||||||
|
if (src) {
|
||||||
|
memcpy(req.src, src.rawIpData(), target.isV4() ? 4 : 16);
|
||||||
|
}
|
||||||
|
|
||||||
unsigned char resp;
|
unsigned char resp;
|
||||||
__eodXchg(req, add ? ZT_EOD_MSG_ADDROUTERESP : ZT_EOD_MSG_DELROUTERESP, resp);
|
__eodXchg(req, add ? ZT_EOD_MSG_ADDROUTERESP : ZT_EOD_MSG_DELROUTERESP, resp);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ExtOsdep::getBindAddrs(std::map<InetAddress,std::string> &ret) {
|
bool ExtOsdep::getBindAddrs(std::map<InetAddress, std::string>& ret)
|
||||||
|
{
|
||||||
Mutex::Lock l(eodMutex);
|
Mutex::Lock l(eodMutex);
|
||||||
|
|
||||||
unsigned char req = ZT_EOD_MSG_GETBINDADDRS;
|
unsigned char req = ZT_EOD_MSG_GETBINDADDRS;
|
||||||
|
@ -199,13 +248,14 @@ bool ExtOsdep::getBindAddrs(std::map<InetAddress,std::string> &ret) {
|
||||||
zt_eod_msg_getbindaddrsresp* resp;
|
zt_eod_msg_getbindaddrsresp* resp;
|
||||||
unsigned char buf[ZT_EOD_MAXMSGSIZE];
|
unsigned char buf[ZT_EOD_MAXMSGSIZE];
|
||||||
int r = __eodWait(ZT_EOD_MSG_GETBINDADDRSRESP, (unsigned char*)buf, sizeof(*resp), sizeof(buf));
|
int r = __eodWait(ZT_EOD_MSG_GETBINDADDRSRESP, (unsigned char*)buf, sizeof(*resp), sizeof(buf));
|
||||||
if (r < (int)sizeof(*resp)) return false;
|
if (r < (int)sizeof(*resp)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
int c = (r - (int)sizeof(*resp)) / sizeof(resp->addrs[0]);
|
int c = (r - (int)sizeof(*resp)) / sizeof(resp->addrs[0]);
|
||||||
resp = (zt_eod_msg_getbindaddrsresp*)buf;
|
resp = (zt_eod_msg_getbindaddrsresp*)buf;
|
||||||
for (int i = 0; i < c; ++i) {
|
for (int i = 0; i < c; ++i) {
|
||||||
ret[InetAddress(resp->addrs[i].data, resp->addrs[i].afi == 1 ? 4 : 16, resp->addrs[i].len)]
|
ret[InetAddress(resp->addrs[i].data, resp->addrs[i].afi == 1 ? 4 : 16, resp->addrs[i].len)] = resp->addrs[i].ifname;
|
||||||
= resp->addrs[i].ifname;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return resp->result;
|
return resp->result;
|
||||||
|
@ -219,16 +269,16 @@ ExtOsdepTap::ExtOsdepTap(
|
||||||
uint64_t nwid,
|
uint64_t nwid,
|
||||||
const char* friendlyName,
|
const char* friendlyName,
|
||||||
void (*handler)(void*, void*, uint64_t, const MAC&, const MAC&, unsigned int, unsigned int, const void*, unsigned int),
|
void (*handler)(void*, void*, uint64_t, const MAC&, const MAC&, unsigned int, unsigned int, const void*, unsigned int),
|
||||||
void *arg) :
|
void* arg)
|
||||||
_handler(handler),
|
: _handler(handler)
|
||||||
_arg(arg),
|
, _arg(arg)
|
||||||
_nwid(nwid),
|
, _nwid(nwid)
|
||||||
_mac(mac),
|
, _mac(mac)
|
||||||
_homePath(homePath),
|
, _homePath(homePath)
|
||||||
_mtu(mtu),
|
, _mtu(mtu)
|
||||||
_fd(0),
|
, _fd(0)
|
||||||
_enabled(true),
|
, _enabled(true)
|
||||||
_run(true)
|
, _run(true)
|
||||||
{
|
{
|
||||||
zt_eod_msg_addtap req;
|
zt_eod_msg_addtap req;
|
||||||
req.cmd = ZT_EOD_MSG_ADDTAP;
|
req.cmd = ZT_EOD_MSG_ADDTAP;
|
||||||
|
@ -243,16 +293,17 @@ ExtOsdepTap::ExtOsdepTap(
|
||||||
Mutex::Lock l(eodMutex);
|
Mutex::Lock l(eodMutex);
|
||||||
__eodSend(req);
|
__eodSend(req);
|
||||||
_fd = -1;
|
_fd = -1;
|
||||||
if (__eodWait(ZT_EOD_MSG_ADDTAPRESP, (unsigned char *)&resp, sizeof(resp), sizeof(resp), &_fd) != sizeof(resp))
|
if (__eodWait(ZT_EOD_MSG_ADDTAPRESP, (unsigned char*)&resp, sizeof(resp), sizeof(resp), &_fd) != sizeof(resp)) {
|
||||||
throw std::runtime_error(std::string("could not create TAP"));
|
throw std::runtime_error(std::string("could not create TAP"));
|
||||||
|
}
|
||||||
|
|
||||||
_dev = resp.name;
|
_dev = resp.name;
|
||||||
if (_dev.empty() || _fd < 0)
|
if (_dev.empty() || _fd < 0) {
|
||||||
throw std::runtime_error(std::string("could not create TAP"));
|
throw std::runtime_error(std::string("could not create TAP"));
|
||||||
|
}
|
||||||
|
|
||||||
fcntl(_fd, F_SETFL, O_NONBLOCK);
|
fcntl(_fd, F_SETFL, O_NONBLOCK);
|
||||||
|
|
||||||
// processing shamelessly copied from LinuxEthernetTap
|
|
||||||
(void)::pipe(_shutdownSignalPipe);
|
(void)::pipe(_shutdownSignalPipe);
|
||||||
for (unsigned int t = 0; t < 2; ++t) {
|
for (unsigned int t = 0; t < 2; ++t) {
|
||||||
_tapReaderThread[t] = std::thread([this, t] {
|
_tapReaderThread[t] = std::thread([this, t] {
|
||||||
|
@ -261,8 +312,9 @@ ExtOsdepTap::ExtOsdepTap(
|
||||||
void* buf = nullptr;
|
void* buf = nullptr;
|
||||||
std::vector<void*> buffers;
|
std::vector<void*> buffers;
|
||||||
|
|
||||||
if (!_run)
|
if (! _run) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
FD_ZERO(&readfds);
|
FD_ZERO(&readfds);
|
||||||
FD_ZERO(&nullfds);
|
FD_ZERO(&nullfds);
|
||||||
|
@ -274,8 +326,9 @@ ExtOsdepTap::ExtOsdepTap(
|
||||||
FD_SET(_fd, &readfds);
|
FD_SET(_fd, &readfds);
|
||||||
select(nfds, &readfds, &nullfds, &nullfds, (struct timeval*)0);
|
select(nfds, &readfds, &nullfds, &nullfds, (struct timeval*)0);
|
||||||
|
|
||||||
if (FD_ISSET(_shutdownSignalPipe[0],&readfds)) // writes to shutdown pipe terminate thread
|
if (FD_ISSET(_shutdownSignalPipe[0], &readfds)) { // writes to shutdown pipe terminate thread
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if (FD_ISSET(_fd, &readfds)) {
|
if (FD_ISSET(_fd, &readfds)) {
|
||||||
for (;;) { // read until there are no more packets, then return to outer select() loop
|
for (;;) { // read until there are no more packets, then return to outer select() loop
|
||||||
|
@ -289,9 +342,11 @@ ExtOsdepTap::ExtOsdepTap(
|
||||||
}
|
}
|
||||||
if (buffers.empty()) {
|
if (buffers.empty()) {
|
||||||
buf = malloc(ZT_TAP_BUF_SIZE);
|
buf = malloc(ZT_TAP_BUF_SIZE);
|
||||||
if (!buf)
|
if (! buf) {
|
||||||
break;
|
break;
|
||||||
} else {
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
buf = buffers.back();
|
buf = buffers.back();
|
||||||
buffers.pop_back();
|
buffers.pop_back();
|
||||||
}
|
}
|
||||||
|
@ -304,8 +359,9 @@ ExtOsdepTap::ExtOsdepTap(
|
||||||
// data until we have at least a frame.
|
// data until we have at least a frame.
|
||||||
r += n;
|
r += n;
|
||||||
if (r > 14) {
|
if (r > 14) {
|
||||||
if (r > ((int)_mtu + 14)) // sanity check for weird TAP behavior on some platforms
|
if (r > ((int)_mtu + 14)) { // sanity check for weird TAP behavior on some platforms
|
||||||
r = _mtu + 14;
|
r = _mtu + 14;
|
||||||
|
}
|
||||||
|
|
||||||
if (_enabled && _tapqsize.load() < 1000) {
|
if (_enabled && _tapqsize.load() < 1000) {
|
||||||
++_tapqsize;
|
++_tapqsize;
|
||||||
|
@ -315,7 +371,8 @@ ExtOsdepTap::ExtOsdepTap(
|
||||||
|
|
||||||
r = 0;
|
r = 0;
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
r = 0;
|
r = 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -338,16 +395,23 @@ ExtOsdepTap::ExtOsdepTap(
|
||||||
_handler(_arg, nullptr, _nwid, from, to, etherType, 0, (const void*)(b + 14), (unsigned int)(qi.second - 14));
|
_handler(_arg, nullptr, _nwid, from, to, etherType, 0, (const void*)(b + 14), (unsigned int)(qi.second - 14));
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> l(_buffers_l);
|
std::lock_guard<std::mutex> l(_buffers_l);
|
||||||
if (_buffers.size() < 128)
|
if (_buffers.size() < 128) {
|
||||||
_buffers.push_back(qi.first);
|
_buffers.push_back(qi.first);
|
||||||
else free(qi.first);
|
|
||||||
}
|
}
|
||||||
} else break;
|
else {
|
||||||
|
free(qi.first);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
ExtOsdepTap::~ExtOsdepTap() {
|
ExtOsdepTap::~ExtOsdepTap()
|
||||||
|
{
|
||||||
_run = false;
|
_run = false;
|
||||||
|
|
||||||
(void)::write(_shutdownSignalPipe[1], "\0", 1); // causes reader thread(s) to exit
|
(void)::write(_shutdownSignalPipe[1], "\0", 1); // causes reader thread(s) to exit
|
||||||
|
@ -361,13 +425,15 @@ ExtOsdepTap::~ExtOsdepTap() {
|
||||||
::close(_shutdownSignalPipe[0]);
|
::close(_shutdownSignalPipe[0]);
|
||||||
::close(_shutdownSignalPipe[1]);
|
::close(_shutdownSignalPipe[1]);
|
||||||
|
|
||||||
for(std::vector<void *>::iterator i(_buffers.begin());i!=_buffers.end();++i)
|
for (std::vector<void*>::iterator i(_buffers.begin()); i != _buffers.end(); ++i) {
|
||||||
free(*i);
|
free(*i);
|
||||||
|
}
|
||||||
std::vector<std::pair<void*, int> > dv(_tapq.drain());
|
std::vector<std::pair<void*, int> > dv(_tapq.drain());
|
||||||
for (std::vector<std::pair<void*, int> >::iterator i(dv.begin()); i != dv.end(); ++i) {
|
for (std::vector<std::pair<void*, int> >::iterator i(dv.begin()); i != dv.end(); ++i) {
|
||||||
if (i->first)
|
if (i->first) {
|
||||||
free(i->first);
|
free(i->first);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
zt_eod_msg_deltap req;
|
zt_eod_msg_deltap req;
|
||||||
req.cmd = ZT_EOD_MSG_DELTAP;
|
req.cmd = ZT_EOD_MSG_DELTAP;
|
||||||
|
@ -377,15 +443,18 @@ ExtOsdepTap::~ExtOsdepTap() {
|
||||||
eodXchg(req, ZT_EOD_MSG_DELTAPRESP, resp);
|
eodXchg(req, ZT_EOD_MSG_DELTAPRESP, resp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ExtOsdepTap::setEnabled(bool en) {
|
void ExtOsdepTap::setEnabled(bool en)
|
||||||
|
{
|
||||||
_enabled = en;
|
_enabled = en;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ExtOsdepTap::enabled() const {
|
bool ExtOsdepTap::enabled() const
|
||||||
|
{
|
||||||
return _enabled;
|
return _enabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ExtOsdepTap::doRemoveIp(const InetAddress &ip) {
|
void ExtOsdepTap::doRemoveIp(const InetAddress& ip)
|
||||||
|
{
|
||||||
zt_eod_msg_ip req;
|
zt_eod_msg_ip req;
|
||||||
req.cmd = ZT_EOD_MSG_DELIP;
|
req.cmd = ZT_EOD_MSG_DELIP;
|
||||||
strcpy(req.name, _dev.c_str());
|
strcpy(req.name, _dev.c_str());
|
||||||
|
@ -397,12 +466,17 @@ void ExtOsdepTap::doRemoveIp(const InetAddress &ip) {
|
||||||
__eodXchg(req, ZT_EOD_MSG_DELIPRESP, resp);
|
__eodXchg(req, ZT_EOD_MSG_DELIPRESP, resp);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ExtOsdepTap::addIp(const InetAddress &ip) {
|
bool ExtOsdepTap::addIp(const InetAddress& ip)
|
||||||
|
{
|
||||||
Mutex::Lock l(eodMutex);
|
Mutex::Lock l(eodMutex);
|
||||||
|
|
||||||
for (auto i = allIps.begin(); i != allIps.end(); ++i) {
|
for (auto i = allIps.begin(); i != allIps.end(); ++i) {
|
||||||
if (*i == ip) return true;
|
if (*i == ip) {
|
||||||
if (i->ipsEqual(ip)) doRemoveIp(*i);
|
return true;
|
||||||
|
}
|
||||||
|
if (i->ipsEqual(ip)) {
|
||||||
|
doRemoveIp(*i);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
zt_eod_msg_ip req;
|
zt_eod_msg_ip req;
|
||||||
|
@ -419,10 +493,13 @@ bool ExtOsdepTap::addIp(const InetAddress &ip) {
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
bool ExtOsdepTap::addIps(std::vector<InetAddress> ips) {
|
bool ExtOsdepTap::addIps(std::vector<InetAddress> ips)
|
||||||
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
bool ExtOsdepTap::removeIp(const InetAddress &ip) {
|
|
||||||
|
bool ExtOsdepTap::removeIp(const InetAddress& ip)
|
||||||
|
{
|
||||||
Mutex::Lock l(eodMutex);
|
Mutex::Lock l(eodMutex);
|
||||||
for (auto i = allIps.begin(); i != allIps.end(); ++i) {
|
for (auto i = allIps.begin(); i != allIps.end(); ++i) {
|
||||||
if (*i == ip) {
|
if (*i == ip) {
|
||||||
|
@ -432,7 +509,8 @@ bool ExtOsdepTap::removeIp(const InetAddress &ip) {
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
std::vector<InetAddress> ExtOsdepTap::ips() const {
|
std::vector<InetAddress> ExtOsdepTap::ips() const
|
||||||
|
{
|
||||||
std::vector<InetAddress> ret;
|
std::vector<InetAddress> ret;
|
||||||
|
|
||||||
Mutex::Lock l(eodMutex);
|
Mutex::Lock l(eodMutex);
|
||||||
|
@ -445,7 +523,9 @@ std::vector<InetAddress> ExtOsdepTap::ips() const {
|
||||||
zt_eod_msg_getipsresp* resp;
|
zt_eod_msg_getipsresp* resp;
|
||||||
unsigned char buf[ZT_EOD_MAXMSGSIZE];
|
unsigned char buf[ZT_EOD_MAXMSGSIZE];
|
||||||
int r = __eodWait(ZT_EOD_MSG_GETIPSRESP, (unsigned char*)buf, sizeof(*resp), sizeof(buf));
|
int r = __eodWait(ZT_EOD_MSG_GETIPSRESP, (unsigned char*)buf, sizeof(*resp), sizeof(buf));
|
||||||
if (r < (int)sizeof(*resp)) return ret;
|
if (r < (int)sizeof(*resp)) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
int c = (r - (int)sizeof(*resp)) / sizeof(resp->addrs[0]);
|
int c = (r - (int)sizeof(*resp)) / sizeof(resp->addrs[0]);
|
||||||
resp = (zt_eod_msg_getipsresp*)buf;
|
resp = (zt_eod_msg_getipsresp*)buf;
|
||||||
|
@ -455,7 +535,8 @@ std::vector<InetAddress> ExtOsdepTap::ips() const {
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
void ExtOsdepTap::put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len) {
|
void ExtOsdepTap::put(const MAC& from, const MAC& to, unsigned int etherType, const void* data, unsigned int len)
|
||||||
|
{
|
||||||
char putBuf[ZT_MAX_MTU + 64];
|
char putBuf[ZT_MAX_MTU + 64];
|
||||||
if ((_fd > 0) && (len <= _mtu) && (_enabled)) {
|
if ((_fd > 0) && (len <= _mtu) && (_enabled)) {
|
||||||
to.copyTo(putBuf, 6);
|
to.copyTo(putBuf, 6);
|
||||||
|
@ -466,12 +547,16 @@ void ExtOsdepTap::put(const MAC &from,const MAC &to,unsigned int etherType,const
|
||||||
(void)::write(_fd, putBuf, len);
|
(void)::write(_fd, putBuf, len);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::string ExtOsdepTap::deviceName() const {
|
std::string ExtOsdepTap::deviceName() const
|
||||||
|
{
|
||||||
return _dev;
|
return _dev;
|
||||||
}
|
}
|
||||||
void ExtOsdepTap::setFriendlyName(const char *friendlyName) {}
|
void ExtOsdepTap::setFriendlyName(const char* friendlyName)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
void ExtOsdepTap::scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed) {
|
void ExtOsdepTap::scanMulticastGroups(std::vector<MulticastGroup>& added, std::vector<MulticastGroup>& removed)
|
||||||
|
{
|
||||||
char *ptr, *ptr2;
|
char *ptr, *ptr2;
|
||||||
unsigned char mac[6];
|
unsigned char mac[6];
|
||||||
std::vector<MulticastGroup> newGroups;
|
std::vector<MulticastGroup> newGroups;
|
||||||
|
@ -487,39 +572,48 @@ void ExtOsdepTap::scanMulticastGroups(std::vector<MulticastGroup> &added,std::ve
|
||||||
char* devname = (char*)0;
|
char* devname = (char*)0;
|
||||||
char* mcastmac = (char*)0;
|
char* mcastmac = (char*)0;
|
||||||
for (char* f = strtok_r(l, " \t", &ptr2); (f); f = strtok_r((char*)0, " \t", &ptr2)) {
|
for (char* f = strtok_r(l, " \t", &ptr2); (f); f = strtok_r((char*)0, " \t", &ptr2)) {
|
||||||
if (fno == 1)
|
if (fno == 1) {
|
||||||
devname = f;
|
devname = f;
|
||||||
else if (fno == 4)
|
}
|
||||||
|
else if (fno == 4) {
|
||||||
mcastmac = f;
|
mcastmac = f;
|
||||||
|
}
|
||||||
++fno;
|
++fno;
|
||||||
}
|
}
|
||||||
if ((devname)&&(!strcmp(devname,_dev.c_str()))&&(mcastmac)&&(Utils::unhex(mcastmac,mac,6) == 6))
|
if ((devname) && (! strcmp(devname, _dev.c_str())) && (mcastmac) && (Utils::unhex(mcastmac, mac, 6) == 6)) {
|
||||||
newGroups.push_back(MulticastGroup(MAC(mac, 6), 0));
|
newGroups.push_back(MulticastGroup(MAC(mac, 6), 0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
::close(fd);
|
::close(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<InetAddress> allIps(ips());
|
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));
|
newGroups.push_back(MulticastGroup::deriveMulticastGroupForAddressResolution(*ip));
|
||||||
|
}
|
||||||
|
|
||||||
std::sort(newGroups.begin(), newGroups.end());
|
std::sort(newGroups.begin(), newGroups.end());
|
||||||
newGroups.erase(std::unique(newGroups.begin(), newGroups.end()), newGroups.end());
|
newGroups.erase(std::unique(newGroups.begin(), newGroups.end()), newGroups.end());
|
||||||
|
|
||||||
for (std::vector<MulticastGroup>::iterator m(newGroups.begin()); m != newGroups.end(); ++m) {
|
for (std::vector<MulticastGroup>::iterator m(newGroups.begin()); m != newGroups.end(); ++m) {
|
||||||
if (!std::binary_search(_multicastGroups.begin(),_multicastGroups.end(),*m))
|
if (! std::binary_search(_multicastGroups.begin(), _multicastGroups.end(), *m)) {
|
||||||
added.push_back(*m);
|
added.push_back(*m);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
for (std::vector<MulticastGroup>::iterator m(_multicastGroups.begin()); m != _multicastGroups.end(); ++m) {
|
for (std::vector<MulticastGroup>::iterator m(_multicastGroups.begin()); m != _multicastGroups.end(); ++m) {
|
||||||
if (!std::binary_search(newGroups.begin(),newGroups.end(),*m))
|
if (! std::binary_search(newGroups.begin(), newGroups.end(), *m)) {
|
||||||
removed.push_back(*m);
|
removed.push_back(*m);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_multicastGroups.swap(newGroups);
|
_multicastGroups.swap(newGroups);
|
||||||
}
|
}
|
||||||
void ExtOsdepTap::setMtu(unsigned int mtu) {
|
void ExtOsdepTap::setMtu(unsigned int mtu)
|
||||||
if (mtu == _mtu) return;
|
{
|
||||||
|
if (mtu == _mtu) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
_mtu = mtu;
|
_mtu = mtu;
|
||||||
|
|
||||||
zt_eod_msg_setmtu req;
|
zt_eod_msg_setmtu req;
|
||||||
|
|
|
@ -1,3 +1,16 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c)2019 ZeroTier, Inc.
|
||||||
|
*
|
||||||
|
* Use of this software is governed by the Business Source License included
|
||||||
|
* in the LICENSE.TXT file in the project's root directory.
|
||||||
|
*
|
||||||
|
* Change Date: 2026-01-01
|
||||||
|
*
|
||||||
|
* On the date above, in accordance with the Business Source License, use
|
||||||
|
* of this software will be governed by version 2.0 of the Apache License.
|
||||||
|
*/
|
||||||
|
/****/
|
||||||
|
|
||||||
#ifndef ZT_EXTOSDEP_HPP
|
#ifndef ZT_EXTOSDEP_HPP
|
||||||
#define ZT_EXTOSDEP_HPP
|
#define ZT_EXTOSDEP_HPP
|
||||||
|
|
||||||
|
@ -110,19 +123,20 @@ struct zt_eod_mgmt_reply {
|
||||||
|
|
||||||
#ifndef ZT_EXTOSDEP_IFACEONLY
|
#ifndef ZT_EXTOSDEP_IFACEONLY
|
||||||
|
|
||||||
|
#include "../node/AtomicCounter.hpp"
|
||||||
|
#include "../node/Hashtable.hpp"
|
||||||
#include "../node/InetAddress.hpp"
|
#include "../node/InetAddress.hpp"
|
||||||
#include "../node/MAC.hpp"
|
#include "../node/MAC.hpp"
|
||||||
#include "Thread.hpp"
|
|
||||||
#include "../node/Hashtable.hpp"
|
|
||||||
#include "../node/Mutex.hpp"
|
#include "../node/Mutex.hpp"
|
||||||
#include "../node/AtomicCounter.hpp"
|
|
||||||
#include "EthernetTap.hpp"
|
|
||||||
#include "BlockingQueue.hpp"
|
#include "BlockingQueue.hpp"
|
||||||
|
#include "EthernetTap.hpp"
|
||||||
|
#include "Thread.hpp"
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <thread>
|
|
||||||
#include <mutex>
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
namespace ZeroTier {
|
namespace ZeroTier {
|
||||||
|
|
||||||
|
@ -134,13 +148,11 @@ public:
|
||||||
static void routeAddDel(bool, const InetAddress& target, const InetAddress& via, const InetAddress& src, const char* ifaceName);
|
static void routeAddDel(bool, const InetAddress& target, const InetAddress& via, const InetAddress& src, const char* ifaceName);
|
||||||
static bool getBindAddrs(std::map<InetAddress, std::string>&);
|
static bool getBindAddrs(std::map<InetAddress, std::string>&);
|
||||||
|
|
||||||
static bool mgmtRecv(void *cookie, void *data, unsigned long len,
|
static bool mgmtRecv(void* cookie, void* data, unsigned long len, std::function<unsigned(unsigned, const std::string&, const std::string&, std::string&)>);
|
||||||
std::function<unsigned (unsigned, const std::string &, const std::string &, std::string &)>);
|
|
||||||
static bool mgmtWritable(void*);
|
static bool mgmtWritable(void*);
|
||||||
};
|
};
|
||||||
|
|
||||||
class ExtOsdepTap : public EthernetTap
|
class ExtOsdepTap : public EthernetTap {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
ExtOsdepTap(
|
ExtOsdepTap(
|
||||||
const char* homePath,
|
const char* homePath,
|
||||||
|
@ -165,7 +177,10 @@ public:
|
||||||
virtual void setFriendlyName(const char* friendlyName);
|
virtual void setFriendlyName(const char* friendlyName);
|
||||||
virtual void scanMulticastGroups(std::vector<MulticastGroup>& added, std::vector<MulticastGroup>& removed);
|
virtual void scanMulticastGroups(std::vector<MulticastGroup>& added, std::vector<MulticastGroup>& removed);
|
||||||
virtual void setMtu(unsigned int mtu);
|
virtual void setMtu(unsigned int mtu);
|
||||||
virtual void setDns(const char *domain, const std::vector<InetAddress> &servers) {}
|
virtual void setDns(const char* domain, const std::vector<InetAddress>& servers)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void (*_handler)(void*, void*, uint64_t, const MAC&, const MAC&, unsigned int, unsigned int, const void*, unsigned int);
|
void (*_handler)(void*, void*, uint64_t, const MAC&, const MAC&, unsigned int, unsigned int, const void*, unsigned int);
|
||||||
void* _arg;
|
void* _arg;
|
||||||
|
|
Loading…
Add table
Reference in a new issue