TestEthernetTap.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. static Mutex printLock;
  40. TestEthernetTap::TestEthernetTap(
  41. TestEthernetTapFactory *parent,
  42. const MAC &mac,
  43. unsigned int mtu,
  44. unsigned int metric,
  45. uint64_t nwid,
  46. const char *desiredDevice,
  47. const char *friendlyName,
  48. void (*handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &),
  49. void *arg) :
  50. EthernetTap("TestEthernetTap",mac,mtu,metric),
  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. {
  72. Mutex::Lock _l(_pq_m);
  73. _pq.push(TestFrame()); // 0 length frame = exit
  74. }
  75. _pq_c.signal();
  76. Thread::join(_thread);
  77. }
  78. void TestEthernetTap::setEnabled(bool en)
  79. {
  80. _enabled = en;
  81. }
  82. bool TestEthernetTap::enabled() const
  83. {
  84. return _enabled;
  85. }
  86. bool TestEthernetTap::addIP(const InetAddress &ip)
  87. {
  88. return true;
  89. }
  90. bool TestEthernetTap::removeIP(const InetAddress &ip)
  91. {
  92. return true;
  93. }
  94. std::set<InetAddress> TestEthernetTap::ips() const
  95. {
  96. return std::set<InetAddress>();
  97. }
  98. void TestEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
  99. {
  100. Mutex::Lock _l(printLock);
  101. 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());
  102. fflush(stdout);
  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(TestFrame(from,to,data,etherType & 0xffff,len));
  122. }
  123. _pq_c.signal();
  124. {
  125. Mutex::Lock _l(printLock);
  126. 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());
  127. fflush(stdout);
  128. }
  129. return true;
  130. }
  131. void TestEthernetTap::threadMain()
  132. throw()
  133. {
  134. TestFrame tf;
  135. for(;;) {
  136. tf.len = 0;
  137. {
  138. Mutex::Lock _l(_pq_m);
  139. if (!_pq.empty()) {
  140. if (_pq.front().len == 0)
  141. break;
  142. memcpy(&tf,&(_pq.front()),sizeof(tf));
  143. _pq.pop();
  144. }
  145. }
  146. if ((tf.len > 0)&&(_enabled))
  147. _handler(_arg,tf.from,tf.to,tf.etherType,Buffer<4096>(tf.data,tf.len));
  148. _pq_c.wait();
  149. }
  150. }
  151. } // namespace ZeroTier