WindowsEthernetTap.hpp 4.4 KB

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