2013-07-10 21:24:27 +00:00
|
|
|
/*
|
|
|
|
* ZeroTier One - Global Peer to Peer Ethernet
|
|
|
|
* Copyright (C) 2012-2013 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/
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _ZT_MULTICASTER_HPP
|
|
|
|
#define _ZT_MULTICASTER_HPP
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2013-09-16 17:57:57 +00:00
|
|
|
#include <stdexcept>
|
2013-07-10 21:24:27 +00:00
|
|
|
#include <map>
|
2013-09-25 14:55:27 +00:00
|
|
|
#include <set>
|
2013-09-22 17:35:40 +00:00
|
|
|
#include <list>
|
2013-09-21 20:46:00 +00:00
|
|
|
#include <algorithm>
|
2013-07-10 21:24:27 +00:00
|
|
|
|
|
|
|
#include "Constants.hpp"
|
2013-09-21 20:46:00 +00:00
|
|
|
#include "Mutex.hpp"
|
2013-07-10 21:24:27 +00:00
|
|
|
#include "MulticastGroup.hpp"
|
|
|
|
#include "Address.hpp"
|
2013-09-26 21:45:19 +00:00
|
|
|
#include "Buffer.hpp"
|
2013-07-10 21:24:27 +00:00
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
/**
|
2013-09-25 14:55:27 +00:00
|
|
|
* Multicast propagation algorithm core and database
|
2013-07-10 21:24:27 +00:00
|
|
|
*/
|
|
|
|
class Multicaster
|
|
|
|
{
|
|
|
|
public:
|
2013-09-25 14:55:27 +00:00
|
|
|
Multicaster();
|
|
|
|
~Multicaster();
|
2013-07-11 02:58:43 +00:00
|
|
|
|
2013-07-10 21:24:27 +00:00
|
|
|
/**
|
2013-09-21 20:46:00 +00:00
|
|
|
* Add or renew a peer's subscription to a multicast group
|
2013-07-10 21:24:27 +00:00
|
|
|
*
|
2013-09-25 14:55:27 +00:00
|
|
|
* @param nwid Network ID
|
2013-09-21 20:46:00 +00:00
|
|
|
* @param a Address that LIKEd
|
|
|
|
* @param mg Multicast group
|
|
|
|
* @param now Current time
|
2013-07-10 21:24:27 +00:00
|
|
|
*/
|
2013-09-25 14:55:27 +00:00
|
|
|
void likesGroup(uint64_t nwid,const Address &a,const MulticastGroup &mg,uint64_t now);
|
2013-07-10 21:24:27 +00:00
|
|
|
|
2013-07-13 02:07:48 +00:00
|
|
|
/**
|
2013-09-21 20:46:00 +00:00
|
|
|
* Bring a peer closer in terms of propagation priority
|
2013-07-13 02:07:48 +00:00
|
|
|
*
|
2013-10-01 20:31:46 +00:00
|
|
|
* This gets called from PacketDecoder when a unicast frame is received.
|
|
|
|
*
|
2013-09-25 14:55:27 +00:00
|
|
|
* @param nwid Network ID
|
2013-09-21 20:46:00 +00:00
|
|
|
* @param a Address to bring closer (e.g. due to unicast message)
|
2013-07-13 02:07:48 +00:00
|
|
|
* @param now Current time
|
|
|
|
*/
|
2013-09-25 14:55:27 +00:00
|
|
|
void bringCloser(uint64_t nwid,const Address &a);
|
2013-07-10 21:24:27 +00:00
|
|
|
|
2013-07-13 02:07:48 +00:00
|
|
|
/**
|
2013-09-26 21:45:19 +00:00
|
|
|
* Erase entries for expired LIKEs and GOT records
|
2013-07-13 02:07:48 +00:00
|
|
|
*/
|
2013-09-26 21:45:19 +00:00
|
|
|
void clean();
|
2013-07-13 02:07:48 +00:00
|
|
|
|
2013-07-13 19:17:21 +00:00
|
|
|
/**
|
2013-09-26 21:45:19 +00:00
|
|
|
* Multicast deduplicator
|
|
|
|
*
|
|
|
|
* This checks to see if a multicast GUID has been seen before. If not, it
|
|
|
|
* adds it to the history and returns false.
|
|
|
|
*
|
|
|
|
* @param nwid Network ID
|
|
|
|
* @param mcGuid Multicast GUID (sender address + sender unique ID)
|
|
|
|
* @return True if multicast IS a duplicate, false otherwise
|
2013-07-13 19:17:21 +00:00
|
|
|
*/
|
2013-09-26 21:45:19 +00:00
|
|
|
inline bool deduplicate(uint64_t nwid,uint64_t mcGuid)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
Mutex::Lock _l(_lock);
|
|
|
|
_NetInfo &n = _nets[nwid];
|
|
|
|
for(unsigned int i=0;i<ZT_MULTICAST_DEDUP_HISTORY_LENGTH;++i) {
|
|
|
|
if (n.multicastHistory[i] == mcGuid)
|
|
|
|
return true;
|
|
|
|
}
|
2013-09-27 20:03:13 +00:00
|
|
|
n.multicastHistory[n.multicastHistoryPtr++ % ZT_MULTICAST_DEDUP_HISTORY_LENGTH] = mcGuid;
|
2013-09-26 21:45:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-08-21 15:45:06 +00:00
|
|
|
|
|
|
|
/**
|
2013-09-21 20:46:00 +00:00
|
|
|
* Pick next hops for a multicast by proximity
|
2013-08-21 15:45:06 +00:00
|
|
|
*
|
2013-09-22 17:35:40 +00:00
|
|
|
* The function or function object must return true if more hops are desired
|
|
|
|
* or false to stop finding new hops and return.
|
|
|
|
*
|
2013-09-25 14:55:27 +00:00
|
|
|
* @param nwid Network ID
|
2013-08-21 15:45:06 +00:00
|
|
|
* @param mg Multicast group
|
2013-09-21 20:46:00 +00:00
|
|
|
* @param nextHopFunc Function to call for each address, search stops if it returns false
|
2013-08-21 15:45:06 +00:00
|
|
|
*/
|
2013-09-21 20:46:00 +00:00
|
|
|
template<typename F>
|
2013-09-27 20:03:13 +00:00
|
|
|
inline void getNextHops(uint64_t nwid,const MulticastGroup &mg,F nextHopFunc)
|
2013-07-10 21:24:27 +00:00
|
|
|
{
|
2013-09-21 20:46:00 +00:00
|
|
|
Mutex::Lock _l(_lock);
|
2013-09-25 14:55:27 +00:00
|
|
|
|
|
|
|
std::map< uint64_t,_NetInfo >::iterator n(_nets.find(nwid));
|
|
|
|
if (n == _nets.end())
|
2013-09-27 20:03:13 +00:00
|
|
|
return;
|
2013-09-25 14:55:27 +00:00
|
|
|
std::map< MulticastGroup,std::list< Address > >::iterator p(n->second.proximity.find(mg));
|
|
|
|
if (p == n->second.proximity.end())
|
2013-09-27 20:03:13 +00:00
|
|
|
return;
|
2013-09-25 14:55:27 +00:00
|
|
|
|
|
|
|
for(std::list< Address >::iterator a(p->second.begin());a!=p->second.end();++a) {
|
2013-09-26 21:45:19 +00:00
|
|
|
if (!nextHopFunc(*a))
|
|
|
|
break;
|
2013-07-10 21:24:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-27 20:03:13 +00:00
|
|
|
/**
|
|
|
|
* Functor to add addresses to multicast frame propagation queues
|
|
|
|
*
|
|
|
|
* This function object checks the origin, bloom filter, and restriction
|
|
|
|
* prefix for each address and if all these pass it adds the address and
|
|
|
|
* increments the pointer pointed to by ptr. It stops (returns false) when
|
|
|
|
* *ptr reaches end. It's used in PacketDecoder and Switch with getNextHops()
|
|
|
|
* to compose multicast frame headers.
|
|
|
|
*/
|
|
|
|
class AddToPropagationQueue
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @param ptr Pointer to pointer to current position in queue
|
|
|
|
* @param end End of queue
|
|
|
|
* @param bloom Bloom filter field (must be 1024 bytes in length)
|
|
|
|
* @param bloomNonce Random nonce for bloom filter randomization
|
|
|
|
* @param origin Originating address
|
|
|
|
* @param prefixBits Number of bits in propagation restriction prefix
|
|
|
|
* @param prefix Propagation restrition prefix
|
|
|
|
*/
|
|
|
|
AddToPropagationQueue(unsigned char **ptr,unsigned char *end,unsigned char *bloom,uint16_t bloomNonce,const Address &origin,unsigned int prefixBits,unsigned int prefix)
|
|
|
|
throw() :
|
|
|
|
_origin(origin),
|
|
|
|
_bloomNonce((uint64_t)bloomNonce),
|
2013-10-01 21:19:24 +00:00
|
|
|
_prefixMask(0xffffffffffffffffULL >> (64 - prefixBits)),
|
|
|
|
_prefix((uint64_t)prefix & _prefixMask),
|
2013-09-27 20:03:13 +00:00
|
|
|
_ptr(ptr),
|
|
|
|
_end(end),
|
2013-10-01 21:19:24 +00:00
|
|
|
_bloom(bloom) {}
|
2013-09-27 20:03:13 +00:00
|
|
|
|
|
|
|
inline bool operator()(const Address &a)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
// Exclude original sender -- obviously they've already seen it
|
|
|
|
if (a == _origin)
|
|
|
|
return true;
|
|
|
|
|
2013-10-01 21:19:24 +00:00
|
|
|
// Exclude addresses not in this prefix domain
|
|
|
|
if ((a.toInt() & _prefixMask) != _prefix)
|
2013-09-27 20:03:13 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Exclude addresses remembered in bloom filter -- or else remember them
|
|
|
|
uint64_t aint = a.toInt() + _bloomNonce;
|
|
|
|
const unsigned int bit = (unsigned int)(aint ^ (aint >> 13) ^ (aint >> 26) ^ (aint >> 39)) & 0x1fff;
|
|
|
|
unsigned char *const bbyte = _bloom + (bit >> 3); // note: bloom filter size == 1024 is hard-coded here
|
2013-10-02 17:50:42 +00:00
|
|
|
const unsigned char bmask = 1 << (bit & 7);
|
2013-09-27 20:03:13 +00:00
|
|
|
if ((*bbyte & bmask))
|
|
|
|
return true;
|
|
|
|
else *bbyte |= bmask;
|
|
|
|
|
|
|
|
a.copyTo(*_ptr,ZT_ADDRESS_LENGTH);
|
|
|
|
return ((*_ptr += ZT_ADDRESS_LENGTH) != _end);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Address _origin;
|
|
|
|
const uint64_t _bloomNonce;
|
2013-10-01 21:19:24 +00:00
|
|
|
const uint64_t _prefixMask;
|
|
|
|
const uint64_t _prefix;
|
2013-09-27 20:03:13 +00:00
|
|
|
unsigned char **const _ptr;
|
|
|
|
unsigned char *const _end;
|
|
|
|
unsigned char *const _bloom;
|
|
|
|
};
|
|
|
|
|
2013-07-10 21:24:27 +00:00
|
|
|
private:
|
2013-09-22 17:35:40 +00:00
|
|
|
// Information about a subscription
|
|
|
|
struct _SubInfo
|
2013-09-21 20:46:00 +00:00
|
|
|
{
|
2013-09-22 17:35:40 +00:00
|
|
|
_SubInfo() :
|
|
|
|
lastLike(0),
|
|
|
|
proximitySlot() {}
|
|
|
|
|
|
|
|
// Time of last MULTICAST_LIKE for this group
|
|
|
|
uint64_t lastLike;
|
2013-09-21 20:46:00 +00:00
|
|
|
|
2013-09-22 17:35:40 +00:00
|
|
|
// Slot in corresponding list in _proximity
|
2013-09-21 20:46:00 +00:00
|
|
|
std::list< Address >::iterator proximitySlot;
|
|
|
|
};
|
2013-07-10 21:24:27 +00:00
|
|
|
|
2013-09-25 14:55:27 +00:00
|
|
|
// An address and multicast group tuple
|
2013-09-26 21:45:19 +00:00
|
|
|
typedef std::pair< Address,MulticastGroup > _Subscription;
|
2013-09-25 14:55:27 +00:00
|
|
|
|
|
|
|
// Multicast info for a given network
|
|
|
|
struct _NetInfo
|
|
|
|
{
|
2013-09-26 21:45:19 +00:00
|
|
|
_NetInfo()
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
memset(multicastHistory,0,sizeof(multicastHistory));
|
|
|
|
multicastHistoryPtr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ring buffer of most recently injected multicast packet GUIDs
|
|
|
|
uint64_t multicastHistory[ZT_MULTICAST_DEDUP_HISTORY_LENGTH];
|
|
|
|
unsigned int multicastHistoryPtr;
|
2013-09-25 14:55:27 +00:00
|
|
|
|
|
|
|
// Peer proximity ordering for peers subscribed to each group
|
|
|
|
std::map< MulticastGroup,std::list< Address > > proximity;
|
|
|
|
|
|
|
|
// Peer subscriptions to multicast groups
|
|
|
|
std::map< _Subscription,_SubInfo > subscriptions;
|
|
|
|
};
|
2013-07-13 19:17:21 +00:00
|
|
|
|
2013-09-25 14:55:27 +00:00
|
|
|
std::map< uint64_t,_NetInfo > _nets;
|
2013-09-21 20:46:00 +00:00
|
|
|
Mutex _lock;
|
2013-07-10 21:24:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|