From 0a195e7bc0e23a5a7978a75befe8843d6e69aecd Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Wed, 22 Oct 2014 09:52:29 -0700 Subject: Some work on simulated net... --- node/Condition.hpp | 4 +- testnet/SimNet.cpp | 89 +++++++++++++++++++++++++++++++++++++++++ testnet/SimNet.hpp | 78 ++++++++++++++++++++++++++++++++++++ testnet/SimNetSocketManager.cpp | 61 +++++++++++++++++++++++----- testnet/SimNetSocketManager.hpp | 87 ++++++++++++++++++++++++++++++++-------- 5 files changed, 291 insertions(+), 28 deletions(-) create mode 100644 testnet/SimNet.cpp create mode 100644 testnet/SimNet.hpp diff --git a/node/Condition.hpp b/node/Condition.hpp index 3895f4df..74af06a2 100644 --- a/node/Condition.hpp +++ b/node/Condition.hpp @@ -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 diff --git a/testnet/SimNet.cpp b/testnet/SimNet.cpp new file mode 100644 index 00000000..a644a9bc --- /dev/null +++ b/testnet/SimNet.cpp @@ -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 . + * + * -- + * + * 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 SimNet::all() +{ + std::vector 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 diff --git a/testnet/SimNet.hpp b/testnet/SimNet.hpp new file mode 100644 index 00000000..6f7c5c11 --- /dev/null +++ b/testnet/SimNet.hpp @@ -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 . + * + * -- + * + * 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 +#include + +#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 all(); + +private: + std::map< InetAddress,SimNetSocketManager > _endpoints; + CMWC4096 _prng; + Mutex _lock; +}; + +} // namespace ZeroTier + +#endif diff --git a/testnet/SimNetSocketManager.cpp b/testnet/SimNetSocketManager.cpp index b1cf40ee..f6f24184 100644 --- a/testnet/SimNetSocketManager.cpp +++ b/testnet/SimNetSocketManager.cpp @@ -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 &,void *,const InetAddress &,Buffer &), - void *arg) +void SimNetSocketManager::poll(unsigned long timeout,void (*handler)(const SharedPtr &,void *,const InetAddress &,Buffer &),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() diff --git a/testnet/SimNetSocketManager.hpp b/testnet/SimNetSocketManager.hpp index 22fb4753..031cbd0c 100644 --- a/testnet/SimNetSocketManager.hpp +++ b/testnet/SimNetSocketManager.hpp @@ -28,43 +28,98 @@ #ifndef ZT_SIMNETSOCKETMANAGER_HPP #define ZT_SIMNETSOCKETMANAGER_HPP -#include -#include - #include -#include +#include +#include #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 &,void *,const InetAddress &,Buffer &), - void *arg); + /** + * @return Local endpoint stats + */ + inline const TransferStats &totals() const { return _totals; } - virtual void whack(); + /** + * @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 >(from,Buffer(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 &,void *,const InetAddress &,Buffer &),void *arg); + virtual void whack(); virtual void closeTcpSockets(); private: + // These are set by SimNet after object creation + SimNet *_sn; + InetAddress _address; + + SharedPtr _mySocket; + TransferStats _totals; + + std::queue< std::pair< InetAddress,Buffer > > _inbox; + Mutex _inbox_m; + + std::map< InetAddress,TransferStats > _stats; + Mutex _stats_m; + + Condition _waitCond; }; } // namespace ZeroTier -- cgit v1.2.3