Network.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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_NETWORK_HPP
  28. #define _ZT_NETWORK_HPP
  29. #include <string>
  30. #include <set>
  31. #include <vector>
  32. #include <stdexcept>
  33. #include "EthernetTap.hpp"
  34. #include "Address.hpp"
  35. #include "Mutex.hpp"
  36. #include "InetAddress.hpp"
  37. #include "Constants.hpp"
  38. #include "SharedPtr.hpp"
  39. #include "AtomicCounter.hpp"
  40. #include "RuntimeEnvironment.hpp"
  41. #include "MulticastGroup.hpp"
  42. #include "NonCopyable.hpp"
  43. #include "MAC.hpp"
  44. namespace ZeroTier {
  45. class NodeConfig;
  46. /**
  47. * Local network endpoint
  48. */
  49. class Network : NonCopyable
  50. {
  51. friend class SharedPtr<Network>;
  52. friend class NodeConfig;
  53. private:
  54. Network(const RuntimeEnvironment *renv,uint64_t id)
  55. throw(std::runtime_error);
  56. ~Network();
  57. public:
  58. /**
  59. * @return Network ID
  60. */
  61. inline uint64_t id() const throw() { return _id; }
  62. /**
  63. * @return Ethernet tap
  64. */
  65. inline EthernetTap &tap() throw() { return _tap; }
  66. /**
  67. * Get this network's members
  68. *
  69. * If this is an open network, membership isn't relevant and this doesn't
  70. * mean much. If it's a closed network, frames will only be exchanged to/from
  71. * members.
  72. *
  73. * @return Members of this network
  74. */
  75. inline std::set<Address> members() const
  76. {
  77. Mutex::Lock _l(_lock);
  78. return _members;
  79. }
  80. /**
  81. * @param addr Address to check
  82. * @return True if address is a member
  83. */
  84. inline bool isMember(const Address &addr) const
  85. throw()
  86. {
  87. Mutex::Lock _l(_lock);
  88. return (_members.count(addr) > 0);
  89. }
  90. /**
  91. * Shortcut to check open() and then isMember()
  92. *
  93. * @param addr Address to check
  94. * @return True if network is open or if address is a member
  95. */
  96. inline bool isAllowed(const Address &addr) const
  97. throw()
  98. {
  99. Mutex::Lock _l(_lock);
  100. return ((_open)||(_members.count(addr) > 0));
  101. }
  102. /**
  103. * @return True if network is open (no membership required)
  104. */
  105. inline bool open() const
  106. throw()
  107. {
  108. Mutex::Lock _l(_lock);
  109. return _open;
  110. }
  111. /**
  112. * Update internal multicast group set and return true if changed
  113. *
  114. * @return True if internal multicast group set has changed
  115. */
  116. inline bool updateMulticastGroups()
  117. {
  118. Mutex::Lock _l(_lock);
  119. return _tap.updateMulticastGroups(_multicastGroups);
  120. }
  121. /**
  122. * @return Latest set of multicast groups
  123. */
  124. inline std::set<MulticastGroup> multicastGroups() const
  125. {
  126. Mutex::Lock _l(_lock);
  127. return _multicastGroups;
  128. }
  129. private:
  130. static void _CBhandleTapData(void *arg,const MAC &from,const MAC &to,unsigned int etherType,const Buffer<4096> &data);
  131. const RuntimeEnvironment *_r;
  132. uint64_t _id;
  133. EthernetTap _tap;
  134. std::set<Address> _members;
  135. std::set<MulticastGroup> _multicastGroups;
  136. bool _open;
  137. Mutex _lock;
  138. AtomicCounter __refCount;
  139. };
  140. } // naemspace ZeroTier
  141. #endif