Membership.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  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. #ifndef ZT_MEMBERSHIP_HPP
  19. #define ZT_MEMBERSHIP_HPP
  20. #include <stdint.h>
  21. #include <utility>
  22. #include <algorithm>
  23. #include "Constants.hpp"
  24. #include "../include/ZeroTierOne.h"
  25. #include "CertificateOfMembership.hpp"
  26. #include "Capability.hpp"
  27. #include "Tag.hpp"
  28. #include "Hashtable.hpp"
  29. #include "NetworkConfig.hpp"
  30. // Expiration time for capability and tag cache
  31. #define ZT_MEMBERSHIP_STATE_EXPIRATION_TIME (ZT_NETWORK_COM_DEFAULT_REVISION_MAX_DELTA * 4)
  32. // Expiration time for Memberships (used in Peer::clean())
  33. #define ZT_MEMBERSHIP_EXPIRATION_TIME (ZT_MEMBERSHIP_STATE_EXPIRATION_TIME * 4)
  34. namespace ZeroTier {
  35. class Peer;
  36. class RuntimeEnvironment;
  37. /**
  38. * Information related to a peer's participation on a network
  39. *
  40. * This structure is not thread-safe and must be locked during use.
  41. */
  42. class Membership
  43. {
  44. private:
  45. struct TState
  46. {
  47. TState() : lastPushed(0),lastReceived(0) {}
  48. // Last time we pushed this tag to this peer
  49. uint64_t lastPushed;
  50. // Last time we received this tag from this peer
  51. uint64_t lastReceived;
  52. // Tag from peer
  53. Tag tag;
  54. };
  55. struct CState
  56. {
  57. CState() : lastPushed(0),lastReceived(0) {}
  58. // Last time we pushed this capability to this peer
  59. uint64_t lastPushed;
  60. // Last time we received this capability from this peer
  61. uint64_t lastReceived;
  62. // Capability from peer
  63. Capability cap;
  64. };
  65. public:
  66. Membership() :
  67. _lastPushedCom(0),
  68. _com(),
  69. _caps(8),
  70. _tags(8)
  71. {
  72. }
  73. /**
  74. * Send COM and other credentials to this peer if needed
  75. *
  76. * This checks last pushed times for our COM and for other credentials and
  77. * sends VERB_NETWORK_CREDENTIALS if the recipient might need them.
  78. *
  79. * @param RR Runtime environment
  80. * @param now Current time
  81. * @param peer Peer that "owns" this membership
  82. * @param nconf Network configuration
  83. * @param capIds Capability IDs that this peer might need
  84. * @param capCount Number of capability IDs
  85. * @param tagIds Tag IDs that this peer might need
  86. * @param tagCount Number of tag IDs
  87. * @return True if we pushed something
  88. */
  89. bool sendCredentialsIfNeeded(const RuntimeEnvironment *RR,const uint64_t now,const Peer &peer,const NetworkConfig &nconf,const uint32_t *capIds,const unsigned int capCount,const uint32_t *tagIds,const unsigned int tagCount);
  90. /**
  91. * Send COM if needed
  92. *
  93. * @param RR Runtime environment
  94. * @param now Current time
  95. * @param peer Peer that "owns" this membership
  96. * @param nconf Network configuration
  97. * @return True if we pushed something
  98. */
  99. inline bool sendCredentialsIfNeeded(const RuntimeEnvironment *RR,const uint64_t now,const Peer &peer,const NetworkConfig &nconf)
  100. {
  101. return sendCredentialsIfNeeded(RR,now,peer,nconf,(const uint32_t *)0,0,(const uint32_t *)0,0);
  102. }
  103. /**
  104. * @param nconf Network configuration
  105. * @param id Tag ID
  106. * @return Pointer to tag or NULL if not found
  107. */
  108. inline const Tag *getTag(const NetworkConfig &nconf,const uint32_t id) const
  109. {
  110. const TState *t = _tags.get(id);
  111. return ((t) ? (((t->lastReceived != 0)&&(t->tag.expiration() < nconf.timestamp)) ? &(t->tag) : (const Tag *)0) : (const Tag *)0);
  112. }
  113. /**
  114. * @param nconf Network configuration
  115. * @param id Capablity ID
  116. * @return Pointer to capability or NULL if not found
  117. */
  118. inline const Capability *getCapability(const NetworkConfig &nconf,const uint32_t id) const
  119. {
  120. const CState *c = _caps.get(id);
  121. return ((c) ? (((c->lastReceived != 0)&&(c->cap.expiration() < nconf.timestamp)) ? &(c->cap) : (const Capability *)0) : (const Capability *)0);
  122. }
  123. /**
  124. * Validate and add a credential if signature is okay and it's otherwise good
  125. *
  126. * @return 0 == OK, 1 == waiting for WHOIS, -1 == BAD signature or credential
  127. */
  128. int addCredential(const RuntimeEnvironment *RR,const uint64_t now,const CertificateOfMembership &com);
  129. /**
  130. * Validate and add a credential if signature is okay and it's otherwise good
  131. *
  132. * @return 0 == OK, 1 == waiting for WHOIS, -1 == BAD signature or credential
  133. */
  134. int addCredential(const RuntimeEnvironment *RR,const uint64_t now,const Tag &tag);
  135. /**
  136. * Validate and add a credential if signature is okay and it's otherwise good
  137. *
  138. * @return 0 == OK, 1 == waiting for WHOIS, -1 == BAD signature or credential
  139. */
  140. int addCredential(const RuntimeEnvironment *RR,const uint64_t now,const Capability &cap);
  141. /**
  142. * Clean up old or stale entries
  143. *
  144. * @return Time of most recent activity in this Membership
  145. */
  146. inline uint64_t clean(const uint64_t now)
  147. {
  148. uint64_t lastAct = _lastPushedCom;
  149. uint32_t *i = (uint32_t *)0;
  150. CState *cs = (CState *)0;
  151. Hashtable<uint32_t,CState>::Iterator csi(_caps);
  152. while (csi.next(i,cs)) {
  153. const uint64_t la = std::max(cs->lastPushed,cs->lastReceived);
  154. if ((now - la) > ZT_MEMBERSHIP_STATE_EXPIRATION_TIME)
  155. _caps.erase(*i);
  156. else if (la > lastAct)
  157. lastAct = la;
  158. }
  159. i = (uint32_t *)0;
  160. TState *ts = (TState *)0;
  161. Hashtable<uint32_t,TState>::Iterator tsi(_tags);
  162. while (tsi.next(i,ts)) {
  163. const uint64_t la = std::max(ts->lastPushed,ts->lastReceived);
  164. if ((now - la) > ZT_MEMBERSHIP_STATE_EXPIRATION_TIME)
  165. _tags.erase(*i);
  166. else if (la > lastAct)
  167. lastAct = la;
  168. }
  169. return lastAct;
  170. }
  171. private:
  172. // Last time we pushed our COM to this peer
  173. uint64_t _lastPushedCom;
  174. // COM from this peer
  175. CertificateOfMembership _com;
  176. // Capability-related state
  177. Hashtable<uint32_t,CState> _caps;
  178. // Tag-related state
  179. Hashtable<uint32_t,TState> _tags;
  180. };
  181. } // namespace ZeroTier
  182. #endif