TestEthernetTap.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include "TestEthernetTap.hpp"
  28. #include "TestEthernetTapFactory.hpp"
  29. #include "../node/Constants.hpp"
  30. #include "../node/Utils.hpp"
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #ifdef __WINDOWS__
  34. #include <process.h>
  35. #else
  36. #include <unistd.h>
  37. #endif
  38. namespace ZeroTier {
  39. TestEthernetTap::TestEthernetTap(
  40. TestEthernetTapFactory *parent,
  41. const MAC &mac,
  42. unsigned int mtu,
  43. unsigned int metric,
  44. uint64_t nwid,
  45. const char *desiredDevice,
  46. const char *friendlyName,
  47. void (*handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &),
  48. void *arg) :
  49. EthernetTap("TestEthernetTap",mac,mtu,metric),
  50. _nwid(nwid),
  51. _parent(parent),
  52. _handler(handler),
  53. _arg(arg),
  54. _enabled(true)
  55. {
  56. static volatile unsigned int testTapCounter = 0;
  57. char tmp[64];
  58. int pid = 0;
  59. #ifdef __UNIX_LIKE__
  60. pid = (int)getpid();
  61. #endif
  62. #ifdef __WINDOWS__
  63. pid = (int)_getpid();
  64. #endif
  65. Utils::snprintf(tmp,sizeof(tmp),"test%dtap%d",pid,testTapCounter++);
  66. _dev = tmp;
  67. _thread = Thread::start(this);
  68. }
  69. TestEthernetTap::~TestEthernetTap()
  70. {
  71. static const TestFrame zf;
  72. {
  73. Mutex::Lock _l(_pq_m);
  74. _pq.push_back(zf); // 0 length frame = exit
  75. _pq_c.signal();
  76. }
  77. Thread::join(_thread);
  78. }
  79. void TestEthernetTap::setEnabled(bool en)
  80. {
  81. _enabled = en;
  82. }
  83. bool TestEthernetTap::enabled() const
  84. {
  85. return _enabled;
  86. }
  87. bool TestEthernetTap::addIP(const InetAddress &ip)
  88. {
  89. return true;
  90. }
  91. bool TestEthernetTap::removeIP(const InetAddress &ip)
  92. {
  93. return true;
  94. }
  95. std::set<InetAddress> TestEthernetTap::ips() const
  96. {
  97. return std::set<InetAddress>();
  98. }
  99. void TestEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
  100. {
  101. Mutex::Lock _l(_gq_m);
  102. _gq.push_back(TestFrame(from,to,data,etherType,len));
  103. }
  104. std::string TestEthernetTap::deviceName() const
  105. {
  106. return _dev;
  107. }
  108. void TestEthernetTap::setFriendlyName(const char *friendlyName)
  109. {
  110. }
  111. bool TestEthernetTap::updateMulticastGroups(std::set<MulticastGroup> &groups)
  112. {
  113. return false;
  114. }
  115. bool TestEthernetTap::injectPacketFromHost(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
  116. {
  117. if ((len == 0)||(len > 2800))
  118. return false;
  119. {
  120. Mutex::Lock _l(_pq_m);
  121. _pq.push_back(TestFrame(from,to,data,etherType & 0xffff,len));
  122. _pq_c.signal();
  123. }
  124. return true;
  125. }
  126. void TestEthernetTap::threadMain()
  127. throw()
  128. {
  129. std::vector<TestFrame> q;
  130. for(;;) {
  131. {
  132. Mutex::Lock _l(_pq_m);
  133. q = _pq;
  134. _pq.clear();
  135. }
  136. for(std::vector<TestFrame>::iterator f(q.begin());f!=q.end();++f) {
  137. if (!f->len)
  138. return; // empty frame signals thread to die
  139. else if (_enabled) {
  140. try {
  141. _handler(_arg,f->from,f->to,f->etherType,Buffer<4096>(f->data,f->len));
  142. } catch ( ... ) {} // handlers should not throw
  143. }
  144. }
  145. _pq_c.wait(1000);
  146. }
  147. }
  148. } // namespace ZeroTier