Membership.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 CertificateOfMembership &com,const Capability *cap,const Tag **tags,const unsigned int tagCount)
  28. {
  29. try {
  30. Buffer<ZT_PROTO_MAX_PACKET_LENGTH> capsAndTags;
  31. unsigned int appendedCaps = 0;
  32. if (cap) {
  33. capsAndTags.addSize(2);
  34. std::map<uint32_t,CState>::iterator cs(_caps.find(cap->id()));
  35. if ((cs != _caps.end())&&((now - cs->second.lastPushed) >= ZT_CREDENTIAL_PUSH_EVERY)) {
  36. cap->serialize(capsAndTags);
  37. cs->second.lastPushed = now;
  38. ++appendedCaps;
  39. }
  40. capsAndTags.setAt<uint16_t>(0,(uint16_t)appendedCaps);
  41. } else {
  42. capsAndTags.append((uint16_t)0);
  43. }
  44. unsigned int appendedTags = 0;
  45. const unsigned int tagCountPos = capsAndTags.size();
  46. capsAndTags.addSize(2);
  47. for(unsigned int i=0;i<tagCount;++i) {
  48. TState *const ts = _tags.get(tags[i]->id());
  49. if ((now - ts->lastPushed) >= ZT_CREDENTIAL_PUSH_EVERY) {
  50. if ((capsAndTags.size() + sizeof(Tag)) > (ZT_PROTO_MAX_PACKET_LENGTH - sizeof(CertificateOfMembership)))
  51. break;
  52. tags[i]->serialize(capsAndTags);
  53. ts->lastPushed = now;
  54. ++appendedTags;
  55. }
  56. }
  57. capsAndTags.setAt<uint16_t>(tagCountPos,(uint16_t)appendedTags);
  58. if ( ((com)&&((now - _lastPushedCom) >= ZT_CREDENTIAL_PUSH_EVERY)) || (appendedCaps) || (appendedTags) ) {
  59. Packet outp(peerAddress,RR->identity.address(),Packet::VERB_NETWORK_CREDENTIALS);
  60. if (com)
  61. com.serialize(outp);
  62. outp.append((uint8_t)0x00);
  63. outp.append(capsAndTags.data(),capsAndTags.size());
  64. outp.compress();
  65. RR->sw->send(outp,true,0);
  66. _lastPushedCom = now;
  67. return true;
  68. }
  69. } catch ( ... ) {
  70. TRACE("unable to send credentials due to unexpected exception");
  71. }
  72. return false;
  73. }
  74. int Membership::addCredential(const RuntimeEnvironment *RR,const CertificateOfMembership &com)
  75. {
  76. if (com.issuedTo() != RR->identity.address())
  77. return -1;
  78. if (_com == com)
  79. return 0;
  80. const int vr = com.verify(RR);
  81. if ((vr == 0)&&(com.revision() > _com.revision()))
  82. _com = com;
  83. return vr;
  84. }
  85. int Membership::addCredential(const RuntimeEnvironment *RR,const Tag &tag)
  86. {
  87. if (tag.issuedTo() != RR->identity.address())
  88. return -1;
  89. TState *t = _tags.get(tag.id());
  90. if ((t)&&(t->lastReceived != 0)&&(t->tag == tag))
  91. return 0;
  92. const int vr = tag.verify(RR);
  93. if (vr == 0) {
  94. if (!t)
  95. t = &(_tags[tag.id()]);
  96. if (t->tag.timestamp() <= tag.timestamp()) {
  97. t->lastReceived = RR->node->now();
  98. t->tag = tag;
  99. }
  100. }
  101. return vr;
  102. }
  103. int Membership::addCredential(const RuntimeEnvironment *RR,const Capability &cap)
  104. {
  105. if (cap.issuedTo() != RR->identity.address())
  106. return -1;
  107. std::map<uint32_t,CState>::iterator c(_caps.find(cap.id()));
  108. if ((c != _caps.end())&&(c->second.lastReceived != 0)&&(c->second.cap == cap))
  109. return 0;
  110. const int vr = cap.verify(RR);
  111. if (vr == 0) {
  112. if (c == _caps.end()) {
  113. CState &c2 = _caps[cap.id()];
  114. c2.lastReceived = RR->node->now();
  115. c2.cap = cap;
  116. } else if (c->second.cap.timestamp() <= cap.timestamp()) {
  117. c->second.lastReceived = RR->node->now();
  118. c->second.cap = cap;
  119. }
  120. }
  121. return vr;
  122. }
  123. } // namespace ZeroTier