WindowsEthernetTap.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 ZeroTier Networks LLC
  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 <string>
  32. #include <queue>
  33. #include <stdexcept>
  34. #include "../Constants.hpp"
  35. #include "../EthernetTap.hpp"
  36. #include "../Mutex.hpp"
  37. #include "../Thread.hpp"
  38. #include "../Array.hpp"
  39. namespace ZeroTier {
  40. /**
  41. * Windows Ethernet tap device using bundled ztTap driver
  42. */
  43. class WindowsEthernetTap : public EthernetTap
  44. {
  45. public:
  46. /**
  47. * Open tap device, installing and creating one if it does not exist
  48. *
  49. * @param renv Runtime environment
  50. * @param tag A tag (presently the hex network ID) used to identify persistent tap devices in the registry
  51. * @param mac MAC address of device
  52. * @param mtu MTU of device
  53. * @param desc If non-NULL, a description (not used on all OSes)
  54. * @param handler Handler function to be called when data is received from the tap
  55. * @param arg First argument to handler function
  56. * @throws std::runtime_error Unable to allocate device
  57. */
  58. WindowsEthernetTap(
  59. const RuntimeEnvironment *renv,
  60. const char *tag,
  61. const MAC &mac,
  62. unsigned int mtu,
  63. void (*handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &),
  64. void *arg)
  65. throw(std::runtime_error);
  66. virtual ~WindowsEthernetTap();
  67. virtual void setEnabled(bool en);
  68. virtual bool enabled() const;
  69. virtual void setDisplayName(const char *dn);
  70. virtual bool addIP(const InetAddress &ip);
  71. virtual bool removeIP(const InetAddress &ip);
  72. virtual std::set<InetAddress> ips() const;
  73. virtual void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
  74. virtual std::string deviceName() const;
  75. virtual std::string persistentId() const;
  76. virtual bool updateMulticastGroups(std::set<MulticastGroup> &groups);
  77. /**
  78. * Thread main method; do not call elsewhere
  79. */
  80. void threadMain()
  81. throw();
  82. /**
  83. * Remove persistent tap device by device name
  84. *
  85. * @param _r Runtime environment
  86. * @param pdev Device name as returned by persistentId() while tap is running
  87. * @return True if a device was deleted
  88. */
  89. static bool deletePersistentTapDevice(const RuntimeEnvironment *_r,const char *pid);
  90. /**
  91. * Clean persistent tap devices that are not in the supplied set
  92. *
  93. * @param _r Runtime environment
  94. * @param exceptThese Devices to leave in place
  95. * @param alsoRemoveUnassociatedDevices If true, remove devices not associated with any network as well
  96. * @return Number of devices deleted or -1 if an error prevented the operation from being performed
  97. */
  98. static int cleanPersistentTapDevices(const RuntimeEnvironment *_r,const std::set<std::string> &exceptThese,bool alsoRemoveUnassociatedDevices);
  99. private:
  100. void (*_handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &);
  101. void *_arg;
  102. Thread _thread;
  103. volatile HANDLE _tap;
  104. HANDLE _injectSemaphore;
  105. GUID _deviceGuid;
  106. std::string _netCfgInstanceId; // NetCfgInstanceId, a GUID
  107. std::string _deviceInstanceId; // DeviceInstanceID, another kind of "instance ID"
  108. std::queue< std::pair< Array<char,ZT_IF_MTU + 32>,unsigned int > > _injectPending;
  109. Mutex _injectPending_m;
  110. volatile bool _run;
  111. volatile bool _initialized;
  112. volatile bool _enabled;
  113. };
  114. } // namespace ZeroTier
  115. #endif