EthernetTap.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 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_ETHERNETTAP_HPP
  28. #define ZT_ETHERNETTAP_HPP
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <map>
  32. #include <list>
  33. #include <vector>
  34. #include <set>
  35. #include <string>
  36. #include <queue>
  37. #include <stdexcept>
  38. #include "Constants.hpp"
  39. #include "InetAddress.hpp"
  40. #include "MAC.hpp"
  41. #include "Mutex.hpp"
  42. #include "Condition.hpp"
  43. #include "MulticastGroup.hpp"
  44. #include "Thread.hpp"
  45. #include "Buffer.hpp"
  46. #include "Array.hpp"
  47. #ifdef __WINDOWS__
  48. #include <WinSock2.h>
  49. #include <Windows.h>
  50. #endif
  51. namespace ZeroTier {
  52. class RuntimeEnvironment;
  53. /**
  54. * System ethernet tap device
  55. */
  56. class EthernetTap
  57. {
  58. public:
  59. /**
  60. * Construct a new TAP device
  61. *
  62. * Handler arguments: arg,from,to,etherType,data
  63. *
  64. * @param renv Runtime environment
  65. * @param tag A tag used to identify persistent taps at the OS layer (e.g. nwid in hex)
  66. * @param mac MAC address of device
  67. * @param mtu MTU of device
  68. * @param desc If non-NULL, a description (not used on all OSes)
  69. * @param handler Handler function to be called when data is received from the tap
  70. * @param arg First argument to handler function
  71. * @throws std::runtime_error Unable to allocate device
  72. */
  73. EthernetTap(
  74. const RuntimeEnvironment *renv,
  75. const char *tag,
  76. const MAC &mac,
  77. unsigned int mtu,
  78. void (*handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &),
  79. void *arg)
  80. throw(std::runtime_error);
  81. /**
  82. * Close tap and shut down thread
  83. *
  84. * This may block for a few seconds while thread exits.
  85. */
  86. ~EthernetTap();
  87. /**
  88. * Perform OS dependent actions on network configuration change detection
  89. */
  90. void whack();
  91. /**
  92. * Set whether or not DHCP is enabled (disabled by default)
  93. *
  94. * @param dhcp DHCP status
  95. * @return New state of DHCP (may be false even on 'true' if DHCP enable failed)
  96. */
  97. bool setDhcpEnabled(bool dhcp);
  98. /**
  99. * Set whether or not DHCP6 is enabled (disabled by default)
  100. *
  101. * @param dhcp DHCP6 status
  102. * @return New state of DHCP6 (may be false even on 'true' if DHCP enable failed)
  103. */
  104. bool setDhcp6Enabled(bool dhcp);
  105. /**
  106. * Set the user display name for this connection
  107. *
  108. * This does nothing on platforms that don't have this concept.
  109. *
  110. * @param dn User display name
  111. */
  112. void setDisplayName(const char *dn);
  113. /**
  114. * @return MAC address of this interface
  115. */
  116. inline const MAC &mac() const throw() { return _mac; }
  117. /**
  118. * @return MTU of this interface
  119. */
  120. inline unsigned int mtu() const throw() { return _mtu; }
  121. /**
  122. * Add an IP to this interface
  123. *
  124. * @param ip IP and netmask (netmask stored in port field)
  125. * @return True if IP added successfully
  126. */
  127. bool addIP(const InetAddress &ip);
  128. /**
  129. * Remove an IP from this interface
  130. *
  131. * @param ip IP and netmask (netmask stored in port field)
  132. * @return True if IP removed successfully
  133. */
  134. bool removeIP(const InetAddress &ip);
  135. /**
  136. * @return Set of IP addresses / netmasks
  137. */
  138. inline std::set<InetAddress> ips() const
  139. {
  140. Mutex::Lock _l(_ips_m);
  141. return _ips;
  142. }
  143. /**
  144. * @return Set of IP addresses / netmasks included any we did not assign, link-local, etc.
  145. */
  146. std::set<InetAddress> allIps() const;
  147. /**
  148. * Set this tap's IP addresses to exactly this set of IPs
  149. *
  150. * New IPs are created, ones not in this list are removed.
  151. *
  152. * @param ips IP addresses with netmask in port field
  153. */
  154. inline void setIps(const std::set<InetAddress> &allIps)
  155. {
  156. for(std::set<InetAddress>::iterator i(allIps.begin());i!=allIps.end();++i)
  157. addIP(*i);
  158. std::set<InetAddress> myIps(ips());
  159. for(std::set<InetAddress>::iterator i(myIps.begin());i!=myIps.end();++i) {
  160. if (!allIps.count(*i))
  161. removeIP(*i);
  162. }
  163. }
  164. /**
  165. * Put a frame, making it available to the OS for processing
  166. *
  167. * @param from MAC address from which frame originated
  168. * @param to MAC address of destination (typically MAC of tap itself)
  169. * @param etherType Ethernet protocol ID
  170. * @param data Frame payload
  171. * @param len Length of frame
  172. */
  173. void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
  174. /**
  175. * @return OS-specific device or connection name
  176. */
  177. std::string deviceName() const;
  178. /**
  179. * Fill or modify a set to contain multicast groups for this device
  180. *
  181. * This populates a set or, if already populated, modifies it to contain
  182. * only multicast groups in which this device is interested.
  183. *
  184. * This should always include the blind wildcard MulticastGroup (MAC of
  185. * ff:ff:ff:ff:ff:ff and 0 ADI field).
  186. *
  187. * @param groups Set to modify in place
  188. * @return True if set was changed since last call
  189. */
  190. bool updateMulticastGroups(std::set<MulticastGroup> &groups);
  191. /**
  192. * Thread main method; do not call elsewhere
  193. */
  194. void threadMain()
  195. throw();
  196. private:
  197. const MAC _mac;
  198. const unsigned int _mtu;
  199. const RuntimeEnvironment *_r;
  200. std::set<InetAddress> _ips;
  201. Mutex _ips_m;
  202. void (*_handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &);
  203. void *_arg;
  204. bool _dhcp;
  205. bool _dhcp6;
  206. Thread _thread;
  207. #ifdef __UNIX_LIKE__
  208. char _dev[16];
  209. int _fd;
  210. int _shutdownSignalPipe[2];
  211. #endif
  212. #ifdef __WINDOWS__
  213. HANDLE _tap;
  214. OVERLAPPED _tapOvlRead,_tapOvlWrite;
  215. char _tapReadBuf[ZT_IF_MTU + 32];
  216. HANDLE _injectSemaphore;
  217. GUID _deviceGuid;
  218. std::string _myDeviceInstanceId; // NetCfgInstanceId, a GUID
  219. std::string _myDeviceInstanceIdPath; // DeviceInstanceID, another kind of "instance ID"
  220. std::queue< std::pair< Array<char,ZT_IF_MTU + 32>,unsigned int > > _injectPending;
  221. Mutex _injectPending_m;
  222. volatile bool _run;
  223. #endif
  224. };
  225. } // namespace ZeroTier
  226. #endif