TestEthernetTap.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #ifndef ZT_TESTETHERNETTAP_HPP
  28. #define ZT_TESTETHERNETTAP_HPP
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <string>
  33. #include "../node/Constants.hpp"
  34. #include "../node/EthernetTap.hpp"
  35. #include "../node/Thread.hpp"
  36. #include "../node/Mutex.hpp"
  37. #include "MTQ.hpp"
  38. namespace ZeroTier {
  39. class TestEthernetTapFactory;
  40. /**
  41. * Dummy Ethernet tap
  42. *
  43. * This tap device prints the contents of packets it receives on stdout
  44. * and also prints outgoing packets when they are injected. It does not
  45. * connect to any real tap or other interface. It's useful for running
  46. * test networks.
  47. */
  48. class TestEthernetTap : public EthernetTap
  49. {
  50. public:
  51. struct TestFrame
  52. {
  53. TestFrame() : from(),to(),timestamp(0),etherType(0),len(0) {}
  54. TestFrame(const MAC &f,const MAC &t,const void *d,unsigned int et,unsigned int l) :
  55. from(f),
  56. to(t),
  57. timestamp(Utils::now()),
  58. etherType(et),
  59. len(l)
  60. {
  61. memcpy(data,d,l);
  62. }
  63. MAC from;
  64. MAC to;
  65. uint64_t timestamp;
  66. unsigned int etherType;
  67. unsigned int len;
  68. char data[4096];
  69. };
  70. TestEthernetTap(
  71. const MAC &mac,
  72. unsigned int mtu,
  73. unsigned int metric,
  74. uint64_t nwid,
  75. const char *desiredDevice,
  76. const char *friendlyName,
  77. void (*handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &),
  78. void *arg);
  79. virtual ~TestEthernetTap();
  80. virtual void setEnabled(bool en);
  81. virtual bool enabled() const;
  82. virtual bool addIP(const InetAddress &ip);
  83. virtual bool removeIP(const InetAddress &ip);
  84. virtual std::set<InetAddress> ips() const;
  85. virtual void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
  86. virtual std::string deviceName() const;
  87. virtual void setFriendlyName(const char *friendlyName);
  88. virtual bool updateMulticastGroups(std::set<MulticastGroup> &groups);
  89. virtual bool injectPacketFromHost(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
  90. inline uint64_t nwid() const { return _nwid; }
  91. inline bool getNextReceivedFrame(TestFrame &v,unsigned long timeout) { return _gq.pop(v,timeout); }
  92. void threadMain()
  93. throw();
  94. private:
  95. uint64_t _nwid;
  96. void (*_handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &);
  97. void *_arg;
  98. Thread _thread;
  99. std::string _dev;
  100. volatile bool _enabled;
  101. MTQ<TestFrame> _pq;
  102. MTQ<TestFrame> _gq;
  103. };
  104. } // namespace ZeroTier
  105. #endif