EthernetTap.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 "Array.hpp"
  38. #include "Utils.hpp"
  39. #include "InetAddress.hpp"
  40. #include "NonCopyable.hpp"
  41. #include "MAC.hpp"
  42. #include "Constants.hpp"
  43. #include "Mutex.hpp"
  44. #include "MulticastGroup.hpp"
  45. namespace ZeroTier {
  46. class RuntimeEnvironment;
  47. /**
  48. * System ethernet tap device
  49. */
  50. class EthernetTap : NonCopyable
  51. {
  52. public:
  53. /**
  54. * Construct a new TAP device
  55. *
  56. * @param renv Runtime environment
  57. * @param mac MAC address of device
  58. * @param mtu MTU of device
  59. * @throws std::runtime_error Unable to allocate device
  60. */
  61. EthernetTap(const RuntimeEnvironment *renv,const MAC &mac,unsigned int mtu)
  62. throw(std::runtime_error);
  63. ~EthernetTap();
  64. /**
  65. * Perform OS dependent actions on network configuration change detection
  66. */
  67. void whack();
  68. /**
  69. * @return MAC address of this interface
  70. */
  71. inline const MAC &mac() const throw() { return _mac; }
  72. /**
  73. * @return MTU of this interface
  74. */
  75. inline unsigned int mtu() const throw() { return _mtu; }
  76. /**
  77. * Add an IP to this interface
  78. *
  79. * @param ip IP and netmask (netmask stored in port field)
  80. * @return True if IP added successfully
  81. */
  82. bool addIP(const InetAddress &ip);
  83. /**
  84. * Remove an IP from this interface
  85. *
  86. * @param ip IP and netmask (netmask stored in port field)
  87. * @return True if IP removed successfully
  88. */
  89. bool removeIP(const InetAddress &ip);
  90. /**
  91. * @return Set of IP addresses / netmasks
  92. */
  93. inline std::set<InetAddress> ips() const
  94. {
  95. Mutex::Lock _l(_ips_m);
  96. return _ips;
  97. }
  98. /**
  99. * Set this tap's IP addresses to exactly this set of IPs
  100. *
  101. * New IPs are created, ones not in this list are removed.
  102. *
  103. * @param ips IP addresses with netmask in port field
  104. */
  105. inline void setIps(const std::set<InetAddress> &allIps)
  106. {
  107. for(std::set<InetAddress>::iterator i(allIps.begin());i!=allIps.end();++i)
  108. addIP(*i);
  109. std::set<InetAddress> myIps(ips());
  110. for(std::set<InetAddress>::iterator i(myIps.begin());i!=myIps.end();++i) {
  111. if (!allIps.count(*i))
  112. removeIP(*i);
  113. }
  114. }
  115. /**
  116. * Put a frame, making it available to the OS for processing
  117. *
  118. * @param from MAC address from which frame originated
  119. * @param to MAC address of destination (typically MAC of tap itself)
  120. * @param etherType Ethernet protocol ID
  121. * @param data Frame payload
  122. * @param len Length of frame
  123. */
  124. void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
  125. /**
  126. * Get the next packet from the interface, blocking if none is available.
  127. *
  128. * @param from Filled with MAC address of source (normally our own)
  129. * @param to Filled with MAC address of destination
  130. * @param etherType Filled with Ethernet frame type
  131. * @param buf Buffer to fill (must have room for MTU bytes)
  132. * @return Number of bytes read or 0 if none
  133. */
  134. unsigned int get(MAC &from,MAC &to,unsigned int &etherType,void *buf);
  135. /**
  136. * @return OS-specific device or connection name
  137. */
  138. std::string deviceName();
  139. /**
  140. * @return True if tap is open
  141. */
  142. bool open() const;
  143. /**
  144. * Close this tap, invalidating the object and causing get() to abort
  145. */
  146. void close();
  147. /**
  148. * Fill or modify a set to contain multicast groups for this device
  149. *
  150. * This populates a set or, if already populated, modifies it to contain
  151. * only multicast groups in which this device is interested.
  152. *
  153. * This should always include the blind wildcard MulticastGroup (MAC of
  154. * ff:ff:ff:ff:ff:ff and 0 ADI field).
  155. *
  156. * @param groups Set to modify in place
  157. * @return True if set was changed since last call
  158. */
  159. bool updateMulticastGroups(std::set<MulticastGroup> &groups);
  160. private:
  161. const MAC _mac;
  162. const unsigned int _mtu;
  163. const RuntimeEnvironment *_r;
  164. std::set<InetAddress> _ips;
  165. Mutex _ips_m;
  166. #if defined(__APPLE__) || defined(__linux__) || defined(linux) || defined(__LINUX__) || defined(__linux)
  167. char _dev[16];
  168. unsigned char *_putBuf;
  169. unsigned char *_getBuf;
  170. int _fd;
  171. bool _isReading;
  172. pthread_t _isReadingThreadId;
  173. Mutex _isReading_m;
  174. #elif defined(_WIN32) /* -------------------------------------------------- */
  175. #endif /* ----------------------------------------------------------------- */
  176. };
  177. } // namespace ZeroTier
  178. #endif