Membership.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #include "Membership.hpp"
  19. #include "RuntimeEnvironment.hpp"
  20. #include "Peer.hpp"
  21. #include "Topology.hpp"
  22. #include "Switch.hpp"
  23. #include "Packet.hpp"
  24. #include "Node.hpp"
  25. #define ZT_CREDENTIAL_PUSH_EVERY (ZT_NETWORK_AUTOCONF_DELAY / 4)
  26. namespace ZeroTier {
  27. bool Membership::sendCredentialsIfNeeded(const RuntimeEnvironment *RR,const uint64_t now,const Address &peerAddress,const NetworkConfig &nconf,const Capability *cap)
  28. {
  29. if ((now - _lastPushAttempt) < 1000ULL)
  30. return false;
  31. _lastPushAttempt = now;
  32. try {
  33. Buffer<ZT_PROTO_MAX_PACKET_LENGTH> capsAndTags;
  34. unsigned int appendedCaps = 0;
  35. if (cap) {
  36. capsAndTags.addSize(2);
  37. std::map<uint32_t,CState>::iterator cs(_caps.find(cap->id()));
  38. if ((cs != _caps.end())&&((now - cs->second.lastPushed) >= ZT_CREDENTIAL_PUSH_EVERY)) {
  39. cap->serialize(capsAndTags);
  40. cs->second.lastPushed = now;
  41. ++appendedCaps;
  42. }
  43. capsAndTags.setAt<uint16_t>(0,(uint16_t)appendedCaps);
  44. } else {
  45. capsAndTags.append((uint16_t)0);
  46. }
  47. unsigned int appendedTags = 0;
  48. const unsigned int tagCountPos = capsAndTags.size();
  49. capsAndTags.addSize(2);
  50. for(unsigned int i=0;i<nconf.tagCount;++i) {
  51. TState *const ts = _tags.get(nconf.tags[i].id());
  52. if ((now - ts->lastPushed) >= ZT_CREDENTIAL_PUSH_EVERY) {
  53. if ((capsAndTags.size() + sizeof(Tag)) > (ZT_PROTO_MAX_PACKET_LENGTH - sizeof(CertificateOfMembership)))
  54. break;
  55. nconf.tags[i].serialize(capsAndTags);
  56. ts->lastPushed = now;
  57. ++appendedTags;
  58. }
  59. }
  60. capsAndTags.setAt<uint16_t>(tagCountPos,(uint16_t)appendedTags);
  61. const bool needCom = ((nconf.isPrivate())&&(nconf.com)&&((now - _lastPushedCom) >= ZT_CREDENTIAL_PUSH_EVERY));
  62. if ( (needCom) || (appendedCaps) || (appendedTags) ) {
  63. Packet outp(peerAddress,RR->identity.address(),Packet::VERB_NETWORK_CREDENTIALS);
  64. if (needCom) {
  65. nconf.com.serialize(outp);
  66. _lastPushedCom = now;
  67. }
  68. outp.append((uint8_t)0x00);
  69. outp.append(capsAndTags.data(),capsAndTags.size());
  70. outp.compress();
  71. RR->sw->send(outp,true);
  72. return true;
  73. }
  74. } catch ( ... ) {
  75. TRACE("unable to send credentials due to unexpected exception");
  76. }
  77. return false;
  78. }
  79. int Membership::addCredential(const RuntimeEnvironment *RR,const CertificateOfMembership &com)
  80. {
  81. if (_com == com) {
  82. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx ACCEPTED (redundant)",com.issuedTo().toString().c_str(),com.networkId());
  83. return 0;
  84. }
  85. const int vr = com.verify(RR);
  86. if (vr == 0) {
  87. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx ACCEPTED (new)",com.issuedTo().toString().c_str(),com.networkId());
  88. if (com.timestamp().first > _com.timestamp().first) {
  89. _com = com;
  90. }
  91. } else {
  92. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx REJECTED (%d)",com.issuedTo().toString().c_str(),com.networkId(),vr);
  93. }
  94. return vr;
  95. }
  96. int Membership::addCredential(const RuntimeEnvironment *RR,const Tag &tag)
  97. {
  98. TState *t = _tags.get(tag.id());
  99. if ((t)&&(t->lastReceived != 0)&&(t->tag == tag)) {
  100. TRACE("addCredential(Tag) for %s on %.16llx ACCEPTED (redundant)",tag.issuedTo().toString().c_str(),tag.networkId());
  101. return 0;
  102. }
  103. const int vr = tag.verify(RR);
  104. if (vr == 0) {
  105. TRACE("addCredential(Tag) for %s on %.16llx ACCEPTED (new)",tag.issuedTo().toString().c_str(),tag.networkId());
  106. if (!t) {
  107. while (_tags.size() >= ZT_MAX_NETWORK_TAGS) {
  108. uint32_t oldest = 0;
  109. uint64_t oldestLastReceived = 0xffffffffffffffffULL;
  110. uint32_t *i = (uint32_t *)0;
  111. TState *ts = (TState *)0;
  112. Hashtable<uint32_t,TState>::Iterator tsi(_tags);
  113. while (tsi.next(i,ts)) {
  114. if (ts->lastReceived < oldestLastReceived) {
  115. oldestLastReceived = ts->lastReceived;
  116. oldest = *i;
  117. }
  118. }
  119. if (oldestLastReceived != 0xffffffffffffffffULL)
  120. _tags.erase(oldest);
  121. }
  122. t = &(_tags[tag.id()]);
  123. }
  124. if (t->tag.timestamp() <= tag.timestamp()) {
  125. t->lastReceived = RR->node->now();
  126. t->tag = tag;
  127. }
  128. } else {
  129. TRACE("addCredential(Tag) for %s on %.16llx REJECTED (%d)",tag.issuedTo().toString().c_str(),tag.networkId(),vr);
  130. }
  131. return vr;
  132. }
  133. int Membership::addCredential(const RuntimeEnvironment *RR,const Capability &cap)
  134. {
  135. std::map<uint32_t,CState>::iterator c(_caps.find(cap.id()));
  136. if ((c != _caps.end())&&(c->second.lastReceived != 0)&&(c->second.cap == cap)) {
  137. TRACE("addCredential(Capability) for %s on %.16llx ACCEPTED (redundant)",cap.issuedTo().toString().c_str(),cap.networkId());
  138. return 0;
  139. }
  140. const int vr = cap.verify(RR);
  141. if (vr == 0) {
  142. TRACE("addCredential(Capability) for %s on %.16llx ACCEPTED (new)",cap.issuedTo().toString().c_str(),cap.networkId());
  143. if (c == _caps.end()) {
  144. while (_caps.size() >= ZT_MAX_NETWORK_CAPABILITIES) {
  145. std::map<uint32_t,CState>::iterator oldest;
  146. uint64_t oldestLastReceived = 0xffffffffffffffffULL;
  147. for(std::map<uint32_t,CState>::iterator i(_caps.begin());i!=_caps.end();++i) {
  148. if (i->second.lastReceived < oldestLastReceived) {
  149. oldestLastReceived = i->second.lastReceived;
  150. oldest = i;
  151. }
  152. }
  153. if (oldestLastReceived != 0xffffffffffffffffULL)
  154. _caps.erase(oldest);
  155. }
  156. CState &c2 = _caps[cap.id()];
  157. c2.lastReceived = RR->node->now();
  158. c2.cap = cap;
  159. } else if (c->second.cap.timestamp() <= cap.timestamp()) {
  160. c->second.lastReceived = RR->node->now();
  161. c->second.cap = cap;
  162. }
  163. } else {
  164. TRACE("addCredential(Capability) for %s on %.16llx REJECTED (%d)",cap.issuedTo().toString().c_str(),cap.networkId(),vr);
  165. }
  166. return vr;
  167. }
  168. } // namespace ZeroTier