ZeroTierOne/node/Membership.cpp

135 lines
4.1 KiB
C++
Raw Normal View History

2016-08-04 16:51:15 +00:00
/*
* ZeroTier One - Network Virtualization Everywhere
* Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Membership.hpp"
#include "RuntimeEnvironment.hpp"
#include "Peer.hpp"
#include "Topology.hpp"
#include "Switch.hpp"
#include "Packet.hpp"
#include "Node.hpp"
2016-08-05 22:55:38 +00:00
#define ZT_CREDENTIAL_PUSH_EVERY (ZT_NETWORK_AUTOCONF_DELAY / 4)
2016-08-04 16:51:15 +00:00
namespace ZeroTier {
2016-08-05 22:02:01 +00:00
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)
2016-08-04 16:51:15 +00:00
{
try {
Buffer<ZT_PROTO_MAX_PACKET_LENGTH> capsAndTags;
unsigned int appendedCaps = 0;
2016-08-05 22:02:01 +00:00
if (cap) {
capsAndTags.addSize(2);
std::map<uint32_t,CState>::iterator cs(_caps.find(cap->id()));
if ((cs != _caps.end())&&((now - cs->second.lastPushed) >= ZT_CREDENTIAL_PUSH_EVERY)) {
2016-08-05 22:02:01 +00:00
cap->serialize(capsAndTags);
cs->second.lastPushed = now;
2016-08-05 22:02:01 +00:00
++appendedCaps;
2016-08-04 16:51:15 +00:00
}
2016-08-05 22:02:01 +00:00
capsAndTags.setAt<uint16_t>(0,(uint16_t)appendedCaps);
} else {
capsAndTags.append((uint16_t)0);
2016-08-04 16:51:15 +00:00
}
2016-08-05 22:02:01 +00:00
unsigned int appendedTags = 0;
2016-08-04 16:51:15 +00:00
const unsigned int tagCountPos = capsAndTags.size();
capsAndTags.addSize(2);
for(unsigned int i=0;i<tagCount;++i) {
2016-08-05 22:02:01 +00:00
TState *const ts = _tags.get(tags[i]->id());
2016-08-04 16:51:15 +00:00
if ((now - ts->lastPushed) >= ZT_CREDENTIAL_PUSH_EVERY) {
if ((capsAndTags.size() + sizeof(Tag)) > (ZT_PROTO_MAX_PACKET_LENGTH - sizeof(CertificateOfMembership)))
break;
2016-08-05 22:02:01 +00:00
tags[i]->serialize(capsAndTags);
ts->lastPushed = now;
++appendedTags;
2016-08-04 16:51:15 +00:00
}
}
capsAndTags.setAt<uint16_t>(tagCountPos,(uint16_t)appendedTags);
2016-08-05 22:02:01 +00:00
if ( ((com)&&((now - _lastPushedCom) >= ZT_CREDENTIAL_PUSH_EVERY)) || (appendedCaps) || (appendedTags) ) {
Packet outp(peerAddress,RR->identity.address(),Packet::VERB_NETWORK_CREDENTIALS);
if (com)
com.serialize(outp);
2016-08-04 16:51:15 +00:00
outp.append((uint8_t)0x00);
outp.append(capsAndTags.data(),capsAndTags.size());
outp.compress();
RR->sw->send(outp,true);
2016-08-04 16:51:15 +00:00
_lastPushedCom = now;
return true;
}
} catch ( ... ) {
TRACE("unable to send credentials due to unexpected exception");
}
2016-08-04 17:39:28 +00:00
return false;
2016-08-04 16:51:15 +00:00
}
int Membership::addCredential(const RuntimeEnvironment *RR,const CertificateOfMembership &com)
2016-08-04 16:51:15 +00:00
{
if (com.issuedTo() != RR->identity.address())
return -1;
if (_com == com)
return 0;
const int vr = com.verify(RR);
if ((vr == 0)&&(com.revision() > _com.revision()))
2016-08-04 16:51:15 +00:00
_com = com;
return vr;
}
int Membership::addCredential(const RuntimeEnvironment *RR,const Tag &tag)
2016-08-04 16:51:15 +00:00
{
if (tag.issuedTo() != RR->identity.address())
return -1;
TState *t = _tags.get(tag.id());
2016-08-04 16:51:15 +00:00
if ((t)&&(t->lastReceived != 0)&&(t->tag == tag))
return 0;
const int vr = tag.verify(RR);
if (vr == 0) {
if (!t)
t = &(_tags[tag.id()]);
if (t->tag.timestamp() <= tag.timestamp()) {
t->lastReceived = RR->node->now();
t->tag = tag;
}
2016-08-04 16:51:15 +00:00
}
return vr;
}
int Membership::addCredential(const RuntimeEnvironment *RR,const Capability &cap)
2016-08-04 16:51:15 +00:00
{
if (cap.issuedTo() != RR->identity.address())
2016-08-04 16:51:15 +00:00
return -1;
std::map<uint32_t,CState>::iterator c(_caps.find(cap.id()));
if ((c != _caps.end())&&(c->second.lastReceived != 0)&&(c->second.cap == cap))
2016-08-04 16:51:15 +00:00
return 0;
const int vr = cap.verify(RR);
if (vr == 0) {
if (c == _caps.end()) {
CState &c2 = _caps[cap.id()];
c2.lastReceived = RR->node->now();
c2.cap = cap;
} else if (c->second.cap.timestamp() <= cap.timestamp()) {
c->second.lastReceived = RR->node->now();
c->second.cap = cap;
}
2016-08-04 16:51:15 +00:00
}
return vr;
}
} // namespace ZeroTier