Network.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 membership to a network
  48. *
  49. * Networks configure themselves via RPC by accessing the function
  50. * com.zerotier.one.Network.bootstrap at any supernode. This returns
  51. * a series of key/value pairs that includes the IP address
  52. * information for this node on the network and -- for closed
  53. * networks -- a URL to retreive the network's membership list.
  54. * A SHA-256 hash is also included to verify the return from this
  55. * URL query.
  56. */
  57. class Network : NonCopyable
  58. {
  59. friend class SharedPtr<Network>;
  60. friend class NodeConfig;
  61. private:
  62. Network(const RuntimeEnvironment *renv,uint64_t id)
  63. throw(std::runtime_error);
  64. ~Network();
  65. public:
  66. /**
  67. * @return Network ID
  68. */
  69. inline uint64_t id() const throw() { return _id; }
  70. /**
  71. * @return Ethernet tap
  72. */
  73. inline EthernetTap &tap() throw() { return _tap; }
  74. /**
  75. * Get this network's members
  76. *
  77. * If this is an open network, membership isn't relevant and this doesn't
  78. * mean much. If it's a closed network, frames will only be exchanged to/from
  79. * members.
  80. *
  81. * @return Members of this network
  82. */
  83. inline std::set<Address> members() const
  84. {
  85. Mutex::Lock _l(_lock);
  86. return _members;
  87. }
  88. /**
  89. * @param addr Address to check
  90. * @return True if address is a member
  91. */
  92. inline bool isMember(const Address &addr) const
  93. throw()
  94. {
  95. Mutex::Lock _l(_lock);
  96. return (_members.count(addr) > 0);
  97. }
  98. /**
  99. * Shortcut to check open() and then isMember()
  100. *
  101. * @param addr Address to check
  102. * @return True if network is open or if address is a member
  103. */
  104. inline bool isAllowed(const Address &addr) const
  105. throw()
  106. {
  107. Mutex::Lock _l(_lock);
  108. return ((_open)||(_members.count(addr) > 0));
  109. }
  110. /**
  111. * @return True if network is open (no membership required)
  112. */
  113. inline bool open() const
  114. throw()
  115. {
  116. Mutex::Lock _l(_lock);
  117. return _open;
  118. }
  119. /**
  120. * Update internal multicast group set and return true if changed
  121. *
  122. * @return True if internal multicast group set has changed
  123. */
  124. inline bool updateMulticastGroups()
  125. {
  126. Mutex::Lock _l(_lock);
  127. return _tap.updateMulticastGroups(_multicastGroups);
  128. }
  129. /**
  130. * @return Latest set of multicast groups
  131. */
  132. inline std::set<MulticastGroup> multicastGroups() const
  133. {
  134. Mutex::Lock _l(_lock);
  135. return _multicastGroups;
  136. }
  137. private:
  138. static void _CBhandleTapData(void *arg,const MAC &from,const MAC &to,unsigned int etherType,const Buffer<4096> &data);
  139. const RuntimeEnvironment *_r;
  140. uint64_t _id;
  141. EthernetTap _tap;
  142. std::set<Address> _members;
  143. std::set<MulticastGroup> _multicastGroups;
  144. bool _open;
  145. Mutex _lock;
  146. AtomicCounter __refCount;
  147. };
  148. } // naemspace ZeroTier
  149. #endif