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