WindowsEthernetTap.hpp 4.3 KB

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