2
0

Network.hpp 3.6 KB

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