TestEthernetTap.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
  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. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_TESTETHERNETTAP_HPP
  27. #define ZT_TESTETHERNETTAP_HPP
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <stdint.h>
  31. #include <sys/types.h>
  32. #include <sys/socket.h>
  33. #include <string>
  34. #include <vector>
  35. #include <stdexcept>
  36. #include <set>
  37. #include "../node/Constants.hpp"
  38. #include "../node/InetAddress.hpp"
  39. #include "../node/MulticastGroup.hpp"
  40. #include "../node/Mutex.hpp"
  41. #include "../node/Utils.hpp"
  42. #include "../osdep/OSUtils.hpp"
  43. namespace ZeroTier {
  44. /**
  45. * Dummy test Ethernet tap that does not actually open a device on the system
  46. */
  47. class TestEthernetTap
  48. {
  49. public:
  50. TestEthernetTap(
  51. const char *homePath,
  52. const MAC &mac,
  53. unsigned int mtu,
  54. unsigned int metric,
  55. uint64_t nwid,
  56. const char *friendlyName,
  57. void (*handler)(void *,void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int),
  58. void *arg) :
  59. _nwid(nwid),
  60. _dev("zt_test_"),
  61. _enabled(true)
  62. {
  63. char tmp[32];
  64. OSUtils::ztsnprintf(tmp,sizeof(tmp),"%.16llx",(unsigned long long)_nwid);
  65. _dev.append(tmp);
  66. #ifdef ZT_TEST_TAP_REPORT_TO
  67. _reportTo.fromString(ZT_TEST_TAP_REPORT_TO);
  68. if (_reportTo.ss_family == AF_INET)
  69. _reportsock = socket(AF_INET,SOCK_DGRAM,0);
  70. else if (_reportTo.ss_family == AF_INET6)
  71. _reportsock = socket(AF_INET6,SOCK_DGRAM,0);
  72. else _reportsock = -1;
  73. #endif
  74. }
  75. ~TestEthernetTap()
  76. {
  77. #ifdef ZT_TEST_TAP_REPORT_TO
  78. if (_reportsock >= 0)
  79. close(_reportsock);
  80. #endif
  81. }
  82. inline void setEnabled(bool en) { _enabled = en; }
  83. inline bool enabled() const { return _enabled; }
  84. inline bool addIp(const InetAddress &ip)
  85. {
  86. Mutex::Lock _l(_lock);
  87. _ips.insert(ip);
  88. return true;
  89. }
  90. inline bool removeIp(const InetAddress &ip)
  91. {
  92. Mutex::Lock _l(_lock);
  93. _ips.erase(ip);
  94. return true;
  95. }
  96. inline std::vector<InetAddress> ips() const
  97. {
  98. Mutex::Lock _l(_lock);
  99. return std::vector<InetAddress>(_ips.begin(),_ips.end());
  100. }
  101. inline void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
  102. {
  103. #ifdef ZT_TEST_TAP_REPORT_TO
  104. char tmp[10000];
  105. if ((_reportsock >= 0)&&(len < (sizeof(tmp) - 22))) {
  106. const uint64_t nwid2 = Utils::hton(_nwid);
  107. memcpy(tmp,&nwid2,8);
  108. from.copyTo(tmp + 8,6);
  109. to.copyTo(tmp + 14,6);
  110. const uint16_t etherType2 = Utils::hton((uint16_t)etherType);
  111. memcpy(tmp + 20,&etherType2,2);
  112. memcpy(tmp + 22,data,len);
  113. sendto(_reportsock,tmp,len + 22,0,reinterpret_cast<const struct sockaddr *>(&_reportTo),(_reportTo.ss_family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6));
  114. }
  115. #endif
  116. }
  117. inline std::string deviceName() const
  118. {
  119. return _dev;
  120. }
  121. inline void setFriendlyName(const char *friendlyName)
  122. {
  123. }
  124. inline void scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed)
  125. {
  126. }
  127. inline void setMtu(unsigned int mtu)
  128. {
  129. }
  130. private:
  131. uint64_t _nwid;
  132. std::string _dev;
  133. std::set<InetAddress> _ips;
  134. InetAddress _reportTo;
  135. #ifdef ZT_TEST_TAP_REPORT_TO
  136. int _reportsock;
  137. #endif
  138. bool _enabled;
  139. Mutex _lock;
  140. };
  141. } // namespace ZeroTier
  142. #endif