EthernetTap.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. * Set the user display name for this connection
  65. *
  66. * This does nothing on platforms that don't have this concept.
  67. *
  68. * @param dn User display name
  69. */
  70. virtual void setDisplayName(const char *dn) = 0;
  71. /**
  72. * @return MAC address of this interface
  73. */
  74. inline const MAC &mac() const throw() { return _mac; }
  75. /**
  76. * @return MTU of this interface
  77. */
  78. inline unsigned int mtu() const throw() { return _mtu; }
  79. /**
  80. * Add an IP to this interface
  81. *
  82. * @param ip IP and netmask (netmask stored in port field)
  83. * @return True if IP added successfully
  84. */
  85. virtual bool addIP(const InetAddress &ip) = 0;
  86. /**
  87. * Remove an IP from this interface
  88. *
  89. * Link-local IP addresses may not be able to be removed, depending on platform and type.
  90. *
  91. * @param ip IP and netmask (netmask stored in port field)
  92. * @return True if IP removed successfully
  93. */
  94. virtual bool removeIP(const InetAddress &ip) = 0;
  95. /**
  96. * @return All IP addresses (V4 and V6) assigned to this interface (including link-local)
  97. */
  98. virtual std::set<InetAddress> ips() const = 0;
  99. /**
  100. * Set this tap's IP addresses to exactly this set of IPs
  101. *
  102. * New IPs are created, ones not in this list are removed.
  103. *
  104. * @param ips IP addresses with netmask in port field
  105. */
  106. inline void setIps(const std::set<InetAddress> &allIps)
  107. {
  108. for(std::set<InetAddress>::iterator i(allIps.begin());i!=allIps.end();++i)
  109. addIP(*i);
  110. std::set<InetAddress> myIps(ips());
  111. #ifdef __APPLE__
  112. bool haveV6LinkLocal = false;
  113. for(std::set<InetAddress>::iterator i(myIps.begin());i!=myIps.end();++i) {
  114. if (i->isLinkLocal()) {
  115. if (i->isV6())
  116. haveV6LinkLocal = true;
  117. } else if (!allIps.count(*i))
  118. removeIP(*i);
  119. }
  120. if (!haveV6LinkLocal)
  121. addIP(InetAddress::makeIpv6LinkLocal(_mac));
  122. #else
  123. for(std::set<InetAddress>::iterator i(myIps.begin());i!=myIps.end();++i) {
  124. if ((!i->isLinkLocal())&&(!allIps.count(*i)))
  125. removeIP(*i);
  126. }
  127. #endif
  128. }
  129. /**
  130. * Put a frame, making it available to the OS for processing
  131. *
  132. * @param from MAC address from which frame originated
  133. * @param to MAC address of destination (typically MAC of tap itself)
  134. * @param etherType Ethernet protocol ID
  135. * @param data Frame payload
  136. * @param len Length of frame
  137. */
  138. virtual void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len) = 0;
  139. /**
  140. * @return OS-specific device or connection name
  141. */
  142. virtual std::string deviceName() const = 0;
  143. /**
  144. * @return OS-internal persistent device ID or empty string if not applicable to this platform or not persistent
  145. */
  146. virtual std::string persistentId() const = 0;
  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 neither includes nor removes the broadcast (ff:ff:ff:ff:ff:ff / 0)
  154. * group.
  155. *
  156. * @param groups Set to modify in place
  157. * @return True if set was changed since last call
  158. */
  159. virtual bool updateMulticastGroups(std::set<MulticastGroup> &groups) = 0;
  160. protected:
  161. EthernetTap(const char *cn,const MAC &m,unsigned int mt) :
  162. _implName(cn),
  163. _mac(m),
  164. _mtu(mt) {}
  165. const char *_implName;
  166. MAC _mac;
  167. unsigned int _mtu;
  168. };
  169. } // namespace ZeroTier
  170. #endif