summaryrefslogtreecommitdiff
path: root/node/Membership.cpp
blob: 59058a3e1d61bc3b9381029976105c957c9f14f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * 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"

#define ZT_CREDENTIAL_PUSH_EVERY (ZT_NETWORK_AUTOCONF_DELAY / 4)

namespace ZeroTier {

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)
{
	try {
		Buffer<ZT_PROTO_MAX_PACKET_LENGTH> capsAndTags;

		unsigned int appendedCaps = 0;
		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)) {
				cap->serialize(capsAndTags);
				cs->second.lastPushed = now;
				++appendedCaps;
			}
			capsAndTags.setAt<uint16_t>(0,(uint16_t)appendedCaps);
		} else {
			capsAndTags.append((uint16_t)0);
		}

		unsigned int appendedTags = 0;
		const unsigned int tagCountPos = capsAndTags.size();
		capsAndTags.addSize(2);
		for(unsigned int i=0;i<tagCount;++i) {
			TState *const ts = _tags.get(tags[i]->id());
			if ((now - ts->lastPushed) >= ZT_CREDENTIAL_PUSH_EVERY) {
				if ((capsAndTags.size() + sizeof(Tag)) > (ZT_PROTO_MAX_PACKET_LENGTH - sizeof(CertificateOfMembership)))
					break;
				tags[i]->serialize(capsAndTags);
				ts->lastPushed = now;
				++appendedTags;
			}
		}
		capsAndTags.setAt<uint16_t>(tagCountPos,(uint16_t)appendedTags);

		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);
			outp.append((uint8_t)0x00);
			outp.append(capsAndTags.data(),capsAndTags.size());
			outp.compress();
			RR->sw->send(outp,true);
			_lastPushedCom = now;
			return true;
		}
	} catch ( ... ) {
		TRACE("unable to send credentials due to unexpected exception");
	}
	return false;
}

int Membership::addCredential(const RuntimeEnvironment *RR,const CertificateOfMembership &com)
{
	TRACE("addCredential(COM) for %.16llx signed by %s issued to %s",com.networkId(),com.signedBy().toString().c_str(),com.issuedTo().toString().c_str());
	if (_com == com)
		return 0;
	const int vr = com.verify(RR);
	if ((vr == 0)&&(com.timestamp().first > _com.timestamp().first))
		_com = com;
	return vr;
}

int Membership::addCredential(const RuntimeEnvironment *RR,const Tag &tag)
{
	TState *t = _tags.get(tag.id());
	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;
		}
	}
	return vr;
}

int Membership::addCredential(const RuntimeEnvironment *RR,const Capability &cap)
{
	std::map<uint32_t,CState>::iterator c(_caps.find(cap.id()));
	if ((c != _caps.end())&&(c->second.lastReceived != 0)&&(c->second.cap == cap))
		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;
		}
	}
	return vr;
}

} // namespace ZeroTier