summaryrefslogtreecommitdiff
path: root/node/Multicaster.cpp
blob: 39fc700d58d38604d87efee4426e121d1f97400b (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * ZeroTier One - Global Peer to Peer Ethernet
 * Copyright (C) 2011-2014  ZeroTier Networks LLC
 *
 * 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/>.
 *
 * --
 *
 * ZeroTier may be used and distributed under the terms of the GPLv3, which
 * are available at: http://www.gnu.org/licenses/gpl-3.0.html
 *
 * If you would like to embed ZeroTier into a commercial application or
 * redistribute it in a modified binary form, please contact ZeroTier Networks
 * LLC. Start here: http://www.zerotier.com/
 */

#include <algorithm>

#include "Constants.hpp"
#include "SharedPtr.hpp"
#include "Multicaster.hpp"
#include "Topology.hpp"
#include "Switch.hpp"
#include "Packet.hpp"
#include "Peer.hpp"
#include "RuntimeEnvironment.hpp"

namespace ZeroTier {

Multicaster::Multicaster() :
	_limit(ZT_MULTICAST_DEFAULT_LIMIT)
{
}

Multicaster::~Multicaster()
{
}

void send(const RuntimeEnvironment *RR,uint64_t nwid,unsigned int limit,uint64_t now,const MulticastGroup &mg,const MAC &src,unsigned int etherType,const void *data,unsigned int len)
{
	Mutex::Lock _l(_groups_m);
	MulticastGroupStatus &gs = _groups[mg];

	if (gs.members.size() >= limit) {
		// If we already have enough members, just send and we're done -- no need for TX queue
		OutboundMulticast out;

		out.init(now,RR->identity.address(),nwid,ZT_MULTICAST_DEFAULT_IMPLICIT_GATHER,src,mg,etherType,data,len);
		unsigned int count = 0;
		for(std::vector<MulticastGroupMember>::const_reverse_iterator m(gs.members.rbegin());m!=gs.members.rend();++gs) {
			out.sendOnly(*(RR->sw),m->address);
			if (++count >= limit)
				break;
		}
	} else {
		// If we don't already have enough members, send to the ones we have and then gather (if allowed within gather rate limit delay)
		gs.txQueue.push_back(OutboundMulticast());
		OutboundMulticast &out = gs.txQueue.back();

		out.init(now,RR->identity.address(),nwid,ZT_MULTICAST_DEFAULT_IMPLICIT_GATHER,src,mg,etherType,data,len);
		for(std::vector<MulticastGroupMember>::const_reverse_iterator m(gs.members.rbegin());m!=gs.members.rend();++gs)
			out.sendAndLog(*(RR->sw),m->address);

		if ((now - gs.lastExplicitGather) >= ZT_MULTICAST_GATHER_DELAY) {
			gs.lastExplicitGather = now;

			// Explicitly gather -- right now we only do this from supernodes since they
			// know all multicast group memberships. In the future this might be more
			// distributed somehow.
			SharedPtr<Peer> sn(RR->topology->getBestSupernode());
			if (sn) {
				Packet outp(sn->address(),RR->identity.address(),Packet::VERB_MULTICAST_GATHER);
				outp.append(nwid);
				outp.append((char)0); // TODO: include network membership cert
				mg.mac().appendTo(outp);
				outp.append((uint32_t)mg.adi());
				outp.append((uint32_t)((limit - (unsigned int)gs.members.size()) + 1)); // +1 just means we'll have an extra in the queue if available
				outp.armor(sn->key(),true);
				sn->send(RR,outp.data(),outp.size(),now);
			}
		}
	}
}

void Multicaster::clean(const RuntimeEnvironment *RR,uint64_t now,unsigned int limit)
{
	Mutex::Lock _l(_groups_m);
	for(std::map< MulticastGroup,MulticastGroupStatus >::iterator mm(_groups.begin());mm!=_groups.end();) {
		// Remove expired outgoing multicasts from multicast TX queue
		for(std::list<OutboundMulticast>::iterator tx(mm->second.txQueue.begin());tx!=mm->second.txQueue.end();) {
			if ((tx->expired(now))||(tx->sentToCount() >= limit))
				mm->second.txQueue.erase(tx++);
			else ++tx;
		}

		// Remove expired members from membership list, and update rank
		// so that remaining members can be sorted in ascending order of
		// transmit priority.
		std::vector<MulticastGroupMember>::iterator reader(mm->second.members.begin());
		std::vector<MulticastGroupMember>::iterator writer(mm->second.members.begin());
		unsigned int count = 0;
		while (reader != mm->second.members.end()) {
			if ((now - reader->timestamp) < ZT_MULTICAST_LIKE_EXPIRE) {
				*writer = *reader;

				/* We rank in ascending order of most recent relevant activity. For peers we've learned
				 * about by direct LIKEs, we do this in order of their own activity. For indirectly
				 * acquired peers we do this minus a constant to place these categorically below directly
				 * learned peers. For peers with no active Peer record, we use the time we last learned
				 * about them minus one day (a large constant) to put these at the bottom of the list.
				 * List is sorted in ascending order of rank and multicasts are sent last-to-first. */
				if (writer->learnedFrom) {
					SharedPtr<Peer> p(RR->topology.getPeer(writer->learnedFrom));
					if (p)
						writer->rank = p->lastUnicastFrame() - ZT_MULTICAST_LIKE_EXPIRE;
					else writer->rank = writer->timestamp - (86400000 + ZT_MULTICAST_LIKE_EXPIRE);
				} else {
					SharedPtr<Peer> p(RR->topology.getPeer(writer->address));
					if (p)
						writer->rank = p->lastUnicastFrame();
					else writer->rank = writer->timestamp - 86400000;
				}

				++writer;
				++count;
			}
			++reader;
		}

		if (count) {
			// There are remaining members, so re-sort them by rank and resize the vector
			std::sort(mm->second.members.begin(),writer); // sorts in ascending order of rank
			mm->second.members.resize(count); // trim off the ones we cut, after writer
			++mm;
		} else if (mm->second.txQueue.empty()) {
			// There are no remaining members and no pending multicasts, so erase the entry
			_groups.erase(mm++);
		} else ++mm;
	}
}

void Multicaster::_add(const RuntimeEnvironment *RR,uint64_t now,MulticastGroupStatus &gs,const Address &learnedFrom,const Address &member)
{
	// assumes _groups_m is locked
	for(std::vector<MulticastGroupMember>::iterator m(gs.members.begin());m!=gs.members.end();++m) {
		if (m->address == member) {
			if (m->learnedFrom)
				m->learnedFrom = learnedFrom; // only update with indirect learnedFrom if we've never directly learned from this peer
			m->timestamp = now;
			return;
		}
	}
	gs.members.push_back(MulticastGroupMember(member,learnedFrom,now));
}

} // namespace ZeroTier