WindowsEthernetTap.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2018 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_WINDOWSETHERNETTAP_HPP
  27. #define ZT_WINDOWSETHERNETTAP_HPP
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <ifdef.h>
  31. #include <string>
  32. #include <queue>
  33. #include <stdexcept>
  34. #include "../node/Constants.hpp"
  35. #include "../node/Mutex.hpp"
  36. #include "../node/MulticastGroup.hpp"
  37. #include "../node/InetAddress.hpp"
  38. #include "../osdep/Thread.hpp"
  39. namespace ZeroTier {
  40. class WindowsEthernetTap
  41. {
  42. public:
  43. /**
  44. * Installs a new instance of the ZT tap driver
  45. *
  46. * @param pathToInf Path to zttap driver .inf file
  47. * @param deviceInstanceId Buffer to fill with device instance ID on success (and if SetupDiGetDeviceInstanceIdA succeeds, which it should)
  48. * @return Empty string on success, otherwise an error message
  49. */
  50. static std::string addNewPersistentTapDevice(const char *pathToInf,std::string &deviceInstanceId);
  51. /**
  52. * Uninstalls all persistent tap devices that have legacy drivers
  53. *
  54. * @return Empty string on success, otherwise an error message
  55. */
  56. static std::string destroyAllLegacyPersistentTapDevices();
  57. /**
  58. * Uninstalls all persistent tap devices on the system
  59. *
  60. * @return Empty string on success, otherwise an error message
  61. */
  62. static std::string destroyAllPersistentTapDevices();
  63. /**
  64. * Uninstalls a specific persistent tap device by instance ID
  65. *
  66. * @param instanceId Device instance ID
  67. * @return Empty string on success, otherwise an error message
  68. */
  69. static std::string deletePersistentTapDevice(const char *instanceId);
  70. /**
  71. * Disable a persistent tap device by instance ID
  72. *
  73. * @param instanceId Device instance ID
  74. * @param enabled Enable device?
  75. * @return True if device was found and disabled
  76. */
  77. static bool setPersistentTapDeviceState(const char *instanceId,bool enabled);
  78. WindowsEthernetTap(
  79. const char *hp,
  80. const MAC &mac,
  81. unsigned int mtu,
  82. unsigned int metric,
  83. uint64_t nwid,
  84. const char *friendlyName,
  85. void (*handler)(void *,void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int),
  86. void *arg);
  87. ~WindowsEthernetTap();
  88. void setEnabled(bool en);
  89. bool enabled() const;
  90. bool addIp(const InetAddress &ip);
  91. bool removeIp(const InetAddress &ip);
  92. std::vector<InetAddress> ips() const;
  93. void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
  94. std::string deviceName() const;
  95. void setFriendlyName(const char *friendlyName);
  96. void scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed);
  97. void setMtu(unsigned int mtu);
  98. inline const NET_LUID &luid() const { return _deviceLuid; }
  99. inline const GUID &guid() const { return _deviceGuid; }
  100. inline const std::string &instanceId() const { return _deviceInstanceId; }
  101. NET_IFINDEX interfaceIndex() const;
  102. void threadMain()
  103. throw();
  104. bool isInitialized() const { return _initialized; };
  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. std::vector<InetAddress> _assignedIps; // IPs assigned with addIp
  125. Mutex _assignedIps_m;
  126. std::vector<MulticastGroup> _multicastGroups;
  127. struct _InjectPending
  128. {
  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. };
  139. } // namespace ZeroTier
  140. #endif