EthernetTap.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 <stdexcept>
  37. #include "Constants.hpp"
  38. #include "InetAddress.hpp"
  39. #include "MAC.hpp"
  40. #include "Mutex.hpp"
  41. #include "MulticastGroup.hpp"
  42. #include "Thread.hpp"
  43. #include "Buffer.hpp"
  44. namespace ZeroTier {
  45. class RuntimeEnvironment;
  46. /**
  47. * System ethernet tap device
  48. */
  49. class EthernetTap
  50. {
  51. public:
  52. /**
  53. * Construct a new TAP device
  54. *
  55. * Handler arguments: arg,from,to,etherType,data
  56. *
  57. * @param renv Runtime environment
  58. * @param tag A tag used to identify persistent taps at the OS layer (e.g. nwid in hex)
  59. * @param mac MAC address of device
  60. * @param mtu MTU of device
  61. * @param desc If non-NULL, a description (not used on all OSes)
  62. * @param handler Handler function to be called when data is received from the tap
  63. * @param arg First argument to handler function
  64. * @throws std::runtime_error Unable to allocate device
  65. */
  66. EthernetTap(
  67. const RuntimeEnvironment *renv,
  68. const char *tag,
  69. const MAC &mac,
  70. unsigned int mtu,
  71. void (*handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &),
  72. void *arg)
  73. throw(std::runtime_error);
  74. /**
  75. * Close tap and shut down thread
  76. *
  77. * This may block for a few seconds while thread exits.
  78. */
  79. ~EthernetTap();
  80. /**
  81. * Perform OS dependent actions on network configuration change detection
  82. */
  83. void whack();
  84. /**
  85. * @return MAC address of this interface
  86. */
  87. inline const MAC &mac() const throw() { return _mac; }
  88. /**
  89. * @return MTU of this interface
  90. */
  91. inline unsigned int mtu() const throw() { return _mtu; }
  92. /**
  93. * Add an IP to this interface
  94. *
  95. * @param ip IP and netmask (netmask stored in port field)
  96. * @return True if IP added successfully
  97. */
  98. bool addIP(const InetAddress &ip);
  99. /**
  100. * Remove an IP from this interface
  101. *
  102. * @param ip IP and netmask (netmask stored in port field)
  103. * @return True if IP removed successfully
  104. */
  105. bool removeIP(const InetAddress &ip);
  106. /**
  107. * @return Set of IP addresses / netmasks
  108. */
  109. inline std::set<InetAddress> ips() const
  110. {
  111. Mutex::Lock _l(_ips_m);
  112. return _ips;
  113. }
  114. /**
  115. * Set this tap's IP addresses to exactly this set of IPs
  116. *
  117. * New IPs are created, ones not in this list are removed.
  118. *
  119. * @param ips IP addresses with netmask in port field
  120. */
  121. inline void setIps(const std::set<InetAddress> &allIps)
  122. {
  123. for(std::set<InetAddress>::iterator i(allIps.begin());i!=allIps.end();++i)
  124. addIP(*i);
  125. std::set<InetAddress> myIps(ips());
  126. for(std::set<InetAddress>::iterator i(myIps.begin());i!=myIps.end();++i) {
  127. if (!allIps.count(*i))
  128. removeIP(*i);
  129. }
  130. }
  131. /**
  132. * Put a frame, making it available to the OS for processing
  133. *
  134. * @param from MAC address from which frame originated
  135. * @param to MAC address of destination (typically MAC of tap itself)
  136. * @param etherType Ethernet protocol ID
  137. * @param data Frame payload
  138. * @param len Length of frame
  139. */
  140. void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
  141. /**
  142. * @return OS-specific device or connection name
  143. */
  144. std::string deviceName() const;
  145. /**
  146. * Fill or modify a set to contain multicast groups for this device
  147. *
  148. * This populates a set or, if already populated, modifies it to contain
  149. * only multicast groups in which this device is interested.
  150. *
  151. * This should always include the blind wildcard MulticastGroup (MAC of
  152. * ff:ff:ff:ff:ff:ff and 0 ADI field).
  153. *
  154. * @param groups Set to modify in place
  155. * @return True if set was changed since last call
  156. */
  157. bool updateMulticastGroups(std::set<MulticastGroup> &groups);
  158. /**
  159. * Thread main method; do not call elsewhere
  160. */
  161. void threadMain()
  162. throw();
  163. private:
  164. const MAC _mac;
  165. const unsigned int _mtu;
  166. const RuntimeEnvironment *_r;
  167. Thread _thread;
  168. std::set<InetAddress> _ips;
  169. Mutex _ips_m;
  170. void (*_handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &);
  171. void *_arg;
  172. #ifdef __UNIX_LIKE__
  173. char _dev[16];
  174. int _fd;
  175. int _shutdownSignalPipe[2];
  176. #endif
  177. };
  178. } // namespace ZeroTier
  179. #endif