WindowsEthernetTap.hpp 4.4 KB

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