EthernetTap.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2025-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #include "EthernetTap.hpp"
  14. #include "OSUtils.hpp"
  15. #ifdef ZT_SDK
  16. #include "../controller/EmbeddedNetworkController.hpp"
  17. #include "../core/Node.hpp"
  18. #include "../include/VirtualTap.hpp"
  19. #else
  20. #ifdef __APPLE__
  21. #include <sys/sysctl.h>
  22. #include "MacEthernetTap.hpp"
  23. #include "MacKextEthernetTap.hpp"
  24. #endif // __APPLE__
  25. #ifdef __LINUX__
  26. #include "LinuxEthernetTap.hpp"
  27. #endif // __LINUX__
  28. #ifdef __WINDOWS__
  29. #include "WindowsEthernetTap.hpp"
  30. #endif // __WINDOWS__
  31. #ifdef __FreeBSD__
  32. #include "BSDEthernetTap.hpp"
  33. #endif // __FreeBSD__
  34. #ifdef __NetBSD__
  35. #include "NetBSDEthernetTap.hpp"
  36. #endif // __NetBSD__
  37. #ifdef __OpenBSD__
  38. #include "BSDEthernetTap.hpp"
  39. #endif // __OpenBSD__
  40. #endif
  41. namespace ZeroTier {
  42. std::shared_ptr<EthernetTap> EthernetTap::newInstance(
  43. const char *tapDeviceType, // OS-specific, NULL for default
  44. const char *homePath,
  45. const MAC &mac,
  46. unsigned int mtu,
  47. unsigned int metric,
  48. uint64_t nwid,
  49. const char *friendlyName,
  50. void (*handler)(void *,void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int),
  51. void *arg)
  52. {
  53. #ifdef ZT_SDK
  54. return std::shared_ptr<EthernetTap>(new VirtualTap(homePath,mac,mtu,metric,nwid,friendlyName,handler,arg));
  55. #else // not ZT_SDK
  56. #ifdef __APPLE__
  57. char osrelease[256];
  58. size_t size = sizeof(osrelease);
  59. if (sysctlbyname("kern.osrelease",osrelease,&size,nullptr,0) == 0) {
  60. char *dotAt = strchr(osrelease,'.');
  61. if (dotAt) {
  62. *dotAt = (char)0;
  63. // The "feth" virtual Ethernet device type appeared in Darwin 17.x.x. Older versions
  64. // (Sierra and earlier) must use the a kernel extension.
  65. if (strtol(osrelease,(char **)0,10) < 17) {
  66. return std::shared_ptr<EthernetTap>(new MacKextEthernetTap(homePath,mac,mtu,metric,nwid,friendlyName,handler,arg));
  67. } else {
  68. return std::shared_ptr<EthernetTap>(new MacEthernetTap(homePath,mac,mtu,metric,nwid,friendlyName,handler,arg));
  69. }
  70. }
  71. }
  72. #endif // __APPLE__
  73. #ifdef __LINUX__
  74. return std::shared_ptr<EthernetTap>(new LinuxEthernetTap(homePath,mac,mtu,metric,nwid,friendlyName,handler,arg));
  75. #endif // __LINUX__
  76. #ifdef __WINDOWS__
  77. return std::shared_ptr<EthernetTap>(new WindowsEthernetTap(homePath,mac,mtu,metric,nwid,friendlyName,handler,arg));
  78. #endif // __WINDOWS__
  79. #ifdef __FreeBSD__
  80. return std::shared_ptr<EthernetTap>(new BSDEthernetTap(homePath,mac,mtu,metric,nwid,friendlyName,handler,arg));
  81. #endif // __FreeBSD__
  82. #ifdef __NetBSD__
  83. return std::shared_ptr<EthernetTap>(new NetBSDEthernetTap(homePath,mac,mtu,metric,nwid,friendlyName,handler,arg));
  84. #endif // __NetBSD__
  85. #ifdef __OpenBSD__
  86. return std::shared_ptr<EthernetTap>(new BSDEthernetTap(homePath,mac,mtu,metric,nwid,friendlyName,handler,arg));
  87. #endif // __OpenBSD__
  88. #endif // ZT_SDK?
  89. return std::shared_ptr<EthernetTap>();
  90. }
  91. EthernetTap::EthernetTap() {}
  92. EthernetTap::~EthernetTap() {}
  93. bool EthernetTap::addIps(std::vector<InetAddress> ips)
  94. {
  95. for(std::vector<InetAddress>::const_iterator i(ips.begin());i!=ips.end();++i) {
  96. if (!addIp(*i))
  97. return false;
  98. }
  99. return true;
  100. }
  101. std::string EthernetTap::routingDeviceName() const
  102. {
  103. #ifdef __WINDOWS__
  104. char tapdev[64];
  105. OSUtils::ztsnprintf(tapdev,sizeof(tapdev),"%.16llx",(unsigned long long)(((const WindowsEthernetTap *)(this))->luid().Value));
  106. return std::string(tapdev);
  107. #else
  108. return this->deviceName();
  109. #endif
  110. }
  111. } // namespace ZeroTier