2014-09-19 01:28:14 +00:00
|
|
|
/*
|
2019-08-23 16:23:39 +00:00
|
|
|
* Copyright (c)2019 ZeroTier, Inc.
|
2014-09-19 01:28:14 +00:00
|
|
|
*
|
2019-08-23 16:23:39 +00:00
|
|
|
* Use of this software is governed by the Business Source License included
|
|
|
|
* in the LICENSE.TXT file in the project's root directory.
|
2014-09-19 01:28:14 +00:00
|
|
|
*
|
2019-08-23 16:23:39 +00:00
|
|
|
* Change Date: 2023-01-01
|
2014-09-19 01:28:14 +00:00
|
|
|
*
|
2019-08-23 16:23:39 +00:00
|
|
|
* On the date above, in accordance with the Business Source License, use
|
|
|
|
* of this software will be governed by version 2.0 of the Apache License.
|
2014-09-19 01:28:14 +00:00
|
|
|
*/
|
2019-08-23 16:23:39 +00:00
|
|
|
/****/
|
2014-09-19 01:28:14 +00:00
|
|
|
|
2014-09-24 21:02:16 +00:00
|
|
|
#ifndef ZT_MULTICASTER_HPP
|
|
|
|
#define ZT_MULTICASTER_HPP
|
2014-09-19 01:28:14 +00:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "Constants.hpp"
|
2015-08-27 23:17:21 +00:00
|
|
|
#include "Hashtable.hpp"
|
2014-09-19 01:28:14 +00:00
|
|
|
#include "Address.hpp"
|
2014-09-24 20:45:58 +00:00
|
|
|
#include "MAC.hpp"
|
2014-09-19 01:28:14 +00:00
|
|
|
#include "MulticastGroup.hpp"
|
|
|
|
#include "Utils.hpp"
|
2014-09-23 17:26:30 +00:00
|
|
|
#include "Mutex.hpp"
|
2018-01-27 01:38:44 +00:00
|
|
|
#include "SharedPtr.hpp"
|
2019-09-09 22:49:17 +00:00
|
|
|
#include "Packet.hpp"
|
|
|
|
|
2019-09-10 23:20:28 +00:00
|
|
|
// Size in bits -- do not change as this is about as large as we can support
|
|
|
|
// This leaves room for up to 10000 MTU data (max supported MTU) and header
|
|
|
|
// information in a maximum supported size packet. Note that data compression
|
|
|
|
// will practically reduce this size in transit for sparse or saturated fields.
|
|
|
|
#define ZT_MULTICAST_BLOOM_FILTER_SIZE_BITS 50048
|
2014-09-19 01:28:14 +00:00
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
2014-09-25 22:08:29 +00:00
|
|
|
class RuntimeEnvironment;
|
2014-09-26 21:18:25 +00:00
|
|
|
class CertificateOfMembership;
|
2014-09-30 23:28:25 +00:00
|
|
|
class Packet;
|
2018-01-27 01:38:44 +00:00
|
|
|
class Network;
|
2014-09-19 01:28:14 +00:00
|
|
|
|
|
|
|
/**
|
2019-09-09 17:44:31 +00:00
|
|
|
* Multicast database and outbound multicast logic
|
2014-09-19 01:28:14 +00:00
|
|
|
*/
|
2018-01-27 01:00:37 +00:00
|
|
|
class Multicaster
|
2014-09-19 01:28:14 +00:00
|
|
|
{
|
2019-09-10 23:20:28 +00:00
|
|
|
private:
|
|
|
|
// Composite key of network ID and multicast group
|
|
|
|
struct _K
|
|
|
|
{
|
|
|
|
uint64_t nwid;
|
|
|
|
MulticastGroup mg;
|
|
|
|
|
|
|
|
ZT_ALWAYS_INLINE _K() : nwid(0),mg() {}
|
|
|
|
ZT_ALWAYS_INLINE _K(const uint64_t n,const MulticastGroup &g) : nwid(n),mg(g) {}
|
|
|
|
ZT_ALWAYS_INLINE bool operator==(const _K &k) const { return ((nwid == k.nwid)&&(mg == k.mg)); }
|
|
|
|
ZT_ALWAYS_INLINE bool operator!=(const _K &k) const { return ((nwid != k.nwid)||(mg != k.mg)); }
|
|
|
|
ZT_ALWAYS_INLINE unsigned long hashCode() const { return (mg.hashCode() ^ (unsigned long)(nwid ^ (nwid >> 32))); }
|
|
|
|
};
|
|
|
|
|
|
|
|
// Multicast group info
|
|
|
|
struct _G
|
|
|
|
{
|
|
|
|
ZT_ALWAYS_INLINE _G() : lastGather(0),members(16) {}
|
|
|
|
int64_t lastGather;
|
|
|
|
Hashtable< Address,int64_t > members;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Outbound multicast
|
|
|
|
struct _OM
|
|
|
|
{
|
|
|
|
uint64_t nwid;
|
|
|
|
MAC src;
|
|
|
|
MulticastGroup mg;
|
|
|
|
unsigned int etherType;
|
|
|
|
unsigned int dataSize;
|
|
|
|
unsigned int count;
|
|
|
|
unsigned int limit;
|
|
|
|
unsigned int bloomFilterMultiplier;
|
|
|
|
uint64_t bloomFilter[ZT_MULTICAST_BLOOM_FILTER_SIZE_BITS / 64];
|
|
|
|
uint8_t data[ZT_MAX_MTU];
|
|
|
|
Mutex lock;
|
|
|
|
};
|
|
|
|
|
2014-09-19 01:28:14 +00:00
|
|
|
public:
|
2014-10-01 21:05:25 +00:00
|
|
|
Multicaster(const RuntimeEnvironment *renv);
|
2014-09-24 21:02:16 +00:00
|
|
|
~Multicaster();
|
2014-09-19 01:28:14 +00:00
|
|
|
|
|
|
|
/**
|
2014-09-30 23:28:25 +00:00
|
|
|
* Add or update a member in a multicast group
|
2014-09-19 01:28:14 +00:00
|
|
|
*
|
2014-09-25 22:57:43 +00:00
|
|
|
* @param now Current time
|
2014-09-30 23:28:25 +00:00
|
|
|
* @param nwid Network ID
|
2014-09-19 01:28:14 +00:00
|
|
|
* @param mg Multicast group
|
2014-09-24 20:45:58 +00:00
|
|
|
* @param member New member address
|
2014-09-19 01:28:14 +00:00
|
|
|
*/
|
2019-09-09 22:49:17 +00:00
|
|
|
ZT_ALWAYS_INLINE void add(const int64_t now,const uint64_t nwid,const MulticastGroup &mg,const Address &member)
|
2019-09-09 17:44:31 +00:00
|
|
|
{
|
|
|
|
Mutex::Lock l(_groups_l);
|
2019-09-10 23:20:28 +00:00
|
|
|
_groups[_K(nwid,mg)].members.set(member,now);
|
2019-09-09 17:44:31 +00:00
|
|
|
}
|
2014-09-19 01:28:14 +00:00
|
|
|
|
2014-10-29 22:26:32 +00:00
|
|
|
/**
|
|
|
|
* Add multiple addresses from a binary array of 5-byte address fields
|
|
|
|
*
|
|
|
|
* It's up to the caller to check bounds on the array before calling this.
|
|
|
|
*
|
2017-03-28 00:03:17 +00:00
|
|
|
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
|
2014-10-29 22:26:32 +00:00
|
|
|
* @param now Current time
|
|
|
|
* @param nwid Network ID
|
|
|
|
* @param mg Multicast group
|
|
|
|
* @param addresses Raw binary addresses in big-endian format, as a series of 5-byte fields
|
|
|
|
* @param count Number of addresses
|
|
|
|
* @param totalKnown Total number of known addresses as reported by peer
|
|
|
|
*/
|
2019-09-09 22:49:17 +00:00
|
|
|
ZT_ALWAYS_INLINE void addMultiple(const int64_t now,const uint64_t nwid,const MulticastGroup &mg,const void *addresses,unsigned int count,const unsigned int totalKnown)
|
2019-09-09 17:44:31 +00:00
|
|
|
{
|
|
|
|
Mutex::Lock l(_groups_l);
|
|
|
|
const uint8_t *a = (const uint8_t *)addresses;
|
2019-09-10 23:20:28 +00:00
|
|
|
_G &g = _groups[_K(nwid,mg)];
|
2019-09-09 17:44:31 +00:00
|
|
|
while (count--) {
|
2019-09-10 23:20:28 +00:00
|
|
|
g.members.set(Address(a,ZT_ADDRESS_LENGTH),now);
|
2019-09-09 17:44:31 +00:00
|
|
|
a += ZT_ADDRESS_LENGTH;
|
|
|
|
}
|
|
|
|
}
|
2014-10-29 22:26:32 +00:00
|
|
|
|
2015-07-07 18:49:38 +00:00
|
|
|
/**
|
|
|
|
* Remove a multicast group member (if present)
|
|
|
|
*
|
|
|
|
* @param nwid Network ID
|
|
|
|
* @param mg Multicast group
|
|
|
|
* @param member Member to unsubscribe
|
|
|
|
*/
|
2019-09-09 22:49:17 +00:00
|
|
|
ZT_ALWAYS_INLINE void remove(const uint64_t nwid,const MulticastGroup &mg,const Address &member)
|
2019-09-09 17:44:31 +00:00
|
|
|
{
|
|
|
|
Mutex::Lock l(_groups_l);
|
2019-09-09 22:49:17 +00:00
|
|
|
const _K gk(nwid,mg);
|
2019-09-10 23:20:28 +00:00
|
|
|
_G *const g = _groups.get(gk);
|
|
|
|
if (g) {
|
|
|
|
g->members.erase(member);
|
|
|
|
if (g->members.empty())
|
2019-09-09 17:44:31 +00:00
|
|
|
_groups.erase(gk);
|
|
|
|
}
|
|
|
|
}
|
2015-07-07 18:49:38 +00:00
|
|
|
|
2014-09-30 23:28:25 +00:00
|
|
|
/**
|
2019-09-08 02:15:21 +00:00
|
|
|
* Iterate over members of a multicast group until function returns false
|
2014-09-30 23:28:25 +00:00
|
|
|
*
|
2019-09-08 02:15:21 +00:00
|
|
|
* Iteration order is in inverse order of most recent receipt of a LIKE
|
|
|
|
* for a given membership.
|
2014-10-05 17:34:25 +00:00
|
|
|
*
|
|
|
|
* @param nwid Network ID
|
|
|
|
* @param mg Multicast group
|
2019-09-08 02:15:21 +00:00
|
|
|
* @param func f(Address)
|
|
|
|
* @return Total number of known members (regardless of when function aborted)
|
2014-10-05 17:34:25 +00:00
|
|
|
*/
|
2019-09-08 02:15:21 +00:00
|
|
|
template<typename F>
|
2019-09-09 22:49:17 +00:00
|
|
|
ZT_ALWAYS_INLINE unsigned long eachMember(const uint64_t nwid,const MulticastGroup &mg,F func) const
|
2019-09-08 02:15:21 +00:00
|
|
|
{
|
|
|
|
std::vector< std::pair<int64_t,Address> > sortedByTime;
|
2019-09-10 23:20:28 +00:00
|
|
|
{
|
|
|
|
Mutex::Lock l(_groups_l);
|
|
|
|
const _K gk(nwid,mg);
|
|
|
|
const _G *const g = _groups.get(gk);
|
|
|
|
if (g) {
|
|
|
|
sortedByTime.reserve(g->members.size());
|
|
|
|
{
|
|
|
|
Hashtable< Address,int64_t >::Iterator mi(const_cast<_G *>(g)->members);
|
|
|
|
Address *mik = nullptr;
|
|
|
|
int64_t *miv = nullptr;
|
|
|
|
while (mi.next(mik,miv))
|
|
|
|
sortedByTime.push_back(std::pair<int64_t,Address>(*miv,*mik));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-09 22:49:17 +00:00
|
|
|
std::sort(sortedByTime.begin(),sortedByTime.end());
|
2019-09-11 22:52:18 +00:00
|
|
|
for(std::vector< std::pair<int64_t,Address> >::const_reverse_iterator i(sortedByTime.rbegin());i!=sortedByTime.rend();++i) {
|
2019-09-08 02:15:21 +00:00
|
|
|
if (!func(i->second))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return sortedByTime.size();
|
|
|
|
}
|
2014-09-30 23:28:25 +00:00
|
|
|
|
2014-09-24 20:45:58 +00:00
|
|
|
/**
|
|
|
|
* Send a multicast
|
|
|
|
*
|
2017-03-28 00:03:17 +00:00
|
|
|
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
|
2014-09-24 20:45:58 +00:00
|
|
|
* @param now Current time
|
2018-01-27 01:38:44 +00:00
|
|
|
* @param network Network
|
2014-09-24 20:45:58 +00:00
|
|
|
* @param mg Multicast group
|
2014-10-11 22:49:31 +00:00
|
|
|
* @param src Source Ethernet MAC address or NULL to skip in packet and compute from ZT address (non-bridged mode)
|
2014-09-24 20:45:58 +00:00
|
|
|
* @param etherType Ethernet frame type
|
2019-09-09 22:49:17 +00:00
|
|
|
* @param existingBloomMultiplier Existing bloom filter multiplier or 0 if none
|
|
|
|
* @param existingBloom Existing bloom filter or NULL if none
|
2014-09-24 20:45:58 +00:00
|
|
|
* @param data Packet data
|
|
|
|
* @param len Length of packet data
|
2019-09-10 23:20:28 +00:00
|
|
|
* @return Number of known recipients for multicast (including bridges and replicators)
|
2014-09-24 20:45:58 +00:00
|
|
|
*/
|
2019-09-10 23:20:28 +00:00
|
|
|
unsigned int send(
|
2017-03-28 00:03:17 +00:00
|
|
|
void *tPtr,
|
2017-10-02 22:52:57 +00:00
|
|
|
int64_t now,
|
2018-01-27 01:38:44 +00:00
|
|
|
const SharedPtr<Network> &network,
|
2014-09-26 21:18:25 +00:00
|
|
|
const MulticastGroup &mg,
|
|
|
|
const MAC &src,
|
|
|
|
unsigned int etherType,
|
2019-09-09 22:49:17 +00:00
|
|
|
const unsigned int existingBloomMultiplier,
|
|
|
|
const uint8_t existingBloom[ZT_MULTICAST_BLOOM_FILTER_SIZE_BITS / 8],
|
|
|
|
const void *const data,
|
2014-09-26 21:18:25 +00:00
|
|
|
unsigned int len);
|
2014-09-19 01:28:14 +00:00
|
|
|
|
|
|
|
/**
|
2019-09-09 17:44:31 +00:00
|
|
|
* Clean up database
|
2014-09-19 01:28:14 +00:00
|
|
|
*
|
2014-09-25 22:08:29 +00:00
|
|
|
* @param RR Runtime environment
|
2014-09-22 20:18:24 +00:00
|
|
|
* @param now Current time
|
2014-09-19 01:28:14 +00:00
|
|
|
*/
|
2017-10-02 22:52:57 +00:00
|
|
|
void clean(int64_t now);
|
2014-09-19 01:28:14 +00:00
|
|
|
|
|
|
|
private:
|
2018-01-27 01:00:37 +00:00
|
|
|
const RuntimeEnvironment *const RR;
|
2016-09-09 18:36:10 +00:00
|
|
|
|
2019-09-09 22:49:17 +00:00
|
|
|
_OM _txQueue[ZT_TX_QUEUE_SIZE];
|
2019-09-08 02:15:21 +00:00
|
|
|
unsigned int _txQueuePtr;
|
|
|
|
Mutex _txQueue_l;
|
|
|
|
|
2019-09-10 23:20:28 +00:00
|
|
|
Hashtable< _K,_G > _groups;
|
2019-09-08 02:15:21 +00:00
|
|
|
Mutex _groups_l;
|
2014-09-19 01:28:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|