WindowsEthernetTap.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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: 2023-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 void scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed);
  85. virtual void setMtu(unsigned int mtu);
  86. inline const NET_LUID &luid() const { return _deviceLuid; }
  87. inline const GUID &guid() const { return _deviceGuid; }
  88. inline const std::string &instanceId() const { return _deviceInstanceId; }
  89. NET_IFINDEX interfaceIndex() const;
  90. void threadMain()
  91. throw();
  92. bool isInitialized() const { return _initialized; };
  93. private:
  94. NET_IFINDEX _getDeviceIndex(); // throws on failure
  95. std::vector<std::string> _getRegistryIPv4Value(const char *regKey);
  96. void _setRegistryIPv4Value(const char *regKey,const std::vector<std::string> &value);
  97. void _syncIps();
  98. void (*_handler)(void *,void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int);
  99. void *_arg;
  100. MAC _mac;
  101. uint64_t _nwid;
  102. volatile unsigned int _mtu;
  103. Thread _thread;
  104. volatile HANDLE _tap;
  105. HANDLE _injectSemaphore;
  106. GUID _deviceGuid;
  107. NET_LUID _deviceLuid;
  108. std::string _netCfgInstanceId;
  109. std::string _deviceInstanceId;
  110. std::string _mySubkeyName;
  111. std::string _friendlyName;
  112. std::vector<InetAddress> _assignedIps; // IPs assigned with addIp
  113. Mutex _assignedIps_m;
  114. std::vector<MulticastGroup> _multicastGroups;
  115. struct _InjectPending
  116. {
  117. unsigned int len;
  118. char data[ZT_MAX_MTU + 32];
  119. };
  120. std::queue<_InjectPending> _injectPending;
  121. Mutex _injectPending_m;
  122. std::string _pathToHelpers;
  123. volatile bool _run;
  124. volatile bool _initialized;
  125. volatile bool _enabled;
  126. };
  127. } // namespace ZeroTier
  128. #endif