WindowsEthernetTap.hpp 4.5 KB

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