TestEthernetTap.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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; // use a static empty frame because of weirdo G++ warning bug...
  72. _pq.push(zf); // empty frame terminates thread
  73. Thread::join(_thread);
  74. }
  75. void TestEthernetTap::setEnabled(bool en)
  76. {
  77. _enabled = en;
  78. }
  79. bool TestEthernetTap::enabled() const
  80. {
  81. return _enabled;
  82. }
  83. bool TestEthernetTap::addIP(const InetAddress &ip)
  84. {
  85. return true;
  86. }
  87. bool TestEthernetTap::removeIP(const InetAddress &ip)
  88. {
  89. return true;
  90. }
  91. std::set<InetAddress> TestEthernetTap::ips() const
  92. {
  93. return std::set<InetAddress>();
  94. }
  95. void TestEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
  96. {
  97. _gq.push(TestFrame(from,to,data,etherType,len));
  98. }
  99. std::string TestEthernetTap::deviceName() const
  100. {
  101. return _dev;
  102. }
  103. void TestEthernetTap::setFriendlyName(const char *friendlyName)
  104. {
  105. }
  106. bool TestEthernetTap::updateMulticastGroups(std::set<MulticastGroup> &groups)
  107. {
  108. return false;
  109. }
  110. bool TestEthernetTap::injectPacketFromHost(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
  111. {
  112. if ((len == 0)||(len > _mtu))
  113. return false;
  114. _pq.push(TestFrame(from,to,data,etherType & 0xffff,len));
  115. return true;
  116. }
  117. void TestEthernetTap::threadMain()
  118. throw()
  119. {
  120. TestFrame f;
  121. for(;;) {
  122. if (_pq.pop(f,0)) {
  123. if (f.len) {
  124. try {
  125. _handler(_arg,f.from,f.to,f.etherType,Buffer<4096>(f.data,f.len));
  126. } catch ( ... ) {}
  127. } else break;
  128. }
  129. }
  130. }
  131. } // namespace ZeroTier