mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-04-08 11:44:13 +00:00
Work on in-place testnet support.
This commit is contained in:
parent
1109046782
commit
96fa3f7550
@ -416,4 +416,17 @@
|
||||
*/
|
||||
#define ZT_IPC_TIMEOUT 600
|
||||
|
||||
/**
|
||||
* A test pseudo-network-ID that can be joined
|
||||
*
|
||||
* Joining this network ID will result in a network with no IP addressing
|
||||
* and default parameters. No network configuration master will be consulted
|
||||
* and instead a static config will be used. This is used in built-in testnet
|
||||
* scenarios and can also be used for external testing.
|
||||
*
|
||||
* This is an impossible real network ID since 0xff is a reserved address
|
||||
* prefix.
|
||||
*/
|
||||
#define ZT_TEST_NETWORK_ID 0xffffffffffffffffULL
|
||||
|
||||
#endif
|
||||
|
141
testnet/Condition.hpp
Normal file
141
testnet/Condition.hpp
Normal file
@ -0,0 +1,141 @@
|
||||
/*
|
||||
* ZeroTier One - Global Peer to Peer Ethernet
|
||||
* Copyright (C) 2012-2014 ZeroTier Networks LLC
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
|
||||
#ifndef ZT_CONDITION_HPP
|
||||
#define ZT_CONDITION_HPP
|
||||
|
||||
#include "../node/Constants.hpp"
|
||||
#include "../node/NonCopyable.hpp"
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
|
||||
#include <Windows.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "../node/Utils.hpp"
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
class Condition : NonCopyable
|
||||
{
|
||||
public:
|
||||
Condition()
|
||||
throw()
|
||||
{
|
||||
_sem = CreateSemaphore(NULL,0,1,NULL);
|
||||
}
|
||||
|
||||
~Condition()
|
||||
{
|
||||
CloseHandle(_sem);
|
||||
}
|
||||
|
||||
inline void wait() const
|
||||
throw()
|
||||
{
|
||||
WaitForSingleObject(_sem,INFINITE);
|
||||
}
|
||||
|
||||
inline void wait(unsigned long ms) const
|
||||
throw()
|
||||
{
|
||||
WaitForSingleObject(_sem,(DWORD)ms);
|
||||
}
|
||||
|
||||
inline void signal() const
|
||||
throw()
|
||||
{
|
||||
ReleaseSemaphore(_sem,1,NULL);
|
||||
}
|
||||
|
||||
private:
|
||||
HANDLE _sem;
|
||||
};
|
||||
|
||||
} // namespace ZeroTier
|
||||
|
||||
#else // !__WINDOWS__
|
||||
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
#include "../node/Utils.hpp"
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
class Condition : NonCopyable
|
||||
{
|
||||
public:
|
||||
Condition()
|
||||
throw()
|
||||
{
|
||||
pthread_mutex_init(&_mh,(const pthread_mutexattr_t *)0);
|
||||
pthread_cond_init(&_cond,(const pthread_condattr_t *)0);
|
||||
}
|
||||
|
||||
~Condition()
|
||||
{
|
||||
pthread_cond_destroy(&_cond);
|
||||
pthread_mutex_destroy(&_mh);
|
||||
}
|
||||
|
||||
inline void wait() const
|
||||
throw()
|
||||
{
|
||||
pthread_mutex_lock(const_cast <pthread_mutex_t *>(&_mh));
|
||||
pthread_cond_wait(const_cast <pthread_cond_t *>(&_cond),const_cast <pthread_mutex_t *>(&_mh));
|
||||
pthread_mutex_unlock(const_cast <pthread_mutex_t *>(&_mh));
|
||||
}
|
||||
|
||||
inline void wait(unsigned long ms) const
|
||||
throw()
|
||||
{
|
||||
uint64_t when = Utils::now() + (uint64_t)ms;
|
||||
struct timespec ts;
|
||||
ts.tv_sec = (unsigned long)(when / 1000);
|
||||
ts.tv_nsec = (unsigned long)(when % 1000) * 1000000;
|
||||
pthread_mutex_lock(const_cast <pthread_mutex_t *>(&_mh));
|
||||
pthread_cond_timedwait(const_cast <pthread_cond_t *>(&_cond),const_cast <pthread_mutex_t *>(&_mh),&ts);
|
||||
pthread_mutex_unlock(const_cast <pthread_mutex_t *>(&_mh));
|
||||
}
|
||||
|
||||
inline void signal() const
|
||||
throw()
|
||||
{
|
||||
pthread_cond_signal(const_cast <pthread_cond_t *>(&_cond));
|
||||
}
|
||||
|
||||
private:
|
||||
pthread_cond_t _cond;
|
||||
pthread_mutex_t _mh;
|
||||
};
|
||||
|
||||
} // namespace ZeroTier
|
||||
|
||||
#endif // !__WINDOWS__
|
||||
|
||||
#endif
|
135
testnet/TestEthernetTap.cpp
Normal file
135
testnet/TestEthernetTap.cpp
Normal file
@ -0,0 +1,135 @@
|
||||
/*
|
||||
* ZeroTier One - Global Peer to Peer Ethernet
|
||||
* Copyright (C) 2011-2014 ZeroTier Networks LLC
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
|
||||
#include "TestEthernetTap.hpp"
|
||||
#include "TestEthernetTapFactory.hpp"
|
||||
#include "../node/Utils.hpp"
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
TestEthernetTap::TestEthernetTap(
|
||||
TestEthernetTapFactory *parent,
|
||||
const MAC &mac,
|
||||
unsigned int mtu,
|
||||
unsigned int metric,
|
||||
uint64_t nwid,
|
||||
const char *desiredDevice,
|
||||
const char *friendlyName,
|
||||
void (*handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &),
|
||||
void *arg) :
|
||||
EthernetTap("TestEthernetTap",mac,mtu,metric),
|
||||
_parent(parent),
|
||||
_handler(handler),
|
||||
_arg(arg),
|
||||
_enabled(true)
|
||||
{
|
||||
char tmp[64];
|
||||
Utils::snprintf(tmp,sizeof(tmp),"%.16llx",(unsigned long long)nwid);
|
||||
_dev = tmp;
|
||||
_thread = Thread::start(this);
|
||||
}
|
||||
|
||||
TestEthernetTap::~TestEthernetTap()
|
||||
{
|
||||
{
|
||||
Mutex::Lock _l(_pq_m);
|
||||
_pq.push(TestFrame()); // 0-length frame = exit
|
||||
}
|
||||
_pq_c.signal();
|
||||
Thread::join(_thread);
|
||||
}
|
||||
|
||||
void TestEthernetTap::setEnabled(bool en)
|
||||
{
|
||||
_enabled = en;
|
||||
}
|
||||
|
||||
bool TestEthernetTap::enabled() const
|
||||
{
|
||||
return _enabled;
|
||||
}
|
||||
|
||||
bool TestEthernetTap::addIP(const InetAddress &ip)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TestEthernetTap::removeIP(const InetAddress &ip)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
std::set<InetAddress> TestEthernetTap::ips() const
|
||||
{
|
||||
return std::set<InetAddress>();
|
||||
}
|
||||
|
||||
void TestEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
|
||||
{
|
||||
static Mutex printLock;
|
||||
Mutex::Lock _l(printLock);
|
||||
fprintf(stderr,"%s << %s %.4x %s"ZT_EOL_S,to.toString().c_str(),from.toString().c_str(),etherType,std::string((const char *)data,len).c_str());
|
||||
}
|
||||
|
||||
std::string TestEthernetTap::deviceName() const
|
||||
{
|
||||
return _dev;
|
||||
}
|
||||
|
||||
void TestEthernetTap::setFriendlyName(const char *friendlyName)
|
||||
{
|
||||
}
|
||||
|
||||
bool TestEthernetTap::updateMulticastGroups(std::set<MulticastGroup> &groups)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void TestEthernetTap::threadMain()
|
||||
throw()
|
||||
{
|
||||
TestFrame tf;
|
||||
for(;;) {
|
||||
tf.len = 0;
|
||||
{
|
||||
Mutex::Lock _l(_pq_m);
|
||||
if (!_pq.empty()) {
|
||||
if (_pq.front().len == 0)
|
||||
break;
|
||||
memcpy(&tf,&(_pq.front()),sizeof(tf));
|
||||
_pq.pop();
|
||||
}
|
||||
}
|
||||
|
||||
if ((tf.len > 0)&&(_enabled))
|
||||
_handler(_arg,tf.from,tf.to,ZT_TEST_ETHERNET_ETHERTYPE,Buffer<4096>(tf.data,tf.len));
|
||||
|
||||
_pq_c.wait();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ZeroTier
|
130
testnet/TestEthernetTap.hpp
Normal file
130
testnet/TestEthernetTap.hpp
Normal file
@ -0,0 +1,130 @@
|
||||
/*
|
||||
* ZeroTier One - Global Peer to Peer Ethernet
|
||||
* Copyright (C) 2011-2014 ZeroTier Networks LLC
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
|
||||
#ifndef ZT_TESTETHERNETTAP_HPP
|
||||
#define ZT_TESTETHERNETTAP_HPP
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <stdexcept>
|
||||
#include <queue>
|
||||
#include <string>
|
||||
|
||||
#include "../node/AtomicCounter.hpp"
|
||||
#include "../node/SharedPtr.hpp"
|
||||
#include "../node/EthernetTap.hpp"
|
||||
#include "../node/Thread.hpp"
|
||||
#include "../node/Mutex.hpp"
|
||||
#include "Condition.hpp"
|
||||
|
||||
// Ethernet frame type to use on fake testnet
|
||||
#define ZT_TEST_ETHERNET_ETHERTYPE 0xdead
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
class TestEthernetTapFactory;
|
||||
|
||||
class TestEthernetTap : public EthernetTap
|
||||
{
|
||||
friend class SharedPtr<TestEthernetTap>;
|
||||
|
||||
private:
|
||||
struct TestFrame
|
||||
{
|
||||
TestFrame() : len(0) {}
|
||||
TestFrame(const MAC &f,const MAC &t,const void *d,unsigned int l) :
|
||||
from(f),
|
||||
to(t),
|
||||
len(l)
|
||||
{
|
||||
memcpy(data,d,l);
|
||||
}
|
||||
MAC from;
|
||||
MAC to;
|
||||
unsigned int len;
|
||||
char data[4096];
|
||||
};
|
||||
|
||||
public:
|
||||
TestEthernetTap(
|
||||
TestEthernetTapFactory *parent,
|
||||
const MAC &mac,
|
||||
unsigned int mtu,
|
||||
unsigned int metric,
|
||||
uint64_t nwid,
|
||||
const char *desiredDevice,
|
||||
const char *friendlyName,
|
||||
void (*handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &),
|
||||
void *arg);
|
||||
|
||||
virtual ~TestEthernetTap();
|
||||
|
||||
virtual void setEnabled(bool en);
|
||||
virtual bool enabled() const;
|
||||
virtual bool addIP(const InetAddress &ip);
|
||||
virtual bool removeIP(const InetAddress &ip);
|
||||
virtual std::set<InetAddress> ips() const;
|
||||
virtual void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
|
||||
virtual std::string deviceName() const;
|
||||
virtual void setFriendlyName(const char *friendlyName);
|
||||
virtual bool updateMulticastGroups(std::set<MulticastGroup> &groups);
|
||||
|
||||
void threadMain()
|
||||
throw();
|
||||
|
||||
inline void sendFromHost(const MAC &from,const MAC &to,const void *data,unsigned int len)
|
||||
{
|
||||
if (!len)
|
||||
return;
|
||||
{
|
||||
Mutex::Lock _l(_pq_m);
|
||||
_pq.push(TestFrame(from,to,data,len));
|
||||
}
|
||||
_pq_c.signal();
|
||||
}
|
||||
|
||||
private:
|
||||
TestEthernetTapFactory *_parent;
|
||||
|
||||
void (*_handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &);
|
||||
void *_arg;
|
||||
Thread _thread;
|
||||
std::string _dev;
|
||||
volatile bool _enabled;
|
||||
|
||||
std::queue< TestFrame > _pq;
|
||||
Mutex _pq_m;
|
||||
Condition _pq_c;
|
||||
|
||||
AtomicCounter __refCount;
|
||||
};
|
||||
|
||||
} // namespace ZeroTier
|
||||
|
||||
#endif
|
86
testnet/TestEthernetTapFactory.cpp
Normal file
86
testnet/TestEthernetTapFactory.cpp
Normal file
@ -0,0 +1,86 @@
|
||||
/*
|
||||
* ZeroTier One - Global Peer to Peer Ethernet
|
||||
* Copyright (C) 2011-2014 ZeroTier Networks LLC
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
|
||||
#include "TestEthernetTapFactory.hpp"
|
||||
#include "TestEthernetTap.hpp"
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
TestEthernetTapFactory::TestEthernetTapFactory()
|
||||
{
|
||||
}
|
||||
|
||||
TestEthernetTapFactory::~TestEthernetTapFactory()
|
||||
{
|
||||
}
|
||||
|
||||
EthernetTap *TestEthernetTapFactory::open(
|
||||
const MAC &mac,
|
||||
unsigned int mtu,
|
||||
unsigned int metric,
|
||||
uint64_t nwid,
|
||||
const char *desiredDevice,
|
||||
const char *friendlyName,
|
||||
void (*handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &),
|
||||
void *arg)
|
||||
{
|
||||
SharedPtr<TestEthernetTap> tap(new TestEthernetTap(this,mac,mtu,metric,nwid,desiredDevice,friendlyName,handler,arg));
|
||||
{
|
||||
Mutex::Lock _l(_taps_m);
|
||||
_taps.insert(tap);
|
||||
}
|
||||
{
|
||||
Mutex::Lock _l(_tapsByDevice_m);
|
||||
_tapsByDevice[tap->deviceName()] = tap;
|
||||
}
|
||||
{
|
||||
Mutex::Lock _l(_tapsByMac_m);
|
||||
_tapsByMac[mac] = tap;
|
||||
}
|
||||
return tap.ptr();
|
||||
}
|
||||
|
||||
void TestEthernetTapFactory::close(EthernetTap *tap,bool destroyPersistentDevices)
|
||||
{
|
||||
if (!tap)
|
||||
return;
|
||||
SharedPtr<TestEthernetTap> tapp((TestEthernetTap *)tap);
|
||||
{
|
||||
Mutex::Lock _l(_taps_m);
|
||||
_taps.erase(tapp);
|
||||
}
|
||||
{
|
||||
Mutex::Lock _l(_tapsByDevice_m);
|
||||
_tapsByDevice.erase(tapp->deviceName());
|
||||
}
|
||||
{
|
||||
Mutex::Lock _l(_tapsByMac_m);
|
||||
_tapsByMac.erase(tapp->mac());
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ZeroTier
|
120
testnet/TestEthernetTapFactory.hpp
Normal file
120
testnet/TestEthernetTapFactory.hpp
Normal file
@ -0,0 +1,120 @@
|
||||
/*
|
||||
* ZeroTier One - Global Peer to Peer Ethernet
|
||||
* Copyright (C) 2011-2014 ZeroTier Networks LLC
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
* --
|
||||
*
|
||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||
*
|
||||
* If you would like to embed ZeroTier into a commercial application or
|
||||
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
|
||||
#ifndef ZT_TESTETHERNETTAPFACTORY_HPP
|
||||
#define ZT_TESTETHERNETTAPFACTORY_HPP
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <set>
|
||||
|
||||
#include "../node/SharedPtr.hpp"
|
||||
#include "../node/EthernetTapFactory.hpp"
|
||||
#include "../node/Mutex.hpp"
|
||||
#include "../node/MAC.hpp"
|
||||
#include "../node/CMWC4096.hpp"
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
class TestEthernetTap;
|
||||
|
||||
class TestEthernetTapFactory : public EthernetTapFactory
|
||||
{
|
||||
public:
|
||||
TestEthernetTapFactory();
|
||||
virtual ~TestEthernetTapFactory();
|
||||
|
||||
virtual EthernetTap *open(
|
||||
const MAC &mac,
|
||||
unsigned int mtu,
|
||||
unsigned int metric,
|
||||
uint64_t nwid,
|
||||
const char *desiredDevice,
|
||||
const char *friendlyName,
|
||||
void (*handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &),
|
||||
void *arg);
|
||||
|
||||
virtual void close(EthernetTap *tap,bool destroyPersistentDevices);
|
||||
|
||||
inline SharedPtr<TestEthernetTap> getByMac(const MAC &mac) const
|
||||
{
|
||||
Mutex::Lock _l(_tapsByMac_m);
|
||||
std::map< MAC,SharedPtr<TestEthernetTap> >::const_iterator t(_tapsByMac.find(mac));
|
||||
if (t == _tapsByMac.end())
|
||||
return SharedPtr<TestEthernetTap>();
|
||||
return t->second;
|
||||
}
|
||||
|
||||
inline SharedPtr<TestEthernetTap> getByDevice(const std::string &dev) const
|
||||
{
|
||||
Mutex::Lock _l(_tapsByDevice_m);
|
||||
std::map< std::string,SharedPtr<TestEthernetTap> >::const_iterator t(_tapsByDevice.find(dev));
|
||||
if (t == _tapsByDevice.end())
|
||||
return SharedPtr<TestEthernetTap>();
|
||||
return t->second;
|
||||
}
|
||||
|
||||
inline SharedPtr<TestEthernetTap> getFirst() const
|
||||
{
|
||||
Mutex::Lock _l(_taps_m);
|
||||
if (_taps.empty())
|
||||
return SharedPtr<TestEthernetTap>();
|
||||
return *(_taps.begin());
|
||||
}
|
||||
|
||||
inline SharedPtr<TestEthernetTap> getRandom() const
|
||||
{
|
||||
Mutex::Lock _l(_taps_m);
|
||||
Mutex::Lock _l2(_prng_m);
|
||||
if (_taps.empty())
|
||||
return SharedPtr<TestEthernetTap>();
|
||||
unsigned int x = (const_cast<CMWC4096 *>(&_prng))->next32() % (unsigned int)_taps.size();
|
||||
unsigned int i = 0;
|
||||
for(std::set< SharedPtr<TestEthernetTap> >::const_iterator t(_taps.begin());t!=_taps.end();++t) {
|
||||
if (i++ == x)
|
||||
return *t;
|
||||
}
|
||||
return SharedPtr<TestEthernetTap>(); // never reached
|
||||
}
|
||||
|
||||
private:
|
||||
std::set< SharedPtr<TestEthernetTap> > _taps;
|
||||
Mutex _taps_m;
|
||||
|
||||
std::map<std::string,SharedPtr<TestEthernetTap> > _tapsByDevice;
|
||||
Mutex _tapsByDevice_m;
|
||||
|
||||
std::map<MAC,SharedPtr<TestEthernetTap> > _tapsByMac;
|
||||
Mutex _tapsByMac_m;
|
||||
|
||||
CMWC4096 _prng;
|
||||
Mutex _prng_m;
|
||||
};
|
||||
|
||||
} // namespace ZeroTier
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user