WindowsEthernetTap.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/EthernetTap.hpp"
  37. #include "../node/Mutex.hpp"
  38. #include "../node/Thread.hpp"
  39. #include "../node/Array.hpp"
  40. #include "../node/MulticastGroup.hpp"
  41. namespace ZeroTier {
  42. class WindowsEthernetTap : public EthernetTap
  43. {
  44. public:
  45. WindowsEthernetTap(
  46. const char *pathToHelpers,
  47. const MAC &mac,
  48. unsigned int mtu,
  49. unsigned int metric,
  50. uint64_t nwid,
  51. const char *desiredDevice,
  52. const char *friendlyName,
  53. void (*handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &),
  54. void *arg);
  55. virtual ~WindowsEthernetTap();
  56. virtual void setEnabled(bool en);
  57. virtual bool enabled() const;
  58. virtual bool addIP(const InetAddress &ip);
  59. virtual bool removeIP(const InetAddress &ip);
  60. virtual std::set<InetAddress> ips() const;
  61. virtual void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
  62. virtual std::string deviceName() const;
  63. virtual void setFriendlyName(const char *friendlyName);
  64. virtual bool updateMulticastGroups(std::set<MulticastGroup> &groups);
  65. virtual bool injectPacketFromHost(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
  66. inline const NET_LUID &luid() const { return _deviceLuid; }
  67. inline const GUID &guid() const { return _deviceGuid; }
  68. inline const std::string &instanceId() const { return _deviceInstanceId; }
  69. void threadMain()
  70. throw();
  71. private:
  72. bool _disableTapDevice();
  73. bool _enableTapDevice();
  74. NET_IFINDEX _getDeviceIndex(); // throws on failure
  75. std::vector<std::string> _getRegistryIPv4Value(const char *regKey);
  76. void _setRegistryIPv4Value(const char *regKey,const std::vector<std::string> &value);
  77. void (*_handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &);
  78. void *_arg;
  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::queue< std::pair< Array<char,ZT_IF_MTU + 32>,unsigned int > > _injectPending;
  88. Mutex _injectPending_m;
  89. std::string _pathToHelpers;
  90. volatile bool _run;
  91. volatile bool _initialized;
  92. volatile bool _enabled;
  93. };
  94. } // namespace ZeroTier
  95. #endif