WindowsEthernetTap.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (c)2019 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: 2026-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. #ifndef ZT_WINDOWSETHERNETTAP_HPP
  14. #define ZT_WINDOWSETHERNETTAP_HPP
  15. #include "../node/Constants.hpp"
  16. #include "../node/InetAddress.hpp"
  17. #include "../node/MulticastGroup.hpp"
  18. #include "../node/Mutex.hpp"
  19. #include "../osdep/Thread.hpp"
  20. #include "EthernetTap.hpp"
  21. #include <ifdef.h>
  22. #include <queue>
  23. #include <stdexcept>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string>
  27. namespace ZeroTier {
  28. class WindowsEthernetTap : public EthernetTap {
  29. public:
  30. /**
  31. * Installs a new instance of the ZT tap driver
  32. *
  33. * @param pathToInf Path to zttap driver .inf file
  34. * @param deviceInstanceId Buffer to fill with device instance ID on success (and if SetupDiGetDeviceInstanceIdA succeeds, which it should)
  35. * @return Empty string on success, otherwise an error message
  36. */
  37. static std::string addNewPersistentTapDevice(const char* pathToInf, std::string& deviceInstanceId);
  38. /**
  39. * Uninstalls all persistent tap devices that have legacy drivers
  40. *
  41. * @return Empty string on success, otherwise an error message
  42. */
  43. static std::string destroyAllLegacyPersistentTapDevices();
  44. /**
  45. * Uninstalls all persistent tap devices on the system
  46. *
  47. * @return Empty string on success, otherwise an error message
  48. */
  49. static std::string destroyAllPersistentTapDevices();
  50. /**
  51. * Uninstalls a specific persistent tap device by instance ID
  52. *
  53. * @param instanceId Device instance ID
  54. * @return Empty string on success, otherwise an error message
  55. */
  56. static std::string deletePersistentTapDevice(const char* instanceId);
  57. /**
  58. * Disable a persistent tap device by instance ID
  59. *
  60. * @param instanceId Device instance ID
  61. * @param enabled Enable device?
  62. * @return True if device was found and disabled
  63. */
  64. static bool setPersistentTapDeviceState(const char* instanceId, bool enabled);
  65. WindowsEthernetTap(
  66. const char* hp,
  67. const MAC& mac,
  68. unsigned int mtu,
  69. unsigned int metric,
  70. uint64_t nwid,
  71. const char* friendlyName,
  72. void (*handler)(void*, void*, uint64_t, const MAC&, const MAC&, unsigned int, unsigned int, const void*, unsigned int),
  73. void* arg);
  74. virtual ~WindowsEthernetTap();
  75. virtual void setEnabled(bool en);
  76. virtual bool enabled() const;
  77. virtual bool addIp(const InetAddress& ip);
  78. virtual bool removeIp(const InetAddress& ip);
  79. virtual std::vector<InetAddress> ips() const;
  80. virtual void put(const MAC& from, const MAC& to, unsigned int etherType, const void* data, unsigned int len);
  81. virtual std::string deviceName() const;
  82. virtual void setFriendlyName(const char* friendlyName);
  83. virtual std::string friendlyName() const;
  84. virtual void scanMulticastGroups(std::vector<MulticastGroup>& added, std::vector<MulticastGroup>& removed);
  85. virtual void setMtu(unsigned int mtu);
  86. virtual void setDns(const char* domain, const std::vector<InetAddress>& servers);
  87. inline const NET_LUID& luid() const
  88. {
  89. return _deviceLuid;
  90. }
  91. inline const GUID& guid() const
  92. {
  93. return _deviceGuid;
  94. }
  95. inline const std::string& instanceId() const
  96. {
  97. return _deviceInstanceId;
  98. }
  99. NET_IFINDEX interfaceIndex() const;
  100. void threadMain() throw();
  101. bool isInitialized() const
  102. {
  103. return _initialized;
  104. };
  105. private:
  106. NET_IFINDEX _getDeviceIndex(); // throws on failure
  107. std::vector<std::string> _getRegistryIPv4Value(const char* regKey);
  108. void _setRegistryIPv4Value(const char* regKey, const std::vector<std::string>& value);
  109. void _syncIps();
  110. void (*_handler)(void*, void*, uint64_t, const MAC&, const MAC&, unsigned int, unsigned int, const void*, unsigned int);
  111. void* _arg;
  112. MAC _mac;
  113. uint64_t _nwid;
  114. volatile unsigned int _mtu;
  115. Thread _thread;
  116. volatile HANDLE _tap;
  117. HANDLE _injectSemaphore;
  118. GUID _deviceGuid;
  119. NET_LUID _deviceLuid;
  120. std::string _netCfgInstanceId;
  121. std::string _deviceInstanceId;
  122. std::string _mySubkeyName;
  123. std::string _friendlyName;
  124. Mutex _friendlyName_m;
  125. std::vector<InetAddress> _assignedIps; // IPs assigned with addIp
  126. Mutex _assignedIps_m;
  127. std::vector<MulticastGroup> _multicastGroups;
  128. struct _InjectPending {
  129. unsigned int len;
  130. char data[ZT_MAX_MTU + 32];
  131. };
  132. std::queue<_InjectPending> _injectPending;
  133. Mutex _injectPending_m;
  134. std::string _pathToHelpers;
  135. volatile bool _run;
  136. volatile bool _initialized;
  137. volatile bool _enabled;
  138. mutable std::vector<InetAddress> _ifaddrs;
  139. mutable uint64_t _lastIfAddrsUpdate;
  140. };
  141. } // namespace ZeroTier
  142. #endif