Style match

This commit is contained in:
Joseph Henry 2025-02-26 11:16:03 -08:00
parent cd191778c2
commit 269c8d415a
No known key found for this signature in database
GPG key ID: 3C2C8A1EB4269827
2 changed files with 437 additions and 328 deletions

View file

@ -1,11 +1,26 @@
#include <sys/times.h>
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
/*
* 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.
*/
/****/
#include "ExtOsdep.hpp"
#include <list>
#include "../node/AtomicCounter.hpp"
#include <fcntl.h>
#include <iostream>
#include <list>
#include <sys/times.h>
#include <unistd.h>
#define ZT_TAP_BUF_SIZE 16384
namespace ZeroTier {
@ -22,18 +37,24 @@ struct EodRoute {
};
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));
}
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);
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,
unsigned maxl = 0, int *recvfd = nullptr) {
if (!maxl) maxl = l;
static int __eodWait(unsigned char msg, unsigned char* d, unsigned l, unsigned maxl = 0, int* recvfd = nullptr)
{
if (! maxl) {
maxl = l;
}
auto start = times(NULL);
while (1) {
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);
if (r > 0) {
if (recvfd && mh.msg_controllen >= sizeof(cmsg)
&& cmsg.cmsg_len == sizeof(cmsg)
&& cmsg.cmsg_level == SOL_SOCKET
&& cmsg.cmsg_type == SCM_RIGHTS) {
if (recvfd && mh.msg_controllen >= sizeof(cmsg) && cmsg.cmsg_len == sizeof(cmsg) && cmsg.cmsg_level == SOL_SOCKET && cmsg.cmsg_type == SCM_RIGHTS) {
*recvfd = cmsg.fd;
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);
}
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);
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);
return __eodXchg(m, rm, r);
}
void ExtOsdep::init(int fd1, int fd2) {
void ExtOsdep::init(int fd1, int fd2)
{
eodFd = fd1;
eodMgmtFd = fd2;
fcntl(eodMgmtFd, F_SETFL, O_NONBLOCK);
}
void ExtOsdep::started(int *f, void **cp) {
void ExtOsdep::started(int* f, void** cp)
{
*f = eodMgmtFd;
*cp = (void*)eodMgmtFd;
@ -114,30 +137,42 @@ void ExtOsdep::started(int *f, void **cp) {
static std::string mgmtrd;
static std::string mgmtwr;
bool ExtOsdep::mgmtWritable(void *cookie) {
if (cookie != (void *)eodMgmtFd) return false;
if (mgmtwr.size() == 0) return true;
bool ExtOsdep::mgmtWritable(void* cookie)
{
if (cookie != (void*)eodMgmtFd) {
return false;
}
if (mgmtwr.size() == 0) {
return true;
}
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);
return mgmtwr.empty();
}
bool ExtOsdep::mgmtRecv(void *cookie, void *data, unsigned long len,
std::function<unsigned (unsigned, const std::string &, const std::string &, std::string &)> cb) {
if (cookie != (void *)eodMgmtFd) return false;
bool ExtOsdep::mgmtRecv(void* cookie, void* data, unsigned long len, std::function<unsigned(unsigned, const std::string&, const std::string&, std::string&)> cb)
{
if (cookie != (void*)eodMgmtFd) {
return false;
}
mgmtrd.append((char*)data, len);
while (1) {
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;
if (mgmtrd.size() < reqsz) break;
if (mgmtrd.size() < reqsz) {
break;
}
std::string resp;
char* p = (char*)req->data;
zt_eod_mgmt_reply rep;
rep.scode = cb(req->method, std::string(p, p + req->pathlen),
std::string(p + req->pathlen, p + req->pathlen + req->datalen), resp);
rep.scode = cb(req->method, std::string(p, p + req->pathlen), std::string(p + req->pathlen, p + req->pathlen + req->datalen), resp);
rep.datalen = resp.size();
mgmtrd.erase(mgmtrd.begin(), mgmtrd.begin() + reqsz);
@ -146,34 +181,41 @@ bool ExtOsdep::mgmtRecv(void *cookie, void *data, unsigned long len,
mgmtwr.append(resp);
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();
}
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);
std::string ifn;
if (ifname) ifn = ifname;
if (ifname) {
ifn = ifname;
}
if (add) {
for (auto x = allRoutes.begin(); x != allRoutes.end(); ++x) {
if (x->target == target && x->via == via
&& x->src == src && x->ifname == ifn) return;
if (x->target == target && x->via == via && x->src == src && x->ifname == ifn) {
return;
}
}
allRoutes.push_back({ target, via, src, ifn });
}
else {
bool found = false;
for (auto x = allRoutes.begin(); x != allRoutes.end(); ++x) {
if (x->target == target && x->via == via
&& x->src == src && x->ifname == ifn) {
if (x->target == target && x->via == via && x->src == src && x->ifname == ifn) {
allRoutes.erase(x);
found = true;
break;
}
}
if (!found) return;
if (! found) {
return;
}
}
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.dstlen = target.netmaskBits();
memcpy(req.dst, target.rawIpData(), target.isV4() ? 4 : 16);
if (ifname) strncpyx(req.dev, ifname, sizeof(req.dev));
if (via) memcpy(req.gw, via.rawIpData(), target.isV4() ? 4 : 16);
if (src) memcpy(req.src, src.rawIpData(), target.isV4() ? 4 : 16);
if (ifname) {
strncpyx(req.dev, ifname, sizeof(req.dev));
}
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;
__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);
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;
unsigned char buf[ZT_EOD_MAXMSGSIZE];
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]);
resp = (zt_eod_msg_getbindaddrsresp*)buf;
for (int i = 0; i < c; ++i) {
ret[InetAddress(resp->addrs[i].data, resp->addrs[i].afi == 1 ? 4 : 16, resp->addrs[i].len)]
= resp->addrs[i].ifname;
ret[InetAddress(resp->addrs[i].data, resp->addrs[i].afi == 1 ? 4 : 16, resp->addrs[i].len)] = resp->addrs[i].ifname;
}
return resp->result;
@ -219,16 +269,16 @@ ExtOsdepTap::ExtOsdepTap(
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) :
_handler(handler),
_arg(arg),
_nwid(nwid),
_mac(mac),
_homePath(homePath),
_mtu(mtu),
_fd(0),
_enabled(true),
_run(true)
void* arg)
: _handler(handler)
, _arg(arg)
, _nwid(nwid)
, _mac(mac)
, _homePath(homePath)
, _mtu(mtu)
, _fd(0)
, _enabled(true)
, _run(true)
{
zt_eod_msg_addtap req;
req.cmd = ZT_EOD_MSG_ADDTAP;
@ -243,16 +293,17 @@ ExtOsdepTap::ExtOsdepTap(
Mutex::Lock l(eodMutex);
__eodSend(req);
_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"));
}
_dev = resp.name;
if (_dev.empty() || _fd < 0)
if (_dev.empty() || _fd < 0) {
throw std::runtime_error(std::string("could not create TAP"));
}
fcntl(_fd, F_SETFL, O_NONBLOCK);
// processing shamelessly copied from LinuxEthernetTap
(void)::pipe(_shutdownSignalPipe);
for (unsigned int t = 0; t < 2; ++t) {
_tapReaderThread[t] = std::thread([this, t] {
@ -261,8 +312,9 @@ ExtOsdepTap::ExtOsdepTap(
void* buf = nullptr;
std::vector<void*> buffers;
if (!_run)
if (! _run) {
return;
}
FD_ZERO(&readfds);
FD_ZERO(&nullfds);
@ -274,8 +326,9 @@ ExtOsdepTap::ExtOsdepTap(
FD_SET(_fd, &readfds);
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;
}
if (FD_ISSET(_fd, &readfds)) {
for (;;) { // read until there are no more packets, then return to outer select() loop
@ -289,9 +342,11 @@ ExtOsdepTap::ExtOsdepTap(
}
if (buffers.empty()) {
buf = malloc(ZT_TAP_BUF_SIZE);
if (!buf)
if (! buf) {
break;
} else {
}
}
else {
buf = buffers.back();
buffers.pop_back();
}
@ -304,8 +359,9 @@ ExtOsdepTap::ExtOsdepTap(
// data until we have at least a frame.
r += n;
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;
}
if (_enabled && _tapqsize.load() < 1000) {
++_tapqsize;
@ -315,7 +371,8 @@ ExtOsdepTap::ExtOsdepTap(
r = 0;
}
} else {
}
else {
r = 0;
break;
}
@ -338,16 +395,23 @@ ExtOsdepTap::ExtOsdepTap(
_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);
if (_buffers.size() < 128)
if (_buffers.size() < 128) {
_buffers.push_back(qi.first);
else free(qi.first);
}
} else break;
else {
free(qi.first);
}
}
}
else {
break;
}
}
});
}
ExtOsdepTap::~ExtOsdepTap() {
ExtOsdepTap::~ExtOsdepTap()
{
_run = false;
(void)::write(_shutdownSignalPipe[1], "\0", 1); // causes reader thread(s) to exit
@ -361,13 +425,15 @@ ExtOsdepTap::~ExtOsdepTap() {
::close(_shutdownSignalPipe[0]);
::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);
}
std::vector<std::pair<void*, int> > dv(_tapq.drain());
for (std::vector<std::pair<void*, int> >::iterator i(dv.begin()); i != dv.end(); ++i) {
if (i->first)
if (i->first) {
free(i->first);
}
}
zt_eod_msg_deltap req;
req.cmd = ZT_EOD_MSG_DELTAP;
@ -377,15 +443,18 @@ ExtOsdepTap::~ExtOsdepTap() {
eodXchg(req, ZT_EOD_MSG_DELTAPRESP, resp);
}
void ExtOsdepTap::setEnabled(bool en) {
void ExtOsdepTap::setEnabled(bool en)
{
_enabled = en;
}
bool ExtOsdepTap::enabled() const {
bool ExtOsdepTap::enabled() const
{
return _enabled;
}
void ExtOsdepTap::doRemoveIp(const InetAddress &ip) {
void ExtOsdepTap::doRemoveIp(const InetAddress& ip)
{
zt_eod_msg_ip req;
req.cmd = ZT_EOD_MSG_DELIP;
strcpy(req.name, _dev.c_str());
@ -397,12 +466,17 @@ void ExtOsdepTap::doRemoveIp(const InetAddress &ip) {
__eodXchg(req, ZT_EOD_MSG_DELIPRESP, resp);
}
bool ExtOsdepTap::addIp(const InetAddress &ip) {
bool ExtOsdepTap::addIp(const InetAddress& ip)
{
Mutex::Lock l(eodMutex);
for (auto i = allIps.begin(); i != allIps.end(); ++i) {
if (*i == ip) return true;
if (i->ipsEqual(ip)) doRemoveIp(*i);
if (*i == ip) {
return true;
}
if (i->ipsEqual(ip)) {
doRemoveIp(*i);
}
}
zt_eod_msg_ip req;
@ -419,10 +493,13 @@ bool ExtOsdepTap::addIp(const InetAddress &ip) {
return true;
}
bool ExtOsdepTap::addIps(std::vector<InetAddress> ips) {
bool ExtOsdepTap::addIps(std::vector<InetAddress> ips)
{
return false;
}
bool ExtOsdepTap::removeIp(const InetAddress &ip) {
bool ExtOsdepTap::removeIp(const InetAddress& ip)
{
Mutex::Lock l(eodMutex);
for (auto i = allIps.begin(); i != allIps.end(); ++i) {
if (*i == ip) {
@ -432,7 +509,8 @@ bool ExtOsdepTap::removeIp(const InetAddress &ip) {
}
return false;
}
std::vector<InetAddress> ExtOsdepTap::ips() const {
std::vector<InetAddress> ExtOsdepTap::ips() const
{
std::vector<InetAddress> ret;
Mutex::Lock l(eodMutex);
@ -445,7 +523,9 @@ std::vector<InetAddress> ExtOsdepTap::ips() const {
zt_eod_msg_getipsresp* resp;
unsigned char buf[ZT_EOD_MAXMSGSIZE];
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]);
resp = (zt_eod_msg_getipsresp*)buf;
@ -455,7 +535,8 @@ std::vector<InetAddress> ExtOsdepTap::ips() const {
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];
if ((_fd > 0) && (len <= _mtu) && (_enabled)) {
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);
}
}
std::string ExtOsdepTap::deviceName() const {
std::string ExtOsdepTap::deviceName() const
{
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;
unsigned char mac[6];
std::vector<MulticastGroup> newGroups;
@ -487,39 +572,48 @@ void ExtOsdepTap::scanMulticastGroups(std::vector<MulticastGroup> &added,std::ve
char* devname = (char*)0;
char* mcastmac = (char*)0;
for (char* f = strtok_r(l, " \t", &ptr2); (f); f = strtok_r((char*)0, " \t", &ptr2)) {
if (fno == 1)
if (fno == 1) {
devname = f;
else if (fno == 4)
}
else if (fno == 4) {
mcastmac = f;
}
++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));
}
}
}
::close(fd);
}
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());
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);
}
}
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);
}
}
_multicastGroups.swap(newGroups);
}
void ExtOsdepTap::setMtu(unsigned int mtu) {
if (mtu == _mtu) return;
void ExtOsdepTap::setMtu(unsigned int mtu)
{
if (mtu == _mtu) {
return;
}
_mtu = mtu;
zt_eod_msg_setmtu req;

View file

@ -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
#define ZT_EXTOSDEP_HPP
@ -110,19 +123,20 @@ struct zt_eod_mgmt_reply {
#ifndef ZT_EXTOSDEP_IFACEONLY
#include "../node/AtomicCounter.hpp"
#include "../node/Hashtable.hpp"
#include "../node/InetAddress.hpp"
#include "../node/MAC.hpp"
#include "Thread.hpp"
#include "../node/Hashtable.hpp"
#include "../node/Mutex.hpp"
#include "../node/AtomicCounter.hpp"
#include "EthernetTap.hpp"
#include "BlockingQueue.hpp"
#include "EthernetTap.hpp"
#include "Thread.hpp"
#include <atomic>
#include <thread>
#include <mutex>
#include <functional>
#include <mutex>
#include <string>
#include <thread>
namespace ZeroTier {
@ -134,13 +148,11 @@ public:
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 mgmtRecv(void *cookie, void *data, unsigned long len,
std::function<unsigned (unsigned, const std::string &, const std::string &, std::string &)>);
static bool mgmtRecv(void* cookie, void* data, unsigned long len, std::function<unsigned(unsigned, const std::string&, const std::string&, std::string&)>);
static bool mgmtWritable(void*);
};
class ExtOsdepTap : public EthernetTap
{
class ExtOsdepTap : public EthernetTap {
public:
ExtOsdepTap(
const char* homePath,
@ -165,7 +177,10 @@ public:
virtual void setFriendlyName(const char* friendlyName);
virtual void scanMulticastGroups(std::vector<MulticastGroup>& added, std::vector<MulticastGroup>& removed);
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:
void (*_handler)(void*, void*, uint64_t, const MAC&, const MAC&, unsigned int, unsigned int, const void*, unsigned int);
void* _arg;