Membership.cpp 6.3 KB

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