EthernetTap.cpp 3.9 KB

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