WindowsEthernetTap.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2017 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/Array.hpp"
  37. #include "../node/MulticastGroup.hpp"
  38. #include "../node/InetAddress.hpp"
  39. #include "../osdep/Thread.hpp"
  40. namespace ZeroTier {
  41. class WindowsEthernetTap
  42. {
  43. public:
  44. /**
  45. * Installs a new instance of the ZT tap driver
  46. *
  47. * @param pathToInf Path to zttap driver .inf file
  48. * @param deviceInstanceId Buffer to fill with device instance ID on success (and if SetupDiGetDeviceInstanceIdA succeeds, which it should)
  49. * @return Empty string on success, otherwise an error message
  50. */
  51. static std::string addNewPersistentTapDevice(const char *pathToInf,std::string &deviceInstanceId);
  52. /**
  53. * Uninstalls all persistent tap devices that have legacy drivers
  54. *
  55. * @return Empty string on success, otherwise an error message
  56. */
  57. static std::string destroyAllLegacyPersistentTapDevices();
  58. /**
  59. * Uninstalls all persistent tap devices on the system
  60. *
  61. * @return Empty string on success, otherwise an error message
  62. */
  63. static std::string destroyAllPersistentTapDevices();
  64. /**
  65. * Uninstall a specific persistent tap device by instance ID
  66. *
  67. * @param instanceId Device instance ID
  68. * @return Empty string on success, otherwise an error message
  69. */
  70. static std::string deletePersistentTapDevice(const char *instanceId);
  71. /**
  72. * Disable a persistent tap device by instance ID
  73. *
  74. * @param instanceId Device instance ID
  75. * @param enabled Enable device?
  76. * @return True if device was found and disabled
  77. */
  78. static bool setPersistentTapDeviceState(const char *instanceId,bool enabled);
  79. WindowsEthernetTap(
  80. const char *hp,
  81. const MAC &mac,
  82. unsigned int mtu,
  83. unsigned int metric,
  84. uint64_t nwid,
  85. const char *friendlyName,
  86. void (*handler)(void *,void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int),
  87. void *arg);
  88. ~WindowsEthernetTap();
  89. void setEnabled(bool en);
  90. bool enabled() const;
  91. bool addIp(const InetAddress &ip);
  92. bool removeIp(const InetAddress &ip);
  93. std::vector<InetAddress> ips() const;
  94. void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
  95. std::string deviceName() const;
  96. void setFriendlyName(const char *friendlyName);
  97. void scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed);
  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. Thread _thread;
  115. volatile HANDLE _tap;
  116. HANDLE _injectSemaphore;
  117. GUID _deviceGuid;
  118. NET_LUID _deviceLuid;
  119. std::string _netCfgInstanceId;
  120. std::string _deviceInstanceId;
  121. std::vector<InetAddress> _assignedIps; // IPs assigned with addIp
  122. Mutex _assignedIps_m;
  123. std::vector<MulticastGroup> _multicastGroups;
  124. std::queue< std::pair< Array<char,ZT_IF_MTU + 32>,unsigned int > > _injectPending;
  125. Mutex _injectPending_m;
  126. std::string _pathToHelpers;
  127. volatile bool _run;
  128. volatile bool _initialized;
  129. volatile bool _enabled;
  130. };
  131. } // namespace ZeroTier
  132. #endif