WindowsEthernetTap.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "../osdep/Thread.hpp"
  40. namespace ZeroTier {
  41. class WindowsEthernetTap
  42. {
  43. public:
  44. WindowsEthernetTap(
  45. const char *hp,
  46. const MAC &mac,
  47. unsigned int mtu,
  48. unsigned int metric,
  49. uint64_t nwid,
  50. const char *friendlyName,
  51. void (*handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int),
  52. void *arg);
  53. ~WindowsEthernetTap();
  54. void setEnabled(bool en);
  55. bool enabled() const;
  56. bool addIp(const InetAddress &ip);
  57. bool removeIp(const InetAddress &ip);
  58. std::vector<InetAddress> ips() const;
  59. void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
  60. std::string deviceName() const;
  61. void setFriendlyName(const char *friendlyName);
  62. void scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed);
  63. inline const NET_LUID &luid() const { return _deviceLuid; }
  64. inline const GUID &guid() const { return _deviceGuid; }
  65. inline const std::string &instanceId() const { return _deviceInstanceId; }
  66. void threadMain()
  67. throw();
  68. static void destroyAllPersistentTapDevices(const char *pathToHelpers);
  69. static void deletePersistentTapDevice(const char *pathToHelpers,const char *instanceId);
  70. private:
  71. bool _disableTapDevice();
  72. bool _enableTapDevice();
  73. NET_IFINDEX _getDeviceIndex(); // throws on failure
  74. std::vector<std::string> _getRegistryIPv4Value(const char *regKey);
  75. void _setRegistryIPv4Value(const char *regKey,const std::vector<std::string> &value);
  76. void (*_handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int);
  77. void *_arg;
  78. MAC _mac;
  79. uint64_t _nwid;
  80. Thread _thread;
  81. volatile HANDLE _tap;
  82. HANDLE _injectSemaphore;
  83. GUID _deviceGuid;
  84. NET_LUID _deviceLuid;
  85. std::string _netCfgInstanceId; // NetCfgInstanceId, a GUID
  86. std::string _deviceInstanceId; // DeviceInstanceID, another kind of "instance ID"
  87. std::vector<MulticastGroup> _multicastGroups;
  88. std::queue< std::pair< Array<char,ZT_IF_MTU + 32>,unsigned int > > _injectPending;
  89. Mutex _injectPending_m;
  90. std::string _pathToHelpers;
  91. volatile bool _run;
  92. volatile bool _initialized;
  93. volatile bool _enabled;
  94. };
  95. } // namespace ZeroTier
  96. #endif