TestEthernetTap.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. const MAC &mac,
  41. unsigned int mtu,
  42. unsigned int metric,
  43. uint64_t nwid,
  44. const char *desiredDevice,
  45. const char *friendlyName,
  46. void (*handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &),
  47. void *arg) :
  48. EthernetTap("TestEthernetTap",mac,mtu,metric),
  49. _nwid(nwid),
  50. _handler(handler),
  51. _arg(arg),
  52. _enabled(true)
  53. {
  54. static volatile unsigned int testTapCounter = 0;
  55. char tmp[64];
  56. int pid = 0;
  57. #ifdef __UNIX_LIKE__
  58. pid = (int)getpid();
  59. #endif
  60. #ifdef __WINDOWS__
  61. pid = (int)_getpid();
  62. #endif
  63. Utils::snprintf(tmp,sizeof(tmp),"test%dtap%d",pid,testTapCounter++);
  64. _dev = tmp;
  65. _thread = Thread::start(this);
  66. }
  67. TestEthernetTap::~TestEthernetTap()
  68. {
  69. static const TestFrame zf; // use a static empty frame because of weirdo G++ warning bug...
  70. _pq.push(zf); // empty frame terminates thread
  71. Thread::join(_thread);
  72. }
  73. void TestEthernetTap::setEnabled(bool en)
  74. {
  75. _enabled = en;
  76. }
  77. bool TestEthernetTap::enabled() const
  78. {
  79. return _enabled;
  80. }
  81. bool TestEthernetTap::addIP(const InetAddress &ip)
  82. {
  83. return true;
  84. }
  85. bool TestEthernetTap::removeIP(const InetAddress &ip)
  86. {
  87. return true;
  88. }
  89. std::set<InetAddress> TestEthernetTap::ips() const
  90. {
  91. return std::set<InetAddress>();
  92. }
  93. void TestEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
  94. {
  95. _gq.push(TestFrame(from,to,data,etherType,len));
  96. }
  97. std::string TestEthernetTap::deviceName() const
  98. {
  99. return _dev;
  100. }
  101. void TestEthernetTap::setFriendlyName(const char *friendlyName)
  102. {
  103. }
  104. bool TestEthernetTap::updateMulticastGroups(std::set<MulticastGroup> &groups)
  105. {
  106. return false;
  107. }
  108. bool TestEthernetTap::injectPacketFromHost(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
  109. {
  110. if ((len == 0)||(len > _mtu))
  111. return false;
  112. _pq.push(TestFrame(from,to,data,etherType & 0xffff,len));
  113. return true;
  114. }
  115. void TestEthernetTap::threadMain()
  116. throw()
  117. {
  118. TestFrame f;
  119. for(;;) {
  120. if (_pq.pop(f,0)) {
  121. if (f.len) {
  122. try {
  123. _handler(_arg,f.from,f.to,f.etherType,Buffer<4096>(f.data,f.len));
  124. } catch ( ... ) {}
  125. } else break;
  126. }
  127. }
  128. }
  129. } // namespace ZeroTier