Membership.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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) const;
  90. /**
  91. * @param nconf Network configuration
  92. * @param id Tag ID
  93. * @return Pointer to tag or NULL if not found
  94. */
  95. inline const Tag *getTag(const NetworkConfig &nconf,const uint32_t id) const
  96. {
  97. const TState *t = _tags.get(id);
  98. return ((t) ? (((t->lastReceived != 0)&&(t->tag.expiration() < nconf.timestamp)) ? &(t->tag) : (const Tag *)0) : (const Tag *)0);
  99. }
  100. /**
  101. * @param nconf Network configuration
  102. * @param id Capablity ID
  103. * @return Pointer to capability or NULL if not found
  104. */
  105. inline const Capability *getCapability(const NetworkConfig &nconf,const uint32_t id) const
  106. {
  107. const CState *c = _caps.get(id);
  108. return ((c) ? (((c->lastReceived != 0)&&(c->cap.expiration() < nconf.timestamp)) ? &(c->cap) : (const Capability *)0) : (const Capability *)0);
  109. }
  110. /**
  111. * Validate and add a credential if signature is okay and it's otherwise good
  112. *
  113. * @return 0 == OK, 1 == waiting for WHOIS, -1 == BAD signature or credential
  114. */
  115. inline int addCredential(const RuntimeEnvironment *RR,const uint64_t now,const CertificateOfMembership &com)
  116. {
  117. if (com.issuedTo() != RR->identity.address())
  118. return -1;
  119. if (_com == com)
  120. return 0;
  121. const int vr = com.verify(RR);
  122. if (vr == 0)
  123. _com = com;
  124. return vr;
  125. }
  126. /**
  127. * Validate and add a credential if signature is okay and it's otherwise good
  128. *
  129. * @return 0 == OK, 1 == waiting for WHOIS, -1 == BAD signature or credential
  130. */
  131. inline int addCredential(const RuntimeEnvironment *RR,const uint64_t now,const Tag &tag)
  132. {
  133. if (tag.issuedTo() != RR->identity.address())
  134. return -1;
  135. TState *t = _tags.get(tag.networkId());
  136. if ((t)&&(t->lastReceived != 0)&&(t->tag == tag))
  137. return 0;
  138. const int vr = tag.verify(RR);
  139. if (vr == 0) {
  140. if (!t)
  141. t = &(_tags[tag.networkId()]);
  142. t->lastReceived = now;
  143. t->tag = tag;
  144. }
  145. return vr;
  146. }
  147. /**
  148. * Validate and add a credential if signature is okay and it's otherwise good
  149. *
  150. * @return 0 == OK, 1 == waiting for WHOIS, -1 == BAD signature or credential
  151. */
  152. inline int addCredential(const RuntimeEnvironment *RR,const uint64_t now,const Capability &cap)
  153. {
  154. if (!cap.wasIssuedTo(RR->identity.address()))
  155. return -1;
  156. CState *c = _caps.get(cap.networkId());
  157. if ((c)&&(c->lastReceived != 0)&&(c->cap == cap))
  158. return 0;
  159. const int vr = cap.verify(RR);
  160. if (vr == 0) {
  161. if (!c)
  162. c = &(_caps[cap.networkId()]);
  163. c->lastReceived = now;
  164. c->cap = cap;
  165. }
  166. return vr;
  167. }
  168. /**
  169. * Clean up old or stale entries
  170. *
  171. * @return Time of most recent activity in this Membership
  172. */
  173. inline uint64_t clean(const uint64_t now)
  174. {
  175. uint64_t lastAct = _lastPushedCom;
  176. uint32_t *i = (uint32_t *)0;
  177. CState *cs = (CState *)0;
  178. Hashtable<uint32_t,CState>::Iterator csi(_caps);
  179. while (csi.next(i,cs)) {
  180. const uint64_t la = std::max(cs->lastPushed,cs->lastReceived);
  181. if ((now - la) > ZT_MEMBERSHIP_STATE_EXPIRATION_TIME)
  182. _caps.erase(*i);
  183. else if (la > lastAct)
  184. lastAct = la;
  185. }
  186. i = (uint32_t *)0;
  187. TState *ts = (TState *)0;
  188. Hashtable<uint32_t,TState>::Iterator tsi(_tags);
  189. while (tsi.next(i,ts)) {
  190. const uint64_t la = std::max(ts->lastPushed,ts->lastReceived);
  191. if ((now - la) > ZT_MEMBERSHIP_STATE_EXPIRATION_TIME)
  192. _tags.erase(*i);
  193. else if (la > lastAct)
  194. lastAct = la;
  195. }
  196. return lastAct;
  197. }
  198. private:
  199. // Last time we pushed our COM to this peer
  200. uint64_t _lastPushedCom;
  201. // COM from this peer
  202. CertificateOfMembership _com;
  203. // Capability-related state
  204. Hashtable<uint32_t,CState> _caps;
  205. // Tag-related state
  206. Hashtable<uint32_t,TState> _tags;
  207. };
  208. } // namespace ZeroTier
  209. #endif