WindowsEthernetTap.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef ZT_WINDOWSETHERNETTAP_HPP
  28. #define ZT_WINDOWSETHERNETTAP_HPP
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <ifdef.h>
  32. #include <string>
  33. #include <queue>
  34. #include <stdexcept>
  35. #include "../node/Constants.hpp"
  36. #include "../node/Mutex.hpp"
  37. #include "../node/Array.hpp"
  38. #include "../node/MulticastGroup.hpp"
  39. #include "../node/InetAddress.hpp"
  40. #include "../osdep/Thread.hpp"
  41. namespace ZeroTier {
  42. class WindowsEthernetTap
  43. {
  44. public:
  45. /**
  46. * Installs a new instance of the ZT tap driver
  47. *
  48. * @param pathToInf Path to zttap driver .inf file
  49. * @param deviceInstanceId Buffer to fill with device instance ID on success (and if SetupDiGetDeviceInstanceIdA succeeds, which it should)
  50. * @return Empty string on success, otherwise an error message
  51. */
  52. static std::string addNewPersistentTapDevice(const char *pathToInf,std::string &deviceInstanceId);
  53. /**
  54. * Uninstalls all persistent tap devices that have legacy drivers
  55. *
  56. * @return Empty string on success, otherwise an error message
  57. */
  58. static std::string destroyAllLegacyPersistentTapDevices();
  59. /**
  60. * Uninstalls all persistent tap devices on the system
  61. *
  62. * @return Empty string on success, otherwise an error message
  63. */
  64. static std::string destroyAllPersistentTapDevices();
  65. /**
  66. * Uninstall a specific persistent tap device by instance ID
  67. *
  68. * @param instanceId Device instance ID
  69. * @return Empty string on success, otherwise an error message
  70. */
  71. static std::string deletePersistentTapDevice(const char *instanceId);
  72. /**
  73. * Disable a persistent tap device by instance ID
  74. *
  75. * @param instanceId Device instance ID
  76. * @param enabled Enable device?
  77. * @return True if device was found and disabled
  78. */
  79. static bool setPersistentTapDeviceState(const char *instanceId,bool enabled);
  80. WindowsEthernetTap(
  81. const char *hp,
  82. const MAC &mac,
  83. unsigned int mtu,
  84. unsigned int metric,
  85. uint64_t nwid,
  86. const char *friendlyName,
  87. void (*handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int),
  88. void *arg);
  89. ~WindowsEthernetTap();
  90. void setEnabled(bool en);
  91. bool enabled() const;
  92. bool addIp(const InetAddress &ip);
  93. bool removeIp(const InetAddress &ip);
  94. std::vector<InetAddress> ips() const;
  95. void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
  96. std::string deviceName() const;
  97. void setFriendlyName(const char *friendlyName);
  98. void scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed);
  99. inline const NET_LUID &luid() const { return _deviceLuid; }
  100. inline const GUID &guid() const { return _deviceGuid; }
  101. inline const std::string &instanceId() const { return _deviceInstanceId; }
  102. void threadMain()
  103. throw();
  104. private:
  105. NET_IFINDEX _getDeviceIndex(); // throws on failure
  106. std::vector<std::string> _getRegistryIPv4Value(const char *regKey);
  107. void _setRegistryIPv4Value(const char *regKey,const std::vector<std::string> &value);
  108. void _syncIps();
  109. void (*_handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int);
  110. void *_arg;
  111. MAC _mac;
  112. uint64_t _nwid;
  113. Thread _thread;
  114. volatile HANDLE _tap;
  115. HANDLE _injectSemaphore;
  116. GUID _deviceGuid;
  117. NET_LUID _deviceLuid;
  118. std::string _netCfgInstanceId;
  119. std::string _deviceInstanceId;
  120. std::vector<InetAddress> _assignedIps; // IPs assigned with addIp
  121. Mutex _assignedIps_m;
  122. std::vector<MulticastGroup> _multicastGroups;
  123. std::queue< std::pair< Array<char,ZT_IF_MTU + 32>,unsigned int > > _injectPending;
  124. Mutex _injectPending_m;
  125. std::string _pathToHelpers;
  126. volatile bool _run;
  127. volatile bool _initialized;
  128. volatile bool _enabled;
  129. };
  130. } // namespace ZeroTier
  131. #endif