EthernetTap.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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_ETHERNETTAP_HPP
  28. #define ZT_ETHERNETTAP_HPP
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string>
  32. #include <set>
  33. #include "Constants.hpp"
  34. #include "MAC.hpp"
  35. #include "InetAddress.hpp"
  36. #include "Buffer.hpp"
  37. #include "MulticastGroup.hpp"
  38. #include "NonCopyable.hpp"
  39. namespace ZeroTier {
  40. /**
  41. * Base class for Ethernet tap device implementations
  42. */
  43. class EthernetTap : NonCopyable
  44. {
  45. public:
  46. virtual ~EthernetTap() {}
  47. /**
  48. * @return Implementation class name (e.g. UnixEthernetTap)
  49. */
  50. inline const char *implementationName() const { return _implName; }
  51. /**
  52. * Sets whether device is 'up'
  53. *
  54. * This may do nothing on some platforms.
  55. *
  56. * @param en Is device enabled?
  57. */
  58. virtual void setEnabled(bool en) = 0;
  59. /**
  60. * @return Is device 'up'?
  61. */
  62. virtual bool enabled() const = 0;
  63. /**
  64. * @return MAC address of this interface
  65. */
  66. inline const MAC &mac() const throw() { return _mac; }
  67. /**
  68. * @return MTU of this interface
  69. */
  70. inline unsigned int mtu() const throw() { return _mtu; }
  71. /**
  72. * @return Interface metric
  73. */
  74. inline unsigned int metric() const throw() { return _metric; }
  75. /**
  76. * Add an IP to this interface
  77. *
  78. * @param ip IP and netmask (netmask stored in port field)
  79. * @return True if IP added successfully
  80. */
  81. virtual bool addIP(const InetAddress &ip) = 0;
  82. /**
  83. * Remove an IP from this interface
  84. *
  85. * Link-local IP addresses may not be able to be removed, depending on platform and type.
  86. *
  87. * @param ip IP and netmask (netmask stored in port field)
  88. * @return True if IP removed successfully
  89. */
  90. virtual bool removeIP(const InetAddress &ip) = 0;
  91. /**
  92. * @return All IP addresses (V4 and V6) assigned to this interface (including link-local)
  93. */
  94. virtual std::set<InetAddress> ips() const = 0;
  95. /**
  96. * Set this tap's IP addresses to exactly this set of IPs
  97. *
  98. * New IPs are created. Any IP that overlaps with the network of an IP in
  99. * this list is removed, but other IPs are left intact.
  100. *
  101. * @param ips IP addresses with netmask in port field
  102. */
  103. inline void setIps(const std::set<InetAddress> &allIps)
  104. {
  105. for(std::set<InetAddress>::iterator i(allIps.begin());i!=allIps.end();++i)
  106. addIP(*i);
  107. std::set<InetAddress> myIps(ips());
  108. #ifdef __APPLE__
  109. bool haveV6LinkLocal = false;
  110. for(std::set<InetAddress>::iterator i(myIps.begin());i!=myIps.end();++i) {
  111. if (i->isLinkLocal()) {
  112. if (i->isV6())
  113. haveV6LinkLocal = true;
  114. } else if (!allIps.count(*i)) {
  115. for(std::set<InetAddress>::const_iterator i2(allIps.begin());i2!=allIps.end();++i2) {
  116. if (i->sameNetworkAs(*i2)) {
  117. removeIP(*i);
  118. break;
  119. }
  120. }
  121. }
  122. }
  123. if (!haveV6LinkLocal)
  124. addIP(InetAddress::makeIpv6LinkLocal(_mac));
  125. #else
  126. for(std::set<InetAddress>::iterator i(myIps.begin());i!=myIps.end();++i) {
  127. if ((!i->isLinkLocal())&&(!allIps.count(*i))) {
  128. for(std::set<InetAddress>::const_iterator i2(allIps.begin());i2!=allIps.end();++i2) {
  129. if (i->sameNetworkAs(*i2)) {
  130. removeIP(*i);
  131. break;
  132. }
  133. }
  134. }
  135. }
  136. #endif
  137. }
  138. /**
  139. * Put a frame, making it available to the OS for processing
  140. *
  141. * @param from MAC address from which frame originated
  142. * @param to MAC address of destination (typically MAC of tap itself)
  143. * @param etherType Ethernet protocol ID
  144. * @param data Frame payload
  145. * @param len Length of frame
  146. */
  147. virtual void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len) = 0;
  148. /**
  149. * @return OS-specific device or connection name
  150. */
  151. virtual std::string deviceName() const = 0;
  152. /**
  153. * @return OS-internal persistent device ID or empty string if not applicable to this platform or not persistent
  154. */
  155. virtual std::string persistentId() const = 0;
  156. /**
  157. * Fill or modify a set to contain multicast groups for this device
  158. *
  159. * This populates a set or, if already populated, modifies it to contain
  160. * only multicast groups in which this device is interested.
  161. *
  162. * This neither includes nor removes the broadcast (ff:ff:ff:ff:ff:ff / 0)
  163. * group.
  164. *
  165. * @param groups Set to modify in place
  166. * @return True if set was changed since last call
  167. */
  168. virtual bool updateMulticastGroups(std::set<MulticastGroup> &groups) = 0;
  169. protected:
  170. EthernetTap(const char *cn,const MAC &m,unsigned int mt,unsigned int met) :
  171. _implName(cn),
  172. _mac(m),
  173. _mtu(mt),
  174. _metric(met) {}
  175. const char *_implName;
  176. MAC _mac;
  177. unsigned int _mtu;
  178. unsigned int _metric;
  179. };
  180. } // namespace ZeroTier
  181. #endif