EthernetTap.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #include "EthernetTap.hpp"
  27. #include "OSUtils.hpp"
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #ifdef ZT_SDK
  31. #include "../controller/EmbeddedNetworkController.hpp"
  32. #include "../node/Node.hpp"
  33. #include "../include/VirtualTap.hpp"
  34. #else
  35. #ifdef __APPLE__
  36. #include <sys/sysctl.h>
  37. #include "MacEthernetTap.hpp"
  38. #include "MacKextEthernetTap.hpp"
  39. #endif // __APPLE__
  40. #ifdef __LINUX__
  41. #include "LinuxEthernetTap.hpp"
  42. #endif // __LINUX__
  43. #ifdef __WINDOWS__
  44. #include "WindowsEthernetTap.hpp"
  45. #endif // __WINDOWS__
  46. #ifdef __FreeBSD__
  47. #include "BSDEthernetTap.hpp"
  48. #endif // __FreeBSD__
  49. #ifdef __NetBSD__
  50. #include "NetBSDEthernetTap.hpp"
  51. #endif // __NetBSD__
  52. #ifdef __OpenBSD__
  53. #include "BSDEthernetTap.hpp"
  54. #endif // __OpenBSD__
  55. #endif
  56. namespace ZeroTier {
  57. std::shared_ptr<EthernetTap> EthernetTap::newInstance(
  58. const char *tapDeviceType, // OS-specific, NULL for default
  59. const char *homePath,
  60. const MAC &mac,
  61. unsigned int mtu,
  62. unsigned int metric,
  63. uint64_t nwid,
  64. const char *friendlyName,
  65. void (*handler)(void *,void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int),
  66. void *arg)
  67. {
  68. #ifdef ZT_SDK
  69. return std::shared_ptr<EthernetTap>(new VirtualTap(homePath,mac,mtu,metric,nwid,friendlyName,handler,arg));
  70. #else // not ZT_SDK
  71. #ifdef __APPLE__
  72. char osrelease[256];
  73. size_t size = sizeof(osrelease);
  74. if (sysctlbyname("kern.osrelease",osrelease,&size,nullptr,0) == 0) {
  75. char *dotAt = strchr(osrelease,'.');
  76. if (dotAt) {
  77. *dotAt = (char)0;
  78. // The "feth" virtual Ethernet device type appeared in Darwin 17.x.x. Older versions
  79. // (Sierra and earlier) must use the a kernel extension.
  80. if (strtol(osrelease,(char **)0,10) < 17) {
  81. return std::shared_ptr<EthernetTap>(new MacKextEthernetTap(homePath,mac,mtu,metric,nwid,friendlyName,handler,arg));
  82. } else {
  83. return std::shared_ptr<EthernetTap>(new MacEthernetTap(homePath,mac,mtu,metric,nwid,friendlyName,handler,arg));
  84. }
  85. }
  86. }
  87. #endif // __APPLE__
  88. #ifdef __LINUX__
  89. return std::shared_ptr<EthernetTap>(new LinuxEthernetTap(homePath,mac,mtu,metric,nwid,friendlyName,handler,arg));
  90. #endif // __LINUX__
  91. #ifdef __WINDOWS__
  92. return std::shared_ptr<EthernetTap>(new WindowsEthernetTap(homePath,mac,mtu,metric,nwid,friendlyName,handler,arg));
  93. #endif // __WINDOWS__
  94. #ifdef __FreeBSD__
  95. return std::shared_ptr<EthernetTap>(new BSDEthernetTap(homePath,mac,mtu,metric,nwid,friendlyName,handler,arg));
  96. #endif // __FreeBSD__
  97. #ifdef __NetBSD__
  98. return std::shared_ptr<EthernetTap>(new NetBSDEthernetTap(homePath,mac,mtu,metric,nwid,friendlyName,handler,arg));
  99. #endif // __NetBSD__
  100. #ifdef __OpenBSD__
  101. return std::shared_ptr<EthernetTap>(new BSDEthernetTap(homePath,mac,mtu,metric,nwid,friendlyName,handler,arg));
  102. #endif // __OpenBSD__
  103. #endif // ZT_SDK?
  104. return std::shared_ptr<EthernetTap>();
  105. }
  106. EthernetTap::EthernetTap() {}
  107. EthernetTap::~EthernetTap() {}
  108. } // namespace ZeroTier