diff options
author | Adam Ierymenko <adam.ierymenko@gmail.com> | 2014-10-03 11:59:50 -0700 |
---|---|---|
committer | Adam Ierymenko <adam.ierymenko@gmail.com> | 2014-10-03 11:59:50 -0700 |
commit | 67aa23530b7e051709002b521aa6e93cb6350918 (patch) | |
tree | 35d9c8dbd1125279596949479b6e915908c3b358 /testnet | |
parent | 2a58c3fb98c3ff06cf7f5255a13ba4d128f3ce49 (diff) | |
download | infinitytier-67aa23530b7e051709002b521aa6e93cb6350918.tar.gz infinitytier-67aa23530b7e051709002b521aa6e93cb6350918.zip |
More work on adding testnet and user-mode capabilities for local network simulation.
Diffstat (limited to 'testnet')
-rw-r--r-- | testnet/Condition.hpp | 141 | ||||
-rw-r--r-- | testnet/TestEthernetTap.cpp | 53 | ||||
-rw-r--r-- | testnet/TestEthernetTap.hpp | 26 | ||||
-rw-r--r-- | testnet/TestEthernetTapFactory.hpp | 3 | ||||
-rw-r--r-- | testnet/TestRoutingTable.cpp | 50 | ||||
-rw-r--r-- | testnet/TestRoutingTable.hpp | 50 | ||||
-rw-r--r-- | testnet/local-testnet/sn0000/identity.public (renamed from testnet/nodes/sn0000/identity.public) | 0 | ||||
-rw-r--r-- | testnet/local-testnet/sn0000/identity.secret (renamed from testnet/nodes/sn0000/identity.secret) | 0 | ||||
-rw-r--r-- | testnet/local-testnet/sn0001/identity.public (renamed from testnet/nodes/sn0001/identity.public) | 0 | ||||
-rw-r--r-- | testnet/local-testnet/sn0001/identity.secret (renamed from testnet/nodes/sn0001/identity.secret) | 0 | ||||
-rw-r--r-- | testnet/local-testnet/sn0002/identity.public (renamed from testnet/nodes/sn0002/identity.public) | 0 | ||||
-rw-r--r-- | testnet/local-testnet/sn0002/identity.secret (renamed from testnet/nodes/sn0002/identity.secret) | 0 | ||||
-rw-r--r-- | testnet/local-testnet/sn0003/identity.public (renamed from testnet/nodes/sn0003/identity.public) | 0 | ||||
-rw-r--r-- | testnet/local-testnet/sn0003/identity.secret (renamed from testnet/nodes/sn0003/identity.secret) | 0 |
14 files changed, 157 insertions, 166 deletions
diff --git a/testnet/Condition.hpp b/testnet/Condition.hpp deleted file mode 100644 index 89d3a531..00000000 --- a/testnet/Condition.hpp +++ /dev/null @@ -1,141 +0,0 @@ -/* - * 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 diff --git a/testnet/TestEthernetTap.cpp b/testnet/TestEthernetTap.cpp index 421d6650..f3273d00 100644 --- a/testnet/TestEthernetTap.cpp +++ b/testnet/TestEthernetTap.cpp @@ -27,10 +27,23 @@ #include "TestEthernetTap.hpp" #include "TestEthernetTapFactory.hpp" + +#include "../node/Constants.hpp" #include "../node/Utils.hpp" +#include <stdio.h> +#include <stdlib.h> + +#ifdef __WINDOWS__ +#include <process.h> +#else +#include <unistd.h> +#endif + namespace ZeroTier { +static Mutex printLock; + TestEthernetTap::TestEthernetTap( TestEthernetTapFactory *parent, const MAC &mac, @@ -47,9 +60,19 @@ TestEthernetTap::TestEthernetTap( _arg(arg), _enabled(true) { + static volatile unsigned int testTapCounter = 0; + char tmp[64]; - Utils::snprintf(tmp,sizeof(tmp),"%.16llx",(unsigned long long)nwid); + int pid = 0; +#ifdef __UNIX_LIKE__ + pid = (int)getpid(); +#endif +#ifdef __WINDOWS__ + pid = (int)_getpid(); +#endif + Utils::snprintf(tmp,sizeof(tmp),"test%dtap%d",pid,testTapCounter++); _dev = tmp; + _thread = Thread::start(this); } @@ -57,7 +80,7 @@ TestEthernetTap::~TestEthernetTap() { { Mutex::Lock _l(_pq_m); - _pq.push(TestFrame()); // 0-length frame = exit + _pq.push(TestFrame()); // 0 length frame = exit } _pq_c.signal(); Thread::join(_thread); @@ -90,9 +113,9 @@ std::set<InetAddress> TestEthernetTap::ips() const 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()); + fprintf(stdout,"[%s] %s << %s %.4x %s"ZT_EOL_S,_dev.c_str(),to.toString().c_str(),from.toString().c_str(),etherType,std::string((const char *)data,len).c_str()); + fflush(stdout); } std::string TestEthernetTap::deviceName() const @@ -109,6 +132,26 @@ bool TestEthernetTap::updateMulticastGroups(std::set<MulticastGroup> &groups) return false; } +bool TestEthernetTap::injectPacketFromHost(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len) +{ + if ((len == 0)||(len > 2800)) + return false; + + { + Mutex::Lock _l(_pq_m); + _pq.push(TestFrame(from,to,data,etherType & 0xffff,len)); + } + _pq_c.signal(); + + { + Mutex::Lock _l(printLock); + fprintf(stdout,"[%s] %s >> %s %.4x %s"ZT_EOL_S,_dev.c_str(),from.toString().c_str(),to.toString().c_str(),etherType,std::string((const char *)data,len).c_str()); + fflush(stdout); + } + + return true; +} + void TestEthernetTap::threadMain() throw() { @@ -126,7 +169,7 @@ void TestEthernetTap::threadMain() } if ((tf.len > 0)&&(_enabled)) - _handler(_arg,tf.from,tf.to,ZT_TEST_ETHERNET_ETHERTYPE,Buffer<4096>(tf.data,tf.len)); + _handler(_arg,tf.from,tf.to,tf.etherType,Buffer<4096>(tf.data,tf.len)); _pq_c.wait(); } diff --git a/testnet/TestEthernetTap.hpp b/testnet/TestEthernetTap.hpp index c1e9c720..93eb4c5b 100644 --- a/testnet/TestEthernetTap.hpp +++ b/testnet/TestEthernetTap.hpp @@ -36,15 +36,13 @@ #include <queue> #include <string> +#include "../node/Constants.hpp" +#include "../node/EthernetTap.hpp" #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 +#include "../node/Condition.hpp" namespace ZeroTier { @@ -57,16 +55,18 @@ class TestEthernetTap : public EthernetTap private: struct TestFrame { - TestFrame() : len(0) {} - TestFrame(const MAC &f,const MAC &t,const void *d,unsigned int l) : + TestFrame() : etherType(0),len(0) {} + TestFrame(const MAC &f,const MAC &t,const void *d,unsigned int et,unsigned int l) : from(f), to(t), + etherType(et), len(l) { memcpy(data,d,l); } MAC from; MAC to; + unsigned int etherType; unsigned int len; char data[4096]; }; @@ -94,21 +94,11 @@ public: virtual std::string deviceName() const; virtual void setFriendlyName(const char *friendlyName); virtual bool updateMulticastGroups(std::set<MulticastGroup> &groups); + virtual bool injectPacketFromHost(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len); 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; diff --git a/testnet/TestEthernetTapFactory.hpp b/testnet/TestEthernetTapFactory.hpp index caceec06..5327b844 100644 --- a/testnet/TestEthernetTapFactory.hpp +++ b/testnet/TestEthernetTapFactory.hpp @@ -37,11 +37,10 @@ #include "../node/Mutex.hpp" #include "../node/MAC.hpp" #include "../node/CMWC4096.hpp" +#include "TestEthernetTap.hpp" namespace ZeroTier { -class TestEthernetTap; - class TestEthernetTapFactory : public EthernetTapFactory { public: diff --git a/testnet/TestRoutingTable.cpp b/testnet/TestRoutingTable.cpp new file mode 100644 index 00000000..8c132693 --- /dev/null +++ b/testnet/TestRoutingTable.cpp @@ -0,0 +1,50 @@ +/* + * 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 "TestRoutingTable.hpp" + +namespace ZeroTier { + +TestRoutingTable::TestRoutingTable() +{ +} + +TestRoutingTable::~TestRoutingTable() +{ +} + +std::vector<RoutingTable::Entry> TestRoutingTable::get(bool includeLinkLocal,bool includeLoopback) const +{ + return std::vector<RoutingTable::Entry>(); +} + +RoutingTable::Entry TestRoutingTable::set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric) +{ + return RoutingTable::Entry(); +} + +} // namespace ZeroTier diff --git a/testnet/TestRoutingTable.hpp b/testnet/TestRoutingTable.hpp new file mode 100644 index 00000000..98390201 --- /dev/null +++ b/testnet/TestRoutingTable.hpp @@ -0,0 +1,50 @@ +/* + * 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_TESTROUTINGTABLE_HPP +#define ZT_TESTROUTINGTABLE_HPP + +#include "../node/RoutingTable.hpp" + +namespace ZeroTier { + +/** + * Dummy routing table -- right now this just does nothing + */ +class TestRoutingTable : public RoutingTable +{ +public: + TestRoutingTable(); + virtual ~TestRoutingTable(); + + virtual std::vector<RoutingTable::Entry> get(bool includeLinkLocal = false,bool includeLoopback = false) const; + virtual RoutingTable::Entry set(const InetAddress &destination,const InetAddress &gateway,const char *device,int metric); +}; + +} // namespace ZeroTier + +#endif diff --git a/testnet/nodes/sn0000/identity.public b/testnet/local-testnet/sn0000/identity.public index 041c7479..041c7479 100644 --- a/testnet/nodes/sn0000/identity.public +++ b/testnet/local-testnet/sn0000/identity.public diff --git a/testnet/nodes/sn0000/identity.secret b/testnet/local-testnet/sn0000/identity.secret index bc404424..bc404424 100644 --- a/testnet/nodes/sn0000/identity.secret +++ b/testnet/local-testnet/sn0000/identity.secret diff --git a/testnet/nodes/sn0001/identity.public b/testnet/local-testnet/sn0001/identity.public index e65fcb04..e65fcb04 100644 --- a/testnet/nodes/sn0001/identity.public +++ b/testnet/local-testnet/sn0001/identity.public diff --git a/testnet/nodes/sn0001/identity.secret b/testnet/local-testnet/sn0001/identity.secret index 5eb7b37d..5eb7b37d 100644 --- a/testnet/nodes/sn0001/identity.secret +++ b/testnet/local-testnet/sn0001/identity.secret diff --git a/testnet/nodes/sn0002/identity.public b/testnet/local-testnet/sn0002/identity.public index 2cfe94c5..2cfe94c5 100644 --- a/testnet/nodes/sn0002/identity.public +++ b/testnet/local-testnet/sn0002/identity.public diff --git a/testnet/nodes/sn0002/identity.secret b/testnet/local-testnet/sn0002/identity.secret index 265b070a..265b070a 100644 --- a/testnet/nodes/sn0002/identity.secret +++ b/testnet/local-testnet/sn0002/identity.secret diff --git a/testnet/nodes/sn0003/identity.public b/testnet/local-testnet/sn0003/identity.public index cf6df640..cf6df640 100644 --- a/testnet/nodes/sn0003/identity.public +++ b/testnet/local-testnet/sn0003/identity.public diff --git a/testnet/nodes/sn0003/identity.secret b/testnet/local-testnet/sn0003/identity.secret index cadafae1..cadafae1 100644 --- a/testnet/nodes/sn0003/identity.secret +++ b/testnet/local-testnet/sn0003/identity.secret |