Some work on simulated net...

This commit is contained in:
Adam Ierymenko 2014-10-22 09:52:29 -07:00
parent ae228ade8c
commit 0a195e7bc0
5 changed files with 291 additions and 28 deletions

@ -63,7 +63,9 @@ public:
inline void wait(unsigned long ms) const
throw()
{
WaitForSingleObject(_sem,(DWORD)ms);
if (ms)
WaitForSingleObject(_sem,(DWORD)ms);
else WaitForSingleObject(_sem,INFINITE);
}
inline void signal() const

89
testnet/SimNet.cpp Normal file

@ -0,0 +1,89 @@
/*
* 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 "SimNet.hpp"
#include "../node/Constants.hpp"
#include "../node/Utils.hpp"
namespace ZeroTier {
SimNet::SimNet()
{
}
SimNet::~SimNet()
{
}
SimNetSocketManager *newEndpoint()
{
Mutex::Lock _l(_lock);
if (_endpoints.size() >= ZT_SIMNET_MAX_TESTNET_SIZE)
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 *get(const InetAddress &addr)
{
Mutex::Lock _l(_lock);
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;
}
} // namespace ZeroTier

78
testnet/SimNet.hpp Normal file

@ -0,0 +1,78 @@
/*
* 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_SIMNET_HPP
#define ZT_SIMNET_HPP
#include <map>
#include <vector>
#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 {
/**
* A simulated headless IP network for testing
*/
class SimNet
{
public:
SimNet();
~SimNet();
/**
* @return New endpoint with random IP address
*/
SimNetSocketManager *newEndpoint();
/**
* @param addr Address to look up
* @return Endpoint or NULL if none
*/
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;
Mutex _lock;
};
} // namespace ZeroTier
#endif

@ -27,9 +27,32 @@
#include "SimNetSocketManager.hpp"
#include "../node/Constants.hpp"
#include "../node/Socket.hpp"
namespace ZeroTier {
SimNetSocketManager::SimNetSocketManager()
class SimNetSocket : public Socket
{
public:
SimNetSocket(SimNetSocketManager *sm) :
Socket(ZT_SOCKET_TYPE_UDP_V4),
_parent(sm) {}
virtual bool send(const InetAddress &to,const void *msg,unsigned int msglen)
{
SimNetSocketManager *dest = _parent->net()->get(to);
if (dest)
dest->enqueue(_parent->address(),msg,msglen);
return true; // we emulate UDP, which has no delivery guarantee semantics
}
SimNetSocketManager *_parent;
};
SimNetSocketManager::SimNetSocketManager() :
_sn((SimNet *)0), // initialized by SimNet
_mySocket(new SimNetSocket(this))
{
}
@ -37,24 +60,40 @@ SimNetSocketManager::~SimNetSocketManager()
{
}
bool SimNetSocketManager::send(
const InetAddress &to,
bool tcp,
bool autoConnectTcp,
const void *msg,
unsigned int msglen)
bool SimNetSocketManager::send(const InetAddress &to,bool tcp,bool autoConnectTcp,const void *msg,unsigned int msglen)
{
if (tcp)
return false; // we emulate UDP
SimNetSocketManager *dest = _sn->get(to);
if (dest)
dest->enqueue(_address,msg,msglen);
return true; // we emulate UDP, which has no delivery guarantee semantics
}
void SimNetSocketManager::poll(
unsigned long timeout,
void (*handler)(const SharedPtr<Socket> &,void *,const InetAddress &,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> &),
void *arg)
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();
}
}
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();
}
}
}
void SimNetSocketManager::whack()
{
_waitCond.signal();
}
void SimNetSocketManager::closeTcpSockets()

@ -28,43 +28,98 @@
#ifndef ZT_SIMNETSOCKETMANAGER_HPP
#define ZT_SIMNETSOCKETMANAGER_HPP
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <stdexcept>
#include <utility>
#include <queue>
#include "Constants.hpp"
#include "../node/SocketManager.hpp"
#include "../node/Mutex.hpp"
#include "../node/Condition.hpp"
namespace ZeroTier {
class SimNet;
/**
* Socket I/O implementation
* Socket manager for an IP endpoint in a simulated network
*/
class SimNetSocketManager : public SocketManager
{
friend class SimNet;
public:
struct TransferStats
{
TransferStats() : received(0),sent(0) {}
unsigned long long received;
unsigned long long sent;
};
SimNetSocketManager();
virtual ~SimNetSocketManager();
virtual bool send(
const InetAddress &to,
bool tcp,
bool autoConnectTcp,
const void *msg,
unsigned int msglen);
/**
* @return IP address of this simulated endpoint
*/
inline const InetAddress &address() const { return _address; }
virtual void poll(
unsigned long timeout,
void (*handler)(const SharedPtr<Socket> &,void *,const InetAddress &,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> &),
void *arg);
/**
* @return Local endpoint stats
*/
inline const TransferStats &totals() const { return _totals; }
/**
* @param peer Peer IP address
* @return Transfer stats for this peer
*/
inline TransferStats stats(const InetAddress &peer) const
{
Mutex::Lock _l(_stats_m);
return _stats[peer];
}
/**
* @return Network to which this endpoint belongs
*/
inline SimNet *net() const { return _sn; }
/**
* Enqueue data from another endpoint to be picked up on next poll()
*
* @param from Originating endpoint address
* @param data Data
* @param len Length of data in bytes
*/
inline void enqueue(const InetAddress &from,const void *data,unsigned int len)
{
{
Mutex::Lock _l(_inbox_m);
_inbox.push(std::pair< InetAddress,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> >(from,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN>(data,len)));
}
_waitCond.signal();
}
virtual bool send(const InetAddress &to,bool tcp,bool autoConnectTcp,const void *msg,unsigned int msglen);
virtual void poll(unsigned long timeout,void (*handler)(const SharedPtr<Socket> &,void *,const InetAddress &,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> &),void *arg);
virtual void whack();
virtual void closeTcpSockets();
private:
// These are set by SimNet after object creation
SimNet *_sn;
InetAddress _address;
SharedPtr<Socket> _mySocket;
TransferStats _totals;
std::queue< std::pair< InetAddress,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> > > _inbox;
Mutex _inbox_m;
std::map< InetAddress,TransferStats > _stats;
Mutex _stats_m;
Condition _waitCond;
};
} // namespace ZeroTier