Membership.cpp 4.0 KB

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