summaryrefslogtreecommitdiff
path: root/testnet
diff options
context:
space:
mode:
Diffstat (limited to 'testnet')
-rw-r--r--testnet/SimNet.cpp43
-rw-r--r--testnet/SimNet.hpp15
-rw-r--r--testnet/SimNetSocketManager.cpp17
-rw-r--r--testnet/SimNetSocketManager.hpp7
-rw-r--r--testnet/TestEthernetTapFactory.cpp8
-rw-r--r--testnet/TestEthernetTapFactory.hpp35
6 files changed, 29 insertions, 96 deletions
diff --git a/testnet/SimNet.cpp b/testnet/SimNet.cpp
index a644a9bc..3349d5c2 100644
--- a/testnet/SimNet.cpp
+++ b/testnet/SimNet.cpp
@@ -40,50 +40,29 @@ SimNet::~SimNet()
{
}
-SimNetSocketManager *newEndpoint()
+SimNetSocketManager *SimNet::newEndpoint(const InetAddress &addr)
{
Mutex::Lock _l(_lock);
if (_endpoints.size() >= ZT_SIMNET_MAX_TESTNET_SIZE)
return (SimNetSocketManager *)0;
+ if (_endpoints.find(addr) != _endpoints.end())
+ return (SimNetSocketManager *)0;
- InetAddress fake;
- uint32_t ip = _prng.next32();
- for(;;) {
- ++ip;
- ip &= 0x00ffffff;
- ip |= 0x0a000000; // 10.x.x.x
- if (((ip >> 16) & 0xff) == 0xff) ip ^= 0x00010000;
- if (((ip >> 8) & 0xff) == 0xff) ip ^= 0x00000100;
- if ((ip & 0xff) == 0xff) --ip;
- if ((ip & 0xff) == 0x00) ++ip;
- uint32_t ipn = Utils::hton(ip);
- fake.set(&ipn,4,8); // 10.x.x.x/8
- if (_endpoints.find(fake) == _endpoints.end()) {
- SimNetSocketManager *sm = &(_endpoints[fake]);
- sm->_sn = this;
- sm->_address = fake;
- return sm;
- }
- }
+ SimNetSocketManager *sm = new SimNetSocketManager();
+ sm->_sn = this;
+ sm->_address = addr;
+ _endpoints[addr] = sm;
+ return sm;
}
-SimNetSocketManager *get(const InetAddress &addr)
+SimNetSocketManager *SimNet::get(const InetAddress &addr)
{
Mutex::Lock _l(_lock);
- std::map< InetAddress,SimNetSocketManager >::iterator ep(_endpoints.find(addr));
+ std::map< InetAddress,SimNetSocketManager * >::iterator ep(_endpoints.find(addr));
if (ep == _endpoints.end())
return (SimNetSocketManager *)0;
- return &(ep->second);
-}
-
-std::vector<SimNetSocketManager *> SimNet::all()
-{
- std::vector<SimNetSocketManager *> a;
- Mutex::Lock _l(_lock);
- for (std::map< InetAddress,SimNetSocketManager >::iterator ep(_endpoints.begin());ep!=_endpoints.end();++ep)
- a.push_back(&(ep->second));
- return a;
+ return ep->second;
}
} // namespace ZeroTier
diff --git a/testnet/SimNet.hpp b/testnet/SimNet.hpp
index 6f7c5c11..4fdd6939 100644
--- a/testnet/SimNet.hpp
+++ b/testnet/SimNet.hpp
@@ -34,13 +34,12 @@
#include "../node/Constants.hpp"
#include "../node/InetAddress.hpp"
#include "../node/Mutex.hpp"
-#include "../node/CMWC4096.hpp"
#include "SimNetSocketManager.hpp"
#define ZT_SIMNET_MAX_TESTNET_SIZE 1048576
-namespcae ZeroTier {
+namespace ZeroTier {
/**
* A simulated headless IP network for testing
@@ -52,9 +51,9 @@ public:
~SimNet();
/**
- * @return New endpoint with random IP address
+ * @return New endpoint or NULL on failure
*/
- SimNetSocketManager *newEndpoint();
+ SimNetSocketManager *newEndpoint(const InetAddress &addr);
/**
* @param addr Address to look up
@@ -62,14 +61,8 @@ public:
*/
SimNetSocketManager *get(const InetAddress &addr);
- /**
- * @return All socket managers (pointers remain safe while SimNet is running-- these aren't cleaned)
- */
- std::vector<SimNetSocketManager *> all();
-
private:
- std::map< InetAddress,SimNetSocketManager > _endpoints;
- CMWC4096 _prng;
+ std::map< InetAddress,SimNetSocketManager * > _endpoints;
Mutex _lock;
};
diff --git a/testnet/SimNetSocketManager.cpp b/testnet/SimNetSocketManager.cpp
index f6f24184..c75c864f 100644
--- a/testnet/SimNetSocketManager.cpp
+++ b/testnet/SimNetSocketManager.cpp
@@ -26,6 +26,7 @@
*/
#include "SimNetSocketManager.hpp"
+#include "SimNet.hpp"
#include "../node/Constants.hpp"
#include "../node/Socket.hpp"
@@ -73,20 +74,20 @@ bool SimNetSocketManager::send(const InetAddress &to,bool tcp,bool autoConnectTc
void SimNetSocketManager::poll(unsigned long timeout,void (*handler)(const SharedPtr<Socket> &,void *,const InetAddress &,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> &),void *arg)
{
{
- Mutex::Lock _l(_lock);
- while (!_queue.empty()) {
- handler(_mySocket,arg,_queue.front().first,_queue.front().second);
- _queue.pop();
+ Mutex::Lock _l(_inbox_m);
+ while (!_inbox.empty()) {
+ handler(_mySocket,arg,_inbox.front().first,_inbox.front().second);
+ _inbox.pop();
}
}
if (timeout)
_waitCond.wait(timeout);
else _waitCond.wait();
{
- Mutex::Lock _l(_lock);
- while (!_queue.empty()) {
- handler(_mySocket,arg,_queue.front().first,_queue.front().second);
- _queue.pop();
+ Mutex::Lock _l(_inbox_m);
+ while (!_inbox.empty()) {
+ handler(_mySocket,arg,_inbox.front().first,_inbox.front().second);
+ _inbox.pop();
}
}
}
diff --git a/testnet/SimNetSocketManager.hpp b/testnet/SimNetSocketManager.hpp
index 031cbd0c..69f49556 100644
--- a/testnet/SimNetSocketManager.hpp
+++ b/testnet/SimNetSocketManager.hpp
@@ -32,7 +32,7 @@
#include <utility>
#include <queue>
-#include "Constants.hpp"
+#include "../node/Constants.hpp"
#include "../node/SocketManager.hpp"
#include "../node/Mutex.hpp"
#include "../node/Condition.hpp"
@@ -76,7 +76,10 @@ public:
inline TransferStats stats(const InetAddress &peer) const
{
Mutex::Lock _l(_stats_m);
- return _stats[peer];
+ std::map< InetAddress,TransferStats >::const_iterator s(_stats.find(peer));
+ if (s == _stats.end())
+ return TransferStats();
+ return s->second;
}
/**
diff --git a/testnet/TestEthernetTapFactory.cpp b/testnet/TestEthernetTapFactory.cpp
index c4a8a988..836f586e 100644
--- a/testnet/TestEthernetTapFactory.cpp
+++ b/testnet/TestEthernetTapFactory.cpp
@@ -54,10 +54,6 @@ EthernetTap *TestEthernetTapFactory::open(
_taps.insert(tap);
}
{
- Mutex::Lock _l(_tapsByDevice_m);
- _tapsByDevice[tap->deviceName()] = tap;
- }
- {
Mutex::Lock _l(_tapsByMac_m);
_tapsByMac[mac] = tap;
}
@@ -74,10 +70,6 @@ void TestEthernetTapFactory::close(EthernetTap *tap,bool destroyPersistentDevice
_taps.erase(tapp);
}
{
- Mutex::Lock _l(_tapsByDevice_m);
- _tapsByDevice.erase(tapp->deviceName());
- }
- {
Mutex::Lock _l(_tapsByMac_m);
_tapsByMac.erase(tapp->mac());
}
diff --git a/testnet/TestEthernetTapFactory.hpp b/testnet/TestEthernetTapFactory.hpp
index 5327b844..57d195d2 100644
--- a/testnet/TestEthernetTapFactory.hpp
+++ b/testnet/TestEthernetTapFactory.hpp
@@ -68,45 +68,10 @@ public:
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;